我的树对象:
private[package] object Tree {
/** A node in a Tree. */
class Node[T](val parent: Node[T]) extends Serializable {
var item: T = _
var count: Long = 0L
val children: mutable.Map[T, Node[T]] = mutable.Map.empty
def isRoot: Boolean = parent == null
}
/** Attribute in a tree */
private class Attribute [T] extends Serializable {
var count: Long = 0L
val nodes: ListBuffer[Node[T]] = ListBuffer.empty
}
}
上课:
private[package] class Tree[T] extends Serializable {
import Tree._
val root: Node[T] = new Node(null)
private val attributes: mutable.Map[T, Attribute[T]] = mutable.Map.empty
def extract(
minCount: Long,
validateSuffix: T => Boolean = _ => true): Iterator[(List[T], Long)] = {
//missing code
}
函数 extract 必须生成包含路径root-to-leaf的Iterator [List [T]]。如果每个节点的计数大于minCount,则该路径有效。
编辑:这是我的尝试: def extract(minCount: Long, validateSuffix: T => Boolean = _ => true): Iterator[(List[T], Long)] = {
def traverse(node: Node[T], path: List[T]): Iterator[(List[T], Long)] = {
path.::(node.item)
node.children.iterator.flatMap { case (item, child) =>
traverse(child, path).map { case (t, c) =>
(item :: t, c)
}
} ++ {
if (node.children.isEmpty && node.count >= minCount) {
Iterator.single((path, node.count))
} else {
Iterator.empty
}
}
}
traverse(root, List.empty)
编辑: 这就是我如何建造一棵树:
val tree = new Tree[String]
.add(Seq("a", "b", "c"))
.add(Seq("a", "b", "y"))
.add(Seq("a", "b"))
.add(Seq("a"))
.add(Seq("b"))
.add(Seq("b", "n"))
val paths = tree.extract(3L).map { case (items, count) =>
(items.toSet, count)
}.toSet
答案 0 :(得分:1)
这样做:
paths
private void SaveCustomer(TestModel ctx, Customer customer)
{
//save back
ctx.Customers.Attach(customer); //GET ERROR HERE
ctx.Entry(customer).State = EntityState.Modified;
ctx.SaveChanges();
}
方法返回一个路径数组,每个路径都是一个值数组。