CS 261 Data Structures Priority Queue ADT & Heaps - - PowerPoint PPT Presentation

cs 261 data structures
SMART_READER_LITE
LIVE PREVIEW

CS 261 Data Structures Priority Queue ADT & Heaps - - PowerPoint PPT Presentation

CS261 Data Structures CS 261 Data Structures Priority Queue ADT & Heaps Goals Introduce the Priority Queue ADT Heap Data Structure


slide-1
SLIDE 1

CS ¡261 ¡– ¡Data ¡Structures ¡

Priority ¡Queue ¡ADT ¡& ¡Heaps ¡

CS261 ¡Data ¡Structures ¡

slide-2
SLIDE 2

Goals ¡

  • Introduce ¡the ¡Priority ¡Queue ¡ADT ¡
  • Heap ¡Data ¡Structure ¡Concepts ¡
slide-3
SLIDE 3

Priority ¡Queue ¡ADT ¡

  • Not ¡really ¡a ¡FIFO ¡queue ¡– ¡misnomer!! ¡
  • Associates ¡a ¡“priority” ¡with ¡each ¡element ¡in ¡the ¡

collecGon: ¡

– First ¡element ¡has ¡the ¡highest ¡priority ¡(typically, ¡lowest ¡

value) ¡

  • ApplicaGons ¡of ¡priority ¡queues: ¡

– To ¡do ¡list ¡with ¡prioriGes ¡ – Graph ¡Searching ¡ – AcGve ¡processes ¡in ¡an ¡OS ¡

slide-4
SLIDE 4

Priority ¡Queue ¡ADT: ¡Interface ¡

  • Next ¡element ¡returned ¡has ¡highest ¡priority ¡

¡ ¡ ¡void ¡ ¡add(); ¡ ¡ ¡TYPE ¡ ¡ ¡ ¡ ¡ ¡getMin(); ¡ ¡ ¡void ¡ ¡ ¡removeMin(); ¡

slide-5
SLIDE 5

Priority ¡Queue ¡ADT: ¡ImplementaGon ¡

¡Heap: ¡has ¡2 ¡completely ¡different ¡meanings ¡

  • 1. Classic ¡data ¡structure ¡used ¡to ¡implement ¡priority ¡

queues ¡

  • 2. Memory ¡space ¡used ¡for ¡dynamic ¡allocaGon ¡

We ¡will ¡study ¡the ¡data ¡structure ¡(not ¡dynamic ¡memory ¡ allocaGon) ¡

¡ ¡

slide-6
SLIDE 6

Priority ¡Queue ¡ADT: ¡ImplementaGon ¡

¡Binary ¡Heap ¡data ¡structure: ¡a ¡complete ¡binary ¡tree ¡in ¡

which ¡every ¡node’s ¡value ¡is ¡less ¡than ¡or ¡equal ¡to ¡the ¡values ¡of ¡its ¡ children ¡(min ¡heap) ¡

¡Review: ¡a ¡complete ¡binary ¡tree ¡is ¡a ¡tree ¡in ¡which ¡

  • 1. Every ¡node ¡has ¡at ¡most ¡two ¡children ¡(binary) ¡
  • 2. The ¡tree ¡is ¡enGrely ¡filled ¡except ¡for ¡the ¡boYom ¡level ¡

which ¡is ¡filled ¡from ¡leZ ¡to ¡right ¡(complete) ¡ – Longest ¡path ¡is ¡ceiling(log ¡n) ¡for ¡n ¡nodes ¡

slide-7
SLIDE 7

Min-­‑Heap: ¡Example ¡

2 ¡ 5 ¡ 8 ¡ 3 ¡ 7 ¡ 9 ¡ 10 ¡ 14 ¡ ¡ 12 ¡ 11 ¡ 16 ¡

Root ¡= ¡Smallest ¡element ¡ Next ¡open ¡spot ¡

¡ ¡

Last ¡filled ¡posiGon ¡

(not ¡necessarily ¡the ¡last ¡element ¡added) ¡

