This preview shows pages 1–3. Sign up to view the full content.
CS 61A, Fall 1997
Midterm #2
Professor Harvey
Problem #1
This problem concerns the tree abstract data type defined by the constructor maketree, invoked with
(maketree <datum> <children>), and the selectors datum and children.
(a) Write the procedure treeify:
The argument to treeify is a nonempty list representing arithmetic computation in infix notation, such as this
example:
((3 + 4) * (7  (2 / 2)))
More precisely, a computation is either a number or a list of three elements, in which the first and last are
computations and the middle element is a symbol representing an operator (such as +).
The value returned by treeify is a tree in which the branch nodes are operator symbols and the leaf nodes are
numbers:
*
/
\
+

/
\ /
\
3
4 7
/
/
\
2
2
(b) Suppose that the time required for an addition or subtraction is one microsecond, and the time required for
a multiplication or a division is five microseconds.
+  1
* / 5
Write a time procedure that takes as its argument a computation tree as produced by treeify, and returns the
number of microseconds required to compute the desired value.
>(time (treeify '((3 + 4) * (7  (2 / 2))))
12
Problem #2
The procedure addup, shown later, takes two arguments: a list of numbers, and a goal number. It returns a list
of numbers, a subset of the original list, whose sum is the goal number, or #f if there is no such list. It returns
the shortest possible list.
CS 61A, Midterm #2, Fall 1997
CS 61A, Fall 1997Midterm #2Professor Harvey
1
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document > (addup '(2 6 3 4 5) 10)
(4 6)
> (addup '(2 3 4 5) 10)
(5 3 2)
Note that (5 3 2) would be a solution in the first example, but addup chooses the twonumber solution
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.
 Fall '08
 Harvey
 Computer Programming

Click to edit the document details