Skip to content

Commit aba7053

Browse files
authored
Merge pull request #6 from ztick/write_3_additional_tests
Write 3 additional tests
2 parents 3e1a6cd + 364f183 commit aba7053

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

data_structures/binary_tree/red_black_tree.py

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,71 @@ 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+
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+
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+
624689

625690
def test_insert_and_search() -> bool:
626691
"""Tests searching through the tree for values."""
@@ -748,14 +813,17 @@ def main() -> None:
748813
"""
749814
print_results("Rotating right and left", test_rotations())
750815
print_results("Inserting", test_insert())
816+
print_results("Removing", test_remove())
817+
print_results("Removing right child", test_remove_right_child())
818+
print_results("Emptying tree", test_remove_last_element())
751819
print_results("Searching", test_insert_and_search())
752820
print_results("Deleting", test_insert_delete())
753821
print_results("Floor and ceil", test_floor_ceil())
754822
print_results("Tree traversal", test_tree_traversal())
755823
print_results("Tree traversal", test_tree_chaining())
756824
print("Testing tree balancing...")
757825
print("This should only be a few seconds.")
758-
#test_insertion_speed()
826+
test_insertion_speed()
759827
print("Done!")
760828
coverageList = list(dict.fromkeys(coverageList))
761829
coverageList.sort()

0 commit comments

Comments
 (0)