关于二叉查找树如何获取其双亲结点的方法
二叉树结构体的定义如下
struct binaryTree;
typedef struct binaryTree
{
int value;
binaryTree* LeftChild;
binaryTree* RightChild;
}TreeNode;
以下是如何获取双亲结点的具体代码
TreeNode* Parent_LinkedList(TreeNode* node)
{
TreeNode* p=root;//先创建指针p使其指向根节点root
if(node==root)//如果结点node是根节点,则其双亲结点为空
{
return NULL;
}
else{
while(p->LeftChild!=node&&p->RightChild!=node)//当指针p的左右子树不为所找结点时,执行如下代码:
{
if(node->value<p->value)
{
p=p->LeftChild;
}
else if(node->value>p->value)
{
p=p->RightChild;
}
}
return p;//返回双亲结点指针
}
}
};