Insertion in avl trees c program
A single left rotation is performed. This type of rotation is identified when a node has a balanced factor as -2, and its right-child has a balance factor as Insert operation is almost the same as in simple binary search trees.
After every insertion, we balance the height of the tree. Insert operation takes O log n worst time complexity. In the above example, insert Step 2 : Once the node is added, the balance factor of each node is updated. After is inserted, the balance factor of every node is updated.
Step 3 : Now check if any node violates the range of the balance factor if the balance factor is violated, then perform rotations using the below case. In the above example, the balance factor of is violated and case 1 becomes applicable there, we perform LL rotation and the tree is balanced again. In the third case when the node has 2 children, we replace the content of the nodes with its successor and it reduces to the deletion of the node with either one child or none.
After the deletion procedure, the heights of ancestor nodes will decrease and this may cause unbalance in the tree.
Let's look at the loss of balances that can happen and how to fix them. Let's assume that the first ancestor which is unbalanced is x and y is its child with greater height among the heights of the two children of x. And z is the child of y with greater height among the children of y.
We fix the unbalancing by performing the required rotations involving the nodes x , y and z as we did in the insertion part. Let's talk about the outside case first in which the node x is unbalanced because of the deletion of a node from the subtree a.
The height of the subtree a is h before deletion and the node x is balanaced. But after deletion, the height of the subtree a is changed to h-1 and the node x is unbalanced now.
Since z is the child of y with larger height, so its height will be h. As y is balanced, the height of b will be either h or h The heights of the nodes c and d can be either h-1 or h However, one of these must have a height of h-1 because the height of node x is h. After deletion, the height of the node a changed to h-1 and thus caused the unbalance on the node x.
To fix this, we have performed a right rotation on the node x as we did in insertion. Now, the node y is the root of the subtree. In this way, the unbalance propagates above the tree in deletion. One thing should be noted that the subtree d can have a height of either h-1 or h. But if it is h equal to its sibling , then both children of y have same heights and there isn't a taller child.
In that case, we will pick the root of the subtree d as z which will lead to the outside case and thus requiring only one rotation to fix the unbalance. However, the reason for picking the root of the subtree d as z and going for the outside case is not to fix the unbalance in a single rotation only but if we would have picked the other node and gone for the double rotation inside case , then it wouldn't fix the unbalance.
This is shown in the following picture. The code for the deletion is pretty much similar to that of binary search tree. So, let's focus on the rebalancing part. Similar to the fixing of insertion, we will start with a loop but we will not stop just after fixing an unbalanced node because we have already discussed that the unbalance might propagate above in case of deletion.
Firstly, we have set y as the taller child of x - if x. Also, the unbalances in the insertion process get fixed after a fixed number of rotations. We can delete any node in constant time i. Username Password min 6 characters Password again Email. Go to Sign Up By signing up or logging in, you agree to our Terms of service and confirm that you have read our Privacy Policy. Recommended Articles. Article Contributed By :.
Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments. What's New. Previous Tutorial:. Next Tutorial:. Share on:. Did you find this article helpful? Sorry about that. How can we improve it? Leave this field blank. Related Tutorials.
0コメント