java - 如何在二叉搜索树中查找节点

标签 java

我正在阅读有关 java 中的二叉树的内容。我找到了这段代码:

public BSTNode findNode(Comparable val){
        int delta = val.compareTo(value);
        // the value is less than this.value
        if(delta < 0){
            // if there is a leftChild, return left.findNode(val)
            // there is no leftChild, so the val does not exist
            // in the node, so return null
            return (left!= null)? left.findNode(val): null;
        }
        // else if the value is greater than this.value
        else if (delta > 0){
            // if there is a rightChild, then return right.findNode(val)
            // else, there is no rightChild, return null
            return (right != null)? right.findNode(val): null;
        }
        // else, dela == 0, so we have found the node with that
        // val, return the node
        return this;
    }

我不明白这是如何工作的:

return (left!= null)? left.findNode(val): null;
return (right != null)? right.findNode(val): null;

你能用另一种方式重写它吗?

谢谢

最佳答案

好的,我们一步步来。首先,我将重点关注算法本身。

class Node<T> {
    T value;
    Node left;
    Node right;
}

保证左边的所有值都小于或等于value,并且右边的所有值都大于或等于。这使得搜索更加容易。如果您要查找元素val,只需将其与当前Node 中的value 进行比较即可。如果所需元素与当前元素相同,则您已找到它。如果它更大,它只能位于树的右侧部分。否则在左侧。

该元素可能不在这里。如果您发现它应该位于当前节点的左侧/右侧,但那里什么也没有 (null),就会发生这种情况。

所以 BinaryTreeSearch 是:

T search(Node tree, T val) {
    int delta = tree.getValue.compareTo(val);
    if (delta == 0) {
        return tree.getValue;
    } else if (delta > 0) {
        return search(tree.getRight(), val);
    } else {
        return search(tree.getLeft(), val);
    }
}

但是等等...如果该项目不在这里,这会导致 NPE。 我们来修改一下:

T search(Node tree, T val) {
    if (tree == null)
         return null;
    int delta = tree.getValue.compareTo(val);
    if (delta == 0) {
        return tree.getValue;
    } else if (delta > 0) {
        return search(tree.getRight(), val);
    } else {
        return search(tree.getLeft(), val);
    }
}

这也可以这样重写:

T search(Node tree, T val) {
    int delta = tree.getValue.compareTo(val);
    if (delta == 0) {
        return tree.getValue;
    } else if (delta > 0) {
        if (tree.getRight() == null) 
            return null;
        return search(tree.getRight(), val);
    } else {
        if (tree.getLeft() == null)
            return null;
        return search(tree.getLeft(), val);
    }
}

但是这里出现了三元运算符,它被缩短和简化了if-else

result = testCondition ? value1 : value2

相同
if (testCondition) {
    result = value1;
} else {
    result = value2;
}

Another conditional operator is ?:, which can be thought of as shorthand for an if-then-else statement (discussed in the Control Flow Statements section of this lesson). This operator is also known as the ternary operator because it uses three operands. In the following example, this operator should be read as: "If someCondition is true, assign the value of value1 to result. Otherwise, assign the value of value2 to result."

所以我们最终收到:

T search(Node tree, T val) {
    int delta = tree.getValue.compareTo(val);
    if (delta == 0) {
        return tree.getValue;
    } else if (delta > 0) {
        return (tree.getRight() == null) ? null : search(tree.getRight(), val);
    } else {
        return (tree.getLeft() == null) ? null : search(tree.getLeft(), val);
    }
}

关于java - 如何在二叉搜索树中查找节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40340776/

相关文章:

java - 将微调器信息传递给另一个 Activity

java - Spark 上下文 WholeTextFiles 和 JavaStreamingContext textFileStream 在 Apache Spark 集群中不起作用

java - eclipse 。如何使用快捷方式激活工具提示弹出窗口?

java - 断言可用于维护循环不变量和检查程序正确性

java - 如何从 parquet 文件读取和写入自定义类

java - 如何在 HSQLDB 中创建特定日期?

java - 如何检索最大 id 值并存储在 int 变量中?

java - 为什么所有匿名类都是隐式最终的?

java - 理解java中的泛型语法——实现多个接口(interface)作为参数的泛型类型

java - Spring Zuul - 网关超时