title: Binary search tree delete not working, why?
body: <p>I am trying to implement Binary search tree in C.
  But I am stuck at the delete operation, when I run the code it doesn't delete the value specified.</p>
  
  <p>Before calling delete:(calling <code>inorder()</code>)</p>
  
  <pre><code>16 19 23
  </code></pre>
  
  <p>After calling delete:(calling <code>inorder()</code>)</p>
  
  <pre><code>16 19 23
  </code></pre>
  
  <p>code:</p>
  
  <pre><code>void deleteNode(struct node *n, int data)
  {
       struct node *temp;
       if(n-&gt;data==data)
       {
         if(n-&gt;left == NULL &amp;&amp; n-&gt;right == NULL)
         {
          n=NULL;
         }
         else if(n-&gt;left == NULL &amp;&amp; n-&gt;right!=NULL)
         {
          n-&gt;data = (n-&gt;right)-&gt;data;
          n-&gt;right = NULL;
         }
         else if(n-&gt;left!=NULL &amp;&amp; n-&gt;right == NULL)
         {
          n-&gt;data = (n-&gt;left)-&gt;data;
          n-&gt;left=NULL;
         }
         else if(n-&gt;left != NULL &amp;&amp; n-&gt;right != NULL)
         {
          temp = findMax(root);
          n-&gt;data = temp-&gt;data;
          temp = NULL;
         }
       }
       else if(n-&gt;data &gt; data)
       {
        deleteNode(n-&gt;left, data);
       }
       else if(n-&gt;data &lt; data)
       {
        deleteNode(n-&gt;right, data);
       }
  }
  </code></pre>
  
  <p>I have other code which is working, but I want to know what is wrong with this code?</p>
  
  <p><strong>Edit:</strong>
  I have edited the code with a few changes in it.</p>
  
  <p>Now, When I try to delete the <strong><em>ROOT</em></strong> node.
  I end up with this:
  (inorder traversal)-> <code>16 23 23</code></p>
  
  <p>Now, 
  Why is this happening when <code>temp = NULL</code> is making the maximum node NULL.</p>
  
  <p>Note: I am not initializing temp as the code has been changed and it is initialized just before its use (<code>temp = findMax(root)</code>).</p>
  
  <p>code inorder():</p>
  
  <pre><code>void inorder(struct node *root)
  {
      if(root!=NULL)
      {
           inorder(root-&gt;left);
           printf("%d\n", root-&gt;data);
           inorder(root-&gt;right);
      }
  }
  </code></pre>