AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees...

99
AVL Trees 1 AVL Trees

Transcript of AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees...

Page 1: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

1

AVL Trees

Page 2: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

2

Outline

Background

Define balance

Maintaining balance within a tree– AVL trees

– Difference of heights

– Rotations to maintain balance

Page 3: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

3

Background

From previous lectures:– Binary search trees store linearly ordered data

– Best case height: (ln(n))

– Worst case height: O(n)

Requirement:– Define and maintain a balance to ensure (ln(n)) height

Page 4: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

4

Prototypical Examples

These two examples demonstrate how we can correct for imbalances: starting with this tree, add 1:

Page 5: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

5

Prototypical Examples

This is more like a linked list; however, we can fix this…

Page 6: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

6

Prototypical Examples

Promote 2 to the root, demote 3 to be 2’s right child, and 1 remains the left child of 2

Page 7: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

7

Prototypical Examples

The result is a perfect, though trivial tree

Page 8: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

8

Prototypical Examples

Alternatively, given this tree, insert 2

Page 9: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

9

Prototypical Examples

Again, the product is a linked list; however, we can fix this, too

Page 10: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

10

Prototypical Examples

Promote 2 to the root, and assign 1 and 3 to be its children

Page 11: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

11

Prototypical Examples

The result is, again, a perfect tree

These examples may seem trivial, but they are the basis for the corrections in the next data structure we will see: AVL trees

Page 12: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

12

AVL Trees

We will focus on the first strategy: AVL trees– Named after Adelson-Velskii and Landis

Balance is defined by comparing the height of the two sub-trees of a node– An empty tree has height –1

– A tree with a single node has height 0

Page 13: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

13

AVL Trees

A binary search tree is said to be AVL balanced if:– The difference in the heights between the left and right sub-trees is at

most 1, and

– Both sub-trees are themselves AVL trees

Page 14: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

14

AVL Trees

AVL trees with 1, 2, 3, and 4 nodes:

Page 15: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

15

AVL Trees

Here is a larger AVL tree (42 nodes):

Page 16: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

16

AVL Trees

The root node is AVL-balanced:– Both sub-trees are of height 4:

Page 17: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

17

AVL Trees

All other nodes (e.g., AF and BL) are AVL balanced– The sub-trees differ in height by at most one

Page 18: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

18

Height of an AVL Tree

By the definition of complete trees, any complete binary search tree is an AVL tree

Thus an upper bound on the number of nodes in an AVL tree of height h a perfect binary tree with 2h + 1 – 1 nodes– What is an lower bound?

Page 19: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

19

Height of an AVL Tree

Let F(h) be the fewest number of nodes in a tree of height h

From a previous slide:F(0) = 1

F(1) = 2

F(2) = 4

Can we find F(h)?

Page 20: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

20

Height of an AVL Tree

The worst-case AVL tree of height h would have:– A worst-case AVL tree of height h – 1 on one side,

– A worst-case AVL tree of height h – 2 on the other, and

– The root node

We get: F(h) = F(h – 1) + 1 + F(h – 2)

Page 21: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

21

Height of an AVL Tree

This is a recurrence relation:

The solution?

