Skip to content

Commit 364f183

Browse files
committed
feat: issue #3 adds test for removing a right-side red child
1 parent 040defe commit 364f183

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

data_structures/binary_tree/red_black_tree.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,32 @@ def test_remove_last_element() -> bool:
660660
print(ans)
661661
return tree == ans
662662

663+
def test_remove_right_child() -> bool:
664+
"""Test the remove() method of the tree correctly balances, colors,
665+
and removes. The tested tree should be the same as in test_insert()
666+
to rule out any insertion errors.
667+
This instance should test removing a right-side red child.
668+
"""
669+
tree = RedBlackTree(0)
670+
tree.insert(8)
671+
tree.insert(-8)
672+
tree.insert(-5)
673+
tree.insert(-6)
674+
tree.insert(4)
675+
tree.insert(12)
676+
tree.insert(10)
677+
tree.insert(11)
678+
tree.remove(12)
679+
ans = RedBlackTree(0, 0)
680+
ans.left = RedBlackTree(-6, 0, ans)
681+
ans.left.left = RedBlackTree(-8, 1, ans)
682+
ans.left.right = RedBlackTree(-5, 1, ans)
683+
ans.right = RedBlackTree(8, 1, ans)
684+
ans.right.left = RedBlackTree(4, 0, ans.right)
685+
ans.right.right = RedBlackTree(11, 0, ans.right)
686+
ans.right.right.left = RedBlackTree(10, 1, ans.right.right)
687+
return tree == ans
688+
663689

664690
def test_insert_and_search() -> bool:
665691
"""Tests searching through the tree for values."""
@@ -788,6 +814,7 @@ def main() -> None:
788814
print_results("Rotating right and left", test_rotations())
789815
print_results("Inserting", test_insert())
790816
print_results("Removing", test_remove())
817+
print_results("Removing right child", test_remove_right_child())
791818
print_results("Emptying tree", test_remove_last_element())
792819
print_results("Searching", test_insert_and_search())
793820
print_results("Deleting", test_insert_delete())
@@ -796,7 +823,7 @@ def main() -> None:
796823
print_results("Tree traversal", test_tree_chaining())
797824
print("Testing tree balancing...")
798825
print("This should only be a few seconds.")
799-
#test_insertion_speed()
826+
test_insertion_speed()
800827
print("Done!")
801828
coverageList = list(dict.fromkeys(coverageList))
802829
coverageList.sort()

0 commit comments

Comments
 (0)