Quiz: Box and Pointer fun! What we’re doing today… (cons (cons (cons ‘hey Flat vs. Deep List Recursion (cons ‘there nil)) Trees…what they are…and WHY I don’t nil) like them… (cons ‘wow nil)) Tree Recursion Intro to DDP (list ‘boo (append (list ‘hoo ‘hoo) (cons ‘see ‘me))) Flat vs. Deep Recursion Think about counting the elements in a list: ((a) ((b) c) d) Flat vs. Deep Recursion… d …ooh…that reaches all levels… a c b 1 Flat vs. Deep Recursion Flat vs. Deep Recursion How many elements are there in flat recursion? How many elements in deep recursion? d d a c a c b b Flat vs. Deep Recursion Flat vs. Deep Recursion So think about flat recursion as the top Last Discussion… level of the list….so just go through the £ (define (deep-square L) (cond ((null? L) nil) backbone of a box and pointer diagram. ((list? (car L)) (cons (deep-square (car L)) (deep-square (cdr L)))) (else (cons (square (car L)) Deep recursion goes through EVERY level (deep-square (cdr L)))))) of the list. ¢ You thought that was easy?...let’s shorten it even more! 2 Flat vs. Deep Recursion Flat vs. Deep Recursion Using pair? or list? ¢ Templates! £(define (deep-square L) £Flat Recursion (cond ((null? L) ‘()) (define (flat L) ((not (pair? L)) (square L)) (if (null? L) (else (cons (deep-square (car L)) <return value at the end of the list> (deep-square (cdr L)))))) <combine first & recurse on ‘cdr’ list>)) ¢ Wasn’t that easier? Flat vs. Deep Recursion Deep Recursion Practice £Deep Recursion ¢ Write deep-accumulate. (define (deep L) £(deep-accumulate + 0 ‘(1 (2 3) 4)) (cond ((null? L) <return value when end>) ‰ 10 ((not (pair? L)) <do something to It should work like the 3 argument accumulate element>) but on deep lists. No HOFs (else <combine recursive call to ‘car’ list & recursive call to ‘cdr’ list>))) 3 Deep Recursion Practice Answers Deep Recursion using HOFs (define (deep-accumulate op init L) It’s AS easy as normal recursion. (cond ((null? L) init) ((not (pair? L)) L) Let’s take a closer look at what MAP does: (else £(map f (list x y z)) (op (deep-accumulate op init (car L)) ‰( (f x) (f y) (f z) ) (deep-accumulate op init (cdr L))))) ¢ What if x, y and z were lists? Deep Recursion using HOFs Deep Recursion using HOFs ¢ Map DOESN’T care! ¢ Well, look at the structure of deep-square. £ (define (deep-square L) £ (map f (list '(x y z) '(a b c) '(d e f))) (cond ((null? L) ‘()) ((not (pair? L)) (square L)) ‰ ( (f '(x y z)) (f '(a b c)) (f '(d e f)) ) (else (cons (deep-square (car L)) (deep-square (cdr L)))))) ¢ Map just applies the function to all the car's of a ¢ Here is a new version using map: (define (deep-square-map L) ;;assume L is a list list. (map (lambda (sublist) (cond ((null? sublist) sublist) ((not (pair? sublist)) (square sublist)) ¢ So the question is, how can we use map on (else (deep-square-map sublist)))) deep lists? L)) 4 Deep Recursion Practice w/ HOF Deep Recursion Answer Write deep-appearances ¢ (define (deep-appearances x struct) (cond ((null? struct) 0) £ (deep-appearances ‘a ‘(a (b c ((a))) d)) ((not (pair? struct)) ‰ 2 (if (equal? x struct) 1 0)) First version without HOFs. (else (+ (deep-appearances x (car struct)) Second version with HOFs. (deep-appearances x (cdr struct)))))) ¢ Which condition isn’t needed in this case? Deep Recursion w/ HOF Answer ¢ (define (deep-appearances x struct) (accumulate + 0 (map (lambda (sublist) Hierarchical Data… (if (not (pair? sublist)) (if (equal? x sublist) 1 0) (deep-appearances x sublist))) struct))) …trees…my nemesis… 5 Hierarchical Data Trees…*shudder* Examples: ¢ The reason as to why I don’t like them… £Animal Classification: Kingdom, Phylum… Kurt £ Government: President, VP…etc. Greg Alex Carolen ¢ J £CS Staff: Lecturer, TAs, Readers, Lab But they’re cool Assitants and they’re a great way to represent the £Family Trees hierarchical data. Binary Tree Traversals Binary Tree Traversals: Prefix + ¢ How you visit each node in a tree - * ¢ Three ways: £Prefix: visit the node, left child, right child 1 2 4 5 £Infix: visit left child, node, right child £Postfix: visit left child, right child, node + - 1 2 * 4 5 6 Binary Tree Traversals: Infix Binary Tree Traversals: Postfix + + - * - * 1 2 4 5 1 2 4 5 1 – 2 + 4 * 5 1 2 – 4 5 * + Trees? Those things outside? Trees…what do you need? Trees are a data structure. ¢ To implement trees you need most of the They can be implemented in many ways. following: £Nodes have or don’t have data £Constructor: make-tree £Extra information can be held in each node or £Selectors: datum, children branch £Operations: apply function on each of the £We talked about this in lecture today datum, add/delete a child, count children, count all datum. 7 Tree Abstraction Tree Abstraction Constructor: ¢ Selectors: ¢ Implementation: (make-tree datum children) (datum tree) £ (define (datum tree) £ returns a tree where the datum is an element and £ returns the element in (car tree)) children is a list of trees the node of the tree (define (children tree) ¢ Implementation: (children tree) (cdr tree)) £ (define (make-tree datum children) £ returns a list of trees OR (cons datum children)) (a forest) £ (define datum car) OR (define children cdr) £ (define make-tree cons) Tree Abstraction Tree Abstraction Practice ¢ Procedures: ¢ (define a-t (leaf? tree) £ returns #t if the tree has no children, otherwise #f ‘(4 (7 (8) (5 (2) (4))) (5 (7)) (3 (4 (9)))) ) (map-tree funct tree) £Draw a-t £ Returns a tree where each datum is (funct datum) ¢ Root: ¢ Implementation: ¢ Leaves: £ (define (leaf? tree) ¢ Underline Data (null? (children tree))) £Use tree abstraction to construct a-t £ We’ll leave map-tree for an exercise. 8 Tree Recursion Tree Recursion So how to write operations on trees… ¢ How would you go about counting the £So you can think of it like car/cdr recursion, leaves in a tree. < What are leaves?> but with using the tree abstraction. £Steps for count-leaves: £You don’t need to check for the null? tree. ¢ If the tree is a leaf return 1 £Otherwise, you basically do something to the ¢ Otherwise it has children, so go through the list of datum and recurse through the children. children by calling count-leaves on all of the children ¢ Add everything up. Tree Recursion Tree Recursion ¢ (define (count-leaves tree) ¢ Wait…count-list-in-forest kinda looks like… (if (leaf? tree) (define (accumulate op init lst) 1 (if (null? lst) (count-leaves-in-forest (children tree)))) init (define (count-leaves-in-forest list-of-trees) (op (car lst) (if (null? forest) (accumulate op init (cdr lst))))) 0 ¢ And we’re calling count-leaves with each child…it’s like (+ (count-leaves (car list-of-trees)) MAPPING! (count-leaves-in-forest (cdr list-of-trees))))) ¢ Why not use HOFs instead of creating a new procedure! This is what we call mutual recursion ! The two functions depend on each other 9 Tree Recursion w/ HOFs Tree Recursion Practice (define (count-leaves tree) ¢ Write tree-search (cond ((null? tree) 0) £Takes an element and a tree ((leaf? tree) 1) £Returns #t if the element is found, otherwise (else (accumulate + 0 #f (map count-leaves (children tree))))) £Use no Helper Procedures Doesn’t that look better J Tree Recursion Answers Tree Operation Practice ¢ (define (tree-search data tree) ¢ Write map-tree (We did this in class L) (if (equal? (datum tree) data) £Takes a function and a tree #t £Returns a new tree where the function is (accumulate (lambda (x y) (or x y)) applied to each of the datum #f (map (lambda (child) ¢ Write update-nodes (tree-search data child)) £Returns you a new tree where all the nodes (children tree)))) are the sum of it’s children 10 Tree Operation Answers (define (update-nodes tree) (if (leaf? tree) Next Time: DDP & tree (let ((new-children Midterm Review… (map update-nodes (children tree)))) (make-tree (accumulate + 0 (map datum new-children)) …get your mind ready! new-children)))) 11.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages11 Page
-
File Size-