In this tutorial, you will learn about the Priority Queue Data Structure, its operations and basic implementation example with Java

Priority Queue Data Structure

Priority Queue Data Structure is an extension of a regular queue data structure with additional properties

  • Each element has a priority associated with it
  • An element with high priority is served before an element with low priority
  • If two elements have the same priority, they are served according to the order in which they are enqueue

Basic operations

  • enqueue(element), inserts an element into the queue with an associated priority
  • dequeue(), retrieves and removes the highest priority element
  • front() aka peek(), retrieves without removing the highest priority element

Implementations

You can implement a priority queue with either an array, a linked list or a heap (although priority queues conceptually distinct from heaps)

Implementation example with a max heap


MaxHeapByArray is defined in Heap Data Structure