SlideShare a Scribd company logo
1 of 98
SEARCHING (2)
Sesi 13
Outline
Metode Pohon Biner
Metode Hash
2
3
Binary Trees
• A binary tree is a tree in which no node can
have more than two subtrees; the maximum
outdegree for a node is two.
• In other words, a node can have zero, one, or
two subtrees.
• These subtrees are designated as the left
subtree and the right subtree.
6
7
A null
tree is a
tree with
no nodes
8
Some Properties of Binary Trees
• The height of binary trees can be
mathematically predicted
• Given that we need to store N nodes in a
binary tree, the maximum height is
maxH N
A tree with a maximum height is rare. It occurs when all of
the nodes in the entire tree have only one successor.
9
Some Properties of Binary Trees
• The minimum height of a binary tree is
determined as follows:
 min 2log 1H N 
For instance, if there are three nodes to be stored in the
binary tree (N=3) then Hmin=2.
10
Some Properties of Binary Trees
• Given a height of the binary tree, H, the
minimum number of nodes in the tree is given
as follows:
minN H
11
Some Properties of Binary Trees
• The formula for the maximum number of
nodes is derived from the fact that each node
can have only two descendents. Given a
height of the binary tree, H, the maximum
number of nodes in the tree is given as
follows:
max 2 1H
N  
12
Complete and nearly
complete binary trees
• A complete tree has the maximum number of
entries for its height. The maximum number is
reached when the last level is full.
• A tree is considered nearly complete if it has the
minimum height for its nodes and all nodes in
the last level are found on the left
13
14
Binary Tree Traversal
• A binary tree traversal requires that each node
of the tree be processed once and only once in
a predetermined sequence.
• In the depth-first traversal processing process
along a path from the root through one child to
the most distant descendant of that first child
before processing a second child.
Tree Traversal
• Is a procedure that systematically visits
every vertex of an ordered rooted tree
• Three most commonly used algorithms
– Preorder traversal.
– Inorder traversal.
– Postorder traversal.
18
19
20
21
22
23
24
Data Structures: A Pseudocode Approach
with C
25
26
27
28
29
30
31
32
33
34
Metode Pohon Biner
• Disebut dengan Binary Search Tree (BST)
• Binary search tree (BST) merupakan binary tree
dengan sifat berikut:
 Semua item pada left subtree bernilai kurang dari
root.
 Semua item pada right subtree bernilai lebih atau
sama dengan root.
 Setiap subtree merupakan BST.
35
BST
36
BST: Contoh
37
56
26 200
18 28 190 213
12 24 27
Binary Search Trees
A binary search tree Not a binary search tree
Binary Search Trees
• Average depth of a node is O(log N)
• Maximum depth of a node is O(N)
The same set of keys may have different BSTs
BST Valid
40
(a), (b) - complete and balanced trees;
(d) – nearly complete and balanced tree;
(c), (e) – neither complete nor balanced trees
BST Invalid
41
BST: Operasi
• Traversals
• Searches
• Insertion
• Deletion
42
BST: Contoh
43
BST: Traversals
 Preorder traversal
23 18 12 20 44 35 52
 Postorder traversal
12 20 18 35 52 44 23
 Inorder traversal
12 18 20 23 35 44 52
44
Inorder traversal pada BST menghasilkan
nilai yang terurut dari kecil  besar
45
Right-Node-Left Traversal
52 44 35 23 20 18 12
Right-node-left traversal of a binary search tree produces a
descending sequence
Searching BST
• If we are searching for 15, then we are done.
• If we are searching for a key < 15, then we
should search in the left subtree.
• If we are searching for a key > 15, then we
should search in the right subtree.
Time complexity = O(height of the tree)
BST: Search
 Beberapa algoritma search:
 Mencari node dengan nilai terkecil
 Mencari node dengan nilai terbesar
 Mencari node dengan nilai tertentu (BST search)
48
BST: Search
 Dari BST sudah langsung diketahui bahwa:
» Nilai minimum terletak di node sebelah kiri.
» Nilai maksimum terletak di node sebelah kanan.
49
Tree-Minimum(x) Tree-Maximum(x)
1. while left[x]  NIL 1. while right[x]  NIL
2. do x  left[x] 2. do x  right[x]
3. return x 3. return x
BST: Search  mencari node terkecil
50
51
BST: Search  mencari node terbesar
52
right subtree
not empty
right subtree
not empty
right subtree
empty return
53
54
Mencari node dengan nilai
tertentu (BST search)
55
56
57
BST Insertion
• To insert data all we need to do is follow the
branches to an empty subtree and then insert
the new node.
• In other words, all inserts take place at a leaf or
at a leaflike node – a node that has only one null
subtree.
Insertion
• Proceed down the tree as you would with a find
• If X is found, do nothing (or update something)
• Otherwise, insert X at the last spot on the path
traversed
• Time complexity = O(height of the tree)
BST: Insertion
• Sisipkan 4
• Kunci penyisipan akan ditambahkan di left node.
59
6
2
5
8
7 9
4
6
2
5
8
7 9
60
61
62
63
30
30 30
30
64
Deletion
• There are the following possible cases when we delete a
node:
• The node to be deleted has no children. In this case, all
we need to do is delete the node.
• The node to be deleted has only a right subtree. We
delete the node and attach the right subtree to the
deleted node’s parent.
• The node to be deleted has only a left subtree. We
delete the node and attach the left subtree to the deleted
node’s parent.
• The node to be deleted has two subtrees. It is possible
to delete a node from the middle of a tree, but the result
tends to create very unbalanced trees.
Deletion
• When we delete a node, we need to consider
how we take care of the children of the deleted
node.
– This has to be done such that the property of the
search tree is maintained.
Deletion under Different Cases
• Case 1: the node is a leaf
– Delete it immediately
• Case 2: the node has one child
– Adjust a pointer from the parent to bypass that node
Deletion Case 3
• Case 3: the node has 2 children
– Replace the key of that node with the minimum element at
the right subtree
– Delete that minimum element
• Has either no child or only right child because if it has a left child,
that left child would be smaller and would have been chosen. So
invoke case 1 or 2.
• Time complexity = O(height of the tree)
BST: Deletion
 Untuk menghapus sebuah node dari BST,
