Tuesday 9 July 2013

Program to implement the traversals (Postorder , Inorder , Postorder) and searching in Binary Search Tree in c++

#include<iostream>
using namespace std;
class Btree
{
      private:
      int data;
      Btree *left;
      Btree *right;
      Btree *root;
      void insert(Btree* troot,int value);
      void destroy(Btree* troot);
      void preOrder(Btree *troot);
      void inOrder(Btree *troot);
      void postOrder(Btree *troot);
      void search(Btree *troot , int value);
     
      public:
             Btree();
             void insert(int value);
             void preOrder();
             void inOrder();
             void postOrder();
             void search(int value);
             void destroy();
           
};
     
     
      Btree::Btree()
      {
             data = 0;
       
             left = NULL;
             right = NULL;
             root = NULL;
      }
      void Btree:: insert(int value)
      {
           if( root == NULL )
           {
               Btree *temp = new Btree();
           temp -> data = value;
                  root = temp;
                  temp -> left = NULL;
                  temp -> right = NULL;
             
           }
           else
           {
               insert(root , value);
           }
       }
     
      void Btree :: insert( Btree *troot , int value)
      {
         
           if(troot -> data > value)
           {
                if( troot -> left == NULL)
                {
                     Btree *temp = new Btree();
                     temp -> data = value;
                     troot -> left = temp;
                     temp -> left = NULL;
                     temp -> right = NULL;
                   
                }
                else
                {
               
                    insert(troot->left , value);
                }
           }
           else if(troot -> data < value)
           {
                if( troot -> right == NULL )
                {
                    Btree *temp = new Btree();
                    temp -> data = value;
                    troot -> right = temp;
                    temp -> left = NULL ;
                    temp -> right = NULL;
                   
                }
                else
                {
                 insert (troot->right , value);
                }
           }
           else
           {
               cout << " The given data is already in the the tree " << endl;
           }
      }  
   
      void Btree :: preOrder()
      {
           cout << "Preorder Traversal " << endl;
           preOrder(root);
       }
     
        void Btree :: preOrder(Btree* troot)
       {
            if(troot != NULL)
            {
            cout<<troot -> data<<endl;
            preOrder(troot -> left);
            preOrder(troot -> right);
            }
        }
     
       void Btree :: inOrder()
       {
            cout << " Inorder Traversal " << endl;
            inOrder(root);
        }
       
        void Btree :: inOrder (Btree* troot)
        {
           
             if(troot != NULL)
             {
             inOrder(troot -> left);
             cout << troot -> data <<endl;
             inOrder(troot -> right);
             }
         }
     
     
        void Btree:: postOrder()
        {
             cout << "Postorder Traversal "<<endl;
             postOrder(root);
        }
        void Btree :: postOrder(Btree* troot)
        {
             if(troot != NULL)
             {
             postOrder(troot -> left);
             postOrder(troot -> right);
             cout << troot -> data <<endl;
             }
         }
       
         void Btree :: search(int value)
         {
             
              search(root , value);
             
         }
         void Btree :: search(Btree* troot , int value)
         {
              if(troot -> data == value)
                  {
                           cout <<"The value is present"<<endl;
                  }
             
              else if( (value < troot -> data) && (troot -> left != NULL))
              {
                 
                 
                      search(troot -> left , value);
                 
                 
              }
              else if ( (value > troot -> data) && (troot -> right != NULL) )
              {
         
                       search (troot -> right , data);
                 
              }
              else
              {
                  cout << "The value is not in the Tree " << endl;
              }
             
         }
       
       void Btree :: destroy()
      {
           destroy(root);
      }
     
      void Btree :: destroy(Btree* troot)
      {
           if(troot != NULL)
           {
                    destroy(troot -> left);
                    destroy(troot -> right);
                    delete troot;
           }
      }
     
      int main()
      {
          Btree obj;
          int data = 0;
         obj.insert(40);
         obj.insert(60);
         obj.insert(50);
         obj.insert(33);
         obj.insert(55);
         obj.insert(11);
         obj.insert(10);
       
         obj.preOrder();   // Preorder traversal
         obj.inOrder();    // inorder traversal
         obj.postOrder();  // postorder traversal
       
         cout << " Enter the Data to be searched " << endl;
         cin >> data;
         obj.search(data);  // we can also return the node
         obj.destroy();
         
          cin.get();
          cin.get();
          return 0;
}
                                                                                  BY: SAURABH BHATIA          
     

No comments:

Post a Comment