CS 61A Lecture 11 Wednesday, February 18 Announcements 2 - - PowerPoint PPT Presentation

cs 61a lecture 11
SMART_READER_LITE
LIVE PREVIEW

CS 61A Lecture 11 Wednesday, February 18 Announcements 2 - - PowerPoint PPT Presentation

CS 61A Lecture 11 Wednesday, February 18 Announcements 2 Announcements Optional Hog Contest due Wednesday 2/18 @ 11:59pm 2 Announcements Optional Hog Contest due Wednesday 2/18 @ 11:59pm Homework 3 due Thursday 2/19 @ 11:59pm 2


slide-1
SLIDE 1

CS 61A Lecture 11

Wednesday, February 18

slide-2
SLIDE 2

Announcements

2

slide-3
SLIDE 3

Announcements

  • Optional Hog Contest due Wednesday 2/18 @ 11:59pm

2

slide-4
SLIDE 4

Announcements

  • Optional Hog Contest due Wednesday 2/18 @ 11:59pm
  • Homework 3 due Thursday 2/19 @ 11:59pm

2

slide-5
SLIDE 5

Announcements

  • Optional Hog Contest due Wednesday 2/18 @ 11:59pm
  • Homework 3 due Thursday 2/19 @ 11:59pm
  • Project 2 due Thursday 2/26 @ 11:59pm

2

slide-6
SLIDE 6

Announcements

  • Optional Hog Contest due Wednesday 2/18 @ 11:59pm
  • Homework 3 due Thursday 2/19 @ 11:59pm
  • Project 2 due Thursday 2/26 @ 11:59pm

§Bonus point for early submission by Wednesday 2/25 @ 11:59pm!

2

slide-7
SLIDE 7

Box-and-Pointer Notation

slide-8
SLIDE 8

The Closure Property of Data Types

4

slide-9
SLIDE 9

The Closure Property of Data Types

  • A method for combining data values satisfies the closure property if:



 The result of combination can itself be combined using the same method

4

slide-10
SLIDE 10

The Closure Property of Data Types

  • A method for combining data values satisfies the closure property if:



 The result of combination can itself be combined using the same method

  • Closure is powerful because it permits us to create hierarchical structures

4

slide-11
SLIDE 11

The Closure Property of Data Types

  • A method for combining data values satisfies the closure property if:



 The result of combination can itself be combined using the same method

  • Closure is powerful because it permits us to create hierarchical structures
  • Hierarchical structures are made up of parts, which themselves are made up
  • f parts, and so on

4

slide-12
SLIDE 12

The Closure Property of Data Types

  • A method for combining data values satisfies the closure property if:



 The result of combination can itself be combined using the same method

  • Closure is powerful because it permits us to create hierarchical structures
  • Hierarchical structures are made up of parts, which themselves are made up
  • f parts, and so on

Lists can contain lists as elements (in addition to anything else)

4

slide-13
SLIDE 13

Box-and-Pointer Notation in Environment Diagrams

5

Interactive Diagram

slide-14
SLIDE 14

Box-and-Pointer Notation in Environment Diagrams

Lists are represented as a row of index-labeled adjacent boxes, one per element

5

Interactive Diagram

slide-15
SLIDE 15

Box-and-Pointer Notation in Environment Diagrams

Lists are represented as a row of index-labeled adjacent boxes, one per element Each box either contains a primitive value or points to a compound value

5

Interactive Diagram

slide-16
SLIDE 16

Box-and-Pointer Notation in Environment Diagrams

Lists are represented as a row of index-labeled adjacent boxes, one per element Each box either contains a primitive value or points to a compound value

5

Interactive Diagram

slide-17
SLIDE 17

Box-and-Pointer Notation in Environment Diagrams

Lists are represented as a row of index-labeled adjacent boxes, one per element Each box either contains a primitive value or points to a compound value

5

Interactive Diagram

slide-18
SLIDE 18

Box-and-Pointer Notation in Environment Diagrams

Lists are represented as a row of index-labeled adjacent boxes, one per element Each box either contains a primitive value or points to a compound value

6

Interactive Diagram

slide-19
SLIDE 19

Sequence Operations

slide-20
SLIDE 20

Membership & Slicing

Python sequences have operators for membership and slicing

8

slide-21
SLIDE 21

Membership & Slicing

Python sequences have operators for membership and slicing Membership.

8

slide-22
SLIDE 22

Membership & Slicing

>>> digits = [1, 8, 2, 8] >>> 2 in digits True >>> 1828 not in digits True Python sequences have operators for membership and slicing Membership.

