-
Notifications
You must be signed in to change notification settings - Fork 0
/
RBTree.h
568 lines (485 loc) · 13.5 KB
/
RBTree.h
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
#pragma once
#include<iostream>
#include"Others.h"
/*
RB Tree Manual:
Rules:
-> The root will always be Black
-> There are no Red Red parent child
-> Number of Black nodes on each path are same (all over the tree)
Insertion:
-> If tree is emnewNodey then create new node as root node with color Black
-> If tree is not emnewNodey then insert newnode with color Red
-> If the parent of newnode is Black then return
-> If the parent of newnode is Red then check the parent's sibling's color
-> If the Parent_Sibling of newnode is Black or Null then make suitable rotation LL or RR
-> If the Parent_Sibling of newnode is Red the Recolor the newnode and parent node
Deletion:
-> Find the node to be deleted using binary search tree traversal
-> If node to be deleted has 2 Non-Null children, replace it with it's inorder successor
-> If node to be deleted is re then just delete it
-> If node to be deleted is black but has one red child replace it with that child and change color of child to black
-> Otherwise Double Black situation comes and we see the 6 cases
*/
enum COLOR { RED, BLACK };
template<typename T>
class RBnode {
public:
Value<T> val;
COLOR color;
RBnode<T>* leftChild;
RBnode<T>* rightChild;
RBnode<T>* parent;
RBnode(Value<T> val) : val(val) {
parent = leftChild = rightChild = NULL;
// RBnode is red at insertion
color = RED;
}
//Checking which child of the parent is the current node
bool Is_left_child() {
return this == parent->leftChild;
}
// returns pointer to Parent_Sibling
RBnode<T>* Parent_Sibling() {
// If no parent or grandparent, then no Parent_Sibling
if (parent == NULL || parent->parent == NULL)
return NULL;
if (this == parent->leftChild)
// Parent_Sibling on rightChild
return parent->parent->rightChild;
else
// Parent_Sibling on leftChild
return parent->parent->leftChild;
}
// returns pointer to sibling
RBnode<T>* sibling() {
// sibling null if no parent
if (parent == NULL)
return NULL;
if (Is_left_child())
return parent->rightChild;
return parent->leftChild;
}
// moves node down and moves given node in its place
void moveDown(RBnode<T>* Num_parent) {
if (parent != NULL) {
if (Is_left_child()) {
parent->leftChild = Num_parent;
}
else {
parent->rightChild = Num_parent;
}
}
Num_parent->parent = parent;
parent = Num_parent;
}
bool Is_Red_child() {
return (leftChild != NULL and leftChild->color == RED) || (rightChild != NULL and rightChild->color == RED);
}
};
template<typename T>
class RBTree {
RBnode<T>* root;
// leftChild rotates the given node
void left_rotate(RBnode<T>* node) {
// new parent will be node's rightChild child
RBnode<T>* Num_parent = node->rightChild;
// update root if current node is root
if (node == root)
root = Num_parent;
node->moveDown(Num_parent);
// connect node with new parent's leftChild element
node->rightChild = Num_parent->leftChild;
// connect new parent's leftChild element with node
// if it is not null
if (Num_parent->leftChild != NULL)
Num_parent->leftChild->parent = node;
// connect new parent with node
Num_parent->leftChild = node;
}
void rightRotate(RBnode<T>* node) {
// new parent will be node's leftChild child
RBnode<T>* Num_parent = node->leftChild;
// update root if current node is root
if (node == root)
root = Num_parent;
node->moveDown(Num_parent);
// connect node with new parent's rightChild element
node->leftChild = Num_parent->rightChild;
// connect new parent's rightChild element with node
// if it is not null
if (Num_parent->rightChild != NULL)
Num_parent->rightChild->parent = node;
// connect new parent with node
Num_parent->rightChild = node;
}
void swap_color(RBnode<T>* node1, RBnode<T>* node2) {
COLOR temp;
temp = node1->color;
node1->color = node2->color;
node2->color = temp;
}
void swap_value(RBnode<T>* node1, RBnode<T>* node2) {
Value<T> temp;
temp = node1->val;
node1->val = node2->val;
node2->val = temp;
}
// fix red red at given node
void Red_Red_relation(RBnode<T>* node) {
// if node is root color it black and return
if (node == root) {
node->color = BLACK;
return;
}
// initialize parent, grandparent, Parent_Sibling
RBnode<T>* parent = node->parent;
RBnode<T>* grandparent = parent->parent;
RBnode<T>* Parent_Sibling = node->Parent_Sibling(); //Uncle
if (parent->color != BLACK) {
if (Parent_Sibling != NULL && Parent_Sibling->color == RED) {
// Parent_Sibling red, perform recoloring and recurse
parent->color = BLACK;
Parent_Sibling->color = BLACK;
grandparent->color = RED;
Red_Red_relation(grandparent);
}
else {
// Else perform LR, LL, RL, RR
if (parent->Is_left_child()) {
if (node->Is_left_child()) {
// for leftChild rightChild
swap_color(parent, grandparent);
}
else {
left_rotate(parent);
swap_color(node, grandparent);
}
// for leftChild leftChild and leftChild rightChild
rightRotate(grandparent);
}
else {
if (node->Is_left_child()) {
// for rightChild leftChild
rightRotate(parent);
swap_color(node, grandparent);
}
else {
swap_color(parent, grandparent);
}
// for rightChild rightChild and rightChild leftChild
left_rotate(grandparent);
}
}
}
}
// find node that do not have a leftChild child
// in the subtree of the given node
RBnode<T>* successor(RBnode<T>* node) {
RBnode<T>* temp = node;
while (temp->leftChild != NULL)
temp = temp->leftChild;
return temp;
}
// find node that replaces a deleted node in BST
RBnode<T>* ReplaceNode(RBnode<T>* val) {
// when node have 2 children
if (val->leftChild != NULL and val->rightChild != NULL)
return successor(val->rightChild);
// when leaf
if (val->leftChild == NULL and val->rightChild == NULL)
return NULL;
// when single child
if (val->leftChild != NULL)
return val->leftChild;
else
return val->rightChild;
}
// deletes the given node
void deleteNode(RBnode<T>* node) {
RBnode<T>* temp = ReplaceNode(node);
// True when temp and node are both black
bool Is_both_black = ((temp == NULL or temp->color == BLACK) and (node->color == BLACK));
RBnode<T>* parent = node->parent;
if (temp == NULL) {
// temp is NULL therefore node is leaf
if (node == root) {
// node is root, making root null
root = NULL;
}
else {
if (Is_both_black) {
// temp and node both black
// node is leaf, fix double black at node
Double_Black(node);
}
else {
// temp or node is red
if (node->sibling() != NULL)
// sibling is not null, make it red"
node->sibling()->color = RED;
}
// delete node from the tree
if (node->Is_left_child()) {
parent->leftChild = NULL;
}
else {
parent->rightChild = NULL;
}
}
delete node;
return;
}
if (node->leftChild == NULL or node->rightChild == NULL) {
// node has 1 child
if (node == root) {
// node is root, assign the value of temp to node, and delete temp
node->val = temp->val;
node->leftChild = node->rightChild = NULL;
delete temp;
}
else {
// Detach node from tree and move temp up
if (node->Is_left_child()) {
parent->leftChild = temp;
}
else {
parent->rightChild = temp;
}
delete node;
temp->parent = parent;
if (Is_both_black) {
// temp and node both black, fix double black at temp
Double_Black(temp);
}
else {
// temp or node red, color temp black
temp->color = BLACK;
}
}
return;
}
// node has 2 children, swap values with successor and recurse
swap_value(temp, node);
deleteNode(temp);
}
void Double_Black(RBnode<T>* node) {
if (node == root)
// Reached root
return;
RBnode<T>* sibling = node->sibling();
RBnode<T>* parent = node->parent;
if (sibling == NULL) {
// No sibiling, double black pushed up
Double_Black(parent);
}
else {
if (sibling->color == RED) {
// Sibling red
parent->color = RED;
sibling->color = BLACK;
if (sibling->Is_left_child()) {
// leftChild case
rightRotate(parent);
}
else {
// rightChild case
left_rotate(parent);
}
Double_Black(node);
}
else {
// Sibling black
if (sibling->Is_Red_child()) {
// at least 1 red children
if (sibling->leftChild != NULL and sibling->leftChild->color == RED) {
if (sibling->Is_left_child()) {
// leftChild leftChild
sibling->leftChild->color = sibling->color;
sibling->color = parent->color;
rightRotate(parent);
}
else {
// rightChild leftChild
sibling->leftChild->color = parent->color;
rightRotate(sibling);
left_rotate(parent);
}
}
else {
if (sibling->Is_left_child()) {
// leftChild rightChild
sibling->rightChild->color = parent->color;
left_rotate(sibling);
rightRotate(parent);
}
else {
// rightChild rightChild
sibling->rightChild->color = sibling->color;
sibling->color = parent->color;
left_rotate(parent);
}
}
parent->color = BLACK;
}
else {
// 2 black children
sibling->color = RED;
if (parent->color == BLACK)
Double_Black(parent);
else
parent->color = BLACK;
}
}
}
}
// prints level order for given node
void levelOrder(RBnode<T>* node) {
if (node == NULL)
// return if node is null
return;
// queue for level order
Queue<RBnode<T>*> q;
RBnode<T>* curr;
// push x
q.push(node);
while (!q.empty()) {
// while q is not empty
// dequeue
curr = q.front();
q.pop();
// print node value
cout << curr->val << " ";
// push children to queue
if (curr->leftChild != NULL)
q.push(curr->leftChild);
if (curr->rightChild != NULL)
q.push(curr->rightChild);
}
}
// prints inorder recursively
void inorder(RBnode<T>* Val) {
if (Val == NULL)
return;
inorder(Val->leftChild);
cout << Val->val.tuple << " ";
inorder(Val->rightChild);
}
public:
// constructor
// initialize root
RBTree() { root = NULL; }
RBnode<T>* getRoot() { return root; }
// searches for given value
// if found returns the node (used for delete)
// else returns the last node while traversing (used in insert)
RBnode<T>* search(Value<T> val) {
RBnode<T>* temp = root;
while (temp != NULL) {
if (val < temp->val) {
if (temp->leftChild == NULL)
break;
else
temp = temp->leftChild;
}
else if (val == temp->val) {
break;
}
else {
if (temp->rightChild == NULL)
break;
else
temp = temp->rightChild;
}
}
return temp;
}
// inserts the given value to tree
void insert(Value<T> val) {
RBnode<T>* newNode = new RBnode<T>(val);
if (root == NULL) {
// when root is null simply insert value at root
newNode->color = BLACK;
root = newNode;
}
else {
RBnode<T>* temp = search(val);
if (temp->val == val) {
// return if value already exists and record the duplicate of the val in the node
root->val.duplicates(val.Entries,val.fileName, val.lineNumber, val.tuple);
return;
}
// if value is not found, search returns the node
// where the value is to be inserted
// connect new node to correct node
newNode->parent = temp;
if (val < temp->val)
temp->leftChild = newNode;
else
temp->rightChild = newNode;
// fix red red voilaton if exists
Red_Red_relation(newNode);
}
}
// utility function that deletes the node with given value
void deleteByVal(Value<T> val) {
// Tree is empty
if (root == NULL)
return;
RBnode<T>* node = search(val);
if (node->val != val) {
cout << "No node found to delete with value:" << val.tuple << endl;
return;
}
deleteNode(node);
}
// prints inorder of the tree
void printInOrder() {
cout << "Inorder: " << endl;
if (root == NULL)
cout << "Tree is empty" << endl;
else
inorder(root);
cout << endl;
}
// prints level order of the tree
void printLevelOrder() {
cout << "Level order: " << endl;
if (root == NULL)
cout << "Tree is empty" << endl;
else
levelOrder(root);
cout << endl;
}
};
// // Driver Code
// int main()
// {
// RBTree<int> tree;
// Value<int> val;
// val.insert("22", 22, 22);
// tree.insert(val);
// // val.insert("22", 22, 7);
// // tree.insert(val);
// val.insert("22", 22, 6);
// tree.insert(val);
// val.insert("22", 22, 5);
// tree.insert(val);
// // val.insert("22", 22, 4);
// // tree.insert(val);
// tree.deleteByVal(val);
// tree.printInOrder();
// // val.insert("22", 22, 2);
// // tree.insert(val);
// // val.insert("22", 22, 1);
// // tree.insert(val);
// // tree.printInOrder();
// // cout << "Inorder Traversal of Created Tree\n";
// // tree.printInOrder();
// // val.insert("22",22,3);
// // tree.deleteByVal(val);
// // val.insert("22",22,5);
// // tree.deleteByVal(val);
// // tree.printInOrder();
// return 0;
// }