avl-tree-typed 2.1.1 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +3859 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/esm/index.mjs +3852 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/types/data-structures/base/index.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +182 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +135 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +291 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +754 -29
- package/dist/types/data-structures/binary-tree/bst.d.ts +413 -12
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +208 -3
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -1
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +190 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +270 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -14
- package/dist/types/data-structures/graph/directed-graph.d.ts +207 -1
- package/dist/types/data-structures/graph/index.d.ts +2 -1
- package/dist/types/data-structures/graph/map-graph.d.ts +78 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +188 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +345 -19
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +503 -5
- package/dist/types/data-structures/heap/index.d.ts +2 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +32 -1
- package/dist/types/data-structures/heap/min-heap.d.ts +33 -1
- package/dist/types/data-structures/index.d.ts +7 -7
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +769 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +451 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -4
- package/dist/types/data-structures/matrix/index.d.ts +1 -1
- package/dist/types/data-structures/matrix/matrix.d.ts +168 -7
- package/dist/types/data-structures/matrix/navigator.d.ts +54 -13
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -1
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -2
- package/dist/types/data-structures/queue/deque.d.ts +431 -4
- package/dist/types/data-structures/queue/queue.d.ts +308 -4
- package/dist/types/data-structures/stack/stack.d.ts +306 -2
- package/dist/types/data-structures/tree/tree.d.ts +62 -1
- package/dist/types/data-structures/trie/trie.d.ts +350 -4
- package/dist/types/index.d.ts +15 -3
- package/dist/{interfaces → types/interfaces}/binary-tree.d.ts +1 -1
- package/dist/types/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +12 -0
- package/dist/{data-structures → types/types/data-structures}/binary-tree/index.d.ts +2 -3
- package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/graph/abstract-graph.d.ts +14 -0
- package/dist/types/types/data-structures/graph/directed-graph.d.ts +1 -0
- package/dist/{data-structures → types/types/data-structures}/graph/index.d.ts +1 -2
- package/dist/types/types/data-structures/graph/map-graph.d.ts +1 -0
- package/dist/types/types/data-structures/graph/undirected-graph.d.ts +1 -0
- package/dist/types/types/data-structures/hash/hash-map.d.ts +19 -0
- package/dist/types/types/data-structures/hash/index.d.ts +2 -0
- package/dist/types/types/data-structures/heap/heap.d.ts +5 -0
- package/dist/types/types/data-structures/heap/index.d.ts +1 -0
- package/dist/types/types/data-structures/heap/max-heap.d.ts +1 -0
- package/dist/types/types/data-structures/heap/min-heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +4 -0
- package/dist/types/types/data-structures/matrix/matrix.d.ts +7 -0
- package/dist/types/types/data-structures/matrix/navigator.d.ts +14 -0
- package/dist/types/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +2 -0
- package/dist/types/types/data-structures/queue/deque.d.ts +4 -0
- package/dist/types/types/data-structures/queue/queue.d.ts +4 -0
- package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
- package/dist/types/types/data-structures/tree/tree.d.ts +1 -0
- package/dist/types/types/data-structures/trie/trie.d.ts +4 -0
- package/dist/types/types/index.d.ts +3 -0
- package/dist/types/types/utils/index.d.ts +2 -0
- package/dist/types/types/utils/utils.d.ts +22 -0
- package/dist/types/utils/index.d.ts +1 -1
- package/dist/types/utils/utils.d.ts +209 -22
- package/dist/umd/avl-tree-typed.js +3852 -0
- package/dist/umd/avl-tree-typed.js.map +1 -0
- package/dist/umd/avl-tree-typed.min.js +13 -0
- package/dist/umd/avl-tree-typed.min.js.map +1 -0
- package/package.json +25 -5
- package/src/data-structures/binary-tree/avl-tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +4 -4
- package/src/data-structures/binary-tree/bst.ts +1 -1
- package/src/data-structures/binary-tree/red-black-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/tree-multi-map.ts +1 -1
- package/src/data-structures/heap/heap.ts +5 -5
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/interfaces/binary-tree.ts +1 -1
- package/tsconfig.base.json +23 -0
- package/tsconfig.json +8 -34
- package/tsconfig.test.json +8 -0
- package/tsconfig.types.json +15 -0
- package/tsup.config.js +28 -0
- package/tsup.node.config.js +37 -0
- package/dist/common/index.js +0 -28
- package/dist/constants/index.js +0 -8
- package/dist/data-structures/base/index.d.ts +0 -2
- package/dist/data-structures/base/index.js +0 -18
- package/dist/data-structures/base/iterable-element-base.js +0 -243
- package/dist/data-structures/base/iterable-entry-base.js +0 -183
- package/dist/data-structures/base/linear-base.js +0 -415
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +0 -182
- package/dist/data-structures/binary-tree/avl-tree-counter.js +0 -374
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -135
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +0 -250
- package/dist/data-structures/binary-tree/avl-tree.d.ts +0 -291
- package/dist/data-structures/binary-tree/avl-tree.js +0 -611
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -174
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +0 -294
- package/dist/data-structures/binary-tree/binary-tree.d.ts +0 -754
- package/dist/data-structures/binary-tree/binary-tree.js +0 -1925
- package/dist/data-structures/binary-tree/bst.d.ts +0 -413
- package/dist/data-structures/binary-tree/bst.js +0 -903
- package/dist/data-structures/binary-tree/index.js +0 -26
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +0 -208
- package/dist/data-structures/binary-tree/red-black-tree.js +0 -546
- package/dist/data-structures/binary-tree/segment-tree.d.ts +0 -160
- package/dist/data-structures/binary-tree/segment-tree.js +0 -297
- package/dist/data-structures/binary-tree/tree-counter.d.ts +0 -190
- package/dist/data-structures/binary-tree/tree-counter.js +0 -413
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +0 -270
- package/dist/data-structures/binary-tree/tree-multi-map.js +0 -384
- package/dist/data-structures/graph/abstract-graph.d.ts +0 -340
- package/dist/data-structures/graph/abstract-graph.js +0 -896
- package/dist/data-structures/graph/directed-graph.d.ts +0 -207
- package/dist/data-structures/graph/directed-graph.js +0 -525
- package/dist/data-structures/graph/index.js +0 -20
- package/dist/data-structures/graph/map-graph.d.ts +0 -78
- package/dist/data-structures/graph/map-graph.js +0 -107
- package/dist/data-structures/graph/undirected-graph.d.ts +0 -188
- package/dist/data-structures/graph/undirected-graph.js +0 -424
- package/dist/data-structures/hash/hash-map.d.ts +0 -345
- package/dist/data-structures/hash/hash-map.js +0 -692
- package/dist/data-structures/hash/index.d.ts +0 -1
- package/dist/data-structures/hash/index.js +0 -17
- package/dist/data-structures/heap/heap.d.ts +0 -503
- package/dist/data-structures/heap/heap.js +0 -901
- package/dist/data-structures/heap/index.d.ts +0 -3
- package/dist/data-structures/heap/index.js +0 -19
- package/dist/data-structures/heap/max-heap.d.ts +0 -32
- package/dist/data-structures/heap/max-heap.js +0 -40
- package/dist/data-structures/heap/min-heap.d.ts +0 -33
- package/dist/data-structures/heap/min-heap.js +0 -31
- package/dist/data-structures/index.js +0 -28
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -769
- package/dist/data-structures/linked-list/doubly-linked-list.js +0 -1111
- package/dist/data-structures/linked-list/index.js +0 -19
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +0 -451
- package/dist/data-structures/linked-list/singly-linked-list.js +0 -850
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -27
- package/dist/data-structures/linked-list/skip-linked-list.js +0 -144
- package/dist/data-structures/matrix/index.js +0 -18
- package/dist/data-structures/matrix/matrix.d.ts +0 -168
- package/dist/data-structures/matrix/matrix.js +0 -448
- package/dist/data-structures/matrix/navigator.d.ts +0 -55
- package/dist/data-structures/matrix/navigator.js +0 -111
- package/dist/data-structures/priority-queue/index.js +0 -19
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +0 -27
- package/dist/data-structures/priority-queue/max-priority-queue.js +0 -34
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +0 -26
- package/dist/data-structures/priority-queue/min-priority-queue.js +0 -24
- package/dist/data-structures/priority-queue/priority-queue.d.ts +0 -15
- package/dist/data-structures/priority-queue/priority-queue.js +0 -20
- package/dist/data-structures/queue/deque.d.ts +0 -431
- package/dist/data-structures/queue/deque.js +0 -879
- package/dist/data-structures/queue/index.js +0 -18
- package/dist/data-structures/queue/queue.d.ts +0 -308
- package/dist/data-structures/queue/queue.js +0 -473
- package/dist/data-structures/stack/index.js +0 -17
- package/dist/data-structures/stack/stack.d.ts +0 -306
- package/dist/data-structures/stack/stack.js +0 -401
- package/dist/data-structures/tree/index.js +0 -17
- package/dist/data-structures/tree/tree.d.ts +0 -62
- package/dist/data-structures/tree/tree.js +0 -107
- package/dist/data-structures/trie/index.js +0 -17
- package/dist/data-structures/trie/trie.d.ts +0 -350
- package/dist/data-structures/trie/trie.js +0 -610
- package/dist/index.d.ts +0 -15
- package/dist/index.js +0 -31
- package/dist/interfaces/binary-tree.js +0 -2
- package/dist/interfaces/doubly-linked-list.js +0 -2
- package/dist/interfaces/graph.js +0 -2
- package/dist/interfaces/heap.js +0 -2
- package/dist/interfaces/index.js +0 -24
- package/dist/interfaces/navigator.js +0 -2
- package/dist/interfaces/priority-queue.js +0 -2
- package/dist/interfaces/segment-tree.js +0 -2
- package/dist/interfaces/singly-linked-list.js +0 -2
- package/dist/types/common.js +0 -2
- package/dist/types/data-structures/base/base.js +0 -2
- package/dist/types/data-structures/base/index.js +0 -17
- package/dist/types/data-structures/binary-tree/avl-tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/bst.js +0 -2
- package/dist/types/data-structures/binary-tree/index.js +0 -25
- package/dist/types/data-structures/binary-tree/red-black-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.js +0 -2
- package/dist/types/data-structures/graph/abstract-graph.js +0 -2
- package/dist/types/data-structures/graph/directed-graph.js +0 -2
- package/dist/types/data-structures/graph/index.js +0 -19
- package/dist/types/data-structures/graph/map-graph.js +0 -2
- package/dist/types/data-structures/graph/undirected-graph.js +0 -2
- package/dist/types/data-structures/hash/hash-map.js +0 -2
- package/dist/types/data-structures/hash/index.js +0 -17
- package/dist/types/data-structures/heap/heap.js +0 -2
- package/dist/types/data-structures/heap/index.js +0 -17
- package/dist/types/data-structures/heap/max-heap.js +0 -2
- package/dist/types/data-structures/heap/min-heap.js +0 -2
- package/dist/types/data-structures/index.js +0 -28
- package/dist/types/data-structures/linked-list/doubly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/index.js +0 -19
- package/dist/types/data-structures/linked-list/singly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.js +0 -2
- package/dist/types/data-structures/matrix/index.js +0 -18
- package/dist/types/data-structures/matrix/matrix.js +0 -2
- package/dist/types/data-structures/matrix/navigator.js +0 -2
- package/dist/types/data-structures/priority-queue/index.js +0 -19
- package/dist/types/data-structures/priority-queue/max-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/min-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/priority-queue.js +0 -2
- package/dist/types/data-structures/queue/deque.js +0 -2
- package/dist/types/data-structures/queue/index.js +0 -18
- package/dist/types/data-structures/queue/queue.js +0 -2
- package/dist/types/data-structures/stack/index.js +0 -17
- package/dist/types/data-structures/stack/stack.js +0 -2
- package/dist/types/data-structures/tree/index.js +0 -17
- package/dist/types/data-structures/tree/tree.js +0 -2
- package/dist/types/data-structures/trie/index.js +0 -17
- package/dist/types/data-structures/trie/trie.js +0 -2
- package/dist/types/index.js +0 -19
- package/dist/types/utils/index.js +0 -18
- package/dist/types/utils/utils.js +0 -2
- package/dist/types/utils/validate-type.js +0 -2
- package/dist/utils/index.d.ts +0 -2
- package/dist/utils/index.js +0 -18
- package/dist/utils/number.js +0 -23
- package/dist/utils/utils.d.ts +0 -209
- package/dist/utils/utils.js +0 -353
- package/dist/{common → types/common}/index.d.ts +0 -0
- package/dist/{constants → types/constants}/index.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-element-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-entry-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/linear-base.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/doubly-linked-list.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/graph.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/heap.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/index.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/navigator.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/priority-queue.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/segment-tree.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/singly-linked-list.d.ts +0 -0
- package/dist/types/{common.d.ts → types/common.d.ts} +0 -0
- package/dist/types/{data-structures → types/data-structures}/base/base.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/index.d.ts +7 -7
- package/dist/{data-structures → types/types/data-structures}/linked-list/index.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/matrix/index.d.ts +1 -1
- /package/dist/{data-structures → types/types/data-structures}/priority-queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/stack/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/tree/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/trie/index.d.ts +0 -0
- /package/dist/types/{utils → types/utils}/validate-type.d.ts +0 -0
- /package/dist/{utils → types/utils}/number.d.ts +0 -0
|
@@ -1,901 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* data-structure-typed
|
|
4
|
-
*
|
|
5
|
-
* @author Pablo Zeng
|
|
6
|
-
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
7
|
-
* @license MIT License
|
|
8
|
-
*/
|
|
9
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
10
|
-
var t = {};
|
|
11
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
12
|
-
t[p] = s[p];
|
|
13
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
14
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
15
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
16
|
-
t[p[i]] = s[p[i]];
|
|
17
|
-
}
|
|
18
|
-
return t;
|
|
19
|
-
};
|
|
20
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
-
exports.FibonacciHeap = exports.FibonacciHeapNode = exports.Heap = void 0;
|
|
22
|
-
const base_1 = require("../base");
|
|
23
|
-
/**
|
|
24
|
-
* Binary heap with pluggable comparator; supports fast insertion and removal of the top element.
|
|
25
|
-
* @remarks Time O(1), Space O(1)
|
|
26
|
-
* @template E
|
|
27
|
-
* @template R
|
|
28
|
-
* 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
|
|
29
|
-
* 2. Heap Properties: Each node in a heap follows a specific order property, which varies depending on the type of heap:
|
|
30
|
-
* Max Heap: The value of each parent node is greater than or equal to the value of its children.
|
|
31
|
-
* Min Heap: The value of each parent node is less than or equal to the value of its children.
|
|
32
|
-
* 3. Root Node Access: In a heap, the largest element (in a max heap) or the smallest element (in a min heap) is always at the root of the tree.
|
|
33
|
-
* 4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
|
|
34
|
-
* 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
|
|
35
|
-
* 6. Non-linear Search: While a heap allows rapid access to its largest or smallest element, it is less efficient for other operations, such as searching for a specific element, as it is not designed for these tasks.
|
|
36
|
-
* 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
|
|
37
|
-
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prime's minimum-spanning tree algorithm, which use heaps to improve performance.
|
|
38
|
-
* @example
|
|
39
|
-
* // Use Heap to sort an array
|
|
40
|
-
* function heapSort(arr: number[]): number[] {
|
|
41
|
-
* const heap = new Heap<number>(arr, { comparator: (a, b) => a - b });
|
|
42
|
-
* const sorted: number[] = [];
|
|
43
|
-
* while (!heap.isEmpty()) {
|
|
44
|
-
* sorted.push(heap.poll()!); // Poll minimum element
|
|
45
|
-
* }
|
|
46
|
-
* return sorted;
|
|
47
|
-
* }
|
|
48
|
-
*
|
|
49
|
-
* const array = [5, 3, 8, 4, 1, 2];
|
|
50
|
-
* console.log(heapSort(array)); // [1, 2, 3, 4, 5, 8]
|
|
51
|
-
* @example
|
|
52
|
-
* // Use Heap to solve top k problems
|
|
53
|
-
* function topKElements(arr: number[], k: number): number[] {
|
|
54
|
-
* const heap = new Heap<number>([], { comparator: (a, b) => b - a }); // Max heap
|
|
55
|
-
* arr.forEach(num => {
|
|
56
|
-
* heap.add(num);
|
|
57
|
-
* if (heap.size > k) heap.poll(); // Keep the heap size at K
|
|
58
|
-
* });
|
|
59
|
-
* return heap.toArray();
|
|
60
|
-
* }
|
|
61
|
-
*
|
|
62
|
-
* const numbers = [10, 30, 20, 5, 15, 25];
|
|
63
|
-
* console.log(topKElements(numbers, 3)); // [15, 10, 5]
|
|
64
|
-
* @example
|
|
65
|
-
* // Use Heap to merge sorted sequences
|
|
66
|
-
* function mergeSortedSequences(sequences: number[][]): number[] {
|
|
67
|
-
* const heap = new Heap<{ value: number; seqIndex: number; itemIndex: number }>([], {
|
|
68
|
-
* comparator: (a, b) => a.value - b.value // Min heap
|
|
69
|
-
* });
|
|
70
|
-
*
|
|
71
|
-
* // Initialize heap
|
|
72
|
-
* sequences.forEach((seq, seqIndex) => {
|
|
73
|
-
* if (seq.length) {
|
|
74
|
-
* heap.add({ value: seq[0], seqIndex, itemIndex: 0 });
|
|
75
|
-
* }
|
|
76
|
-
* });
|
|
77
|
-
*
|
|
78
|
-
* const merged: number[] = [];
|
|
79
|
-
* while (!heap.isEmpty()) {
|
|
80
|
-
* const { value, seqIndex, itemIndex } = heap.poll()!;
|
|
81
|
-
* merged.push(value);
|
|
82
|
-
*
|
|
83
|
-
* if (itemIndex + 1 < sequences[seqIndex].length) {
|
|
84
|
-
* heap.add({
|
|
85
|
-
* value: sequences[seqIndex][itemIndex + 1],
|
|
86
|
-
* seqIndex,
|
|
87
|
-
* itemIndex: itemIndex + 1
|
|
88
|
-
* });
|
|
89
|
-
* }
|
|
90
|
-
* }
|
|
91
|
-
*
|
|
92
|
-
* return merged;
|
|
93
|
-
* }
|
|
94
|
-
*
|
|
95
|
-
* const sequences = [
|
|
96
|
-
* [1, 4, 7],
|
|
97
|
-
* [2, 5, 8],
|
|
98
|
-
* [3, 6, 9]
|
|
99
|
-
* ];
|
|
100
|
-
* console.log(mergeSortedSequences(sequences)); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
101
|
-
* @example
|
|
102
|
-
* // Use Heap to dynamically maintain the median
|
|
103
|
-
* class MedianFinder {
|
|
104
|
-
* private low: MaxHeap<number>; // Max heap, stores the smaller half
|
|
105
|
-
* private high: MinHeap<number>; // Min heap, stores the larger half
|
|
106
|
-
*
|
|
107
|
-
* constructor() {
|
|
108
|
-
* this.low = new MaxHeap<number>([]);
|
|
109
|
-
* this.high = new MinHeap<number>([]);
|
|
110
|
-
* }
|
|
111
|
-
*
|
|
112
|
-
* addNum(num: number): void {
|
|
113
|
-
* if (this.low.isEmpty() || num <= this.low.peek()!) this.low.add(num);
|
|
114
|
-
* else this.high.add(num);
|
|
115
|
-
*
|
|
116
|
-
* // Balance heaps
|
|
117
|
-
* if (this.low.size > this.high.size + 1) this.high.add(this.low.poll()!);
|
|
118
|
-
* else if (this.high.size > this.low.size) this.low.add(this.high.poll()!);
|
|
119
|
-
* }
|
|
120
|
-
*
|
|
121
|
-
* findMedian(): number {
|
|
122
|
-
* if (this.low.size === this.high.size) return (this.low.peek()! + this.high.peek()!) / 2;
|
|
123
|
-
* return this.low.peek()!;
|
|
124
|
-
* }
|
|
125
|
-
* }
|
|
126
|
-
*
|
|
127
|
-
* const medianFinder = new MedianFinder();
|
|
128
|
-
* medianFinder.addNum(10);
|
|
129
|
-
* console.log(medianFinder.findMedian()); // 10
|
|
130
|
-
* medianFinder.addNum(20);
|
|
131
|
-
* console.log(medianFinder.findMedian()); // 15
|
|
132
|
-
* medianFinder.addNum(30);
|
|
133
|
-
* console.log(medianFinder.findMedian()); // 20
|
|
134
|
-
* medianFinder.addNum(40);
|
|
135
|
-
* console.log(medianFinder.findMedian()); // 25
|
|
136
|
-
* medianFinder.addNum(50);
|
|
137
|
-
* console.log(medianFinder.findMedian()); // 30
|
|
138
|
-
* @example
|
|
139
|
-
* // Use Heap for load balancing
|
|
140
|
-
* function loadBalance(requests: number[], servers: number): number[] {
|
|
141
|
-
* const serverHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // min heap
|
|
142
|
-
* const serverLoads = new Array(servers).fill(0);
|
|
143
|
-
*
|
|
144
|
-
* for (let i = 0; i < servers; i++) {
|
|
145
|
-
* serverHeap.add({ id: i, load: 0 });
|
|
146
|
-
* }
|
|
147
|
-
*
|
|
148
|
-
* requests.forEach(req => {
|
|
149
|
-
* const server = serverHeap.poll()!;
|
|
150
|
-
* serverLoads[server.id] += req;
|
|
151
|
-
* server.load += req;
|
|
152
|
-
* serverHeap.add(server); // The server after updating the load is re-entered into the heap
|
|
153
|
-
* });
|
|
154
|
-
*
|
|
155
|
-
* return serverLoads;
|
|
156
|
-
* }
|
|
157
|
-
*
|
|
158
|
-
* const requests = [5, 2, 8, 3, 7];
|
|
159
|
-
* console.log(loadBalance(requests, 3)); // [12, 8, 5]
|
|
160
|
-
* @example
|
|
161
|
-
* // Use Heap to schedule tasks
|
|
162
|
-
* type Task = [string, number];
|
|
163
|
-
*
|
|
164
|
-
* function scheduleTasks(tasks: Task[], machines: number): Map<number, Task[]> {
|
|
165
|
-
* const machineHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // Min heap
|
|
166
|
-
* const allocation = new Map<number, Task[]>();
|
|
167
|
-
*
|
|
168
|
-
* // Initialize the load on each machine
|
|
169
|
-
* for (let i = 0; i < machines; i++) {
|
|
170
|
-
* machineHeap.add({ id: i, load: 0 });
|
|
171
|
-
* allocation.set(i, []);
|
|
172
|
-
* }
|
|
173
|
-
*
|
|
174
|
-
* // Assign tasks
|
|
175
|
-
* tasks.forEach(([task, load]) => {
|
|
176
|
-
* const machine = machineHeap.poll()!;
|
|
177
|
-
* allocation.get(machine.id)!.push([task, load]);
|
|
178
|
-
* machine.load += load;
|
|
179
|
-
* machineHeap.add(machine); // The machine after updating the load is re-entered into the heap
|
|
180
|
-
* });
|
|
181
|
-
*
|
|
182
|
-
* return allocation;
|
|
183
|
-
* }
|
|
184
|
-
*
|
|
185
|
-
* const tasks: Task[] = [
|
|
186
|
-
* ['Task1', 3],
|
|
187
|
-
* ['Task2', 1],
|
|
188
|
-
* ['Task3', 2],
|
|
189
|
-
* ['Task4', 5],
|
|
190
|
-
* ['Task5', 4]
|
|
191
|
-
* ];
|
|
192
|
-
* const expectedMap = new Map<number, Task[]>();
|
|
193
|
-
* expectedMap.set(0, [
|
|
194
|
-
* ['Task1', 3],
|
|
195
|
-
* ['Task4', 5]
|
|
196
|
-
* ]);
|
|
197
|
-
* expectedMap.set(1, [
|
|
198
|
-
* ['Task2', 1],
|
|
199
|
-
* ['Task3', 2],
|
|
200
|
-
* ['Task5', 4]
|
|
201
|
-
* ]);
|
|
202
|
-
* console.log(scheduleTasks(tasks, 2)); // expectedMap
|
|
203
|
-
*/
|
|
204
|
-
class Heap extends base_1.IterableElementBase {
|
|
205
|
-
/**
|
|
206
|
-
* Create a Heap and optionally bulk-insert elements.
|
|
207
|
-
* @remarks Time O(N), Space O(N)
|
|
208
|
-
* @param [elements] - Iterable of elements (or raw values if toElementFn is set).
|
|
209
|
-
* @param [options] - Options such as comparator and toElementFn.
|
|
210
|
-
* @returns New Heap instance.
|
|
211
|
-
*/
|
|
212
|
-
constructor(elements = [], options) {
|
|
213
|
-
super(options);
|
|
214
|
-
this._equals = Object.is;
|
|
215
|
-
this._elements = [];
|
|
216
|
-
this._DEFAULT_COMPARATOR = (a, b) => {
|
|
217
|
-
if (typeof a === 'object' || typeof b === 'object') {
|
|
218
|
-
throw TypeError('When comparing object types, define a custom comparator in options.');
|
|
219
|
-
}
|
|
220
|
-
if (a > b)
|
|
221
|
-
return 1;
|
|
222
|
-
if (a < b)
|
|
223
|
-
return -1;
|
|
224
|
-
return 0;
|
|
225
|
-
};
|
|
226
|
-
this._comparator = this._DEFAULT_COMPARATOR; /**
|
|
227
|
-
* Get the comparator used to order elements.
|
|
228
|
-
* @remarks Time O(1), Space O(1)
|
|
229
|
-
* @returns Comparator function.
|
|
230
|
-
*/
|
|
231
|
-
if (options) {
|
|
232
|
-
const { comparator } = options;
|
|
233
|
-
if (comparator)
|
|
234
|
-
this._comparator = comparator;
|
|
235
|
-
}
|
|
236
|
-
this.addMany(elements);
|
|
237
|
-
}
|
|
238
|
-
/**
|
|
239
|
-
* Get the backing array of the heap.
|
|
240
|
-
* @remarks Time O(1), Space O(1)
|
|
241
|
-
* @returns Internal elements array.
|
|
242
|
-
*/
|
|
243
|
-
get elements() {
|
|
244
|
-
return this._elements;
|
|
245
|
-
}
|
|
246
|
-
/**
|
|
247
|
-
* Get the number of elements.
|
|
248
|
-
* @remarks Time O(1), Space O(1)
|
|
249
|
-
* @returns Heap size.
|
|
250
|
-
*/
|
|
251
|
-
get size() {
|
|
252
|
-
return this.elements.length;
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* Get the last leaf element.
|
|
256
|
-
* @remarks Time O(1), Space O(1)
|
|
257
|
-
* @returns Last element or undefined.
|
|
258
|
-
*/
|
|
259
|
-
get leaf() {
|
|
260
|
-
var _a;
|
|
261
|
-
return (_a = this.elements[this.size - 1]) !== null && _a !== void 0 ? _a : undefined;
|
|
262
|
-
}
|
|
263
|
-
/**
|
|
264
|
-
* Create a heap of the same class from an iterable.
|
|
265
|
-
* @remarks Time O(N), Space O(N)
|
|
266
|
-
* @template T
|
|
267
|
-
* @template R
|
|
268
|
-
* @template S
|
|
269
|
-
* @param [elements] - Iterable of elements or raw records.
|
|
270
|
-
* @param [options] - Heap options including comparator.
|
|
271
|
-
* @returns A new heap instance of this class.
|
|
272
|
-
*/
|
|
273
|
-
static from(elements, options) {
|
|
274
|
-
return new this(elements, options);
|
|
275
|
-
}
|
|
276
|
-
/**
|
|
277
|
-
* Build a Heap from an iterable in linear time given a comparator.
|
|
278
|
-
* @remarks Time O(N), Space O(N)
|
|
279
|
-
* @template EE
|
|
280
|
-
* @template RR
|
|
281
|
-
* @param elements - Iterable of elements.
|
|
282
|
-
* @param options - Heap options including comparator.
|
|
283
|
-
* @returns A new Heap built from elements.
|
|
284
|
-
*/
|
|
285
|
-
static heapify(elements, options) {
|
|
286
|
-
return new Heap(elements, options);
|
|
287
|
-
}
|
|
288
|
-
/**
|
|
289
|
-
* Insert an element.
|
|
290
|
-
* @remarks Time O(1) amortized, Space O(1)
|
|
291
|
-
* @param element - Element to insert.
|
|
292
|
-
* @returns True.
|
|
293
|
-
*/
|
|
294
|
-
add(element) {
|
|
295
|
-
this._elements.push(element);
|
|
296
|
-
return this._bubbleUp(this.elements.length - 1);
|
|
297
|
-
}
|
|
298
|
-
/**
|
|
299
|
-
* Insert many elements from an iterable.
|
|
300
|
-
* @remarks Time O(N log N), Space O(1)
|
|
301
|
-
* @param elements - Iterable of elements or raw values.
|
|
302
|
-
* @returns Array of per-element success flags.
|
|
303
|
-
*/
|
|
304
|
-
addMany(elements) {
|
|
305
|
-
const flags = [];
|
|
306
|
-
for (const el of elements) {
|
|
307
|
-
if (this.toElementFn) {
|
|
308
|
-
const ok = this.add(this.toElementFn(el));
|
|
309
|
-
flags.push(ok);
|
|
310
|
-
}
|
|
311
|
-
else {
|
|
312
|
-
const ok = this.add(el);
|
|
313
|
-
flags.push(ok);
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
return flags;
|
|
317
|
-
}
|
|
318
|
-
/**
|
|
319
|
-
* Remove and return the top element.
|
|
320
|
-
* @remarks Time O(log N), Space O(1)
|
|
321
|
-
* @returns Top element or undefined.
|
|
322
|
-
*/
|
|
323
|
-
poll() {
|
|
324
|
-
if (this.elements.length === 0)
|
|
325
|
-
return;
|
|
326
|
-
const value = this.elements[0];
|
|
327
|
-
const last = this.elements.pop();
|
|
328
|
-
if (this.elements.length) {
|
|
329
|
-
this.elements[0] = last;
|
|
330
|
-
this._sinkDown(0, this.elements.length >> 1);
|
|
331
|
-
}
|
|
332
|
-
return value;
|
|
333
|
-
}
|
|
334
|
-
/**
|
|
335
|
-
* Get the current top element without removing it.
|
|
336
|
-
* @remarks Time O(1), Space O(1)
|
|
337
|
-
* @returns Top element or undefined.
|
|
338
|
-
*/
|
|
339
|
-
peek() {
|
|
340
|
-
return this.elements[0];
|
|
341
|
-
}
|
|
342
|
-
/**
|
|
343
|
-
* Check whether the heap is empty.
|
|
344
|
-
* @remarks Time O(1), Space O(1)
|
|
345
|
-
* @returns True if size is 0.
|
|
346
|
-
*/
|
|
347
|
-
isEmpty() {
|
|
348
|
-
return this.size === 0;
|
|
349
|
-
}
|
|
350
|
-
/**
|
|
351
|
-
* Remove all elements.
|
|
352
|
-
* @remarks Time O(1), Space O(1)
|
|
353
|
-
* @returns void
|
|
354
|
-
*/
|
|
355
|
-
clear() {
|
|
356
|
-
this._elements = [];
|
|
357
|
-
}
|
|
358
|
-
/**
|
|
359
|
-
* Replace the backing array and rebuild the heap.
|
|
360
|
-
* @remarks Time O(N), Space O(N)
|
|
361
|
-
* @param elements - Iterable used to refill the heap.
|
|
362
|
-
* @returns Array of per-node results from fixing steps.
|
|
363
|
-
*/
|
|
364
|
-
refill(elements) {
|
|
365
|
-
this._elements = Array.from(elements);
|
|
366
|
-
return this.fix();
|
|
367
|
-
}
|
|
368
|
-
/**
|
|
369
|
-
* Check if an equal element exists in the heap.
|
|
370
|
-
* @remarks Time O(N), Space O(1)
|
|
371
|
-
* @param element - Element to search for.
|
|
372
|
-
* @returns True if found.
|
|
373
|
-
*/
|
|
374
|
-
has(element) {
|
|
375
|
-
for (const el of this.elements)
|
|
376
|
-
if (this._equals(el, element))
|
|
377
|
-
return true;
|
|
378
|
-
return false;
|
|
379
|
-
}
|
|
380
|
-
/**
|
|
381
|
-
* Delete one occurrence of an element.
|
|
382
|
-
* @remarks Time O(N), Space O(1)
|
|
383
|
-
* @param element - Element to delete.
|
|
384
|
-
* @returns True if an element was removed.
|
|
385
|
-
*/
|
|
386
|
-
delete(element) {
|
|
387
|
-
let index = -1;
|
|
388
|
-
for (let i = 0; i < this.elements.length; i++) {
|
|
389
|
-
if (this._equals(this.elements[i], element)) {
|
|
390
|
-
index = i;
|
|
391
|
-
break;
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
if (index < 0)
|
|
395
|
-
return false;
|
|
396
|
-
if (index === 0) {
|
|
397
|
-
this.poll();
|
|
398
|
-
}
|
|
399
|
-
else if (index === this.elements.length - 1) {
|
|
400
|
-
this.elements.pop();
|
|
401
|
-
}
|
|
402
|
-
else {
|
|
403
|
-
this.elements.splice(index, 1, this.elements.pop());
|
|
404
|
-
this._bubbleUp(index);
|
|
405
|
-
this._sinkDown(index, this.elements.length >> 1);
|
|
406
|
-
}
|
|
407
|
-
return true;
|
|
408
|
-
}
|
|
409
|
-
/**
|
|
410
|
-
* Delete the first element that matches a predicate.
|
|
411
|
-
* @remarks Time O(N), Space O(1)
|
|
412
|
-
* @param predicate - Function (element, index, heap) → boolean.
|
|
413
|
-
* @returns True if an element was removed.
|
|
414
|
-
*/
|
|
415
|
-
deleteBy(predicate) {
|
|
416
|
-
let idx = -1;
|
|
417
|
-
for (let i = 0; i < this.elements.length; i++) {
|
|
418
|
-
if (predicate(this.elements[i], i, this)) {
|
|
419
|
-
idx = i;
|
|
420
|
-
break;
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
if (idx < 0)
|
|
424
|
-
return false;
|
|
425
|
-
if (idx === 0) {
|
|
426
|
-
this.poll();
|
|
427
|
-
}
|
|
428
|
-
else if (idx === this.elements.length - 1) {
|
|
429
|
-
this.elements.pop();
|
|
430
|
-
}
|
|
431
|
-
else {
|
|
432
|
-
this.elements.splice(idx, 1, this.elements.pop());
|
|
433
|
-
this._bubbleUp(idx);
|
|
434
|
-
this._sinkDown(idx, this.elements.length >> 1);
|
|
435
|
-
}
|
|
436
|
-
return true;
|
|
437
|
-
}
|
|
438
|
-
/**
|
|
439
|
-
* Set the equality comparator used by has/delete operations.
|
|
440
|
-
* @remarks Time O(1), Space O(1)
|
|
441
|
-
* @param equals - Equality predicate (a, b) → boolean.
|
|
442
|
-
* @returns This heap.
|
|
443
|
-
*/
|
|
444
|
-
setEquality(equals) {
|
|
445
|
-
this._equals = equals;
|
|
446
|
-
return this;
|
|
447
|
-
}
|
|
448
|
-
/**
|
|
449
|
-
* Traverse the binary heap as a complete binary tree and collect elements.
|
|
450
|
-
* @remarks Time O(N), Space O(H)
|
|
451
|
-
* @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
452
|
-
* @returns Array of visited elements.
|
|
453
|
-
*/
|
|
454
|
-
dfs(order = 'PRE') {
|
|
455
|
-
const result = [];
|
|
456
|
-
const _dfs = (index) => {
|
|
457
|
-
const left = 2 * index + 1, right = left + 1;
|
|
458
|
-
if (index < this.size) {
|
|
459
|
-
if (order === 'IN') {
|
|
460
|
-
_dfs(left);
|
|
461
|
-
result.push(this.elements[index]);
|
|
462
|
-
_dfs(right);
|
|
463
|
-
}
|
|
464
|
-
else if (order === 'PRE') {
|
|
465
|
-
result.push(this.elements[index]);
|
|
466
|
-
_dfs(left);
|
|
467
|
-
_dfs(right);
|
|
468
|
-
}
|
|
469
|
-
else if (order === 'POST') {
|
|
470
|
-
_dfs(left);
|
|
471
|
-
_dfs(right);
|
|
472
|
-
result.push(this.elements[index]);
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
};
|
|
476
|
-
_dfs(0);
|
|
477
|
-
return result;
|
|
478
|
-
}
|
|
479
|
-
/**
|
|
480
|
-
* Restore heap order bottom-up (heapify in-place).
|
|
481
|
-
* @remarks Time O(N), Space O(1)
|
|
482
|
-
* @returns Array of per-node results from fixing steps.
|
|
483
|
-
*/
|
|
484
|
-
fix() {
|
|
485
|
-
const results = [];
|
|
486
|
-
for (let i = Math.floor(this.size / 2) - 1; i >= 0; i--) {
|
|
487
|
-
results.push(this._sinkDown(i, this.elements.length >> 1));
|
|
488
|
-
}
|
|
489
|
-
return results;
|
|
490
|
-
}
|
|
491
|
-
/**
|
|
492
|
-
* Return all elements in ascending order by repeatedly polling.
|
|
493
|
-
* @remarks Time O(N log N), Space O(N)
|
|
494
|
-
* @returns Sorted array of elements.
|
|
495
|
-
*/
|
|
496
|
-
sort() {
|
|
497
|
-
const visited = [];
|
|
498
|
-
const cloned = this._createInstance();
|
|
499
|
-
for (const x of this.elements)
|
|
500
|
-
cloned.add(x);
|
|
501
|
-
while (!cloned.isEmpty()) {
|
|
502
|
-
const top = cloned.poll();
|
|
503
|
-
if (top !== undefined)
|
|
504
|
-
visited.push(top);
|
|
505
|
-
}
|
|
506
|
-
return visited;
|
|
507
|
-
}
|
|
508
|
-
/**
|
|
509
|
-
* Deep clone this heap.
|
|
510
|
-
* @remarks Time O(N), Space O(N)
|
|
511
|
-
* @returns A new heap with the same elements.
|
|
512
|
-
*/
|
|
513
|
-
clone() {
|
|
514
|
-
const next = this._createInstance();
|
|
515
|
-
for (const x of this.elements)
|
|
516
|
-
next.add(x);
|
|
517
|
-
return next;
|
|
518
|
-
}
|
|
519
|
-
/**
|
|
520
|
-
* Filter elements into a new heap of the same class.
|
|
521
|
-
* @remarks Time O(N log N), Space O(N)
|
|
522
|
-
* @param callback - Predicate (element, index, heap) → boolean to keep element.
|
|
523
|
-
* @param [thisArg] - Value for `this` inside the callback.
|
|
524
|
-
* @returns A new heap with the kept elements.
|
|
525
|
-
*/
|
|
526
|
-
filter(callback, thisArg) {
|
|
527
|
-
const out = this._createInstance();
|
|
528
|
-
let i = 0;
|
|
529
|
-
for (const x of this) {
|
|
530
|
-
if (thisArg === undefined ? callback(x, i++, this) : callback.call(thisArg, x, i++, this)) {
|
|
531
|
-
out.add(x);
|
|
532
|
-
}
|
|
533
|
-
else {
|
|
534
|
-
i++;
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
return out;
|
|
538
|
-
}
|
|
539
|
-
/**
|
|
540
|
-
* Map elements into a new heap of possibly different element type.
|
|
541
|
-
* @remarks Time O(N log N), Space O(N)
|
|
542
|
-
* @template EM
|
|
543
|
-
* @template RM
|
|
544
|
-
* @param callback - Mapping function (element, index, heap) → newElement.
|
|
545
|
-
* @param options - Options for the output heap, including comparator for EM.
|
|
546
|
-
* @param [thisArg] - Value for `this` inside the callback.
|
|
547
|
-
* @returns A new heap with mapped elements.
|
|
548
|
-
*/
|
|
549
|
-
map(callback, options, thisArg) {
|
|
550
|
-
const _a = options !== null && options !== void 0 ? options : {}, { comparator, toElementFn } = _a, rest = __rest(_a, ["comparator", "toElementFn"]);
|
|
551
|
-
if (!comparator)
|
|
552
|
-
throw new TypeError('Heap.map requires options.comparator for EM');
|
|
553
|
-
const out = this._createLike([], Object.assign(Object.assign({}, rest), { comparator, toElementFn }));
|
|
554
|
-
let i = 0;
|
|
555
|
-
for (const x of this) {
|
|
556
|
-
const v = thisArg === undefined ? callback(x, i++, this) : callback.call(thisArg, x, i++, this);
|
|
557
|
-
out.add(v);
|
|
558
|
-
}
|
|
559
|
-
return out;
|
|
560
|
-
}
|
|
561
|
-
/**
|
|
562
|
-
* Map elements into a new heap of the same element type.
|
|
563
|
-
* @remarks Time O(N log N), Space O(N)
|
|
564
|
-
* @param callback - Mapping function (element, index, heap) → element.
|
|
565
|
-
* @param [thisArg] - Value for `this` inside the callback.
|
|
566
|
-
* @returns A new heap with mapped elements.
|
|
567
|
-
*/
|
|
568
|
-
mapSame(callback, thisArg) {
|
|
569
|
-
const out = this._createInstance();
|
|
570
|
-
let i = 0;
|
|
571
|
-
for (const x of this) {
|
|
572
|
-
const v = thisArg === undefined ? callback(x, i++, this) : callback.call(thisArg, x, i++, this);
|
|
573
|
-
out.add(v);
|
|
574
|
-
}
|
|
575
|
-
return out;
|
|
576
|
-
}
|
|
577
|
-
/**
|
|
578
|
-
* Get the comparator used to order elements.
|
|
579
|
-
* @remarks Time O(1), Space O(1)
|
|
580
|
-
* @returns Comparator function.
|
|
581
|
-
*/
|
|
582
|
-
get comparator() {
|
|
583
|
-
return this._comparator;
|
|
584
|
-
}
|
|
585
|
-
*_getIterator() {
|
|
586
|
-
for (const element of this.elements)
|
|
587
|
-
yield element;
|
|
588
|
-
}
|
|
589
|
-
_bubbleUp(index) {
|
|
590
|
-
const element = this.elements[index];
|
|
591
|
-
while (index > 0) {
|
|
592
|
-
const parent = (index - 1) >> 1;
|
|
593
|
-
const parentItem = this.elements[parent];
|
|
594
|
-
if (this.comparator(parentItem, element) <= 0)
|
|
595
|
-
break;
|
|
596
|
-
this.elements[index] = parentItem;
|
|
597
|
-
index = parent;
|
|
598
|
-
}
|
|
599
|
-
this.elements[index] = element;
|
|
600
|
-
return true;
|
|
601
|
-
}
|
|
602
|
-
_sinkDown(index, halfLength) {
|
|
603
|
-
const element = this.elements[index];
|
|
604
|
-
while (index < halfLength) {
|
|
605
|
-
let left = (index << 1) | 1;
|
|
606
|
-
const right = left + 1;
|
|
607
|
-
let minItem = this.elements[left];
|
|
608
|
-
if (right < this.elements.length && this.comparator(minItem, this.elements[right]) > 0) {
|
|
609
|
-
left = right;
|
|
610
|
-
minItem = this.elements[right];
|
|
611
|
-
}
|
|
612
|
-
if (this.comparator(minItem, element) >= 0)
|
|
613
|
-
break;
|
|
614
|
-
this.elements[index] = minItem;
|
|
615
|
-
index = left;
|
|
616
|
-
}
|
|
617
|
-
this.elements[index] = element;
|
|
618
|
-
return true;
|
|
619
|
-
}
|
|
620
|
-
/**
|
|
621
|
-
* (Protected) Create an empty instance of the same concrete class.
|
|
622
|
-
* @remarks Time O(1), Space O(1)
|
|
623
|
-
* @param [options] - Options to override comparator or toElementFn.
|
|
624
|
-
* @returns A like-kind empty heap instance.
|
|
625
|
-
*/
|
|
626
|
-
_createInstance(options) {
|
|
627
|
-
const Ctor = this.constructor;
|
|
628
|
-
const next = new Ctor([], Object.assign({ comparator: this.comparator, toElementFn: this.toElementFn }, (options !== null && options !== void 0 ? options : {})));
|
|
629
|
-
return next;
|
|
630
|
-
}
|
|
631
|
-
/**
|
|
632
|
-
* (Protected) Create a like-kind instance seeded by elements.
|
|
633
|
-
* @remarks Time O(N log N), Space O(N)
|
|
634
|
-
* @template EM
|
|
635
|
-
* @template RM
|
|
636
|
-
* @param [elements] - Iterable of elements or raw values to seed.
|
|
637
|
-
* @param [options] - Options forwarded to the constructor.
|
|
638
|
-
* @returns A like-kind heap instance.
|
|
639
|
-
*/
|
|
640
|
-
_createLike(elements = [], options) {
|
|
641
|
-
const Ctor = this.constructor;
|
|
642
|
-
return new Ctor(elements, options);
|
|
643
|
-
}
|
|
644
|
-
/**
|
|
645
|
-
* (Protected) Spawn an empty like-kind heap instance.
|
|
646
|
-
* @remarks Time O(1), Space O(1)
|
|
647
|
-
* @template EM
|
|
648
|
-
* @template RM
|
|
649
|
-
* @param [options] - Options forwarded to the constructor.
|
|
650
|
-
* @returns An empty like-kind heap instance.
|
|
651
|
-
*/
|
|
652
|
-
_spawnLike(options) {
|
|
653
|
-
return this._createLike([], options);
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
exports.Heap = Heap;
|
|
657
|
-
/**
|
|
658
|
-
* Node container used by FibonacciHeap.
|
|
659
|
-
* @remarks Time O(1), Space O(1)
|
|
660
|
-
* @template E
|
|
661
|
-
*/
|
|
662
|
-
class FibonacciHeapNode {
|
|
663
|
-
constructor(element, degree = 0) {
|
|
664
|
-
this.element = element;
|
|
665
|
-
this.degree = degree;
|
|
666
|
-
this.marked = false;
|
|
667
|
-
}
|
|
668
|
-
}
|
|
669
|
-
exports.FibonacciHeapNode = FibonacciHeapNode;
|
|
670
|
-
/**
|
|
671
|
-
* Fibonacci heap (min-heap) optimized for fast merges and amortized operations.
|
|
672
|
-
* @remarks Time O(1), Space O(1)
|
|
673
|
-
* @template E
|
|
674
|
-
* @example examples will be generated by unit test
|
|
675
|
-
*/
|
|
676
|
-
class FibonacciHeap {
|
|
677
|
-
/**
|
|
678
|
-
* Create a FibonacciHeap.
|
|
679
|
-
* @remarks Time O(1), Space O(1)
|
|
680
|
-
* @param [comparator] - Comparator to order elements (min-heap by default).
|
|
681
|
-
* @returns New FibonacciHeap instance.
|
|
682
|
-
*/
|
|
683
|
-
constructor(comparator) {
|
|
684
|
-
this._size = 0;
|
|
685
|
-
this.clear();
|
|
686
|
-
this._comparator = comparator || this._defaultComparator;
|
|
687
|
-
if (typeof this.comparator !== 'function')
|
|
688
|
-
throw new Error('FibonacciHeap: comparator must be a function.');
|
|
689
|
-
}
|
|
690
|
-
/**
|
|
691
|
-
* Get the circular root list head.
|
|
692
|
-
* @remarks Time O(1), Space O(1)
|
|
693
|
-
* @returns Root node or undefined.
|
|
694
|
-
*/
|
|
695
|
-
get root() {
|
|
696
|
-
return this._root;
|
|
697
|
-
}
|
|
698
|
-
get size() {
|
|
699
|
-
return this._size;
|
|
700
|
-
}
|
|
701
|
-
/**
|
|
702
|
-
* Get the current minimum node.
|
|
703
|
-
* @remarks Time O(1), Space O(1)
|
|
704
|
-
* @returns Min node or undefined.
|
|
705
|
-
*/
|
|
706
|
-
get min() {
|
|
707
|
-
return this._min;
|
|
708
|
-
}
|
|
709
|
-
get comparator() {
|
|
710
|
-
return this._comparator;
|
|
711
|
-
}
|
|
712
|
-
clear() {
|
|
713
|
-
this._root = undefined;
|
|
714
|
-
this._min = undefined;
|
|
715
|
-
this._size = 0;
|
|
716
|
-
}
|
|
717
|
-
add(element) {
|
|
718
|
-
this.push(element);
|
|
719
|
-
return true;
|
|
720
|
-
}
|
|
721
|
-
/**
|
|
722
|
-
* Push an element into the root list.
|
|
723
|
-
* @remarks Time O(1) amortized, Space O(1)
|
|
724
|
-
* @param element - Element to insert.
|
|
725
|
-
* @returns This heap.
|
|
726
|
-
*/
|
|
727
|
-
push(element) {
|
|
728
|
-
const node = this._createNode(element);
|
|
729
|
-
node.left = node;
|
|
730
|
-
node.right = node;
|
|
731
|
-
this.mergeWithRoot(node);
|
|
732
|
-
if (!this.min || this.comparator(node.element, this.min.element) <= 0)
|
|
733
|
-
this._min = node;
|
|
734
|
-
this._size++;
|
|
735
|
-
return this;
|
|
736
|
-
}
|
|
737
|
-
peek() {
|
|
738
|
-
return this.min ? this.min.element : undefined;
|
|
739
|
-
}
|
|
740
|
-
/**
|
|
741
|
-
* Collect nodes from a circular doubly linked list starting at head.
|
|
742
|
-
* @remarks Time O(K), Space O(K)
|
|
743
|
-
* @param [head] - Start node of the circular list.
|
|
744
|
-
* @returns Array of nodes from the list.
|
|
745
|
-
*/
|
|
746
|
-
consumeLinkedList(head) {
|
|
747
|
-
const elements = [];
|
|
748
|
-
if (!head)
|
|
749
|
-
return elements;
|
|
750
|
-
let node = head;
|
|
751
|
-
let started = false;
|
|
752
|
-
while (true) {
|
|
753
|
-
if (node === head && started)
|
|
754
|
-
break;
|
|
755
|
-
else if (node === head)
|
|
756
|
-
started = true;
|
|
757
|
-
elements.push(node);
|
|
758
|
-
node = node.right;
|
|
759
|
-
}
|
|
760
|
-
return elements;
|
|
761
|
-
}
|
|
762
|
-
/**
|
|
763
|
-
* Insert a node into a parent's child list (circular).
|
|
764
|
-
* @remarks Time O(1), Space O(1)
|
|
765
|
-
* @param parent - Parent node.
|
|
766
|
-
* @param node - Child node to insert.
|
|
767
|
-
* @returns void
|
|
768
|
-
*/
|
|
769
|
-
mergeWithChild(parent, node) {
|
|
770
|
-
if (!parent.child)
|
|
771
|
-
parent.child = node;
|
|
772
|
-
else {
|
|
773
|
-
node.right = parent.child.right;
|
|
774
|
-
node.left = parent.child;
|
|
775
|
-
parent.child.right.left = node;
|
|
776
|
-
parent.child.right = node;
|
|
777
|
-
}
|
|
778
|
-
}
|
|
779
|
-
poll() {
|
|
780
|
-
return this.pop();
|
|
781
|
-
}
|
|
782
|
-
/**
|
|
783
|
-
* Remove and return the minimum element, consolidating the root list.
|
|
784
|
-
* @remarks Time O(log N) amortized, Space O(1)
|
|
785
|
-
* @returns Minimum element or undefined.
|
|
786
|
-
*/
|
|
787
|
-
pop() {
|
|
788
|
-
if (this._size === 0)
|
|
789
|
-
return undefined;
|
|
790
|
-
const z = this.min;
|
|
791
|
-
if (z.child) {
|
|
792
|
-
const elements = this.consumeLinkedList(z.child);
|
|
793
|
-
for (const node of elements) {
|
|
794
|
-
this.mergeWithRoot(node);
|
|
795
|
-
node.parent = undefined;
|
|
796
|
-
}
|
|
797
|
-
}
|
|
798
|
-
this.removeFromRoot(z);
|
|
799
|
-
if (z === z.right) {
|
|
800
|
-
this._min = undefined;
|
|
801
|
-
this._root = undefined;
|
|
802
|
-
}
|
|
803
|
-
else {
|
|
804
|
-
this._min = z.right;
|
|
805
|
-
this._consolidate();
|
|
806
|
-
}
|
|
807
|
-
this._size--;
|
|
808
|
-
return z.element;
|
|
809
|
-
}
|
|
810
|
-
/**
|
|
811
|
-
* Meld another heap into this heap.
|
|
812
|
-
* @remarks Time O(1), Space O(1)
|
|
813
|
-
* @param heapToMerge - Another FibonacciHeap to meld into this one.
|
|
814
|
-
* @returns void
|
|
815
|
-
*/
|
|
816
|
-
merge(heapToMerge) {
|
|
817
|
-
if (heapToMerge.size === 0)
|
|
818
|
-
return;
|
|
819
|
-
if (this.root && heapToMerge.root) {
|
|
820
|
-
const thisRoot = this.root, otherRoot = heapToMerge.root;
|
|
821
|
-
const thisRootRight = thisRoot.right, otherRootLeft = otherRoot.left;
|
|
822
|
-
thisRoot.right = otherRoot;
|
|
823
|
-
otherRoot.left = thisRoot;
|
|
824
|
-
thisRootRight.left = otherRootLeft;
|
|
825
|
-
otherRootLeft.right = thisRootRight;
|
|
826
|
-
}
|
|
827
|
-
else if (!this.root && heapToMerge.root) {
|
|
828
|
-
this._root = heapToMerge.root;
|
|
829
|
-
}
|
|
830
|
-
if (!this.min || (heapToMerge.min && this.comparator(heapToMerge.min.element, this.min.element) < 0)) {
|
|
831
|
-
this._min = heapToMerge.min;
|
|
832
|
-
}
|
|
833
|
-
this._size += heapToMerge.size;
|
|
834
|
-
heapToMerge.clear();
|
|
835
|
-
}
|
|
836
|
-
_createNode(element) {
|
|
837
|
-
return new FibonacciHeapNode(element);
|
|
838
|
-
}
|
|
839
|
-
isEmpty() {
|
|
840
|
-
return this._size === 0;
|
|
841
|
-
}
|
|
842
|
-
_defaultComparator(a, b) {
|
|
843
|
-
if (a < b)
|
|
844
|
-
return -1;
|
|
845
|
-
if (a > b)
|
|
846
|
-
return 1;
|
|
847
|
-
return 0;
|
|
848
|
-
}
|
|
849
|
-
mergeWithRoot(node) {
|
|
850
|
-
if (!this.root)
|
|
851
|
-
this._root = node;
|
|
852
|
-
else {
|
|
853
|
-
node.right = this.root.right;
|
|
854
|
-
node.left = this.root;
|
|
855
|
-
this.root.right.left = node;
|
|
856
|
-
this.root.right = node;
|
|
857
|
-
}
|
|
858
|
-
}
|
|
859
|
-
removeFromRoot(node) {
|
|
860
|
-
if (this.root === node)
|
|
861
|
-
this._root = node.right;
|
|
862
|
-
if (node.left)
|
|
863
|
-
node.left.right = node.right;
|
|
864
|
-
if (node.right)
|
|
865
|
-
node.right.left = node.left;
|
|
866
|
-
}
|
|
867
|
-
_link(y, x) {
|
|
868
|
-
this.removeFromRoot(y);
|
|
869
|
-
y.left = y;
|
|
870
|
-
y.right = y;
|
|
871
|
-
this.mergeWithChild(x, y);
|
|
872
|
-
x.degree++;
|
|
873
|
-
y.parent = x;
|
|
874
|
-
}
|
|
875
|
-
_consolidate() {
|
|
876
|
-
const A = new Array(this._size);
|
|
877
|
-
const elements = this.consumeLinkedList(this.root);
|
|
878
|
-
let x, y, d, t;
|
|
879
|
-
for (const node of elements) {
|
|
880
|
-
x = node;
|
|
881
|
-
d = x.degree;
|
|
882
|
-
while (A[d]) {
|
|
883
|
-
y = A[d];
|
|
884
|
-
if (this.comparator(x.element, y.element) > 0) {
|
|
885
|
-
t = x;
|
|
886
|
-
x = y;
|
|
887
|
-
y = t;
|
|
888
|
-
}
|
|
889
|
-
this._link(y, x);
|
|
890
|
-
A[d] = undefined;
|
|
891
|
-
d++;
|
|
892
|
-
}
|
|
893
|
-
A[d] = x;
|
|
894
|
-
}
|
|
895
|
-
for (let i = 0; i < A.length; i++) {
|
|
896
|
-
if (A[i] && (!this.min || this.comparator(A[i].element, this.min.element) <= 0))
|
|
897
|
-
this._min = A[i];
|
|
898
|
-
}
|
|
899
|
-
}
|
|
900
|
-
}
|
|
901
|
-
exports.FibonacciHeap = FibonacciHeap;
|