Professional Documents
Culture Documents
AVL property
violated here
Overview of AVL Trees
Insertion and deletion are handled in the same manner
as with an ordinary BST.
A value is kept in each node which denotes the balance
condition of that node or the current height of the node
(depending on implementation)
Balancing act (single rotation)
We use single and double rotations to keep the balance.
A B
B
AR A
BL
BL BR C BR AR
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
Single rotation
2 2
1 1 4
4
3 5 3 5
Fig 7 Fig 8
6
4 4 Single rota
2 5 2 5
1 3 6 6
1 3
Fig 9 4 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
Deletion
Delete a node x 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,
perform an appropriate rotation at x. There are 4 cases as in the
case of insertion.
For deletion, after we perform a rotation at x, we may have to
perform a rotation at some ancestor of x. Thus, we must continue
to trace the path until we reach the root.
In Class Exercises
15
15 24
20
10
24
13
20 20
13 24 15 24
10 15 13
10
15, 20, 24, 10, 13, 7, 30, 36, 25
20
13
13 24 10 20
10 15 7 15 24
7 30
13 36
10 20
7 15 30
24 36
15, 20, 24, 10, 13, 7, 30, 36, 25
13 13
10 20 10 20
7 15 30 7 15 24
24 36 30
25 13 25 36
10 24
7 20 30
15 25 36
Remove 24 and 20 from the AVL tree.
13 13
10 24 10 20
7 20 30 7 15 30
15 25 36 25 36
13 13
10 30 10 15
7 15 36 7 30
25 25 36