AVL Tree
AVL Tree
AVL Tree
Note that our goal is to perform all the operations search, insert
and delete in O(log N) time, including the operations involved in
adjusting the tree to maintain the above balance condition.
AVL Tree
An AVL tree is a binary search tree in which
for every node in the tree, the height of the left and right subtrees
differ by at most 1.
Height of subtree: Max # of edges to a leaf
Height of an empty subtree: -1
Height of one node: 0
AVL tree property
violated here
AVL tree
AVL Tree with Minimum Number of Nodes
N1 = 2 N2 =4 N3 = N1+N2+1=7
N0 = 1
Smallest AVL tree
of height 7
Smallest AVL tree
of height 8
6 8
6
Insert 6
Original AVL tree Restore AVL property
Property violated
Some Observations
After an insertion (using the insertion algorithm for a binary search
tree), only nodes that are on the path from the insertion point to the
root might have their balance altered.
Because only those nodes have their subtrees altered
Example:
2.5
Different Cases for Rebalance
Let α be the pivot node
Case 1: inserted node is in the left subtree of the left child of α (LL-
rotation)
Case 2: inserted node is in the right subtree of the left child of α
(RL-rotation)
Case 3: inserted node is in the left subtree of the right child of α
(LR-rotation)
Case 4: inserted node is in the right subtree of the right child of α
(RR-rotation)
Cases 3 & 4 are mirror images of cases 1 & 2 so we will focus on 1
& 2.
Rotations
Rebalance of AVL tree are done with simple modification to tree,
known as rotation.
A single rotation B
B A C
C
T0 T3
T1 T3 T0 T1 T2
T2
C single rotation B
B A C
A
T3 T3
T0 T2 T2 T1 T0
T1 13
Rotations (contd.)
Case 2 & 3
A double rotation B
C A C
B
T0 T2
T2 T3 T0 T1 T3
T1
C double rotation B
A A C
B
T0 T2
T3 T2 T3 T1 T0
T1
14
Insertion Algorithm (outline)
First, insert the new key as a new leaf just as in ordinary binary
search tree
Then trace the path from the new leaf towards the root. For each
node x encountered, check if heights of left(x) and right(x) differ by
at most 1.
If yes, proceed to parent(x)
If not, restructure by doing either a single rotation or a double rotation
Note: once we perform a rotation at a node x, we won’t need to
perform any rotation at any ancestor of x.
Summary
AVL trees with n nodes will have height at most 1.5 log2 n
14
15
16
• Need to rotate.
AVL Tree Rotations
Single rotations:
14
15
16
• Need to rotate.
AVL Tree Rotations
Single rotations:
• Rotation type:
14
15
16
AVL Tree Rotations
Single rotations:
15
14 16
AVL Tree Rotations
Single rotations:
14 16
13
12
• Rotation type:
15
14 16
13
12
AVL Tree Rotations
Single rotations:
15
13 16
12 14
15
13 16
12 14
11
15
• Rotation type:
13 16
12 14
11
AVL Tree Rotations
Single rotations:
13
12 15
14 16
11
13
12 15
14 16
11
10
• Rotation type:
13
12 15
14 16
11
10
AVL Tree Rotations
Single rotations:
13
11 15
10 12 14 16
13
11 15
10 12 14 16
AVL Tree Rotations
Double rotations:
11 15
14 16
10 12
2
AVL Tree Rotations
Double rotations:
• Rotation type:
13
11 15
14 16
10 12
2
AVL Tree Rotations
Double rotations: • AVL balance restored:
13
11 15
2 12 14 16
1 10
• Now insert 3.
AVL Tree Rotations
Double rotations: • AVL violation – rotate:
13
11 15
2 12 14 16
1 10
3
AVL Tree Rotations
Double rotations: • Rotation type:
13
11 15
2 12 14 16
1 10
3
AVL Tree Rotations
Double rotations: • AVL balance restored:
13
10 15
2 11 14 16
1 3 12
• Now insert 4.
AVL Tree Rotations
Double rotations: • AVL violation - rotate
13
10 15
2 11 14 16
1 3 12
4
AVL Tree Rotations
Single rotations:
• Rotation type:
13
10 15
2 11 14 16
1 3 12
4
AVL Tree Rotations
Double rotations:
10
2 13
1 11 15
3
4 12 14 16
• Now insert 5.
AVL Tree Rotations
Double rotations:
10
2 13
1 11 15
3
4 12 14 16
1 11 15
3
4 12 14 16
5
AVL Tree Rotations
Single rotations:
• AVL balance restored:
10
2 13
1 11 15
4
3 5 12 14 16
• Now insert 7.
AVL Tree Rotations
Single rotations:
• AVL violation – rotate.
10
2 13
1 11 15
4
3 5 12 14 16
7
AVL Tree Rotations
Single rotations:
• Rotation type:
10
2 13
1 11 15
4
3 5 12 14 16
7
AVL Tree Rotations
Double rotations:
2 11 15
5
7 12 14 16
1 3
• Now insert 6.
AVL Tree Rotations
Double rotations:
2 11 15
5
7 12 14 16
1 3
6
AVL Tree Rotations
Double rotations:
• Rotation type:
10
4 13
2 11 15
5
7 12 14 16
1 3
6
AVL Tree Rotations
Double rotations:
2 11 15
6
12 14 16
1 3 5 7
2 11 15
6
12 14 16
1 3 5 7
8
AVL Tree Rotations
Double rotations:
• Rotation type:
10
4 13
2 11 15
6
12 14 16
1 3 5 7
8
AVL Tree Rotations
Final tree:
2 11 15
6
12 14 16
1 3 5 8
7 9