CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.1

Slides:



Advertisements
Similar presentations
Lists vs. Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.1 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Advertisements

Basics of Inheritance CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1 © 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.
Structural Decomposition CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.1 © Mitchell Wand, This work is licensed under a Creative Commons.
Trees CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.2 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
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.
From Templates to Folds CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
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.
Invariants and Performance CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.6 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
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.
The Design Recipe using Classes CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.5 © Mitchell Wand, This work is licensed under a Creative.
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.
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.
Searching in a Graph CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
The Design Recipe using Classes CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative.
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.
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.
More linear search with invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before.
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
Contracts, Purpose Statements, Examples and Tests
CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.2
The Point of This Course
Generalizing Similar Functions
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.2
Examining Two Pieces of Data
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.4
More About Recursive Data Types
CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.1
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.3
CS 5010 Program Design Paradigms "Bootcamp" Lesson 9.4
Introduction to Invariants
More Recursive Data Types
Chapter 2: Data Abstraction 2
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 4.1
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3
CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.3
From Templates to Folds
CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.4
CS 5010 Program Design Paradigms "Bootcamp" Lesson 9.3
Case Study: Undefined Variables
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
The Iterative Design Recipe
CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1
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
CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.4
Design Strategies 3: Divide into cases
Presentation transcript:

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

Module 05 Basic Principles Tools and Techniques Designing Data Designing Functions Designing Systems Tools and Techniques Computing with Lists Computing with Trees and Graphs Computing with Higher-Order Functions Designing with Invariants Thinking about Efficiency Object-Oriented Programming Interfaces and Classes Inheritance Objects with Mutable State Efficiency, Part 2

Module Introduction In this module we will learn about a number of topics having to do with trees and their representation. We will learn about branching structures, such as trees mutually recursive data definitions S-expressions How to represent trees and related structures in Java What makes the observer template work in general.

Lesson Introduction Many examples of information have a natural structure which is not a sequence, but is rather a tree, 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.

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

Binary Trees: Data Definition ;; A Binary Tree is represented as a BinTree, which is either: ;; (make-leaf datum) ;; (make-node lson rson) ;; INTERPRETATON: ;; datum : Real some real data ;; lson, rson : BinTree the left and right sons of this node ;; IMPLEMENTATION: (define-struct leaf (datum)) (define-struct node (lson rson)) ;; CONSTRUCTOR TEMPLATES: ;; -- (make-leaf Number) ;; -- (make-node BinTree BinTree) There are many ways to define binary trees. We choose this one because it is clear and simple. Observer Template to follow...

This definition is self-referential (recursive) ;; A BinTree is either ;; -- (make-leaf Number) ;; -- (make-node BinTree BinTree)

Observer Template 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. tree-fn : BinTree -> ??? (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.

Remember: The Shape of the Program Follows the Shape of the Data is-component-of BinTree leaf data calls tree-fn leaf data function Data Hierarchy (a BinTree is either leaf data or has two components which are BinTrees Call Tree (tree-fn either calls a function on the leaf data, or it calls itself twice.)

The template questions What’s the answer for a leaf? tree-fn : Tree -> ??? (define (tree-fn t) (cond [(leaf? t) (... (leaf-datum t))] [else (... (tree-fn (node-lson t)) (tree-fn (node-rson t) ))])) 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 the template, we fill in the template with the answers to these questions.

What’s the answer for a leaf? Let’s see how the template questions help us define some functions that observe binary trees. leaf-sum What’s the answer for a leaf? 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)))])) 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.

What’s the answer for a leaf? leaf-max What’s the answer for a leaf? 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)))])) If you knew the answers for the 2 sons, how could you find the answer for the whole tree? We just fill in the answer to each question.

What’s the answer for a leaf? leaf-min What’s the answer for a leaf? 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)))])) If you knew the answers for the 2 sons, how could you find the answer for the whole tree? And leaf-min is similar.

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

Next Steps Study the file 05-1-trees.rkt in the Examples folder. If you have questions about this lesson, ask them on the Discussion Board Do Guided Practice 5.1 Go on to the next lesson