Professional Documents
Culture Documents
Motivation
• When building a binary search tree, what type of
trees would we like? Example: 3, 5, 8, 20, 18, 13, 22
5
13
8
5 20
13
18 3 8 18 22
20
22
2
Motivation
• Complete binary tree is hard to build when we
allow dynamic insert and remove.
– We want a tree that has the following properties
• Tree height = O(log(N))
• allows dynamic insert and remove with O(log(N)) time complexity.
– The AVL tree is one of this kind of trees.
8
13
5 18
5 20
3 13 20
3 8 18 22 22
3
Introduction
• AVL tree is the first balanced binary search
tree (name after its discovers, Adelson-
Velskii and Landis).
• The AVL tree is a balanced search tree that
has an additional balance condition.
• The balance condition must be easy to
maintain and ensures that the depth of the
tree is O(logN)
§4 AVL Trees
4 5 7
3 5 2 8 2 8
2 6 1 4 7 1 4
1 7 3 3 5
3/19
AVL Trees
A tree is said to be balanced if for each
node, the number of nodes in the left subtree
and the number of nodes in the right subtree
differ by at most one.
1 7 1 40
0 3 0 8 1 30 45 -1
0 0 5 0
1 -1 20 0 35 60
0
• Is this an AVL tree? 25
• What is the balance factor for each node in this AVL tree?
• Is this an AVL search tree?
Rotation
Definition
• To switch children and parents among two
or three adjacent nodes to restore balance of
a tree.
• A rotation may change the depth of some
nodes, but does not change their relative
ordering.
Imbalance Types
• After an insertion, when the balance factor of node A is –2 or 2,
the node A is one of the following four imbalance types
1. LL: new node is in the left subtree of the left subtree of A
2. LR: new node is in the right subtree of the left subtree of A
3. RR: new node is in the right subtree of the right subtree of A
4. RL: new node is in the left subtree of the right subtree of A
4.5.1 AVL Trees: Single Rotation
Construct AVL tree (height balanced)
4.5.1 AVL Trees: Single Rotation
Insert
Insert 4,
4, 55
Insert
Insert 66
4.5.1 AVL Trees: Single Rotation
Insert
Insert 77
AVL Trees
(Adelson – Velskii – Landis)
AVL tree:
20
16 25
7 18 30
5 17 19
AVL Trees
Not AVL tree:
20
16 25
7 18 30
5 17 19 27
AVL Tree Rotations
Single rotations: insert 14, 15, 16, 13, 12, 11, 10
• First insert 14 and 15:
14
15
14
15
16
• Need to rotate.
AVL Tree Rotations
Single rotations:
• Inserting 16 causes AVL violation:
14
15
16
• Need to rotate.
AVL Tree Rotations
Single rotations:
• Rotation type:
14
15
16
AVL Tree Rotations
Single rotations:
• Rotation restores AVL balance:
15
14 16
AVL Tree Rotations
Single rotations:
• Now insert 13 and 12:
15
14 16
13
12
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
16
11 14
13
12 15
16
11 14
10
12 15
16
11 14
10
AVL Tree Rotations
Single rotations:
13
11 15
10 12 14 16
A 15 A 15 C 12
B 7 C 12 B 7 15 A
C 12 B 7
AVL Trees
4. Inserted into the left subtree of A’s right child (lets call this B).
A 15 A 15 C 21
B 32 C 21 A 15 32 B
C 21 B 32
AVL Tree Rotations
Double rotations: insert 1, 2, 3, 4, 5, 7, 6, 9, 8
• First insert 1 and 2:
13
11 15
10 12 14 16
AVL Tree Rotations
Double rotations:
• AVL violation - rotate
13
11 15
16
10 12 14
2
AVL Tree Rotations
Double rotations:
• Rotation type:
13
11 15
16
10 12 14
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
2 13
1 11 15
3
4 12 14 16
5
AVL Tree Rotations
Single rotations:
• AVL balance restored:
10
2 13
1 11 15
4
5 12 14 16
3
• Now insert 7.
AVL Tree Rotations
Single rotations:
• AVL violation – rotate.
10
2 13
1 11 15
4
5 12 14 16
3
7
AVL Tree Rotations
Single rotations:
• Rotation type:
10
2 13
1 11 15
4
5 12 14 16
3
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
4 13
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