notes06s - 1 CS61ANotes05FakePlasticTrees[Solutionsv1.0]...

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

View Full Document Right Arrow Icon
CS61A Notes 05 – Fake Plastic Trees [Solutions v1.0] Fake Plastic Trees QUESTIONS 1. Write  (square-tree tree) , which returns the same tree structure, but with every element  squared.  Don’t use “ map ”! (define (square-tree tree) (make-tree (square (datum tree)) (square-forest (children tree)))) (define (square-forest forest) (if (null? forest) ‘() (cons (square-tree (car forest)) (square-forest (cdr forest))))) 2. Write  (max-of-tree tree)  that does the obvious thing. The tree has at least one element. (define (max-of-tree tree) (if (null? (children tree)) (datum tree) (max (datum tree) (max-of-forest (children tree))))) (define (max-of-forest forest) (if (null? (cdr forest)) (max-of-tree (car forest)) (max (max-of-tree (car forest)) (max-of-forest (cdr forest))))) 3. Write  (listify-tree tree)  that turns the tree into a list in any order.  (This one you can’t use  map even if you tried. .. Muwahahaha) (define (listify-tree tree) (cons (datum tree) (listify-forest (children tree)))) (define (listify-forest forest) (if (null? forest) ‘() (append (listify-tree (car forest)) (listify-forest (cdr forest))))) 4. A maximum heap is a tree whose children’s data are all less-than-or-equal-to the root’s datum.  Of  course, its children are all maximum heaps as well.  Write  (valid-max-heap? tree)  that checks  if this is true for a given tree. (define (valid-max-heap? tree)
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

notes06s - 1 CS61ANotes05FakePlasticTrees[Solutionsv1.0]...

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

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