slide-8
SLIDE 8

Maintaining ¡the ¡Heap: ¡AddiGon ¡

2 ¡ 5 ¡ 8 ¡ 3 ¡ 7 ¡ 9 ¡ 10 ¡ 14 ¡ ¡ 12 ¡ 11 ¡ 16 ¡ 4 ¡ Place ¡new ¡element ¡in ¡next ¡available ¡posiGon, ¡ then ¡fix ¡it ¡by ¡“percolaGng ¡up” ¡

Add ¡element: ¡4 ¡

New ¡element ¡in ¡ next ¡open ¡spot. ¡

slide-9
SLIDE 9

Maintaining ¡the ¡Heap: ¡AddiGon ¡(cont.) ¡

PercolaGng ¡up: ¡

¡while ¡new ¡value ¡is ¡less ¡than ¡parent, ¡ ¡ ¡swap ¡value ¡with ¡parent ¡

2 ¡ 5 ¡ 8 ¡ 3 ¡ 4 ¡ 9 ¡ 10 ¡ 14 ¡ ¡ 12 ¡ 11 ¡ 16 ¡ 7 ¡ 2 ¡ 4 ¡ 8 ¡ 3 ¡ 5 ¡ 9 ¡ 10 ¡ 14 ¡ ¡ 12 ¡ 11 ¡ 16 ¡ 7 ¡

AZer ¡first ¡iteraGon ¡(swapped ¡with ¡7) ¡ AZer ¡second ¡iteraGon ¡(swapped ¡with ¡5) ¡

¡New ¡value ¡not ¡less ¡than ¡parent ¡à ¡Done ¡

slide-10
SLIDE 10

Maintaining ¡the ¡Heap: ¡Removal ¡

  • Since ¡each ¡node’s ¡value ¡is ¡less ¡than ¡or ¡equal ¡to ¡

the ¡values ¡of ¡its ¡children, ¡the ¡root ¡is ¡always ¡the ¡ smallest ¡element ¡

  • Thus, ¡the ¡operaGons ¡getMin ¡and ¡removeMin ¡access ¡

and ¡remove ¡the ¡root ¡node, ¡respecGvely ¡

  • Heap ¡removal ¡(removeMin): ¡

What ¡do ¡we ¡replace ¡the ¡root ¡node ¡with? ¡ Hint: ¡ ¡How ¡do ¡we ¡maintain ¡the ¡completeness ¡of ¡the ¡ tree? ¡

slide-11
SLIDE 11

Maintaining ¡the ¡Heap: ¡Removal ¡

Heap ¡removal ¡(removeMin): ¡

  • 1. Replace ¡root ¡with ¡the ¡element ¡in ¡the ¡last ¡filled ¡

posiGon ¡

  • 2. Fix ¡heap ¡by ¡“percolaGng ¡down” ¡
slide-12
SLIDE 12

Maintaining ¡the ¡Heap: ¡Removal ¡

2 ¡ 5 ¡ 8 ¡ 3 ¡ 7 ¡ 9 ¡ 10 ¡ 14 ¡ ¡ 12 ¡ 11 ¡ 16 ¡

Root ¡= ¡Smallest ¡element ¡

removeMin ¡: ¡

  • 1. Move ¡element ¡in ¡last ¡

¡filled ¡pos ¡into ¡root ¡

  • 2. ¡Percolate ¡down ¡

Last ¡filled ¡posiGon ¡

slide-13
SLIDE 13

Maintaining ¡the ¡Heap: ¡Removal ¡(cont.) ¡

PercolaGng ¡down: ¡

¡while ¡greater ¡than ¡smallest ¡child ¡ ¡ ¡swap ¡with ¡smallest ¡child ¡

16 ¡ 5 ¡ 8 ¡ 3 ¡ 7 ¡ 9 ¡ 10 ¡ 14 ¡ ¡ 12 ¡ 11 ¡ 3 ¡ 5 ¡ 8 ¡ 16 ¡ 7 ¡ 9 ¡ 10 ¡ 14 ¡ ¡ 12 ¡ 11 ¡

