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

Property Methods

slide-33
SLIDE 33

Property Methods

In some cases, we want the value of instance attributes to be computed on demand

8

For example, if we want to access the second element of a linked list

slide-34
SLIDE 34

Property Methods

In some cases, we want the value of instance attributes to be computed on demand >>> s = Link(3, Link(4, Link(5)))

8

For example, if we want to access the second element of a linked list

slide-35
SLIDE 35

Property Methods

In some cases, we want the value of instance attributes to be computed on demand >>> s = Link(3, Link(4, Link(5))) >>> s.second 4

8

For example, if we want to access the second element of a linked list

slide-36
SLIDE 36

Property Methods

In some cases, we want the value of instance attributes to be computed on demand >>> s = Link(3, Link(4, Link(5))) >>> s.second 4 >>> s.second = 6

8

For example, if we want to access the second element of a linked list

slide-37
SLIDE 37

Property Methods

In some cases, we want the value of instance attributes to be computed on demand >>> s = Link(3, Link(4, Link(5))) >>> s.second 4 >>> s.second = 6 >>> s.second 6

8

For example, if we want to access the second element of a linked list

slide-38
SLIDE 38

Property Methods

In some cases, we want the value of instance attributes to be computed on demand >>> s = Link(3, Link(4, Link(5))) >>> s.second 4 >>> s.second = 6 >>> s.second 6 >>> s Link(3, Link(6, Link(5)))

8

For example, if we want to access the second element of a linked list

slide-39
SLIDE 39

Property Methods

In some cases, we want the value of instance attributes to be computed on demand >>> s = Link(3, Link(4, Link(5))) >>> s.second 4 >>> s.second = 6 >>> s.second 6 >>> s Link(3, Link(6, Link(5)))

8

No method calls! For example, if we want to access the second element of a linked list

slide-40
SLIDE 40

Property Methods

In some cases, we want the value of instance attributes to be computed on demand >>> s = Link(3, Link(4, Link(5))) >>> s.second 4 >>> s.second = 6 >>> s.second 6 >>> s Link(3, Link(6, Link(5))) The @property decorator on a method designates that it will be called whenever it is looked up on an instance

8

No method calls! For example, if we want to access the second element of a linked list

slide-41
SLIDE 41

Property Methods

In some cases, we want the value of instance attributes to be computed on demand >>> s = Link(3, Link(4, Link(5))) >>> s.second 4 >>> s.second = 6 >>> s.second 6 >>> s Link(3, Link(6, Link(5))) The @property decorator on a method designates that it will be called whenever it is looked up on an instance A @<attribute>.setter decorator on a method designates that it will be called whenever that attribute is assigned. <attribute> must be an existing property method.

8

No method calls! For example, if we want to access the second element of a linked list

slide-42
SLIDE 42

Property Methods

In some cases, we want the value of instance attributes to be computed on demand >>> s = Link(3, Link(4, Link(5))) >>> s.second 4 >>> s.second = 6 >>> s.second 6 >>> s Link(3, Link(6, Link(5))) The @property decorator on a method designates that it will be called whenever it is looked up on an instance (Demo) A @<attribute>.setter decorator on a method designates that it will be called whenever that attribute is assigned. <attribute> must be an existing property method.

8

No method calls! For example, if we want to access the second element of a linked list

slide-43
SLIDE 43

Tree Class

slide-44
SLIDE 44

Tree Abstraction (Review)

10

2 3 1 1 1 1 1

slide-45
SLIDE 45

Tree Abstraction (Review)

10

Recursive description (wooden trees):

2 3 1 1

Relative description (family trees):

1 1 1

slide-46
SLIDE 46

Tree Abstraction (Review)

10

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-47
SLIDE 47

Tree Abstraction (Review)

10

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-48
SLIDE 48

Tree Abstraction (Review)

10

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-49
SLIDE 49

Tree Abstraction (Review)

10

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-50
SLIDE 50

Tree Abstraction (Review)

10

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-51
SLIDE 51

Tree Abstraction (Review)

10

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-52
SLIDE 52

Tree Abstraction (Review)

10

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-53
SLIDE 53

Tree Abstraction (Review)

10

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-54
SLIDE 54

Tree Abstraction (Review)

10

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-55
SLIDE 55

Tree Abstraction (Review)

10

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-56
SLIDE 56

Tree Abstraction (Review)

10

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-57
SLIDE 57

Tree Abstraction (Review)

10

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-58
SLIDE 58

Tree Abstraction (Review)

10

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-59
SLIDE 59

Tree Abstraction (Review)

10

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-60
SLIDE 60

Tree Abstraction (Review)

10

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-61
SLIDE 61

Tree Abstraction (Review)

10

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-62
SLIDE 62

Tree Abstraction (Review)

10

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-63
SLIDE 63

Tree Abstraction (Review)

