{"record":{"id":"d7b4bc68fe788d1e","repo":"TheAlgorithms/Python","slug":"warning-tree-is-empty-please-use-another","errorCode":null,"errorMessage":"Warning: Tree is empty! please use another.","messagePattern":"Warning: Tree is empty! please use another\\.","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"data_structures/binary_tree/binary_search_tree.py","lineNumber":226,"sourceCode":"        {'30': (None, {'40': (None, 50)})}\n        >>> tree.search(40)\n        {'40': (None, 50)}\n        >>> tree.search(50)\n        50\n        >>> tree.search(5) is None  # element not present\n        True\n        >>> tree.search(0) is None  # element not present\n        True\n        >>> tree.search(-5) is None  # element not present\n        True\n        >>> BinarySearchTree().search(10)\n        Traceback (most recent call last):\n            ...\n        IndexError: Warning: Tree is empty! please use another.\n        \"\"\"\n\n        if self.empty():\n            raise IndexError(\"Warning: Tree is empty! please use another.\")\n        else:\n            node = self.root\n            # use lazy evaluation here to avoid NoneType Attribute error\n            while node is not None and node.value is not value:\n                node = node.left if value < node.value else node.right\n            return node\n\n    def get_max(self, node: Node | None = None) -> Node | None:\n        \"\"\"\n        We go deep on the right branch\n\n        >>> BinarySearchTree().insert(10, 20, 30, 40, 50).get_max()\n        50\n        >>> BinarySearchTree().insert(-5, -1, 0.1, -0.3, -4.5).get_max()\n        {'0.1': (-0.3, None)}\n        >>> BinarySearchTree().insert(1, 78.3, 30, 74.0, 1).get_max()\n        {'78.3': ({'30': (1, 74.0)}, None)}\n        >>> BinarySearchTree().insert(1, 783, 30, 740, 1).get_max()","sourceCodeStart":208,"sourceCodeEnd":244,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/data_structures/binary_tree/binary_search_tree.py#L208-L244","documentation":"Raised by BinarySearchTree.search() in data_structures/binary_tree/binary_search_tree.py when the tree is empty (self.empty() is True, i.e. no root). Searching requires at least one node to start the descent, so the method raises IndexError with a 'Warning:' message instead of returning None. Note the unusual choice of IndexError for this condition.","triggerScenarios":"Calling BinarySearchTree().search(10) on a freshly constructed tree, or searching a tree after all nodes were removed (remove() can leave an empty tree).","commonSituations":"Searching before any inserts (data not yet loaded), removing all elements and then querying, or test code that instantiates a tree and immediately probes it.","solutions":["Check emptiness before searching: if tree.empty(): return None / handle.","Ensure inserts complete before any search runs (ordering in setup code).","Catch IndexError if you must, but prefer the explicit empty() guard — the exception type is unconventional."],"exampleFix":"# before\nnode = BinarySearchTree().search(10)  # IndexError\n\n# after\ntree = BinarySearchTree()\nnode = None if tree.empty() else tree.search(10)","handlingStrategy":"validation","validationCode":"node = tree.search(value) if not tree.empty() else None","typeGuard":null,"tryCatchPattern":"try:\n    node = tree.search(value)\nexcept IndexError:\n    node = None  # empty tree, nothing to find","preventionTips":["Check tree.empty() before search","Complete all inserts before the first query","Remember this library uses IndexError (not ValueError) for the empty-tree case"],"tags":["value-validation","binary-tree","bst","search","empty-input"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}