Algoritma dan Struktur Data Binary Tree & Binary Search Tree (BST) Teknik Informatika Universitas Muhammadiyah Malang 2016
Outline Tree Binary tree Istilah pada tree Operasi dasar binary tree BST
Definisi Tree adalah Kumpulan element yang saling terhubung secara hirarki (one to many). Element pada tree disebut node. http://people.cs.ksu.edu/~schmidt/300s05/lectures/week7b.html
Aturan Sebuah node hanya boleh memiliki satu induk/parent. Kecuali root, tidak memiliki induk/parent. Setiap node dapat memiliki nol atau banyak cabang anak (one to many). Node yang tidak memiliki cabang anak disebut daun
Contoh Tree Ada berapa node pada tree diatas? Node manakah yang menjadi root? Ada berapa jumlah node leaf/daun pada tree tersebut?
Contoh Tree Ada berapa node pada tree diatas? 18 Node manakah yang menjadi root? Animal Ada berapa jumlah node leaf/daun pada tree tersebut? 8
Istilah pada Tree
Latihan Predesesor(F)? Succesor(B)? Ancestor (F)? Descendant (B)? Parent (I)? Child (C)? Sibling (G)? Size? Height? Root? Leaf? Degree(C)?
Jawaban Predecesor (F) = A,B,C Succesor(B) = D,E,F,G,H,I Ancestor (F) = C,A Descendant (B) = D, E Parent (I) = H Child (C) = F,G,H Sibling (G) = F,H Size = 9 Height = 4 Root = A Leaf = D,E,F,G,I Degree (C) = 3
Test Pemahaman 1. Apa itu leaf/daun? 2. Apa itu root? 3. Apa itu level pada tree? 4. Apa itu subtree? 5. Apa itu binary tree?
Binary Tree Binary tree = pohon biner Tiap node-nya memiliki maksimal 2 cabang
Contoh tree http://people.cs.ksu.edu/~schmidt/300s05/lectures/week7b.html
Contoh binary tree R S T X Y U V Z W
Contoh Binary Tree
Latihan Cari sebuah contoh kasus yang dapat direpresentasikan dengan tree! Gambarkan bentuk tree dari kasus tersebut sampai level 3! Dari tree yang dibuat tsb, tentukan root, daun, height, size, predesesor, ancestor, descendant, successor, dll. Tentukan apakah tree yang terbentuk termasuk binary tree atau bukan?
Representasi Binary Tree Binary tree dapat direpresentasikan dengan menggunakan array maupun linked list.
Representasi Binary Tree Representasi binary tree menggunakan array (root pada index 0) : H D K B F J L A C E G I 0 1 2 3 4 5 6 7 8 9 10 11
Latihan Representasikan dengan array! a b c d e f g h i j
Jawaban tree[] a b c d e f g h i 0 j 11
Linkedlist Representation root A B C H D I E F J G leftchild element rightchild
Akses Elemen Posisi node dapat ditentukan berdasarkan rumus berikut : Asumsi root dimulai dari index 0 : Anak kiri dari node i berada pada indeks : 2*i+1 Anak kanan dari node i berada pada indeks : 2*i+2 Asumsi root dimulai dari index 1 : Anak kiri dari node i berada pada indeks : 2*i Anak kanan dari node i berada pada indeks : 2*i+1 Struktur Data - Tree 21
Binary Tree Traversal Teknik Penelusuran seluruh node pada binary tree. Ada 4 metode : Preorder Inorder Postorder Level order
PreOrder Traversal Algoritma 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
Preorder Example (visit = print) a b c a b c
Preorder Example (visit = print) a b d e g h i f c j
Preorder Example (visit = print) a b d e g h i f c j a b d g h e i c f j
Preorder Of Expression Tree / * + + - e f a b c d / * + a b - c d + e f Gives prefix form of expression!
Inorder Traversal Algoritma 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
Inorder Example (visit = print) a b c b a c
Inorder Example (visit = print) a b d e g h i f c j
Inorder Example (visit = print) a b d e g h i f c j g d h b e i a f j c
Postorder Traversal Algoritma 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
Postorder Example (visit = print) a b c b c a
Postorder Example (visit = print) a b d e g h i f c j
Postorder Example (visit = print) a b d e g h i f c j g h d i e b j f c a
Postorder Of Expression Tree / * + + - e f a b c d a b + c d - * e f + / Gives postfix form of expression!
BINARY SEARCH TREE (BST)
Binary Search Tree (BST) Disebut juga Ordered Binary tree yaitu binary tree yang seluruh node-nya terurut. Aturan : data pada subtree kiri lebih kecil dari data pada subtree kanan.
Contoh BST Root = 20 20 10 40 6 15 30 2 8 25
Penambahan BST Aturan minmax: Node yang bernilai lebih kecil dari root diletakkan pada subtree sebelah kiri. Node yang bernilai lebih besar diletakkan pada subtree sebelah kanan. Jika ada nilai yang sama maka node tersebut di-overwrite.
Latihan Buatlah sebuah Binary Search Tree berdasarkan proses-proses berikut : insert 17, insert 8, insert 32, insert 6, insert 10, insert 21, insert 45, insert 1, insert 7, insert 9, insert 26, insert 37, insert 50.
Penghapusan BST Ada 3 kasus : Node adalah leaf/daun. Node memiliki degree 1. Node memiliki degree 2.
Penghapusan Node Daun (Node 7) 20 10 40 6 15 30 2 8 18 25 35 7 Remove a leaf element. key = 7
Penhapusan Node Daun (Node 35) 20 10 40 6 15 30 2 8 18 25 35 7 Remove a leaf element. key = 35
Penghapusan Node Ber-degree 1 (Node 40) 20 10 40 6 15 30 2 8 18 25 35 7 Remove from a degree 1 node. key = 40
Penghapusan Node Ber-degree 1 (Node 15) 20 10 40 6 15 30 2 8 18 25 35 7 Remove from a degree 1 node. key = 15
Aturan penghapusan node degree 2 Sebagai pengganti node yang dihapus dengan degree 2. aturannya sbb : ambil node paling besar dari subtree kiri atau ambil node paling kecil dari subtree kanan.
Penghapusan Node Ber-degree 2 (Node 10) 20 10 40 6 15 30 2 8 18 25 35 7 Remove from a degree 2 node. key = 10
Replace with largest key in left subtree (or smallest in right subtree). Remove From A Degree 2 Node 20 10 40 6 15 30 2 8 18 25 35 7
Replace with largest key in left subtree (or smallest in right subtree). Penghapusan Node Ber-degree 2 20 10 40 6 15 30 2 8 18 25 35 7
Replace with largest key in left subtree (or smallest in right subtree). Penghapusan Node Ber-degree 2 20 8 40 6 15 30 2 8 18 25 35 7
Latihan 20 10 40 6 15 30 2 8 18 25 35 7 Remove from a degree 2 node. key = 20
Penghapusan Node Ber-degree 2 20 10 40 6 15 30 2 8 18 25 35 7 Replace with largest in left subtree.
Penghapusan Node Ber-degree 2 20 10 40 6 15 30 2 8 18 25 35 7 Replace with largest in left subtree.
Penghapusan Node Ber-degree 2 18 10 40 6 15 30 2 8 18 25 35 7 Replace with largest in left subtree.
Hasil Akhir 18 10 40 6 15 30 2 8 25 35 7
Implementasi Binary Search Tree
Operasi Binary Search Tree Operasi-operasi yang dilakukan pada binary search tree meliputi : 1. Penambahan node 2. Penghapusan node 3. Pencarian node
Penghapusan: Contoh program Node2P remove(int x, Node2P t) { if (t == null) t=null; if (x < t.data) { t.previous = remove(x, t.previous); } else if (x > t.data) { t.next = remove(x, t.next); } else if (t.previous!= null && t.next!= null) { t.data = findmin(t.next).data; t.next = removemin(t.next); } else { t = (t.previous!= null)? t.previous : t.next; } return t; }
Contoh program Penghapusan node terkecil : Node2P removemin(node2p t) { if (t == null) t=null; } if (t.previous!= null) { t.previous = removemin (t.previous); } else { t = t.next; } return t;
Contoh program Pencarian Node terkecil : Node2P findmin (Node2P t) { if (t == null) t=null; } while (t.previous!= null) { t = t.previous; } return t;
Pustaka Sartaj Sahni, Data Structures & Algorithms, Presentation L20-24. Mitchell Waite, Data Structures & Algorithms in Java, SAMS, 2001