cs61a lecture 11
DESCRIPTION
CS61A Lecture 11. 2011-07-07 Colleen Lewis. Clicker poll . What do you think of the pace of lecture? Too slow A little slow About right A little fast Too fast. Tree recursion. A procedure in which each invocation makes more than one recursive call. Is this tree-recursion?. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/1.jpg)
CS61A Lecture 11
2011-07-07Colleen Lewis
![Page 2: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/2.jpg)
Clicker poll What do you think of the pace of lecture?
A)Too slowB)A little slowC)About rightD)A little fastE)Too fast
![Page 3: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/3.jpg)
Tree recursion
• A procedure in which each invocation makes more than one recursive call
![Page 4: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/4.jpg)
Is this tree-recursion?(define (fib n)
(if (< n 2)
1
(+ (fib (- n 1))(fib (- n 2)))))
A) Yes B) No C)??
![Page 5: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/5.jpg)
Is this tree-recursion?(define (filter pred seq)
(cond
((null? Seq) ‘())
((pred (car seq))
(cons (car seq)
(filter pred (cdr seq))))
(else (filter pred (cdr seq)))))
A) Yes B) No C)??
![Page 6: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/6.jpg)
calc-eval(define (calc-eval exp)
(cond
((number? exp) exp)
((list? exp)
(calc-apply
(car exp)
(map calc-eval (cdr exp))))
(else (error “Calc: bad exp”))))
Is this tree-recursion?A) Yes B) No C)??
![Page 7: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/7.jpg)
Representing Math in Scheme
(+ (* 2 4) 5)
car: +
cdr: ((* 2 4) 5)
![Page 8: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/8.jpg)
Capital-T Tree
• Abstract Data Type• Not defined in the book• The book calls any deep-list a “tree”. – Lower-case-t tree (book) is different than Capital-T
Tree
![Page 9: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/9.jpg)
Representing Trees in Scheme
(World (N-America) (Antarctica))
car: World
cdr: ((N-America) (Antarctica))
Extra set of parens
![Page 10: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/10.jpg)
Try IT!
(World
(N-America(US)(Canada)(Mexico))
(Antarctica))
car: World
cdr: ((N-America(US)(Canada)(Mexico))
(Antarctica))
How many open parens?A) 3 B) 4 C) 5 D) 6 E) 7
![Page 11: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/11.jpg)
Representing Trees in Scheme
(World (N-America) (Antarctica))
datum: World
children:((N-America) (Antarctica))List of trees
(forest)
root node
leaf (no children)
![Page 12: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/12.jpg)
Tree constructors & selectors
(define (make-tree data children)
(cons data children))
(define (datum Tree)
(car Tree))
(define (children Tree)
(cdr Tree))
![Page 13: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/13.jpg)
children
• What type of thing is (children tree)? A)List of ListsB)List of TreesC)List of treesD)ListE)??
![Page 14: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/14.jpg)
CONSTRUCTING Trees in Scheme
datum: World
children:((N-America) (Antarctica))
(make-tree
‘World
(list
(make-tree ‘N-America ‘())
(make-tree ‘Antarctica ‘())))
![Page 15: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/15.jpg)
Try IT!
(make-tree
‘World
(list
(make-tree
‘N-America
(list (make-tree ‘US ‘())
(make-tree ‘Canada ‘())
(make-tree ‘Mexico ‘())))
(make-tree ‘Antarctica ‘())))
How many calls to make-tree?A) 3 B) 4 C) 5 D) 6 E) 7
![Page 16: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/16.jpg)
TreemapSTk>(define t1 (make-tree 3 ‘())
t1
STk>(datum t1)
3
STk>(children t1)
()
STk>(define t2 (treemap square t1))
t2
STk>(datum t2)
9
![Page 17: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/17.jpg)
treemapIf TREE is the tree to the right, what call to treemap will create the below?
A.(treemap
(λ(x) (length(datum x))
TREE)
B.(treemap length TREE)
C.(treemap count TREE)
D.(treemap
(λ(x) (count (datum x))
TREE)
![Page 18: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/18.jpg)
forest-map(define (forest-map fn forest)
•What type of thing is a forest? A)List of ListsB)List of TreesC)List of treesD)ListE)??Write it without calling map! It should call treemap on each tree
![Page 19: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/19.jpg)
forest-map(define (forest-map fn forest)
(if (null? forest)
‘()
(cons
(treemap fn (car forest))
(forest-map fn (cdr forest)))))
Is using cons, car & cdr a data abstraction violation (DAV)? A) Yes B) No C) It depends
![Page 20: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/20.jpg)
forest-map
(define (forest-map fn forest)
•What type of thing is a forest? A)List of ListsB)List of TreesC)List of treesD)ListE)??
![Page 21: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/21.jpg)
deep-map
REVIEW
![Page 22: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/22.jpg)
(deep-map sq ‘((3 . 4) (5 6))
car cdr car cdr
3
car cdr
4
car cdr
6
car cdr
5
car cdr
?
?
(cons (deep-map fn (car arg)) (deep-map fn (cdr arg)))
![Page 23: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/23.jpg)
deep-map solution
(define (deep-map fn arg)
(cond
((null? arg) '())
((pair? arg)
(cons
(deep-map fn (car arg))
(deep-map fn (cdr arg))))
(else (fn arg))))
![Page 24: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/24.jpg)
treemap
(define (treemap fn tree)
(make-tree
(fn (datum tree))
(forest-map fn (children tree))))
![Page 25: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/25.jpg)
treemap and forest-map(define (treemap fn tree)
(make-tree
(fn (datum tree))
(forest-map fn (children tree))))
(define (forest-map fn forest)
(if (null? forest)
‘()
(cons
(treemap fn (car forest))
(forest-map fn (cdr forest)))))
Mutual recursion
Why don’t we need a base case
in treemap?
![Page 26: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/26.jpg)
forest-map using map(define (forest-map fn forest)
(map
(lambda (one-tree)
(tree-map fn one-tree))
forest))
![Page 27: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/27.jpg)
treeadd
STk>(define kid1 (make-tree 3 ‘())
STk>(define kid2 (make-tree 4 ‘())
STk>(define parent
(make-tree ‘5
(list kid1 kid2))
STk>(treeadd parent)
12
![Page 28: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/28.jpg)
treeadd and forest-add
(define (treeadd tree)
(make-tree
(datum tree)
(forest-add (children tree))))
(define (forest-add forest)
(if (null? forest)
‘()
(cons
(treeadd (car forest))
(forest-add (cdr forest)))))
Modify forest-addto return the sum of all
trees in the forest
Modify treeaddto return the sum of
the trees
![Page 29: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/29.jpg)
deep-add
STk> (deep-add 3)
3
STk>(deep-add ‘())
0
STk>(deep-add ‘( 1 2 3))
6
STk>(deep-add ‘((1 2) (3 . 2) 1))
9
![Page 30: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/30.jpg)
Modify to become deep-add
(define (deep-add fn arg)
(cond
((null? arg) '())
((pair? arg)
(cons
(deep-add fn (car arg))
(deep-add fn (cdr arg))))
(else (fn arg))))
![Page 31: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/31.jpg)
Draw the tree
(define a (make-tree 2 '()))
(define b (make-tree 3 '()))
(define c (make-tree 5 (list a b)))
(define d (make-tree 1 (list c)))
![Page 32: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/32.jpg)
Solution treeadd & forest-add
(define (treeadd tree)
(+ (datum tree)
(forest-add (children tree))))
(define (forest-add forest)
(if (null? forest)
0
(+ (treeadd (car forest))
(forest-add (cdr forest)))))
![Page 33: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/33.jpg)
SOLUTION deep-add
(define (deep-add arg)
(cond
((null? arg) 0)
((pair? arg)
(+
(deep-add (car arg))
(deep-add (cdr arg))))
(else arg)))
![Page 34: CS61A Lecture 11](https://reader035.vdocuments.net/reader035/viewer/2022081802/56815a91550346895dc80675/html5/thumbnails/34.jpg)
SOLUTION Draw the tree
(define a (make-tree 2 '()))
(define b (make-tree 3 '()))
(define c (make-tree 5 (list a b)))
(define d (make-tree 1 (list c)))