-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRBT.h
386 lines (374 loc) · 8.94 KB
/
RBT.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
#pragma once
#include "Node.h"
#include <iostream>
using namespace std;
template <class T>
class RBT {
protected:
Node<T>* root;
Node<T>*& createNode(const T& value) {
Node<T>* tempNode = new Node<T>;
tempNode->color = 'R'; //new nodes are inserted as red color
tempNode->data = value;
tempNode->left = nullptr;
tempNode->right = nullptr;
tempNode->count = 1;
return tempNode;
}
void INSERT(Node<T>*& ptr, T val) {
//if ptr empty
if (ptr != nullptr && ptr->data == val) {
ptr->count++;
}
else if (ptr == nullptr) {
ptr = createNode(val);
return;
}
else if (val < ptr->data) {
INSERT(ptr->left, val);
}
else {
INSERT(ptr->right, val);
}
bool red = false;
Node<T>* VAR = nullptr;
if (ptr->color == 'R') {
if (ptr->right!=nullptr && ptr->right->color == 'R') {
val = ptr->right->data;
VAR = ptr->right;
red = true;
}
else if (ptr->left != nullptr && ptr->left->color == 'R') {
val = ptr->left->data;
VAR = ptr->left;
red = true;
}
if (red == true) {
//find ptr(parent) ka bhai
Node<T>* grandParent = findParent(ptr->data);
Node<T>* uncle = findUncle(grandParent, ptr);
//if uncle (temp) = R then recolor else rotate
//
//RECOLORING
if (uncle != nullptr && uncle->color == 'R')
{
switchColor(ptr); //parent
switchColor(uncle);
switchColor(grandParent);
if (root->color == 'R') { switchColor(root); }
}
else {
Node<T>* tempParent = ptr;
Node<T>* tempGrandpa = grandParent;
//rotate here
//right right case
// parent is on right of grandparent // // child is on right of parent
if (grandParent->right != nullptr && ptr->data == grandParent->right->data && ptr->right != nullptr && VAR->data == ptr->right->data) { // masla is here -> val is at leaf
//rotate left grandparent
leftRotation(grandParent);
switchColor(tempParent); //parent
switchColor(tempGrandpa);
}
//left left case
// parent is on left of grandparent // // child is on left of parent
else if (grandParent->left != nullptr && ptr->data == grandParent->left->data && ptr->left != nullptr && VAR->data == ptr->left->data) {
//rotate right grandparent
rightRotation(grandParent);
switchColor(tempParent); //parent
switchColor(tempGrandpa);
}
//right left case
// parent is on right of grandparent // // child is on left of parent
else if (grandParent->right != nullptr && ptr->data == grandParent->right->data && ptr->left != nullptr && VAR->data == ptr->left->data) {
//rotate right parent
rightRotation(ptr);
//rotate left grandparent
leftRotation(grandParent);
switchColor(tempParent); //parent
switchColor(tempGrandpa);
}
//left right case
// parent is on left of grandparent // // child is on right of parent
else if (grandParent->left != nullptr && ptr->data == grandParent->left->data && ptr->right != nullptr && VAR->data == ptr->right->data) {
//rotate left parent
leftRotation(ptr);
//rotate right grandparent
rightRotation(grandParent);
switchColor(tempParent); //parent
switchColor(tempGrandpa);
}
if (root->color == 'R') { switchColor(root); }
}
}
}
}
Node<T>* findUncle(const Node<T>* grandParent, const Node<T>* parent) {
if (grandParent->right != nullptr && grandParent->right == parent) {
return grandParent->left;
}
else if (grandParent->left != nullptr) {
return grandParent->right;
}
}
void INORDER(Node<T>* ptr) {
if (ptr != nullptr) {
INORDER(ptr->left);
cout << ptr->data << ptr->color << " ";
INORDER(ptr->right);
}
}
void PREORDER(Node<T>* ptr)
{
if (ptr != nullptr){
cout << ptr->data << ptr->color << " ";
PREORDER(ptr->left);
PREORDER(ptr->right);
}
}
void delTree(Node<T>* ptr)
{
if (ptr != nullptr){
delTree(ptr->left);
delTree(ptr->right);
delete ptr;
ptr = nullptr;
}
}
void POSTORDER(Node<T>* ptr)
{
if (ptr != nullptr){
POSTORDER(ptr->left);
POSTORDER(ptr->right);
cout << ptr->data << ptr->color << " ";
}
}
void PREORDER2(Node<T>* ptr)
{
if (ptr != nullptr){
cout << ptr->data << ptr->color << " ";
PREORDER2(ptr->right);
PREORDER2(ptr->left);
}
}
void INORDER2(Node<T>* ptr)
{
if (ptr != nullptr){
INORDER2(ptr->right);
cout << ptr->data << ptr->color << " ";
INORDER2(ptr->left);
}
}
void POSTORDER2(Node<T>* ptr) {
if (ptr != nullptr){
POSTORDER2(ptr->right);
POSTORDER2(ptr->left);
cout << ptr->data << ptr->color << " ";
}
}
void switchColor(Node<T>*& nodePtr) // & by reference = change in original
{
if (nodePtr->color == 'R'){
nodePtr->color = 'B';
}
else if (nodePtr->color == 'B'){
nodePtr->color = 'R';
}
}
Node<T>* findParent(T data) {
return FINDPARENT(root, data);
}
Node<T>* FINDPARENT(Node<T>* r, T& val){
if (r == nullptr || r->left!=nullptr && r->left->data == val || r->right!=nullptr && r->right->data == val) {
return r;
}
else if (r->data < val) {
FINDPARENT(r->right, val);
}
else if (r->data > val) {
FINDPARENT(r->left, val);
}
}
void rightRotation(Node<T>*& grandpa) {
Node<T>* parent = grandpa->left;
grandpa->left = parent->right;
parent->right = grandpa;
if (grandpa != root) {
Node<T>* t = findParent(grandpa->data);
if (t->right == grandpa) {
t->right = parent;
}
else {
t->left = parent;
}
}
else {
root = parent;
}
}
void leftRotation(Node<T>*& grandpa) {
Node<T>* parent = grandpa->right;
grandpa->right = parent->left;
parent->left = grandpa;
if (grandpa != root) {
Node<T>* t = findParent(grandpa->data);
if (t->right == grandpa) {
t->right = parent;
}
else {
t->left = parent;
}
}
else {
root = parent;
}
}
Node<T>*& SEARCH(Node<T>* r, T val) {
if (r == nullptr || r->data == val) {
return r;
}
else if (r->data < val) {
SEARCH(r->right, val);
}
else if (r->data > val) {
SEARCH(r->left, val);
}
}
Node<T>*& maxLeft(Node<T>* n) {
Node<T>* nn = n;
if (n -> left != nullptr) {
nn = n->left;
while (nn->right != nullptr){
nn = nn->right;
}
}
return nn;
}
public:
Node<T>* getRoot() {
return root;
}
void del(T value) {
Node<T>* TBD = SEARCH(root, value);
if (TBD != nullptr) {
if (TBD->count == 1) {
if (TBD->right == nullptr && TBD->left == nullptr) {
Node<T>* parent = findParent(TBD->data);
if (parent->right != nullptr && parent->right == TBD) {
parent->right = nullptr;
}
else {
parent->left = nullptr;
}
delete TBD;
return;
}
else {
Node<T>* max = maxLeft(TBD);
T data = max->data;
del(max->data);
TBD->data = data;
}
}
else if (TBD->count > 1) {
TBD->count = 1;
}
}
else {
cout << "Value doesnt exist\n";
}
}
void snapShot(std::ostream& out, Node<T>* p) {
out << '\"' << p->data << p->color << '\"';
if (p->left != nullptr || p->right != nullptr) {
out << '[';
if (p->left == nullptr)
out << "\"\"";
else
snapShot(out, p->left);
out << ',';
if (p->right == nullptr)
out << "\"\"";
else
snapShot(out, p->right);
out << ']';
}
}
~RBT() {
if (root != nullptr){
delTree(root);
root = nullptr;
}
}
bool search(T value) {
if (root != nullptr) {
if (SEARCH(root, value) != nullptr) {
return true;
}
}
return false;
}
RBT() {
root = nullptr;
}
void insert(T val) {
if (root == nullptr) {
root = createNode(val);
if (root->color == 'R') { switchColor(root); }
}
else if (root->data == val) {
root->count++;
}
else if (val > root->data) {
//insert right since value is greater
INSERT(root->right, val);
}
else if (val < root->data) {
//insert left since value is smaller
INSERT(root->left, val);
}
}
void preOrder2(){
if (root != nullptr){
PREORDER2(root);
}
}
void inOrder2() {
if (root != nullptr) {
INORDER2(root);
}
}
void postOrder2(){
if (root != nullptr){
POSTORDER2(root);
}
}
void inOrder() {
if (root != nullptr) {
INORDER(root);
}
}
void preOrder(){
if (root != nullptr){
PREORDER(root);
}
}
void postOrder(){
if (root != nullptr){
POSTORDER(root);
}
}
void displayParent(T data){
if (root == nullptr || root->data == data){
cout << "Root node has no parent / is empty" << endl;
}
else {
Node<T>* parent = findParent(data);
if (parent != nullptr) {
cout << "Parent: " << parent->data<<endl;
}
else {
cout << "Node not found\n" << endl;
}
}
}
};