-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAvlTree.c
374 lines (307 loc) · 7.89 KB
/
AvlTree.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
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
typedef int Chave;
typedef struct {
Chave chave;
} Registro;
typedef struct Node * Ponteiro;
typedef struct Node {
Registro reg;
Ponteiro left, right;
} Node;
int count_nodes(Ponteiro ponteiro){
if (!ponteiro) {
return 0;
}
return 1 + count_nodes(ponteiro->left) + count_nodes(ponteiro->right);
}
int height(Ponteiro ponteiro){
int left_height = 0, right_height = 0;
if (!ponteiro){
return 0;
}
left_height = height(ponteiro->left);
right_height = height(ponteiro->right);
if (left_height > right_height) {
return 1 + left_height;
} else {
return 1 + right_height;
}
}
int get_balancing_factor(Ponteiro *ponteiro){
return height((*ponteiro)->left) - height((*ponteiro)->right);
}
void rotacao_esq(Ponteiro *ponteiro_to_root){
Ponteiro aux;
aux = (*ponteiro_to_root)->right;
(*ponteiro_to_root)->right = aux->left;
aux->left = (*ponteiro_to_root);
(*ponteiro_to_root) = aux;
}
void rotacao_dir(Ponteiro *ponteiro_to_root){
Ponteiro aux;
aux = (*ponteiro_to_root)->left;
(*ponteiro_to_root)->left = aux->right;
aux->right = (*ponteiro_to_root);
(*ponteiro_to_root) = aux;
}
int balance_left(Ponteiro *ponteiro){
int left_bf = get_balancing_factor(&(*ponteiro)->left);
if (left_bf > 0) {
rotacao_dir(ponteiro);
return 1;
} else if (left_bf < 0) {
rotacao_esq(&((*ponteiro)->left));
rotacao_dir(ponteiro);
return 1;
}
return 0;
}
int balance_right(Ponteiro *ponteiro){
int right_bf = get_balancing_factor(&(*ponteiro)->right);
if (right_bf < 0) {
rotacao_esq(ponteiro);
return 1;
} else if (right_bf > 0) {
rotacao_dir(&((*ponteiro)->right));
rotacao_esq(ponteiro);
return 1;
}
return 0;
}
int balance(Ponteiro *ponteiro){
int root_bf = get_balancing_factor(ponteiro);
if (root_bf > 1) {
return balance_left(ponteiro);
}
if (root_bf < -1) {
return balance_right(ponteiro);
}
return 0;
}
void search(Registro *reg, Ponteiro ponteiro){
if (!ponteiro) {
printf("\nError: Registro not found.\n");
return;
}
if (reg->chave < ponteiro->reg.chave) {
search(reg, ponteiro->left);
return;
}
if (reg->chave > ponteiro->reg.chave) {
search(reg, ponteiro->right);
return;
}
*reg = ponteiro->reg;
}
int inserir(Registro reg, Ponteiro *ponteiro) {
if (!*ponteiro) {
*ponteiro = (Ponteiro) malloc(sizeof(Node));
(*ponteiro)->reg = reg;
(*ponteiro)->left = NULL;
(*ponteiro)->right = NULL;
return 1;
}
if (reg.chave < (*ponteiro)->reg.chave) {
if (inserir(reg, &(*ponteiro)->left)){
if (balance(ponteiro)){
return 0;
} else {
return 1;
}
} else {
return 0;
}
}
if (reg.chave > (*ponteiro)->reg.chave) {
if (inserir(reg, &(*ponteiro)->right)) {
if (balance(ponteiro)){
return 0;
} else {
return 1;
}
} else {
return 0;
}
}
printf("\nError: Registro %d ja exite.\n", reg.chave);
return 0;
}
void total_del(Ponteiro to_remove, Ponteiro *to_replace){
Ponteiro aux;
if ((*to_replace)->right) {
total_del(to_remove, &(*to_replace)->right);
return;
}
to_remove->reg = (*to_replace)->reg;
aux = *to_replace;
*to_replace = (*to_replace)->left;
free(aux);
}
int delete(Registro reg, Ponteiro *ponteiro) {
Ponteiro aux;
if (!*ponteiro) {
printf("Error: Registro %d not found.", reg.chave);
return 0;
}
if (reg.chave < (*ponteiro)->reg.chave){
if (delete(reg, &(*ponteiro)->left)){
balance(ponteiro);
return 1;
} else {
return 0;
}
}
if (reg.chave > (*ponteiro)->reg.chave) {
if (delete(reg, &(*ponteiro)->right)){
balance(ponteiro);
return 1;
} else {
return 0;
}
}
if (!(*ponteiro)->right) {
aux = *ponteiro;
*ponteiro = (*ponteiro)->left;
free(aux);
return 1;
}
if (!(*ponteiro)->left) {
aux = *ponteiro;
*ponteiro = (*ponteiro)->right;
free(aux);
return 1;
}
total_del(*ponteiro, &(*ponteiro)->left);
return 1;
}
void print_tree(Ponteiro *ponteiro) {
if (*ponteiro){
printf("(%d", (*ponteiro)->reg.chave);
print_tree(&(*ponteiro)->left);
print_tree(&(*ponteiro)->right);
printf(")");
}
}
void initialize(Ponteiro *dictionary){
*dictionary = NULL;
}
void reset_tree(Ponteiro ponteiro){
if (!ponteiro){
return;
}
reset_tree(ponteiro->left);
reset_tree(ponteiro->right);
free(ponteiro);
}
int standard_avl_verification(Ponteiro ponteiro){
int bf = 0;
if (!ponteiro) {
return 1;
}
if (!standard_avl_verification(ponteiro->left)){return 0;}
if (!standard_avl_verification(ponteiro->right)){return 0;}
bf = get_balancing_factor(&ponteiro);
if ((bf > 1) || (bf < -1)){
return 0;
} else {
return 1;
}
}
int calc_avl_verification(int nodes_qty, int tree_height){
if (((1.44 * log2f((float) nodes_qty + 2)) + 1.0) > (float) tree_height){
return 1;
} else {
return 0;
}
}
void print_tree_status(Ponteiro *tree){
printf("\nArvore: ");
print_tree(tree);
printf("\n-> Verificacao da AVL:\t%d", standard_avl_verification(*tree));
printf("\n-> Calculo verififcacao AVL:\t%d", calc_avl_verification(count_nodes(*tree), height(*tree)));
printf("\nTudo certo = 1; Cuidado, algo de errado= 0\n");
}
void primeiro_cen(){
int node_qty = 0;
Registro reg;
Ponteiro *tree = (Ponteiro *) malloc(sizeof(Ponteiro));
srand((unsigned) time(0));
initialize(tree);
printf("\nPrimeiro cenario\n");
printf("\nEntre com o numero de nos: ");
scanf("%d", &node_qty);
for (int i = 0; i < node_qty; i++) {
reg.chave = rand() % 50;
inserir(reg, tree);
}
print_tree_status(tree);
reset_tree(*tree);
free(tree);
}
void segundo_cen(){
Registro reg;
Ponteiro *tree = (Ponteiro *) malloc(sizeof(Ponteiro));
initialize(tree);
printf("Segundo cenario\n");
reg.chave = 90;
inserir(reg, tree);
print_tree_status(tree);
reg.chave = 55;
inserir(reg, tree);
print_tree_status(tree);
printf("\nRotacao a direita\n");
reg.chave = 42;
inserir(reg, tree);
print_tree_status(tree);
reg.chave = 47;
inserir(reg, tree);
print_tree_status(tree);
printf("\nRotacao a esquerda\n");
reg.chave = 50;
inserir(reg, tree);
print_tree_status(tree);
reg.chave = 42;
delete(reg, tree);
print_tree_status(tree);
printf("\nRotacao dupla a direita\n");
reg.chave = 90;
delete(reg, tree);
print_tree_status(tree);
reg.chave = 52;
inserir(reg, tree);
print_tree_status(tree);
printf("\nRotacao dupla a esquerda\n");
reg.chave = 47;
delete(reg, tree);
print_tree_status(tree);
reset_tree(*tree);
free(tree);
}
int main(int argc, char const *argv[])
{
int continuar=1;
while(continuar != 0)
{
printf("1. primeiro caso\n");
printf("2. segundo caso\n");;
printf("0. Sair\n");
scanf("%d", &continuar);
getc(stdin);
switch (continuar)
{
case 1:
primeiro_cen();
break;
case 2:
segundo_cen();
break;
case 0:
printf ("saindo.....");
break;
}
}
return 0;
}