0% found this document useful (0 votes)
54 views4 pages

Laporan Praktikum Binary Tree

The document describes a Java program that implements binary tree data structures and traversal methods. It includes code for Node and BinaryTree classes that can build sample trees from characters and calculate their height and preorder traversal. The main method tests the trees by printing their preorder traversal and height.

Uploaded by

Rafiq Muzakki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views4 pages

Laporan Praktikum Binary Tree

The document describes a Java program that implements binary tree data structures and traversal methods. It includes code for Node and BinaryTree classes that can build sample trees from characters and calculate their height and preorder traversal. The main method tests the trees by printing their preorder traversal and height.

Uploaded by

Rafiq Muzakki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Algoritma dan Struktur Data

Resmi

Praktikum Binary Tree


-

Praditya Nafiis Muhammad

2 D3 IT B

2103181032

Yuliana Setyowati

28 November 2019
A. Listing Program
 TNode.java
public class TNode<T> {
public T nodeValue;
public TNode<T> left, right;

public TNode(T item)


{
nodeValue = item;
left = right = null;
}

public TNode (T item, TNode<T> left, TNode<T> right)


{
nodeValue = item;
this.left = left;
this.right = right;
}
}

 BinaryTree.java
public class BinaryTree<T> {

public static TNode<Character> buildTree() {


TNode<Character> a, b, c, d, kali, plus, bagi;
a = new TNode<Character>('A');
b = new TNode<Character>('B');
c = new TNode<Character>('C');
d = new TNode<Character>('D');
plus = new TNode<Character>('+', a, b);
kali = new TNode<Character>('*', plus, c);
bagi = new TNode<Character>('/', kali, d);

return bagi;
}

public static TNode<Character> buildTree2() {


TNode<Character> a, b, c, d, e,f,g;

d = new TNode<Character>('D');
c = new TNode<Character>('C');
g = new TNode<Character>('G');
e = new TNode<Character>('E', d, null);
b = new TNode<Character>('B', c, e);
f = new TNode<Character>('F', null, g);
a = new TNode<Character>('A', b, f);

return a;
}
public static <T> int height(TNode<T> t) {
int heightLeft = 0, heightRight = 0, heightval = 0;

if (t == null) // tinggi dari tree kosong adalah -1


{
heightval = -1;
} else {
heightLeft = height(t.left);
heightRight = height(t.right);
heightval = 1 + (heightLeft > heightRight ? heightLeft : heightRight);
}
return heightval;
}
public static <T> String preorderDisplay(TNode<T> t) {
String s = "";

if (t != null) {
s += t.nodeValue + " "; // display the node
s += preorderDisplay(t.left); // descend left
s += preorderDisplay(t.right); // descend right
}
return s;
}
}

 Main.java
package percobaan1;
public class Main2 {

public static void main(String[] args) {

System.out.println("Notasi Aritmatika");

TNode<Character> root1 = BinaryTree.buildTree4();


System.out.println("Preorder = " + BinaryTree.postorderDisplay(root1));
System.out.println("Height = " + BinaryTree.height(root1));

System.out.println("\nNotasi Huruf");

TNode<Character> root2 = BinaryTree.buildTree();


System.out.println("Preorder = " + BinaryTree.postorderDisplay(root2));
System.out.println("Height = " + BinaryTree.height(root2));
}
}
B. Output Program

You might also like