8

slide-23
SLIDE 23

Membership & Slicing

>>> digits = [1, 8, 2, 8] >>> 2 in digits True >>> 1828 not in digits True Python sequences have operators for membership and slicing Membership. Slicing.

8

slide-24
SLIDE 24

Membership & Slicing

>>> digits = [1, 8, 2, 8] >>> 2 in digits True >>> 1828 not in digits True >>> digits[0:2] [1, 8] >>> digits[1:] [8, 2, 8] Python sequences have operators for membership and slicing Membership. Slicing.

8

slide-25
SLIDE 25

Membership & Slicing

>>> digits = [1, 8, 2, 8] >>> 2 in digits True >>> 1828 not in digits True >>> digits[0:2] [1, 8] >>> digits[1:] [8, 2, 8] Python sequences have operators for membership and slicing Membership. Slicing.

8

Slicing creates a new object

slide-26
SLIDE 26

Membership & Slicing

>>> digits = [1, 8, 2, 8] >>> 2 in digits True >>> 1828 not in digits True >>> digits[0:2] [1, 8] >>> digits[1:] [8, 2, 8] Python sequences have operators for membership and slicing Membership. Slicing.

8

Slicing creates a new object

slide-27
SLIDE 27

Membership & Slicing

>>> digits = [1, 8, 2, 8] >>> 2 in digits True >>> 1828 not in digits True >>> digits[0:2] [1, 8] >>> digits[1:] [8, 2, 8] Python sequences have operators for membership and slicing Membership. Slicing.

8

Slicing creates a new object

slide-28
SLIDE 28

Trees

slide-29
SLIDE 29

Tree Abstraction

10

slide-30
SLIDE 30

Tree Abstraction

10

5 2 1 3 1 1 1 1 2 1 1 1

slide-31
SLIDE 31

Tree Abstraction

10

A tree has a root value and a sequence of branches; each branch is a tree

5 2 1 3 1 1 1 1 2 1 1 1

slide-32
SLIDE 32

Tree Abstraction

10

A tree has a root value and a sequence of branches; each branch is a tree

5 2 1 3 1 1 1 1 2 1 1 1

Root

slide-33
SLIDE 33

Tree Abstraction

10

A tree has a root value and a sequence of branches; each branch is a tree

5 2 1 3 1 1 1 1 2 1 1 1

Root Branch

slide-34
SLIDE 34

Tree Abstraction

10

A tree has a root value and a sequence of branches; each branch is a tree A tree with zero branches is called a leaf

5 2 1 3 1 1 1 1 2 1 1 1

Root Branch

slide-35
SLIDE 35

Tree Abstraction

10

A tree has a root value and a sequence of branches; each branch is a tree A tree with zero branches is called a leaf

5 2 1 3 1 1 1 1 2 1 1 1

Root Leaf Branch

slide-36
SLIDE 36

Tree Abstraction

10

A tree has a root value and a sequence of branches; each branch is a tree A tree with zero branches is called a leaf

5 2 1 3 1 1 1 1 2 1 1 1

Root Leaf Branch

slide-37
SLIDE 37

Tree Abstraction

10

A tree has a root value and a sequence of branches; each branch is a tree A tree with zero branches is called a leaf The root values of sub-trees within a tree are often called node values or nodes

5 2 1 3 1 1 1 1 2 1 1 1

Root Leaf Branch

slide-38
SLIDE 38

Tree Abstraction

10

A tree has a root value and a sequence of branches; each branch is a tree A tree with zero branches is called a leaf The root values of sub-trees within a tree are often called node values or nodes

5 2 1 3 1 1 1 1 2 1 1 1

Root Leaf Branch Sub-tree

slide-39
SLIDE 39

Tree Abstraction

10

A tree has a root value and a sequence of branches; each branch is a tree A tree with zero branches is called a leaf The root values of sub-trees within a tree are often called node values or nodes

5 2 1 3 1 1 1 1 2 1 1 1

Root Node Leaf Branch Sub-tree

slide-40
SLIDE 40

Implementing the Tree Abstraction

11

slide-41
SLIDE 41

Implementing the Tree Abstraction

A tree has a root value and a sequence of branches; each branch is a tree

11

slide-42
SLIDE 42

Implementing the Tree Abstraction

A tree has a root value and a sequence of branches; each branch is a tree

11

2 1 3 1 1

slide-43
SLIDE 43

Implementing the Tree Abstraction

