Karoline
Karoline

Reputation: 125

Finding a parent node to a child node in a binary tree

I was trying to make a function that gets a child node and a binary tree and returns the parent node of that given child. If the child node given is the root it should return null. This is what I was trying to do :

//input : The function gets a binary tree and a child node
//output : The function returns the parent of a given child of the tree, if the given child is the root
//then it will return null
public static BinTreeNode<Character> Parent(BinTreeNode<Character> bt, BinTreeNode<Character> child){
    if(bt==null){
        return bt;
    }
    else{
        if(bt.hasLeft()&&(bt.hasRight())){
            if((bt.getLeft().getValue()==child.getValue())||(bt.getRight().getValue()==child.getValue())){
                return bt;
            }
            else{
                Parent(bt.getLeft(),child);
                Parent(bt.getRight(),child);

            }
        }
        if(bt.hasLeft()){
            if(bt.getLeft().getValue()==child.getValue()){
                return bt;
            }
            else{
                return Parent(bt.getLeft(),child);
            }
        }
        if(bt.hasRight()){
            if(bt.getRight().getValue()==child.getValue()){
                return bt;
            }
            else{
                return Parent(bt.getRight(),child);
            }
        }
    }
    return null;
}

for some reason it keeps returning me null, after debugging I saw that when it gets to the first condition it checks if it equals but then instead of continuing in the recursion towards the other conditions and what I told it to do it just went staring to the return null at the bottom and I don't know why? I will be very thankful for any help I an kind of new to java.

Upvotes: 2

Views: 1758

Answers (2)

SeverityOne
SeverityOne

Reputation: 2691

The problem is that you will only ever check all the left children. So your code will only work for children on the left (of children on the left, etc). Once you reach the bottom, you return null and that's it.

To fix it, change your code to the following:

public static BinTreeNode<Character> parent(BinTreeNode<Character> bt, BinTreeNode<Character> child) {
    if (bt == null) {
        return bt;
    } else {
        if (bt.hasLeft()) {
            if (bt.getLeft().equals(child)) {
                return bt;
            }

            BinTreeNode<Character> possibleParent = parent(bt.getLeft(), child);
            if (possibleParent != null) {
                return possibleParent;
            }
        }

        if (bt.hasRight()) {
            if (bt.getRight().equals(child)) {
                return bt;
            } else {
                return parent(bt.getRight(), child);
            }
        }

        return null;
    }
}

And it'll work.

Upvotes: 0

Glim
Glim

Reputation: 361

You need to walk through the tree before return it.

Your implementation is only walking left and then returning null (probably your child is on the right)...

Try something like that:

public static BinTreeNode<Character> getParent(BinTreeNode<Character> 
bt, BinTreeNode<Character> child){
    if(bt==null){
        return bt;
    }
    BinTreeNode<Character> left = null;
    BinTreeNode<Character> right = null;
    if(bt.hasLeft()){
        if(bt.getLeft().equals(child))
            return bt;
        left = getParent(bt.getLeft(),child);
    }
    if(left == null){
        if(bt.hasRight()) {
            if(bt.getRight().equals(child))
                return bt;
            right = getParent(bt.getRight(),child);
        }
    } else {
        return left;
    }
    return right;
}

Here we have an implementation of Depth-first search.

Upvotes: 2

Related Questions