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

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

Module 8: Binary trees

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

Module 9: Binary trees

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

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

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

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

Module 10: General trees

CS115 - Module 10 - General 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

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

CS115 - Module 8 - Binary trees

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

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

Problem 1: Binary Trees: Flatten

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.

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

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

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

CS115 INTRODUCTION TO COMPUTER SCIENCE 1. Additional Notes Module 5

CSC/MAT-220: Lab 6. Due: 11/26/2018

Graphs. Directed graphs. Readings: Section 28

Working with recursion

CS3 Midterm 2 Summer 2008

Chapter 20: Binary Trees

CMSC 330, Fall 2013, Practice Problems 3

Functional Programming. Pure Functional Programming

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

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

CS115 - Module 4 - Compound data: structures

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

CS 171: Introduction to Computer Science II. Binary Search Trees

CS F-11 B-Trees 1

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

Local defini1ons. Func1on mul1ples- of

ormap, andmap, and filter

Data Structures and Algorithms

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

CS 1101 Exam 3 A-Term 2013

Module 8: Local and functional abstraction

CMSC 330, Fall 2013, Practice Problem 3 Solutions

From Templates to Folds

Balanced Trees. Nate Foster Spring 2018

Recursion & Iteration

CS 115 Lecture Notes Winter 2019

CS1 Recitation. Week 2

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

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

Functional abstraction

In addition to the correct answer, you MUST show all your work in order to receive full credit.

CSC148-Section:L0301

Balanced Trees. Prof. Clarkson Fall Today s music: Get the Balance Right by Depeche Mode

How to Design Programs

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

User-defined Functions. Conditional Expressions in Scheme

CS 206 Introduction to Computer Science II

Programming Languages and Techniques (CIS120)

Functional Programming Mid-term exam Tuesday 3/10/2017

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

We have the pointers reference the next node in an inorder traversal; called threads

Typed Racket: Racket with Static Types

CS 5010 Program Design Paradigms Lesson 6.1

CIS 120 Midterm I February 16, 2015 SOLUTIONS

foldr CS 5010 Program Design Paradigms Lesson 5.4

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

Algorithms and Data Structures

CSCI Trees. Mark Redekopp David Kempe

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 206 Introduction to Computer Science II

Rewriting your function using map and foldr

CSCC24 Functional Programming Scheme Part 2

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

Linked Data Structures. Linked lists. Readings: CP:AMA The primary goal of this section is to be able to use linked lists and trees.

Search Trees. Data and File Structures Laboratory. DFS Lab (ISI) Search Trees 1 / 17

I2206 Data Structures TS 6: Binary Trees - Binary Search Trees

The Typed Racket Guide

CSE341 Spring 2017, Final Examination June 8, 2017

Trees. CS 5010 Program Design Paradigms Lesson 6.2

Points off Total off Net Score. CS 314 Final Exam Spring 2017

Fall, 2015 Prof. Jungkeun Park

Loops in Scheme, II. (early slides assume map/filter)

Q1 Q2 Q3 Q4 Q5 Q6 Total

Trees. CS 5010 Program Design Paradigms Bootcamp Lesson 5.1

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

LINKED LISTS AND MIDTERM REVIEW

Loops. When to Use Loops

Programming Languages and Techniques (CIS120)

NET/JRF-COMPUTER SCIENCE & APPLICATIONS. Time: 01 : 00 Hour Date : M.M. : 50

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

Programming Languages and Techniques (CIS120)

Programming II (CS300)

CSC324 Functional Programming Efficiency Issues, Parameter Lists

CS 314 Principles of Programming Languages

Repetition Through Recursion

Scheme: Strings Scheme: I/O

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

Module 3: New types of data

Transcription:

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

Goals of this tutorial You should be able to... understand the difference between structural and accumulative recursion. write functions using accumulative recursion. understand the difference between binary trees and binary search trees. perform structural recursion on binary trees and binary search trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 2

