Trees

This week we talked about two-dimensional data structures, specifically trees. This is one of the subjects students have the most difficulty with on MT2, so it's worth your time to work through these practice problems!

We've seen four kinds of trees by now; let's recap:

Branches have data Branches do not have data
N children "Capital-T" Trees

MAKE-TREE DATUM CHILDREN LEAF?
(names from CS61A)

No empty Trees; "base case" is rarely necessary.
Deep lists (SICP "trees")

CONS LIST APPEND CAR CDR NULL? LIST? etc.

Base cases are the empty list (no more children) and other not-pairs ("atoms"), which are data.
2 children Binary trees

MAKE-BINARY-TREE ENTRY LEFT-BRANCH RIGHT-BRANCH NULL?
(names from SICP)

Base case is the "empty binary tree" (empty list).
car/cdr recursion

CONS CAR CDR NULL? PAIR?

Base case is any not-pair ("atom"), though sometimes the empty list is treated specially.

Pairwise recursion is the one that trips most people up, but it's basically how you do list recursion most of the time anyway (do something to the car, then to the cdr). Refer to the lecture notes in the Volume II reader for a good review.

The problems we did in section mostly focused on Trees:

Here's the main group problem I posed in class: given a tree where all data are numbers, find a node which has a given number as its datum and return it. Return #f if no such node exists. Can you solve it...

We did a few more practice problems with Trees in class: tree-map and leaf?. Both are also in the lecture notes (page 300 in my copy of the Vol. II reader), so I'm not going to discuss them here.

Finally, here's a quick thought experiment (that we didn't get to in class). What if we used Trees in the calculator program, rather than deep lists? What do we have to change in calc-eval to make this work?

Back to Jordy's home page | Course home page