struct leaf
{
int data;
leaf *l;
leaf *r;
};
struct leaf *p;
void tree::findparent(int n,int &found,leaf *&parent)
This is piece of code of BST. I want to ask. why
leaf *&parent
Why we need “reference mark” here?
parent is also a leaf, why can’t I just use leaf* parent?
code below for your reference. Thank you!
void tree::findparent(int n,int &found,leaf *&parent)
{
leaf *q;
found=NO;
parent=NULL;
if(p==NULL)
return;
q=p;
while(q!=NULL)
{
if(q->data==n)
{
found=YES;
return;
}
if(q->data>n)
{
parent=q;
q=q->l;
}
else
{
parent=q;
q=q->r;
}
}
}
You are passing the pointer
parentin by reference, so that you can modify that pointer:If you passed the pointer in by value, the modifications would be to a copy of the pointer that expires at the end of the function.