## What is a priority queue?A priority queue is an abstract data type that behaves similarly to the normal queue except that each element has some priority, i.e., the element with the highest priority would come first in a priority queue. The priority of the elements in a priority queue will determine the order in which elements are removed from the priority queue. The priority queue supports only comparable elements, which means that the elements are either arranged in an ascending or descending order. For example, suppose we have some values like 1, 3, 4, 8, 14, 22 inserted in a priority queue with an ordering imposed on the values is from least to the greatest. Therefore, the 1 number would be having the highest priority while 22 will be having the lowest priority. ## Characteristics of a Priority queueA priority queue is an extension of a queue that contains the following characteristics: - Every element in a priority queue has some priority associated with it.
- An element with the higher priority will be deleted before the deletion of the lesser priority.
- If two elements in a priority queue have the same priority, they will be arranged using the FIFO principle.
We have a priority queue that contains the following values:
All the values are arranged in ascending order. Now, we will observe how the priority queue will look after performing the following operations: **poll():**This function will remove the highest priority element from the priority queue. In the above priority queue, the '1' element has the highest priority, so it will be removed from the priority queue.**add(2):**This function will insert '2' element in a priority queue. As 2 is the smallest element among all the numbers so it will obtain the highest priority.**poll():**It will remove '2' element from the priority queue as it has the highest priority queue.**add(5):**It will insert 5 element after 4 as 5 is larger than 4 and lesser than 8, so it will obtain the third highest priority in a priority queue.
## Types of Priority Queue
**Ascending order priority queue:**In ascending order priority queue, a lower priority number is given as a higher priority in a priority. For example, we take the numbers from 1 to 5 arranged in an ascending order like 1,2,3,4,5; therefore, the smallest number, i.e., 1 is given as the highest priority in a priority queue.**Descending order priority queue:**In descending order priority queue, a higher priority number is given as a higher priority in a priority. For example, we take the numbers from 1 to 5 arranged in descending order like 5, 4, 3, 2, 1; therefore, the largest number, i.e., 5 is given as the highest priority in a priority queue.
## Representation of priority queueNow, we will see how to represent the priority queue through a one-way list. We will create the priority queue by using the list given below in which
## Implementation of Priority QueueThe priority queue can be implemented in four ways that include arrays, linked list, heap data structure and binary search tree. The heap data structure is the most efficient way of implementing the priority queue, so we will implement the priority queue using a heap data structure in this topic. Now, first we understand the reason why heap is the most efficient way among all the other data structures.
## What is Heap?A heap is a tree-based data structure that forms a complete binary tree, and satisfies the heap property. If A is a parent node of B, then A is ordered with respect to the node B for all nodes A and B in a heap. It means that the value of the parent node could be more than or equal to the value of the child node, or the value of the parent node could be less than or equal to the value of the child node. Therefore, we can say that there are two types of heaps: **Max heap:**The max heap is a heap in which the value of the parent node is greater than the value of the child nodes.**Min heap:**The min heap is a heap in which the value of the parent node is less than the value of the child nodes.
Both the heaps are the binary heap, as each has exactly two child nodes. ## Priority Queue OperationsThe common operations that we can perform on a priority queue are insertion, deletion and peek. Let's see how we can maintain the heap data structure. **Inserting the element in a priority queue (max heap)**
If we insert an element in a priority queue, it will move to the empty slot by looking from top to bottom and left to right. If the element is not in a correct place then it is compared with the parent node; if it is found out of order, elements are swapped. This process continues until the element is placed in a correct position. **Removing the minimum element from the priority queue**
As we know that in a max heap, the maximum element is the root node. When we remove the root node, it creates an empty slot. The last inserted element will be added in this empty slot. Then, this element is compared with the child nodes, i.e., left-child and right child, and swap with the smaller of the two. It keeps moving down the tree until the heap property is restored. ## Applications of Priority queue
- It is used in the Dijkstra's shortest path algorithm.
- It is used in prim's algorithm
- It is used in data compression techniques like Huffman code.
- It is used in heap sort.
- It is also used in operating system like priority scheduling, load balancing and interrupt handling.
**int parent(int i):**This function returns the index of the parent node of a child node, i.e., i.**int left_child(int i):**This function returns the index of the left child of a given index, i.e., i.**int right_child(int i):**This function returns the index of the right child of a given index, i.e., i.**void moveUp(int i):**This function will keep moving the node up the tree until the heap property is restored.**void moveDown(int i):**This function will keep moving the node down the tree until the heap property is restored.**void removeMax():**This function removes the element which is having the highest priority.**void insert(int p):**It inserts the element in a priority queue which is passed as an argument in a function**.****void delete(int i):**It deletes the element from a priority queue at a given index.**int get_Max():**It returns the element which is having the highest priority, and we know that in max heap, the root node contains the element which has the largest value, and highest priority.**int get_Min():**It returns the element which is having the minimum priority, and we know that in max heap, the last node contains the element which has the smallest value, and lowest priority.
Next TopicDS Tree |