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

Tugas Algoritma Tree Traversal Preorder

The document describes Java code to perform tree traversal (preorder, inorder, postorder) on a binary tree. It defines a BinaryTree class with TreeNode nodes. The main method creates a sample binary tree and calls the preorder, postorder, and inorder traversal methods to print the node values in different orders. The traversal methods recursively visit the nodes by printing values before or after traversing left and right subtrees.
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)
63 views4 pages

Tugas Algoritma Tree Traversal Preorder

The document describes Java code to perform tree traversal (preorder, inorder, postorder) on a binary tree. It defines a BinaryTree class with TreeNode nodes. The main method creates a sample binary tree and calls the preorder, postorder, and inorder traversal methods to print the node values in different orders. The traversal methods recursively visit the nodes by printing values before or after traversing left and right subtrees.
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

Tugas Algoritma Tree Traversal Preorder, Inorder, dan Postorder

Nama kelompok : 20411007_Elen lika nggji


20411022_Shintia Damayanti Putri

Coding Java Binary Tree (Preorder, Inorder, Postorder)


package binarytree;
/**
*
*
*/
import java.util.Stack;
public class BinaryTree4 {
public static class TreeNode
{
int data;
TreeNode left;
TreeNode right;
TreeNode(char data)
{
this.data=data;
}
}
// Recursive Solution
public void preorder(TreeNode root) {
if(root != null) {
//Visit the node-Printing the node data
System.out.printf("%c ",root.data);
preorder(root.left);
preorder(root.right);
}
}
// Recursive Solution
public void postOrder(TreeNode root) {
if(root != null) {
postOrder(root.left);
postOrder(root.right);
//Visit the node by Printing the node data
System.out.printf("%c ",root.data);
}
}
// Recursive Solution
public void inOrder(TreeNode root) {
if(root != null) {
inOrder(root.left);
//Visit the node by Printing the node data
System.out.printf("%c ",root.data);
inOrder(root.right);
}
}
public static void main(String[] args)
{
BinaryTree4 bi=new BinaryTree4();
// Creating a binary tree
TreeNode rootNode=createBinaryTree();
System.out.println("Tampilan PreOrder Tree :");
bi.preorder(rootNode);
System.out.println();
System.out.println("-------------------------");
System.out.println("Tampilan PostOrder Tree :");
bi.postOrder(rootNode);
System.out.println();
System.out.println("-------------------------");
System.out.println("Tampilan InOrder Tree:");
bi.inOrder(rootNode);
}
public static TreeNode createBinaryTree()
{
TreeNode rootNode =new TreeNode('A');
TreeNode nodeB=new TreeNode('B');
TreeNode nodeC=new TreeNode('C');
TreeNode nodeD=new TreeNode('D');
TreeNode nodeE=new TreeNode('E');
TreeNode nodeF=new TreeNode('F');
TreeNode nodeG=new TreeNode('G');
TreeNode nodeH=new TreeNode('H');
TreeNode nodeI=new TreeNode('I');
TreeNode nodeJ=new TreeNode('J');

rootNode.left=nodeB;
rootNode.right=nodeC;
nodeB.left=nodeD;
nodeB.right=nodeE;
nodeC.left=nodeF;
nodeD.left=nodeG;
nodeD.right=nodeH;
nodeE.left=nodeI;
nodeF.right=nodeJ;
return rootNode;
}
}
Running :
Tampilan PreOrder Tree :
ABDGHEICFJ
-------------------------
Tampilan PostOrder Tree :
GHDIEBJFCA
-------------------------
Tampilan InOrder Tree:
G D H B I E A F J C BUILD SUCCESSFUL (total time: 0 seconds)

You might also like