title: Binary Search Tree in C.
body: <p>I am confused that what mistake i am doing. Why isn't it printing the tree in preorder, postorder and inorder. I have written my questions beside the line of code where i am confused. Plus i don't understand how to relate to structs.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  
  typedef struct BST_{
      int data;
      struct BST_ *lchild, *rchild, *parent;
  }BST;
  
  typedef struct BiTree_{
      int size;
      BST *root;  // is it possible to relate this root with the pointer parent of type BST? i yes then how?
  }BiTree;;
  BST *temp;
  BST *createNode(int data){
      BST *new_ele;
      new_ele=(BST*)malloc(sizeof(BST));
      new_ele-&gt;data=data;
      new_ele-&gt;lchild=NULL;
      new_ele-&gt;rchild=NULL;
      new_ele-&gt;parent=NULL;
      return new_ele;
      }
  
  void insertNode(BST *temp,BST *r,int data){
      if(temp==NULL){
              temp=createNode(data);
           }
  
      else if(temp-&gt;data &gt;= r-&gt;data){
              if(r-&gt;rchild==NULL){
                  r-&gt;rchild=temp;
              }
              else{
                  insertNode(r-&gt;rchild,temp,data);
              }
      }
      else if(temp-&gt;data &lt;= r-&gt;data){
              if(r-&gt;lchild==NULL){
                  r-&gt;lchild=temp; 
              }
              else{
                  insertNode(r-&gt;lchild,temp,data);    
                              }
      }
  //  return r-&gt;data;  //why cann't i do this?
  }
  
  void preorder(BST *r){
      if(r!=NULL){
          printf("%d",r-&gt;data);
          preorder(r-&gt;lchild);
          preorder(r-&gt;rchild);
      }
  }
  void inorder(BST *c){
      if(c!=NULL){
  
          inorder(c-&gt;lchild);
          printf("%d",c-&gt;data);
          inorder(c-&gt;rchild);
      }
  }
  void postorder(BST *r){
      if(r!=NULL){
          postorder(r-&gt;lchild);
          postorder(r-&gt;rchild);
          printf("%d",r-&gt;data);
      }
  }
  void delet(BST *r){
      if(r!=NULL){
          free(r);
      }
  }
  void main(){
      BST *new_node,*r=NULL;
      BiTree *search;
      search=malloc(sizeof(BiTree));
      search-&gt;root=NULL;
      search-&gt;size=0;
      int i,a,n,data;
      printf("Enter the number of element to be inserted\n");
      scanf("%d",&amp;n);
      printf("Enter the data\n");
      for(i=0;i&lt;n;i++){
              scanf("%d",&amp;data);
  
  
              insertNode(temp,r,data);
              //  printf("Enter the data %d\n",r-&gt;data);  // unable to print this
  
              search-&gt;size++;
              }
      printf("size is %d\n",search-&gt;size);
  
      preorder(r);//  why cann't i print the data of r. r is the root of the tree.
      postorder(r);
      inorder(r);
      delet(r);
  
  }
  </code></pre>