我有一个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;
}
答案 0 :(得分:0)
修正了它。我以前忘了解决某些情况。