Composition Announcements Linked Lists Linked List Structure A - - PowerPoint PPT Presentation

composition announcements linked lists linked list
SMART_READER_LITE
LIVE PREVIEW

Composition Announcements Linked Lists Linked List Structure A - - PowerPoint PPT Presentation

Composition Announcements Linked Lists Linked List Structure A linked list is either empty or a first value and the rest of the linked list 4 Linked List Structure A linked list is either empty or a first value and the rest of the linked list


slide-1
SLIDE 1

Composition

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Linked Lists

slide-4
SLIDE 4

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

4

slide-5
SLIDE 5

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

4

3 , 4 , 5

slide-6
SLIDE 6

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

4

3 , 4 , 5 first: 3 rest: Link instance

slide-7
SLIDE 7

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

4

3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance

slide-8
SLIDE 8

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

4

3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance

slide-9
SLIDE 9

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

4

3 , 4 , 5 Link.empty first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance

slide-10
SLIDE 10

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

4

3 , 4 , 5 Link.empty first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance A linked list is a pair

slide-11
SLIDE 11

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

4

3 , 4 , 5 Link.empty first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance A linked list is a pair The first (zeroth) element is an attribute value

slide-12
SLIDE 12

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

4

3 , 4 , 5 Link.empty first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance A linked list is a pair The first (zeroth) element is an attribute value The rest of the elements are stored in a linked list

slide-13
SLIDE 13

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

4

3 , 4 , 5 Link.empty first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance A linked list is a pair The first (zeroth) element is an attribute value The rest of the elements are stored in a linked list A class attribute represents an empty linked list

slide-14
SLIDE 14

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

4

3 , 4 , 5 Link.empty first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance Link(3, Link(4, Link(5, Link.empty))) A linked list is a pair The first (zeroth) element is an attribute value The rest of the elements are stored in a linked list A class attribute represents an empty linked list

slide-15
SLIDE 15

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

5

3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance Link.empty , Link.empty ) Link(3, Link(4, Link(5 )))

slide-16
SLIDE 16

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

5

3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance Link.empty , Link.empty ) Link(3, Link(4, Link(5 )))

slide-17
SLIDE 17

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

5

3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance Link.empty , Link.empty ) Link(3, Link(4, Link(5 )))

slide-18
SLIDE 18

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

5

3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance Link.empty , Link.empty ) Link(3, Link(4, Link(5 )))

slide-19
SLIDE 19

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

5

3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance , Link.empty ) Link(3, Link(4, Link(5 )))

slide-20
SLIDE 20

Linked List Structure

A linked list is either empty or a first value and the rest of the linked list

5

3 , 4 , 5 first: 3 rest: Link instance first: 4 rest: Link instance first: 5 rest: Link instance Link(3, Link(4, Link(5 )))

slide-21
SLIDE 21

Linked List Class

6

Link(3, Link(4, Link(5 )))

slide-22
SLIDE 22

Linked List Class

6

Linked list class: attributes are passed to __init__ Link(3, Link(4, Link(5 )))

slide-23
SLIDE 23

Linked List Class

class Link:

6

Linked list class: attributes are passed to __init__ Link(3, Link(4, Link(5 )))

slide-24
SLIDE 24

Linked List Class

class Link:

6

Linked list class: attributes are passed to __init__ def __init__(self, first, rest=empty): Link(3, Link(4, Link(5 )))

slide-25
SLIDE 25

Linked List Class

class Link:

6

Linked list class: attributes are passed to __init__ def __init__(self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) Link(3, Link(4, Link(5 )))

slide-26
SLIDE 26

Linked List Class

class Link:

6

Linked list class: attributes are passed to __init__ def __init__(self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) self.first = first self.rest = rest Link(3, Link(4, Link(5 )))

slide-27
SLIDE 27

Linked List Class

class Link:

6

Linked list class: attributes are passed to __init__ def __init__(self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) self.first = first self.rest = rest Link(3, Link(4, Link(5 ))) Returns whether rest is a Link

slide-28
SLIDE 28

Linked List Class

class Link:

6

Linked list class: attributes are passed to __init__ def __init__(self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) self.first = first self.rest = rest Link(3, Link(4, Link(5 ))) Returns whether rest is a Link help(isinstance): Return whether an object is an instance of a class or of a subclass thereof.

slide-29
SLIDE 29

