当我将非NULL指针传递给方法时,为什么非NULL指针看起来会转换为NULL?(C++)
我想打印一个二叉树。我的树结构是:当我将非NULL指针传递给方法时,为什么非NULL指针看起来会转换为NULL?(C++),c++,methods,printing,null,binary-tree,C++,Methods,Printing,Null,Binary Tree,我想打印一个二叉树。我的树结构是: struct tree { Node * root; tree() {root = NULL;} Node * insert(Node * n, people * pp); void print(Node * pp) { if(pp==NULL) { cout<<"Node sent to print is null"<<endl; return;} print(pp->left
struct tree
{
Node * root;
tree() {root = NULL;}
Node * insert(Node * n, people * pp);
void print(Node * pp)
{
if(pp==NULL)
{ cout<<"Node sent to print is null"<<endl; return;}
print(pp->left);
cout<<pp->p->lname<<endl;
print(pp->right);
}
};
我主要这样称呼:
if(tr->root == NULL) cout<<"drat";
tr -> print(tr->root);
我完全确定tr->root不是空的,因为我只是在打印东西之前跟了它一行。为什么我的打印方法坚持要传递空值?您的打印函数是递归的。它将沿着树向下移动,直到找到一个空叶子并打印该消息。然后它将打印其余的
我建议作出以下更改:
void print(Node * pp)
{
if(pp==NULL)
{ cout<<"Node sent to print is null"<<endl; return;}
if (pp->left) print(pp->left);
cout<<pp->p->lname<<endl;
if (pp->right) print(pp->right);
}
职业环境也是如此。请不要骂人。我已从您的问题中删除了诅咒。您可能需要Node*insertNode*&n,people*pp;但只要您没有在那里显示实现,我就不知道了。您是说您看到发送到print的节点为null,并且在它之前没有看到drat?我看不出有什么办法。但是上面的代码绝对允许发送到print的节点后面没有换行的drat为null,例如发送到print的dratNode为null。