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

cs 10 problem solving via object oriented programming
SMART_READER_LITE
LIVE PREVIEW

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.


slide-1
SLIDE 1

CS ¡10: ¡ Problem ¡solving ¡via ¡Object ¡Oriented ¡ Programming ¡

Winter ¡2017 ¡

¡

Tim ¡Pierson ¡

260 ¡(255) ¡Sudikoff ¡

Day ¡9 ¡– ¡Hierarchies ¡Part ¡2 ¡

slide-2
SLIDE 2

2 ¡

Agenda ¡

  • 1. Binary ¡search ¡

¡

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

¡

slide-3
SLIDE 3

3 ¡

Binary ¡search ¡can ¡quickly ¡find ¡items ¡if ¡the ¡ data ¡is ¡ordered ¡

0 ¡ 1 ¡ 2 ¡ 3 ¡ 4 ¡ 5 ¡ 6 ¡ 7 ¡ 8 ¡ 1 ¡ 5 ¡ 9 ¡ 14 ¡ 25 ¡ 53 ¡ 107 ¡ 214 ¡ 512 ¡

Binary ¡search ¡on ¡an ¡array ¡

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 ¡

slide-4
SLIDE 4

4 ¡

At ¡each ¡iteraQon ¡half ¡of ¡the ¡indexes ¡are ¡ eliminated ¡

0 ¡ 1 ¡ 2 ¡ 3 ¡ 4 ¡ 5 ¡ 6 ¡ 7 ¡ 8 ¡ 1 ¡ 5 ¡ 9 ¡ 14 ¡ 25 ¡ 53 ¡ 107 ¡ 214 ¡ 512 ¡

Binary ¡search ¡on ¡an ¡array ¡

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 ¡ Max ¡= ¡8 ¡ Idx ¡= ¡(0+8)/2 ¡= ¡4 ¡ Array[idx] ¡= ¡25 ¡ 1

Min ¡ Max ¡

slide-5
SLIDE 5

5 ¡

At ¡each ¡iteraQon ¡half ¡of ¡the ¡indexes ¡are ¡ eliminated ¡

0 ¡ 1 ¡ 2 ¡ 3 ¡ 4 ¡ 5 ¡ 6 ¡ 7 ¡ 8 ¡ 1 ¡ 5 ¡ 9 ¡ 14 ¡ 25 ¡ 53 ¡ 107 ¡ 214 ¡ 512 ¡

Binary ¡search ¡on ¡an ¡array ¡

Index ¡ Data ¡ Target ¡53 ¡ Min ¡= ¡5 ¡ Max ¡= ¡8 ¡ Idx ¡= ¡(5+8)/2 ¡= ¡6 ¡ Array[idx] ¡= ¡107 ¡ 1 2

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 ¡

slide-6
SLIDE 6

6 ¡

Binary ¡search ¡finds ¡data ¡generally ¡faster ¡ than ¡linear ¡search ¡

0 ¡ 1 ¡ 2 ¡ 3 ¡ 4 ¡ 5 ¡ 6 ¡ 7 ¡ 8 ¡ 1 ¡ 5 ¡ 9 ¡ 14 ¡ 25 ¡ 53 ¡ 107 ¡ 214 ¡ 512 ¡

Binary ¡search ¡on ¡an ¡array ¡

Index ¡ Data ¡ Target ¡53 ¡ Min ¡= ¡5 ¡ Max ¡= ¡5 ¡ Idx ¡= ¡(5+5)/2 ¡= ¡5 ¡ Array[idx] ¡= ¡53 ¡ 1 2 3

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 ¡

Binary ¡vs. ¡linear ¡search ¡

  • Binary ¡found ¡item ¡in ¡3 ¡tries ¡
  • Linear ¡search ¡would ¡have ¡taken ¡6 ¡tries ¡
  • On ¡large ¡data ¡sets ¡binary ¡search ¡can ¡make ¡

a ¡huge ¡difference ¡ ¡

slide-7
SLIDE 7

7 ¡

We ¡can ¡extend ¡binary ¡search ¡to ¡find ¡a ¡key ¡ and ¡return ¡a ¡value ¡

