简化定义Scala Tree结构,包含两个部分: Branch和Tree。为了简化数据结构,Branch只包含 Tree类型的 左节点 和 右节点, Leaf包含具体 Valuecss
sealed trait Tree[+A] case class Leaf[A](value: A) extends Tree[A] case class Branch[A](left: Tree[A], right: Tree[A]) extends Tree[A]
树的遍历右两种方式:
- 深度优先
- 广度优先
这里用DFS 实现,深度优先搜索属于图算法的一种,英文缩写为DFS即Depth First Search,其过程简要来讲是对每个可能的分支路径深刻到不能再深刻为止,并且每一个节点只能访问一次。python
具体搜索顺序能够参考附图
1. 搜索根节点 左子树
2. 搜索当前树的左子树
3. 搜索当前树的左子树
4. 返回父节点,搜索父节点 右子树
5. 搜索当前树的左子树
6. 返回父节点,搜索父节点 右子树
7. 返回父节点, 返回父节点,返回父节点,搜索右子树
8. ….算法
咱们从一道题来熟悉Scala遍历操做,求Scala树中节点总数
按照DFS 思想实现代码以下数据结构
def countNodes[A](tree: Tree[A]): Int = { def go[A](tree: Tree[A], sum: Int): Int = tree match { case Leaf(v) => sum + 1 //叶子节点 sum+1 case Branch(left, right) => sum + 1 + go(left, 0) + go(right, 0) //分支节点 sum = sum + 1 + 左子树节点总数 + 右子树节点总数 case _ => 0 } go(tree, 0) //递归 }
结合【Scala笔记——道】Scala List HOF foldLeft / foldRight 中讲到的List fold思想函数
咱们将countNode 方法的遍历进行抽象化
,首先一个函数最重要的就是输入 / 输出,参考List fold,不难理解对Tree的函数操做必然是将Tree[A]转化为 [B],咱们这里实现的简化树模型中,Value的具体存储都在叶子节点中,所以测试
def deepFirstSearch[A, B](tree: Tree[A])(f: A => B)... = tree match { case Leaf(value) => f(value) ... }
其次,将DFS 搜索的过程进行抽象。对每个 枝点,首先搜索 枝点的左节点,获得左节点执行结果之后,再搜索右节点,获得右节点执行结果之后,执行 对左右子树 函数结果的 函数操做,所以spa
def deepFirstSearch[A, B](tree: Tree[A])(f: A => B)(g: (B, B) => B) : B = tree match {
case Leaf(value) => f(value)
case Branch(l, r) => g( deepFirstSearch(l), deepFirstSearch(r) )
}
经过几个小例子来实践deepFirstSearch.net
获取Tree[Int]中最大值scala
def maximum(tree: Tree[Int]): Int =
deepFirstSearch(tree)(l => l)(_ max _)
求树的最大深度3d
def depth[A](tree: Tree[A]): Int = deepFirstSearch(tree)(_ => 1)(_.max(_) + 1)
MAP函数 将A 转化为B
def map[A, B](tree: Tree[A])(f: A => B): Tree[B] = {
deepFirstSearch(tree)( x => (Leaf(f(x)): Tree[B]))( (a, b) => Branch(a, b))
测试以下
def main(args: Array[String]): Unit = { val tree = Branch( Branch( Branch (Leaf(1), Branch( Leaf(7), Branch( Leaf(8), Leaf(9) ))), Branch( Leaf(34), Leaf(4))), Branch( Leaf(5), Leaf(6))) println("Max value :" + maximum(tree)) println("Depth :" + depth(tree)) println("Map :" + map(tree)(x => if(x%2 == 0) Branch(Leaf(1), Leaf(2)) else x)) }
结果以下
Max value :34 Depth :6 Map :Branch(Branch(Branch(Leaf(1),Branch(Leaf(7),Branch(Leaf(Branch(Leaf(1),Leaf(2))),Leaf(9)))),Branch(Leaf(Branch(Leaf(1),Leaf(2))),Leaf(Branch(Leaf(1),Leaf(2))))),Branch(Leaf(5),Leaf(Branch(Leaf(1),Leaf(2)))))