Вы находитесь на странице: 1из 38

IKI 10100: Data Structures & Algorithms

AVL Tree

Ruli Manurung
(acknowledgments to Denny & Ade Azurat)

Fasilkom UI

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 1


Outline
AVL Tree
 Definition
 Properties
 Operations

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 2


AVL Trees
Unbalanced 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.

X X

H
H-2
H-1

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 3


AVL Trees

10 10

5 20 5 20

3 3 43

1
2

1 3

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 4


AVL Trees

12

8 16

4 10 14

2 6

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 5


Insertion for AVL Tree
After insert 1

12

8 16

4 10 14

2 6

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 6


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

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 7


Insertions Causing Imbalance

HP=HQ=HR k2 k1

k1 k2

R P
P Q Q R

An insertion into the subtree: An insertion into the subtree:


 P (outside) - case 1  Q (inside) - case 3
 Q (inside) - case 2  R (outside) - case 4

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 8


Single Rotation (case 1)

HA=HB+1
HB=HC

k2 k1

k1 k2

C A
B B C
A

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 9


Single Rotation (case 4)

HA=HB
HC=HB+1

k2 k1

k1 k2

A
A B C B C

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 10


Problem with Single Rotation
Single rotation does not work for case 2 and 3 (inside
case)

k2 k1

k1 k2

R P
P R
Q Q

HQ=HP+1
HP=HR
Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 11
Double Rotation: Step

k3 k3

k1 k2
k2 k1

D D
A C
B C A B

HA=HB=HC=HD
Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 12
Double Rotation: Step

k2

k1 k3

A B C D

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 13


Double Rotation

k3 k2

k1 k1 k3
k2

D B C
A D
A
B C

HA=HB=HC=HD

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 14


Double Rotation

k2 k1

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

HA=HB=HC=HD

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 15


Example
Insert 3 into the AVL tree

11 11

8 20 4 20

4 16 27 3 8 16 27
3

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 16


Example
Insert 5 into the AVL tree

11 11

8 20 5 20

4 16 27 4 8 16 27
5

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 17


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

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 18


Remove Operation in AVL Tree
Removing 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.

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 19


Deletion X in AVL Trees
Deletion:
 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

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 20


Delete 55 (case 1)

60

20 70

10 40 65 85

5 15 30 50 80 90

55

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 21


Delete 55 (case 1)

60

20 70

10 40 65 85

5 15 30 50 80 90

55

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 22


Delete 50 (case 2)

60

20 70

10 40 65 85

5 15 30 50 80 90

55

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 23


Delete 50 (case 2)

60

20 70

10 40 65 85

5 15 30 50 80 90

55

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 24


Delete 60 (case 3)

60

20 70

10 40 65 85

5 15 30 50 prev 80 90

55

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 25


Delete 60 (case 3)

55

20 70

10 40 65 85

5 15 30 50 80 90

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 26


Delete 55 (case 3)

55

20 prev 70

10 40 65 85

5 15 30 50 80 90

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 27


Delete 55 (case 3)

50

20 70

10 40 65 85

5 15 30 80 90

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 28


Delete 50 (case 3)

50

20 prev 70

10 40 65 85

5 15 30 80 90

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 29


Delete 50 (case 3)

40

20 70

10 30 65 85

5 15 80 90

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 30


Delete 40 (case 3)

40

20 prev 70

10 30 65 85

5 15 80 90

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 31


Delete 40 : Rebalancing

30

20 70

10 Case ? 65 85

5 15 80 90

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 32


Delete 40: after rebalancing

30

10 70

5 20 65 85

15 80 90

Single rotation is preferred!

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 33


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

H
H-2
H-1 SH-2
SH-1

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 34


AVL Tree: analysis (1)

Fi  φ / 5 i

φ  (1  5 ) / 2  1.618

AVL Tree of height H has at least (roughly )


H 3
φ / 5
H  1.44 log( N  2)  1.328

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 35


AVL Tree: analysis (2)
The depth of AVL Trees 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.

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 36


Summary
Find 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

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 37


Further Study
http://telaga.cs.ui.ac.id/WebKuliah/IKI101
00/resources/animation/data-
structure/avl/avltree.html
Section 19.4 of Weiss book

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 27th Mar 2007 38

Вам также может понравиться