Root ¡value ¡removed ¡

(16 ¡copied ¡to ¡root ¡and ¡last ¡node ¡removed) ¡

AZer ¡first ¡iteraGon ¡(swapped ¡with ¡3) ¡

slide-14
SLIDE 14

Maintaining ¡the ¡Heap: ¡Removal ¡(cont.) ¡

3 ¡ 9 ¡ 16 ¡ 9 ¡ 12 ¡ 16 ¡

AZer ¡second ¡iteraGon ¡(moved ¡9 ¡up) ¡ AZer ¡third ¡iteraGon ¡(moved ¡12 ¡up) ¡

¡Reached ¡leaf ¡node ¡à ¡Stop ¡percolaGng ¡

PercolaGng ¡down: ¡

¡while ¡greater ¡than ¡smallest ¡child ¡ ¡ ¡swap ¡with ¡smallest ¡child ¡

5 ¡ 8 ¡ 7 ¡ 10 ¡ 14 ¡ ¡ 12 ¡ 11 ¡ 8 ¡ 3 ¡ 5 ¡ 8 ¡ 7 ¡ 10 ¡ 14 ¡ ¡ 11 ¡

slide-15
SLIDE 15

Maintaining ¡the ¡Heap: ¡Removal ¡(cont.) ¡

3 ¡ 5 ¡ 8 ¡ 9 ¡ 7 ¡ 12 ¡ 10 ¡ 14 ¡ ¡ 16 ¡ 11 ¡

Root ¡= ¡New ¡smallest ¡element ¡ New ¡last ¡filled ¡posiGon ¡

slide-16
SLIDE 16

PracGce ¡ ¡

Insert ¡the ¡following ¡numbers ¡into ¡a ¡min-­‑heap ¡in ¡ the ¡order ¡given: ¡ ¡ ¡ ¡54, ¡13, ¡32, ¡42, ¡52, ¡12, ¡6, ¡28, ¡ 73, ¡36 ¡

slide-17
SLIDE 17
slide-18
SLIDE 18

PracGce ¡

Remove ¡the ¡minimum ¡value ¡from ¡the ¡min-­‑heap ¡

5 ¡ 50 ¡ 70 ¡ 6 ¡ 60 ¡ 7 ¡ 8 ¡

slide-19
SLIDE 19

5 ¡ 50 ¡ 70 ¡ 6 ¡ 60 ¡ 7 ¡ 8 ¡

slide-20
SLIDE 20

Your ¡Turn ¡ ¡

  • Complete ¡Worksheet: ¡Heaps ¡PracGce ¡
slide-21
SLIDE 21

CS ¡261 ¡– ¡Data ¡Structures ¡

Heap ¡ImplementaGon ¡

CS261 ¡Data ¡Structures ¡

slide-22
SLIDE 22

Goals ¡

  • Heap ¡RepresentaGon ¡
  • Heap ¡Priority ¡Queue ¡ADT ¡ImplementaGon ¡
slide-23
SLIDE 23

Dynamic ¡Array ¡RepresentaGon ¡

¡Complete ¡binary ¡tree ¡has ¡structure ¡that ¡is ¡efficiently ¡represented ¡ ¡ with ¡an ¡array ¡(or ¡dynamic ¡array) ¡ – Children ¡of ¡node ¡i ¡are ¡stored ¡at ¡2i ¡+ ¡1 ¡and ¡2i ¡+ ¡2 ¡ – Parent ¡of ¡node ¡i ¡is ¡at ¡floor((i ¡-­‑ ¡1) ¡/ ¡2) ¡ a ¡ b ¡ c ¡ d ¡ e ¡ f ¡ 0 ¡ a ¡ 1 ¡ b ¡ 2 ¡ c ¡ 3 ¡ d ¡ 4 ¡ e ¡ 5 ¡ f ¡ 6 ¡ ¡ 7 ¡ ¡ Root ¡

