AVL Tree Insertion
Write a C Program to implement AVL Tree Insertion. Here’s simple Program to implement AVL Tree Insertion 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 Trees?
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 Insertion 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 |
/* C Program to implement AVL Tree Insertion */ #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); 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.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: exit(1); default: printf("Wrong choice\n"); }/*End of switch */ }/*End of while */ return 0; }/*End of main()*/ 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( )*/ 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()*/ |
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 |
/* C Program to implement AVL Tree Insertion */ Enter your choice : 1 Enter the key to be inserted : 6 1.Insert 2.Display 3.Quit Enter your choice : 2 8 7 6 1.Insert 2.Display 3.Quit Enter your choice : 1 Enter the key to be inserted : 5 1.Insert 2.Display 3.Quit Enter your choice : 1 Enter the key to be inserted : 4 1.Insert 2.Display 3.Quit Enter your choice : 2 8 7 6 5 4 1.Insert 2.Display 3.Quit Enter your choice : 1 Enter the key to be inserted : 3 1.Insert 2.Display 3.Quit Enter your choice : 1 Enter the key to be inserted : 2 1.Insert 2.Display 3.Quit Enter your choice : 1 Enter the key to be inserted : 1 1.Insert 2.Display 3.Quit Enter your choice : 2 8 7 6 5 4 3 2 1 1.Insert 2.Display 3.Quit Enter your choice : 1 Enter the key to be inserted : 0 1.Insert 2.Display 3.Quit Enter your choice : 2 8 7 6 5 4 3 2 1 0 1.Insert 2.Display 3.Quit Enter your choice : 1 Enter the key to be inserted : 9 1.Insert 2.Display 3.Quit Enter your choice : 2 9 8 7 6 5 4 3 2 1 0 1.Insert 2.Display 3.Quit Enter your choice : 3 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….