简体   繁体   English

从Java中的inOrder和preOrder数据构建二进制树

[英]Build a binary Tree from inOrder and preOrder data in Java

I've gone through this program a few times, but I can't really see the issue. 我已经看过几次这个程序,但是我看不到这个问题。 Basically, it is supposed to reconstruct a binary tree from inOrder and preOrder traversal data (sent in as integer arrays) It's a binary tree of integers. 基本上,应该从inOrder和preOrder遍历数据(以整数数组形式发送)重构二叉树。它是整数的二叉树。

Here's the tree I'm using: 这是我正在使用的树:

     234
    /   \
  98    678
 / \      \
45 124    1789

preOrder is 234, 98, 45, 124, 678, 1789 inOrder is 45, 98, 124, 234, 678, 1789 preOrder是234、98、45、124、678、1789 inOrder是45、98、124、234、678、1789

The problem that comes up is with 1789. The code reconstructs the tree up to 1789, leaving it out for some odd reason. 出现的问题出在1789年。代码重构树直到1789年,出于某种奇怪的原因而将其省略。 I decided to switch 678 and 1789 in the inOrder array, and that put 1789 to the left of 678, and a 0 to the right of 678. In the code below, the arrays are in the order they are supposed to be (or what I assume is supposed to be). 我决定在inOrder数组中切换678和1789,然后将1789放在678的左边,将0放在678的右边。在下面的代码中,数组按照它们应该是的顺序排列(或我认为应该是)。

Build Tree code: 构建树代码:

public class BuildTree
{

public static BinaryTreeNode buildTree(int inOrder[], int preOrder[], int preIndex )
{               
    if (inOrder.length > 1)  
    {
        int inIndex = 0;
        for (int i = 0; i < inOrder.length; i++)
        {
            if (preOrder[preIndex] == inOrder[i])
            {
                inIndex = i ;
                break;                  
            }
        }

        if (inIndex > 0)
        {          
            BinaryTreeNode node = new BinaryTreeNode(inOrder[inIndex]);

            if (preIndex < preOrder.length - 1 )
            {
            node.setLeft(buildTree(leftArray(inOrder, inIndex), preOrder, preIndex + 1));
            node.setRight(buildTree(rightArray(inOrder, inIndex), preOrder, inIndex + 1));  
            }

            return node;
        }           
    }       
    return new BinaryTreeNode(inOrder[0]);
}


public static int[] leftArray(int[] input, int index)
{        
    int left[] = new int [index];

    for (int i = 0 ; i < index ; i ++)
    {
        left[i] = input[i] ;
    }

    return left;        
}


public static int[] rightArray(int[] input, int index)
{               
    int right[] = new int [index];
    int x= 0;

    for (int i = index +1  ; i < input.length  ; i ++)
    {
        right[x] = input[i] ;
        ++x;
    }

    return right;       
}
} //end class

BinaryTreeNode: BinaryTreeNode:

public class BinaryTreeNode
{
    private int data;
    private BinaryTreeNode left;
    private BinaryTreeNode right;

    BinaryTreeNode()
    {

    }

    BinaryTreeNode(int data)
    {
        this.data = data;
    }

    public void setData(int data) {
        this.data = data;
    }

    public void setLeft(BinaryTreeNode left) {
        this.left = left;
    }

    public void setRight(BinaryTreeNode right) {
        this.right = right;
    }

    public int getData() {
        return data;
    }

    public BinaryTreeNode getLeft() {
        return left;
    }

    public BinaryTreeNode getRight() {
        return right;
    }
}

Main method for testing: 主要测试方法:

public static void main(String[] args)
{
    int[] preOrder = {234, 98, 45,  124, 678, 1789};
    int[] inOrder =  {45,  98, 124, 234, 678, 1789};

    BinaryTreeNode bsTree = BuildTree.buildTree(inOrder, preOrder, 0);

    System.out.println(bsTree.getData());
    System.out.println(bsTree.getLeft().getData());
    System.out.println(bsTree.getLeft().getLeft().getData());
    System.out.println(bsTree.getLeft().getRight().getData());
    System.out.println(bsTree.getRight().getData());
    System.out.println(bsTree.getRight().getRight().getData());
    System.out.println(bsTree.getRight().getLeft().getData());

}

There are at least the following issues: 至少存在以下问题:

  1. inIndex can be equal to zero, an element can be found on the first position in the inOrder list. inIndex可以等于零,可以在inOrder列表的第一个位置上找到一个元素。
  2. return new BinaryTreeNode(inOrder[0]); should be returned only if there are elements in the inOrder list. 仅当inOrder列表中有元素时才应返回。 When the tree is not complete, you are missing leaf nodes, you should return null; 当树不完整时,您缺少叶节点,应return null;否则, return null;
  3. In rightArray you need to allocate input.length-index-1 elements, not only index . rightArray您需要分配input.length-index-1元素,而不仅是index

Other then the above issues the logic is working, at least with your provided test. 除上述问题外,逻辑至少在您提供的测试中有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM