/*Tree Traversal:
->Process of visiting each node in the tree exactly once in some order.
->Visit:-Reading/Processing data in node.
->2-types:
->Breadth First Traversal:Visit all nodes at same level(left to right)
->Also called level order traversal
->Depth First Traversal:-First visit all nodes in left subtree and then right subtree.
1.Preorder(root,left,right)(data,left,right)
2.Inorder(left,root,right)(left,data,right)
3.Postorder(left,right,root)(left,right,data)
->Tree: start from the root
->Preorder:-FDBACEJGIHK
->InOrder:-ABCDEFGHIJK
->Postorder:-ACBEDHIGKJF
F
/ \
D J
/ \ / \
B E G K
/ \ \
A C I
/
H
*/
import java.util.Queue;
import java.util.LinkedList;
import java.util.Stack;
import java.lang.*;
import java.io.*;
import java.util.logging.Level;
class LevelOrderNode
{
int data;
LevelOrderNode left;
LevelOrderNode right;
LevelOrderNode()
{
left=null;
right=null;
}
}
class BinaryTreeTraversal {
//Global variable that will store address of root node
public static LevelOrderNode root=null;
public static LevelOrderNode GetNewNode(int data)
{
LevelOrderNode newNode=new LevelOrderNode();
newNode.data=data;
newNode.left=null;
newNode.right=null;
return newNode;
}
//Recursive Version of Adding a node to binary search tree
public static LevelOrderNode Add_recursive(LevelOrderNode root,int data)
{
if(root==null)
{
//Empty tree
root=GetNewNode(data);
return root;
}
else if(data<=root.data)
{
root.left=Add_recursive(root.left,data);
}
else
{
root.right=Add_recursive(root.right,data);
}
return root;
}
//Iterative Version of Adding Node to a binary search tree
public static LevelOrderNode Add_Iterative(LevelOrderNode root,int data)
{
LevelOrderNode node=new LevelOrderNode();
node.data=data;
if(root==null)
{
root=node;
return root;
}
LevelOrderNode parent=null;
LevelOrderNode current=root;
while(current!=null)
{
parent=current;
if(current.data<=data)
{
current=current.right;
}
else
{
current=current.left;
}
}
if(parent.data<=data)
{
parent.right=node;
}
else
{
parent.left=node;
}
return root;
}
public static void PreOrder(LevelOrderNode root)
{
/*Steps:->first visit root
->Visit left subtree
->Visit right subtree
*/
if(root==null)
{
return;
}
System.
out.
print(root.
data+" "); PreOrder(root.left);
PreOrder(root.right);
}
public static void InOrder(LevelOrderNode root)
{
if(root==null)
{
return;
}
InOrder(root.left);
System.
out.
print(root.
data+" "); InOrder(root.right);
}
public static void PostOrder(LevelOrderNode root)
{
if(root==null)
{
return;
}
PostOrder(root.left);
PostOrder(root.right);
System.
out.
print(root.
data+" "); }
//Iterative versions of PreOrder,InOrder,PostOrder Traversals
public static void IterativePreOrder(LevelOrderNode root)
{
if(root==null)
{
return;
}
Stack<LevelOrderNode> s=new Stack<LevelOrderNode>();
s.push(root);
while(!s.isEmpty())
{
root=s.pop();
System.
out.
print(root.
data+" "); if(root.right!=null)
{
s.push(root);
}
if(root.left!=null)
{
s.push(root);
}
}
}
public static void IterativeInOrder(LevelOrderNode root)
{
if(root==null)
{
return;
}
Stack<LevelOrderNode> s=new Stack<LevelOrderNode>();
while(true)
{
if(root!=null)
{
s.push(root);
root=root.left;
}
else
{
if(s.isEmpty())
{
//Terminating condition
break;
}
root=s.pop();
System.
out.
print(root.
data+" "); root=root.right;
}
}
}
public static void IterativePostOrder(LevelOrderNode root)
{
if(root==null)
{
return;
}
Stack<LevelOrderNode> s1=new Stack<LevelOrderNode>();
Stack<LevelOrderNode> s2=new Stack<LevelOrderNode>();
s1.push(root);
while(!s1.isEmpty())
{
root=s1.pop();
s2.push(root);
if(root.left!=null)
{
s1.push(root.left);
}
if(root.right!=null)
{
s1.push(root.right);
}
}
while(!s2.isEmpty())
{
root=s2.pop();
}
}
//This is the code for Level Order Traversal(Breadth First Search)
public static void LevelOrder_Traverse(LevelOrderNode root)
{
//Time complexity of level order traversal:O(n)
//Space complexity of level order traversal:O(1)(best case)
//Worst case:O(n)
if(root==null)
{
return;
}
//Here we create queue to store node and its children
//Queue stores pointer to particular node
Queue<LevelOrderNode> q = new LinkedList<LevelOrderNode>();
//Intially add root node to the queue
q.add(root);
//Run a loop till queue is not empty
//q.isEmpty() returns true is the queue is empty
//While there is atleast one discovered node
while(!q.isEmpty())
{
//Object poll() It is used to retrieves and removes the head of this queue, or returns null if this queue is empty.
LevelOrderNode current=q.poll();
System.
out.
print(current.
data);
//if left and right child are not null then add them to the queue
if(current.left!=null)
{
q.add(current.left);
}
if(current.right!=null)
{
q.add(current.right);
}
}
}
public static void main
(String[] args
) {
root=Add_recursive(root,1);
root=Add_recursive(root,2);
root=Add_recursive(root,3);
root=Add_recursive(root,4);
root=Add_recursive(root,5);
//printing LevelOrder
//System.out.print("LevelOrder traversal:");
//LevelOrder_Traverse(root);
//Printing PreOrder
System.
out.
print("PreOrder traversal:"); PreOrder(root);
//System.out.println("Hello");
/*//Printing Inorder
System.out.print("InOrder traversal:");
InOrder(root);
System.out.println();
//printing postorder
System.out.print("PostOrder traversal:");
PostOrder(root);
System.out.println();*/
//Printing all traversals iteratively
//Printing PreOrder
/* System.out.print("PreOrder traversal:");
IterativePreOrder(root);
System.out.println();
*/
/* //Printing Inorder
System.out.print("InOrder traversal:");
IterativeInOrder(root);
System.out.println();
//printing postorder
System.out.print("PostOrder traversal:");
IterativePostOrder(root);
System.out.println();
*/
}
}