**Introduction**

The **priority queue in the ****data structure** is an extension of the “normal” queue. It is an abstract data type that contains a group of items. It is like the “normal” queue except that the dequeuing elements follow a priority order. The priority order dequeues those items first that have the highest priority. This blog will give you a deeper understanding of the priority queue and its implementation in the C programming language.

**What is a Priority Queue?**

It is an abstract data type that provides a way to maintain the dataset. The “normal” queue follows a pattern of first-in-first-out. It dequeues elements in the same order followed at the time of insertion operation. However, the element order in a priority queue depends on the element’s priority in that queue. The priority queue moves the highest priority elements at the beginning of the priority queue and the lowest priority elements at the back of the priority queue.

It supports only those elements that are comparable. Hence, a **priority queue in the ****data structure** arranges the elements in either ascending or descending order.

You can think of a priority queue as several patients waiting in line at a hospital. Here, the situation of the patient defines the priority order. The patient with the most severe injury would be the first in the queue.

**What are the Characteristics of a Priority Queue?**

A queue is termed as a priority queue if it has the following characteristics:

- Each item has some priority associated with it.
- An item with the highest priority is moved at the front and deleted first.
- If two elements share the same priority value, then the priority queue follows the first-in-first-out principle for de queue operation.

**What are the Types of Priority Queue?**

A priority queue is of two types:

- Ascending Order Priority Queue
- Descending Order Priority Queue

**Ascending Order Priority Queue**

An ascending order priority queue gives the highest priority to the lower number in that queue. For example, you have six numbers in the priority queue that are 4, 8, 12, 45, 35, 20. Firstly, you will arrange these numbers in ascending order. The new list is as follows: 4, 8, 12, 20. 35, 45. In this list, 4 is the smallest number. Hence, the ascending order priority queue treats number 4 as the highest priority.

4 |
8 |
12 |
20 |
35 |
45 |

In the above table, 4 has the highest priority, and 45 has the lowest priority.

**Descending Order Priority Queue**

A descending order priority queue gives the highest priority to the highest number in that queue. For example, you have six numbers in the priority queue that are 4, 8, 12, 45, 35, 20. Firstly, you will arrange these numbers in ascending order. The new list is as follows: 45, 35, 20, 12, 8, 4. In this list, 45 is the highest number. Hence, the descending order priority queue treats number 45 as the highest priority.

45 |
35 |
20 |
12 |
8 |
4 |

In the above table, 4 has the lowest priority, and 45 has the highest priority.

**Implementation of the Priority Queue in Data Structure**

You can implement the priority queues in one of the following ways:

- Linked list
- Binary heap
- Arrays
- Binary search tree

The binary heap is the most efficient method for implementing the **priority queue in the ****data structure**.

The below tables summarize the complexity of different operations in a priority queue.

Operation |
Unordered Array |
Ordered Array |
Binary Heap |
Binary Search Tree |

Insert | 0(1) | 0(N) | 0(log(N)) | 0(log(N)) |

Peek | 0(N) | 0(1) | 0(1) | 0(1) |

Delete | 0(N) | 0(1) | 0(log (N)) | 0(log(N)) |

**Binary Heap**

A binary heap tree organises all the parent and child nodes of the tree in a particular order. In a binary heap tree, a parent node can have a maximum of 2 child nodes. The value of the parent node could either be:

- equal to or less than the value of a child node.
- equal to or more than the value of a child node.

The above process divides the binary heap into two types: max heap and min-heap.

**Max Heap**

The max heap is a binary heap in which a parent node has a value either equal to or greater than the child node value. The root node of the tree has the highest value.

**Inserting an Element in a Max Heap Binary Tree**

You can perform the following steps to insert an element/number in the **priority queue in the data structure**.

- The algorithm scans the tree from top to bottom and left to right to find an empty slot. It then inserts the element at the last node in the tree.
- After inserting the element, the order of the binary tree is disturbed. You must swap the data with each other to sort the order of the max heap binary tree. You must keep shuffling the data until the tree satisfies the max-heap property.

**Algorithm to Insert an Element in a Max Heap Binary Tree**

If the tree is empty and contains no node,

Â Â Â Â create a new parent node newElement.

else (a parent node is already available)

Â Â Â Â insert the newElement at the end of the tree (i.e., last node of the tree from left to right.)

max-heapify the tree

**Deleting an Element in a Max Heap Binary Tree**

