AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt...

37
AVL Tree AVL Tree Smt Genap 2011-2012

Transcript of AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt...

Page 1: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

AVL TreeAVL Tree

Smt Genap 2011-2012

Page 2: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

OutlineOutlineAVL Tree

◦Definition◦Properties◦Operations

Smt Genap 2011-2012

Page 3: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

AVL TreesAVL TreesUnbalanced Binary Search Trees are bad. Worst case: operations take O(n).

AVL (Adelson-Velskii & Landis) trees maintain balance.

For each node in tree, height of left subtree and height of right subtree differ by a maximum of 1.

Smt Genap 2011-2012

X X

H

H-1H-2

Page 4: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

AVL TreesAVL Trees

Smt Genap 2011-2012

10

5

3

20

2

1 3

10

5

3

20

1

43

5

Page 5: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

AVL TreesAVL Trees

Smt Genap 2011-2012

12

8 16

4 10

2 6

14

Page 6: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Insertion for AVL TreeInsertion for AVL TreeAfter insert 1

Smt Genap 2011-2012

12

8 16

4 10

2 6

14

1

Page 7: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Insertion for AVL TreeInsertion for AVL TreeTo ensure balance condition for

AVL-tree, after insertion of a new node, we back up the path from the inserted node to root and check the balance condition for each node.

If after insertion, the balance condition does not hold in a certain node, we do one of the following rotations:◦Single rotation◦Double rotation

Smt Genap 2011-2012

Page 8: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Insertions Causing Insertions Causing ImbalanceImbalance

An insertion into the subtree:◦ P (outside) - case 1◦ Q (inside) - case 2

An insertion into the subtree:◦ Q (inside) - case 3◦ R (outside) - case 4

Smt Genap 2011-2012

RP

Q

k1

k2

Q

k2

P

k1

R

HP=HQ=HR

Page 9: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Single Rotation (case 1)Single Rotation (case 1)

Smt Genap 2011-2012

A

k2

B

k1

CCBA

k1

k2

HA=HB+1HB=HC

Page 10: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Single Rotation (case 4)Single Rotation (case 4)

Smt Genap 2011-2012

C

k1

B

k2

AA B C

k2

k1

HA=HB

HC=HB+1

Page 11: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Problem with Single Problem with Single RotationRotationSingle rotation does not work for case

2 and 3 (inside case)

Smt Genap 2011-2012

Q

k2

P

k1

RR

P

Q

k1

k2

HQ=HP+1HP=HR

Page 12: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Double Rotation: StepDouble Rotation: Step

Smt Genap 2011-2012

C

k3

A

k1

D

B

k2

C

k3

A

k1

D

B

k2

HA=HB=HC=HD

Page 13: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Double Rotation: StepDouble Rotation: Step

Smt Genap 2011-2012

C

k3

A

k1

DB

k2

Page 14: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Double RotationDouble Rotation

Smt Genap 2011-2012

C

k3

A

k1

D

B

k2

C

k3

A

k1

DB

k2

HA=HB=HC=HD

Page 15: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Double RotationDouble Rotation

Smt Genap 2011-2012

B

k1

D

k3

A

C

k2

B

k1

D

k3

A C

k2

HA=HB=HC=HD

Page 16: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

ExampleExampleInsert 3 into the AVL tree

Smt Genap 2011-2012

3

11

8 20

4 16 27

8

8

11

4 20

3 16 27

Page 17: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

ExampleExampleInsert 5 into the AVL tree

Smt Genap 2011-2012

5

11

8 20

4 16 27 8

11

5 20

4 16 27

8

Page 18: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

AVL Trees: ExerciseAVL Trees: ExerciseInsertion order:

◦10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55

Smt Genap 2011-2012

Page 19: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Remove Operation in AVL Remove Operation in AVL TreeTreeRemoving a node from an AVL Tree is

the same as removing from a binary search tree. However, it may unbalance the tree.

Similar to insertion, starting from the removed node we check all the nodes in the path up to the root for the first unbalance node.

Use the appropriate single or double rotation to balance the tree.

May need to continue searching for unbalanced nodes all the way to the root.

Smt Genap 2011-2012

Page 20: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Deletion X in AVL TreesDeletion X in AVL TreesDeletion:

◦Case 1: if X is a leaf, delete X◦Case 2: if X has 1 child, use it to

replace X◦Case 3: if X has 2 children, replace X

with its inorder predecessor (and recursively delete it)

Rebalancing

Smt Genap 2011-2012

Page 21: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Delete 55 (case 1)Delete 55 (case 1)

Smt Genap 2011-2012

60

20 70

10 40 65 85

5 15 30 50 80 90

55

Page 22: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Delete 55 (case 1)Delete 55 (case 1)

Smt Genap 2011-2012

60

20 70

10 40 65 85

5 15 30 50 80 90

55

Page 23: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Delete 50 (case 2)Delete 50 (case 2)

Smt Genap 2011-2012

60

20 70

10 40 65 85

5 15 30 50 80 90

55

Page 24: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Delete 50 (case 2)Delete 50 (case 2)

Smt Genap 2011-2012

60

20 70

10 40 65 85

5 15 30 50 80 90

55

Page 25: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Delete 60 (case 3)Delete 60 (case 3)

Smt Genap 2011-2012

60

20 70

10 40 65 85

5 15 30 50 80 90

55

prev

Page 26: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Delete 60 (case 3)Delete 60 (case 3)

Smt Genap 2011-2012

55

20 70

10 40 65 85

5 15 30 50 80 90

Page 27: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Delete 55 (case 3)Delete 55 (case 3)

Smt Genap 2011-2012

55

20 70

10 40 65 85

5 15 30 50 80 90

prev

Page 28: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Delete 55 (case 3)Delete 55 (case 3)

Smt Genap 2011-2012

50

20 70

10 40 65 85

5 15 30 80 90

Page 29: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Delete 50 (case 3)Delete 50 (case 3)

Smt Genap 2011-2012

50

20 70

10 40 65 85

5 15 30 80 90

prev

Page 30: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Delete 50 (case 3)Delete 50 (case 3)

Smt Genap 2011-2012

40

20 70

10 30 65 85

5 15 80 90

Page 31: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Delete 40 (case 3)Delete 40 (case 3)

Smt Genap 2011-2012

40

20 70

10 30 65 85

5 15 80 90

prev

Page 32: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Delete 40 : RebalancingDelete 40 : Rebalancing

Smt Genap 2011-2012

30

20 70

10 65 85

5 15 80 90

Case ?

Page 33: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Delete 40: after Delete 40: after rebalancingrebalancing

Smt Genap 2011-2012

30

7010

20 65 855

15 80 90

Single rotation is preferred!

Page 34: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Minimum Element in AVL Minimum Element in AVL TreeTreeAn AVL Tree of height H has at least FH+3-1

nodes, where Fi is the i-th fibonacci numberS0 = 1S1 = 2SH = SH-1 + SH-2 + 1

Smt Genap 2011-2012

SH-1SH-2

H

H-1H-2

Page 35: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

AVL Tree: analysis (2)AVL Tree: analysis (2)The depth of AVL Trees is at most

logarithmic.So, all of the operations on AVL trees

are also logarithmic.

Smt Genap 2011-2012

Page 36: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

SummarySummaryFind element, insert element, and

remove element operations all have complexity O(log n) for worst case

Insert operation: top-down insertion and bottom up balancing

Smt Genap 2011-2012

Page 37: AVL Tree Smt Genap 2011-2012. Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap 2011-2012.

Further StudyFurther StudySection 19.4 of Weiss book

Smt Genap 2011-2012