0

I have a question i want to create postorder from inorder and preorder, but i don't wana use reconstruction of tree, i want only recursive do this. I code this, and at this moment, i have a part of, right side of tree in preorder( First char in preorder is root, i find this in inorder, and i have left and right side, i recurency translate to right side), but i have a problem with left side of tree. I don't have no idea to do this. Can someone give me some of suggestion or code ? Please help :)

3
  • oh i forgot, i have preorder and inorder from two String ;)
    – pkruk
    May 29, 2012 at 17:54
  • An example would probably be helpful.
    – biziclop
    May 29, 2012 at 17:58
  • Hm i show example and my code :
    – pkruk
    May 29, 2012 at 19:17

1 Answer 1

0
 public static String a(String pre, String in, int start, int end) {
            char c = pre.charAt(start); //first char in preorder is root
            int ix = find(pre, in, c); // if we find this char in inorder translation we know where is left and right side of tree
            stack += c;
            if (start == 0 && flaga == true) {
                left = pre.substring(1, ix + 1);
                right = pre.substring(ix + 1, end);
                flaga = false;
                return a(right, in, 0, end);
            }
            String reverse = new StringBuffer(stos).reverse().toString();
    //stack to see
//        System.out.println("STACK " + stos);
            if (start < right.length()-1) {
                return a(right, in, start + 1, end - 1);
            }
            return ""; 
     }
    public static int find(String a, String b, char c) {
        int b_l = b.length();
        for (int i = 0; i < b_l; ++i)
            if (b.charAt(i) == c)
                return i;
        return -1;

First Test : String pre = "FBADCEGIH"; String inorder = "ABCDEFGHI"; ANswer should be : //A, C, E, D, B, H, I, G, F My problem is with left side of tree, i don't have any idea to do this correct, and i'm not sure my code work for all situation of preorder and inorder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.