
Untitled
By: a guest on
May 2nd, 2012 | syntax:
None | size: 1.53 KB | hits: 13 | expires: Never
Change node in Scala case class tree
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))
// Changed Tree to Node, b/c that seems more accurate
// Since Branch and Leaf both have value, pull that up to base class
sealed abstract class Node(val value: Int) {
/** Replaces this node or its children according to the given function */
def replace(fn: Node => Node): Node
/** Helper to replace nodes that have a given value */
def replace(value: Int, node: Node): Node =
replace(n => if (n.value == value) node else n)
}
// putting value first makes class structure match tree structure
case class Branch(override val value: Int, left: Node, right: Node)
extends Node(value) {
def replace(fn: Node => Node): Node = {
val newSelf = fn(this)
if (this eq newSelf) {
// this node's value didn't change, check the children
val newLeft = left.replace(fn)
val newRight = right.replace(fn)
if ((left eq newLeft) && (right eq newRight)) {
// neither this node nor children changed
this
} else {
// change the children of this node
copy(left = newLeft, right = newRight)
}
} else {
// change this node
newSelf
}
}
}
val tree1 = Branch(Branch(Leaf(1),Leaf(2),3),Branch(Leaf(4), Leaf(5),6))
val tree2 = tree1.copy(b2 = tree1.b2.copy(b1 = Leaf(5))
// -> Branch(Branch(Leaf(1),Leaf(2),3),Branch(Leaf(5), Leaf(5),6))