scala - 使用 Scala 模式匹配实现 BST
问题描述
我是 scala 的新手,并试图在 scala 中使用模式匹配概念来实现 BST。
编辑:我已经修改了插入函数,现在它的行为符合预期,有人可以帮我让它尾递归吗?此外,任何其他代码改进将不胜感激。
trait IntTree {
def contains(v: Int): Boolean
def insert(x: Int): IntTree
}
case object EmptyTree extends IntTree {
override def insert(x: Int): IntTree = Node(x, EmptyTree, EmptyTree)
override def contains(v: Int): Boolean = false
}
case class Node(elem: Int, left: IntTree, right: IntTree) extends IntTree {
override def contains(v: Int): Boolean = {
@scala.annotation.tailrec
def contains(t: IntTree, v: Int): Boolean = t match {
case Node(data, _, _) if (data == v) => true
case Node(data, l, r) => if (data > v) contains(l, v) else contains(r, v)
case _ => false
}
contains(this, v)
}
override def insert(x: Int): IntTree = {
def insert(t: IntTree, x: Int): IntTree = t match {
case Node(data, l, r) if (data > x) => Node(data, insert(l, x), r)
case Node(data, l, r) if (data < x) => Node(data, l, insert(r, x))
case EmptyTree => t insert x
case _ => t
}
insert(this, x)
}
}
解决方案
在您走下叶子之后,它需要重新访问和更新父节点:
sealed trait IntTree {
def contains(v: Int): Boolean
def insert(x: Int): Node // better to return Node here
}
def insert(x: Int): Node = {
@annotation.tailrec
def insert(t: IntTree, x: Int, parents: List[Node]): Node = t match {
case EmptyTree =>
parents.foldLeft(t insert x) { case (n, p) =>
if (p.elem >= n.elem) p.copy(left = n)
else p.copy(right = n)
}
case Node(data, l, r) =>
insert(if(data >= x) l else r, x, t :: parents)
}
insert(this, x, List.empty)
}
推荐阅读
- python - 在多个地方分割字符串Python
- javascript - 如何知道我在 Azure 认知服务语音合成 (TTS) 中使用了多少个字符?
- c# - .net core 3.1 分离模型之间的关系
- node.js - 剧作家拖放
- java - Spring Data JPA 规范 FETCH JOIN 问题
- react-native - 如何使用 Jest 测试 Modal (react-native) 是否可见?
- angular - 订阅和模板离子的问题
- mysql - Maria DB - 为 SQL 事件中的每一行执行存储过程
- python - How do I open an FTP directory so that I can use it the same way I could a normal directory?
- mongodb - Docker compose-Spring 引导服务未连接到 mongodb