-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbt.c
293 lines (214 loc) · 5.33 KB
/
rbt.c
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
#include <stdio.h>
#include <stdlib.h>
#define red 1
#define black 0
struct node
{
int data;
struct node* left;
struct node* right;
struct node* parent;
int color; //1 for red 0 = black
};
struct node* root=NULL; //setting the root to null initially
struct node* Create(int key) //creates a new node of red color
{
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->data= key;
temp->left= NULL;
temp->right=NULL;
temp->parent=NULL;
temp->color=red;
return temp;
}
struct node* Search(struct node* root, int key)
{
if(!root)return root;
if(root->data == key)return root;
if( key > root->data)return Search(root->right,key);
return Search(root->left,key);
}
void Display(struct node* root)
{
if(root==NULL)return;
Display(root->left);
printf("%d\t",root->data );
Display(root->right);
}
void left_rotate(struct node* a) // left rotates the tree about a
{
struct node* b = a->right;
a->right = b->left;
if(b->left)b->left->parent=a;
b->parent = a->parent;
if(a->parent==NULL)root=b;
else if(a == a->parent->left) a->parent->left = b;
else a->parent->right = b;
b->left =a;
a->parent =b;
}
void right_rotate(struct node* a ) // right rotates the tree about a
{
struct node* b = a->left;
a->left =b->right;
if(b->right)b->right->parent = a;
b->parent = a->parent;
if(a->parent==NULL)root=b;
else if(a== a->parent->left) a->parent->left = b;
else a->parent->right = b;
b->right =a;
a->parent =b;
}
void recolor (struct node* a ) //makes a and it's sibling black and a's parent red
{
a->parent->color=red; //set gp red
a->parent->left->color=black; /* set parent and uncle to black */
a->parent->right->color=black;
}
void swap(int* a, int* b)
{
int temp = *a;
*a=*b;
*b= temp;
// printf("yooooooooo\n");
}
void RB_INSERT_FIXUP(struct node* temp) //takes the newly inserted node as the input
{
struct node* y=NULL;
struct node* gp=NULL;
while( (temp!=root) && (temp->color==red) && (temp->parent->color==red) )
{
y=temp->parent; //y= parent
gp=temp->parent->parent;
//case 1)parent is left child of gp
//includes cases left-left and left-right
if(y==gp->left)
{
struct node* uncle= gp->right;
//case 1.1 uncle is also red . only recolor
if(uncle!=NULL && uncle->color==red)
{
recolor(y);
temp=gp;
}
//case 1.2 uncle is not red or no uncle but parent is red
else
{
//case 1.2.2 new node is right child. =>left right case (parent=left, new node= right)
//left rotation both codes in the if block and below are executed
if(temp == y->right)
{
left_rotate(y);
temp= y;
y= temp->parent;
}
// case 1.2.1 new node is left child of parent =>left-left case (parent =left child and so is the new node)
//right rotation
right_rotate(gp);
swap(&y->color,&gp->color);
temp= y;
}
}
// case 2 parent is right child of gp
// includes cases right-left and right-right
else
{
struct node* uncle= gp->left;
//case 2.1 uncle also red , only recolor
if((uncle!=NULL) && uncle->color==red)
{
recolor(y);
temp= gp;
}
else
{
if(temp==y->left)
{
right_rotate(y);
temp=y;
y=temp->parent;
}
left_rotate(gp);
swap(&y->color,&gp->color);
temp=y;
}
}
} //while
root->color=black;
}
void insert(int key) //return the root
{
struct node* y = NULL;
struct node* current = root;
if(current==NULL) //if the tree is Empty tree
{
current=Create(key);
current->color=black; //coz the root is always black
root= current;
return; //this is the new root
}
while(current!=NULL)
{
y= current;
if(key<current->data)current=current->left;
else current=current->right;
}
//now we've reached the place to insert the node. new node will be y's child
struct node* temp= Create(key);
temp->parent= y; //temp is the new node
if (y==NULL)
{
root=temp;
}
else if(temp->data < y->data)
{
y->left=temp;
}
else y->right=temp;
if(y->color==black)return ; //if parent is black no need for anything
RB_INSERT_FIXUP(temp); //we call this when the parent is red
//temp is the newly inserted node of red color
}
void main()
{
int stop=0;
int n;
int choice=0;
while(stop==0)
{
printf("1) Insert\n");
printf("2) Search\n");
printf("3) Display\n");
printf("4) EXIT\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the number you want to insert\n");
scanf("%d",&n);
insert(n);
//break; //REMOVE THIS IF YOU DON'T WANT TO DISPLAY AFTER EVERY INSERTION
case 3: if(!root)
{
printf("Empty Tree\n");
break;
}
Display(root);
printf("\n");
break;
case 2: if(root==NULL)
{
printf("Empty tree\n");
break;
}
printf("Enter the number you want to Search\n");
scanf("%d",&n);
if(Search(root,n) )printf("Number is present in the tree\n");
else printf("Number not present in the tree\n");
break;
case 4: stop=1;
break;
default: printf("Invalid input\n");
break;
}
}
}