-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrbt.cpp
More file actions
194 lines (173 loc) · 4.04 KB
/
rbt.cpp
File metadata and controls
194 lines (173 loc) · 4.04 KB
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
#include<iostream>
#define SPACES 10;
using namespace std;
char colors[2] = {'R', 'B'};
int max(int a, int b) {
return a > b ? a : b;
}
struct tree {
int value, color;
tree *left, *right;
tree(int v) {
value = v;
color = 0;
left = NULL;
right = NULL;
}
};
tree *rotateLeft(tree *x) {
tree *y = x->right;
tree *t = y->left;
y->left = x;
x->right = t;
return y;
}
tree *rotateRight(tree *x) {
tree *y = x->left;
tree *t = y->right;
y->right = x;
x->left = t;
return y;
}
void inorder(tree *t) {
if(t != NULL) {
inorder(t->left);
cout << t->value;
inorder(t->right);
}
}
tree *minValueNode(tree *root) {
tree* current = root;
while (current && current->left != NULL)
current = current->left;
return current;
}
tree* insert(tree *root, int x) {
if(root == NULL) {
root = new tree(x);
} else {
if(x < root->value) {
root->left = insert(root->left, x);
if(root->color == 1 && root->left->color == 0) {
if(root->left->left != NULL && root->left->left->color == 0) {
if(root->right == NULL || root->right->color == 1) {
root = rotateRight(root);
root->color = 1;
root->left->color = 0;
root->right->color = 0;
} else {
root->color = 0;
root->left->color = 1;
root->right->color = 1;
}
} else if (root->left->right != NULL && root->left->right->color == 0) {
if(root->right == NULL || root->right->color == 1) {
root->left = rotateLeft(root->left);
root = rotateRight(root);
root->color = 1;
root->left->color = 0;
root->right->color = 0;
} else {
root->color = 0;
root->left->color = 1;
root->right->color = 1;
}
}
}
} else if (x > root->value) {
root->right = insert(root->right, x);
if(root->color == 1 && root->right->color == 0) {
if(root->right->left != NULL && root->right->left->color == 0) {
if(root->left == NULL || root->left->color == 1) {
root->right = rotateRight(root->right);
root = rotateLeft(root);
root->color = 1;
root->left->color = 0;
root->right->color = 0;
} else {
root->color = 0;
root->left->color = 1;
root->right->color = 1;
}
} else if (root->right->right != NULL && root->right->right->color == 0) {
if(root->left == NULL || root->left->color == 1) {
root = rotateLeft(root);
root->color = 1;
root->left->color = 0;
root->right->color = 0;
} else {
root->color = 0;
root->left->color = 1;
root->right->color = 1;
}
}
}
}
}
return root;
}
tree* deleteNode(tree *root, int x) {
if(root == NULL) {
cout << "Node not found.";
return root;
}
if(x == root->value) {
if(root->left == NULL) {
tree *t = root;
root = root->right;
delete t;
} else if(root->right == NULL) {
tree *t = root;
root = root->left;
delete t;
} else {
tree *t = minValueNode(root->right);
root->value = t->value;
root->right = deleteNode(root->right, t->value);
}
} else if(x < root->value) {
root->left = deleteNode(root->left, x);
} else if (x > root->value) {
root->right = deleteNode(root->right, x);
}
if(root == NULL) return root;
return root;
}
void displayTree(tree *root, int space = 0) {
if(!(root == NULL)) {
space += SPACES;
displayTree(root->right, space);
cout << endl;
int i = SPACES;
for (; i < space; i++) {
cout<<" ";
}
cout << root->value << ' ' << colors[root->color];
displayTree(root->left, space);
}
}
int main() {
tree *root = NULL;
int ch,n;
do {
cout << "\n1.Insert Node\n2.Delete Node\n3.Display Tree\n0. Exit\nEnter your choice: ";
cin >> ch;
switch(ch){
case 1:
cout << "\nEnter value of node:";
cin >> n;
root=insert(root,n);
if(root->color == 0) root->color = 1;
break;
case 2:
cout << "\nEnter value to be deleted: ";
cin >> n;
root=deleteNode(root,n);
break;
case 3:
displayTree(root);
break;
}
} while (ch != 0);
return 0;
}