Build a binary Tree from inOrder and preOrder data in Java -
i've gone through program few times, can't see issue. basically, supposed reconstruct binary tree inorder , preorder traversal data (sent in integer arrays) it's binary tree of integers.
here's tree i'm using:
234 / \ 98 678 / \ \ 45 124 1789
preorder 234, 98, 45, 124, 678, 1789 inorder 45, 98, 124, 234, 678, 1789
the problem comes 1789. code reconstructs tree 1789, leaving out odd reason. decided switch 678 , 1789 in inorder array, , put 1789 left of 678, , 0 right of 678. in code below, arrays in order supposed (or assume supposed be).
build tree code:
public class buildtree { public static binarytreenode buildtree(int inorder[], int preorder[], int preindex ) { if (inorder.length > 1) { int inindex = 0; (int = 0; < inorder.length; i++) { if (preorder[preindex] == inorder[i]) { inindex = ; 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]; (int = 0 ; < index ; ++) { left[i] = input[i] ; } return left; } public static int[] rightarray(int[] input, int index) { int right[] = new int [index]; int x= 0; (int = index +1 ; < input.length ; ++) { 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 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 @ least following issues:
inindex
can equal zero, element can found on first position in inorder list.return new binarytreenode(inorder[0]);
should returned if there elements in inorder list. when tree not complete, missing leaf nodes, shouldreturn null;
- in
rightarray
need allocateinput.length-index-1
elements, notindex
.
other above issues logic working, @ least provided test.
Comments
Post a Comment