Linked List Class

class Link: empty = ()

6

Linked list class: attributes are passed to __init__ def __init__(self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) self.first = first self.rest = rest Link(3, Link(4, Link(5 ))) Returns whether rest is a Link help(isinstance): Return whether an object is an instance of a class or of a subclass thereof.

slide-30
SLIDE 30

Linked List Class

class Link: empty = ()

6

Some zero-length sequence Linked list class: attributes are passed to __init__ def __init__(self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) self.first = first self.rest = rest Link(3, Link(4, Link(5 ))) Returns whether rest is a Link help(isinstance): Return whether an object is an instance of a class or of a subclass thereof.

slide-31
SLIDE 31

Linked List Class

class Link: empty = ()

6

Some zero-length sequence Linked list class: attributes are passed to __init__ def __init__(self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) self.first = first self.rest = rest (Demo) Link(3, Link(4, Link(5 ))) Returns whether rest is a Link help(isinstance): Return whether an object is an instance of a class or of a subclass thereof.

slide-32
SLIDE 32

Linked List Processing

slide-33
SLIDE 33

Example: Range, Map, and Filter for Linked Lists

square, odd = lambda x: x * x, lambda x: x % 2 == 1 list(map(square, filter(odd, range(1, 6)))) # [1, 9, 25] map_link(square, filter_link(odd, range_link(1, 6))) # Link(1, Link(9, Link(25))) def range_link(start, end): """Return a Link containing consecutive integers from start to end. >>> range_link(3, 6) Link(3, Link(4, Link(5))) """ def map_link(f, s): """Return a Link that contains f(x) for each x in Link s. >>> map_link(square, range_link(3, 6)) Link(9, Link(16, Link(25))) """ def filter_link(f, s): """Return a Link that contains only the elements x of Link s for which f(x) is a true value. >>> filter_link(odd, range_link(3, 6)) Link(3, Link(5)) """

8

slide-34
SLIDE 34

Linked Lists Mutation

slide-35
SLIDE 35

Linked Lists Can Change

Attribute assignment statements can change first and rest attributes of a Link

10

slide-36
SLIDE 36

Linked Lists Can Change

Attribute assignment statements can change first and rest attributes of a Link

10

The rest of a linked list can contain the linked list as a sub-list

slide-37
SLIDE 37

>>> s = Link(1, Link(2, Link(3)))

Linked Lists Can Change

Attribute assignment statements can change first and rest attributes of a Link

10

The rest of a linked list can contain the linked list as a sub-list

slide-38
SLIDE 38

>>> s = Link(1, Link(2, Link(3)))

Linked Lists Can Change

Attribute assignment statements can change first and rest attributes of a Link

10

The rest of a linked list can contain the linked list as a sub-list

Rest First

1

s Rest First

2

Global frame Rest First

3

slide-39
SLIDE 39

>>> s = Link(1, Link(2, Link(3)))

Linked Lists Can Change

Attribute assignment statements can change first and rest attributes of a Link

10

The rest of a linked list can contain the linked list as a sub-list Note: The actual environment diagram is much more complicated.

Rest First

1

s Rest First

2

Global frame Rest First

3

slide-40
SLIDE 40

>>> s = Link(1, Link(2, Link(3)))

Linked Lists Can Change

Attribute assignment statements can change first and rest attributes of a Link

10

The rest of a linked list can contain the linked list as a sub-list Note: The actual environment diagram is much more complicated.

slide-41
SLIDE 41

>>> s = Link(1, Link(2, Link(3))) >>> s.first = 5

Linked Lists Can Change

Attribute assignment statements can change first and rest attributes of a Link

10

The rest of a linked list can contain the linked list as a sub-list Note: The actual environment diagram is much more complicated.

slide-42
SLIDE 42

>>> s = Link(1, Link(2, Link(3))) >>> s.first = 5 >>> t = s.rest

Linked Lists Can Change

Attribute assignment statements can change first and rest attributes of a Link

10

The rest of a linked list can contain the linked list as a sub-list Note: The actual environment diagram is much more complicated.

slide-43
SLIDE 43

>>> s = Link(1, Link(2, Link(3))) >>> s.first = 5 >>> t = s.rest >>> t.rest = s

Linked Lists Can Change

Attribute assignment statements can change first and rest attributes of a Link

10

The rest of a linked list can contain the linked list as a sub-list Note: The actual environment diagram is much more complicated.

