ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Avl trees
ï‚ž First-invented self-balancing binary
search tree
ï‚ž Named after its two inventors,
1. G.M. Adelson-Velsky and
2. E.M. Landis,
› published it in their 1962 paper "An
algorithm for the organization of
information."
ï‚ž First, its a binary search tree...
ï‚ž L <= P and P <= R
RL
P
ï‚ž Is this a binary tree search tree?
3 9
5
15 21
17
28 34
32
43 51
46
52 54
53
58 68
60 71 79
7712 35 56
29 70
50
ï‚ž An AVL tree is a balanced binary tree
ï‚ž To understand balance we need to
understand the notion of Tree Height
55
32 71
64 86 Height 0
Height 1
Height 2
ï‚ž By default, nodes with no children have
a height of Height of 0.
55
32 71
64 86 Height 0
Height 1
Height 2
Height
0
Height 0
ï‚ž But, we must also understand the
concept of Sub-trees
55
32 71
64 86
Height 0 Height 1
Height 2
Height 0 Height 0
sub-tree L has a
height of 0 sub-tree R has a
height of 1
Height = max(L.height, R.height) + 1
ï‚ž Also empty sub-trees have a Height of -1
44
58
91
Height = 2 = max(0, 1) + 1
Height = 0 = max(-1,-1) + 1
Height = max(L.height, R.height) + 1
Height = 1 = max(-1, 0) + 1
ï‚ž Anyway, the AVL Balance Property is as
follows...
ï‚ž For ALL nodes, the Height of the Left and
Right Sub-trees can only differ by 1.
P A Node
L R
1.. heightRheightL
1. After every insertion
2. Check to see if an imbalance was
created.
• All you have to do backtrack up the tree
3. If you find an imbalance, correct it.
4. As long as the original tree is an AVL
tree, there are only 4 types of
imbalances that can occur.
ï‚ž Left-Left (Single Rotation)
ï‚ž Right-Right(Single Rotation)
ï‚ž Left-Right (Double Rotation)
ï‚ž Right-Left(Double Rotation)
Single rotation: insert 14, 15, 16, 13, 12, 11, 10
14
15
• First insert 14 and
15:
• Now insert 16.
Single rotations:
14
15
• Inserting 16 causes AVL violation:
• Need to rotate.
16
Single rotations:
14
15
• Rotation type:
16
Single rotations:
14
15
• Rotation restores AVL balance:
16
Single rotations:
14
15
16
• Now insert 13 and 12:
13
12
• AVL violation - need to
rotate.
Single rotations:
• Rotation type:
14
15
16
13
12
Single rotations:
13
15
16
• Now insert 11.
12 14
Single rotations:
13
15
16
• AVL violation – need to
rotate
12 14
11
Single rotations:
• Rotation type:
13
15
16
12 14
11
Single rotations:
13
15
16
12
1411
• Now insert 10.
Single rotations:
13
15
16
12
1411
10
• AVL violation – need to
rotate
Single rotations:
13
15
16
12
1411
10
• Rotation type:
Single rotations:
13
15
16
11
1410 12
• AVL balance restored.
Double rotations: insert 1, 2, 3, 4, 5, 7, 6, 9, 8
• First insert 1 and 2:
13
15
16
11
1410 12
Double rotations:
13
15
16
11
1410
• AVL violation -
rotate
1
2
12
Double rotations:
13
15
16
11
1410
• Rotation type:
1
2
12
Double rotations:
• Now insert 3.
• AVL balance
restored:
1 10
13
15
16
11
142 12
Double rotations:
• AVL violation –
rotate:
1 10
13
15
16
11
142 12
3
12
Double rotations:
• Rotation type:
1 10
13
15
16
11
142 12
3
Double rotations:
• AVL balance
restored:
1 3
13
15
16
10
142 11
12
• Now insert 4.
Double rotations:
• AVL violation - rotate
1 3
13
15
16
10
142 11
12
4
Double rotations:
• Rotation type:
1 3
13
15
16
10
142 11
12
4
Double rotations:
10
13
15
2
111 3
4 12 14 16
• Now insert 5.
Double rotations:
10
13
15
2
111 3
4 12 14 16
• AVL violation –
rotate.
5
Single rotations:
10
13
15
2
111 3
4 12 14 16
5
• Rotation type:
Single rotations:
10
13
15
2
111 4
5 12 14 16
3
• AVL violation –
rotate.
7
Single rotations:
10
13
15
2
111 4
5 12 14 16
3
7
• Rotation type:
Double rotations:
10
13
15
4
112 5
7 12 14 16
31
• AVL violation -
rotate.
6
Double rotations:
10
13
15
4
112 5
7 12 14 16
31
• Rotation type:
6
Double rotations:
10
13
15
4
112 6
7
12 14 16
31
• AVL balance
restored.
• Now insert 9 and 8.
5
Double rotations:
10
13
15
4
112 6
7
12 14 16
31
• Rotation type:
9
8
5
Final tree:
10
13
15
4
112 6
8
12 14 16
31
• Tree is almost perfectly
balanced
5
97

More Related Content

Avl trees