@@ -621,6 +621,31 @@ def test_insert() -> bool:
621
621
ans .right .right .right = RedBlackTree (12 , 1 , ans .right .right )
622
622
return tree == ans
623
623
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
+
624
649
625
650
def test_insert_and_search () -> bool :
626
651
"""Tests searching through the tree for values."""
@@ -748,6 +773,7 @@ def main() -> None:
748
773
"""
749
774
print_results ("Rotating right and left" , test_rotations ())
750
775
print_results ("Inserting" , test_insert ())
776
+ print_results ("Removing" , test_remove ())
751
777
print_results ("Searching" , test_insert_and_search ())
752
778
print_results ("Deleting" , test_insert_delete ())
753
779
print_results ("Floor and ceil" , test_floor_ceil ())
0 commit comments