0 ¡ 1 ¡ 2 ¡ 3 ¡ 4 ¡ 5 ¡ 6 ¡ 7 ¡ 8 ¡ 1 ¡ 5 ¡ 9 ¡ 14 ¡ 25 ¡ 53 ¡ 107 ¡ 214 ¡ 512 ¡ Index ¡ Student ¡ID ¡ “Alice” ¡ “Bob” ¡ “Charlie” ¡ … ¡

Key ¡is ¡Student ¡ID, ¡Value ¡is ¡student ¡name ¡ 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? ¡
slide-8
SLIDE 8

8 ¡

Agenda ¡

  • 1. Binary ¡search ¡

¡

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

¡

slide-9
SLIDE 9

9 ¡

Binary ¡Search ¡Trees ¡(BSTs) ¡allow ¡for ¡binary ¡ search ¡by ¡keeping ¡keys ¡sorted ¡

D ¡ B ¡ A ¡ C ¡ F ¡ E ¡ G ¡

Keys ¡sorted ¡in ¡Binary ¡Search ¡Tree ¡ Binary ¡Search ¡Tree ¡property ¡

  • Let ¡x ¡be ¡a ¡node ¡in ¡a ¡

binary ¡search ¡tree ¡

  • len.key ¡< ¡x.key ¡
  • right.key ¡> ¡x.key ¡
  • We ¡will ¡assume ¡for ¡now ¡

duplicate ¡keys ¡are ¡not ¡ allowed ¡

slide-10
SLIDE 10

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 ¡ B ¡ A ¡ C ¡ F ¡ E ¡ G ¡ C ¡ A ¡ B ¡ E ¡ D ¡ F ¡ G ¡

slide-11
SLIDE 11

11 ¡

BSTs ¡make ¡searching ¡fast ¡and ¡simple ¡

D ¡ B ¡ A ¡ C ¡ F ¡ E ¡ G ¡

Find ¡Key ¡“C” ¡ Search ¡process ¡

  • Check ¡root ¡
  • “D” ¡> ¡“C”, ¡so ¡go ¡len ¡
slide-12
SLIDE 12

12 ¡

BSTs ¡make ¡searching ¡fast ¡and ¡simple ¡

D ¡ B ¡ A ¡ C ¡ F ¡ E ¡ G ¡

Find ¡Key ¡“C” ¡

  • Check ¡“B” ¡
  • “B” ¡< ¡“C”, ¡so ¡go ¡right ¡

Search ¡process ¡

  • Check ¡root ¡
  • “D” ¡> ¡“C”, ¡so ¡go ¡len ¡
slide-13
SLIDE 13

13 ¡

BSTs ¡make ¡searching ¡fast ¡and ¡simple ¡

D ¡ B ¡ A ¡ C ¡ F ¡ E ¡ G ¡

Find ¡Key ¡“C” ¡

  • Check ¡“B” ¡
  • “B” ¡< ¡“C”, ¡so ¡go ¡right ¡
  • Check ¡“C” ¡
  • Yahtzee! ¡ ¡Found ¡it ¡

Search ¡process ¡

  • Check ¡root ¡
  • “D” ¡> ¡“C”, ¡so ¡go ¡len ¡
slide-14
SLIDE 14

14 ¡

BSTs ¡make ¡searching ¡fast ¡and ¡simple ¡

D ¡ B ¡ A ¡ C ¡ F ¡ E ¡ G ¡

Find ¡Key ¡“C” ¡

  • Check ¡“B” ¡
  • “B” ¡< ¡“C”, ¡so ¡go ¡right ¡
  • Check ¡“C” ¡
  • Yahtzee! ¡ ¡Found ¡it ¡
  • Would ¡know ¡by ¡now ¡if ¡

key ¡not ¡in ¡BST ¡ Search ¡process ¡

  • Check ¡root ¡
  • “D” ¡> ¡“C”, ¡so ¡go ¡len ¡
slide-15
SLIDE 15

15 ¡

Agenda ¡

  • 1. Binary ¡search ¡

¡

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

¡

slide-16
SLIDE 16

