-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathred-black-tree.html
More file actions
2455 lines (1993 loc) · 115 KB
/
red-black-tree.html
File metadata and controls
2455 lines (1993 loc) · 115 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Red Black Tree</title>
<!-- Google tag (gtag.js) -->
<script async="" src="https://www.googletagmanager.com/gtag/js?id=G-HCERBDV76D"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'G-HCERBDV76D');
</script>
<meta content="B0487B46A104E90209E8A3BEA24ECA0E" name="msvalidate.01"/>
<meta content="f044b3a12c7918f1" name="yandex-verification"/>
<!--end-->
<meta content="learn about data types, variables, lists, tuples, dictionaries,if else,DSA,loops,user-defined functions, oop, threading and scripting." name="description"/>
<meta content="learnPython" name="author"/>
<meta content="Learn Python for free,learn python for beginners,Core Python,Web frameworks,Multiprocess architecture,Serverside templating language,python tutorials,python4" name="keywords"/>
<meta content="website" property="og:type"/>
<meta content="US-CA" name="”geo.region”"/>
<meta content="353 Jane Stanford Way, Stanford, CA 94305, United States" name="”geo.placename”"/>
<meta content="37.430089898615456;-122.17332683124829" name="”geo.position”"/>
<meta content="37.430089898615456, -122.17332683124829" name="”ICBM”"/>
<link href="https://pythonread.github.io/?m=1" rel="alternate"/>
<link href="/favicon.png" rel="icon"/>
<meta charset="utf-8"/>
<meta content="IE=edge" http-equiv="X-UA-Compatible"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<link href="/style1.css" media="all" rel="stylesheet" type="text/css"/>
<link href="/style2.css" media="all" rel="stylesheet" type="text/css"/>
<link href="/style0.css" media="all" rel="stylesheet" type="text/css"/>
<!--<![endif]-->
</head>
<body>
<div class="nav-wrapper">
<div class="container">
<nav>
<div class="logo wave">
<a href="/" id="logo">
Python Tutorial
</a>
</div>
<div class="nav-toggle-icon" id="nav-toggle-icon" onclick="mobileMenu()">
<div class="material-hamburger">
<span>
</span>
<span>
</span>
<span>
</span>
</div>
</div>
<div class="menu-wrapper" id="menu-wrapper">
<div class="nav-indicator">
</div>
<ul class="menus">
<li>
<a class="wave" href="/">
Home
</a>
</li>
<li>
<a class="wave" href="/projects.html">
Projects
</a>
</li>
<li>
<a class="wave" href="/free-course.html" target="_blank">
Free Course
</a>
</li>
<li>
<a class="wave" href="/dsa.html">
DSA
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div class="contents contents--neg" style="margin-top: 60px">
<div class="container">
<div class="row">
<div class="col-sm-12">
<div class="d-flex">
<div class="left-bar d-none d-lg-block">
<div class="card-alt mb-10x">
<h3>Page Index</h3>
<div class="list">
<ul>
<li><a href="/dsa.html#data-structure-1" title="Data Structures (I)">Data Structures
(I)</a></li>
<li><a href="/dsa.html#data-structure-2" title="Data Structures (II)">Data Structures
(II)</a></li>
<li><a href="/dsa.html#tree-1" title="Tree based DSA (I)">Tree based DSA (I)</a></li>
<li><a href="/dsa.html#tree-2" title="Tree based DSA (II)">Tree based DSA (II)</a></li>
<li><a href="/dsa.html#graph" title="Graph Data Structures and Algorithm">Graph based
DSA</a></li>
<li><a href="/dsa.html#sorting-searching" title="Sorting and Searching">Sorting and
Searching</a></li>
<li><a href="/dsa.html#greedy-algorithm" title="Greedy Algorithms">Greedy Algorithms</a>
</li>
<li><a href="/dsa.html#dynamic-programming" title="Dynamic Programming">Dynamic
Programming</a></li>
<li><a href="/dsa.html#other-algorithms" title="Other Algorithms">Other Algorithms</a>
</li>
</ul>
</div>
</div>
</div>
<div class="right-bar">
<!--first part end-------------------------------------------->
<iframe width="560" height="315" src="https://www.youtube.com/embed/cP_-HxHkS_8" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<div class="editor-contents">
<h1>Red-Black Tree</h1>
<p class="editor-contents__short-description">In this tutorial, you will learn what a red-black tree is. Also, you will find working examples of various operations performed on a red-black tree in C, C++, Java and Python.</p>
<div id="node-1520" class="node node-algorithm clearfix" about="/dsa/red-black-tree" typeof="sioc:Item foaf:Document">
<span property="dc:title" content="Red-Black Tree" class="rdf-meta element-hidden"></span>
<div class="content">
<p id="definition">Red-Black tree is a self-balancing binary search tree in which each node contains an extra bit for denoting the color of the node, either red or black.</p>
<p>A red-black tree satisfies the following properties:</p>
<ol><li><strong>Red/Black Property:</strong> Every node is colored, either red or black.</li>
<li><strong>Root Property:</strong> The root is black.</li>
<li><strong>Leaf Property:</strong> Every leaf (NIL) is black.</li>
<li><strong>Red Property:</strong> If a red node has children then, the children are always black.</li>
<li><strong>Depth Property:</strong> For each node, any simple path from this node to any of its descendant leaf has the same black-depth (the number of black nodes).</li>
</ol><p>An example of a red-black tree is:</p>
<figure><img alt="red-black tree" src="//cdn.programiz.com/sites/tutorial2program/files/red-black-tree_0.png" title="red-black tree" width="500" height="358"><figcaption>Red Black Tree</figcaption></figure><p>Each node has the following attributes:</p>
<ul><li>color</li>
<li>key</li>
<li>leftChild</li>
<li>rightChild</li>
<li>parent (except root node)</li>
</ul><hr><p><strong>How the red-black tree maintains the property of self-balancing?</strong></p>
<p>The red-black color is meant for balancing the tree.</p>
<p>The limitations put on the node colors ensure that any simple path from the root to a leaf is not more than twice as long as any other such path. It helps in maintaining the self-balancing property of the red-black tree.</p>
<hr><h2>Operations on a Red-Black Tree</h2>
<p>Various operations that can be performed on a red-black tree are:</p>
<h3 id="rotation">Rotating the subtrees in a Red-Black Tree</h3>
<p>In rotation operation, the positions of the nodes of a subtree are interchanged.</p>
<p>Rotation operation is used for maintaining the properties of a red-black tree when they are violated by other operations such as insertion and deletion.</p>
<p>There are two types of rotations:</p>
<hr><h3>Left Rotate</h3>
<p>In left-rotation, the arrangement of the nodes on the right is transformed into the arrangements on the left node.</p>
<p><strong>Algorithm</strong></p>
<ol><li>Let the initial tree be:
<figure><img alt="left-rotate" src="//cdn.programiz.com/sites/tutorial2program/files/leftrotate-1_0.png" title="Initial Tree" width="150" height="177"><figcaption>Initial tree</figcaption></figure></li>
<li>If <var>y</var> has a left subtree, assign <var>x</var> as the parent of the left subtree of <var>y</var>.
<figure><img alt="left-rotate" src="//cdn.programiz.com/sites/tutorial2program/files/leftrotate-2_0.png" title="Assign x as the parent of the left subtree of y" width="150" height="136"><figcaption>Assign x as the parent of the left subtree of y</figcaption></figure></li>
<li>If the parent of <var>x</var> is <code>NULL</code>, make <var>y</var> as the root of the tree.</li>
<li>Else if <var>x</var> is the left child of <var>p</var>, make <var>y</var> as the left child of <var>p</var>.</li>
<li>Else assign <var>y</var> as the right child of <var>p</var>.
<figure><img alt="left-rotate" src="//cdn.programiz.com/sites/tutorial2program/files/leftrotate-3_0.png" title="change the parent of x to that of y" width="150" height="117"><figcaption>Change the parent of x to that of y</figcaption></figure></li>
<li>Make <var>y</var> as the parent of <var>x</var>.
<figure><img alt="left-rotate" src="//cdn.programiz.com/sites/tutorial2program/files/leftrotate-4_0.png" title="Assign y as the parent of x." width="150" height="167"><figcaption>Assign y as the parent of x.</figcaption></figure></li>
</ol><hr><h3>Right Rotate</h3>
<p>In right-rotation, the arrangement of the nodes on the left is transformed into the arrangements on the right node.</p>
<ol><li>Let the initial tree be:
<figure><img alt="right-rotate" src="//cdn.programiz.com/sites/tutorial2program/files/rightrotate-1_0.png" title="initial tree" width="150" height="168"><figcaption>Initial Tree</figcaption></figure></li>
<li>If <var>x</var> has a right subtree, assign y as the parent of the right subtree of <var>x</var>.
<figure><img alt="right-rotate" src="//cdn.programiz.com/sites/tutorial2program/files/rightrotate-2_0.png" title="assign y as the parent of the right subtree of x." width="150" height="140"><figcaption>Assign y as the parent of the right subtree of x</figcaption></figure></li>
<li>If the parent of <var>y</var> is <code>NULL</code>, make <var>x</var> as the root of the tree.</li>
<li>Else if <var>y</var> is the right child of its parent <var>p</var>, make <var>x</var> as the right child of <var>p</var>.</li>
<li>Else assign <var>x</var> as the left child of <var>p</var>.
<figure><img alt="right-rotate" src="//cdn.programiz.com/sites/tutorial2program/files/rightrotate-3_0.png" title="Assign the parent of y as the parent of x." width="150" height="121"><figcaption>Assign the parent of y as the parent of x</figcaption></figure></li>
<li>Make <var>x</var> as the parent of <var>y</var>.
<figure><img alt="right-rotate" src="//cdn.programiz.com/sites/tutorial2program/files/rightrotate-4_0.png" title="assign x as the parent of y" width="150" height="168"><figcaption>Assign x as the parent of y</figcaption></figure></li>
</ol><hr><h3>Left-Right and Right-Left Rotate</h3>
<p>In left-right rotation, the arrangements are first shifted to the left and then to the right.</p>
<ol><li>Do left rotation on x-y.
<figure><img alt="left-right rotate" src="//cdn.programiz.com/sites/tutorial2program/files/leftright-rotate-1_0.png" title="left rotate x-y" width="375" height="205"><figcaption>Left rotate x-y</figcaption></figure></li>
<li>Do right rotation on y-z.
<figure><img alt="left-right rotate" src="//cdn.programiz.com/sites/tutorial2program/files/leftright-rotate-2_0.png" title="right rotate z-y" width="375" height="194"><figcaption>Right rotate z-y</figcaption></figure></li>
</ol><p></p><div class="clearfix"></div><p>In right-left rotation, the arrangements are first shifted to the right and then to the left.</p>
<ol><li>Do right rotation on x-y.
<figure><img alt="right-left rotate" src="//cdn.programiz.com/sites/tutorial2program/files/rightleft-rotate-1_0.png" title="right rotate x-y" width="375" height="200"><figcaption>Right rotate x-y</figcaption></figure></li>
<li>Do left rotation on z-y.
<figure><img alt="right-left rotate" src="//cdn.programiz.com/sites/tutorial2program/files/rightleft-rotate-2_0.png" title="left rotate z-y" width="375" height="196"><figcaption>Left rotate z-y</figcaption></figure></li>
</ol><hr><h3 id="insertion">Inserting an element into a Red-Black Tree</h3>
<p>While inserting a new node, the new node is always inserted as a RED node. After insertion of a new node, if the tree is violating the properties of the red-black tree then, we do the following operations.</p>
<ol><li>Recolor</li>
<li>Rotation</li>
</ol><hr><h3>Algorithm to insert a node</h3>
<p>Following steps are followed for inserting a new element into a red-black tree:</p>
<ol><li>Let y be the leaf (ie. <code>NIL</code>) and x be the root of the tree.</li>
<li>Check if the tree is empty (ie. whether <var>x</var> is <code>NIL</code>). If yes, insert <var>newNode</var> as a root node and color it black.</li>
<li>Else, repeat steps following steps until leaf (<code>NIL</code>) is reached.
<ol type="a"><li>Compare <var>newKey</var> with <var>rootKey</var>.</li>
<li>If <var>newKey</var> is greater than rootKey, traverse through the right subtree.</li>
<li>Else traverse through the left subtree.</li>
</ol></li>
<li>Assign the parent of the leaf as a parent of <var>newNode</var>.</li>
<li>If <var>leafKey</var> is greater than <var>newKey</var>, make <var>newNode</var> as <var>rightChild</var>.</li>
<li>Else, make <var>newNode</var> as <var>leftChild</var>.</li>
<li>Assign <code>NULL</code> to the left and <var>rightChild</var> of <var>newNode</var>.</li>
<li>Assign RED color to <var>newNode</var>.</li>
<li>Call InsertFix-algorithm to maintain the property of red-black tree if violated.</li>
</ol><hr><p><strong>Why newly inserted nodes are always red in a red-black tree?</strong></p>
<p>This is because inserting a red node does not violate the depth property of a red-black tree.</p>
<p>If you attach a red node to a red node, then the rule is violated but it is easier to fix this problem than the problem introduced by violating the depth property.</p>
<hr><h3>Algorithm to maintain red-black property after insertion</h3>
<p>This algorithm is used for maintaining the property of a red-black tree if the insertion of a newNode violates this property.</p>
<ol><li>Do the following while the parent of <var>newNode</var> <var>p</var> is RED.</li>
<li>If <var>p</var> is the left child of <var>grandParent</var> <var>gP</var> of <var>z</var>, do the following.<br><strong>Case-I:</strong>
<ol type="a"><li>If the color of the right child of <var>gP</var> of <var>z</var> is RED, set the color of both the children of <var>gP</var> as BLACK and the color of <var>gP</var> as RED.</li>
<li>Assign <var>gP</var> to <var>newNode</var>.<br><strong>Case-II:</strong></li>
<li>Else if <var>newNode</var> is the right child of <var>p</var> then, assign <var>p</var> to <var>newNode</var>.</li>
<li>Left-Rotate <var>newNode</var>.<br><strong>Case-III:</strong></li>
<li>Set color of <var>p</var> as BLACK and color of <var>gP</var> as RED.</li>
<li>Right-Rotate <var>gP</var>.</li>
</ol></li>
<li>Else, do the following.
<ol type="a"><li>If the color of the left child of <var>gP</var> of <var>z</var> is RED, set the color of both the children of <var>gP</var> as BLACK and the color of <var>gP</var> as RED.</li>
<li>Assign <var>gP</var> to <var>newNode</var>.</li>
<li>Else if newNode is the left child of <var>p</var> then, assign <var>p</var> to <var>newNode</var> and Right-Rotate <var>newNode</var>.</li>
<li>Set color of <var>p</var> as BLACK and color of <var>gP</var> as RED.</li>
<li>Left-Rotate <var>gP</var>.</li>
</ol></li>
<li>Set the root of the tree as BLACK.</li>
</ol><hr><h3 id="deletion">Deleting an element from a Red-Black Tree</h3>
<p>This operation removes a node from the tree. After deleting a node, the red-black property is maintained again.</p>
<hr><h3>Algorithm to delete a node</h3>
<ol><li>Save the color of <var>nodeToBeDeleted</var> in <var>origrinalColor</var>.</li>
<li>If the left child of <var>nodeToBeDeleted</var> is <code>NULL</code>
<ol type="a"><li>Assign the right child of <var>nodeToBeDeleted</var> to x.</li>
<li>Transplant <var>nodeToBeDeleted</var> with <var>x</var>.</li>
</ol></li>
<li>Else if the right child of <var>nodeToBeDeleted</var> is <code>NULL</code>
<ol type="a"><li>Assign the left child of <var>nodeToBeDeleted</var> into <var>x</var>.</li>
<li>Transplant <var>nodeToBeDeleted</var> with <var>x</var>.</li>
</ol></li>
<li>Else
<ol type="a"><li>Assign the minimum of right subtree of <var>noteToBeDeleted</var> into <var>y</var>.</li>
<li>Save the color of <var>y</var> in <var>originalColor</var>.</li>
<li>Assign the <var>rightChild</var> of <var>y</var> into <var>x</var>.</li>
<li>If <var>y</var> is a child of <var>nodeToBeDeleted</var>, then set the parent of <var>x</var> as <var>y</var>.</li>
<li>Else, transplant <var>y</var> with <var>rightChild</var> of <var>y</var>.</li>
<li>Transplant <var>nodeToBeDeleted</var> with <var>y</var>.</li>
<li>Set the color of y with originalColor.</li>
</ol></li>
<li>If the <var>originalColor</var> is BLACK, call DeleteFix(x).</li>
</ol><hr><h3>Algorithm to maintain Red-Black property after deletion</h3>
<p>This algorithm is implemented when a black node is deleted because it violates the black depth property of the red-black tree.</p>
<p>This violation is corrected by assuming that node <var>x</var> (which is occupying <var>y</var>'s original position) has an extra black. This makes node <var>x</var> neither red nor black. It is either doubly black or black-and-red. This violates the red-black properties.</p>
<p>However, the color attribute of <var>x</var> is not changed rather the extra black is represented in <var>x</var>'s pointing to the node.</p>
<p>The extra black can be removed if</p>
<ol><li>It reaches the root node.</li>
<li>If <var>x</var> points to a red-black node. In this case, <var>x</var> is colored black.</li>
<li>Suitable rotations and recoloring are performed.</li>
</ol><p>The following algorithm retains the properties of a red-black tree.</p>
<ol><li>Do the following until the <var>x</var> is not the root of the tree and the color of <var>x</var> is BLACK</li>
<li>If <var>x</var> is the left child of its parent then,
<ol type="a"><li>Assign w to the sibling of x.</li>
<li>If the right child of parent of <var>x</var> is RED,<br><strong>Case-I:</strong>
<ol type="i"><li>Set the color of the right child of the parent of <var>x</var> as BLACK.</li>
<li>Set the color of the parent of <var>x</var> as RED.</li>
<li>Left-Rotate the parent of <var>x</var>.</li>
<li>Assign the <var>rightChild</var> of the parent of <var>x</var> to <var>w</var>.</li>
</ol></li>
<li>If the color of both the right and the <var>leftChild</var> of <var>w</var> is BLACK,<br><strong>Case-II:</strong>
<ol type="i"><li>Set the color of <var>w</var> as RED</li>
<li>Assign the parent of <var>x</var> to <var>x</var>.</li>
</ol></li>
<li>Else if the color of the <var>rightChild</var> of <var>w</var> is BLACK<br><strong>Case-III:</strong>
<ol type="i"><li>Set the color of the <var>leftChild</var> of <var>w</var> as BLACK</li>
<li>Set the color of <var>w</var> as RED</li>
<li>Right-Rotate <var>w</var>.</li>
<li>Assign the <var>rightChild</var> of the parent of <var>x</var> to <var>w.</var></li>
</ol></li>
<li>If any of the above cases do not occur, then do the following.<br><strong>Case-IV:</strong>
<ol type="i"><li>Set the color of <var>w</var> as the color of the parent of <var>x</var>.</li>
<li>Set the color of the parent of <var>x</var> as BLACK.</li>
<li>Set the color of the right child of <var>w</var> as BLACK.</li>
<li>Left-Rotate the parent of <var>x</var>.</li>
<li>Set <var>x</var> as the root of the tree.</li>
</ol></li>
</ol></li>
<li>Else the same as above with right changed to left and vice versa.</li>
<li>Set the color of <var>x</var> as BLACK.</li>
</ol><hr><p>Please refer to <a href="/dsa/insertion-in-a-red-black-tree.html">insertion</a> and <a href="/deletion-from-a-red-black-tree.html">deletion</a> operations for more explanation with examples.</p>
<hr><h2 id="examples">Python, Java and C/C++ Examples</h2>
<div class="tabbed-editor">
<div id="py1" onclick="changepy()" class="tabbed-editor__node tabbed-editor__node--active"><a href="#python-code">Python</a></div>
<div id="java1" onclick="changejava()" class="tabbed-editor__node"><a href="#java-code">Java</a></div>
<div id="c1" onclick="changec()" class="tabbed-editor__node"><a href="#c-code">C</a></div>
<div id="cpp1" onclick="changecpp()" class="tabbed-editor__node"><a href="#cpp-code">C++</a></div>
</div>
<div class="code-editor code-editor--tabbed">
<div class="code-editor__area code-editor__area--active" id="python-code">
<div class="pre-code-wrapper"><div title="Click to copy" class="copy-code-button"></div><pre class="exec" style="max-height: 600px;"><code class="python hljs"><span class="hljs-comment"># Implementing Red-Black Tree in Python</span>
<span class="hljs-keyword">import</span> sys
<span class="hljs-comment"># Node creation</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Node</span><span class="hljs-params">()</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, item)</span>:</span>
self.item = item
self.parent = <span class="hljs-literal">None</span>
self.left = <span class="hljs-literal">None</span>
self.right = <span class="hljs-literal">None</span>
self.color = <span class="hljs-number">1</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RedBlackTree</span><span class="hljs-params">()</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
self.TNULL = Node(<span class="hljs-number">0</span>)
self.TNULL.color = <span class="hljs-number">0</span>
self.TNULL.left = <span class="hljs-literal">None</span>
self.TNULL.right = <span class="hljs-literal">None</span>
self.root = self.TNULL
<span class="hljs-comment"># Preorder</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">pre_order_helper</span><span class="hljs-params">(self, node)</span>:</span>
<span class="hljs-keyword">if</span> node != TNULL:
sys.stdout.write(node.item + <span class="hljs-string">" "</span>)
self.pre_order_helper(node.left)
self.pre_order_helper(node.right)
<span class="hljs-comment"># Inorder</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">in_order_helper</span><span class="hljs-params">(self, node)</span>:</span>
<span class="hljs-keyword">if</span> node != TNULL:
self.in_order_helper(node.left)
sys.stdout.write(node.item + <span class="hljs-string">" "</span>)
self.in_order_helper(node.right)
<span class="hljs-comment"># Postorder</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">post_order_helper</span><span class="hljs-params">(self, node)</span>:</span>
<span class="hljs-keyword">if</span> node != TNULL:
self.post_order_helper(node.left)
self.post_order_helper(node.right)
sys.stdout.write(node.item + <span class="hljs-string">" "</span>)
<span class="hljs-comment"># Search the tree</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">search_tree_helper</span><span class="hljs-params">(self, node, key)</span>:</span>
<span class="hljs-keyword">if</span> node == TNULL <span class="hljs-keyword">or</span> key == node.item:
<span class="hljs-keyword">return</span> node
<span class="hljs-keyword">if</span> key < node.item:
<span class="hljs-keyword">return</span> self.search_tree_helper(node.left, key)
<span class="hljs-keyword">return</span> self.search_tree_helper(node.right, key)
<span class="hljs-comment"># Balancing the tree after deletion</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">delete_fix</span><span class="hljs-params">(self, x)</span>:</span>
<span class="hljs-keyword">while</span> x != self.root <span class="hljs-keyword">and</span> x.color == <span class="hljs-number">0</span>:
<span class="hljs-keyword">if</span> x == x.parent.left:
s = x.parent.right
<span class="hljs-keyword">if</span> s.color == <span class="hljs-number">1</span>:
s.color = <span class="hljs-number">0</span>
x.parent.color = <span class="hljs-number">1</span>
self.left_rotate(x.parent)
s = x.parent.right
<span class="hljs-keyword">if</span> s.left.color == <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> s.right.color == <span class="hljs-number">0</span>:
s.color = <span class="hljs-number">1</span>
x = x.parent
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">if</span> s.right.color == <span class="hljs-number">0</span>:
s.left.color = <span class="hljs-number">0</span>
s.color = <span class="hljs-number">1</span>
self.right_rotate(s)
s = x.parent.right
s.color = x.parent.color
x.parent.color = <span class="hljs-number">0</span>
s.right.color = <span class="hljs-number">0</span>
self.left_rotate(x.parent)
x = self.root
<span class="hljs-keyword">else</span>:
s = x.parent.left
<span class="hljs-keyword">if</span> s.color == <span class="hljs-number">1</span>:
s.color = <span class="hljs-number">0</span>
x.parent.color = <span class="hljs-number">1</span>
self.right_rotate(x.parent)
s = x.parent.left
<span class="hljs-keyword">if</span> s.right.color == <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> s.right.color == <span class="hljs-number">0</span>:
s.color = <span class="hljs-number">1</span>
x = x.parent
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">if</span> s.left.color == <span class="hljs-number">0</span>:
s.right.color = <span class="hljs-number">0</span>
s.color = <span class="hljs-number">1</span>
self.left_rotate(s)
s = x.parent.left
s.color = x.parent.color
x.parent.color = <span class="hljs-number">0</span>
s.left.color = <span class="hljs-number">0</span>
self.right_rotate(x.parent)
x = self.root
x.color = <span class="hljs-number">0</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__rb_transplant</span><span class="hljs-params">(self, u, v)</span>:</span>
<span class="hljs-keyword">if</span> u.parent == <span class="hljs-literal">None</span>:
self.root = v
<span class="hljs-keyword">elif</span> u == u.parent.left:
u.parent.left = v
<span class="hljs-keyword">else</span>:
u.parent.right = v
v.parent = u.parent
<span class="hljs-comment"># Node deletion</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">delete_node_helper</span><span class="hljs-params">(self, node, key)</span>:</span>
z = self.TNULL
<span class="hljs-keyword">while</span> node != self.TNULL:
<span class="hljs-keyword">if</span> node.item == key:
z = node
<span class="hljs-keyword">if</span> node.item <= key:
node = node.right
<span class="hljs-keyword">else</span>:
node = node.left
<span class="hljs-keyword">if</span> z == self.TNULL:
<span class="hljs-keyword">print</span>(<span class="hljs-string">"Cannot find key in the tree"</span>)
<span class="hljs-keyword">return</span>
y = z
y_original_color = y.color
<span class="hljs-keyword">if</span> z.left == self.TNULL:
x = z.right
self.__rb_transplant(z, z.right)
<span class="hljs-keyword">elif</span> (z.right == self.TNULL):
x = z.left
self.__rb_transplant(z, z.left)
<span class="hljs-keyword">else</span>:
y = self.minimum(z.right)
y_original_color = y.color
x = y.right
<span class="hljs-keyword">if</span> y.parent == z:
x.parent = y
<span class="hljs-keyword">else</span>:
self.__rb_transplant(y, y.right)
y.right = z.right
y.right.parent = y
self.__rb_transplant(z, y)
y.left = z.left
y.left.parent = y
y.color = z.color
<span class="hljs-keyword">if</span> y_original_color == <span class="hljs-number">0</span>:
self.delete_fix(x)
<span class="hljs-comment"># Balance the tree after insertion</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fix_insert</span><span class="hljs-params">(self, k)</span>:</span>
<span class="hljs-keyword">while</span> k.parent.color == <span class="hljs-number">1</span>:
<span class="hljs-keyword">if</span> k.parent == k.parent.parent.right:
u = k.parent.parent.left
<span class="hljs-keyword">if</span> u.color == <span class="hljs-number">1</span>:
u.color = <span class="hljs-number">0</span>
k.parent.color = <span class="hljs-number">0</span>
k.parent.parent.color = <span class="hljs-number">1</span>
k = k.parent.parent
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">if</span> k == k.parent.left:
k = k.parent
self.right_rotate(k)
k.parent.color = <span class="hljs-number">0</span>
k.parent.parent.color = <span class="hljs-number">1</span>
self.left_rotate(k.parent.parent)
<span class="hljs-keyword">else</span>:
u = k.parent.parent.right
<span class="hljs-keyword">if</span> u.color == <span class="hljs-number">1</span>:
u.color = <span class="hljs-number">0</span>
k.parent.color = <span class="hljs-number">0</span>
k.parent.parent.color = <span class="hljs-number">1</span>
k = k.parent.parent
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">if</span> k == k.parent.right:
k = k.parent
self.left_rotate(k)
k.parent.color = <span class="hljs-number">0</span>
k.parent.parent.color = <span class="hljs-number">1</span>
self.right_rotate(k.parent.parent)
<span class="hljs-keyword">if</span> k == self.root:
<span class="hljs-keyword">break</span>
self.root.color = <span class="hljs-number">0</span>
<span class="hljs-comment"># Printing the tree</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__print_helper</span><span class="hljs-params">(self, node, indent, last)</span>:</span>
<span class="hljs-keyword">if</span> node != self.TNULL:
sys.stdout.write(indent)
<span class="hljs-keyword">if</span> last:
sys.stdout.write(<span class="hljs-string">"R----"</span>)
indent += <span class="hljs-string">" "</span>
<span class="hljs-keyword">else</span>:
sys.stdout.write(<span class="hljs-string">"L----"</span>)
indent += <span class="hljs-string">"| "</span>
s_color = <span class="hljs-string">"RED"</span> <span class="hljs-keyword">if</span> node.color == <span class="hljs-number">1</span> <span class="hljs-keyword">else</span> <span class="hljs-string">"BLACK"</span>
<span class="hljs-keyword">print</span>(str(node.item) + <span class="hljs-string">"("</span> + s_color + <span class="hljs-string">")"</span>)
self.__print_helper(node.left, indent, <span class="hljs-literal">False</span>)
self.__print_helper(node.right, indent, <span class="hljs-literal">True</span>)
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">preorder</span><span class="hljs-params">(self)</span>:</span>
self.pre_order_helper(self.root)
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">inorder</span><span class="hljs-params">(self)</span>:</span>
self.in_order_helper(self.root)
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">postorder</span><span class="hljs-params">(self)</span>:</span>
self.post_order_helper(self.root)
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">searchTree</span><span class="hljs-params">(self, k)</span>:</span>
<span class="hljs-keyword">return</span> self.search_tree_helper(self.root, k)
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">minimum</span><span class="hljs-params">(self, node)</span>:</span>
<span class="hljs-keyword">while</span> node.left != self.TNULL:
node = node.left
<span class="hljs-keyword">return</span> node
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">maximum</span><span class="hljs-params">(self, node)</span>:</span>
<span class="hljs-keyword">while</span> node.right != self.TNULL:
node = node.right
<span class="hljs-keyword">return</span> node
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">successor</span><span class="hljs-params">(self, x)</span>:</span>
<span class="hljs-keyword">if</span> x.right != self.TNULL:
<span class="hljs-keyword">return</span> self.minimum(x.right)
y = x.parent
<span class="hljs-keyword">while</span> y != self.TNULL <span class="hljs-keyword">and</span> x == y.right:
x = y
y = y.parent
<span class="hljs-keyword">return</span> y
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">predecessor</span><span class="hljs-params">(self, x)</span>:</span>
<span class="hljs-keyword">if</span> (x.left != self.TNULL):
<span class="hljs-keyword">return</span> self.maximum(x.left)
y = x.parent
<span class="hljs-keyword">while</span> y != self.TNULL <span class="hljs-keyword">and</span> x == y.left:
x = y
y = y.parent
<span class="hljs-keyword">return</span> y
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">left_rotate</span><span class="hljs-params">(self, x)</span>:</span>
y = x.right
x.right = y.left
<span class="hljs-keyword">if</span> y.left != self.TNULL:
y.left.parent = x
y.parent = x.parent
<span class="hljs-keyword">if</span> x.parent == <span class="hljs-literal">None</span>:
self.root = y
<span class="hljs-keyword">elif</span> x == x.parent.left:
x.parent.left = y
<span class="hljs-keyword">else</span>:
x.parent.right = y
y.left = x
x.parent = y
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">right_rotate</span><span class="hljs-params">(self, x)</span>:</span>
y = x.left
x.left = y.right
<span class="hljs-keyword">if</span> y.right != self.TNULL:
y.right.parent = x
y.parent = x.parent
<span class="hljs-keyword">if</span> x.parent == <span class="hljs-literal">None</span>:
self.root = y
<span class="hljs-keyword">elif</span> x == x.parent.right:
x.parent.right = y
<span class="hljs-keyword">else</span>:
x.parent.left = y
y.right = x
x.parent = y
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">insert</span><span class="hljs-params">(self, key)</span>:</span>
node = Node(key)
node.parent = <span class="hljs-literal">None</span>
node.item = key
node.left = self.TNULL
node.right = self.TNULL
node.color = <span class="hljs-number">1</span>
y = <span class="hljs-literal">None</span>
x = self.root
<span class="hljs-keyword">while</span> x != self.TNULL:
y = x
<span class="hljs-keyword">if</span> node.item < x.item:
x = x.left
<span class="hljs-keyword">else</span>:
x = x.right
node.parent = y
<span class="hljs-keyword">if</span> y == <span class="hljs-literal">None</span>:
self.root = node
<span class="hljs-keyword">elif</span> node.item < y.item:
y.left = node
<span class="hljs-keyword">else</span>:
y.right = node
<span class="hljs-keyword">if</span> node.parent == <span class="hljs-literal">None</span>:
node.color = <span class="hljs-number">0</span>
<span class="hljs-keyword">return</span>
<span class="hljs-keyword">if</span> node.parent.parent == <span class="hljs-literal">None</span>:
<span class="hljs-keyword">return</span>
self.fix_insert(node)
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_root</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword">return</span> self.root
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">delete_node</span><span class="hljs-params">(self, item)</span>:</span>
self.delete_node_helper(self.root, item)
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">print_tree</span><span class="hljs-params">(self)</span>:</span>
self.__print_helper(self.root, <span class="hljs-string">""</span>, <span class="hljs-literal">True</span>)
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">"__main__"</span>:
bst = RedBlackTree()
bst.insert(<span class="hljs-number">55</span>)
bst.insert(<span class="hljs-number">40</span>)
bst.insert(<span class="hljs-number">65</span>)
bst.insert(<span class="hljs-number">60</span>)
bst.insert(<span class="hljs-number">75</span>)
bst.insert(<span class="hljs-number">57</span>)
bst.print_tree()
<span class="hljs-keyword">print</span>(<span class="hljs-string">"\nAfter deleting an element"</span>)
bst.delete_node(<span class="hljs-number">40</span>)
bst.print_tree()
</code></pre></div>
</div>
<div class="code-editor__area" id="java-code">
<div class="pre-code-wrapper"><div title="Click to copy" class="copy-code-button"></div><pre class="exec" style="max-height: 600px;"><code class="java hljs"><span class="hljs-comment">// Implementing Red-Black Tree in Java</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Node</span> </span>{
<span class="hljs-keyword">int</span> data;
Node parent;
Node left;
Node right;
<span class="hljs-keyword">int</span> color;
}
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RedBlackTree</span> </span>{
<span class="hljs-keyword">private</span> Node root;
<span class="hljs-keyword">private</span> Node TNULL;
<span class="hljs-comment">// Preorder</span>
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">preOrderHelper</span><span class="hljs-params">(Node node)</span> </span>{
<span class="hljs-keyword">if</span> (node != TNULL) {
System.out.print(node.data + <span class="hljs-string">" "</span>);
preOrderHelper(node.left);
preOrderHelper(node.right);
}
}
<span class="hljs-comment">// Inorder</span>
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">inOrderHelper</span><span class="hljs-params">(Node node)</span> </span>{
<span class="hljs-keyword">if</span> (node != TNULL) {
inOrderHelper(node.left);
System.out.print(node.data + <span class="hljs-string">" "</span>);
inOrderHelper(node.right);
}
}
<span class="hljs-comment">// Post order</span>
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">postOrderHelper</span><span class="hljs-params">(Node node)</span> </span>{
<span class="hljs-keyword">if</span> (node != TNULL) {
postOrderHelper(node.left);
postOrderHelper(node.right);
System.out.print(node.data + <span class="hljs-string">" "</span>);
}
}
<span class="hljs-comment">// Search the tree</span>
<span class="hljs-function"><span class="hljs-keyword">private</span> Node <span class="hljs-title">searchTreeHelper</span><span class="hljs-params">(Node node, <span class="hljs-keyword">int</span> key)</span> </span>{
<span class="hljs-keyword">if</span> (node == TNULL || key == node.data) {
<span class="hljs-keyword">return</span> node;
}
<span class="hljs-keyword">if</span> (key < node.data) {
<span class="hljs-keyword">return</span> searchTreeHelper(node.left, key);
}
<span class="hljs-keyword">return</span> searchTreeHelper(node.right, key);
}
<span class="hljs-comment">// Balance the tree after deletion of a node</span>
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">fixDelete</span><span class="hljs-params">(Node x)</span> </span>{
Node s;
<span class="hljs-keyword">while</span> (x != root && x.color == <span class="hljs-number">0</span>) {
<span class="hljs-keyword">if</span> (x == x.parent.left) {
s = x.parent.right;
<span class="hljs-keyword">if</span> (s.color == <span class="hljs-number">1</span>) {
s.color = <span class="hljs-number">0</span>;
x.parent.color = <span class="hljs-number">1</span>;
leftRotate(x.parent);
s = x.parent.right;
}
<span class="hljs-keyword">if</span> (s.left.color == <span class="hljs-number">0</span> && s.right.color == <span class="hljs-number">0</span>) {
s.color = <span class="hljs-number">1</span>;
x = x.parent;
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">if</span> (s.right.color == <span class="hljs-number">0</span>) {
s.left.color = <span class="hljs-number">0</span>;
s.color = <span class="hljs-number">1</span>;
rightRotate(s);
s = x.parent.right;
}
s.color = x.parent.color;
x.parent.color = <span class="hljs-number">0</span>;
s.right.color = <span class="hljs-number">0</span>;
leftRotate(x.parent);
x = root;
}
} <span class="hljs-keyword">else</span> {
s = x.parent.left;
<span class="hljs-keyword">if</span> (s.color == <span class="hljs-number">1</span>) {
s.color = <span class="hljs-number">0</span>;
x.parent.color = <span class="hljs-number">1</span>;
rightRotate(x.parent);
s = x.parent.left;
}
<span class="hljs-keyword">if</span> (s.right.color == <span class="hljs-number">0</span> && s.right.color == <span class="hljs-number">0</span>) {
s.color = <span class="hljs-number">1</span>;
x = x.parent;
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">if</span> (s.left.color == <span class="hljs-number">0</span>) {
s.right.color = <span class="hljs-number">0</span>;
s.color = <span class="hljs-number">1</span>;
leftRotate(s);
s = x.parent.left;
}
s.color = x.parent.color;
x.parent.color = <span class="hljs-number">0</span>;
s.left.color = <span class="hljs-number">0</span>;
rightRotate(x.parent);
x = root;
}
}
}
x.color = <span class="hljs-number">0</span>;
}
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">rbTransplant</span><span class="hljs-params">(Node u, Node v)</span> </span>{
<span class="hljs-keyword">if</span> (u.parent == <span class="hljs-keyword">null</span>) {
root = v;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (u == u.parent.left) {
u.parent.left = v;
} <span class="hljs-keyword">else</span> {
u.parent.right = v;
}
v.parent = u.parent;
}
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">deleteNodeHelper</span><span class="hljs-params">(Node node, <span class="hljs-keyword">int</span> key)</span> </span>{
Node z = TNULL;
Node x, y;
<span class="hljs-keyword">while</span> (node != TNULL) {
<span class="hljs-keyword">if</span> (node.data == key) {
z = node;
}
<span class="hljs-keyword">if</span> (node.data <= key) {
node = node.right;
} <span class="hljs-keyword">else</span> {
node = node.left;
}
}
<span class="hljs-keyword">if</span> (z == TNULL) {
System.out.println(<span class="hljs-string">"Couldn't find key in the tree"</span>);
<span class="hljs-keyword">return</span>;
}
y = z;
<span class="hljs-keyword">int</span> yOriginalColor = y.color;
<span class="hljs-keyword">if</span> (z.left == TNULL) {
x = z.right;
rbTransplant(z, z.right);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (z.right == TNULL) {
x = z.left;
rbTransplant(z, z.left);
} <span class="hljs-keyword">else</span> {
y = minimum(z.right);
yOriginalColor = y.color;
x = y.right;
<span class="hljs-keyword">if</span> (y.parent == z) {
x.parent = y;
} <span class="hljs-keyword">else</span> {
rbTransplant(y, y.right);
y.right = z.right;
y.right.parent = y;
}
rbTransplant(z, y);
y.left = z.left;
y.left.parent = y;
y.color = z.color;
}
<span class="hljs-keyword">if</span> (yOriginalColor == <span class="hljs-number">0</span>) {
fixDelete(x);
}
}
<span class="hljs-comment">// Balance the node after insertion</span>
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">fixInsert</span><span class="hljs-params">(Node k)</span> </span>{
Node u;
<span class="hljs-keyword">while</span> (k.parent.color == <span class="hljs-number">1</span>) {
<span class="hljs-keyword">if</span> (k.parent == k.parent.parent.right) {
u = k.parent.parent.left;
<span class="hljs-keyword">if</span> (u.color == <span class="hljs-number">1</span>) {
u.color = <span class="hljs-number">0</span>;
k.parent.color = <span class="hljs-number">0</span>;
k.parent.parent.color = <span class="hljs-number">1</span>;
k = k.parent.parent;
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">if</span> (k == k.parent.left) {
k = k.parent;
rightRotate(k);
}
k.parent.color = <span class="hljs-number">0</span>;
k.parent.parent.color = <span class="hljs-number">1</span>;
leftRotate(k.parent.parent);
}
} <span class="hljs-keyword">else</span> {
u = k.parent.parent.right;
<span class="hljs-keyword">if</span> (u.color == <span class="hljs-number">1</span>) {
u.color = <span class="hljs-number">0</span>;
k.parent.color = <span class="hljs-number">0</span>;
k.parent.parent.color = <span class="hljs-number">1</span>;
k = k.parent.parent;
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">if</span> (k == k.parent.right) {
k = k.parent;
leftRotate(k);
}
k.parent.color = <span class="hljs-number">0</span>;
k.parent.parent.color = <span class="hljs-number">1</span>;
rightRotate(k.parent.parent);
}
}
<span class="hljs-keyword">if</span> (k == root) {
<span class="hljs-keyword">break</span>;
}
}
root.color = <span class="hljs-number">0</span>;
}
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">printHelper</span><span class="hljs-params">(Node root, String indent, <span class="hljs-keyword">boolean</span> last)</span> </span>{
<span class="hljs-keyword">if</span> (root != TNULL) {
System.out.print(indent);
<span class="hljs-keyword">if</span> (last) {
System.out.print(<span class="hljs-string">"R----"</span>);
indent += <span class="hljs-string">" "</span>;
} <span class="hljs-keyword">else</span> {
System.out.print(<span class="hljs-string">"L----"</span>);
indent += <span class="hljs-string">"| "</span>;
}
String sColor = root.color == <span class="hljs-number">1</span> ? <span class="hljs-string">"RED"</span> : <span class="hljs-string">"BLACK"</span>;
System.out.println(root.data + <span class="hljs-string">"("</span> + sColor + <span class="hljs-string">")"</span>);
printHelper(root.left, indent, <span class="hljs-keyword">false</span>);
printHelper(root.right, indent, <span class="hljs-keyword">true</span>);
}
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">RedBlackTree</span><span class="hljs-params">()</span> </span>{
TNULL = <span class="hljs-keyword">new</span> Node();
TNULL.color = <span class="hljs-number">0</span>;
TNULL.left = <span class="hljs-keyword">null</span>;
TNULL.right = <span class="hljs-keyword">null</span>;
root = TNULL;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">preorder</span><span class="hljs-params">()</span> </span>{
preOrderHelper(<span class="hljs-keyword">this</span>.root);
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">inorder</span><span class="hljs-params">()</span> </span>{
inOrderHelper(<span class="hljs-keyword">this</span>.root);
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">postorder</span><span class="hljs-params">()</span> </span>{
postOrderHelper(<span class="hljs-keyword">this</span>.root);
}
<span class="hljs-function"><span class="hljs-keyword">public</span> Node <span class="hljs-title">searchTree</span><span class="hljs-params">(<span class="hljs-keyword">int</span> k)</span> </span>{
<span class="hljs-keyword">return</span> searchTreeHelper(<span class="hljs-keyword">this</span>.root, k);
}
<span class="hljs-function"><span class="hljs-keyword">public</span> Node <span class="hljs-title">minimum</span><span class="hljs-params">(Node node)</span> </span>{
<span class="hljs-keyword">while</span> (node.left != TNULL) {
node = node.left;
}
<span class="hljs-keyword">return</span> node;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> Node <span class="hljs-title">maximum</span><span class="hljs-params">(Node node)</span> </span>{
<span class="hljs-keyword">while</span> (node.right != TNULL) {
node = node.right;
}
<span class="hljs-keyword">return</span> node;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> Node <span class="hljs-title">successor</span><span class="hljs-params">(Node x)</span> </span>{
<span class="hljs-keyword">if</span> (x.right != TNULL) {
<span class="hljs-keyword">return</span> minimum(x.right);
}
Node y = x.parent;