{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture16 - Priority Queues, Heaps

# lecture16 - Priority Queues, Heaps - Monday March 10 th...

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

hash_map Representing Binary Trees Using Arrays Complete Binary Trees Priority Queues Heaps… of what? The Heap Sort Review Challenge Monday, March 10 th

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

View Full Document
The hash_map: A hash-based version of a map #include<hash_map> #include<iostream> #include<string> using namespacestdext; // required for a hashmap using namespacestd; int main( ) { hash_map <string,int> hm; // definea new hash_map hash_map <string,int>::iterator iter; // definean iterator for a HM hm["Carey"] = 10; // insert a new item into theHM 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 }
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
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.
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

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

View Full Document
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. The heap is a special typeof Binary Tree . So first, weneed to learn a few more things about Binary Trees.
Representing Binary Trees As we’ve seen, you can represent a binary tree with a linked-list data structure. You can also represent a binary treewith an array ! 1. Theroot of thebinary treegoes in array[0] 1. If the data for a nodeappears in array[i] , its children, if they exist, are in theselocations: Left child : array[2i+1] Right child : array[2i+2] 1. If the data for a non-root node is in array[i] , then its parent is always at array[(i-1)/2] (Use integer division) 4. You can storean invalid valuelike ‘?’ or -1 in non-existent nodes to mark them as unused.

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 ]}

### Page1 / 30

lecture16 - Priority Queues, Heaps - Monday March 10 th...

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

View Full Document
Ask a homework question - tutors are online