【问题标题】:Java Binary Trees: Finding the node that reaches two nodes with shortest distanceJava二叉树:找到到达两个节点​​最短距离的节点
【发布时间】:2017-07-26 04:53:44
【问题描述】:

我目前正在编写一种方法,该方法在 Java 中搜索二叉树以查找在最短距离内到达两个节点​​的节点。我的想法是,如果两个节点都存在于树中,那么根节点将是第一个可以同时到达这两个节点的节点。所以我递归并检查根的左/右,看看它们是否也可以同时到达。通过找到至少一个节点后无法同时到达的第一个节点,我应该找到与我正在搜索的两个节点距离最短的节点。

我已将此任务分解为两种方法,一种名为 canReach 用于搜索树中的节点,另一种使用 canReach 的布尔返回来确定向下移动树以及沿哪个方向移动,名为reachBoth。

通过调试,我非常有信心 canReach 准确地搜索树。但是,如果两个节点都不存在于树或根中,那么我似乎永远不会给出任何答案,除非它们之间存在任何答案。

在树下导航时反复检查对两个节点的访问是否是个好主意?如果有人能看到我的方法达到的地方Both 有问题,我将不胜感激。

public boolean canReach(T d) {
        // Helper method for reachesBoth. Takes an argument of data of a Node
        int comp = d.compareTo(data); // Compare data to current node
        if (comp == 0) return true; // Found the node
        if (comp < 0) { // search left for the node
            if (left != null) {
                if (left.canReach(d) == true) return true;
            }
            return false;
        }
        if (comp > 0) { // search right for the node
            if (right != null) {
                if (right.canReach(d) == true) return true;
            }
            return false;
        }
        return false; // Cannot find the node in our tree
    }

    public T reachesBoth(T a, T b) {
        if (canReach(a) == true && canReach(b) == true) { 
            // Found the first node that can reach both desired nodes.
            // Must continue to see if a node with shorter distance
            // can reach both nodes as well.
            if (left != null && right != null) {
                // Case 1: Two more branches to check 
                if (left.reachesBoth(a, b) != null) left.reachesBoth(a, b);
                if (right.reachesBoth(a, b) != null) right.reachesBoth(a, b);
                //Both branches cannot reach both nodes sooner than we can.
                if (left.reachesBoth(a, b) == null & right.reachesBoth(a, b) == null) {
                    return this.data;
                } 
            }
            if (left != null && right == null) {
                // Case 2: Only left branch to check
                if (left.reachesBoth(a, b) == null) return this.data;
            }
            if (left == null && right != null) {
                // Case 3: Only right branch to check
                if (right.reachesBoth(a, b) == null) return this.data;
            }
            // Case 4: No more tree to search for a better solution
            if (left == null && right == null) return this.data;

        }

        return null; // Cannot locate both nodes in our tree from our start
    }

【问题讨论】:

    标签: java binary-search-tree distance


    【解决方案1】:

    在检查左右孩子时将递归更改为返回:

    if (left.reachesBoth(a, b) != null) return left.reachesBoth(a, b);

    if (right.reachesBoth(a, b) != null) return right.reachesBoth(a, b);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-15
      • 1970-01-01
      • 2020-08-02
      • 1970-01-01
      相关资源
      最近更新 更多