[   ^ to index...   |   <-- previous   |   next -->   ]

Exercises

Using trees

Recall the binary tree definition from class slides

datatype 'a BTree
    = EmptyBTree
    | BTNode of 'a * 'a BTree * 'a BTree

Write the following recursive functions, and deduce their types. Meta-note: in pure functional style, do not think about modifying the original tree; think about how to construct the desired result.

    Write a function that...
  1. ...sums the elements of a BTree.
  2. ...counts the maximum depth of a BTree.
  3. ...merges the elements of two BTrees. You need not worry or think about ordering, just produce a tree that has all the elements of both trees, but no more. (This is quite easy, actually; step back and think of the dumbest possible way to make a tree out of two trees.)
  4. ...computes the intersection of the elements of two BTrees. In other words, consider the two trees as sets of elements; construct a list that contains only the elements found in both trees.

Keunwoo Lee
Last modified: Wed Apr 18 16:42:10 PDT 2001