Reputation: 37
#include<iostream>
#include<cstdio>
#include<sstream>
#include<algorithm>
#define pow2(n) (1 << (n))
using namespace std;
template<class T> struct avl_node
{
T data;
struct avl_node *left;
struct avl_node *right;
};
avl_node<int>* root;
template<class T>
class avlTree
{
public:
int height(avl_node<T> *);
int diff(avl_node<T> *);
avl_node<T> *rr_rotation(avl_node<T> *);
avl_node<T> *ll_rotation(avl_node<T> *);
avl_node<T> *lr_rotation(avl_node<T> *);
avl_node<T> *rl_rotation(avl_node<T> *);
avl_node<T> *balance(avl_node<T> *);
avl_node<T> *insert(avl_node<T> *, int );
void display(avl_node<T> *, int);
avlTree()
{
root = NULL;
}
};
int main()
{
int choice, item;
avlTree<int> avl;
while (1)
{
cout<<"\n---------------------"<<endl;
cout<<"AVL Tree "<<endl;
cout<<"\n---------------------"<<endl;
cout<<"1.Insert Element into the tree"<<endl;
cout<<"2.Display Balanced AVL Tree"<<endl;
cout<<"3.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter value to be inserted: ";
cin>>item;
root = avl.insert(root, item);
break;
case 2:
if (root == NULL)
{
cout<<"Tree is Empty"<<endl;
continue;
}
cout<<"Balanced AVL Tree:"<<endl;
avl.display(root, 1);
break;
case 3:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}
template<class T>
int avlTree<T>::height(avl_node<T> *temp)
{
int h = 0;
if (temp != NULL)
{
int l_height = height (temp->left);
int r_height = height (temp->right);
int max_height = max (l_height, r_height);
h = max_height + 1;
}
return h;
}
template<class T>
int avlTree<T>::diff(avl_node<T> *temp)
{
int l_height = height (temp->left);
int r_height = height (temp->right);
int b_factor= l_height - r_height;
return b_factor;
}
template<class T>
avl_node<T> *avlTree<T>::rr_rotation(avl_node<T> *parent)
{
avl_node *temp;
temp = parent->right;
parent->right = temp->left;
temp->left = parent;
return temp;
}
template<class T>
avl_node<T> *avlTree<T>::ll_rotation(avl_node<T> *parent)
{
avl_node *temp;
temp = parent->left;
parent->left = temp->right;
temp->right = parent;
return temp;
}
template<class T>
avl_node<T> *avlTree<T>::lr_rotation(avl_node<T> *parent)
{
avl_node *temp;
temp = parent->left;
parent->left = rr_rotation (temp);
return ll_rotation (parent);
}
template<class T>
avl_node<T> *avlTree<T>::rl_rotation(avl_node<T> *parent)
{
avl_node *temp;
temp = parent->right;
parent->right = ll_rotation (temp);
return rr_rotation (parent);
}
template<class T>
avl_node<T> *avlTree<T>::balance(avl_node<T> *temp)
{
int bal_factor = diff (temp);
if (bal_factor > 1)
{
if (diff (temp->left) > 0)
temp = ll_rotation (temp);
else
temp = lr_rotation (temp);
}
else if (bal_factor < -1)
{
if (diff (temp->right) > 0)
temp = rl_rotation (temp);
else
temp = rr_rotation (temp);
}
return temp;
}
template<class T>
avl_node<T> *avlTree<T>::insert(avl_node<T> *root, int value)
{
if (root == NULL)
{
root = new avl_node;
root->data = value;
root->left = NULL;
root->right = NULL;
return root;
}
else if (value < root->data)
{
root->left = insert(root->left, value);
root = balance (root);
}
else if (value >= root->data)
{
root->right = insert(root->right, value);
root = balance (root);
}
return root;
}
template<class T>
void avlTree<T>::display(avl_node<T> *ptr, int level)
{
int i;
if (ptr!=NULL)
{
display(ptr->right, level + 1);
printf("\n");
if (ptr == root)
cout<<"Root -> ";
for (i = 0; i < level && ptr != root; i++)
cout<<" ";
cout<<ptr->data;
display(ptr->left, level + 1);
}
}
Can you help me please? I must finish this task today.
1 error C2955: avl_node: use of template requires template argument list 2 error C2512: avl_node: unable to match function definition to an existing declaration
Upvotes: 2
Views: 104
Reputation: 2033
Along with Cyber's answer, you also need to make changes to temp
variable.
template<class T>
avl_node<T> *avlTree<T>::rr_rotation(avl_node<T> *parent)
{
avl_node<T> *temp;
//rest of code
}
template<class T>
avl_node<T> *avlTree<T>::ll_rotation(avl_node<T> *parent)
{
avl_node<T> *temp;
// rest of code
}
template<class T>
avl_node<T> *avlTree<T>::lr_rotation(avl_node<T> *parent)
{
avl_node<T> *temp;
//rest of code
}
template<class T>
avl_node<T> *avlTree<T>::rl_rotation(avl_node<T> *parent)
{
avl_node<T> *temp;
//rest of code
}
template<class T>
avl_node<T> *avlTree<T>::insert(avl_node<T> *root, int value)
{
if (root == NULL)
{
root = new avl_node; //make it avl_node<T>
//rest of code
}
Upvotes: 2
Reputation: 117856
You need to add the template argument to the members within your structs, for example
template<class T>
struct avl_node
{
T data;
struct avl_node *left;
struct avl_node *right;
};
Should be
template<class T>
struct avl_node
{
T data;
struct avl_node<T> *left;
struct avl_node<T> *right;
};
This is true of the functions you define later which create or declare avl_node*
variables, these should be using avl_node<T>*
variables.
Upvotes: 2