-
Notifications
You must be signed in to change notification settings - Fork 18
/
RedBlackTree.c
1402 lines (1062 loc) · 40.1 KB
/
RedBlackTree.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
#include "../Headers/RedBlackTree.h"
#include "../../../System/Utils.h"
#include "../../../Unit Test/CuTest/CuTest.h"
typedef enum RotationType {
NONE, RIGHT, LEFT, RIGHT_LEFT, LEFT_RIGHT
} RotationType;
RBNode *createRBNode(RBNode *parent, COLOR color, void *item);
RBNode *rBTreeInsertR(RBNode *root, RBNode *parent, void *item, void (*freeFun)(void *),
int (*cmp)(const void *, const void *));
void rBTreeDeleteR(RBTree *tree, RBNode *root, void *item);
RBNode *getUncle(RBNode *currentNode);
COLOR getNodeColor(RBNode *node);
RotationType getRotationType(RBNode *grandParent);
RBNode *performRotation(RBNode *node, RotationType rotationType);
RBNode *leftRotation(RBNode *grandParent);
RBNode *rightRotation(RBNode *grandParent);
int isRotationRequired(RBNode *grandParent);
void rBPreOrderTraversalR(RBNode *root, void (*printFun)(const void *item, COLOR color));
int isCaseOne(RBNode *root);
RBNode *getSuccessorNode(RBNode *node);
void performRotationRecoloring(RBNode *root, RotationType rotationType);
void doubleBlackCase(RBTree *tree, RBNode *root, RBNode *parent);
void deleteNodeLeafCase(RBTree *tree, RBNode *root);
void deleteRightNullNode(RBTree *tree, RBNode *root);
void deleteRightNoneNullNode(RBTree *tree, RBNode *root);
void deleteLeftNullNode(RBTree *tree, RBNode *root);
RotationType doubleBlackCaseRotationType(RBNode *grandParent, RBNode *parent, RBNode *grandSon);
void doubleBlackCasePerformRotation(RBTree *tree, RBNode *parent, RotationType rotationType);
void RBTreeToArrayRecurs(RBNode *node, void **arr, int *i);
void printRBTreeHelper(RBNode *root, int space, void (*printFun)(const void *item, COLOR color));
void *rbTreeGetR(RBTree *rbTree, RBNode *node, void *item);
int rBTreeContainsR(RBTree *tree, RBNode *root, void *item);
void rBInOrderTraversalR(RBNode *root, void (*printFun)(const void *, COLOR));
void rBPostOrderTraversalR(RBNode *root, void (*printFun)(const void *, COLOR));
void freeRBNode(RBNode *node, void (*freeItem)(void *item));
void freeRBTreeNodesR(RBNode *root, void (*freeItem)(void *item));
void *rBTreeDeleteRWtoFr(RBTree *tree, RBNode *root, void *item);
void *deleteNodeLeafCaseWtoFr(RBTree *tree, RBNode *root);
void *deleteRightNullNodeWtoFr(RBTree *tree, RBNode *root);
void *deleteLeftNullNodeWtoFr(RBTree *tree, RBNode *root);
void *deleteRightNoneNullNodeWtoFr(RBTree *tree, RBNode *root);
/** Initialization Function for Red Black Tree. A comparison and freeing items functions must be provided
* for the tree to be able to level its Nodes. Returns a reference to an allocated Red Black Tree pointer on the heap.
*
* @param freeItem Reference to the dGFreeValue function, that will be called to free the tree items
* @param cmp Reference to the comparator function.
* @return Pointer to the allocated Red Black tree on the heap.
* */
RBTree *redBlackTreeInitialization(void (*freeItem)(void *), int (*cmp)(const void *, const void *)) {
if (freeItem == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return NULL;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "free function pointer", "red black tree data structure");
exit(INVALID_ARG);
#endif
} else if (cmp == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return NULL;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "comparator function pointer", "red black tree data structure");
exit(INVALID_ARG);
#endif
}
RBTree *tree = (RBTree *) malloc(sizeof(RBTree));
if (tree == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = FAILED_ALLOCATION;
return NULL;
#else
fprintf(stderr, FAILED_ALLOCATION_MESSAGE, "tree", "red black tree data structure");
exit(FAILED_ALLOCATION);
#endif
}
tree->root = NULL;
tree->nodeCount = 0;
tree->freeItem = freeItem;
tree->cmp = cmp;
return tree;
}
/** Creates a new Red Black TreeNode for a given key, and parent and if required a certain color.
*
* @param parent Reference to the Red Black tree's root 's or insertion point's parent.
* @param color color of the node to be created.
* @param item Reference pointer to pre allocated key.
* @return Returns a pointer to an allocated node on the heap.
* */
RBNode *createRBNode(RBNode *parent, COLOR color, void *item) {
RBNode *node = (RBNode *) malloc(sizeof(RBNode));
node->color = color;
node->key = item;
node->right = node->left = NULL;
node->parent = parent;
return node;
}
/** This function will take the tree address as a parameter,
* then it will clear and free all the tree nodes without destroying the tree.
*
* @param tree a reference to the tree address
*/
void clearRBTree(RBTree *tree) {
if (tree == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "tree", "red black tree data structure");
exit(NULL_POINTER);
#endif
}
freeRBTreeNodesR(tree->root, tree->freeItem);
tree->root = NULL;
tree->nodeCount = 0;
}
/**This function will take the root node address, and the freeing item function address as a parameters,
* then recursively it will destroy and free all the tree nodes.
*
* @param root the address of the node
* @param freeItem dGFreeValue the freeing item function address
*/
void freeRBTreeNodesR(RBNode *root, void (*freeItem)(void *)) {
if (root == NULL)
return;
freeRBTreeNodesR(root->left, freeItem);
freeRBTreeNodesR(root->right, freeItem);
freeRBNode(root, freeItem);
}
/** Given a node it frees it's Key and the node.
*
* @param node Exact Reference for node to Free.
* @param freeItem the freeing item function address
**/
void freeRBNode(RBNode *node, void (*freeItem)(void *)) {
if (node == NULL)
return;
freeItem(node->key);
free(node);
}
/** Given an Red Black Tree's root node it frees it's elements recursively.Setting the root node to Null.
*
* @param node Exact Reference to root node to start freeing at.
**/
void destroyRBTree(void *tree) {
clearRBTree(tree);
free(tree);
}
/** Inserts a node at the a reference node (preferably the root) with the provided key and it's size.
*
* @param tree Reference to the Red Black tree.
* @param item reference pointer to pre allocated key.
**/
void rBTreeInsert(RBTree *tree, void *item) {
if (tree == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "tree", "red black tree data structure");
exit(NULL_POINTER);
#endif
} else if (item == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "item pointer", "red black tree data structure");
exit(INVALID_ARG);
#endif
}
tree->root = rBTreeInsertR(tree->root, NULL, item, tree->freeItem, tree->cmp);
tree->nodeCount++;
tree->root->color = BLACK;
}
/** Helper function to insert into the tree a node with given key item.
*
* @param root Reference to the Red Black tree's root or insertion point.
* @param parent Reference to the Red Black tree's root 's or insertion point's parent.
* @param item Reference pointer to pre allocated key.
* @param freeFun Reference pointer to the free item function
* @param cmp Reference to the comparator function.
* @return it will return the passed root to it's parent
**/
RBNode *rBTreeInsertR(RBNode *root, RBNode *parent, void *item, void (*freeFun)(void *),
int (*cmp)(const void *, const void *)) {
if (root == NULL) {
root = createRBNode(parent, parent == NULL ? BLACK : RED, item);
} else if (cmp(item, root->key) < 0)
root->left = rBTreeInsertR(root->left, root, item, freeFun, cmp);
else if (cmp(item, root->key) > 0 || cmp(item, root->key) == 0)
root->right = rBTreeInsertR(root->right, root, item, freeFun, cmp);
//Case 1:
if (isCaseOne(root)) {
root->parent->color = BLACK;
getUncle(root)->color = BLACK;
root->parent->parent->color = RED;
}
//Case 2:
if (isRotationRequired(root)) {
RotationType rotationType = getRotationType(root);
root = performRotation(root, rotationType);
performRotationRecoloring(root, rotationType);
}
return root;
}
/** if present Deletes a given Node of key item from the tree.
*
* @param tree Reference to the Red Black tree.
* @param item Reference pointer to pre allocated key.
**/
void rBTreeDelete(RBTree *tree, void *item) {
if (tree == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "tree", "red black tree data structure");
exit(NULL_POINTER);
#endif
} else if (item == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "item pointer", "red black tree data structure");
exit(INVALID_ARG);
#endif
}
rBTreeDeleteR(tree, tree->root, item);
}
/** Helper function to the @link rBTreeDelete @endlink function.
*
* @param tree Reference to the Red Black tree.
* @param root Reference to the Red Black tree's root or insertion point.
* @param item Reference to key to delete from the tree.
**/
void rBTreeDeleteR(RBTree *tree, RBNode *root, void *item) {
if (root == NULL)
return;
else if (tree->cmp(item, root->key) == 0) {
if (root->right == NULL && root->left == NULL)
deleteNodeLeafCase(tree, root);
else if (root->right == NULL)
deleteRightNullNode(tree, root);
else if (root->left == NULL)
deleteLeftNullNode(tree, root);
else
deleteRightNoneNullNode(tree, root);
} else if (tree->cmp(item, root->key) < 0)
rBTreeDeleteR(tree, root->left, item);
else if (tree->cmp(item, root->key) > 0)
rBTreeDeleteR(tree, root->right, item);
}
/** if the node to be deleted has no children this function handles it's deletion.
*
* @param tree Reference to the Red Black tree.
* @param root Reference to the Red Black tree's root or insertion point.
**/
void deleteNodeLeafCase(RBTree *tree, RBNode *root) {
COLOR rootColor = root->color;
RBNode *parent = root->parent;
tree->freeItem(root->key);
free(root);
if (parent == NULL)
tree->root = NULL;
else {
if (parent->right == root)
parent->right = NULL;
else
parent->left = NULL;
}
if (rootColor != RED)
doubleBlackCase(tree, NULL, parent);
tree->nodeCount--;
}
/** If the node has no right child this function handles it's deletion.
*
* @param tree Reference to the Red Black tree.
* @param root Reference to the Red Black tree's root or insertion point.
**/
void deleteRightNullNode(RBTree *tree, RBNode *root) {
RBNode *returnNode = root->left;
RBNode *parent = root->parent;
returnNode->parent = parent;
COLOR rootColor = root->color;
if (parent == NULL)
tree->root = root->left;
else {
if (parent->right == root)
parent->right = returnNode;
else
parent->left = returnNode;
}
tree->freeItem(root->key);
free(root);
//This case mean the current node is black and it's child is red colored.
if (returnNode->color == RED && rootColor == BLACK)
returnNode->color = BLACK;
//This case mean the deleted node is black and the replaced node is also black.
else if (returnNode->color == BLACK && rootColor == BLACK)
doubleBlackCase(tree, returnNode, returnNode->parent);
tree->nodeCount--;
}
/** If the node has no left child this function handles it's deletion.
*
* @param tree Reference to the Red Black tree.
* @param root Reference to the Red Black tree's root or insertion point.
**/
void deleteLeftNullNode(RBTree *tree, RBNode *root) {
RBNode *returnNode = root->right;
RBNode *parent = root->parent;
returnNode->parent = parent;
COLOR rootColor = root->color;
if (parent == NULL)
tree->root = root->right;
else {
if (parent->right == root)
parent->right = returnNode;
else
parent->left = returnNode;
}
tree->freeItem(root->key);
free(root);
//This case mean the current node is black and it's child is red colored.
if (returnNode->color == RED && rootColor == BLACK)
returnNode->color = BLACK;
//This case mean the deleted node is black and the replaced node is also black.
else if (returnNode->color == BLACK && rootColor == BLACK)
doubleBlackCase(tree, returnNode, returnNode->parent);
tree->nodeCount--;
}
/** If the node has both a right and a left child this function handles it's deletion.
*
* @param tree Reference to the Red Black tree.
* @param root Reference to the Red Black tree's root or insertion point.
**/
void deleteRightNoneNullNode(RBTree *tree, RBNode *root) {
RBNode *successorNode = getSuccessorNode(root);
void *temp = root->key;
root->key = successorNode->key;
successorNode->key = temp;
rBTreeDeleteR(tree, root->right, successorNode->key);
}
/** if present Deletes a given Node of key item from the tree without freeing it.
*
* @param tree Reference to the Red Black tree.
* @param item Reference pointer to pre allocated key.
* @return it will return the deleted item pointer
**/
void *rBTreeDeleteWtoFr(RBTree *tree, void *item) {
if (tree == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return NULL;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "tree", "red black tree data structure");
exit(NULL_POINTER);
#endif
} else if (item == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return NULL;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "item pointer", "red black tree data structure");
exit(INVALID_ARG);
#endif
}
return rBTreeDeleteRWtoFr(tree, tree->root, item);
}
/** Helper function to the @link rBTreeDeleteWtoFr @endlink function.
*
* @param tree Reference to the Red Black tree.
* @param root Reference to the Red Black tree's root or insertion point.
* @param item Reference to key to delete from the tree.
* @return it will return the deleted item pointer
**/
void *rBTreeDeleteRWtoFr(RBTree *tree, RBNode *root, void *item) {
if (root == NULL)
return NULL;
else if (tree->cmp(item, root->key) == 0) {
if (root->right == NULL && root->left == NULL)
return deleteNodeLeafCaseWtoFr(tree, root);
else if (root->right == NULL)
return deleteRightNullNodeWtoFr(tree, root);
else if (root->left == NULL)
return deleteLeftNullNodeWtoFr(tree, root);
else
return deleteRightNoneNullNodeWtoFr(tree, root);
} else if (tree->cmp(item, root->key) < 0)
return rBTreeDeleteRWtoFr(tree, root->left, item);
else if (tree->cmp(item, root->key) > 0)
return rBTreeDeleteRWtoFr(tree, root->right, item);
return NULL;
}
/** if the node to be deleted has no children this function handles it's deletion without freeing.
*
* @param tree Reference to the Red Black tree.
* @param root Reference to the Red Black tree's root or insertion point.
* @return it will return the deleted item pointer
**/
void *deleteNodeLeafCaseWtoFr(RBTree *tree, RBNode *root) {
COLOR rootColor = root->color;
RBNode *parent = root->parent;
void *returnValue = root->key;
free(root);
if (parent == NULL)
tree->root = NULL;
else {
if (parent->right == root)
parent->right = NULL;
else
parent->left = NULL;
}
if (rootColor != RED)
doubleBlackCase(tree, NULL, parent);
tree->nodeCount--;
return returnValue;
}
/** If the node has no right child this function handles it's deletion without freeing.
*
* @param tree Reference to the Red Black tree.
* @param root Reference to the Red Black tree's root or insertion point.
* @return it will return the deleted item pointer
**/
void *deleteRightNullNodeWtoFr(RBTree *tree, RBNode *root) {
RBNode *returnNode = root->left;
RBNode *parent = root->parent;
returnNode->parent = parent;
COLOR rootColor = root->color;
if (parent == NULL)
tree->root = root->left;
else {
if (parent->right == root)
parent->right = returnNode;
else
parent->left = returnNode;
}
void *returnValue = root->key;
free(root);
//This case mean the current node is black and it's child is red colored.
if (returnNode->color == RED && rootColor == BLACK)
returnNode->color = BLACK;
//This case mean the deleted node is black and the replaced node is also black.
else if (returnNode->color == BLACK && rootColor == BLACK)
doubleBlackCase(tree, returnNode, returnNode->parent);
tree->nodeCount--;
return returnValue;
}
/** If the node has no left child this function handles it's deletion without freeing.
*
* @param tree Reference to the Red Black tree.
* @param root Reference to the Red Black tree's root or insertion point.
* @return it will return the deleted item pointer
**/
void *deleteLeftNullNodeWtoFr(RBTree *tree, RBNode *root) {
RBNode *returnNode = root->right;
RBNode *parent = root->parent;
returnNode->parent = parent;
COLOR rootColor = root->color;
if (parent == NULL)
tree->root = root->right;
else {
if (parent->right == root)
parent->right = returnNode;
else
parent->left = returnNode;
}
void *returnItem = root->key;
free(root);
//This case mean the current node is black and it's child is red colored.
if (returnNode->color == RED && rootColor == BLACK)
returnNode->color = BLACK;
//This case mean the deleted node is black and the replaced node is also black.
else if (returnNode->color == BLACK && rootColor == BLACK)
doubleBlackCase(tree, returnNode, returnNode->parent);
tree->nodeCount--;
return returnItem;
}
/** If the node has both a right and a left child this function handles it's deletion without freeing.
*
* @param tree Reference to the Red Black tree.
* @param root Reference to the Red Black tree's root or insertion point.
* @return it will return the deleted item pointer
**/
void *deleteRightNoneNullNodeWtoFr(RBTree *tree, RBNode *root) {
RBNode *successorNode = getSuccessorNode(root);
void *temp = root->key;
root->key = successorNode->key;
successorNode->key = temp;
return rBTreeDeleteRWtoFr(tree, root->right, successorNode->key);
}
/** Handles cases where a double black node is caused by deletion.
*
* @param tree Reference to the Red Black tree.
* @param root Reference to the Red Black tree's root or insertion point.
* @param parent Reference to the Red Black tree's root 's or insertion point's parent.
**/
void doubleBlackCase(RBTree *tree, RBNode *root, RBNode *parent) {
if (parent == NULL)
return;
RBNode *sibling = parent->right == root ? parent->left : parent->right;
if (sibling->color == BLACK && (getNodeColor(sibling->right) == RED || getNodeColor(sibling->left) == RED)) {
RBNode *sRedChild = getNodeColor(sibling->right) == RED ? sibling->right : sibling->left;
RotationType rotationType = doubleBlackCaseRotationType(sibling->parent, sibling, sRedChild);
sRedChild->color = parent->color;
parent->color = BLACK;
doubleBlackCasePerformRotation(tree, parent, rotationType);
} else if (sibling->color == RED) {
RotationType rotationType = parent->right == sibling ? LEFT : RIGHT;
sibling->color = BLACK;
doubleBlackCasePerformRotation(tree, parent, rotationType);
if (rotationType == LEFT && parent->right != NULL)
parent->right->color = RED;
else if (rotationType == RIGHT && parent->left != NULL)
parent->left->color = RED;
} else if (sibling->color == BLACK) {
sibling->color = RED;
if (parent->color == BLACK)
doubleBlackCase(tree, parent, parent->parent);
else
parent->color = BLACK;
}
}
/** Handles the rotation for cases where a double black node is caused by deletion.
*
* @param tree Reference to the Red Black tree.
* @param parent Reference to the Red Black tree's root 's or insertion point's parent.
* @param rotationType Type of rotation to perform.
**/
void doubleBlackCasePerformRotation(RBTree *tree, RBNode *parent, RotationType rotationType) {
RBNode *grandParent = parent->parent;
if (grandParent == NULL) {
tree->root = performRotation(parent, rotationType);
} else if (grandParent->right == parent) {
grandParent->right = performRotation(parent, rotationType);
} else if (grandParent->left == parent) {
grandParent->left = performRotation(parent, rotationType);
}
}
/** Gets the rotation type to fix the double black case.
*
* @param grandParent Reference to the Red Black tree's root 's or point's grandparent.
* @param parent Reference to the Red Black tree's root 's or insertion point's parent.
* @param grandSon Reference to the grandson relative to the grandparent .
* @return Returns Rotation type of values from the @link RotationType @endlink enum.
**/
RotationType doubleBlackCaseRotationType(RBNode *grandParent, RBNode *parent, RBNode *grandSon) {
if (grandParent->left == parent && parent->left == grandSon) {
return RIGHT;
} else if (grandParent->right == parent && parent->right == grandSon) {
return LEFT;
} else if (grandParent->left == parent && parent->right == grandSon) {
return LEFT_RIGHT;
} else if (grandParent->right == parent && parent->left == grandSon) {
return RIGHT_LEFT;
} else {
return NONE;
}
}
/** Given a reference node it returns a reference to it's successor.
*
* @param node Reference node to get the in-order successor for.
* @return Pointer to the inorder successor for that node.
**/
RBNode *getSuccessorNode(RBNode *node) {
RBNode *currentNode = node->right;
if (currentNode == NULL)
return node;
while (currentNode->left != NULL)
currentNode = currentNode->left;
return currentNode;
}
/** if a node is red and it's parent and uncle and red, the function returns 1 ie true else 0, false.
*
* @param root Reference to the Red Black tree's root or insertion point.
* @return 1 is the current node matches the description else 0.
**/
int isCaseOne(RBNode *root) {
return root->color == RED
&& getNodeColor(root->parent) == RED
&& root->parent->parent != NULL
&& getNodeColor(getUncle(root)) == RED;
}
/** If the parent and the uncle aren't of the same color (ie one of them is red) whose children
* are also red then a rotation is also required.
*
* @param grandParent Reference to the Red Black tree's root 's or point's grandparent.
* @return
**/
int isRotationRequired(RBNode *grandParent) {
if (getNodeColor(grandParent->right) != getNodeColor(grandParent->left)) {
RBNode *redNode = getNodeColor(grandParent->right) == RED ? grandParent->right : grandParent->left;
if (getNodeColor(redNode->right) == RED || getNodeColor(redNode->left) == RED)
return 1;
else
return 0;
} else
return 0;
}
/** Given a reference to a grandparent node determine what rotation to perform.
*
* @param grandParent Reference to the Red Black tree's root 's or point's grandparent.
* @return Rotation to perform of values from the @link RotationType enum.
**/
RotationType getRotationType(RBNode *grandParent) {
RBNode *redChild = getNodeColor(grandParent->right) == RED ? grandParent->right : grandParent->left;
RBNode *redGrandSon = getNodeColor(redChild->right) == RED ? redChild->right : redChild->left;
if (grandParent->right == redChild && redChild->right == redGrandSon)
return LEFT;
else if (grandParent->left == redChild && redChild->left == redGrandSon)
return RIGHT;
else if (grandParent->left == redChild && redChild->right == redGrandSon)
return LEFT_RIGHT;
else if (grandParent->right == redChild && redChild->left == redGrandSon)
return RIGHT_LEFT;
else
return NONE;
}
/** Given a rotation type and a node to rotate about,the subtree of the node is rotated with respect tot he rotation type.
*
* @param node Reference to the Red Black tree's root or reference node to rotated at.
* @param rotationType Type of rotation to perform.
* @return Returns the newly rotated subtree
**/
RBNode *performRotation(RBNode *node, RotationType rotationType) {
RBNode *newRoot;
switch (rotationType) {
case LEFT:
newRoot = leftRotation(node);
return newRoot;
case RIGHT:
newRoot = rightRotation(node);
return newRoot;
case LEFT_RIGHT:
node->left = leftRotation(node->left);
newRoot = rightRotation(node);
return newRoot;
case RIGHT_LEFT:
node->right = rightRotation(node->right);
newRoot = leftRotation(node);
return newRoot;
case NONE:
return node;
}
return node;
}
/** Recolours nodes after rotation to maintain the reb black tree property.
*
* @param root Reference to the Red Black tree's root or insertion point.
* @param rotationType Type of rotation to perform.
**/
void performRotationRecoloring(RBNode *root, RotationType rotationType) {
switch (rotationType) {
case LEFT:
root->color = BLACK;
root->left->color = RED;
break;
case RIGHT:
root->color = BLACK;
root->right->color = RED;
break;
case LEFT_RIGHT:
root->color = BLACK;
root->right->color = RED;
break;
case RIGHT_LEFT:
root->color = BLACK;
root->left->color = RED;
break;
}
}
/** Given a grandparent node it performs left rotation at it.
*
* @param grandParent Reference to the Red Black tree's root 's or point's grandparent.
* @return returns pointer to the newly left rotated subtree.
**/
RBNode *leftRotation(RBNode *grandParent) {
RBNode *newGrandParent = grandParent->right;
grandParent->right = newGrandParent->left;
newGrandParent->left = grandParent;
newGrandParent->parent = grandParent->parent;
grandParent->parent = newGrandParent;
if (grandParent->right != NULL)
grandParent->right->parent = grandParent;
return newGrandParent;
}
/** Given a grandparent node it performs right rotation at it.
*
* @param grandParent Reference to the Red Black tree's root 's or point's grandparent.
* @return returns pointer to the newly right rotated subtree.
**/
RBNode *rightRotation(RBNode *grandParent) {
RBNode *newGrandParent = grandParent->left;
grandParent->left = newGrandParent->right;
newGrandParent->right = grandParent;
newGrandParent->parent = grandParent->parent;
grandParent->parent = newGrandParent;
if (grandParent->left != NULL)
grandParent->left->parent = grandParent;
return newGrandParent;
}
/** Proved with a node it gets it's returns a pointer to it's uncle.
*
* @param currentNode Reference node to get the uncle at.
* @return Pointer to the uncle of the given node.
**/
RBNode *getUncle(RBNode *currentNode) {
RBNode *parent = currentNode->parent;
RBNode *grandParent = parent->parent;
return grandParent->right == parent ? grandParent->left : grandParent->right;
}
/** Returns the Node's Color, if the node is null it is considered black else returns the actual node's color.
*
* @param node Node to get the color at
* @return Color information about the given node.
**/
COLOR getNodeColor(RBNode *node) {
return node == NULL ? BLACK : node->color;
}
/** Prints the Tree PreOrder traversing it recursively.
*
* @param tree Reference to the Red Black tree.
* @param printFun Pointer to the print function.
**/
void rBPreOrderTraversal(RBTree *tree, void (*printFun)(const void *, COLOR)) {
if (tree == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = NULL_POINTER;
return;
#else
fprintf(stderr, NULL_POINTER_MESSAGE, "tree", "red black tree data structure");
exit(NULL_POINTER);
#endif
} else if (printFun == NULL) {
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
ERROR_TEST->errorCode = INVALID_ARG;
return;
#else
fprintf(stderr, INVALID_ARG_MESSAGE, "print function pointer", "red black tree data structure");
exit(INVALID_ARG);
#endif
}
rBPreOrderTraversalR(tree->root, printFun);
}
/** Helper function for @link rBPreOrderTraversal @endlink printing tree traversing it in order. Sends a pointer ot the node directly.
*
* @param root Reference to the Red Black tree's root or insertion point.Reference to the root node.
* @param printFun Pointer to the print function.
**/
void rBPreOrderTraversalR(RBNode *root, void (*printFun)(const void *, COLOR)) {
if (root == NULL)
return;
printFun(root->key, root->color);
rBPreOrderTraversalR(root->left, printFun);
rBPreOrderTraversalR(root->right, printFun);
}
/** Prints the Tree inOrder traversing it recursively.
*
* @param tree Reference to the Red Black tree.
* @param printFun Pointer to the print function.
**/