Trees CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.2 © 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.
Basics of Inheritance CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1 © Mitchell Wand, This work is licensed under a Creative Commons.
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.:
The Data Design Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
Classes, Objects, and Methods CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.1 © Mitchell Wand, This work is licensed under a Creative.
Multi-way Trees CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.6 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
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.
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.
Searching in a Graph CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
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.
Patterns of Communication Between Objects CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.1 © Mitchell Wand, This work is licensed under.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
Finishing the Data Definition Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson © 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.
Examining Two Pieces of Data CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative.
Classes, Objects, and Methods CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.1 © Mitchell Wand, This work is licensed under a Creative.
The Design Recipe using Classes CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.5 © Mitchell Wand, This work is licensed under a Creative.
Design Strategies 3: Divide into cases CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under.
The Point of This Course CS 5010 Program Design Paradigms “Bootcamp” Lesson 0.1 © Mitchell Wand, This work is licensed under a Creative Commons.
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.
Two Draggable Cats CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
More examples of invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative.
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.
Halting Measures and Termination Arguments CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
Non-Empty Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
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.
More linear search with invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before.
Midterm Review CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
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 5.2
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.4
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 8.7
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.2
Halting Functions for Tree-Like Structures
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.1
From Templates to Folds
The Different Kinds of Data
Solving Your Problem by Generalization
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.5
More examples of invariants
ormap, andmap, and filter
Rewriting your function using map and foldr
Examining Two Pieces of Data
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
Presentation transcript:

Trees CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.2 © 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/Outline We've now learned about two ways to represent sequence information. Many examples of information have a natural branching structure. These are represented as trees, which you should have learned about in your data structures course. In this lesson, we'll study how to apply the Design Recipe to trees. 2

Learning Objectives At the end of this lesson you should be able to: – Write a data definition for tree-structured information – Write a template for tree-structured information – Write functions that manipulate that data, using the template 3

Binary Trees (define-struct leaf (datum)) (define-struct node (lson rson)) ;; A Tree is either ;; -- (make-leaf Number) ;; -- (make-node Tree Tree) There are many ways to define binary trees. We choose this one because it is clear and simple. 4

Template tree-fn : Tree -> ??? (define (tree-fn t) (cond [(leaf? t) (... (leaf-datum t))] [else (... (tree-fn (node-lson t)) (tree-fn (node-rson t)))])) Self-reference in the data definition leads to self-reference in the template; Self-reference in the template leads to self-reference in the code. Here's the template for this data definition. Observe that we have two self-references in the template, corresponding to the two self-references in the data definition. 5

The template questions tree-fn : Tree -> ??? (define (tree-fn t) (cond [(leaf? t) (... (leaf-datum t))] [else (... (tree-fn (node-lson t)) (tree-fn (node-rson t) ))])) What’s the answer for a leaf? If you knew the answers for the 2 sons, how could you find the answer for the whole tree? And here are the template questions. When we write a function using structural decomposition, we fill in the template with the answers to these questions. 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. The template recipe doesn't need to change 7

leaf-sum leaf-sum : Tree -> Number (define (leaf-sum t) (cond [(leaf? t) (leaf-datum t)] [else (+ (leaf-sum (node-lson t)) (leaf-sum (node-rson t)))])) What’s the answer for a leaf? If you knew the answers for the 2 sons, how could you find the answer for the whole tree? Next we'll do some examples of functions on binary trees. 8

leaf-max leaf-max : Tree -> Number (define (leaf-max t) (cond [(leaf? t) (leaf-datum t)] [else (max (leaf-max (node-lson t)) (leaf-max (node-rson t)))])) What’s the answer for a leaf? If you knew the answers for the 2 sons, how could you find the answer for the whole tree? 9

leaf-min leaf-min : Tree -> Number (define (leaf-min t) (cond [(leaf? t) (leaf-datum t)] [else (min (leaf-min (node-lson t)) (leaf-min (node-rson t)))])) What’s the answer for a leaf? If you knew the answers for the 2 sons, how could you find the answer for the whole tree? 10

Summary You should now be able to: – Write a data definition for tree-structured information – Write a template for tree-structured information – Write functions that manipulate that data, using the template 11

Next Steps If you have questions about this lesson, ask them on the Discussion Board Do Guided Practice 6.2 Go on to the next lesson 12