--- a/progs/lecture2.scala Thu Nov 17 15:12:47 2016 +0000
+++ b/progs/lecture2.scala Thu Nov 17 16:21:56 2016 +0000
@@ -220,11 +220,18 @@
val t1 = Node(4, Node(2, Leaf(), Leaf()), Node(7, Leaf(), Leaf()))
insert(t1, 3)
+def depth(tr: Tree): Int = tr match {
+ case Leaf() => 0
+ case Node(_, left, right) => 1 + List(depth(left), depth(right)).max
+}
+
+
def balance(tr: Tree): Int = tr match {
case Leaf() => 0
- case Node(_, left, right) => balance(left) - balance(right)
+ case Node(_, left, right) => depth(left) - depth(right)
}
+balance(insert(t1, 3))
// another example