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.
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
The problem that comes up is with 1789. The code reconstructs the tree up to 1789, leaving it out for some odd reason. 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).
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:
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());
}
-
similar: stackoverflow.com/questions/15399835/…Ray Tayek– Ray Tayek2013年04月08日 22:16:45 +00:00Commented Apr 8, 2013 at 22:16
1 Answer 1
There are at least the following issues:
inIndexcan be equal to zero, an element can be found on the first position in the inOrder list.return new BinaryTreeNode(inOrder[0]);should be returned only if there are elements in the inOrder list. When the tree is not complete, you are missing leaf nodes, you shouldreturn null;- In
rightArrayyou need to allocateinput.length-index-1elements, not onlyindex.
Other then the above issues the logic is working, at least with your provided test.
3 Comments
InIndex can be zero when the buildTree function is called. 3. I was suggesting that the allocated space for the remaining right elements should be length-index-1 instead of just index, you were allocating much more. Anyway, here are all my changes to your initial submission.