pertama lakukan pencarian (search) untuk
mencari node yang akan dihapus.
 Terdapat empat kasus pada penghapusan
sebuah node di BST. Node yang dihapus :
 Tidak memiliki child
 Hanya punya right subtree.
 Hanya punya left subtree
 Punya dua subtree
68
BST: Deletion
1. Node tidak memiliki child
 Hapus node
2. Node hanya memiliki right subtree.
 Hapus node
 Sambungkan right subtree ke parent node yang
akan dihapus.
3. Node hanya memiliki left subtree.
 Hapus node
 Sambungkan left subtree ke parent node yang akan
dihapus.
69
BST: Deletion
4. Node memiliki dua subtree.
 Temukan node dengan nilai terbesar pada left
subtree node yang dihapus kemudian pindahkan
node tersebut untuk menggantikan node yang
dihapus atau
 Temukan node dengan nilai terkecil pada right
subtree node yang dihapus kemudian pindahkan
node tersebut untuk menggantikan node yang
dihapus.
70
71
Deletion from the middle of a
tree
• Rather than simply delete the node, we try to
maintain the existing structure as much as
possible by finding data to take the place of the
deleted data. This can be done in one of two
ways.
72
Deletion from the middle of a
tree
• We can find the largest node in the deleted
node’s left subtree and move its data to replace
the deleted node’s data.
• We can find the smallest node on the deleted
node’s right subtree and move its data to replace
the deleted node’s data.
• Either of these moves preserves the integrity of
the binary search tree.
73
74
(continued)
75
27 27
27 27
76
Searching
• Consider the problem of searching an array for a
given value
– If the array is not sorted, the search requires O(n)
time
• If the value isn’t there, we need to search all n elements
• If the value is there, we search n/2 elements on average
– If the array is sorted, we can do a binary search
• A binary search requires O(log n) time
• About equally fast whether the element is found or not
– It doesn’t seem like we could do much better
• How about an O(1), that is, constant time search?
• We can do it if the array is organized in a particular way
Metode Hash
• Hash adalah algoritma yang memetakan
kumpulan data besar dengan variabel panjang,
disebut kunci, ke kumpulan data yang lebih kecil
dan mempunyai ukuran tetap.
• Informasi dapat diakses pada waktu yang
konstan
77
Concept of hashing
• The problem at hand is to define and
implement a mapping from a domain of keys
to a domain of locations
• From the performance standpoint, the goal is
to avoid collisions (A collision occurs when
two or more keys map to the same location)
• From the compactness standpoint, no
application ever stores all keys in a domain
simultaneously unless the size of the domain
is small
Concept of hashing (con’t)
• The information to be retrieved is stored in a
hash table which is best thought of as an
array of m locations, called buckets
• The mapping between a key and a bucket is
called the hash function
• The time to store and retrieve data is
proportional to the time to compute the hash
function
Tabel Hash
• Hashing digunakan untuk menyimpan jumlah
data yang relatif besar ke tabel yang disebut
tabel hash ADT.
• Tabel hash biasanya mempunyai ukuran H yang
tetap, yang lebih besar dari jumlah data yang
ingin disimpan
• load factor (): rasio data dengan ukuran tabel
hash.
• Fungsi hash memetakan item ke indeks dalam
rentang 0 sampai H-ukuran-1
80
Tabel Hash
81
0
1
2
3
H-1
key
hash
function
item
hash table
Tabel Hash
• Operasi pada tabel hash
– Search: komputasi f(k) dan lihat apakah
pasangan ada
– Insert: komputasi f(k) dan tempatkan dalam
posisi
– Delete: komputasi f(k) dan hapus pasangan
dalam posisi
82
Fungsi Hash
• Fungsi hash harus memenuhi fitur berikut ini:
– Mudah untuk dikomputasi.
– Dua kunci yang berbeda dipetakan ke sel array yang berbeda.
– Dapat dilakukan menggunakan tabel pengalamatan langsung
dimana kumpulan umum kunci yang cukup kecil.
– Distribusikan kunci dengan merata ke semua sel.
• Sebuah fungsi hash digunakan dengan fungsi mod
bilangan prima.
• Semua manipulasi pada digit, dengan kompleksitas
minimum dan distribusi yang baik dapat digunakan.
83
Minimal Perfect Hash Functions
• Minimal perfect hash function (MPHF) adalah
fungsi hash sempurna dengan properti yang
memecah beberapa kunci m ke beberapa
wadah m tanpa adanya benturan antar data
• Cichelli(1980) and of Cercone et al.(1983)
mengusulkan dua konsep penting:
(1) menggunakan tables of values sebagai
paramater
(2) menggunakan pendekatan mapping,
ordering, dan searching (MOS)
84
Minimal Perfect Hash Functions
• Mapping: mentransformasikan kumpulan kunci
dari asal ke tempat baru
• Ordering: menempatkan kunci-kunci dalam
urutan yang menunjukkan nilai hash terhadap
kunci
• Searching: menentukan nilai hash pada kunci
setiap level
85
Mapping → Ordering → Searching
Contoh Ideal Fungsi Hash
• Misalkan fungsi hash dengan
nilai-nilai berikut:
hashCode("apple") = 5
hashCode("watermelon") = 3
hashCode("grapes") = 8
hashCode("cantaloupe") = 7
hashCode("kiwi") = 0
hashCode("strawberry") = 9
hashCode("mango") = 6
hashCode("banana") = 2
86
kiwi
banana
watermelon
apple
mango
cantaloupe
grapes
strawberry
0
1
2
3
4
5
6
7
8
9
Contoh (non) Ideal Fungsi Hash
• Misalkan fungsi hash dengan nilai-
nilai berikut:
hashCode("apple") = 5
hashCode("watermelon") = 3
hashCode("grapes") = 8
hashCode("cantaloupe") = 7
hashCode("kiwi") = 0
hashCode("strawberry") = 9
hashCode("mango") = 6
hashCode("banana") = 2
hashCode(“honeydew”) = 6
Apa yang terjadi? 87
kiwi
banana
watermelon
apple
mango
cantaloupe
grapes
strawberry
0
1
2
3
4
5
6
7
8
9
Collision
• Saat terdapat dua nilai yang terhubung ke lokasi
array yang sama, maka disebut collision
• Biasanya collision akan dilayani secara “first
come, first served”—nilai pertama yang muncul
yang akan mendapatkan lokasi
• Teknik khusus akan dilakukan untuk menangani
nilai kedua
88
Penanganan Collision
• Ada 3 cara yang bisa dilakukan:
– Solusi #1: mencari lokasi kosong yang lain
• Pencarian bisa dihentikan saat menemukan nilai
atau lokasi kosong
• Pencarian harus berakhir
– Solusi #2: menggunakan fungsi hash kedua
• ..dan ketiga, dan keempat, dan kelima,….dst
– Solusi #3: menggunakan lokasi array
sebagai header dari linked list nilai yang
melakukan hash ke lokasi tersebut
89
90
Insertion, I
• Misalkan ingin
menambahkan seagull ke
dalam tabel hash
• Diketahui:
– hashCode(seagull) = 143
– table[143] is not empty
– table[143] != seagull
– table[144] is not empty
– table[144] != seagull
– table[145] is empty
• Maka, seagull diletakkan
pada lokasi145
robin
sparrow
hawk
bluejay
owl
. . .
141
142
143
144
145
146
147
148
. . .
seagull
91
Insertion, II
• Misal ingin menambahkan
hawk ke dalam tabel hash
• Misal:
– hashCode(hawk) = 143
– table[143] is not empty
– table[143] != hawk
– table[144] is not empty
– table[144] == hawk
• Hawk ternyata sudah ada
dalam tabel, sehingga
tidak perlu dilakukan
apapun
robin
sparrow
hawk
seagull
bluejay
owl
. . .
141
142
143
144
145
146
147
148
. . .
92
Insertion, III
• Misal:
– Ingin menambah cardinal ke
dalam tabel hash
– hashCode(cardinal) = 147
– Lokasi terakhir 148
– 147 dan 148 sudah diisi
• Solusi:
– Anggap tabel sebagai
lingkaran; dimana setelah 148
adalah 0
– Maka, cardinal dapat menuju
lokasi 0 (atau 1, atau 2, ...)
robin
sparrow
hawk
seagull
bluejay
owl
. . .
141
142
143
144
145
146
147
148
93
Searching, I
• Misalkan ingin mencari
seagull dalam tabel hash
• Misalkan:
– hashCode(seagull) = 143
– table[143] is not empty
– table[143] != seagull
– table[144] is not empty
– table[144] != seagull
– table[145] is not empty
– table[145] == seagull !
• seagull ditemukan di lokasi
145
robin
sparrow
hawk
bluejay
owl
. . .
141
142
143
144
145
146
147
148
. . .
seagull
94
Searching, II
• Misalkan ingin mencari
cow dalam tabel hash
• Misal:
– hashCode(cow) = 144
– table[144] is not empty
– table[144] != cow
– table[145] is not empty
– table[145] != cow
– table[146] is empty
• Jika cow ada dalam tabel,
maka akan ditemukan
• Dalam kasus ini tidak
ditemukan
robin
sparrow
hawk
bluejay
owl
. . .
141
142
143
144
145
146
147
148
. . .
seagull
Solusi #2: Rehashing
• Cara lain untuk menangani collision adalah
menggunakan pendekatan rehash: buat fungsi hash lain
– Karena perlu dilakukan rehash berulangkali, maka diperlukan
deretan fungsi komputasi yang mudah
• Contoh sederhana: pada kasus hashing Strings, kita
memerlukan kode hash sebelumnya dan menambahkan
panjang String kedalamnya
– Akan lebih baik jika panjang string bukan sebuah komponen
yang digunakan dalam komputasi fungsi hash asal
• Rehashing bukanlah sebuah pendekatan yang umum,
sehingga tidak akan dijelaskan lebih jauh
95
Solusi #3: Bucket Hashing
• Solusi sebelumnya
menggunakan open
hashing: semua entri
berada pada sebuah
array yang sejajar
(tidak terstruktur)
• Solusi lain adalah
membuat setiap lokasi
array sebagai header
linked list yang
membagi ke lokasi
tersebut
96
robin
sparrow
hawk
bluejay
owl
. . .
141
142
143
144
145
146
147
148
. . .
seagull
Referensi
• http://www.cs.unc.edu/~plaisted/comp550/15-
btrees.ppt
• http://m1perpustakaanmateri.files.wordpress.co
m/2011/10/a2-asd-binarysearchtree-gnt.ppt
• http://www2.cs.siu.edu/~mengxia/Courses%20P
PT/220/carrano_ppt17.ppt
• http://www.cs.pitt.edu/~zzl/CS0445/rec8/rec8-
BST.ppt
• http://par.cse.nsysu.edu.tw/~cbyang/course/ds/c
hap7.ppt
97
• http://cms.dt.uh.edu/faculty/linh/courses/cs3304/
presentations/chap12/chapter12.ppt
• http://www.cs.ui.ac.id/WebKuliah/IKI10100/mater
ials/iki10100_20070508_slides.ppt
• http://dpnm.postech.ac.kr/cs233/lecture/lecture7.
ppt
• http://www.cis.upenn.edu/~matuszek/cit594-
2005/Lectures/18-hashing.ppt
98

