假设二叉树b采用二叉链存储结构,设计一个算法void findparent(BTNode *b,ElemType x,BTNode *&p)求指定值为x的结点的双亲结点p,提示,根结点的双亲为NULL,若在b中未找到值为x的结点,p亦为NULL。
void findparent(BTNode *b,ElemType x,BTNode *&p)
{ if (b!=NULL)
{ if (b->data==x) p=NULL;
else if (b->lchild!=NULL && b->lchild->data==x)
p=b;
else if (b->rchild!=NULL && b->rchild->data==x)
p=b;
else
{ findparent(b->lchild,x,p);
if (p==NULL)
findparent(b->rchild,x,p);
}
}
else p=NULL;
}