private items: { value: any; priority: number }[];
// Add item with priority
enqueue(value: any, priority: number): void {
this.items.push({ value, priority });
this.bubbleUp(this.items.length - 1);
// Remove and return highest priority item
dequeue(): any | undefined {
if (this.items.length === 0) return undefined;
if (this.items.length === 1) return this.items.pop()!.value;
const min = this.items[0]!.value;
this.items[0] = this.items.pop() as { value: any; priority: number };
// Helper: bubble up to maintain heap property
private bubbleUp(index: number): void {
const parent = Math.floor((index - 1) / 2);
this.items[index]!.priority < this.items[parent]!.priority
if (this.items[index] && this.items[parent]) {
[this.items[index], this.items[parent]] = [
// Helper: bubble down to maintain heap property
private bubbleDown(index: number): void {
const left = 2 * index + 1;
const right = 2 * index + 2;
left < this.items.length &&
this.items[left]!.priority < this.items[smallest]!.priority
right < this.items.length &&
this.items[right]!.priority < this.items[smallest]!.priority
if (smallest !== index) {
[this.items[index]!, this.items[smallest]!] = [
this.bubbleDown(smallest);
const pq = new PriorityQueue();
console.log(pq.dequeue()); // Output: "task2" (priority 1)
console.log(pq.dequeue()); // Output: "task1" (priority 2)