ADSL Dictionary

//============================================================================
// Name        : demo.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <string.h>
//#include <cstring>
using namespace std;
class node
{
public:
    char word[20];
    char mng[40];
    node*lptr;
    node*rptr;
};
class BST
{
public:
    node *root, *temp;
    int top;
    int st[50];
    int l,l1,l2;
    int strlen(char*);
    void  create();
    node* insert(node*,node*);
    void display();
    void inorder(node*);
    void search(node*,char[]);
    void modify(node*,char[]);
    node* FindMin(node*);
    node* delet(node* ,char[]);
    BST()
    {  root=temp=NULL;
         top=-1;
         l1=l=l2=0;
    }
};
int main()
    {
        char srch[20];
        int ans,ch;
        BST b;
        do
        {
            cout<<"\n\t\t***MENU***\n1:Create \n 2:Display " ;
            cout<<"\n 3.Search \n 4.Modify\n 5.Delete\n 0.EXIT ";
            cout<<"\n Enter your choice ";
            cin>>ch;
            switch(ch)
            {
            case 1: b.create();
            break;
            case 2: b.inorder(b.root);
            break;
            case 3:cout<<"\nEnter the word to search:\t ";
                cin>>srch;
                b.search(b.root,srch);
            break;
            case 4:cout<<"\nEnter the word to modify:\t ";
                 cin>>srch;
                b.modify(b.root,srch);
            break;
            case 5:
                cout<<"\nEnter the word to delete:\t ";
                cin>>srch;
                b.delet(b.root,srch);
                break;
            }
        }while(ans==0);
}
void BST::modify(node* root,char srch[])
{
        l1=strlen(srch);
        l2=strlen(root->word);
        if(l1<l2)
        {
            l=l1;
        }
        else
        {
            l=l2;
        }
        if(strcmp(root->word,srch)==0)
        {
            cout<<"\nThe word has been found ";
            cout<<"\nWord is "<<root->word;
            cout<<"\nIt's meaning is "<<root->mng;
            cout<<"\nEnter new meaning for the word ";
            cin>>root->mng;
            inorder(root);
        }
        else if(strcmp(root->word,srch)!=0)
        {
            for(int k=0;k<l;k++)
            {
                if(root->word[k]>srch[k])
                {
                    modify(root->lptr,srch);
                }
                else if(root->word[k]<srch[k])
                {
                    modify(root->rptr,srch);
                    break;
                }
            }
        }
}
void BST::search(node* root,char srch[])
{
    int flag=1;
    l1=strlen(srch);
    l2=strlen(root->word);
    if(l1<l2)
    {
        l=l1;
    }
    else
    {
        l=l2;
    }
    if(strcmp(root->word,srch)==0)
    {
        cout<<"\nThe word has been found \n ";
        cout<<"\nWord is "<<root->word<<"\n";
        cout<<"\nIt's meaning is "<<root->mng;
        flag=0;
        if(flag==1)
        {
            cout<<"\n word not found! ";
        }

    }
    else if(strcmp(root->word,srch)!=0)
    {
        for(int k=0;k<l;k++)
        {
            if(root->word[k]>srch[k])
            {
                search(root->lptr,srch);
            }
            else if(root->word[k]<srch[k])
            {
                search(root->rptr,srch);
                break;
            }
        }
    }
}
    void BST::create()
    {
        char ans1;
        do
        {
            temp=new node;
            cout<<"\nEnter any word ";
            cin>>temp->word;
            cout<<"\nEnter meaning of the word ";
            cin>>temp->mng;
            temp->lptr=NULL;
            temp->rptr=NULL;

            if(root==NULL)
                root=temp;
            else
                insert(root,temp);
            cout<<"Do you want to continue?(y/n)";
            cin>>ans1;
        }while(ans1=='Y' || ans1=='y');
    }

    int BST::strlen(char* x)
    {
        int c=0;
        for(int j=0;x[j]!='\0';j++)
        {
            ++c;
        }
        return c;
    }

    node* BST::insert(node*root,node*temp)
    {
        l1=strlen(temp->word);
        l2=strlen(root->word);
        if(l1<l2)
        {
            l=l1;
        }
        else
        {
            l=l2;
        }
        for(int i=0;i<=l;i++)
        {
            if(root->word[i]>temp->word[i])
            {
                if(root->lptr==NULL)
                {
                    root->lptr=temp;
                    return root;
                }
                else
                {
                    root->lptr=insert(root->lptr,temp);
                    return root;
                }
            }
            else if(root->word[i]<temp->word[i])
            {
                if(root->rptr==NULL)
                {
                    root->rptr=temp;
                    return root;
                }
                else
                {
                    root->rptr=insert(root->rptr,temp);
                    return root;
                }
            }
        }
        return root;
    }


        void BST::inorder(node* temp)
        {
            if(temp!=NULL)
                {
                    inorder(temp->lptr);
                    cout<<" | "<<temp->word<<"\t|\t"<<temp->mng<<" | \n";
                    inorder(temp->rptr);
                }
         }


        node* BST::delet(node *root , char src[])
        {
            if(root != NULL)
            {
               if((strcmp(root->word , src)) > 0)
               {
                    root->lptr = delet(root->lptr , src);
               }
               else if((strcmp(root->word , src)) < 0)
                {
                    root->rptr = delet(root->rptr , src);
                 }
               else
               {
                   if(root == NULL && root->rptr == NULL)
                   {
                     delete(root);
                     root = NULL;
                   }

                   else if(root->lptr == NULL)
                   {
                       node *temp = root;
                       root = root->rptr;
                       delete(temp);
                   }

                   else if(root->rptr == NULL)
                   {
                       node *temp = root;
                       root = root->lptr;
                       delete(temp);
                   }

                   else
                   {
                       node *temp =  FindMin(root->rptr);
                       strcpy(root->word , temp->word);
                       strcpy(root->mng , temp->mng);
                                  root->rptr = delet(root->rptr , temp->word);
                       delete(temp);
                   }
               }
            }
            return root;
        }

        node* BST:: FindMin(node* root)
        {
          while(root->lptr!= NULL) root = root->lptr;
          return root;
        }

Comments

Popular Posts