Priority Queue | Set 1 (Introduction)Priority Queue is an abstract data type, which is similar to a queue, however, in the priority queue, every element has some priority. The priority of the elements in a priority queue determines the order in which elements are removed from the priority queue. Therefore all the elements are either arranged in an ascending or descending order. So, a priority Queue is an extension of the queue with the following properties.
In the below priority queue, an element with a maximum ASCII value will have the highest priority. The elements with higher priority are served first. A typical priority queue supports the following operations: 2) Deletion: As you know that in a max heap, the maximum element is the root node. And it will remove the element which has maximum priority first. Thus, you remove the root node from the queue. This removal creates an empty slot, which will be further filled with new insertion. Then, it compares the newly inserted element with all the elements inside the queue to maintain the heap invariant. 3) Peek: This operation helps to return the maximum element from Max Heap or minimum element from Min Heap without deleting the node from the priority queue. Types of Priority Queue:1) Ascending Order: As the name suggests, in ascending order priority queue, the element with a lower priority value is given a higher priority in the priority list. For example, if we have the following elements in a priority queue arranged in ascending order like 4,6,8,9,10. Here, 4 is the smallest number, therefore, it will get the highest priority in a priority queue. 2) Descending order: The root node is the maximum element in a max heap, as you may know. It will also remove the element with the highest priority first. As a result, the root node is removed from the queue. This deletion leaves an empty space, which will be filled with fresh insertions in the future. The heap invariant is then maintained by comparing the newly inserted element to all other entries in the queue. How to Implement Priority Queue?Priority queue can be implemented using the following data structures:
Let’s discuss all these in detail.
C++
Output
16
14
12
2) Using Linked List: In a LinkedList implementation, the entries are sorted in descending order based on their priority. The highest priority element is always added to the front of the priority queue, which is formed using linked lists. The functions like push(), pop(), and peek() are used to implement priority queue using a linked list and are explained as follows:
C++
Output
7 4 5 6
Refer to this article for more details.
3) Using Heaps:Binary Heap is generally preferred for priority queue implementation because heaps provide better performance compared to arrays or LinkedList. Operation on Binary Heap are as follows:
4) Using Binary Search Tree: A Self-Balancing Binary Search Tree like AVL Tree, Red-Black Tree, etc. can also be used to implement a priority queue. Operations like peek(), insert() and delete() can be performed using BST.
What is the difference between Priority Queue and Normal Queue? There is no priority attached to elements in a queue, the rule of first-in-first-out(FIFO) is implemented whereas, in a priority queue, the elements have a priority. The elements with higher priority are served first. Applications of Priority Queue:
See also:
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Article Tags :
Queue
priority-queue Practice Tags :
Queue priority-queue |