/** * A priority queue implementation using a binary heap. * Higher priority values are dequeued first. * * ## Features * - **enqueue(value, priority)**: Add element with priority * - **enqueueBack(value)**: Add element to the back with lowest priority * - **dequeue()**: Remove and return highest priority element * - **peek()**: View highest priority element without removing * - **peekPriority()**: View highest priority value * - **size**: Get number of elements * - **isEmpty**: Check if queue is empty * - **clear()**: Remove all elements * - **toArray()**: Get all elements as array * - **toArrayWithPriorities()**: Get all elements with priorities * * ## Time Complexity * - enqueue: O(log n) * - enqueueBack: O(log n) * - dequeue: O(log n) * - peek: O(1) * - peekPriority: O(1) * * @example * ```typescript * const queue = new PriorityQueue(); * queue.enqueue("low", 1); * queue.enqueue("high", 3); * queue.enqueue("medium", 2); * * console.log(queue.dequeue()); // "high" * console.log(queue.dequeue()); // "medium" * console.log(queue.dequeue()); // "low" * ``` * * @example * ```typescript * // Initialize with elements * const queue = new PriorityQueue([ * { value: "task1", priority: 1 }, * { value: "task2", priority: 3 }, * { value: "task3", priority: 2 } * ]); * * console.log(queue.peek()); // "task2" * console.log(queue.peekPriority()); // 3 * ``` * * @template T - The type of elements stored in the queue */ export declare class PriorityQueue { private heap; private minPriority; /** * Creates a new PriorityQueue instance. * @param initialElements - Optional array of initial elements with priorities * @example * ```typescript * const queue = new PriorityQueue(); * // or * const queue = new PriorityQueue([ * { value: "item1", priority: 10 }, * { value: "item2", priority: 5 } * ]); * ``` */ constructor(initialElements?: { value: T; priority: number; }[]); /** * Returns the number of elements in the queue. * @returns The number of elements in the queue * @example * ```typescript * const queue = new PriorityQueue(); * queue.enqueue("item", 1); * console.log(queue.size); // 1 * ``` */ get size(): number; /** * Checks if the queue is empty. * @returns True if the queue is empty, false otherwise * @example * ```typescript * const queue = new PriorityQueue(); * console.log(queue.isEmpty); // true * queue.enqueue("item", 1); * console.log(queue.isEmpty); // false * ``` */ get isEmpty(): boolean; /** * Adds an element to the queue with a specified priority. * Higher priority values are dequeued first. * @param value - The value to add * @param priority - The priority value (higher values have higher priority) * @example * ```typescript * const queue = new PriorityQueue(); * queue.enqueue("low", 1); * queue.enqueue("high", 10); * queue.enqueue("medium", 5); * console.log(queue.dequeue()); // "high" * ``` */ enqueue(value: T, priority: number): void; /** * Adds an element to the end of the queue with lowest priority. * This element will be dequeued last (FIFO for equal lowest priority). * * @param value - The value to add * @example * ```typescript * const queue = new PriorityQueue(); * queue.enqueue("high", 10); * queue.enqueue("medium", 5); * queue.enqueueBack("back1"); * queue.enqueueBack("back2"); * console.log(queue.dequeue()); // "high" * console.log(queue.dequeue()); // "medium" * console.log(queue.dequeue()); // "back1" * console.log(queue.dequeue()); // "back2" * ``` */ enqueueBack(value: T): void; /** * Removes and returns the element with the highest priority. * @returns The element with highest priority, or undefined if queue is empty * @example * ```typescript * const queue = new PriorityQueue(); * queue.enqueue("low", 1); * queue.enqueue("high", 10); * console.log(queue.dequeue()); // "high" * console.log(queue.dequeue()); // "low" * console.log(queue.dequeue()); // undefined * ``` */ dequeue(): T | undefined; /** * Returns the element with the highest priority without removing it. * @returns The element with highest priority, or undefined if queue is empty * @example * ```typescript * const queue = new PriorityQueue(); * queue.enqueue("low", 1); * queue.enqueue("high", 10); * console.log(queue.peek()); // "high" * console.log(queue.size); // 2 (element not removed) * ``` */ peek(): T | undefined; /** * Returns the priority of the element with the highest priority. * @returns The highest priority value, or undefined if queue is empty * @example * ```typescript * const queue = new PriorityQueue(); * queue.enqueue("low", 1); * queue.enqueue("high", 10); * console.log(queue.peekPriority()); // 10 * ``` */ peekPriority(): number | undefined; /** * Removes all elements from the queue. * @example * ```typescript * const queue = new PriorityQueue(); * queue.enqueue("item1", 1); * queue.enqueue("item2", 2); * console.log(queue.size); // 2 * queue.clear(); * console.log(queue.size); // 0 * console.log(queue.isEmpty); // true * ``` */ clear(): void; /** * Returns an array of all elements in the queue (without removing them). * The order is not guaranteed to be sorted by priority. * @returns Array of all elements in the queue * @example * ```typescript * const queue = new PriorityQueue(); * queue.enqueue("low", 1); * queue.enqueue("high", 10); * queue.enqueue("medium", 5); * console.log(queue.toArray()); // ["high", "medium", "low"] (order may vary) * ``` */ toArray(): T[]; /** * Returns an array of all elements with their priorities. * The order is not guaranteed to be sorted by priority. * @returns Array of all elements with their priorities * @example * ```typescript * const queue = new PriorityQueue(); * queue.enqueue("low", 1); * queue.enqueue("high", 10); * console.log(queue.toArrayWithPriorities()); * // [{ value: "high", priority: 10 }, { value: "low", priority: 1 }] (order may vary) * ``` */ toArrayWithPriorities(): { value: T; priority: number; }[]; /** * Updates the minimum priority when adding elements. * @param priority - The priority being added */ private updateMinPriorityOnAdd; /** * Updates the minimum priority from all elements (used in constructor). */ private updateMinPriority; /** * Builds a max heap from the current heap array. */ private buildHeap; /** * Moves an element up the heap to maintain heap property. * @param index - The index of the element to move up */ private heapifyUp; /** * Moves an element down the heap to maintain heap property. * @param index - The index of the element to move down */ private heapifyDown; /** * Swaps two elements in the heap. * @param i - First index * @param j - Second index */ private swap; }