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();
    }
}

Are you looking for a design/algorithm
or do you have one and need help coding it in java?

@NormR1 I need coding in java and I haven't any design or algorithm on this.

Edited 4 Years Ago by Hammer Engineer: no

Please provide me a suitable algo that matches with my procedure of code(if possible).I'm trying to solve it using Stack but not successfull yet.

Edited 4 Years Ago by Hammer Engineer

i think you should go through linked list or treemap or hashmap in java.
It will be suited for your algorithm.
First design your algorithm then start your implementation.

Thanks

This article has been dead for over six months. Start a new discussion instead.