Review: Structural vs. Accumulative In (pure) structural recursion, all arguments to the recursive call(s) are either unchanged, or one step closer to a base case. In accumulative recursion, arguments are the same as above, plus one or more accumulators, or arguments containing partial answers. The accumulatively recursive function is a helper function, and a wrapper function sets the initial value of the accumulator(s). If a parameter is used to produce the answer in the base case, then that parameter is probably an accumulator. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 3

CQ: Consider the following function: (define (mystery m n lst) (cond [(empty? lst) (list 1412)] [(<= m 72) (list 7)] [(>= n 48) (list 3)] [else (cons ( m n (first lst)) (mystery (sub1 m) (add1 n) (rest lst)))])) The type of recursion of this function is: A pure structural recursion B accumulative recursion C generative recursion CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 4

Group Problem: separate-sums-within? Write a function separate-sums-within? that consumes two numbers: split-point and threshold, and a list of numbers lon. The function produces true if the difference between (the sum of all the numbers in lon less than split-point) and (the sum of all the numbers in lon greater than split-point) is within the tolerance of threshold, and false otherwise. Your solution must use at most one helper function and accumulative recursion. (separate-sums-within? 9 2 (list 10 7 9 8 2 6)) true (separate-sums-within? 5 3 (list 2 4 6 8)) false CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 5

Group Problem: remove-leaf/bt (define-struct node (key val left right)) ;; A Node is a (make-node Num Str BT BT) ;; A Binary Tree (BT) is one of: ;; empty ;; Node Write a function remove-leaf/bt that consumes a number and a binary tree. The consumed number must only be a key for leaf nodes it cannot be a key for any internal nodes. The function removes all leaf nodes in the tree with the same key as the consumed number. If none of the leaf nodes in the consumed tree have the same key as the consumed number, the same tree is produced. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 6

Review: BST Data Definition Recall the data definition of a Binary Search Tree (BST): (define-struct node (key val left right)) ;; A Node is a (make-node Num Str BST BST) ;; requires: key > every key in left BST ;; key < every key in right BST ;; A Binary Search Tree (BST) is one of: ;; empty ;; Node CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 7

Clicker Question: Create BST Which BST would be created from the list (11 29 15 13 9 10) by inserting all numbers in this list into an empty BST in reverse order (which means the last element in this list would be first inserted)? A A B B C C D None of the above CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 8

Clicker Question: BST Insertion Where would you add 4 to the above BST? A A B B C C D Any of the above CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 9

Group Problem: remove-leaf/bst (define-struct node (key val left right)) ;; A Node is a (make-node Num Str BST BST) ;; requires: key > every key in left BST ;; key < every key in right BST ;; A Binary Search Tree (BST) is one of: ;; empty ;; Node Write a function remove-leaf/bst that consumes a number and a binary search tree. The function will do the same thing as the remove-leaf/bt function, with the same restrictions on the leaf nodes. Do you need to traverse the entire tree again? CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 10

Group Problem: path-to-key Write a function path-to-key that consumes a number and a binary search tree, and produces a list of the keys along the path from the root node to the node with the same key as the consumed number. If none of the nodes in the consumed tree have the same key as the consumed number, the function should produce false. You may find it helpful to use the search-bst function on slide 29 of module 8. (define sample-bt (make-node 4 "a" (make-node 2 "b" empty empty) (make-node 9 "c" (make-node 6 "d" empty empty) empty))) (path-to-key 6 sample-bt) (list 4 9 6) (path-to-key 8 sample-bt) false CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 11

Taken from module 8, slide 29: ;; (search-bst n t) produces the value associated with n, ;; or false if n is not in t ;; search-bst: Num BST (anyof Str false) (define (search-bst n t) (cond [(empty? t) false] [(= n (node-key t)) (node-val t)] [(< n (node-key t)) (search-bst n (node-left t))] [(> n (node-key t)) (search-bst n (node-right t))])) CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 12