You are on page 1of 9

//205083

#include<iostream>
#include<string>
#include<stack>
using namespace std;

class node
{
string key,mean;
node *right,*left;
public:
node(string val,string m)
{
right=left=NULL;
key=val;
mean=m;
}

void setmean(string p)
{
mean=p;
}
string getmean()
{
return mean;
}
void setkey(string p)
{
key=p;
}
string getkey()
{
return key;
}
void setleft(node *l)
{
left=l;
}
node* getleft()
{
return left;
}
void setright(node *r)
{
right=r;
}
node* getright()
{
return right;
}
};
//////////////////////////////////////////////////////////////////////
class AVL
{
node *root;
public:
AVL()
{
root=NULL;
}
node* RightRotation(node*);
node* LeftRotation(node*);
node* LR(node*);
node* RL(node*);
node* LL(node*);
node* RR(node*);
int BF(node*);
int BF();
node* insert(node*,string,string);
void ascending();
void ascending(node *);
void descending();
void descending(node *);
void create(int n);
void update(string k);
node* delete1(node* root,string key);
node* search(string k);
void deletefun();
node* getroot(){return root;}
};
/////////////////////////////////////////////////////////////////////////
node* AVL::RightRotation(node* A)
{
node* B;
B=A->getleft();
A->setleft(B->getright());
B->setright(A);
return B;
}
////////////////////////////////////////////////////////////////////////
node* AVL::LeftRotation(node* A)
{
node* B;
B=A->getright();
A->setright(B->getleft());
B->setleft(A);
return B;
}
////////////////////////////////////////////////////////////////////////
node* AVL::LR(node* A)
{
A->setleft(LeftRotation(A->getleft()));
A=RightRotation(A);
return A;
}
//////////////////////////////////////////////////////////////////////////
node* AVL::RL(node* A)
{
A->setright(RightRotation(A->getright()));
A=LeftRotation(A);
return A;
}
//////////////////////////////////////////////////////////////////////////
node* AVL::RR(node* A)
{
A=LeftRotation(A);
return A;
}
//////////////////////////////////////////////////////////////////////////
node* AVL::LL(node* A)
{
A=RightRotation(A);
return A;
}
//////////////////////////////////////////////////////////////////////////
int AVL::BF(node* root)
{
int lh,rh;

if(root==NULL)
return 0;
if(root->getleft()==NULL)
lh=0;
else
lh=1+BF(root->getleft());
if(root->getright()==NULL)
rh=0;
else
rh=1+BF(root->getright());
return(lh-rh);
}
///////////////////////////////////////////////////////////////////////////////////
////////
node* AVL::insert(node* root,string k,string m)
{
if(root==NULL)
root=new node(k,m);
// else if(val<root->getdata())
else if(k.compare(root->getkey())<0)
{
root->setleft(insert(root->getleft(),k,m));
int h=BF(root);
if(h==2)
{
//if(val<root->getleft()->getkey())
if(k.compare(root->getleft()->getkey())<0)
root=LL(root);
else
root=LR(root);
}
}
// else if(val>root->getdata())
else if(k.compare(root->getkey())>0)
{
root->setright(insert(root->getright(),k,m));
int f=BF(root);
if(f==-2)
{
//if(val>root->getright()->getkey())
if(k.compare(root->getright()->getkey())>0)
root=RR(root);
else
root=RL(root);
}
}
else
cout<<"\nDuplicate value:\n";
return root;
}
///////////////////////////////////////////////////////////////////////////////////
////////
void AVL::ascending()
{
ascending(root);
}
/////////////////////////////////////////////////////////////////////////////////
void AVL::ascending(node* root)
{
if(root!=NULL)
{
ascending(root->getleft());
cout<<endl<<root->getkey()<<" : "<<root->getmean();
ascending(root->getright());
}
}
///////////////////////////////////////////////////////////////////////////////////
///////
void AVL::descending()
{
descending(root);
}
/////////////////////////////////////////////////////////////////////////////////
void AVL::descending(node* root)
{
if(root!=NULL)
{
descending(root->getright());
cout<<endl<<root->getkey()<<" : "<<root->getmean();
descending(root->getleft());
}
}
/////////////////////////////////////////////////////////////////////////////////
void AVL::create(int n)
{
string a,b;
int i;
for(i=0;i<n;i++)
{
cout<<"\nEnter word to be inserted : ";
cin>>a;
cout<<"\nEnter meaning of "<<a<<" ";
cin>>b;
root=insert(root,a,b);
}
}
/////////////////////////////////////////////////////////////////////////////////
*/
node* AVL::search(string k)
{
int f=0;
node* t=root;
while(t!=NULL)
{
if(k.compare(t->getkey())>0)
t=t->getright();
else if(k.compare(t->getkey())<0)
t=t->getleft();
else
{
f=1;
break;
}
}
if(f==0)
{
cout<<"\n"<<k<<" not found ";
return NULL;
}
else
{
return t;
}
}

