【问题标题】:construct binary search tree from Post-order traversal in Java从Java中的后序遍历构造二叉搜索树
【发布时间】:2014-01-16 21:44:57
【问题描述】:

我正在实现代码以在this algorithm 之后从给定的post-order traversal array 构造BST(binary search tree)。我不回binary Search Tree。我得到了一些毫无意义的东西。这是我的代码

public class BinaryTreemethods {
     public static void main(String[] args) {
             int[] preOrder = { 5, 3, 1, 4, 8, 6, 9 };
        int[] inOrder = { 1, 3, 4, 5, 6, 8, 9 };
        int[] postOrder = {1,4,3,8,6,9,5};

           static int postIndex=postOrder.length-1;
           Node postordertree= buildBinarytreefromPostOrder(postOrder, 0, postOrder.length-1);
           System.out.println("pre order traversal of node from postorder reconstructed tree ");
           printPreOrder(postordertree);

      }
      private static void printPreOrder(Node tree) {
        if (tree != null) {
            System.out.print(" " + tree.data);
            printPreOrder(tree.left);
            printPreOrder(tree.right);
        }

    }
      //this just reconstructs BT from post-order traversal elements
    public static Node buildBinarytreefromPostOrder(int[] post, int start, int end){
        if (postIndex<start || start > end ){
            return null;
        }

        Node root = new Node(post[postIndex]);
        postIndex--;
        if (end == start){
            //System.out.println("called");
            return root;
        }

        int i = 0;
        for (i=end;i>=start;i--){
            if (post[i]<root.data)
                break;
        }

        // Use the index of element found in postorder to divide postorder array
        // in two parts. Left subtree and right subtree
            root.right=buildBinarytreefromPostOrder(post,i+1, postIndex);
        root.left=buildBinarytreefromPostOrder(post,start,i);
             //root.left=buildBinarytreefromPostOrder(post,start,i);
        //root.right=buildBinarytreefromPostOrder(post,i+1, postIndex);

        return root;
    }
}

我在pre-order traversal is 5 9 6 8 3 4 中打印时的输出不正确。

知道我哪里出错了吗?

编辑:在将行的顺序交换为root.right and root.left 之后(之前注释掉了一个), left tree 是正确构建的,但正确的树不是。我得到的输出是 5 3 1 4 9 6 8

【问题讨论】:

标签: java algorithm tree binary-search-tree postorder


【解决方案1】:

作为每个子树的根,您将采用postIndex,它对于整个结构都是全局的。您应该取子数组的最后一个元素 (end)。

应该是这样的

public static Node buildBinarytreefromPostOrder(int[] post, int start, int end)
{
    if (end < start)
        return null;

    Node root = new Node(post[end]);

    if (end == start)
        return root;

    int i;
    for (i = end; i >= start; i--)
        if (post[i] < root.data)
            break;

    root.left = buildBinarytreefromPostOrder(post, start, i);
    root.right = buildBinarytreefromPostOrder(post, i + 1, end - 1);

    return root;
}

【讨论】:

  • 您的意思是在递归调用中使用end 而不是postIndex
  • 嗯,我的意思是那里肯定有错误。我认为你根本不需要postIndex(或者我不明白它的目的:)。
  • 我添加了一些编辑,不确定这是否有助于您理解为什么 postIndex
猜你喜欢
  • 2021-09-25
  • 1970-01-01
  • 2014-12-30
  • 1970-01-01
  • 2018-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多