Module 10: General trees

Similar documents
Trees. Readings: HtDP, sections 14, 15, 16.

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

CS115 - Module 10 - General Trees

Trees. Example: Binary expression trees. Example: Evolution trees. Readings: HtDP, sections 14, 15, 16.

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

Module 9: Binary trees

Module 8: Binary trees

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

CS115 - Module 8 - Binary trees

Working with recursion

Types of recursion. Readings: none. In this module: a glimpse of non-structural recursion. CS 135 Winter : Types of recursion 1

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

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

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

Module 8: Local and functional abstraction

Use recursion to write a function that duplicates the following function: (def (f L) (map (lambda (x) (+ (sqr x) x)) L))

Types of recursion. Structural vs. general recursion. Pure structural recursion. Readings: none. In this module: learn to use accumulative recursion

Lists. Readings: HtDP, sections 9 and 10. Avoid 10.3 (uses draw.ss). CS 135 Winter : Lists 1

CS2500 Exam 2 Fall 2011

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

Local definitions and lexical scope

Local definitions and lexical scope. Local definitions. Motivating local definitions. s(s a)(s b)(s c), where s = (a + b + c)/2.

Graphs. Directed graphs. Readings: Section 28

Assignment: 7. Due: Language level: Allowed recursion:

Module 10: Imperative Programming, Modularization, and The Future

Module 3: New types of data

How to Design Programs

Generative and accumulative recursion. What is generative recursion? Example revisited: GCD. Readings: Sections 25, 26, 27, 30, 31

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

CS 115 Lecture Notes Winter 2019

Chapter 20: Binary Trees

CS115 - Module 9 - filter, map, and friends

CS 275 Name Final Exam Solutions December 16, 2016

Typed Racket: Racket with Static Types

(Provisional) Lecture 08: List recursion and recursive diagrams 10:00 AM, Sep 22, 2017

CSE 341 Section Handout #6 Cheat Sheet

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

CS 314 Principles of Programming Languages. Lecture 16

CS115 - Module 3 - Booleans, Conditionals, and Symbols

The design recipe. Readings: HtDP, sections 1-5. (ordering of topics is different in lectures, different examples will be used)

CSU211 Exam 2 Fall 2007

CS115 - Module 4 - Compound data: structures

CS 5010 Program Design Paradigms Lesson 6.1

The Typed Racket Guide

CSE341 Spring 2017, Final Examination June 8, 2017

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Functional Programming - 2. Higher Order Functions

Local defini1ons. Func1on mul1ples- of

Scheme Basics > (butfirst '(help!)) ()

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))

Welcome to CS 115 (Winter 2019)

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

More About Recursive Data Types

CSE341 Autumn 2017, Final Examination December 12, 2017

A brief tour of history

Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 4.1

Problem 1: Binary Trees: Flatten

Welcome to CS 115 (Winter 2018)

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

Introduction to Scheme

CSE341 Spring 2017, Final Examination June 8, 2017

Scheme: Strings Scheme: I/O

User-defined Functions. Conditional Expressions in Scheme

Style and Submission Guide

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

A Brief Introduction to Scheme (I)

Module 4: Compound data: structures

Module 4: Compound data: structures

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

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

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Intro. Scheme Basics. scm> 5 5. scm>

CSCC24 Functional Programming Scheme Part 2

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

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

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

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

The syntax and semantics of Beginning Student

The syntax and semantics of Beginning Student

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

Trees. CS 5010 Program Design Paradigms Bootcamp Lesson 5.1

CS115 INTRODUCTION TO COMPUTER SCIENCE 1. Additional Notes Module 5

Computer Science II Fall 2009

1.3. Conditional expressions To express case distinctions like

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

From Templates to Folds

Comp 311: Sample Midterm Examination

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

Scheme Quick Reference

Wellesley College CS251 Programming Languages Spring, 2000 FINAL EXAM REVIEW PROBLEM SOLUTIONS

LECTURE 16. Functional Programming


Functional Programming. Pure Functional Programming

Discussion 4. Data Abstraction and Sequences

COP4020 Programming Assignment 1 - Spring 2011

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

Transcription:

Module 10: General trees Readings: HtDP, Sections 15 and 16 CS 115 Winter 2019 10: General trees 1

General trees Binary trees can be used for a large variety of application areas. One limitation is the restriction on the number of children. How might we represent a node that can have up to three children? What if there can be any number of children? CS 115 Winter 2019 10: General trees 2

General arithmetic expressions For binary arithmetic expressions, we formed binary trees. Racket expressions using the functions + and can have an unbounded number of arguments. For simplicity, we will restrict the operations to + and. (+ ( 4 2) 3 (+ 5 1 2) 2) CS 115 Winter 2019 10: General trees 3

We can visualize an arithmetic expression as a general tree. + * 3 + 2 4 2 5 1 2 (+ ( 4 2) 3 (+ 5 1 2) 2) CS 115 Winter 2019 10: General trees 4

For a binary arithmetic expression, we defined a structure with three fields: the operation, the first argument, and the second argument. For a general arithmetic expression, we define a structure with two fields: the operation and a list of arguments (which is a list of arithmetic expressions). We also need the data definition of a list of arithmetic expressions. CS 115 Winter 2019 10: General trees 5

