avl tree fix
TRANSCRIPT
-
7/23/2019 Avl Tree Fix
1/40
Algoritma dan Struktur Data
AVL TREE
Review BST
-
7/23/2019 Avl Tree Fix
2/40
Pemakaian tree structure dalam proses pencarian (search)
Sifat Binary Tree:
Pada sebuah node x,
elemen yang berada di LEFT sub-tree selalu lebih KECIL
daripada x
elemen yang berada di RIGHT sub-tree selalu lebih BESAR
Atau SAMA DENGAN daripada x
Binary Search Tree: proses pencarian (SEARCHING) berbasis
binary tree
Apakah Binary
Search Tree itu ?
-
7/23/2019 Avl Tree Fix
3/40
Example of a binary search tree
-
7/23/2019 Avl Tree Fix
4/40
LATIHAN BST
Coba simulasikan penambahan pada
sebuah BST dengan urutanpenambahan:
14, 16, 10, 8, 12, 4, 9, 1
8, 12, 4, 9, 1, 14, 16, 10
-
7/23/2019 Avl Tree Fix
5/40
Proses Pencarian (contoh SUKSES)
2
6
13
15
215
7
search(7) 75
7==7
Data yang dicari BERHASILditemukan
135 7
-
7/23/2019 Avl Tree Fix
6/40
14
10 16
8 12
4 9
1
LATIHAN BST
9
8
4
12
1 14
1610
-
7/23/2019 Avl Tree Fix
7/40
Algoritma dan Struktur Data
AVL TREE
-
7/23/2019 Avl Tree Fix
8/40
Tujuan
Memahami variant dari Binary Search Tree yang
balanced
Binary Search Tree yang tidak balance dapat
membuat seluruh operasi memiliki kompleksitas
running time O(n) pada kondisi worst case.
-
7/23/2019 Avl Tree Fix
9/40
X X
AVL Trees
Untuk setiap node dalam tree, ketinggian
subtree di anak kiri dan subtree di anakkanan hanya berbedamaksimum1.
H
H-1
H-2
-
7/23/2019 Avl Tree Fix
10/40
AVL Trees
10
5
3
20
2
1 3
10
5
3
20
1
43
5
-
7/23/2019 Avl Tree Fix
11/40
AVL Trees
12
8 16
4 10
2 6
14
-
7/23/2019 Avl Tree Fix
12/40
12
8 16
4 10
2 6
14
1
Insertion pada AVL Tree
Setelah insert1
-
7/23/2019 Avl Tree Fix
13/40
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 rotasiberikut:
Single rotation
Double rotation
-
7/23/2019 Avl Tree Fix
14/40
Kondisi tidakbalance
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
-
7/23/2019 Avl Tree Fix
15/40
A
k2
B
k1
CCBA
k1
k2
Single Rotation (case 1)
HA=HB+1
HB=HC
-
7/23/2019 Avl Tree Fix
16/40
Single Rotation (case 4)
C
k1
B
k2
A
A B C
k2
k1
HA=HBHC=HB+1
-
7/23/2019 Avl Tree Fix
17/40
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+1
HP=HR
-
7/23/2019 Avl Tree Fix
18/40
C
k3
A
k1
D
B
k2
Double Rotation: Langkah
C
k3
A
k1
D
B
k2
HA=HB=HC=HD
-
7/23/2019 Avl Tree Fix
19/40
C
k3
A
k1
DB
k2
Double Rotation: Langkah
-
7/23/2019 Avl Tree Fix
20/40
C
k3
A
k1
D
B
k2
C
k3
A
k1
DB
k2
Double Rotation
HA=HB=HC=HD
-
7/23/2019 Avl Tree Fix
21/40
B
k1
D
k3
A
C
k2
B
k1
D
k3
A C
k2
Double Rotation
HA=HB=HC=HD
-
7/23/2019 Avl Tree Fix
22/40
3
Contoh
penambahan 3 pada AVL tree
11
8 20
4 16 27
8
8
11
4 20
3 16 27
-
7/23/2019 Avl Tree Fix
23/40
Contoh
penambahan 5 pada AVL tree
5
11
8 20
4 16 27
8
4
11
8 20
5 16 27
8
-
7/23/2019 Avl Tree Fix
24/40
Contoh
Rotasi ke 2
8
11
5 20
4 16 27
-
7/23/2019 Avl Tree Fix
25/40
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
http://www.strille.net/works/media_technology_projects/av
l-tree_2001/
http://www.site.uottawa.ca/~stan/csi2514/applets/avl/BT.ht
ml
http://www.strille.net/works/media_technology_projects/avl-tree_2001/http://www.strille.net/works/media_technology_projects/avl-tree_2001/http://www.site.uottawa.ca/~stan/csi2514/applets/avl/BT.htmlhttp://www.site.uottawa.ca/~stan/csi2514/applets/avl/BT.htmlhttp://www.site.uottawa.ca/~stan/csi2514/applets/avl/BT.htmlhttp://www.site.uottawa.ca/~stan/csi2514/applets/avl/BT.htmlhttp://www.strille.net/works/media_technology_projects/avl-tree_2001/http://www.strille.net/works/media_technology_projects/avl-tree_2001/http://www.strille.net/works/media_technology_projects/avl-tree_2001/http://www.strille.net/works/media_technology_projects/avl-tree_2001/ -
7/23/2019 Avl Tree Fix
26/40
Operasi: Remove pada AVL Tree
1. Menghapus node pada AVL Tree sama dengan
menghapus binary search tree procedure denganperbedaan 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 s ing leatau doub le rotat ionuntuk
menyeimbangkan tree.
4. Bila Tree masih belum balance, ulangi lagi dari langkah
2.
-
7/23/2019 Avl Tree Fix
27/40
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 Xsecara rekursif dengan predecessor-nya
secara inorder
Rebalancing
l ( )
-
7/23/2019 Avl Tree Fix
28/40
Delete 55 (Kasus 1)
60
20 70
10 40 65 85
5 15 30 50 80 90
55
D l 55 (K 1)
-
7/23/2019 Avl Tree Fix
29/40
Delete 55 (Kasus 1)
60
20 70
10 40 65 85
5 15 30 50 80 90
55
D l 50 (K 2)
-
7/23/2019 Avl Tree Fix
30/40
Delete 50 (Kasus 2)
60
20 70
10 40 65 85
5 15 30 50 80 90
55
D l 50 (K 2)
-
7/23/2019 Avl Tree Fix
31/40
Delete 50 (Kasus 2)
60
20 70
10 40 65 85
5 15 3050 80 90
55
D l 60 (K 3)
-
7/23/2019 Avl Tree Fix
32/40
Delete 60 (Kasus 3)
60
20 70
10 40 65 85
5 15 30 50 80 90
55
prev
D l t 60 (K 3)
-
7/23/2019 Avl Tree Fix
33/40
Delete 60 (Kasus 3)
55
20 70
10 40 65 85
5 15 30 50 80 90
D l t 55 (K 3)
-
7/23/2019 Avl Tree Fix
34/40
Delete 55 (Kasus 3)
55
20 70
10 40 65 85
5 15 30 50 80 90
prev
D l t 55 (K 3)
-
7/23/2019 Avl Tree Fix
35/40
Delete 55 (Kasus 3)
50
20 70
10 40 65 85
5 15 30 80 90
D l t 50 (K 3)
-
7/23/2019 Avl Tree Fix
36/40
Delete 50 (Kasus 3)
50
20 70
10 40 65 85
5 15 30 80 90
prev
D l t 50 (K 3)
-
7/23/2019 Avl Tree Fix
37/40
Delete 50 (Kasus 3)
40
20 70
10 30 65 85
5 15 80 90
D l t 40 (K 3)
-
7/23/2019 Avl Tree Fix
38/40
Delete 40 (Kasus 3)
40
20 70
10 30 65 85
5 15 80 90
prev
D l t 40 R b l i
-
7/23/2019 Avl Tree Fix
39/40
Delete 40 : Rebalancing
30
20 70
10 65 85
5 15 80 90
Kasus ?
Delete 40: setelah b l i
-
7/23/2019 Avl Tree Fix
40/40
Delete 40: setelah rebalancing
30
7010
20 65 855
15 80 90
Single rotation is preferred!