package testcase;
class Node{
private Object data;
private Node leftChild;
private Node rightChild;
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getLeftChild() {
return leftChild;
}
public void setLeftChild(Node leftChild) {
this.leftChild = leftChild;
}
public Node getRightChild() {
return rightChild;
}
public void setRightChild(Node rightChild) {
this.rightChild = rightChild;
}
public Node(Object data) {
super();
this.data = data;
}
}
public class BinTree {
public static void main(String[] args) {
Node n1=new Node(10);
Node n2=new Node(9);
Node n3=new Node(20);
Node n4=new Node(15);
Node n5=new Node(35);
n1.setLeftChild(n2);
n1.setRightChild(n3);
n3.setLeftChild(n4);
n3.setRightChild(n5);
BinTree br=new BinTree();
br.preOrder(n1);
System.out.println();
br.inOrder(n1);
System.out.println();
br.postOrder(n1);
}
public void preOrder(Node current){
if(current!=null){
System.out.print(current.getData()+" ");
preOrder(current.getLeftChild());
preOrder(current.getRightChild());
}
}
public void inOrder(Node current){
if(current!=null){
inOrder(current.getLeftChild());
System.out.print(current.getData()+" ");
inOrder(current.getRightChild());
}
}
public void postOrder(Node current){
if(current!=null){
postOrder(current.getRightChild());
postOrder(current.getLeftChild());
System.out.print(current.getData()+" ");
}
}
}