【发布时间】:2011-11-29 16:43:38
【问题描述】:
当我在二叉搜索树中给出一些整数时,我做了最接近的下限
def lowerBound(x : Int) : Int = {
var t = root
var result : Int = 0
while(t.key != x) {
if(x == t.key) {
result = t.left.key
}
else{
if(x < t.key) {
t = t.left
if(t == null) {
throw new NoSuchElementException
}
else {
result = t.key
}
}
else {
t = t.right
}
}
}
result
}
我就是这样做的。但它不打印任何结果。呜呜呜…… 我的算法中有反例吗?
如果有 {2, 3, 5, 7 ,8, 10, 99} lowerBound(6) 是 5。
【问题讨论】:
标签: algorithm scala data-structures binary-search-tree