Objectives Memahami variant dari Binary Search Tree yang balanced - - PowerPoint PPT Presentation

objectives
SMART_READER_LITE
LIVE PREVIEW

Objectives Memahami variant dari Binary Search Tree yang balanced - - PowerPoint PPT Presentation

Objectives Memahami variant dari Binary Search Tree yang balanced Struktur Data & Algoritme ( Data Structures & Algorithms ) AVL Tree Denny ( denny@cs.ui.ac.id ) Suryana Setiawan ( setiawan@cs.ui.ac.id ) Fakultas I lm u Kom puter


slide-1
SLIDE 1

1

Struktur Data & Algoritme ( Data Structures & Algorithms)

Denny (denny@cs.ui.ac.id) Suryana Setiawan (setiawan@cs.ui.ac.id)

Fakultas I lm u Kom puter Universitas I ndonesia Sem ester Genap - 2 0 0 4 / 2 0 0 5

Version 2 .0 - I nternal Use Only

AVL Tree

SDA/ TOPIC/ V2.0/ 2

Objectives

Memahami variant dari Binary Search Tree yang

balanced

SDA/ TOPIC/ V2.0/ 3

Outline

AVL Tree Definition Property

  • perations

SDA/ TOPIC/ V2.0/ 4

Motivation

Unbalanced Binary Search Tree would make all

  • perations take O(n) operation - worst case.
slide-2
SLIDE 2

2

SDA/ TOPIC/ V2.0/ 5

X X

AVL Trees

Untuk setiap node dalam tree, ketinggian subtree di

anak kiri dan subtree di anak kanan hanya berbeda maksimum 1.

H H-1 H-2

SDA/ TOPIC/ V2.0/ 6

AVL Trees

10 5 3 20 2 1 3 10 5 3 20 1 43 5

SDA/ TOPIC/ V2.0/ 7

AVL Trees

12 8 16 4 10 2 6 14

SDA/ TOPIC/ V2.0/ 8

12 8 16 4 10 2 6 14 1

Insertion for AVL Tree

After insert 1

slide-3
SLIDE 3

3

SDA/ TOPIC/ V2.0/ 9

Insertion for AVL Tree

To 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

SDA/ TOPIC/ V2.0/ 10

Unbalanced Condition

R P Q k1 k2 Q k2 P k1 R

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

H P= H Q= H R

SDA/ TOPIC/ V2.0/ 11

A k2 B k1 C C B A k1 k2

Single Rotation (case 1)

H A= H B+ 1 H B= H C

SDA/ TOPIC/ V2.0/ 12

Single Rotation (case 4)

C k1 B k2 A A B C k2 k1 H A= H B H C= H B+ 1

slide-4
SLIDE 4

4

SDA/ TOPIC/ V2.0/ 13

Q k2 P k1 R R P Q k1 k2

Problem with Single Rotation

Single rotation does not work for case 2 and 3 (inside

case) H Q= H P+ 1 H P= H R

SDA/ TOPIC/ V2.0/ 14

C k3 A k1 D B k2

Double Rotation: Step

C k3 A k1 D B k2 H A= H B= H C= H D

SDA/ TOPIC/ V2.0/ 15

C k3 A k1 D B k2

Double Rotation: Step

SDA/ TOPIC/ V2.0/ 16

C k3 A k1 D B k2 C k3 A k1 D B k2

Double Rotation

H A= H B= H C= H D

slide-5
SLIDE 5

5

SDA/ TOPIC/ V2.0/ 17

B k1 D k3 A C k2 B k1 D k3 A C k2

Double Rotation

H A= H B= H C= H D

SDA/ TOPIC/ V2.0/ 18

3

Example

Insert 3 into the AVL tree

11 8 20 4 16 27 8 8 11 4 20 3 16 27

SDA/ TOPIC/ V2.0/ 19

Example

Insert 5 into the AVL tree

5 11 8 20 4 16 27 8 11 5 20 4 16 27 8

SDA/ TOPIC/ V2.0/ 20