A tree has a root value and a sequence of branches; each branch is a tree

11

>>> tree(3, [tree(1), ... tree(2, [tree(1), ... tree(1)])])

2 1 3 1 1

slide-44
SLIDE 44

Implementing the Tree Abstraction

A tree has a root value and a sequence of branches; each branch is a tree

11

>>> tree(3, [tree(1), ... tree(2, [tree(1), ... tree(1)])]) [3, [1], [2, [1], [1]]]

2 1 3 1 1

slide-45
SLIDE 45

Implementing the Tree Abstraction

A tree has a root value and a sequence of branches; each branch is a tree

11

>>> tree(3, [tree(1), ... tree(2, [tree(1), ... tree(1)])]) [3, [1], [2, [1], [1]]]

2 1 3 1 1

def tree(root, branches=[]):

slide-46
SLIDE 46

Implementing the Tree Abstraction

A tree has a root value and a sequence of branches; each branch is a tree

11

>>> tree(3, [tree(1), ... tree(2, [tree(1), ... tree(1)])]) [3, [1], [2, [1], [1]]]

2 1 3 1 1

def tree(root, branches=[]): return [root] + branches

slide-47
SLIDE 47

Implementing the Tree Abstraction

A tree has a root value and a sequence of branches; each branch is a tree

11

>>> tree(3, [tree(1), ... tree(2, [tree(1), ... tree(1)])]) [3, [1], [2, [1], [1]]]

2 1 3 1 1

def tree(root, branches=[]): return [root] + branches def root(tree):

slide-48
SLIDE 48

Implementing the Tree Abstraction

A tree has a root value and a sequence of branches; each branch is a tree

11

>>> tree(3, [tree(1), ... tree(2, [tree(1), ... tree(1)])]) [3, [1], [2, [1], [1]]]

2 1 3 1 1

def tree(root, branches=[]): return [root] + branches def root(tree): return tree[0]

slide-49
SLIDE 49

Implementing the Tree Abstraction

A tree has a root value and a sequence of branches; each branch is a tree

11

>>> tree(3, [tree(1), ... tree(2, [tree(1), ... tree(1)])]) [3, [1], [2, [1], [1]]]

2 1 3 1 1

def tree(root, branches=[]): return [root] + branches def root(tree): return tree[0] def branches(tree):

slide-50
SLIDE 50

Implementing the Tree Abstraction

A tree has a root value and a sequence of branches; each branch is a tree

11

>>> tree(3, [tree(1), ... tree(2, [tree(1), ... tree(1)])]) [3, [1], [2, [1], [1]]]

2 1 3 1 1

def tree(root, branches=[]): return [root] + branches def root(tree): return tree[0] def branches(tree): return tree[1:]

slide-51
SLIDE 51

Implementing the Tree Abstraction

A tree has a root value and a sequence of branches; each branch is a tree

12

for branch in branches: assert is_tree(branch) return [root] + list(branches) >>> tree(3, [tree(1), ... tree(2, [tree(1), ... tree(1)])]) [3, [1], [2, [1], [1]]]

2 1 3 1 1

def root(tree): return tree[0]

  • def branches(tree):

return tree[1:] def tree(root, branches=[]):

slide-52
SLIDE 52

Implementing the Tree Abstraction

A tree has a root value and a sequence of branches; each branch is a tree

12

for branch in branches: assert is_tree(branch) return [root] + list(branches) >>> tree(3, [tree(1), ... tree(2, [tree(1), ... tree(1)])]) [3, [1], [2, [1], [1]]]

2 1 3 1 1

Creates a list from a sequence

  • f branches

def root(tree): return tree[0]

  • def branches(tree):

return tree[1:] def tree(root, branches=[]):

slide-53
SLIDE 53

Implementing the Tree Abstraction

A tree has a root value and a sequence of branches; each branch is a tree

12

for branch in branches: assert is_tree(branch) return [root] + list(branches) >>> tree(3, [tree(1), ... tree(2, [tree(1), ... tree(1)])]) [3, [1], [2, [1], [1]]]

2 1 3 1 1

Creates a list from a sequence

  • f branches

def root(tree): return tree[0]

  • def branches(tree):

return tree[1:] def tree(root, branches=[]): Verifies the tree definition

slide-54
SLIDE 54

Implementing the Tree Abstraction

A tree has a root value and a sequence of branches; each branch is a tree

12

for branch in branches: assert is_tree(branch) return [root] + list(branches) >>> tree(3, [tree(1), ... tree(2, [tree(1), ... tree(1)])]) [3, [1], [2, [1], [1]]]

