Expdsa 8
Expdsa 8
Assignment 8
Input :
#include<iostream>
#include<string.h>
class dict
dict *root,*node,*left,*right,*tree1;
string s1,s2;
int flag,flag1,flag2,flag3,cmp;
public:
dict()
flag=0,flag1=0,flag2=0,flag3=0,cmp=0;
root=NULL;
void input();
void create_root(dict*,dict*);
void check_same(dict*,dict*);
void input_display();
void display(dict*);
void input_remove();
dict* remove(dict*,string);
dict* findmin(dict*);
void input_find();
dict* find(dict*,string);
void input_update();
dict* update(dict*,string);
};
void dict::input()
node=new dict;
cin>>node->s1;
cin.ignore();
getline(cin,node->s2);
create_root(root,node);
int i=0,result;
char a[20],b[20];
if(root==NULL)
root=new dict;
root=node1;
root->left=NULL;
root->right=NULL;
return;
for(i=0;node1->s1[i]!='\0';i++)
a[i]=node1->s1[i];
}
for(i=0;tree->s1[i]!='\0';i++)
b[i]=tree->s1[i];
result=strcmp(b,a);
check_same(tree,node1);
if(flag==1)
flag=0;
else
if(result>0)
if(tree->left!=NULL)
create_root(tree->left,node1);
else
tree->left=node1;
(tree->left)->left=NULL;
(tree->left)->right=NULL;
return;
else if(result<0)
if(tree->right!=NULL)
{
create_root(tree->right,node1);
else
tree->right=node1;
(tree->right)->left=NULL;
(tree->right)->right=NULL;
return;
if(tree->s1==node1->s1)
flag=1;
return;
else if(tree->s1>node1->s1)
if(tree->left!=NULL)
check_same(tree->left,node1);
else if(tree->s1<node1->s1)
{
if(tree->right!=NULL)
check_same(tree->right,node1);
void dict::input_display()
if(root!=NULL)
display(root);
else
if(tree->left==NULL&&tree->right==NULL)
cout<<tree->s1<<" = "<<tree->s2<<"\n\n";
else
{
if(tree->left!=NULL)
display(tree->left);
cout<<tree->s1<<" = "<<tree->s2<<"\n\n";
if(tree->right!=NULL)
display(tree->right);
void dict::input_remove()
char t;
if(root!=NULL)
cin>>s1;
remove(root,s1);
if(flag1==0)
flag1=0;
else
}
}
dict *temp;
if(tree==NULL)
flag1=1;
return tree;
else if(tree->s1>s3)
tree->left=remove(tree->left,s3);
return tree;
else if(tree->s1<s3)
tree->right=remove(tree->right,s3);
return tree;
else
if(tree->left==NULL&&tree->right==NULL)
delete tree;
tree=NULL;
else if(tree->left==NULL)
{
temp=tree;
tree=tree->right;
delete temp;
else if(tree->right==NULL)
temp=tree;
tree=tree->left;
delete temp;
else
temp=findmin(tree->right);
tree=temp;
tree->right=remove(tree->right,temp->s1);
return tree;
while(tree->left!=NULL)
tree=tree->left;
return tree;
}
void dict::input_find()
flag2=0,cmp=0;
if(root!=NULL)
cin>>s1;
find(root,s1);
if(flag2==0)
cmp=0;
else
if(tree==NULL)
flag2=1;
flag3=1;
cmp=0;
else
{
if(tree->s1==s3)
cmp++;
cout<<"\nWord found.\n";
cout<<tree->s1<<": "<<tree->s2<<"\n";
tree1=tree;
return tree;
else if(tree->s1>s3)
cmp++;
find(tree->left,s3);
else if(tree->s1<s3)
cmp++;
find(tree->right,s3);
return tree;
void dict::input_update()
if(root!=NULL)
cin>>s1;
update(root,s1);
}
else
flag3=0;
find(tree,s3);
if(flag3==0)
cin.ignore();
getline(cin,tree1->s2);
return tree;
int main()
int ch;
dict d;
do
cout<<"\n==========================================\n"
"\n********DICTIONARY***********:\n"
"3.Delete a keyword.\n"
"4.Find a keyword.\n"
"6.Exit.\n"
"===============================================\n";
cin>>ch;
switch(ch)
case 1:d.input();
break;
case 2:d.input_display();
break;
case 3:d.input_remove();
break;
case 4:d.input_find();
break;
case 5:d.input_update();
break;
break;
}while(ch!=6);
return 0;
/*
==========================================
********DICTIONARY***********:
Enter your choice:
3.Delete a keyword.
4.Find a keyword.
6.Exit.
===============================================
alpha
strong
==========================================
********DICTIONARY***********:
3.Delete a keyword.
4.Find a keyword.
6.Exit.
===============================================
1
Enter the keyword:
beta
intelligent
==========================================
********DICTIONARY***********:
3.Delete a keyword.
4.Find a keyword.
6.Exit.
===============================================
young
==========================================
********DICTIONARY***********:
3.Delete a keyword.
4.Find a keyword.
6.Exit.
===============================================
alpha = strong
beta = intelligent
z = young
==========================================
********DICTIONARY***********:
3.Delete a keyword.
4.Find a keyword.
6.Exit.
===============================================
3
Enter a keyword to be deleted:
==========================================
********DICTIONARY***********:
3.Delete a keyword.
4.Find a keyword.
6.Exit.
===============================================
alpha = strong
beta = intelligent
==========================================
********DICTIONARY***********:
3.Delete a keyword.
4.Find a keyword.
6.Exit.
===============================================
beta
Word found.
beta: intelligent
==========================================
********DICTIONARY***********:
3.Delete a keyword.
4.Find a keyword.
6.Exit.
===============================================
alpha
Word found.
alpha: strong
superior
==========================================
********DICTIONARY***********:
3.Delete a keyword.
4.Find a keyword.
6.Exit.
===============================================
alpha = superior
beta = intelligent
==========================================
********DICTIONARY***********:
3.Delete a keyword.
4.Find a keyword.
6.Exit.
===============================================
*/