title: Deleting the entire binary search tree at once
body: <p>I have been trying to implement the delete BST function but I don't know why it is not working, I think it's logically correct. Can any body please tell me, why I'm getting run time error and how should I correct it.</p>
  
  <pre><code>  #include &lt;iostream&gt;
    using namespace std;
  
  class node{
  public:
  int data;
  node *right;
  node *left;
  node(){
      data=0;
      right=NULL;
      left=NULL;
        }
  };
  
  class tree{
  node *head;
  int maxheight;
       public:
  tree(){head=0;maxheight=-1;}
  bool deletenode(int key,node* root);
  int get_height(){return maxheight;}
  void insert(int key);
  void pre_display(node* root);
       void delete_tree(node *root);
       node* get_head(){return head;}
           };
  
  void tree::insert(int key){
       node *current=head;
      node *newnode=new node;
  
      if(newnode==NULL)
      throw(key);
  
      newnode-&gt;data=key;
      int height=0;
  
  if(head==0){
  head=newnode;
       }
  else
  {
      while(1){
      if(current-&gt;right==NULL &amp;&amp; current-&gt;data &lt; newnode-&gt;data)
      {
          current-&gt;right=newnode;
          height++;
          break;
      }
      else if(current-&gt;left==NULL &amp;&amp; current-&gt;data &gt; newnode-&gt;data)
      {
          current-&gt;left=newnode;
          height++;
          break;
      }
      else if(current-&gt;right!=NULL &amp;&amp; current-&gt;data &lt; newnode-&gt;data)
      {
           current=current-&gt;right;
           height++;
     }
      else if(current-&gt;left!=NULL &amp;&amp; current-&gt;data &gt; newnode-&gt;data)
      {
                 current=current-&gt;left;
            height++;
      }
           }
   }
   if(height&gt;maxheight)
   maxheight=height;
   }
  
   void tree::pre_display(node *root){
   if(root!=NULL)
   {
   cout&lt;&lt;root-&gt;data&lt;&lt;" ";
   pre_display(root-&gt;left);
   pre_display(root-&gt;right);
   }
   }
  
   void tree::delete_tree(node *root){
    if(root!=NULL)
   {
   delete_tree(root-&gt;left);
   delete_tree(root-&gt;right);
   delete(root);
   if(root-&gt;left!=NULL)
   root-&gt;left=NULL;
   if(root-&gt;right!=NULL)
   root-&gt;right=NULL;
   root=NULL;
   }
   }
  
  int main(){
  tree BST;
  int arr[9]={17,9,23,5,11,21,27,20,22},i=0;
  
  for(i=0;i&lt;9;i++)
  BST.insert(arr[i]);
  
  BST.pre_display(BST.get_head());
  cout&lt;&lt;endl;
  BST.delete_tree(BST.get_head());
  BST.pre_display(BST.get_head());
  cout&lt;&lt;endl;
  
  system("pause");
  return 0;
  }
  </code></pre>
  
  <p>All the other functions are working correctly, you just need to check the <code>delete_tree</code> function, the other code is provided to give the idea of the structure of my BST.</p>