cs 10 problem solving via object oriented programming
play

CS 10: Problem solving via Object Oriented Programming - PowerPoint PPT Presentation

CS 10: Problem solving via Object Oriented Programming Winter 2017 Tim Pierson 260 (255) Sudikoff Day 9 Hierarchies Part 2 Agenda 1.


  1. CS ¡10: ¡ Problem ¡solving ¡via ¡Object ¡Oriented ¡ Programming ¡ Winter ¡2017 ¡ ¡ Tim ¡Pierson ¡ 260 ¡(255) ¡Sudikoff ¡ Day ¡9 ¡– ¡Hierarchies ¡Part ¡2 ¡

  2. Agenda ¡ 1. Binary ¡search ¡ ¡ 2. Binary ¡Search ¡Trees ¡(BST) ¡ 3. BST ¡find ¡analysis ¡ 4. OperaQons ¡on ¡BSTs ¡ 5. ImplementaQon ¡ ¡ 2 ¡

  3. Binary ¡search ¡can ¡quickly ¡find ¡items ¡if ¡the ¡ data ¡is ¡ordered ¡ Binary ¡search ¡on ¡an ¡array ¡ 0 ¡ 1 ¡ 2 ¡ 3 ¡ 4 ¡ 5 ¡ 6 ¡ 7 ¡ 8 ¡ Index ¡ Data ¡ 1 ¡ 5 ¡ 9 ¡ 14 ¡ 25 ¡ 53 ¡ 107 ¡ 214 ¡ 512 ¡ Min ¡ Max ¡ Pseudo ¡code ¡ Looking ¡for ¡target ¡= ¡53 ¡ Set ¡min ¡= ¡0, ¡max ¡= ¡n-­‑1 ¡ While ¡(min ¡<= ¡max) ¡{ ¡ idx ¡= ¡(min ¡+ ¡max)/2 ¡ ¡ If ¡array[idx] ¡== ¡target ¡ ¡return ¡idx ¡ ¡ array[idx] ¡> ¡target ¡ max ¡= ¡idx-­‑1 ¡ else ¡ min ¡= ¡idx ¡+1 ¡ 3 ¡ } ¡

  4. At ¡each ¡iteraQon ¡half ¡of ¡the ¡indexes ¡are ¡ eliminated ¡ Binary ¡search ¡on ¡an ¡array ¡ 1 0 ¡ 1 ¡ 2 ¡ 3 ¡ 4 ¡ 5 ¡ 6 ¡ 7 ¡ 8 ¡ Index ¡ Data ¡ 1 ¡ 5 ¡ 9 ¡ 14 ¡ 25 ¡ 53 ¡ 107 ¡ 214 ¡ 512 ¡ Min ¡ Max ¡ Target ¡53 ¡ Pseudo ¡code ¡ Min ¡= ¡0 ¡ Looking ¡for ¡target ¡= ¡53 ¡ Max ¡= ¡8 ¡ Set ¡min ¡= ¡0, ¡max ¡= ¡n-­‑1 ¡ Idx ¡= ¡(0+8)/2 ¡= ¡4 ¡ While ¡(min ¡<= ¡max) ¡{ ¡ Array[idx] ¡= ¡25 ¡ idx ¡= ¡(min ¡+ ¡max)/2 ¡ ¡ If ¡array[idx] ¡== ¡target ¡ ¡return ¡idx ¡ ¡ array[idx] ¡> ¡target ¡ max ¡= ¡idx-­‑1 ¡ else ¡ min ¡= ¡idx ¡+1 ¡ 4 ¡ } ¡

  5. At ¡each ¡iteraQon ¡half ¡of ¡the ¡indexes ¡are ¡ eliminated ¡ Binary ¡search ¡on ¡an ¡array ¡ 1 2 0 ¡ 1 ¡ 2 ¡ 3 ¡ 4 ¡ 5 ¡ 6 ¡ 7 ¡ 8 ¡ Index ¡ Data ¡ 1 ¡ 5 ¡ 9 ¡ 14 ¡ 25 ¡ 53 ¡ 107 ¡ 214 ¡ 512 ¡ Min ¡ Max ¡ Target ¡53 ¡ Pseudo ¡code ¡ Min ¡= ¡5 ¡ Looking ¡for ¡target ¡= ¡53 ¡ Max ¡= ¡8 ¡ Set ¡min ¡= ¡0, ¡max ¡= ¡n-­‑1 ¡ Idx ¡= ¡(5+8)/2 ¡= ¡6 ¡ While ¡(min ¡<= ¡max) ¡{ ¡ Array[idx] ¡= ¡107 ¡ idx ¡= ¡(min ¡+ ¡max)/2 ¡ ¡ If ¡array[idx] ¡== ¡target ¡ ¡return ¡idx ¡ ¡ array[idx] ¡> ¡target ¡ max ¡= ¡idx-­‑1 ¡ else ¡ min ¡= ¡idx ¡+1 ¡ 5 ¡ } ¡

  6. Binary ¡search ¡finds ¡data ¡generally ¡faster ¡ than ¡linear ¡search ¡ Binary ¡search ¡on ¡an ¡array ¡ 1 3 2 0 ¡ 1 ¡ 2 ¡ 3 ¡ 4 ¡ 5 ¡ 6 ¡ 7 ¡ 8 ¡ Index ¡ Data ¡ 1 ¡ 5 ¡ 9 ¡ 14 ¡ 25 ¡ 53 ¡ 107 ¡ 214 ¡ 512 ¡ Min ¡ Max ¡ Target ¡53 ¡ Pseudo ¡code ¡ Min ¡= ¡5 ¡ Looking ¡for ¡target ¡= ¡53 ¡ Max ¡= ¡5 ¡ Set ¡min ¡= ¡0, ¡max ¡= ¡n-­‑1 ¡ Idx ¡= ¡(5+5)/2 ¡= ¡5 ¡ While ¡(min ¡<= ¡max) ¡{ ¡ Array[idx] ¡= ¡53 ¡ idx ¡= ¡(min ¡+ ¡max)/2 ¡ ¡ If ¡array[idx] ¡== ¡target ¡ Binary ¡vs. ¡linear ¡search ¡ ¡return ¡idx ¡ ¡ • Binary ¡found ¡item ¡in ¡3 ¡tries ¡ array[idx] ¡> ¡target ¡ • Linear ¡search ¡would ¡have ¡taken ¡6 ¡tries ¡ max ¡= ¡idx-­‑1 ¡ • On ¡large ¡data ¡sets ¡binary ¡search ¡can ¡make ¡ a ¡ huge ¡difference ¡ else ¡ ¡ min ¡= ¡idx ¡+1 ¡ 6 ¡ } ¡

  7. We ¡can ¡extend ¡binary ¡search ¡to ¡find ¡a ¡key ¡ and ¡return ¡a ¡value ¡ Key ¡is ¡Student ¡ID, ¡Value ¡is ¡student ¡name ¡ 0 ¡ 1 ¡ 2 ¡ 3 ¡ 4 ¡ 5 ¡ 6 ¡ 7 ¡ 8 ¡ Index ¡ Student ¡ID ¡ 1 ¡ 5 ¡ 9 ¡ 14 ¡ 25 ¡ 53 ¡ 107 ¡ 214 ¡ 512 ¡ “Charlie” ¡ … ¡ “Alice” ¡ “Bob” ¡ ImplicaEons ¡ • Given ¡a ¡Student ¡ID, ¡can ¡quickly ¡find ¡the ¡student’s ¡name ¡ • Each ¡entry ¡must ¡have ¡a ¡key ¡and ¡a ¡value ¡ • Value ¡is ¡an ¡object ¡(e.g. ¡String ¡or ¡student ¡record) ¡ • Of ¡course ¡the ¡keys ¡must ¡be ¡sorted ¡ • How ¡do ¡we ¡do ¡that? ¡ 7 ¡

  8. Agenda ¡ 1. Binary ¡search ¡ ¡ 2. Binary ¡Search ¡Trees ¡(BST) ¡ 3. BST ¡find ¡analysis ¡ 4. OperaQons ¡on ¡BSTs ¡ 5. ImplementaQon ¡ ¡ 8 ¡

  9. Binary ¡Search ¡Trees ¡(BSTs) ¡allow ¡for ¡binary ¡ search ¡by ¡keeping ¡keys ¡sorted ¡ Keys ¡sorted ¡in ¡Binary ¡Search ¡Tree ¡ Binary ¡Search ¡Tree ¡property ¡ D ¡ • Let ¡x ¡be ¡a ¡node ¡in ¡a ¡ binary ¡search ¡tree ¡ • len.key ¡< ¡x.key ¡ B ¡ F ¡ • right.key ¡> ¡x.key ¡ • We ¡will ¡assume ¡for ¡now ¡ G ¡ C ¡ E ¡ duplicate ¡keys ¡are ¡not ¡ A ¡ allowed ¡ 9 ¡

  10. BSTs ¡with ¡same ¡keys ¡could ¡have ¡different ¡ structures ¡and ¡sQll ¡obey ¡BST ¡property ¡ Two ¡valid ¡BSTs ¡with ¡same ¡keys ¡but ¡different ¡structure ¡ Tree ¡1 ¡ Tree ¡2 ¡ D ¡ C ¡ B ¡ A ¡ F ¡ E ¡ G ¡ F ¡ C ¡ E ¡ B ¡ D ¡ A ¡ G ¡ 10 ¡

  11. BSTs ¡make ¡searching ¡fast ¡and ¡simple ¡ Find ¡Key ¡“C” ¡ Search ¡process ¡ D ¡ • Check ¡root ¡ • “D” ¡> ¡“C”, ¡so ¡go ¡len ¡ B ¡ F ¡ G ¡ C ¡ E ¡ A ¡ 11 ¡

  12. BSTs ¡make ¡searching ¡fast ¡and ¡simple ¡ Find ¡Key ¡“C” ¡ Search ¡process ¡ D ¡ • Check ¡root ¡ • “D” ¡> ¡“C”, ¡so ¡go ¡len ¡ B ¡ F ¡ • Check ¡“B” ¡ • “B” ¡< ¡“C”, ¡so ¡go ¡right ¡ G ¡ C ¡ E ¡ A ¡ 12 ¡

  13. BSTs ¡make ¡searching ¡fast ¡and ¡simple ¡ Find ¡Key ¡“C” ¡ Search ¡process ¡ D ¡ • Check ¡root ¡ • “D” ¡> ¡“C”, ¡so ¡go ¡len ¡ B ¡ F ¡ • Check ¡“B” ¡ • “B” ¡< ¡“C”, ¡so ¡go ¡right ¡ G ¡ C ¡ E ¡ A ¡ • Check ¡“C” ¡ • Yahtzee! ¡ ¡Found ¡it ¡ 13 ¡

  14. BSTs ¡make ¡searching ¡fast ¡and ¡simple ¡ Find ¡Key ¡“C” ¡ Search ¡process ¡ D ¡ • Check ¡root ¡ • “D” ¡> ¡“C”, ¡so ¡go ¡len ¡ B ¡ F ¡ • Check ¡“B” ¡ • “B” ¡< ¡“C”, ¡so ¡go ¡right ¡ G ¡ C ¡ E ¡ A ¡ • Check ¡“C” ¡ • Yahtzee! ¡ ¡Found ¡it ¡ • Would ¡know ¡by ¡now ¡if ¡ key ¡not ¡in ¡BST ¡ 14 ¡

  15. Agenda ¡ 1. Binary ¡search ¡ ¡ 2. Binary ¡Search ¡Trees ¡(BST) ¡ 3. BST ¡find ¡analysis ¡ 4. OperaQons ¡on ¡BSTs ¡ 5. ImplementaQon ¡ ¡ 15 ¡

  16. BST ¡takes ¡at ¡most ¡ height+1 ¡checks ¡to ¡find ¡ key ¡or ¡determine ¡the ¡key ¡is ¡not ¡in ¡the ¡tree ¡ Find ¡Key ¡“C” ¡ Search ¡process ¡ • Height ¡ h ¡= ¡2 ¡(count ¡ number ¡of ¡edges ¡to ¡leaf) ¡ Height ¡ D ¡ ¡ • Can ¡take ¡no ¡more ¡than ¡ ¡ ¡ ¡ h+1 ¡checks, ¡O(h) ¡ h=2 ¡ B ¡ F ¡ • Can ¡we ¡say ¡anything ¡ G ¡ C ¡ E ¡ A ¡ more ¡specific ¡about ¡ search ¡Qme? ¡ ¡O(log ¡n)? ¡ Careful, ¡it’s ¡a ¡trap! ¡ 16 ¡

  17. BSTs ¡do ¡not ¡have ¡to ¡be ¡balanced! ¡ ¡Can ¡not ¡ make ¡Qght ¡bound ¡assumpQons! ¡ ¡(yet) ¡ Find ¡Key ¡“G” ¡ Search ¡process ¡ Height ¡ • Height ¡ h ¡= ¡6 ¡(count ¡ A ¡ number ¡of ¡edges ¡to ¡leaf) ¡ B ¡ ¡ • Can ¡take ¡no ¡more ¡than ¡ ¡ ¡ ¡ C ¡ h+1 ¡checks, ¡O(h) ¡ D ¡ h=6 ¡ • Soon ¡we ¡will ¡see ¡how ¡to ¡ E ¡ keep ¡trees ¡balanced ¡ F ¡ G ¡ 17 ¡

  18. Agenda ¡ 1. Binary ¡search ¡ ¡ 2. Binary ¡Search ¡Trees ¡(BST) ¡ 3. BST ¡find ¡analysis ¡ 4. OperaQons ¡on ¡BSTs ¡ 5. ImplementaQon ¡ ¡ 18 ¡

  19. InserQng ¡a ¡new ¡key ¡is ¡easy ¡(compared ¡with ¡ sorted ¡array) ¡ InserEng ¡new ¡node ¡with ¡key ¡H ¡ D ¡ B ¡ F ¡ G ¡ C ¡ E ¡ A ¡ ¡ Comments ¡ • Search ¡for ¡key ¡(H) ¡ • If ¡found, ¡replace ¡value ¡ • If ¡hit ¡leaf, ¡add ¡new ¡node ¡as ¡len ¡or ¡right ¡child ¡of ¡leaf ¡ 19 ¡

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