# Priority Queue in Data Structure

### 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 : -**

Operations | Insert | Peek | Delete |

Linked List | O(1) | O(n) | O(1) |

Binary Heap | O(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.

- Heapify the tree

**2. Removing an element from the Priority Queue : -**

- Select the element which you want to delete

- Swap that element with last element

- Remove the last element and heapify the tree

**3. Getting an element from the Priority Queue :**

Return the root node without deleting the element of tree

### Priority Queue Implementation in C language

#include <stdio.h> int len = 0; void swap(int *a, int *b) { int temp = *b; *b = *a; *a = temp; } void heapify_Tree(int queue[] , int len , int i) // For Re-Balance the tree { if (len == 1) { printf("the heap has only one element"); } else { int largest_ele= i; int l = 2 * i + 1; int r = 2 * i + 2; if (l < len && queue[l] > queue[largest_ele]) largest_ele = l; if (r < len && queue[r] > queue[largest_ele]) largest_ele = r; if (largest_ele != i) { swap(&queue[i] , &queue[largest_ele]); heapify_Tree(queue , len , largest_ele); } } } void insert(int queue[] , int newNum) // For adding new element in to tree { if (len == 0) { queue[0] = newNum; len += 1; } else { queue[len] = newNum; len += 1; for (int i = len / 2-1 ; i >= 0 ; i--) { heapify_Tree(queue , len , i); } } } void deleteElement(int queue[] , int num) // For removing desired element from the tree { int i; for (i = 0 ; i < len ; i++) { if (num == queue[i]) break ; } swap(&queue[i] , &queue[len - 1]); len -= 1; for (int i = len / 2-1 ; i >= 0 ; i--) { heapify_Tree(queue , len , i); } } void delete(int queue[]) // For extracting max element (root node) from the tree { swap(&queue[0],&queue[len-1]); printf("Removed highest priority element : %d\n" , queue[len-1]); len -= 1; for (int i = len-1 ; i >= 0 ; i--) { heapify_Tree(queue , len , i); } } void printQueue(int queue[] , int len) // For printing the elements { for (int i = 0 ; i < len ; ++i) printf("%d " , queue[i]); printf("\n"); } int main() // Driver code { int queue[10]; insert(queue , 3); insert(queue , 4); insert(queue , 9); insert(queue , 5); insert(queue , 2); insert(queue , 1); insert(queue , 0); printf("Max-Heap queue : "); printQueue(queue , len); deleteElement(queue , 4); printf("After deleting an element : "); printQueue(queue , len); delete(queue); printf("After deleting the highest priority element : "); printQueue(queue , len); }

__Output :__

### 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