Multi-way Trees CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.6 © Mitchell Wand, 2012-2014 This work is licensed under a Creative Commons Attribution-NonCommercial.

Slides:



Advertisements
Similar presentations
Templates CS 5010 Program Design Paradigms “Bootcamp” Lesson
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.
The Function Design Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Midterm Review CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Linear Search CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Classes, Objects, and Methods CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.1 © Mitchell Wand, This work is licensed under a Creative.
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 ©
Case Study: Free Variables CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Ormap, andmap, and filter CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.5 TexPoint fonts used in EMF. Read the TexPoint manual.
Function Composition CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
Structural Decomposition CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.1 © Mitchell Wand, This work is licensed under a Creative Commons.
Introducing General Recursion CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.2 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Trees CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.2 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
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.
Foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA © Mitchell.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
The Data Design Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
The Design Recipe using Classes CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.5 © Mitchell Wand, This work is licensed under a Creative.
Generalizing Similar Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Design Strategies 2: Using a template CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.1 © Mitchell Wand, This work is licensed under a Creative.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
Case Study: Free Variables CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Invariants and Performance CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
More examples of invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative.
Generalizing Over Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Lists of Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Destructor Templates CS 5010 Program Design Paradigms “Bootcamp” Lesson
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
Lists vs. Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
The DD  OO Recipe CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.4 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Organization of This Course CS 5010 Program Design Paradigms “Bootcamp” Lesson 0.2 © Mitchell Wand, This work is licensed under a Creative Commons.
Linear Search CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Introducing General Recursion CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you.
Halting Measures and Termination Arguments CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
The Last Lecture CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Using the List Template CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Using the List Template CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.2 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Sometimes Structural Recursion Isn't Enough CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.1 TexPoint fonts used in EMF. Read the TexPoint manual.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under.
Classes, Objects, and Interfaces CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative.
How to use an Observer Template
CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.2
Generalizing Similar Functions
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.2
More About Recursive Data Types
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.3
CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.3
More Recursive Data Types
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.2
Halting Functions for Tree-Like Structures
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3
CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.1
From Templates to Folds
CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.5
Solving Your Problem by Generalization
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.5
More examples of invariants
ormap, andmap, and filter
Generalizing Similar Functions
Rewriting your function using map and foldr
Examining Two Pieces of Data
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.3
Presentation transcript:

Multi-way Trees CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.6 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Creative Commons Attribution-NonCommercial 4.0 International License 1

Introduction We've talked about binary trees Sometimes, we need to construct trees in which each node has an unbounded number of sons. We call these multi-way trees. – example: a file system, in which a directory can have any number of files or directories in it. – example: S-expressions, in which a LoSS may contain any number of strings or SoS's. – an XML item. – in this lesson, we'll do a case study of one application of multi-way trees. 2

Learning Objectives At the end of this lesson, the student should be able to: – recognize situations in which a structure may have a component that is a list of similar structures – write a data definition for such values – write a template for such a structure – write functions on such structures 3

Ancestor Trees (define-struct person (name father mother)) ;; A Person is either ;; --"Adam" ;; --"Eve" ;; --(make-person String Person Person) ;; person-fn : Person -> ??? (define (person-fn p) (cond [(adam? p)...] [(eve? p)...] [else (... (person-name p) (person-fn (person-father p)) (person-fn (person-mother p)))])) Here are ancestor trees, an application of binary trees, which we saw before. For this representation, we needed to introduce "adam" and "eve" as artificial "first people". 4

A Different Info Analysis: Descendant Trees (define-struct person (name children)) ;; A Person is a ;; (make-person String Persons) ;; A Persons is one of ;; -- empty ;; -- (cons Person Persons) Two mutually recursive data definitions Here is a different information analysis: instead of keeping track of each person's parents, let's keep track of each person's children. A person may have any number of children, including no children. So we can represent each person's children as a list of persons. So now we have a pair of mutually- recursive data definitions. 5

This is mutual recursion Person Persons defined in terms of 6

