Binary Tree
Binary Tree Traversals(Inorder, PreOrder and PostOrder) [Implemented in Java] by recursive Approach.
Binary Tree is a tree if each node has zero child,one child or two children.
Note:-Empty tree is also a valid Binary Tree.
Here, we make a simple Binary Tree and study there different types of traversing techniques.
Tree designed by a constructor is like:-
->Visit the root.
->Traverse a left subtree in InOrder.
->Traverse a right subtree in InOrder.
PostOrder:-
->Traverse a left subtree in InOrder.
->Traverse a right subtree in InOrder.
->Visit the root.
Program:-
/**
* @(#)BinaryTreeMode1.java
*
*
* @author Suyash Bhalla
* @version 1.00 2014/12/20
*/
public class BinaryTreeMode1 {
class Node{
private Node left;
private Node right;
private int data;
void setLeft(Node left){
this.left=left;
}
void setRight(Node right){
this.right=right;
}
void setData(int data){
this.data=data;
}
int getData(){
return data;
}
Node getLeft(){
return left;
}
Node getRight(){
return right;
}
}
Node root;
BinaryTreeMode1(){ //This Constructor designs an tree As shown in above figure.
Node n=new Node(); //Setuping an simple Tree to see different types of traversals.
root=n;
Node l=new Node();
Node r=new Node();
n.setData(1);
l.setData(2);
n.setLeft(l);
r.setData(3);
n.setRight(r);
Node t=n.getRight();
n=n.getLeft();
l=new Node();
r=new Node();
l.setData(4);
n.setLeft(l);
r.setData(5);
n.setRight(r);
n=t;
l=new Node();
r=new Node();
l.setData(6);
n.setLeft(l);
r.setData(7);
n.setRight(r);
}
public void inOrder(Node n){
if(n!=null){
inOrder(n.getLeft());
System.out.print(n.getData()+" ");
inOrder(n.getRight());
}
}
public void preOrder(Node n){
if(n!=null){
System.out.print(n.getData()+" ");
preOrder(n.getLeft());
preOrder(n.getRight());
}
}
public void postOrder(Node n){
if(n!=null){
postOrder(n.getLeft());
postOrder(n.getRight());
System.out.print(n.getData()+" ");
}
}
public static void main(String ar[]){
BinaryTreeMode1 ob=new BinaryTreeMode1();
System.out.print("InOrder is=>");
ob.inOrder(ob.root);
System.out.print("\nPreOrder is=>");
ob.preOrder(ob.root);
System.out.print("\nPostOrder is=>");
ob.postOrder(ob.root);
}
}
----------------X----------------X----------------X----------------X----------------X----------------X-------------
PreOrder is=>1 2 4 5 3 6 7
PostOrder is=>4 5 2 6 7 3 1
Binary Tree is a tree if each node has zero child,one child or two children.
Note:-Empty tree is also a valid Binary Tree.
Here, we make a simple Binary Tree and study there different types of traversing techniques.
Tree designed by a constructor is like:-
Binary Tree Designed by Constructer |
InOrder:-
->Traverse a left subtree in InOrder.
->Visit the root.
->Traverse a right subtree in InOrder.
PreOrder:-->Visit the root.
->Traverse a left subtree in InOrder.
->Traverse a right subtree in InOrder.
PostOrder:-
->Traverse a left subtree in InOrder.
->Traverse a right subtree in InOrder.
->Visit the root.
Program:-
/**
* @(#)BinaryTreeMode1.java
*
*
* @author Suyash Bhalla
* @version 1.00 2014/12/20
*/
public class BinaryTreeMode1 {
class Node{
private Node left;
private Node right;
private int data;
void setLeft(Node left){
this.left=left;
}
void setRight(Node right){
this.right=right;
}
void setData(int data){
this.data=data;
}
int getData(){
return data;
}
Node getLeft(){
return left;
}
Node getRight(){
return right;
}
}
Node root;
BinaryTreeMode1(){ //This Constructor designs an tree As shown in above figure.
Node n=new Node(); //Setuping an simple Tree to see different types of traversals.
root=n;
Node l=new Node();
Node r=new Node();
n.setData(1);
l.setData(2);
n.setLeft(l);
r.setData(3);
n.setRight(r);
Node t=n.getRight();
n=n.getLeft();
l=new Node();
r=new Node();
l.setData(4);
n.setLeft(l);
r.setData(5);
n.setRight(r);
n=t;
l=new Node();
r=new Node();
l.setData(6);
n.setLeft(l);
r.setData(7);
n.setRight(r);
}
public void inOrder(Node n){
if(n!=null){
inOrder(n.getLeft());
System.out.print(n.getData()+" ");
inOrder(n.getRight());
}
}
public void preOrder(Node n){
if(n!=null){
System.out.print(n.getData()+" ");
preOrder(n.getLeft());
preOrder(n.getRight());
}
}
public void postOrder(Node n){
if(n!=null){
postOrder(n.getLeft());
postOrder(n.getRight());
System.out.print(n.getData()+" ");
}
}
public static void main(String ar[]){
BinaryTreeMode1 ob=new BinaryTreeMode1();
System.out.print("InOrder is=>");
ob.inOrder(ob.root);
System.out.print("\nPreOrder is=>");
ob.preOrder(ob.root);
System.out.print("\nPostOrder is=>");
ob.postOrder(ob.root);
}
}
----------------X----------------X----------------X----------------X----------------X----------------X-------------
Output:-
InOrder is=>4 2 5 1 6 3 7PreOrder is=>1 2 4 5 3 6 7
PostOrder is=>4 5 2 6 7 3 1
Labels: Binary Tree, Binary Tree Traversals
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home