lec19 - CSE 12 Priority Queues, Trees, Heaps The...

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

View Full Document Right Arrow Icon
19 The PriorityQueue ADT PriorityQueue implementations Trees and tree terminology Binary trees Heaps and heap implementations CSE 12 Priority Queues, Trees, Heaps
Background image of page 1

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

View Full DocumentRight Arrow Icon
Priority Queue : a H ighest P riority- I n- F irst- O ut ( HPIFO ) data structure the highest priority item in the structure will be the first item to be taken out of it Values: a collection of data items of type T, which are comparable to each other for ‘priority’ Operations: T peekHighest() int size() Introduction these are the essential ones!
Background image of page 2
Examples of Priority Queue applications Operating systems schedulers: processes can be placed in a priority queue awaiting their turn to use the processor; the highest priority (most time-critical) process will be selected to run next Many high-performance algorithms (Huffman coding, Dijkstra shortest path, Kruskal spanning tree, etc.) use a priority queue to decide what step to take next Simulations of a priority-queue-like behavior (e.g., emergency room waiting area)
Background image of page 3

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

View Full DocumentRight Arrow Icon
Priority Queue Properties and Attributes Properties • A PriorityQueue is a HPIFO collection. All elements in the collection must be comparable to each other with respect to a quantity called their ‘priority’. A remove operation on the collection removes the item with the highest priority of all items in the collection. Attributes size : The number of elements in the priority queue
Background image of page 4
A picture of a priority queue Highest priority item
Background image of page 5

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

View Full DocumentRight Arrow Icon
The Priority Queue ADT Operations PriorityQueue ( ) pre-condition: none responsibilities: constructor—create an empty priority queue. post-condition: size is 0 add( Type element ) pre-condition: element is comparable to other elements in the queue responsibilities: insert element into the priority queue post-condition: size is increased by 1 Type remove() pre-condition: size is greater than 0 responsibilities: remove & return the highest priority element post-condition: size is decreased by 1, highest priority element is removed return: the highest priority element peek() return the highest priority element
Background image of page 6
Implementing Priority Queue There are always many ways to implement a given ADT However, some ways are better than others! From its name, you might think a priority queue is a kind of queue, and so you should use the Inheritance pattern and implement a PriorityQueue class that extends Queue Or, you might think you should use the Adapter pattern, and define a PriorityQueue class that delegates to Queue Actually neither of these is a very good approach, and we won’t consider them (In fact, it would be better to go the other way, and define Stack or Queue by adapting PriorityQueue! )
Background image of page 7

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

View Full DocumentRight Arrow Icon
Implementing Priority Queue We will first consider using a linked list to implement the Priority Queue ADT There are at least two ways to do that: 1. add() is implemented so that the linked list is always sorted in order of priority ; remove() and peek() then operate on the first element of the list 2. add() is implemented to always add at the front of the list ; remove() and peek() then need to traverse the list to find the highest priority element What are the time costs of each approach?
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 35

lec19 - CSE 12 Priority Queues, Trees, Heaps The...

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

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