-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrb.cpp
521 lines (454 loc) · 10.1 KB
/
rb.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
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
#include<iostream>
using namespace std;
#include <functional>
// Template
// data structure that represents a node in the tree
struct Node {
int data; // holds the key
Node *parent; // pointer to the parent
Node *_left; // pointer to left child
Node *_right; // pointer to right child
int color; // 1 -> Red, 0 -> Black
};
typedef Node *NodePtr;
template< class T>
class const_iterator {
private:
Node* _pNode;
// RBTree<T,CMP> _t;
public:
typedef const_iterator Self; //Type of forward iterator
const_iterator(Node* n=nullptr)
:_pNode(n)// Constructors // we construct an iterator through the pointer of a node
{
}
Self& begin() const {return _pNode;}
// When the iterator dereferences, we return a reference to the node data
const T& operator*() const {
return _pNode->data;
}
//When the iterator performs the - > operation, we return the pointer of the node data
const T* operator->() const {
return &(_pNode->data);
}
Self& operator++() {
Increment();
return *this;
}
Self& operator++(int) {
Self tmp = this;
Increment();
return tmp;
}
// implement = = and != To determine whether the node is the same
bool operator==(const Self& s) {
return _pNode == s._pNode;
}
bool operator!=(const Self& s) {
return _pNode != s._pNode;
}
// begin and end of iterators in red black tree:
//begin returns the iterator of the first node in the middle order, which is the left most node
//end returns the iterator at the next position of the last node in the middle order. Here, use a null pointer.
private:
void Increment() {
if (_pNode->_right) {
Node* temp = _pNode->_right;
while (temp->_left) {
temp = temp->_left;
}
_pNode = temp;
}
else
{
Node* tmp = _pNode->parent;
if (tmp->_right==_pNode) {
while (_pNode==tmp->_right)
{
_pNode = tmp;
tmp = tmp->parent;
}
}
if (_pNode->_right != tmp)
_pNode = tmp;
}
}
};
template< class T, typename CMP>
// class RBTree implements the operations in Red Black Tree
class RBTree {
private:
NodePtr root;
NodePtr TNULL;
// initializes the nodes with appropirate values
// all the pointers are set to point to the null pointer
void initializeNULLNode(NodePtr node, NodePtr parent) {
node->data = 0;
node->parent = parent;
node->_left = nullptr;
node->_right = nullptr;
node->color = 0;
}
// fix the rb tree modified by the delete operation
void fixDelete(NodePtr x) {
NodePtr s;
while (x != root && x->color == 0) {
if (x == x->parent->_left) {
s = x->parent->_right;
if (s->color == 1) {
// case 3.1
s->color = 0;
x->parent->color = 1;
leftRotate(x->parent);
s = x->parent->_right;
}
if (s->_left->color == 0 && s->_right->color == 0) {
// case 3.2
s->color = 1;
x = x->parent;
} else {
if (s->_right->color == 0) {
// case 3.3
s->_left->color = 0;
s->color = 1;
rightRotate(s);
s = x->parent->_right;
}
// case 3.4
s->color = x->parent->color;
x->parent->color = 0;
s->_right->color = 0;
leftRotate(x->parent);
x = root;
}
} else {
s = x->parent->_left;
if (s->color == 1) {
// case 3.1
s->color = 0;
x->parent->color = 1;
rightRotate(x->parent);
s = x->parent->_left;
}
if (s->_right->color == 0 && s->_right->color == 0) {
// case 3.2
s->color = 1;
x = x->parent;
} else {
if (s->_left->color == 0) {
// case 3.3
s->_right->color = 0;
s->color = 1;
leftRotate(s);
s = x->parent->_left;
}
// case 3.4
s->color = x->parent->color;
x->parent->color = 0;
s->_left->color = 0;
rightRotate(x->parent);
x = root;
}
}
}
x->color = 0;
}
void rbTransplant(NodePtr u, NodePtr v){
if (u->parent == nullptr) {
root = v;
} else if (u == u->parent->_left){
u->parent->_left = v;
} else {
u->parent->_right = v;
}
v->parent = u->parent;
}
void deleteNodeHelper(NodePtr node, int key) {
// find the node containing key
NodePtr z = TNULL;
NodePtr x, y;
while (node != TNULL){
if (node->data == key) {
z = node;
}
if (node->data <= key) {
node = node->_right;
} else {
node = node->_left;
}
}
if (z == TNULL) {
cout<<"Couldn't find key in the tree"<<endl;
return;
}
y = z;
int y_original_color = y->color;
if (z->_left == TNULL) {
x = z->_right;
rbTransplant(z, z->_right);
} else if (z->_right == TNULL) {
x = z->_left;
rbTransplant(z, z->_left);
} else {
y = minimum(z->_right);
y_original_color = y->color;
x = y->_right;
if (y->parent == z) {
x->parent = y;
} else {
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;
}
delete z;
if (y_original_color == 0){
fixDelete(x);
}
}
// fix the red-black tree
void fixInsert(NodePtr k){
NodePtr u;
while (k->parent->color == 1) {
if (k->parent == k->parent->parent->_right) {
u = k->parent->parent->_left; // uncle
if (u->color == 1) {
// case 3.1
u->color = 0;
k->parent->color = 0;
k->parent->parent->color = 1;
k = k->parent->parent;
} else {
if (k == k->parent->_left) {
// case 3.2.2
k = k->parent;
rightRotate(k);
}
// case 3.2.1
k->parent->color = 0;
k->parent->parent->color = 1;
leftRotate(k->parent->parent);
}
} else {
u = k->parent->parent->_right; // uncle
if (u->color == 1) {
// mirror case 3.1
u->color = 0;
k->parent->color = 0;
k->parent->parent->color = 1;
k = k->parent->parent;
} else {
if (k == k->parent->_right) {
// mirror case 3.2.2
k = k->parent;
leftRotate(k);
}
// mirror case 3.2.1
k->parent->color = 0;
k->parent->parent->color = 1;
rightRotate(k->parent->parent);
}
}
if (k == root) {
break;
}
}
root->color = 0;
}
void printHelper(NodePtr root, string indent, bool last) {
// print the tree structure on the screen
if (root != TNULL) {
cout<<indent;
if (last) {
cout<<"R----";
indent += " ";
} else {
cout<<"L----";
indent += "| ";
}
string sColor = root->color?"RED":"BLACK";
cout<<root->data<<"("<<sColor<<")"<<endl;
printHelper(root->_left, indent, false);
printHelper(root->_right, indent, true);
}
// cout<<root->left->data<<endl;
}
public:
RBTree<T,CMP>() {
TNULL = new Node;
TNULL->color = 0;
TNULL->_left = nullptr;
TNULL->_right = nullptr;
root = TNULL;
}
typedef const_iterator<T> iterator;
iterator begin()
{
Node* left = root;
while (left && left->_left)
{
left = left->_left;
}
return iterator(left);
}
iterator end()
{
return iterator(nullptr);
}
// find the node with the minimum key
NodePtr minimum(NodePtr node) {
while (node->_left != TNULL) {
node = node->_left;
}
return node;
}
// find the node with the maximum key
NodePtr maximum(NodePtr node) {
while (node->_right != TNULL) {
node = node->_right;
}
return node;
}
// find the successor of a given node
NodePtr successor(NodePtr x) {
// if the right subtree is not null,
// the successor is the leftmost node in the
// right subtree
if (x->_right != TNULL) {
return minimum(x->_right);
}
// else it is the lowest ancestor of x whose
// left child is also an ancestor of x.
NodePtr y = x->parent;
while (y != TNULL && x == y->_right) {
x = y;
y = y->parent;
}
return y;
}
// find the predecessor of a given node
NodePtr predecessor(NodePtr x) {
// if the left subtree is not null,
// the predecessor is the rightmost node in the
// left subtree
if (x->_left != TNULL) {
return maximum(x->_left);
}
NodePtr y = x->parent;
while (y != TNULL && x == y->_left) {
x = y;
y = y->parent;
}
return y;
}
// rotate left at node x
void leftRotate(NodePtr x) {
NodePtr y = x->_right;
x->_right = y->_left;
if (y->_left != TNULL) {
y->_left->parent = x;
}
y->parent = x->parent;
if (x->parent == nullptr) {
this->root = y;
} else if (x == x->parent->_left) {
x->parent->_left = y;
} else {
x->parent->_right = y;
}
y->_left = x;
x->parent = y;
}
// rotate right at node x
void rightRotate(NodePtr x) {
NodePtr y = x->_left;
x->_left = y->_right;
if (y->_right != TNULL) {
y->_right->parent = x;
}
y->parent = x->parent;
if (x->parent == nullptr) {
this->root = y;
} else if (x == x->parent->_right) {
x->parent->_right = y;
} else {
x->parent->_left = y;
}
y->_right = x;
x->parent = y;
}
// insert the key to the tree in its appropriate position
// and fix the tree
void insert(int key) {
// Ordinary Binary Search Insertion
NodePtr node = new Node;
node->parent = nullptr;
node->data = key;
node->_left = TNULL;
node->_right = TNULL;
node->color = 1; // new node must be red
NodePtr y = nullptr;
NodePtr x = this->root;
while (x != TNULL) {
y = x;
if (node->data < x->data) {
x = x->_left;
} else {
x = x->_right;
}
}
// y is parent of x
node->parent = y;
if (y == nullptr) {
root = node;
} else if (node->data < y->data) {
y->_left = node;
} else {
y->_right = node;
}
// if new node is a root node, simply return
if (node->parent == nullptr){
node->color = 0;
return;
}
// if the grandparent is null, simply return
if (node->parent->parent == nullptr) {
return;
}
// Fix the tree
fixInsert(node);
}
NodePtr getRoot(){
return this->root;
}
// delete the node from the tree
void deleteNode(int data) {
deleteNodeHelper(this->root, data);
}
// print the tree structure on the screen
void prettyPrint() {
if (root) {
printHelper(this->root, "", true);
}
}
};
int main() {
RBTree<int,std::less<int>> bst;
bst.insert(8);
bst.insert(18);
bst.insert(5);
bst.insert(15);
bst.insert(17);
bst.insert(25);
bst.insert(40);
bst.insert(80);
bst.insert(100);
bst.prettyPrint();
bst.deleteNode(25);
bst.prettyPrint();
return 0;
}