Index: Binary Search Trees, Arithmetic Expressions
Trees are a very common form of data. In fact, lists are simply trees with at most one branch at each node. In lab, we'll see more uses of trees.
We often want to be able to build a data structure that allows us to look up information based on some key value. One simple idea would be to just create a list of all the data items. The disadvantage is that it can then take a long time to find the items at the end of the list, since we have to look at all the prior items first. Many data structures can be used for this problem, one of which is a binary search tree.
For simplicity, let's assume we just want to keep track of a bunch of numbers, remembering which ones we've seen. From this, we could easily generalize our results to, for example, keeping track of team player information indexed by player number, or to indexing on names rather than numbers.
The following are some binary search trees for the numbers 1,3,4,6,9:
4 / \ 1 6 \ \ 3 9
3 / \ 1 6 / \ 4 9
3 / \ 1 4 \ 6 \ 9
Many operations on binary search trees would be faster if the trees were also balanced, i.e., the branches were all more-or-less equally deep. Implementing balanced binary trees is much more difficult, as you'll see in Comp 212.
Arithmetic expressions can also be modeled as trees. For example, the Scheme expression
(+ 5 (* (- 1 8) (+ 7 1)))is pictorially
+ / \ 5 * / \ - + / \ / \ 1 8 7 1We would like to treat this expression as a piece of data. This is just like how DrScheme works -- one of your programs is really just a big piece of data for DrScheme. For simplicity, we'll just work with two arguments at a time.
An A-Expression is one of the following:
DrScheme, like any language interpreter, is just a generalization of evaluate. A Scheme expression is like an arithmetic expression, but with more cases. You'll see more of this in an upcoming homework assignment.