lecture16 - Priority Queues, Heaps

lecture16 - Priority Queues, Heaps - hash_map Representing...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: hash_map Representing Binary Trees Using Arrays Complete Binary Trees Priority Queues Heaps of what? The Heap Sort Review Challenge Monday, March 10 th The hash_map: A hash-based version of a map #include <hash_map> #include <iostream> #include <string> using namespace stdext; // required for a hashmap using namespace std; int main( ) { hash_map <string,int> hm; // define a new hash_map hash_map <string,int>::iterator iter; // define an iterator for a HM hm["Carey"] = 10; // insert a new item into the HM 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!; // couldnt 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 } 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 dont just take the next patient in line, I take the one who has the most severe injuries. Next patient please! A priority queue is a special type of queue that allows us to keep a prioritized list of items. Priority Queues A 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. Priority Queues Question : What data structures can we use to implement a priority queue? Hmmm Lets 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 Priority Queues Question : Ok, but what data structure should we use if we have a huge number of priorities? Hmmm! The HEAP data structure is one of the most efficient ones we can use to implement a Priority Queue. The heap is a special type of Binary Tree . So first, we need to learn a few more things about Binary Trees. Representing Binary Trees As weve seen, you can represent a binary tree with a linked-list data structure....
View Full Document

This note was uploaded on 01/20/2011 for the course CS cs32 taught by Professor John during the Spring '09 term at UCLA.

Page1 / 30

lecture16 - Priority Queues, Heaps - hash_map Representing...

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

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