1
這裏的工會是二叉樹的實現,我在Coursera,3周馬丁·奧德斯基的視頻講座看到:二叉樹 - 斯卡拉遞歸的樹木
abstract class IntSet
{
def incl(x: Int): IntSet
def contains(x: Int): Boolean
def union(other: IntSet): IntSet
}
object Empty extends IntSet
{
def contains(x: Int): Boolean = false
def incl(x: Int): IntSet = new NonEmpty(x, Empty, Empty)
def union(other: IntSet): IntSet = other
override def toString = "."
}
class NonEmpty(elem: Int, left: IntSet, right: IntSet) extends IntSet
{
def contains(x: Int): Boolean =
if (x<elem) left contains x
else if (x > elem) right contains x
else true
def incl(x: Int): IntSet =
if (x<elem) new NonEmpty(elem, left incl x, right)
else if (x > elem) new NonEmpty(elem, left, right incl x)
else this
def union(other: IntSet): IntSet =
((left union right) union other) incl elem
override def toString = "{" + left + elem + right + "}"
}
如此空虛和非空遵循的標準概述由IntSet。我感興趣的是NonEmpty類中的聯合方法。我想了解它是如何工作的。
對我來說,看起來好像有一個無限循環出現。但我比某些更把我所造的下方某處的評估錯誤:
- ((L_1üR_1)UO),包括E1
- ((L_3üR_3)U R_1),包括E3
- (歐盟R_1),包括E3
- 2.
- 3.
- 2. 等 等