201410370311140_Bayu Firmansyah_ASD 4

Embed Size (px)

DESCRIPTION

bmjbvkj

Citation preview

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    Pengampu :

    [Yufis Azhar, Skom]

    Nama:

    [201410370311140] [Bayu Firmansyah[

    Laporan Praktikum Algoritma &

    Struktur Data

    Modul [4]

    [BINARY TREE & BINARY SEARCH

    TREE (BST)]

    Daftar Isi :

    1. Deskripsi Praktikum

    2. Perangkat Lunak

    3. Teori Penunjang

    4. Prosedur Pelaksanaan

    5. Hasil Soal Latihan Dan Penjelasan

    6. Implementasi dan Hasil Praktikum

    7. Kesimpulan

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    Daftar Isi:

    1. Deskripsi Praktikum ...................................................................................................................... 3

    2. Perangkat Lunak ............................................................................................................................ 3

    3. Teori Penunjang ............................................................................................................................. 3

    4. Prosedur Pelaksanaan .................................................................................................................... 4

    6. Implementasi dan Hasil Praktikum ............................................................................................ 19

    7. Kesimpulan ................................................................................................................................... 30

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    1. Deskripsi Praktikum

    Mahasiswa mampu :

    1. Memahami struktur pohon tree sebagai model penyimpanan data.

    2. Memahami cara menyimpan dan mengakses elemen dari sebuah struktur pohon biner.

    3. Memahami struktur pohon biner dalam versi linked list.

    4. Memahami operasi-operasi standar yang terkait dengan pohon biner.

    2. Perangkat Lunak

    1. Java JDK

    2. Netbeans

    3. Teori Penunjang

    Tree adalah kumpulan element yang saling terhubung secara hirarki (one to many). Tree

    terdiri dari root (akar), path (cabang), dan leaf (daun). Element pada tree yaitu berupa node.

    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.

    Root (Node Root) adalah node yang memiliki hirarki tertinggi, yang pertama kali dibentuk

    sehingga tidak memiliki parent (node induk). Penelusuran path tiap node dimulai dari root.

    Subtree adalah node-node lain dibawah root yang saling terhubung satu sama lain secara

    hirarki.

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    Path merupakan percabangan dari satu node ke node lainnya. Setiap node dapat memiliki

    lebih dari satu cabang node atau tidak sama sekali memiliki cabang.

    Leaf adalah node pada tree yang terletak pada pangkal dan tidak memiliki cabang ke

    element node lainnya. Dalam artian leaf terdapat pada hirarki terbawah pada sebuah tree.

    Istilah pada Tree antara lain:

    4. Prosedur Pelaksanaan

    Prosedur pelaksanaan praktikum adalah sebagai berikut :

    1. Mahasiswa mencoba latihan yang ada pada modul praktikum

    2. Mahasiswa menganalisa hasil dari program pada latihan yang telah dijalankan

    3. Mahasiswa mengerjakan tugas latihan dan praktikum yang diberikan

    4. Mahasiswa mendemonstrasikan program tugas praktikum yang telah

    dikerjakan pada dosen/assisten

    5. Mahasiswa membuat laporan dari tugas yang telah dikerjakan

    6. Upload laporan melalui e-labit.umm.ac.id.

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    5. Hasil Soal Latihan Dan Penjelasan

    1. Interface BinaryTree

    import java.lang.reflect.*;

    public interface BinaryTree

    {

    public boolean isEmpty();

    public Object root();

    public void makeTree(Object root, Object left, Object right);

    public BinaryTree removeLeftSubtree();

    public BinaryTree removeRightSubtree();

    public void preOrder(Method visit);

    public void inOrder(Method visit);

    public void postOrder(Method visit);

    public void levelOrder(Method visit);

    }

    2. Class BinaryTreeNode

    public class BinaryTreeNode

    {

    // package visible data members

    Object element;

    BinaryTreeNode leftChild; // left subtree

    BinaryTreeNode rightChild; // right subtree

    // constructors

    public BinaryTreeNode() {}

    public BinaryTreeNode(Object theElement)

    {element = theElement;}

    public BinaryTreeNode(Object theElement,BinaryTreeNode

    theleftChild,BinaryTreeNode therightChild)

    {

    element = theElement;

    leftChild = theleftChild;

    rightChild = therightChild;

    }

    // accessor methods

    public BinaryTreeNode getLeftChild() {return leftChild;}

    public BinaryTreeNode getRightChild() {return rightChild;}

    public Object getElement() {return element;}

    // mutator methods

    public void setLeftChild(BinaryTreeNode theLeftChild)

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    {leftChild = theLeftChild;}

    public void setRightChild(BinaryTreeNode theRightChild)

    {rightChild = theRightChild;}

    public void setElement(Object theElement)

    {element = theElement;}

    // output method

    public String toString()

    {return element.toString();}

    }

    3. Class LinkedBinaryTree

    import java.lang.reflect.*;

    public class LinkedBinaryTree implements BinaryTree

    {

    // instance data member

    BinaryTreeNode root; // root node

    // class data members

    static Method visit; // visit method to use during a traversal

    static Object [] visitArgs = new Object [1];

    // parameters of visit method

    static int count; // counter

    static Class [] paramType = {BinaryTreeNode.class};

    // type of parameter for visit

    static Method theAdd1; // method to increment count by 1

    static Method theOutput; // method to output node element

    // method to initialize class data members

    static

    {

    try

    {

    Class lbt = LinkedBinaryTree.class;

    theAdd1 = lbt.getMethod("add1", paramType);

    theOutput = lbt.getMethod("output", paramType);

    }

    catch (Exception e) {}

    // exception not possible

    }

    // only default constructor available

    // class methods

    /** visit method that outputs element */

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    public static void output(BinaryTreeNode t)

    {System.out.print(t.element + " ");}

    /** visit method to count nodes */

    public static void add1(BinaryTreeNode t)

    {count++;}

    // instance methods

    /** @return true iff tree is empty */

    public boolean isEmpty()

    {return root == null;}

    /** @return root element if tree is not empty

    * @return null if tree is empty */

    public Object root()

    {return (root == null) ? null : root.element;}

    /** set this to the tree with the given root and subtrees

    * CAUTION: does not clone left and right */

    public void makeTree(Object root, Object left, Object right)

    {

    this.root = new BinaryTreeNode(root,

    ((LinkedBinaryTree) left).root,

    ((LinkedBinaryTree) right).root);

    }

    /** remove the left subtree

    * @throws IllegalArgumentException when tree is empty

    * @return removed subtree */

    public BinaryTree removeLeftSubtree()

    {

    if (root == null)

    throw new IllegalArgumentException("tree is empty");

    // detach left subtree and save in leftSubtree

    LinkedBinaryTree leftSubtree = new LinkedBinaryTree();

    leftSubtree.root = root.leftChild;

    root.leftChild = null;

    return (BinaryTree) leftSubtree;

    }

    /** remove the right subtree

    * @throws IllegalArgumentException when tree is empty

    * @return removed subtree */

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    public BinaryTree removeRightSubtree()

    {

    if (root == null)

    throw new IllegalArgumentException("tree is empty");

    // detach right subtree and save in rightSubtree

    LinkedBinaryTree rightSubtree = new LinkedBinaryTree();

    rightSubtree.root = root.rightChild;

    root.rightChild = null;

    return (BinaryTree) rightSubtree;

    }

    /** preorder traversal */

    public void preOrder(Method visit)

    {

    this.visit = visit;

    thePreOrder(root);

    }

    /** actual preorder traversal method */

    static void thePreOrder(BinaryTreeNode t)

    {

    if (t != null)

    {

    visitArgs[0] = t;

    try {visit.invoke(null, visitArgs);} // visit tree root

    catch (Exception e)

    {System.out.println(e);}

    thePreOrder(t.leftChild); // do left subtree

    thePreOrder(t.rightChild); // do right subtree

    }

    }

    /** inorder traversal */

    public void inOrder(Method visit)

    {

    this.visit = visit;

    theInOrder(root);

    }

    /** actual inorder traversal method */

    static void theInOrder(BinaryTreeNode t)

    {

    if (t != null)

    {

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    theInOrder(t.leftChild); // do left subtree

    visitArgs[0] = t;

    try {visit.invoke(null, visitArgs);} // visit tree root

    catch (Exception e)

    {System.out.println(e);}

    theInOrder(t.rightChild); // do right subtree

    }

    }

    /** postorder traversal */

    public void postOrder(Method visit)

    {

    this.visit = visit;

    thePostOrder(root);

    }

    /** actual postorder traversal method */

    static void thePostOrder(BinaryTreeNode t)

    {

    if (t != null)

    {

    thePostOrder(t.leftChild); // do left subtree

    thePostOrder(t.rightChild); // do right subtree

    visitArgs[0] = t;

    try {visit.invoke(null, visitArgs);} // visit tree root

    catch (Exception e)

    {System.out.println(e);}

    }

    }

    /** level order traversal */

    public void levelOrder(Method visit)

    {

    ArrayQueue q = new ArrayQueue();

    BinaryTreeNode t = root;

    while (t != null)

    {

    visitArgs[0] = t;

    try {visit.invoke(null, visitArgs);} // visit tree root

    catch (Exception e)

    {System.out.println(e);}

    // put t's children on queue

    if (t.leftChild != null)

    q.put(t.leftChild);

    if (t.rightChild != null)

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    q.put(t.rightChild);

    // get next node to visit

    t = (BinaryTreeNode) q.remove();

    }

    }

    /** output elements in preorder */

    public void preOrderOutput()

    {preOrder(theOutput);}

    /** output elements in inorder */

    public void inOrderOutput()

    {inOrder(theOutput);}

    /** output elements in postorder */

    public void postOrderOutput()

    {postOrder(theOutput);}

    /** output elements in level order */

    public void levelOrderOutput()

    {levelOrder(theOutput);}

    /** count number of nodes in tree */

    public int size()

    {

    count = 0;

    preOrder(theAdd1);

    return count;

    }

    /** test program */

    public static void main(String [] args)

    {

    LinkedBinaryTree a = new LinkedBinaryTree(),

    x = new LinkedBinaryTree(),

    y = new LinkedBinaryTree(),

    z = new LinkedBinaryTree();

    y.makeTree(new Integer(1), a, a);

    z.makeTree(new Integer(2), a, a);

    x.makeTree(new Integer(3), y, z);

    y.makeTree(new Integer(4), x, a);

    System.out.println("Preorder sequence is ");

    y.preOrderOutput();

    System.out.println();

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    System.out.println("Inorder sequence is ");

    y.inOrderOutput();

    System.out.println();

    System.out.println("Postorder sequence is ");

    y.postOrderOutput();

    System.out.println();

    System.out.println("Level order sequence is ");

    y.levelOrderOutput();

    System.out.println();

    System.out.println("Number of nodes = " + y.size());

    }

    }

    Output LinkedBinaryTree :

    4. Class ArrayBinaryTree

    public class ArrayBinaryTree

    {

    // data members

    static Object [] a; // array that contains the tree

    static int last; // position of last element in array a

    /** visit method that prints the element in a[i] */

    public static void visit(int i)

    {System.out.print(a[i] + " ");}

    /** inorder traversal */

    public static void inOrder(Object [] theArray, int theLast)

    {

    // set static data members

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    a = theArray;

    last = theLast;

    // start the recursive traversal method at the root

    theInOrder(1);

    }

    /** actual method to do the inorder traversal */

    static void theInOrder(int i)

    {// traverse subtree rooted at a[i]

    if (i

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    Output ArrayBinaryTree.java

    5. Class BinarySearchTree

    /** binary search tree */

    //package dataStructures;

    public class BinarySearchTree extends

    LinkedBinaryTree

    {

    // top-level nested class

    static class Data

    {

    // data members

    Object element; // element in node

    Comparable key; // its key

    // constructor

    Data(Comparable theKey, Object

    theElement)

    {

    key = theKey;

    element = theElement;

    }

    public String toString()

    {return element.toString();}

    }

    /** @return element with specified key

    * @return null if no matching element */

    public Object get(Object theKey)

    {

    // pointer p starts at the root and moves

    through

    // the tree looking for an element with

    key theKey

    BinaryTreeNode p = root;

    Comparable searchKey = (Comparable)

    theKey;

    while (p != null)

    // examine p.element.key

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    if (searchKey.compareTo(((Data)

    p.element).key) < 0)

    p = p.leftChild;

    else

    if (searchKey.compareTo(((Data)

    p.element).key) > 0)

    p = p.rightChild;

    else // found matching element

    return ((Data) p.element).element;

    // no matching element

    return null;

    }

    /** insert an element with the specified

    key

    * overwrite old element if there is already

    an

    * element with the given key

    * @return old element (if any) with key

    theKey */

    public Object put(Object theKey, Object

    theElement)

    {

    BinaryTreeNode p = root; // search

    pointer

    BinaryTreeNode pp = null; //

    parent of p

    Comparable elementKey = (Comparable)

    theKey;

    // find place to insert theElement

    while (p != null)

    {// examine p.element.key

    pp = p;

    // move p to a child

    if (elementKey.compareTo(((Data)

    p.element).key) < 0)

    p = p.leftChild;

    else if (elementKey.compareTo(((Data)

    p.element).key) > 0)

    p = p.rightChild;

    else

    {// overwrite element with same key

    Object elementToReturn = ((Data)

    p.element).element;

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    ((Data) p.element).element =

    theElement;

    return elementToReturn;

    }

    }

    // get a node for theElement and attach

    to pp

    BinaryTreeNode r = new BinaryTreeNode

    (new Data(elementKey,

    theElement));

    if (root != null)

    // the tree is not empty

    if (elementKey.compareTo(((Data)

    pp.element).key) < 0)

    pp.leftChild = r;

    else

    pp.rightChild = r;

    else // insertion into empty tree

    root = r;

    return null;

    }

    /** @return matching element and

    remove it

    * @return null if no matching element */

    public Object remove(Object theKey)

    {

    Comparable searchKey = (Comparable)

    theKey;

    // set p to point to node with key

    searchKey

    BinaryTreeNode p = root, // search

    pointer

    pp = null; // parent of p

    while (p != null && !((Data)

    p.element).key.equals(searchKey))

    {// move to a child of p

    pp = p;

    if (searchKey.compareTo(((Data)

    p.element).key) < 0)

    p = p.leftChild;

    else

    p = p.rightChild;

    }

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    if (p == null) // no element with key

    searchKey

    return null;

    // save element to be removed

    Object theElement = ((Data)

    p.element).element;

    // restructure tree

    // handle case when p has two children

    if (p.leftChild != null && p.rightChild !=

    null)

    {// two children

    // convert to zero or one child case

    // find element with largest key in left

    subtree of p

    BinaryTreeNode s = p.leftChild,

    ps = p; // parent of s

    while (s.rightChild != null)

    {// move to larger element

    ps = s;

    s = s.rightChild;

    }

    // move largest element from s to p

    p.element = s.element;

    p = s;

    pp = ps;

    }

    // p has at most one child, save this child

    in c

    BinaryTreeNode c;

    if (p.leftChild == null)

    c = p.rightChild;

    else

    c = p.leftChild;

    // remove node p

    if (p == root) root = c;

    else

    {// is p left or right child of pp?

    if (p == pp.leftChild)

    pp.leftChild = c;

    else

    pp.rightChild = c;

    }

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    return theElement;

    }

    /** output elements in ascending order of

    key */

    public void ascend()

    {inOrderOutput();}

    // test binary search tree class

    public static void main(String [] args)

    {

    BinarySearchTree y = new

    BinarySearchTree();

    // insert a few elements

    y.put(new Integer(1), new

    Character('a'));

    y.put(new Integer(6), new Character('c'));

    y.put(new Integer(4), new

    Character('b'));

    y.put(new Integer(8), new

    Character('d'));

    System.out.println("Elements in

    ascending order are");

    y.ascend();

    System.out.println();

    // remove an element

    System.out.println("Removed element "

    + y.remove(new Integer(4))

    +

    " with key 4");

    System.out.println("Elements in

    ascending order are");

    y.ascend();

    System.out.println();

    // remove another element

    System.out.println("Removed element "

    + y.remove(new Integer(8))

    +

    " with key 8");

    System.out.println("Elements in

    ascending order are");

    y.ascend();

    System.out.println();

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    // remove yet another element

    System.out.println("Removed element "

    + y.remove(new Integer(6))

    +

    " with key 6");

    System.out.println("Elements in

    ascending order are");

    y.ascend();

    System.out.println();

    // try to remove a nonexistent element

    System.out.println("Removed element "

    + y.remove(new Integer(6))

    +

    " with key 6");

    System.out.println("Elements in

    ascending order are");

    y.ascend();

    System.out.println();

    }

    }

    output class BinarySearchTree :

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    6. Implementasi dan Hasil Praktikum

    no 1 kelas ArrayBinaryTree

    /*

    * To change this license header, choose License Headers in Project Properties.

    * To change this template file, choose Tools | Templates

    * and open the template in the editor.

    */

    package algodat4;

    /**

    *

    * @author bayu

    */

    public class ArrayBinaryTree

    {

    // data members

    static Object [] a; // array that contains the tree

    static int last; // position of last element in array a

    /** visit method that prints the element in a[i] */

    public static void visit(int i)

    {System.out.print(a[i] + " ");}

    /** inorder traversal */

    public static void inOrder(Object [] theArray, int theLast)

    {

    // set static data members

    a = theArray;

    last = theLast;

    // start the recursive traversal method at the root

    theInOrder(1);

    }

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    /** actual method to do the inorder traversal */

    static void theInOrder(int i)

    {// traverse subtree rooted at a[i]

    if (i

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    }

    public static void postOrder(Object [] theArray, int theLast)

    {

    // set static data members

    a = theArray;

    last = theLast;

    // start the recursive traversal method at the root

    thePostOrder(1);

    }

    static void thePostOrder(int i)

    {// traverse subtree rooted at a[i]

    if (i

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    a[5] = new Integer(5);

    System.out.println("The elements in inorder are");

    inOrder(a, 11);

    System.out.println("\nThe elements in postorder are");

    postOrder(a, 11);

    System.out.println("\nThe elements in preorder are");

    preOrder(a, 11);

    System.out.println();

    }

    }

    No 2 kelas ArrayBinary

    /*

    * To change this license header, choose License Headers in Project Properties.

    * To change this template file, choose Tools | Templates

    * and open the template in the editor.

    */

    package algodat4;

    /*

    * To change this license header, choose License Headers in Project Properties.

    * To change this template file, choose Tools | Templates

    * and open the template in the editor.

    */

    import java.util.Scanner;

    /**

    *

    * @author embah,uyab,sinyo

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    */

    public class ArrayBinary {

    static String[] a;

    static int last;

    public static void visit(int i) {

    if (a[i] != null) {

    System.out.print(a[i] + ", ");

    }

    }

    // Anak ke-X dari orang tua Y

    public static void pilihan_1(int anak, String parent, String[] theArray, int theLast) {

    try {

    a = theArray;

    last = theLast;

    int z = 0;

    for (int x = 1; x

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    }

    }

    // Orang tua dari anak ke-X

    public static void pilihan_2(String anak, String[] theArray, int theLast) {

    try {

    a = theArray;

    last = theLast;

    for (int x = 1; x

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    visit(4 * x);

    visit(4 * x + 1);

    }

    }

    }

    public static void rumus(int n2) {

    visit(4 * n2 - 2);

    visit(4 * n2 - 1);

    visit(4 * n2);

    visit(4 * n2 + 1);

    }

    // Cucu dari orang tua Y

    public static void pilihan_4(String parent, String[] theArray, int theLast) {

    try {

    a = theArray;

    last = theLast;

    int n1;

    for (int x = 1; x

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    }

    // Semua anggota keluarga

    public static void pilihan_5(String[] theArray, int theLast) {

    a = theArray;

    last = theLast;

    for (int x = 2; x

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    System.out.println("4. Cucu dari orang tua Y");

    System.out.println("5. Semua anggota keluarga");

    System.out.println("6. Keluar");

    System.out.println("===============================================

    =====");

    boolean kondisi = true;

    while (kondisi) {

    System.out.print("Masukkan pilihan : ");

    int pil = in.nextInt();

    if (pil == 1) {

    System.out.print("Nama orang tua : ");

    String orangtua = in.next();

    System.out.print("Anak nomor : ");

    int anak = in.nextInt();

    System.out.print("Anak ke-" + anak + " dari " + orangtua + " adalah ");

    pilihan_1(anak, orangtua, b, 58);

    } else if (pil == 2) {

    System.out.print("Nama anak : ");

    String anak = in.next();

    System.out.print("Orang tua dari " + anak + " adalah ");

    pilihan_2(anak, b, 58);

    } else if (pil == 3) {

    System.out.print("Nama orang tua : ");

    String orangtua = in.next();

    System.out.print("Semua anak dari " + orangtua + " adalah ");

    pilihan_3(orangtua, b, 58);

    } else if (pil == 4) {

    System.out.print("Nama orang tua : ");

    String orangtua = in.next();

    System.out.print("Cucu dari " + orangtua + " adalah ");

    pilihan_4(orangtua, b, 58);

    } else if (pil == 5) {

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    System.out.println("Semua anggota keluarga : ");

    pilihan_5(b, 58);

    } else if (pil == 6) {

    System.out.println("---------------------------------------------------------");

    System.exit(0);

    kondisi = false;

    } else {

    System.out.println("Maaf inputan salah !!");

    }

    System.out.println("\n.........................................................");

    }

    }

    }

    1

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    2

    1. Tambah postorder dan preorder pada latihan array binary tree

    a. Menambahkan postorder dan preorder pada latihan array binary tree

    b. Postorder :

    Kunjungi cabang kiri

    Kunjungi cabang kanan

    Cetak isi simpul yang di kunjungi

    c. Preorder:

    Cetak isi simpul yang di kunjungi

    Kunjungi cabang kiri

    Kunjungi cabang kanan

    d. Selesai.

  • Dokumen Laboratorium Teknik Informatika UMM @ 2015 Laporan Modul Praktikum Algoritma dan Struktur Data By. [201410370311140] [Bayu Firmansyah]

    2. Modifikasi program binary tree pada latihan

    untuk menyimpan nama-nama dari silsilah keluarga dan dapat mengenali pola seperti

    berikut :

    1) Anak ke-X dari orang tua Y

    2) Orang tua dari anak ke-X

    3) Semua anak dari orang tua Y

    4) Cucu dari orang tua Y

    5) Semua anggota keluarga

    7. Kesimpulan

    Tree adalah kumpulan element yang saling terhubung secara hirarki (one to many). Tree terdiri

    dari root (akar), path (cabang), dan leaf (daun). Element pada tree yaitu berupa node. 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. Root (Node Root) adalah node yang memiliki hirarki tertinggi,

    yang pertama kali dibentuk sehingga tidak memiliki parent (node induk). Penelusuran path

    tiap node dimulai dari root. Subtree adalah node-node lain dibawah root yang saling terhubung

    satu sama lain secara hirarki. Path merupakan percabangan dari satu node ke node lainnya.

    Setiap node dapat memiliki lebih dari satu cabang node atau tidak sama sekali memiliki cabang.

    Leaf adalah node pada tree yang terletak pada pangkal dan tidak memiliki cabang ke element

    node lainnya. Dalam artian leaf terdapat pada hirarki terbawah pada sebuah tree.