A priority queue is an abstract data type similar to a regular queue or stack but where each element has an associated priority; elements are served in order of their priority rather than their insertion order. It supports at minimum the operations of inserting an element and extracting the element with the highest (or lowest) priority. Priority queues are commonly implemented using binary heaps, and they are essential in algorithms such as Dijkstra's shortest path, A* search, Prim's minimum spanning tree, and task scheduling in operating systems.
| Implementation | Insert | Extract Min/Max | Decrease Key | Space |
|---|---|---|---|---|
| Unsorted Array | O(1) | O(n) | O(n) | O(n) |
| Sorted Array | O(n) | O(1) | O(n) | O(n) |
| Binary Heap | O(log n) | O(log n) | O(log n) | O(n) |
| Fibonacci Heap | O(1) amortized | O(log n) amortized | O(1) amortized | O(n) |
| Binomial Heap | O(log n) | O(log n) | O(log n) | O(n) |
Wikimedia Commons, CC BY-SA
A heap is a specialized tree-based data structure that satisfies the heap property: in a max-heap, each parent node is greater than or equal to its children; in a min-heap, each parent is less than or equal to its children. Heaps are typically implemented as complete binary trees stored in arrays, enabling O(1) access to the minimum or maximum element. They are the underlying structure of priority queues and are used in heap sort and Dijkstra's shortest path algorithm.
A binary tree is a hierarchical data structure in which each node has at most two children, referred to as the left child and the right child. It is one of the most fundamental structures in computer science, forming the basis for more specialized trees like binary search trees and heaps. Binary trees are widely used in expression parsing, Huffman coding, and database indexing.
A disjoint set (also called union-find or merge-find set) is a data structure that keeps track of a collection of non-overlapping sets and supports two primary operations: Union (merging two sets) and Find (determining which set an element belongs to by returning a representative). With path compression and union by rank optimizations, operations run in nearly O(1) amortized time per operation (Inverse Ackermann function). Disjoint sets are foundational in Kruskal's minimum spanning tree algorithm, network connectivity checking, and image segmentation.
The term "priority queue" emerged in computer science literature in the 1960s and 1970s as a generalization of queues where ordering is based on priority rather than arrival time. The word "priority" comes from the Latin "prioritas," meaning "fact of being earlier."