AVLTrees
AVLTrees
AVLTrees
4
4
5
2 6
6
1 3 5 7 7
Balancing Binary Search Trees
Many algorithms exist for keeping binary search
trees balanced
Adelson-Velskii and Landis (AVL) trees (height-
balanced trees) and Red black trees
Splay trees and other self-adjusting trees
B-trees and other multiway search trees
AVL Trees
An AVL tree is a binary search tree with a balance
condition.
AVL is named for its inventors: Adel’son-Vel’skii and
Landis
AVL tree approximates the ideal tree (completely balanced
tree) and maintains a height close to the minimum.
Definition:
An AVL tree is a binary search tree such that for
any node in the tree, the height of the left and
right subtrees can differ by at most 1.
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.
AVL property
violated here
Binary Search Trees
14
15
14
15
16
Need to rotate.
AVL Tree Rotations
Single rotations:
Rotation type:
14
15
16
AVL Tree Rotations
Single rotations:
15
14 16
Balance Condition Violation
If condition violated after a node insertion
Which nodes do we need to rotate?
Only nodes on path from insertion point to root may have their
balance altered
Rebalance the tree through rotation at the deepest node
with balance violated
The entire tree will be rebalanced
Balance Condition Violation
18
An insertion into left subtree of left
child of k
Single R – rotation
Single L – rotation
Double LR – rotation
Double RL – rotation
X1 X1 X3
Double right
X2 X2 X2
Single right X2 X1
X3 X3
X3 X1
X1 X1
X2 X3
Single left Double left
X2 X2
X1 X3 X1 X2
X3
X3
Example
26
Example (Cont’d)
2
Inserting 4
1 3
4
Inserting 5
2 2
1 3 4
1
4 5
3
5 27
Example (Cont’d)
4
2
Inserting 6
2 5
1 4
1 3 6
3 5
Inserting 7 6
4
4
2 6
2 5
1 3 5 7
1 3 6
7 28
Example
Continuing the previous example by inserting
16 down to 10, and then 8 and 9
Inserting 16 and 15 4
4
2 6
2 6
1 3 5 15
1 3 5 7
7 16
16
15
29
Example (Cont’d)
Inserting 14
4
4
2 7
2 6
1 3 6 15
1 3 5 15
14 16
7 16 5
14
30
Another Example
3 2
3
3
2 1
Fig 1 2 3
Fig 4
Fig 2
2 1 2
Single rotation
Fig 3
1
1 3
3
Fig 5 Fig 6 4
4
5
2
2 Single rotation
1
1 4
4
3 5
3 5
Fig 8
Fig 7 6
4 4
Single rotation
2 2
5 5
1 3 6 1 3 6
4
Fig 9 Fig 10 7
2
6
1 3 7
5 Fig 11
4
2
6
1 3 7
5 16
Fig 12
4
Double rotation
4
2
6
2
7 6
1 3
1 3 15
5 16 5
16
Fig 13 7
15 Fig 14
4 4
Double rotation
2 2 7
6
15 1 3 15
1 3 5 6
16
7 5 14 16
Fig 15
14 Fig 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
13 16
12 14
11
13 16
12 14
11
AVL Tree Rotations
Single rotations:
13
12 15
14 16
11
13
12 15
14 16
11
10
12 15
14 16
11
10
AVL Tree Rotations
Single rotations:
13
11 15
10 12 14 16
11 15
10 12 14 16
AVL Tree Rotations
Double rotations:
AVL violation - rotate
13
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
Double 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:
AVL balance restored.
10
4 13
2 11 15
5
7 12 14 16
1 3
Now insert 6.
AVL Tree Rotations
Double rotations:
AVL violation - rotate.
10
4 13
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:
AVL balance restored.
10
4 13
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:
Tree is almost perfectly balanced
10
4 13
2 11 15
6
12 14 16
1 3 5 8
7 9
Deletion
20 20
10 35 15 35
5 15 25 40 10 18 25 40
18 30 38 45 30 38 45
50 50
Single Rotation
Delete 5, Node 10 is unbalanced
Cont’d
20 35
15 35 20 40
10 18 25 40 15 25 38 45
30 38 45 10 18 30 50