61a lecture 23
play

61A Lecture 23 Friday, October 19 Trees with Internal Node Values - PowerPoint PPT Presentation

Last day of Midterm 2 Material 61A Lecture 23 Friday, October 19 Trees with Internal Node Values 2 Trees with Internal Node Values Trees can have values at their roots as well as their leaves. 2 Trees with Internal Node Values Trees can


  1. Last day of Midterm 2 Material 61A Lecture 23 Friday, October 19

  2. Trees with Internal Node Values 2

  3. Trees with Internal Node Values Trees can have values at their roots as well as their leaves. 2

  4. Trees with Internal Node Values Trees can have values at their roots as well as their leaves. fib(6) fib(4) fib(5) fib(2) fib(3) fib(3) fib(4) fib(1) fib(2) 1 fib(1) fib(2) fib(2) fib(3) 0 1 fib(1) fib(2) 0 1 1 0 1 2

  5. Trees with Internal Node Values (Entries) Trees need not only have values at their leaves. A valid tree cannot be a subtree of itself (no cycles!) 3

  6. Trees with Internal Node Values (Entries) Trees need not only have values at their leaves. class Tree(object): A valid tree cannot be a subtree of itself (no cycles!) 3

  7. Trees with Internal Node Values (Entries) Trees need not only have values at their leaves. class Tree(object): def __init__(self, entry, left=None, right=None): A valid tree cannot be a subtree of itself (no cycles!) 3

  8. Trees with Internal Node Values (Entries) Trees need not only have values at their leaves. class Tree(object): def __init__(self, entry, left=None, right=None): self.entry = entry A valid tree cannot be a subtree of itself (no cycles!) 3

  9. Trees with Internal Node Values (Entries) Trees need not only have values at their leaves. class Tree(object): def __init__(self, entry, left=None, right=None): self.entry = entry self.left = left A valid tree cannot be a subtree of itself (no cycles!) 3

  10. Trees with Internal Node Values (Entries) Trees need not only have values at their leaves. class Tree(object): def __init__(self, entry, left=None, right=None): self.entry = entry self.left = left self.right = right A valid tree cannot be a subtree of itself (no cycles!) 3

  11. Trees with Internal Node Values (Entries) Trees need not only have values at their leaves. class Tree(object): def __init__(self, entry, left=None, right=None): self.entry = entry self.left = left Valid if left and right are each either None or self.right = right a Tree instance A valid tree cannot be a subtree of itself (no cycles!) 3

  12. Trees with Internal Node Values (Entries) Trees need not only have values at their leaves. class Tree(object): def __init__(self, entry, left=None, right=None): self.entry = entry self.left = left Valid if left and right are each either None or self.right = right a Tree instance def fib_tree(n): A valid tree cannot be a subtree of itself (no cycles!) 3

  13. Trees with Internal Node Values (Entries) Trees need not only have values at their leaves. class Tree(object): def __init__(self, entry, left=None, right=None): self.entry = entry self.left = left Valid if left and right are each either None or self.right = right a Tree instance def fib_tree(n): A valid tree cannot if n == 1: be a subtree of itself (no cycles!) 3

  14. Trees with Internal Node Values (Entries) Trees need not only have values at their leaves. class Tree(object): def __init__(self, entry, left=None, right=None): self.entry = entry self.left = left Valid if left and right are each either None or self.right = right a Tree instance def fib_tree(n): A valid tree cannot if n == 1: be a subtree of return Tree(0) itself (no cycles!) 3

  15. Trees with Internal Node Values (Entries) Trees need not only have values at their leaves. class Tree(object): def __init__(self, entry, left=None, right=None): self.entry = entry self.left = left Valid if left and right are each either None or self.right = right a Tree instance def fib_tree(n): A valid tree cannot if n == 1: be a subtree of return Tree(0) itself (no cycles!) if n == 2: 3

  16. Trees with Internal Node Values (Entries) Trees need not only have values at their leaves. class Tree(object): def __init__(self, entry, left=None, right=None): self.entry = entry self.left = left Valid if left and right are each either None or self.right = right a Tree instance def fib_tree(n): A valid tree cannot if n == 1: be a subtree of return Tree(0) itself (no cycles!) if n == 2: return Tree(1) 3

  17. Trees with Internal Node Values (Entries) Trees need not only have values at their leaves. class Tree(object): def __init__(self, entry, left=None, right=None): self.entry = entry self.left = left Valid if left and right are each either None or self.right = right a Tree instance def fib_tree(n): A valid tree cannot if n == 1: be a subtree of return Tree(0) itself (no cycles!) if n == 2: return Tree(1) left = fib_tree(n-2) 3

  18. Trees with Internal Node Values (Entries) Trees need not only have values at their leaves. class Tree(object): def __init__(self, entry, left=None, right=None): self.entry = entry self.left = left Valid if left and right are each either None or self.right = right a Tree instance def fib_tree(n): A valid tree cannot if n == 1: be a subtree of return Tree(0) itself (no cycles!) if n == 2: return Tree(1) left = fib_tree(n-2) right = fib_tree(n-1) 3

  19. Trees with Internal Node Values (Entries) Trees need not only have values at their leaves. class Tree(object): def __init__(self, entry, left=None, right=None): self.entry = entry self.left = left Valid if left and right are each either None or self.right = right a Tree instance def fib_tree(n): A valid tree cannot if n == 1: be a subtree of return Tree(0) itself (no cycles!) if n == 2: return Tree(1) left = fib_tree(n-2) right = fib_tree(n-1) return Tree(left.entry + right.entry, left, right) 3

  20. Trees with Internal Node Values (Entries) Trees need not only have values at their leaves. class Tree(object): def __init__(self, entry, left=None, right=None): self.entry = entry self.left = left Valid if left and right are each either None or self.right = right a Tree instance def fib_tree(n): A valid tree cannot if n == 1: be a subtree of return Tree(0) itself (no cycles!) if n == 2: return Tree(1) left = fib_tree(n-2) Demo right = fib_tree(n-1) return Tree(left.entry + right.entry, left, right) 3

  21. The Consumption of Time Implementations of the same functional abstraction can require different amounts of time to compute their result. 4

  22. The Consumption of Time Implementations of the same functional abstraction can require different amounts of time to compute their result. def count_factors(n): 4

  23. The Consumption of Time Implementations of the same functional abstraction can require different amounts of time to compute their result. (Demo) def count_factors(n): 4

  24. The Consumption of Time Implementations of the same functional abstraction can require different amounts of time to compute their result. Time (remainders) (Demo) def count_factors(n): 4

  25. The Consumption of Time Implementations of the same functional abstraction can require different amounts of time to compute their result. Time (remainders) (Demo) def count_factors(n): factors = 0 for k in range(1, n+1): if n % k == 0: factors += 1 return factors 4

  26. The Consumption of Time Implementations of the same functional abstraction can require different amounts of time to compute their result. Time (remainders) (Demo) def count_factors(n): factors = 0 for k in range(1, n+1): if n % k == 0: n factors += 1 return factors 4

  27. The Consumption of Time Implementations of the same functional abstraction can require different amounts of time to compute their result. Time (remainders) (Demo) def count_factors(n): factors = 0 for k in range(1, n+1): if n % k == 0: n factors += 1 return factors 4

  28. The Consumption of Time Implementations of the same functional abstraction can require different amounts of time to compute their result. Time (remainders) (Demo) def count_factors(n): factors = 0 for k in range(1, n+1): if n % k == 0: n factors += 1 return factors sqrt_n = sqrt(n) k, factors = 1, 0 while k < sqrt_n: if n % k == 0: factors += 2 k += 1 if k * k == n: factors += 1 return factors 4

  29. The Consumption of Time Implementations of the same functional abstraction can require different amounts of time to compute their result. Time (remainders) (Demo) def count_factors(n): factors = 0 for k in range(1, n+1): if n % k == 0: n factors += 1 return factors sqrt_n = sqrt(n) k, factors = 1, 0 while k < sqrt_n: bp n c if n % k == 0: factors += 2 k += 1 if k * k == n: factors += 1 return factors 4

  30. The Consumption of Space 5

  31. The Consumption of Space Which environment frames do we need to keep during evaluation? 5

  32. The Consumption of Space Which environment frames do we need to keep during evaluation? Each step of evaluation has a set of active environments. 5

  33. The Consumption of Space Which environment frames do we need to keep during evaluation? Each step of evaluation has a set of active environments. Values and frames in active environments consume memory. 5

  34. The Consumption of Space Which environment frames do we need to keep during evaluation? Each step of evaluation has a set of active environments. Values and frames in active environments consume memory. Memory used for other values and frames can be reclaimed. 5

Recommend


More recommend