title: Binary Search Tree Implementation(C++)
body: <p>Most of the code is from Weiss' "Data structures and algorithm analysis in C++", after I typed the code on CodeBlock(gnu,gcc), compiler reported no error, but then I tried to create an instance and test some functions it went wrong. It seems there's something wrong with BST() or insert() because the program stucked instantly since running. 
    Would someone help me find out how to solve this problem?　Great thanks!!!</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  struct TreeNode
  {
      int element;
      TreeNode*left,*right;
      TreeNode(const int&amp;e,TreeNode*le=NULL,TreeNode*rt=NULL):element(e),left(le),right(rt){};
  };
  
  class BST
  {
  public:
      BST(){root=NULL;};
      ~BST(){ makeEmpty(root); };
  // use public member function to call private member functions.
      void insert(const int &amp; x){ insert(x, root); }
      void remove(const int &amp; x){ remove(x, root); }
      TreeNode*findMin() const{ return findMin(root); }
      bool contain(const int &amp; x) const{ return contain(x,root); }
      void printNodes() const{ printNodes(root); }
  
  private:
      TreeNode*root;
  
      void makeEmpty(TreeNode*&amp;);
      TreeNode* findMin(TreeNode*) const;
      void insert(const int &amp;, TreeNode*) const;
      void remove(const int &amp;, TreeNode*) const;
      bool contain(const int &amp;, TreeNode*) const;
      void printNodes(TreeNode*) const;
  };
  
  void BST::makeEmpty(TreeNode*&amp;t)
  {
      if(t!=NULL)
      {
          makeEmpty(t-&gt;left);
          makeEmpty(t-&gt;right);
          delete t;
      }
      t=NULL;
  }
  TreeNode* BST::findMin(TreeNode*t) const
  {
      if(t-&gt;left==NULL)    return t;
      return findMin(t-&gt;left);
  }
  void BST::insert(const int &amp; x, TreeNode* t) const
  {
      if(t==NULL) t=new TreeNode(x,NULL,NULL);
      else if(x &lt; t-&gt;element)    insert(x,t-&gt;left);
      else if(x &gt; t-&gt;element)    insert(x,t-&gt;right);
      else;   /// duplicate, do nothing
  }
  void BST::remove(const int &amp; x, TreeNode* t) const
  {
      if(t==NULL) return;
      if(t-&gt;element &gt; x)    remove(x, t-&gt;left);
      else if(t-&gt;element &lt; x)  remove(x, t-&gt;right);
      else if(t-&gt;left!=NULL &amp;&amp; t-&gt;right!=NULL)
      {
          t-&gt;element=findMin(t-&gt;right)-&gt;element;
          remove(t-&gt;element,t-&gt;right);
      }
      else
      {
          TreeNode*oldNode=t;
          t=(t-&gt;left==NULL)?t-&gt;right:t-&gt;left;
          delete oldNode;
      }
  }
  bool BST::contain(const int &amp; x, TreeNode*t) const
  {
      if(t==NULL)    return false;
      else if(x&lt;t-&gt;element)    return contain(x,t-&gt;left);
      else if(x&gt;t-&gt;element)    return contain(x,t-&gt;right);
      else    return true;
  }
  void BST::printNodes(TreeNode*t) const
  {
      if(t==NULL) return;
      cout&lt;&lt;t-&gt;element&lt;&lt;" ";
      printNodes(t-&gt;left);
      printNodes(t-&gt;right);
      cout&lt;&lt;endl;
  };
  </code></pre>
  
  <p>Here's the code I wrote to test class BST:</p>
  
  <pre><code>int main()
  {
      BST BinarySearchTree;
      int element,node;
      for(int i=0; i&lt;5; i++)
      {
          cin&gt;&gt;element;
          BinarySearchTree.insert(element);
      }
      BinarySearchTree.printNodes();
  
      cout&lt;&lt;BinarySearchTree.findMin()-&gt;element&lt;&lt;endl;
  
      cin&gt;&gt;node;
      if(BinarySearchTree.contain(node)){ cout&lt;&lt;"item "&lt;&lt;node&lt;&lt;" is in BST."&lt;&lt;endl; }
  
      BinarySearchTree.remove(node);
      BinarySearchTree.printNodes();
      return 0;
  }
  </code></pre>