Binary Search Tree

Download Report

Transcript Binary Search Tree

Binary Search Tree
Oleh : Nur Hayatin, S.ST
Teknik Informatika - Universitas Muhammadiyah Malang (UMM)
Tahun Akademik 2010-2011
Sub Topik
•
•
•
•
Heap Tree
Binary Search Tree
AVL Tree
Red-Black Tree
Heap Tree
Heap Tree
• Definisi :
– Heap tree adalah pohon biner yang tiap node nya
memenuhi heap property.
• Heap Property :
– Kondisi 1:
Nilai pada node induk harus lebih besar ( > ) atau lebih
besar sama dengan ( >= ) dari node anak.
– Kondisi 2:
Nilai pada node induk harus lebih besar ( < ) atau lebih
besar sama dengan ( <= ) dari node anak.
4
Contoh Heap Tree
12
8
12
3
Node 12 memiliki
properti heap
8
12
12
Node 12 memiliki
properti heap
8
14
Node 12 tidak memiliki
properti heap
5
Sift Up
• Penukaran posisi node induk terhadap node anak yang nilainya
paling besar sehingga binary tree memenuhi heap property.
12
8
14
14
Tree tidak memenuhi
heap property
8
12
Tree memenuhi
heap property
• Proses ini disebut juga sifting up
6
Sift Up
• Proses sift up ini dilakukan berulang kali hingga :
– Node berada di posisi yang tepat dalam arti nilai node tsb masih
lebih kecil daripada node induknya, atau
– Prosesnya telah sampai pada node root
Jenis Heap Tree
• Berdasarkan Heap Property :
– Max heap tree
– Min heap tree
Max Heap Tree
• Nilai node induk (root) lebih besar atau sama
dengan nilai subtree. (heap property kondisi 1)
Contoh max heap tree
9
8
6
5
7
7
1
2
6
Min Heap Tree
• Nilai dari node induk (root) lebih kecil atau sama
dengan nilai dari subtree. (heap property kondisi
2)
Contoh min heap tree
2
4
6
8
3
7
6
9
3
Representasi Heap Tree
• Representasi Heap Tree lebih efisien menggunakan
Array.
Contoh representasi heap tree
9
8
7
6
5
7
2
1
0
9
8
7
6
7
2
6
5
1
1
2
3
4
5
6
7
8
9
10
6
Aturan Penambahan Node
• Penambahan node baru di posisi paling kiri pada
level terbawah.
• Jika level terbawah penuh, tambahkan node pada
level baru.
• Harus mengikuti aturan heap property. Jika tidak
lakukan sift up.
Contoh Penambahan Node
Tambahkan
node baru disini
Tambahkan
node baru disini
Contoh 1 : penambahan node
dari cabang kiri ke kanan
Contoh 2 : Penambahan node
pada level baru
16
Penambahan Node Pada Max Heap Tree
1
9
2
3
8
7
4
5
6
7
5
1
8
9
7
6
2
6
Penambahan Node Baru
9
8
7
6
5
7
1
2
7
Complete binary tree with 10 nodes.
6
Penambahan Node(5)
9
8
7
6
5
7
1
5
7
New element is 5.
2
6
Penambahan Node(20)
9
8
7
6
5
7
1
7
New element is 20.
2
6
Sifting Up(7)
9
8
7
6
5
2
1
77
New element is 20.
6
Sifting Up(8)
9
7
6
5
8
1
77
New element is 20.
2
6
Hasil Penambahan Node(20)
20
9
7
6
5
8
1
77
New element is 20.
2
6
Penambahan Node(15) ????
20
9
7
6
5
8
1
77
New element is 15.
2
6
Sifting Up(8)
20
9
7
6
5
2
1
77
8
New element is 15.
6
Sifting Up(9)
20
7
6
5
9
1
77
2
8
New element is 15.
6
Hasil Penambahan Node(15)
20
7
15
6
5
9
1
77
2
8
New element is 15.
6
Penghapusan Max Node
20
7
15
6
5
9
1
77
2
8
Max element is in the root.
6
Removed Root
7
15
6
5
9
1
77
2
8
After max element is removed.
6
Sifting Up(15)
15
7
6
5
9
1
77
2
8
Reinsert 15 into the heap.
6
Sifting Up(9)
15
9
7
6
5
2
1
77
8
Reinsert 9 into the heap.
6
Sifting Up(8)
15
9
7
6
5
8
1
77
Reinsert 8 into the heap
Max element is 15.
2
6
Penghapusan Max Node
9
7
6
5
8
1
2
77
After max element is removed.
6
Sifting Up(9)
9
7
6
5
8
1
7
2
6
Sifting Up(8)
9
7
8
6
5
8
1
7
2
6
Sifting Up(7)
9
8
6
5
7
7
1
2
6
Hasil Remove Max Element (15)
9
8
7
6
5
7
1
7
2
6
Latihan
1
3
2
4
8
5
9
7
10
6
7
8
11
input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Max Heap Tree
1
3
2
4
8
5
9
7
10
6
8
11
Start at rightmost array position that has a child.
7
Sifting up(11)
1
3
2
4
8
11
9
7
10
6
8
5
Move to next lower array position.
7
Initializing A Max Heap
1
3
2
4
8
11
9
7
10
6
8
5
7
Initializing A Max Heap
1
3
2
9
8
11
4
7
10
6
8
5
7
Initializing A Max Heap
1
3
2
9
8
11
4
7
10
6
8
5
7
Initializing A Max Heap
1
7
2
9
8
11
4
7
10
6
8
5
3
Initializing A Max Heap
1
7
2
9
8
11
4
7
10
6
8
5
3
Initializing A Max Heap
1
7
11
9
8
6
4
7
10
8
5
Find a home for 2.
3
Initializing A Max Heap
1
7
11
9
8
10
4
8
6
75
Find a home for 2.
3
Initializing A Max Heap
1
7
11
9
8
10
4
72
6
3
8
5
Done, move to next lower array position.
Initializing A Max Heap
1
7
11
9
8
10
4
72
6
8
5
Find home for 1.
3
Initializing A Max Heap
11
7
9
8
10
4
72
6
8
5
Find home for 1.
3
Initializing A Max Heap
11
10
7
9
8
6
4
72
8
5
Find home for 1.
3
Initializing A Max Heap
11
10
7
9
8
5
4
72
6
8
Find home for 1.
3
Hasil Akhir
11
10
7
9
8
5
4
Done.
72
6
8
1
3
Binary Search Tree
(BST)
Definisi
• Sebuah binary tree dimana subtree sebelah
kiri lebih kecil dari subtree sebelah kanan.
Contoh BST
20
10
6
2
40
15
8
30
25
Binary Search Tree
• Operasi BST : penambahan, penghapusan, pencarian
node tertentu, pencarian niai terkecil dan pencarian
nilai terbesar.
• Properti Binary Search Tree :
– Untuk setiap node X, semua elemen di subpohon kirinya
bernilai lebih kecil dari nilai X dan semua elemen di
subpohon kanannya bernilai lebih besar dari nilai X.
Contoh
Binary Search Tree
Bukan Binary Search Tree
Insert
• Dimulai dengan penelusuran
dari root untuk mencari posisi
yang tepat.
• Jika elemen X ditemukan
(berarti X sudah ada di BST),
maka tidak perlu melakukan
aksi apapun.
• Jika tidak, maka letakkan X
sebagai node terakhir pada jalur
penelusuran
Delete
• Saat akan menghapus sebuah node, kita juga harus
memikirkan seluruh node anak dari node tsb.
– Hal penting adalah agar pohon setelah dihapus tetap
merupakan BST.
Operasi Penghapusan / remove()
Ada 3 kasus :
 Elemen ada di leaf/daun.
 Elemen yang memiliki degree 1.
 Elemen yang memiliki degree 2.
