为什么这不能递归地工作?

发布于 2024-12-18 21:34:40 字数 744 浏览 2 评论 0原文

下面是 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
}

我在二叉搜索树中创建了范围方法,实现了中序遍历算法。所以它必须递归地工作,但它不打印任何东西,List()。如何修复我的算法?或编辑我的代码?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溺孤伤于心 2024-12-25 21:34:40

我不知道 scala,但您需要使用作为参数传递到递归函数的列表 l 并使用 rangerec 函数的输出。

private def rangerec(r : Node, l : List[Int], low : Int, high :Int) : List[Int] = {
      var resultList : List[Int] = l
      if(r.left != null) {
        resultList = rangerec(r.left, l, low, high)
      } else if(r.right != null) {
        resultList = rangerec(r.right, l, low, high)
      } else {
        if(r.key >= low && r.key <= high) {
            resultList = l ::: List(r.key)
        } 
      }
      resultList
}

I don't know scala, but you need to use list l passed as a parameter into recursive function and use output from rangerec function.

private def rangerec(r : Node, l : List[Int], low : Int, high :Int) : List[Int] = {
      var resultList : List[Int] = l
      if(r.left != null) {
        resultList = rangerec(r.left, l, low, high)
      } else if(r.right != null) {
        resultList = rangerec(r.right, l, low, high)
      } else {
        if(r.key >= low && r.key <= high) {
            resultList = l ::: List(r.key)
        } 
      }
      resultList
}
烦人精 2024-12-25 21:34:40

在函数外部定义您的 resultList,因为我看到您将结果附加到该变量。顺便说一句,顺序遍历也遵循这个规则。访问左,访问根,最后访问右。然而从代码中(虽然我不懂scala),我可以破译你正在访问左,右,最后是根。

等效的递归按顺序打印 javacode 看起来像这样

 public void printOrdered(Node node){
  if(node.left != null){
   printOrdered(node.left); //VISIT LEFT
  }
  System.out.println(node.data); //VISIT ROOT AND PRINT
  if(node.right!=null){
   printOrdered(node.right); //VISIT RIGHT
  }
 }

所以,scala 可能看起来像这样(语法可能是错误的)

private def rangerec(r : Node) : Void = {
      if(r.left != null) {
        rangerec(r.left)
      } 
      resultList = resultList :: List(r.key)
      if(r.right != null) {
        rangerec(r.right)
      }
}

这里 resultList 是一个 List 类型的变量,应该从外部传递。

Define your resultList outside the function as I see you appending result to this variable. By the way, in order traversal follows this rule. Visit Left, Visit Root and finally Visit Right. However from the code (although I don't know scala), I can decipher that you are visiting left, right and finally root.

A equivalent recursive in-order printing javacode would have looked like this

 public void printOrdered(Node node){
  if(node.left != null){
   printOrdered(node.left); //VISIT LEFT
  }
  System.out.println(node.data); //VISIT ROOT AND PRINT
  if(node.right!=null){
   printOrdered(node.right); //VISIT RIGHT
  }
 }

So, the scala may look like this (syntax may be wrong)

private def rangerec(r : Node) : Void = {
      if(r.left != null) {
        rangerec(r.left)
      } 
      resultList = resultList :: List(r.key)
      if(r.right != null) {
        rangerec(r.right)
      }
}

Here resultList is a variable of type List which should be passed from outside.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文