- You can perform the following steps to delete an element in the
**Priority Queue in Data Structure**. - Choose the element that you want to delete from the binary tree.
- Shift the data at the end of the tree by swapping it with the last node data.
- Remove the last element of the binary tree.
- After deleting the element, the order of the binary tree is disturbed. You must sort the order to satisfy the property of max-heap. You must keep shuffling the data until the tree meets the max-heap property.

**Algorithm to Delete an Element in a Max Heap Binary Tree**

If the elementUpForDeletion is the lastNode,

delete the elementUpForDeletion

else replace elementUpForDeletion with the lastNode

delete the elementUpForDeletion

max-heapify the tree

**Find the Maximum or Minimum Element in a Max Heap Binary Tree**

In a max heap binary tree, the find operation returns the parent node (the highest element) of the tree.

Algorithm to Find the Max or Min in a Max Heap Binary Tree

return ParentNode

**Program Implementation of the Priority Queue using the Max Heap Binary Tree**

#include <stdio.h>Â
int binary_tree = 10; int max_heap = 0; const int test = 100000; Â void swap( int *x, int *y ) { Â Â int a; Â Â a = *x; Â Â *x= *y; Â Â *y = a; } Â //Code to find the parent in the max heap tree int findParentNode(int node[], int root) { Â Â if ((root > 1) && (root < binary_tree)) { return root/2; Â Â } Â Â return -1; } Â void max_heapify(int node[], int root) { Â Â int leftNodeRoot = findLeftChild(node, root); Â Â int rightNodeRoot = findRightChild(node, root); Â Â Â // finding highest among root, left child and right child Â Â int highest = root; Â Â Â if ((leftNodeRoot <= max_heap) && (leftNodeRoot >0)) { if (node[leftNodeRoot] > node[highest]) { Â Â highest = leftNodeRoot; } Â Â } Â Â Â if ((rightNodeRoot <= max_heap) && (rightNodeRoot >0)) { if (node[rightNodeRoot] > node[highest]) { Â Â highest = rightNodeRoot; } Â Â } Â Â Â Â Â if (highest != root) { swap(&node[root], &node[highest]); Â Â Â Â max_heapify(node, highest); Â Â } } Â void create_max_heap(int node[]) { Â Â int d; Â Â for(d=max_heap/2; d>=1; d–) { Â Â Â Â max_heapify(node, d); Â Â } } Â int maximum(int node[]) { Â Â return node[1]; } Â int find_max(int node[]) { Â Â int maxNode = node[1]; Â Â node[1] = node[max_heap]; Â Â max_heap–; Â Â max_heapify(node, 1); Â Â return maxNode; } void descend_key(int node[], int node, int key) { Â Â A[root] = key; Â Â max_heapify(node, root); } void increase_key(int node[], int root, int key) { Â Â node[root] = key; Â Â while((root>1) && (node[findParentNode(node, root)] < node[root])) { swap(&node[root], &node[findParentNode(node, root)]); root = findParentNode(node, root); Â Â } } Â void insert(int node[], int key) { Â Â max_heap++; Â Â node[max_heap] = -1*test; Â Â increase_key(node, max_heap, key); } Â void display_heap(int node[]) { Â Â int d; Â Â for(d=1; d<=max_heap; d++) { Â Â Â Â printf(“%d\n”,node[d]); Â Â } Â Â printf(“\n”); } Â int main() { Â Â int node[binary_tree]; Â Â insert(node, 10); Â Â insert(node, 4); Â Â insert(node, 20); Â Â insert(node, 50); Â Â insert(node, 1); Â Â insert(node, 15); Â Â Â display_heap(node); Â Â Â printf(“%d\n\n”, maximum(node)); Â Â display_heap(node); Â Â Â printf(“%d\n”, extract_max(node)); Â Â printf(“%d\n”, extract_max(node)); Â Â return 0; } |

**Min Heap**

The min-heap is a binary heap in which a parent node has a value equal to or lesser than the child node value. The root node of the tree has the lowest value.

You can implement the min-heap in the same manner as the max-heap except reverse the order.

**Conclusion**

The examples given in the article are only for explanatory purposes. You can modify the statements given above as per your requirements. In this blog, we learned about the concept of the **priority queue in the data structure**. You can try out the example to strengthen your data structure knowledge.Â Â

If you are curious to learn about data science, check out IIIT-B & upGradâ€™sÂ Executive PG Programme in Data ScienceÂ which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.