Penghapusan Node Daun (Node 7)
20
10
6
2
40
15
8
30
18
25
35
7
Remove a leaf element. key = 7
Penhapusan Node Daun (Node 35)
20
10
6
2
40
15
8
30
18
25
35
7
Remove a leaf element. key = 35
Penghapusan Node Ber-degree 1
20
10
6
2
40
15
8
30
18
25
35
7
Remove from a degree 1 node. key = 40
Penghapusan Node Ber-degree 1
20
10
6
2
40
15
8
30
18
25
35
7
Remove from a degree 1 node. key = 15
Penghapusan Node Ber-degree 2
20
10
6
2
40
15
8
30
18
25
35
7
Remove from a degree 2 node. key = 10
Remove From A Degree 2 Node
20
10
6
2
40
15
8
30
18
25
35
7
Replace with largest key in left subtree (or
smallest in right subtree).
Penghapusan Node Ber-degree 2
20
10
6
2
40
15
8
30
18
25
35
7
Replace with largest key in left subtree (or
smallest in right subtree).
Penghapusan Node Ber-degree 2
20
8
6
2
40
15
8
30
18
25
35
7
Replace with largest key in left subtree (or
smallest in right subtree).
Latihan
20
10
6
2
40
15
8
30
18
25
35
7
Remove from a degree 2 node. key = 20
Penghapusan Node Ber-degree 2
20
10
6
2
40
15
8
30
18
25
35
7
Replace with largest in left subtree.
Penghapusan Node Ber-degree 2
20
10
6
2
40
15
8
30
18
25
35
7
Replace with largest in left subtree.
Penghapusan Node Ber-degree 2
18
10
6
2
40
15
8
30
18
25
35
7
Replace with largest in left subtree.
Hasil Akhir
18
10
6
2
15
8
7
40
30
25
35
Pencarian pada BST
• Jika mencari elemen bernilai
15, maka akan langsung
ditemukan.
• Jika mencari elemen bernilai
< 15, maka kita cari di
subpohon kiri.
• Jika mencari elemen bernilai
> 15, maka kita cari di
subpohon kanan.
findMin/ findMax
• findMin : mengembalikan node dengan elemen terkecil
pada BST
• Pencarian dimulai dari root dan bergerak ke kiri terus
sepanjang subpohon kiri dan berhenti pada elemen terakhir.
• Proses serupa terhadap metode findMax
Red-Black Tree
Red-Black Tree
• Red-black tree adalah binary search tree yang
memenuhi properti sbb :
– Setiap node adalah red atau black
– Node root adalah black
– Setiap daun adalah black
– Semua anak dari node red adalah black
– Setiap jalur (path) dari sebuah node ke daun berisi node
black yang jumlahnya sama
Struktur Data - Tree
79
Contoh Red-Black Tree
Struktur Data - Tree
80
AVL Tree
AVL Tree
• AVL Properti :
– Sebuah node memiliki AVL property jika height (tinggi)
subpohon kiri & subpohon kanan node tsb sama atau
berbeda 1.
– Height (tinggi) pohon adalah jarak dari root menuju daun
terbawah yang dimiliki pohon tsb
• AVL Tree adalah pohon yang setiap node nya
memiliki AVL property
Pustaka
• Sartaj Sahni , “Data Structures & Algorithms”, Presentation
L1.
• Mitchell Waite, “Data Structures & Algorithms in Java”,
SAMS, 2001