(define-struct ainode (op args)) ;; An Arithmetic expression Internal Node (AINode) is a ;; (make-ainode (anyof * +) (listof AExp)) ;; An Arithmetic Expression (AExp) is one of: ;; * a Num ;; * an AINode Each definition depends on the other, and each template will depend on the other. This is called mutual recursion. CS 115 Winter 2019 10: General trees 6

Examples of arithmetic expressions: 3 (make-ainode + (list 3 4)) (make-ainode (list 3 4)) (make-ainode + (list (make-ainode (list 4 2)) 3 (make-ainode + (list 5 1 2)) 2)) It is also possible to have an operation and an empty list; recall substitution rules for and and or. CS 115 Winter 2019 10: General trees 7

Templates for arithmetic expressions ;; aexp-template: AExp Any (define (aexp-template ex) (cond [(number? ex)... ex... ] [else (... (ainode-op ex)... (listof-aexp-template (ainode-args ex))... )])) ;; listof-aexp-template: (listof AExp) Any (define (listof-aexp-template exlist) (cond [(empty? exlist)... ] [else (... (aexp-template (first exlist))... (listof-aexp-template (rest exlist))... )])) CS 115 Winter 2019 10: General trees 8

The function eval ;; eval: AExp Num (define (eval ex) (cond [(number? ex) ex] [else (my-apply (ainode-op ex) (ainode-args ex))])) CS 115 Winter 2019 10: General trees 9

;; my-apply: (anyof * +) (listof AExp) Num (define (my-apply f exlist) (cond [(empty? exlist) (cond [(symbol=? f ) 1] [(symbol=? f +) 0])] [else (cond [(symbol=? f ) ( (eval (first exlist)) (my-apply f (rest exlist)))] [(symbol=? f +) (+ (eval (first exlist)) (my-apply f (rest exlist)))])])) CS 115 Winter 2019 10: General trees 10

An alternate implementation of eval ;; eval: AExp Num (define (my-apply f exlist) (cond [(number? ex) ex] [(symbol=? (ainode-op ex) +) (foldr + 0 (map eval (ainode-args ex)))] [else (foldr 1 (map eval (ainode-args ex)))])) CS 115 Winter 2019 10: General trees 11

Functions can produce AExp values as well Write a function remainder-n that consumes an AExp (in which all numbers are non-negative integers) and a positive integer n, and produces a new AExp in which all numbers have been replaced with their remainder when divided by n. ;; remainder-n: AExp Nat AExp ;; requires: n > 0 (define (remainder-n ex n)... ) There are different ways to approach solving this problem. CS 115 Winter 2019 10: General trees 12

Some uses of general trees The contents of organized text and web pages can be stored as a general list. (list chapter (list section (list paragraph "This is the first sentence." "This is the second sentence.") (list paragraph "We can continue in this manner.")) (list section... )... ) CS 115 Winter 2019 10: General trees 13

(list webpage (list title "CS 115: Introduction to Computer Science 1") (list paragraph "For a course description," (list link "click here." "desc.html") "Enjoy the course!") (list horizontal-line) (list paragraph "(Last modified yesterday.)")) CS 115 Winter 2019 10: General trees 14

Computer file structures can be viewed as general trees. The general tree template and an example will be covered in lab. CS 115 Winter 2019 10: General trees 15

Nested lists So far, we have discussed flat lists (no nesting): (list 1 a "hello" x) and lists of lists (one level of nesting): (list (list 1 "a") (list 2 "b")) We now consider nested lists (arbitrary nesting): (list (list 1 (list 2 3)) 4 (list 5 (list 6 7 8) 9)) CS 115 Winter 2019 10: General trees 16

Nested lists as leaf-labelled trees It is often helpful to visualize a nested list as a leaf-labelled tree, in which the leaves correspond to the elements of the list, and the internal nodes indicate the nesting. 4 1 5 9 2 3 6 7 8 (list (list 1 (list 2 3)) 4 (list 5 (list 6 7 8) 9)) CS 115 Winter 2019 10: General trees 17

Examples of leaf-labelled trees: empty (list 4 2) (list (list 4 2) 3 (list 4 1 6)) (list (list 3) 2 (list 5) (list 4 (list 3 6))) Each non-empty tree is a list of subtrees. The first subtree in the list is either a single leaf (not a list) or a subtree rooted at an internal node (a list). CS 115 Winter 2019 10: General trees 18

Data definition for leaf-labelled trees ;; An Atom is an (anyof Num Str Sym) ;; A leaf-labelled tree (LLT) is one of the following ;; * empty ;; * (cons Atom LLT) ;; * (cons LLT LLT) where first LLT is nonempty. The labels could be any non-list Racket value, but we will just limit it to the types listed in Atom. CS 115 Winter 2019 10: General trees 19

Template for leaf-labelled trees The template follows from the data definition. ;; llt-template: LLT Any (define (llt-template l) (cond [(empty? l)... ] [(cons? (first l)) (... (llt-template (first l))... (llt-template (rest l))... )] [else (... (first l)... (llt-template (rest l))... )])) CS 115 Winter 2019 10: General trees 20

