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

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

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

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

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

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

CS 275 Name Final Exam Solutions December 16, 2016

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

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


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

Discussion 4. Data Abstraction and Sequences

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

Introduction to Functional Programming

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work?

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

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

CS450 - Structure of Higher Level Languages

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

CS61A Midterm 2 Review (v1.1)

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

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

Functional Programming - 2. Higher Order Functions

CS115 - Module 10 - General Trees

CS3L Summer 2011 Final Exam, Part 2 You may leave when finished; or, you must stop promptly at 12:20

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

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

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

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

CS3: Introduction to Symbolic Programming. Lecture 14: Lists.

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

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

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

How to Design Programs

Functional Programming. Pure Functional Languages

Repetition Through Recursion

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream?

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

CSc 520 Principles of Programming Languages

Assignment 1. Due Tuesday October 11 at 11pm. No late assignments will be accepted. What to do

This exam is worth 30 points, or about 28% of your total course grade. The exam contains

Lisp: Question 1. Dr. Zoran Duric () Midterm Review 1 1/ 13 September 23, / 13

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

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

Trees. CS 5010 Program Design Paradigms Bootcamp Lesson 5.1

Project 2: Scheme Interpreter

Sample Final Exam Questions

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

Functional Programming. Pure Functional Languages

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

CS 1101 Exam 3 A-Term 2013

Building a system for symbolic differentiation

Functional Programming. Pure Functional Programming

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

The Typed Racket Guide

Problem 1: Binary Trees: Flatten

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

April 2 to April 4, 2018

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

Typed Racket: Racket with Static Types

CSC324 Functional Programming Efficiency Issues, Parameter Lists

Structure and Interpretation of Computer Programs

From Templates to Folds

YOUR NAME PLEASE: *** SOLUTIONS ***

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Lisp Basic Example Test Questions

Module 9: 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

CS 61A Midterm #2 - October 5, 1998

Delayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses

Fall 2017 December 4, Scheme. Instructions

Trees. CS 5010 Program Design Paradigms Lesson 6.2

Streams and Evalutation Strategies

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

Recursion & Iteration

Structure and Interpretation of Computer Programs

