Quiz: Box and Pointer fun! (cons (cons (cons ‘hey (cons ‘there nil)) nil) (cons ‘wow nil)) (list ‘boo (append (list ‘hoo ‘hoo) (cons ‘see ‘me)))

Slides:



Advertisements
Similar presentations
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
Advertisements

מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
6.001 SICP SICP – October Trees Trevor Darrell 32-D512 Office Hour: W web page:
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Data Abstraction… The truth comes out…. What we’re doing today… Abstraction ADT: Dotted Pair ADT: List Box and Pointer List Recursion Deep List Recursion.
6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section.
6.001 SICP SICP – October Introduction Trevor Darrell 32-D512 Office Hour: W web page:
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
מבוא מורחב 1 Lecture #8. מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list )) ==> ( ) (scramble.
General Info Check out gilgarn.org. QUIZ DO YOU: – Love CSC 171 ? – Want a job? – Like to exert power over others? – Want to improve CSC UR?
Binary Trees. Node structure Data A data field and two pointers, left and right.
PPL Pairs, lists and data abstraction. Data Abstraction? An interface: separate implementation from usage Think of the Map interface in Java: we know.
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
CS 261 – Fall 2009 Binary Search Trees Again, but in detail.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
1 Lecture 16: Lists and vectors Binary search, Sorting.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
1 Append: process  (append list1 list2) (cons 1 (append ‘(2) list2)) (cons 1 (cons 2 (append ‘() list2))) (cons 1 (cons 2 list2)) (define (append list1.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
1 You’re Invited! Course VI Freshman Open House! Friday, April 7, :30-5:00 PM FREE Course VI T-Shirts (while supplies last) and Department.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Trees. What is a tree? You know…  tall  green  leafy  possibly fruit  branches  roots  I’m sure you’ve seen them.
Fall 2008Programming Development Techniques 1 Topic 8 Sequences as Conventional Interfaces Section October 2008.
Functional Programming: Lisp MacLennan Chapter 10.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Outline 1.The special form quote 2.Data abstraction: Trie 3.Alternative list: Triplets 4.Accumulate-n.
CS61A Lecture Colleen Lewis. Clicker poll Where do you think you’re learning the most? I assume you’ll learn the most in lab & disc (so.
CS 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
Spring 16 CSCI 4430, A Milanova/BG Ryder 1 Announcements HW5 due March 28 Homework Server link is up I will have office hours, Fri or Mon, check Announcements.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
1 Recursive Data Structures CS 270 Math Foundations of CS Jeremy Johnson.
Data Abstraction: Sets
Additional Scheme examples
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.2
CS 550 Programming Languages Jeremy Johnson
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
PPL Lazy Lists.
Halting Functions for Tree-Like Structures
Week 6 - Wednesday CS221.
From Templates to Folds
Trees Another Abstract Data Type (ADT)
CS 270 Math Foundations of CS Jeremy Johnson
Binomial Priority Queues
6.001 SICP Data abstractions
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Trees Another Abstract Data Type (ADT)
PPL Sequence Interface.
Lecture #8 מבוא מורחב.
Trees Another Abstract Data Type (ADT)
Lecture #9 מבוא מורחב.
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.5
topics mutable data structures
6.001 SICP Data abstractions
Announcements Quiz 5 HW6 due October 23
Lecture #7 מבוא מורחב.
List and list operations (continue).
Functional Programming: Lisp
Lecture # , , , , מבוא מורחב.
CS 403: Programming Languages
Binary Search Trees CS 580U Fall 17.
Presentation transcript:

Quiz: Box and Pointer fun! (cons (cons (cons ‘hey (cons ‘there nil)) nil) (cons ‘wow nil)) (list ‘boo (append (list ‘hoo ‘hoo) (cons ‘see ‘me)))

What we’re doing today… Flat vs. Deep List Recursion Trees…what they are…and WHY I don’t like them… Tree Recursion Intro to DDP

Flat vs. Deep Recursion… …ooh…that reaches all levels…

Flat vs. Deep Recursion Think about counting the elements in a list: ((a) ((b) c) d) a b d c

Flat vs. Deep Recursion How many elements are there in flat recursion? a b d c

Flat vs. Deep Recursion How many elements in deep recursion? a b d c

Flat vs. Deep Recursion So think about flat recursion as the top level of the list….so just go through the backbone of a box and pointer diagram. Deep recursion goes through EVERY level of the list.

Flat vs. Deep Recursion Last Discussion…  (define (deep-square L) (cond ((null? L) nil) ((list? (car L)) (cons (deep-square (car L)) (deep-square (cdr L)))) (else (cons (square (car L)) (deep-square (cdr L)))))) You thought that was easy?...let’s shorten it even more!

Flat vs. Deep Recursion Using pair? or list?  (define (deep-square L) (cond ((null? L) ‘()) ((not (pair? L)) (square L)) (else (cons (deep-square (car L)) (deep-square (cdr L)))))) Wasn’t that easier?

Flat vs. Deep Recursion Templates!  Flat Recursion (define (flat L) (if (null? L) ))

Flat vs. Deep Recursion  Deep Recursion (define (deep L) (cond ((null? L) ) ((not (pair? L)) ) (else )))

Deep Recursion Practice Write deep-accumulate.  (deep-accumulate + 0 ‘(1 (2 3) 4))  10 It should work like the 3 argument accumulate but on deep lists. No HOFs

Deep Recursion Practice Answers (define (deep-accumulate op init L) (cond ((null? L) init) ((not (pair? L)) L) (else (op (deep-accumulate op init (car L)) (deep-accumulate op init (cdr L)))))

Deep Recursion using HOFs It’s AS easy as normal recursion. Let’s take a closer look at what MAP does:  (map f (list x y z))  ( (f x) (f y) (f z) ) What if x, y and z were lists?

Deep Recursion using HOFs Map DOESN’T care!  (map f (list '(x y z) '(a b c) '(d e f)))  ( (f '(x y z)) (f '(a b c)) (f '(d e f)) ) Map just applies the function to all the car's of a list. So the question is, how can we use map on deep lists?

Deep Recursion using HOFs Well, look at the structure of deep-square.  (define (deep-square L) (cond ((null? L) ‘()) ((not (pair? L)) (square L)) (else (cons (deep-square (car L)) (deep-square (cdr L)))))) Here is a new version using map: (define (deep-square-map L) ;;assume L is a list (map (lambda (sublist) (cond ((null? sublist) sublist) ((not (pair? sublist)) (square sublist)) (else (deep-square-map sublist)))) L))

Deep Recursion Practice w/ HOF Write deep-appearances  (deep-appearances ‘a ‘(a (b c ((a))) d))  2 First version without HOFs. Second version with HOFs.

Deep Recursion Answer (define (deep-appearances x struct) (cond ((null? struct) 0) ((not (pair? struct)) (if (equal? x struct) 1 0)) (else (+ (deep-appearances x (car struct)) (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) (if (not (pair? sublist)) (if (equal? x sublist) 1 0) (deep-appearances x sublist))) struct)))

Hierarchical Data… …trees…my nemesis…

Hierarchical Data Examples:  Animal Classification: Kingdom, Phylum…  Government: President, VP…etc.  CS Staff: Lecturer, TAs, Readers, Lab Assitants  Family Trees

Trees…*shudder* The reason as to why I don’t like them… But they’re cool and they’re a great way to represent the hierarchical data. Kurt GregAlexCarolen

Binary Tree Traversals How you visit each node in a tree Three ways:  Prefix: visit the node, left child, right child  Infix: visit left child, node, right child  Postfix: visit left child, right child, node

Binary Tree Traversals: Prefix * *

Binary Tree Traversals: Infix 1 – * 5 + *

Binary Tree Traversals: Postfix 1 2 – 4 5 * + + *

Trees? Those things outside? Trees are a data structure. They can be implemented in many ways.  Nodes have or don’t have data  Extra information can be held in each node or branch  We talked about this in lecture today

Trees…what do you need? To implement trees you need most of the following:  Constructor: make-tree  Selectors: datum, children  Operations: apply function on each of the datum, add/delete a child, count children, count all datum.

Tree Abstraction Constructor: (make-tree datum children)  returns a tree where the datum is an element and children is a list of trees Implementation:  (define (make-tree datum children) (cons datum children)) OR  (define make-tree cons)

Tree Abstraction Selectors: (datum tree)  returns the element in the node of the tree (children tree)  returns a list of trees (a forest) Implementation:  (define (datum tree) (car tree)) (define (children tree) (cdr tree)) OR  (define datum car) (define children cdr)

Tree Abstraction Procedures: (leaf? tree)  returns #t if the tree has no children, otherwise #f (map-tree funct tree)  Returns a tree where each datum is (funct datum) Implementation:  (define (leaf? tree) (null? (children tree)))  We’ll leave map-tree for an exercise.

Tree Abstraction Practice (define a-t ‘(4 (7 (8) (5 (2) (4))) (5 (7)) (3 (4 (9)))) )  Draw a-t Root: Leaves: Underline Data  Use tree abstraction to construct a-t

Tree Recursion So how to write operations on trees…  So you can think of it like car/cdr recursion, but with using the tree abstraction.  You don’t need to check for the null? tree.  Otherwise, you basically do something to the datum and recurse through the children.

Tree Recursion How would you go about counting the leaves in a tree.  Steps for count-leaves: If the tree is a leaf return 1 Otherwise it has children, so go through the list of children by calling count-leaves on all of the children Add everything up.

Tree Recursion (define (count-leaves tree) (if (leaf? tree) 1 (count-leaves-in-forest (children tree)))) (define (count-leaves-in-forest list-of-trees) (if (null? forest) 0 (+ (count-leaves (car list-of-trees)) (count-leaves-in-forest (cdr list-of-trees))))) This is what we call mutual recursion! The two functions depend on each other

Tree Recursion Wait…count-list-in-forest kinda looks like… (define (accumulate op init lst) (if (null? lst) init (op (car lst) (accumulate op init (cdr lst))))) And we’re calling count-leaves with each child…it’s like MAPPING! Why not use HOFs instead of creating a new procedure!

Tree Recursion w/ HOFs (define (count-leaves tree) (cond ((null? tree) 0) ((leaf? tree) 1) (else (accumulate + 0 (map count-leaves (children tree))))) Doesn’t that look better

Tree Recursion Practice Write tree-search  Takes an element and a tree  Returns #t if the element is found, otherwise #f  Use no Helper Procedures

Tree Recursion Answers (define (tree-search data tree) (if (equal? (datum tree) data) #t (accumulate (lambda (x y) (or x y)) #f (map (lambda (child) (tree-search data child)) (children tree))))

Tree Operation Practice Write map-tree (We did this in class  )  Takes a function and a tree  Returns a new tree where the function is applied to each of the datum Write update-nodes  Returns you a new tree where all the nodes are the sum of it’s children

Tree Operation Answers (define (update-nodes tree) (if (leaf? tree) tree (let ((new-children (map update-nodes (children tree)))) (make-tree (accumulate + 0 (map datum new-children)) new-children))))

Next Time: DDP & Midterm Review… …get your mind ready!