More Related Content

More from BintangWijaya5 (7)

Materi Searching
Materi Searching Materi Searching
Materi Searching
 
Buku struktur data Sorting
Buku struktur data SortingBuku struktur data Sorting
Buku struktur data Sorting
 
Asd sesi sorting part2
Asd sesi sorting part2Asd sesi sorting part2
Asd sesi sorting part2
 
Asd sesi sorting part1
Asd sesi sorting part1Asd sesi sorting part1
Asd sesi sorting part1
 
Asd sesi searching part1
Asd sesi searching part1Asd sesi searching part1
Asd sesi searching part1
 
Asd sesi tree part1
Asd sesi tree part1Asd sesi tree part1
Asd sesi tree part1
 
Buku struktur data pages-111-122
Buku struktur data pages-111-122Buku struktur data pages-111-122
Buku struktur data pages-111-122
 

Recently uploaded

Penjelasan Asmaul Khomsah bahasa arab nahwu
Penjelasan Asmaul Khomsah bahasa arab nahwuPenjelasan Asmaul Khomsah bahasa arab nahwu
Penjelasan Asmaul Khomsah bahasa arab nahwu
Khiyaroh1
 
prinsip dasar kepramukaan dan metode kepramukaan
prinsip dasar kepramukaan dan metode kepramukaanprinsip dasar kepramukaan dan metode kepramukaan
prinsip dasar kepramukaan dan metode kepramukaan
aji guru
 
