所以我想在二叉树中找到Node的父节点。 假设我通过文本文件在树中输入30,15,17,45,69,80,7。
树应该是
30
15 45
7 17 69
80
这是我的代码:
Node* BST::searchforparentnode(Node* pRoot, int value)
{
if(pRoot->pleft == NULL && pRoot->pright == NULL)
return NULL;
if(pRoot->pleft->value == value || pRoot->pright->value == value)
return pRoot;
if(pRoot->value > value)
return searchforparentnode(pRoot->pleft,value);
if(pRoot->value < value)
return searchforparentnode(pRoot->pright,value);
}
在这种情况下,我不考虑用户是否输入了Root节点的值。
事情是,当我输入15,17,7,Root节点的左分支中的所有值时,它就出来了。但是当我想从右分支(69,80)中找到值为父节点时除了45,程序停止运行。
有关导致此错误的人的任何想法?谢谢你的阅读。
答案 0 :(得分:4)
45
似乎没有left
节点,它是NULL
,因此检查pRoot->pleft->value == value
是未定义的行为。
30
/ \
15 45
/ \ \
7 17 69
\
80
所以你需要做一个检查,例如:
Node* BST::searchforparentnode(Node* pRoot, int value)
{
if(pRoot->pleft == NULL && pRoot->pright == NULL)
return NULL;
if( (pRoot->pleft != NULL && pRoot->pleft->value == value)
|| (pRoot->pright != NULL && pRoot->pright->value == value))
return pRoot;
if(pRoot->value > value)
return searchforparentnode(pRoot->pleft,value);
if(pRoot->value < value)
return searchforparentnode(pRoot->pright,value);
}
但是,要检查的另一件事是pRoot
本身是NULL
:
Node* BST::searchforparentnode(Node* pRoot, int value)
{
if (pRoot == NULL)
return NULL;
if(pRoot->pleft == NULL && pRoot->pright == NULL)
return NULL;
if( (pRoot->pleft != NULL && pRoot->pleft->value == value)
|| (pRoot->pright != NULL && pRoot->pright->value == value))
return pRoot;
if(pRoot->value > value)
return searchforparentnode(pRoot->pleft,value);
if(pRoot->value < value)
return searchforparentnode(pRoot->pright,value);
}
答案 1 :(得分:3)
你的问题是:
if(pRoot->pleft->value == value || pRoot->pright->value == value)
因为在您检查之前是否 AND 右为空。在上面提到的部分中,一个可以为null
您可能想要将代码更改为以下内容:
Node* BST::searchforparentnode(Node* pRoot, int value)
{
if(pRoot->pleft == NULL && pRoot->pright == NULL)
return NULL;
//Added check
if(pRoot->pleft && pRoot->pleft->value == value)
return pRoot;
//Added check
if(pRoot->pright && pRoot->pright->value == value)
return pRoot;
//Check also needed here
if(pRoot->pleft && pRoot->value > value)
return searchforparentnode(pRoot->pleft,value);
//Check also needed here
if(pRoot->pright && pRoot->value < value)
return searchforparentnode(pRoot->pright,value);
}
恕我直言:您还可以在每个节点中创建指向父节点的指针。这可以加速某些事情!
答案 2 :(得分:0)
public Node nodeParent(Node root, Node n){
if(root==null){
return null;
}
Node p=root;
if(p.left==null && p.right==null ){
return null;
}
if(p.left!=null && p.key>n.key){
p=p.left;
if(p.left.key==n.key){
return p;
}
}
if(p.right!=null && p.key<n.key){
p=p.right;
if(p.right.key==n.key){
return p;
}
}
if(p.left!=null && p.right!=null ){
if(p.key<n.key){
p=p.right;
}else{
p=p.left;
}
}
return p;
}
答案 3 :(得分:0)
下面的代码段应该可以解决问题:
public Node parent(Node root, int childValue){
if(root == null)
return null;
Node left = root.left;
Node right = root.right;
if(left != null && left.value == childValue)
return root;
else if(right != null && right.value == childValue)
return root;
else if(left != null && left.value > childValue)
return parent(left, childValue);
else if(right != null && right.value < childValue)
return parent(right, childValue);
else
return null;
}
答案 4 :(得分:0)
您还可以编写此子程序的简单迭代版本,如下所示:
public Node parent(Node root, int childValue){
Node parent = NULL;
while(root != NULL && root.value != childValue) {
parent=root;
if(root.value > childValue)
root = root.left;
else
root=root.right;
}
return parent