The function count-leaves Write a function that produces the number of Atom leaves in an LLT. ;; (count-leaves l) produces the number of leaves in l. ;; count-leaves: LLT Nat (define (count-leaves l)... ) CS 115 Winter 2019 10: General trees 21

An implementation of count-leaves (define (count-leaves l) (cond [(empty? l) 0] [(cons? (first l)) (+ (count-leaves (first l)) (count-leaves (rest l)))] [else (+ 1 (count-leaves (rest l)))])) CS 115 Winter 2019 10: General trees 22

Condensed trace of count-leaves (count-leaves (list (list a b) c)) (+ (count-leaves (list a b)) (count-leaves (list c))) (+ (+ 1 (count-leaves (list b))) (count-leaves (list c))) (+ (+ 1 (+ 1 (count-leaves empty))) (count-leaves (list c))) (+ (+ 1 (+ 1 0)) (count-leaves (list c))) (+ (+ 1 1) (count-leaves (list c))) (+ 2 (count-leaves (list c))) (+ 2 (+ 1 (count-leaves empty))) (+ 2 (+ 1 0)) (+ 2 1) 3 CS 115 Winter 2019 10: General trees 23

Flattening a nested list flatten produces a flat list from a nested list. ;; flatten: LLT (listof Num) (define (flatten l)... ) We make use of the built-in Racket function, append, which we examined in Module 7. (append (list 1 2) (list 3 4)) (list 1 2 3 4) CS 115 Winter 2019 10: General trees 24

;; (flatten l) produces a flat list from l. ;; flatten: LLT (listof Num) (define (flatten l) (cond [(empty? l) empty] [(cons? (first l)) (append (flatten (first l)) (flatten (rest l)))] [else (cons (first l) (flatten (rest l)))])) CS 115 Winter 2019 10: General trees 25

Condensed trace of flatten (flatten (list (list a b) c)) (append (flatten (list a b)) (flatten (list c))) (append (cons a (flatten (list b))) (flatten (list c))) (append (cons a (cons b (flatten empty))) (flatten (list c))) (append (cons a (cons b empty)) (flatten (list c))) (append (cons a (cons b empty)) (cons c (flatten empty))) (append (cons a (cons b empty)) (cons c empty)) (cons a (cons b (cons c empty))) CS 115 Winter 2019 10: General trees 26

Using flatten The following is a simplified version of our computer file tree as a LLT: (define my-computer (list (list (list (list "a1q1" "a1q2") (list "a2q1")) (list "l01" "l02" "l03")) (list "m1" "m2" "m3"))) (flatten my-computer) (list "a1q1" "a1q2" "a2q1" "l01" "l02" "l03" "m1" "m2" "m3") CS 115 Winter 2019 10: General trees 27

Summary: Structuring data using mutual recursion Mutual recursion arises when two or more data definitions reference each other. Structures and lists may be used as part of the definitions. In general, Create templates from the data definitions and Create one function for each template. In some cases, the mutually recursive relationships do not actually require separate functions. For example, with BST and Node, a separate function to process a Node is not typically needed. CS 115 Winter 2019 10: General trees 28

Goals of this module You should understand the idea of mutual recursion for both examples given in lecture and new ones that might be introduced in lab, assignments, or exams. You should be able to develop templates from mutually recursive data definitions, and to write functions using the templates. CS 115 Winter 2019 10: General trees 29

Summing up CS 115 With only a few language constructs we have described and implemented ideas from introductory computer science in a brief and natural manner. We have done so without many of the features (static types, mutation, I/O) that courses using conventional languages have to introduce on the first day. The ideas we have covered carry over into languages in more widespread use. CS 115 Winter 2019 10: General trees 30

We hope you have been convinced that the goal of computer science is to implement useful computation in a way that is correct and efficient as far as the machine is concerned, but that is understandable and extendable as far as other humans are concerned. These themes will continue in CS 116, but new themes will be added, and a new programming language using a different paradigm will be studied. CS 115 Winter 2019 10: General trees 31

Looking ahead to CS 116 and beyond We have been fortunate to work with very small languages (the teaching languages) writing very small programs which operate on small amounts of data. In CS 116, we will broaden our scope, moving towards the messy but also rewarding realm of the real world. CS 115 Winter 2019 10: General trees 32

Other CS courses address questions such as: How do we organize a program that is bigger than a few screenfuls, or large amounts of data? How do we reuse and share code, apart from cutting-and-pasting it into a new program file? How do we design programs so that they run efficiently? These are issues which arise not just for computer scientists, but for anyone making use of computation in a working environment. We can build on what we have learned this term in order to meet these challenges with confidence. CS 115 Winter 2019 10: General trees 33

Options for continuing CS studies: CS minor or major: Take CS 116 and then CS 136 (with 90+ in CS 115, you can go directly to CS 136). Talk to a CS advisor about the process. Many students have been successful in CS after starting in CS 115. Computing Option: Take CS 116. Talk to an advisor to understand your choices. Note: Participate in course selection! CS 115 Winter 2019 10: General trees 34