443016507-Sediaan-obat-PHYCOPHYTA-MYOPHYTA-dan-MYCOPHYTA-pptx.pptx
443016507-Sediaan-obat-PHYCOPHYTA-MYOPHYTA-dan-MYCOPHYTA-pptx.pptx443016507-Sediaan-obat-PHYCOPHYTA-MYOPHYTA-dan-MYCOPHYTA-pptx.pptx
443016507-Sediaan-obat-PHYCOPHYTA-MYOPHYTA-dan-MYCOPHYTA-pptx.pptx
ErikaPutriJayantini
 
PPt-Juknis-PPDB-2024 (TerbarU) kabupaten GIanyar.pptx
PPt-Juknis-PPDB-2024 (TerbarU) kabupaten GIanyar.pptxPPt-Juknis-PPDB-2024 (TerbarU) kabupaten GIanyar.pptx
PPt-Juknis-PPDB-2024 (TerbarU) kabupaten GIanyar.pptx
iwidyastama85
 
Aksi Nyata Cegah Perundungan Mulai dari Kelas [Guru].pptx
Aksi Nyata Cegah Perundungan Mulai dari Kelas [Guru].pptxAksi Nyata Cegah Perundungan Mulai dari Kelas [Guru].pptx
Aksi Nyata Cegah Perundungan Mulai dari Kelas [Guru].pptx
AgusSuarno2
 

Recently uploaded (20)

Penjelasan Asmaul Khomsah bahasa arab nahwu
Penjelasan Asmaul Khomsah bahasa arab nahwuPenjelasan Asmaul Khomsah bahasa arab nahwu
Penjelasan Asmaul Khomsah bahasa arab nahwu
 
Modul Ajar Matematika Kelas 5 Fase C Kurikulum Merdeka [abdiera.com]
Modul Ajar Matematika Kelas 5 Fase C Kurikulum Merdeka [abdiera.com]Modul Ajar Matematika Kelas 5 Fase C Kurikulum Merdeka [abdiera.com]
Modul Ajar Matematika Kelas 5 Fase C Kurikulum Merdeka [abdiera.com]
 
MODUL AJAR PENDIDIKAN PANCASILA KELAS 5 KURIKULUM MERDEKA.pdf
MODUL AJAR PENDIDIKAN PANCASILA KELAS 5 KURIKULUM MERDEKA.pdfMODUL AJAR PENDIDIKAN PANCASILA KELAS 5 KURIKULUM MERDEKA.pdf
MODUL AJAR PENDIDIKAN PANCASILA KELAS 5 KURIKULUM MERDEKA.pdf
 
PPDB SMAN 1 SURADE - PROV JABAR 2024 / 2025
PPDB SMAN 1 SURADE - PROV JABAR 2024 / 2025PPDB SMAN 1 SURADE - PROV JABAR 2024 / 2025
PPDB SMAN 1 SURADE - PROV JABAR 2024 / 2025
 
PELAKSANAAN + Link2 MATERI Training_ "AUDIT INTERNAL + SISTEM MANAJEMEN MUTU ...
PELAKSANAAN + Link2 MATERI Training_ "AUDIT INTERNAL + SISTEM MANAJEMEN MUTU ...PELAKSANAAN + Link2 MATERI Training_ "AUDIT INTERNAL + SISTEM MANAJEMEN MUTU ...
PELAKSANAAN + Link2 MATERI Training_ "AUDIT INTERNAL + SISTEM MANAJEMEN MUTU ...
 
prinsip dasar kepramukaan dan metode kepramukaan
prinsip dasar kepramukaan dan metode kepramukaanprinsip dasar kepramukaan dan metode kepramukaan
prinsip dasar kepramukaan dan metode kepramukaan
 
