-
Notifications
You must be signed in to change notification settings - Fork 481
/
AVL.c
39 lines (33 loc) · 990 Bytes
/
AVL.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
/*
Question: Given a positive integer n, find the maximum and the minimum heights of AVL trees with n nodes considering
• The height of an AVL tree with a single node is 0.
• The height of an AVL tree with a single node is 1.
*/
#include<stdio.h>
#include<math.h>
int heightfunc(int h){
if(h == 0){
return 1;
}
if(h == 1){
return 2;
}
return heightfunc(h-1) + heightfunc(h-2) + 1;
}
int main(){
int n, h, temp;
printf("Enter the number of nodes: ");
scanf("%d", &n);
printf("When the height of an AVL tree with a single node is 0");
printf("\nMinimum height of the AVL tree is: %d", (int)(log(n)/log(2)));
for(h = 0; ; h++){
if(heightfunc(h) > n){
printf("\nMaximum height of the AVL tree is: %d \n",h-1);
break;
}
}
printf("\nWhen the height of an AVL tree with a single node is 1");
printf("\nMinimum height of the AVL tree is: %d", ((int)(log(n)/log(2))+1));
printf("\nMaximum height of the AVL tree is: %d",h);
return 0;
}