求二叉树中某结点的父结点(左右孩子表示法)
数据结构:
struct BinTreeNode;
typedef struct BinTreeNode * PBinTreeNode;
struct BinTreeNode
{
int info;
PBinTreeNode llink;
PBinTreeNode rlink;
};
typedef struct BinTreeNode * BinTree;
程序代码:
PBinTreeNode parent_btree(PBinTreeNode p,BinTree t)
{
PBinTreeNode r;
if(p == NULL)
return NULL;
if(p == t || t == NULL)
return NULL;
if(t->llink == t || t->rlink == t)
return t;
r = parent_btree(p,t->llink);
if(r != NULL)
return r;
r = parent_btree(p,t->rlink);
if(r != NULL)
return r;
return NULL;
}