-1

我正在尝试使用堆栈在二叉搜索树中执行 dfs,但它给出了一个错误,请帮忙。

#include<iostream>

using namespace std;

class TreeNode{
    private:
        int data;
        TreeNode *left;
        TreeNode *right;
    public:
        TreeNode(){
            data=-1;
            left=right=NULL;
        }
        TreeNode(int n){
            data=n;
            left=right=NULL;
        }
        int getData(){
            return data;
        }
        void setData(int n){
            data=n;
        }
        void setLeft(TreeNode *left){
            this->left=left;                
        }
        void setRight(TreeNode *right){
            this->right=right;
        }
        TreeNode *getLeft(){
            return left;
        }
        TreeNode *getRight(){
            return right;
        }
        void print(){
            cout<<data<<"";
        }
};

class Stack{
    private:
        TreeNode *nodes;
    public:
        int size;
        int top;
        Stack(int size){
            top=0;
            this->size=size;
                nodes = new Treenode[size];
        }

        TreeNode pop(){
            if (top == 0){
                cout<<"Stack is empty"<<endl;
                return -1;
            }
            top--;
            return nodes[top];
        }

        TreeNode push(TreeNode *node){
            if (top==size){
                cout<<"Stack is full"<<endl;
                return 0;
                nodes[top]= *node;
                top++;
            }
        }
};

class BST{
    private:
        TreeNode * root;
    public:
        BST(){
            root=NULL;
        }

        void preorder(){
            preorder(root);
            cout<<endl;
        }

        void preorder(TreeNode *root){
            if(root !=NULL){
                root->print();
                preorder(root->getLeft());
                preorder(root->getRight());
            }
        }

        void insert(int n){
            TreeNode * newNode = new TreeNode(n);
            if(root == NULL)
            {
                root = newNode;
            }       
            else{
                TreeNode *temp = root;
                TreeNode *prev = NULL;
                while(temp != NULL){
                    prev=temp;
                    if (n < temp->getData()){
                        temp=temp->getLeft();
                    }
                    else{
                        temp=temp->getRight();
                    }
                    if (n > prev->getData()){
                        prev->setRight( newNode );
                    }
                    else{
                        prev->setLeft( newNode );
                    }
                }
            }
        }

        void print(){
            Stack *s= new Stack(50);
            s->push(root);
            while( s->size > 0 ){
                TreeNode t = s->pop();
                cout<<t.getData()<<"";
                if(t.getLeft() != NULL){
                    s->push(t.getLeft());
                }
                if(t.getRight() != NULL){
                    s->push(t.getRight());
                }
            }
            cout<<endl;
        }
};

int main(){
    int n;
    BST *bst = new BST(); 
    do{
        cin>>n;
        if( n == -1)
            break;
        bst->insert(n);
    }while( true ); 

    bst->print();
    bst->preorder();
    return 0;
}

这些是我得到的错误: -

testtree.cpp: In constructor ‘Stack::Stack(int)’:


testtree.cpp:51: error: expected type-specifier before ‘Treenode’


testtree.cpp:51: error: cannot convert ‘int*’ to ‘TreeNode*’ in assignment


testtree.cpp:51: error: expected ‘;’ before ‘Treenode’
4

2 回答 2

4

此行也是无效的,因为它指的是堆栈大小而不是堆栈上的当前项目数:

while( s->size > 0 ){
....
}
于 2013-03-31T12:40:11.757 回答
3

你有一个错字!是TreeNode,不是Treenode。第 51 行应为:

nodes = new TreeNode[size];

请注意,由于未知类型在 C++ 中被自动假定为 int 类型,因此您会遇到该特定错误(? - 我不是 100% 的)。

于 2013-03-31T12:29:54.113 回答