06-Heaps

# 06-Heaps - Heaps, Priority Queues, and Sorting Priority...

This preview shows pages 1–7. Sign up to view the full content.

Heaps, Priority Queues, and Sorting

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

View Full Document
Priority Queue ADT A priority queue is an ADT that supports the following basic operations: 1. Insert a new item (deletion is often needed as well) 2. Check whether the priority queue is empty/full 3. Retrieve the item with the smallest/largest key. In many applications the keys are referred to as “priorities.” yp p y p The priority queue ADT is defined to retrieve either the minimum or maximum priority, but not generally both. A priority queue for retrieving the element with minimum priority is often referred to as a “min priority queue,” and a priority queue for retrieving the element with maximum priority is referred to as a “max priority queue.”
Applications of Priority Queues 1. Different applications on a multitasking computer pp g p will spawn tasks for a CPU. Instead of simply using time as a priority (i.e., a FIFO queue), it is ommon to permit tasks to be given higher priorities common to permit tasks to be given higher priorities to cause them to be processed sooner. 2. A school assigns a ranking for each student as soon as his/her application is received. The ranking is based on high school GPA, class rank, SAT scores, g, , , and other criteria (e.g., is parent a wealthy alumnus). Later, students are admitted in priority order until no ore slots are available more slots are available.

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

View Full Document
One method for maintaining a priority queue is to maintain a linked list in sorted order. Such an plementation supports insertion in O(N) time and implementation supports insertion in O(N) time and retrieval in O(1) time. A sorting routine can be implemented using a priority queue in which all items are inserted and then each item is retrieved by rank. Unfortunately, O(N) insertion time for each of N items results in O(N 2 ) complexity. e should suspect that a more efficient algorithm + We should suspect that a more efficient algorithm + data structure may exist for implementing priority queues.
A BST seems like a very good candidate data structure for supporting a priority queue ADT: Insertions of randomly-ordered data can be performed in O(log N) time, and finding the item with minimum (or maximum) key value an be achieved in O(log N) time if the tree is balanced can be achieved in O(log N) time if the tree is balanced. Unfortunately, we have no control over the sequence of insertions into a priority queue, so we cannot guarantee that we can produce a balanced tree. AVL and other dynamic BST balancing methods can be used to maintain balanced BSTs under arbitrary insertions and deletions so that updates and searches take O(log N) time, but such lgorithms incur significant computational overhead algorithms incur significant computational overhead. It also seems that maintaining a BST is overkill because it essentially maintains a complete sorted ordering at all times when all we ever need at any given time is the min/max key.

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

View Full Document
A BST enforces a single invariant condition: The root of every subtree has a key larger than that
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 11/06/2010 for the course CS 2050 taught by Professor Uhlmann during the Fall '09 term at Missouri (Mizzou).

### Page1 / 35

06-Heaps - Heaps, Priority Queues, and Sorting Priority...

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

View Full Document
Ask a homework question - tutors are online