slide-44
SLIDE 44

>>> s = Link(1, Link(2, Link(3))) >>> s.first = 5 >>> t = s.rest >>> t.rest = s >>> s.first

Linked Lists Can Change

Attribute assignment statements can change first and rest attributes of a Link

10

The rest of a linked list can contain the linked list as a sub-list Note: The actual environment diagram is much more complicated.

slide-45
SLIDE 45

>>> s = Link(1, Link(2, Link(3))) >>> s.first = 5 >>> t = s.rest >>> t.rest = s >>> s.first 5

Linked Lists Can Change

Attribute assignment statements can change first and rest attributes of a Link

10

The rest of a linked list can contain the linked list as a sub-list Note: The actual environment diagram is much more complicated.

slide-46
SLIDE 46

>>> s = Link(1, Link(2, Link(3))) >>> s.first = 5 >>> t = s.rest >>> t.rest = s >>> s.first 5 >>> s.rest.rest.rest.rest.rest.first

Linked Lists Can Change

Attribute assignment statements can change first and rest attributes of a Link

10

The rest of a linked list can contain the linked list as a sub-list Note: The actual environment diagram is much more complicated.

slide-47
SLIDE 47

>>> s = Link(1, Link(2, Link(3))) >>> s.first = 5 >>> t = s.rest >>> t.rest = s >>> s.first 5 >>> s.rest.rest.rest.rest.rest.first 2

Linked Lists Can Change

Attribute assignment statements can change first and rest attributes of a Link

10

The rest of a linked list can contain the linked list as a sub-list Note: The actual environment diagram is much more complicated.

slide-48
SLIDE 48

>>> s = Link(1, Link(2, Link(3))) >>> s.first = 5 >>> t = s.rest >>> t.rest = s >>> s.first 5 >>> s.rest.rest.rest.rest.rest.first 2

Linked Lists Can Change

Attribute assignment statements can change first and rest attributes of a Link

10

Rest First

5

s Rest First

2

t Global frame

The rest of a linked list can contain the linked list as a sub-list Note: The actual environment diagram is much more complicated.

slide-49
SLIDE 49

Linked List Mutation Example

slide-50
SLIDE 50

Adding to an Ordered List

12

first: 1 rest: Link instance first: 3 rest: Link instance first: 5 rest: Link instance s:

slide-51
SLIDE 51

Adding to an Ordered List

12

