Sub Topik
• Penjelasan Tree
• Istilah pada tree
• Binary Tree
• Jenis Binary Tree
• ADT Binary tree
Tree (Pohon)
Real World
leaves
branches
root
Computer Scientist’s View
root leaves
branches
nodes
Definisi
• Kumpulan node yang saling terhubung secara
hirarki.
• Hirarki = bertingkat.
• Tiap node dapat berisi data dan link
(penghubung) ke node lainnya
• Tiap node memiliki satu induk, kecuali node
root (akar) yang tidak memiliki induk.
• Tiap node dapat memiliki anak dalam jumlah
berapapun.
Linked list dan Tree
• Linked list linear/serial data
– Contoh : nama-nama mahasiswa dalam satu kelas.
• Tree non linear/hierachically data
– Contoh : tingkatan pegawai dalam perusahaan.
Contoh Tree
• Mis. : Struktur organisasi sebuah perusahaan
Contoh Tree
– Mis. : Daftar isi sebuah buku
Contoh Tree
– Mis. : File system
Tree (Pohon)
• Root adalah node yang memiliki hirarki
tertinggi.
• Subtree (pohon anak) adalah beberapa node
yang tersusun hirarki yang ada dibawah root.
Root and Subtrees
Object
root
RuntimeException
Tree (Pohon)
• Level adalah posisi hirarki dari sebuah node.
Untuk root bisa diberikan level 0 atau 1.
• Leaf (Daun) adalah node yang tidak memiliki
anak atau node yang berada pada hirarki
paling bawah.
• Height (tinggi)/depth adalah jumlah level dari
sebuah tree.
Leaves
Object
RuntimeException
Node Degree
3
Object
2 1 1 OutputStream
Number Throwable
0 0 1 0
0
RuntimeException
Level
Object
Level
Level 1
1
Level
Level22
Number Throwable OutputStream
RuntimeException
Level 4
Level 4
Contoh Tree (Pohon)
Level 0 R Root/Akar
Level 1 S T
Level 2 X U V W Daun/
Leaf
Level 3 Y Z
17
Istilah Tree (Pohon)
Latihan
Ancestor (F)?
Descendant (B)?
Parent (I)?
Child (C)?
Sibling (G)?
Size?
Height?
Root?
Leaf?
Degree (C)?
Tree (Pohon)
• Dimana,
Ancestor (F) = C,A
Descendant (B) = D,E
Parent (I) = H
Child (A) = B,C
Sibling (F) = G,H
Size = 9
Height = 3/4
Root = A
Leaf = D,E,F,G,I
Degree (C) = 3
Binary Tree
Gambar Binary Trees
Binary Tree
D K
B F J L
A C E G I
Incomplete Binary Tree
Gambar a Gambar b
Jenis Tree
(Skewed Binary Tree)
• Binary tree yang semua nodenya (kecuali leaf)
hanya memiliki satu anak.
• Disebut juga minimum binary tree.
a1
22 33
b c
44 55 66 77
d e f g
88 99 10
10
h i j
tree[] a b c d e f g h i j
0
0 5 5 10 10
Right-Skewed Binary Tree
11
a
33
b 77
c
15
15
d
tree[] a - b - - - c - - - - - - - d
0
0 5
5 10
10 15
15
Linked List Representation
Class BinaryTreeNode
class BinaryTreeNode
{
Object element;
BinaryTreeNode leftChild; // left subtree
BinaryTreeNode rightChild;// right subtree
// constructors and any other methods come here
}
Contoh Representasi Linked List
root
root a
b c
d e
g
f
leftChild
element h
rightChild
Binary Tree Traversal
Definisi
• Penelusuran seluruh node pada binary tree.
• Metode :
– Preorder
– Inorder
– Postorder
– Level order
Preorder Traversal
a b c
Preorder Example (visit = print)
a b d g h e i c f j
Preorder Of Expression Tree
/ * + a b - c d + e f
b c
b a c
Inorder Example (visit = print)
g d h b e i a f j c
Inorder By Projection (Squishing)
a
b c
f
d e
g h i j
g d h b e i a f j c
Inorder Of Expression Tree
a + b * c - d / e + f
b c
b c a
Postorder Example (visit = print)
g h d i e b j f c a
Postorder Of Expression Tree
a b + c d - * e f + /
b c
f
d e
g h i j
• Make a clone.
• Determine height.
•Determine number of nodes.
Level Order
Let t be the tree root.
while (t != null)
{
visit t and put its children on a FIFO queue;
remove a node from the FIFO queue and call it t;
// remove returns null when queue is empty
}
Latihan
• Telusuri pohon biner berikut dengan
menggunakan metode pre, in, post, dan level
traversal.
Latihan 1
a.
* b.
-
3 5 2 /
8 4
Latihan 2
Level-Order Example (visit = print)
a
b c
f
d e
g h i j
a b c d e f g h i j
Contoh : Pohon Ekspresi
PreOrder, PostOrder, InOrder
• Pre-order :
– Node, left, right
– Ekspresi Prefix : ++a*bc*+*defg
• Post-order :
– Node, left, right
– Ekspresi Postfix : abc*+de*f+g*+
• In-order :
– Node, left, right
– Ekspresi Infix : a+b*c+d*e+f*g
Pustaka
• Sartaj Sahni , “Data Structures & Algorithms”,
Presentation L20-24.
• Mitchell Waite, “Data Structures & Algorithms in
Java”, SAMS, 2001