title: Binary Search Tree Insertion using a void function
body: <p>I have written two different codes for inserting into a binary tree, one works whereas other doesn't.</p>
  
  <p>This is how my node looks:</p>
  
  <pre><code>struct node
  {
      int data;
      node *left;
      node *right;
  };
  </code></pre>
  
  <p>The following is the code for <code>node* newnode(int a)</code>  </p>
  
  <pre><code>node* newnode(int a)
  {
      node *temp=new node;
      temp-&gt;data=a;
      temp-&gt;left=nullptr;
      temp-&gt;right=nullptr;
      return temp;
  }
  </code></pre>
  
  <p>And following are the two different codes for insertion:</p>
  
  <p>This one returns a pointer to the node:</p>
  
  <pre><code> node* insertion(node *root, int a)
   {
      if(root==nullptr)
          return newnode(a);
      else if(a&lt;root-&gt;data)
          root-&gt;left=insertion(root-&gt;left, a);
      else
          root-&gt;right=insertion(root-&gt;right, a);
   }
  </code></pre>
  
  <p>This one returns void:</p>
  
  <pre><code>void insertion2(node *root,int a)
  {
      if(root==nullptr)
          root=newnode(a);
      else if(a&lt;root-&gt;data)
          insertion2(root-&gt;left,a);
      else
          insertion2(root-&gt;right,a);
  }
  </code></pre>
  
  <p>The one which returns void doesn't work. And as per the analysis I made, after the function call, <code>root</code> is still <code>nullptr</code>. Can anyone explain me why does it not work?</p>