cs61a lecture 26
play

CS61A Lecture 26 Amir Kamil and Hamilton Nguyen UC Berkeley March - PowerPoint PPT Presentation

CS61A Lecture 26 Amir Kamil and Hamilton Nguyen UC Berkeley March 22, 2013 Announcements HW9 out tonight, due 4/3 Ants extra credit due 4/3 See Piazza for submission instructions Data Structure Applications Data


  1. Set Intersection Using Ordered Sequences This algorithm assumes that elements are in order.

  2. Set Intersection Using Ordered Sequences This algorithm assumes that elements are in order. Order of growth?

  3. Set Intersection Using Ordered Sequences This algorithm assumes that elements are in order. Order of growth?

  4. Set Intersection Using Ordered Sequences This algorithm assumes that elements are in order. def intersect_set2(set1, set2): Order of growth?

  5. Set Intersection Using Ordered Sequences This algorithm assumes that elements are in order. def intersect_set2(set1, set2): if empty(set1) or empty(set2): Order of growth?

  6. Set Intersection Using Ordered Sequences This algorithm assumes that elements are in order. def intersect_set2(set1, set2): if empty(set1) or empty(set2): return Rlist.empty Order of growth?

  7. Set Intersection Using Ordered Sequences This algorithm assumes that elements are in order. def intersect_set2(set1, set2): if empty(set1) or empty(set2): return Rlist.empty e1, e2 = set1.first, set2.first Order of growth?

  8. Set Intersection Using Ordered Sequences This algorithm assumes that elements are in order. def intersect_set2(set1, set2): if empty(set1) or empty(set2): return Rlist.empty e1, e2 = set1.first, set2.first if e1 == e2: Order of growth?

  9. Set Intersection Using Ordered Sequences This algorithm assumes that elements are in order. def intersect_set2(set1, set2): if empty(set1) or empty(set2): return Rlist.empty e1, e2 = set1.first, set2.first if e1 == e2: rest = intersect_set2(set1.rest, set2.rest) Order of growth?

  10. Set Intersection Using Ordered Sequences This algorithm assumes that elements are in order. def intersect_set2(set1, set2): if empty(set1) or empty(set2): return Rlist.empty e1, e2 = set1.first, set2.first if e1 == e2: rest = intersect_set2(set1.rest, set2.rest) return Rlist(e1, rest) Order of growth?

  11. Set Intersection Using Ordered Sequences This algorithm assumes that elements are in order. def intersect_set2(set1, set2): if empty(set1) or empty(set2): return Rlist.empty e1, e2 = set1.first, set2.first if e1 == e2: rest = intersect_set2(set1.rest, set2.rest) return Rlist(e1, rest) elif e1 < e2: Order of growth?

  12. Set Intersection Using Ordered Sequences This algorithm assumes that elements are in order. def intersect_set2(set1, set2): if empty(set1) or empty(set2): return Rlist.empty e1, e2 = set1.first, set2.first if e1 == e2: rest = intersect_set2(set1.rest, set2.rest) return Rlist(e1, rest) elif e1 < e2: return intersect_set2(set1.rest, set2) Order of growth?

  13. Set Intersection Using Ordered Sequences This algorithm assumes that elements are in order. def intersect_set2(set1, set2): if empty(set1) or empty(set2): return Rlist.empty e1, e2 = set1.first, set2.first if e1 == e2: rest = intersect_set2(set1.rest, set2.rest) return Rlist(e1, rest) elif e1 < e2: return intersect_set2(set1.rest, set2) elif e2 < e1: Order of growth?

  14. Set Intersection Using Ordered Sequences This algorithm assumes that elements are in order. def intersect_set2(set1, set2): if empty(set1) or empty(set2): return Rlist.empty e1, e2 = set1.first, set2.first if e1 == e2: rest = intersect_set2(set1.rest, set2.rest) return Rlist(e1, rest) elif e1 < e2: return intersect_set2(set1.rest, set2) elif e2 < e1: return intersect_set2(set1, set2.rest) Order of growth?

  15. Tree Sets

  16. Tree Sets Proposal 3: A set is represented as a Tree. Each entry is:

  17. Tree Sets Proposal 3: A set is represented as a Tree. Each entry is: • Larger than all entries in its left branch and

  18. Tree Sets Proposal 3: A set is represented as a Tree. Each entry is: • Larger than all entries in its left branch and • Smaller than all entries in its right branch

  19. Tree Sets Proposal 3: A set is represented as a Tree. Each entry is: • Larger than all entries in its left branch and • Smaller than all entries in its right branch 7 3 9 1 5 11

  20. Tree Sets Proposal 3: A set is represented as a Tree. Each entry is: • Larger than all entries in its left branch and • Smaller than all entries in its right branch 7 3 3 9 1 7 1 5 11 5 9 11

  21. Tree Sets Proposal 3: A set is represented as a Tree. Each entry is: • Larger than all entries in its left branch and • Smaller than all entries in its right branch 7 3 5 3 9 3 9 1 7 1 5 11 5 9 1 7 11 11

  22. Membership in Tree Sets

  23. Membership in Tree Sets Set membership tests traverse the tree

  24. Membership in Tree Sets Set membership tests traverse the tree • The element is either in the left or right sub ‐ branch

  25. Membership in Tree Sets Set membership tests traverse the tree • The element is either in the left or right sub ‐ branch • By focusing on one branch, we reduce the set by about half

  26. Membership in Tree Sets Set membership tests traverse the tree • The element is either in the left or right sub ‐ branch • By focusing on one branch, we reduce the set by about half def set_contains3(s, v):

  27. Membership in Tree Sets Set membership tests traverse the tree • The element is either in the left or right sub ‐ branch • By focusing on one branch, we reduce the set by about half def set_contains3(s, v): if s is None:

  28. Membership in Tree Sets Set membership tests traverse the tree • The element is either in the left or right sub ‐ branch • By focusing on one branch, we reduce the set by about half def set_contains3(s, v): if s is None: return False

  29. Membership in Tree Sets Set membership tests traverse the tree • The element is either in the left or right sub ‐ branch • By focusing on one branch, we reduce the set by about half def set_contains3(s, v): if s is None: return False elif s.entry == v:

  30. Membership in Tree Sets Set membership tests traverse the tree • The element is either in the left or right sub ‐ branch • By focusing on one branch, we reduce the set by about half def set_contains3(s, v): if s is None: return False elif s.entry == v: return True

  31. Membership in Tree Sets Set membership tests traverse the tree • The element is either in the left or right sub ‐ branch • By focusing on one branch, we reduce the set by about half def set_contains3(s, v): if s is None: return False elif s.entry == v: return True elif s.entry < v:

  32. Membership in Tree Sets Set membership tests traverse the tree • The element is either in the left or right sub ‐ branch • By focusing on one branch, we reduce the set by about half def set_contains3(s, v): if s is None: return False elif s.entry == v: return True elif s.entry < v: return set_contains3(s.right, v)

  33. Membership in Tree Sets Set membership tests traverse the tree • The element is either in the left or right sub ‐ branch • By focusing on one branch, we reduce the set by about half def set_contains3(s, v): if s is None: return False elif s.entry == v: return True elif s.entry < v: return set_contains3(s.right, v) elif s.entry > v:

  34. Membership in Tree Sets Set membership tests traverse the tree • The element is either in the left or right sub ‐ branch • By focusing on one branch, we reduce the set by about half def set_contains3(s, v): if s is None: return False elif s.entry == v: return True elif s.entry < v: return set_contains3(s.right, v) elif s.entry > v: return set_contains3(s.left, v)

  35. Membership in Tree Sets Set membership tests traverse the tree • The element is either in the left or right sub ‐ branch • By focusing on one branch, we reduce the set by about half def set_contains3(s, v): if s is None: 5 return False elif s.entry == v: 3 9 return True elif s.entry < v: return set_contains3(s.right, v) 1 7 11 elif s.entry > v: return set_contains3(s.left, v)

  36. Membership in Tree Sets Set membership tests traverse the tree • The element is either in the left or right sub ‐ branch • By focusing on one branch, we reduce the set by about half 9 def set_contains3(s, v): if s is None: 5 return False elif s.entry == v: 3 9 return True elif s.entry < v: return set_contains3(s.right, v) 1 7 11 elif s.entry > v: return set_contains3(s.left, v)

  37. Membership in Tree Sets Set membership tests traverse the tree • The element is either in the left or right sub ‐ branch • By focusing on one branch, we reduce the set by about half 9 def set_contains3(s, v): if s is None: 5 return False elif s.entry == v: 3 9 return True elif s.entry < v: return set_contains3(s.right, v) 1 7 11 elif s.entry > v: return set_contains3(s.left, v) If 9 is in the set, it is in this branch

  38. Membership in Tree Sets Set membership tests traverse the tree • The element is either in the left or right sub ‐ branch • By focusing on one branch, we reduce the set by about half 9 def set_contains3(s, v): if s is None: 5 return False elif s.entry == v: 3 9 return True elif s.entry < v: return set_contains3(s.right, v) 1 7 11 elif s.entry > v: return set_contains3(s.left, v) If 9 is in the set, it is in this branch Order of growth?

  39. Adjoining to a Tree Set

  40. Adjoining to a Tree Set 8 5 3 9 1 7 11

  41. Adjoining to a Tree Set 8 5 3 9 1 7 11 Right!

  42. Adjoining to a Tree Set 8 5 3 9 1 7 11 Right!

  43. Adjoining to a Tree Set 8 8 5 9 3 9 7 11 1 7 11 Right!

  44. Adjoining to a Tree Set 8 8 5 9 3 9 7 11 1 7 11 Right! Left!

  45. Adjoining to a Tree Set 8 8 8 5 9 7 3 9 7 11 1 7 11 Right! Left!

  46. Adjoining to a Tree Set 8 8 8 5 9 7 3 9 7 11 None None 1 7 11 Right! Left!

  47. Adjoining to a Tree Set 8 8 8 5 9 7 3 9 7 11 None None 1 7 11 Right! Left! Right!

  48. Adjoining to a Tree Set 8 8 8 8 5 9 7 None 3 9 7 11 None None 1 7 11 Right! Left! Right!

  49. Adjoining to a Tree Set 8 8 8 8 5 9 7 None 3 9 7 11 None None 1 7 11 Right! Left! Right! Stop!

Recommend


More recommend