week6ReviewSol - DarrenKuo cs61atd OH:W56pm(277Soda...

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

View Full Document Right Arrow Icon
Darren Kuo cs61a-td OH:W 5 -6 pm (277 Soda) Review for trees, deeplist and scheme -1 Tree Problems! 1) Write depth-of-datum. It takes in a tree and an datum, and returns the depth of the datum in the tree.  You can assume that the datum is in the tree at most once. If the  datum is not in the tree, return #f. For example, if mytree is the tree                         A                        / \                      /      \                    B       C                   / \       / \                 D  E F    G               /                 / \             H               I     J                             / \       \                           K L      M then > (depth-of-datum mytree 'a) 0 > (depth-of-datum mytree 'j) 3 > (depth-of-datum mytree 'z) #f (Use: datum and children selectors) Write it both with and without mutual recursion. Here are the definitions: (I will also post them online, so you can load them) (define (make-tree datum children)   (list datum children)) (define datum car) (define children cadr) (define (leaf? tree) (null? (children tree))) (define tree1 (make-tree 'A (list (make-tree 'B (list (make-tree 'D (list (make-tree 'H '()))) (make-tree 'E  '()))) (make-tree 'C (list (make-tree 'F '()) (make-tree 'G (list (make-tree 'I (list (make-tree 'K '()) (make- tree 'L '()))) (make-tree 'J (list (make-tree 'M '()))))))))))
Background image of page 1

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

View Full DocumentRight Arrow Icon
Please attempt these on paper before you try typing them into Stk. Solution 1: (clean way) (define (depth-of-datum mytree ele)   (cond ((equal? (datum mytree) ele) 0) ((leaf? mytree) #f) (else (let ((tmp (accumulate (lambda (x y) (or x y)) (map (lambda (y) (depth-of-datum y  ele)) (children mytree)))))
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 08/07/2010 for the course COMPUTER S 61A taught by Professor Harvey during the Spring '06 term at Berkeley.

Page1 / 7

week6ReviewSol - DarrenKuo cs61atd OH:W56pm(277Soda...

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