cs2351 data structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 avl tree •let x be...

42
1 CS2351 Data Structures Lecture 14: AVL Tree

Upload: others

Post on 08-Jun-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

1

CS2351Data Structures

Lecture 14:AVL Tree

Page 2: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

2

About this lecture•A general binary search tree (BST) does

not have good worst-case performancesince its height can be (n)

•In this lecture, we discuss a balanced BSTcalled AVL tree, whose height = O(log n) Query is done in O( log n ) time•More involved updates due to balancing• invented by Adelson-Velskii and Landis

Page 3: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

3

AVL Tree

•Let x be a node.•Let L and R be its left and right subtrees.•We define balance factor of x to be :

bf(x) = Height of L –Height of R

•An AVL tree is a BST with the property :

Each node has a balance factorof either 1, 0, or –1

Page 4: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

4

Example of AVL Tree

8

23

48

31

5

3

Are these AVL trees?

8

5

3 6

–1

+1 –2

+1

0

0

+2

0

0 0

Page 5: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

5

Example of AVL Tree

8

23

48

5

3

Are these AVL trees?

8

235

3 6

0

+1 –1

00

+1

0

0 0

0

Page 6: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

6

Height of an AVL Tree

•Let h be the node-height of an AVL tree.•Then we have :

Theorem : h 1.4405 log n + O(1)

•The idea of the proof is that :If an AVL tree has node-height h, then itmust have a lot of nodes so that it cannotbe too “skewed”

Page 7: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

7

Proof

•Let Nh be the number of nodes in thesmallest AVL tree with height h N1 = 1, N2 = 2 Nh = Nh–1 + Nh–2 + 1 (why?)

•Indeed, we can show that (how?)Nh = Fh+1 –1

where Fk = kth Fibonacci number (F0 = F1 = 1)

Page 8: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

8

Proof (cont)

•It is known that for Fibonacci number Fk :Fk (k)

where = (1+5)/2 = 1.61803…

•Thus, if n is the number of nodes in anAVL tree with node-height h

n Nh c h+1 [c is a constant]

h logn + O(1) 1.4405 log n + O(1)

Page 9: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

9

Query Performance

Corollary :The queries minimum, maximum, search,predecessor, and successor can each beperformed in O(log n) time in an AVL tree

Page 10: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

10

Updates in an AVL Tree

Page 11: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

11

Updates in an AVL Tree•Updates are performed in the same way as

in a general BST, except that we needbalancing if the tree shape is too “skewed”

•The balancing is based on a powerfuloperation called “rotation”•also used in other balanced BST, such

as Red-Black tree or Splay tree

Page 12: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

12

Rotation

A B

C A

B C

right rotation

left rotation

Observation : After rotation, the inordertraversal ordering remains unchanged.

Page 13: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

13

Remark•If one subtree is too tall, we may use some

rotations to balance the tree•Ex : How to balance the following cases ?

8

5

3

8

5

6

In fact, we can always transform one BST toanother just by rotations (how to show?)

Page 14: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

14

•We can define R_rotate as follows usingTransplant from the previous lecture :

Implementation in C

// Assume *x has left childNode * R_rotate( Node *x ) {

y = x->left ;Transplant( y, y->right );Transplant( x, y );y->right = x ;x->parent = y ;

}

Page 15: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

15

•Similarly, we can define L_rotate, andthen LR_rotate or RL_rotate :

Implementation in C

// Assume *x has left child// and *(x->left) has right childNode * LR_rotate( Node *x ) {

L_rotate( x->left ) ;R_rotate( x );

}

Page 16: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

16

Insertion in an AVL Tree

Page 17: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

17

Insertion•Insertion is the same way as before,

except that after insertion, the balancefactor of some nodes (along the insertionpath) may increase

Insertion path

Page 18: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

18

Insertion•Consequently, we need to balance these

nodes so that AVL property is maintained•This is done by a bottom-up fashion

Insertion path

Page 19: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

19

Case 1 (No Height Change)•If no height change in the subtree balance factor of a node (and its

ancestors) is not changed done !

Height notchanged

Page 20: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

20

Case 2.1 (Height Increases)•If height of the subtree increases (by 1) If balance factor was 0 originally Update balance factor and continue

to balance the ancestors

Heightincreases

0

Heightincreases

-1

Page 21: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

21

Case 2.2 (Height Increases)•If height of the subtree increases (by 1) If other subtree was taller originally Set balance factor to 0, and done !

Heightincreases

+1 0

Page 22: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

22

Case 2.3 (Height Increases)•If height of the subtree increases (by 1) If other subtree was shorter originally Perform rotation

Heightincreases

-1 -2Performrotation