The template recipe QuestionAnswer Does the data definition distinguish among different subclasses of data? Your template needs as many cond clauses as subclasses that the data definition distinguishes.cond How do the subclasses differ from each other? Use the differences to formulate a condition per clause. Do any of the clauses deal with structured values? If so, add appropriate selector expressions to the clause. Does the data definition use self- references? Formulate ``natural recursions'' for the template to represent the self-references of the data definition. Do any of the fields contain compound or mixed data? If the value of a field is a foo, add a call to a foo-fn to use it. Here is the recipe for templates again. Let's apply it to our Person trees. 7

Template: functions come in pairs ;; person-fn : Person -> ?? (define (person-fn p) (... (person-name p) (persons-fn (person-children p)))) ;; persons-fn : Persons -> ?? (define (persons-fn ps) (cond [(empty? ps)...] [else (... (person-fn (first ps)) (persons-fn (rest ps)))])) Here is the pair of templates that we get by applying the recipe to our data definition. They are mutually recursive, as you might expect. 8

The template questions ;; person-fn : Person -> ?? (define (person-fn p) (... (person-name p) (persons-fn (person-children p)))) ;; persons-fn : Persons -> ?? (define (persons-fn ps) (cond [(empty? ps)...] [else (... (person-fn (first ps)) (persons-fn (rest ps)))])) And here are the template questions, as usual. Given the answer for a person’s children, how do we find the answer for the person? What’s the answer for the empty Persons? Given the answer for the first person in the list and the answer for the rest of the people in the list, how do we find the answer for the whole list? 9

Examples (define alice (make-person "alice" empty)) (define bob (make-person "bob" empty)) (define chuck (make-person "chuck" (list alice bob))) (define dave (make-person "dave" empty)) (define eddie (make-person "eddie" (list dave))) (define fred (make-person "fred" (list chuck eddie))) fred chuckeddie alicebobdave 10

Vocabulary A tree where each node contains a list of subtrees is called a multi-way tree, or a rose tree. Observe that the "base case" is a tree containing an empty list of subtrees. 11

Grandchildren ;; grandchildren : Person -> Persons ;; GIVEN: a Person ;; RETURNS: a list of the grandchildren of the given ;; person. ;; EXAMPLE: (grandchildren fred) = (list alice bob dave) ;; STRATEGY: Structural Decomposition on p : Person (define (grandchildren p) (... (person-children p))) A: We need a function which, given a list of persons, produces a list of all their children Here's a simple function we might want to write. Q: Given p’s children, how do we find p’s grandchildren? 12

persons-all-children ;; persons-all-children : Persons -> Persons ;; GIVEN: a list of persons ;; RETURNS: a list of all their children. ;; (persons-all-children (list fred eddie)) ;; = (list chuck eddie dave) (define (persons-all-children ps) (cond [(empty? ps) empty] [else (append (person-children (first ps)) (persons-all-children (rest ps)))])) This one was too easy! It didn't require mutual recursion. ;; persons-all-children : Persons -> Persons ;; GIVEN: a list of persons ;; RETURNS: a list of all their children. ;; (persons-all-children (list fred eddie)) ;; = (list chuck eddie dave) (define (persons-all-children ps) (cond [(empty? ps)... ] [else (... (person-children (first ps)) (persons-all-children (rest ps)))])) 13

Putting it together ;; grandchildren : Person -> Persons ;; STRATEGY: Structural decomposition on p : Person (define (grandchildren p) (persons-all-children (person-children p))) ;; persons-all-children : Persons -> Persons ;; STRATEGY: Structural decomposition on ps : Persons (define (persons-all-children ps) (cond [(empty? ps) empty] [else (append (person-children (first ps)) (persons-all-children (rest ps)))])) 14

We could use abstraction, too ;; grandchildren : Person -> Persons ;; STRATEGY: Structural decomposition on p : Person (define (grandchildren p) (persons-all-children (person-children p))) ;; persons-all-children : Persons -> Persons ;; STRATEGY: HOFC (define (persons-all-children ps) (foldr append empty (map person-children ps))) Of course, a Persons is a list, so we can use our list abstractions to define persons-all-children. This will often be the case. 15