16 ¡

BST ¡takes ¡at ¡most ¡height+1 ¡checks ¡to ¡find ¡ key ¡or ¡determine ¡the ¡key ¡is ¡not ¡in ¡the ¡tree ¡

D ¡ B ¡ A ¡ C ¡ F ¡ E ¡ G ¡

Find ¡Key ¡“C” ¡ Search ¡process ¡

  • Height ¡h ¡= ¡2 ¡(count ¡

number ¡of ¡edges ¡to ¡leaf) ¡ ¡

  • Can ¡take ¡no ¡more ¡than ¡ ¡ ¡ ¡

h+1 ¡checks, ¡O(h) ¡

  • Can ¡we ¡say ¡anything ¡

more ¡specific ¡about ¡ search ¡Qme? ¡ ¡O(log ¡n)? ¡ Careful, ¡it’s ¡a ¡trap! ¡

h=2 ¡ Height ¡

slide-17
SLIDE 17

17 ¡

BSTs ¡do ¡not ¡have ¡to ¡be ¡balanced! ¡ ¡Can ¡not ¡ make ¡Qght ¡bound ¡assumpQons! ¡ ¡(yet) ¡

A ¡ B ¡

Find ¡Key ¡“G” ¡ Search ¡process ¡

  • Height ¡h ¡= ¡6 ¡(count ¡

number ¡of ¡edges ¡to ¡leaf) ¡ ¡

  • Can ¡take ¡no ¡more ¡than ¡ ¡ ¡ ¡

h+1 ¡checks, ¡O(h) ¡

  • Soon ¡we ¡will ¡see ¡how ¡to ¡

keep ¡trees ¡balanced ¡

h=6 ¡ Height ¡

C ¡ D ¡ E ¡ F ¡ G ¡

slide-18
SLIDE 18

18 ¡

Agenda ¡

  • 1. Binary ¡search ¡

¡

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

¡

slide-19
SLIDE 19

19 ¡

InserQng ¡a ¡new ¡key ¡is ¡easy ¡(compared ¡with ¡ sorted ¡array) ¡

D ¡ B ¡ A ¡ C ¡ F ¡ E ¡ G ¡

InserEng ¡new ¡node ¡with ¡key ¡H ¡ ¡ Comments ¡

  • Search ¡for ¡key ¡(H) ¡
  • If ¡found, ¡replace ¡value ¡
  • If ¡hit ¡leaf, ¡add ¡new ¡node ¡as ¡len ¡or ¡right ¡child ¡of ¡leaf ¡
slide-20
SLIDE 20

20 ¡

InserQng ¡a ¡new ¡key ¡is ¡easy ¡(compared ¡with ¡ sorted ¡array) ¡

D ¡ B ¡ A ¡ C ¡ F ¡ E ¡ G ¡

InserEng ¡new ¡node ¡with ¡key ¡H ¡ ¡ Comments ¡

  • Search ¡for ¡key ¡(H) ¡
  • If ¡found, ¡replace ¡value ¡
  • If ¡hit ¡leaf, ¡add ¡new ¡node ¡as ¡len ¡or ¡right ¡child ¡of ¡leaf ¡
slide-21
SLIDE 21

21 ¡

InserQng ¡a ¡new ¡key ¡is ¡easy ¡(compared ¡with ¡ sorted ¡array) ¡

D ¡ B ¡ A ¡ C ¡ F ¡ E ¡ G ¡

InserEng ¡new ¡node ¡with ¡key ¡H ¡ ¡ Comments ¡

  • Search ¡for ¡key ¡(H) ¡
  • If ¡found, ¡replace ¡value ¡
  • If ¡hit ¡leaf, ¡add ¡new ¡node ¡as ¡len ¡or ¡right ¡child ¡of ¡leaf ¡

G ¡is ¡a ¡leaf, ¡add ¡new ¡node ¡

slide-22
SLIDE 22

22 ¡

InserQng ¡a ¡new ¡key ¡is ¡easy ¡(compared ¡with ¡ sorted ¡array) ¡

D ¡ B ¡ A ¡ C ¡ F ¡ E ¡ G ¡

