首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >scala n-性树尾递归计算

scala n-性树尾递归计算
EN

Stack Overflow用户
提问于 2016-12-21 00:48:32
回答 2查看 199关注 0票数 2

我有一个树结构,它比二叉树结构更通用

代码语言:javascript
复制
sealed trait Tree[+A]

case class Leaf[A](value: Terminal[A]) extends Tree[A]
case class Node[A](op: Function[A], branches: Tree[A]*) extends Tree[A]

正如您所看到的,它可以有任意数量的分支。

我试图使一个评估方法成为尾部递归,但我不能这样做。

代码语言:javascript
复制
def evaluateTree[A](tree: Tree[A]): A = tree match {
  case Leaf(terminal) => terminal.value
  case Node(op: Function[A],  args @ _*) => op.operator((for (i <- args) yield evaluateTree(i)))
}

如何手动保存堆栈?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-01-03 11:56:37

实际上,这是有可能的,使用深入的第一次搜索。

代码语言:javascript
复制
def evaluateTree[A](tree: Tree[A]): A = {
  @tailrec
  def evaluateWhile[C](l: List[Function[C]], arguments: List[List[C]], n_args: List[Int], f: Int => Boolean, acc: C): (List[Function[C]], List[List[C]], List[Int]) =
    n_args match {
      case h :: t if f(h) =>
        evaluateWhile(l.tail, arguments.tail, n_args.tail, f, l.head.operator(arguments.head ::: List(acc)))
      case h :: t  =>
        (l, (List(acc) ::: arguments.head) :: arguments.tail,  List(n_args.head - 1) ::: n_args.tail)
      case _ =>
        (l, List(acc) :: arguments, n_args)
    }
  @tailrec
  def DFS(toVisit: List[Tree[A]], visited: List[String] = Nil, operators: List[Function[A]] = Nil, arguments: List[List[A]] = Nil, n_args: List[Int] = Nil, debug: Int = 0): A = toVisit match {
    case Leaf(id, terminal) :: tail if !visited.contains(id) => {
      val (operators_to_pass, args_to_pass, n_args_to_pass) =
        evaluateWhile[A](operators, arguments, n_args, x => x == 1, terminal.value)
      DFS(toVisit.tail, visited ::: List(id), operators_to_pass, args_to_pass, n_args_to_pass, debug + 1)
    }
    case Node(id, op, args @_*) :: tail if !visited.contains(id) => {
      DFS(args.toList ::: toVisit.tail, visited ::: List(id), op :: operators, List(Nil) ::: arguments, List(args.length ) ::: n_args, debug + 1)
    }
    case _ => arguments.flatten.head
  }
  DFS(List(tree))
}
票数 0
EN

Stack Overflow用户

发布于 2016-12-21 03:59:16

如果每个Node都能容纳不同的op,那么,不,我认为尾递归是不可能的。

另一方面,如果您可以将所有Leaf.value提供给单个op,则可能是可能的。

代码语言:javascript
复制
def evaluateTree[A](tree: Tree[A]): A = {
  @tailrec
  def allValues(branches: Seq[Tree[A]], acc: Seq[A] = Seq()): Seq[A] =
    if (branches.length < 1) acc
    else branches.head match {
      case Leaf(term) => allValues(branches.tail, term.value +: acc)
      case Node(_, args: Seq[Tree[A]]) => allValues(branches.tail ++ args, acc)
    }

  tree match {
    case Leaf(terminal) => terminal.value
    case Node(op: Function[A], args: Seq[Tree[A]]) => op.operator(allValues(args))
  }
}

我无法编译它,因为我没有TerminalFunction的定义,但它应该是解决问题的一种方法的合理大纲。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41253506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档