ch15: avl tree · avl tree •avl trees are balanced. •the worst case is when the right...

37
Ch15: AVL Tree 305233, 305234 Algorithm Analysis and Design Jiraporn Pooksook Naresuan University

Upload: others

Post on 25-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Ch15: AVL Tree

305233, 305234 Algorithm Analysis and Design

Jiraporn PooksookNaresuan University

Page 2: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Review: Binary Search Tree

• Rooted binary tree

• Each node has– Key

– Left pointer

41

20 65

Left pointer

– Right pointer

– Parent pointer

• Binary search tree property– Let x be a node in a binary search tree. If y is a node

in the left subtree of x, then key[y] ≤ key[x]. If y is a node in the right subtree of x, then key[x] ≤ key[y].

11 29 50

26

Page 3: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Review: Binary Search Tree

• Binary search tree supports

– Insert

– Delete

– Minimum– Minimum

– Maximum

– Successor

• In O(h) time where h is the height of a binary search tree

Page 4: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Height Balanced

15

6 20

3 9 25

Height = lg n

17

2 4 137 16 18 23 29

Page 5: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Height Unbalanced

15

20

25

Height = n

29

Page 6: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Height

• Height of a tree is the length of longest path of the root down to a leaf.

• Height of a node is the length of longest path of it node down to a leaf.of it node down to a leaf.

Page 7: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Example: Height

41

20 65

2 1

3

-1

11 29 50

26

0

10 0

Height of node = max {height of left child , height of right child}

-1 -1

-1

-1-1

-1

-1

-1

Page 8: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

AVL Tree

• An AVL tree is a binary search tree that is height balanced.

• For each node x, the heights of the left and right subtrees of x differ by at most 1.right subtrees of x differ by at most 1.

• An AVL tree with n nodes has height O(lg n).

x

|hr - hr | ≤ 1hr hl

Page 9: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

AVL Tree

• AVL trees are balanced.

• The worst case is when the right subtree has height 1 more than left for every node.

1 1

Page 10: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Analyze the Height of AVL Tree

• Let Nh to be a minimum number of nodes in an AVL tree of height h.

• Nh = 1 + Nh-1 + Nh-2

• Nh > Fh where Fh is a Fibonacci function• Nh > Fh where Fh is a Fibonacci function

• Nh > 1.618h /51/2

• Denote Nh = n

• 1.618h /51/2 < n

• h < 1.440 lg n h-1h-2

h

Height of AVL tree = lg n

Page 11: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

AVL Insert

• Use a simple binary search tree insert

• Fix the AVL property using rotations

Page 12: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Example: AVL Insert(T,23)

41

20 65

3 1

4

-1

11 29

26

1

20

-1

-1

-1-1

23

0

The Tree is unbalanced !!!

50

0

-1

-1

-1

Page 13: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Rotations

x

y x

y

Left Rotate(T,x)

Right Rotate(T,y)

A

y

B C

x

A B

C

Page 14: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Left-Rotate(T,x)y = right[x]right[x] = left[y]if left[y] != nil[T]

then p[left[y]] = xp[y] = p[x]p[y] = p[x]if p[x]= nil[T]

then root[T] = yelse if x = left[p[x]]

then left[p[x]] = yelse right[p[x]] = yleft[y] =xp[x] = y

Page 15: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Example: Left-Rotate(T,29)

41

20 65

x

11 2950

32

36

y

x

Page 16: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Example: Left-Rotate(T,29)

41

20 65

x

11 29 50

32

36

y

x

Page 17: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Example: Left-Rotate(T,29)

41

20 65

x

11 2950

32

36

y

x

Page 18: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Example: Left-Rotate(T,29)

41

20 65

y

11

29

5032

36

y

x

Page 19: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Example: Left-Rotate(T,29)

41

20 65

y

11

29

5032

36

y

x

Page 20: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Example: Right-Rotate(T,29)

41

20 65

11 29 50

26

23

Page 21: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Example: Right-Rotate(T,29)

41

20 65

11 26 50

23 29

Page 22: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Example: AVL Insert(T,23)

41

20 65

3 1

4

-1

11 29

26

1

20

-1

-1

-1-1

23

0

Right Rotate(T,29)

50

0

-1

-1

-1

Page 23: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Example: AVL Insert(T,23)

41

20 65

2 1

3

-1

11

2923

0

10

-1

-1-1

26

-1 -1-1

0

50

0

-1

-1

-1

Page 24: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Example: AVL Insert(T,55)

41

20 65

2

1

2

3

11

29

50

23

0

101

-1

-1-1 -1

26

-1 -1-1

0

55

The Tree is unbalanced !!!

Page 25: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Example: AVL Insert(T,55)

41

20 65

2

1

2

3

x

y

11

29

50

23

0

101

-1

-1-1 -1

26

-1 -1-1

0

55

Right Rotate(T,65)

x

Page 26: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Example: AVL Insert(T,55)

41

20 50

2

1

2

3

11

29

65

23

0

101

-1

-1-1

26

-1 -1-1

0

55

-1

The Tree is unbalanced !!!

Page 27: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Example: AVL Insert(T,55)

41

20 65

2

1

2

3

x

11

29

50

23

0

101

-1

-1-1 -1

26

-1 -1-1

0

55

Left-Rotate(T,50)

x

y

Page 28: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Example: AVL Insert(T,55)

41

20 65

2

1

2

3

y

11

29

55

23

0

101

-1

-1-1

26

-1 -1-1

0

50

The Tree is unbalanced !!!Right-Rotate(T,65)

x

Page 29: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Example: AVL Insert(T,55)

41

20

2

0

1

3

55

0

6511

29

50

23

0

100

-1

-1-1

26

-1 -1-1

0

0

-1-1

Page 30: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

AVL Insert

• Use a simple binary search tree insert

• Fix the AVL property from changed node up

– Suppose x is lowest node violating AVL property

Page 31: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

AVL Insert

• Assume the right child of x is higher

• If the right child of x is right-heavy or balanced, then we do right-rotate(x)

k-1

x

A

y

BC

x

A

y

B

C

Right Rotate(T,x)

k

k-1

k-2k-3

k-3

k-3 k-3

k-2

k-1

k-2

Page 32: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

AVL Insert

• Assume the right child of x is higher

• Else

xx

Right-Rotate(T,z)x

A

y

B C

A

y

B

C

z

Dz

D

Page 33: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

AVL Insert

• Assume the right child of x is higher

• Else

y

Left-Rotate(T,x)x

x

A

y

B C

z

D

A

y

B

C

z

D

Page 34: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

AVL Insert

• Assume the right child of x is higher

• Else

x y

Right-Rotate(T,z)Left-Rotate(T,x)x

A

y

B C

x

A

y

B C

Left-Rotate(T,x)

z

D

z

D

Page 35: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

AVL Sort

• Insert n items

– Take O(n lg n) time

• In-order tree traversal

– Take O(n) time– Take O(n) time

Page 36: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Summary

• Abstract Data Types Data Structure

• Insert , Delete ●Heap or AVL

• Min ● Balanced BST

• Successor/ predecessor

Priority queue

• Successor/ predecessor

Page 37: Ch15: AVL Tree · AVL Tree •AVL trees are balanced. •The worst case is when the right subtreehas height 1 more than left for every node. 1 1. Analyze the Height of AVL Tree •Let

Practice: AVL Insert

4 7 5 10 23 65 73