简体   繁体   中英

Finding the parent of a node in a Binary tree

I am trying to write a method to find the parent of a given node. Here's my method.
I created a BinaryNode object r which initially refers to 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);
            }
        }
    }  

THis code doesn't work properly .I think that's because r is a null object.Because when I do

if (isRoot(findParent) || r==null){
                System.out.println(r==null);
                return null;}  

r==null evaluates to true .How come that happen because I have inserted nodes as

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);  

and the root is not null.
Can some one please point out why that happens and if what I am trying to do in order to find the parent node is logically correct.

The problem is that you MUST keep track of your current node, while keeping the node who's parent you want to find. And as far as I understand your code, you keep the variable, but never change it
I'd recommend using a helper function. This would look something like that:

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);
            }
        }
    }
}  

I compared value to value because I didn't define the way to compare the nodes.

    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;
        }
    }

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

Here is code to find out parent node using a stack Data Structures.

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");}
            }
        }

I prefer to delegate as much work as I can to lower-level components, in this case the Node class. Re: finding a node's parent, this is how I do it ...

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM