我正在尝试使用堆栈在二进制搜索树中执行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’
答案 0 :(得分:4)
此行无效,因为它指的是堆栈大小而不是堆栈中当前的项目数:
while( s->size > 0 ){
....
}
答案 1 :(得分:3)
你有一个错字!它是TreeNode
,而不是Treenode
。第51行应为:
nodes = new TreeNode[size];
请注意,由于未知类型在C ++中被自动假定为int类型,因此您会收到该特定错误(? - 我不是100%)。