Professional Documents
Culture Documents
#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);
}
return 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);
}
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;
}