priorty_queues

# Priorty_queues - Priority Queues Two kinds of priority queues Min priority queue Max priority queue Min Priority Queue Collection of elements Each

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

Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue.

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

View Full Document
Min Priority Queue Collection of elements. Each element has a priority or key. Supports following operations: isEmpty size add/put an element into the priority queue get element with min priority remove element with min priority
Max Priority Queue Collection of elements. Each element has a priority or key. Supports following operations: isEmpty size add/put an element into the priority queue get element with max priority max

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

View Full Document
Complexity Of Operations Two good implementations are heaps and leftist trees. isEmpty, size, and get => O(1) time put and remove => O(log n) time where n is the size of the priority queue
Applications Sorting use element key as priority put elements to be sorted into a priority queue extract elements in priority order if a min priority queue is used, elements are extracted in ascending order of priority (or key) if a max priority queue is used, elements are extracted in descending order of priority (or key)

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

View Full Document
Sorting Example Sort five elements whose keys are 6, 8, 2, 4, 1 using a max priority queue. Put the five elements into a max priority queue. Do five remove max operations placing removed elements into the sorted array from right to left.
After Putting Into Max Priority Queue Sorted Array 6 8 2 4 1 Max Priority Queue

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

View Full Document
After First Remove Max Operation Sorted Array 6 2 4 1 8 Max Priority Queue
After Second Remove Max Operation Sorted Array 2 4 1 8 6 Max Priority Queue

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

View Full Document
After Third Remove Max Operation Sorted Array 2 1 8 6 4 Max Priority Queue
After Fourth Remove Max Operation Sorted Array 1 8 6 4 2 Max Priority Queue

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

View Full Document
After Fifth Remove Max Operation Sorted Array 8 6 4 2 1 Max Priority Queue
Complexity Of Sorting Sort n elements. n put operations => O(n log n) time.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 01/24/2011 for the course CSE 303 taught by Professor Mm during the Spring '10 term at MIT.

### Page1 / 77

Priorty_queues - Priority Queues Two kinds of priority queues Min priority queue Max priority queue Min Priority Queue Collection of elements Each

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

View Full Document
Ask a homework question - tutors are online