Java二叉树:查找到达距离最短的两个节点的节点
我目前正在编写一个方法,在Java中搜索二叉树,寻找距离最短的两个节点。我的想法是,如果两个节点都存在于树中,那么根节点将是第一个可以到达这两个节点的节点。因此,我递归并检查根的左/右,看看它们是否也可以同时到达这两个位置。在找到至少一个到达的节点后,通过找到第一个不能同时到达这两个节点的节点,我应该拥有距离我正在搜索的两个节点最短的节点 我将此任务分解为两种方法,一种是名为canReach的方法,用于在树中搜索节点,另一种是使用canReach的布尔返回来确定向下移动树以及向哪个方向移动,名为ReacheSeals 通过调试,我确信canReach能够准确地搜索树。然而,如果两个节点都不存在于树中,或者根节点不存在于树中,那么我似乎永远不会用除null以外的任何答案来回答这两个问题,如果它们存在,则永远不会在两者之间 在树下导航时反复检查对两个节点的访问是一个好主意吗?如果有人能看到我的方法在哪里,这两个都被窃听了,我会很感激你的洞察力Java二叉树:查找到达距离最短的两个节点的节点,java,binary-search-tree,distance,Java,Binary Search Tree,Distance,我目前正在编写一个方法,在Java中搜索二叉树,寻找距离最短的两个节点。我的想法是,如果两个节点都存在于树中,那么根节点将是第一个可以到达这两个节点的节点。因此,我递归并检查根的左/右,看看它们是否也可以同时到达这两个位置。在找到至少一个到达的节点后,通过找到第一个不能同时到达这两个节点的节点,我应该拥有距离我正在搜索的两个节点最短的节点 我将此任务分解为两种方法,一种是名为canReach的方法,用于在树中搜索节点,另一种是使用canReach的布尔返回来确定向下移动树以及向哪个方向移动,名为
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
}
public boolean canReach(td){
//ReacheSeals的助手方法。接受节点数据的参数
int comp=d.compareTo(数据);//将数据与当前节点进行比较
如果(comp==0)返回true;//找到节点
如果(comp<0){//向左搜索节点
if(左!=null){
if(left.canReach(d)==true)返回true;
}
返回false;
}
如果(comp>0){//右键搜索节点
if(右!=null){
if(right.canReach(d)==true)返回true;
}
返回false;
}
return false;//在树中找不到节点
}
公共T到达两者(T a,T b){
如果(canReach(a)=true&&canReach(b)=true){
//找到第一个可以到达两个所需节点的节点。
//必须继续查看是否存在距离较短的节点
//也可以到达两个节点。
if(左!=null和右!=null){
//案例1:还要检查两个分支
如果(left.reachesleet(a,b)!=null)left.reachesleet(a,b);
如果(right.reachesleet(a,b)!=null)right.reachesleet(a,b);
//两个分支不能比我们更快到达两个节点。
if(left.reacheslow(a,b)==null;right.reacheslow(a,b)==null){
返回此.data;
}
}
if(左!=null&&right==null){
//案例2:只检查左分支
如果(left.reachesleep(a,b)==null)返回此.data;
}
if(left==null&&right!=null){
//案例3:只检查正确的分支
if(right.reachesleep(a,b)==null)返回此.data;
}
//案例4:没有更多的树来搜索更好的解决方案
如果(left==null&&right==null)返回此.data;
}
return null;//无法从一开始就在树中找到两个节点
}
将递归更改为在检查左、右子级时返回:
if(left.reacheslow(a,b)!=null)返回left.reacheslow(a,b)代码>
if(right.reacheslow(a,b)!=null)返回right.reacheslow(a,b)代码>