Professional Documents
Culture Documents
Avl Trees
Avl Trees
AVL TREES
(Adelson-Velskii and Landis 1962)
Key idea: if insertion or deletion get the tree out of balance then
fix it immediately
3
44
1 2
17 78
0 1 0
32 50 88
0 0
48 62
the nodes:
Insertion in an AVL Tree
44
17 78
32 50 88
48 62
Insertion in an AVL Tree
17 78
32 50 88
48 62
Insertion in an AVL Tree
32 50 88
32 50 88
48 62
48 62
54
Insertion in an AVL Tree
32 50 88
32 50 88
48 62
48 62
54
Insertion in an AVL Tree
32 50 1 88
32 50 88
48 62
48 62
54
Insertion in an AVL Tree
32 50 1 88 0
32 50 88
48 62
48 62
54
Insertion in an AVL Tree
32 50 1 88 0
32 50 88
48 62
48 62
54
Unbalanced!!
How does the AVL tree work?
Single rotations
two nodes are “rotated”
Double rotations
three nodes are “rotated”
Single Rotation (Right)
h
h+1
h+2
Node 02 added
Perform rotation.
Example
h
h+1
h
C
B C
k2 k1
k1 k2
h h
Z X
h+2
X h+2
Z
Y
Y
h+1
Delete node 94
h+2
h
B1 B2
y x
A B1 B2 C
44
17 78
32 50 88
48 62
Self-adjusting Structures
44
17 78
32 50 88
48 62
elements: 48, 48, 48, 48, 50, 50, 50, 50, 50.
Self-adjusting Structures
44 In this case,
32 50 88
48 62
elements: 48, 48, 48, 48, 50, 50, 50, 50, 50.
Self-adjusting Structures
A) 42 B) 30 C) 50 D) 50
30 60 10 50 30 60 30 60
10 50 80 42 60 80 10 42 80 10 42 80