-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbt.cpp
349 lines (332 loc) · 9.82 KB
/
rbt.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
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
bool color; // black: false, red: true
Node *right, *left, *parent;
//Function to create a new node
Node(int n)
{
this->data = n;
this->left = this->right = this->parent = NULL;
this->color = true;
}
};
class RBT
{
//root is made private so that functions outside cannot modify it
Node *root;
protected:
// function to right rotate a subtree
void rightRotate(Node *node)
{
Node *temp = node->left;
node->left = temp->right;
if (node->left != NULL)
node->left->parent = node;
temp->parent = node->parent;
if (node == root)
root = temp;
else if (node == node->parent->left)
node->parent->left = temp;
else
node->parent->right = temp;
temp->right = node;
node->parent = temp;
}
// function to left rotate a subtree
void leftRotate(Node *node)
{
Node *temp = node->right;
node->right = temp->left;
if (node->right != NULL)
node->right->parent = node;
temp->parent = node->parent;
if (node == root)
root = temp;
else if (node == node->parent->left)
node->parent->left = temp;
else
node->parent->right = temp;
temp->left = node;
node->parent = temp;
}
// function to interchange colors of 2 nodes
void swapColors(Node *x, Node *y)
{
bool temp = x->color;
x->color = y->color;
y->color = temp;
}
// function to check if insertion led to any anomaly
void checkInsert(Node *node)
{
if (node == root)
node->color = false;
else if (node->parent->color != false) // parent is red
{
Node *p = node->parent; // parent
Node *g = p->parent; // grandparent
Node *u; //parent's sibling
if (p == NULL || g == NULL)
return;
else if (p->data > g->data)
u = g->left;
else
u = g->right;
if (u != NULL && u->color == true) //uncle and parent are red
{
u->color = p->color = false;
g->color = true; //make grandparent red
checkInsert(g);
}
else // parent is red, uncle is black
{
if (p == g->left)
{
if (node == p->left) //LL
swapColors(p, g);
else // LR
{
leftRotate(p);
swapColors(node, g);
}
rightRotate(g);
}
else
{
if (node == p->right) //RR
swapColors(p, g);
else // RL
{
rightRotate(p);
swapColors(node, g);
}
leftRotate(g);
}
}
}
}
// funtion to fix the double black condition after deletion
void fixDoubleBlack(Node *node)
{
if (node != root) // case 1
{
Node *s, *p = node->parent;
if (node->parent->left == node)
s = p->right;
else
s = p->left;
if (s == NULL) // case 2
fixDoubleBlack(p);
else if (s->color == true) // case 3: sibling is red
{
p->color = true;
s->color = false;
if (s == p->left)
rightRotate(p);
else
leftRotate(p);
fixDoubleBlack(node);
}
else if (s->left != NULL && s->left->color == true) // sibling is black
{
if (s == p->left) // case 4: left left
{
s->left->color = s->color;
s->color = p->color;
rightRotate(p);
}
else // case 5: right left
{
s->left->color = p->color;
rightRotate(s);
leftRotate(p);
}
}
else if (s->right != NULL && s->right->color == true) // sibling is black
{
if (s == p->left) // mirror of case 4: left right
{
s->right->color = p->color;
leftRotate(s);
rightRotate(p);
}
else // case 5 mirror: right right
{
s->right->color = s->color;
s->color = p->color;
leftRotate(p);
}
}
else // case 6: sibling is black and both its children are black
{
s->color = true;
if (p->color == false)
fixDoubleBlack(p);
else
p->color = false;
}
}
}
// function to delete a node
void removeNode(Node *node)
{
if (node->left != NULL && node->right != NULL) //both left and right child exists
{
Node *u = node->left; // find inorder predecessor
while (u->right != NULL)
u = u->right;
node->data = u->data;
removeNode(u);
}
else
{
if (node->left == NULL && node->right == NULL)
{
if (node == root)
root = NULL;
else // node is leaf
{
if (node->parent->left == node)
node->parent->left = NULL;
else
node->parent->right = NULL;
if (node->color == false)
fixDoubleBlack(node);
}
}
else
{
Node *u;
if (node->left == NULL)
u = node->right;
else
u = node->left;
u->parent = node->parent;
if (node != root)
{
if (node->parent->left == node)
node->parent->left = u;
else
node->parent->right = u;
}
if (node->color == false && u->color == false)
fixDoubleBlack(u);
else
u->color = false;
}
delete node;
}
}
public:
// constructor function of a new tree
RBT() { root = NULL; }
// function to get root pointer
Node *getRoot() { return root; }
// function to search a node
Node *search(int n, Node *node)
{
if (node == NULL)
return NULL;
else if (node->data < n && node->right != NULL)
return search(n, node->right);
else if (node->data > n && node->left != NULL)
return search(n, node->left);
return node;
}
// function to insert a new node
int insert(int n)
{
Node *node = new Node(n);
if (root == NULL)
root = node;
else
{
Node *temp = search(n, root);
if (temp->data == n)
return -1;
else if (temp->data > n)
temp->left = node;
else
temp->right = node;
node->parent = temp;
checkInsert(node);
}
return 1;
}
// function to identify which node to delete
int remove(int n)
{
if (root != NULL)
{
Node *temp = search(n, root);
if (temp->data == n)
{
removeNode(temp);
return 1;
}
}
return -1;
}
// function to traverse the tree inorder
void traverse(Node *node)
{
if (node == NULL)
return;
traverse(node->left);
cout << node->data << " ";
traverse(node->right);
}
};
// Main Function to take user input and display results
int main()
{
RBT tree;
int i, n;
cout << "A Red Black Tree has been created.\nYou can do the following operations next-\n1. Insert a new node\n2. Search a node\n3. Delete aa node\n4. Traverse the tree (Inorder)\n5. Delete the tree and exit";
while (1)
{
cout << "\nEnter your choice: ";
cin >> i;
switch (i)
{
case 1:
cout << "Enter the number to insert: ";
cin >> n;
n = tree.insert(n);
if (n == 1)
cout << n << " inserted successfully.";
else
cout << n << " already present in tree.";
break;
case 2:
cout << "Enter the number to search: ";
cin >> n;
if (tree.search(n, tree.getRoot()) != NULL && tree.search(n, tree.getRoot())->data == n)
cout << "Node found.";
else
cout << "Node not found.";
break;
case 3:
cout << "Enter the number to delete: ";
cin >> n;
n = tree.remove(n);
if (n == 1)
cout << "Node deleted.";
else
cout << "Node not found.";
break;
case 4:
cout << "Inorder traversal of the tree is: ";
tree.traverse(tree.getRoot());
break;
case 5:
cout << "Program successfully exited. Thank You.";
return 0;
default:
cout << "Please enter the right number to continue.";
}
}
}