CS61A_fa97_mt2

# CS61A_fa97_mt2 - CS 61A Midterm#2 Fall 1997 CS 61A Fall 1997 Midterm#2 Professor Harvey Problem#1 This problem concerns the tree abstract data type

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 make-tree, invoked with (make-tree <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 two-number 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.

### Page1 / 4

CS61A_fa97_mt2 - CS 61A Midterm#2 Fall 1997 CS 61A Fall 1997 Midterm#2 Professor Harvey Problem#1 This problem concerns the tree abstract data type

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

View Full Document
Ask a homework question - tutors are online