Professional Documents
Culture Documents
AVL Tree
Outline
AVL Tree
Definisi Sifat Operasi
AVL Tree
Binary Search Trees yang tidak imbang memiliki efisiensi yang buruk. Worst case: O(n). AVL (Adelson-Velskii & Landis) tree adalah BST yang imbang. Setiap node di AVL tree memiliki balance factor bernilai -1, 0, atau 1.
X X
H H-1
H-2
AVL Tree
10 5 3 1 2 1
27th Mar 2007
10 20 3 5 20 43
AVL Tree
12 8 4 10 14 16
2 1
27th Mar 2007
10
HL =3
HR =1
HL =1
HR =3
11
(continued)
HL =3
HR =1
HL =1
HR =3
12
Case 1:
Left of Left
13
Case 2:
Right of Right
14
Case 3:
Right of Left
15
Case 4:
Left of Right
16
Contoh
Sisipkan 3 ke AVL tree
11 8 4 16 20 27 3 4 8 16 11 20 27
Contoh
Penyisipan 5 ke AVL tree
11 8 4 16 20 27 4 5 8 16 11 20 27
Latihan
Sisipkan data berikut ke AVL tree secara berurutan:
10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55
Tahap menyeimbangkan node yang balance factornya tidak -1, 0, 1, dilakukan dari node yang dihapus menuju root.
27th Mar 2007
22
23
24
25
26
27
28
29
30
Delete 55 (case 1)
60
20 70
10
5 15 30
40
50 55
65
80
85
90
Delete 55 (case 1)
60
20 70
10
5 15 30
40
50 55
65
80
85
90
Delete 50 (case 2)
60
20 70
10
5 15 30
40
50 55
65
80
85
90
Delete 50 (case 2)
60
20 70
10
5 15 30
40
50
65
80
85
90
55
Delete 60 (case 3)
60
20 70
10
5 15 30
40
50
65
prev 55 80
85
90
Delete 60 (case 3)
55
20 70
10
5 15 30
40
50
65
80
85
90
Delete 55 (case 3)
55
20 prev 70
10
5 15 30
40
50
65
80
85
90
Delete 55 (case 3)
50
20 70
10
5 15 30
40
65
80
85
90
Delete 50 (case 3)
50
20 prev 70
10
5 15 30
40
65
80
85
90
Delete 50 (case 3)
40
20 70
10
5 15
30
65
80
85
90
Delete 40 (case 3)
40
20 prev 70
10
5 15
30
65
80
85
90
Delete 40 : Rebalancing
30
20 70
10
5
Case ? 15
65
80
85
90
30
10 70
5
15
20
65
80
85
90