You are on page 1of 25

AVL Trees

Slides Courtesy: Roy Pargas and Huamin Qu


Balanced binary tree
 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.
Types of Binary Trees
 A full binary tree is one in which all nodes have either
two children or none.
 In a complete binary tree every level, except possibly
the last, is completely filled, and all nodes in the last
level are as far left as possible.
What is a balanced tree?

This is not a balanced tree. Clearly the left-sub


tree is deeper than the right sub-tree
What is a balanced tree?

This is a balanced tree. Clearly the left-sub tree is


the same depth as the right sub-tree.
Problems with balanced trees
 A perfectly balanced tree is far too strict a restriction to
place on a binary tree.
Loosening the rules
 Perhaps we could allow the left and right sub-trees to
differ by at most one?
 This is the solution which was proposed by G. M.
Adel'son-Velskii and E. M. Landis (two Russian math
guys)
 Henceforth known as the AVL Tree
So... Why should we use it?

 While the AVL Tree does not yield an average search in


log n comparisons, we can achieve an solution in 1.44
log n comparisons (pretty close)
 Relatively easy to implement (there are a maximum of
two modifications to the tree after an insert)
Overview of AVL Trees
 AVL tree is height-balanced: for every node in the tree,
the height of the left and right sub-trees differ by at
most one
 We will always have to rebalance the tree after each
deletion or insertion (if needed) to keep the tree
balanced, and we'll perform rotations to do that.
AVL tree
 An AVL tree is a binary search tree in which
 for every node in the tree, the height of the left and right
subtrees differ by at most 1.

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

This is the case where we just inserted node C into the


left sub-tree of node A, causing an imbalance. After
performing the single right rotation, we arrive at a
balanced tree again. The mirror case is easily derived.
Rotations
 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) ad right(x) differ by
exactly 2.
 Rotations will be applied to x to restore the AVL tree
property.
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 [next slide].
 For insertion, once we perform a rotation at a node x, we won’t
need to perform any rotation at any ancestor of x.
Insertion

 Let x be the node at which left(x) and right(x) differ


by more than 1
 Assume that the height of x is h+3
 There are 4 cases
 Height of left(x) is h+2 (i.e. height of right(x) is h)
 Height of left(left(x)) is h+1  single rotate with left child
 Height of right(left(x)) is h+1  double rotate with left child
 Height of right(x) is h+2 (i.e. height of left(x) is h)
 Height of right(right(x)) is h+1  single rotate with right child
 Height of left(right(x)) is h+1  double rotate with right child
An Extended Example

Insert 3,2,1,4,5,6,7, 16,15,14 Single rotation

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

 Build an AVL tree with the following values:


15, 20, 24, 10, 13, 7, 30, 36, 25
15, 20, 24, 10, 13, 7, 30, 36, 25
20

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

You might also like