






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Class notes on priority queues and binary heaps from cop 3503 – computer science ii. Topics covered include the structure and ordering properties of binary heaps, basic operations like insertion and deletion, and sorting from a binary heap. The document also discusses an alternative technique for handling insertion and its associated problems.
Typology: Study notes
1 / 11
This page cannot be seen from the preview
Don't miss anything!







COP 3503 – Computer Science II – CLASS NOTES - DAY #
Priority Queues
A priority queue is a structure where the highest priority item is the next item
that will be dequeued.
Implementation typically sets the highest priority item to have the lowest
integer value as its priority.
Thus, the node with the lowest integer value should always be at the head of the
queue.
If implemented as a binary heap, insertion and deletion can be done in
logarithmic time in the worst case.
Binary Heaps
Can be implemented as an array (just like the queue)
Combines the best properties of binary search trees and queues.
Insertion: average case – O(1), worst case – O(log 2 N).
DeleteMin: worst case – O(log 2
FindMin: worst case – O(1).
Like a binary search tree, the binary heap has two properties: a structure property
and an ordering property. Any operation has the potential for destroying both the
structure and ordering properties of the binary heap (hereafter called “the heap”).
Therefore, operations on the heap should not be allowed to terminate until both
properties are preserved.
Structure Property
A tree is the only structure that gives logarithmic time bounds, therefore it
makes sense to organize the heap as a tree.
To ensure a logarithmic worst case time bound, we will need to ensure that the
tree is balanced.
Note: a complete tree requires that every internal node has the maximum
number of children. For a binary tree this means that each internal node
must have two children.
A complete binary tree has several useful properties that make it well suited to
the application of heaps.
contains N nodes). A complete tree of height H has between 2
H and 2
H+ 1
nodes.
the level-order traversal of the tree can be stored in single-dimension array.
The root node is stored in array position 1 (position 0 is utilized later by the
priority queue). For any element in array position i , its left child will be
found in position 2i and its right child will be found in position 2i+1. To
determine it a node at index i has children test to see if 2i > number of
elements. To determine the parent of a node at index i , check at index i/2 .
Using an array to represent a tree is called an implicit tree representation. This
method is very fast on most systems and the traversal operation become trivial
and extremely fast.
Example:
Left child of node “A” ( index 1) is located at index (2*1) = 2 or node “B”
Right child of node “A” (index 1) is located at index (2*1) +1 = 3 or node “C”
Parent of node “F” (index 6) is located at 6/2 = 3 or node “C”
Parent of node “I” (index 9) is located at 9/2 = 4 or node “D”
Child of node “H” (index 8) is located at (2*8) = 16 out of range so no child
exists.
Final tree maintaining structure and ordering properties.
DeleteMin:
the smaller of them with the key value of the root. Interchange if the root
key value is greater than that of the smaller child key value.
applying step #3.
Example: return node “1”
Example: return 1
Sorting From A Binary Heap
Recall that the binary heap has both an ordering property and a structure
property. The ordering property ensures that the items in the heap are basically
sorted from minimum (root) value to maximum values (leafs).
Removing (or reading) everything results in a sort.
If this can be done in O(N log 2 N) time, then we have found a “non-
comparison” based sorting algorithm.
The insertion technique that we used for inserting items into the binary heap,
inserted N items and required O(log 2 N) time to find the right spot at which to do
the insertion. Thus the time required to insert N items is: O(N log 2
Example: Illustration of linear time fixHeap( ) using percolateDown( )
initial tree: has structure property, violates ordering property
no changes
swap 25, 45
no changes
swap 17, 20
no changes
This technique is not without its problems, which include:
Requires 2N space as 2 arrays are needed, one for the unordered heap and one
created in heap order by fixHeap( ).
Fix: This problem can be solved by using a “sliding heap” and altering the
method fixHeap( ) to return the largest item. As maximal items are returned
we remove them from the heap and decrease the size of the heap by one.
The removed item is then placed into the cell of the array which has been
“freed” from the heap.
Example:
heap
end of
heap
heap
end of
heap heap
end of
heap
heap
end of
heap
heap
end of
heap
When the index counter (the sliding part of the heap) gets to 0, the resulting
array is in heap order.
heap
end of
heap
heap
end of
heap
heap
end of
heap
heap
end of
heap
heap
all items in
the heap