Priority Queues
other uses for heaps
A Priority Queue maintains a set of S elements, each element associated with a key. Priority Queues are used for queuing systems and event simulation.
Methods:
Heap-Insert ( S, x ) :: S ¬ S È { x }
Maximum ( S ) :: returns max
Heap-Extract-Max ( S ) :: returns and removes max
Running Times:
Heap-Extract-Max : O ( lg n )
Heap-Insert : O ( lg n )
Heap-Extract-Max (A)
-
if heap-size[A] < 1
-
then error "heap underlfow"
-
max ¬ A[1]
-
A[1] ¬ A[heap-size[A]}
-
heap-size[A] ¬ heap-size[A] - 1
-
Heapify ( A, 1 )
-
return max
Heap-Insert ( A, key )
-
heap-size[A] ¬ heap-size[A] + 1
-
i ¬ heap-size[A]
-
while i > 1 and A[Parent (i)] < key
-
do A[i] ¬ A[Parent (i)]
-
i ¬ Parent (i)
-
A[i] ¬ key

The operation of Heap-Insert
|