10

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-64
SLIDE 64

Tree Class

11

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

slide-65
SLIDE 65

Tree Class

class Tree:

11

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

slide-66
SLIDE 66

Tree Class

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

11

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

slide-67
SLIDE 67

Tree Class

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

11

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

slide-68
SLIDE 68

Tree Class

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

11

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

slide-69
SLIDE 69

Tree Class

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

11

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

slide-70
SLIDE 70

Tree Class

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

11

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-71
SLIDE 71

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

11

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-72
SLIDE 72

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

11

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-73
SLIDE 73

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)

11

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-74
SLIDE 74

Tree Mutation

slide-75
SLIDE 75

Example: Pruning Trees

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

13

slide-76
SLIDE 76

Example: Pruning Trees

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

13

2 3 1 1 1 1 1

slide-77
SLIDE 77

Example: Pruning Trees

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

13

2 3 1 1 1 1 1

slide-78
SLIDE 78

Example: Pruning Trees

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

13

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

2 3 1 1 1 1 1

slide-79
SLIDE 79

Example: Pruning Trees

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

13

def prune(t, n): """Prune sub-trees whose label value 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-80
SLIDE 80

Example: Pruning Trees

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

13

def prune(t, n): """Prune sub-trees whose label value 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

slide-81
SLIDE 81

Example: Pruning Trees

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

13

def prune(t, n): """Prune sub-trees whose label value 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 (Demo)

slide-82
SLIDE 82

Example: Pruning Trees

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

14

fib(5) fib(4) fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1

slide-83
SLIDE 83

Example: Pruning Trees

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

14

fib(5) fib(4) fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1 Memoization:

slide-84
SLIDE 84

Example: Pruning Trees

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

14

Returned by fib

fib(5) fib(4) fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1 Memoization:

slide-85
SLIDE 85

Example: Pruning Trees

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

14

Returned by fib Found in cache

fib(5) fib(4) fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1 Memoization:

slide-86
SLIDE 86

Example: Pruning Trees

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

14

Returned by fib Found in cache

fib(5) fib(4) fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1

Skipped

Memoization:

slide-87
SLIDE 87

Example: Pruning Trees

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

14

Returned by fib Found in cache

fib(5) fib(4) fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1

Skipped

Memoization:

slide-88
SLIDE 88

Example: Pruning Trees

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

14

Returned by fib Found in cache

fib(5) fib(4) fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1

Skipped

Memoization:

slide-89
SLIDE 89

Example: Pruning Trees

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

14

Returned by fib Found in cache

fib(5) fib(4) fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1

Skipped

Memoization:

slide-90
SLIDE 90

Example: Pruning Trees

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

14

Returned by fib Found in cache

fib(5) fib(4) fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1

Skipped

Memoization:

slide-91
SLIDE 91

Example: Pruning Trees

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

14

Returned by fib Found in cache

fib(5) fib(4) fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1

Skipped

Memoization:

slide-92
SLIDE 92

Example: Pruning Trees

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

14

Returned by fib Found in cache

fib(5) fib(4) fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1

Skipped

Memoization:

slide-93
SLIDE 93

Example: Pruning Trees

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

14

Returned by fib Found in cache

fib(5) fib(4) fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1

Skipped

Memoization:

slide-94
SLIDE 94

Example: Pruning Trees

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

14

Returned by fib Found in cache

fib(5) fib(4) fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1

Skipped

Memoization:

slide-95
SLIDE 95

Example: Pruning Trees

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

14

Returned by fib Found in cache

fib(5) fib(4) fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1

Skipped

Memoization:

slide-96
SLIDE 96

Example: Pruning Trees

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

14

Returned by fib Found in cache

fib(5) fib(4) fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1

Skipped

Memoization:

slide-97
SLIDE 97

Example: Pruning Trees

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

14

Returned by fib Found in cache

fib(5) fib(4) fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1

Skipped

Memoization:

slide-98
SLIDE 98

Example: Pruning Trees

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

14

Returned by fib Found in cache

fib(5) fib(4) fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1

Skipped

Memoization:

slide-99
SLIDE 99

Example: Pruning Trees

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

14

Returned by fib Found in cache

fib(5) fib(4) fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1

Skipped

Memoization:

slide-100
SLIDE 100

Example: Pruning Trees

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

14

Returned by fib Found in cache

fib(5) fib(4) fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1

Skipped

Memoization:

slide-101
SLIDE 101

Example: Pruning Trees

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

14

Returned by fib Found in cache

fib(5) fib(4) fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1

Skipped

Memoization:

slide-102
SLIDE 102

Example: Pruning Trees

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

14

Returned by fib Found in cache

fib(5) fib(4) fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(2) fib(0) fib(1) 1 fib(3) fib(1) 1 fib(2) fib(0) fib(1) 1

Skipped

(Demo) Memoization: