title: delete element BST
body: <p>When I delete 25 from BT than it works perfectly.
  But when I want to delete another number like 17,
  it deletes 17 with some other number are deleted from BT.</p>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  
  struct node
  {
  
      int data;
      node *left;
      node *right;
  };
  
  node *root = NULL;
  
  node* createnode(int data)
  {
  
      node *t = new node();
      t-&gt;data = data;
      t-&gt;left = NULL;
      t-&gt;right = NULL;
      return t;
  
  }
  
  node* min(node*);
  
  node* insert(node *root, int data)
  {
  
      if (root == NULL)
      {
  
  
          root = createnode(data);
          return root;
      }
  
      else{
  
          node *t = root;
          if (data &lt;= t-&gt;data)
              t-&gt;left = insert(t-&gt;left, data);
          else{
              t-&gt;right = insert(t-&gt;right, data);
          }
      }
  }
  
  node* print(node *root)
  {
  
      if (root == NULL)
      {
  
          return root;
      }
  
      else{
  
          cout &lt;&lt; root-&gt;data &lt;&lt; " ";
          print(root-&gt;left);
          print(root-&gt;right);
      }
  
      return root;
  }
  
  node* deleten(node *root, int data)
  {
  
      if (root == NULL)
          return root;
      else{
          if (data&lt;root-&gt;data)
              root-&gt;left = deleten(root-&gt;left, data);
          if (data&gt;root-&gt;data)
              root-&gt;right = deleten(root-&gt;right, data);
          else{
              if (root-&gt;left == NULL &amp;&amp; root-&gt;right == NULL)
              {
                  node *t = root;
                  root = NULL;
                  delete t;
              }
              else if (root-&gt;left == NULL)
              {
                  node *t = root;
                  root = root-&gt;right;
                  delete t;
              }
              else if (root-&gt;right == NULL)
              {
                  node *t = root;
                  root = root-&gt;left;
                  delete t;
              }
              else{
                  node *t = min(root-&gt;right);
                  root-&gt;data = t-&gt;data;
                  root-&gt;right = deleten(root-&gt;right, t-&gt;data);
              }
          }
          return root;
      }
  }
  
  node* min(node *root)
  {
  
      if (root == NULL)
          return root;
      else{
          if (root-&gt;left != NULL)
              min(root-&gt;left);
          else{
              return root;
          }
      }
  }
  
  int main()
  {
  
      root = insert(root, 15);
      root = insert(root, 10);
      root = insert(root, 8);
      root = insert(root, 12);
      root = insert(root, 11);
      root = insert(root, 20);
      root = insert(root, 23);
      root = insert(root, 17);
      root = insert(root, 25);
      root = print(root);
      cout &lt;&lt; endl;
      root = deleten(root, 17);
      root = print(root);
  }
  </code></pre>
  
  <p>case 1:</p>
  
  <p>before deletion : 15 10 8 12 11 20 17 23 25</p>
  
  <p>after  deletion : 15 10 8 12 11 23 25 // root=deleten(root,17);</p>
  
  <p>expectation         :    15 10 8 12 11 20 23 25</p>
  
  <p>case 2:</p>
  
  <p>before deletion : 15 10 8 12 11 20 17 23 25</p>
  
  <p>after  deletion : 15 10 8 12 11 20 17 23 // root=deleten(root,25);</p>
  
  <p>expectation      : 15 10 8 12 11 20 17 23 </p>
  
  <p>case 3:</p>
  
  <p>before deletion : 15 10 8 12 11 20 17 23 25</p>
  
  <p>after  deletion  : 17 12 11 23 25         // root= deleten(root,8);</p>
  
  <p>expectation      : 15 10 12 11 20 17 23 25</p>