/* Program Name : AVL Tree in 'C++'*/
#include<string.h>
#include<iostream>
using namespace std;
#include<stdlib.h>
struct word
{
char w[20];
};
struct node
{
word data;
node*left,*right;
int ht;
};
class AVL
{
node*AVL_root;
node*insert1(node*T,word);
int height(node*T);
node*Rotate_Left(node*T);
node*Rotate_Right(node*T);
void inorder1(node*);
void preorder1(node*);
void postorder1(node*);
int BF(node*T);
public:
AVL()
{
AVL_root=NULL;
}
void insert(word x)
{
AVL_root=insert1(AVL_root,x);
}
void Preorder()
{
preorder1(AVL_root);
}
void Inorder()
{
inorder1(AVL_root);
}
void Postorder()
{
postorder1(AVL_root);
}
};
void AVL::inorder1(node*T)
{
if(T!=NULL)
{ inorder1(T->left);
cout<<" "<<T->data.w;
inorder1(T->right);
}
}
void AVL::preorder1(node*T)
{
if(T!=NULL)
{
cout<<" "<<T->data.w;
preorder1(T->left);
preorder1(T->right);
}
}
void AVL::postorder1(node*T)
{
if(T!=NULL)
{ postorder1(T->left);
postorder1(T->right);
cout<<" "<<T->data.w;
}
}
int AVL::BF(node*T)
{
int lh,rh;
if(T==NULL)
return 0;
else
{
if(T->left==NULL)
{
lh=0;
}
else
{
lh=1+T->left->ht;
}
if(T->right==NULL)
{
rh=0;
}
else
{
rh=1+T->right->ht;
}
}
return(lh-rh);
}
node*AVL::Rotate_Right(node*x)
{
node*y;
y=x->left;
x->left=y->right;
y->right=x;
x->ht=height(x);
y->ht=height(y);
return(y);
}
node*AVL::Rotate_Left(node*T)
{
node*temp;
temp=T->right;
T->right=temp->left;
temp->left=T;
T->ht=height(T);
temp->ht=height(temp);
return(temp);
}
int AVL::height(node*T)
{
int left_height,right_height;
if(T==NULL)
{
return(0);
}
if(T->left==NULL)
{
left_height=0;
}
else
{
left_height=1+(T->left->ht);
}
if(T->right==NULL)
{
right_height=0;
}
else
{
right_height=1+(T->right->ht);
}
if(left_height>right_height)
return(left_height);
else
return(right_height);
}
node*AVL::insert1(node*T,word x)
{
if(T==NULL)
{
T=new node;
T->data=x;
T->left=NULL;
T->right=NULL;
}
else
{
if(strcmp((x.w),(T->data.w))>0)
{
T->right=insert1(T->right,x);
if(BF(T)==-2)
{
if(strcmp(x.w,T->right->data.w)>0)
{
T=Rotate_Left(T);
}
else
{
T->right=Rotate_Right(T->right);
T=Rotate_Left(T);
}
}
}
else
{
if(strcmp(x.w,T->data.w)<0)
{
T->left=insert1(T->left,x);
if(BF(T)==2)
{
if(strcmp(x.w,T->left->data.w)<0)
{
T=Rotate_Right(T);
}
else
{
T->left=Rotate_Left(T->left);
T=Rotate_Right(T);
}
}
}
}
}
T->ht=height(T);
return(T);
}
int main()
{
AVL obj;
int choice,no_of_nodes;
word x;
while(1)
{
cout<<"\n1.Create AVL Tree";
cout<<"\n2.Preorder";
cout<<"\n3.inorder";
cout<<"\n4.Postorder";
cout<<"\n5.Quit";
cout<<"\n.Enter your choice ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\nHow many nodes you want to insert ";
cin>>no_of_nodes;
for(int i=0;i<no_of_nodes;i++)
{
cout<<"\nEnter data : ";
cin>>x.w;
obj.insert(x);
}
break;
case 2:
obj.Preorder();
break;
case 3:
obj.Inorder();
break;
case 4:
obj.Postorder();
break;
case 5:
exit(0);
}
}
return 0;
}
#include<string.h>
#include<iostream>
using namespace std;
#include<stdlib.h>
struct word
{
char w[20];
};
struct node
{
word data;
node*left,*right;
int ht;
};
class AVL
{
node*AVL_root;
node*insert1(node*T,word);
int height(node*T);
node*Rotate_Left(node*T);
node*Rotate_Right(node*T);
void inorder1(node*);
void preorder1(node*);
void postorder1(node*);
int BF(node*T);
public:
AVL()
{
AVL_root=NULL;
}
void insert(word x)
{
AVL_root=insert1(AVL_root,x);
}
void Preorder()
{
preorder1(AVL_root);
}
void Inorder()
{
inorder1(AVL_root);
}
void Postorder()
{
postorder1(AVL_root);
}
};
void AVL::inorder1(node*T)
{
if(T!=NULL)
{ inorder1(T->left);
cout<<" "<<T->data.w;
inorder1(T->right);
}
}
void AVL::preorder1(node*T)
{
if(T!=NULL)
{
cout<<" "<<T->data.w;
preorder1(T->left);
preorder1(T->right);
}
}
void AVL::postorder1(node*T)
{
if(T!=NULL)
{ postorder1(T->left);
postorder1(T->right);
cout<<" "<<T->data.w;
}
}
int AVL::BF(node*T)
{
int lh,rh;
if(T==NULL)
return 0;
else
{
if(T->left==NULL)
{
lh=0;
}
else
{
lh=1+T->left->ht;
}
if(T->right==NULL)
{
rh=0;
}
else
{
rh=1+T->right->ht;
}
}
return(lh-rh);
}
node*AVL::Rotate_Right(node*x)
{
node*y;
y=x->left;
x->left=y->right;
y->right=x;
x->ht=height(x);
y->ht=height(y);
return(y);
}
node*AVL::Rotate_Left(node*T)
{
node*temp;
temp=T->right;
T->right=temp->left;
temp->left=T;
T->ht=height(T);
temp->ht=height(temp);
return(temp);
}
int AVL::height(node*T)
{
int left_height,right_height;
if(T==NULL)
{
return(0);
}
if(T->left==NULL)
{
left_height=0;
}
else
{
left_height=1+(T->left->ht);
}
if(T->right==NULL)
{
right_height=0;
}
else
{
right_height=1+(T->right->ht);
}
if(left_height>right_height)
return(left_height);
else
return(right_height);
}
node*AVL::insert1(node*T,word x)
{
if(T==NULL)
{
T=new node;
T->data=x;
T->left=NULL;
T->right=NULL;
}
else
{
if(strcmp((x.w),(T->data.w))>0)
{
T->right=insert1(T->right,x);
if(BF(T)==-2)
{
if(strcmp(x.w,T->right->data.w)>0)
{
T=Rotate_Left(T);
}
else
{
T->right=Rotate_Right(T->right);
T=Rotate_Left(T);
}
}
}
else
{
if(strcmp(x.w,T->data.w)<0)
{
T->left=insert1(T->left,x);
if(BF(T)==2)
{
if(strcmp(x.w,T->left->data.w)<0)
{
T=Rotate_Right(T);
}
else
{
T->left=Rotate_Left(T->left);
T=Rotate_Right(T);
}
}
}
}
}
T->ht=height(T);
return(T);
}
int main()
{
AVL obj;
int choice,no_of_nodes;
word x;
while(1)
{
cout<<"\n1.Create AVL Tree";
cout<<"\n2.Preorder";
cout<<"\n3.inorder";
cout<<"\n4.Postorder";
cout<<"\n5.Quit";
cout<<"\n.Enter your choice ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\nHow many nodes you want to insert ";
cin>>no_of_nodes;
for(int i=0;i<no_of_nodes;i++)
{
cout<<"\nEnter data : ";
cin>>x.w;
obj.insert(x);
}
break;
case 2:
obj.Preorder();
break;
case 3:
obj.Inorder();
break;
case 4:
obj.Postorder();
break;
case 5:
exit(0);
}
}
return 0;
}