简体   繁体   English

在二叉树中找到子节点的父节点

[英]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. 如果给定的子节点是根节点,则应返回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? 由于某种原因,它一直使我返回null,在调试之后,我看到当它到达第一个条件时会检查它是否等于,但是然后继续进行递归,而不是继续朝其他条件递归。底部的返回null,我不知道为什么? I will be very thankful for any help I an kind of new to java. 我将非常感谢我对java的新帮助。

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)... 您的实现只向左走,然后返回null(可能您的孩子在右边)...

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. 这里我们有一个深度优先搜索的实现。

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. 一旦到达最低点,就返回null就是这样。

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. 而且会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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