0% found this document useful (0 votes)
99 views14 pages

Dsa Practical 5

Cdsunoks6jbindrvbhhbbbbbbbbbbhhhj

Uploaded by

priyankanarode16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views14 pages

Dsa Practical 5

Cdsunoks6jbindrvbhhbbbbbbbbbbhhhj

Uploaded by

priyankanarode16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Program 5: Write a program to implement Dictionary by using concept of Binary Search Tree

#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;
}
}

void Dictionary::display _asc(node *ptr)


{
if(ptr!=NULL)
{
display_asc(ptr->left);
cout<<"\n" << ptr->word<<" \t" << ptr->meaning;
display_asc(ptr->right);
}
}

void Dictionary::display_desc(node *ptr)


{
if(ptr!=NULL)
{
display_desc(ptr->right);
cout<<"\n" << ptr->word<<" \t" << ptr->meaning;
display_desc(ptr->left);
}
}

void Dictionary::comparisons(node* ptr, string word)


{
static int count = 0;
while(ptr!=NULL)
{
if(word < ptr->word)
{
count++;
ptr = ptr->left;
}
else if(word > ptr->word)
{
count++;
ptr = ptr->right;
}
else if(word == ptr->word)
{
count++;
cout<<"Number of comparisons to find the word: " << count;
return ;
}
}
cout<<"\nWord not found!";
}

node* Dictionary::minimum(node *ptr)


{
node *temp;
while(ptr->left != NULL)
{
temp = ptr;
ptr = ptr->left;
}
return ptr;
}
void Dictionary::delet(node* ptr, string key)
{
node *s, *q;
while(ptr!=NULL)
{
if(key < ptr->word)
{
q = ptr;
ptr = ptr->left;
}
else if(key > ptr->word)
{
q=ptr;
ptr = ptr->right;
}
else if(key == ptr->word)
{
if(ptr->left==NULL && ptr->right==NULL) //Deleting a leaf node
{
if(q->left==ptr)
{
delete ptr;
q->left=NULL;
return;
}
if(q->right==ptr)
{
delete ptr;
q->right=NULL;
return;
}
}
if(ptr->right!=NULL && ptr->left==NULL) //Deleting a node having one right child
{
if(q->right == ptr)
{
q->right = ptr->right;
delete ptr;
return;
}
else if(q->left == ptr)
{
q->left = ptr->right;
delete ptr;
return;
}
}
else if(ptr->left!=NULL && ptr->right==NULL) //Deleting a node having one left child
{
if(q->right == ptr)
{
q->right = ptr->left;
delete ptr;
return;
}
else if(q->left == ptr)
{
q->left=ptr->left;
delete ptr;
return;
}
}
else if(ptr->left!=NULL && ptr->right!=NULL)
{
s = minimum(ptr->right);
ptr->word = s->word;
ptr->meaning = s->meaning;
delet(s, s->word);
return;
}
}
}
cout<<"\nWord NOT found!";
}

void Dictionary::update(node* ptr, string key)


{
while(ptr!=NULL)
{
if(key < ptr->word)
ptr = ptr->left;
else if(key > ptr->word)
ptr = ptr->right;
else if(key == ptr->word)
{
cout<<"\nEnter its new meaning: ";
cin>>ptr->meaning;
return;
}
}
cout<<"\nWord not found!";
}

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 . . .

You might also like