PWS KIA (Pemantauan Wilayah Setempat) Kesehatan Ibu dan Anak
PWS KIA (Pemantauan Wilayah Setempat) Kesehatan Ibu dan AnakPWS KIA (Pemantauan Wilayah Setempat) Kesehatan Ibu dan Anak
PWS KIA (Pemantauan Wilayah Setempat) Kesehatan Ibu dan Anak
 
MODUL AJAR PENDIDIKAN AGAMA ISLAM & BUDI PEKERTI (PAIBP) KELAS 6.pdf
MODUL AJAR PENDIDIKAN AGAMA ISLAM & BUDI PEKERTI (PAIBP) KELAS 6.pdfMODUL AJAR PENDIDIKAN AGAMA ISLAM & BUDI PEKERTI (PAIBP) KELAS 6.pdf
MODUL AJAR PENDIDIKAN AGAMA ISLAM & BUDI PEKERTI (PAIBP) KELAS 6.pdf
 
443016507-Sediaan-obat-PHYCOPHYTA-MYOPHYTA-dan-MYCOPHYTA-pptx.pptx
443016507-Sediaan-obat-PHYCOPHYTA-MYOPHYTA-dan-MYCOPHYTA-pptx.pptx443016507-Sediaan-obat-PHYCOPHYTA-MYOPHYTA-dan-MYCOPHYTA-pptx.pptx
443016507-Sediaan-obat-PHYCOPHYTA-MYOPHYTA-dan-MYCOPHYTA-pptx.pptx
 
MODUL AJAR BAHASA INDONESIA KELAS 5 KURIKULUM MERDEKA.pdf
MODUL AJAR BAHASA INDONESIA KELAS 5 KURIKULUM MERDEKA.pdfMODUL AJAR BAHASA INDONESIA KELAS 5 KURIKULUM MERDEKA.pdf
MODUL AJAR BAHASA INDONESIA KELAS 5 KURIKULUM MERDEKA.pdf
 
PPt-Juknis-PPDB-2024 (TerbarU) kabupaten GIanyar.pptx
PPt-Juknis-PPDB-2024 (TerbarU) kabupaten GIanyar.pptxPPt-Juknis-PPDB-2024 (TerbarU) kabupaten GIanyar.pptx
PPt-Juknis-PPDB-2024 (TerbarU) kabupaten GIanyar.pptx
 
Variasi dan Gaya Mengajar, Mata Kuliah Strategi Belajar Mengajar
Variasi dan Gaya Mengajar, Mata Kuliah Strategi Belajar MengajarVariasi dan Gaya Mengajar, Mata Kuliah Strategi Belajar Mengajar
Variasi dan Gaya Mengajar, Mata Kuliah Strategi Belajar Mengajar
 
MODUL AJAR SENI RUPA KELAS 5 KURIKULUM MERDEKA.pdf
MODUL AJAR SENI RUPA KELAS 5 KURIKULUM MERDEKA.pdfMODUL AJAR SENI RUPA KELAS 5 KURIKULUM MERDEKA.pdf
MODUL AJAR SENI RUPA KELAS 5 KURIKULUM MERDEKA.pdf
 
AKUNTANSI INVESTASI PD SEKURITAS UTANG.pptx
AKUNTANSI INVESTASI PD SEKURITAS UTANG.pptxAKUNTANSI INVESTASI PD SEKURITAS UTANG.pptx
AKUNTANSI INVESTASI PD SEKURITAS UTANG.pptx
 
Aksi Nyata Modul 1.3 Visi Guru penggerak
Aksi Nyata Modul 1.3 Visi Guru penggerakAksi Nyata Modul 1.3 Visi Guru penggerak
Aksi Nyata Modul 1.3 Visi Guru penggerak
 
Aksi Nyata Cegah Perundungan Mulai dari Kelas [Guru].pptx
Aksi Nyata Cegah Perundungan Mulai dari Kelas [Guru].pptxAksi Nyata Cegah Perundungan Mulai dari Kelas [Guru].pptx
Aksi Nyata Cegah Perundungan Mulai dari Kelas [Guru].pptx
 
Materi Penggolongan Obat Undang-Undang Kesehatan
Materi Penggolongan Obat Undang-Undang KesehatanMateri Penggolongan Obat Undang-Undang Kesehatan
Materi Penggolongan Obat Undang-Undang Kesehatan
 
MODUL AJAR BAHASA INDONESIA KELAS 2 KURIKULUM MERDEKA.pdf
MODUL AJAR BAHASA INDONESIA KELAS 2 KURIKULUM MERDEKA.pdfMODUL AJAR BAHASA INDONESIA KELAS 2 KURIKULUM MERDEKA.pdf
MODUL AJAR BAHASA INDONESIA KELAS 2 KURIKULUM MERDEKA.pdf
 
E-modul materi Ekosistem Kelas 10 SMA (Preview)
E-modul materi Ekosistem Kelas 10 SMA (Preview)E-modul materi Ekosistem Kelas 10 SMA (Preview)
E-modul materi Ekosistem Kelas 10 SMA (Preview)
 
MODUL AJAR SENI TARI KELAS 5 KURIKULUM MERDEKA.pdf
MODUL AJAR SENI TARI KELAS 5 KURIKULUM MERDEKA.pdfMODUL AJAR SENI TARI KELAS 5 KURIKULUM MERDEKA.pdf
MODUL AJAR SENI TARI KELAS 5 KURIKULUM MERDEKA.pdf
 

