Below is a program in Scala.
def range(low : Int, high : Int) : List[Int] = {
var result : List[Int] = Nil
result = rangerec(root, result, low, high)
result
}
private def rangerec(r : Node, l : List[Int], low : Int, high :Int) : List[Int] = {
var resultList : List[Int] = List()
if(r.left != null) {
rangerec(r.left, resultList, low, high)
} else if(r.right != null) {
rangerec(r.right, resultList, low, high)
} else {
if(r.key >= low && r.key <= high) {
resultList = resultList ::: List(r.key)
resultList
}
}
resultList
}
I made range method in my binary search tree, implementing in-order traversal algorithm. So it has to work recursively, but it doesn’t print anything, List(). How to fix my algorithm? or edit my code?
I don’t know scala, but you need to use list
lpassed as a parameter into recursive function and use output fromrangerecfunction.