AVL Tree
Problem with Binary search tree
• The disadvantage of a binary search tree is that its height can be as large
as N-1
• This means that the time needed to perform insertion and deletion and
many other operations can be N times in the worst case
• We want a tree with small height
• A binary tree with N node has height at least log2 ( N+1)-1
• Thus, our goal is to keep the height of a binary search tree log2 ( N) times
• Such trees are called balanced binary search trees. Examples are AVL tree,
red-black tree.
Binary search tree
balanced Tree
Unbalanced Tree
AVL
• There is a need to maintain the binary search tree to be of balanced
height, so that it is possible to obtain for the search option of log2 ( N)
time in the worst case.
• One of the most popular balanced tree was introduced by Adelson velskii
and landis (AVL)
AVL Tree
• An empty binary search tree is an AVL tree
• A non empty binary tree T is an AVL tree iff TL (left subtree) and TR (right
subtree ) of T and h(TL ) (height of left subtree) and h( TR ) (height of right
subtree ) where | h(TL ) - h( TR ) | <= 1
• Balance factor BF is difference between h(TL ) and h( TR ) and the value will
be -1 0 1
BF = h(TL ) - h( TR )
50
BF = 2-1 =1
h=3 h=2
TL TR
5
Not AVL Tree
3
1 4
3 8 AVL Tree
1 4 10
Rotations
• Left to left: Inserted node is in the left subtree of left subtree of node A
• Right to right: Inserted node is in the right subtree of right subtree of node
A
• Right to left: Inserted node is in the right subtree of left subtree of node A
• Left of right: Inserted node is in the left subtree of right subtree of node A
• Trick to Solve
1. For LL and RR find A and B and make A as child of B
2. For LR and RL find A , B and C and make A, B as child of C
Insertion in left child of left subtree
Single Rotation
V
U
U
V
Z X
Y Z
X After Rotation
Before Rotation
5 5 U
8 8 Z
3 3 V
1 4 4
1 Y
AVL Tree X
0.8
Insert 0.8
3
5
1 After Rotation
4 8
0.8
Double Rotation
Suppose, imbalance is due to an insertion in the left subtree of right child
Single Rotation does not work!
U W
Before Rotation
After Rotation
V V U
D
W
A A B C D
B C
5 5 U
D
V
8
3 3
8
1 4 A 1 4 W
AVL Tree B 3.5
Insert 3.5
4
5 After Rotation
3
8
3.5
0.8
Extended Example
Insert 3,2,1,4,5,6,7, 16,15,14
3 2
3
3
2 1
Fig 1 2 3
Fig 4
Fig 2
2 1 2
Fig 3
1 3
1 3
Fig 5 Fig 6 4
4 5
2
2
1 4
1 4
3 5
3 5
Fig 8
Fig 7 6
4
4
2 5
2 5
1 3 6
1 3 6
4
Fig 10 7
Fig 9
2 6
1 3 7
5 Fig 11
4 4
2 6 2 6
1 3 7 1 3 7
5 16 5 16
Fig 12
Fig 13
15
4
2 6
1 3 15
5
16
Fig 14 7
4 4
2 2 7
6
15 1 3 15
1 3 5 6
16
7 5 14 16
Fig 15
14 Fig 16
AVL Tree Deletion
• Similar to insertion: do the delete and then rebalance
– Rotations and double rotations
– Imbalance may propagate upward so rotations at multiple nodes along path
to root may be needed (unlike with insert)
• Simple example: a deletion on the right causes the left-left grandchild
to be too tall
Properties of BST delete
We first do the normal BST deletion:
– 0 children: just delete it
– 1 child: delete it, connect child to parent
– 2 children: put successor in your place, delete successor leaf
Which nodes’ heights may have changed:
– 0 children: path from deleted node to root
– 1 child: path from deleted node to root
– 2 children: path from deleted successor leaf to root
Will rebalance as we return along the “path in question” to the
root
Case #1 Left-left due to right deletion
• Start with some subtree where if right child becomes shorter we are
unbalanced due to height of left-left grandchild
• A delete in the right child could cause this right-side shortening
Case #1: Left-left due to right deletion
• Same single rotation as when an insert in the left-left grandchild caused imbalance
due to X becoming taller
• But here the “height” at the top decreases, so more rebalancing farther up the tree
might still be necessary
Case #2: Left-right due to right deletion
• Same double rotation when an insert in the left-right grandchild caused imbalance
due to c becoming taller
• But here the “height” at the top decreases, so more rebalancing farther up the tree
might still be necessary
AVL Tree Deletion
Naturally there are two mirror-image cases not shown here
– Deletion in left causes right-right grandchild to be too tall
– Deletion in left causes right-left grandchild to be too tall