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:
Let's understand the priority queue through an example. We have a priority queue that contains the following values: 1, 3, 4, 8, 14, 22 All the values are arranged in ascending order. Now, we will observe how the priority queue will look after performing the following operations:
Types of Priority QueueThere are two types of 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 INFO list contains the data elements, PRN list contains the priority numbers of each data element available in the INFO list, and LINK basically contains the address of the next node. Let's create the priority queue step by step. In the case of priority queue, lower priority number is considered the higher priority, i.e., lower priority number = higher priority. Step 1: In the list, lower priority number is 1, whose data value is 333, so it will be inserted in the list as shown in the below diagram: Step 2: After inserting 333, priority number 2 is having a higher priority, and data values associated with this priority are 222 and 111. So, this data will be inserted based on the FIFO principle; therefore 222 will be added first and then 111. Step 3: After inserting the elements of priority 2, the next higher priority number is 4 and data elements associated with 4 priority numbers are 444, 555, 777. In this case, elements would be inserted based on the FIFO principle; therefore, 444 will be added first, then 555, and then 777. Step 4: After inserting the elements of priority 4, the next higher priority number is 5, and the value associated with priority 5 is 666, so it will be inserted at the end of the queue. 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. Analysis of complexities using different implementations
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:
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.
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.
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 queueThe following are the applications of the priority queue:
Program to create the priority queue using the binary max heap. In the above program, we have created the following functions:
Output Next TopicDS Tree |