void AVL::update(string k)
{
node* t=search(k);
if(t!=NULL)
{
string a;
cout<<"\nEnter new meaning of word "<<k<<" ";
cin>>a;
t->setmean(a);
}
}

///////////////////////////////////////////////////////////////////////////
void AVL::deletefun()
{
string w;
cout<<"\nEnter a word you want to delete:\n";
cin>>w;
root=delete1(root,w);
}
////////////////////////////////////////////////////////////////////////
node* AVL::delete1(node* root,string key)
{
if(key.compare(root->getkey())<0)
{

root->setleft(delete1(root->getleft(),key));
if(BF(root)==-2)
{
if(BF(root->getright())==0)
RR(root);
else if(BF(root->getright())==-1)
RR(root);
else
RL(root);
}

}
else if(key.compare(root->getkey())>0)
{
root->setright(delete1(root->getright(),key));

if(BF(root)==2)
{
if(BF(root->getleft())>=0)
LL(root);
else
LR(root);
}
}
else
{
if(root->getright()!=NULL)
{
node *temp;
temp=root->getright();
while(temp->getleft()!=NULL)
temp=temp->getleft();
root->setkey(temp->getkey());
root->setmean(temp->getmean());
root->setright(delete1(root->getright(),temp->getkey()));
if(BF(root)==2)
{
if(BF(root->getleft())>=0)
LL(root);
else
LR(root);
}
}
else
{
return root->getleft();
}
}
return root;
}
/////////////////////////////////////////////////////////////////////////
int main()
{
int c,n,flag1=0;
string k,m;
node *t;
AVL a;
do
{
cout<<"\n1.Create AVL tree.";
cout<<"\n2.Insert.";
cout<<"\n3.Delete.";
cout<<"\n4.Update.";
cout<<"\n5.Search.";
cout<<"\n6.Ascending Display.";
cout<<"\n7.Descending display.";
cout<<"\n8.Exit.";
cout<<"\n\tSelect an option. ";
cin>>c;

switch(c)
{
case 1: if(flag1 ==1 )
cout<<"\nAlready created.";
else
{
flag1=1;
cout<<"\nEnter number of words you want to add. ";
cin>>n;
a.create(n);
}
break;
case 2: a.create(1);
break;
case 3: a.deletefun();
break;
case 4: cout<<"\nEnter word whose meaning has to be updated. ";
cin>>k;
a.update(k);
break;
case 5: cout<<"\nEnter word whose meaning has to be searched. ";
cin>>k;
t=a.search(k);
if(t!=NULL)
{
cout<<"\nWord = "<<t->getkey();
cout<<"\nMeaning = "<<t->getmean();
}
break;
case 6: a.ascending(); break;
case 7: a.descending(); break;
case 8: break;
default: cout<<"\nEnter valid option.";

}
}
while(c!=8);
return 0;
}

/*
1.Create AVL tree.
2.Insert.
3.Delete.
4.Update.
5.Search.
6.Ascending Display.
7.Descending display.
8.Exit.
Select an option. 10

Enter valid option.


1.Create AVL tree.
2.Insert.
3.Delete.
4.Update.
5.Search.
6.Ascending Display.
7.Descending display.
8.Exit.
Select an option. 1

Enter number of words you want to add. 3

Enter word to be inserted : Aaditya

Enter meaning of Aaditya Sun

Enter word to be inserted : Akash

Enter meaning of Akash Sky

Enter word to be inserted : Hello

Enter meaning of Hello Hi

1.Create AVL tree.


2.Insert.
3.Delete.
4.Update.
5.Search.
6.Ascending Display.
7.Descending display.
8.Exit.
Select an option.
4

Enter word whose meaning has to be updated. Aaditya

Enter new meaning of word Aaditya Surya

1.Create AVL tree.


2.Insert.
3.Delete.
4.Update.
5.Search.
6.Ascending Display.
7.Descending display.
8.Exit.
Select an option. 3

Enter a word you want to delete:


Akash

1.Create AVL tree.


2.Insert.
3.Delete.
4.Update.
5.Search.
6.Ascending Display.
7.Descending display.
8.Exit.
Select an option. 6

Aaditya : Surya
Hello : Hi
1.Create AVL tree.
2.Insert.
3.Delete.
4.Update.
5.Search.
6.Ascending Display.
7.Descending display.
8.Exit.
Select an option. 7

Hello : Hi
Aaditya : Surya
1.Create AVL tree.
2.Insert.
3.Delete.
4.Update.
5.Search.
6.Ascending Display.
7.Descending display.
8.Exit.
Select an option. 2

Enter word to be inserted : Earth

Enter meaning of Earth Prithvi

1.Create AVL tree.


2.Insert.
3.Delete.
4.Update.
5.Search.
6.Ascending Display.
7.Descending display.
8.Exit.
Select an option. 6

Aaditya : Surya
Earth : Prithvi
Hello : Hi
1.Create AVL tree.
2.Insert.
3.Delete.
4.Update.
5.Search.
6.Ascending Display.
7.Descending display.
8.Exit.
Select an option. 8

*/

You might also like