descendants Given a person, find all his/her descendants. What’s a descendant? – a person’s children are his/her descendants. – any descendant of any of a person’s children is also that person’s descendant. Hey: this definition is recursive! Here's a slightly harder task. 16

Contracts and Purpose Statements ;; person-descendants : Person -> Persons ;; GIVEN: a Person ;; RETURNS: the list of his/her descendants ;; persons-descendants : Persons -> Persons ;; GIVEN: a Persons ;; RETURNS: the list of all their descendants Here are the contracts and purpose statements. The task description talked about "all the descendants of a person's children". A person's children are a list of persons, so that gives us a clue that we will need the function we've called persons-descendants here. 17

Examples (person-descendants fred) = (list chuck eddie alice bob dave) (persons-descendants (list chuck eddie)) = (list alice bob dave) fred chuckeddie alicebobdave 18

The template questions ;; person-fn : Person -> ?? (define (person-fn p) (... (person-name p) (persons-fn (person-children p)))) ;; persons-fn : Persons -> ?? (define (persons-fn ps) (cond [(empty? ps)...] [else (... (person-fn (first ps)) (persons-fn (rest ps)))])) And here are the template questions, as usual. Given the answer for a person’s children, how do we find the answer for the person? What’s the answer for the empty Persons? Given the answer for the first person in the list and the answer for the rest of the people in the list, how do we find the answer for the whole list? 19

;; Person -> Persons ;; STRATEGY: Struct. Decomp. on p : Person (define (person-descendants p) (... (person-children p) (persons-descendants (person-children p)))) ;; Persons -> Persons ;; STRATEGY: Struct. Decomp. on ps : Persons (define (persons-descendants ps) (cond [(empty? ps)... ] [else (... (person-descendants (first ps)) (persons-descendants (rest ps)))])) Function Definitions ;; Person -> Persons ;; STRATEGY: Struct. Decomp. on p : Person (define (person-descendants p) (append (person-children p) (persons-descendants (person-children p)))) ;; Persons -> Persons ;; STRATEGY: Struct. Decomp. on ps : Persons (define (persons-descendants ps) (cond [(empty? ps) empty] [else (append (person-descendants (first ps)) (persons-descendants (rest ps)))])) The answers come right from the definition! We fill in the blanks in the template with the answers to the template questions. 20

Or, with the abstractions ;; Person -> Persons ;; STRATEGY: Struct. Decomp. on p : Person (define (person-descendants p) (append (person-children p) (persons-descendants (person-children p)))) ;; Persons -> Persons ;; STRATEGY: HOFC (define (persons-descendants ps) (foldr append empty (map person-descendants ps))) As we did before, we could replace the structural decomposition on Persons with Higher-Order Function Composition. The functions are still mutually recursive. 21

Tests (check-equal? (person-descendants fred) (list chuck eddie alice bob dave)) (check-equal? (persons-descendants (list chuck eddie)) (list alice bob dave)) 22

Are these good tests? Could a program fail these tests but still be correct? If so, how? Answer: Yes! It could produce the list of descendants in a different order. 23

Better Tests (require "sets.rkt") ;; or whatever... (check set-equal? (person-descendants fred) (list chuck eddie alice bob dave)) (check set-equal? (person-descendants fred) (list chuck eddie alice dave bob)) (check set-equal? (persons-descendants (list chuck eddie)) (list alice bob dave)) There are two ways we could solve this problem: 1. We could have our purpose statement specify the order in which the descendants are to be listed. 2. We could use smarter tests that would accept the answer list in any order. Here we've adopted the second approach. Instead of check- equal?, we use check, which takes as its first argument a predicate to be used to compare the actual and expected answers. We'll have to require a library that provides set-equal?- - the file sets.rkt, which we worked with last week, will do nicely. We've put a working copy of sets.rkt in the Examples file for this week. Here are some tests for (descendants fred) that list the answer in two different orders. 24

Summary You should now be able to: – recognize situations in which a structure may have a component that is a list of similar structures – write a data definition for such values – write a template for such a structure – write functions on such structures 25

Next Steps If you have questions about this lesson, ask them on the Discussion Board Do Guided Practice 6.6Guided Practice 6.6 Do the problem set 26