Page 23: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

23

Case 2.3 (Height Increases)•There are two subcases for Case 2.3 :

-2

-1one rotation 0

0

First subcase

Page 24: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

24

Case 2.3 (Height Increases)•There are two subcases for Case 2.3 :

-2

+1two rotations

Second subcase

What is theresulting tree ?

Page 25: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

25

Case 2.3 (Height Increases)•First Rotation :

-2

+1 firstrotation

Second subcase

bC

B

-2

?

b’C

B

Page 26: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

26

Case 2.3 (Height Increases)•Second Rotation :

secondrotation

Second subcase

-2

?

b’C

B

0

b’b” C

B

Page 27: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

27

Case 2.3 (Height Increases)•What if the child node on the insertion

path has balance factor 0 ?-2

0 Third subcase ?

We can prove that using ourinsertion scheme, if childnode has balance factor 0,height cannot increase

Page 28: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

28

Case 2.3 (Height Increases)

•After perform rotations in eithersubcases, the node becomes balanced No change is needed for the ancestor Done !

Page 29: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

29

Implementation in C

void Insert( Node *x, Node *z ) {if ( x->key < z->key ) {

if ( x->right ) Insert( x->right, z );else x->right = z ;

}else ...

}

•Recall the Insert function in the BST :

•We now modify it to handle balancing …

Page 30: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

30

void Insert( Node *x, Node *z ) {if ( x->key < z->key ) {

if ( x->right ) Insert( x->right, z );else { x->right = z ; z->bf = 0 ;

height_inc = TRUE ; }if ( height_inc ){ /* Handle Cases 2.1, 2.2 & 2.3 */ }

}else ...

}

height_inc is a global variable to indicate if height ofsubtree of x has increased during insertion

Implementation in C

Page 31: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

31

/* Case 2.1 */if ( x->bf == 0 )

x->bf = -1 ;

/* Case 2.2 */else if ( x->bf == 1 ){ x->bf = 0 ; height_inc = FALSE ; }

/* Case 2.3 */else ...

Handling Cases 2.1 and 2.2

Page 32: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

32

/* Case 2.3 */else {

/* First Subcase */if ( x->right->bf == -1 ) {

L_rotate( x );x->bf = x->parent->bf = 0 ;height_inc = FALSE ;

}/* Second Subcase */ else ...

} ...

Handling Case 2.3

Page 33: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

33

/* Second Subcase */else if ( x->right->bf == 1 ) {

int b = x->right->left->bf ;LR_rotate( x ); x->parent->bf = 0;if ( b == 0 ){ x->bf = x->parent->right->bf = 0; }else if ( b == 1 ){ x->bf = 0; x->parent->right->bf = -1; }else if ( b == -1 ){ x->bf = 1; x->parent->right->bf = 0; }height_inc = FALSE ;

}

Handling Case 2.3

Page 34: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

34

Deletion in an AVL Tree

Page 35: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

35

Deletion•Deletion is the same way as before,

except that after deletion, balance factorof the ancestors of the node “actually”deleted (ex: successor) may decrease

Page 36: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

36

Deletion•Consequently, we need to balance these

nodes so that AVL property is maintained•Again, this is done by a bottom-up fashion

Page 37: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

37

Case 1 (No Height Change)•If no height change in the subtree balance factor of a node (and its

ancestors) is not changed done !

Height notchanged

Page 38: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

38

Case 2.1 (Height Decreases)•If height of the subtree decreases (by 1) If balance factor was 0 originally Update balance factor and done !

( handle differently from insertion )

Heightdecreases

0 +1

Height notchanged

Page 39: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

39

Case 2.2 (Height Decreases)•If height of the subtree decreases (by 1) If other subtree was taller originally Rotations, set balance factor to 0 Continue to balance ancestors (why?)

+1

Heightdecreases

Perform one ortwo rotations 0

Heightdecreases

Page 40: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

40

Case 2.3 (Height Decreases)•If height of the subtree decreases (by 1) If other subtree was shorter originally Set balance factor to 0 Continue to balance ancestors

-1

Heightdecreases

0

Heightdecreases

Page 41: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

41

Update Performance

Corollary :Insertion or deletion can each beperformed in O(log n) time in an AVL tree

Remarks : Each insertion requires at most 2rotations, but may update O(log n) nodes

Q: How about deletion ?

Page 42: CS2351 Data Structureswkhon/ds/ds12/lecture/lecture14.pdf · 2010-04-28 · 3 AVL Tree •Let x be a node. •Let L and R be its left and right subtrees. •We define balance factor

42

# of Rotations in Deletion

+1

+1

+1

In the worst case, we needto rotate in each level

O( log n ) rotations !