struktur data & algoritma - perbanyaklah pengetahuan filestruktur data & algoritma suryana...
TRANSCRIPT
Struktur Data & Algoritma
Suryana Setiawan, Ruli Manurung & Ade Azurat(acknowledgments: Denny)
1
Fasilkom UI
SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P
AVL Tree
Semester Ganjil – 2009/2010
2SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Tujuan
Memahami variant dari Binary Search Tree yang
balanced
3SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Outline
AVL Tree
Definition
Property
operations
4SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Motivasi
Binary Search Tree yang tidak balance dapat membuat
seluruh operasi memiliki kompleksitas running time
O(n) pada kondisi worst case.
5SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
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-1H-2
6SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
AVL Trees
10
5
3
20
2
1 3
10
5
3
20
1
43
5
7SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
AVL Trees
12
8 16
4 10
2 6
14
8SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
12
8 16
4 10
2 6
14
1
Insertion pada AVL Tree
Setelah insert 1
9SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Insertion pada AVL Tree
Untuk menjamin kondisi balance pada AVL tree,
setelah penambahan sebuah node. jalur dari node baru
tersebut hingga root di simpan dan di periksa kondisi
balance pada tiap node-nya.
Jika setelah penambahan, kondisi balance tidak
terpenuhi pada node tertentu, maka lakukan salah satu
rotasi berikut:
Single rotation
Double rotation
10SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Kondisi tidak balance
RP
Q
k1
k2
Q
k2
P
k1
R
Sebuah penambahan pada subtree:
P (outside) - case 1
Q (inside) - case 2
Sebuah penambahan pada subtree:
Q (inside) - case 3
R (outside) - case 4
HP=HQ=HR
11SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
A
k2
B
k1
CCBA
k1
k2
Single Rotation (case 1)
HA=HB+1HB=HC
12SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Single Rotation (case 4)
C
k1
B
k2
AA B C
k2
k1
HA=HBHC=HB+1
13SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Q
k2
P
k1
RR
P
Q
k1
k2
Keterbatasan Single Rotation
Single rotation tidak bisa digunakan untuk kasus 2 dan
3 (inside case)
HQ=HP+1HP=HR
14SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
C
k3
A
k1
D
B
k2
Double Rotation: Langkah
C
k3
A
k1
D
B
k2
HA=HB=HC=HD
15SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
C
k3
A
k1
DB
k2
Double Rotation: Langkah
16SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
C
k3
A
k1
D
B
k2
C
k3
A
k1
DB
k2
Double Rotation
HA=HB=HC=HD
17SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
B
k1
D
k3
A
C
k2
B
k1
D
k3
A C
k2
Double Rotation
HA=HB=HC=HD
18SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
3
Contoh
penambahan 3 pada AVL tree
11
8 20
4 16 27
8
8
11
4 20
3 16 27
19SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Contoh
penambahan 5 pada AVL tree
5
11
8 20
4 16 27 8
11
5 20
4 16 27
8
20SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
AVL Trees: Latihan
Coba simulasikan penambahan pada sebuah AVL
dengan urutan penambahan:
10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55
21SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Operasi: Remove pada AVL Tree
1. Menghapus node pada AVL Tree sama dengan menghapus binary search
tree procedure dengan perbedaan pada penanganan kondisi tidak balance.
2. Penanganan kondisi tidak balance pada operasi menghapus node AVL tree,
serupa dengan pada operasi penambahan. Mulai dari node yang diproses
(dihapus) periksa seluruh node pada jalur yang menuju root (termasuk
root) untuk menentukan node tidak balance yang pertama
3. Terapkan single atau double rotation untuk menyeimbangkan tree.
4. Bila Tree masih belum balance, ulangi lagi dari langkah 2.
22SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Menghapus node X pada AVL Trees
Deletion:
Kasus 1: jika X adalah leaf, delete X
Kasus 2: jika X punya 1 child, X digantikan oleh child tsb.
Kasus 3: jika X punya 2 child, ganti X secara rekursif dengan
predecessor-nya secara inorder
Rebalancing
23SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Delete 55 (Kasus 1)
60
20 70
10 40 65 85
5 15 30 50 80 90
55
24SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Delete 55 (Kasus 1)
60
20 70
10 40 65 85
5 15 30 50 80 90
55
25SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Delete 50 (Kasus 2)
60
20 70
10 40 65 85
5 15 30 50 80 90
55
26SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Delete 50 (Kasus 2)
60
20 70
10 40 65 85
5 15 3050 80 90
55
27SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Delete 60 (Kasus 3)
60
20 70
10 40 65 85
5 15 30 50 80 90
55
prev
28SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Delete 60 (Kasus 3)
55
20 70
10 40 65 85
5 15 30 50 80 90
29SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Delete 55 (Kasus 3)
55
20 70
10 40 65 85
5 15 30 50 80 90
prev
30SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Delete 55 (Kasus 3)
50
20 70
10 40 65 85
5 15 30 80 90
31SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Delete 50 (Kasus 3)
50
20 70
10 40 65 85
5 15 30 80 90
prev
32SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Delete 50 (Kasus 3)
40
20 70
10 30 65 85
5 15 80 90
33SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Delete 40 (Kasus 3)
40
20 70
10 30 65 85
5 15 80 90
prev
34SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Delete 40 : Rebalancing
30
20 70
10 65 85
5 15 80 90
Kasus ?
35SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Delete 40: setelah rebalancing
30
7010
20 65 855
15 80 90
Single rotation is preferred!
36SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Delete 5
30
7010
20 65 855
15 80 90
37SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Delete 5
30
7010
20 65 85
15 80 90
Double rotation is required!
38SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Delete 5 (After Rotation)
30
70
1020 65 85
15
80 90
Double rotation is required!
39SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Delete 10 & 15
30
70
1020 65 85
15
80 90
Double rotation is required!
40SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Delete 10 & 15
30
7020
65 85
80 90
Single rotation is required!
41SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Delete 10 & 15
30
70
2065
85
80 90
Single rotation is required!
42SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Delete 60
30
50
2040
70
60 80
1015
5
4590
43SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Delete 60 (Setelah rotasi pertama)
30
50
2040
80
70 90
1015
5
45
44SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Delete 60 (Setelah rotasi kedua)
30
5020
4080
70 90
1015
5 45
45SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Jumlah node minimum pada AVL Tree
Sebuah AVL Tree dengan tinggi H memiliki paling tidak sebanyak FH+3-1
nodes, dimana Fi elemen ke-i dari deret bilangan fibonacci.
S0 = 1
S1 = 2
SH = SH-1 + SH-2 + 1
SH-1
SH-2
H
H-1H-2
46SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
AVL Tree: analisa (1)
1.3282log1.44
5/
(roughly)leastathasHheightofTree
1.6182/51
5/
3
)+(N<H
φ
AVL
)+(=φ
φF
+H
i
i
47SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
AVL Tree: analisa (2)
Tinggi sebuah AVL tree merupakan fungsi logaritmik
dari jumlah seluruh node. (ukuran AVL Tree)
Oleh karena itu, seluruh operations pada AVL trees
juga logaritmik
48SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Implementasi AVL Tree
Beberapa method sama atau serupa dengan Binary
Search Tree.
Perbedaan utama terdapat pada tambahan proses
balancing dengan single dan double rotation.
Perlu tidak nya dilakukan balancing perlu diperiksa
setiap kali melakukan insert dan remove.
Kita akan pelajari lebih dalam bagaimana implementasi
method insert pada AVL Tree.
49SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Ide
Setiap kali melakukan insert, perlu mencek pada node
yang dilewati apakah node tersebut masih balance
atau tidak.
Proses insertion adalah top-down, dari root ke leaf.
Proses pengecekan balancing adalah bottom-up, dari
leaf ke root.
50SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Algoritma insertion
1. Letakkan node baru pada posisi yang sesuai
sebagaimana pada Binary Search Tree. Proses
pencarian posisi dapat dilakukan secara rekursif.
2. Ketika kembali dari pemanggilan rekursif, lakukan
pengecekan apakah tiap node yang dilewati dari leaf
hingga kembali ke root, apakah masih balance atau
tidak.
3. Bila seluruh node yang dilewati hingga kembali ke
root masih balance. Proses selesai.
51SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Algoritma insertion (lanj.)
1. Untuk setiap node yang tidak balance lakukan
balancing.
a. Bila insertion terjadi pada “outside” lakukan single
rotation
b. Bila insertion terjadi pada “inside” lakukan double
rotation.
2. Lakukan pengecekan dan balancing hingga root.
52SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Diskusi?
Bagaimana menentukan insertion terjadi pada bagian
“inside” atau “outside” ?
53SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Pseudo code
public static <A extends Comparable<A>> AvlNode<A>
insert( A x, AvlNode<A> t ){
if( t == null )
t = new AvlNode<A>( x, null, null );
else if( x.compareTo( t.element ) < 0 )
{
t.left = insert( x, t.left );
if(Math.abs(height( t.left ) - height( t.right )) == 2 )
if( x.compareTo( t.left.element ) < 0 )
t = rotateWithLeftChild( t );
else
t = doubleWithLeftChild( t );
}
else if( x.compareTo( t.element ) > 0 )
{
// simetris dengan program diatas
}
return t;
}
54SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Diskusi
Apakah implementasi tersebut sudah efisien?
Perhatikan pemanggilan method height !
55SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Pseudo code
public static <A extends Comparable<A>> AvlNode<A>
insert( A x, AvlNode<A> t ){
if( t == null )
t = new AvlNode<A>( x, null, null );
else if( x.compareTo( t.element ) < 0 )
{
t.left = insert( x, t.left );
if(Math.abs(height( t.left ) - height( t.right )) == 2 )
if( x.compareTo( t.left.element ) < 0 )
t = rotateWithLeftChild( t );
else
t = doubleWithLeftChild( t );
}
else if( x.compareTo( t.element ) > 0 )
{
// simetris dengan program diatas
}
return t;
}
56SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Pseudo code AvlNode
class AvlNode<A extends Comparable<A>> extends BinaryNode<A>{
// Constructors
AvlNode( A theElement ) {
this( theElement, null, null );
}
AvlNode( A theElement, AvlNode<A> lt, AvlNode<A> rt ) {
element = theElement;
left = lt;
right = rt;
height = 0;
}
public int height(){
return t.height;
}
// Friendly data; accessible by other package routines
A element; // The data in the node
AvlNode<A> left; // Left child
AvlNode<A> right; // Right child
int height; // Height
}
57SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Pseudo code
public static <A extends Comparable<A>> AvlNode<A>
insert( A x, AvlNode<A> t ){
if( t == null )
t = new AvlNode<A>( x, null, null );
else if( x.compareTo( t.element ) < 0 )
{
t.left = insert( x, t.left );
if(Math.abs(height( t.left ) - height( t.right )) == 2 )
if( x.compareTo( t.left.element ) < 0 )
t = singleRotateWithLeftChild( t );
else
t = doubleRotateWithLeftChild( t );
}
else if( x.compareTo( t.element ) > 0 )
{
// simetris dengan program diatas
}
t.height = max( height( t.left ), height( t.right ) ) + 1;
return t;
}
58SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Diskusi
Apakah ada cara lain?
Hanya menyimpan nilai perbandingan saja.
• Nilai -1, menyatakan sub tree kiri lebih tinggi 1 dari sub tree kanan.
• Nilai +1, menyatakan sub tree kanan lebih tinggi 1 dari sub tree kiri
• Nilai 0, menyatakan tinggi sub tree kiri = tinggi sub tree kanan
Kapan dilakukan rotasi?
• Bila harus diletakkan ke kiri dan node tersebut sudah bernilai -1
maka dinyatakan tidak balance
• Berlaku simetris
• Lihat contoh animasi
59SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Pseudocode: Single rotasi
static <A extends Comparable<A>> AvlNode<A>
singleRotateWithLeftChild( AvlNode<A> k2 )
{
AvlNode<A> k1 = k2.left;
k2.left = k1.right;
k1.right = k2;
// update tinggi kedua node.
return k1;
}
60SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Pseudocode: Double rotasi
static <A extends Comparable<A>> AvlNode<A>
doubleRotateWithLeftChild( AvlNode<A> k3 )
{
k3.left = singleRotateWithRightChild( k3.left );
return singleRotateWithLeftChild( k3 );
}
61SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
AVL Trees: Latihan
Coba simulasikan urutan proses pada sebuah AVL Tree
berikut ini
insert 10, 85, 15, 70, 20, 60, 30,
delete 15, 10,
insert 50, 65, 80,
delete 20, 60,
insert 90, 40, 5, 55
delete 70
Gambarkan kondisi akhir dari AVL Tree tersebut.
62SUR – HMM – AA Fasilkom UI - IKI20100/ IKI80110P Semester Ganjil – 2009/2010
Rangkuman
Mencari elemen, menambahkan, dan menghapus,
seluruhnya memiliki kompleksitas running time O(log
n) pada kondisi worst case
Insert operation: top-down insertion dan bottom up
balancing