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

Dsal 9

Uploaded by

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

Dsal 9

Uploaded by

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

Experiment no 9

#include <iostream>
#include <string.h>
#include <queue>
using namespace std;

class node
{

char key[10];
char mean[30];
int BF;
node *left,*right;
public:
node()
{
strcpy(key," ");
strcpy(mean," ");
BF=0;
left=NULL;
right=NULL;

}
friend class avl;

};

class avl
{
node* root;
public:
avl()
{
root=NULL;
}
node* insert(node*,char[],char[]);
void display();
void insert();
node* LL(node*);
node* LR(node*);
node* RR(node*);
node* RL(node*);
int bal_fact(node*);
int get_height(node*);
void levelwisedisplay();
node* delete_(node*,char[]);
void delete_();
node* FindMin(node*);
void update(node*);
void update();
void display_ascending(node*);
void display_descending(node*);
void search();
node*find(node*,char[]);

};
void avl :: insert()
{

char data[10],meaning[30];
cout<<"\n Enter the keyword to be added in the dictionary ";
cin>>data;
cout<<"\n Enter the meaning of the entered keyword ";
cin>>meaning;
root= insert(root,data,meaning);

}
node* avl :: insert(node*root,char data[10],char meaning[30])
{
int bal;
if(root==NULL)
{
root=new node;
strcpy(root->key,data);
strcpy(root->mean,meaning);
root->left=NULL;
root->right=NULL;
root->BF=0;
}
else
{
if(strcmp(data,root->key)<0)
{
cout<<root->key;
root->left=insert(root->left,data,meaning);
bal=bal_fact(root);
cout<<"\n node "<<root->key<<" BF "<<bal;
if(bal==2)
{
if(strcmp(data,root->left->key)<0)
root=LL(root);
else
root=LR(root);
}
}
else
{
root->right=insert(root->right,data,meaning);
bal=bal_fact(root);
cout<<"\n node "<<root->key<<" BF "<<bal;
if(bal==-2)
{
if(strcmp(data,root->right->key)>0)
root=RR(root);
else
root=RL(root);
}
}
root->BF=get_height(root);

}
return (root);
}

int avl :: get_height(node*temp)


{
int hl,hr;
if(temp==NULL) //to check if node is null
return 0;
else{
hl=get_height(temp->left); //for finding height of left subtree
hr=get_height(temp->right);//for finding height of right subtree
if(hl>hr)
return hl+1; //returing height of left subtree with adding the height by 1 for
root
else
return hr+1;//returing height of right subtree with adding the height by 1 for
root
}

node * avl::LL(node* root)


{
node *temp;
temp=root->left;
root->left=temp->right;
temp->right=root;
temp->BF=get_height(temp);
root->BF=get_height(root);
cout<<"\n LL rotation done ";
return (temp);
}
node * avl::RR(node* root)
{
node *temp;
temp=root->right;
root->right=temp->left;
temp->left=root;
temp->BF=get_height(temp);
root->BF=get_height(root);
cout<<"\n RR rotation done ";
return (temp);
}

node *avl :: RL(node *root)


{
root->right=LL(root->right);
root=RR(root);

return root;
}

node* avl :: LR(node* root)


{
root->left=RR(root->left);
root=LL(root);
return root;
}

int avl :: bal_fact(node* temp)


{
int hr,hl;
hl=get_height(temp->left);
hr=get_height(temp->right);
return hl-hr;
}

void avl :: display()


{
int ch=0;
cout<<root->key;
cout<<"\nEnter your choice \n1.For Ascending display \n2.For Descending display ";
cin>>ch;
if(ch==1)
display_ascending(root);
else
display_descending(root);

}
void avl :: display_ascending(node* root)
{
if(root!=NULL) //to check if node is null
{
display_ascending(root->left);
cout<<"keyword:"<<root->key<<" ";
cout<<"meaning:"<<root->mean<<endl;
display_ascending(root->right);
}

void avl :: display_descending(node* root)


{
if(root!=NULL) //to check if node is null
{
display_descending(root->right);
cout<<"keyword:"<<root->key<<" ";
cout<<"meaning:"<<root->mean<<endl;
display_descending(root->left);
}

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


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

node* avl ::delete_(node*root,char data[10])


{
node *temp;

if(root == NULL) return root;


else if(strcmp(data , root->key)<0)
root->left = delete_(root->left,data);
else if(strcmp(data , root->key)>0)
root->right = delete_(root->right, data);
else {
// Case 1: No Child
if(root->left == NULL && root->right == NULL){
delete root;
root = NULL;
// Case 2: one child
} else if(root->left == NULL){
temp = root;
root = root->right;
delete temp;
} else if(root->right == NULL){
temp = root;
root = root->left;
delete temp;
} else{
temp = FindMin(root->right);
strcpy(root->key , temp->key);
root->right = delete_(root->right, temp->key);
}
}
return root;
}

void avl :: delete_()


{
char data[10];
cout<<"\n Enter the keyword to be deleted from the dictionary ";
cin>>data;
delete_(root,data);
}

void avl :: update()


{
update(root);
}

void avl :: update(node* root)


{
char key[10],meaning[30];

cout<<"\n Enter the keyword to be updated ";


cin>>key;
find(root,key);
cout<<"\n Enter the new meaning of the word ";
cin>>meaning;
strcpy(root->mean,meaning);
}

node* avl::find(node*root,char key[10]){


int flag=0;
if(root!=NULL&&flag==0){
if(strcmp(key , root->key)<0){
return find(root->left,key);
}if(strcmp(key , root->key)>0){
return find(root->right,key);
}else if(strcmp(key , root->key)==0){
flag=1;
}
}if(flag==1){

cout<<"DATA FOUND"<<endl;
cout<<"keyword:"<<root->key<<endl;
cout<<"meaning:"<<root->mean<<endl;
}if(flag==0){
cout<<"DATA NOT FOUND";
}
return root;
}

void avl::search(){
avl t;
char d[10];
cout<<"ENTER DATA TO FIND";
cin>>d;
t.find(root,d);

}
int main() {
avl *a;
a =new avl();
char ans;
int ch;
do{
cout<<"\n======MENU======\nEnter your choice ";
cout<<"\n1.Insert \n2.Display \n3.Delete \n4.Update\n5.search:";
cin>>ch;
switch(ch)
{
case 1: a->insert();
break;

case 2: a->display();
break;

case 3: a->delete_();
break;

case 4: a->update();
break;

case 5:a->search();

break;
}
cout<<"\nDo you want to continue ?? ";
cin>>ans;

}while(ans=='y');
return 0;
}

You might also like