Chapter 8 - AVL Trees
Chapter 8 - AVL Trees
1
OUTLINE
AVL trees
▪ Intro
▪ Rotations
▪ Insertion
▪ Deletion
▪ Implementation
CSC315 2
Introduction
•The disadvantage of a binary search tree is that its height can be as large as N-1
•This means that the time needed to perform insertion and deletion and many
other operations can be O(N) in the worst case
•We want a tree with small height
•A binary tree with N node has height at least (log N)
•Thus, our goal is to keep the height of a binary search tree O(log N)
•Such trees are called balanced binary search trees.
•Examples are AVL tree, red-black tree.
CSC315 3
AVL trees
An Adelson-Velskii and Landis (AVL) tree is a BST with a balance condition.
CSC315 4
AVL trees
CSC315 5
AVL trees
AVL property
violated here
•The insertion and deletion operations should maintain the property for the AVL
CSC315 7
Rotations
• The AVL tree property states that for any node x: ∣height(left(x)) −
height(right(x))∣ ≤ 1
• Since an insertion/deletion involves adding/deleting a single node, this can only
increase/decrease the height of some subtree by 1
• Thus, if the AVL tree property is violated at a node x, it means that the heights of
left(x) and right(x) differ by exactly 2: ∣height(left(x)) − height(right(x))∣=2
CSC315 8
Insertion
•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.
• 2) Starting from w, travel up and find the first unbalanced node. Let z be the first
unbalanced node, y be the child of z that comes on the path from w to z and x be the
grandchild of z that comes on the path from w to z.
• 3) Re-balance the tree by performing appropriate rotations on the subtree rooted with z.
There can be 4 possible cases that needs to be handled as x, y and z can be arranged in 4
ways. Following are the possible 4 arrangements:
• a) y is left child of z and x is left child of y (Left Left Case)
• b) y is left child of z and x is right child of y (Left Right Case)
• c) y is right child of z and x is right child of y (Right Right Case)
• d) y is right child of z and x is left child of y (Right Left Case)
10
Insertion
Double rotation
11
AVL Tree 5 z
5
3 y 8 C
3 8
1 4
1 4 x B
A
0.8
Insert 0.8
3
1
5
8
After rotation
4
0.8
CSC315 12
AVL Tree
5
C
y 8
3
3 8
A 1 4 x
1 4
B 3.5
Insert 3.5
4
3
5
Double rotation
14
An Extended 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
CSC315 CSC315 - DR MIREILLE MAKARY 19 5 15
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
16
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
CSC315 17
4 4
Double rotation
2 2 7
6
15 1 3 15
1 3 5 6
16
7 5
Fig 15 14 16
14 Fig 16
18
Deletion
•Delete a node x as in ordinary binary search tree. Note that the last node deleted
is a leaf.
•Then trace the path from the new leaf towards the root.
•For each node x encountered, check if heights of left(x) and
CSC315 19
Deletion
•Steps for deletion:
• Let w be the node to be deleted
• 1) Perform standard BST delete for w.
• 2) Starting from w, travel up and find the first unbalanced node. Let z be the first unbalanced node, y be
the larger height child of z, and x be the larger height child of y. Note that the definitions of x and y are
different from insertion here.
• 3) Re-balance the tree by performing appropriate rotations on the subtree rooted with z. There can be 4
possible cases that needs to be handled as x, y and z can be arranged in 4 ways. Following are the
possible 4 arrangements:
• a) y is left child of z and x is left child of y (Left Left Case)
• b) y is left child of z and x is right child of y (Left Right Case)
• c) y is right child of z and x is right child of y (Right Right Case)
• d) y is right child of z and x is left child of y (Right Left Case)
•Like insertion, following are the operations to be performed in above mentioned 4 cases.
•Unlike insertion, fixing the node z won’t fix the complete AVL tree. After
fixing z, we may have to fix ancestors of z as well
CSC315 20
Deletion
CSC315 21
Deletion
CSC315 22
Rotation in Deletion
CSC315 23
Rotation in Deletion
CSC315 24
Rotation in Deletion
CSC315 25
Deletion Example 1
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
CSC315 26
Continued
20 35
15 35 20 40
10 18 25 40 15 25 38 45
30 38 45 10 18 30 50
Continue to check parents
50
Node 20 is unbalanced! Single Rotation
CSC315 27
Running Times for AVL Trees
CSC315 28
Visualization
CSC315 29