CS61A_fa97_mt2_sol - CS 61A Fall 1996 Midterm 2 solutions...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 61A Fall 1996 Midterm 2 solutions 1. expression trees (a) Many people got confused about which abstract data type we're using. As the problem clearly states, you are given the constructor MAKE-TREE with two arguments, and the selectors DATUM and CHILDREN. (define (treeify computation) (if (number? computation) (make-tree computation '()) (make-tree (cadr computation) (list (treeify (car computation)) (treeify (caddr computation)))))) The most common errors were about the base case: * Suppose the argument is a number. What should the result be? IT MUST STILL BE A TREE! You can't just return the number; numbers are not in the range of this procedure. * The problem statement specifically says, "the argument to treeify is a NONEMPTY list. .." Many people always use NULL? by reflex as their base case test. It's not too bad if you make that test unnecessarily, in addition to the correct test, although it makes your program harder to read. But some people checked for an empty list as their *only* base case test. In any problem involving trees, you have to consider leaf nodes as a base case! * Some people used things like (NUMBER? (CAR COMPUTATION)) as the base case test. It's possible to write a working procedure with that approach, but it's much harder to avoid bugs, and the result is very hard to read and understand. Perhaps you were influenced by the fact that the domain is supposed to be lists, which is true, but it doesn't hurt to extend the domain if that improves the structure of the procedure. Other people thought that something like (3) -- a list of one element -- could be an argument, but the problem statement says "either a number or a list of three elements," not "a list of either a number or three elements." The next most common errors were data abstraction violations: * Some people called make-tree with three arguments, following the model of the binary tree ADT in the text. This problem was about the more general tree ADT in which a node can have any number of children, even though in this example the nodes do have only two. [Why would that make sense? Maybe this procedure is part of a larger program in which we also generate expression trees from Scheme expressions like (+ 3 4 5 6).] * Others applied the selectors DATUM and CHILDREN to the procedure's argument, which is *not* a tree. It's just as much a data abstraction violation to use it when not appropriate as to not-use it when it is appropriate. But the worst error that came up more than once is to say one of these: ((treeify (car computation)) (treeify (cadr computation))) '((treeify (car computation)) (treeify (cadr computation)))
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
instead of calling LIST. If you don't understand why that's wrong, ask your TA to meet with you. (b)
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/23/2010 for the course CS 61A taught by Professor Harvey during the Fall '08 term at University of California, Berkeley.

Page1 / 7

CS61A_fa97_mt2_sol - CS 61A Fall 1996 Midterm 2 solutions...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online