我正在尝试编写一个方法,该方法返回给定节点的父节点。
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;
}
但是,我收到 NullPointerExceptions,当其中一个或两个子节点都是空节点并且 equals 尝试将该值与空值进行比较时。我该如何继续解决这个问题?我还是 Java 新手。