PriorityQueue()

This class is a Priority Queue implementation. This data-structure allows add and remove elements with priorities. The queue is always ordered by the highest priority.

new PriorityQueue()

Creates a PRIORITY-QUEUE data-structure

Example
const { PriorityQueue } = require('data-structures-algorithms-js');
const pq = new PriorityQueue();

Methods

back() → {*}

Returns the element at the back of Priority Queue

Returns:
* -

The element at the back

Example
const { PriorityQueue } = require('data-structures-algorithms-js');
const pq = new PriorityQueue();

pq.push(6); 
pq.push(3, -2);

pq.back(); //returns 3;

clear()

Resets the the Priority Queue

Example
const { PriorityQueue } = require('data-structures-algorithms-js');
const pq = new PriorityQueue();

pq.push(15);

pq.clear(); // now Priority Queue is empty

front() → {*}

Returns the element at the front of the Priority Queue

Returns:
* -

The element at the front

Example
const { PriorityQueue } = require('data-structures-algorithms-js');
const pq = new PriorityQueue();

pq.push(6); 
pq.push(3);

pq.front(); //returns 6;

isEmpty() → {Boolean}

Returns if the Priority Queue is empty

Returns:
Boolean
Example
const { PriorityQueue } = require('data-structures-algorithms-js');
const pq = new PriorityQueue();

pq.isEmpty(); // returns true;

pq.push(23, 9);

pq.isEmpty(); //returns false;

pop()

removes the first element at the Priority Queue

Example
const { PriorityQueue } = require('data-structures-algorithms-js');
const pq = new PriorityQueue();

pq.push(3, 8);
pq.push(9, 10);

pq.pop(); //removes number 9 from the Priority Queue

push(element, priority)

Adds a element at the Priority-Queue

Parameters:
Name Type Default Description
element *

Element passed to insert

priority Integer 0

Priority can be any integer number

Example
const { PriorityQueue } = require('data-structures-algorithms-js');
const pq = new PriorityQueue();

pq.push(10, 2); //inserts element 10 with priority 2

size() → {Number}

Returns the size of the Priority Queue

Returns:
Number -

The number of elements in the Priority Queue

Example
const { PriorityQueue } = require('data-structures-algorithms-js');
const pq = new PriorityQueue();

pq.size(); // returns 0;

pq.push(8, 10);

pq.size(); //returns 1;