InserEng ¡new ¡node ¡with ¡key ¡H ¡ ¡ Comments ¡

  • Search ¡for ¡key ¡(H) ¡
  • If ¡found, ¡replace ¡value ¡
  • If ¡hit ¡leaf, ¡add ¡new ¡node ¡as ¡len ¡or ¡right ¡child ¡of ¡leaf ¡

D ¡ B ¡ A ¡ C ¡ F ¡ E ¡ G ¡ H ¡

slide-23
SLIDE 23

23 ¡

DeleQon ¡is ¡trickier, ¡need ¡to ¡consider ¡ children, ¡but ¡no ¡children ¡is ¡easy ¡

D ¡ B ¡ A ¡ C ¡ F ¡ E ¡ G ¡

¡ Comments ¡

  • Search ¡for ¡parent ¡of ¡A ¡
  • If ¡found ¡and ¡A ¡has ¡no ¡children, ¡set ¡appropriate ¡len ¡or ¡right ¡

to ¡null ¡on ¡parent ¡ DeleEng ¡node ¡A ¡ ¡(no ¡children) ¡

slide-24
SLIDE 24

24 ¡

DeleQon ¡is ¡trickier, ¡need ¡to ¡consider ¡ children, ¡but ¡no ¡children ¡is ¡easy ¡

D ¡ B ¡ A ¡ C ¡ F ¡ E ¡ G ¡

¡ Comments ¡

  • Search ¡for ¡parent ¡of ¡A ¡
  • If ¡found ¡and ¡A ¡has ¡no ¡children, ¡set ¡appropriate ¡len ¡or ¡right ¡

to ¡null ¡on ¡parent ¡ DeleEng ¡node ¡A ¡ ¡(no ¡children) ¡

slide-25
SLIDE 25

25 ¡

DeleQon ¡is ¡trickier, ¡need ¡to ¡consider ¡ children, ¡but ¡no ¡children ¡is ¡easy ¡

D ¡ B ¡ A ¡ C ¡ F ¡ E ¡ G ¡

¡ Comments ¡

  • Search ¡for ¡parent ¡of ¡A ¡
  • If ¡found ¡and ¡A ¡has ¡no ¡children, ¡set ¡appropriate ¡len ¡or ¡right ¡

to ¡null ¡on ¡parent ¡ DeleEng ¡node ¡A ¡ ¡(no ¡children) ¡

B ¡is ¡ parent ¡

  • f ¡A ¡
slide-26
SLIDE 26

26 ¡

DeleQon ¡is ¡trickier, ¡need ¡to ¡consider ¡ children, ¡but ¡no ¡children ¡is ¡easy ¡

D ¡ B ¡ A ¡ C ¡ F ¡ E ¡ G ¡

¡ Comments ¡

  • Search ¡for ¡parent ¡of ¡A ¡
  • If ¡found ¡and ¡A ¡has ¡no ¡children, ¡set ¡appropriate ¡len ¡or ¡right ¡

to ¡null ¡on ¡parent ¡ DeleEng ¡node ¡A ¡ ¡(no ¡children) ¡

B ¡is ¡ parent ¡

  • f ¡A ¡

D ¡ B ¡ C ¡ F ¡ E ¡ G ¡

slide-27
SLIDE 27

27 ¡

DeleQng ¡with ¡one ¡child ¡is ¡not ¡difficult ¡

DeleEng ¡node ¡B ¡(1 ¡child) ¡

D ¡ B ¡ C ¡ F ¡ E ¡ G ¡

¡ Comments ¡

  • Search ¡for ¡parent ¡of ¡B ¡
  • If ¡found ¡and ¡B ¡has ¡1 ¡child, ¡set ¡appropriate ¡len ¡or ¡right ¡on ¡

parent ¡to ¡B’s ¡only ¡child ¡

slide-28
SLIDE 28

28 ¡

DeleQng ¡with ¡one ¡child ¡is ¡not ¡difficult ¡

DeleEng ¡node ¡B ¡(1 ¡child) ¡

D ¡ B ¡ C ¡ F ¡ E ¡ G ¡

