typescript-ds-lib
Version:
A collection of TypeScript data structure implementations
59 lines • 1.59 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.PriorityQueue = void 0;
const heap_1 = require("./heap");
const base_collection_1 = require("./base-collection");
class PriorityQueue extends base_collection_1.BaseCollection {
heap;
constructor(comparator = (a, b) => a > b) {
super();
this.heap = new heap_1.Heap(comparator);
}
/**
* Adds an element with a priority to the queue.
* Lower priority numbers have higher precedence.
*/
push(element) {
this.heap.push(element);
}
/**
* Removes and returns the highest priority element from the queue, or undefined if queue is empty.
*/
pop() {
return this.heap.pop();
}
/**
* Returns the highest priority element without removing it, or undefined if queue is empty.
*/
front() {
return this.heap.top();
}
/**
* Checks if the queue is empty. Returns true if empty, false otherwise.
*/
isEmpty() {
return this.heap.isEmpty();
}
/**
* Returns the number of elements in the queue.
*/
size() {
return this.heap.size();
}
/**
* Removes all elements from the queue.
*/
clear() {
this.heap.clear();
}
/**
* Checks if two priority queues are equal.
*/
equals(other) {
if (!other || !(other instanceof PriorityQueue))
return false;
return this.heap.equals(other.heap);
}
}
exports.PriorityQueue = PriorityQueue;
//# sourceMappingURL=priority-queue.js.map
;