T15-Binary Tree

Download Report

Transcript T15-Binary Tree

Binary Tree Traversal
Definisi
• Penelusuran seluruh node pada binary tree.
• Metode :
– Preorder
– Inorder
– Postorder
– Level order
PreOrder Traversal
•
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
f
e
d
g
c
h
i
a b d g h e i c f j
j
Preorder Of Expression Tree
/
+
*
e
+
a
b
c
d
/ * + a b - c d + e f
Gives prefix form of expression!
f
InOrder Traversal
• 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
f
e
d
g
c
h
i
g d h b e i a f j c
j
Postorder Traversal
• 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
f
e
d
g
c
h
i
g h d i e b j f c a
j
Postorder Of Expression Tree
/
+
*
e
+
a
b
c
d
a b + c d - * e f + /
Gives postfix form of expression!
f
Traversal Applications
a
b
f
e
d
g
c
h
• Make a clone.
• Determine height.
•Determine number of nodes.
i
j
Latihan
• Telusuri pohon biner berikut dengan
menggunakan metode pre, in, post, dan level
traversal.
Latihan 1
+
a.
3
*
b.
5
-
2
/
8
4
Latihan 2
Level-Order Example (visit = print)
a
b
f
e
d
g
c
h
i
a b c d e f g h i j
j
Contoh : Pohon Ekspresi
Implementasi Binary Tree
Operasi pada Binary Tree
• Operas-operasi yang ada pada binary tree
adalah :
1. Deklarasi
2. Pengecekkan kosong (isEmpty)
3. Penambahan node
4. Traversal (penelusuran node)
Deklarasi dengan Array
• Dengan menggunakan array, prosesnya meliputi :
1. Deklarasi class ArrayBinaryTree
2. Deklarasi variabel array
3. Instansiasi variabel array
variabel array digunakan untuk menyimpan node-node yang
membentuk binary tree. Yaitu berupa variabel array 1
dimensi. Tipe variabel array akan menentukan jenis data
yang dapat disimpan pada binary tree.
4. Deklarasi variabel size (untuk menyimpan jumlah
node)
5. Deklarasi variabel last (untuk menyimpan index
node terakhir)
Contoh program
• Deklarasi :
public class ArrayBinaryTree //deklarasi class
{
static Object [] a; //deklarasi array
static int last; //deklarasi last
static int size=0; //deklarasi size
public static void main(String [] args)
{
a = new Integer [15]; //instansiasi*
.....
.....
*contoh : instansiasi variabel array dengan panjang 15 element.
isEmpty dengan Array
• Digunakan untuk mengecek binary tree dalam
kondisi kosong atau terisi node.
• Pengecekan menggunakan variabel size.
• Mengembalikan true jika size =0.
• Mengembalikan false jika nilai size > 0.
Contoh program
• isEmpty :
static boolean isEmpty()
{
return (size==0);
}
Penambahan Node dengan Array
• Penambahan node dengan menggunakan
index.
• Ketika terjadi penambahan node terjadi
increment nilai pada variabel size. Dan
variabel last akan menunjuk index node yang
terakhir kali ditambahkan.
Contoh program
• Penambahan node :
static void addNode(int i, int isi)
{
a[i] = new Integer(isi);
size++;
last = i;
}
Traversal dengan Array
• Penelusuran (traversal) digunakan untuk
menelusuri node pada binary tree satu persatu.
• Terdiri dari 3 metode traversal :
– Pre-order
– In-order
– Post-order
Contoh program
• Penelusuran node (inorder):
public static void inOrder(Object [] theArray, int theLast)
{
a = theArray;
if(!isEmpty())
{
theInOrder(1);
}
else
System.out.println("Binary Tree Kosong");
}
Contoh program
• Program rekursif untuk penelusuran subtree :
static void theInOrder(int i)
{
if (i <= last && a[i]!=null)
{
theInOrder(2 * i);
visit(i);
theInOrder(2 * i + 1);
}
}
Kunjungan Node
• Digunakan untuk mengunjungi node. Proses
yang dilakukan adalah mencetak node untuk
menandai bahwa node tersebut sudah
dikunjungi.
Contoh program
• Kunjungan/visit node :
public static void visit(int i)
{
System.out.print(a[i] + " ");
}
Deklarasi dengan Linked list
• Dengan menggunakan linked list, prosesnya
meliputi :
1. Pembuatan class node (double linked list)
2. Pembuatan class LinkedBinaryTree
3. Deklarasi variabel root bertipe Node
4. Deklarasi variabel size
Contoh program
• Deklarasi :
public class LinkedBTree{
static Node2P root;
static int size=0;
.....
}
isEmpty
• Digunakan untuk mengecek binary tree dalam
kondisi kosong atau tidak.
• Pengecekan dilakukan pada variabel root.
• Jika root menunjuk null, kondisi kosong dan
mengembalikan nilai true. Sebaliknya, jika
root tidak menunjuk null berarti kondisi binary
tree tidak kosong dan mengembalikan nilai
false.
Contoh program
• Pengecekan kosong:
static boolean isEmpty()
{
return (root==null);
}
Penambahan Node
• Untuk melakukan penambahan node, terlebih
dahulu harus di-create node baru.
Node x = new Node(data);
• Ketika ada penambahan node terjadi
increment nilai pada variabel size.
Contoh program
• Penambahan node :
static void addNode(Node2P baru, Node2P kiri, Node2P kanan)
{
baru.next = kanan;
baru.previous = kiri;
size++;
}
setRoot
• Digunakan untuk menandai node mana yang
dijadikan sebagai root.
static void setRoot(Node2P r)
{
root = r;
}
Traversal dengan linkedlist
• Penelusuran (traversal) digunakan untuk
menelusuri node pada binary tree satu persatu.
• Terdiri dari 3 metode traversal :
– Pre-order
– In-order
– Post-order
Contoh program
• Penelusuran node (postOrder) :
static void postOrder()
{
if(!isEmpty())
thePostOrder(root);
else
System.out.println("Binary Tree Kosong");
}
Contoh program
• Program rekursif untuk penelusuran subtree :
static void thePostOrder(Node2P node)
{
if(node != null)
{
thePostOrder(node.previous);
thePostOrder(node.next);
System.out.print(node.data + "
}
}
");
Contoh program
• Penelusuran node (level order) :
static void theLevelOrder(Node2P node)
{
QueueArray temp = new QueueArray();
temp.inisialisasi(15);
temp.enqueue(node);
while(temp.jumlah_item >0)
{
if(node.previous !=null)
temp.enqueue(node.previous);
if(node.next !=null)
temp.enqueue(node.next);
System.out.print(node.data + " ");
if(!temp.isEmpty())
{
temp.dequeue();
node = temp.peekQueue();
}
}
}
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
Penambahan BST
• Penambahan node pada BST harus mengikuti
aturan minMax, dimana node yang bernilai
lebih kecil dari root diletakkan pada subtree
sebelah kiri sedangkan node yang bernilai
lebih besar diletakkan pada subtree sebelah
kanan. Jika ada nilai yang sama maka node
tersebut di-overwrite.
Contoh program
• Penambahan :
Node2P insert(int x, Node2P t)
{
if (t == null) {
t = new Node2P (x, null, null);
} else if (x < t.data) {
t.previous = insert (x, t.previous);
} else if (x > t.data) {
t.next = insert (x, t.next);
} else {
t=t;
}
return t;
}
Penghapusan BST
Ada 3 kasus :
 Elemen ada di leaf/daun.
 Elemen yang memiliki degree 1.
 Elemen yang memiliki degree 2.
Penghapusan Node Daun (Node 7)
20
10
6
2
40
15
8
30
18
25
35
7
Remove a leaf element. key = 7
Penhapusan Node Daun (Node 35)
20
10
6
2
40
15
8
30
18
25
35
7
Remove a leaf element. key = 35
Penghapusan Node Ber-degree 1
20
10
6
2
40
15
8
30
18
25
35
7
Remove from a degree 1 node. key = 40
Penghapusan Node Ber-degree 1
20
10
6
2
40
15
8
30
18
25
35
7
Remove from a degree 1 node. key = 15
Penghapusan Node Ber-degree 2
20
10
6
2
40
15
8
30
18
25
35
7
Remove from a degree 2 node. key = 10
Remove From A Degree 2 Node
20
10
6
2
40
15
8
30
18
25
35
7
Replace with largest key in left subtree (or
smallest in right subtree).
Penghapusan Node Ber-degree 2
20
10
6
2
40
15
8
30
18
25
35
7
Replace with largest key in left subtree (or
smallest in right subtree).
Penghapusan Node Ber-degree 2
20
8
6
2
40
15
8
30
18
25
35
7
Replace with largest key in left subtree (or
smallest in right subtree).
Latihan
20
10
6
2
40
15
8
30
18
25
35
7
Remove from a degree 2 node. key = 20
Penghapusan Node Ber-degree 2
20
10
6
2
40
15
8
30
18
25
35
7
Replace with largest in left subtree.
Penghapusan Node Ber-degree 2
20
10
6
2
40
15
8
30
18
25
35
7
Replace with largest in left subtree.
Penghapusan Node Ber-degree 2
18
10
6
2
40
15
8
30
18
25
35
7
Replace with largest in left subtree.
Hasil Akhir
18
10
6
2
15
8
7
40
30
25
35
Contoh program
• Penghapusan:
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