-
Notifications
You must be signed in to change notification settings - Fork 48
/
rb_tree_tests.py
2210 lines (1951 loc) · 90.6 KB
/
rb_tree_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import unittest
import random
from datetime import datetime
from rb_tree import RedBlackTree, Node, BLACK, RED, NIL
NIL_LEAF = RedBlackTree.NIL_LEAF
class RbTreeTests(unittest.TestCase):
def test_find_node(self):
""" Use the tree we get from the test_build function
and test the find function on each node"""
rb_tree = RedBlackTree()
rb_tree.add(2)
node_2 = rb_tree.root
rb_tree.add(1)
node_1 = rb_tree.root.left
rb_tree.add(4)
node_4 = rb_tree.root.right
rb_tree.add(5)
node_5 = node_4.right
rb_tree.add(9)
node_9 = node_5.right
rb_tree.add(3)
node_3 = node_4.left
rb_tree.add(6)
node_6 = node_9.left
rb_tree.add(7)
node_7 = node_5.right
rb_tree.add(15)
node_15 = node_9.right
"""
___5B___
__2R__ 7R
1B 4B 6B 9B
3R 15R
"""
# valid cases
self.assertEqual(rb_tree.find_node(5), node_5)
self.assertEqual(rb_tree.find_node(2), node_2)
self.assertEqual(rb_tree.find_node(1), node_1)
self.assertEqual(rb_tree.find_node(4), node_4)
self.assertEqual(rb_tree.find_node(3), node_3)
self.assertEqual(rb_tree.find_node(7), node_7)
self.assertEqual(rb_tree.find_node(6), node_6)
self.assertEqual(rb_tree.find_node(9), node_9)
self.assertEqual(rb_tree.find_node(15), node_15)
# invalid cases
self.assertIsNone(rb_tree.find_node(-1))
self.assertIsNone(rb_tree.find_node(52454225))
self.assertIsNone(rb_tree.find_node(0))
self.assertIsNone(rb_tree.find_node(401))
self.assertIsNone(rb_tree.find_node(3.00001))
# ***************TEST INSERTIONS***************
def test_recoloring_only(self):
"""
Create a red-black tree, add a red node such that we only have to recolor
upwards twice
add 4, which recolors 2 and 8 to BLACK,
6 to RED
-10, 20 to BLACK
:return:
"""
tree = RedBlackTree()
root = Node(value=10, color=BLACK, parent=None)
# LEFT SUBTREE
node_m10 = Node(value=-10, color=RED, parent=root) #OK
node_6 = Node(value=6, color=BLACK, parent=node_m10) #OK
node_8 = Node(value=8, color=RED, parent=node_6, left=NIL_LEAF, right=NIL_LEAF) #OK
node_2 = Node(value=2, color=RED, parent=node_6, left=NIL_LEAF, right=NIL_LEAF) #OK
node_6.left = node_2 #OK
node_6.right = node_8 #OK
node_m20 = Node(value=-20, color=BLACK, parent=node_m10, left=NIL_LEAF, right=NIL_LEAF) #OK
node_m10.left = node_m20 #OK
node_m10.right = node_6 #OK
# RIGHT SUBTREE
node_20 = Node(value=20, color=RED, parent=root) #OK
node_15 = Node(value=15, color=BLACK, parent=node_20, left=NIL_LEAF, right=NIL_LEAF) #OK
node_25 = Node(25, color=BLACK, parent=node_20, left=NIL_LEAF, right=NIL_LEAF) #OK
node_20.left = node_15 #OK
node_20.right = node_25 #OK
root.left = node_m10 #OK
root.right = node_20 #OK
tree.root = root
tree.add(4)
"""
_____10B_____ _____10B_____
__-10R__ __20R__ __-10R__ __20R__
-20B 6B 15B 25B --FIRST RECOLOR--> -20B 6R 15B 25B
2R 8R 2B 8B
Add-->4R 4R
_____10B_____
__-10B__ __20B__
--SECOND RECOLOR--> -20B 6R 15B 25B
2B 8B
4R
"""
""" This should trigger two recolors.
2 and 8 should turn to black,
6 should turn to red,
-10 and 20 should turn to black
10 should try to turn to red, but since it's the root it can't be black"""
expected_values = [-20, -10, 2, 4, 6, 8, 10, 15, 20, 25]
values = list(tree)
self.assertEqual(values, expected_values)
self.assertEqual(node_2.color, BLACK)
self.assertEqual(node_8.color, BLACK)
self.assertEqual(node_6.color, RED)
self.assertEqual(node_m10.color, BLACK)
self.assertEqual(node_20.color, BLACK)
def test_recoloring_two(self):
rb_tree = RedBlackTree()
root = Node(value=10, color=BLACK, parent=None, left=NIL_LEAF, right=NIL_LEAF)
# left subtree
node_m10 = Node(value=-10, color=RED, parent=root, left=NIL_LEAF, right=NIL_LEAF)
node_m20 = Node(value=-20, color=BLACK, parent=node_m10, left=NIL_LEAF, right=NIL_LEAF)
node_6 = Node(value=6, color=BLACK, parent=node_m10, left=NIL_LEAF, right=NIL_LEAF)
node_m10.left = node_m20
node_m10.right = node_6
# right subtree
node_20 = Node(value=20, color=RED, parent=root, left=NIL_LEAF, right=NIL_LEAF)
node_15 = Node(value=15, color=BLACK, parent=node_20, left=NIL_LEAF, right=NIL_LEAF)
node_25 = Node(value=25, color=BLACK, parent=node_20, left=NIL_LEAF, right=NIL_LEAF)
node_20.left = node_15
node_20.right = node_25
node_12 = Node(value=12, color=RED, parent=node_15, left=NIL_LEAF, right=NIL_LEAF)
node_17 = Node(value=17, color=RED, parent=node_15, left=NIL_LEAF, right=NIL_LEAF)
node_15.left = node_12
node_15.right = node_17
root.left = node_m10
root.right = node_20
rb_tree.root = root
rb_tree.add(19)
"""
_____10B_____ _____10B_____
__-10R__ __20R__ __-10R__ __20R__
-20B 6B 15B 25B FIRST RECOLOR--> -20B 6B 15R 25B
12R 17R 12B 17B
Add-->19R 19R
SECOND RECOLOR
_____10B_____
__-10B__ __20B__
-20B 6B 15R 25B
12B 17B
19R
"""
expected_values = [-20, -10, 6, 10, 12, 15, 17, 19, 20, 25]
values = list(rb_tree)
self.assertEqual(values, expected_values)
node_19 = node_17.right
self.assertEqual(node_19.value, 19)
self.assertEqual(node_19.color, RED)
self.assertEqual(node_19.parent, node_17)
self.assertEqual(node_17.color, BLACK)
self.assertEqual(node_12.color, BLACK)
self.assertEqual(node_15.color, RED)
self.assertEqual(node_20.color, BLACK)
self.assertEqual(node_25.color, BLACK)
self.assertEqual(node_m10.color, BLACK)
self.assertEqual(rb_tree.root.color, BLACK)
def test_right_rotation(self):
tree = RedBlackTree()
root = Node(value=10, color=BLACK, parent=None)
# LEFT SUBTREE
node_m10 = Node(value=-10, color=BLACK, parent=root, left=NIL_LEAF, right=NIL_LEAF)
node_7 = Node(value=7, color=RED, parent=node_m10, left=NIL_LEAF, right=NIL_LEAF)
node_m10.right = node_7
# RIGHT SUBTREE
node_20 = Node(value=20, color=BLACK, parent=root, left=NIL_LEAF, right=NIL_LEAF)
node_15 = Node(value=15, color=RED, parent=node_20, left=NIL_LEAF, right=NIL_LEAF)
node_20.left = node_15
root.left = node_m10
root.right = node_20
tree.root = root
tree.add(13)
"""
____10B____ ____10B____
-10B 20B --(LL -> R) RIGHT ROTATE--> -10B 15B
7R 15R 7R 13R 20R
Add -> 13R
"""
expected_values = [-10, 7, 10, 13, 15, 20]
values = list(tree)
self.assertEqual(values, expected_values)
node_20 = node_15.right
node_13 = node_15.left
self.assertEqual(node_15.color, BLACK) # this should be the parent of both now
self.assertEqual(node_15.parent.value, 10)
self.assertEqual(node_20.value, 20)
self.assertEqual(node_20.color, RED)
self.assertEqual(node_20.parent.value, 15)
self.assertEqual(node_20.left, NIL_LEAF)
self.assertEqual(node_20.right, NIL_LEAF)
self.assertEqual(node_13.value, 13)
self.assertEqual(node_13.color, RED)
self.assertEqual(node_13.parent.value, 15)
self.assertEqual(node_13.left, NIL_LEAF)
self.assertEqual(node_13.right, NIL_LEAF)
def test_left_rotation_no_sibling(self):
rb_tree = RedBlackTree()
root = Node(value=10, color=BLACK, parent=None, left=NIL_LEAF, right=NIL_LEAF)
# LEFT SUBTREE
node_7 = Node(value=7, color=BLACK, parent=root, left=NIL_LEAF, right=NIL_LEAF)
node_8 = Node(value=8, color=RED, parent=node_7, left=NIL_LEAF, right=NIL_LEAF)
node_7.right = node_8
# RIGHT SUBTREE
rightest = Node(value=20, color=BLACK, parent=root, left=NIL_LEAF, right=NIL_LEAF)
root.left = node_7
root.right = rightest
rb_tree.root = root
rb_tree.add(9)
"""
--> 10B 10B
ORIGINAL --> 7B 20B --LEFT ROTATION--> 8B 20B
--> 8R 7R 9R
--> 9R
We add 9, which is the right child of 8 and causes a red-red relationship
this calls for a left rotation, so 7 becomes left child of 8 and 9 the right child of 8
8 is black, 7 and 9 are red
"""
expected_values = [7, 8, 9, 10, 20]
values = list(rb_tree)
self.assertEqual(values, expected_values)
node_9 = node_8.right
self.assertEqual(node_9.value, 9)
self.assertEqual(node_9.color, RED)
self.assertEqual(node_9.parent.value, 8)
self.assertEqual(node_9.left, NIL_LEAF)
self.assertEqual(node_9.right, NIL_LEAF)
self.assertEqual(node_8.parent.value, 10)
self.assertEqual(node_8.color, BLACK)
self.assertEqual(node_8.left.value, 7)
self.assertEqual(node_8.right.value, 9)
self.assertEqual(node_7.color, RED)
self.assertEqual(node_7.parent.value, 8)
self.assertEqual(node_7.left, NIL_LEAF)
self.assertEqual(node_7.right, NIL_LEAF)
def test_right_rotation_no_sibling_left_subtree(self):
rb_tree = RedBlackTree()
root = Node(value=10, color=BLACK, parent=None, left=NIL_LEAF, right=NIL_LEAF)
# LEFT SUBTREE
node_m10 = Node(value=-10, color=BLACK, parent=root, left=NIL_LEAF, right=NIL_LEAF)
node_m11 = Node(value=-11, color=RED, parent=node_m10, left=NIL_LEAF, right=NIL_LEAF)
node_m10.left = node_m11
# RIGHT SUBTREE
node_20 = Node(value=20, color=BLACK, parent=root, left=NIL_LEAF, right=NIL_LEAF)
node_15 = Node(value=15, color=RED, parent=node_20, left=NIL_LEAF, right=NIL_LEAF)
node_20.left = node_15
root.left = node_m10
root.right = node_20
rb_tree.root = root
rb_tree.add(-12)
"""
____10____ ____10____
__-10B__ 20B (LL->R) Right rotate--> -11B 20B
-11R 15R -12R -10R 15R
Add--> 12R
red-red relationship with -11 -12, so we do a right rotation where -12 becomes the left child of -11,
-10 becomes the right child of -11
-11's parent is root, -11 is black, -10,-12 are RED
"""
expected_values = [-12, -11, -10, 10, 15, 20]
values = list(rb_tree)
self.assertEqual(values, expected_values)
node_m12 = node_m11.left
self.assertEqual(rb_tree.root.left.value, -11)
self.assertEqual(node_m12.value, -12)
self.assertEqual(node_m12.color, RED)
self.assertEqual(node_m12.parent.value, -11)
self.assertEqual(node_m12.left, NIL_LEAF)
self.assertEqual(node_m12.right, NIL_LEAF)
self.assertEqual(node_m11.color, BLACK)
self.assertEqual(node_m11.parent, rb_tree.root)
self.assertEqual(node_m11.left.value, -12)
self.assertEqual(node_m11.right.value, -10)
self.assertEqual(node_m10.color, RED)
self.assertEqual(node_m10.parent.value, -11)
self.assertEqual(node_m10.left, NIL_LEAF)
self.assertEqual(node_m10.right, NIL_LEAF)
def test_left_right_rotation_no_sibling(self):
rb_tree = RedBlackTree()
root = Node(value=10, color=BLACK, parent=None, left=NIL_LEAF, right=NIL_LEAF)
# LEFT PART
node_m10 = Node(value=-10, color=BLACK, parent=root, left=NIL_LEAF, right=NIL_LEAF)
node_7 = Node(value=7, color=RED, parent=node_m10, left=NIL_LEAF, right=NIL_LEAF)
node_m10.right = node_7
# RIGHT PART
node_20 = Node(value=20, color=BLACK, parent=root, left=NIL_LEAF, right=NIL_LEAF)
node_15 = Node(value=15, color=RED, parent=node_20, left=NIL_LEAF, right=NIL_LEAF)
node_20.left = node_15
root.left=node_m10
root.right=node_20
rb_tree.root = root
rb_tree.add(17)
"""
___10___ ____10____
-10B 20B -10B 20B
7R 15R --(LR=>RL) Left Rotate (no recolor) --> 7R 17R
Add--> 17R 15R
____10____
Right Rotate (with recolor) --> -10B 17B
7R 15R 20R
15-17 should do a left rotation so 17 is now the parent of 15.
Then, a right rotation should be done so 17 is the parent of 20(15's prev parent)
Also, a recoloring should be done such that 17 is now black and his children are red
"""
expected_values = [-10, 7, 10, 15, 17, 20]
values = list(rb_tree)
self.assertEqual(values, expected_values)
node_15 = node_15
node_20 = node_20
node_17 = node_15.parent
self.assertEqual(rb_tree.root.right, node_17)
self.assertEqual(node_17.value, 17)
self.assertEqual(node_17.color, BLACK)
self.assertEqual(node_17.parent, rb_tree.root)
self.assertEqual(node_17.left.value, 15)
self.assertEqual(node_17.right.value, 20)
self.assertEqual(node_20.parent.value, 17)
self.assertEqual(node_20.color, RED)
self.assertEqual(node_20.left, NIL_LEAF)
self.assertEqual(node_20.right, NIL_LEAF)
self.assertEqual(node_15.parent.value, 17)
self.assertEqual(node_15.color, RED)
self.assertEqual(node_15.left, NIL_LEAF)
self.assertEqual(node_15.right, NIL_LEAF)
def test_right_left_rotation_no_sibling(self):
rb_tree = RedBlackTree()
root = Node(value=10, color=BLACK, parent=None, left=NIL_LEAF, right=NIL_LEAF)
# LEFT PART
nodem10 = Node(value=-10, color=BLACK, parent=root, left=NIL_LEAF, right=NIL_LEAF)
node_7 = Node(value=7, color=RED, parent=nodem10, left=NIL_LEAF, right=NIL_LEAF)
nodem10.right = node_7
# RIGHT PART
node_20 = Node(value=20, color=BLACK, parent=root, left=NIL_LEAF, right=NIL_LEAF)
node_15 = Node(value=15, color=RED, parent=node_20, left=NIL_LEAF, right=NIL_LEAF)
node_20.left = node_15
root.left = nodem10
root.right = node_20
rb_tree.root = root
rb_tree.add(2)
"""
___10___ ___10___
-10B 20B -10B 20B
7R 15R --- (LR=>RL) Right Rotation (no recolor)--> 2R 15R
Add--> 2R 7R
_____10_____
Left Rotation (with recolor) --> __2B__ __20B__
-10R 7R 15R
2 goes as left to 7, but both are red so we do a RIGHT-LEFT rotation
First a right rotation should happen, so that 2 becomes the parent of 7 [2 right-> 7]
Second a left rotation should happen, so that 2 becomes the parent of -10 and 7
2 is black, -10 and 7 are now red. 2's parent is the root - 10
"""
expected_values = [-10, 2, 7, 10, 15, 20]
values = list(rb_tree)
self.assertEqual(values, expected_values)
node_2 = node_7.parent
self.assertEqual(node_2.parent.value, 10)
self.assertEqual(node_2.color, BLACK)
self.assertEqual(node_2.left.value, -10)
self.assertEqual(node_2.right.value, 7)
self.assertEqual(node_7.color, RED)
self.assertEqual(node_7.parent.value, 2)
self.assertEqual(node_7.left, NIL_LEAF)
self.assertEqual(node_7.right, NIL_LEAF)
self.assertEqual(nodem10.color, RED)
self.assertEqual(nodem10.parent.value, 2)
self.assertEqual(nodem10.left, NIL_LEAF)
self.assertEqual(nodem10.right, NIL_LEAF)
def test_recolor_lr(self):
rb_tree = RedBlackTree()
root = Node(value=10, color=BLACK, parent=None)
# RIGHT SUBTREE
node_m10 = Node(value=-10, color=RED, parent=root, left=NIL_LEAF, right=NIL_LEAF)
node_m20 = Node(value=-20, color=BLACK, parent=node_m10, left=NIL_LEAF, right=NIL_LEAF)
node_m10.left = node_m20
node_6 = Node(value=6, color=BLACK, parent=node_m10, left=NIL_LEAF, right=NIL_LEAF)
node_m10.right = node_6
node_1 = Node(value=1, color=RED, parent=node_6, left=NIL_LEAF, right=NIL_LEAF)
node_6.left = node_1
node_9 = Node(value=9, color=RED, parent=node_6, left=NIL_LEAF, right=NIL_LEAF)
node_6.right = node_9
# LEFT SUBTREE
node_20 = Node(value=20, color=BLACK, parent=root, left=NIL_LEAF, right=NIL_LEAF)
node_15 = Node(value=15, color=RED, parent=node_20, left=NIL_LEAF, right=NIL_LEAF)
node_20.left = node_15
node_30 = Node(value=30, color=RED, parent=node_20, left=NIL_LEAF, right=NIL_LEAF)
node_20.right = node_30
root.left = node_m10
root.right = node_20
rb_tree.root = root
rb_tree.add(4)
"""
_________10B_________ _________10B_________
___-10R___ __20B__ ___-10R___ __20B__
-20B __6B__ 15R 30R ---RECOLORS TO --> -20B __6R__ 15R 30R
1R 9R 1B 9B
4R 4R
_________10B_________
___6R___ __20B__ ______6B__
LEFT ROTATOES TO --> __-10B__ 9B 15R 30R ---RIGHT ROTATES TO--> __-10R__ _10R_
-20B 1B -20B 1B 9B __20B__
4R 4R 15R 30R
Adding 4, we recolor once, then we check upwards and see that there's a black sibling.
We see that our direction is RightLeft (RL) and do a Left Rotation followed by a Right Rotation
-10 becomes 6's left child and 1 becomes -10's right child
"""
expected_values = [-20, -10, 1, 4, 6, 9, 10, 15, 20, 30]
values = list(rb_tree)
self.assertEqual(values, expected_values)
node_10 = rb_tree.root.right
node_4 = node_1.right
self.assertEqual(rb_tree.root.value, 6)
self.assertEqual(rb_tree.root.parent, None)
self.assertEqual(rb_tree.root.left.value, -10)
self.assertEqual(rb_tree.root.right.value, 10)
self.assertEqual(node_m10.parent.value, 6)
self.assertEqual(node_m10.color, RED)
self.assertEqual(node_m10.left.value, -20)
self.assertEqual(node_m10.right.value, 1)
self.assertEqual(node_10.color, RED)
self.assertEqual(node_10.parent.value, 6)
self.assertEqual(node_10.left.value, 9)
self.assertEqual(node_10.right.value, 20)
self.assertEqual(node_m20.color, BLACK)
self.assertEqual(node_m20.parent.value, -10)
self.assertEqual(node_m20.left, NIL_LEAF)
self.assertEqual(node_m20.right, NIL_LEAF)
self.assertEqual(node_1.color, BLACK)
self.assertEqual(node_1.parent.value, -10)
self.assertEqual(node_1.left, NIL_LEAF)
self.assertEqual(node_1.right.color, RED)
self.assertEqual(node_4.value, 4)
self.assertEqual(node_4.color, RED)
def test_functional_test_build_tree(self):
rb_tree = RedBlackTree()
rb_tree.add(2)
self.assertEqual(rb_tree.root.value, 2)
self.assertEqual(rb_tree.root.color, BLACK)
node_2 = rb_tree.root
""" 2 """
expected_values = [2]
values = list(rb_tree)
self.assertEqual(values, expected_values)
rb_tree.add(1)
"""
2B
1R
"""
expected_values = [1, 2]
values = list(rb_tree)
self.assertEqual(values, expected_values)
node_1 = rb_tree.root.left
self.assertEqual(node_1.value, 1)
self.assertEqual(node_1.color, RED)
rb_tree.add(4)
"""
2B
1R 4R
"""
expected_values = [1, 2, 4]
values = list(rb_tree)
self.assertEqual(values, expected_values)
node_4 = rb_tree.root.right
self.assertEqual(node_4.value, 4)
self.assertEqual(node_4.color, RED)
self.assertEqual(node_4.left, NIL_LEAF)
self.assertEqual(node_4.right, NIL_LEAF)
rb_tree.add(5)
"""
2B 2B
1R 4R ---CAUSES RECOLOR--> 1B 4B
5R 5R
"""
expected_values = [1, 2, 4, 5]
values = list(rb_tree)
self.assertEqual(values, expected_values)
node_5 = node_4.right
self.assertEqual(node_5.value, 5)
self.assertEqual(node_4.color, BLACK)
self.assertEqual(node_1.color, BLACK)
self.assertEqual(node_5.color, RED)
rb_tree.add(9)
"""
2B __2B__
1B 4B ---CAUSES LEFT ROTATION--> 1B 5B
5R 4R 9R
9R
"""
expected_values = [1, 2, 4, 5, 9]
values = list(rb_tree)
self.assertEqual(values, expected_values)
node_9 = node_5.right
self.assertEqual(node_9.value, 9)
self.assertEqual(node_9.color, RED)
self.assertEqual(node_9.left, NIL_LEAF)
self.assertEqual(node_9.right, NIL_LEAF)
self.assertEqual(node_4.color, RED)
self.assertEqual(node_4.left, NIL_LEAF)
self.assertEqual(node_4.right, NIL_LEAF)
self.assertEqual(node_4.parent.value, 5)
self.assertEqual(node_5.parent.value, 2)
self.assertEqual(node_5.color, BLACK)
self.assertEqual(node_5.left.value, 4)
self.assertEqual(node_5.right.value, 9)
rb_tree.add(3)
"""
__2B__ __2B__
1B 5B ---CAUSES RECOLOR--> 1B 5R
4R 9R 4B 9B
3R 3R
"""
expected_values = [1, 2, 3, 4, 5, 9]
values = list(rb_tree)
self.assertEqual(values, expected_values)
node_3 = node_4.left
self.assertEqual(node_3.value, 3)
self.assertEqual(node_3.color, RED)
self.assertEqual(node_3.left, NIL_LEAF)
self.assertEqual(node_3.right, NIL_LEAF)
self.assertEqual(node_3.parent.value, 4)
self.assertEqual(node_4.color, BLACK)
self.assertEqual(node_4.right, NIL_LEAF)
self.assertEqual(node_4.parent.value, 5)
self.assertEqual(node_9.color, BLACK)
self.assertEqual(node_9.parent.value, 5)
self.assertEqual(node_5.color, RED)
self.assertEqual(node_5.left.value, 4)
self.assertEqual(node_5.right.value, 9)
rb_tree.add(6)
"""
Nothing special
__2B__
1B 5R___
4B _9B
3R 6R
"""
expected_values = [1, 2, 3, 4, 5, 6, 9]
values = list(rb_tree)
self.assertEqual(values, expected_values)
node_6 = node_9.left
self.assertEqual(node_6.value, 6)
self.assertEqual(node_6.color, RED)
self.assertEqual(node_6.parent.value, 9)
self.assertEqual(node_6.left, NIL_LEAF)
self.assertEqual(node_6.right, NIL_LEAF)
rb_tree.add(7)
"""
__2B__ __2B__
1B ___5R___ ---LEFT ROTATION TO--> 1B ___5R___
4B _9B_ 4B 9B
3R 6R 3R 7R
7R 6B
RIGHT ROTATION (RECOLOR) TO
__2B__
1B ___5R___
4B 7B
3R 6R 9R
"""
expected_values = [1, 2, 3, 4, 5, 6, 7, 9]
values = list(rb_tree)
self.assertEqual(values, expected_values)
node_7 = node_5.right
self.assertEqual(node_7.value, 7)
self.assertEqual(node_7.color, BLACK)
self.assertEqual(node_7.left.value, 6)
self.assertEqual(node_7.right.value, 9)
self.assertEqual(node_7.parent.value, 5)
self.assertEqual(node_5.color, RED)
self.assertEqual(node_5.right.value, 7)
self.assertEqual(node_6.color, RED)
self.assertEqual(node_6.left, NIL_LEAF)
self.assertEqual(node_6.right, NIL_LEAF)
self.assertEqual(node_6.parent.value, 7)
self.assertEqual(node_9.color, RED)
self.assertEqual(node_9.left, NIL_LEAF)
self.assertEqual(node_9.right, NIL_LEAF)
self.assertEqual(node_9.parent.value, 7)
rb_tree.add(15)
"""
__2B__ __2B__
1B ___5R___ 1B ___5R___
4B 7B ---RECOLORS TO--> 4B 7R
3R 6R 9R 3R 6B 9B
15R 15R
Red-red relationship on 5R-7R. 7R's sibling is BLACK, so we need to rotate.
7 is the right child of 5, 5 is the right child of 2, so we have RR => L: Left rotation with recolor
What we get is:
___5B___
__2R__ 7R
1B 4B 6B 9B
3R 15R
"""
expected_values = [1, 2, 3, 4, 5, 6, 7, 9, 15]
values = list(rb_tree)
self.assertEqual(values, expected_values)
node_15 = node_9.right
self.assertEqual(node_15.color, RED)
self.assertEqual(node_15.parent.value, 9)
self.assertEqual(node_15.left, NIL_LEAF)
self.assertEqual(node_15.right, NIL_LEAF)
self.assertEqual(node_9.color, BLACK)
self.assertEqual(node_9.left, NIL_LEAF)
self.assertEqual(node_9.right.value, 15)
self.assertEqual(node_9.parent.value, 7)
self.assertEqual(node_6.color, BLACK)
self.assertEqual(node_7.color, RED)
self.assertEqual(node_7.left.value, 6)
self.assertEqual(node_7.right.value, 9)
self.assertEqual(rb_tree.root.value, 5)
self.assertIsNone(node_5.parent)
self.assertEqual(node_5.right.value, 7)
self.assertEqual(node_5.left.value, 2)
self.assertEqual(node_2.color, RED)
self.assertEqual(node_2.parent.value, 5)
self.assertEqual(node_2.left.value, 1)
self.assertEqual(node_2.right.value, 4)
self.assertEqual(node_4.parent.value, 2)
self.assertEqual(node_4.color, BLACK)
self.assertEqual(node_4.left.value, 3)
self.assertEqual(node_4.right, NIL_LEAF)
def test_right_left_rotation_after_recolor(self):
rb_tree = RedBlackTree()
root = Node(value=10, color=BLACK, parent=None, left=NIL_LEAF, right=NIL_LEAF)
node_10 = root
# left subtree
node_5 = Node(value=5, color=BLACK, parent=root, left=NIL_LEAF, right=NIL_LEAF)
# right subtree
node_20 = Node(value=20, color=RED, parent=root, left=NIL_LEAF, right=NIL_LEAF)
node_15 = Node(value=15, color=BLACK, parent=node_20, left=NIL_LEAF, right=NIL_LEAF)
node_25 = Node(value=25, color=BLACK, parent=node_20, left=NIL_LEAF, right=NIL_LEAF)
node_20.left = node_15
node_20.right = node_25
node_12 = Node(value=12, color=RED, parent=node_15, left=NIL_LEAF, right=NIL_LEAF)
node_17 = Node(value=17, color=RED, parent=node_15, left=NIL_LEAF, right=NIL_LEAF)
node_15.left = node_12
node_15.right = node_17
root.left = node_5
root.right = node_20
rb_tree.root = root
rb_tree.add(19)
"""
____10B____ ____10B____
5B __20R__ 5B __20R__
__15B__ 25B --RECOLORS TO--> __15R__ 25B
12R 17R 12B 17B
Add-->19R 19R
____10B____
LR=>RL: Right rotation to 5B ___15R___
12B __20R__
17B 25B
19R
______15B_____
Left rotation to 10R __20R__
5B 12B __17B__ 25B
19R
"""
expected_values = [5, 10, 12, 15, 17, 19, 20, 25]
values = list(rb_tree)
self.assertEqual(values, expected_values)
node_19 = node_17.right
self.assertEqual(node_19.value, 19)
self.assertEqual(node_19.color, RED)
self.assertEqual(node_19.left, NIL_LEAF)
self.assertEqual(node_19.right, NIL_LEAF)
self.assertEqual(node_19.parent, node_17)
self.assertEqual(node_17.parent, node_20)
self.assertEqual(node_17.color, BLACK)
self.assertEqual(node_17.left, NIL_LEAF)
self.assertEqual(node_17.right, node_19)
self.assertEqual(node_20.parent, node_15)
self.assertEqual(node_20.color, RED)
self.assertEqual(node_20.left, node_17)
self.assertEqual(node_20.right, node_25)
self.assertEqual(rb_tree.root, node_15)
self.assertIsNone(node_15.parent)
self.assertEqual(node_15.left, node_10)
self.assertEqual(node_15.right, node_20)
self.assertEqual(node_15.color, BLACK)
self.assertEqual(node_10.parent, node_15)
self.assertEqual(node_10.color, RED)
self.assertEqual(node_10.right, node_12)
self.assertEqual(node_10.left, node_5)
self.assertEqual(node_12.color, BLACK)
self.assertEqual(node_12.parent, node_10)
self.assertEqual(node_12.left, NIL_LEAF)
self.assertEqual(node_12.right, NIL_LEAF)
def test_right_rotation_after_recolor(self):
rb_tree = RedBlackTree()
root = Node(value=10, color=BLACK, parent=None, left=NIL_LEAF, right=NIL_LEAF)
node_10 = root
# left subtree
node_m10 = Node(value=-10, color=RED, parent=root, left=NIL_LEAF, right=NIL_LEAF)
node_6 = Node(value=6, color=BLACK, parent=node_m10, left=NIL_LEAF, right=NIL_LEAF)
node_m20 = Node(value=-20, color=BLACK, parent=node_m10, left=NIL_LEAF, right=NIL_LEAF)
node_m10.left = node_m20
node_m10.right = node_6
node_m21 = Node(value=-21, color=RED, parent=node_m20, left=NIL_LEAF, right=NIL_LEAF)
node_m19 = Node(value=-19, color=RED, parent=node_m20, left=NIL_LEAF, right=NIL_LEAF)
node_m20.left = node_m21
node_m20.right = node_m19
# right subtree
node_20 = Node(value=20, color=BLACK, parent=root, left=NIL_LEAF, right=NIL_LEAF)
node_15 = Node(value=15, color=RED, parent=node_20, left=NIL_LEAF, right=NIL_LEAF)
node_25 = Node(value=25, color=RED, parent=node_20, left=NIL_LEAF, right=NIL_LEAF)
node_20.left = node_15
node_20.right = node_25
root.left = node_m10
root.right = node_20
rb_tree.root = root
rb_tree.add(-22)
"""
_____10_____ _____10_____
/ \ / \
-10R 20B -10R 20B
/ \ / \ / \ / \
-20B 6B 15R 25R --RECOLOR TO--> -20R 6B 15R 25R
/ \ / \
-21R -19R -21B -19B
/ /
Add-> -22R 22R
____-10B_____
/ \
-20R __10R__
/ \ / \
Right rotation to-> -21B -19B 6B __20B__
/ / \
-22R 15R 25R
"""
expected_values = [-22, -21, -20, -19, -10, 6, 10, 15, 20, 25]
values = list(rb_tree)
self.assertEqual(values, expected_values)
self.assertEqual(rb_tree.root, node_m10)
self.assertEqual(node_m10.parent, None)
self.assertEqual(node_m10.left, node_m20)
self.assertEqual(node_m10.right, node_10)
self.assertEqual(node_m10.color, BLACK)
self.assertEqual(node_10.parent, node_m10)
self.assertEqual(node_10.color, RED)
self.assertEqual(node_10.left, node_6)
self.assertEqual(node_10.right, node_20)
self.assertEqual(node_m20.parent, node_m10)
self.assertEqual(node_m20.color, RED)
self.assertEqual(node_m20.left, node_m21)
self.assertEqual(node_m20.right, node_m19)
self.assertEqual(node_m21.color, BLACK)
self.assertEqual(node_m21.left.value, -22)
self.assertEqual(node_6.parent, node_10)
self.assertEqual(node_6.color, BLACK)
self.assertEqual(node_6.left, NIL_LEAF)
self.assertEqual(node_6.right, NIL_LEAF)
self.assertEqual(node_m19.color, BLACK)
self.assertEqual(node_m19.parent, node_m20)
self.assertEqual(node_m19.left, NIL_LEAF)
self.assertEqual(node_m19.right, NIL_LEAF)
# ***************TEST INSERTIONS***************
# ***************TEST DELETIONS***************
def test_deletion_root(self):
rb_tree = RedBlackTree()
root = Node(value=5, color=BLACK, parent=None, left=NIL_LEAF, right=NIL_LEAF)
left_child = Node(value=3, color=RED, parent=root, left=NIL_LEAF, right=NIL_LEAF)
right_child = Node(value=8, color=RED, parent=root, left=NIL_LEAF, right=NIL_LEAF)
root.left = left_child
root.right = right_child
"""
REMOVE--> __5__ __8B__
/ \ --Result--> /
3R 8R 3R
"""
rb_tree.root = root
rb_tree.remove(5)
expected_values = [3, 8]
values = list(rb_tree)
self.assertEqual(values, expected_values)
node_8 = rb_tree.root
self.assertEqual(node_8.value, 8)
self.assertEqual(node_8.color, BLACK)
self.assertEqual(node_8.parent, None)
self.assertEqual(node_8.left.value, 3)
self.assertEqual(node_8.right, NIL_LEAF)
node_3 = node_8.left
self.assertEqual(node_3.color, RED)
self.assertEqual(node_3.parent, node_8)
self.assertEqual(node_3.left, NIL_LEAF)
self.assertEqual(node_3.right, NIL_LEAF)
def test_deletion_root_2_nodes(self):
rb_tree = RedBlackTree()
root = Node(value=5, color=BLACK, parent=None, left=NIL_LEAF, right=NIL_LEAF)
right_child = Node(value=8, color=RED, parent=root, left=NIL_LEAF, right=NIL_LEAF)
root.right = right_child
rb_tree.root = root
rb_tree.remove(5)
"""
__5B__ <-- REMOVE __8B__
\ Should become--^
8R
"""
expected_values = [8]
values = list(rb_tree)
self.assertEqual(values, expected_values)
root = rb_tree.root
self.assertEqual(root.value, 8)
self.assertEqual(root.parent, None)
self.assertEqual(root.color, BLACK)
self.assertEqual(root.left, NIL_LEAF)
self.assertEqual(root.right, NIL_LEAF)
def test_delete_single_child(self):
rb_tree = RedBlackTree()
root = Node(value=5, color=BLACK, parent=None, left=NIL_LEAF, right=NIL_LEAF)
left_child = Node(value=1, color=RED, parent=root, left=NIL_LEAF, right=NIL_LEAF)
right_child = Node(value=6, color=RED, parent=root, left=NIL_LEAF, right=NIL_LEAF)
root.left = left_child
root.right = right_child
rb_tree.root = root
rb_tree.remove(6)
"""
5 5B
/ \ should become /
1R 6R 1R
"""
expected_values = [1, 5]
values = list(rb_tree)
self.assertEqual(values, expected_values)
self.assertEqual(root.right, NIL_LEAF)
self.assertEqual(root.value, 5)
self.assertEqual(root.color, BLACK)
self.assertEqual(root.parent, None)
self.assertEqual(root.left.value, 1)
node_1 = root.left
self.assertEqual(node_1.left, NIL_LEAF)
self.assertEqual(node_1.right, NIL_LEAF)
def test_delete_single_deep_child(self):
rb_tree = RedBlackTree()
root = Node(value=20, color=BLACK, parent=None, left=NIL_LEAF, right=NIL_LEAF)
# left subtree
node_10 = Node(value=10, color=BLACK, parent=None, left=NIL_LEAF, right=NIL_LEAF)
node_5 = Node(value=5, color=RED, parent=node_10, left=NIL_LEAF, right=NIL_LEAF)
node_15 = Node(value=15, color=RED, parent=node_10, left=NIL_LEAF, right=NIL_LEAF)