GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

Construct a complete binary tree from an array - C

I get unexpected output in the code for the following problem:

Construct a complete (or almost complete) binary tree from given integer array. Use a linked list representation of a queue.

Complete binary tree is full BT, and all leafs are on the same level. Almost complete BT doesn't have to be full BT, and all leafs are on the same level.

Example:

arr[]={10,9,-5,1,2}

    Tree:
    10
   /  \
  9   -5
 / \
1   2

This is almost complete BT

arr[]={10,9,-5,1,2,7,4}

Tree:

    10
   /  \
  9   -5
 / \  /  \
1   2 7   4


This is complete BT
#include <stdio.h>
#include <stdlib.h>

typedef struct node_tree
{
    int info;
    struct node_tree *left,*right;
}TREE_NODE;

typedef struct node_queue
{
    TREE_NODE *tn;
    struct node_queue *next;
}QUEUE_NODE;

TREE_NODE *newTN(int info)
{
    TREE_NODE *newN=(TREE_NODE*)malloc(sizeof(TREE_NODE));
    newN->left=newN->right=0;
    newN->info=info;
    return newN;
}

void push_queueN(QUEUE_NODE **pf,QUEUE_NODE **pr,TREE_NODE *tn)
{
    QUEUE_NODE *newQN=(QUEUE_NODE*)malloc(sizeof(QUEUE_NODE));
    newQN->tn=tn;
    newQN->next=0;
    if(*pf==0)
      *pf=*pr=newQN;
    else
    {
        (*pr)->next=newQN;
        *pr=newQN;
    }
}

int pop_queueN(QUEUE_NODE **pf,QUEUE_NODE **pr,TREE_NODE **tn)
{
    if(*pf==0)
        return 0;
    QUEUE_NODE *p=*pf;
    *tn=p->tn;
    if(*pf==*pr)
        *pf=*pr=0;
    else
        *pf=p->next;
    free(p);
    return 1;
}

TREE_NODE *complete(int *arr,int n)
{
    TREE_NODE *root;
    QUEUE_NODE *pf=0,*pr=0;
    int check=0,i;
    int *p=arr;

    if(p==NULL)
        root=NULL;

    root=newTN(*p);
    push_queueN(&pf,&pr,root);

    p++;
    for(i=0;i<n;i++)
    {

       TREE_NODE *parent=pf->tn;
       if(pop_queueN(&pf,&pr,&parent))
            check=1;
       else
            check=0;
       TREE_NODE *leftChild=NULL,*rightChild=NULL;
       leftChild=newTN(*p);
       push_queueN(&pf,&pr,leftChild);
       p++;
       if(p)
       {
         rightChild=newTN(*p);
         push_queueN(&pf,&pr,rightChild);
         p++;
       }

       parent->left=leftChild;
       parent->right=rightChild;
    }

    return root;
}

int height(TREE_NODE *root)
{
    if(root==0)
        return 0;
    int hl=height(root->left);
    int hr=height(root->right);

    return 1+(hl>hr?hl:hr);
}

void printCurrLevel(TREE_NODE *root,int level)
{
   if(root==0)
        return;
   if(level==1)
        printf("%d",root->info);
   else if(level>1)
   {
      printCurrLevel(root->left,level-1);
      printCurrLevel(root->right,level-1);
   }
}

void levelOrder(TREE_NODE *root)
{
   int h=height(root),i;

   for(i=1;i<=h;i++)
      printCurrLevel(root,i);

}

int main()
{
    int arr[]={10,9,-5,1,2};
    int n=sizeof(arr)/sizeof(arr[0]);
    TREE_NODE *root;
    root=complete(arr,n);
    levelOrder(root);

    return 0;
}

There are some issues with pointers in function complete().

Could someone point out where are the possible errors?

asked Jul 21, 2016 by username_hidden
...