Why ¡is ¡this ¡a ¡bad ¡idea ¡if ¡tree ¡is ¡not ¡complete? ¡

slide-24
SLIDE 24

Dynamic ¡Array ¡ImplementaGon ¡(cont.) ¡

¡If ¡the ¡tree ¡is ¡not ¡complete ¡(it ¡is ¡thin, ¡

unbalanced, ¡etc.), ¡the ¡DynArr ¡

implementaGon ¡will ¡be ¡full ¡of ¡holes ¡

a ¡ b ¡ c ¡ f ¡ d ¡ e ¡ 0 ¡ a ¡ 1 ¡ b ¡ 2 ¡ c ¡ 3 ¡ 4 ¡ d ¡ 5 ¡ 6 ¡ e ¡ 7 ¡ 8 ¡ 9 ¡ 10 ¡ 11 ¡ 12 ¡ 13 ¡ f ¡ 14 ¡ 15 ¡ Big ¡gaps ¡where ¡the ¡level ¡is ¡not ¡filled! ¡

slide-25
SLIDE 25

Heap ¡ImplementaGon: ¡add ¡

  • Things ¡to ¡consider ¡

– Where ¡does ¡the ¡new ¡value ¡get ¡placed ¡to ¡maintain ¡ completeness? ¡ ¡ – How ¡do ¡we ¡guarantee ¡the ¡heap ¡order ¡property? ¡ – How ¡do ¡we ¡compute ¡a ¡parent ¡index? ¡ – When ¡do ¡we ¡‘stop’ ¡

  • Complete ¡Worksheet ¡#33 ¡– ¡addHeap( ¡) ¡ ¡
slide-26
SLIDE 26

Write ¡: ¡ ¡addHeap ¡

¡void ¡addHeap ¡(struct ¡dyArray ¡* ¡heap, ¡TYPE ¡newValue) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡

slide-27
SLIDE 27

10 ¡

16 ¡

Heap ¡ImplementaGon: ¡removeMin ¡

void ¡removeMinHeap(DynArr ¡*heap){ ¡ ¡ ¡ ¡int ¡last; ¡ ¡last ¡= ¡sizeDynArr(heap) ¡– ¡1; ¡ ¡putDynArr(heap, ¡0, ¡getDynArr(heap, ¡last)); ¡ ¡/* ¡Copy ¡the ¡last ¡element ¡to ¡the ¡first ¡*/ ¡ ¡removeAtDynArr(heap, ¡last); ¡ ¡ ¡/* ¡Remove ¡last ¡element. ¡*/ ¡ ¡ ¡ ¡_adjustHeap(heap, ¡last ¡, ¡0); ¡ ¡ ¡/* ¡Rebuild ¡heap ¡*/ ¡ } ¡

0 ¡

2 ¡

1 ¡

3 ¡

2 ¡

4 ¡

3 ¡

9 ¡

4 ¡

10 ¡

5 ¡

5 ¡

6 ¡

8 ¡

7 ¡

14 ¡

8 ¡

12 ¡

9 ¡

11 ¡

Last ¡element ¡

(last) ¡

First ¡element ¡

(data[0]) ¡

1 ¡

3 ¡

2 ¡

4 ¡

3 ¡

9 ¡

4 ¡

10 ¡

5 ¡

5 ¡

6 ¡

8 ¡

7 ¡

14 ¡

8 ¡

12 ¡

9 ¡

11 ¡

0 ¡

7 ¡

11 ¡

7 ¡

10 ¡

16 ¡

11 ¡

¡ ¡

Percolates ¡down ¡ ¡from ¡ ¡ Index ¡0 ¡to ¡last ¡(not ¡including ¡ ¡ last…which ¡is ¡one ¡beyond ¡ the ¡end ¡now!) ¡

Last ¡element ¡

(last) ¡

slide-28
SLIDE 28

Heap ¡ImplementaGon: ¡removeMin ¡

