Docsity

Priority Queues: Implementation and Analysis using Heaps, Slides of Data Structures and Algorithms

Partial preview of the text

Download Priority Queues: Implementation and Analysis using Heaps and more Slides Data Structures and Algorithms in PDF only on Docsity!

COSC 2436

Priority Queues

Daniel Biediger (Dan)

Today

  • (^) Priority Queues
    • (^) Analysis of the implementations
    • (^) Time complexity
    • (^) Applications

Priority

Depends on the Application

Telephone Answering System

The length of waiting (FIFO queue)

Airline Check-In System

Travel Class

Special Status

Hospital Emergency Room

Severeness of Injury

Triage

Implementation Level

How items are enqueued into the DS?

How items are dequeued from the DS?

How do we sort/maintain priority of the items?

Based on their priorities (key)

ComparedTo( ) functions

Heaps

Max Heap

A complete binary tree , each of whose

elements contains a value that is greater than

or equal to the value of each of its children

Remember? Complete tree

Heaps

Heap with letters 'A' .. 'J'

Heaps

C A T 50 20 18 30 10 Are these heaps?

Heaps

Is this a heap?

Heaps

Heap is good for Priority Queue.

We have immediate access to highest priority

item BUT if we remove it, the structure isn't a

heap

Can we "fix" the problem efficiently to maintain

the heap for the next value in the queue?

Heaps

Shape property is restored. Can order property be restored? How?

Heaps

HeapDown()

If the root is not a leaf node If the value of the root is smaller than its largest child Swap the value of the root with the value of this child. HeapDown the subtree with the new root value.  Why just compare the root with its children? How about the rest of the tree?  (^) Base Cases  (^) The root of the current subtree is a leaf node.  (^) The value of the root is greater or equal to the values in both its children.

Heap Up

Function: Restores the heap property to the

heap between root and bottom.

Precondition: The heap property is satisfied from

the root of the heap through the next-to-last leaf

node; the leaf nodes have no children.

Postcondition: The heap property applies to all

elements of the heap from root through bottom.