说我有一些节点和他们的直接父母,比如:
case class Mapping(name: String, parents: Seq[String] = Nil)
val mappings = Seq(
Mapping("aaa"),
Mapping("bbb"),
Mapping("ccc"),
Mapping("ddd", Seq("aaa", "bbb")),
Mapping("eee", Seq("ccc")),
Mapping("fff", Seq("ddd")),
Mapping("ggg", Seq("aaa", "fff")),
Mapping("hhh")
)
如何在Scala中编写一个函数,它将根据它们构建一个树?
def buildTrees(data: Seq[Mapping]): Seq[Node] = ???
case class Node(name: String, children: Seq[Node] = Nil)
val trees = buildTrees(mappings)
private val expectedTree = Seq(
Node("aaa", Seq(
Node("ggg"),
Node("ddd", Seq(
Node("fff", Seq(
Node("ggg")
))))
)),
Node("bbb", Seq(
Node("ddd", Seq(
Node("fff", Seq(
Node("ggg")
))))
)),
Node("ccc", Seq(
Node("eee")
)),
Node("hhh", Seq())
)
if (trees == expectedTree) {
println("OK")
} else {
println("Not equal")
}
如何实施buildTrees
方法?我想了一会儿,但可以得到一个优雅的解决方案。
更新:希望看到一个包含不可变数据的解决方案
答案 0 :(得分:4)
另一个实现是:
import scala.collection.immutable.Queue
class CyclicReferences(val nodes: Seq[String])
extends RuntimeException(f"elements withing cycle detected: ${nodes mkString ","}")
def buildTrees(data: Seq[Mapping]): Seq[Node] = {
val parents = data.map(m => (m.name, m.parents)).toMap withDefaultValue Seq.empty
val children = data.flatMap(m => m.parents map ((_, m.name))).groupBy(_._1).mapValues(_.map(_._2))
def loop(queue: Queue[String], unresolved: Map[String, Set[String]], nodes: Map[String, Node]): TraversableOnce[Node] = queue match {
case Seq() => if (unresolved.isEmpty) nodes.values else throw new CyclicReferences(unresolved.keys.toSeq)
case key +: rest =>
val (newQueue, newUnresolved) = ((rest, unresolved) /: parents(key)) { (pair, parent) =>
val (queue, children) = pair
val ch = children(parent) - key
if (ch.isEmpty) (queue :+ parent, children - parent)
else (queue, children.updated(parent, ch))
}
val node = Node(key, children.getOrElse(key, Seq.empty) map nodes)
loop(newQueue, newUnresolved, nodes + (key -> node))
}
val initial = Queue(parents.keys.filter(key => !children.contains(key)).toSeq: _*)
val unresolved = children mapValues (_.toSet) withDefaultValue Set.empty
loop(initial, unresolved, Map()).filter(node => parents(node.name).isEmpty).toIndexedSeq
}
与谢飞的解决方案的主要区别在于:
copy
电话Map
和Set
操作所以它可能不是最简单但50%的生产准备就绪。
答案 1 :(得分:1)
def buildTrees(data: Seq[Mapping]): Seq[Node] = {
def attachToParents(newChild: Mapping, parents: Seq[Node]): Seq[Node] = {
for (parent <- parents) yield {
val attachedChildren = attachToParents(newChild, parent.children)
if (newChild.parents.contains(parent.name))
parent.copy(children = Node(newChild.name) +: attachedChildren)
else
parent.copy(children = attachedChildren)
}
}
@tailrec
def helper(xs: Seq[Mapping], accu: Seq[Node]): Seq[Node] = xs match {
case Seq() => accu
case head +: tail => head match {
case Mapping(name, Seq()) => helper(tail, accu :+ Node(name))
case Mapping(name, parents) => helper(tail, attachToParents(head, accu))
}
}
helper(data, Seq())
}