CS 261 – Data Structures - Classes

9 downloads 1643 Views 2MB Size Report
1. Classic data structure used to implement priority queues. 2. Memory space used for dynamic alloca?on. We will study the data structure (not dynamic memory.
CS261  Data  Structures  

CS  261  –  Data  Structures   Priority  Queue  ADT  &  Heaps  

Goals   •  Introduce  the  Priority  Queue  ADT   •  Heap  Data  Structure  Concepts  

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   –  AcGve  processes  in  an  OS  

Priority  Queue  ADT:  Interface   •  Next  element  returned  has  highest  priority        void    add(newValue);      TYPE            getMin();      void      removeMin();  

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)  

   

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  

Min-­‐Heap:  Example   Root  =  Smallest  element  

2  

3  

5  

9  

14    

10  

12  

11  

Last  filled  posiGon  

7  

16  

   

(not  necessarily  the  last  element  added)  

8  

Next  open  spot  

Maintaining  the  Heap:  AddiGon   2  

Add  element:  4   3  

5  

9  

14    

10  

12  

11  

7  

16  

4  

8   New  element  in   next  open  spot.  

Place  new  element  in  next  available  posiGon,   then  fix  it  by  “percolaGng  up”  

Maintaining  the  Heap:  AddiGon  (cont.)   2   3  

5  

9   14    

10   12  

11  

4   16  

2  

8   3  

7  

4  

AZer  first  iteraGon  (swapped  with  7)   9   14    

10  

 swap  value  with  parent  

11  

16  

8  

7   AZer  second  iteraGon  (swapped  with  5)   PercolaGng  up:    New  value  not  less  than  parent  à  Done    while  new  value  is  less  than  parent,  

 

12  

5  

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?  

Maintaining  the  Heap:  Removal   Heap  removal  (removeMin):   1. Replace  root  with  the  element  in  the  last  filled   posiGon   2. Fix  heap  by  “percolaGng  down”  

Maintaining  the  Heap:  Removal   removeMin  :  

2  

1.  Move  element  in  last    filled  pos  into  root   2.  Percolate  down  

Root  =  Smallest  element  

3  

5  

9  

14    

10  

12  

11  

7  

16  

Last  filled  posiGon  

8  

Maintaining  the  Heap:  Removal  (cont.)   16   3  

5  

9   14    

10   12  

7  

3  

8   16  

11  

5  

Root  value  removed  

(16  copied  to  root  and  last  node  removed)  

9  

14    

PercolaGng  down:  

 while  greater  than  smallest  child      swap  with  smallest  child  

10   12  

7  

11  

AZer  first  iteraGon  (swapped  with  3)  

8  

Maintaining  the  Heap:  Removal  (cont.)   3   9  

5  

16   14    

10   12  

7  

3  

8   9  

11  

5  

AZer  second  iteraGon  (moved  9  up)   12   14    

PercolaGng  down:  

 while  greater  than  smallest  child      swap  with  smallest  child  

10   16  

7  

8  

11  

AZer  third  iteraGon  (moved  12  up)  

 Reached  leaf  node  à  Stop  percolaGng  

Maintaining  the  Heap:  Removal  (cont.)   3  

Root  =  New  smallest  element  

9  

5  

12  

14    

10  

16  

11  

New  last  filled  posiGon  

7  

8  

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

PracGce   Remove  the  minimum  value  from  the  min-­‐heap   5  

6  

7  

50  

8  

60  

70  

5   6   7  

50   8  

60  

70  

Your  Turn

   

•  Complete  Worksheet:  Heaps  PracGce  

Suggest Documents