0% found this document useful (0 votes)
53 views15 pages

Lecture 6 Heaps and Priority Queues

This document discusses priority queues and heaps. Priority queues allow items to be processed based on a priority key rather than order of entry. Heaps can efficiently implement priority queues with logarithmic time insertion and deletion by keeping items in a complete binary tree structure. Items are inserted at the bottom and bubbled up, and the largest item is removed from the root and replaced with the last item, then bubbled down. Arrays are commonly used to represent heaps due to efficient navigation using index calculations.

Uploaded by

uploadingperson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views15 pages

Lecture 6 Heaps and Priority Queues

This document discusses priority queues and heaps. Priority queues allow items to be processed based on a priority key rather than order of entry. Heaps can efficiently implement priority queues with logarithmic time insertion and deletion by keeping items in a complete binary tree structure. Items are inserted at the bottom and bubbled up, and the largest item is removed from the root and replaced with the last item, then bubbled down. Arrays are commonly used to represent heaps due to efficient navigation using index calculations.

Uploaded by

uploadingperson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Priority Queues and Heaps

Computing 2 COMP1927 15x1

PRIORITY
Some applications of queues require items
processed in order of "key" or priority
rather than in order of entry (FIFO)
Priority Queues (PQueues or PQs) provide this via:

insert item into PQ


leave: remove item with highest priority key

Highest priority key may be one with smallest or largest value


depending on the application

Plus generic ADT operations: new, drop, empty, ...

PRIORITY QUEUES

Characteristic priority queue (PQ) operations:

insert item
delete item with highest priority key

Highest priority key may be the smallest or largest key


depending on the application

Along with generic ADT operations, such as:


create new queue
test if queue is empty
destroy existing queue (reclaim memory)

PRIORITY QUEUE INTERFACE


typedef struct priQ * PriQ;
//We assume we have a more complex Item type that has
//a key and a value, where the key is the priority and the
//value is the data being stored
// Core operations
PriQ initPriQ(void);
void insert(PriQ q, Item i);
//retrieve and delete Item with highest priority
Item delete(PriQ q);
// Useful operations
int sizePriQ(PriQ q);
void changePriority(PriQ q, Key k, Item i);
void deleteKey(PriQ q, Key k);
int maxSize(PriQ q);

COMPARISON OF POSSIBLE
IMPLEMENTATIONS
Implementation
ordered array/list
unordered array/list

insert delete
O(N)
O(1)
O(1)
O(N)

Can we implement BOTH


Heap
O(log N) for insert and delete

operations efficiently?

HEAPS

Heaps can be viewed as trees with top-to-bottom


ordering

Heap-ordered trees

for all keys both subtrees are root


property applies to all nodes in tree (i.e. root contains largest value
in that subtree)

COMPLETE TREE PROPERTY

Heaps are "complete trees

every level is filled in before adding a node to the next


level
the nodes in a given level are filled in from left to right,
with no breaks.

HEAP IMPLEMENTATIONS
BSTs are typically implemented as linked data
structures
Heaps CAN be implemented as linked data
structures

Heaps are TYPICALLY implemented via arrays.


The property of being complete makes array
implementations suitable

ARRAY BASED HEAP IMPLEMENTATION

Simple index calculations allow navigation through


the tree:

left child of node at index i is located at 2i


right child of node at index i is located at 2i+1
parent of node at index i is located at i/2

HEAP INSERTION

Insertion is a two-step process

add new element at bottom-most, rightmost position


reorganise values along path to root to restore heap
property

HEAP INSERTION FIX-UP CODE

Bottom-up heapify:
// force value at a[k] into correct position
void fixUp(Item a[], int k) {
while (k > 1 && less(a[k/2],a[k])) {
swap(a, k, k/2);
k = k/2; // integer division
}
}

HEAP INSERTION

DELETION WITH HEAPS

Deletion is a three-step process

replace root value by bottom-most, rightmost value


remove bottom-most, rightmost value
reorganise values along path from root to restore heap

HEAP DELETION FIX-DOWN CODE

Top-down heapify:

void fixDown(Item a[], int k) {


int done = 0;
while (2*k <= N && !done) {
int j = 2*k; //choose larger of two children
if (j < N && less(a[j], a[j+1])){
j++;
}
if (!less(a[k], a[j])){
done =1;
}else{
swap(a, k, j);
k = j;
}
}
}

EXERCISE:

Show the construction of the heap produced by


inserting

HEAPSFUN

Show the heap after an item is deleted.


Show the heap after another item is deleted.

You might also like