(defun fill-nodes (nodes texts name) (mapcar #'fill-node (replicate-nodes nodes (length texts) name) texts))

More Scheme CS 331. Quiz. 4. What is the length of the list (()()()())? Which element does (car (cdr (x y z))) extract from the list?

CSE341 Autumn 2017, Final Examination December 12, 2017

CS 314 Principles of Programming Languages

Structure and Interpretation of Computer Programs

Functional programming with Common Lisp

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

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

CS 5010 Program Design Paradigms Lesson 6.1

Module 10: General trees

Module 8: Binary trees

Laboratory: Recursion Basics

University of Massachusetts Lowell

CSCC24 Functional Programming Scheme Part 2

A Brief Introduction to Scheme (II)

INTERPRETATION AND SCHEME LISTS 12

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

Quiz 1. Queues , Fall 2007 Recitation 10 Solutions 1

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

Documentation for LISP in BASIC

Scheme as implemented by Racket

SCHEME AND CALCULATOR 5b

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 3. LISP: ZÁKLADNÍ FUNKCE, POUŽÍVÁNÍ REKURZE,

Transcription:

HIERARCHICAL DATA 5 COMPUTER SCIENCE 61AS Concepts and Definitions 1. What is a Tree? How is it different from a Deep List? When would you use one over the other? A Tree is an Abstract Data Type composed of a datum (some data value) and children (a List of Trees). Thus each Tree may connect to zero or more other Trees. Trees are used to structure data in a hierarchical fashion (compared with Lists, which structure data in a sequential fashion.) A Deep List is a list that may have other lists inside and is also used as a hiarchical data structure. When to use a Tree over a Deep List depends on the application. For example, you can model a filesystem (files and folders) as a Tree the name of the file or folder would be the datum, and the children would be the list of Trees, each containing the name of another file or folder. Files have no children, while folders have a child for each file it holds. Deep lists are useful when you have a list of lists. For example, if you represented full names as lists, and you had a list of full names, i.e. ((john lennon) (paul mccartney) (george harrison) (ringo starr)). 2. What is mutual recursion? Mutual recursion is a way to do recursion on two different types of data that are nested within each other. Usually, we will use mutual recursion on trees. In a tree, we have two different types of things we have the tree itself (and other trees in it), and we have forests, which are lists of trees. 3. When working with trees, what is (typically) the input to each mutually recursive procedure? Normally, we ll have one function that deals only with trees and another function that deals only with forests. Since trees contain forests, the procedure that 1

DISCUSSION 5: HIERARCHICAL DATA Page 2 deals with trees neecs to call the one that deals with forests. Since forests contain trees, the procedure that deals with forests need to call the one that deals with trees. In other words, mutual recursion. 4. What is a binary tree? Which selectors do we use for it? A binary tree is the same as a normal tree except it has at most two children. We use left-branch and right-branch to select the two child trees. 5. What is car/cdr recursion? In car/cdr recursion, you treat the cars and cdrs symmetrically, just like you would treat left-branch and right-branch symmetrically for binary trees. This allows your procedure to work any structure created by cons, not just lists. Practice with Capital-T Trees 1. Assume forest is a forest (list of Trees) and tree is a Tree where each element is a number. For each of the following lines of code, say whether it is a data abstraction violation, a domain/range mismatch, or valid. a. (car forest) Valid b. (car tree) DAV c. (children tree) Valid d. (children forest) Domain/range mismatch e. (null? (children tree))

DISCUSSION 5: HIERARCHICAL DATA Page 3 Valid f. (cdr (children tree)) Valid 2. Write a procedure add-child-lengths which takes as input a tree, and produces as output a new tree in which each datum is now a list whose first element is the original datum, and whose second element is the number of children it has. (You can find the length of a list using the length procedure.) Use mutual recursion here. (define (add-child-lengths tree) (make-tree (list (datum tree) (length (children tree))) (forest-lengths (children tree)))) (define (forest-lengths forest) nil (cons (add-child-lengths (car forest)) (forest-lengths (cdr forest))))) 3. Write sum-tree, which takes as input a tree of numbers and produces as output the sum of all the numbers in the tree. You should use sequence operations (map, filter, accumulate) to implement it. No helper procedures allowed! (This is a hard question to start with, so you may want to first do it with mutual recursion, which is easier.) Using sequence operations: (define (sum-tree tree) (+ (datum tree) (accumulate + 0 (map sum-tree (children tree))))) Using mutual recursion: (define (sum-tree tree) (+ (datum tree)

DISCUSSION 5: HIERARCHICAL DATA Page 4 (sum-forest (children tree)))) (define (sum-forest forest) 0 (+ (sum-tree (car forest)) (sum-forest (cdr forest))))) 4. listify-tree takes as input a tree and produces as output a flat (not deep) list of all of the datums in the tree, in any order. a. Write listify-tree using higher order functions. (define (listify-tree tree) (cons (datum tree) (accumulate append nil (map listify-tree (children tree))))) b. Write listify-tree using mutual recursion. (define (listify-tree tree) (cons (datum tree) (listify-forest (children tree)))) (define (listify-forest forest) nil (append (listify-tree (car forest)) (listify-forest (cdr forest))))) 5. Write count-leaves, which returns the number of leaves in the input tree. A leaf is any tree which has no children. Using sequence operations: (define (count-leaves tree)

DISCUSSION 5: HIERARCHICAL DATA Page 5 (if (null? (children tree)) 1 (accumulate + 0 (map count-leaves (children-tree))))) Using mutual recursion: (define (count-leaves tree) (if (null? (children tree)) 1 (count-leaves-forest (children tree)))) (define (count-leaves-forest forest) 0 (+ (count-leaves (car forest)) (count-leaves-forest (cdr forest))))) Practice with HOFs and Deep Lists 1. Louis Reasoner writes a procedure, deep-squares, that takes in a deep list and squares every number in it. He writes the following code. (define (deep-squares lol) (cond ((null? lol) ()) ((list? (car lol)) (cons (map square (car lol)) (deep-squares (cdr lol)) )) (else (cons (square (car lol)) (deep-squares (cdr lol)) )))) a. Say whether the code above will error, work, or is a data abstraction violation. (deep-squares ()) (deep-squares (1 (2 3) (4 5)) (deep-squares (1 (2 (3) 4) (((5))))

DISCUSSION 5: HIERARCHICAL DATA Page 6 This code will only work for lists up to a depth of two. This is because, when the car of the list is a list, we simply map square over the elements, which means we assume the elements are simple numbers (not lists themselves). b. Now fix Louis s code. Hint: it can be fixed with an extremely small change. Do NOT use deep-map in your solution. Replace (map square (car lol)) with (deep-squares (car lol)) 2. Express the following function without using recursion. (Hint: use map). (define (jelly-words lst) (if (null? lst) () (cons (word jelly (car lst)) (jelly-words (cdr lst))))) > (jelly-words (fish bean donut car cdr) ) (jellyfish jellybean jellydonut jellycar jellycdr) (define (jelly-words lst) (map (lambda (x) (word jelly x)) lst)) 3. Express the following function without using recursion. (define (sqrt-positives nums) (cond ((null? nums) ()) ((<= (car nums) 0) (sqrt-positives (cdr nums))) (else (cons (sqrt (car nums)) (sqrt-positives (cdr nums)))))) > (sqrt-positive (1-9 5-4)) (1 25) (define (sqrt-positives nums)

DISCUSSION 5: HIERARCHICAL DATA Page 7 (map sqrt (filter (lambda (x) (> x 0)) nums))) Practice with Car/cdr Recursion 1. Write the procedure sum-binary-tree, which sums the datum of a binary tree. (define (sum-binary-tree bt) (if (equal? bt the-empty-tree) 0 (+ (datum bt) (sum-binary-tree (left-branch bt)) (sum-binary-tree (right-branch bt))))) 2. Now, write the procedure sum-cons-structure, which should work on any structure created by cons. Would (sum-cons-structure bt) evaluate to the same thing as (sum-binary-tree bt) (assuming that bt is a valid input to sum-binary-tree)? If yes, why don t we generally do this? If no, why not? (define (sum-cons-structure struct) (if (pair? struct) (+ (sum-cons-structure (car struct)) (sum-cons-structure (cdr struct))) struct)) Yes, we could use sum-cons-structure in place of sum-binary-tree, but only with our current representation of binary trees. If we changed the representation, then it would no longer work, while sum-binary-tree would still work. That s the point of data abstraction!