11)2F()1F(

12

01

)F(

hhh

h

h

h

Page 22: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

22

Height of an AVL Tree

Use Maple:> rsolve( {F(0) = 1, F(1) = 2,

F(h) = 1 + F(h - 1) + F(h - 2)}, F(h) );

> asympt( %, h );

3 5

10

1

2

1

2

5

2

h

1

2

3 5

10

1

2

5

2

h 2 5

2

1 5

h

5 ( )1 5

2 5

2

1 5

h

5 ( )1 51

( )3 5 5 ( )1 5 h

5 ( ) 1 5 2h1

1

5

e( )h I

( ) 5 3 5 2h

( )1 5 ( )1 5 h

1 5

2

h

c

Page 23: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

23

Height of an AVL Tree

This is approximately

F(h) ≈ 1.8944 h

where ≈ 1.6180 is the golden ratio– That is, F(h) = (h)

Thus, we may find the maximum value of h for a given n:

log( n / 1.8944 ) = log( n ) – 1.3277

Page 24: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

24

Height of an AVL Tree

In this example, n = 88, the worst- and best-case scenarios differ in height by only 2

Page 25: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

25

Height of an AVL Tree

If n = 106, the bounds on h are:– The minimum height: log2( 106 ) – 1 ≈ 19

– the maximum height : log( 106 / 1.8944 ) < 28

Page 26: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

26

Maintaining Balance

To maintain AVL balance, observe that:– Inserting a node can increase the height of a tree by at most 1

– Removing a node can decrease the height of a tree by at most 1

Page 27: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

27

Maintaining Balance

Insert either 15 or 42 into this AVL tree

Page 28: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

28

Maintaining Balance

Inserting 15 does not change any heights

Page 29: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

29

Maintaining Balance

Inserting 42 changes the height of two of the sub-trees

Page 30: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

30

Maintaining Balance

To calculate changes in height, the member function must run in O(1) time

Our implementation of height is O(n):

template <typename Comp>int Binary_search_node<Comp>::height() const { return max( ( left() == 0 ) ? 0 : 1 + left()->height(), ( right() == 0 ) ? 0 : 1 + right()->height() ); }

Page 31: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

31

Maintaining Balance

Introduce a member variable

int tree_height;

The member function is now:

template <typename Comp>int BinarySearchNode<Comp>::height() const { return tree_height; }

Page 32: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

32

Maintaining Balance

Only insert and erase may change the height– This is the only place we need to update the height

– These algorithms are already recursive

Page 33: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

33

Insert

template <typename Comp>void AVL_node<Comp>::insert( const Comp & obj ) { if ( obj < element ) { if ( left() == 0 ) { left_tree = new AVL_node<Comp>( obj ); tree_height = 1; } else { left()->insert( obj ); tree_height = max( tree_height, 1 + left()->height() ); } } else if ( obj > element ) { // ... }}

Page 34: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

34

Maintaining Balance

Using the same, tree, we mark the stored heights:

Page 35: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

35

Maintaining Balance

Again, consider inserting 42– We update the heights of 38 and 44:

Page 36: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

36

Maintaining Balance

If a tree is AVL balanced, for an insertion to cause an imbalance:– The heights of the sub-trees must differ by 1

– The insertion must increase the height of the deeper sub-tree by 1

Page 37: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

37

Maintaining Balance

Starting with our sample tree

Page 38: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

38

Maintaining Balance

Inserting 23:– 4 nodes change height

Page 39: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

39

Maintaining Balance

Insert 9– Again 4 nodes change height

Page 40: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

40

Maintaining Balance

In both cases, there is one deepest node which becomes unbalanced

Page 41: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

41

Maintaining Balance

Other nodes may become unbalanced– E.g., 36 in the left tree is also unbalanced

We only have to fix the imbalance at the lowest point

Page 42: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

42

Maintaining Balance

Inserting 23– Lowest imbalance at 17

– The root is also unbalanced

Page 43: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

43

Maintaining Balance

A quick rearrangement solves all the imbalances at all levels

Page 44: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

44

Maintaining Balance

Insert 31– Lowest imbalance at 17

Page 45: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

45

Maintaining Balance

A simple rearrangement solves the imbalances at all levels

Page 46: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

46

Maintaining Balance: Case 1

Consider the following setup– Each blue triangle represents a tree of height h

Page 47: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

47

Maintaining Balance: Case 1

Insert a into this tree: it falls into the left subtree BL of b

– Assume BL remains balanced

– Thus, the tree rooted at b is also balanced

Page 48: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

48

Maintaining Balance: Case 1

However, the tree rooted at node f is now unbalanced– We will correct the imbalance at this node

Page 49: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

49

Maintaining Balance: Case 1

We will modify these three pointers– At this point, this references the unbalanced root node f

Page 50: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

50

Maintaining Balance: Case 1

Specifically, we will rotate these two nodes around the root:– Recall the first prototypical example

– Promote node b to the root and demote node f to be the right child of b

AVL_node<Comp> *pl = left(), *BR = left()->right();

Page 51: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

51

Maintaining Balance: Case 1

This requires the address of node f to be assigned to the right_tree member variable of node b

AVL_node<Comp> *pl = left(), *BR = left()->right();pl->right_tree = this;

Page 52: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

52

Maintaining Balance: Case 1

Assign any former parent of node f to the address of node b

Assign the address of the tree BR to left_tree of node f

AVL_node<Comp> *pl = left(), *BR = left()->right();pl->right_tree = this;ptr_to_this = pl;left_node = BR;

Page 53: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

53

Maintaining Balance: Case 1

The nodes b and f are now balanced and all remaining nodes of the subtrees are in their correct positions

Page 54: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

54

Maintaining Balance: Case 1

Additionally, height of the tree rooted at b equals the original height of the tree rooted at f – Thus, this insertion will no longer affect the balance of any ancestors all

the way back to the root

Page 55: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

55

Maintaining Balance: Case 2

Alternatively, consider the insertion of c where b < c < f into our original tree

Page 56: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

56

Maintaining Balance: Case 2

Assume that the insertion of c increases the height of BR

– Once again, f becomes unbalanced

Page 57: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

57

Maintaining Balance: Case 2

Unfortunately, the previous rotation does not fix the imbalance at the root of this subtree: the new root, b, remains unbalanced

Page 58: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

58

Maintaining Balance: Case 2

Unfortunately, this does not fix the imbalance at the root of this subtree

Page 59: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

59

Maintaining Balance: Case 2

Re-label the tree by dividing the left subtree of f into a tree rooted at d with two subtrees of height h – 1

Page 60: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

60

Maintaining Balance: Case 2

Now an insertion causes an imbalance at f– The addition of either c or e will cause this

Page 61: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

61

Maintaining Balance: Case 2

We will reassign the following pointersAVL_node<Comp> *pl = left(), *plr = left()->right(), *LRL = left()->right()->left(), *LRR = left()->right()->right();

Page 62: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

62

Maintaining Balance: Case 2

Specifically, we will order these three nodes as a perfect tree– Recall the second prototypical example

Page 63: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

63

Maintaining Balance: Case 2

To achieve this, b and f will be assigned as children of the new root dAVL_node<Comp> *pl = left(),

*plr = left()->right(), *LRL = left()->right()->left(), *LRR = left()->right()->right();plr->left_tree = pl;plr->right_tree = this;

Page 64: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

64

Maintaining Balance: Case 2

We also have to connect the two subtrees and original parent of fAVL_node<Comp> *pl = left(), *plr = left()->right(), *LRL = left()->right()->left(), *LRR = left()->right()->right();plr->left_tree = pl;plr->right_tree = this;ptr_to_this = lrptr;pl->right_tree = LRL;left_tree = LRR;

Page 65: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

65

Maintaining Balance: Case 2

Now the tree rooted at d is balanced

Page 66: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

66

Maintaining Balance: Case 2

Again, the height of the root did not change

Page 67: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

67

Rotations: Summary

There are two symmetric cases to those we have examined:– insertions into either the

• left-left sub-tree or the right-right sub-tree

which cause an imbalance require a single rotation to balance the node

– insertions into either the• left-right sub-tree or the right-left sub-tree

which cause an imbalance require two rotations to balance the node

Page 68: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

68

Insert (Implementation)

template <typename Comp>

void AVL_node<Comp>::insert( const Comp & obj ) {

if ( obj < element ) {

if ( left() == 0 ) {

// cannot cause an AVL imbalance

left_tree = new AVL_node<Comp>( obj );

tree_height = 1;

} else {

left()->insert( obj );

if ( left()->height() - right()->height() == 2 ) {

// determine if it is a left-left or left-right insertion

// perform the appropriate rotation

}

}

} else if ( obj > element ) {

// ...

Page 69: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

69

Insertion (Implementation)

Comments:– All the rotation statements are (1)

– An insertion requires at most 2 rotations

– All insertions are still (ln(n))

– It is possible to tighten the previous code

– Aside• if you want to explicitly rotate the nodes A and B, you must also pass a

reference to the parent pointer as an argument:

insert( Object obj, AVL_node<Comp> * & parent )

Page 70: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

70

Example Rotations

Consider the following AVL tree

Page 71: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

71

Example Rotations

• Usually an insertion does not require a rotation

Page 72: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

72

Example Rotations

• Inserting 71 cause a right-right imbalance at 61

Page 73: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

73

Example Rotations

• A single rotation balances the tree

Page 74: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

74

Example Rotations

• The insertion of 46 causes a right-left imbalance at 42

Page 75: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

75

Example Rotations

• First, we rotate outwards with 43-48

Page 76: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

76

Example Rotations

• We balance the tree by rotating 42-43

Page 77: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

77

Example Rotations

• Inserting 37 causes a left-left imbalance at 42

Page 78: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

78

Example Rotations

• A single rotation around 38-42 balances the tree

Page 79: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

79

Example Rotations

• Inserting 54 causes a left-right imbalance at 48

Page 80: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

80

Example Rotations

• We start with a rotation around 48-43

Page 81: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

81

Example Rotations

• We balance the tree by rotating 48-55

Page 82: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

82

Example Rotations

• Finally, we insert 99, causing a right-right imbalance at the root 36

Page 83: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

83

Example Rotations

• A single rotation balances the tree• Note the weight has been shifted to the left sub-tree

Page 84: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

84

Erase

Removing a node from an AVL tree may cause more than one AVL imbalance– Like insert, erase must check after it has been successfully called on a

child to see if it caused an imbalance

– Unfortunately, it may cause h/2 imbalances that must be corrected• Insertions will only cause one imbalance that must be fixed

Page 85: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

85

Erase

Consider the following AVL tree

Page 86: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

86

Erase

Suppose we erase the front node: 1

Page 87: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

87

Erase

While its previous parent, 2, is not unbalanced, its grandparent 3 is– The imbalance is in the right-right subtree

Page 88: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

88

Erase

We can correct this with a simple rotation

Page 89: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

89

Erase

The node of that subtree, 5, is now balanced

Page 90: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

90

Erase

Recursing to the root, however, 8 is also unbalanced– This is a right-left imbalance

Page 91: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

91

Erase

Promoting 11 to the root corrects the imbalance

Page 92: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

92

Erase

At this point, the node 11 is balanced

Page 93: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

93

Erase

Still, the root node is unbalanced– This is a right-right imbalance

Page 94: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

94

Erase

Again, a simple rotation fixes the imbalance

Page 95: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

95

Erase

The resulting tree is now AVL balanced– Note, few erases will require one rotation, even fewer will require more

than one

Page 96: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

96

AVL Trees as Arrays?

We previously saw that:– Complete tree can be stored using an array using (n) memory

– An arbitrary tree of n nodes requires O(2n) memory

Is it possible to store an AVL tree as an array and not require exponentially more memory?

Page 97: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

97

AVL Trees as Arrays?

Recall that in the worst case, an AVL tree of n nodes has a height at most

log(n) – 1.3277

Such a tree requires an array of size

2log(n) – 1.3277 + 1 – 1

We can rewrite this as

2–0.3277 nlog(2) ≈ 0.7968 n1.44

Thus, we would require O(n1.44) memory

Page 98: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

98

AVL Trees as Arrays?

While the polynomial behaviour of n1.44 is not as bad as exponential behaviour, it is still reasonably sub-optimal when compared to the linear growth associated with node-based trees

Here we see n and n1.44 on [0, 1000]

Page 99: AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.

AVL Trees

99

Summary

• In this topic we have covered:– Insertions and removals are like binary search trees

– With each insertion or removal there is at most one correction required• a single rotation, or• a double rotation;

which runs in O(1) time

– Depth is O( ln(n) )

∴ all operations are O( ln(n) )