composition announcements linked lists linked list
play

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


  1. Property Methods In some cases, we want the value of instance attributes to be computed on demand For example, if we want to access the second element of a linked list >>> s = Link(3, Link(4, Link(5))) >>> s.second 4 >>> s.second = 6 No method >>> s.second calls! 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

  2. Property Methods In some cases, we want the value of instance attributes to be computed on demand For example, if we want to access the second element of a linked list >>> s = Link(3, Link(4, Link(5))) >>> s.second 4 >>> s.second = 6 No method >>> s.second calls! 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

  3. Property Methods In some cases, we want the value of instance attributes to be computed on demand For example, if we want to access the second element of a linked list >>> s = Link(3, Link(4, Link(5))) >>> s.second 4 >>> s.second = 6 No method >>> s.second calls! 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. (Demo) 8

  4. Tree Class

  5. Tree Abstraction (Review) 3 1 2 0 1 1 1 0 1 10

  6. Tree Abstraction (Review) 3 1 2 0 1 1 1 0 1 Recursive description (wooden trees): Relative description (family trees): 10

  7. Tree Abstraction (Review) 3 1 2 0 1 1 1 0 1 Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches 10

  8. Tree Abstraction (Review) Root label 3 1 2 0 1 1 1 0 1 Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches 10

  9. Tree Abstraction (Review) Root label 3 Branch 1 2 0 1 1 1 0 1 Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches 10

  10. Tree Abstraction (Review) Root label 3 Branch 1 2 0 1 1 1 0 1 Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches Each branch is a tree 10

  11. Tree Abstraction (Review) Root label 3 Branch 1 2 (also a tree) 0 1 1 1 0 1 Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches Each branch is a tree 10

  12. Tree Abstraction (Review) Root label 3 Branch 1 2 (also a tree) 0 1 1 1 0 1 Recursive description (wooden trees): Relative description (family 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 10

  13. Tree Abstraction (Review) Root label 3 Branch 1 2 (also a tree) 0 1 1 1 Leaf 0 1 (also a tree) Recursive description (wooden trees): Relative description (family 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 10

  14. Tree Abstraction (Review) Root label 3 Branch 1 2 (also a tree) 0 1 1 1 Leaf 0 1 (also a tree) Recursive description (wooden trees): Relative description (family 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 10

  15. Tree Abstraction (Review) Root of the whole tree Root label 3 Branch 1 2 (also a tree) 0 1 1 1 Leaf 0 1 (also a tree) Recursive description (wooden trees): Relative description (family 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 10

  16. Tree Abstraction (Review) Root of the whole tree Root label 3 Root of a branch Branch 1 2 (also a tree) 0 1 1 1 Leaf 0 1 (also a tree) Recursive description (wooden trees): Relative description (family 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 10

  17. Tree Abstraction (Review) Root of the whole tree Nodes Root label 3 Root of a branch Branch 1 2 (also a tree) 0 1 1 1 Leaf 0 1 (also a tree) Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches Each location in a tree is called a node Each branch is a tree A tree with zero branches is called a leaf A tree starts at the root 10

  18. Tree Abstraction (Review) Root of the whole tree Nodes Root label 3 Root of a branch Branch 1 2 (also a tree) 0 1 1 1 Leaf 0 1 (also a tree) Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches Each location in a tree is called a node Each branch is a tree Each node has a label that can be any value A tree with zero branches is called a leaf A tree starts at the root 10

  19. Tree Abstraction (Review) Root of the whole tree Nodes Root label 3 Labels Root of a branch Branch 1 2 (also a tree) 0 1 1 1 Leaf 0 1 (also a tree) Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches Each location in a tree is called a node Each branch is a tree Each node has a label that can be any value A tree with zero branches is called a leaf A tree starts at the root 10

  20. Tree Abstraction (Review) Root of the whole tree Nodes Root label 3 Labels Root of a branch Branch 1 2 (also a tree) 0 1 1 1 Leaf 0 1 (also a tree) Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches Each location in a tree is called a node Each branch is a tree Each node has a label that can be any value A tree with zero branches is called a leaf One node can be the parent / child of another A tree starts at the root 10

  21. Tree Abstraction (Review) Root of the whole tree Nodes Root label 3 Labels Root of a branch Branch 1 2 (also a tree) 0 1 1 1 Leaf 0 1 (also a tree) Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches Each location in a tree is called a node Each branch is a tree Each node has a label that can be any value A tree with zero branches is called a leaf One node can be the parent / child of another A tree starts at the root The top node is the root node 10

  22. Tree Abstraction (Review) or Root Node Root of the whole tree Nodes Root label 3 Labels Root of a branch Branch 1 2 (also a tree) 0 1 1 1 Leaf 0 1 (also a tree) Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches Each location in a tree is called a node Each branch is a tree Each node has a label that can be any value A tree with zero branches is called a leaf One node can be the parent / child of another A tree starts at the root The top node is the root node 10

  23. Tree Abstraction (Review) or Root Node Root of the whole tree Nodes Root label 3 Labels Root of a branch Branch 1 2 (also a tree) 0 1 1 1 Leaf 0 1 (also a tree) Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches Each location in a tree is called a node Each branch is a tree Each node has a label that can be any value A tree with zero branches is called a leaf One node can be the parent / child of another A tree starts at the root The top node is the root node People often refer to labels by their locations: "each parent is the sum of its children" 10

  24. Tree Abstraction (Review) or Root Node Root of the whole tree Nodes Root label 3 Labels Root of a branch Branch 1 2 (also a tree) 0 1 1 1 Leaf 0 1 (also a tree) Path Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches Each location in a tree is called a node Each branch is a tree Each node has a label that can be any value A tree with zero branches is called a leaf One node can be the parent / child of another A tree starts at the root The top node is the root node People often refer to labels by their locations: "each parent is the sum of its children" 10

  25. Tree Class A Tree has a label and a list of branches; each branch is a Tree 11

  26. Tree Class A Tree has a label and a list of branches; each branch is a Tree class Tree: 11

  27. Tree Class A Tree has a label and a list of branches; each branch is a Tree class Tree: def __init__(self, label, branches=[]): 11

  28. Tree Class A Tree has a label and a list of branches; each branch is a Tree class Tree: def __init__(self, label, branches=[]): self.label = label 11

  29. Tree Class A Tree has a label and a list of branches; each branch is a Tree class Tree: def __init__(self, label, branches=[]): self.label = label for branch in branches: assert isinstance(branch, Tree) 11

  30. Tree Class A Tree has a label and a list of branches; each branch is a Tree class Tree: def __init__(self, label, branches=[]): self.label = label for branch in branches: assert isinstance(branch, Tree) self.branches = list(branches) 11

  31. Tree Class A Tree has a label and a list of branches; each branch is a Tree class Tree: def tree(label, branches=[]): def __init__(self, label, branches=[]): for branch in branches: self.label = label assert is_tree(branch) for branch in branches: return [label] + list(branches) assert isinstance(branch, Tree) def label(tree): self.branches = list(branches) return tree[0] def branches(tree): return tree[1:] 11

  32. Tree Class A Tree has a label and a list of branches; each branch is a Tree class Tree: def tree(label, branches=[]): def __init__(self, label, branches=[]): for branch in branches: self.label = label assert is_tree(branch) for branch in branches: return [label] + list(branches) assert isinstance(branch, Tree) def label(tree): self.branches = list(branches) return tree[0] def branches(tree): return tree[1:] 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

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

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

  35. Tree Mutation

  36. Example: Pruning Trees Removing subtrees from a tree is called pruning Prune branches before recursive processing 13

  37. Example: Pruning Trees 3 Removing subtrees from a tree is called pruning Prune branches before 1 2 recursive processing 0 1 1 1 0 1 13

  38. Example: Pruning Trees 3 Removing subtrees from a tree is called pruning Prune branches before 1 2 recursive processing 0 1 1 1 0 1 13

  39. Example: Pruning Trees 3 Removing subtrees from a tree is called pruning Prune branches before 1 2 recursive processing 0 1 1 1 0 1 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(_______________________________, _______________________________) 13

  40. Example: Pruning Trees 3 Removing subtrees from a tree is called pruning Prune branches before 1 2 recursive processing 0 1 1 1 0 1 def prune(t, n): """Prune sub-trees whose label value is n.""" b b.label != n t.branches = [______________ for b in t.branches if _____________________] for b in t.branches: prune(_______________________________, _______________________________) 13

  41. Example: Pruning Trees 3 Removing subtrees from a tree is called pruning Prune branches before 1 2 recursive processing 0 1 1 1 0 1 def prune(t, n): """Prune sub-trees whose label value is n.""" b b.label != n t.branches = [______________ for b in t.branches if _____________________] for b in t.branches: b n prune(_______________________________, _______________________________) 13

  42. Example: Pruning Trees 3 Removing subtrees from a tree is called pruning Prune branches before 1 2 recursive processing 0 1 1 1 0 1 def prune(t, n): """Prune sub-trees whose label value is n.""" b b.label != n t.branches = [______________ for b in t.branches if _____________________] for b in t.branches: b n prune(_______________________________, _______________________________) (Demo) 13

  43. Example: Pruning Trees Removing subtrees from a tree is called pruning fib(5) Prune branches before recursive processing fib(3) fib(4) fib(1) fib(2) fib(2) fib(3) fib(0) fib(1) 1 fib(0) fib(1) fib(1) fib(2) 0 1 fib(0) fib(1) 0 1 1 0 1 14

  44. Example: Pruning Trees Removing subtrees from a tree is called pruning fib(5) Prune branches before recursive processing fib(3) fib(4) fib(1) fib(2) fib(2) fib(3) Memoization : fib(0) fib(1) 1 fib(0) fib(1) fib(1) fib(2) 0 1 fib(0) fib(1) 0 1 1 0 1 14

  45. Example: Pruning Trees Removing subtrees from a tree is called pruning fib(5) Prune branches before recursive processing fib(3) fib(4) fib(1) fib(2) fib(2) fib(3) Memoization : fib(0) fib(1) 1 Returned by fib fib(0) fib(1) fib(1) fib(2) 0 1 fib(0) fib(1) 0 1 1 0 1 14

  46. Example: Pruning Trees Removing subtrees from a tree is called pruning fib(5) Prune branches before recursive processing fib(3) fib(4) fib(1) fib(2) fib(2) fib(3) Memoization : fib(0) fib(1) 1 Returned by fib fib(0) fib(1) fib(1) fib(2) 0 1 Found in cache fib(0) fib(1) 0 1 1 0 1 14

  47. Example: Pruning Trees Removing subtrees from a tree is called pruning fib(5) Prune branches before recursive processing fib(3) fib(4) fib(1) fib(2) fib(2) fib(3) Memoization : fib(0) fib(1) 1 Returned by fib fib(0) fib(1) fib(1) fib(2) 0 1 Found in cache fib(0) fib(1) 0 1 1 Skipped 0 1 14

  48. Example: Pruning Trees Removing subtrees from a tree is called pruning fib(5) Prune branches before recursive processing fib(3) fib(4) fib(1) fib(2) fib(2) fib(3) Memoization : fib(0) fib(1) 1 Returned by fib fib(0) fib(1) fib(1) fib(2) 0 1 Found in cache fib(0) fib(1) 0 1 1 Skipped 0 1 14

  49. Example: Pruning Trees Removing subtrees from a tree is called pruning fib(5) Prune branches before recursive processing fib(3) fib(4) fib(1) fib(2) fib(2) fib(3) Memoization : fib(0) fib(1) 1 Returned by fib fib(0) fib(1) fib(1) fib(2) 0 1 Found in cache fib(0) fib(1) 0 1 1 Skipped 0 1 14

  50. Example: Pruning Trees Removing subtrees from a tree is called pruning fib(5) Prune branches before recursive processing fib(3) fib(4) fib(1) fib(2) fib(2) fib(3) Memoization : fib(0) fib(1) 1 Returned by fib fib(0) fib(1) fib(1) fib(2) 0 1 Found in cache fib(0) fib(1) 0 1 1 Skipped 0 1 14

  51. Example: Pruning Trees Removing subtrees from a tree is called pruning fib(5) Prune branches before recursive processing fib(3) fib(4) fib(1) fib(2) fib(2) fib(3) Memoization : fib(0) fib(1) 1 Returned by fib fib(0) fib(1) fib(1) fib(2) 0 1 Found in cache fib(0) fib(1) 0 1 1 Skipped 0 1 14

  52. Example: Pruning Trees Removing subtrees from a tree is called pruning fib(5) Prune branches before recursive processing fib(3) fib(4) fib(1) fib(2) fib(2) fib(3) Memoization : fib(0) fib(1) 1 Returned by fib fib(0) fib(1) fib(1) fib(2) 0 1 Found in cache fib(0) fib(1) 0 1 1 Skipped 0 1 14

  53. Example: Pruning Trees Removing subtrees from a tree is called pruning fib(5) Prune branches before recursive processing fib(3) fib(4) fib(1) fib(2) fib(2) fib(3) Memoization : fib(0) fib(1) 1 Returned by fib fib(0) fib(1) fib(1) fib(2) 0 1 Found in cache fib(0) fib(1) 0 1 1 Skipped 0 1 14

  54. Example: Pruning Trees Removing subtrees from a tree is called pruning fib(5) Prune branches before recursive processing fib(3) fib(4) fib(1) fib(2) fib(2) fib(3) Memoization : fib(0) fib(1) 1 Returned by fib fib(0) fib(1) fib(1) fib(2) 0 1 Found in cache fib(0) fib(1) 0 1 1 Skipped 0 1 14

  55. Example: Pruning Trees Removing subtrees from a tree is called pruning fib(5) Prune branches before recursive processing fib(3) fib(4) fib(1) fib(2) fib(2) fib(3) Memoization : fib(0) fib(1) 1 Returned by fib fib(0) fib(1) fib(1) fib(2) 0 1 Found in cache fib(0) fib(1) 0 1 1 Skipped 0 1 14

  56. Example: Pruning Trees Removing subtrees from a tree is called pruning fib(5) Prune branches before recursive processing fib(3) fib(4) fib(1) fib(2) fib(2) fib(3) Memoization : fib(0) fib(1) 1 Returned by fib fib(0) fib(1) fib(1) fib(2) 0 1 Found in cache fib(0) fib(1) 0 1 1 Skipped 0 1 14

  57. Example: Pruning Trees Removing subtrees from a tree is called pruning fib(5) Prune branches before recursive processing fib(3) fib(4) fib(1) fib(2) fib(2) fib(3) Memoization : fib(0) fib(1) 1 Returned by fib fib(0) fib(1) fib(1) fib(2) 0 1 Found in cache fib(0) fib(1) 0 1 1 Skipped 0 1 14

  58. Example: Pruning Trees Removing subtrees from a tree is called pruning fib(5) Prune branches before recursive processing fib(3) fib(4) fib(1) fib(2) fib(2) fib(3) Memoization : fib(0) fib(1) 1 Returned by fib fib(0) fib(1) fib(1) fib(2) 0 1 Found in cache fib(0) fib(1) 0 1 1 Skipped 0 1 14

  59. Example: Pruning Trees Removing subtrees from a tree is called pruning fib(5) Prune branches before recursive processing fib(3) fib(4) fib(1) fib(2) fib(2) fib(3) Memoization : fib(0) fib(1) 1 Returned by fib fib(0) fib(1) fib(1) fib(2) 0 1 Found in cache fib(0) fib(1) 0 1 1 Skipped 0 1 14

  60. Example: Pruning Trees Removing subtrees from a tree is called pruning fib(5) Prune branches before recursive processing fib(3) fib(4) fib(1) fib(2) fib(2) fib(3) Memoization : fib(0) fib(1) 1 Returned by fib fib(0) fib(1) fib(1) fib(2) 0 1 Found in cache fib(0) fib(1) 0 1 1 Skipped 0 1 14

  61. Example: Pruning Trees Removing subtrees from a tree is called pruning fib(5) Prune branches before recursive processing fib(3) fib(4) fib(1) fib(2) fib(2) fib(3) Memoization : fib(0) fib(1) 1 Returned by fib fib(0) fib(1) fib(1) fib(2) 0 1 Found in cache fib(0) fib(1) 0 1 1 Skipped 0 1 14

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend