/* This program demonstrate AVL Tree implementation
* with Double Rotation Right(DRR), Double rotation left(DRL),
* Single rotation Left(SRL), Single Rotation Right(SRR) and
* insert rotines.
*/
#include <iostream>
#include <cstdlib>
#include <queue>
using namespace std;
template<class T>
class AVL{
private:
T data;
AVL *left;
AVL *right;
int balance;
public:
AVL(T d = 0, int b = 0, AVL<T> *l = NULL, AVL<T> *r = NULL):data(d), balance(b),left(l),right(r){};
AVL<T> *insert(T val);
void preorder();
void level_order_traversal( AVL<T>* rootNode);
bool search(T val);
AVL<T>* DRR();
AVL<T>* DRL();
AVL<T>* SRR();
AVL<T>* SRL();
int Balance(AVL<T>* r);
};
template<class T>
AVL<T>* AVL<T> :: DRR()
{
right = right -> SRL();
return SRR();
}
template<class T>
AVL<T>* AVL<T> :: DRL()
{
left = left -> SRR();
return SRL();
}
template<class T>
AVL<T>* AVL<T> :: SRR()
{
AVL<T> *Temp;
Temp = right;
right = Temp-> left;
Temp -> left = this;
balance = max(Balance(left),Balance(right))+1;
Temp -> balance = max(Balance(Temp -> left),Balance(Temp -> right))+1;
return Temp;
}
template<class T>
AVL<T>* AVL<T> :: SRL()
{
AVL<T> *Temp;
Temp = left;
left = Temp -> right;
Temp -> right = this;
balance = max(Balance(left),Balance(right))+1;
Temp -> balance = max(Balance(Temp -> left),Balance(Temp -> right))+1;
return Temp;
}
template<class T>
int AVL<T> :: Balance(AVL<T> *r)
{
if(r == NULL)
return -1;
else
return(r -> balance);
}
template<class T>
AVL<T>* AVL<T>:: insert(T val)
{
AVL<T> * Temp = this;
if(val < data && left == NULL){
left = new AVL<T>(val);
}else if(val > data && right == NULL){
right = new AVL<T>(val);
}else if(val < data){
left = left ->insert(val);
if(Balance(left) - Balance(right) == 2)
if(val < left->data)
Temp = SRL();
else
Temp = DRL();
}else if(val > data){
right = right ->insert(val);
if(Balance(right) - Balance(left) == 2)
if(val > right->data)
Temp = SRR();
else
Temp = DRR();
}else{
cout <<"Duplicate value";
}
Temp->balance = max(Balance(Temp ->left),Balance(Temp->right))+1;
return Temp;
}
template<class T>
void AVL<T> :: preorder()
{
cout<<data<<"\t";
if(left != NULL)
left->preorder();
if(right != NULL)
right->preorder();
}
template<class T>
bool AVL<T>:: search(T val)
{
bool re;
if(data == val )
re = true;
else if(val < data && left != NULL){
re = left -> search(val);
}else if (val > data && right != NULL){
re = right -> search(val);
}
return re;
}
template<class T>
void AVL<T> :: level_order_traversal( AVL<T>* root)
{
queue<AVL<T>*> Queue;
AVL<T>* Traverse;
int count = 0,m = 1,x = 1;
Queue.push(root);
while(!Queue.empty()){
Traverse = Queue.front();
Queue.pop();
cout<<Traverse->data<<"\t";
count++;
if(count == m){
cout<<"\n";
m = x * 2 + count;
x= x * 2;
}
if(Traverse->left != NULL)
Queue.push(Traverse->left);
if(Traverse->right != NULL)
Queue.push(Traverse->right);
}
}
int main()
{
AVL<int> *root = NULL;
int choice;
while(1){
cout<<"\n 1.Insert.";
cout<<"\n 2.Search";
cout<<"\n 3.Print";
cout<<"\n 4.Exit";
cout<<"\n Enter your choice:";
cin>>choice;
switch(choice){
case 1:
int val;
cout<<"\n Enter the value to insert:";
cin>>val;
if(root == NULL){
root = new AVL<int>(val);
}else{
root = root -> insert(val);
}
break;
case 2:
cout<<"\n Enter the value to search:";
cin>>val;
if(root == NULL){
cout<<"\n Empty tree.";
}else if(root->search(val) == true){
cout<<"\n The data "<<val <<" present in the Tree";
}else{
cout<<"\n The data "<<val <<" not present in the Tree";
}
break;
case 3:
if(root == NULL){
cout<<"\n Empty Tree.";
}else{
cout<<"\n--------In Preorder Traversal---------\n";
root->preorder();
cout<<"\n--------In Level order Travelsal------\n";
root->level_order_traversal(root);
}
break;
case 4:
exit(0);
break;
default:
cout<<"Enter correct choice";
}
}
}
* with Double Rotation Right(DRR), Double rotation left(DRL),
* Single rotation Left(SRL), Single Rotation Right(SRR) and
* insert rotines.
*/
#include <iostream>
#include <cstdlib>
#include <queue>
using namespace std;
template<class T>
class AVL{
private:
T data;
AVL *left;
AVL *right;
int balance;
public:
AVL(T d = 0, int b = 0, AVL<T> *l = NULL, AVL<T> *r = NULL):data(d), balance(b),left(l),right(r){};
AVL<T> *insert(T val);
void preorder();
void level_order_traversal( AVL<T>* rootNode);
bool search(T val);
AVL<T>* DRR();
AVL<T>* DRL();
AVL<T>* SRR();
AVL<T>* SRL();
int Balance(AVL<T>* r);
};
template<class T>
AVL<T>* AVL<T> :: DRR()
{
right = right -> SRL();
return SRR();
}
template<class T>
AVL<T>* AVL<T> :: DRL()
{
left = left -> SRR();
return SRL();
}
template<class T>
AVL<T>* AVL<T> :: SRR()
{
AVL<T> *Temp;
Temp = right;
right = Temp-> left;
Temp -> left = this;
balance = max(Balance(left),Balance(right))+1;
Temp -> balance = max(Balance(Temp -> left),Balance(Temp -> right))+1;
return Temp;
}
template<class T>
AVL<T>* AVL<T> :: SRL()
{
AVL<T> *Temp;
Temp = left;
left = Temp -> right;
Temp -> right = this;
balance = max(Balance(left),Balance(right))+1;
Temp -> balance = max(Balance(Temp -> left),Balance(Temp -> right))+1;
return Temp;
}
template<class T>
int AVL<T> :: Balance(AVL<T> *r)
{
if(r == NULL)
return -1;
else
return(r -> balance);
}
template<class T>
AVL<T>* AVL<T>:: insert(T val)
{
AVL<T> * Temp = this;
if(val < data && left == NULL){
left = new AVL<T>(val);
}else if(val > data && right == NULL){
right = new AVL<T>(val);
}else if(val < data){
left = left ->insert(val);
if(Balance(left) - Balance(right) == 2)
if(val < left->data)
Temp = SRL();
else
Temp = DRL();
}else if(val > data){
right = right ->insert(val);
if(Balance(right) - Balance(left) == 2)
if(val > right->data)
Temp = SRR();
else
Temp = DRR();
}else{
cout <<"Duplicate value";
}
Temp->balance = max(Balance(Temp ->left),Balance(Temp->right))+1;
return Temp;
}
template<class T>
void AVL<T> :: preorder()
{
cout<<data<<"\t";
if(left != NULL)
left->preorder();
if(right != NULL)
right->preorder();
}
template<class T>
bool AVL<T>:: search(T val)
{
bool re;
if(data == val )
re = true;
else if(val < data && left != NULL){
re = left -> search(val);
}else if (val > data && right != NULL){
re = right -> search(val);
}
return re;
}
template<class T>
void AVL<T> :: level_order_traversal( AVL<T>* root)
{
queue<AVL<T>*> Queue;
AVL<T>* Traverse;
int count = 0,m = 1,x = 1;
Queue.push(root);
while(!Queue.empty()){
Traverse = Queue.front();
Queue.pop();
cout<<Traverse->data<<"\t";
count++;
if(count == m){
cout<<"\n";
m = x * 2 + count;
x= x * 2;
}
if(Traverse->left != NULL)
Queue.push(Traverse->left);
if(Traverse->right != NULL)
Queue.push(Traverse->right);
}
}
int main()
{
AVL<int> *root = NULL;
int choice;
while(1){
cout<<"\n 1.Insert.";
cout<<"\n 2.Search";
cout<<"\n 3.Print";
cout<<"\n 4.Exit";
cout<<"\n Enter your choice:";
cin>>choice;
switch(choice){
case 1:
int val;
cout<<"\n Enter the value to insert:";
cin>>val;
if(root == NULL){
root = new AVL<int>(val);
}else{
root = root -> insert(val);
}
break;
case 2:
cout<<"\n Enter the value to search:";
cin>>val;
if(root == NULL){
cout<<"\n Empty tree.";
}else if(root->search(val) == true){
cout<<"\n The data "<<val <<" present in the Tree";
}else{
cout<<"\n The data "<<val <<" not present in the Tree";
}
break;
case 3:
if(root == NULL){
cout<<"\n Empty Tree.";
}else{
cout<<"\n--------In Preorder Traversal---------\n";
root->preorder();
cout<<"\n--------In Level order Travelsal------\n";
root->level_order_traversal(root);
}
break;
case 4:
exit(0);
break;
default:
cout<<"Enter correct choice";
}
}
}
No comments:
Post a Comment