You are on page 1of 86

AVL-Trees

COMP171 Fall 2005

AVL trees

Binary Search Tree - Best Time


 

All BST operations are O(d), where d is tree depth minimum d is for a binary tree with N nodes


What is the best case tree?

! l
 

What is the worst case tree?

So, best case running time of BST operations is O(log N)

AVL trees

Binary Search Tree - Worst Time




Worst case running time is O(N)


root 2 4 6 8 10
AVL trees 3

What happens when you Insert elements in ascending order?

Insert: 2, 4, 6, 8, 10 into an empty BST

Balanced binary tree


 

  

The disadvantage of a binary search tree is that its height can be as large as N 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 5(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.
AVL trees 4

AVL (Adelson-Velskii and Landis) trees




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

AVL trees

 

AVL trees are height-balanced binary search trees Balance factor of a node


height(left subtree) - height(right subtree)

An AVL tree has balance factor calculated at every node


For every node, heights of left and right subtree can differ by no more than 1  Store current heights in each node


AVL trees

Node Heights
Tree A (AVL) BF=2-1=1 1 Tree B (AVL) 2 0 1

6 9
0 0

6
0 0

4
0

4 1 5 8

height of node = h balance factor = hleft-hright

AVL trees

Node Heights after Insert 7


Tree A (AVL) 2 1 Tree B (not AVL) 2-3 = -1 1

6
0 0

6
0 1

balance factor 2-(0) = 2

4
0

9
0

4 1 5
0

9
-1

8 7

height of node = h balance factor = hleft-hright empty height = -1


AVL trees

AVL trees

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.

AVL trees

10

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.

AVL trees

11

Insertions in AVL Trees


Let the node that needs rebalancing be E. There are 4 cases: (require single rotation) : 1. Insertion into left subtree of left child of pivot node. 2. Insertion into right subtree of right child of pivot node. (require double rotation) : 3. Insertion into right subtree of left child of pivot node. 4. Insertion into left subtree of right child of pivot node. The rebalancing is performed through four separate rotation algorithms.
AVL trees 12

Single Rotation in an AVL Tree


2-3 = 1 0 0 2-0 2 =2

6
0 1

6 4
0 0

4
0

9 5
0

8
0

8 7

AVL trees

13

AVL Tree Rotations


Single rotations: insert 14, 15, 16, 13, 12, 11, 10


First insert 14 and 15:

14

15

Now insert 16.


AVL trees 14

AVL Tree Rotations


Single rotations:


Inserting 16 causes AVL violation:


14 15 16

Need to rotate.
AVL trees 15

AVL Tree Rotations


Single rotations:


Rotation type:
14 15 16

AVL trees

16

AVL Tree Rotations


Single rotations:


Rotation restores AVL balance:


15

14

16

AVL trees

17

AVL Tree Rotations


Single rotations:


Now insert 13 and 12:


15

14

16

13

12


AVL violation - need to rotate.


AVL trees 18

AVL Tree Rotations


Single rotations:


Rotation type:
15

14

16

13

12

AVL trees

19

AVL Tree Rotations


Single rotations:
15

13

16

12

14

Now insert 11.


AVL trees 20

AVL Tree Rotations


Single rotations:
15

13

16

12

14

11

AVL violation need to rotate


AVL trees 21

AVL Tree Rotations


Single rotations:


Rotation type:
13

15

16

12

14

11

AVL trees

22

AVL Tree Rotations


Single rotations:
13

12

15

11

14

16

Now insert 10.


AVL trees 23

AVL Tree Rotations


Single rotations:
13

12

15

11

14

16

10


AVL violation need to rotate

AVL trees

24

AVL Tree Rotations


Single rotations:


Rotation type:
13

12

15

11

14

16

10

AVL trees

25

AVL Tree Rotations


Single rotations:
13

11

15

10

12

14

16

AVL balance restored.


AVL trees 26

AVL Tree Rotations




AVL tree
13

10

15

11

14

16

1


12

Now insert 4.
AVL trees 27

AVL Tree Rotations


Double rotations:


AVL violation - rotate


13

10

15

11

14

16

12

4
AVL trees 28

AVL Tree Rotations


Double rotations:


Rotation type:
13

10

15

11

14

16

12

4
AVL trees 29

AVL Tree Rotations


Double rotations:
10 2 13

11

15

12

14

16

Now insert 5.
AVL trees 30

AVL Tree Rotations


Double rotations:
10 2 13

3 4

11

15

12

14

16

5


AVL violation rotate.


AVL trees 31

AVL Tree Rotations


Single rotations:


Rotation type:
10 2 13

3 4

11

15

12

14

16

AVL trees

32

AVL Tree Rotations


Single rotations:


AVL balance restored:


10 2 13

4 5

11

15

12

14

16

Now insert 7.
AVL trees 33

AVL Tree Rotations


Single rotations:


AVL violation rotate.


10 2 13

4 5 7

11

15

12

14

16

AVL trees

34

AVL Tree Rotations


Single rotations:


Rotation type:
10 2 13

4 5 7

11

15

12

14

16

AVL trees

35

AVL Tree Rotations


Double rotations:


AVL balance restored.


10 4 13

5 7

11

15

12

14

16

Now insert 6.
AVL trees 36

Example of Insertions in an AVL Tree


2

20
0 1 0

Insert 5, 40 0

10 25

30

35

AVL trees

37

Example of Insertions in an AVL Tree


2-2 = 0 2-3 = -1 1 0 1

20
1

20
-1

10
0

30

10
0

30 25
0 -1

25

35

5
Now Insert 45

35
0

40

AVL trees

38

Single rotation (outside case)


3

20
1 2 1

20 30 25
0 0

10
0 0

30
-2 0

10 35
-1

25
Imbalance

5 40
0

40 1
0

35
Now Insert 34

45

45
AVL trees

39

Inserting 33 in AVL tree

AVL trees

40

AVL trees

41

AVL Tree Rotations


Double rotations:


AVL balance restored.


10 4 13

5 7

11

15

12

14

16

Now insert 6.
AVL trees 42

AVL Tree Rotations


Double rotations: insert 1, 2, 3, 4, 5, 7, 6, 9, 8


First insert 1 and 2:


13

11

15

10

12

14

16

AVL trees

43

AVL Tree Rotations


Double rotations:


AVL violation - rotate


13

11

15

10

12

14

16

2
AVL trees 44

AVL Tree Rotations


Double rotations: L-R rotation


Rotation type: L-R rotation( insertion in right subtree of left child node of pivot node) 13

11
Pivot node

15

10

2nd

12

14

16

Left child

1st

2
AVL trees 45

AVL Tree Rotations


Double rotations: L-R rotation


Rotation type: L-R rotation( insertion in right subtree of left child node of pivot node) 13

11
Pivot node

15

10

2nd

12

14

16

2 1
AVL trees 46

AVL Tree Rotations


Double rotations:


AVL balance restored:


13

11

15

12

14

16

1


10

Now insert 3.
AVL trees 47

AVL Tree Rotations


Double rotations:


AVL violation rotate:


13

11

15

12

14

16

1 3

10

AVL trees

48

AVL Tree Rotations


Double rotations:


AVL violation - rotate. R-L rotation( insertion in left subtree of right


child node of pivot node)
10 4
Pivot node

13

11

15

child

12

14

16

AVL trees

49

AVL Tree Rotations


Double rotations:


AVL violation - rotate. R-L rotation( insertion in left subtree of right


child node of pivot node)
10 4
Pivot node

13

2 2nd 1 3

11

15

7
1st

child

12

14

16

AVL trees

50

AVL Tree Rotations


Double rotations:


AVL violation - rotate. R-L rotation( insertion in left subtree of right


child node of pivot node)
10 4
Pivot node

13

2 2nd 1 3

11

15

child

12

14

16

AVL trees

51

AVL Tree Rotations


Double rotations:


AVL balance restored.


10 4 13

11

15

12

14

16

Now insert 9 and 8.


AVL trees 52

AVL Tree Rotations


Double rotations:


AVL violation - rotate.


10 4 13

11

15

12

14

16

9 8

AVL trees

53

AVL Tree Rotations


Final tree:


Tree is almost perfectly balanced


10 4 13

11

15

12

14

16

AVL trees

54

Double rotation (inside case)


3

20
1 -2 1

20 35
1

10
0 0 Imbalance

30
1 0

10 40 5 45 0 25 30

25
1 35

40 -1
0

34

45

Insertion of 34

34
AVL trees 55

An Extended Example
Insert 3,2,1,4,5,6,7, 16,15,14
3 3 Fig 1 2 Fig 2 2 1 Fig 3 1 Fig 5 4
AVL trees

Single rotation
3 2

1 Fig 4

Single rotation
1 Fig 6

3
4

56

2 1 3 1

Single rotation

4
5 Fig 7 4 2 3

4
5 Fig 8 4

6
Single rotation

5
3 Fig 9 2 1 3 5 6 4 1

2 3

5
6 Fig 10 7

6
7 Fig 11
AVL trees 57

4 2 1 3 5 Fig 12 4

6
7 16

Double rotation
2 1 3 5 Fig 13 15

4 2

6
7 16 1 3

6
5 15 16

AVL trees

Fig 14

7
58

4 2 1 3

Double rotation

4 2 7 3 6 5 15

6
5 15 16 7 Fig 15 14 1

14 Fig 16

16

AVL trees

59

AVL trees

60

Restore AVL tree property Case 2

Case 2 : Double rotate with left child.


AVL trees 61

Restore AVL tree property Case 3

Case 3 : Single rotate with right child.


AVL trees 62

AVL trees

63

Restart our example Weve inserted 3, 2, 1, 4, 5, 6, 7, 16 Well insert 15, 14, 13, 12, 11, 10, 8, 9

4 2 1 3 5 6 7 k1 16 k3 1 2 3

4 6 5 7 Double rotation 15 k2 k1 15 k2 16 k3

Insert 16, fine Insert 15 violation at node 7

AVL trees

64

4 2 1 3 A 5 k2 Insert 14 4 X 1 Insert 13 2 3 5 Y 6 14 13 Z k1 7 k2 15 16 1
AVL trees

4 6 k1 15 k3 7 16 14 C D 1 2 3 5 Double rotation 7 k2

6 k1 15 k3 14 16

7 4 2 3 6 5 14 13 Single rotation65 15 16

7 4 2 1 3 6 5 12 14 13 15 16 1 2 3 4 6 5

7 15 13 12 14 16

Insert 12

Single rotation 7 15 4 2 1
AVL trees

7 4 2 1 3 6 5 11 13 12 14

13 6 12 11 15 14 16
66

16

Insert 11

Single rotation

7 4 2 1 3 6 5 10 7 4 4 2 1 3 6 5 8 9 10 11 12 13 2 15 1 14 16 3 5 6 12 11 13 15 14 16 1 2 3 4 6 5

7 13 11 10 12 15 14 16

Insert 10

Single rotation 7 13 11 8 9 Insert 8, fine then insert 9


AVL trees

15 14 16

12 10

Double rotation

67

Insertion Analysis


logN

Insert the new key as a new leaf just as in ordinary binary search tree: O(logN) Then trace the path from the new leaf towards the root, for each node x encountered: O(logN)
Check height difference: O(1)  If satisfies AVL property, proceed to next node: O(1)  If not, perform a rotation: O(1)


The insertion stops when


A rotation is performed  Or, weve checked all nodes in the path


Time complexity for insertion O(logN) AVL trees

68

Searching AVL Trees




Searching an AVL tree is exactly the same as searching a regular binary tree
all descendants to the right of a node are greater than the node  all descendants to the left of a node are less than the node


AVL trees

69

Deletion from AVL Tree




Delete a node x as in ordinary binary search tree




Note that the last (deepest) node in a tree deleted is a leaf or a node with one child

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 no, perform an appropriate rotation at x




Continue to trace the path until we reach the root


AVL trees 70

AVL trees

71

Deletion


The only difference from insertion is that after we perform a rotation at x, we may have to perform a rotation at some ancestors of x. It may involve several rotations. Therefore, we must continue to trace the path until we reach the root. The time complexity to delete a node is dependent on the height of the tree, which is also O(log(n)).

AVL trees

72

Deletion Example 1
20 10 5 15 18 25 30 35 40 38 45 50 Delete 5, Node 10 is unbalanced Single Rotation 10 15 18 25 30 20 35 40 38 45 50

AVL trees

73

Contd
20 15 10 18 25 30 35 40 38 45 50 10 15 18 20 25 30 35 40 38 45 50

Continue to check parents Oops!! Node 20 is unbalanced!!

Single Rotation

For deletion, after rotation, we need to continue tracing upward to see if AVL-tree property is violated at other node.
AVL trees 74

Deletion Example 2

Right most child of left subtree = I

Double rotation
AVL trees

Ok here!

75

Example 2 Contd

New case

AVL trees

76

Deletion : no rotation

No need to rotate.

AVL trees

77

Deletion : one rotation

Case 3 : Single rotate with right child.


AVL trees 78

Deletion : several rotations

Case 4 : Double rotate with right child.

Case 3 : Single rotate with right child.


AVL trees 79

Height of an AVL tree




 

Let Nh represent the number of nodes of an AVL tree of height h. Let nh be the lower bound Nh, i.e., Nh nh By definition, we have n1 = 1, and n2 = 2, and n3 = 4, etc, and nh+1 nh.
n1 = 1 15 10 n2 = 2 15 10 n3 = 4 15 20

13
AVL trees 80

Height of an AVL tree


 

Let x be the root of an AVL Tree. To keep the number of nodes in the AVL tree of height h small, one strategy is to make the height of left(x)=h-1 and the height of right(x)=h-2.
x

nh=nh-1+nh-2 +1

h nh-1 nh-2

AVL trees

81

Height of an AVL tree




We then have : nh = nh-1 + nh-2 + 1 2 nh-2 + 1 > 2 nh-2 > 22nh-4 By repeated substitution, we obtain the general form nh > 2i nh-2i

AVL trees

82

Height of an AVL tree




Nh nh Nh log2(Nh) h h

> 2i nh-2i 2h/2 (Since n1 = 1 and n2 = 2) h/2 2 log2(Nh) = O(log Nh)

In other words, if n is the number of nodes of an AVL tree, then its height h is at most () O(log n). This implies that operations including search, insert, delete, successor, and predecessor on an AVL tree will take O(h)=O(log n).

AVL trees

83

AVL tree: Running times


 

find takes O(log n) time, because height of the tree is always O(log n). insert: O(log n) time because we do a find (O(log n) time), and then we may have to visit every node on the path back to the root, performing up to 2 single rotations (O(1) time each) to fix the tree. remove: O(log n) time. Left as an exercise.

AVL trees

84

Summary of AVL Trees




Maintains a balanced tree by posing an AVL tree property:


guarantees the height of the AVL tree be O(log n) implies that functions search, min, and max, insert, and delete will be performed in O(logn)

Modifies the insertion and deletion routine




Performs single or double rotation to restore the AVL tree property if necessary. Requires a little more work for insertion and deletion.
AVL trees 85

Pros and Cons of AVL Trees


Arguments for AVL trees:
1. Search is O(log N) since AVL trees are always balanced. 2. Insertion and deletions are also O(logn) 3. The height balancing adds no more than a constant factor to the speed of insertion.

Arguments against using AVL trees:


1. Difficult to program & debug; more space for balance factor. 2. Asymptotically faster but rebalancing costs time. 3. Most large searches are done in database systems on disk and use other structures (e.g. B-trees).

AVL trees

86