struct node* constructTreeUtil(int arr[], int* preIndex, int key, int min, int max, int size){

if(*preIndex >= size)return NULL;

struct node* root = NULL;


if(key >min && key < max)
{
	root = newNode(key);
    *preIndex = *preIndex + 1;


	if(*preIndex < size)
	{
	root->left = constructTreeUtil( arr, preIndex, arr[*preIndex],
                                        min, key, size );
     root->right = constructTreeUtil( arr, preIndex, arr[*preIndex],
                                         key, max, size );
   }
}

//printf(*preIndex);

return root;	
}
Beispiel #2
0
// A recursive function to construct Full from pre[] and post[].
// preIndex is used to keep track of index in pre[].
// l is low index and h is high index for the current subarray in post[]
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;
}
struct node* constructTree(int* arr, int size){

int preIndex = 0;

return constructTreeUtil(arr,&preIndex,arr[0], INT_MIN,INT_MAX,size);

}
Beispiel #4
0
// 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);
}