@@ -116,38 +116,55 @@ def insert(self, label: int) -> RedBlackTree:
116116
117117 def _insert_repair (self ) -> None :
118118 """Repair the coloring from inserting into a tree."""
119+ coverageList .append (1 )
119120 if self .parent is None :
121+ coverageList .append (2 )
120122 # This node is the root, so it just needs to be black
121123 self .color = 0
122124 elif color (self .parent ) == 0 :
125+ coverageList .append (3 )
123126 # If the parent is black, then it just needs to be red
124127 self .color = 1
125128 else :
129+ coverageList .append (4 )
126130 uncle = self .parent .sibling
127131 if color (uncle ) == 0 :
132+ coverageList .append (5 )
128133 if self .is_left () and self .parent .is_right ():
134+ coverageList .append (6 )
129135 self .parent .rotate_right ()
130136 if self .right :
137+ coverageList .append (7 )
131138 self .right ._insert_repair ()
132139 elif self .is_right () and self .parent .is_left ():
140+ coverageList .append (8 )
133141 self .parent .rotate_left ()
134142 if self .left :
143+ coverageList .append (9 )
135144 self .left ._insert_repair ()
136145 elif self .is_left ():
146+ coverageList .append (10 )
137147 if self .grandparent :
148+ coverageList .append (11 )
138149 self .grandparent .rotate_right ()
139150 self .parent .color = 0
140151 if self .parent .right :
152+ coverageList .append (12 )
141153 self .parent .right .color = 1
142154 else :
155+ coverageList .append (13 )
143156 if self .grandparent :
157+ coverageList .append (14 )
144158 self .grandparent .rotate_left ()
145159 self .parent .color = 0
146160 if self .parent .left :
161+ coverageList .append (15 )
147162 self .parent .left .color = 1
148163 else :
164+ coverageList .append (16 )
149165 self .parent .color = 0
150166 if uncle and self .grandparent :
167+ coverageList .append (17 )
151168 uncle .color = 0
152169 self .grandparent .color = 1
153170 self .grandparent ._insert_repair ()
@@ -719,6 +736,9 @@ def pytests() -> None:
719736
720737
721738def main () -> None :
739+ global coverageList
740+ coverageList = []
741+ coverageList .append (0 ) #this ID is excluded from the percentage calculation at the end main()
722742 """
723743 >>> pytests()
724744 """
@@ -731,8 +751,12 @@ def main() -> None:
731751 print_results ("Tree traversal" , test_tree_chaining ())
732752 print ("Testing tree balancing..." )
733753 print ("This should only be a few seconds." )
734- test_insertion_speed ()
754+ # test_insertion_speed()
735755 print ("Done!" )
756+ coverageList = list (dict .fromkeys (coverageList ))
757+ coverageList .sort ()
758+ print ("Covered IDs:" , coverageList )
759+ print ("Coverage percentage: " , (len (coverageList )- 1 )/ 17 )
736760
737761
738762if __name__ == "__main__" :
0 commit comments