-
Notifications
You must be signed in to change notification settings - Fork 2
/
RedBlackTree.cpp
executable file
·443 lines (396 loc) · 13.3 KB
/
RedBlackTree.cpp
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
#include <iostream>
class Node{
public:
bool isRed;
int key;
Node *parent;
Node *left;
Node *right;
Node(int key);
};
Node::Node(int key) {
this->key = key;
this->parent = nullptr;
this->left = nullptr;
this->right = nullptr;
this->isRed = false;
}
class RBTree{
public:
Node *root;
// Necessary nodes for delete fix(So we have no memory leak)
Node *replacingNode;
Node *siblingNode;
void RotateLeft(Node *node);
void RotateRight(Node *node);
void Insert(Node *node);
void InsertFix(Node *node);
void Transplant(Node *oldNode, Node *newNode);
void Delete(Node *node);
void DeleteFix(Node *node, bool rep);
void InOrderWalk(Node *node);
Node* TreeMinimum(Node *begin);
RBTree();
};
void RBTree::RotateLeft(Node *node) {
/*
* This function rotates the given node to left.
* Right child of the given node will become given node's parent.
* Given node will be the left child of its right child.
*/
if (node->right){
Node *temp = node->right;
node->right = temp->left;
if (temp->left != nullptr){
temp->left->parent = node;
}
temp->parent = node->parent;
if (node->parent == nullptr){
root = temp;
} else if (node == node->parent->left){
node->parent->left = temp;
} else {
node->parent->right = temp;
}
temp->left = node;
node->parent = temp;
} else {
std::cerr << "You cannot left rotate if there is no right child!" << std::endl;
}
}
void RBTree::RotateRight(Node *node) {
/*
* This function rotates the given node to right.
* Given node's left child become given node's parent.
* Given node becomes left child's right child.
*/
if (node->left){
Node *temp = node->left;
node->left = temp->right;
if (temp->right != nullptr){
temp->right->parent = node;
}
temp->parent = node->parent;
if (node->parent == nullptr){
root = temp;
} else if (node == node->parent->left){
node->parent->left = temp;
} else {
node->parent->right = temp;
}
temp->right = node;
node->parent = temp;
} else {
std::cerr << "You cannot right rotate if there is no left child!" << std::endl;
}
}
void RBTree::Insert(Node *node) {
/*
* This function inserts a new node into tree and colors it red.
* It preserves binary search tree properties, however to ensure
* that we are not violating any properties of red-black trees,
* we call another function called InsertFix that fixes any coloring
* issues.
*/
Node *temp = nullptr;
Node *traverse = this->root;
while (traverse){
temp = traverse;
if (node->key < traverse->key){
traverse = traverse->left;
} else traverse = traverse->right;
}
node->parent = temp;
if (temp == nullptr){
this->root = node;
} else if (node->key < temp->key){
temp->left = node;
} else {
temp->right = node;
}
node->left = nullptr;
node->right = nullptr;
node->isRed = true;
InsertFix(node);
}
void RBTree::InsertFix(Node *node) {
/*
* This function fixes coloring issues of the red-black tree
* in an iterative fashion. If node's parent is black, there
* are no coloring problems because black height does not change.
*
* However, if parent of the newly inserted node is red we are
* violating the red-black tree properties(red nodes do not have
* red children).
*/
while (node->parent && node->parent->isRed){
if (node->parent->parent && (node->parent == node->parent->parent->left)){
Node *uncle = node->parent->parent->right;
if (uncle && uncle->isRed){
node->parent->isRed = false;
uncle->isRed = false;
node->parent->parent->isRed = true;
node = node->parent->parent;
} else {
if (node == node->parent->right){
node = node->parent;
RotateLeft(node);
}
node->parent->isRed = false;
node->parent->parent->isRed = true;
RotateRight(node->parent->parent);
}
} else {
if (!node->parent->parent) break;
Node *uncle = node->parent->parent->left;
if (uncle && uncle->isRed){
node->parent->isRed = false;
uncle->isRed = false;
node->parent->parent->isRed = true;
node = node->parent->parent;
} else {
if (node == node->parent->left){
node = node->parent;
RotateRight(node);
}
node->parent->isRed = false;
node->parent->parent->isRed = true;
RotateLeft(node->parent->parent);
}
}
}
this->root->isRed = false;
}
void RBTree::Transplant(Node *oldNode, Node *newNode) {
/*
* This function transplants subtrees. Subtree with root newNode
* will replace the subtree with root oldNode. Information about oldNode
* is deleted after this operation.
*/
if (oldNode == root){
root = newNode;
} else if (oldNode == oldNode->parent->left){
oldNode->parent->left = newNode;
} else {
oldNode->parent->right = newNode;
}
if (newNode != nullptr) {
newNode->parent = oldNode->parent;
}
}
void RBTree::Delete(Node *node) {
/*
* This function deletes the given node from the tree.
*
* temp either represents the node that is going to be deleted
* or the node that is going to replace the deleted node.
*
* replace represents the node that is going to replace temp.
*
* After doing the operations to make sure wo do not violate
* binary search tree properties, we call DeleteFix if deleted node
* or node that replaced the deleted node was originally black.
* Otherwise, we might have different number of black nodes in different
* paths that lead to leaves which would violate the red-black tree
* properties.
*/
Node *temp = node;
Node *replace = nullptr;
bool originalIsRed = temp->isRed;
bool rep = false;
if (node->left == nullptr){
replace = node->right;
if (!replace){
if (node == root) {
root = nullptr;
return;
}
rep = true;
// If there is no replacing node, we have to create an artificial one
replace = new Node(0);
this->replacingNode = replace;
replace->isRed = false;
replace->parent = node;
replace->parent->right = replace;
}
Transplant(node, node->right);
} else if (node->right == nullptr){
replace = node->left; // Since we check the left child first, there is no need to create an artificial node
Transplant(node, node->left);
} else {
temp = TreeMinimum(node->right);
originalIsRed = temp->isRed;
replace = temp->right;
if (!replace){
rep = true;
// If there is no replacing node, we have to create an artificial one
replace = new Node(0);
this->replacingNode = replace;
replace->isRed = false;
replace->parent = temp;
replace->parent->right = replace;
}
if (temp->parent == node){
replace->parent = temp;
} else {
Transplant(temp, temp->right);
temp->right = node->right;
temp->right->parent = temp;
}
Transplant(node, temp);
temp->left = node->left;
temp->left->parent = temp;
temp->isRed = node->isRed;
}
if(!(originalIsRed))
DeleteFix(replace, rep);
if (rep){
rep = false;
if(!replacingNode->parent){
delete replacingNode;
} else if (replacingNode->parent->left == replacingNode){
replacingNode->parent->left = nullptr;
delete replacingNode;
} else if (replacingNode->parent->right == replacingNode) {
replacingNode->parent->right = nullptr;
delete replacingNode;
}
}
}
void RBTree::DeleteFix(Node *node, bool rep) {
Node *sibling = nullptr;
bool siblingChange = false;
bool replaceChange = rep;
while (node != root && !node->isRed){
if (node == node->parent->left){
sibling = node->parent->right;
if (!sibling){
siblingChange = true;
sibling = new Node(0);
this->siblingNode = sibling;
sibling->parent = node->parent;
sibling->isRed = false;
node->parent->right = sibling;
}
if (sibling->isRed){
sibling->isRed = false;
node->parent->isRed = true;
RotateLeft(node->parent);
sibling = node->parent->right;
}
if ((!sibling->left || !sibling->left->isRed) && (!sibling->right || !sibling->right->isRed)){
sibling->isRed = true;
node = node->parent;
} else {
if (!sibling->right || !sibling->right->isRed){
if (sibling->left) sibling->left->isRed = false;
sibling->isRed = true;
RotateRight(sibling);
sibling = node->parent->right;
}
sibling->isRed = node->parent->isRed;
node->parent->isRed = false;
sibling->right->isRed = false;
RotateLeft(node->parent);
node = root;
}
} else {
sibling = node->parent->left;
if (!sibling){
siblingChange = true;
sibling = new Node(0);
this->siblingNode = sibling;
sibling->parent = node->parent;
sibling->isRed = false;
node->parent->left = sibling;
}
if (sibling->isRed){
sibling->isRed = false;
node->parent->isRed = true;
RotateRight(node->parent);
sibling = node->parent->left;
}
if ((!sibling->right || !sibling->right->isRed) && (!sibling->left || !sibling->left->isRed)){
sibling->isRed = true;
node = node->parent;
} else {
if (!sibling->left || !sibling->left->isRed) {
if (sibling->right) sibling->right->isRed = false;
sibling->isRed = true;
RotateLeft(sibling);
sibling = node->parent->left;
}
sibling->isRed = node->parent->isRed;
node->parent->isRed = false;
sibling->left->isRed = false;
RotateRight(node->parent);
node = root;
}
}
if (replaceChange){
replaceChange = false;
if(!replacingNode->parent){
delete replacingNode;
} else if (replacingNode->parent->left == replacingNode){
replacingNode->parent->left = nullptr;
delete replacingNode;
} else if (replacingNode->parent->right == replacingNode) {
replacingNode->parent->right = nullptr;
delete replacingNode;
}
}
if (siblingChange){
siblingChange = false;
if(!siblingNode->parent){
delete siblingNode;
} else if (siblingNode->parent->left == siblingNode){
siblingNode->parent->left = nullptr;
delete siblingNode;
} else if (siblingNode->parent->right == siblingNode){
siblingNode->parent->right = nullptr;
delete siblingNode;
}
}
}
if (node) node->isRed = false;
}
void RBTree::InOrderWalk(Node *node) {
if (!node) std::cout << "nil" << std::endl;
if(node != nullptr) {
std::cout << node->key << "'s left: ";
InOrderWalk(node->left);
std::cout << node->key << "\t" << (node->isRed ? "Red" : "Black") << std::endl;
std::cout << node->key << "'s right: ";
InOrderWalk(node->right);
}
}
Node *RBTree::TreeMinimum(Node *begin) {
Node *retNode = begin;
Node *traverse = begin;
while(traverse){
retNode = traverse;
traverse = traverse->left;
}
return retNode;
}
RBTree::RBTree() {
root = nullptr;
}
int main() {
RBTree tree;
Node *arr[15] = {new Node(974), new Node(834), new Node(204), new Node(463), new Node(332),
new Node(293), new Node(706), new Node(126), new Node(980), new Node(630),
new Node(718), new Node(760), new Node(76), new Node(607), new Node(805)};
for(int i = 0; i < 15; i++){
tree.Insert(arr[i]);
}
std::cout << "Tree:\n";
tree.Delete(arr[3]);
tree.Delete(arr[1]);
tree.Delete(arr[11]);
tree.InOrderWalk(tree.root);
for(int i = 0; i < 15; i++){
delete arr[i];
}
return 0;
}