Here you will get program for AVL tree in C.
An AVL (Adelson-Velskii and Landis) tree is a height balance tree. These trees are binary search trees in which the height of two siblings are not permitted to differ by more than one. i.e. [Height of the left subtree – Height of right subtree] <= 1.
A C program is given below which performs various operations like creation, insertion, deletion and printing for an AVL tree.
Program for AVL Tree in 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 |
#include<stdio.h> typedef struct node { int data; struct node *left,*right; int ht; }node; node *insert(node *,int); node *Delete(node *,int); void preorder(node *); void inorder(node *); int height( node *); node *rotateright(node *); node *rotateleft(node *); node *RR(node *); node *LL(node *); node *LR(node *); node *RL(node *); int BF(node *); int main() { node *root=NULL; int x,n,i,op; do { printf("\n1)Create:"); printf("\n2)Insert:"); printf("\n3)Delete:"); printf("\n4)Print:"); printf("\n5)Quit:"); printf("\n\nEnter Your Choice:"); scanf("%d",&op); switch(op) { case 1: printf("\nEnter no. of elements:"); scanf("%d",&n); printf("\nEnter tree data:"); root=NULL; for(i=0;i<n;i++) { scanf("%d",&x); root=insert(root,x); } break; case 2: printf("\nEnter a data:"); scanf("%d",&x); root=insert(root,x); break; case 3: printf("\nEnter a data:"); scanf("%d",&x); root=Delete(root,x); break; case 4: printf("\nPreorder sequence:\n"); preorder(root); printf("\n\nInorder sequence:\n"); inorder(root); printf("\n"); break; } }while(op!=5); return 0; } node * insert(node *T,int x) { if(T==NULL) { T=(node*)malloc(sizeof(node)); T->data=x; T->left=NULL; T->right=NULL; } else if(x > T->data) // insert in right subtree { T->right=insert(T->right,x); if(BF(T)==-2) if(x>T->right->data) T=RR(T); else T=RL(T); } else if(x<T->data) { T->left=insert(T->left,x); if(BF(T)==2) if(x < T->left->data) T=LL(T); else T=LR(T); } T->ht=height(T); return(T); } node * Delete(node *T,int x) { node *p; if(T==NULL) { return NULL; } else if(x > T->data) // insert in right subtree { T->right=Delete(T->right,x); if(BF(T)==2) if(BF(T->left)>=0) T=LL(T); else T=LR(T); } else if(x<T->data) { T->left=Delete(T->left,x); if(BF(T)==-2) //Rebalance during windup if(BF(T->right)<=0) T=RR(T); else T=RL(T); } else { //data to be deleted is found if(T->right!=NULL) { //delete its inorder succesor p=T->right; while(p->left!= NULL) p=p->left; T->data=p->data; T->right=Delete(T->right,p->data); if(BF(T)==2)//Rebalance during windup if(BF(T->left)>=0) T=LL(T); else T=LR(T);\ } else return(T->left); } T->ht=height(T); return(T); } int height(node *T) { int lh,rh; if(T==NULL) return(0); if(T->left==NULL) lh=0; else lh=1+T->left->ht; if(T->right==NULL) rh=0; else rh=1+T->right->ht; if(lh>rh) return(lh); return(rh); } node * rotateright(node *x) { node *y; y=x->left; x->left=y->right; y->right=x; x->ht=height(x); y->ht=height(y); return(y); } node * rotateleft(node *x) { node *y; y=x->right; x->right=y->left; y->left=x; x->ht=height(x); y->ht=height(y); return(y); } node * RR(node *T) { T=rotateleft(T); return(T); } node * LL(node *T) { T=rotateright(T); return(T); } node * LR(node *T) { T->left=rotateleft(T->left); T=rotateright(T); return(T); } node * RL(node *T) { T->right=rotateright(T->right); T=rotateleft(T); return(T); } int BF(node *T) { int lh,rh; if(T==NULL) return(0); if(T->left==NULL) lh=0; else lh=1+T->left->ht; if(T->right==NULL) rh=0; else rh=1+T->right->ht; return(lh-rh); } void preorder(node *T) { if(T!=NULL) { printf("%d(Bf=%d)",T->data,BF(T)); preorder(T->left); preorder(T->right); } } void inorder(node *T) { if(T!=NULL) { inorder(T->left); printf("%d(Bf=%d)",T->data,BF(T)); inorder(T->right); } } |
Output
1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:
Enter Your Choice:1
Enter no. of elements:4
Enter tree data:7 12 4 9
1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:
Enter Your Choice:4
Preorder sequence:
7(Bf=-1)4(Bf=0)12(Bf=1)9(Bf=0)
Inorder sequence:
4(Bf=0)7(Bf=-1)9(Bf=0)12(Bf=1)
1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:
Enter Your Choice:3
Enter a data:7
1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:
Enter Your Choice:4
Preorder sequence:
9(Bf=0)4(Bf=0)12(Bf=0)
Inorder sequence:
4(Bf=0)9(Bf=0)12(Bf=0)
1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:
Enter Your Choice:5
what the function BF will do?
BF function is to calculate the difference of level between left and right subtrees
i need real easy program
Thanks for the implementation.
Hey.. I wan to write an iterative C function that displays the integer values of a given AVL tree ..if somebody can help would be perfect ?
Not working properly
if i select create node and enter 5 elements 1 to 5 in order then it is giving the wrong values of BF for each node
yes..you are right…it is not working for that case.
first check it…its working.
Its working yaar
there is a mistake regarding the content of the avl tree
Does anyone know a function to count the number of rotations of an avl tree?
hello my windows is closing while im using delete and indicating that it has a problm with program
can i get the algorithm for this program????pls
The height() function is not proper.
if(T->left==NULL)
lh=1; //this programmer uses lh=0 which is wrong
Same goes for T->right==NULL.
Hope this helps.
Is this self-balancing tree?
[Error] ‘malloc’ was not declared in this scope
Can someone help me? What should I do with this error?
At the beginning insert the sentence below:
#include
very nice, just one comment: if there is malloc, there also should be free.
The free should be near the end of the Delete function:
instead of
else
return(T->left);
the following should be used:
else {
node *TL=T->left;
free (T);
return(TL);
}
How to calculate the stack size for this?