lec12 - 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
13-1/47 The PriorityQueue ADT PriorityQueue implementations Trees and tree terminology Binary trees Heaps and heap implementations Heapsort 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
13-2/47 The Priority Queue ADT Priority queues are used in many high-performance algorithms A stack is a LIFO structure. .. a queue is a FIFO structure. .. a priority queue is a “HPIFO” structure: highest priority in, first out A real-world queue is a lineup at a supermarket checkout; a real-world priority queue is an emergency room waiting area (Could you implement a priority queue by adapting a queue or stack? In fact it would easier to go the other way: define priority of an item in terms of how long it has been waiting in the priority queue. ..)
Background image of page 2
13-3/47 The Priority Queue ADT Description A priority queue stores a collection of elements that are totally ordered with respect to their “priority”. It allows adding elements, and inspecting and removing the element with the highest priority. Attributes size : The number of elements in the priority queue.
Background image of page 3

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

View Full DocumentRight Arrow Icon
13-4/47 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 4
13-5/47 Implementing Priority Queue There are always many ways to implement a given ADT. .. e.g. a linked list could be adapted to implement that Priority Queue ADT Consider two approaches: 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 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 5

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

View Full DocumentRight Arrow Icon
13-6/47 Linked-list Priority Queue time costs Assume a linked-list Priority Queue with N elements. Approach 1 (sorted list): add() has worst-case time cost ______ remove(),peek() have worst-case time cost _____ Approach 2 (unsorted list): add() has worst-case time cost ______ remove(),peek() have worst-case time cost _____
Background image of page 6
13-7/47 Implementing Priority Queue A linked list could be used to implement the Priority Queue ADT. .. but it is much more common to use a heap A heap data structure is a kind of binary tree So let’s look at trees, binary trees, and heaps…
Background image of page 7

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

View Full DocumentRight Arrow Icon
13-8/47 Tree structures A tree is a hierarchical structure (a generalization of a linear structure such as a list. ..) A tree is a set of elements called nodes , structured by a "parent" relation: If the tree is nonempty, exactly one node in the set is the
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.

This note was uploaded on 06/15/2011 for the course ECON 1 taught by Professor Aben during the Fall '07 term at City College of San Francisco.

Page1 / 49

lec12 - 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