title: Removing element from a binary search tree
body: <p>Here is my <code>Binarysearchtree.h</code>. The remove function is not working when the     element that has to be deleted has one child.</p>
  
  <pre><code>#ifndef BINARYSEARCHTREE_H_INCLUDED
  #define BINARYSEARCHTREE_H_INCLUDED
  
  class BinarySearchTreeNode {
      public:
      int data;
      BinarySearchTreeNode* left;
      BinarySearchTreeNode* right;
  
      BinarySearchTreeNode( int data):data(data), left(NULL), right(NULL) {
      }
  
  
      ~BinarySearchTreeNode() {
          if (left)
              delete left;
          if (right)
              delete right;
      }
      friend class BinarySearchTree;
  };
  
  class BinarySearchTree
  {
      private:
       int size;
  
      public:
      BinarySearchTreeNode * root;
  
      BinarySearchTree()
      {
          size=0;
          root= NULL;
      }
  
      private:
      void insert_help(BinarySearchTreeNode*root,BinarySearchTreeNode*node)
      {
          if(root==NULL)
             {
                 this-&gt;root=node;
                 return;
             }
  
           if(node-&gt;data&gt;root-&gt;data and root-&gt;right==NULL)
                  {
                      root-&gt;right=node;
                      return;
                  }
            if(node-&gt;data&lt;=root-&gt;data and root-&gt;left==NULL)
                  {
                     root-&gt;left=node;
                      return;
                  }
  
          if(node-&gt;data&lt;=root-&gt;data)
              insert_help(root-&gt;left,node);
          if(node-&gt;data&gt;root-&gt;data)
              insert_help(root-&gt;right,node);
  
  
  
  
      }
  
  
     public:
      void insert(int data)
      {
          BinarySearchTreeNode* newnode=new BinarySearchTreeNode(data);
          size++;
          insert_help(this-&gt;root,newnode);
  
      }
  
      void printLevelWise()
  {
      if(root==NULL)
          return;
  
     queue&lt;BinarySearchTreeNode*&gt; pending;
     pending.push(root);
     while(pending.empty()!=1)
     {
         BinarySearchTreeNode*latest=pending.front();
         pending.pop();
         cout&lt;&lt;latest-&gt;data&lt;&lt;": ";
         if(latest-&gt;left)
         {
             cout&lt;&lt;latest-&gt;left-&gt;data&lt;&lt;",";
         pending.push(latest-&gt;left);
         }
         if(latest-&gt;right)
          {
              cout&lt;&lt;latest-&gt;right-&gt;data&lt;&lt;",";
              pending.push(latest-&gt;right);
          }
          cout&lt;&lt;"\n";
     }
  }
  
  
  bool isEmpty()
  {
      if(root==NULL)
          return true;
      else
          return false;
  }
  
  int Size()
  {
      return size;
  }
  
     private:
         BinarySearchTreeNode* Search_help(BinarySearchTreeNode*root,int data)
         {
             if(root==NULL or root-&gt;data==data)
          return root;
  
      if(data&gt;root-&gt;data)
         return Search_help(root-&gt;right,data);
      if(data &lt;= root-&gt;data)
         return Search_help(root-&gt;left,data);
  
  
  
         }
  
  
  
  public:
  
  BinarySearchTreeNode* Search(int data)
  {
    return Search_help(root,data);
  
  
  }
  private:
  
   BinarySearchTreeNode*return_min(BinarySearchTreeNode*root)
   {
       if(root-&gt;left==NULL)
          return root;
       return return_min(root-&gt;left);
   }
  
  /*this is my remove function*/
  BinarySearchTreeNode* remove_help(BinarySearchTreeNode*root,int data) 
  
  {
      if(root==NULL)
          return root;
  
      if(data&gt;root-&gt;data)
        root-&gt;right=  remove_help(root-&gt;right,data);
  
     else if(data&lt;root-&gt;data)
          root-&gt;left=remove_help(root-&gt;left,data);
  
      else
      {
          //no child
          if(root-&gt;left==NULL and root-&gt;right==NULL)
          {
              delete root;
              return NULL;
          }
           //one child
           if(root-&gt;left==NULL)
           {
               BinarySearchTreeNode*temp=root-&gt;right;
  
               delete root; // when i remove this line the code works idk why
               return temp;  
  
           }
           if(root-&gt;right==NULL)
           {
               BinarySearchTreeNode*temp=root-&gt;left;
               delete root;
               return temp;
  
           }
           //more than one child
  
           BinarySearchTreeNode*temp=return_min(root-&gt;right);
           root-&gt;data=temp-&gt;data;
           root-&gt;right=remove_help(root-&gt;right,temp-&gt;data);
  
  
  
      }
  
      return root; 
      }
  
  public:
  void Remove(int data)
  {
      root=remove_help(root,data);
      return;
  
  }
  
  };
  
  #endif // BINARYSEARCHTREE_H_INCLUDED
  ////////////////////////////////
  </code></pre>
  
  <p>This program is working for when the node has no child or more than two childs but not one child.</p>
  
  <pre><code>/*here is my main program*/
  
  #include&lt;iostream&gt;
  #include"tree.h"
  #include&lt;limits.h&gt;
  #include&lt;math.h&gt;
  #include&lt;stack&gt;
  #include"BinaryTree.h"
  #include"linked_list.h"
  #include&lt;vector&gt;
  #include"BinarySearchTree.h"
  using namespace std;
  
  int main()
  {
  BinarySearchTree bst;
  bst.insert(6);
  bst.insert(3);
  bst.insert(9);
  bst.insert(4);
  bst.insert(8);
  bst.printLevelWise();
  
  bst.Remove(9);
  
  bst.printLevelWise();
  
  }
  </code></pre>
  
  <p><em>/i dont know just commenting as my question needed more context</em>/</p>