TheAlgorithms/Python · error · IndexError
Warning: Tree is empty! please use another.
Error message
Warning: Tree is empty! please use another.
What it means
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.
Source
Thrown at data_structures/binary_tree/binary_search_tree.py:226
{'30': (None, {'40': (None, 50)})}
>>> tree.search(40)
{'40': (None, 50)}
>>> tree.search(50)
50
>>> tree.search(5) is None # element not present
True
>>> tree.search(0) is None # element not present
True
>>> tree.search(-5) is None # element not present
True
>>> BinarySearchTree().search(10)
Traceback (most recent call last):
...
IndexError: Warning: Tree is empty! please use another.
"""
if self.empty():
raise IndexError("Warning: Tree is empty! please use another.")
else:
node = self.root
# use lazy evaluation here to avoid NoneType Attribute error
while node is not None and node.value is not value:
node = node.left if value < node.value else node.right
return node
def get_max(self, node: Node | None = None) -> Node | None:
"""
We go deep on the right branch
>>> BinarySearchTree().insert(10, 20, 30, 40, 50).get_max()
50
>>> BinarySearchTree().insert(-5, -1, 0.1, -0.3, -4.5).get_max()
{'0.1': (-0.3, None)}
>>> BinarySearchTree().insert(1, 78.3, 30, 74.0, 1).get_max()
{'78.3': ({'30': (1, 74.0)}, None)}
>>> BinarySearchTree().insert(1, 783, 30, 740, 1).get_max()View on GitHub (pinned to f5988cc097)
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.
Example fix
# before node = BinarySearchTree().search(10) # IndexError # after tree = BinarySearchTree() node = None if tree.empty() else tree.search(10)
Defensive patterns
Strategy: validation
Validate before calling
node = tree.search(value) if not tree.empty() else None
Try / catch
try:
node = tree.search(value)
except IndexError:
node = None # empty tree, nothing to find Prevention
- Check tree.empty() before search
- Complete all inserts before the first query
- Remember this library uses IndexError (not ValueError) for the empty-tree case
When it happens
Trigger: Calling BinarySearchTree().search(10) on a freshly constructed tree, or searching a tree after all nodes were removed (remove() can leave an empty tree).
Common situations: 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.
Related errors
- Value {value} not found
- The parameter s must not be empty.
- The parameter bwt_string must not be empty.
- We need some text to work with.
- The array is empty.
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/d7b4bc68fe788d1e.
Report an issue: GitHub.