-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRedBlack.cpp
234 lines (194 loc) · 4.72 KB
/
RedBlack.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
#include <iostream>
#include <bits/stdc++.h>
#include <limits.h>
using namespace std;
enum { red, black};
struct node
{
node *left,*right,*parent;
int data;
bool colour;
node(int key)
{
data=key;
left=right=parent=NULL;
//color=false by default ie red
}
};
void rightRotate(node *&root, node *&pt)
{
node *pt_left = pt->left;
pt->left = pt_left->right;
if (pt->left != NULL)
pt->left->parent = pt;
pt_left->parent = pt->parent;
if (pt->parent == NULL)
root = pt_left;
else if (pt == pt->parent->left)
pt->parent->left = pt_left;
else
pt->parent->right = pt_left;
pt_left->right = pt;
pt->parent = pt_left;
}
void leftRotate(node *&root, node *&pt)
{
node *pt_right = pt->right;
pt->right = pt_right->left;
if (pt->right != NULL)
pt->right->parent = pt;
pt_right->parent = pt->parent;
if (pt->parent == NULL)
root = pt_right;
else if (pt == pt->parent->left)
pt->parent->left = pt_right;
else
pt->parent->right = pt_right;
pt_right->left = pt;
pt->parent = pt_right;
}
node *insertBST(node *root, node *t)
{
if(root==NULL)
return t;
if(t->data < root->data)
{
root->left = insertBST(root->left,t);
root->left->parent = root;
//to prevent recursion fucking up t everytime
//t->parent = root;
}
else if( t->data > root->data)
{
root->right = insertBST(root->right,t);
root->right->parent=root;
//t->parent = root;
}
return root;
}
void violationCheck(node *&root, node *&t)
{
node *parent = NULL;
node *gparent = NULL;
//parent is red, child is red, child is not root
while((t!=root) && (t->parent->colour == red) && (t->colour ==red))
{
parent = t->parent;
gparent = parent->parent;
//develop the direction of first link parent-gparent
if(parent == gparent -> left)
{
//determine uncle
node *uncle = gparent->right;
//now depending on the uncle we make next move
if((uncle!=NULL) && (uncle->colour==red))
{
//switch colours
bool x= parent->colour;
uncle->colour=gparent->colour;
parent->colour=gparent->colour;
gparent->colour=x;
t=gparent;
}
else
{
//uncle is black or doesn't exist, gotta rotate,recolour(one sided tree)
if(t == parent->right)
{
leftRotate(root, parent);
t = parent;
parent = t->parent;
}
rightRotate(root, gparent);
swap(parent->colour, gparent->colour);
t=parent;
}
}
else
{
node *uncle = gparent->left;
if((uncle!=NULL) && (uncle->colour==red))
{
//normal switch
bool temp = parent->colour;
parent->colour = gparent->colour;
uncle->colour = gparent->colour;
gparent->colour=temp;
}
else
{
if(t == parent -> left)
{
rightRotate(root, parent);
t = parent;
parent = t->parent;
}
leftRotate(root, gparent);
swap(gparent->colour, parent->colour);
t=parent;
}
}
}
//will always remain black no matter what
root->colour=black;
}
void levelOrderHelper(node *root)
{
if (root == NULL)
return;
std::queue<node *> q;
q.push(root);
while (!q.empty())
{
node *temp = q.front();
cout << temp->data << " ";
q.pop();
if (temp->left != NULL)
q.push(temp->left);
if (temp->right != NULL)
q.push(temp->right);
}
}
class Tree
{
private:
node *root;
public:
Tree(){root=NULL;}
void insert(int key)
{
node *t = new node(key);
root = insertBST(root,t);
violationCheck(root,t);
}
void level()
{
levelOrderHelper(root);
}
};
int main()
{
Tree tree;
tree.insert(7);
cout << endl;
tree.level();
tree.insert(6);
cout << endl;
tree.level();
tree.insert(5);
cout << endl;
tree.level();
tree.insert(4);
cout << endl;
tree.level();
tree.insert(3);
cout << endl;
tree.level();
tree.insert(2);
cout << endl;
tree.level();
tree.insert(1);
cout << endl;
tree.level();
return 0;
}