forked from shivaylamba/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVL tree.c
491 lines (461 loc) · 9.05 KB
/
AVL 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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//geeksforgeeks
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int i;
int h;
struct node *left;
struct node *right;
}node;
node *search(node *root,int a)
{
node *temp=root;
while(1)
{
if(temp=='\0')
return '\0';
if(temp->i==a)
return temp;
if(a<temp->i)
temp=temp->left;
else
{
temp=temp->right;
}
}
}
node *parent(node *root,int a)
{
node *t=root;
node *p=root;
if(t->i==a)
return '\0';
while(t->i!=a)
{
if(a<t->i)
{
p=t;
t=t->left;
}
else
{
p=t;
t=t->right;
}
}
return p;
}
int max(int lh,int rh)
{
int ht=(lh>rh?lh:rh);
return ht;
}
int height(node *root)
{
if(root=='\0')
return 0;
return root->h;
}
int balance(node *root)
{
if(root=='\0')
return 0;
return (height(root->left)-height(root->right));
}
node *right_rotation(node *root)
{
node *x,*y,*z;
x=root;
y=root->left;
z=y->right;
y->right=x;
x->left=z;
x->h=1+max(height(x->left),height(x->right));
y->h=1+max(height(y->left),height(y->right));
return y;
}
node *left_rotation(node *root)
{
node *x,*y,*z;
x=root;
y=root->right;
z=y->left;
y->left=x;
x->right=z;
x->h=1+max(height(x->left),height(x->right));
y->h=1+max(height(y->left),height(y->right));
return y;
}
node *insert(node *root, int a)
{
if(root=='\0')
{
node *new=(node *)malloc(sizeof(node));
new->i=a;
new->h=1;
new->left='\0';
new->right='\0';
return new;
}
if(a<root->i)
root->left=insert(root->left,a);
else
{
if(a>root->i)
root->right=insert(root->right,a);
else
return root;
}
root->h=1+max(height(root->left),height(root->right));
int bf;
bf= balance(root);
if(bf>1 && a<root->left->i)
{
return right_rotation(root);
}
if(bf>1 && a>root->left->i)
{
root->left=left_rotation(root->left);
return right_rotation(root);
}
if(bf<-1 && a>root->right->i)
{
return left_rotation(root);
}
if(bf<-1 && a<root->right->i)
{
root->right=right_rotation(root->right);
return left_rotation(root);
}
return root;
}
node *min(node *root)
{
node *temp=root;
while(temp->left!='\0')
{
temp=temp->left;
}
return temp;
}
node* delete(node* root, int a)
{
if (root == NULL)
return root;
if ( a < root->i)
root->left = delete(root->left, a);
else if( a > root->i )
root->right = delete(root->right, a);
else
{
if( (root->left == NULL) || (root->right == NULL) )
{
node *temp = root->left?root->left:root->right;
if (temp == NULL)
{
temp = root;
root = NULL;
}
else
{
root->i=temp->i;
root->left='\0';
root->right='\0';
}
free(temp);
}
else
{
node* temp = min(root->right);
root->i= temp->i;
root->right = delete(root->right, temp->i);
}
}
if (root == NULL)
return root;
root->h = 1 + max(height(root->left),height(root->right));
int bf = balance(root);
if (bf > 1 && balance(root->left) >= 0)
return right_rotation(root);
if (bf > 1 && balance(root->left) < 0)
{
root->left = left_rotation(root->left);
return right_rotation(root);
}
if (bf < -1 && balance(root->right) <= 0)
return left_rotation(root);
if (bf < -1 && balance(root->right) > 0)
{
root->right = right_rotation(root->right);
return left_rotation(root);
}
return root;
}
int depth(node *root,int a, node *t)
{
int c=1;
while(t!=root)
{
t=parent(root,t->i);
c++;
}
return c;
}
void inorder(node *root)
{
if(root=='\0')
return;
inorder(root->left);
printf("%d ",root->i);
inorder(root->right);
}
void preorder(node *root)
{
if(root=='\0')
return;
printf("%d ",root->i);
preorder(root->left);
preorder(root->right);
}
void postorder(node *root)
{
if(root=='\0')
return;
postorder(root->left);
postorder(root->right);
printf("%d ",root->i);
}
void levelprint(node *root,int a)
{
if(root=='\0')
return;
if(a==1)
printf("%d ",root->i);
else
{
levelprint(root->left,a-1);
levelprint(root->right,a-1);
}
}
void levelorder(node *root)
{
int i,h;
h=height(root);
for(i=1;i<=h;i++)
{
levelprint(root,i);
}
}
node *successor(node *root,int a,node *s)
{
node *temp=s->right;
if(temp!='\0')
{
while(temp->left!='\0')
temp=temp->left;
return temp;
}
else
{
while(1)
{
s=parent(root,s->i);
if(s->i>a)
{
return s;
}
if(root==s)
return '\0';
}
}
}
node *predecessor(node *root,int a,node *s)
{
node *temp=s->left;
if(temp!='\0')
{
while(temp->right!='\0')
temp=temp->right;
return temp;
}
else
{
while(1)
{
s=parent(root,s->i);
if(s->i<a)
{
return s;
}
if(root==s)
return '\0';
}
}
}
node *lca(node *root,node *t,node *s)
{
while(1)
{
if(root->i > t->i && root->i > s->i)
root=root->left;
else
if(root->i < t->i && root->i < s->i)
root=root->right;
else
break;
}
return root;
}
int main()
{
node *root='\0',*t,*p,*q;
int a,j,n,m,b,h;
printf("1.insert an element in AVL tree\n");
printf("2.Delete an element from the AVL tree\n");
printf("3.Search an element from the given AVL tree\n");
printf("4.inorder\n");
printf("5.preorder\n");
printf("6.postorder\n");
printf("7.levelorder\n");
printf("8.height of the AVL tree\n");
printf("9.height of a given node of the AVL tree\n");
printf("10.depth of a given node of the AVL tree\n");
printf("11.predecessor of a given node of the AVL tree\n");
printf("12.the successor of a given node of the AVL tree\n");
printf("13.the least common ancestor of two given nodes of the AVL tree\n");
printf("14.exit\n");
while(1)
{
printf("enter the key for above operation = ");
scanf("%d",&n);
if(n==14)
break;
switch(n)
{
case 1:
printf("enter the element = ");
scanf("%d",&a);
root=insert(root,a);
printf("element is inserted\n");
break;
case 2:
if(root=='\0')
{
printf("AVL tree is null");
break;
}
printf("enter the element which you want to delete = ");
scanf("%d",&a);
t= search(root,a);
if(t=='\0'){
printf("%d is not found in BST\n",a);
break;}
printf("%d is deleted",t->i);
root=delete(root,a);
break;
case 3:
printf("enter the element which you want to search = ");
scanf("%d",&a);
t=search(root,a);
if(t=='\0')
printf("%d is not found\n",a);
else
printf("%d is found",t->i);
break;
case 4:
if(root=='\0')
printf("tree is null");
else
inorder(root);
break;
case 5:
if(root=='\0')
printf("tree is null");
else
preorder(root);
break;
case 6:
if(root=='\0')
printf("tree is null");
else
postorder(root);
break;
case 7:
if(root=='\0')
printf("tree is null");
else
levelorder(root);
break;
case 8:
h=height(root);
if(h==0)
printf("tree is null");
else
printf("the height of tree is %d",h);
break;
case 9:
printf("enter the element = ");
scanf("%d",&a);
t=search(root,a);
if(t=='\0')
printf("%d is not found",a);
else
printf("the height of element is %d",height(t));
break;
case 10:
printf("enter the element = ");
scanf("%d",&a);
t=search(root,a);
if(t=='\0')
printf("%d is not found",a);
else
{
printf("the depth of element is %d",depth(root,a,t));
}
break;
case 11:
printf("enter the element which you want to find predecessor = ");
scanf("%d",&a);
t=search(root,a);
if(t=='\0')
printf("%d is not found",a);
else{
p=predecessor(root,a,t);
if(p=='\0')
printf("predecessor of %d does not exist",a);
else
printf("predecessor of %d is %d",a,p->i);
}
break;
case 12:
printf("enter the element which you want to find successor = ");
scanf("%d",&a);
t=search(root,a);
if(t=='\0')
printf("%d is not found",a);
else{
p=successor(root,a,t);
if(p=='\0')
printf("successor of %d does not exist",a);
else
printf("successor of %d is %d",a,p->i);
}
break;
case 13:
printf("enter the elements\n");
scanf("%d %d",&a,&b);
t=search(root,a);
p=search(root,b);
if(t=='\0'||p=='\0')
printf("lca can't be find");
else
{
q=lca(root,t,p);
printf("%d is lca",q->i);
}
break;
}
printf("\n\n");
}
return 0;
}