2 1 3 1 1

Creates a list from a sequence

  • f branches

def root(tree): return tree[0]

  • def branches(tree):

return tree[1:] def is_tree(tree): if type(tree) != list or len(tree) < 1: return False for branch in branches(tree): if not is_tree(branch): return False return True def tree(root, branches=[]): Verifies the tree definition

slide-55
SLIDE 55

Implementing the Tree Abstraction

A tree has a root value and a sequence of branches; each branch is a tree

12

for branch in branches: assert is_tree(branch) return [root] + list(branches) >>> tree(3, [tree(1), ... tree(2, [tree(1), ... tree(1)])]) [3, [1], [2, [1], [1]]]

2 1 3 1 1

Verifies that tree is bound to a list Creates a list from a sequence

  • f branches

def root(tree): return tree[0]

  • def branches(tree):

return tree[1:] def is_tree(tree): if type(tree) != list or len(tree) < 1: return False for branch in branches(tree): if not is_tree(branch): return False return True def tree(root, branches=[]): Verifies the tree definition

slide-56
SLIDE 56

Implementing the Tree Abstraction

A tree has a root value and a sequence of branches; each branch is a tree

12

for branch in branches: assert is_tree(branch) return [root] + list(branches) >>> tree(3, [tree(1), ... tree(2, [tree(1), ... tree(1)])]) [3, [1], [2, [1], [1]]]

2 1 3 1 1

def is_leaf(tree): return not branches(tree) Verifies that tree is bound to a list Creates a list from a sequence

  • f branches

def root(tree): return tree[0]

  • def branches(tree):

return tree[1:] def is_tree(tree): if type(tree) != list or len(tree) < 1: return False for branch in branches(tree): if not is_tree(branch): return False return True def tree(root, branches=[]): Verifies the tree definition

slide-57
SLIDE 57

Implementing the Tree Abstraction

A tree has a root value and a sequence of branches; each branch is a tree (Demo)

12

for branch in branches: assert is_tree(branch) return [root] + list(branches) >>> tree(3, [tree(1), ... tree(2, [tree(1), ... tree(1)])]) [3, [1], [2, [1], [1]]]

2 1 3 1 1

def is_leaf(tree): return not branches(tree) Verifies that tree is bound to a list Creates a list from a sequence

  • f branches

def root(tree): return tree[0]

  • def branches(tree):

return tree[1:] def is_tree(tree): if type(tree) != list or len(tree) < 1: return False for branch in branches(tree): if not is_tree(branch): return False return True def tree(root, branches=[]): Verifies the tree definition

slide-58
SLIDE 58

Tree Processing Uses Recursion

13

slide-59
SLIDE 59

Tree Processing Uses Recursion

13

def count_leaves(tree): """Count the leaves of a tree."""

slide-60
SLIDE 60

Tree Processing Uses Recursion

Processing a leaf is often the base case of a tree processing function

13

def count_leaves(tree): """Count the leaves of a tree."""

slide-61
SLIDE 61

Tree Processing Uses Recursion

Processing a leaf is often the base case of a tree processing function

13

def count_leaves(tree): """Count the leaves of a tree.""" if is_leaf(tree): return 1

slide-62
SLIDE 62

Tree Processing Uses Recursion

Processing a leaf is often the base case of a tree processing function The recursive case typically makes a recursive call on each branch, then aggregates

13

def count_leaves(tree): """Count the leaves of a tree.""" if is_leaf(tree): return 1

slide-63
SLIDE 63

Tree Processing Uses Recursion

Processing a leaf is often the base case of a tree processing function The recursive case typically makes a recursive call on each branch, then aggregates

13

def count_leaves(tree): """Count the leaves of a tree.""" if is_leaf(tree): return 1 else: branch_counts = [count_leaves(b) for b in tree]

slide-64
SLIDE 64

Tree Processing Uses Recursion

Processing a leaf is often the base case of a tree processing function The recursive case typically makes a recursive call on each branch, then aggregates

13

def count_leaves(tree): """Count the leaves of a tree.""" if is_leaf(tree): return 1 else: branch_counts = [count_leaves(b) for b in tree] return sum(branch_counts)

slide-65
SLIDE 65

Tree Processing Uses Recursion

Processing a leaf is often the base case of a tree processing function The recursive case typically makes a recursive call on each branch, then aggregates

13

