Priority queue

Definition

A priority queue is an abstract data structure consisting of elements that have an associated value called a key or priority that carries the following operations:
- Insert(Q,x): insert element x in the priority queue Q;
- ExtractMax(Q): extracts the maximum value element from the priority queue Q.

Observation!

Analogously, a min-priority queue can be defined, and we are interested in extracting the minimum priority element from the queue.
There are many applications of priority queues. For example, scheduling the execution of programs on a computer: the priority queue holds the programs to be executed according to their relative priorities. When the execution of a program ends or is interrupted, the program with the highest priority in the queue is selected and launched(operation ExtractMax). Adding a new program to the queue is done with the operation Insert.

Introductory Notions

  1. Trees
  2. Binary trees
  3. Heaps
There are various ways to implement a priority queue: as a vector, as a chained list, ordered or not, etc. Each of these variants optimizes one of the two operations, the other being performed in linear time. A simple data structure that allows the implementation of both operations in logarithmic time is the heap. Thus, for the Insert operation, it is used the function InsertHeap and for the Extract operation, it is used the function ExtractMax.