# HG changeset patch # User Christian Urban # Date 1479399716 0 # Node ID 8bdc44963c0c6d815e04be23d609643164948f46 # Parent 8e866d0af03a2339e0167df2e07cd042b37bed1c updated diff -r 8e866d0af03a -r 8bdc44963c0c 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