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.
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
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.
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
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.
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
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
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.
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
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