Data Structures Lab 11 AVL Trees
Data Structures Lab 11 AVL Trees
To restore the AVL tree property, we start at the insertion point and keep going to the root of the tree.
While moving to the root, we need to consider the first node that is not satisfying the AVL
property. From that node onwards, every node on the path to the root will have the issue.
Also, if we fix the issue for that first node, then all other nodes on the path to the root will automatically
satisfy the AVL tree property. That means we always need to care for the first node that is not satisfying the
AVL property on the path from the insertion point to the root and fix it.
Types of Violations
Let us assume the node that must be rebalanced is X. X is the first node on the path from new node to
root, which has its balance factor violated
Since any node has at most two children, and a height imbalance requires that X’s two sub-tree heights
differ by two, we can observe that a violation might occur in four cases:
1. An insertion into the left subtree of the left child of X (LL case) – Single Rotation
2. An insertion into the right subtree of the left child of X. (LR case) – Double Rotation
3. An insertion into the left subtree of the right child of X (RL case) – Double Rotation
4. An insertion into the right subtree of the right child of X (RR case) – Single Rotation
Cases 1 and 4 are symmetric and easily solved with single rotations.
Similarly, cases 2 and 3 are also symmetric and can be solved with double rotations (needs two single
rotations).
Case – 1 Left Left Rotation (LL rotation – Right Rotate)
Node X is unbalanced because of an insertion into its left child’s left sub-tree
Perform a Right rotation - LL rotation at X i.e. make X’s left child - W as its parent, and X becomes W’s
right child
Example : 9 has become unbalanced after insertion of 7, hence perform one LL rotation-right rotate at 9
unbalanced
LL rotate at 9
Right rotate at 9
Case – 1 Left Left Rotation (LL rotation – Right Rotate)
ALGORITHM:
struct Node *LLRotation(struct Node *p)
{
struct Node *pl=p->lchild;
struct Node *plr=pl->rchild;
pl->rchild=p;
p->lchild=plr;
p->height=NodeHeight(p);
pl->height=NodeHeight(pl);
if(root==p)
root=pl;
return pl;
}
We can also call this function as : Node* RotateRight(Node *x)
Case – 4 Right Right Rotation (RR rotation – Left Rotate)
Node X is unbalanced because of an insertion into its right child’s right sub-tree
Perform a Left rotation - RR rotation
Example : 15 has become unbalanced after insertion of 29, hence perform one RR rotation-Left rotate at
15
unbalanced
RR rotate at 15
Left rotate at 15
Case – 4 Right Right Rotation (RR rotation – Left Rotate)
ALGORITHM:
struct Node *RRRotation(struct Node *p)
{
struct Node *pr=p->rchild;
struct Node *prl=pr->lchild;
pr->lchild = p;
p->rchild = prl;
// Update height
p->height = NodeHeight(p);
pr->height = NodeHeight(pr);
// Update root
if (root == p){
root = pr;
}
return pr;
}
We can also call this function as : Node* RotateLeft(Node *x)
Case – 2 Left Right Rotation (LR rotation)
Node Z is unbalanced because of an insertion into its left child’s right subtree (Double Rotate)
Perform a RR rotation(left rotate) at X(left child of Z) and then a LL rotation(right rotate) at Z
unbalanced
Case – 2 Left Right Rotation (LR rotation)
Example : 8 has become unbalanced after insertion of 7,
7 has been inserted into node 8’s left child’s – Right sub-tree
One RR (left) Rotation at node 5 ( left child of 8)
One LL(right) Rotation at node 8
unbalanced
LL rotate at the
RR rotate at left node - 8
child - 5
right rotate at 8
Left rotate at 5
Case – 3 Right Left Rotation (RL rotation)
Node X is unbalanced because of an insertion into its right child’s left subtree (Double Rotate)
Perform a LL rotation(right rotate) at Z(Right child of X) and then a RR rotation(left rotate) at X
unbalanced
Case – 3 Right Left Rotation (RL rotation)
Example : 4 has become unbalanced after insertion of 5,
5 has been inserted into node 4’s Right child’s – Left sub-tree
One LL (right) Rotation at node 7 ( right child of 4)
One RR(left) Rotation at node 4
unbalanced
RR rotate at the
LL rotate at
node - 4
right child - 7
Left rotate at 4
Right rotate at 7
AVL Insertion Example 1
• Given an AVL Tree:
unbalanced
• Case 1 : LL violation because 2 has been inserted into the left subtree of 8’s left child
• LL Rotate ( Right rotate) at 8
AVL Insertion Example 2
• Given an AVL Tree:
• Insert 7 : node 10 becomes unbalance( BF=2) , because of insertion into its left child’s right
sub-tree ( Case 2 : LR Rotation) – Double rotations
i) Perform a RR rotation(left rotate) at left child of 10 i.e 5
ii) Perform a LL rotation(right rotate) at node 10
AVL Insertion Example 2
b.If the balance factor is less than -1,then the current node is unbalanced
Find the type of violation, perform rotations to balance the tree.
– Get the balance factor (BF) of the node’s right sub-tree
» If the BF is <=0 the its right-right case
» Else it is right- left case
Example 1
Delete node 32