@@ -621,6 +621,71 @@ 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
+
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
+
624
689
625
690
def test_insert_and_search () -> bool :
626
691
"""Tests searching through the tree for values."""
@@ -748,14 +813,17 @@ def main() -> None:
748
813
"""
749
814
print_results ("Rotating right and left" , test_rotations ())
750
815
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 ())
751
819
print_results ("Searching" , test_insert_and_search ())
752
820
print_results ("Deleting" , test_insert_delete ())
753
821
print_results ("Floor and ceil" , test_floor_ceil ())
754
822
print_results ("Tree traversal" , test_tree_traversal ())
755
823
print_results ("Tree traversal" , test_tree_chaining ())
756
824
print ("Testing tree balancing..." )
757
825
print ("This should only be a few seconds." )
758
- # test_insertion_speed()
826
+ test_insertion_speed ()
759
827
print ("Done!" )
760
828
coverageList = list (dict .fromkeys (coverageList ))
761
829
coverageList .sort ()
0 commit comments