0% found this document useful (0 votes)
13 views9 pages

Dsa10 Practical

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

Dsa10 Practical

About dsa practical sppu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include<iostream>

#include<string>

#include<cstring>

#include<cstdlib>

using namespace std;

int op; class

node{

public:

node *left, *right; char

word[50], mean[50];

};

class BT{

public:

node* root;

BT(){ root

= NULL;

void create(); node*

insert(node *, node *); void

inorder(node *); void

preorder(node *); void

postorder(node *);

node *findsmallest(node *);

node *findlargest(node *); void

search(node *, char[]); void

modify(node *, char[]); node

*dlt(node *, char[]); node

*FindMin(node *);
};

void BT::create(){

int op;

node *temp;

do{ temp=new node;

cout<<"Enter a word ";

cin>>temp->word;

cout<<"Enter its meaning ";

cin>>temp->mean; temp-

>left=temp->right=NULL;

if(root==NULL){

root=temp;

else{

root=insert(root, temp);

cout<<"\nDo you want to add more? ";

cin>>op;

}while(op==1);

node* BT::insert(node *root, node *temp){

int l;

int l1=strlen(temp->word);

int l2=strlen(root->word);

if(l1<l2){

l=l1;

}
else{

l=l2;

for(int i=0; i<l; i++){

if(temp->word[i]<root->word[i]){

if(root->left==NULL){ root-

>left=temp; return root;

else{ root->left=insert(root-

>left, temp);

return root;

else if(temp->word[i]>root->word[i]){

if(root->right==NULL){ root-

>right=temp; return root;

else{

root->right=insert(root->right, temp);

return root;

return root;

void BT::inorder(node *temp){

if(temp!=NULL){ inorder(temp-

>left);

cout<<" "<<temp->word<<" "<<temp->mean;

inorder(temp->right);
}

void BT::preorder(node *temp){ if(temp!=NULL)

{ cout<<" "<<temp->word<<" "<<temp-

>mean; preorder(temp->left);

preorder(temp->right);

void BT::postorder(node *temp){ if(temp!

=NULL){ postorder(temp->left);

postorder(temp->right); cout<<" "<<temp-

>word<<" "<<temp->mean;

node *BT::findsmallest(node *root){ if(root-

>left==NULL){

cout<<"\n\n Smallest Node: "<<root->word;

else{ findsmallest(root-

>left);

node *BT::findlargest(node *root){ if(root-

>right==NULL){ cout<<"\n\n Largest Node:

"<<root->word;

}
else{ findlargest(root-

>right);

void BT::search(node *temp, char src[]){

if(temp!=NULL){ if((strcmp(temp-

>word,src))==0){ cout<<"\n Word

Found: "; cout<<"\n Word : "<<temp-

>word; cout<<"\n Meaning : "<<temp-

>mean;

else if((strcmp(temp->word,src))!=0){

if((strcmp(temp->word,src))< 0){

search(temp->right, src);

else if((strcmp(temp->word ,src ))>0){

search(temp->left, src);

} else{ cout<<"\nWord Not

Found";

void BT::modify(node *temp, char src[]){ if(temp!=NULL){

if((strcmp(temp->word, src)) == 0){ cout<<"word

found "<<endl; cout<<"Enter New meaning of word :

"<<temp->word; cin>>temp->mean;

}
else if((strcmp(temp->word, src))!= 0){

if((strcmp(temp->word, src))< 0){

modify(temp->right, src);

else if((strcmp(temp->word, src)) > 0){

modify(temp->left, src);

else{

cout<<"\nWord Not Found";

node *BT::dlt(node *root, char src[]){

if(root == NULL){ if((strcmp(root-

>word , src)) > 0){ root->left = dlt(root-

>left, src);

else if((strcmp(root->word, src)) < 0){

root->right = dlt(root->right, src);

else {

if(root == NULL && root->right == NULL){

delete root;

root = NULL;

else if(root->left == NULL){

node *temp = root; root

= root->right; delete

temp;
}

else if(root->right == NULL){

node *temp = root; root

= root->left; delete temp;

else {

node *temp = FindMin(root->right);

strcpy(root->word, temp->word); strcpy(root-

>mean, temp->mean); root->right = dlt(root-

>right, temp->word);

return root;

node *BT::FindMin(node *root){

while(root->left !=NULL) root = root->left;

return root;

int main(){ BT b; int op; char src[100]; while(1){

cout<<"\n "; cout<<"\n 1. Insert Binary Search Tree : ";

cout<<"\n 2. Display Inorder , Preorder and Postorder ";

cout<<"\n 3. Search the word "; cout<<"\n 4. Modify

the meaning of word "; cout<<"\n 5. Delete word from

dictionary "; cout<<"\n 6. Largest and smallest node ";

cout<<"\n 7. Exit "; cout<<"\n Enter your choice : ";

cin>>op;

switch(op){

case 1:
[Link]();

break;

case 2:

cout<<"\n Inorder : ";

[Link]([Link]);

cout<<"\n Preorder : ";

[Link]([Link]);

cout<<"\n Postorder : ";

[Link]([Link]);

break;

case 3:

cout<<"\n Enter Word you want to search : ";

cin>>src;

[Link]([Link] , src);

break;

case 4:

cout<<"\n Enter Word you want to modify : ";

cin>>src;

[Link]([Link], src);

break;

case

5:

cout<<"\n Enter word you want to delete : ";

cin>>src;

[Link]([Link] , src);

break;

case 6:
[Link]([Link]);

[Link]([Link]);

break;

case 7:

exit(0);

break; default :

cout<<"\n Invalid

Option ";

break;

You might also like