UNPKG

es-next-tools

Version:

A comprehensive utility library for JavaScript and TypeScript that provides a wide range of functions for common programming tasks, including mathematical operations, date manipulations, array and object handling, string utilities, and more.

71 lines (70 loc) 2.37 kB
/** * Represents a priority queue using a binary heap. * @template T The type of elements in the priority queue. */ export class PriorityQueueHeap { heap = []; /** * Inserts an item into the priority queue. * @param {T} value - The item to insert. * @param {number} priority - The priority of the item. */ enqueue(value, priority) { this.heap.push({ value, priority }); this.bubbleUp(); } /** * Removes and returns the item with the highest priority. * @returns {T | undefined} The item removed, or undefined if empty. */ dequeue() { if (this.heap.length === 0) return undefined; const top = this.heap[0]; const bottom = this.heap.pop(); if (this.heap.length > 0 && bottom) { this.heap[0] = bottom; this.bubbleDown(); } return top.value; } bubbleUp() { let index = this.heap.length - 1; while (index > 0) { const parentIndex = Math.floor((index - 1) / 2); if (this.heap[index].priority >= this.heap[parentIndex].priority) break; [this.heap[index], this.heap[parentIndex]] = [this.heap[parentIndex], this.heap[index]]; index = parentIndex; } } bubbleDown() { let index = 0; const length = this.heap.length; const element = this.heap[0]; while (true) { let leftChildIndex = 2 * index + 1; let rightChildIndex = 2 * index + 2; let leftChild, rightChild; let swap = null; if (leftChildIndex < length) { leftChild = this.heap[leftChildIndex]; if (leftChild.priority < element.priority) { swap = leftChildIndex; } } if (rightChildIndex < length) { rightChild = this.heap[rightChildIndex]; if ((swap === null && rightChild.priority < element.priority) || (swap !== null && rightChild.priority < leftChild.priority)) { swap = rightChildIndex; } } if (swap === null) break; this.heap[index] = this.heap[swap]; this.heap[swap] = element; index = swap; } } }