Skip to content

Commit 040defe

Browse files
committed
feat: issue #3 adds a unit test for emptying the tree, which fails when it shouldn't
1 parent b50bf90 commit 040defe

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

data_structures/binary_tree/red_black_tree.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,20 @@ def test_remove() -> bool:
646646
ans.right.right.right = RedBlackTree(12, 1, ans.right.right)
647647
return tree == ans
648648

649+
def test_remove_last_element() -> bool:
650+
"""Test the remove() method of the tree correctly removes
651+
the final element in a tree. Afterwards, the tree should be
652+
empty.
653+
"""
654+
tree = RedBlackTree(0)
655+
tree.insert(8)
656+
tree.remove(0)
657+
tree.remove(8)
658+
print(tree)
659+
ans = RedBlackTree(None)
660+
print(ans)
661+
return tree == ans
662+
649663

650664
def test_insert_and_search() -> bool:
651665
"""Tests searching through the tree for values."""
@@ -774,6 +788,7 @@ def main() -> None:
774788
print_results("Rotating right and left", test_rotations())
775789
print_results("Inserting", test_insert())
776790
print_results("Removing", test_remove())
791+
print_results("Emptying tree", test_remove_last_element())
777792
print_results("Searching", test_insert_and_search())
778793
print_results("Deleting", test_insert_delete())
779794
print_results("Floor and ceil", test_floor_ceil())

0 commit comments

Comments
 (0)