module02

module02 - Module 2: Priority Queues CS 240 - Data...

Info iconThis preview shows pages 1–10. 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

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: Module 2: Priority Queues CS 240 - Data Structures and Data Management Arne Storjohann Based on lecture notes by R. Dorrigiv and D. Roche School of Computer Science, University of Waterloo Fall 2010 Arne Storjohann (CS, UW) CS240 - Module 2 Fall 2010 1 / 17 Abstract Data Types Abstract Data Type (ADT): A set of values and a collection of operations on those values. An ADT is accessed only through an interface . We can have various implementations of an ADT. Arne Storjohann (CS, UW) CS240 - Module 2 Fall 2010 2 / 17 Stack ADT Stack: an ADT consisting of a collection of items with operations: push : inserting an item pop : removing the most recently inserted item Items are removed in LIFO ( last-in first-out ) order. We can have extra operations: size , isEmpty , and top Applications: Addresses of recently visited sites in a Web browser, procedure calls Implementations of Stack ADT using arrays using linked lists Arne Storjohann (CS, UW) CS240 - Module 2 Fall 2010 3 / 17 Queue ADT Queue: an ADT consisting of a collection of items with operations: enqueue : inserting an item dequeue : removing the least recently inserted item Items are removed in FIFO ( first-in first-out ) order. Items enter the queue at the rear and are removed from the front . We can have extra operations: size , isEmpty , and front Implementations of Queue ADT using (circular) arrays using linked lists Arne Storjohann (CS, UW) CS240 - Module 2 Fall 2010 4 / 17 Priority Queue ADT Priority Queue: An ADT consisting of a collection of items (each having a priority ) with operations insert : inserting an item tagged with a priority deleteMax : removing the item of highest priority deleteMax is also called extractMax . Applications: typical “todo” list, simulation systems The above definition is for a maximum-oriented priority queue. A minimum-oriented priority queue is defined in the natural way, by replacing the operation deleteMax by deleteMin . Arne Storjohann (CS, UW) CS240 - Module 2 Fall 2010 5 / 17 Using a Priority Queue to Sort PQ- Sort ( A ) 1. initialize PQ to an empty priority queue 2. for i ← to n- 1 do 3. PQ . insert ( A [ i ] , A [ i ]) 4. for i ← to n- 1 do 5. A [ n- 1- i ] ← PQ . deleteMax () Arne Storjohann (CS, UW) CS240 - Module 2 Fall 2010 6 / 17 Implementation of Priority Queues Attempt 1: Use unsorted arrays Arne Storjohann (CS, UW) CS240 - Module 2 Fall 2010 7 / 17 Implementation of Priority Queues Attempt 1: Use unsorted arrays insert: O (1) deleteMax: O ( n ) Using unsorted linked-lists is identical. Arne Storjohann (CS, UW) CS240 - Module 2 Fall 2010 7 / 17 Implementation of Priority Queues Attempt 1: Use unsorted arrays insert: O (1) deleteMax: O ( n ) Using unsorted linked-lists is identical....
View Full Document

This note was uploaded on 01/14/2012 for the course CS 246 taught by Professor Wormer during the Winter '08 term at Waterloo.

Page1 / 35

module02 - Module 2: Priority Queues CS 240 - Data...

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

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