package Tree;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
/**
* @author ssundara
*
*/
public class BinaryTree {
BinaryTree left;
BinaryTree right;
int value;
BinaryTree(int value){
this.value=value;
}
/**
* @param args
*/
public static void main(String[] args){
BinaryTree root=new BinaryTree(20);
insert(root,8);
insert(root,12);
insert(root,4);
insert(root,22);
insert(root,14);
insert(root,10);
//insert(root,50);
System.out.print("Inorder :");
Inorder(root);
System.out.println();
System.out.print("Preorder :");
Preorder(root);
System.out.println();
System.out.print("Postorder :");
Postorder(root);
System.out.println();
System.out.print("Level order :");
LevelOrder(root);
System.out.println();
System.out.print("Spiral order :");
SpiralOrder(root);
System.out.println();
System.out.print("DFS :");
dfs(root);
}
public static void insert(BinaryTree node, int value){
if(value<node.value){
if(node.left!=null)
insert(node.left,value);
else
node.left=new BinaryTree(value);
}
if(value>node.value){
if(node.right!=null)
insert(node.right,value);
else
node.right=new BinaryTree(value);
}
}
public static void Inorder(BinaryTree root){
if(root!=null){
Inorder(root.left);
System.out.print(root.value + " ");
Inorder(root.right);
}
}
public static void Preorder(BinaryTree root){
if(root!=null){
System.out.print(root.value + " ");
Inorder(root.left);
Inorder(root.right);
}
}
public static void Postorder(BinaryTree root){
if(root!=null){
Inorder(root.left);
Inorder(root.right);
System.out.print(root.value + " ");
}
}
public static void LevelOrder(BinaryTree root){
Queue<BinaryTree> q=new LinkedList<BinaryTree>();
q.add(root);
while(!q.isEmpty()){
System.out.print(q.poll().value + " ");
if(root.left!=null)
q.add(root.left);
if(root.right!=null)
q.add(root.right);
if(q.peek()!=null)
root=q.peek();
}
}
public static void SpiralOrder(BinaryTree root){
Stack<BinaryTree> st1=new Stack<BinaryTree>();
Stack<BinaryTree> st2=new Stack<BinaryTree>();
BinaryTree temp=null;
st1.push(root);
while(!st1.isEmpty() || !st2.isEmpty()){
while(!st1.isEmpty()){
if(st1.peek()!=null)
temp=st1.peek();
System.out.print(st1.pop().value + " ");
if(temp.left!=null)
st2.push(temp.left);
if(temp.right!=null)
st2.push(temp.right);
}
while(!st2.isEmpty()){
if(st2.peek()!=null)
temp=st2.peek();
System.out.print(st2.pop().value + " ");
if(temp.right!=null)
st1.push(temp.right);
if(temp.left!=null)
st1.push(temp.left);
}
}
}
public static void dfs(BinaryTree root) {
Stack<BinaryTree> st1=new Stack<BinaryTree>();
BinaryTree temp;
BinaryTree tmp;
st1.push(root);
System.out.print(root.value + " ");
while(!st1.isEmpty()){
temp=st1.peek();
if(temp.left!=null){
st1.push(temp.left);
System.out.print(temp.left.value + " ");
}
else{
tmp=st1.peek();
while(tmp.right==null){
if(st1.isEmpty()) return;
tmp=st1.pop();
}
st1.push(tmp.right);
System.out.print(tmp.right.value + " ");
}
}
}
}
Output :
Inorder :4 8 10 12 14 20 22
Preorder :20 4 8 10 12 14 22
Postorder :4 8 10 12 14 22 20
Level order :20 8 22 4 12 10 14
Spiral order :20 22 8 4 12 14 10
DFS :20 8 4 12 10 14 22
No comments:
Post a Comment