title: Construct a complete binary tree from given integer array in C
body: <p>I get unexpected output in the code for the following problem:</p>
  
  <p>Construct a complete (or almost complete) binary tree from given integer array.
  Use a linked list representation of a queue.</p>
  
  <p><em>Complete binary tree</em> is <em>full BT</em>, and all leafs are on the same level.
  <em>Almost complete BT</em> doesn't have to be <em>full BT</em>, and all leafs are on the same level.</p>
  
  <h3>Example 1: Almost Complete BT</h3>
  
  <pre><code>arr[]={10,9,-5,1,2}
  
      Tree:
      10
     /  \
    9   -5
   / \
  1   2
  </code></pre>
  
  <h3>Example 2: Complete BT</h3>
  
  <pre><code>arr[]={10,9,-5,1,2,7,4}
  
  Tree:
  
      10
     /  \
    9   -5
   / \  /  \
  1   2 7   4
  </code></pre>
  
  <h3>Code</h3>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  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-&gt;left=newN-&gt;right=0;
      newN-&gt;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-&gt;tn=tn;
      newQN-&gt;next=0;
      if(*pf==0)
        *pf=*pr=newQN;
      else
      {
          (*pr)-&gt;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-&gt;tn;
      if(*pf==*pr)
          *pf=*pr=0;
      else
          *pf=p-&gt;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(arr[0]);
      push_queueN(&amp;pf,&amp;pr,root);
  
      p++;
      for(i=0;i&lt;n;i++)
      {
  
         TREE_NODE *parent=pf-&gt;tn;
         if(pop_queueN(&amp;pf,&amp;pr,&amp;parent))
              check=1;
         else
              check=0;
         TREE_NODE *leftChild=NULL,*rightChild=NULL;
         leftChild=newTN(&amp;p);
         push_queueN(&amp;pf,&amp;pr,leftChild);
         p++;
         if(p)
         {
           rightChild=newTN(&amp;p);
           push_queueN(&amp;pf,&amp;pr,rightChild);
           p++;
         }
  
         parent-&gt;left=leftChild;
         parent-&gt;right=rightChild;
      }
  
      return root;
  }
  
  int height(TREE_NODE *root)
  {
      if(root==0)
          return 0;
      int hl=height(root-&gt;left);
      int hr=height(root-&gt;right);
  
      return 1+(hl&gt;hr?hl:hr);
  }
  
  void printCurrLevel(TREE_NODE *root,int level)
  {
     if(root==0)
          return;
     if(level==1)
          printf("%d",root-&gt;info);
     else if(level&gt;1)
     {
        printCurrLevel(root-&gt;left,level-1);
        printCurrLevel(root-&gt;right,level-1);
     }
  }
  
  void levelOrder(TREE_NODE *root)
  {
     int h=height(root),i;
  
     for(i=1;i&lt;=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;
  }
  </code></pre>
  
  <p>There are some issues with pointers in function <code>complete()</code>.</p>
  
  <p>Could someone point out where are the possible errors?</p>