-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathred_black_tree.c
280 lines (216 loc) · 5.19 KB
/
red_black_tree.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
#include<stdio.h>
#include<stdlib.h>
#define RED 5
#define BLACK 10
typedef struct Leaf{
int value;
struct Leaf *left;
struct Leaf *right;
struct Leaf *parent;
int color;
}leaf;
typedef struct Tree{
leaf *root;
}tree;
void init(tree* t){
t->root = NULL;
}
leaf* create_leaf(int value){
leaf *new = (leaf *) malloc (sizeof(leaf));
if(new == NULL) return NULL;
new->value = value;
new->color = RED;
new->left = new->right = new->parent = NULL;
return new;
}
void inorder(leaf *root){
if (root != NULL){
inorder(root->left);
printf("%d %d\t", root->value,root->color);
inorder(root->right);
}
}
leaf* uncle(leaf* root){
leaf* aux = NULL;
if((root != NULL) && (root->parent != NULL)){
if (root->parent->parent->left == root->parent){
aux = root->parent->parent->right;
}
else{
aux = root->parent->parent->left;
}
}
return aux;
}
leaf* grandp(leaf *root){
if((root != NULL) && (root->parent!= NULL)) return root->parent->parent;
return NULL;
}
leaf* rot_right(leaf *root){
leaf *p = root->parent, *g = p->parent;
g->left = p->right;
p->right = g;
if(g->parent != NULL){
if(g->parent->left == g){
g->parent->left = p;
}
else{
g->parent->right = p;
}
}
p->parent = g->parent;
g->parent = p;
//printf("valor de p:%d, valor de root:%d\n",p->value,root->value);
root = p;
//printf("valor de p:%d, valor de root:%d\n",p->value,root->value);
return root;
}
leaf* rot_left(leaf *root){
leaf *p = root->parent, *g = p->parent;
g->right = p->left;
p->left = g;
if(g->parent != NULL){
if(g->parent->right == g){
g->parent->right = p;
}
else{
g->parent->left = p;
}
}
p->parent = g->parent;
g->parent = p;
root = p;
return root;
}
leaf* rot_right_left(leaf *root){
leaf *p = root->parent, *g = p->parent;
p->left = root->right;
root->right = p;
if (g->left == p)
g->left = root;
else
{
g->right = root;
}
root->parent = p->parent;
p->parent = root;
return rot_left(p);
}
leaf* rot_left_right(leaf *root){
leaf *p = root->parent, *g = p->parent;
p->right = root->left;
root->left = p;
if (g->left == p)
g->left = root;
else
{
g->right = root;
}
root->parent = p->parent;
p->parent = root;
return rot_right(p);
}
leaf* balance(leaf* root){
leaf *p, *u; //parente, uncle
if(root->parent == NULL){ //case 1
//printf("entrou caso 1\n");
root->color = BLACK;
}
else{
p = root->parent;
if(p->color == RED){
u = uncle(root);
if((u != NULL) && (u->color == RED)){//case 2
//printf("entrou caso 2\n");
p->color = u->color = BLACK;
root = grandp(root);
if(root->parent != NULL)
root->color = RED;
}
else{//case 3
if(p->left == root){
if(p->parent->right == p){
//printf("entrou caso 3c\n");
root = rot_right_left(root);//case 3c
}
else{
//printf("entrou caso 3a\n");
root = rot_right(root); //case 3a
}
}
else{
if(p->parent->left == p){
//printf("entrou caso 3d\n");
root = rot_left_right(root);//case 3d
}
else{
//printf("entrou caso 3b\n");
root = rot_left(root); //case 3b
}
}
//printf("fazendo pintura dos nós\n");
//printf("valor de root:%d, valor pai:%d\n",root->value,root->parent->value);
root->color = BLACK;
if(root->left != NULL) root->left->color = RED;
if(root->right != NULL) root->right->color = RED;
//inorder(root->parent);
//return root->parent;
}
}
}
return root;
}
leaf* insert(leaf* root, int value){
leaf *auxP, *aux;
if (root == NULL){
return create_leaf(value);
}
else{
//auxParent, aux = new
auxP = aux = root;
while (aux != NULL){
auxP = aux;
if(value < aux->value) aux = aux->left;
else aux = aux->right;
}
aux = create_leaf(value);
aux->parent = auxP;
if(aux->value < auxP->value) auxP->left = aux;
else auxP->right = aux;
while ((aux->parent != NULL) && (aux->color == RED) && (aux->parent->color == RED)){
//printf("entrou balance\n");
aux = balance(aux);
//printf("\nvalor aux:%d\nsaiu balance\n",aux->value);
//inorder(root->parent);
}
}
//printf("\nvalor root:%d\n",root->value);
while (aux->parent != NULL) aux = aux->parent;
if(aux != root) root = aux;
//printf("\nvalor aux:%d valor root:%d\n",aux->value,root->value);
return root;
}
int bh(leaf* root){
if(root== NULL) return 0;
if(root->color == BLACK) return 1 + bh(root->left);
return bh(root->left);
}
int main(void){
int n;
scanf(" %d",&n);
tree *t = (tree*) malloc (sizeof(tree));
init(t);
int i, key;
scanf(" %d",&key);
t->root = insert(t->root, key);
t->root->color = BLACK;
for(i=1; i<n; i++){
scanf(" %d",&key);
t->root = insert(t->root, key);
}
//printf("\nt->root:%d\n", t->root->value);
//printf("\ninorder main:");
//inorder(t->root);
printf("%d\n",bh(t->root));
return 0;
}