2 ¡ 4 ¡ 8 ¡ 3 ¡ 5 ¡ 9 ¡ 10 ¡ 14 ¡ ¡ 12 ¡ 11 ¡ 16 ¡ 7 ¡

last ¡ 1 ¡

3 ¡

2 ¡

4 ¡

3 ¡

9 ¡

4 ¡

10 ¡

5 ¡

5 ¡

6 ¡

8 ¡

7 ¡

14 ¡

8 ¡

12 ¡

9 ¡

11 ¡

0 ¡

2 ¡

11 ¡

7 ¡

10 ¡

16 ¡

slide-29
SLIDE 29

Heap ¡ImplementaGon: ¡removeMin ¡(cont.) ¡

7 ¡ 4 ¡ 8 ¡ 3 ¡ 5 ¡ 9 ¡ 10 ¡ 14 ¡ ¡ 12 ¡ 11 ¡ 16 ¡

1 ¡

3 ¡

2 ¡

4 ¡

3 ¡

9 ¡

4 ¡

10 ¡

5 ¡

5 ¡

6 ¡

8 ¡

7 ¡

14 ¡

8 ¡

12 ¡

9 ¡

11 ¡

0 ¡

7 ¡

11 ¡

¡

10 ¡

16 ¡

New ¡root ¡

last ¡= ¡sizeDynArr(heap) ¡– ¡1; ¡ putDynArr(heap, ¡0, ¡getDynArr(heap, ¡last)); ¡ ¡/* ¡Copy ¡the ¡last ¡element ¡to ¡the ¡first ¡*/ ¡ removeAtDynArr(heap, ¡last); ¡ _adjustHeap(heap, ¡last, ¡0); ¡ ¡

Last ¡element ¡

(last) ¡

slide-30
SLIDE 30

Heap ¡ImplementaGon: ¡_adjustHeap ¡

7 ¡ 4 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 smallest ¡child ¡so ¡ ¡ _adjustHeap ¡exits ¡ and ¡removeMin ¡ ¡ exits ¡

slide-33
SLIDE 33

Recursive ¡_adjustHeap ¡

¡

