new PriorityQueueMax()
In computer science, a priority queue is an abstract data type
which is like a regular queue or stack data structure, but where additionally
each element has a "priority" associated with it. In a priority queue, an
element with high priority is served before an element with low priority. If
two elements have the same priority, they are served according to their order
in the queue. Full wikipedia article at:
https://en.wikipedia.org/wiki/Priority_queue
- Source:
Example
const PriorityQueueMax = require('dstructures').PriorityQueueMax;
const myPriorityQueueMax = new PriorityQueueMax();
Methods
dequeue() → {Boolean|Any}
Removes and returns the first element in a priority queue
(the element with most priority).
- Source:
Returns:
- Type:
-
Boolean
|Any
Returns false if a priority queue is
empty, otherwise the first element in a priority queue.
Example
PriorityQueueMax.enqueue('Cat', 1); // ['Cat']
PriorityQueueMax.enqueue('Dog', 2); // ['Dog', 'Cat']
PriorityQueueMax.enqueue('Fox', 3); // ['Fox', 'Dog', 'Cat']
PriorityQueueMax.dequeue(); // ['Dog', 'Cat']
enqueue(element, priorityopt) → {Boolean|Void}
Adds element in a priority queue.
- Source:
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
element |
any
|
Given element. | ||
priority |
Number
|
<optional> |
0 | Priority defaults to 0 if is not present. |
Returns:
- Type:
-
Boolean
|Void
Returns false if 'priority' is not number or
'element' is undefined or null.
Example
PriorityQueueMax.enqueue('Cat', 1); // ['Cat']
PriorityQueueMax.enqueue('Dog', 2); // ['Dog', 'Cat']
head() → {Boolean|Any}
Returns the first element in a priority queue.
- Source:
Returns:
- Type:
-
Boolean
|Any
Returns false if a priority queue is
empty, otherwise the first element in a priority queue.
Example
PriorityQueueMax.enqueue('Cat', 1); // ['Cat']
PriorityQueueMax.enqueue('Dog', 2); // ['Dog', 'Cat']
PriorityQueueMax.enqueue('Fox', 3); // ['Fox', 'Dog', 'Cat']
PriorityQueueMax.head();// 'Fox' , ['Fox', 'Dog', 'Cat']
isEmpty() → {Boolean}
Checks if a priority queue is empty.
- Source:
Returns:
- Type:
-
Boolean
Returns true if a priority queue is empty,
otherwise fasle.
Example
PriorityQueueMax.isEmpty(); // true
PriorityQueueMax.enqueue('Cat', 1); // ['Cat']
PriorityQueueMax.isEmpty(); // false
toArray() → {Array}
Returns array representation of a priority queue.
- Source:
Returns:
- Type:
-
Array
Returns array representation of a priority queue.