Priority Queue

A priority queue is a special kind of queue, in priority queue we give some priority to an element and according to this priority an element can be served in the queue.

Generally, the value of element is treated as the priority of an element. Like, the element has highest value treated as the highest priority element as well as we assume that if element has lowest value treated as the highest priority element in the other case, we can give the priorities to elements according to our requirements.

Difference between in normal queue and a Priority Queue

If we talk about the normal queue works on first in first out rule whereas, in a priority queue element is removed according to the priority, an element with the highest priority is removed first.

Types of Priority Queue

There are two types of priority queue :

a Max-Priority Queue and a Min- Priority Queue. In these types, the priority queue stores the collections of elements and it is only able to provide the most “Extreme” element.

Implementation of Priority Queue

There are many ways to implement priority queue like, array, linked list, binary heap etc. If we talk about heap data structure then it provides an efficient way of implementing priority queues.

Complexity of Priority Queue : –

OperationsInsertPeekDelete
Linked ListO(1)O(n)O(1)
Binary HeapO(1)O(log n)O(log n)

Operations on Priority Queue

The operations of priority queues are inserting element, removing element, getting element. We are using Binary heap data structure for implementing of priority queue.

1. Add an new element into Priority Queue :

Here we are using max heap data structure for priority queue

  • Add new element at the end of tree.
Priority Queue in Data Structure
  • Heapify the tree
Priority Queue in Data Structure

2. Removing an element from the Priority Queue : –

  • Select the element which you want to delete
Priority Queue in Data Structure
  • Swap that element with last element
Priority Queue in Data Structure
  • Remove the last element and heapify the tree
Priority Queue in Data Structure

3. Getting an element from the Priority Queue :

Return the root node without deleting the element of tree

Priority Queue Implementation in C language

Output :

Priority Queue in Data Structure

Applications of Priority Queue

  • In Hauffman Coding
  • For Implementing Stack
  • In Dijkstra’s Algorithm
  • In Prim’s Minimum Spanning Tree
  • Process Scheduling in OS
  • All Queue Applications where Priority Involved

Pin It on Pinterest

Share This