我不知道我的代码出了什么问题 - 二叉树



我现在正在用c++制作一个二叉树,使用friend类。

但是,有些地方出了问题,我不知道该怎么改

template <class Type>
class BinaryTree{
public:
BinaryTree(){
    root = new BTNode<Type>();  
    currentNode = NULL;
}
~BinaryTree(){
    delete root, currentNode;
};
void insertItem(Type data){
    if(currentNode==NULL){
        Item = data;
        currentNode = root;
    }
    if(data<currentNode){
        if(currentNode->Left.is_empty()){
            currentNode->Left = new BTNode(data);
            currentNode = root;
            return;
        }
        else{
            currentNode = currentNode->Left;
            return insertItem(data);
        }
    }
    if(data>currentNode){
        if(currentNode->Right.is_empty()){
            currentNode->Right = new BTNode(data);
            currentNode = root;
            return;
        }
        else{
            currentNode = currentNode->Right;
            return insertItem(data);
        }
    }
    currentNode = root;
    return; 
}
void deleteItem(Type data){}
void is_empty(){
    if (this == NULL) return 1;
    else return 0;
}
void printInOrder(){                                
    if(!(currentNode->Left).is_empty()){
        currentNode = currentNode->Left;            
    }
}
private:
    BTNode<Type>* currentNode;
    BTNode<Type>* root;
};

,这里是BTNode类,它存储BinaryTree的项,并指向下一个Node:

template <class Type>
class BTNode{
public:
    friend class BinaryTree<Type>;
    BTNode(){}
    BTNode(Type data){
        Item = data;
    }
    ~BTNode(){}

private:
    Type Item;
    BTNode<Type> *Left, *Right;
};

二叉树类的BTNode*根指向第一个节点,而currentNode将在做插入或合并节点时指向'当前节点'。

但是当我编译时,编译错误C2143发生在BinaryTree类中,:

BTNode<Type>* root;
BTNode<Type>* currentNode;

错误提示<前面没有符号;

似乎BTNodeBinaryTree类中没有正确定义。编译器不理解BTNode是一个类型。确保你正确地包含和链接了这些文件。

最新更新