@@ -51,18 +51,13 @@ def rotate_left(self) -> RedBlackTree:
51
51
"""
52
52
parent = self .parent
53
53
right = self .right
54
-
55
54
if right is None :
56
55
return self
57
-
58
56
self .right = right .left
59
-
60
57
if self .right :
61
58
self .right .parent = self
62
59
self .parent = right
63
-
64
60
right .left = self
65
-
66
61
if parent is not None :
67
62
if parent .left == self :
68
63
parent .left = right
@@ -80,16 +75,11 @@ def rotate_right(self) -> RedBlackTree:
80
75
return self
81
76
parent = self .parent
82
77
left = self .left
83
-
84
78
self .left = left .right
85
-
86
79
if self .left :
87
80
self .left .parent = self
88
-
89
81
self .parent = left
90
-
91
82
left .right = self
92
-
93
83
if parent is not None :
94
84
if parent .right is self :
95
85
parent .right = left
@@ -143,24 +133,20 @@ def _insert_repair(self) -> None:
143
133
self .parent .rotate_left ()
144
134
if self .left :
145
135
self .left ._insert_repair ()
146
-
147
136
elif self .is_left ():
148
137
if self .grandparent :
149
138
self .grandparent .rotate_right ()
150
139
self .parent .color = 0
151
-
152
140
if self .parent .right :
153
141
self .parent .right .color = 1
154
142
else :
155
143
if self .grandparent :
156
144
self .grandparent .rotate_left ()
157
145
self .parent .color = 0
158
-
159
146
if self .parent .left :
160
147
self .parent .left .color = 1
161
148
else :
162
149
self .parent .color = 0
163
-
164
150
if uncle and self .grandparent :
165
151
uncle .color = 0
166
152
self .grandparent .color = 1
@@ -186,7 +172,6 @@ def remove(self, label: int) -> RedBlackTree:
186
172
# is if both children are None leaves.
187
173
# We can just remove this node and call it a day.
188
174
if self .parent :
189
-
190
175
if self .is_left ():
191
176
self .parent .left = None
192
177
else :
@@ -232,7 +217,6 @@ def _remove_repair(self) -> None:
232
217
or self .grandparent is None
233
218
):
234
219
return
235
-
236
220
if color (self .sibling ) == 1 :
237
221
self .sibling .color = 0
238
222
self .parent .color = 1
@@ -266,7 +250,6 @@ def _remove_repair(self) -> None:
266
250
):
267
251
self .sibling .rotate_right ()
268
252
self .sibling .color = 0
269
-
270
253
if self .sibling .right :
271
254
self .sibling .right .color = 1
272
255
if (
@@ -277,7 +260,6 @@ def _remove_repair(self) -> None:
277
260
):
278
261
self .sibling .rotate_left ()
279
262
self .sibling .color = 0
280
-
281
263
if self .sibling .left :
282
264
self .sibling .left .color = 1
283
265
if (
@@ -314,21 +296,17 @@ def check_color_properties(self) -> bool:
314
296
"""
315
297
# I assume property 1 to hold because there is nothing that can
316
298
# make the color be anything other than 0 or 1.
317
-
318
299
# Property 2
319
300
if self .color :
320
301
# The root was red
321
302
print ("Property 2" )
322
303
return False
323
-
324
304
# Property 3 does not need to be checked, because None is assumed
325
305
# to be black and is all the leaves.
326
-
327
306
# Property 4
328
307
if not self .check_coloring ():
329
308
print ("Property 4" )
330
309
return False
331
-
332
310
# Property 5
333
311
if self .black_height () is None :
334
312
print ("Property 5" )
@@ -385,7 +363,6 @@ def search(self, label: int) -> RedBlackTree | None:
385
363
"""
386
364
if self .label == label :
387
365
return self
388
-
389
366
elif self .label is not None and label > self .label :
390
367
if self .right is None :
391
368
return None
@@ -474,7 +451,6 @@ def is_left(self) -> bool:
474
451
"""Returns true iff this node is the left child of its parent."""
475
452
if self .parent is None :
476
453
return False
477
-
478
454
return self .parent .left is self .parent .left is self
479
455
480
456
def is_right (self ) -> bool :
@@ -491,7 +467,6 @@ def __len__(self) -> int:
491
467
Return the number of nodes in this tree.
492
468
"""
493
469
ln = 1
494
-
495
470
if self .left :
496
471
ln += len (self .left )
497
472
if self .right :
@@ -538,7 +513,6 @@ def __eq__(self, other: object) -> bool:
538
513
"""Test if two trees are equal."""
539
514
if not isinstance (other , RedBlackTree ):
540
515
return NotImplemented
541
-
542
516
if self .label == other .label :
543
517
return self .left == other .left and self .right == other .right
544
518
else :
@@ -563,9 +537,7 @@ def test_rotations() -> bool:
563
537
"""Test that the rotate_left and rotate_right functions work."""
564
538
# Make a tree to test on
565
539
tree = RedBlackTree (0 )
566
-
567
540
tree .left = RedBlackTree (- 10 , parent = tree )
568
-
569
541
tree .right = RedBlackTree (10 , parent = tree )
570
542
tree .left .left = RedBlackTree (- 20 , parent = tree .left )
571
543
tree .left .right = RedBlackTree (- 5 , parent = tree .left )
@@ -580,10 +552,8 @@ def test_rotations() -> bool:
580
552
left_rot .left .left .right = RedBlackTree (- 5 , parent = left_rot .left .left )
581
553
left_rot .right = RedBlackTree (20 , parent = left_rot )
582
554
tree = tree .rotate_left ()
583
-
584
555
if tree != left_rot :
585
556
return False
586
-
587
557
tree = tree .rotate_right ()
588
558
tree = tree .rotate_right ()
589
559
# Make the left rotation
@@ -752,19 +722,12 @@ def main() -> None:
752
722
>>> pytests()
753
723
"""
754
724
print_results ("Rotating right and left" , test_rotations ())
755
-
756
725
print_results ("Inserting" , test_insert ())
757
-
758
726
print_results ("Searching" , test_insert_and_search ())
759
-
760
727
print_results ("Deleting" , test_insert_delete ())
761
-
762
728
print_results ("Floor and ceil" , test_floor_ceil ())
763
-
764
729
print_results ("Tree traversal" , test_tree_traversal ())
765
-
766
730
print_results ("Tree traversal" , test_tree_chaining ())
767
-
768
731
print ("Testing tree balancing..." )
769
732
print ("This should only be a few seconds." )
770
733
test_insertion_speed ()
0 commit comments