【发布时间】:2015-12-25 02:43:21
【问题描述】:
我有一个 BST 任务,我必须找到最接近给定节点的节点。显然,如果节点在树中,它将被返回。否则,需要返回的节点是树上最近的节点(它可以在任一侧)。
脚本工作正常,除了节点存在的情况。该函数没有返回节点,而是继续运行并吐出一个不同的节点而不是杀死该函数。我想知道是否有人可以帮助我。
树的结构如下:
- 由节点组成
- 每个节点都有一个“入口”(一个字符串值)、一个“右”和一个“左”
这就是我所拥有的,谢谢!
public Entry getClosestEntry(String w) {
if (root == null) return null;
else if (root.left == null && root.right == null) return root.entry;
else return getClosestEntry(w, root, null, null, null);
}
private Entry getClosestEntry(String w, Node baseNode, Node highestLow, Node lowestHi, Node finalClosest) {
System.out.println("Dict: " + this);
System.out.println("Top string to compare: " + w);
System.out.println("Top baseNode: " + baseNode.entry.word);
if (w.compareTo(baseNode.entry.word) == 0) {
System.out.println("Top finalClosest is baseNode: " + baseNode.entry.word);
finalClosest = baseNode;
} else {
if (highestLow != null) System.out.println("Top highestLow: " + highestLow.entry.word);
else System.out.println("Top highestLow is null");
if (lowestHi != null) System.out.println("Top lowestHi: " + lowestHi.entry.word);
else System.out.println("Top lowestHi is null");
if (finalClosest != null) System.out.println("Top finalClosest: " + finalClosest.entry.word);
else System.out.println("Top finalClosest is null");
int cmp = w.compareTo(baseNode.entry.word);
if (cmp < 0) {
System.out.println("Word is less than base.");
if (lowestHi == null) {
lowestHi = baseNode;
System.out.println("lowestHi set to: " + lowestHi.entry.word);
}
else {
if (w.compareTo(lowestHi.entry.word) < 0 && baseNode.entry.word.compareTo(lowestHi.entry.word) < 0) {
lowestHi = baseNode;
System.out.println("lowestHi changed to: " + lowestHi.entry.word);
}
}
System.out.println("Returning right side of base.");
if (baseNode.right != null) getClosestEntry(w, baseNode.right, highestLow, lowestHi, finalClosest);
} else {
System.out.println("Word is greater than base.");
if (highestLow == null) {
highestLow = baseNode;
System.out.println("highestLow set to: " + highestLow.entry.word);
}
else {
if (w.compareTo(highestLow.entry.word) > 0 && baseNode.entry.word.compareTo(highestLow.entry.word) > 0) {
highestLow = baseNode;
System.out.println("highestLow changed to: " + highestLow.entry.word);
}
}
System.out.println("Returning left side of base.");
if (baseNode.left != null) getClosestEntry(w, baseNode.left, highestLow, lowestHi, finalClosest);
}
if (lowestHi == null && highestLow != null && finalClosest == null) {
System.out.println("lowestHi is null, so finalClosest must be highestLow.");
finalClosest = highestLow;
} else if (highestLow == null && lowestHi != null && finalClosest == null) {
System.out.println("highestLow is null, so finalClosest must be lowestHi.");
finalClosest = lowestHi;
} else if (lowestHi == null && highestLow == null && finalClosest == null) {
System.out.println("Both sides are null, so node must be null.");
return null;
} else {
System.out.println("Both sides are there. Default to highestLow if finalClosest is null");
if (finalClosest == null) finalClosest = highestLow;
}
}
System.out.println("Final Closest: " + finalClosest.entry.word);
return finalClosest.entry;
}
【问题讨论】:
-
if (...) return getClosestEntry(...);,不是if (...) getClosestEntry(...); -
@Dukeling 我一直在这条路上...做同样的事情。
-
@ClaytonAndrewCohn 在这种情况下,可以删除该行,因为结果不会在任何地方使用。例如
if (baseNode.left != null) getClosestEntry(w, baseNode.left, highestLow, lowestHi, finalClosest);将只是死代码,因为结果是return finalClosest.entry;并且递归调用不会改变任何值。 -
你能发一个minimal reproducible example(一个完整的程序以及示例输入和输出)吗?
标签: java search recursion binary-search-tree closest