Hi I am trying to implement an array based BT.Here is my code for this. But I'm stuck with Postorder traversal.
How can I get a Postorder traversal from my array based BT ?
My Entire code is given here.
package com.TreeLab;
public class Stack {
private final int SIZE = 20;
private String[] st;
private int top;
//------------------------------------------------------------
public Stack() // constructor
{
st = new String[SIZE]; // make array
top = -1;
}
//------------------------------------------------------------
public void push(String j) // put item on stack
{ st[++top] = j; }
//------------------------------------------------------------
public String pop() // take item off stack
{ return st[top--]; }
//------------------------------------------------------------
public String peek() // peek at top of stack
{ return st[top]; }
//------------------------------------------------------------
public boolean isEmpty() // true if nothing on stack
{ return (top == -1); }
}
package com.TreeLab;
public class TreeBuilder {
//String root = "";
int root=0;
public String [] arr = new String [20];
public void createRoot(String data){
arr[0] = data;
root =0;
}
public void setLeft(String data, int root){
arr[(root*2)+1] = data;
}
public String getLeft(int root){
return arr[(root*2)+1];
}
public void setRight(String data , int root){
arr[(root*2)+2] = data;
}
public String getRight(int root){
return arr[(root*2)+2];
}
public void printTree(){
for(int i=0;i<20;i++){
if(arr[i] !=null)
System.out.print(arr[i]);
}
}
}
package com.TreeLab;
public class Test {
public static void main(String[] args){
TreeBuilder TB = new TreeBuilder();
TB.createRoot("A");
TB.setLeft("B", 0);
TB.setRight("C", 0);
TB.setLeft("D", 1);
TB.setRight("E", 1);
TB.setLeft("P", 2);
//TB.setRight(, root)
//TB.printTree();
System.out.println();
TB.PostOrder();
}
}