tree
DESCRIPTION
membahas pohon dalam struktur data, pohon biner, dan penelusuran pohon binerTRANSCRIPT
tReE
Tenia Wahyuningrum, S.Kom. MT
www.st3telkom.ac.id
Tree …
Kumpulan node yang saling
terhubung satu sama lain
dalam suatu kesatuan yang
membentuk layakya struktur sebuah
pohon.
www.st3telkom.ac.idTenia Wahyuningrum
Tree … merepresentasikan suatu struktur
hirarki (one-to-many)
tampak sebagai kumpulan node dari
atas ke bawah.
www.st3telkom.ac.idTenia Wahyuningrum
Tree …
salah satu bentuk implementasi
banyak linked list yang
digunakan untuk menggambarkan
hubungan yang bersifat hirarkis
www.st3telkom.ac.idTenia Wahyuningrum
Karena harapan lah, kita menanam pohon
Meskipun kita tahu tak akan memetik buahnya berpuluh-puluh tahun kemudian
Family tree
www.st3telkom.ac.idTenia Wahyuningrum
Contoh penggunaan struktur
tree
Tournamen schedule
www.st3telkom.ac.idTenia Wahyuningrum
Contoh penggunaan struktur
tree
Organization Structure
www.st3telkom.ac.idTenia Wahyuningrum
Contoh penggunaan struktur
tree
Tree anatomy
R
ST
X
ZY
WVU
Root
Internal Node
Leaf
Child of X
Subtree
Parent of Z and Y
Level 0
Level 1
Level 2
Level 3
Terminologi Tree
www.st3telkom.ac.idTenia Wahyuningrum
a Tree
R
S T
ZY WVU
www.st3telkom.ac.idTenia Wahyuningrum
Ancestor (U) = T, R
Descendant (T) = U, V, W
Parent (Y) = S
Child (R) = S, T
Sibling (U) = V, W
Size = 8
Height = 3
Root = R
Leaf = Y, Z, U, V, W
Degree (T) = 3
www.st3telkom.ac.idTenia Wahyuningrum
Representasi Tree
Notasi Tingkat
Tree Representation
www.st3telkom.ac.idTenia Wahyuningrum
Notasi Kurung
(A(B(D,E(I,J)),C(F,G,H)))
Tree Representation
www.st3telkom.ac.idTenia Wahyuningrum
Latihan• Buat diagram venn, notasi kurung dan
notasi tingkat!X
Y R S
QT WU
Z
P M
N
www.st3telkom.ac.idTenia Wahyuningrum
• Identifikasikan !
Ancestor (N) =
Descendant (Y) =
Parent (Z) =
Child (Q) =
Sibling (U) =
Size =
Height =
Root =
Leaf =
Degree (R) =
www.st3telkom.ac.idTenia Wahyuningrum
Binnary Tree
Semakin tinggi pohon, semakin kuat angin meniupnya
– Suatu tree dengan syarat bahwa tiap
node hanya boleh memiliki maksimal dua
subtree dan kedua subtree tersebut harus
terpisah.
– Tiap node dalam binary tree hanya boleh
memiliki paling banyak dua child.
www.st3telkom.ac.idTenia Wahyuningrum
www.st3telkom.ac.idTenia Wahyuningrum
Binary Tree
Binary Tree
www.st3telkom.ac.idTenia Wahyuningrum
A
B C
ED GF
www.st3telkom.ac.idTenia Wahyuningrum
A
B C
ED
Binary Tree
www.st3telkom.ac.idTenia Wahyuningrum
A
B E
C F
D
Binary Tree
Jumlah maksimum node pada setiap
tingkat adalah 2n
Node pada binary tree maksimum
berjumlah 2n-1
www.st3telkom.ac.idTenia Wahyuningrum
Node pada Binary Tree
www.st3telkom.ac.idTenia Wahyuningrum
A
B C
ED GF
Node pada Binary Tree
Tingkat ke-0, jumlah max = 20
Tingkat ke-1, jumlah max = 21
Tingkat ke-2, jumlah max = 22
Latihan• Gambarkan pohon biner dengan
ketentuan sbb : Ancestor (M) = Z, X
Descendant (Y) = K, L
Parent (N) = Z
Child (Z) = M, N
Sibling (Y) = Z
Size = 7
Height = 3
Root = X
Leaf = K, L, M, N
www.st3telkom.ac.idTenia Wahyuningrum
xxx
www.st3telkom.ac.idTenia Wahyuningrum
Penelusuran Pohon Biner
Jika kau teriaki dan maki pohon, lambat laun pohon akan mati
Definisi• Penelusuran seluruh node pada binary
tree.
• Metode :
–Preorder
–Inorder
–Postorder
www.st3telkom.ac.idTenia Wahyuningrum
PreOrder Traversal
1. Cetak data pada root
2. Secara rekursif mencetak seluruh data
pada subpohon kiri
3. Secara rekursif mencetak seluruh data
pada subpohon kanan
www.st3telkom.ac.idTenia Wahyuningrum
Preorder Example (visit =
print)a
b c
a b c
www.st3telkom.ac.idTenia Wahyuningrum
Preorder Example (visit =
print)a
b c
d ef
g h i j
a b d g h e i c f j
www.st3telkom.ac.idTenia Wahyuningrum
Preorder Of Expression Tree
+
a b
-
c d
+
e f
*
/
Gives prefix form of expression!
/ * + a b - c d + e f
www.st3telkom.ac.idTenia Wahyuningrum
InOrder Traversal
1.Secara rekursif mencetak seluruh data
pada subpohon kiri
2.Cetak data pada root
3.Secara rekursif mencetak seluruh data
pada subpohon kanan
www.st3telkom.ac.idTenia Wahyuningrum
Inorder Example (visit = print)a
b c
b a c
www.st3telkom.ac.idTenia Wahyuningrum
Inorder Example (visit = print)
a
b c
d ef
g h i j
g d h b e i a f j c
www.st3telkom.ac.idTenia Wahyuningrum
Postorder Traversal
1.Secara rekursif mencetak seluruh data
pada subpohon kiri
2.Secara rekursif mencetak seluruh data
pada subpohon kanan
3.Cetak data pada root
www.st3telkom.ac.idTenia Wahyuningrum
Postorder Example (visit =
print)a
b c
b c a
www.st3telkom.ac.idTenia Wahyuningrum
Postorder Example (visit =
print) a
b c
d ef
g h i j
g h d i e b j f c a
www.st3telkom.ac.idTenia Wahyuningrum
Postorder Of Expression Tree
+
a b
-
c d
+
e f
*
/
Gives postfix form of expression!
a b + c d - * e f + /
www.st3telkom.ac.idTenia Wahyuningrum
Latihan• Telusuri pohon biner berikut dengan
menggunakan metode pre, in, post !
www.st3telkom.ac.idTenia Wahyuningrum
Latihan 1
+
*
3 5
-
2 /
8 4
www.st3telkom.ac.idTenia Wahyuningrum
Latihan 2
www.st3telkom.ac.idTenia Wahyuningrum
2
7 5
62 9
4115