title: Error in insertion in binary search tree
body: <p>My code is insert elements in binary search tree but after inserting first element the program is stop running and is not executing further</p>
  
  <pre><code> typedef struct BST
   {
     int info;
     struct BST * left;
     struct BST *right;
   }
  bst;
  </code></pre>
  
  <p>//Global root variable</p>
  
  <pre><code>bst *root; 
  </code></pre>
  
  <p>//Insert function</p>
  
  <pre><code>void insert(int x) //x is the elemnent to be inserted
  {
  bst *ptr,*ptr1;
  ptr=(bst*)malloc(sizeof(bst));
  if(root==NULL) //checking whether the tree is empty or not
    {
    ptr-&gt;left=ptr-&gt;right=NULL;
    root=ptr;
    }
  else
    {
    ptr1=root;
  while(ptr1!=NULL)
    {
    if(x&gt;ptr1-&gt;info)   //traversing to right if element is greater than  element present
      ptr1=ptr1-&gt;right;
    else
      ptr1=ptr1-&gt;left; //traversing to left if element is present than the the element
    }
   }
  if(x&gt;ptr1-&gt;info)
   {
    ptr1-&gt;right=ptr;
    ptr-&gt;info=x;
   }
  else
   {
    ptr1-&gt;left=ptr;
    ptr-&gt;info=x;
   }
  }
  </code></pre>
  
  <p>//show function using preorder traversal</p>
  
  <pre><code>void preorder(bst *root)
  {
   bst *ptr=root;
   printf("%d",ptr-&gt;info);
   preorder(ptr-&gt;left);
   preorder(ptr-&gt;right);
  }
  
  int main()
  {
  int n,x,i;
  puts("Enter number of elements");
  scanf("%d",&amp;n);
  for(i=0;i&lt;n;i++)
    {
    puts("Enter elements");
    scanf("%d",&amp;x);
    insert(x);
  }
  preorder(root);
  return 0;
  }
  </code></pre>