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...
- ...sums the elements of a BTree.
- ...counts the maximum depth of a BTree.
- ...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.)
- ...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