Lecture 15 - Priority Queues, Heaps and HeapSort

Lecture 15 - Priority Queues, Heaps and HeapSort -...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
unordered_map Priority Queues Heaps Using classic binary trees Using arrays! The Heap Sort Review Challenge Monday, March 12th   
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
The unordered_map: A hash-based version of a map #include <unordered_map> #include <iostream> #include <string> using namespace std::tr1; // required for a hash-based map using namespace std; int main( ) {     unordered_map <string,int> hm;   // define a new U_M    unordered_map <string,int>::iterator iter; // define an iterator for a U_M    hm["Carey"] = 10; // insert a new item into the U_M    hm["David"] = 20;    iter = hm.find("Carey"); // find Carey in the hash map    if (iter == hm.end()) // did we find Carey or not?        cout << “Carey was not found!”; // couldn’t find “Carey” in the hash map    else     {         cout << "When we look up " << iter-> first ; // “When we look up  Carey         cout << " we find " << iter-> second ; // “we find  10    } }
Background image of page 2
In a  priority queue , each item you  insert  into the queue has a “ priority rating ” indicating how  important it is.  Any time you  dequeue  an item from a priority queue, it always dequeues the item with the  highest  priority  (instead of just the first item inserted). Priority Queues Example : If I have a queue of patients in the emergency room, I  don’t just take the next patient in line, I take the one who has  the most severe injuries. Next patient please! priority queue  is a special type of queue that allows us to keep a prioritized list of items. 
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Priority Queues Priority Queue  supports three operations:   Insert a new item into the queue  Get the value of the highest priority item  Remove the highest priority item from the queue When you define a Priority Queue, you must specify how to determine the priority of each item in the  queue. Priority  = amount of blood lost + number of cuts You must then design your PQ data structure/algorithms so you can efficiently retrieve the highest- priority item.
Background image of page 4
Priority Queues Question : What data structures can we use to implement a priority queue? Hmmm… Let’s make it easier…  What if we have just a limited set of priorities, e.g.:  high medium   low ? Hint : Think of an airport ticket line with  first business  and  coach  (cattle) class… ptrToHigh ptrToMed ptrToLow next next next next next next next next Carey Linda Emily Xiaotian Wahid David Susan Wade Right – we can use  linked  lists, one for each priority  level.   To obtain the highest-priority item, always take the first  item from the highest priority, non-empty list.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Priority Queues Question : Ok, but what data structure should we use if we have a huge number of priorities? Hmmm!
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 45

Lecture 15 - Priority Queues, Heaps and HeapSort -...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online