¡ Comments ¡

  • Search ¡for ¡parent ¡of ¡B ¡
  • If ¡found ¡and ¡B ¡has ¡1 ¡child, ¡set ¡appropriate ¡len ¡or ¡right ¡on ¡

parent ¡to ¡B’s ¡only ¡child ¡

D ¡is ¡ parent ¡

  • f ¡B ¡
slide-29
SLIDE 29

29 ¡

DeleQng ¡with ¡one ¡child ¡is ¡not ¡difficult ¡

DeleEng ¡node ¡B ¡(1 ¡child) ¡

D ¡ B ¡ C ¡ F ¡ E ¡ G ¡

¡ Comments ¡

  • Search ¡for ¡parent ¡of ¡B ¡
  • If ¡found ¡and ¡B ¡has ¡1 ¡child, ¡set ¡appropriate ¡len ¡or ¡right ¡on ¡

parent ¡to ¡B’s ¡only ¡child ¡

D ¡is ¡ parent ¡

  • f ¡B ¡

D ¡ C ¡ F ¡ E ¡ G ¡

slide-30
SLIDE 30

30 ¡

DeleQng ¡node ¡with ¡2 ¡children ¡requires ¡ finding ¡the ¡node’s ¡“successor” ¡

D ¡ C ¡ F ¡ E ¡ G ¡

DeleEng ¡node ¡F ¡(2 ¡children) ¡ ¡ Comments ¡

  • Search ¡for ¡F ¡
  • If ¡found ¡and ¡F ¡has ¡2 ¡children, ¡find ¡successor ¡(smallest ¡on ¡right) ¡
  • Successor ¡will ¡be ¡greater ¡than ¡E ¡and ¡less ¡than ¡or ¡equal ¡to ¡G ¡
  • May ¡have ¡to ¡recurse ¡down ¡right ¡child’s ¡len ¡descendants ¡
  • Delete ¡successor, ¡but ¡save ¡successor's ¡key ¡and ¡value ¡
  • Replace ¡F ¡with ¡key ¡and ¡value ¡of ¡successor ¡
slide-31
SLIDE 31

31 ¡

DeleQng ¡node ¡with ¡2 ¡children ¡requires ¡ finding ¡the ¡node’s ¡“successor” ¡

D ¡ C ¡ F ¡ E ¡ G ¡

DeleEng ¡node ¡F ¡(2 ¡children) ¡ ¡ Comments ¡

  • Search ¡for ¡F ¡
  • If ¡found ¡and ¡F ¡has ¡2 ¡children, ¡find ¡successor ¡(smallest ¡on ¡right) ¡
  • Successor ¡will ¡be ¡greater ¡than ¡E ¡and ¡less ¡than ¡or ¡equal ¡to ¡G ¡
  • May ¡have ¡to ¡recurse ¡down ¡right ¡child’s ¡len ¡descendants ¡
  • Delete ¡successor, ¡but ¡save ¡successor's ¡key ¡and ¡value ¡
  • Replace ¡F ¡with ¡key ¡and ¡value ¡of ¡successor ¡
slide-32
SLIDE 32

32 ¡

DeleQng ¡node ¡with ¡2 ¡children ¡requires ¡ finding ¡the ¡node’s ¡“successor” ¡

D ¡ C ¡ F ¡ E ¡ G ¡

DeleEng ¡node ¡F ¡(2 ¡children) ¡ ¡ Comments ¡

  • Search ¡for ¡F ¡
  • If ¡found ¡and ¡F ¡has ¡2 ¡children, ¡find ¡successor ¡(smallest ¡on ¡right) ¡
  • Successor ¡will ¡be ¡greater ¡than ¡E ¡and ¡less ¡than ¡or ¡equal ¡to ¡G ¡
  • May ¡have ¡to ¡recurse ¡down ¡right ¡child’s ¡len ¡descendants ¡
  • Delete ¡successor, ¡but ¡save ¡successor's ¡key ¡and ¡value ¡
  • Replace ¡F ¡with ¡key ¡and ¡value ¡of ¡successor ¡

