Popular Posts

Wednesday, December 7, 2011

Binary Search Tree in c++

/* This is a Binary Search tree  program
 * using Template.It has implemented insert, findMin,
 * print, search, Remove.
 */

#include <iostream>
#include <cstdlib>
using namespace std;

/* Template class BST(Binary Search Tree Implementation) */
template<class T>
class BST{
private:
  T data;
  BST *left;
  BST *right;
public:
  BST(T d = 0,BST<T> *l = NULL,BST<T> *r = NULL):data(d),left(l),right(r){};
  void insert(T val);
  void print();
  bool search(T val);
  BST<T>* remove(T val,BST<T>*);
  BST<T>* findMin(BST<T>*);
};

template<class T>
BST<T>* BST<T> :: findMin(BST<T>* t){
 
  while(t ->left != NULL){
    t = t->left;
  }
  return t;
}

template<class T>
void BST<T>:: insert(T val)
{
  if(val < data && left == NULL){
    left = new BST<T>(val);
    return;
  }else if(val > data && right == NULL){
    right = new BST<T>(val);
    return;
  }else if(val < data){
    left ->insert(val);
    return;
  }else if(val > data){
    right ->insert(val);
    return;
  }else{
    cout <<"Duplicate value";
  }
}
template<class T>
void BST<T> ::print()
{
  if(left != NULL)
    left->print();
  cout<<"\n"<<data;
  if(right != NULL)
    right->print();
   
}

template<class T>
bool BST<T>:: search(T val)
{
  if(data == val){
    return true;
  }else if(val < data && left != NULL){
    left -> search(val);
  }else if(val > data && right != NULL){
    right -> search(val);
  }else{
    return false;
  }
}



template<class T>
BST<T>* BST<T> :: remove(T val,BST<T> *temp)
{
  BST<T> *tempcell;
  if(temp == NULL){
    cout<<"\nElement not found";
  }else if(val < temp -> data){
    temp->left = temp->left->remove(val,temp->left);
  }else if(val > temp -> data){
    temp->right = temp->right->remove(val,temp->right);
  }else if(temp->left && temp->right){
    tempcell =findMin(temp->right);
    temp->data =tempcell->data;
    temp->right=temp->right->remove(temp->data,temp->right);
  }else{
    tempcell=temp;
    if(temp->left == NULL)
      temp =temp->right;
    else
      temp = temp->left;
    delete tempcell;
  }
  return temp;
}


int main()
{
  BST<int> *root = NULL;
  int choice;
  while(1){
    cout<<"\n 1.Insert.";
    cout<<"\n 2.Delete.";
    cout<<"\n 3.Search";
    cout<<"\n 4.Print";
    cout<<"\n 5.Exit";
    cout<<"\n Enter your choice:";
    cin>>choice;
    switch(choice){

    case 1:
      int val;
      cout<<"\n Enter the value to insert:";
      cin>>val;
      if(root == NULL){
    BST<int> b(val);
    root = &b;
      }else{
    root -> insert(val);
      }
      break;

    case 2:
      cout<<"\n Enter the value to remove:";
      cin>>val;
      if(root == NULL){
    cout<<"\nEmpty tree";
      }else{
    root = root->remove(val,root);
      }
      break;

    case 3:
      cout<<"\n Enter the value to search:";
      cin>>val;
      if(root == NULL){
    cout<<"\n Empty tree.";
      }else if(root->search(val)){
    cout<<"\n The data "<<val <<" present in the Tree";
      }else{
    cout<<"\n The data "<<val <<" not present in the Tree";
      }
      break;
     
    case 4:
      if(root == NULL){
    cout<<"\n Empty Tree.";
      }else{
    root->print();
      }
      break;

    case 5:
      exit(0);
      break;

    default:
      cout<<"Enter correct choice";
    }
  }
}

No comments:

Post a Comment