使用C ++模板的errorC2512和错误C2955

时间:2015-05-18 17:46:29

标签: c++ templates avl-tree

#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);
    }
}
你能帮帮我吗?我今天必须完成这项任务。

1错误C2955:avl_node:使用模板需要模板参数列表 2错误C2512:avl_node:无法将函数定义与现有声明匹配

2 个答案:

答案 0 :(得分:2)

您需要将模板参数添加到结构中的成员,例如

template<class T>
struct avl_node
{
    T data;
    struct avl_node *left;
    struct avl_node *right;
};

应该是

template<class T>
struct avl_node
{
    T data;
    struct avl_node<T> *left;
    struct avl_node<T> *right;
};

对于您稍后定义的创建或声明avl_node*变量的函数,情况也是如此,这些函数应使用avl_node<T>*个变量。

答案 1 :(得分:2)

除了Cyber​​的回答,您还需要对temp变量进行更改。

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
}