title: Binary Tree Level Order Traversal Using Queue in C
body: <p>I am fairly new to C. I started studying data structures and was okay with linked lists, stacks, and queues. Right now I am implementing BST's and they seem to be pretty complicated. Here I tried writing code for Level Order Transversal and I am not getting the required output. <br/>
  <strong>Please help me fix it.</strong> <br/><br/>It showed no errors. On running the program, I get infinite 'G' as my output. I expected to get all the characters (which I had input in the main function) to be printed in some order. <br/><br/></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  struct Node
  {
      char data;
      struct Node* right;
      struct Node* left;
  
      };
  
  void enq(struct Node* temproot);
  struct Node* deq();
  struct Node* Insert(struct Node* ,char x);
  struct Node* newNode (char data);
  void LevelOrder(struct Node* root);
  
  struct Node* front = NULL;
  struct Node* rear = NULL;
  
  int main()
  {
  
      struct Node* root;
      root = NULL;
      root = Insert(root,'F');
      root = Insert(root,'B');
      root = Insert(root,'C');
      root = Insert(root,'D');
      root = Insert(root,'E');
      root = Insert(root,'G');
      root = Insert(root,'A');
      root = Insert(root,'H');
      root = Insert(root,'I');
      LevelOrder(root);
  
      return 0;
  
      }
  
  struct Node* Insert(struct Node* root,char x)
  {
       if (root == NULL)
       {
           root = newNode(x); 
           }
      else if (x &lt;= root-&gt;data)
      {
          root-&gt;left = Insert(root-&gt;left, x);
          }
  
      else
      {
          root-&gt;right = Insert(root-&gt;right,x);
          }
      return root;
  }
  
  struct Node* newNode(char x)
  {
      struct Node* temp1 = (struct Node*)malloc(sizeof(struct Node)); 
      temp1-&gt;data = x;
      temp1-&gt;right = NULL;
      temp1-&gt;left = NULL;
      return  temp1;
      }
  
  
  void enq(struct Node* temproot)
  {
      struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
      temp-&gt;data = temproot-&gt;data;
      temp-&gt;right = temp-&gt;left= NULL;
  
      if(front == NULL &amp;&amp; rear == NULL)
      {
          front = rear = temp;
          return;
          }
      rear-&gt;right = temp;
      rear = temp;
      }
  
  struct Node* deq()
  {
      struct Node* temp = front;
      if (front == NULL)
      {
          printf("The queue is empty!");
          }
      else if (rear == front)
      {
  
          front = rear = NULL;
          }
      else
      {
          front = front-&gt;right;
          }
      return temp;
      }
  
  void LevelOrder(struct Node* root)
  {
      struct Node* temproot = root;
  
      while(temproot != NULL)
      {
          printf("%c ", temproot-&gt;data);
          if(temproot-&gt;left != NULL)
          {enq(temproot-&gt;left);
              }
          if (temproot-&gt;right !=NULL)
          {enq(temproot-&gt;right);
              }
          temproot = deq();
          }
      }
  </code></pre>