61A Lecture 21. Friday, March 13

Similar documents
61A LECTURE 15 MEMOIZATION, RECURSIVE DATA, SETS

61A LECTURE 16 TREES, ORDERS OF GROWTH. Steven Tang and Eric Tzeng July 22, 2013

61A LECTURE 17 ORDERS OF GROWTH, EXCEPTIONS. Steven Tang and Eric Tzeng July 23, 2013

TREES AND ORDERS OF GROWTH 8

TREES AND ORDERS OF GROWTH 7

LINKED LISTS AND MIDTERM REVIEW

Structure and Interpretation of Computer Programs Summer 2015 Midterm 2

61A Lecture 7. Monday, September 15

61A Lecture 3. Friday, September 5

MEMOIZATION, RECURSIVE DATA, AND SETS

Tree-Structured Data. A tree can contains other trees: [5, [6, 7], 8, [[9], 10]] (+ 5 (- 6 7) 8 (* (- 9) 10))

61A Lecture 7. Monday, September 16

Structure and Interpretation of Computer Programs

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

Structure and Interpretation of Computer Programs

CS61A Lecture 20 Object Oriented Programming: Implementation. Jom Magrotker UC Berkeley EECS July 23, 2012

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 21 Scheme TODAY REVIEW: DISPATCH DICTIONARIES DISPATCH DICTIONARIES 7/27/2012

61A Lecture 9. Friday, September 20

61A Lecture 6. Monday, February 2

Lecture #21: Search and Sets. Last modified: Wed Mar 9 15:44: CS61A: Lecture #21 1

Structure and Interpretation of Computer Programs

CSE 115. Introduction to Computer Science I

CS 4800: Algorithms & Data. Lecture 1 January 10, 2017

61A Lecture 4. Monday, September 9

Structure and Interpretation of Computer Programs

CS483 Design and Analysis of Algorithms

Conditionals & Control Flow

Basic Syntax - First Program 1

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

Structure and Interpretation of Computer Programs Spring 2019 Midterm 2

Midterm Review. CS61A Summer Katya Stukalova Jerome Baek

CS Introduction to Programming Fall 2016

CSI33 Data Structures

Structure and Interpretation of Computer Programs

CS3: Introduction to Symbolic Programming. Lecture 11: Tree Recursion, beginning lists, and Midterm 2. Spring 2007 Nate Titterton

UNIT 5B Binary Search

Lecture #21: Search Trees, Sets. Last modified: Tue Mar 18 18:15: CS61A: Lecture #21 1

Lecture 8. Conditionals & Control Flow

Problem Set 6 Due: 11:59 Sunday, April 29

Structure and Interpretation of Computer Programs

CMSC201 Computer Science I for Majors

Lecture 8: Conditionals & Control Flow (Sections ) CS 1110 Introduction to Computing Using Python

STREAMS, ITERATORS, AND BINARY TREES 10

CS 234 Python Review Part 2

Programming Languages and Techniques (CIS120)

CSC148-Section:L0301

CIS192 Python Programming. Robert Rand. August 27, 2015

Today. CISC101 Reminders & Notes. Searching in Python - Cont. Searching in Python. From last time

Strings in Python 1 Midterm#1 Exam Review CS 8: Introduction to Computer Science Lecture #6

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 24 Infinite Sequences LAZY EVALUATION AGENDA 8/7/2012. Jom Magrotker UC Berkeley EECS July 30, 2012

More on Lists, Dictionaries Introduction to File I/O CS 8: Introduction to Computer Science Lecture #10

Working with Lists 4

CS61A Lecture 24 Infinite Sequences. Jom Magrotker UC Berkeley EECS July 30, 2012

Sets and Maps. Set Commands Set ADT Set ADT implementation Map ADT Map ADT implementation

61A Lecture 25. Friday, October 28

Structure and Interpretation of Computer Programs

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Introduction to Python

boolean & if-then-else

Logistics. Half of the people will be in Wu & Chen The other half in DRLB A1 Will post past midterms tonight (expect similar types of questions)

COMPUTER SCIENCE IN THE NEWS (TWO YEARS AGO) CS61A Lecture 16 Mutable Data Structures TODAY REVIEW: OOP CLASS DESIGN 7/24/2012

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Fall 2016 Midterm 2

CS61A Lecture 9 Immutable Data Structures. Jom Magrotker UC Berkeley EECS July 2, 2012

CSC148-Section:L0301

06 - Building Lists Recursively. Spring 2018 COMP110

Last week. Last week. Last week. Today. Binary Trees. CSC148 Intro. to Computer Science. Lecture 8: Binary Trees, BST

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

PREPARING FOR PRELIM 1

Announcements. Homework 1 due Monday 10/12 (today) Homework 2 released next Monday 10/19 is due 11/2

CS61A Lecture 16 Mutable Data Structures. Jom Magrotker UC Berkeley EECS July 16, 2012

GreenThumb Garden Registration

UNIT 5C Merge Sort. Course Announcements

Randomized Algorithms: Element Distinctness

Programming Languages and Techniques (CIS120)

What is an algorithm?

Lecture 27: Learning from relational data

Chapter 8 SETS AND DICTIONARIES

Substitute Quick Reference (SmartFindExpress Substitute Calling System and Web Center)

Searching for Information. A Simple Method for Searching. Simple Searching. Class #21: Searching/Sorting I

Lecture 13. Math 22 Summer 2017 July 17, 2017

PIC 10B Lecture 1 Winter 2014 Homework Assignment #2

61A LECTURE 8 SEQUENCES, ITERABLES

61A LECTURE 8 SEQUENCES, ITERABLES. Steven Tang and Eric Tzeng July 8, 2013

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