first: 1 rest: Link instance first: 3 rest: Link instance first: 5 rest: Link instance s: def add(s, v): """Add v to an ordered list s with no repeats, returning modified s.”””

slide-52
SLIDE 52

Adding to an Ordered List

12

first: 1 rest: Link instance first: 3 rest: Link instance first: 5 rest: Link instance s: def add(s, v): """Add v to an ordered list s with no repeats, returning modified s.””” (Note: If v is already in s, then don't modify s, but still return it.)

slide-53
SLIDE 53

Adding to an Ordered List

12

first: 1 rest: Link instance first: 3 rest: Link instance first: 5 rest: Link instance add(s, 0) s: def add(s, v): """Add v to an ordered list s with no repeats, returning modified s.””” (Note: If v is already in s, then don't modify s, but still return it.)

slide-54
SLIDE 54

Adding to an Ordered List

13

first: 1 0 rest: Link instance first: 3 rest: Link instance first: 1 rest: Link instance first: 5 rest: Link instance s: def add(s, v): """Add v to an ordered list s with no repeats, returning modified s.””” (Note: If v is already in s, then don't modify s, but still return it.) add(s, 0)

slide-55
SLIDE 55

Adding to an Ordered List

13

first: 1 0 rest: Link instance first: 3 rest: Link instance first: 1 rest: Link instance first: 5 rest: Link instance s: add(s, 3) def add(s, v): """Add v to an ordered list s with no repeats, returning modified s.””” (Note: If v is already in s, then don't modify s, but still return it.) add(s, 0)

slide-56
SLIDE 56

Adding to an Ordered List

13

first: 1 0 rest: Link instance first: 3 rest: Link instance first: 1 rest: Link instance first: 5 rest: Link instance s: add(s, 4) add(s, 3) def add(s, v): """Add v to an ordered list s with no repeats, returning modified s.””” (Note: If v is already in s, then don't modify s, but still return it.) add(s, 0)

slide-57
SLIDE 57

Adding to an Ordered List

14

first: 1 0 rest: Link instance first: 3 rest: Link instance first: 5 4 rest: Link instance first: 1 rest: Link instance first: 5 rest: Link instance s: add(s, 4) add(s, 3) add(s, 0) def add(s, v): """Add v to an ordered list s with no repeats...”””

slide-58
SLIDE 58

Adding to an Ordered List

14

first: 1 0 rest: Link instance first: 3 rest: Link instance first: 5 4 rest: Link instance first: 1 rest: Link instance first: 5 rest: Link instance add(s, 6) s: add(s, 4) add(s, 3) add(s, 0) def add(s, v): """Add v to an ordered list s with no repeats...”””

slide-59
SLIDE 59

Adding to an Ordered List

15

first: 1 0 rest: Link instance first: 3 rest: Link instance first: 5 4 rest: Link instance first: 1 rest: Link instance first: 5 rest: Link instance first: 6 rest: Link instance s: add(s, 6) add(s, 4) add(s, 3) add(s, 0) def add(s, v): """Add v to an ordered list s with no repeats..."""

slide-60
SLIDE 60

Adding to a Set Represented as an Ordered List

16

s:

slide-61
SLIDE 61

Adding to a Set Represented as an Ordered List

16

def add(s, v): s:

slide-62
SLIDE 62

Adding to a Set Represented as an Ordered List

16

def add(s, v): """Add v to s, returning modified s.””” s:

slide-63
SLIDE 63

Adding to a Set Represented as an Ordered List

16

def add(s, v): """Add v to s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) s:

slide-64
SLIDE 64

Adding to a Set Represented as an Ordered List

16

def add(s, v): """Add v to s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) s:

slide-65
SLIDE 65

Adding to a Set Represented as an Ordered List

16

def add(s, v): """Add v to s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) s:

slide-66
SLIDE 66

Adding to a Set Represented as an Ordered List

16

def add(s, v): """Add v to s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) s:

slide-67
SLIDE 67

Adding to a Set Represented as an Ordered List

16

def add(s, v): """Add v to s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ s:

slide-68
SLIDE 68

Adding to a Set Represented as an Ordered List

16

def add(s, v): """Add v to s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ assert s is not List.empty s:

slide-69
SLIDE 69

Adding to a Set Represented as an Ordered List

16

def add(s, v): """Add v to s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ assert s is not List.empty if s.first > v: s.first, s.rest = __________________________ , _____________________________ s:

slide-70
SLIDE 70

Adding to a Set Represented as an Ordered List

16

def add(s, v): """Add v to s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ assert s is not List.empty if s.first > v: s.first, s.rest = __________________________ , _____________________________ elif s.first < v and empty(s.rest): s.rest = ___________________________________________________________________ s:

slide-71
SLIDE 71

Adding to a Set Represented as an Ordered List

16

def add(s, v): """Add v to s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ assert s is not List.empty if s.first > v: s.first, s.rest = __________________________ , _____________________________ elif s.first < v and empty(s.rest): s.rest = ___________________________________________________________________ elif s.first < v: ____________________________________________________________________________ return s s:

slide-72
SLIDE 72

Adding to a Set Represented as an Ordered List

16

def add(s, v): """Add v to s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ assert s is not List.empty if s.first > v: s.first, s.rest = __________________________ , _____________________________ elif s.first < v and empty(s.rest): s.rest = ___________________________________________________________________ elif s.first < v: ____________________________________________________________________________ return s v s:

slide-73
SLIDE 73

Adding to a Set Represented as an Ordered List

16

def add(s, v): """Add v to s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ assert s is not List.empty if s.first > v: s.first, s.rest = __________________________ , _____________________________ elif s.first < v and empty(s.rest): s.rest = ___________________________________________________________________ elif s.first < v: ____________________________________________________________________________ return s v Link(s.first, s.rest) s:

slide-74
SLIDE 74

Adding to a Set Represented as an Ordered List

16

def add(s, v): """Add v to s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ assert s is not List.empty if s.first > v: s.first, s.rest = __________________________ , _____________________________ elif s.first < v and empty(s.rest): s.rest = ___________________________________________________________________ elif s.first < v: ____________________________________________________________________________ return s v Link(s.first, s.rest) Link(v) s:

slide-75
SLIDE 75

Adding to a Set Represented as an Ordered List

16

def add(s, v): """Add v to s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ assert s is not List.empty if s.first > v: s.first, s.rest = __________________________ , _____________________________ elif s.first < v and empty(s.rest): s.rest = ___________________________________________________________________ elif s.first < v: ____________________________________________________________________________ return s v Link(s.first, s.rest) add(s.rest, v) Link(v) s:

slide-76
SLIDE 76

Adding to a Set Represented as an Ordered List

16

def add(s, v): """Add v to s, returning modified s.””” >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ assert s is not List.empty if s.first > v: s.first, s.rest = __________________________ , _____________________________ elif s.first < v and empty(s.rest): s.rest = ___________________________________________________________________ elif s.first < v: ____________________________________________________________________________ return s v Link(s.first, s.rest) add(s.rest, v) Link(v) s: .

slide-77
SLIDE 77

Tree Class

slide-78
SLIDE 78

Tree Abstraction (Review)

18

2 3 1 1 1 1 1

slide-79
SLIDE 79

Tree Abstraction (Review)

18

Recursive description (wooden trees):

2 3 1 1

Relative description (family trees):

1 1 1

slide-80
SLIDE 80

Tree Abstraction (Review)

18

Recursive description (wooden trees): A tree has a root label and a list of branches

2 3 1 1

Relative description (family trees):

1 1 1

slide-81
SLIDE 81

Tree Abstraction (Review)

18

Recursive description (wooden trees): A tree has a root label and a list of branches

2 3 1 1

Relative description (family trees):

1 1 1

Root label

slide-82
SLIDE 82

Tree Abstraction (Review)

18

Recursive description (wooden trees): A tree has a root label and a list of branches

2 3 1 1

Relative description (family trees):

1 1 1

Root label Branch

slide-83
SLIDE 83

Tree Abstraction (Review)

18

Recursive description (wooden trees): A tree has a root label and a list of branches Each branch is a tree

2 3 1 1

Relative description (family trees):

1 1 1

Root label Branch

slide-84
SLIDE 84

Tree Abstraction (Review)

18

Recursive description (wooden trees): A tree has a root label and a list of branches Each branch is a tree

2 3 1 1

Relative description (family trees):

1 1 1

Root label Branch (also a tree)

slide-85
SLIDE 85

Tree Abstraction (Review)

18

Recursive description (wooden trees): A tree has a root label and a list of branches Each branch is a tree A tree with zero branches is called a leaf

2 3 1 1

Relative description (family trees):

1 1 1

Root label Branch (also a tree)

slide-86
SLIDE 86

Tree Abstraction (Review)

18

Recursive description (wooden trees): A tree has a root label and a list of branches Each branch is a tree A tree with zero branches is called a leaf

2 3 1 1

Relative description (family trees):

1 1 1

Root label Branch (also a tree) Leaf (also a tree)

slide-87
SLIDE 87

Tree Abstraction (Review)

18

Recursive description (wooden trees): A tree has a root label and a list of branches Each branch is a tree A tree with zero branches is called a leaf A tree starts at the root

2 3 1 1

Relative description (family trees):

1 1 1

Root label Branch (also a tree) Leaf (also a tree)

slide-88
SLIDE 88

Tree Abstraction (Review)

18

Recursive description (wooden trees): A tree has a root label and a list of branches Each branch is a tree A tree with zero branches is called a leaf A tree starts at the root

2 3 1 1

Relative description (family trees):

1 1 1

Root label Branch (also a tree) Leaf (also a tree) Root of the whole tree

slide-89
SLIDE 89

Tree Abstraction (Review)

18

Recursive description (wooden trees): A tree has a root label and a list of branches Each branch is a tree A tree with zero branches is called a leaf A tree starts at the root

2 3 1 1

Relative description (family trees):

1 1 1

Root label Branch (also a tree) Leaf (also a tree) Root of the whole tree Root of a branch

slide-90
SLIDE 90

Tree Abstraction (Review)

18

Recursive description (wooden trees): A tree has a root label and a list of branches Each branch is a tree A tree with zero branches is called a leaf A tree starts at the root

2 3 1 1

Relative description (family trees): Each location in a tree is called a node

1 1 1

Root label Branch (also a tree) Leaf (also a tree) Nodes Root of the whole tree Root of a branch

slide-91
SLIDE 91

Tree Abstraction (Review)

18

Recursive description (wooden trees): A tree has a root label and a list of branches Each branch is a tree A tree with zero branches is called a leaf A tree starts at the root

2 3 1 1

Relative description (family trees): Each location in a tree is called a node Each node has a label that can be any value

1 1 1

Root label Branch (also a tree) Leaf (also a tree) Nodes Root of the whole tree Root of a branch

slide-92
SLIDE 92

Tree Abstraction (Review)

18

Recursive description (wooden trees): A tree has a root label and a list of branches Each branch is a tree A tree with zero branches is called a leaf A tree starts at the root

2 3 1 1

Relative description (family trees): Each location in a tree is called a node Each node has a label that can be any value

1 1 1

Root label Branch (also a tree) Leaf (also a tree) Labels Nodes Root of the whole tree Root of a branch

slide-93
SLIDE 93

Tree Abstraction (Review)

18

Recursive description (wooden trees): A tree has a root label and a list of branches Each branch is a tree A tree with zero branches is called a leaf A tree starts at the root

2 3 1 1

Relative description (family trees): Each location in a tree is called a node Each node has a label that can be any value One node can be the parent/child of another

1 1 1

Root label Branch (also a tree) Leaf (also a tree) Labels Nodes Root of the whole tree Root of a branch

slide-94
SLIDE 94

Tree Abstraction (Review)

18

Recursive description (wooden trees): A tree has a root label and a list of branches Each branch is a tree A tree with zero branches is called a leaf A tree starts at the root

2 3 1 1

Relative description (family trees): Each location in a tree is called a node Each node has a label that can be any value One node can be the parent/child of another The top node is the root node

1 1 1

Root label Branch (also a tree) Leaf (also a tree) Labels Nodes Root of the whole tree Root of a branch

slide-95
SLIDE 95

Tree Abstraction (Review)

18

Recursive description (wooden trees): A tree has a root label and a list of branches Each branch is a tree A tree with zero branches is called a leaf A tree starts at the root

2 3 1 1

Relative description (family trees): Each location in a tree is called a node Each node has a label that can be any value One node can be the parent/child of another The top node is the root node

1 1 1

Root label Branch (also a tree) Leaf (also a tree) Labels Nodes Root of the whole tree Root of a branch

  • r Root Node
slide-96
SLIDE 96

Tree Abstraction (Review)

18

Recursive description (wooden trees): A tree has a root label and a list of branches Each branch is a tree A tree with zero branches is called a leaf A tree starts at the root

2 3 1 1

Relative description (family trees): Each location in a tree is called a node Each node has a label that can be any value One node can be the parent/child of another The top node is the root node

1 1 1

Root label Branch (also a tree) Leaf (also a tree) Labels Nodes People often refer to labels by their locations: "each parent is the sum of its children" Root of the whole tree Root of a branch

  • r Root Node
slide-97
SLIDE 97

Tree Abstraction (Review)

18

Recursive description (wooden trees): A tree has a root label and a list of branches Each branch is a tree A tree with zero branches is called a leaf A tree starts at the root

2 3 1 1

Relative description (family trees): Each location in a tree is called a node Each node has a label that can be any value One node can be the parent/child of another The top node is the root node

1 1 1

Root label Branch (also a tree) Leaf (also a tree) Labels Nodes People often refer to labels by their locations: "each parent is the sum of its children" Root of the whole tree Root of a branch Path

  • r Root Node
slide-98
SLIDE 98

Tree Class

19

A Tree has a label and a list of branches; each branch is a Tree

slide-99
SLIDE 99

Tree Class

class Tree:

19

A Tree has a label and a list of branches; each branch is a Tree

slide-100
SLIDE 100

Tree Class

class Tree: def __init__(self, label, branches=[]):

19

A Tree has a label and a list of branches; each branch is a Tree

slide-101
SLIDE 101

Tree Class

class Tree: def __init__(self, label, branches=[]): self.label = label

19

A Tree has a label and a list of branches; each branch is a Tree

slide-102
SLIDE 102

Tree Class

class Tree: def __init__(self, label, branches=[]): self.label = label for branch in branches: assert isinstance(branch, Tree)

19

A Tree has a label and a list of branches; each branch is a Tree

slide-103
SLIDE 103

Tree Class

class Tree: def __init__(self, label, branches=[]): self.label = label for branch in branches: assert isinstance(branch, Tree) self.branches = list(branches)

19

A Tree has a label and a list of branches; each branch is a Tree

slide-104
SLIDE 104

Tree Class

class Tree: def __init__(self, label, branches=[]): self.label = label for branch in branches: assert isinstance(branch, Tree) self.branches = list(branches)

19

A Tree has a label and a list of branches; each branch is a Tree for branch in branches: assert is_tree(branch) return [label] + list(branches) def label(tree): return tree[0] def branches(tree): return tree[1:] def tree(label, branches=[]):

slide-105
SLIDE 105

Tree Class

class Tree: def __init__(self, label, branches=[]): self.label = label for branch in branches: assert isinstance(branch, Tree) self.branches = list(branches) def fib_tree(n): if n == 0 or n == 1: return Tree(n) else: left = fib_tree(n-2) right = fib_tree(n-1) fib_n = left.label + right.label return Tree(fib_n, [left, right])

19

A Tree has a label and a list of branches; each branch is a Tree for branch in branches: assert is_tree(branch) return [label] + list(branches) def label(tree): return tree[0] def branches(tree): return tree[1:] def tree(label, branches=[]):

slide-106
SLIDE 106

Tree Class

class Tree: def __init__(self, label, branches=[]): self.label = label for branch in branches: assert isinstance(branch, Tree) self.branches = list(branches) def fib_tree(n): if n == 0 or n == 1: return Tree(n) else: left = fib_tree(n-2) right = fib_tree(n-1) fib_n = left.label + right.label return Tree(fib_n, [left, right])

19

A Tree has a label and a list of branches; each branch is a Tree for branch in branches: assert is_tree(branch) return [label] + list(branches) def label(tree): return tree[0] def branches(tree): return tree[1:] def tree(label, branches=[]): def fib_tree(n): if n == 0 or n == 1: return tree(n) else: left = fib_tree(n-2) right = fib_tree(n-1) fib_n = label(left) + label(right) return tree(fib_n, [left, right])

slide-107
SLIDE 107

Tree Class

class Tree: def __init__(self, label, branches=[]): self.label = label for branch in branches: assert isinstance(branch, Tree) self.branches = list(branches) def fib_tree(n): if n == 0 or n == 1: return Tree(n) else: left = fib_tree(n-2) right = fib_tree(n-1) fib_n = left.label + right.label return Tree(fib_n, [left, right]) (Demo)

19

A Tree has a label and a list of branches; each branch is a Tree for branch in branches: assert is_tree(branch) return [label] + list(branches) def label(tree): return tree[0] def branches(tree): return tree[1:] def tree(label, branches=[]): def fib_tree(n): if n == 0 or n == 1: return tree(n) else: left = fib_tree(n-2) right = fib_tree(n-1) fib_n = label(left) + label(right) return tree(fib_n, [left, right])

slide-108
SLIDE 108

Tree Mutation

slide-109
SLIDE 109

Example: Pruning Trees

Removing subtrees from a tree is called pruning Prune branches before recursive processing

21

slide-110
SLIDE 110

Example: Pruning Trees

Removing subtrees from a tree is called pruning Prune branches before recursive processing

21

2 3 1 1 1 1 1

slide-111
SLIDE 111

Example: Pruning Trees

Removing subtrees from a tree is called pruning Prune branches before recursive processing

21

2 3 1 1 1 1 1

slide-112
SLIDE 112

Example: Pruning Trees

Removing subtrees from a tree is called pruning Prune branches before recursive processing

21

def prune(t, n): """Prune all sub-trees whose label is n.""" t.branches = [______________ for b in t.branches if _____________________] for b in t.branches: prune(_______________________________, _______________________________)

2 3 1 1 1 1 1

slide-113
SLIDE 113

Example: Pruning Trees

Removing subtrees from a tree is called pruning Prune branches before recursive processing

21

def prune(t, n): """Prune all sub-trees whose label is n.""" t.branches = [______________ for b in t.branches if _____________________] for b in t.branches: prune(_______________________________, _______________________________)

2 3 1 1 1 1 1

b b.label != n

slide-114
SLIDE 114

Example: Pruning Trees

Removing subtrees from a tree is called pruning Prune branches before recursive processing

21

def prune(t, n): """Prune all sub-trees whose label is n.""" t.branches = [______________ for b in t.branches if _____________________] for b in t.branches: prune(_______________________________, _______________________________)

2 3 1 1 1 1 1

b b.label != n b n