Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.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 在树中查找Parentnode时出现NullPointerException_Java_Tree_Parent - Fatal编程技术网

Java 在树中查找Parentnode时出现NullPointerException

Java 在树中查找Parentnode时出现NullPointerException,java,tree,parent,Java,Tree,Parent,我正在尝试编写一个方法,它返回给定节点的父节点 public BinarySearchTreeNode<T> getParent(BinarySearchTreeNode<T> e) { if (e == null) { return null; } BinarySearchTreeNode<T> current = this.root; T eValue = e.getValue(); while (c

我正在尝试编写一个方法,它返回给定节点的父节点

public BinarySearchTreeNode<T> getParent(BinarySearchTreeNode<T> e) {
    if (e == null) {
        return null;
    }
    BinarySearchTreeNode<T> current = this.root;
    T eValue = e.getValue();
    while (current != null) {
        if (howManyChildren(current) == 0) {
            return null;
        } else if (eValue.equals(current.getLeft().getValue())
                || eValue.equals(current.getRight().getValue())) {
            return current;
        } else if (eValue.compareTo(current.getValue()) < 0) {
            current = current.getLeft();
        } else {
            current = current.getRight();
        }
    }
    return null;

}
public二进制搜索树节点getParent(二进制搜索树节点e){
如果(e==null){
返回null;
}
BinarySearchTreeNode当前=this.root;
T eValue=e.getValue();
while(当前!=null){
if(多少儿童(当前)==0){
返回null;
}else if(eValue.equals(current.getLeft().getValue())
||eValue.equals(current.getRight().getValue()){
回流;
}else if(eValue.compareTo(current.getValue())<0){
current=current.getLeft();
}否则{
current=current.getRight();
}
}
返回null;
}
但是,当一个或两个子节点都是null节点且等于尝试将值与null进行比较时,我会收到NullPointerException。
我该如何解决这个问题?我对Java还是新手。

在调用子对象的方法之前,确实需要检查子对象是否为null。在这种情况下,您可以调用
current.getLeft().getValue()
,但左边的子级可能为null。如果为null,则会得到NullPointerException

下面是一个在调用该方法之前进行检查以确保它们不为null的示例。注意,除了NullPointerException之外,我没有检查整个代码是否正确

public BinarySearchTreeNode<T> getParent(BinarySearchTreeNode<T> e) {
    if (e == null) {
        return null;
    }
    BinarySearchTreeNode<T> current = this.root;
    T eValue = e.getValue();
    while (current != null) {
        if (howManyChildren(current) == 0) {
            return null;
        } else if ((current.getLeft()!=null && eValue.equals(current.getLeft().getValue()))
                || (current.getRight()!=null) && eValue.equals(current.getRight().getValue())) {
            return current;
        } else if (eValue.compareTo(current.getValue()) < 0) {
            current = current.getLeft();
        } else {
            current = current.getRight();
        }
    }
    return null;

}
public二进制搜索树节点getParent(二进制搜索树节点e){
如果(e==null){
返回null;
}
BinarySearchTreeNode当前=this.root;
T eValue=e.getValue();
while(当前!=null){
if(多少儿童(当前)==0){
返回null;
}else如果((current.getLeft()!=null&&eValue.equals(current.getLeft().getValue()))
||(current.getRight()!=null)和&eValue.equals(current.getRight().getValue()){
回流;
}else if(eValue.compareTo(current.getValue())<0){
current=current.getLeft();
}否则{
current=current.getRight();
}
}
返回null;
}

使用对象变量,当值为
null
时,没有对象可以提供您将调用的方法

将对象变量的值与
null
进行比较:

if (obj == null) {
    ... do whatever ...
}
在许多情况下,可以将对象与null进行比较,如下所示:

if (obj.equals(null)) ...

注意

在您的代码中,如果您正在搜索的节点是根节点,它将永远找不到它

类似地,检查所有子项以查看它们是否匹配,然后通过将值与父项进行比较来确定哪些子项可以匹配。也许您应该重新排序一些东西,以便测试当前节点

如果为null,则搜索以失败告终 如果匹配,则返回上次迭代时保存的父项。 如果不匹配,则根据值和循环获取正确的子项(right或left)

这样做的好处是,只有在知道节点不为null时,才能获取节点的值

T eValue = e.getValue();

BinarySearchTreeNode<T> prev = null;
BinarySearchTreeNode<T> current = this.root;
while (current != null) {
    int compare = eValue.compareTo(current.getValue());
    if (compare == 0) {
        return prev;
    }

    prev = current; // save the parent in case it matches
    if (compare < 0) {
        current = current.getLeft();
    } else {
        current = current.getRight();
    }
}
T eValue=e.getValue();
BinarySearchTreeNode prev=null;
BinarySearchTreeNode当前=this.root;
while(当前!=null){
int compare=eValue.compareTo(current.getValue());
如果(比较==0){
返回上一个;
}
prev=current;//保存父项以防匹配
如果(比较<0){
current=current.getLeft();
}否则{
current=current.getRight();
}
}

如此简单的解决方案。。。如果我自己不能想到这一点,我想我已经很累了。谢谢。它起作用了。
T eValue = e.getValue();

BinarySearchTreeNode<T> prev = null;
BinarySearchTreeNode<T> current = this.root;
while (current != null) {
    int compare = eValue.compareTo(current.getValue());
    if (compare == 0) {
        return prev;
    }

    prev = current; // save the parent in case it matches
    if (compare < 0) {
        current = current.getLeft();
    } else {
        current = current.getRight();
    }
}