//utility class for node
public class node{
public int data;
public node left,right;
public node(){
data=0;
left=right=null;
}
}
//class to implement the algorithm
import java.util.Scanner;
public class treeTraversals {
static Scanner in = new Scanner(System.in);
static node inputTree(){
node temp=new node();
System.out.print("Enter the value: ");
temp.data=in.nextInt();
System.out.print("Enter y if the node "+temp.data+" has left subtree: ");
if(in.next().charAt(0)=='y')
temp.left=inputTree();
else
temp.left=null;
System.out.print("Enter y if the node "+temp.data+" has right subtree: ");
if(in.next().charAt(0)=='y')
temp.right=inputTree();
else
temp.right=null;
return temp;
}
static void inorder(node tree){
if(tree==null)
return;
inorder(tree.left);
System.out.print(tree.data+"\t");
inorder(tree.right);
}
static void preorder(node tree){
if(tree==null)
return;
System.out.print(tree.data+"\t");
preorder(tree.left);
preorder(tree.right);
}
static void postorder(node tree){
if(tree==null)
return;
postorder(tree.left);
postorder(tree.right);
System.out.print(tree.data+"\t");
}
public static void main(String[] args) {
System.out.println("Enter the tree: ");
node tree = new node();
tree = inputTree();
System.out.println("Inorder traversal ");
inorder(tree);
System.out.println("\nPreorder traversal ");
preorder(tree);
System.out.println("\nPostorder traversal ");
postorder(tree);
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.