【发布时间】:2012-02-03 13:52:35
【问题描述】:
假设我有一些使用案例类构建的树,类似这样:
abstract class Tree
case class Branch(b1:Tree,b2:Tree, value:Int) extends Tree
case class Leaf(value:Int) extends Tree
var tree = Branch(Branch(Leaf(1),Leaf(2),3),Branch(Leaf(4), Leaf(5),6))
现在我想构建一个方法来将具有某个 id 的节点更改为另一个节点。很容易找到这个节点,但我不知道如何更改它。有什么简单的方法吗?
【问题讨论】:
-
我假设 Branch 和 Leaf
extends Tree,对吧? -
所讨论的数据结构不同,但这个问题与我链接的问题大致相同,并且该问题中的所有答案都是通用的并且适用于您的问题。
-
是的,一般来说问题是关于同一件事,但@dave 为我的问题提供了更好(更简单)的答案。
标签: scala tree pattern-matching case-class