(Demo) def count_leaves(tree): """Count the leaves of a tree.""" if is_leaf(tree): return 1 else: branch_counts = [count_leaves(b) for b in tree] return sum(branch_counts)

slide-66
SLIDE 66

Discussion Question

14

slide-67
SLIDE 67

Discussion Question

Implement leaves, which returns a list of the leaf values of a tree

14

slide-68
SLIDE 68

Discussion Question

Implement leaves, which returns a list of the leaf values of a tree

14

def leaves(tree): """Return a list containing the leaves of tree. >>> leaves(fib_tree(5)) [1, 0, 1, 0, 1, 1, 0, 1] """

slide-69
SLIDE 69

Discussion Question

Implement leaves, which returns a list of the leaf values of a tree Hint: If you sum a list of lists, you get a list containing the elements of those lists

14

def leaves(tree): """Return a list containing the leaves of tree. >>> leaves(fib_tree(5)) [1, 0, 1, 0, 1, 1, 0, 1] """

slide-70
SLIDE 70

Discussion Question

Implement leaves, which returns a list of the leaf values of a tree Hint: If you sum a list of lists, you get a list containing the elements of those lists

14

def leaves(tree): """Return a list containing the leaves of tree. >>> leaves(fib_tree(5)) [1, 0, 1, 0, 1, 1, 0, 1] """ >>> sum([ [1] ], [])

slide-71
SLIDE 71

Discussion Question

Implement leaves, which returns a list of the leaf values of a tree Hint: If you sum a list of lists, you get a list containing the elements of those lists

14

def leaves(tree): """Return a list containing the leaves of tree. >>> leaves(fib_tree(5)) [1, 0, 1, 0, 1, 1, 0, 1] """ >>> sum([ [1] ], []) [1]

slide-72
SLIDE 72

Discussion Question

Implement leaves, which returns a list of the leaf values of a tree Hint: If you sum a list of lists, you get a list containing the elements of those lists

14

def leaves(tree): """Return a list containing the leaves of tree. >>> leaves(fib_tree(5)) [1, 0, 1, 0, 1, 1, 0, 1] """ >>> sum([ [1] ], []) [1] >>> sum([ [[1]], [2] ], [])

slide-73
SLIDE 73

Discussion Question

Implement leaves, which returns a list of the leaf values of a tree Hint: If you sum a list of lists, you get a list containing the elements of those lists

14

def leaves(tree): """Return a list containing the leaves of tree. >>> leaves(fib_tree(5)) [1, 0, 1, 0, 1, 1, 0, 1] """ >>> sum([ [1] ], []) [1] >>> sum([ [[1]], [2] ], []) [[1], 2]

slide-74
SLIDE 74

Discussion Question

Implement leaves, which returns a list of the leaf values of a tree Hint: If you sum a list of lists, you get a list containing the elements of those lists

14

def leaves(tree): """Return a list containing the leaves of tree. >>> leaves(fib_tree(5)) [1, 0, 1, 0, 1, 1, 0, 1] """ >>> sum([ [1] ], []) [1] >>> sum([ [[1]], [2] ], []) [[1], 2] >>> sum([ [1], [2, 3], [4] ], []) [1, 2, 3, 4]

slide-75
SLIDE 75

Discussion Question

Implement leaves, which returns a list of the leaf values of a tree Hint: If you sum a list of lists, you get a list containing the elements of those lists

14

def leaves(tree): """Return a list containing the leaves of tree. >>> leaves(fib_tree(5)) [1, 0, 1, 0, 1, 1, 0, 1] """ if is_leaf(tree): return [root(tree)] else: return _____________________________________________ >>> sum([ [1] ], []) [1] >>> sum([ [[1]], [2] ], []) [[1], 2] >>> sum([ [1], [2, 3], [4] ], []) [1, 2, 3, 4]

slide-76
SLIDE 76

Discussion Question

Implement leaves, which returns a list of the leaf values of a tree Hint: If you sum a list of lists, you get a list containing the elements of those lists

14

sum([leaves(b) for b in branches(tree)], [])) def leaves(tree): """Return a list containing the leaves of tree. >>> leaves(fib_tree(5)) [1, 0, 1, 0, 1, 1, 0, 1] """ if is_leaf(tree): return [root(tree)] else: return _____________________________________________ >>> sum([ [1] ], []) [1] >>> sum([ [[1]], [2] ], []) [[1], 2] >>> sum([ [1], [2, 3], [4] ], []) [1, 2, 3, 4]

slide-77
SLIDE 77

Example: Partition Trees

(Demo) Interactive Diagram