Found ¡F ¡ Successor ¡is ¡smallest ¡on ¡right ¡(G ¡here) ¡

slide-33
SLIDE 33

¡ Comments ¡

  • Search ¡for ¡F ¡
  • If ¡found ¡and ¡F ¡has ¡2 ¡children, ¡find ¡successor ¡(smallest ¡on ¡right) ¡
  • Successor ¡will ¡be ¡greater ¡than ¡E ¡and ¡less ¡than ¡or ¡equal ¡to ¡G ¡
  • May ¡have ¡to ¡recurse ¡down ¡right ¡child’s ¡len ¡descendants ¡
  • Delete ¡successor, ¡but ¡save ¡successor's ¡key ¡and ¡value ¡
  • Replace ¡F ¡with ¡key ¡and ¡value ¡of ¡successor ¡

33 ¡

DeleQng ¡node ¡with ¡2 ¡children ¡requires ¡ finding ¡the ¡node’s ¡“successor” ¡

D ¡ C ¡ F ¡ E ¡ G ¡

DeleEng ¡node ¡F ¡(2 ¡children) ¡

Found ¡F ¡ Successor ¡is ¡smallest ¡on ¡right ¡(G ¡here) ¡ Delete ¡successor ¡

slide-34
SLIDE 34

¡ Comments ¡

  • Search ¡for ¡F ¡
  • If ¡found ¡and ¡F ¡has ¡2 ¡children, ¡find ¡successor ¡(smallest ¡on ¡right) ¡
  • Successor ¡will ¡be ¡greater ¡than ¡E ¡and ¡less ¡than ¡or ¡equal ¡to ¡G ¡
  • May ¡have ¡to ¡recurse ¡down ¡right ¡child’s ¡len ¡descendants ¡
  • Delete ¡successor, ¡but ¡save ¡successor's ¡key ¡and ¡value ¡
  • Replace ¡F ¡with ¡key ¡and ¡value ¡of ¡successor ¡

34 ¡

DeleQng ¡node ¡with ¡2 ¡children ¡requires ¡ finding ¡the ¡node’s ¡“successor” ¡

D ¡ C ¡ F ¡ E ¡

DeleEng ¡node ¡F ¡(2 ¡children) ¡

Found ¡F ¡ Successor ¡is ¡smallest ¡on ¡right ¡(G ¡here) ¡ Delete ¡successor ¡ Replace ¡F ¡value ¡with ¡G ¡key ¡and ¡value ¡

G ¡

slide-35
SLIDE 35

35 ¡

Agenda ¡

  • 1. Binary ¡search ¡

¡

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

¡

slide-36
SLIDE 36

36 ¡

Binary ¡Search ¡Tree ¡nodes ¡each ¡take ¡a ¡key ¡ and ¡value, ¡also ¡have ¡len ¡and ¡right ¡children ¡

public ¡class ¡BST<K,V> ¡{ ¡ ¡ ¡private ¡K ¡key; ¡ ¡ ¡private ¡V ¡value; ¡ ¡ ¡private ¡BST<K,V> ¡len, ¡right; ¡ } ¡ ¡ Comments ¡

  • Key ¡could ¡be ¡a ¡String ¡(e.g., ¡name) ¡and ¡value ¡could ¡be ¡

BufferedImage ¡(e.g., ¡mugshot), ¡both ¡could ¡be ¡integers ¡(key ¡is ¡ zip ¡code, ¡value ¡is ¡populaQon), ¡depends ¡on ¡use ¡case ¡

  • Remember, ¡generics ¡need ¡to ¡be ¡objects, ¡so ¡use ¡wrapper ¡

classes ¡for ¡primiQves ¡such ¡as ¡Integer ¡and ¡Double ¡ Binary ¡Search ¡Tree ¡declaraEon ¡

slide-37
SLIDE 37

37 ¡

We ¡need ¡a ¡way ¡to ¡compare ¡nodes, ¡so ¡the ¡ Key ¡must ¡implement ¡a ¡Comparable ¡

