-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrbt.c
1040 lines (892 loc) · 24.9 KB
/
rbt.c
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
/*
* Copyright (c) International Business Machines Corp., 2001-2004
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
* the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "rbt.h"
/*
* ***********************************************
*
* D.H. IBM, S. Rao
*
* Module: Operations executed on red-black struct
*
* ***********************************************
*/
/* Construct a red-black tree node */
rb_node *rbnode_construct(datatype object, rb_color color)
{
rb_node *node = (rb_node *) malloc(sizeof(rb_node));
if (!node) {
fprintf(stderr, "Memory Shortage - No Execution Possible\n");
return NULL;
}
node->object = object;
node->color = color;
node->parent = node->right = node->left = NULL;
return node;
}
/* Destructor of a red-black tree node */
void rbnode_destruct(rb_node *node, destructor d)
{
if (!node)
return;
if (d != NULL)
d(node->object);
rbnode_destruct(node->right, d);
rbnode_destruct(node->left, d);
free(node);
}
/* Determine the depth of the subtree spanned by a given node */
int rbnode_depth(rb_node *node)
{
/* Recursively determine the depth of the left and right
* subtrees
*/
int irightdepth = (node->right) ? rbnode_depth(node->right) : 0;
int ileftdepth = (node->left) ? rbnode_depth(node->left) : 0;
/* Return the maximal child depth + 1 (the current node) */
return ((irightdepth > ileftdepth) ? (irightdepth + 1) : (ileftdepth + 1));
}
/* Return the leftmost leaf in the tree */
rb_node *rbnode_minimum(rb_node *node)
{
while (node->left)
node = node->left;
return node;
}
/* Return the rightmost leaf in the tree */
rb_node *rbnode_maximum(rb_node *node)
{
while (node->right)
node = node->right;
return node;
}
/* Replace an object */
void rbnode_replace(rb_node *node, datatype object)
{
/* Make sure the replacement does not violate the tree order
* Replace the object at the node
*/
node->object = object;
}
/* Get the next node in the tree (according to the tree order) */
rb_node *rbnode_successor(rb_node *node)
{
rb_node *succ_node;
if (node->right) {
/* If there is a right child, the successor is the
* minimal object in the sub-tree spanned by this
* child.
*/
succ_node = node->right;
while (succ_node->left)
succ_node = succ_node->left;
} else {
/* Otherwise, go up the tree until reaching the parent
* from the left direction.
*/
rb_node *prev_node = node;
succ_node = node->parent;
while (succ_node && prev_node == succ_node->right) {
prev_node = succ_node;
succ_node = succ_node->parent;
}
}
return (succ_node);
}
/* Get the previous node in the tree (according to the tree order) */
rb_node *rbnode_predecessor(rb_node *node)
{
rb_node *pred_node;
if (node->left) {
/* If there is a left child, the predecessor is the
* maximal object in the sub-tree spanned by this
* child.
*/
pred_node = node->left;
while (pred_node->right)
pred_node = pred_node->right;
} else {
/* Otherwise, go up the tree until reaching the parent
* from the right direction.
*/
rb_node *prev_node = node;
pred_node = node->parent;
while (pred_node && prev_node == pred_node->left) {
prev_node = pred_node;
pred_node = pred_node->parent;
}
}
return (pred_node);
}
/* Return a pointer to a duplication of the given node */
rb_node *rbnode_duplicate(rb_node *node)
{
/* Create a node of the same color, containing the same
* object
*/
rb_node *dup_node = rbnode_construct(node->object, node->color);
if (!dup_node)
return NULL;
/* Duplicate the children recursively */
if (node->right) {
dup_node->right = rbnode_duplicate(node->right);
dup_node->right->parent = dup_node;
} else {
dup_node->right = NULL;
}
if (node->left) {
dup_node->left = rbnode_duplicate(node->left);
dup_node->left->parent = dup_node;
} else {
dup_node->left = NULL;
}
return dup_node; /* Return the duplicated node */
}
/* Traverse a red-black subtree */
void rbnode_traverse(rb_node *node, opr *op)
{
if (!node)
return;
rbnode_traverse(node->left, op);
op(node->object);
rbnode_traverse(node->right, op);
}
/*
* ***********************************
*
* Operations on rb_tree struct
*
* ***********************************
*/
/* Intialize a tree */
void rbtree_init(rb_tree *tree)
{
/* tree->comp = comp; */
tree->isize = 0;
tree->root = NULL;
}
/* Construct a tree given a comparison function */
rb_tree *rbtree_construct()
{
rb_tree *tree = (rb_tree *) malloc(sizeof(rb_tree));
if (!tree) {
fprintf(stderr, "Memory Issue - Shortge Exists!\n");
return NULL;
}
rbtree_init(tree);
return tree;
}
/* Remove all objects from a black-red tree */
void rbtree_clean(rb_tree *tree, destructor d)
{
if (tree->root)
rbnode_destruct(tree->root, d);
tree->root = NULL;
tree->isize = 0;
}
/* Destruct a red-black tree */
void rbtree_destruct(rb_tree *tree, destructor d)
{
rbtree_clean(tree, d);
free(tree);
}
/* Returns the size of the tree */
int rbtree_size(rb_tree *tree)
{
return tree->isize;
}
/* Returns the depth of the tree */
int rbtree_depth(rb_tree *tree)
{
if (!(tree->root))
return 0;
return rbnode_depth(tree->root);
}
/* Check whether the tree contains a certain object */
int rbtree_contains(rb_tree *tree, datatype object)
{
return (rbtree_find(tree, object) != NULL);
}
/* Insert an object into the rb-tree */
rb_node *rbtree_insert(rb_tree *tree, datatype object)
{
rb_node *cur_node;
rb_node *new_node;
int comp_result = 0;
if (!(tree->root)) {
/* Assign a new root node (the root is always
* black)
*/
new_node = rbnode_construct(object, black);
if (!new_node)
return NULL;
tree->root = new_node;
tree->isize = 1;
return new_node;
}
/* Find a spot for the new object, insert the object as a red
* leaf
*/
cur_node = tree->root;
new_node = rbnode_construct(object, red);
if (!new_node)
return NULL;
while (cur_node) {
/* Compare inserted object with the object stored in
* the current node
*/
comp_result = COMP_NODES(object, cur_node->object);
if (comp_result == 0) {
printf("Attempted to insert duplicate node, aborting\n");
free(new_node);
return NULL;
}
if (comp_result > 0) {
if (!(cur_node->left)) {
/* Insert the new leaf as the left
* child of the current node
*/
cur_node->left = new_node;
new_node->parent = cur_node;
cur_node = NULL; /* Terminate the while loop */
} else {
/* Go to the left subtree */
cur_node = cur_node->left;
}
} else {
if (!(cur_node->right)) {
/* Insert the new leaf as the right
* child of the current node
*/
cur_node->right = new_node;
new_node->parent = cur_node;
cur_node = NULL; /* Terminate the while loop */
} else {
/* Go to the right subtree */
cur_node = cur_node->right;
}
}
}
/* Mark the fact that a new node was added */
tree->isize++;
/* Fix the tree properties */
rbtree_insert_fixup(tree, new_node);
return new_node;
}
/* Insert a new object to the tree as the a successor of a given
* node
*/
rb_node *insert_successor_at(rb_tree *tree, rb_node *at_node, datatype object)
{
rb_node *parent;
rb_node *new_node;
if (!(tree->root)) {
/* Assign a new root node (the root is always
* black)
*/
new_node = rbnode_construct(object, black);
if (!new_node)
return NULL;
tree->root = new_node;
tree->isize = 1;
return new_node;
}
/* Insert the new object as a red leaf, being the successor of
* node
*/
new_node = rbnode_construct(object, red);
if (!new_node)
return NULL;
if (!at_node) {
/* The new node should become the tree's minimum Place
* is as the left child of the current minimal leaf
*/
parent = rbnode_minimum(tree->root);
parent->left = new_node;
} else {
/* Make sure the insertion does not violate the tree
* order In case given node has no right child, place
* the new node as its right child. Otherwise, place
* it at the leftmost position at the sub-tree rooted
* at its right side.
*/
if (!at_node->right) {
parent = at_node;
parent->right = new_node;
} else {
parent = rbnode_minimum(at_node->right);
parent->left = new_node;
}
}
new_node->parent = parent;
/* Mark that a new node was added */
tree->isize++;
/* Fix the tree properties */
rbtree_insert_fixup(tree, new_node);
return new_node;
}
/* Insert a new object to the tree as the a predecessor of a given node */
rb_node *insert_predecessor_at(rb_tree *tree, rb_node *at_node, datatype object)
{
rb_node *parent;
rb_node *new_node;
if (!(tree->root)) {
/* Assign a new root node (the root is always
* black)
*/
new_node = rbnode_construct(object, black);
if (!new_node)
return NULL;
tree->root = new_node;
tree->isize = 1;
return new_node;
}
/* Insert the new object as a red leaf, being the predecessor
* of at_node
*/
new_node = rbnode_construct(object, red);
if (!new_node)
return NULL;
if (!at_node) {
/* The new node should become the tree maximum. Place
* is as the right child of the current maximal leaf
*/
parent = rbnode_maximum(tree->root);
parent->right = new_node;
} else {
/* Make sure the insertion does not violate the tree
* order In case given node has no left child, place
* the new node as its left child. Otherwise, place it
* at the rightmost position at the sub-tree rooted at
* its left side.
*/
if (!(at_node->left)) {
parent = at_node;
parent->left = new_node;
} else {
parent = rbnode_maximum(at_node->left);
parent->right = new_node;
}
}
new_node->parent = parent;
/* Mark that a new node was added */
tree->isize++;
/* Fix the tree properties */
rbtree_insert_fixup(tree, new_node);
return new_node;
}
/* Remove an object from the tree */
void rbtree_remove(rb_tree *tree, datatype object, destructor d)
{
rb_node *node = rbtree_find(tree, object); /* Find the node */
rbtree_remove_at(tree, node, d); /* Remove the node */
}
/* Remove the object pointed by the given node. */
void rbtree_remove_at(rb_tree *tree, rb_node *node, destructor d)
{
rb_node *child = NULL;
/* In case of deleting the single object stored in the tree,
* free the root, thus emptying the tree
*/
if (tree->isize == 1) {
rbnode_destruct(tree->root, d);
tree->root = NULL;
tree->isize = 0;
return;
}
/* Remove the given node from the tree */
if (node->left && node->right) {
/* If the node we want to remove has two children,
* find its successor, which is the leftmost child in
* its right sub-tree and has at most one child (it
* may have a right child).
*/
rb_node *succ_node = rbnode_minimum(node->right);
/* Now physically swap node and its successor. Notice
* this may temporarily violate the tree properties,
* but we are going to remove node anyway. This way
* we have moved node to a position were it is more
* convinient to delete it.
*/
int immediate_succ = (node->right == succ_node);
rb_node *succ_parent = succ_node->parent;
rb_node *succ_left = succ_node->left;
rb_node *succ_right = succ_node->right;
rb_color succ_color = succ_node->color;
succ_node->parent = node->parent;
succ_node->left = node->left;
succ_node->right = immediate_succ ? node : node->right;
succ_node->color = node->color;
node->parent = immediate_succ ? succ_node : succ_parent;
node->left = succ_left;
node->right = succ_right;
node->color = succ_color;
if (!immediate_succ) {
if (succ_node == node->parent->left)
node->parent->left = node;
else
node->parent->right = node;
}
if (node->left)
node->left->parent = node;
if (node->right)
node->right->parent = node;
if (succ_node->parent) {
if (node == succ_node->parent->left)
succ_node->parent->left = succ_node;
else
succ_node->parent->right = succ_node;
} else {
tree->root = succ_node;
}
if (succ_node->left)
succ_node->left->parent = succ_node;
if (succ_node->right)
succ_node->right->parent = succ_node;
}
/* At this stage, the node we are going to remove has at most
* one child
*/
child = (node->left) ? node->left : node->right;
/* Splice out the node to be removed, by linking its parent
* straight to the removed node's single child.
*/
if (child)
child->parent = node->parent;
if (!(node->parent)) {
/* If we are deleting the root, make the child the new
* tree node
*/
tree->root = child;
} else {
/* Link the removed node parent to its child */
if (node == node->parent->left)
node->parent->left = child;
else
node->parent->right = child;
}
/* Fix-up the red-black properties that may have been damaged:
* If we have just removed a black node, the black-depth
* property is no longer valid
*/
if (node->color == black && child)
rbtree_remove_fixup(tree, child);
/* Delete the un-necessary node (nullify both its children
* because the node's destructor is recursive).
*/
node->left = NULL;
node->right = NULL;
free(node);
/* Decrease the number of objects in the tree */
tree->isize--;
}
/* Get the tree minimum */
rb_node *rbtree_minimum(rb_tree *tree)
{
if (!(tree->root))
return NULL;
/* Return the leftmost leaf in the tree */
return rbnode_minimum(tree->root);
}
/* Get the tree maximum */
rb_node *rbtree_maximum(rb_tree *tree)
{
if (!(tree->root))
return NULL;
/* Return the rightmost leaf in the tree */
return rbnode_maximum(tree->root);
}
/* Return a pointer to the node containing the given object */
rb_node *rbtree_find(rb_tree *tree, datatype object)
{
rb_node *cur_node = tree->root;
int comp_result;
while (cur_node) {
comp_result = COMP_NODES(object, cur_node->object);
/* In case of equality, we can return the current
* node.
*/
if (comp_result == 0)
return cur_node;
/* Go down to the left or right child. */
cur_node = (comp_result > 0) ? cur_node->left : cur_node->right;
}
/* If we get here, the object is not in the tree */
return NULL;
}
void rbtree_rotate_left(rb_tree *tree, rb_node *x_node)
{
/* Get the right child of the node */
rb_node *y_node = x_node->right;
/* Change its left subtree (T2) to x's right subtree */
x_node->right = y_node->left;
/* Link T2 to its new parent x */
if (y_node->left != NULL)
y_node->left->parent = x_node;
/* Assign x's parent to be y's parent */
y_node->parent = x_node->parent;
if (!(x_node->parent)) {
/* Make y the new tree root */
tree->root = y_node;
} else {
/* Assign a pointer to y from x's parent */
if (x_node == x_node->parent->left)
x_node->parent->left = y_node;
else
x_node->parent->right = y_node;
}
/* Assign x to be y's left child */
y_node->left = x_node;
x_node->parent = y_node;
}
/* Right-rotate the sub-tree spanned by the given node */
void rbtree_rotate_right(rb_tree *tree, rb_node *y_node)
{
/* Get the left child of the node */
rb_node *x_node = y_node->left;
/* Change its right subtree (T2) to y's left subtree */
y_node->left = x_node->right;
/* Link T2 to its new parent y */
if (x_node->right != NULL)
x_node->right->parent = y_node;
/* Assign y's parent to be x's parent */
x_node->parent = y_node->parent;
if (!(y_node->parent)) {
/* Make x the new tree root */
tree->root = x_node;
} else {
/* Assign a pointer to x from y's parent */
if (y_node == y_node->parent->left)
y_node->parent->left = x_node;
else
y_node->parent->right = x_node;
}
/* Assign y to be x's right child */
x_node->right = y_node;
y_node->parent = x_node;
}
/* Fix the tree so it maintains the red-black properties after an insert */
void rbtree_insert_fixup(rb_tree *tree, rb_node *node)
{
/* Fix the red-black propreties. We may have inserted a red
* leaf as the child of a red parent - so we have to fix the
* coloring of the parent recursively.
*/
rb_node *curr_node = node;
rb_node *grandparent;
rb_node *uncle;
assert(node && node->color == red);
while (curr_node != tree->root && curr_node->parent->color == red) {
/* Get a pointer to the current node's grandparent
* (the root is always black, so the red parent must
* have a parent).
*/
grandparent = curr_node->parent->parent;
if (curr_node->parent == grandparent->left) {
/* If the red parent is a left child, the
* uncle is the right child of the grandparent.
*/
uncle = grandparent->right;
if (uncle && uncle->color == red) {
/* If both parent and uncle are red,
* color them black and color the
* grandparent red. In case of a NULL
* uncle, treat it as a black node
*/
curr_node->parent->color = black;
uncle->color = black;
grandparent->color = red;
/* Move to the grandparent */
curr_node = grandparent;
} else {
/* Make sure the current node is a
* right child. If not, left-rotate the
* parent's sub-tree so the parent
* becomes the right child of the
* current node (see _rotate_left).
*/
if (curr_node == curr_node->parent->right) {
curr_node = curr_node->parent;
rbtree_rotate_left(tree, curr_node);
}
/* Color the parent black and the
* grandparent red
*/
curr_node->parent->color = black;
grandparent->color = red;
/* Right-rotate the grandparent's
* sub-tree
*/
rbtree_rotate_right(tree, grandparent);
}
} else {
/* If the red parent is a right child, the
* uncle is the left child of the grandparent.
*/
uncle = grandparent->left;
if (uncle && uncle->color == red) {
/* If both parent and uncle are red,
* color them black and color the
* grandparent red. In case of a NULL
* uncle, treat it as a black node
*/
curr_node->parent->color = black;
uncle->color = black;
grandparent->color = red;
/* Move to the grandparent */
curr_node = grandparent;
} else {
/* Make sure the current node is a
* left child. If not, right-rotate
* the parent's sub-tree so the parent
* becomes the left child of the
* current node.
*/
if (curr_node == curr_node->parent->left) {
curr_node = curr_node->parent;
rbtree_rotate_right(tree, curr_node);
}
/* Color the parent black and the
* grandparent red
*/
curr_node->parent->color = black;
grandparent->color = red;
/* Left-rotate the grandparent's
* sub-tree
*/
rbtree_rotate_left(tree, grandparent);
}
}
}
/* Make sure that the root is black */
tree->root->color = black;
}
void rbtree_remove_fixup(rb_tree *tree, rb_node *node)
{
rb_node *curr_node = node;
rb_node *sibling;
while (curr_node != tree->root && curr_node->color == black) {
/* Get a pointer to the current node's sibling (notice
* that the node's parent must exist, since the node
* is not the root).
*/
if (curr_node == curr_node->parent->left) {
/* If the current node is a left child, its
* sibling is the right child of the parent.
*/
sibling = curr_node->parent->right;
/* Check the sibling's color. Notice that NULL
* nodes are treated as if they are colored
* black.
*/
if (sibling && sibling->color == red) {
/* In case the sibling is red, color
* it black and rotate. Then color
* the parent red (the grandparent is
* now black)
*/
sibling->color = black;
curr_node->parent->color = red;
rbtree_rotate_left(tree, curr_node->parent);
sibling = curr_node->parent->right;
}
if (sibling &&
(!(sibling->left) || sibling->left->color == black) &&
(!(sibling->right) || sibling->right->color == black)) {
/* If the sibling has two black
* children, color it red
*/
sibling->color = red;
if (curr_node->parent->color == red) {
/* If the parent is red, we
* can safely color it black
* and terminate the fix
* process.
*/
curr_node->parent->color = black;
/* In order to stop the while loop */
curr_node = tree->root;
} else {
/* The black depth of the
* entire sub-tree rooted at
* the parent is now too small
* - fix it up recursively.
*/
curr_node = curr_node->parent;
}
} else {
if (!sibling) {
/* Take special care of the
* case of a NULL sibling
*/
if (curr_node->parent->color == red) {
curr_node->parent->color = black;
/* In order to stop
* the while loop */
curr_node = tree->root;
} else {
curr_node = curr_node->parent;
}
} else {
/* In this case, at least one
* of the sibling's children
* is red. It is therfore
* obvious that the sibling
* itself is black.
*/
if (sibling->right && sibling->right->color == red) {
/* If the right child
* of the sibling is
* red, color it black
* and rotate around
* the current parent.
*/
sibling->right->color = black;
rbtree_rotate_left(tree, curr_node->parent);
} else {
/* If the left child
* of the sibling is
* red, rotate around
* the sibling, then
* rotate around the
* new sibling of our
* current node.
*/
rbtree_rotate_right(tree, sibling);
sibling = curr_node->parent->right;
rbtree_rotate_left(tree, sibling);
}
/* It is now safe to color the
* parent black and to
* terminate the fix process.
*/
if (curr_node->parent->parent)
curr_node->parent->parent->color = curr_node->parent->color;
curr_node->parent->color = black;
/* In order to stop the while loop */
curr_node = tree->root;
}
}
} else {
/* If the current node is a right child, its
* sibling is the left child of the parent.
*/
sibling = curr_node->parent->left;
/* Check the sibling's color. Notice that NULL
* nodes are treated as if they are colored
* black.
*/
if (sibling && sibling->color == red) {
/* In case the sibling is red, color
* it black and rotate. Then color
* the parent red (the grandparent is
* now black).
*/
sibling->color = black;
curr_node->parent->color = red;
rbtree_rotate_right(tree, curr_node->parent);
sibling = curr_node->parent->left;
}
if (sibling &&
(!(sibling->left) || sibling->left->color == black) &&
(!(sibling->right) || sibling->right->color == black)) {
/* If the sibling has two black children, color it red */
sibling->color = red;
if (curr_node->parent->color == red) {
/* If the parent is red, we
* can safely color it black
* and terminate the fix-up
* process.
*/
curr_node->parent->color = black;
/* In order to stop the while
* loop
*/
curr_node = tree->root;
} else {
/* The black depth of the
* entire sub-tree rooted at
* the parent is now too small
* - fix it up recursively.
*/
curr_node = curr_node->parent;
}
} else {
if (!sibling) {
/* Take special care of the
* case of a NULL sibling */
if (curr_node->parent->color == red) {
curr_node->parent->color = black;
/* In order to stop
* the while loop */
curr_node = tree->root;
} else {
curr_node = curr_node->parent;
}
} else {
/* In this case, at least one
* of the sibling's children is
* red. It is therfore obvious
* that the sibling itself is
* black.
*/
if (sibling->left && sibling->left->color == red) {
/* If the left child
* of the sibling is
* red, color it black
* and rotate around
* the current parent
*/