Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在二叉树中查找节点的父级_Java_Data Structures_Binary Tree_Parent - Fatal编程技术网

Java 在二叉树中查找节点的父级

Java 在二叉树中查找节点的父级,java,data-structures,binary-tree,parent,Java,Data Structures,Binary Tree,Parent,我正在尝试编写一个方法来查找给定节点的父节点。这是我的方法。 我创建了一个BinaryNode对象r,它最初引用root public BinaryNode r=root; public BinaryNode parent(BinaryNode p){ BinaryNode findParent=p; if (isRoot(findParent) || r==null){ return null;

我正在尝试编写一个方法来查找给定节点的父节点。这是我的方法。
我创建了一个
BinaryNode
对象r,它最初引用root

    public BinaryNode r=root;
    public BinaryNode parent(BinaryNode p){
    BinaryNode findParent=p;        
        if (isRoot(findParent) || r==null){
                return null;
        }
        else{
            if(r.left==findParent || r.right==findParent)
                return r;
            else{
                if (r.element<findParent.element)
                    return parent(r.right);
                else
                    return parent(r.left);
            }
        }
    }  
r==null
计算结果为
true
。为什么会发生这种情况,因为我将节点作为

public static void main (String args[]){
        BinaryTree t=new BinaryTree();
        t.insert(5);
        t.insert(t.root,4);
        t.insert(t.root,6);
        t.insert(t.root,60);
        t.insert(t.root,25);
        t.insert(t.root,10);  
并且根不为空。

有人能指出为什么会发生这种情况,以及我为查找父节点所做的尝试是否在逻辑上正确。

使用两个参数:一个用于当前节点,一个用于正在搜索的节点。

问题是,您必须跟踪当前节点,同时保留要查找的父节点。据我所知,您保留了变量,但从不更改它
我建议使用helper函数。这看起来是这样的:

public BinaryNode parent(BinaryNode p){
    parentHelper(root,p)
}
private BinaryNode parentHelper(BinaryNode currentRoot, BinaryNode p) {        
    if (isRoot(p) || currentRoot==null){
            return null;
    }
    else{
        if(currentRoot.left==p || currentRoot.right==p)
            return currentRoot;
        else {
            if (currentRoot.element<p.element) {
                return parentHelper(currentRoot.right,p);
            }
            else {
                return parentHelper(currentRoot.left,p);
            }
        }
    }
}  
public二进制节点父节点(BinaryNode p){
parentHelper(根,p)
}
私有BinaryNode parentHelper(BinaryNode currentRoot,BinaryNode p){
if(isRoot(p)| | currentRoot==null){
返回null;
}
否则{
if(currentRoot.left==p | | currentRoot.right==p)
返回当前根;
否则{

if(currentRoot.element这里是使用堆栈数据结构查找父节点的代码

Stack<TreeNode> parentStack = new Stack<TreeNode>();

public static void inOrderTraversal(TreeNode root){
            if(root != null){
                if(parentStack.size()==0){
                    parentStack.push(root);
                }
                if(root.getLeftChild()!=null){
                    parentStack.push(root); 
                    inOrderTraversal(root.getLeftChild());  
                }
                parent = parentStack.pop();
                System.out.println(root.getNodeValue()+"'s parent is "+parent.getNodeValue());
                if(root.getRightChild()!=null){
                    parentStack.push(root);
                    inOrderTraversal(root.getRightChild());
                }
            }
            else{
                if(root==null){System.err.println("Can't process a empty root tree");}
            }
        }
Stack parentStack=new Stack();
OrderTraversal中的公共静态无效(TreeNode根){
if(root!=null){
if(parentStack.size()==0){
parentStack.push(root);
}
if(root.getLeftChild()!=null){
parentStack.push(root);
inOrderTraversal(root.getLeftChild());
}
parent=parentStack.pop();
System.out.println(root.getNodeValue()+“”的父项是“+parent.getNodeValue());
if(root.getRightChild()!=null){
parentStack.push(root);
inOrderTraversal(root.getRightChild());
}
}
否则{
if(root==null){System.err.println(“无法处理空根目录树”);}
}
}

我比较了值与值,因为我没有定义比较节点的方法

    public static Node FindParent(Node root, Node node)
    {
        if (root == null || node == null)
        {
            return null;
        }
        else if ( (root.Right != null && root.Right.Value == node.Value) || (root.Left != null && root.Left.Value == node.Value))
        {
            return root;
        }
        else
        {
            Node found = FindParent(root.Right, node);
            if (found == null)
            {
                found = FindParent(root.Left, node);
            }
            return found;
        }
    }

我更喜欢将尽可能多的工作委托给较低级别的组件,在本例中是节点类。Re:查找节点的父级,我就是这样做的

template <typename T>
Node<T>* Node<T>::parent (const Node<T>* node) 
{
    if (node)
    {
        if (*node < *this)
        {
            if (left && (*node < *left))
                return left->parent (node);
            return this;
        }
        if (*node > *this)
        {
            if (right && (*right > *node))
                return right->parent (node);
            return this;
        }
    }
    return nullptr;
}
模板
节点*节点::父节点(常量节点*节点)
{
如果(节点)
{
如果(*节点<*此)
{
if(左&(*节点<*左))
返回左->父节点;
归还这个;
}
如果(*节点>*此)
{
if(右&(*右>*节点))
返回权限->父节点;
归还这个;
}
}
返回空ptr;
}

如何调用find parent方法?您提供了哪些参数?@wastl:findParent不是一个方法。它是一个二进制节点,存储我们需要查找sorry父节点的节点,我指的是父节点(binaryNode)method@wastl:如果我想找到位于根左边的节点的父节点,可以将该方法称为`parent(root.left)`您是否检查了isRoot(findParent)的计算结果是否为true?虽然这是正确的,但这并不能解释OP代码的错误以及OP应该这样做的原因。添加这些内容无疑会得到更好的答案。嗯,这个问题看起来像一个家庭作业,所以我不想给出太多提示;)然后将此作为评论发布。我认为这太短了(从长度和内容上看)“我想r是一个变量,它保持当前的状态node@peq:这不是一个家庭作业。我正在学习数据结构,并在此基础上学习树。我只是尝试编写一个方法来查找家长,以帮助自己学习它感谢给定的答案。我意识到我从未更改当前变量并且找到了一种我认为可以解决这个问题的方法。开始时,我试图将根节点放到变量r中。为此,我使用了
public BinaryNode r=root;
just outside parent()方法。然后检查我是否使用了
public BinaryNode parent(BinaryNode p){System.out.println(“r”+r.element)
其中我得到一个空指针异常,这意味着r是一个空对象,这就是为什么
if(isRoot(findParent)| | r==null){
总是被求值,在那里它返回false,其余的代码甚至没有运行。**为什么r会变成null对象**。这是我的二进制树实现。
公共类二进制树{private BinaryNode root;public binaryTree(){root=null;}binaryTree(int nodeValue){root=new BinaryNode(nodeValue);}
我将元素作为
t.insert(5);t.insert(t.root,4);
您的代码运行时,我认为行
返回父级(currentRoot.right,p);
应该更改为
返回父级助手(currentRoot.right,p);
template <typename T>
Node<T>* Node<T>::parent (const Node<T>* node) 
{
    if (node)
    {
        if (*node < *this)
        {
            if (left && (*node < *left))
                return left->parent (node);
            return this;
        }
        if (*node > *this)
        {
            if (right && (*right > *node))
                return right->parent (node);
            return this;
        }
    }
    return nullptr;
}