updated
authorChristian Urban <urbanc@in.tum.de>
Thu, 17 Nov 2016 16:21:56 +0000
changeset 61 8bdc44963c0c
parent 59 8e866d0af03a
child 62 2151c77e1e24
updated
progs/lecture2.scala
--- 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