public ¡class ¡BST<K ¡extends ¡Comparable<K>, ¡V> ¡{ ¡ ¡ ¡private ¡K ¡key; ¡ ¡ ¡private ¡V ¡value; ¡ ¡ ¡private ¡BST<K,V> ¡len, ¡right; ¡ } ¡

¡

Comments ¡

  • Comparable ¡must ¡implement ¡compareTo(K ¡compareKey) ¡method ¡
  • compareTo() ¡built ¡in ¡for ¡primiQve ¡types ¡and ¡wrappers ¡(e.g., ¡

String), ¡no ¡need ¡to ¡implement ¡ourselves ¡if ¡Key ¡is ¡String ¡

  • compareTo() ¡returns ¡0 ¡if ¡node ¡and ¡compareKey ¡are ¡the ¡“equal” ¡
  • Return ¡-­‑1 ¡if ¡node’s ¡key ¡< ¡compareKey ¡
  • Return ¡1 ¡if ¡node’s ¡> ¡compareKey ¡

Extending ¡Comparable ¡interface ¡

slide-38
SLIDE 38

38 ¡

BSTs ¡make ¡searching ¡fast ¡and ¡simple ¡

BST.java ¡

  • Constructors ¡set ¡up ¡trees ¡as ¡expected, ¡just ¡like ¡last ¡class ¡in ¡

BinaryTree.java ¡

  • public ¡V ¡find(K ¡search) ¡
  • Compare ¡search ¡with ¡node’s ¡key ¡
  • If ¡match ¡then ¡return ¡node’s ¡value ¡
  • If ¡(compare ¡< ¡0 ¡&& ¡hasLen()) ¡return ¡len.find(search) ¡
  • If ¡(compare ¡> ¡0 ¡&& ¡hasRight()) ¡return ¡right.find(search) ¡
  • Throw ¡excepQon ¡if ¡key ¡not ¡found ¡(wasn’t ¡match ¡and ¡no ¡

len ¡or ¡right ¡children) ¡

  • public ¡void ¡insert( ¡K ¡key, ¡V ¡value) ¡
  • Search ¡key ¡
  • If ¡key ¡found, ¡replace ¡value ¡
  • else, ¡insert ¡as ¡leaf ¡
slide-39
SLIDE 39

39 ¡

Code ¡to ¡delete ¡nodes ¡

BST.java ¡

  • delete(K ¡search) ¡at ¡line ¡105 ¡
  • Compare ¡search ¡to ¡this ¡node’s ¡key ¡
  • If ¡node’s ¡key ¡< ¡search, ¡set ¡le@ ¡= ¡le@.delete(search), ¡return ¡this ¡
  • If ¡node’s ¡key ¡> ¡search, ¡set ¡right= ¡right.delete(search), ¡return ¡this ¡
  • If ¡keys ¡are ¡the ¡same ¡
  • If ¡node ¡has ¡one ¡child, ¡return ¡child ¡
  • If ¡node ¡has ¡two ¡children ¡
  • Find ¡successor ¡(smallest ¡on ¡right), ¡may ¡have ¡to ¡recurve ¡

right ¡child’s ¡len ¡children ¡

  • Delete ¡successor, ¡but ¡save ¡key ¡and ¡value ¡
  • Set ¡this ¡node’s ¡key ¡and ¡value ¡to ¡successor’s ¡key ¡and ¡

value ¡

  • Return ¡this ¡node ¡
slide-40
SLIDE 40

40 ¡

Can ¡find ¡min ¡value ¡in ¡tree ¡recursively ¡or ¡in ¡ a ¡loop ¡

BST.java ¡ Only ¡need ¡to ¡traverse ¡down ¡len ¡side, ¡so ¡like ¡a ¡linked ¡list ¡

  • min() ¡(line ¡66) ¡
  • If ¡len ¡!= ¡null, ¡return ¡len.min() ¡
  • Else ¡return ¡key ¡(this ¡will ¡be ¡the ¡len ¡most, ¡smallest ¡key) ¡
  • minIter() ¡(line ¡74) ¡
  • Start ¡from ¡current ¡node ¡
  • While ¡(curr.len ¡!= ¡null) ¡curr ¡= ¡curr.len ¡
  • Return ¡curr.key ¡