AVL Tree: Insertion
Insertion In AVL Tree
on this page we will discuss about insertion in AVL Tree in C . AVL tree is self balancing tree in which for all nodes, the difference of height between the left subtree and the right subtree is less than or equal to 1. In this article, an avl tree is created and the difference of height is printed for each node.
Insertion and Creation of an AVL Tree
- A new node can be inserted in an AVL tree by determining the correct position of the node. But insertion of a new node into the tree may affect the height of the tree and the tree might become unbalanced.
- If the new nodes are inserted as child nodes on a non- leaf node there will be no alteration since there will be no effect on the balancing as there is no increase in the height of the tree.
- If the new nodes are inserted as child nodes on a leaf node, the balancing of the tree might get distorted. This depends on whether the node is added to the left subtree or the right subtree.
- Thus to re-balance the tree in order to conserve it’s characteristics we use Rotation Mechanism. There are four types of rotations which help maintain the balancing of the tree as specified above.
- If, due to insertion,the b_fact of multiple nodes is disturbed, balancing occurs on the first ancestor of the node that has been inserted.
Note: During the process of insertion it is important to check the balancing factor of each node at each step, until the process end.
Let us understand with the help of an example. Consider we have following elements given to us: [ 15 , 18 , 12 , 8 , 54 , 5 , 14 , 13 , 9 , 60].
Step 1:
- Insert the first element 15.
- Check for the b_fact of the node.
- b_fact= 0
Step 2:
- Insert the next element 18.
- Check the b_fact each node.
Step 3:
- Insert the next element .
- Check for the b_fact each node.
- The b_fact of each node in this case is 0.
Step 4:
- Insert the next element 8.
- Check for the b_fact of each node.
Step 5:
- Insert the next element 54.
- Check if the b_fact is anything other than -1 , 0 , 1.
Step 6:
- Insert the next element 5.
- Check the b_fact of each node.
- We can see the b_fact of the node 12 is 2. The tree becomes unbalanced.
- We have to balance the tree by identifying the rotation mechanism to be applied.
Step 7:
- We observe the element is inserted to the ,left of the left subtree.
- Thus we have to apply LL Rotation.
- The nodes involved in the rotation are shown in red.
Step 8:
- In order to balance the tree, the tree is rotated towards the right.
- In this case the new parent is 8 and the nodes 5 and 12 becomes the child nodes.
Step 9:
- The next element inserted in the tree is 14.
- Again check the b_fact of each node of the tree.
Step 10:
- The next element to be inserted in the tree is 13.
- After the insertion of the new node, we check for the b_fact of each node.
- Now due to the latest insertion to the tree we can see that the balancing factor of multiple nodes is distorted
- We look for the first ancestor on which the balance factor is disturbed.
- We identify the rotation mechanism to be used.
Step 11:
- To balance the tree we use the RL Rotation Mechanism.
- We identify the nodes that are involved in thre rotation mechanism which are shown in red.
Step 12:
- In this scenario, first a right rotation is applied.
- The tree after right rotation is shown as follows.
Step 13:
- Now to balance the tree, we apply a left rotation.
- After the rotation, the node 13 becomes the new parent node and the nodes 12 and 14 become the new child nodes.
- If we check the balance factor for each node, the balance factor is eithe 0 , 1 or -1.
Step 14:
- The next element to be inserted is 9.
- As the new element is inserted, we observe that the balance factor of the node 8 becomes (-2).
Step 15:
- We can see from the image that for balancing the tree we need to apply RL Rotation.
- Again, first a right rotation is applied and then a left rotation is applied to the tree structure.
Step 16:
- After applying the RL Rotation, the tree structure with the optimal b_fact is shown as follows.
- We can see that the b_fact ranges between 0 , 1 and -1.
- Thus it is an AVL tree.
Step 17:
- The next element to be inserted is 60.
- As 60 is inserted the b_fact of the nodes of right subtree is disturbed.
Step 18:
- Since the disturbance in the balance factor is due to the insertion of an element to the right of the right subtree, we use RR Rotation.
Step 19:
- After applying the RR Rotation the tree so obtained is shown below.
- All the nodes satisfy the condition of an AVL tree.
- The final tree at the end of insertion process with balanced nodes is shown below.
C Code For Insertion of an AVL Tree
Run
// C program to insert a node in AVL tree #include<stdio.h> #include<stdlib.h> // An AVL tree node struct Node { int key; struct Node *left; struct Node *right; int height; }; // A utility function to get the height of the tree int height(struct Node *N) { if (N == NULL) return 0; return N->height; } // A utility function to get maximum of two integers int max(int a, int b) { return (a > b)? a : b; } /* Helper function that allocates a new node with the given key and NULL left and right pointers. */ struct Node* newNode(int key) { struct Node* node = (struct Node*) malloc(sizeof(struct Node)); node->key = key; node->left = NULL; node->right = NULL; node->height = 1; // new node is initially added at leaf return(node); } // A utility function to right rotate subtree rooted with y // See the diagram given above. struct Node *rightRotate(struct Node *y) { struct Node *x = y->left; struct Node *T2 = x->right; // Perform rotation x->right = y; y->left = T2; // Update heights y->height = max(height(y->left), height(y->right)) + 1; x->height = max(height(x->left), height(x->right)) + 1; // Return new root return x; } // A utility function to left rotate subtree rooted with x // See the diagram given above. struct Node *leftRotate(struct Node *x) { struct Node *y = x->right; struct Node *T2 = y->left; // Perform rotation y->left = x; x->right = T2; // Update heights x->height = max(height(x->left), height(x->right)) + 1; y->height = max(height(y->left), height(y->right)) + 1; // Return new root return y; } // Get Balance factor of node N int getBalance(struct Node *N) { if (N == NULL) return 0; return height(N->left) - height(N->right); } // Recursive function to insert a key in the subtree rooted // with node and returns the new root of the subtree. struct Node* insert(struct Node* node, int key) { /* 1. Perform the normal BST insertion */ if (node == NULL) return(newNode(key)); if (key < node->key) node->left = insert(node->left, key); else if (key > node->key) node->right = insert(node->right, key); else // Equal keys are not allowed in BST return node; /* 2. Update height of this ancestor node */ node->height = 1 + max(height(node->left), height(node->right)); /* 3. Get the balance factor of this ancestor node to check whether this node became unbalanced */ int balance = getBalance(node); // If this node becomes unbalanced, then // there are 4 cases // Left Left Case if (balance > 1 && key < node->left->key) return rightRotate(node); // Right Right Case if (balance < -1 && key > node->right->key) return leftRotate(node); // Left Right Case if (balance > 1 && key > node->left->key) { node->left = leftRotate(node->left); return rightRotate(node); } // Right Left Case if (balance < -1 && key < node->right->key) { node->right = rightRotate(node->right); return leftRotate(node); } /* return the (unchanged) node pointer */ return node; } // A utility function to print preorder traversal // of the tree. // The function also prints height of every node void preOrder(struct Node *root) { if(root != NULL) { printf("%d ", root->key); preOrder(root->left); preOrder(root->right); } } /* Driver program to test above function*/ int main() { struct Node *root = NULL; /* Constructing tree given in the above figure */ root = insert(root, 5); root = insert(root, 8); root = insert(root, 11); root = insert(root, 12); root = insert(root, 18); root = insert(root, 17); /* The constructed AVL Tree would be 12 / \ 8 18 / \ / 5 11 17 */ printf("Preorder traversal of the constructed AVL" " tree is \n"); preOrder(root); return 0; }
Output
Preorder traversal of the constructed AVL tree is 12 8 5 11 18 17
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
Binary Search Trees
Traversals
- Traversal in Trees
- Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
- Tree Traversals: Depth First Search (DFS) : C | C++ | Java
- Construct a Binary Tree from Postorder and Inorder
B – Trees
AVL Trees
- AVL Trees
Complete Programs for Trees
- Depth First Traversals – C | C++ | Java
- Level Order Traversal – C | C++ | Java
- Construct Tree from given Inorder and Preorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Inorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Preorder traversal – C | C++ | Java
- Find size of the Binary tree – C | C++ | Java
- Find the height of binary tree – C | C++ | Java
- Find maximum in binary tree – C | C++ | Java
- Check whether two tree are identical- C| C++| Java
- Spiral Order traversal of Tree- C | C++| Java
- Level Order Traversal Line by Line – C | C++| Java
- Hand shaking lemma and some Impotant Tree Properties.
- Check If binary tree if Foldable or not.- C| C++| Java
- check whether tree is Symmetric – C| C++| Java.
- Check for Children-Sum in Binary Tree- C|C++| Java
- Sum of all nodes in Binary Tree- C | C++ | Java
- Lowest Common Ancestor in Binary Tree- C | C++ | Java
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
Binary Search Trees
Traversals
- Traversal in Trees
- Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
- Tree Traversals: Depth First Search (DFS) : C | C++ | Java
- Construct a Binary Tree from Postorder and Inorder
B – Trees
AVL Trees
- AVL Trees
Complete Programs for Trees
- Depth First Traversals – C | C++ | Java
- Level Order Traversal – C | C++ | Java
- Construct Tree from given Inorder and Preorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Inorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Preorder traversal – C | C++ | Java
- Find size of the Binary tree – C | C++ | Java
- Find the height of binary tree – C | C++ | Java
- Find maximum in binary tree – C | C++ | Java
- Check whether two tree are identical- C| C++| Java
- Spiral Order traversal of Tree- C | C++| Java
- Level Order Traversal LIne by Line – C | C++| Java
- Hand shaking lemma and some Impotant Tree Properties.
- Check If binary tree if Foldable or not.- C| C++| Java
- check whether tree is Symmetric C| C++| Java.
- Check for Children-Sum in Binary Tree- C|C++| Java
- Sum of all nodes in Binary Tree- C | C++ | Java
- Lowest Common Ancestor in Binary Tree. C | C++ | Java
Login/Signup to comment