Recursive Data 2 Mutually Recursive Data Definitions (HTDP sec 15.1)

Slides:



Advertisements
Similar presentations
Higher-Order Functions and Loops c. Kathi Fisler,
Advertisements

Lists vs. Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.1 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Natural Numbers or, "All you ever need is compounds, variants, and recursion".
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Multi-way Trees CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.6 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Developing Programs for Family Trees c. Kathi Fisler, 2001.
Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA 1 ©
Developing Programs for Family Trees c. Kathi Fisler, 2001.
Loops in Scheme, II c. Kathi Fisler, 2001 (early slides assume map/filter)
1 General Trees & Binary Trees CSC Trees Previous data structures (e.g. lists, stacks, queues) have a linear structure. Linear structures represent.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
Mutually-Recursive Data Definitions CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.4 © Mitchell Wand, This work is licensed under a Creative.
Lists of Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
From Templates to Folds CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
Subsets. Subsets are sort of like nested Russian dolls: the subset “fits inside” the set.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
Arbitrarily Long Data Structures: Lists and Recursion CMSC Introduction to Computer Programming October 4, 2002.
1 COP 3538 Data Structures with OOP Chapter 8 - Part 2 Binary Trees.
Scheme: Lists Cont’d Chapter 9-10 of HTDP Ms. Knudtzon September 26.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
CS CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 11 Sept 27th, 2001 Fall Semester.
6 Dec 2001Kestrel1 Teaching with Patterns Matthias Felleisen Daniel Jackson.
Data Structures: Binary Trees CMSC Introduction to Computer Programming October 28, 2002.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
TeachScheme, ReachJava Adelphi University Wednesday morning June 24, 2008.
1 Red-Black Trees By Mary Hudachek-Buswell Red Black Tree Properties Rotate Red Black Trees Insertion Red Black Trees.
LINEAR SYSTEMS of INEQUALITIES – Graphing Method When graphing a set of linear inequalities, the solution set will be a “shared” space where the two solutions.
Mutual Recursion: Web pages CMSC Introduction to Computer Programming November 25, 2002.
CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
TeachScheme, ReachJava Adelphi University Thursday morning July 15, 2010.
CS CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 10 Sept 25th, 2001 Fall Semester.
More examples of invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative.
TeachScheme, ReachJava Adelphi University Friday morning July 16, 2010.
NOTES 23 - Pedigrees. What is a pedigree? Pedigree – A picture of an individual’s family tree that shows which members of the family have a particular.
CS 367 – Introduction to Data Structures
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
Lists vs. Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
Cs1321 December 6, 2001 Review. What is computer science? What's an algorithm? Processes and programs Overview of some programming language concepts Functional.
H EAPS. T WO KINDS OF HEAPS : MAX AND MIN Max: Every child is smaller than its parent Meaning the max is the root of the tree 10 / \ 9 7 / \ 6 8 / \ 2.
Lecture on Set! And Local CS 2135 Copyright Kathi Fisler, 2002 This material requires Advanced Language Level.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
CS CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 13 October 4, 2001 Fall Semester.
1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each node is 0, 1, of -1. Basic Operations Construction,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
(c) University of Washington20-1 CSC 143 Java Trees.
Functional Programming Language 1 Scheme Language: part 3.
Data Abstraction: Sets
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.2
More About Recursive Data Types
Class 11: Two-argument recursion
Halting Functions for Tree-Like Structures
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3
From Templates to Folds
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
CMSC 202 Trees.
Lecture 36 Section 12.2 Mon, Apr 23, 2007
General Trees & Binary Trees
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.5
Let's Play "What's the Question"
CSC 143 Java Trees.
More examples of invariants
Developing Programs for Family Trees
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
Bringing it all Together: Family Trees
Presentation transcript:

Recursive Data 2 Mutually Recursive Data Definitions (HTDP sec 15.1)

Previously: Family Trees Specifically, ancestor family trees A family-tree-node is either - empty, or - (make-child fa mo da na ey) where na and ey are symbols and da is a number and fa and mo are family-tree-nodes A self-referential data type of our own invention

A function on ancestor family tree nodes ;; blue-eyed-ancestor? : ftn -> boolean (define (blue-eyed-ancestor? a-ftree) (cond [(empty? a-ftree) false] [(symbol=? (child-eyes a-ftree) 'blue) true] [else (or (blue-eyed-ancestor? (child-father a-ftree)) (blue-eyed-ancestor? (child-mother a-ftree))))])) The colored portions come directly from the template for ancestor family trees

A new data type: descendant family trees Like ancestor f.t.’s, with one key difference each node now knows about its children, instead of its parents Ancestor trees were easy to represent You can have at most two parents! Descendant trees will be harder How do you encapsulate potentially many children in a structure?

Lists inside structures Sure, why not? Let’s write the data definition for a node: (we’ll call it “parent” since each node may have potentially many children) ; a parent is (make-parent loc n d e) ; where n, e are symbols ; and d is a number ; and loc is a list of children Now we have a problem. What’s a “list of children”? ?

Lists inside structures (take 2) Let’s try again, starting with the data definition for a list of children: ; a list of children is either ; - empty, or ; - (cons p loc) ; where p is a parent ; and loc is a list of children We’re still stuck. Now we know what a list of children is, but “parent” is undefined. ?

Mutually Referential Data Definitions The point is, you need both parts of the data definition for it to be complete and legal ; a parent is (make-parent loc n d e) ; where n, e are symbols ; and d is a number ; and loc is a list of children ; ; a list of children is either ; - empty, or ; - (cons p loc) ; where p is a parent ; and loc is a list of children

Examples (define-struct parent (children name date eyes)) (define Violet (make-parent empty 'VioletParr 1990 'brown)) (define Dash (make-parent empty 'DashiellParr 1995 'blue)) (define JackJack (make-parent empty 'JackParr 2002 'blue)) (define Elastigirl (make-parent (list Violet Dash JackJack) 'HelenParr 1962 'brown)) (define MrIncredible (make-parent (list Violet Dash JackJack) 'BobParr 1958 'blue))

Templates for M.R.D.D. The template should match the data definition Because the d.d. has two parts, so must the template ; template for functions on descendant tree nodes ; dtn-func : parent -> ??? (define (dtn-func p) … (loc-func (parent-children p)) … (parent-name p) … (parent-date p) … (parent-eyes p) … ) ; template for functions on lists of children ; loc-func : list of children -> ??? (define (loc-func loc) (cond [(empty? loc) … ] [else … (dtn-func (first loc)) … (loc-func (rest loc)) … ])) (Does the second one look familiar? It should—it’s just the template for lists, with an extra recursive call.)

Example function: blue-eyed-descendant? Unlike blue-eyed-ancestor?, blue-eyed-descendant? must follow this two-part template. (once again, colored portions come from the template) ;; blue-eyed-descendant? : parent -> boolean ;; to determine whether a-parent any of the descendants (children, ;; grandchildren, and so on) have 'blue in the eyes field (define (blue-eyed-descendant? a-parent) (cond [(symbol=? (parent-eyes a-parent) 'blue) true] [else (blue-eyed-children? (parent-children a-parent))])) ;; blue-eyed-children? : list-of-children -> boolean ;; to determine whether any of the structures in aloc is blue-eyed ;; or has any blue-eyed descendant (define (blue-eyed-children? aloc) (cond [(empty? aloc) false] [(blue-eyed-descendant? (first aloc)) true] [else (blue-eyed-children? (rest aloc))]))

blank