aoc-copilot
Version:
Advent of Code automatic runner for examples and inputs
44 lines • 1.68 kB
TypeScript
/**
* These functions implement a min heap which can be used to pop the lowest
* key. It uses an array as the heap, and each row in the heap must be an
* array with the key as the first element:
*
* heap: [number, ...T][]
*
* The main thing to keep in mind is that a heap sort just ensures that the
* parent key <= child keys, it won't be in ascending order.
*
* The >> operator is the bit shift operator. It's used to calculate the index
* of the parent of a node. This is one of the unique characteristics of a
* heap -- no pointers are needed to keep track of child or parent indexes,
* the structure of the heap itself determines that:
* 0 root
* 1 parent 0
* 2 parent 0
* 3 parent 1
* 4 parent 1
* 5 parent 2
* 6 parent 2
* 7 parent 3
*
* parent(7) = 7 >> 1 = 3 (0111 >> 1 = 0011)
* children(2) = 2 * 2 + 1 = 5 and 5 + 1
*/
/**
* Reorder the given array in-place so that it becomes a valid heap. Elements
* in the given array must have a [0] property (e.g. arrays). That [0] value
* serves as the key to establish the heap order. The rest of such an element
* is just payload. It also returns the heap.
*/
declare function heapify<T extends any[]>(arr: [number, ...T][]): [number, ...T][];
/**
* Extract the root of the given heap and return it (the subarray). Returns
* undefined if the heap is empty
*/
declare function pop<T extends any[]>(arr: [number, ...T][]): [number, ...T] | undefined;
/**
* Inserts the given node into the given heap. Return the heap.
*/
declare function push<T extends any[]>(arr: [number, ...T][], value: [number, ...T]): [number, ...T][];
export { heapify, pop, push };
//# sourceMappingURL=heap.d.ts.map