Trees. CS 5010 Program Design Paradigms Bootcamp Lesson 5.1

Similar documents
Trees. CS 5010 Program Design Paradigms Lesson 6.2

From Templates to Folds

Doing it in Java. CS 5010 Program Design Paradigms Bootcamp Lesson 5.4

More About Recursive Data Types

CS 5010 Program Design Paradigms Lesson 6.1

ormap, andmap, and filter

Lists of Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 5.3

Halting Measures and Termination Arguments

Observer Templates. CS 5010 Program Design Paradigms Lesson 1.4

Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 4.1

Lists of Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 6.5

Contracts, Purpose Statements, Examples and Tests

Rewriting your function using map and foldr

Case Study: Undefined Variables

Case Study: Free Variables

The Data Design Recipe

Solving Your Problem by Generalization

Binary Search. CS 5010 Program Design Paradigms Bootcamp Lesson 8.2

Basics of Inheritance

Using Inheritance to Share Implementations

Data Abstraction. An Abstraction for Inductive Data Types. Philip W. L. Fong.

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

HIERARCHICAL DATA What is a Tree? How is it different from a Deep List? When would you use one over the other?

CS115 - Module 8 - Binary trees

Module 9: Trees. If you have not already, make sure you. Read How to Design Programs Sections 14, 15, CS 115 Module 9: Trees

foldr CS 5010 Program Design Paradigms Lesson 5.4

Working with images and scenes

Model-View-Controller Architecture

Module 5: Lists. Readings: HtDP, Sections 9, 10.

Module 10: General trees

Module 9: Binary trees

Racket: Macros. Advanced Functional Programming. Jean-Noël Monette. November 2013

Graphs. Directed graphs. Readings: Section 28

Module 10: Imperative Programming, Modularization, and The Future

CS115 INTRODUCTION TO COMPUTER SCIENCE 1. Additional Notes Module 5

Graphs. Readings: Section 28. CS 135 Fall : Graphs 1

Problem 1: Binary Trees: Flatten

Streams and Evalutation Strategies

Generic BST Interface

CS115 - Module 10 - General Trees

Functional Programming - 2. Higher Order Functions

Functional programming Primer I

Module 8: Binary trees

Algorithms and Data Structures

CSC 1052 Algorithms & Data Structures II: Linked Lists Revisited

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

(add1 3) 4 (check-expect (add1 3) 4)

Big Java Late Objects

Proving Properties of Recursive Functions and Data Structures. CS 270 Math Foundations of CS Jeremy Johnson

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local. CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 1

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

Huffman Coding Assignment For CS211, Bellevue College (rev. 2016)

CS 135 Fall 2018 Final Exam Review. CS 135 Fall 2018 Final Exam Review 1

APCS Semester #1 Final Exam Practice Problems

Splay Trees. Programming II - Elixir Version. Johan Montelius. Spring Term 2018

CITS3211 FUNCTIONAL PROGRAMMING. 6. Folding operators

Comp Intermediate Programming EXAM #2 April 03, 2002 Rice University - Instructors: Cox & Nguyen

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Inheritance - Assignment5

CS115 - Module 4 - Compound data: structures

Curriculum Map Grade(s): Subject: AP Computer Science

Note first midterm date: Wed. evening, Feb. 23 Today's class: Types in OCaml and abstract syntax

An Introduction to Trees

AP Computer Science A (Java) Scope and Sequence

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

Introduction to Linked Lists. Introduction to Recursion Search Algorithms CS 311 Data Structures and Algorithms

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Message-Passing Programming with MPI. Message-Passing Concepts

CS302 - Data Structures using C++

CMSC 150 LECTURE 7 RECURSION

20. Inheritance and Polymorphism

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

CS 1101 Exam 3 A-Term 2013

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

CSE 504: Compiler Design. Runtime Environments

AP Programming - Chapter 17 Lecture page 1 of 5

CS 4349 Lecture September 13th, 2017

Topic 7: Algebraic Data Types

The purpose of this lesson is to familiarize you with the basics of Racket (a dialect of Scheme). You will learn about

Recursion. Comp Sci 1575 Data Structures. Introduction. Simple examples. The call stack. Types of recursion. Recursive programming

STREAMS, ITERATORS, AND BINARY TREES 10

Expression Values Operators. (string-append a b c ) a, b, c string-append. (substring abcd 0 2) abcd, 0, 2 substring

Chapter 20: Binary Trees

This page intentionally left blank

Prelim One Solution. CS211 Fall Name. NetID

Huffman Coding. (EE 575: Source Coding Project) Project Report. Submitted By: Raza Umar. ID: g

CS 432 Fall Mike Lam, Professor. Code Generation

Trees. Binary arithmetic expressions. Visualizing binary arithmetic expressions. ((2 6) + (5 2))/(5 3) can be defined in terms of two smaller

This course supports the assessment for Scripting and Programming Applications. The course covers 4 competencies and represents 4 competency units.

CSCC24 Functional Programming Scheme Part 2

Chapter 5: Algorithms

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

Typed Scheme: Scheme with Static Types

Multiple and Virtual Inheritance in C++

Institut für Informatik D Augsburg

Laboratory: Recursion Basics

STUDENT LESSON A9 Recursion

Best-First Search! Minimizing Space or Time!! IDA*! Save space, take more time!!

Transcription:

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. 1

Module 05 Basic Principles Tools and Techniques Object-Oriented Programming Designing Data Computing with Lists Interfaces and Classes Designing Functions Computing with Trees and Graphs Inheritance Designing Systems Computing with Higher-Order Functions Objects with Mutable State Designing with Invariants Efficiency, Part 2 Thinking about Efficiency 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. 3

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. 4

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 5

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... 6

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

Observer Template tree-fn : BinTree ->??? (define (tree-fn t) (cond [(leaf? t) (... (leaf-datum t))] [else (... (tree-fn (node-lson t)) (tree-fn (node-rson t)))])) 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. 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. 8

Remember: The Shape of the Program Follows the Shape of the Data BinTree is-component-of tree-fn calls leaf data 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.) 9

The template questions tree-fn : Tree ->??? (define (tree-fn t) (cond [(leaf? t) (... (leaf-datum t))] [else (... (tree-fn (node-lson t)) What s the answer for a leaf? (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. 10

Let s see how the template questions help us define some functions that observe binary trees. leaf-sum leaf-sum : Tree -> Number (define (leaf-sum t) (cond [(leaf? t) (leaf-datum t)] [else (+ (leaf-sum (node-lson t)) What s the answer for a leaf? (leaf-sum (node-rson t)))])) If you knew the answers for the 2 sons, how could you find the answer for the whole tree? 11

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

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

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 14

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 15