Problem 1: Binary Trees: Flatten

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

Module 8: Binary trees

Module 9: Binary trees

CS 314 Principles of Programming Languages

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

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

Typed Racket: Racket with Static Types

Functional Programming. Pure Functional Programming

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

User-defined Functions. Conditional Expressions in Scheme

Procedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt

Treaps. 1 Binary Search Trees (BSTs) CSE341T/CSE549T 11/05/2014. Lecture 19

regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

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

Comp 311: Sample Midterm Examination

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

From Templates to Folds

Functional Programming - 2. Higher Order Functions

CS 314 Principles of Programming Languages. Lecture 16

CSc 520. Principles of Programming Languages 7: Scheme List Processing

Repetition Through Recursion

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

Lecture Notes on Binary Search Trees

CSc 520 Principles of Programming Languages. Examining Lists. Constructing Lists... 7: Scheme List Processing

Lisp. Versions of LISP

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Midterm Examination (Sample Solutions), Cmput 325

The Typed Racket Guide

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

Case Study: Free Variables

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

CSE341 Autumn 2017, Final Examination December 12, 2017

Project 2: Scheme Interpreter

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

Ecient nondestructive equality checking for trees and graphs

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

Data Structures and Algorithms

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

CIS 120 Midterm I February 16, 2015 SOLUTIONS

Data Structures CS 315 Spring 2017

Simplifying Logical Formulas

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

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

Binary Trees, Binary Search Trees

Module 10: General trees

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Trees. A tree is a directed graph with the property

CSCI Trees. Mark Redekopp David Kempe

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Review of the Lectures 21-26, 30-32

CS115 - Module 10 - General Trees

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

Sample Final Exam Questions

CS F-11 B-Trees 1

CS61A Midterm 2 Review (v1.1)

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

CS 320 Homework One Due 2/5 11:59pm

61A LECTURE 22 TAIL CALLS, ITERATORS. Steven Tang and Eric Tzeng July 30, 2013

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

Chapter 5. Binary Trees

Typed Scheme: Scheme with Static Types

CS450 - Structure of Higher Level Languages

Binary Trees

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

Spring 2018 Mentoring 8: March 14, Binary Trees

Quicksort. Alternative Strategies for Dividing Lists. Fundamentals of Computer Science I (CS F)

Binary Trees and Binary Search Trees

Data Structures and Algorithms

6.001 Notes: Section 31.1

Binary Search Trees. See Section 11.1 of the text.

CS115 - Module 8 - Binary trees

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

CSc 520 Principles of Programming Languages

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

Introduction to Functional Programming

Lisp Basic Example Test Questions

Lecture #24: Programming Languages and Programs

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

Department of Electrical Engineering and Computer Sciences Fall 2000 Instructor: Dan Garcia CS 3 Final Exam

Homework: More Abstraction, Trees, and Lists

Discussion 4. Data Abstraction and Sequences

Fall 2017 December 4, Scheme. Instructions

Lecture Notes on Binary Search Trees

CSE341 Spring 2017, Final Examination June 8, 2017

Macros & Streams Spring 2018 Discussion 9: April 11, Macros

Trees. (Trees) Data Structures and Programming Spring / 28

Programming Languages and Techniques (CIS120)

Functional Programming. Pure Functional Languages

Programming II (CS300)

Discussion 12 The MCE (solutions)

Lecture 5. Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Trees. CS 5010 Program Design Paradigms Bootcamp Lesson 5.1

Lecture 15 Binary Search Trees

Augmenting Data Structures. General approach Dynamic order statistics Interval trees

Case Study: Undefined Variables

A Brief Introduction to Scheme (II)

Transcription:

Problem 1: Binary Trees: Flatten Recall the data definition for binary trees: A binary-tree is either: #f (make-bt val left right) where val is a number, and left and right are binary-trees. (define-struct bt (val left right)) Write a function flatten that returns a list of the values of all the nodes in the tree. Here are some examples (be sure to use these as tests when developing your function): (define n1 (make-bt 1 #f #f)) (define n2 (make-bt 2 #f #f)) (define n3 (make-bt 3 #f #f)) (define m12 (make-bt 12 n1 n2)) (define r123 (make-bt 123 m12 n3)) (flatten r123) should be (123 12 1 2 3) ;; Note: This is simply a sample output. Any order of the same ;; numbers would be acceptable. (flatten n1) should be (1) Adding Data Add new binary tree nodes and modify as few existing nodes as possible to produce a binary tree with four nodes at depth 2. Submit your revised set of tree definitions. Template Write the template that you will follow in constructing this function. Contract & Purpose Write the contract specifying the input/output types of flatten and the purpose statement specifying its behavior. Implementation Write the code to implement flatten. Confirm that it produces the desired behavior on both the original examples and your modified examples. 1

