Priority Queues

Building up on the previous section, lets take a look at Priority Queues. A priority queue is an extension of a queue that allows us to add elements to the queue based on their priority. The element with higher priors will be placed at the beginning of the queue.

In order to build this priority queue, we will first use the rajatQueue function from previous section to build two empty queues. One of these queues will contain elements of higher priority, while the other queue will contain elements of low priority.

The gist of a priority queue is same as that of a normal queue . The only thing different here is that the elements are now sorted based on their priority.

But implementing simple difference in our priority queue will be quite a work. Lets start by creating a function to add an element to the priority queue.

In this function, we will now have two arguments. The first argument will be the element that we want to add to the queue, and the second argument will be a boolean that will indicate if the element is of high priority or not. We can set it to any boolean value as default.

add(x, high = false) {

high ? highPriority.add(x) : lowPriority.add(x)

}

Inside the add function, if the element x is deemed to be of high priority, then it will be added to the highPriority queue. Else, it will be added to the lowPriority queue.

We can write the remove function in a similar way. But let’s try something a little different by added a condition to check if the highPriority queue is empty or not. If the highPriority is not empty, then we want the code to remove the element from it. Else, we will go to the lowPriority queue and remove an element from there.

remove() {

if (!highPriority.empty()) {

return highPriority.remove()

}

return lowPriority.dequeue()

}

The next function will follow the same basic principle. First check if the highPriority queue is empty. If it is not empty then check the next element to be removed from that queue. Else, go to the low priority queue and check the next element to be removed from it.

next() {

if (!highPriority.empty()) {

return highPriority.next()

}

return lowPriority.next()

The length of our priority queue is going to be cumulation of the length of the high and low priority queues.

length() {

return (

highPriority.length + lowPriority.length

)

}

Finally, we will check if the priority queue is empty or not by check if both the queues (high and low) are empty or not.

empty() {

return (

highPriority.empty() && lowPriority.empty()

)

}

With that, we have our priority queue builder! 🙌 Let’s try it out and build a queue of tasks with different priorities as shown below:

const tasks = priorityQueue()

tasks.add('Learn React', false)

tasks.add('Learn Vue', false)

tasks.add('Learn JavaScript', true)

Here, the Learn JavaScript task is indicated to have a high priority as its add property has a boolean value of true as second argument. Therefore this tasks will be at the top of our priority queue.