CS ¡10: ¡ Problem ¡solving ¡via ¡Object ¡Oriented ¡ Programming ¡
Winter ¡2017 ¡
¡
Tim ¡Pierson ¡
260 ¡(255) ¡Sudikoff ¡
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.
¡
260 ¡(255) ¡Sudikoff ¡
2 ¡
3 ¡
0 ¡ 1 ¡ 2 ¡ 3 ¡ 4 ¡ 5 ¡ 6 ¡ 7 ¡ 8 ¡ 1 ¡ 5 ¡ 9 ¡ 14 ¡ 25 ¡ 53 ¡ 107 ¡ 214 ¡ 512 ¡
Index ¡ Data ¡
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 ¡ } ¡
Min ¡ Max ¡
4 ¡
0 ¡ 1 ¡ 2 ¡ 3 ¡ 4 ¡ 5 ¡ 6 ¡ 7 ¡ 8 ¡ 1 ¡ 5 ¡ 9 ¡ 14 ¡ 25 ¡ 53 ¡ 107 ¡ 214 ¡ 512 ¡
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 ¡ } ¡
Index ¡ Data ¡ Target ¡53 ¡ Min ¡= ¡0 ¡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¡ F ¡ E ¡ G ¡
B ¡is ¡ parent ¡
26 ¡
D ¡ B ¡ A ¡ C ¡ F ¡ E ¡ G ¡
B ¡is ¡ parent ¡
D ¡ B ¡ C ¡ F ¡ E ¡ G ¡
27 ¡
D ¡ B ¡ C ¡ F ¡ E ¡ G ¡
28 ¡
D ¡ B ¡ C ¡ F ¡ E ¡ G ¡
D ¡is ¡ parent ¡
29 ¡
D ¡ B ¡ C ¡ F ¡ E ¡ G ¡
D ¡is ¡ parent ¡
D ¡ C ¡ F ¡ E ¡ G ¡
30 ¡
D ¡ C ¡ F ¡ E ¡ G ¡
31 ¡
D ¡ C ¡ F ¡ E ¡ G ¡
32 ¡
D ¡ C ¡ F ¡ E ¡ G ¡
Found ¡F ¡ Successor ¡is ¡smallest ¡on ¡right ¡(G ¡here) ¡
33 ¡
D ¡ C ¡ F ¡ E ¡ G ¡
Found ¡F ¡ Successor ¡is ¡smallest ¡on ¡right ¡(G ¡here) ¡ Delete ¡successor ¡
34 ¡
D ¡ C ¡ F ¡ E ¡
Found ¡F ¡ Successor ¡is ¡smallest ¡on ¡right ¡(G ¡here) ¡ Delete ¡successor ¡ Replace ¡F ¡value ¡with ¡G ¡key ¡and ¡value ¡
G ¡
35 ¡
36 ¡
37 ¡
¡
38 ¡
39 ¡
40 ¡