{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture16

lecture16 - 1 Monday March 7 re ap unorde d_m ue Priority...

This preview shows pages 1–8. Sign up to view the full content.

1 unordered_map Priority Queues Heaps Using classic binary trees Using arrays! The Heap Sort Review Challenge Monday, March 7 th

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

View Full Document
2 The unordered_map: A hash-based version of a map #include<unordered_map> #include<iostream> #include<string> using namespacestd::tr1; // required for a hash-based map using namespacestd; int main( ) { unordered_map <string,int> hm; // definea new U_M unordered_map <string,int>::iterator iter; // definean iterator for a U_M hm["Carey"] = 10; // insert a new item into theU_M hm["David"] = 20; iter = hm.find("Carey"); // find Carey in thehash map if (iter == hm.end()) // did wefind Carey or not? cout << “Carey was not found!”; // couldn’t find “Carey” in thehash map else { cout << "When welook up " << iter-> first ; // “When welook up Carey cout << " wefind " << iter-> second ; // “wefind 10 } }
3 In a priority queue , each item you insert into thequeue has a “ priority rating ” indicating how important it is. Any timeyou 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 havea queueof patients in the emergency room, I don’t just takethe next patient in line, I taketheone who has themost severe injuries. Next patient please! A priority queue is a special type of queuethat allows us to keep a prioritized list of items.

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

View Full Document
4 Priority Queues A Priority Queue supports threeoperations: Insert a new item into the queue Get thevalue of thehighest priority item Remove the highest priority item from the queue When you definea Priority Queue, you must specify how to determinethe 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 retrievethehighest-priority item.
5 Priority Queues Question : What data structures can we useto implement a priority queue? Hmmm… Let’s makeit easier… What if we havejust a limited set of priorities, e.g.: high , mediumlow ? 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 n linked lists, one for each priority level. To obtain thehighest-priority item, always take the first item from the highest priority, non-empty list.

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

View Full Document
6 Priority Queues Question : Ok, but what data structureshould weuseif wehave a huge number of priorities? Hmmm! The HEAP data structureis one of the most efficient ones we can useto implement a Priority Queue. Theheap data structure uses a special type of binary tree to hold its data. As we’ll see, whilea heap does use a binary tree to storeits data, a heap is NOT a binary search tree .
7 All Heaps Usea “Complete” Binary Tree A completebinary tree is onein which: The top N-1 levels of thetreeare completely filled with nodes All nodes on thebottom-most level must beas far left as possible(with no empty slots between nodes!) Is it complete?

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}