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)
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 Harvey
 Assembly Language, Computer Programming, machine language, Machine code, CS 61A, machine language instruction

Click to edit the document details