Dsa Practical 5
Dsa Practical 5
#include <iostream>
using namespace std;
struct node
{
string word;
string meaning;
node* left=NULL;
node* right=NULL;
};
class Dictionary
{
public:
node* root;
Dictionary()
{
root=NULL;
}
void create_dictionary();
void insert();
void display_asc(node *);
void display_desc(node *);
void comparisons(node*, string);
void update(node*, string);
void delet(node*, string);
node* minimum(node *);
};
void Dictionary::create_dictionary()
{
node *temp=new node;
node *parent=NULL;
int n;
string word,meaning;
cout<<"Enter the size of Dictionary:";
cin>>n;
for(int i=0; i<n;i++)
{
cout<<"Enter the word:";
temp=new node();
cin>>temp->word;
cout<<"Enter the meaning:";
cin>>temp->meaning;
temp->left=temp->right=NULL;
if(root==NULL)
{
root=temp;
}
else
{
node *ptr;
ptr=root;
while(ptr!=NULL)
{
parent=ptr;
if(temp->word>ptr->word)
ptr=ptr->right;
else
ptr=ptr->left;
}
if(temp->word>parent->word)
parent->right=temp;
else
parent->left=temp;
}
}
}
void Dictionary::insert()
{
node *temp=new node;
cout<<"Enter new word to be inserted: ";
cin>>temp->word;
cout<<"Enter its meaning:";
cin>>temp->meaning;
temp->left = temp->right=NULL;
node*parent=NULL;
if(root==NULL)
{
root=temp;
}
else
{
node *ptr;
ptr=root;
while(ptr!=NULL)
{
parent=ptr;
if(temp->word>ptr->word)
ptr=ptr->right;
else
ptr=ptr->left;
}
if(temp->word>parent->word)
parent->right=temp;
else
parent->left=temp;
}
}
int main()
{
int choice;
string search;
Dictionary d1;
do
{
cout<<"\n***************MENU***************";
cout<<"\n1.CREATE DICTIONARY\n2.DISPLAY IN ASCENDING
ORDER\n3.DISPLAY IN DESCENDING ORDER\n4.SEARCH AND
UPDATE\n5.DELETE\n6.COMPARISIONS\n7.EXIT\n";
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1: d1.create_dictionary();
break;
case 2: cout<<"\n*****DICTIONARY*****";
d1.display_asc(d1.root);
break;
case 3: cout<<"\n*****DICTIONARY*****";
d1.display_desc(d1.root);
break;
case 4: cout<<"\nEnter the word to search: ";
cin >> search;
d1.update(d1.root, search);
break;
case 5: cout<<"\nEnter the word to delete: ";
cin>>search;
d1.delet(d1.root, search);
break;
case 6: cout<<"\nEnter the word to find comparisons: ";
cin >>search;
d1.comparisons(d1.root, search);
case 7: break;
}
}while(choice!=7);
return 0;
}
Output:
***************MENU***************
1.CREATE DICTIONARY
2.DISPLAY IN ASCENDING ORDER
3.DISPLAY IN DESCENDING ORDER
4.SEARCH AND UPDATE
5.DELETE
6.COMPARISIONS
7.EXIT
Enter your choice: 1
Enter the size of Dictionary:5
Enter the word:ABC
Enter the meaning:word1
Enter the word:PQR
Enter the meaning:word2
Enter the word:XYZ
Enter the meaning:word3
Enter the word:JKL
Enter the meaning:word4
Enter the word:SNP
Enter the meaning:word5
***************MENU***************
1.CREATE DICTIONARY
2.DISPLAY IN ASCENDING ORDER
3.DISPLAY IN DESCENDING ORDER
4.SEARCH AND UPDATE
5.DELETE
6.COMPARISIONS
7.EXIT
Enter your choice: 2
*****DICTIONARY*****
ABC word1
JKL word4
PQR word2
SNP word5
XYZ word3
***************MENU***************
1.CREATE DICTIONARY
2.DISPLAY IN ASCENDING ORDER
3.DISPLAY IN DESCENDING ORDER
4.SEARCH AND UPDATE
5.DELETE
6.COMPARISIONS
7.EXIT
Enter your choice: 3
*****DICTIONARY*****
XYZ word3
SNP word5
PQR word2
JKL word4
ABC word1
***************MENU***************
1.CREATE DICTIONARY
2.DISPLAY IN ASCENDING ORDER
3.DISPLAY IN DESCENDING ORDER
4.SEARCH AND UPDATE
5.DELETE
6.COMPARISIONS
7.EXIT
Enter your choice: 4
Enter the word to search: ABC
Enter its new meaning: Uword1
***************MENU***************
1.CREATE DICTIONARY
2.DISPLAY IN ASCENDING ORDER
3.DISPLAY IN DESCENDING ORDER
4.SEARCH AND UPDATE
5.DELETE
6.COMPARISIONS
7.EXIT
Enter your choice: 4
Enter the word to search: SSS
Word not found!
***************MENU***************
1.CREATE DICTIONARY
2.DISPLAY IN ASCENDING ORDER
3.DISPLAY IN DESCENDING ORDER
4.SEARCH AND UPDATE
5.DELETE
6.COMPARISIONS
7.EXIT
Enter your choice: 5
Enter the word to delete: XYZ
***************MENU***************
1.CREATE DICTIONARY
2.DISPLAY IN ASCENDING ORDER
3.DISPLAY IN DESCENDING ORDER
4.SEARCH AND UPDATE
5.DELETE
6.COMPARISIONS
7.EXIT
Enter your choice: 2
*****DICTIONARY*****
ABC Uword1
JKL word4
PQR word2
SNP word5
***************MENU***************
1.CREATE DICTIONARY
2.DISPLAY IN ASCENDING ORDER
3.DISPLAY IN DESCENDING ORDER
4.SEARCH AND UPDATE
5.DELETE
6.COMPARISIONS
7.EXIT
Enter your choice: 6
Enter the word to find comparisons: SNP
Number of comparisons to find the word: 3
***************MENU***************
1.CREATE DICTIONARY
2.DISPLAY IN ASCENDING ORDER
3.DISPLAY IN DESCENDING ORDER
4.SEARCH AND UPDATE
5.DELETE
6.COMPARISIONS
7.EXIT
Enter your choice: 7
--------------------------------
Process exited after 121.8 seconds with return value 0
Press any key to continue . . .