-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavl_tree.c
More file actions
305 lines (253 loc) · 7 KB
/
Copy pathavl_tree.c
File metadata and controls
305 lines (253 loc) · 7 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
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
/** BY @OscarScrooge*/
#include<stdio.h>
#include<stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include "treeChart.h"
NODE* newNode(int id);
NODE* add(NODE* node, int id);
NODE* deleteNode(NODE* root, int id);
NODE *rebalance(NODE *root);
NODE* rightRotation(NODE* node);
NODE* leftRotation(NODE* node);
NODE* rightLeftRotation(NODE* node);
NODE* leftRightRotation(NODE* node);
int getBalance(NODE *node);
int search(NODE *root, int valor);
int heigh(NODE *root);
void options(NODE *root);
int main()
{
NODE *root = NULL;
options(root);
return 0;
}
void options(NODE *root){
int option = 0,i=0;
int toDelete,toFind;
int treeValues[17]={58,43,75,86,65,70,67,73,93,69,25,66,68,47,62,10,60};
printf("\nChoose and option:\n1- Add Node\n2- Delete node\n3- Search node\n4- Exit\nOption: ");
scanf("%d", &option);
switch(option){
case 1:
system("cls");
while(i <17){
root = add(root, treeValues[i]);
chart(root);
i++;
}
system("cls");
options(root);
break;
case 2:
system("cls");
printf("ID of node to delete: ");
scanf("%d", &toDelete);
if(toDelete!=0){
root = deleteNode(root, toDelete);
chart(root);
}
options(root);
break;
case 3:
system("cls");
printf("ID of node to search: ");
scanf("%d", &toFind);
int find = search(root, toFind);
if(find == 0){
printf("ID %d not founded\n", toFind);
}else{
printf("ID %d founded\n", toFind);
}
printf("\nPress enter to continue...\n");
getchar();
getchar();
options(root);
break;
case 4:
system("cls");
printf("Exit\n");
system("exit");
break;
default:
printf("\nChoose the right option\n");
getchar();
getchar();
options(root);
}
}
NODE* add(NODE* node, int id)
{
if (node == NULL){
return(newNode(id));
}else{
if(search(node, id) == 0){
if (id < node->id){
node->leftChild = add(node->leftChild, id);
}else if (id > node->id){
node->rightChild = add(node->rightChild, id);
}
else{
printf("node duplicated\n");
return node;
}
node = rebalance(node);
}else{
printf("node duplicated\n");
printf("\nPress enter to continue...\n");
getchar();
getchar();
}
}
return node;
}
NODE* newNode(int id)
{
NODE* newNode = (NODE*) malloc(sizeof(NODE));
newNode->id = id;
newNode->leftChild = NULL;
newNode->rightChild = NULL;
return(newNode);
}
NODE *rebalance(NODE *root){
NODE *node = root;
int balanceFactor = getBalance(node);
if(balanceFactor < -1){
balanceFactor = getBalance(node->leftChild);
if(balanceFactor <=0){
/*LEFT ROTATION*/
return leftRotation(node);
}else{
/*LEFT-RIGHT ROTATION*/
return rightLeftRotation(node);
}
}else if(balanceFactor >1){
balanceFactor = getBalance(node->rightChild);
if(balanceFactor >=0){
/*LEFT ROTATION*/
return rightRotation(node);
}else{
/*RIGHT-LEFT ROTATION*/
return leftRightRotation(node);
}
}
return node;
}
NODE* deleteNode(NODE* root, int id){
bool bo = false;
NODE *otro=NULL,*aux=NULL,*aux1=NULL;
if(root!=NULL){
if( id < root->id){
root->leftChild= deleteNode(root->leftChild,id);
}else if( id > root->id){
root->rightChild= deleteNode(root->rightChild,id);
}else{
otro = root;
if(otro->rightChild == NULL){
root = otro -> leftChild;
}else if(otro->leftChild == NULL){
root = otro -> rightChild;
}else{
aux = root->leftChild;
bo= false;
while( aux-> rightChild !=NULL){
aux1 = aux;
aux = aux->rightChild;
bo = true;
}
root->id = aux->id;
otro = aux;
if(bo==true){
aux1->rightChild= aux->leftChild;
root->leftChild = rebalance( root->leftChild);
}else{
root->leftChild = aux->leftChild;
}
}
free(otro);
}
if(root!= NULL){
root = rebalance(root);
}
}else{
printf("\nID not founded\n");
}
return root;
}
int heigh(NODE *root){
int h=0;
if(root!=NULL){
int hi=0,hd=0;
hi++;
hi+=heigh(root->leftChild);
hd++;
hd+=heigh(root->rightChild);
if(hi>hd){
h = hi;
}else{
h= hd;
}
}
return h;
}
NODE* rightRotation(NODE* node){
NODE* node1;
node1 = node->rightChild;
node->rightChild = node1->leftChild;
node1 ->leftChild = node;
return node1;
}
NODE* leftRotation(NODE* node){
NODE* node1;
node1 = node ->leftChild;
node->leftChild = node1->rightChild;
node1 ->rightChild = node;
return node1;
}
NODE* rightLeftRotation(NODE* node){
NODE* node1;
NODE* node2;
node1 = node ->leftChild;
node2 = node1 -> rightChild;
node1->rightChild = node2->leftChild;
node2 ->leftChild = node1;
node->leftChild = node2->rightChild;
node2->rightChild = node;
return node2;
}
NODE* leftRightRotation(NODE* node){
NODE* node1;
NODE* node2;
node1 = node ->rightChild;
node2 = node1 -> leftChild;
node1->leftChild = node2->rightChild;
node2 ->rightChild = node1;
node->rightChild = node2->leftChild;
node2->leftChild = node;
return node2;
}
int getBalance(NODE *node)
{
if (node == NULL){
return 0;
}
int al;
al= heigh(node->rightChild) - heigh(node->leftChild) ;
return al;
}
int search(NODE *root, int valor){
if(root == 0){
return 0;
}else{
if(root->id == valor){
return 1;
}else{
if(valor > root->id && root->rightChild != 0){
return search(root->rightChild, valor);
}else if(valor < root->id && root->leftChild != 0){
return search(root->leftChild, valor);
}
}
}
return 0;
}