salamanka44 salamanka44 - 6 months ago 18
Java Question

Rewrite a C code in Java to construct full binary tree

I want to write a function to construct a full binary tree from a given preorder and postorder array. I found that link http://www.geeksforgeeks.org/full-and-complete-binary-tree-from-given-preorder-and-postorder-traversals/ which proposes the following C code :

struct node* constructTreeUtil (int pre[], int post[], int* preIndex,
int l, int h, int size)
{
// Base case
if (*preIndex >= size || l > h)
return NULL;

// The first node in preorder traversal is root. So take the node at
// preIndex from preorder and make it root, and increment preIndex
struct node* root = newNode ( pre[*preIndex] );
++*preIndex;

// If the current subarry has only one element, no need to recur
if (l == h)
return root;

// Search the next element of pre[] in post[]
int i;
for (i = l; i <= h; ++i)
if (pre[*preIndex] == post[i])
break;

// Use the index of element found in postorder to divide postorder array in
// two parts. Left subtree and right subtree
if (i <= h)
{
root->left = constructTreeUtil (pre, post, preIndex, l, i, size);
root->right = constructTreeUtil (pre, post, preIndex, i + 1, h, size);
}

return root;
}

// The main function to construct Full Binary Tree from given preorder and
// postorder traversals. This function mainly uses constructTreeUtil()
struct node *constructTree (int pre[], int post[], int size)
{
int preIndex = 0;
return constructTreeUtil (pre, post, &preIndex, 0, size - 1, size);
}


I tried to rewrite this code in Java. Here is my code :

private static TreeNode constructTree(int[] preorder, int[] postorder, Index index, int lowIndex, int highIndex){

// Base case
if (index.index >= preorder.length || lowIndex > highIndex){
return null;
}

// The first node in preorder traversal is root. So take the node at
// preIndex from preorder and make it root, and increment preIndex
TreeNode root = new TreeNode (preorder[lowIndex]);
index.index++;

// If the current subarry has only one element, no need to recur
if (lowIndex == highIndex){
return root;
}

// Search the next element of pre[] in post[]
int i = 0;
for (i = lowIndex; i <= highIndex; ++i)
if (preorder[i]== postorder[lowIndex])
break;

// Use the index of element found in postorder to divide postorder array in
// two parts. Left subtree and right subtree
if (i <= highIndex) {
root.left = constructTree(preorder, postorder, index, lowIndex, i);
root.right = constructTree(preorder, postorder, index, i + 1, highIndex);
}
return root;
}

//The main function to construct Full Binary Tree from given preorder and
//postorder traversals. This function mainly uses constructTreeUtil()
public static TreeNode constructTree (int preorder[], int postorder[]) {
return constructTree (preorder, postorder, new Index(), 0, preorder.length - 1);
}


But I got a continuous loop in the root node (it didn't pass to the other nodes which have to be its child). Can you help me please to see where is the error in my Java code?

I'm not really sure but I think that the error maybe comes from these lines :

int i = 0;
for (i = lowIndex; i <= highIndex; ++i)
if (preorder[i]== postorder[lowIndex])
break;


I didn't understand very well the correspond lines in the original C code. Especially in this part

Answer

Here are the bugs:

  1. Line if (preorder[i]== postorder[lowIndex]) has two errors: the first is that you search in preorder instead of in postorder, and the second is that you use lowIndex instead of preIndex. This line should be: if (preorder[index.index]== postorder[i])
  2. Line TreeNode root = new TreeNode (preorder[lowIndex]); - lowIndex is used again instead of preIndex. This line should be: TreeNode root = new TreeNode (preorder[index.index]);

Pay attention to the fact that this code would work only for full binary trees

Comments