SLIDE 1
Composition Announcements Linked Lists Linked List Structure A - - PowerPoint PPT Presentation
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 2
SLIDE 3
Linked Lists
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Linked List Class
6
Link(3, Link(4, Link(5 )))
SLIDE 22
Linked List Class
6
Linked list class: attributes are passed to __init__ Link(3, Link(4, Link(5 )))
SLIDE 23
Linked List Class
class Link:
6
Linked list class: attributes are passed to __init__ Link(3, Link(4, Link(5 )))
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
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
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
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
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
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
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
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
Linked List Processing
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
Linked Lists Mutation
SLIDE 35
Linked Lists Can Change
Attribute assignment statements can change first and rest attributes of a Link
10
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
>>> 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
>>> 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
>>> 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
>>> 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
>>> 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
>>> 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
>>> 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
>>> 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
>>> 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
>>> 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
>>> 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
>>> 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
Linked List Mutation Example
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
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
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
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
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
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
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
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
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
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
Adding to a Set Represented as an Ordered List
16
s:
SLIDE 61
Adding to a Set Represented as an Ordered List
16
def add(s, v): s:
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Tree Class
SLIDE 78
Tree Abstraction (Review)
18
2 3 1 1 1 1 1
SLIDE 79
Tree Abstraction (Review)
18
Recursive description (wooden trees):
2 3 1 1
Relative description (family trees):
1 1 1
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Tree Class
19
A Tree has a label and a list of branches; each branch is a Tree
SLIDE 99
Tree Class
class Tree:
19
A Tree has a label and a list of branches; each branch is a Tree
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
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
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
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
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
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
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
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
Tree Mutation
SLIDE 109
Example: Pruning Trees
Removing subtrees from a tree is called pruning Prune branches before recursive processing
21
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
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
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
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
Example: Pruning Trees
Removing subtrees from a tree is called pruning Prune branches before recursive processing
21