-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_4_Trees.sc
More file actions
32 lines (28 loc) · 887 Bytes
/
3_4_Trees.sc
File metadata and controls
32 lines (28 loc) · 887 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{
import math.max
enum Tree[+A]:
case Leaf(v: A)
case Branch(l: Tree[A], r: Tree[A])
def depth: Int = this match {
case Leaf(v) => 1
// redbook solution: case Leaf(_) => 0
//case Branch(l, r) => max(1 + depth(l), 1 + depth(r))
// redbook solution:
case Branch(l, r) => 1 + (l.depth.max(r.depth))
}
object Tree:
def maximum(t: Tree[Int]): Int = t match {
case Leaf(v) => v
case Branch(l, r) => max(maximum(l), maximum(r))
}
def map[A, B](t: Tree[A], f: A => B): Tree[B] = t match {
case Leaf(v) => Leaf(f(v))
case Branch(l, r) => Branch(map(l, f), map(r, f))
}
// this one is interesting, compare with List.foldLeft
def fold[A, B](t: Tree[A], f: A => B, g: (B, B) => B): B = t match {
case Leaf(v) => f(v)
case Branch(l, r) => g(fold(l,f,g), fold(r,f,g))
}
}
3.max(4)