ALGORITHM:
We have 3 types of tree traversals
- Preorder traversal
- Inorder traversal
- Postorder traversal
- In preorder traversal a node is visited then its left and right subtree is visite.
- In inorder traversal a node is visited after visiting its left subtree then it passes to the right subtree
- In postorder traversal a node is visited after its left and right subtree is visited or either of it or both are null.
CODE:
//utility class for node
public class node{
public int data;
public node left,right;
public node(){
data=0;
left=right=null;
}
}
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);
}
}
Enter the tree:
Enter the value: 2
Enter y if the node 2 has left subtree: y
Enter the value: 7
Enter y if the node 7 has left subtree: y
Enter the value: 2
Enter y if the node 2 has left subtree: n
Enter y if the node 2 has right subtree: n
Enter y if the node 7 has right subtree: y
Enter the value: 6
Enter y if the node 6 has left subtree: y
Enter the value: 5
Enter y if the node 5 has left subtree: n
Enter y if the node 5 has right subtree: n
Enter y if the node 6 has right subtree: y
Enter the value: 11
Enter y if the node 11 has left subtree: n
Enter y if the node 11 has right subtree: n
Enter y if the node 2 has right subtree: y
Enter the value: 5
Enter y if the node 5 has left subtree: n
Enter y if the node 5 has right subtree: y
Enter the value: 9
Enter y if the node 9 has left subtree: y
Enter the value: 4
Enter y if the node 4 has left subtree: n
Enter y if the node 4 has right subtree: n
Enter y if the node 9 has right subtree: n
Inorder traversal
2 7 5 6 11 2 5 4 9
Preorder traversal
2 7 2 6 5 11 5 9 4
Postorder traversal
2 5 11 6 7 4 9 5 2
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);
}
}
OUTPUT:
Enter the tree:
Enter the value: 2
Enter y if the node 2 has left subtree: y
Enter the value: 7
Enter y if the node 7 has left subtree: y
Enter the value: 2
Enter y if the node 2 has left subtree: n
Enter y if the node 2 has right subtree: n
Enter y if the node 7 has right subtree: y
Enter the value: 6
Enter y if the node 6 has left subtree: y
Enter the value: 5
Enter y if the node 5 has left subtree: n
Enter y if the node 5 has right subtree: n
Enter y if the node 6 has right subtree: y
Enter the value: 11
Enter y if the node 11 has left subtree: n
Enter y if the node 11 has right subtree: n
Enter y if the node 2 has right subtree: y
Enter the value: 5
Enter y if the node 5 has left subtree: n
Enter y if the node 5 has right subtree: y
Enter the value: 9
Enter y if the node 9 has left subtree: y
Enter the value: 4
Enter y if the node 4 has left subtree: n
Enter y if the node 4 has right subtree: n
Enter y if the node 9 has right subtree: n
Inorder traversal
2 7 5 6 11 2 5 4 9
Preorder traversal
2 7 2 6 5 11 5 9 4
Postorder traversal
2 5 11 6 7 4 9 5 2
0 comments :
Post a Comment