Problem 2: Following Directions In Homework 4, we explored the notion of paths in family trees, where the path was the sequence of mom or dad relations between the initial family tree node and an ancestor with a desired trait. Here we will generalize the notion of paths to describe paths through binary trees, rather than family trees in particular. We can view a path in this sense as specifying a route through given binary tree. We will define a function (follow-directions abt path) that returns the value of the node reached by following the input path in the input tree. For example, using the example binary trees defined above: (follow-directions r123 ()) should be 123. (follow-directions m12 (l r r)) should be #f. (follow-directions r123 (l r)) should be 2. Data Definition Here is the data definition for a path: a Path is either: () (cons l path) (cons r path) 2

Template The follow-directions function s arguments are both interesting from the point of view of the template. So, rather than just having two conditions from a binary tree or three conditions from the path, we have 6 conditions, one for each combination of family trees and paths: (define (bt-path-template a-bt a-path) (cond (null? a-path)) (null? path)) (eq? l (car path))) (eq? l (car path))) (eq? r (car path))) (eq? r (car path))) )) 3

Then, when we split out the pieces and do the natural recursions, we end up with many more pieces that we would have. Of course, you won t usually need all of them the best way is to look at each case and think about what pieces you will need. Here they are: (define (bt-path-template a-bt a-path) (cond (null? a-path)) (null? a-path)) (bt-number a-bt) (bt-path-template (bt-left a-bt) a-path) (bt-path-template (bt-right a-bt) a-path)] (eq? l (car a-path))) (bt-path-template a-bt (cdr a-path))] (eq? l (car a-path))) (bt-path-template (bt-left a-bt) a-path) (bt-path-template (bt-left a-bt) (cdr a-path)) (bt-path-template (bt-right a-bt) a-path) (bt-path-template (bt-right a-bt) (cdr a-path)) (bt-path-template a-bt (cdr a-path)) (bt-number a-bt)] (eq? r (car a-path))) (bt-path-template a-path (cdr a-path))] (eq? r (car a-path))) (bt-path-template (bt-left a-bt) a-path) (bt-path-template (bt-left a-bt) (cdr a-path)) (bt-path-template (bt-right a-bt) a-path) (bt-path-template (bt-right a-bt) (cdr a-path)) (bt-path-template a-bt (cdr a-path)) (bt-number a-bt)])) 4

Contract & Purpose Write the contract specifying the input/output types of follow-directions and the purpose statement specifying its behavior. Implementation Write the scheme code to implement follow-directions. 5

Problem 3: Sets: Binary Search Trees Recall the data definition for binary search trees: A binary-search-tree is either: #f (make-bt val left right) where val is a number, and left and right are binary-search-trees. INVARIANT: for each node, n, in a tree, (bt-val n) is bigger than all numbers in (bt-left n) and smaller than all numbers in (bt-right n). (define-struct bt (val left right)) Example Set Write Scheme code that yields a valid binary search tree for the set {8,4,2,6,10,14,12}. Template Create a template for functions on binary search trees. Union Write a function called union that computes the union of two sets, represented as binary search trees. The union of two sets is a set that contains all of the elements in both sets. Here is a function header for union: ;; union : set-of-numbers set-of-numbers set-of-numbers ;; builds a set of the numbers contained in both s1 and s2 (define (union s1 s2)...) Invariants Explain briefly (2-3 lines) how your function enforces the invariant on binary search trees. 6

Problem 4: Binary Search Trees: Ordered Output Write a function inorder that produces a list of the values in a binary search tree ordered from smallest to largest. Contract and Purpose Write the contract and purpose statement for the function inorder. Implementation Write the function (inorder bst). Demonstrate its output the sample trees you constructed for Problem 3. Analysis Explain BRIEFLY how the structure of the binary search tree facilitates the creation of an ordered output list of elements. 7