Skip to content

Commit b50bf90

Browse files
committed
feat: issue #3 adds test that takes 4 previously untested branches
1 parent e9b5705 commit b50bf90

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

data_structures/binary_tree/red_black_tree.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,31 @@ def test_insert() -> bool:
621621
ans.right.right.right = RedBlackTree(12, 1, ans.right.right)
622622
return tree == ans
623623

624+
def test_remove() -> bool:
625+
"""Test the remove() method of the tree correctly balances, colors,
626+
and removes. The tested tree should be the same as in test_insert()
627+
to rule out any insertion errors.
628+
"""
629+
tree = RedBlackTree(0)
630+
tree.insert(8)
631+
tree.insert(-8)
632+
tree.insert(-5)
633+
tree.insert(-6)
634+
tree.insert(4)
635+
tree.insert(12)
636+
tree.insert(10)
637+
tree.insert(11)
638+
tree.remove(-6)
639+
ans = RedBlackTree(0, 0)
640+
ans.left = RedBlackTree(-8, 0, ans)
641+
ans.left.right = RedBlackTree(-5, 1, ans)
642+
ans.right = RedBlackTree(8, 1, ans)
643+
ans.right.left = RedBlackTree(4, 0, ans.right)
644+
ans.right.right = RedBlackTree(11, 0, ans.right)
645+
ans.right.right.left = RedBlackTree(10, 1, ans.right.right)
646+
ans.right.right.right = RedBlackTree(12, 1, ans.right.right)
647+
return tree == ans
648+
624649

625650
def test_insert_and_search() -> bool:
626651
"""Tests searching through the tree for values."""
@@ -748,6 +773,7 @@ def main() -> None:
748773
"""
749774
print_results("Rotating right and left", test_rotations())
750775
print_results("Inserting", test_insert())
776+
print_results("Removing", test_remove())
751777
print_results("Searching", test_insert_and_search())
752778
print_results("Deleting", test_insert_delete())
753779
print_results("Floor and ceil", test_floor_ceil())

0 commit comments

Comments
 (0)