Asd sesi searching part 2

  • 3. 3 Binary Trees • A binary tree is a tree in which no node can have more than two subtrees; the maximum outdegree for a node is two. • In other words, a node can have zero, one, or two subtrees. • These subtrees are designated as the left subtree and the right subtree.
  • 4.
  • 5.
  • 6. 6
  • 7. 7 A null tree is a tree with no nodes
  • 8. 8 Some Properties of Binary Trees • The height of binary trees can be mathematically predicted • Given that we need to store N nodes in a binary tree, the maximum height is maxH N A tree with a maximum height is rare. It occurs when all of the nodes in the entire tree have only one successor.
  • 9. 9 Some Properties of Binary Trees • The minimum height of a binary tree is determined as follows:  min 2log 1H N  For instance, if there are three nodes to be stored in the binary tree (N=3) then Hmin=2.
  • 10. 10 Some Properties of Binary Trees • Given a height of the binary tree, H, the minimum number of nodes in the tree is given as follows: minN H
  • 11. 11 Some Properties of Binary Trees • The formula for the maximum number of nodes is derived from the fact that each node can have only two descendents. Given a height of the binary tree, H, the maximum number of nodes in the tree is given as follows: max 2 1H N  
  • 12. 12 Complete and nearly complete binary trees • A complete tree has the maximum number of entries for its height. The maximum number is reached when the last level is full. • A tree is considered nearly complete if it has the minimum height for its nodes and all nodes in the last level are found on the left
  • 13. 13
  • 14. 14 Binary Tree Traversal • A binary tree traversal requires that each node of the tree be processed once and only once in a predetermined sequence. • In the depth-first traversal processing process along a path from the root through one child to the most distant descendant of that first child before processing a second child.
  • 15. Tree Traversal • Is a procedure that systematically visits every vertex of an ordered rooted tree • Three most commonly used algorithms – Preorder traversal. – Inorder traversal. – Postorder traversal.
  • 16.
  • 17.
  • 18. 18
  • 19. 19
  • 20. 20
  • 21. 21
  • 22. 22
  • 23. 23
  • 24. 24
  • 25. Data Structures: A Pseudocode Approach with C 25
  • 26. 26
  • 27. 27
  • 28. 28
  • 29. 29
  • 30. 30
  • 31. 31
  • 32. 32
  • 33. 33
  • 34. 34
  • 35. Metode Pohon Biner • Disebut dengan Binary Search Tree (BST) • Binary search tree (BST) merupakan binary tree dengan sifat berikut:  Semua item pada left subtree bernilai kurang dari root.  Semua item pada right subtree bernilai lebih atau sama dengan root.  Setiap subtree merupakan BST. 35
  • 37. BST: Contoh 37 56 26 200 18 28 190 213 12 24 27
  • 38. Binary Search Trees A binary search tree Not a binary search tree
  • 39. Binary Search Trees • Average depth of a node is O(log N) • Maximum depth of a node is O(N) The same set of keys may have different BSTs
  • 40. BST Valid 40 (a), (b) - complete and balanced trees; (d) – nearly complete and balanced tree; (c), (e) – neither complete nor balanced trees
  • 42. BST: Operasi • Traversals • Searches • Insertion • Deletion 42
  • 44. BST: Traversals  Preorder traversal 23 18 12 20 44 35 52  Postorder traversal 12 20 18 35 52 44 23  Inorder traversal 12 18 20 23 35 44 52 44 Inorder traversal pada BST menghasilkan nilai yang terurut dari kecil  besar
  • 45. 45 Right-Node-Left Traversal 52 44 35 23 20 18 12 Right-node-left traversal of a binary search tree produces a descending sequence
  • 46. Searching BST • If we are searching for 15, then we are done. • If we are searching for a key < 15, then we should search in the left subtree. • If we are searching for a key > 15, then we should search in the right subtree.
  • 47. Time complexity = O(height of the tree)
  • 48. BST: Search  Beberapa algoritma search:  Mencari node dengan nilai terkecil  Mencari node dengan nilai terbesar  Mencari node dengan nilai tertentu (BST search) 48
  • 49. BST: Search  Dari BST sudah langsung diketahui bahwa: » Nilai minimum terletak di node sebelah kiri. » Nilai maksimum terletak di node sebelah kanan. 49 Tree-Minimum(x) Tree-Maximum(x) 1. while left[x]  NIL 1. while right[x]  NIL 2. do x  left[x] 2. do x  right[x] 3. return x 3. return x
  • 50. BST: Search  mencari node terkecil 50
  • 51. 51
  • 52. BST: Search  mencari node terbesar 52 right subtree not empty right subtree not empty right subtree empty return
  • 53. 53
  • 54. 54 Mencari node dengan nilai tertentu (BST search)
  • 55. 55
  • 56. 56
  • 57. 57 BST Insertion • To insert data all we need to do is follow the branches to an empty subtree and then insert the new node. • In other words, all inserts take place at a leaf or at a leaflike node – a node that has only one null subtree.
  • 58. Insertion • Proceed down the tree as you would with a find • If X is found, do nothing (or update something) • Otherwise, insert X at the last spot on the path traversed • Time complexity = O(height of the tree)
  • 59. BST: Insertion • Sisipkan 4 • Kunci penyisipan akan ditambahkan di left node. 59 6 2 5 8 7 9 4 6 2 5 8 7 9
  • 60. 60
  • 61. 61
  • 62. 62
  • 64. 64 Deletion • There are the following possible cases when we delete a node: • The node to be deleted has no children. In this case, all we need to do is delete the node. • The node to be deleted has only a right subtree. We delete the node and attach the right subtree to the deleted node’s parent. • The node to be deleted has only a left subtree. We delete the node and attach the left subtree to the deleted node’s parent. • The node to be deleted has two subtrees. It is possible to delete a node from the middle of a tree, but the result tends to create very unbalanced trees.
  • 65. Deletion • When we delete a node, we need to consider how we take care of the children of the deleted node. – This has to be done such that the property of the search tree is maintained.
  • 66. Deletion under Different Cases • Case 1: the node is a leaf – Delete it immediately • Case 2: the node has one child – Adjust a pointer from the parent to bypass that node
  • 67. Deletion Case 3 • Case 3: the node has 2 children – Replace the key of that node with the minimum element at the right subtree – Delete that minimum element • Has either no child or only right child because if it has a left child, that left child would be smaller and would have been chosen. So invoke case 1 or 2. • Time complexity = O(height of the tree)
  • 68. BST: Deletion  Untuk menghapus sebuah node dari BST, pertama lakukan pencarian (search) untuk mencari node yang akan dihapus.  Terdapat empat kasus pada penghapusan sebuah node di BST. Node yang dihapus :  Tidak memiliki child  Hanya punya right subtree.  Hanya punya left subtree  Punya dua subtree 68
  • 69. BST: Deletion 1. Node tidak memiliki child  Hapus node 2. Node hanya memiliki right subtree.  Hapus node  Sambungkan right subtree ke parent node yang akan dihapus. 3. Node hanya memiliki left subtree.  Hapus node  Sambungkan left subtree ke parent node yang akan dihapus. 69
  • 70. BST: Deletion 4. Node memiliki dua subtree.  Temukan node dengan nilai terbesar pada left subtree node yang dihapus kemudian pindahkan node tersebut untuk menggantikan node yang dihapus atau  Temukan node dengan nilai terkecil pada right subtree node yang dihapus kemudian pindahkan node tersebut untuk menggantikan node yang dihapus. 70
  • 71. 71 Deletion from the middle of a tree • Rather than simply delete the node, we try to maintain the existing structure as much as possible by finding data to take the place of the deleted data. This can be done in one of two ways.
  • 72. 72 Deletion from the middle of a tree • We can find the largest node in the deleted node’s left subtree and move its data to replace the deleted node’s data. • We can find the smallest node on the deleted node’s right subtree and move its data to replace the deleted node’s data. • Either of these moves preserves the integrity of the binary search tree.
  • 73. 73
  • 76. 76 Searching • Consider the problem of searching an array for a given value – If the array is not sorted, the search requires O(n) time • If the value isn’t there, we need to search all n elements • If the value is there, we search n/2 elements on average – If the array is sorted, we can do a binary search • A binary search requires O(log n) time • About equally fast whether the element is found or not – It doesn’t seem like we could do much better • How about an O(1), that is, constant time search? • We can do it if the array is organized in a particular way
  • 77. Metode Hash • Hash adalah algoritma yang memetakan kumpulan data besar dengan variabel panjang, disebut kunci, ke kumpulan data yang lebih kecil dan mempunyai ukuran tetap. • Informasi dapat diakses pada waktu yang konstan 77
  • 78. Concept of hashing • The problem at hand is to define and implement a mapping from a domain of keys to a domain of locations • From the performance standpoint, the goal is to avoid collisions (A collision occurs when two or more keys map to the same location) • From the compactness standpoint, no application ever stores all keys in a domain simultaneously unless the size of the domain is small
  • 79. Concept of hashing (con’t) • The information to be retrieved is stored in a hash table which is best thought of as an array of m locations, called buckets • The mapping between a key and a bucket is called the hash function • The time to store and retrieve data is proportional to the time to compute the hash function
  • 80. Tabel Hash • Hashing digunakan untuk menyimpan jumlah data yang relatif besar ke tabel yang disebut tabel hash ADT. • Tabel hash biasanya mempunyai ukuran H yang tetap, yang lebih besar dari jumlah data yang ingin disimpan • load factor (): rasio data dengan ukuran tabel hash. • Fungsi hash memetakan item ke indeks dalam rentang 0 sampai H-ukuran-1 80
  • 82. Tabel Hash • Operasi pada tabel hash – Search: komputasi f(k) dan lihat apakah pasangan ada – Insert: komputasi f(k) dan tempatkan dalam posisi – Delete: komputasi f(k) dan hapus pasangan dalam posisi 82
  • 83. Fungsi Hash • Fungsi hash harus memenuhi fitur berikut ini: – Mudah untuk dikomputasi. – Dua kunci yang berbeda dipetakan ke sel array yang berbeda. – Dapat dilakukan menggunakan tabel pengalamatan langsung dimana kumpulan umum kunci yang cukup kecil. – Distribusikan kunci dengan merata ke semua sel. • Sebuah fungsi hash digunakan dengan fungsi mod bilangan prima. • Semua manipulasi pada digit, dengan kompleksitas minimum dan distribusi yang baik dapat digunakan. 83
  • 84. Minimal Perfect Hash Functions • Minimal perfect hash function (MPHF) adalah fungsi hash sempurna dengan properti yang memecah beberapa kunci m ke beberapa wadah m tanpa adanya benturan antar data • Cichelli(1980) and of Cercone et al.(1983) mengusulkan dua konsep penting: (1) menggunakan tables of values sebagai paramater (2) menggunakan pendekatan mapping, ordering, dan searching (MOS) 84
  • 85. Minimal Perfect Hash Functions • Mapping: mentransformasikan kumpulan kunci dari asal ke tempat baru • Ordering: menempatkan kunci-kunci dalam urutan yang menunjukkan nilai hash terhadap kunci • Searching: menentukan nilai hash pada kunci setiap level 85 Mapping → Ordering → Searching
  • 86. Contoh Ideal Fungsi Hash • Misalkan fungsi hash dengan nilai-nilai berikut: hashCode("apple") = 5 hashCode("watermelon") = 3 hashCode("grapes") = 8 hashCode("cantaloupe") = 7 hashCode("kiwi") = 0 hashCode("strawberry") = 9 hashCode("mango") = 6 hashCode("banana") = 2 86 kiwi banana watermelon apple mango cantaloupe grapes strawberry 0 1 2 3 4 5 6 7 8 9
  • 87. Contoh (non) Ideal Fungsi Hash • Misalkan fungsi hash dengan nilai- nilai berikut: hashCode("apple") = 5 hashCode("watermelon") = 3 hashCode("grapes") = 8 hashCode("cantaloupe") = 7 hashCode("kiwi") = 0 hashCode("strawberry") = 9 hashCode("mango") = 6 hashCode("banana") = 2 hashCode(“honeydew”) = 6 Apa yang terjadi? 87 kiwi banana watermelon apple mango cantaloupe grapes strawberry 0 1 2 3 4 5 6 7 8 9
  • 88. Collision • Saat terdapat dua nilai yang terhubung ke lokasi array yang sama, maka disebut collision • Biasanya collision akan dilayani secara “first come, first served”—nilai pertama yang muncul yang akan mendapatkan lokasi • Teknik khusus akan dilakukan untuk menangani nilai kedua 88
  • 89. Penanganan Collision • Ada 3 cara yang bisa dilakukan: – Solusi #1: mencari lokasi kosong yang lain • Pencarian bisa dihentikan saat menemukan nilai atau lokasi kosong • Pencarian harus berakhir – Solusi #2: menggunakan fungsi hash kedua • ..dan ketiga, dan keempat, dan kelima,….dst – Solusi #3: menggunakan lokasi array sebagai header dari linked list nilai yang melakukan hash ke lokasi tersebut 89
  • 90. 90 Insertion, I • Misalkan ingin menambahkan seagull ke dalam tabel hash • Diketahui: – hashCode(seagull) = 143 – table[143] is not empty – table[143] != seagull – table[144] is not empty – table[144] != seagull – table[145] is empty • Maka, seagull diletakkan pada lokasi145 robin sparrow hawk bluejay owl . . . 141 142 143 144 145 146 147 148 . . . seagull
  • 91. 91 Insertion, II • Misal ingin menambahkan hawk ke dalam tabel hash • Misal: – hashCode(hawk) = 143 – table[143] is not empty – table[143] != hawk – table[144] is not empty – table[144] == hawk • Hawk ternyata sudah ada dalam tabel, sehingga tidak perlu dilakukan apapun robin sparrow hawk seagull bluejay owl . . . 141 142 143 144 145 146 147 148 . . .
  • 92. 92 Insertion, III • Misal: – Ingin menambah cardinal ke dalam tabel hash – hashCode(cardinal) = 147 – Lokasi terakhir 148 – 147 dan 148 sudah diisi • Solusi: – Anggap tabel sebagai lingkaran; dimana setelah 148 adalah 0 – Maka, cardinal dapat menuju lokasi 0 (atau 1, atau 2, ...) robin sparrow hawk seagull bluejay owl . . . 141 142 143 144 145 146 147 148
  • 93. 93 Searching, I • Misalkan ingin mencari seagull dalam tabel hash • Misalkan: – hashCode(seagull) = 143 – table[143] is not empty – table[143] != seagull – table[144] is not empty – table[144] != seagull – table[145] is not empty – table[145] == seagull ! • seagull ditemukan di lokasi 145 robin sparrow hawk bluejay owl . . . 141 142 143 144 145 146 147 148 . . . seagull
  • 94. 94 Searching, II • Misalkan ingin mencari cow dalam tabel hash • Misal: – hashCode(cow) = 144 – table[144] is not empty – table[144] != cow – table[145] is not empty – table[145] != cow – table[146] is empty • Jika cow ada dalam tabel, maka akan ditemukan • Dalam kasus ini tidak ditemukan robin sparrow hawk bluejay owl . . . 141 142 143 144 145 146 147 148 . . . seagull
  • 95. Solusi #2: Rehashing • Cara lain untuk menangani collision adalah menggunakan pendekatan rehash: buat fungsi hash lain – Karena perlu dilakukan rehash berulangkali, maka diperlukan deretan fungsi komputasi yang mudah • Contoh sederhana: pada kasus hashing Strings, kita memerlukan kode hash sebelumnya dan menambahkan panjang String kedalamnya – Akan lebih baik jika panjang string bukan sebuah komponen yang digunakan dalam komputasi fungsi hash asal • Rehashing bukanlah sebuah pendekatan yang umum, sehingga tidak akan dijelaskan lebih jauh 95
  • 96. Solusi #3: Bucket Hashing • Solusi sebelumnya menggunakan open hashing: semua entri berada pada sebuah array yang sejajar (tidak terstruktur) • Solusi lain adalah membuat setiap lokasi array sebagai header linked list yang membagi ke lokasi tersebut 96 robin sparrow hawk bluejay owl . . . 141 142 143 144 145 146 147 148 . . . seagull
  • 97. Referensi • http://www.cs.unc.edu/~plaisted/comp550/15- btrees.ppt • http://m1perpustakaanmateri.files.wordpress.co m/2011/10/a2-asd-binarysearchtree-gnt.ppt • http://www2.cs.siu.edu/~mengxia/Courses%20P PT/220/carrano_ppt17.ppt • http://www.cs.pitt.edu/~zzl/CS0445/rec8/rec8- BST.ppt • http://par.cse.nsysu.edu.tw/~cbyang/course/ds/c hap7.ppt 97
  • 98. • http://cms.dt.uh.edu/faculty/linh/courses/cs3304/ presentations/chap12/chapter12.ppt • http://www.cs.ui.ac.id/WebKuliah/IKI10100/mater ials/iki10100_20070508_slides.ppt • http://dpnm.postech.ac.kr/cs233/lecture/lecture7. ppt • http://www.cis.upenn.edu/~matuszek/cit594- 2005/Lectures/18-hashing.ppt 98