lecture16

lecture16 - hash_map Priority Queues Heaps Using classic...

Info iconThis preview shows pages 1–7. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: hash_map Priority Queues Heaps Using classic binary trees Using arrays! The Heap Sort Review Challenge Monday, March 8 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 Right we can use n 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. Priority Queues Question : Ok, but what data structure should we use if we have a huge...
View Full Document

This note was uploaded on 04/10/2012 for the course CS 32 taught by Professor Davidsmallberg during the Winter '08 term at UCLA.

Page1 / 45

lecture16 - hash_map Priority Queues Heaps Using classic...

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