void ¡_adjustHeap(struct ¡DynArr ¡*heap, ¡int ¡max, ¡int ¡pos) ¡{ ¡ ¡ ¡int ¡leWIdx ¡= ¡pos ¡* ¡2 ¡+ ¡1; ¡ ¡ ¡int ¡rghtIdx ¡= ¡pos ¡* ¡2 ¡+ ¡2; ¡ ¡ ¡ ¡if ¡(rghtIdx ¡< ¡max) ¡{ ¡ ¡ ¡/* ¡Have ¡two ¡children ¡*/ ¡ ¡ ¡ ¡ ¡ ¡ ¡/* ¡Get ¡index ¡of ¡smallest ¡child ¡(_minIdx). ¡*/ ¡ ¡/* ¡Compare ¡smallest ¡child ¡to ¡pos. ¡*/ ¡ ¡/* ¡If ¡necessary, ¡swap ¡and ¡call ¡_adjustHeap(max, ¡minIdx). ¡*/ ¡ ¡ ¡} ¡ ¡ ¡else ¡if ¡(leWIdx ¡< ¡max) ¡{ ¡ ¡/* ¡Have ¡only ¡one ¡child. ¡*/ ¡ ¡ ¡ ¡ ¡/* ¡Compare ¡child ¡to ¡parent. ¡*/ ¡ ¡ ¡ ¡ ¡ ¡ ¡/* ¡If ¡necessary, ¡swap ¡*/ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡/* ¡Else ¡no ¡children, ¡we ¡are ¡at ¡a ¡leaf ¡*/ ¡ } ¡

slide-34
SLIDE 34

Useful ¡RouGnes ¡

¡ void ¡swap(struct ¡DynArr ¡*arr, ¡int ¡i, ¡int ¡j) ¡{ ¡ ¡ ¡/* ¡Swap ¡elements ¡at ¡indices ¡i ¡and ¡j. ¡*/ ¡ ¡ ¡TYPE ¡tmp ¡= ¡arr-­‑>data[i]; ¡ ¡ ¡arr-­‑>data[i] ¡= ¡arr-­‑>data[j]; ¡ ¡ ¡arr-­‑>data[j] ¡= ¡tmp; ¡ } ¡ ¡ int ¡minIdx(struct ¡DynArr ¡*arr, ¡int ¡i, ¡int ¡j) ¡{ ¡ ¡ ¡/* ¡Return ¡index ¡of ¡smallest ¡element ¡value. ¡*/ ¡ ¡ ¡if ¡(compare(arr-­‑>data[i], ¡arr-­‑>data[j]) ¡== ¡-­‑1) ¡ ¡ ¡ ¡ ¡return ¡i; ¡ ¡ ¡return ¡j; ¡ } ¡

slide-35
SLIDE 35

Priority ¡Queues: ¡Performance ¡(ave) ¡

¡ So, ¡which ¡is ¡the ¡best ¡implementaGon ¡of ¡a ¡priority ¡queue? ¡ (What ¡if ¡wrote ¡a ¡getMin() ¡for ¡AVLTree ¡?) ¡

SortedVector SortedList Heap add O(n) ¡

Binary ¡search ¡ Slide ¡data ¡up ¡

O(n) ¡

Linear ¡search ¡

O(log ¡n) ¡

Percolate ¡up ¡

getMin O(1) ¡

get(0)

¡ O(1) ¡

Returns ¡firstLink ¡val

¡ O(1) ¡

Get ¡root ¡node ¡

removeMin O(n) ¡

Slide ¡data ¡down ¡ O(1): ¡Reverse ¡Order

¡ O(1) ¡

removeFront()

¡ O(log ¡n) ¡

Percolate ¡down

¡

slide-36
SLIDE 36

Priority ¡Queues: ¡Performance ¡EvaluaGon ¡

  • Recall ¡that ¡a ¡priority ¡queue’s ¡main ¡purpose ¡is ¡rapidly ¡

accessing ¡and ¡removing ¡the ¡smallest ¡element! ¡

  • Consider ¡a ¡case ¡where ¡you ¡will ¡insert ¡(and ¡ulGmately ¡

remove) ¡n ¡elements: ¡ – ReverseSortedVector ¡and ¡SortedList: ¡

InserGons: ¡n ¡* ¡n ¡= ¡n2 ¡ Removals: ¡n ¡* ¡1 ¡= ¡n ¡ Total ¡Gme: ¡n2 ¡+ ¡n ¡= ¡O(n2) ¡

– Heap: ¡

InserGons: ¡n ¡* ¡log ¡n ¡ Removals: ¡n ¡* ¡log ¡n ¡ Total ¡Gme: ¡n ¡* ¡log ¡n ¡+ ¡n ¡* ¡log ¡n ¡ ¡= ¡2n ¡log ¡n ¡= ¡O(n ¡log ¡n) ¡

How ¡do ¡they ¡compare ¡in ¡ ¡ terms ¡of ¡space ¡requirements? ¡

slide-37
SLIDE 37

Your ¡Turn ¡ ¡

  • Complete ¡Worksheet ¡#33 ¡-­‑ ¡_adjustHeap( ¡.. ¡) ¡
slide-38
SLIDE 38

CS ¡261 ¡– ¡Data ¡Structures ¡

BuildHeap ¡and ¡Heapsort ¡

CS261 ¡Data ¡Structures ¡ ¡

slide-39
SLIDE 39

Goals ¡

  • Build ¡a ¡heap ¡from ¡an ¡array ¡of ¡arbitrary ¡values ¡
  • HeapSort ¡algorithm ¡
  • Analysis ¡of ¡HeapSort ¡
slide-40
SLIDE 40

BuildHeap ¡

  • How ¡do ¡we ¡build ¡a ¡heap ¡from ¡an ¡arbitrary ¡

array ¡of ¡values??? ¡

slide-41
SLIDE 41

BuildHeap: ¡Is ¡this ¡a ¡proper ¡heap? ¡

12 ¡ 5 ¡ 4 ¡ 7 ¡ 6 ¡ 8 ¡ 3 ¡ 9 ¡ 11 ¡ 10 ¡ 2 ¡

Are ¡any ¡of ¡the ¡subtrees ¡guaranteed ¡to ¡be ¡proper ¡heaps? ¡

0 ¡

12 ¡

1 ¡

7 ¡

2 ¡

5 ¡

3 ¡

8 ¡

4 ¡

3 ¡

5 ¡

6 ¡

6 ¡

4 ¡

7 ¡

9 ¡

8 ¡

11 ¡

9 ¡

10 ¡

10 ¡

2 ¡

slide-42
SLIDE 42

BuildHeap: ¡Leaves ¡are ¡proper ¡heaps ¡

12 ¡ 5 ¡ 4 ¡ 7 ¡ 6 ¡ 8 ¡ 3 ¡ 9 ¡ 11 ¡ 10 ¡ 2 ¡

0 ¡

12 ¡

1 ¡

7 ¡

2 ¡

5 ¡

3 ¡

8 ¡

4 ¡

3 ¡

5 ¡

6 ¡

6 ¡

4 ¡

7 ¡

9 ¡

8 ¡

11 ¡

9 ¡

10 ¡

10 ¡

2 ¡ Size ¡= ¡11 ¡ Size/2 ¡– ¡1 ¡= ¡4 ¡

11 ¡

¡

First ¡non-­‑leaf ¡at ¡floor(n/2) ¡-­‑ ¡1 ¡ ¡

slide-43
SLIDE 43

BuildHeap ¡

  • How ¡can ¡we ¡use ¡this ¡informaGon ¡to ¡build ¡a ¡

heap ¡from ¡a ¡random ¡array? ¡

  • _adjustHeap: ¡ ¡takes ¡a ¡binary ¡tree, ¡rooted ¡at ¡a ¡

node, ¡where ¡all ¡subtrees ¡of ¡that ¡node ¡are ¡ proper ¡heaps ¡and ¡percolates ¡down ¡from ¡that ¡ node ¡to ¡ensure ¡that ¡it ¡is ¡a ¡proper ¡heap ¡

void ¡_adjustHeap(struct ¡DynArr ¡*heap,int ¡max, ¡int ¡pos) ¡ ¡ Adjust ¡up ¡to ¡ (not ¡inclusive) ¡ Adjust ¡from ¡

slide-44
SLIDE 44

BuildHeap ¡Algorithm ¡

  • Find ¡the ¡first ¡non-­‑leaf ¡node,i, ¡(going ¡from ¡

boYom ¡to ¡top, ¡right ¡to ¡leZ) ¡

  • adjust ¡heap ¡from ¡it ¡to ¡max ¡
  • Decrement ¡i ¡and ¡repeat ¡unGl ¡you ¡process ¡the ¡

root ¡

slide-45
SLIDE 45

SimulaGon: ¡BuildHeap ¡

0 ¡

9 ¡

1 ¡

3 ¡

2 ¡

8 ¡

3 ¡

4 ¡

4 ¡

5 ¡

5 ¡

7 ¡ Size ¡= ¡6 ¡ i=Size/2 ¡– ¡1 ¡= ¡2 ¡ 9 ¡ 8 ¡ 3 ¡ 7 ¡ 4 ¡ 5 ¡ i=2 ¡ _adjustHeap(heap,6,2) ¡

slide-46
SLIDE 46

SimulaGon: ¡BuildHeap ¡

0 ¡

9 ¡

1 ¡

3 ¡

2 ¡

7 ¡

3 ¡

4 ¡

4 ¡

5 ¡

5 ¡

8 ¡ Size ¡= ¡6 ¡ i=1 ¡ 9 ¡ 7 ¡ 3 ¡ 8 ¡ 4 ¡ 5 ¡ i=1 ¡ _adjustHeap(heap,6,1) ¡

slide-47
SLIDE 47

SimulaGon: ¡BuildHeap ¡

0 ¡

9 ¡

1 ¡

3 ¡

2 ¡

7 ¡

3 ¡

4 ¡

4 ¡

5 ¡

5 ¡

8 ¡ Size ¡= ¡6 ¡ i= ¡0 ¡ 9 ¡ 7 ¡ 3 ¡ 8 ¡ 4 ¡ 5 ¡ i=0 ¡ _adjustHeap(heap,6,0) ¡

slide-48
SLIDE 48

SimulaGon: ¡BuildHeap ¡

0 ¡

3 ¡

1 ¡

4 ¡

2 ¡

7 ¡

3 ¡

9 ¡

4 ¡

5 ¡

5 ¡

8 ¡ 3 ¡ 7 ¡ 4 ¡ 8 ¡ 9 ¡ 5 ¡ i=-­‑1 ¡ Done…with ¡BuildHeap ¡….now ¡let’s ¡sort ¡it! ¡

slide-49
SLIDE 49

HeapSort ¡ ¡

  • BuildHeap ¡and ¡_adjustHeap ¡are ¡the ¡keys ¡to ¡an ¡

efficient ¡, ¡in-­‑place, ¡sorGng ¡algorithm ¡

– in-­‑place ¡means ¡that ¡we ¡don’t ¡ ¡require ¡any ¡extra ¡ storage ¡for ¡the ¡algorithm ¡

  • Any ¡ideas??? ¡

– Hint#1: ¡adjust ¡heap ¡only ¡goes ¡up ¡to ¡max ¡ – Hint#2: ¡we’ll ¡produce ¡a ¡reverse ¡sorted ¡array ¡

slide-50
SLIDE 50

HeapSort ¡

  • 1. BuildHeap ¡– ¡turn ¡arbitrary ¡array ¡into ¡a ¡heap ¡
  • 2. Swap ¡first ¡and ¡last ¡elements ¡
  • 3. Adjust ¡Heap ¡(from ¡0 ¡to ¡the ¡last…not ¡inclusive!) ¡
  • 4. Repeat ¡2-­‑3 ¡but ¡decrement ¡last ¡each ¡Gme ¡through ¡
slide-51
SLIDE 51

HeapSort ¡SimulaGon: ¡Sort ¡in ¡Place ¡

0 ¡

3 ¡

1 ¡

4 ¡

2 ¡

7 ¡

3 ¡

9 ¡

4 ¡

5 ¡

5 ¡

8 ¡ 3 ¡ 7 ¡ 4 ¡ 8 ¡ 9 ¡ 5 ¡ i=5 ¡ ¡ ¡ Swap(v, ¡0, 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 5 ¡ 4 ¡ i=1 ¡ Swap(v, ¡0, ¡i) ¡ _adjustHeap(v, ¡i, ¡0); ¡ ¡ ¡

IteraGon ¡5 ¡

slide-61
SLIDE 61

HeapSort ¡SimulaGon: ¡Sort ¡in ¡Place ¡

0 ¡

9 ¡

1 ¡

8 ¡

2 ¡

7 ¡

3 ¡

5 ¡

4 ¡

4 ¡

5 ¡

3 ¡ 9 ¡ 7 ¡ 8 ¡ 3 ¡ 5 ¡ 4 ¡ i=0 ¡ DONE ¡ ¡ ¡

slide-62
SLIDE 62

HeapSort ¡Performance ¡

  • Build ¡Heap: ¡ ¡

n/2 ¡calls ¡to ¡_adjustHeap ¡= ¡O(n ¡log ¡n) ¡

  • HeapSort: ¡ ¡ ¡

n ¡calls ¡to ¡swap ¡and ¡adjust ¡= ¡O(n ¡log ¡n) ¡

  • Total: ¡

O(n ¡log ¡n) ¡

slide-63
SLIDE 63

Your ¡Turn ¡ ¡

  • Worksheet ¡34 ¡– ¡BuildHeap ¡and ¡Heapsort ¡