CS 106B, Lecture 1 Introduction to C++

Structure and Interpretation of Computer Programs

If you took your exam home last time, I will still regrade it if you want.

CSE 373: Data Structures and Algorithms

CS 61A Discussion 8: Scheme. March 23, 2017

Programming Languages and Techniques (CIS120)

isinstance and While Loops

15-110: Principles of Computing, Spring 2018

Structure and Interpretation of Computer Programs

Armstrong State University Engineering Studies MATLAB Marina Switch-Case Statements Primer

CompSci 101 Introduction to Computer Science

Python: Short Overview and Recap

Implementing Hash and AVL

Transcription:

61A Lecture 21 Friday, March 1

Announcements Project is due Thursday 10/2 @ :59pm Please submit two ways: the normal way and using python ok --submit! You can view your ok submission on the ok website: http://ok.cs61a.org Midterm 2 is on Thursday /19 7pm-9pm Review session on Tuesday /17 5pm-6:0pm in 2050 VLSB HKN review session on Sunday /15 12-pm in 10 vans Conflict form submissions are due Friday /1! 1 2-sided sheet of hand-written notes created by you + 2 official study guides Same exam location as midterm 1. See http://cs61a.org/exams/midterm2.html Today's lecture contains the last of the Midterm 2 material (Monday is just examples) No lecture next Wednesday /1 No discussion sections next Thursday /19 or Friday /20 Lecture next Friday /20 is a video (but a great one) 2

Sets

Sets One more built-in Python container type Set literals are enclosed in braces Duplicate elements are removed on construction Sets are unordered, just like dictionary entries >>> s = {, 2, 1, 4, 4} >>> s {1, 2,, 4} >>> in s True >>> len(s) 4 >>> s.union({1, 5}) {1, 2,, 4, 5} >>> s.intersection({6, 5, 4, }) {, 4} 4

Implementing Sets What we should be able to do with a set: Membership testing: Is a value an element of a set? Union: Return a set with all elements in set1 or set2 Intersection: Return a set with any elements in set1 and set2 Adjoin: Return a set with all elements in s and a value v Union Intersection Adjoin 4 1 5 2 4 1 5 2 4 1 2 1 2 1 2 4 5 4 5

Sets as Unordered Sequences

Sets as Unordered Sequences Proposal 1: A set is represented by a linked list that contains no duplicate items. Time order of growth def empty(s): return s is Link.empty (1) def set_contains(s, v): """Return whether set s contains value v.! >>> s = Link(1, Link(2, Link())) >>> set_contains(s, 2) True """ if empty(s): return False elif s.first == v: return True else: return set_contains(s.rest, v) Time depends on whether & where v appears in s (n) Assuming v either does not appear in s or appears in a uniformly distributed random location (Demo) 7

Sets as Unordered Sequences def adjoin_set(s, v): if set_contains(s, v): return s else: return Link(v, s) def intersect_set(set1, set2): in_set2 = lambda v: set_contains(set2, v) return keep_if(set1, in_set2) Need a new version defined for Link instances def union_set(set1, set2): not_in_set2 = lambda v: not set_contains(set2, v) set1_not_set2 = keep_if(set1, not_in_set2) return extend(set1_not_set2, set2) Need a new version defined for Link instances Time order of growth (n) The size of the set (n 2 ) If sets are the same size (n 2 ) (Demo)

Sets as Ordered Sequences

Sets as Ordered Sequences Proposal 2: A set is represented by a linked list with unique elements that is ordered from least to greatest Parts of the program that... Assume that sets are... Using... Use sets to contain values Unordered collections empty, set_contains, adjoin_set, intersect_set, union_set Implement set operations Ordered linked lists first, rest, <, >, == Different parts of a program may make different assumptions about data 10

Sets as Ordered Sequences Proposal 2: A set is represented by a linked list with unique elements that is ordered from least to greatest def intersect_set(set1, set2): if empty(set1) or empty(set2): return Link.empty else: e1, e2 = set1.first, set2.first if e1 == e2: return Link(e1, intersect_set(set1.rest, set2.rest)) elif e1 < e2: return intersect_set(set1.rest, set2) elif e2 < e1: return intersect_set(set1, set2.rest) Order of growth? (n)

Sets as Binary Search Trees

Binary Search Trees Proposal : A set is represented as a Tree with two branches. ach entry is: Larger than all entries in its left branch and Smaller than all entries in its right branch 7 5 9 1 7 9 1 5 5 9 1 7 1

Binary Tree Class A binary tree is a tree that has a left branch and a right branch Idea: Fill the place of a missing left branch with an empty tree Idea 2: An instance of BinaryTree always has exactly two branches 1 7 5 9 : An empty tree class BinaryTree(Tree): empty = Tree(None) empty.is_empty = True!!! def init (self, entry, left=empty, right=empty): Tree. init (self, entry, (left, right)) self.is_empty = False @property def left(self): return self.branches[0] @property def right(self): return self.branches[1] Bin = BinaryTree t = Bin(, Bin(1), Bin(7, Bin(5), Bin(9, Bin.empty, Bin()))) 14

Membership in Binary Search Trees set_contains traverses the tree If the element is not the entry, it can only be in either the left or right branch By focusing on one branch, we reduce the set by about half with each recursive call def set_contains(s, v): if s.is_empty: return False elif s.entry == v: return True elif s.entry < v: return set_contains(s.right, v) elif s.entry > v: return set_contains(s.left, v) 5 1 7 9 If 9 is in the set, it is in this branch 9 Order of growth? (h) on average (log n) on average for a balanced tree 15

Adjoining to a Tree Set 5 9 7 9 7 1 7 Right! Left! Right! Stop! 5 9 7 9 7 1 7 (Demo) 16