Implement AVL Tree and its operations
Write a C Program to implement AVL Tree and its operations. Here’s simple Program to implement AVL Tree and its operations like Insertion, Deletion, Traversal and Display in C Programming Language.
What is AVL Tree ?
AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees cannot be more than one for all nodes.
Why AVL Tree?
Most of the BST operations (e.g., search, max, min, insert, delete.. etc) take O(h) time where h is the height of the BST. The cost of these operations may become O(n) for a skewed Binary tree.
If we make sure that height of the tree remains O(Logn) after every insertion and deletion, then we can guarantee an upper bound of O(Logn) for all these operations. The height of an AVL tree is always O(Logn) where n is the number of nodes in the tree
Below is the source code for C Program to implement AVL Tree and its operations which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
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 |
/* C Program to implement AVL Tree and its operations */ #include <stdio.h> #include <stdlib.h> #define FALSE 0 #define TRUE 1 struct node { struct node *lchild; int info; struct node *rchild; int balance; }; void inorder(struct node *ptr); struct node *RotateLeft(struct node *pptr); struct node *RotateRight(struct node *pptr); struct node *insert(struct node *pptr, int ikey); struct node *insert_left_check(struct node *pptr, int *ptaller); struct node *insert_right_check(struct node *pptr, int *ptaller); struct node *insert_LeftBalance(struct node *pptr); struct node *insert_RightBalance(struct node *pptr); struct node *del(struct node *pptr, int dkey); struct node *del_left_check(struct node *pptr, int *pshorter); struct node *del_right_check(struct node *pptr, int *pshorter); struct node *del_LeftBalance(struct node *pptr,int *pshorter); struct node *del_RightBalance(struct node *pptr,int *pshorter); void display(struct node *ptr,int level); int main() { int choice,key; struct node *root = NULL; while(1) { printf("\n"); printf("1.Insert\n"); printf("2.Display\n"); printf("3.Delete\n"); printf("4.Inorder Traversal\n"); printf("5.Quit\n"); printf("\nEnter your choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("\nEnter the key to be inserted : "); scanf("%d",&key); root = insert(root,key); break; case 2: printf("\n"); display(root,0); printf("\n"); break; case 3: printf("\nEnter the key to be deleted : "); scanf("%d",&key); root = del(root,key); break; case 4: inorder(root); break; case 5: exit(1); default: printf("Wrong choice\n"); }/*End of switch */ }/*End of while */ return 0; }/*End of main()*/ void display(struct node *ptr,int level) { int i; if(ptr == NULL )/*Base Case*/ return; else { display(ptr->rchild, level+1); printf("\n"); for (i=0; i<level; i++) printf(" "); printf("%d", ptr->info); display(ptr->lchild, level+1); } }/*End of display()*/ struct node *insert(struct node *pptr, int ikey) { static int taller; if(pptr==NULL) /*Base case*/ { pptr = (struct node *) malloc(sizeof(struct node)); pptr->info = ikey; pptr->lchild = NULL; pptr->rchild = NULL; pptr->balance = 0; taller = TRUE; } else if(ikey < pptr->info) /*Insertion in left subtree*/ { pptr->lchild = insert(pptr->lchild, ikey); if(taller==TRUE) pptr = insert_left_check( pptr, &taller ); } else if(ikey > pptr->info) /*Insertion in right subtree */ { pptr->rchild = insert(pptr->rchild, ikey); if(taller==TRUE) pptr = insert_right_check(pptr, &taller); } else /*Base Case*/ { printf("Duplicate key\n"); taller = FALSE; } return pptr; }/*End of insert( )*/ struct node *insert_left_check(struct node *pptr, int *ptaller ) { switch(pptr->balance) { case 0: /* Case L_A : was balanced */ pptr->balance = 1; /* now left heavy */ break; case -1: /* Case L_B: was right heavy */ pptr->balance = 0; /* now balanced */ *ptaller = FALSE; break; case 1: /* Case L_C: was left heavy */ pptr = insert_LeftBalance(pptr); /* Left Balancing */ *ptaller = FALSE; } return pptr; }/*End of insert_left_check( )*/ struct node *insert_right_check(struct node *pptr, int *ptaller ) { switch(pptr->balance) { case 0: /* Case R_A : was balanced */ pptr->balance = -1; /* now right heavy */ break; case 1: /* Case R_B : was left heavy */ pptr->balance = 0; /* now balanced */ *ptaller = FALSE; break; case -1: /* Case R_C: Right heavy */ pptr = insert_RightBalance(pptr); /* Right Balancing */ *ptaller = FALSE; } return pptr; }/*End of insert_right_check( )*/ struct node *insert_LeftBalance(struct node *pptr) { struct node *aptr, *bptr; aptr = pptr->lchild; if(aptr->balance == 1) /* Case L_C1 : Insertion in AL */ { pptr->balance = 0; aptr->balance = 0; pptr = RotateRight(pptr); } else /* Case L_C2 : Insertion in AR */ { bptr = aptr->rchild; switch(bptr->balance) { case -1: /* Case L_C2a : Insertion in BR */ pptr->balance = 0; aptr->balance = 1; break; case 1: /* Case L_C2b : Insertion in BL */ pptr->balance = -1; aptr->balance = 0; break; case 0: /* Case L_C2c : B is the newly inserted node */ pptr->balance = 0; aptr->balance = 0; } bptr->balance = 0; pptr->lchild = RotateLeft(aptr); pptr = RotateRight(pptr); } return pptr; }/*End of insert_LeftBalance( )*/ struct node *insert_RightBalance(struct node *pptr) { struct node *aptr, *bptr; aptr = pptr->rchild; if(aptr->balance == -1) /* Case R_C1 : Insertion in AR */ { pptr->balance = 0; aptr->balance = 0; pptr = RotateLeft(pptr); } else /* Case R_C2 : Insertion in AL */ { bptr = aptr->lchild; switch(bptr->balance) { case -1: /* Case R_C2a : Insertion in BR */ pptr->balance = 1; aptr->balance = 0; break; case 1: /* Case R_C2b : Insertion in BL */ pptr->balance = 0; aptr->balance = -1; break; case 0: /* Case R_C2c : B is the newly inserted node */ pptr->balance = 0; aptr->balance = 0; } bptr->balance = 0; pptr->rchild = RotateRight(aptr); pptr = RotateLeft(pptr); } return pptr; }/*End of insert_RightBalance( )*/ struct node *RotateLeft(struct node *pptr) { struct node *aptr; aptr = pptr->rchild; /*A is right child of P*/ pptr->rchild = aptr->lchild; /*Left child of A becomes right child of P */ aptr->lchild = pptr; /*P becomes left child of A*/ return aptr; /*A is the new root of the subtree initially rooted at P*/ }/*End of RotateLeft( )*/ struct node *RotateRight(struct node *pptr) { struct node *aptr; aptr = pptr->lchild; /*A is left child of P */ pptr->lchild = aptr->rchild; /*Right child of A becomes left child of P*/ aptr->rchild = pptr; /*P becomes right child of A*/ return aptr; /*A is the new root of the subtree initially rooted at P*/ }/*End of RotateRight( )*/ struct node *del(struct node *pptr, int dkey) { struct node *tmp, *succ; static int shorter; if( pptr == NULL) /*Base Case*/ { printf("Key not present \n"); shorter = FALSE; return(pptr); } if( dkey < pptr->info ) { pptr->lchild = del(pptr->lchild, dkey); if(shorter == TRUE) pptr = del_left_check(pptr, &shorter); } else if( dkey > pptr->info ) { pptr->rchild = del(pptr->rchild, dkey); if(shorter==TRUE) pptr = del_right_check(pptr, &shorter); } else /* dkey == pptr->info, Base Case*/ { /*pptr has 2 children*/ if( pptr->lchild!=NULL && pptr->rchild!=NULL ) { succ = pptr->rchild; while(succ->lchild) succ = succ->lchild; pptr->info = succ->info; pptr->rchild = del(pptr->rchild, succ->info); if( shorter == TRUE ) pptr = del_right_check(pptr, &shorter); } else { tmp = pptr; if( pptr->lchild != NULL ) /*only left child*/ pptr = pptr->lchild; else if( pptr->rchild != NULL) /*only right child*/ pptr = pptr->rchild; else /* no children */ pptr = NULL; free(tmp); shorter = TRUE; } } return pptr; }/*End of del( )*/ struct node *del_left_check(struct node *pptr, int *pshorter) { switch(pptr->balance) { case 0: /* Case L_A : was balanced */ pptr->balance = -1; /* now right heavy */ *pshorter = FALSE; break; case 1: /* Case L_B : was left heavy */ pptr->balance = 0; /* now balanced */ break; case -1: /* Case L_C : was right heavy */ pptr = del_RightBalance(pptr, pshorter); /*Right Balancing*/ } return pptr; }/*End of del_left_check( )*/ struct node *del_right_check(struct node *pptr, int *pshorter) { switch(pptr->balance) { case 0: /* Case R_A : was balanced */ pptr->balance = 1; /* now left heavy */ *pshorter = FALSE; break; case -1: /* Case R_B : was right heavy */ pptr->balance = 0; /* now balanced */ break; case 1: /* Case R_C : was left heavy */ pptr = del_LeftBalance(pptr, pshorter ); /*Left Balancing*/ } return pptr; }/*End of del_right_check( )*/ struct node *del_LeftBalance(struct node *pptr,int *pshorter) { struct node *aptr, *bptr; aptr = pptr->lchild; if( aptr->balance == 0) /* Case R_C1 */ { pptr->balance = 1; aptr->balance = -1; *pshorter = FALSE; pptr = RotateRight(pptr); } else if(aptr->balance == 1 ) /* Case R_C2 */ { pptr->balance = 0; aptr->balance = 0; pptr = RotateRight(pptr); } else /* Case R_C3 */ { bptr = aptr->rchild; switch(bptr->balance) { case 0: /* Case R_C3a */ pptr->balance = 0; aptr->balance = 0; break; case 1: /* Case R_C3b */ pptr->balance = -1; aptr->balance = 0; break; case -1: /* Case R_C3c */ pptr->balance = 0; aptr->balance = 1; } bptr->balance = 0; pptr->lchild = RotateLeft(aptr); pptr = RotateRight(pptr); } return pptr; }/*End of del_LeftBalance( )*/ struct node *del_RightBalance(struct node *pptr,int *pshorter) { struct node *aptr, *bptr; aptr = pptr->rchild; if (aptr->balance == 0) /* Case L_C1 */ { pptr->balance = -1; aptr->balance = 1; *pshorter = FALSE; pptr = RotateLeft(pptr); } else if(aptr->balance == -1 ) /* Case L_C2 */ { pptr->balance = 0; aptr->balance = 0; pptr = RotateLeft(pptr); } else /* Case L_C3 */ { bptr = aptr->lchild; switch(bptr->balance) { case 0: /* Case L_C3a */ pptr->balance = 0; aptr->balance = 0; break; case 1: /* Case L_C3b */ pptr->balance = 0; aptr->balance = -1; break; case -1: /* Case L_C3c */ pptr->balance = 1; aptr->balance = 0; } bptr->balance = 0; pptr->rchild = RotateRight(aptr); pptr = RotateLeft(pptr); } return pptr; }/*End of del_RightBalance( )*/ void inorder(struct node *ptr) { if(ptr!=NULL) { inorder(ptr->lchild); printf("%d ",ptr->info); inorder(ptr->rchild); } }/*End of inorder()*/ |
OUTPUT : :
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 |
/* C Program to implement AVL Tree and its operations */ 1.Insert 2.Display 3.Delete 4.Inorder Traversal 5.Quit Enter your choice : 1 Enter the key to be inserted : 6 1.Insert 2.Display 3.Delete 4.Inorder Traversal 5.Quit Enter your choice : 1 Enter the key to be inserted : 5 1.Insert 2.Display 3.Delete 4.Inorder Traversal 5.Quit Enter your choice : 1 Enter the key to be inserted : 8 1.Insert 2.Display 3.Delete 4.Inorder Traversal 5.Quit Enter your choice : 1 Enter the key to be inserted : 9 1.Insert 2.Display 3.Delete 4.Inorder Traversal 5.Quit Enter your choice : 1 Enter the key to be inserted : 2 1.Insert 2.Display 3.Delete 4.Inorder Traversal 5.Quit Enter your choice : 1 Enter the key to be inserted : 4 1.Insert 2.Display 3.Delete 4.Inorder Traversal 5.Quit Enter your choice : 1 Enter the key to be inserted : 1 1.Insert 2.Display 3.Delete 4.Inorder Traversal 5.Quit Enter your choice : 1 Enter the key to be inserted : 0 1.Insert 2.Display 3.Delete 4.Inorder Traversal 5.Quit Enter your choice : 1 Enter the key to be inserted : 7 1.Insert 2.Display 3.Delete 4.Inorder Traversal 5.Quit Enter your choice : 2 9 8 7 6 5 4 2 1 0 1.Insert 2.Display 3.Delete 4.Inorder Traversal 5.Quit Enter your choice : 3 Enter the key to be deleted : 6 1.Insert 2.Display 3.Delete 4.Inorder Traversal 5.Quit Enter your choice : 2 9 8 7 5 4 2 1 0 1.Insert 2.Display 3.Delete 4.Inorder Traversal 5.Quit Enter your choice : 3 Enter the key to be deleted : 7 1.Insert 2.Display 3.Delete 4.Inorder Traversal 5.Quit Enter your choice : 2 9 8 5 4 2 1 0 1.Insert 2.Display 3.Delete 4.Inorder Traversal 5.Quit Enter your choice : 4 0 1 2 4 5 8 9 1.Insert 2.Display 3.Delete 4.Inorder Traversal 5.Quit Enter your choice : 5 Process returned 1 |
If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.
Thanks for reading the post….