Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion data_structures/binary_tree/binary_search_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,22 @@
Traceback (most recent call last):
...
IndexError: Warning: Tree is empty! please use another.

Values are compared by equality, not identity, so equal ints that are
different objects (and equal floats) are found:

>>> tree = BinarySearchTree().insert(int("1000"), int("2000"))
>>> tree.search(int("2000")).value
2000
>>> BinarySearchTree().insert(1.5, 2.5).search(float("2.5")).value
2.5
"""

if self.empty():
raise IndexError("Warning: Tree is empty! please use another.")
node = self.root
# use lazy evaluation here to avoid NoneType Attribute error
while node is not None and node.value is not value:
while node is not None and node.value != value:
node = node.left if value < node.value else node.right
return node

Expand Down Expand Up @@ -292,9 +301,9 @@
predecessor = self.get_max(
node.left
) # Gets the max value of the left branch
self.remove(predecessor.value) # type: ignore[union-attr]

Check failure on line 304 in data_structures/binary_tree/binary_search_tree.py

View workflow job for this annotation

GitHub Actions / ty

ty (missing-union-attribute)

data_structures/binary_tree/binary_search_tree.py:304:25: missing-union-attribute: Attribute `value` is not defined on `None` in union `Node | None`
node.value = (
predecessor.value # type: ignore[union-attr]

Check failure on line 306 in data_structures/binary_tree/binary_search_tree.py

View workflow job for this annotation

GitHub Actions / ty

ty (missing-union-attribute)

data_structures/binary_tree/binary_search_tree.py:306:17: missing-union-attribute: Attribute `value` is not defined on `None` in union `Node | None`
) # Assigns the value to the node to delete and keep tree structure

def preorder_traverse(self, node: Node | None) -> Iterable:
Expand Down
Loading