Scala 如何使树映射尾部递归?

Scala 如何使树映射尾部递归?,scala,recursion,functional-programming,tree,tail-recursion,Scala,Recursion,Functional Programming,Tree,Tail Recursion,假设我有这样一个树数据结构: trait Node { val name: String } case class BranchNode(name: String, children: List[Node]) extends Node case class LeafNode(name: String) extends Node 假设我还有一个函数映射到树叶上: def mapLeaves(root: Node, f: LeafNode => LeafNode): Node = root

假设我有这样一个树数据结构:

trait Node { val name: String }
case class BranchNode(name: String, children: List[Node]) extends Node
case class LeafNode(name: String) extends Node
假设我还有一个函数映射到树叶上:

def mapLeaves(root: Node, f: LeafNode => LeafNode): Node = root match {
  case ln: LeafNode => f(ln)
  case bn: BranchNode => BranchNode(bn.name, bn.children.map(ch => mapLeaves(ch, f)))
}
现在我正试图使这个函数尾部递归,但很难弄清楚如何实现它。我读过这篇文章,但仍然不知道如何使二叉树解决方案适用于多路树

您将如何重写Mapleves以使其具有尾部递归性?

调用堆栈和“递归”仅仅是流行的设计模式,后来被纳入了大多数编程语言中(因此变得几乎“不可见”)。没有任何东西可以阻止您使用堆数据结构重新实现这两者。因此,以下是1960年代“显而易见”的TAOCP复古风格解决方案:

trait Node { val name: String }
case class BranchNode(name: String, children: List[Node]) extends Node
case class LeafNode(name: String) extends Node

def mapLeaves(root: Node, f: LeafNode => LeafNode): Node = {
  case class Frame(name: String, mapped: List[Node], todos: List[Node])
  @annotation.tailrec
  def step(stack: List[Frame]): Node = stack match {
    // "return / pop a stack-frame"
    case Frame(name, done, Nil) :: tail => {
      val ret = BranchNode(name, done.reverse)
      tail match {
        case Nil => ret
        case Frame(tn, td, tt) :: more => {
          step(Frame(tn, ret :: td, tt) :: more)
        }
      }
    }
    case Frame(name, done, x :: xs) :: tail => x match {
      // "recursion base"
      case l @ LeafNode(_) => step(Frame(name, f(l) :: done, xs) :: tail)
      // "recursive call"
      case BranchNode(n, cs) => step(Frame(n, Nil, cs) :: Frame(name, done, xs) :: tail)
    }
    case Nil => throw new Error("shouldn't happen")
  }
  root match {
    case l @ LeafNode(_) => f(l)
    case b @ BranchNode(n, cs) => step(List(Frame(n, Nil, cs)))
  }
}
tail recursive
step
函数采用具有“堆栈帧”的具体化堆栈。“堆栈帧”存储当前正在处理的分支节点的名称、已处理的子节点列表以及以后仍必须处理的剩余节点列表。这大致对应于递归
mapLeaves
函数的实际堆栈帧

有了这个数据结构,

  • 从递归调用返回对应于解构
    对象,或者返回最终结果,或者至少使
    堆栈
    缩短一帧
  • 递归调用对应于将
    前置到
    堆栈
  • 基本情况(调用叶子上的
    f
    )不会创建或删除任何帧
一旦理解了通常不可见的堆栈帧是如何显式表示的,转换就会变得简单,而且大部分是机械的

例如:

val example = BranchNode("x", List(
  BranchNode("y", List(
    LeafNode("a"),
    LeafNode("b")
  )),
  BranchNode("z", List(
    LeafNode("c"),
    BranchNode("v", List(
      LeafNode("d"),
      LeafNode("e")
    ))
  ))
))

println(mapLeaves(example, { case LeafNode(n) => LeafNode(n.toUpperCase) }))
输出(缩进):


使用名为的技术实现它可能更容易。 如果您使用它,您将能够使用两个调用自身的函数进行相互递归(使用
tailrec
,您只能使用一个函数)。与
tailrec
类似,此递归将转换为普通循环

蹦床在scala标准库中的
scala.util.control.TailCalls
中实现

import scala.util.control.TailCalls.{TailRec, done, tailcall}

def mapLeaves(root: Node, f: LeafNode => LeafNode): Node = {

  //two inner functions doing mutual recursion

  //iterates recursively over children of node
  def iterate(nodes: List[Node]): TailRec[List[Node]] = {
     nodes match {
       case x :: xs => tailcall(deepMap(x)) //it calls with mutual recursion deepMap which maps over children of node 
         .flatMap(node => iterate(xs).map(node :: _)) //you can flat map over TailRec
       case Nil => done(Nil)
     }
  }

  //recursively visits all branches
  def deepMap(node: Node):  TailRec[Node] = {
    node match {
      case ln: LeafNode => done(f(ln))
      case bn: BranchNode => tailcall(iterate(bn.children))
         .map(BranchNode(bn.name, _)) //calls mutually iterate
    }
  }

  deepMap(root).result //unwrap result to plain node
}
除了
TailCalls
之外,您还可以使用
Cats
中的
Eval
scalaz
中的
Trampoline

有了该实现功能,就可以毫无问题地工作:

def build(counter: Int): Node = {
  if (counter > 0) {
    BranchNode("branch", List(build(counter-1)))
  } else {
    LeafNode("leaf")
  }
}

val root = build(4000)

mapLeaves(root, x => x.copy(name = x.name.reverse)) // no problems

当我在您的实现中运行该示例时,它导致了
java.lang.StackOverflowerError

我认为您需要一些中间格式,正如您在提到的“答案”中所看到的那样-有一个列表(这也是与您的问题不同的结果),谢谢您的建议。我同意我需要一些“待办事项清单”(如上面提到的答案),但我不知道怎么做。你能使用或scalaz吗?我不喜欢(在这种情况下)。谢谢你介绍蹦床。我需要了解他们。非常感谢你这么详细的回答。我了解如何显式实现调用堆栈。现在我只是想知道如何简化这个解决方案。
def build(counter: Int): Node = {
  if (counter > 0) {
    BranchNode("branch", List(build(counter-1)))
  } else {
    LeafNode("leaf")
  }
}

val root = build(4000)

mapLeaves(root, x => x.copy(name = x.name.reverse)) // no problems