我试图定义,定义如下二叉树匹配:斯卡拉图案选项类型
trait Node {
val label: Int
}
case class BranchNode(override val label: Int, left: Option[Node], right: Option[Node]) extends Node
case class LeafNode(override val label: Int) extends Node
,然后使用Scala的模式如下匹配定义一个简单的printTree
方法:
def printTree(aTree: Option[Node]): Unit = aTree match {
case None => print(".")
case Some(LeafNode(label)) => print(label)
case Some(BranchNode(label, left, right)) => "{" + printTree(left) + label + printTree(right) + "}"
}
Intellij IDE警告我说这个匹配可能并不完整。 Option
可以有None
或Some
作为值。在Option[Node]
的情况下,它可以是Some(LeafNode)
或Some(BranchNode)
。我还忽略了什么其他案例?