cs 61a discussion 5
play

CS 61A Discussion 5 Trees, Mutation, Box and Pointers, Nonlocal - PowerPoint PPT Presentation

CS 61A Discussion 5 Trees, Mutation, Box and Pointers, Nonlocal Albert Xu Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/ Announcements Trees An anatomical perspective Nodes Branch Root 1 Labels 2 3 6 7 4


  1. CS 61A Discussion 5 Trees, Mutation, Box and Pointers, Nonlocal Albert Xu Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/

  2. Announcements

  3. Trees An anatomical perspective Nodes Branch Root 1 Labels 2 3 6 7 4 5 8 9 Leaves Credit: From Jerry Chen, and based on Prof. DeNero's tree diagram [Fa 16 CS 61A]

  4. What is a Tree? which of these, if any, is a tree? 1 2 3 6 7 4 5 8 9 Credit: From Jerry Chen, and based on Prof. DeNero's tree diagram [Fa 16 CS 61A]

  5. What is a Tree? which of these, if any, is a tree? 1 2 3 6 7 4 5 8 9 Credit: From Jerry Chen, and based on Prof. DeNero's tree diagram [Fa 16 CS 61A]

  6. What is a Tree? which of these, if any, is a tree? 1 2 3 6 7 4 5 8 9 Credit: From Jerry Chen, and based on Prof. DeNero's tree diagram [Fa 16 CS 61A]

  7. What is a Tree? which of these, if any, is a tree? 1 2 3 6 7 4 5 8 9 Credit: From Jerry Chen, and based on Prof. DeNero's tree diagram [Fa 16 CS 61A]

  8. What is a Tree? which of these, if any, is a tree? 1 2 3 6 7 4 5 8 9 Credit: From Jerry Chen, and based on Prof. DeNero's tree diagram [Fa 16 CS 61A]

  9. What is a Tree? which of these, if any, is a tree? 1 2 3 6 7 4 5 8 9 Credit: From Jerry Chen, and based on Prof. DeNero's tree diagram [Fa 16 CS 61A]

  10. What is a Tree? which of these, if any, is a tree? 1 2 3 6 7 4 5 8 9 Credit: From Jerry Chen, and based on Prof. DeNero's tree diagram [Fa 16 CS 61A]

  11. What is a Tree? which of these, if any, is a tree? 1 2 3 6 7 4 5 8 9 Credit: From Jerry Chen, and based on Prof. DeNero's tree diagram [Fa 16 CS 61A]

  12. What is a Tree? which of these, if any, is a tree? 1 2 3 6 7 4 5 8 9 Credit: From Jerry Chen, and based on Prof. DeNero's tree diagram [Fa 16 CS 61A]

  13. What is a Tree? which of these, if any, is a tree? 1 2 3 6 7 4 5 8 9 Credit: From Jerry Chen, and based on Prof. DeNero's tree diagram [Fa 16 CS 61A]

  14. Trees - Designing an ADT • A tree consists of a label, along with a list of branches, each of which are themselves trees. • This is a recursive definition, can you see why? • Leaves are trees too! They just don’t have any branches of their own. 2 4 5

  15. Trees - Designing an ADT • A tree consists of a label, along with a list of branches, each of which are themselves trees. • This is a recursive definition, can you see why? • Leaves are trees too! They just don’t have any branches of their own. Label 2 4 5

  16. Trees - Designing an ADT • A tree consists of a label, along with a list of branches, each of which are themselves trees. • This is a recursive definition, can you see why? • Leaves are trees too! They just don’t have any branches of their own. Label 2 4 5 branch 0

  17. Trees - Designing an ADT • A tree consists of a label, along with a list of branches, each of which are themselves trees. • This is a recursive definition, can you see why? • Leaves are trees too! They just don’t have any branches of their own. Label 2 4 5 branch 0 branch 1

  18. Trees - Designing an ADT • A tree consists of a label, along with a list of branches, each of which are themselves trees. • This is a recursive definition, can you see why? • Leaves are trees too! They just don’t have any branches of their own. Label 2 4 5 branches[0] branches[1]

  19. Trees - Designing an ADT # Constructor def tree(label, branches=[]): for branch in branches: assert is_tree(branch) return [label] + list(branches) # Selectors def label(tree): return tree[0] def branches(tree): return tree[1:] def is_leaf(tree): return not branches(tree)

  20. Box and Pointer Diagrams how do you draw lists on environment diagrams?

  21. Box and Pointer Diagrams how do you draw lists on environment diagrams?

  22. Box and Pointer Diagrams how do you draw lists on environment diagrams?

  23. Box and Pointer Diagrams how do you draw lists on environment diagrams?

  24. Box and Pointer Diagrams how do you draw lists on environment diagrams?

  25. Examining List Slicing Remember list slicing ? <list>[<start>:<stop>] - returns the part of a list between <start>, inclusive, and <stop>, non-inclusive

  26. Examining List Slicing Remember list slicing ? <list>[<start>:<stop>] - returns the part of a list between <start>, inclusive, and <stop>, non-inclusive If I wrote this: >>> a = [1,2,3] >>> b = a[2:]

  27. Examining List Slicing Remember list slicing ? <list>[<start>:<stop>] - returns the part of a list between <start>, inclusive, and <stop>, non-inclusive If I wrote this: >>> a = [1,2,3] >>> b = a[2:] Question: b is the result of a list slice of a . Did this slice mutate a or create a new list?

  28. Examining List Slicing Remember list slicing ? <list>[<start>:<stop>] - returns the part of a list between <start>, inclusive, and <stop>, non-inclusive If I wrote this: >>> a = [1,2,3] >>> b = a[2:] Question: b is the result of a list slice of a . Did this slice mutate a or create a new list? it created a new list - we’re trying to slice out part of an old list, but we don’t want to be changing that old list!

  29. List Mutation the central question - does it mutate or create a new list?

  30. List Mutation the central question - does it mutate or create a new list? For each of the following, decide whether it mutates or creates a new list, i.e. is lst the same or a new list?

  31. List Mutation the central question - does it mutate or create a new list? For each of the following, decide whether it mutates or creates a new list, i.e. is lst the same or a new list? • lst = lst[a:b] -

  32. List Mutation the central question - does it mutate or create a new list? For each of the following, decide whether it mutates or creates a new list, i.e. is lst the same or a new list? • lst = lst[a:b] - a new list, as we said earlier.

  33. List Mutation the central question - does it mutate or create a new list? For each of the following, decide whether it mutates or creates a new list, i.e. is lst the same or a new list? • lst = lst[a:b] - a new list, as we said earlier. • lst = lst + lst2 -

  34. List Mutation the central question - does it mutate or create a new list? For each of the following, decide whether it mutates or creates a new list, i.e. is lst the same or a new list? • lst = lst[a:b] - a new list, as we said earlier. • lst = lst + lst2 - a new list. Adding two lists shouldn’t change the originals, it should make a third(new) one!

  35. List Mutation the central question - does it mutate or create a new list? For each of the following, decide whether it mutates or creates a new list, i.e. is lst the same or a new list? • lst = lst[a:b] - a new list, as we said earlier. • lst = lst + lst2 - a new list. Adding two lists shouldn’t change the originals, it should make a third(new) one! • lst += lst2 -

  36. List Mutation the central question - does it mutate or create a new list? For each of the following, decide whether it mutates or creates a new list, i.e. is lst the same or a new list? • lst = lst[a:b] - a new list, as we said earlier. • lst = lst + lst2 - a new list. Adding two lists shouldn’t change the originals, it should make a third(new) one! • lst += lst2 - the same list. This is slightly di ff erent, and tricky! This syntax implies that we’re trying to add a second list to the first one, so Python just sticks it onto the end of the old list.

  37. List Mutation the central question - does it mutate or create a new list? For each of the following, decide whether it mutates or creates a new list, i.e. is lst the same or a new list? • lst = lst[a:b] - a new list, as we said earlier. • lst = lst + lst2 - a new list. Adding two lists shouldn’t change the originals, it should make a third(new) one! • lst += lst2 - the same list. This is slightly di ff erent, and tricky! This syntax implies that we’re trying to add a second list to the first one, so Python just sticks it onto the end of the old list. • lst = list(lst) -

  38. List Mutation the central question - does it mutate or create a new list? For each of the following, decide whether it mutates or creates a new list, i.e. is lst the same or a new list? • lst = lst[a:b] - a new list, as we said earlier. • lst = lst + lst2 - a new list. Adding two lists shouldn’t change the originals, it should make a third(new) one! • lst += lst2 - the same list. This is slightly di ff erent, and tricky! This syntax implies that we’re trying to add a second list to the first one, so Python just sticks it onto the end of the old list. • lst = list(lst) - a new list. This is specifically because list takes whatever iterable is given and sticks each element into a new list, even if the original iterable was already a list.

  39. Methods that Mutate some more methods that add to a list

  40. Methods that Mutate some more methods that add to a list • lst.append(elem) • Adds elem to the end of lst • Increases list length by ONE always

  41. Methods that Mutate some more methods that add to a list • lst.append(elem) • Adds elem to the end of lst • Increases list length by ONE always • lst.extend(seq) • Adds elements in seq to the end of lst

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