AVL Trees: Exercise

Insertion order: 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55

slide-6
SLIDE 6

6

SDA/ TOPIC/ V2.0/ 21

Remove Operation in AVL Tree

Removing from AVL Tree is same as removing from a

binary search tree procedure with this difference that 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.

We use the appropriate single or double rotation to

balance the tree.

We may need to continue searching for unbalanced

node all the way to the root.

SDA/ TOPIC/ V2.0/ 22

Deletion X in AVL Trees

Deletion: Case 1: jika X adalah leaf, delete X Case 2: jika X punya 1 child, X digantikan oleh child

tsb.

Case 3: jika X punya 2 child, ganti X secara rekursif

dengan predecessor-nya secara inorder

Rebalancing

SDA/ TOPIC/ V2.0/ 23

Delete 55 (case 1)

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

SDA/ TOPIC/ V2.0/ 24

Delete 55 (case 1)

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

slide-7
SLIDE 7

7

SDA/ TOPIC/ V2.0/ 25

Delete 50 (case 2)

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

SDA/ TOPIC/ V2.0/ 26

Delete 50 (case 2)

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

SDA/ TOPIC/ V2.0/ 27

Delete 60 (case 3)

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

SDA/ TOPIC/ V2.0/ 28

Delete 60 (case 3)

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

slide-8
SLIDE 8

8

SDA/ TOPIC/ V2.0/ 29

Delete 55 (case 3)

55 20 70 10 40 65 85 5 15 30 50 80 90 prev

SDA/ TOPIC/ V2.0/ 30

Delete 55 (case 3)

50 20 70 10 40 65 85 5 15 30 80 90

SDA/ TOPIC/ V2.0/ 31

Delete 50 (case 3)

50 20 70 10 40 65 85 5 15 30 80 90 prev

SDA/ TOPIC/ V2.0/ 32

Delete 50 (case 3)

40 20 70 10 30 65 85 5 15 80 90

slide-9
SLIDE 9

9

SDA/ TOPIC/ V2.0/ 33

Delete 40 (case 3)

40 20 70 10 30 65 85 5 15 80 90 prev

SDA/ TOPIC/ V2.0/ 34

Delete 40 : Rebalancing

30 20 70 10 65 85 5 15 80 90 Case ?

SDA/ TOPIC/ V2.0/ 35

Delete 40: after rebalancing

30 70 10 20 65 85 5 15 80 90

Single rotation is preferred!

SDA/ TOPIC/ V2.0/ 36

Minimum Element in AVL Tree

An AVL Tree of height H has at least FH+3-1 nodes,

where Fi is the i-th fibonacci number

S0 = 1 S1 = 2 SH = SH-1 + SH-2 + 1

SH-1 SH-2

H H-1 H-2

slide-10
SLIDE 10

10

SDA/ TOPIC/ V2.0/ 37

AVL Tree: analysis (1) 328 . 1 ) 2 log( 44 . 1 5 / φ ) ( 618 . 1 2 / ) 5 (1 φ 5 / φ

3 H i

− + < ≈ + = ≈

+

N H roughly least at has H height

  • f

Tree AVL Fi

SDA/ TOPIC/ V2.0/ 38

AVL Tree: analysis (2)

The depth of AVL Trees at is at most logarithmic. So, all of the operations on AVL trees are also

logarithmic.

The worst-case height is at most 44 percent more

than the minimum possible for binary trees.

SDA/ TOPIC/ V2.0/ 39

Summary

Find element, insert element, and remove element

  • perations all have complexity O(log n) for worst

case

Insert operation: top-down insertion and bottom up

balancing

SDA/ TOPIC/ V2.0/ 40

Further Study

http://telaga.cs.ui.ac.id/WebKuliah/IKI101

00/resources/animation/data- structure/avl/avltree.html (mirror from http://www.geocities.com/SiliconValley/ Heights/8471/avltree.html)

Chapter 18

slide-11
SLIDE 11

11

SDA/ TOPIC/ V2.0/ 41

What’s Next

B Trees