lecture16

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

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

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

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

View Full DocumentRight Arrow Icon
2 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
3 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! A 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 DocumentRight Arrow Icon
4 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.
Background image of page 4
5 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 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.
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 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 data structure uses a special type of binary tree to hold its data. As we’ll see, while a heap does use a binary tree to store its data, a heap is NOT a binary search tree .
Background image of page 6
7 All Heaps Use a “Complete” Binary Tree A complete binary tree is one in which: The top N-1 levels of the tree are completely filled with nodes All nodes on the bottom-most level must be as far left as possible (with no empty slots between nodes!) Is it complete? root ptr “carey” “leon”
Background image of page 7

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

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

This note was uploaded on 05/11/2011 for the course CS 33 taught by Professor Rohr during the Spring '08 term at UCLA.

Page1 / 45

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

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