title: C Program to copy one binary search tree to another
body: <p>So, here i have come up with Binary search tree prgram, where i am creating 2 binary trees tmp and tmp2, where i am trying to copy whole tmp2 to tmp, the node which is taken as input from user. But i am getting some segmentation fault, also i am not so sure if the logic is right.
  Here is the whole program, please lemme know where is it going wrong in t_cpy() or please just fix it for me..</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  
  struct node
  {
  int data;
  struct node *rlink;
  struct node *llink;
  }*tmp=NULL,*tmp2=NULL,*tmp3=NULL;
  
  typedef struct node NODE;
  
  NODE *create();
  
  void inorder(NODE *);
  void insert(NODE *);
  void t_cpy(NODE *,NODE *);
  
  int main()
  {
  
  int n,m;
  do
  {
      printf("\n1.create tree 1\n2.Insert element to tree1\n3.create tree 2\n4.Insert element to tree2\n5.Inorder tree1\n6.Inorder tree2\n7.Copy tree2 to tree1\n8.exit\n\n");
      printf("\nEnter ur choice: ");
      scanf("%d",&amp;m);
      switch(m)
      {
          case 1: tmp=create();
                  break;
          case 2: insert(tmp);
                  break;
          case 3: tmp2=create();
                  break;
          case 4:
                  insert(tmp2);
                  break;
          case 5: printf("\n\nInorder Tree1: ");
                  inorder(tmp);
                  break;
          case 6: printf("\n\nInorder Tree 2: ");
                  inorder(tmp2);
                  break;
          case 7: t_cpy(tmp,tmp2);
  
                  break;
          case 8: return(0);
      }
  }while(n!=8);
  return(0);
  }
  void insert(NODE *root)
  {
      NODE *newnode;
      if(root==NULL)
      {
          newnode=create();
          root=newnode;
      }
      else
      {
          newnode=create();
          while(1)
          {
              if(newnode-&gt;data&lt;root-&gt;data)
              {
                  if(root-&gt;llink==NULL)
                  {
                      root-&gt;llink=newnode;
                      break;
                  }
                  root=root-&gt;llink;
              }
              if(newnode-&gt;data&gt;root-&gt;data)
              {
                  if(root-&gt;rlink==NULL)
                  {
                      root-&gt;rlink=newnode;
                      break;
                  }
                  root=root-&gt;rlink;
              }
          }
      }
  }
  
  NODE *create()
  {
      NODE *newnode;
      int n;
      newnode=(NODE *)malloc(sizeof(NODE));
      printf("\n\nEnter the Data ");
      scanf("%d",&amp;n);
      newnode-&gt;data=n;
      newnode-&gt;llink=NULL;
      newnode-&gt;rlink=NULL;
  return(newnode);
  }
  
  
  void t_cpy(NODE *t1,NODE *t2)
  {
      int val,opt=0;
      NODE *temp;
      if(t1==NULL || t2==NULL)
      {
          printf("Can not copy !\n");
      }
      inorder(t1);
  
      printf("\nEnter the node value where tree 2 should be copied\n");
      scanf("%d",&amp;val);
      temp=t1;
      while(temp!=NULL)
      {
          if(val&lt;temp-&gt;data)
              temp=temp-&gt;llink;
          else
              temp=temp-&gt;rlink;
      }
      if(temp-&gt;llink!=NULL || temp-&gt;rlink!=NULL)
          printf("Not possible to copy tree to this node\n");
      else
      {
          printf("Copy tree to \n 1.Left Node \n 2.Right Node\n Enter your choice : ");
          scanf("%d",&amp;opt);
          if(opt==1)
          {
              temp-&gt;llink=t2;
          }
          else if(opt==2)
          {
              temp-&gt;rlink=t2;
          }
          else
              printf("Invalid choice\n");
      }
      printf("Tree1 after copying is\n");
      inorder(temp);
  }
  
  
  void inorder(NODE *tmp)
  {
      if(tmp!=NULL)
      {
          inorder(tmp-&gt;llink);
          printf("%d",tmp-&gt;data);
          inorder(tmp-&gt;rlink);
      }
  }
  </code></pre>
  
  <p>EDIT : Thanks to @xaxxon , who helped me with this.
  Just update the while to make it work :</p>
  
  <pre><code>while(temp!=NULL&amp;&amp;temp-&gt;data!=val)
  {
      if(val&lt;temp-&gt;data)
          temp=temp-&gt;llink;
      else
          temp=temp-&gt;rlink;
      if(temp-&gt;llink==NULL &amp;&amp; temp-&gt;rlink==NULL &amp;&amp; temp-&gt;data!=val)
      { 
          printf("Invalid Node value entered !\n");
          //break;
          return 0;
      }
  </code></pre>
  
  <p>and, Now it works fine for if entered value is present in the tree. </p>
  
  <p>Thanks :)</p>