data-structure-typed 2.5.0 → 2.5.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/.vitepress/cache/deps_temp_51f5f1b0/chunk-7OIKW5WK.js +12984 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/package.json +3 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vue_devtools-api.js +4505 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vueuse_core.js +9731 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vue.js +347 -0
- package/CHANGELOG.md +5 -1
- package/README.md +124 -29
- package/dist/cjs/binary-tree.cjs +26282 -0
- package/dist/cjs/graph.cjs +5422 -0
- package/dist/cjs/hash.cjs +1310 -0
- package/dist/cjs/heap.cjs +1602 -0
- package/dist/cjs/index.cjs +31257 -14673
- package/dist/cjs/linked-list.cjs +4576 -0
- package/dist/cjs/matrix.cjs +1080 -0
- package/dist/cjs/priority-queue.cjs +1376 -0
- package/dist/cjs/queue.cjs +4264 -0
- package/dist/cjs/stack.cjs +907 -0
- package/dist/cjs/trie.cjs +1223 -0
- package/dist/cjs-legacy/binary-tree.cjs +26319 -0
- package/dist/cjs-legacy/graph.cjs +5420 -0
- package/dist/cjs-legacy/hash.cjs +1310 -0
- package/dist/cjs-legacy/heap.cjs +1599 -0
- package/dist/cjs-legacy/index.cjs +31268 -14679
- package/dist/cjs-legacy/linked-list.cjs +4582 -0
- package/dist/cjs-legacy/matrix.cjs +1083 -0
- package/dist/cjs-legacy/priority-queue.cjs +1374 -0
- package/dist/cjs-legacy/queue.cjs +4262 -0
- package/dist/cjs-legacy/stack.cjs +907 -0
- package/dist/cjs-legacy/trie.cjs +1222 -0
- package/dist/esm/binary-tree.mjs +26267 -0
- package/dist/esm/graph.mjs +5409 -0
- package/dist/esm/hash.mjs +1307 -0
- package/dist/esm/heap.mjs +1596 -0
- package/dist/esm/index.mjs +31254 -14674
- package/dist/esm/linked-list.mjs +4569 -0
- package/dist/esm/matrix.mjs +1076 -0
- package/dist/esm/priority-queue.mjs +1372 -0
- package/dist/esm/queue.mjs +4260 -0
- package/dist/esm/stack.mjs +905 -0
- package/dist/esm/trie.mjs +1220 -0
- package/dist/esm-legacy/binary-tree.mjs +26304 -0
- package/dist/esm-legacy/graph.mjs +5407 -0
- package/dist/esm-legacy/hash.mjs +1307 -0
- package/dist/esm-legacy/heap.mjs +1593 -0
- package/dist/esm-legacy/index.mjs +31265 -14680
- package/dist/esm-legacy/linked-list.mjs +4575 -0
- package/dist/esm-legacy/matrix.mjs +1079 -0
- package/dist/esm-legacy/priority-queue.mjs +1370 -0
- package/dist/esm-legacy/queue.mjs +4258 -0
- package/dist/esm-legacy/stack.mjs +905 -0
- package/dist/esm-legacy/trie.mjs +1219 -0
- package/dist/types/common/error.d.ts +9 -0
- package/dist/types/common/index.d.ts +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +288 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +336 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +618 -18
- package/dist/types/data-structures/binary-tree/bst.d.ts +676 -1
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +456 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +144 -1
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3307 -399
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3285 -360
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2674 -325
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3072 -287
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +240 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +216 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +274 -10
- package/dist/types/data-structures/heap/heap.d.ts +336 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +411 -3
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +363 -3
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +434 -2
- package/dist/types/data-structures/matrix/matrix.d.ts +192 -0
- package/dist/types/data-structures/queue/deque.d.ts +364 -4
- package/dist/types/data-structures/queue/queue.d.ts +288 -0
- package/dist/types/data-structures/stack/stack.d.ts +240 -0
- package/dist/types/data-structures/trie/trie.d.ts +292 -4
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
- package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/data-structure-typed.js +31196 -14608
- package/dist/umd/data-structure-typed.min.js +11 -5
- package/docs-site-docusaurus/README.md +41 -0
- package/docs-site-docusaurus/docs/api/README.md +52 -0
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +6644 -0
- package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +282 -0
- package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +2266 -0
- package/docs-site-docusaurus/docs/api/classes/BST.md +6293 -0
- package/docs-site-docusaurus/docs/api/classes/BSTNode.md +333 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +455 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +4647 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +331 -0
- package/docs-site-docusaurus/docs/api/classes/Deque.md +2767 -0
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +2999 -0
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +2685 -0
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +221 -0
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +253 -0
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +21 -0
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +1333 -0
- package/docs-site-docusaurus/docs/api/classes/Heap.md +1881 -0
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +800 -0
- package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +644 -0
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +1632 -0
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +1853 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +1108 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +156 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +2824 -0
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +2929 -0
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +1026 -0
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +1866 -0
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +1883 -0
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +1879 -0
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +1882 -0
- package/docs-site-docusaurus/docs/api/classes/Navigator.md +109 -0
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +1839 -0
- package/docs-site-docusaurus/docs/api/classes/Queue.md +2244 -0
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +6888 -0
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +372 -0
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +2897 -0
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +169 -0
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +1229 -0
- package/docs-site-docusaurus/docs/api/classes/Stack.md +1573 -0
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +1389 -0
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +1591 -0
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +1246 -0
- package/docs-site-docusaurus/docs/api/classes/Trie.md +1708 -0
- package/docs-site-docusaurus/docs/api/classes/TrieNode.md +199 -0
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +2979 -0
- package/docs-site-docusaurus/docs/guide/_category_.json +6 -0
- package/docs-site-docusaurus/docs/guide/architecture.md +615 -0
- package/docs-site-docusaurus/docs/guide/concepts.md +451 -0
- package/docs-site-docusaurus/docs/guide/faq.md +180 -0
- package/docs-site-docusaurus/docs/guide/guides.md +597 -0
- package/docs-site-docusaurus/docs/guide/installation.md +62 -0
- package/docs-site-docusaurus/docs/guide/integrations.md +825 -0
- package/docs-site-docusaurus/docs/guide/overview.md +645 -0
- package/docs-site-docusaurus/docs/guide/performance.md +835 -0
- package/docs-site-docusaurus/docs/guide/quick-start.md +104 -0
- package/docs-site-docusaurus/docs/guide/use-cases/_category_.json +6 -0
- package/docs-site-docusaurus/docs/guide/use-cases/array-sort-alternative.md +158 -0
- package/docs-site-docusaurus/docs/guide/use-cases/heap-vs-sorting.md +92 -0
- package/docs-site-docusaurus/docs/guide/use-cases/map-vs-treemap.md +151 -0
- package/docs-site-docusaurus/docs/guide/use-cases/priority-queue-typescript.md +113 -0
- package/docs-site-docusaurus/docs/guide/use-cases/treemap-javascript.md +151 -0
- package/docs-site-docusaurus/docusaurus.config.ts +159 -0
- package/docs-site-docusaurus/fix-mdx-generics.mjs +75 -0
- package/docs-site-docusaurus/package-lock.json +18667 -0
- package/docs-site-docusaurus/package.json +50 -0
- package/docs-site-docusaurus/prefix-class-to-methods.mjs +48 -0
- package/docs-site-docusaurus/sidebars.ts +23 -0
- package/docs-site-docusaurus/sort-protected.mjs +87 -0
- package/docs-site-docusaurus/src/css/custom.css +96 -0
- package/docs-site-docusaurus/src/pages/index.module.css +13 -0
- package/docs-site-docusaurus/src/pages/index.tsx +120 -0
- package/docs-site-docusaurus/src/pages/markdown-page.md +7 -0
- package/docs-site-docusaurus/src/theme/TOCItems/index.tsx +34 -0
- package/docs-site-docusaurus/static/.nojekyll +0 -0
- package/docs-site-docusaurus/static/img/docusaurus-social-card.jpg +0 -0
- package/docs-site-docusaurus/static/img/docusaurus.png +0 -0
- package/docs-site-docusaurus/static/img/favicon.ico +0 -0
- package/docs-site-docusaurus/static/img/favicon.png +0 -0
- package/docs-site-docusaurus/static/img/logo-180.png +0 -0
- package/docs-site-docusaurus/static/img/logo.jpg +0 -0
- package/docs-site-docusaurus/static/img/logo.png +0 -0
- package/docs-site-docusaurus/static/img/logo.svg +1 -0
- package/docs-site-docusaurus/static/img/og-image.png +0 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_mountain.svg +171 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_react.svg +170 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_tree.svg +40 -0
- package/docs-site-docusaurus/static/llms.txt +37 -0
- package/docs-site-docusaurus/static/robots.txt +4 -0
- package/docs-site-docusaurus/typedoc.json +23 -0
- package/llms.txt +37 -0
- package/package.json +159 -55
- package/src/common/error.ts +19 -1
- package/src/common/index.ts +1 -1
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +3 -2
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +287 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +327 -5
- package/src/data-structures/binary-tree/binary-tree.ts +581 -6
- package/src/data-structures/binary-tree/bst.ts +922 -7
- package/src/data-structures/binary-tree/red-black-tree.ts +453 -0
- package/src/data-structures/binary-tree/segment-tree.ts +139 -2
- package/src/data-structures/binary-tree/tree-map.ts +3300 -495
- package/src/data-structures/binary-tree/tree-multi-map.ts +3384 -563
- package/src/data-structures/binary-tree/tree-multi-set.ts +2757 -493
- package/src/data-structures/binary-tree/tree-set.ts +3122 -440
- package/src/data-structures/graph/abstract-graph.ts +6 -6
- package/src/data-structures/graph/directed-graph.ts +230 -0
- package/src/data-structures/graph/undirected-graph.ts +207 -0
- package/src/data-structures/hash/hash-map.ts +270 -19
- package/src/data-structures/heap/heap.ts +326 -4
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +394 -3
- package/src/data-structures/linked-list/singly-linked-list.ts +348 -3
- package/src/data-structures/linked-list/skip-linked-list.ts +421 -7
- package/src/data-structures/matrix/matrix.ts +194 -10
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +350 -5
- package/src/data-structures/queue/queue.ts +276 -0
- package/src/data-structures/stack/stack.ts +230 -0
- package/src/data-structures/trie/trie.ts +283 -7
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
- package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
- package/vercel.json +6 -0
- package/dist/leetcode/avl-tree-counter.mjs +0 -2957
- package/dist/leetcode/avl-tree-multi-map.mjs +0 -2889
- package/dist/leetcode/avl-tree.mjs +0 -2720
- package/dist/leetcode/binary-tree.mjs +0 -1594
- package/dist/leetcode/bst.mjs +0 -2398
- package/dist/leetcode/deque.mjs +0 -683
- package/dist/leetcode/directed-graph.mjs +0 -1733
- package/dist/leetcode/doubly-linked-list.mjs +0 -709
- package/dist/leetcode/hash-map.mjs +0 -493
- package/dist/leetcode/heap.mjs +0 -542
- package/dist/leetcode/max-heap.mjs +0 -375
- package/dist/leetcode/max-priority-queue.mjs +0 -383
- package/dist/leetcode/min-heap.mjs +0 -363
- package/dist/leetcode/min-priority-queue.mjs +0 -371
- package/dist/leetcode/priority-queue.mjs +0 -363
- package/dist/leetcode/queue.mjs +0 -943
- package/dist/leetcode/red-black-tree.mjs +0 -2765
- package/dist/leetcode/singly-linked-list.mjs +0 -754
- package/dist/leetcode/stack.mjs +0 -217
- package/dist/leetcode/tree-counter.mjs +0 -3039
- package/dist/leetcode/tree-multi-map.mjs +0 -2913
- package/dist/leetcode/trie.mjs +0 -413
- package/dist/leetcode/undirected-graph.mjs +0 -1650
|
@@ -0,0 +1,1879 @@
|
|
|
1
|
+
[**data-structure-typed**](../README.md)
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
[data-structure-typed](../README.md) / MinHeap
|
|
6
|
+
|
|
7
|
+
# Class: MinHeap\<E, R\>
|
|
8
|
+
|
|
9
|
+
Defined in: [data-structures/heap/min-heap.ts:86](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/min-heap.ts#L86)
|
|
10
|
+
|
|
11
|
+
## Examples
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
// Merge K sorted arrays
|
|
15
|
+
const arrays = [
|
|
16
|
+
[1, 4, 7],
|
|
17
|
+
[2, 5, 8],
|
|
18
|
+
[3, 6, 9]
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
// Use min heap to merge: track (value, arrayIndex, elementIndex)
|
|
22
|
+
const heap = new MinHeap<[number, number, number]>([], {
|
|
23
|
+
comparator: (a, b) => a[0] - b[0]
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// Initialize with first element of each array
|
|
27
|
+
arrays.forEach((arr, i) => heap.add([arr[0], i, 0]));
|
|
28
|
+
|
|
29
|
+
const merged: number[] = [];
|
|
30
|
+
while (heap.size > 0) {
|
|
31
|
+
const [val, arrIdx, elemIdx] = heap.poll()!;
|
|
32
|
+
merged.push(val);
|
|
33
|
+
if (elemIdx + 1 < arrays[arrIdx].length) {
|
|
34
|
+
heap.add([arrays[arrIdx][elemIdx + 1], arrIdx, elemIdx + 1]);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
console.log(merged); // [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
// Dijkstra-style shortest distance tracking
|
|
43
|
+
// Simulating distance updates: (distance, nodeId)
|
|
44
|
+
const heap = new MinHeap<[number, string]>([], {
|
|
45
|
+
comparator: (a, b) => a[0] - b[0]
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
heap.add([0, 'start']);
|
|
49
|
+
heap.add([10, 'A']);
|
|
50
|
+
heap.add([5, 'B']);
|
|
51
|
+
heap.add([3, 'C']);
|
|
52
|
+
|
|
53
|
+
// Process nearest node first
|
|
54
|
+
console.log(heap.poll()); // [0, 'start'];
|
|
55
|
+
console.log(heap.poll()); // [3, 'C'];
|
|
56
|
+
console.log(heap.poll()); // [5, 'B'];
|
|
57
|
+
console.log(heap.poll()); // [10, 'A'];
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
// Running median with min heap (upper half)
|
|
62
|
+
const upperHalf = new MinHeap<number>();
|
|
63
|
+
|
|
64
|
+
// Add larger numbers to min heap
|
|
65
|
+
for (const n of [5, 8, 3, 9, 1]) {
|
|
66
|
+
upperHalf.add(n);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Smallest of the upper half is always accessible
|
|
70
|
+
console.log(upperHalf.peek()); // 1;
|
|
71
|
+
console.log(upperHalf.size); // 5;
|
|
72
|
+
|
|
73
|
+
// Remove smallest repeatedly
|
|
74
|
+
console.log(upperHalf.poll()); // 1;
|
|
75
|
+
console.log(upperHalf.poll()); // 3;
|
|
76
|
+
console.log(upperHalf.peek()); // 5;
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Extends
|
|
80
|
+
|
|
81
|
+
- [`Heap`](Heap.md)\<`E`, `R`\>
|
|
82
|
+
|
|
83
|
+
## Type Parameters
|
|
84
|
+
|
|
85
|
+
### E
|
|
86
|
+
|
|
87
|
+
`E` = `any`
|
|
88
|
+
|
|
89
|
+
### R
|
|
90
|
+
|
|
91
|
+
`R` = `any`
|
|
92
|
+
|
|
93
|
+
Min-oriented binary heap.
|
|
94
|
+
Notes and typical use-cases are documented in [Heap](Heap.md).
|
|
95
|
+
|
|
96
|
+
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.
|
|
97
|
+
2. MinHeap Properties: The value of each parent node is less than or equal to the value of its children.
|
|
98
|
+
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.
|
|
99
|
+
4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
|
|
100
|
+
5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
|
|
101
|
+
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.
|
|
102
|
+
7. Efficient Sorting Algorithms: For example, heap sort. MinHeap sort uses the properties of a heap to sort elements.
|
|
103
|
+
8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
104
|
+
|
|
105
|
+
## Constructors
|
|
106
|
+
|
|
107
|
+
### Constructor
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
new MinHeap<E, R>(elements?, options?): MinHeap<E, R>;
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Defined in: [data-structures/heap/min-heap.ts:92](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/min-heap.ts#L92)
|
|
114
|
+
|
|
115
|
+
Create a min-heap.
|
|
116
|
+
|
|
117
|
+
#### Parameters
|
|
118
|
+
|
|
119
|
+
##### elements?
|
|
120
|
+
|
|
121
|
+
`Iterable`\<`E`, `any`, `any`\> \| `Iterable`\<`R`, `any`, `any`\>
|
|
122
|
+
|
|
123
|
+
Optional initial elements.
|
|
124
|
+
|
|
125
|
+
##### options?
|
|
126
|
+
|
|
127
|
+
`HeapOptions`\<`E`, `R`\>
|
|
128
|
+
|
|
129
|
+
Optional configuration.
|
|
130
|
+
|
|
131
|
+
#### Returns
|
|
132
|
+
|
|
133
|
+
`MinHeap`\<`E`, `R`\>
|
|
134
|
+
|
|
135
|
+
#### Overrides
|
|
136
|
+
|
|
137
|
+
[`Heap`](Heap.md).[`constructor`](Heap.md#constructor)
|
|
138
|
+
|
|
139
|
+
## Properties
|
|
140
|
+
|
|
141
|
+
### comparator
|
|
142
|
+
|
|
143
|
+
#### Get Signature
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
get comparator(): Comparator<E>;
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
Defined in: [data-structures/heap/heap.ts:1184](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L1184)
|
|
150
|
+
|
|
151
|
+
Get the comparator used to order elements.
|
|
152
|
+
|
|
153
|
+
##### Remarks
|
|
154
|
+
|
|
155
|
+
Time O(1), Space O(1)
|
|
156
|
+
|
|
157
|
+
##### Returns
|
|
158
|
+
|
|
159
|
+
`Comparator`\<`E`\>
|
|
160
|
+
|
|
161
|
+
Comparator function.
|
|
162
|
+
|
|
163
|
+
#### Inherited from
|
|
164
|
+
|
|
165
|
+
[`Heap`](Heap.md).[`comparator`](Heap.md#comparator)
|
|
166
|
+
|
|
167
|
+
***
|
|
168
|
+
|
|
169
|
+
### elements
|
|
170
|
+
|
|
171
|
+
#### Get Signature
|
|
172
|
+
|
|
173
|
+
```ts
|
|
174
|
+
get elements(): E[];
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
Defined in: [data-structures/heap/heap.ts:180](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L180)
|
|
178
|
+
|
|
179
|
+
Get the backing array of the heap.
|
|
180
|
+
|
|
181
|
+
##### Remarks
|
|
182
|
+
|
|
183
|
+
Time O(1), Space O(1)
|
|
184
|
+
|
|
185
|
+
##### Returns
|
|
186
|
+
|
|
187
|
+
`E`[]
|
|
188
|
+
|
|
189
|
+
Internal elements array.
|
|
190
|
+
|
|
191
|
+
#### Inherited from
|
|
192
|
+
|
|
193
|
+
[`Heap`](Heap.md).[`elements`](Heap.md#elements)
|
|
194
|
+
|
|
195
|
+
***
|
|
196
|
+
|
|
197
|
+
### leaf
|
|
198
|
+
|
|
199
|
+
#### Get Signature
|
|
200
|
+
|
|
201
|
+
```ts
|
|
202
|
+
get leaf(): E | undefined;
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
Defined in: [data-structures/heap/heap.ts:244](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L244)
|
|
206
|
+
|
|
207
|
+
Get the last leaf element.
|
|
208
|
+
|
|
209
|
+
##### Remarks
|
|
210
|
+
|
|
211
|
+
Time O(1), Space O(1)
|
|
212
|
+
|
|
213
|
+
##### Returns
|
|
214
|
+
|
|
215
|
+
`E` \| `undefined`
|
|
216
|
+
|
|
217
|
+
Last element or undefined.
|
|
218
|
+
|
|
219
|
+
#### Inherited from
|
|
220
|
+
|
|
221
|
+
[`Heap`](Heap.md).[`leaf`](Heap.md#leaf)
|
|
222
|
+
|
|
223
|
+
***
|
|
224
|
+
|
|
225
|
+
### size
|
|
226
|
+
|
|
227
|
+
#### Get Signature
|
|
228
|
+
|
|
229
|
+
```ts
|
|
230
|
+
get size(): number;
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
Defined in: [data-structures/heap/heap.ts:234](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L234)
|
|
234
|
+
|
|
235
|
+
Get the number of elements.
|
|
236
|
+
|
|
237
|
+
##### Remarks
|
|
238
|
+
|
|
239
|
+
Time O(1), Space O(1)
|
|
240
|
+
|
|
241
|
+
##### Example
|
|
242
|
+
|
|
243
|
+
```ts
|
|
244
|
+
// Track heap capacity
|
|
245
|
+
const heap = new Heap<number>();
|
|
246
|
+
console.log(heap.size); // 0;
|
|
247
|
+
heap.add(10);
|
|
248
|
+
heap.add(20);
|
|
249
|
+
console.log(heap.size); // 2;
|
|
250
|
+
heap.poll();
|
|
251
|
+
console.log(heap.size); // 1;
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
##### Returns
|
|
255
|
+
|
|
256
|
+
`number`
|
|
257
|
+
|
|
258
|
+
Heap size.
|
|
259
|
+
|
|
260
|
+
*
|
|
261
|
+
|
|
262
|
+
#### Inherited from
|
|
263
|
+
|
|
264
|
+
[`Heap`](Heap.md).[`size`](Heap.md#size)
|
|
265
|
+
|
|
266
|
+
***
|
|
267
|
+
|
|
268
|
+
### toElementFn
|
|
269
|
+
|
|
270
|
+
#### Get Signature
|
|
271
|
+
|
|
272
|
+
```ts
|
|
273
|
+
get toElementFn(): ((rawElement) => E) | undefined;
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
Defined in: [data-structures/base/iterable-element-base.ts:48](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L48)
|
|
277
|
+
|
|
278
|
+
Exposes the current `toElementFn`, if configured.
|
|
279
|
+
|
|
280
|
+
##### Remarks
|
|
281
|
+
|
|
282
|
+
Time O(1), Space O(1).
|
|
283
|
+
|
|
284
|
+
##### Returns
|
|
285
|
+
|
|
286
|
+
((`rawElement`) => `E`) \| `undefined`
|
|
287
|
+
|
|
288
|
+
The converter function or `undefined` when not set.
|
|
289
|
+
|
|
290
|
+
#### Inherited from
|
|
291
|
+
|
|
292
|
+
[`Heap`](Heap.md).[`toElementFn`](Heap.md#toelementfn)
|
|
293
|
+
|
|
294
|
+
## Methods
|
|
295
|
+
|
|
296
|
+
### \[iterator\]()
|
|
297
|
+
|
|
298
|
+
```ts
|
|
299
|
+
iterator: IterableIterator<E>;
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
Defined in: [data-structures/base/iterable-element-base.ts:61](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L61)
|
|
303
|
+
|
|
304
|
+
Returns an iterator over the structure's elements.
|
|
305
|
+
|
|
306
|
+
#### Parameters
|
|
307
|
+
|
|
308
|
+
##### args
|
|
309
|
+
|
|
310
|
+
...`unknown`[]
|
|
311
|
+
|
|
312
|
+
Optional iterator arguments forwarded to the internal iterator.
|
|
313
|
+
|
|
314
|
+
#### Returns
|
|
315
|
+
|
|
316
|
+
`IterableIterator`\<`E`\>
|
|
317
|
+
|
|
318
|
+
An `IterableIterator<E>` that yields the elements in traversal order.
|
|
319
|
+
|
|
320
|
+
#### Remarks
|
|
321
|
+
|
|
322
|
+
Producing the iterator is O(1); consuming the entire iterator is Time O(n) with O(1) extra space.
|
|
323
|
+
|
|
324
|
+
#### Inherited from
|
|
325
|
+
|
|
326
|
+
[`Heap`](Heap.md).[`[iterator]`](Heap.md#iterator)
|
|
327
|
+
|
|
328
|
+
***
|
|
329
|
+
|
|
330
|
+
### add()
|
|
331
|
+
|
|
332
|
+
```ts
|
|
333
|
+
add(element): boolean;
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
Defined in: [data-structures/heap/heap.ts:338](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L338)
|
|
337
|
+
|
|
338
|
+
Insert an element.
|
|
339
|
+
|
|
340
|
+
#### Parameters
|
|
341
|
+
|
|
342
|
+
##### element
|
|
343
|
+
|
|
344
|
+
`E`
|
|
345
|
+
|
|
346
|
+
Element to insert.
|
|
347
|
+
|
|
348
|
+
#### Returns
|
|
349
|
+
|
|
350
|
+
`boolean`
|
|
351
|
+
|
|
352
|
+
True.
|
|
353
|
+
|
|
354
|
+
*
|
|
355
|
+
|
|
356
|
+
#### Remarks
|
|
357
|
+
|
|
358
|
+
Time O(1) amortized, Space O(1)
|
|
359
|
+
|
|
360
|
+
#### Example
|
|
361
|
+
|
|
362
|
+
```ts
|
|
363
|
+
// basic Heap creation and add operation
|
|
364
|
+
// Create a min heap (default)
|
|
365
|
+
const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
366
|
+
|
|
367
|
+
// Verify size
|
|
368
|
+
console.log(minHeap.size); // 6;
|
|
369
|
+
|
|
370
|
+
// Add new element
|
|
371
|
+
minHeap.add(4);
|
|
372
|
+
console.log(minHeap.size); // 7;
|
|
373
|
+
|
|
374
|
+
// Min heap property: smallest element at root
|
|
375
|
+
const min = minHeap.peek();
|
|
376
|
+
console.log(min); // 1;
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
#### Inherited from
|
|
380
|
+
|
|
381
|
+
[`Heap`](Heap.md).[`add`](Heap.md#add)
|
|
382
|
+
|
|
383
|
+
***
|
|
384
|
+
|
|
385
|
+
### addMany()
|
|
386
|
+
|
|
387
|
+
```ts
|
|
388
|
+
addMany(elements): boolean[];
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
Defined in: [data-structures/heap/heap.ts:388](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L388)
|
|
392
|
+
|
|
393
|
+
Insert many elements from an iterable.
|
|
394
|
+
|
|
395
|
+
#### Parameters
|
|
396
|
+
|
|
397
|
+
##### elements
|
|
398
|
+
|
|
399
|
+
`Iterable`\<`E` \| `R`\>
|
|
400
|
+
|
|
401
|
+
Iterable of elements or raw values.
|
|
402
|
+
|
|
403
|
+
#### Returns
|
|
404
|
+
|
|
405
|
+
`boolean`[]
|
|
406
|
+
|
|
407
|
+
Array of per-element success flags.
|
|
408
|
+
|
|
409
|
+
*
|
|
410
|
+
|
|
411
|
+
#### Remarks
|
|
412
|
+
|
|
413
|
+
Time O(N log N), Space O(1)
|
|
414
|
+
|
|
415
|
+
#### Example
|
|
416
|
+
|
|
417
|
+
```ts
|
|
418
|
+
// Add multiple elements
|
|
419
|
+
const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
420
|
+
heap.addMany([5, 3, 7, 1]);
|
|
421
|
+
console.log(heap.peek()); // 1;
|
|
422
|
+
console.log(heap.size); // 4;
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
#### Inherited from
|
|
426
|
+
|
|
427
|
+
[`Heap`](Heap.md).[`addMany`](Heap.md#addmany)
|
|
428
|
+
|
|
429
|
+
***
|
|
430
|
+
|
|
431
|
+
### clear()
|
|
432
|
+
|
|
433
|
+
```ts
|
|
434
|
+
clear(): void;
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
Defined in: [data-structures/heap/heap.ts:676](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L676)
|
|
438
|
+
|
|
439
|
+
Remove all elements.
|
|
440
|
+
|
|
441
|
+
#### Returns
|
|
442
|
+
|
|
443
|
+
`void`
|
|
444
|
+
|
|
445
|
+
void
|
|
446
|
+
|
|
447
|
+
*
|
|
448
|
+
|
|
449
|
+
#### Remarks
|
|
450
|
+
|
|
451
|
+
Time O(1), Space O(1)
|
|
452
|
+
|
|
453
|
+
#### Example
|
|
454
|
+
|
|
455
|
+
```ts
|
|
456
|
+
// Remove all elements
|
|
457
|
+
const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
458
|
+
heap.clear();
|
|
459
|
+
console.log(heap.isEmpty()); // true;
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
#### Inherited from
|
|
463
|
+
|
|
464
|
+
[`Heap`](Heap.md).[`clear`](Heap.md#clear)
|
|
465
|
+
|
|
466
|
+
***
|
|
467
|
+
|
|
468
|
+
### clone()
|
|
469
|
+
|
|
470
|
+
```ts
|
|
471
|
+
clone(): this;
|
|
472
|
+
```
|
|
473
|
+
|
|
474
|
+
Defined in: [data-structures/heap/heap.ts:1020](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L1020)
|
|
475
|
+
|
|
476
|
+
Deep clone this heap.
|
|
477
|
+
|
|
478
|
+
#### Returns
|
|
479
|
+
|
|
480
|
+
`this`
|
|
481
|
+
|
|
482
|
+
A new heap with the same elements.
|
|
483
|
+
|
|
484
|
+
*
|
|
485
|
+
|
|
486
|
+
#### Remarks
|
|
487
|
+
|
|
488
|
+
Time O(N), Space O(N)
|
|
489
|
+
|
|
490
|
+
#### Example
|
|
491
|
+
|
|
492
|
+
```ts
|
|
493
|
+
// Create independent copy
|
|
494
|
+
const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
495
|
+
const copy = heap.clone();
|
|
496
|
+
copy.poll();
|
|
497
|
+
console.log(heap.size); // 3;
|
|
498
|
+
console.log(copy.size); // 2;
|
|
499
|
+
```
|
|
500
|
+
|
|
501
|
+
#### Inherited from
|
|
502
|
+
|
|
503
|
+
[`Heap`](Heap.md).[`clone`](Heap.md#clone)
|
|
504
|
+
|
|
505
|
+
***
|
|
506
|
+
|
|
507
|
+
### delete()
|
|
508
|
+
|
|
509
|
+
```ts
|
|
510
|
+
delete(element): boolean;
|
|
511
|
+
```
|
|
512
|
+
|
|
513
|
+
Defined in: [data-structures/heap/heap.ts:779](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L779)
|
|
514
|
+
|
|
515
|
+
Delete one occurrence of an element.
|
|
516
|
+
|
|
517
|
+
#### Parameters
|
|
518
|
+
|
|
519
|
+
##### element
|
|
520
|
+
|
|
521
|
+
`E`
|
|
522
|
+
|
|
523
|
+
Element to delete.
|
|
524
|
+
|
|
525
|
+
#### Returns
|
|
526
|
+
|
|
527
|
+
`boolean`
|
|
528
|
+
|
|
529
|
+
True if an element was removed.
|
|
530
|
+
|
|
531
|
+
*
|
|
532
|
+
|
|
533
|
+
#### Remarks
|
|
534
|
+
|
|
535
|
+
Time O(N), Space O(1)
|
|
536
|
+
|
|
537
|
+
#### Example
|
|
538
|
+
|
|
539
|
+
```ts
|
|
540
|
+
// Remove specific element
|
|
541
|
+
const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
542
|
+
heap.delete(4);
|
|
543
|
+
console.log(heap.toArray().includes(4)); // false;
|
|
544
|
+
```
|
|
545
|
+
|
|
546
|
+
#### Inherited from
|
|
547
|
+
|
|
548
|
+
[`Heap`](Heap.md).[`delete`](Heap.md#delete)
|
|
549
|
+
|
|
550
|
+
***
|
|
551
|
+
|
|
552
|
+
### deleteBy()
|
|
553
|
+
|
|
554
|
+
```ts
|
|
555
|
+
deleteBy(predicate): boolean;
|
|
556
|
+
```
|
|
557
|
+
|
|
558
|
+
Defined in: [data-structures/heap/heap.ts:807](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L807)
|
|
559
|
+
|
|
560
|
+
Delete the first element that matches a predicate.
|
|
561
|
+
|
|
562
|
+
#### Parameters
|
|
563
|
+
|
|
564
|
+
##### predicate
|
|
565
|
+
|
|
566
|
+
(`element`, `index`, `heap`) => `boolean`
|
|
567
|
+
|
|
568
|
+
Function (element, index, heap) → boolean.
|
|
569
|
+
|
|
570
|
+
#### Returns
|
|
571
|
+
|
|
572
|
+
`boolean`
|
|
573
|
+
|
|
574
|
+
True if an element was removed.
|
|
575
|
+
|
|
576
|
+
#### Remarks
|
|
577
|
+
|
|
578
|
+
Time O(N), Space O(1)
|
|
579
|
+
|
|
580
|
+
#### Inherited from
|
|
581
|
+
|
|
582
|
+
[`Heap`](Heap.md).[`deleteBy`](Heap.md#deleteby)
|
|
583
|
+
|
|
584
|
+
***
|
|
585
|
+
|
|
586
|
+
### dfs()
|
|
587
|
+
|
|
588
|
+
```ts
|
|
589
|
+
dfs(order?): E[];
|
|
590
|
+
```
|
|
591
|
+
|
|
592
|
+
Defined in: [data-structures/heap/heap.ts:878](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L878)
|
|
593
|
+
|
|
594
|
+
Traverse the binary heap as a complete binary tree and collect elements.
|
|
595
|
+
|
|
596
|
+
#### Parameters
|
|
597
|
+
|
|
598
|
+
##### order?
|
|
599
|
+
|
|
600
|
+
`DFSOrderPattern` = `'PRE'`
|
|
601
|
+
|
|
602
|
+
Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
603
|
+
|
|
604
|
+
#### Returns
|
|
605
|
+
|
|
606
|
+
`E`[]
|
|
607
|
+
|
|
608
|
+
Array of visited elements.
|
|
609
|
+
|
|
610
|
+
*
|
|
611
|
+
|
|
612
|
+
#### Remarks
|
|
613
|
+
|
|
614
|
+
Time O(N), Space O(H)
|
|
615
|
+
|
|
616
|
+
#### Example
|
|
617
|
+
|
|
618
|
+
```ts
|
|
619
|
+
// Depth-first traversal
|
|
620
|
+
const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
621
|
+
const result = heap.dfs('IN');
|
|
622
|
+
console.log(result.length); // 3;
|
|
623
|
+
```
|
|
624
|
+
|
|
625
|
+
#### Inherited from
|
|
626
|
+
|
|
627
|
+
[`Heap`](Heap.md).[`dfs`](Heap.md#dfs)
|
|
628
|
+
|
|
629
|
+
***
|
|
630
|
+
|
|
631
|
+
### every()
|
|
632
|
+
|
|
633
|
+
```ts
|
|
634
|
+
every(predicate, thisArg?): boolean;
|
|
635
|
+
```
|
|
636
|
+
|
|
637
|
+
Defined in: [data-structures/base/iterable-element-base.ts:87](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L87)
|
|
638
|
+
|
|
639
|
+
Tests whether all elements satisfy the predicate.
|
|
640
|
+
|
|
641
|
+
#### Parameters
|
|
642
|
+
|
|
643
|
+
##### predicate
|
|
644
|
+
|
|
645
|
+
`ElementCallback`\<`E`, `R`, `boolean`\>
|
|
646
|
+
|
|
647
|
+
Function invoked for each element with signature `(value, index, self)`.
|
|
648
|
+
|
|
649
|
+
##### thisArg?
|
|
650
|
+
|
|
651
|
+
`unknown`
|
|
652
|
+
|
|
653
|
+
Optional `this` binding for the predicate.
|
|
654
|
+
|
|
655
|
+
#### Returns
|
|
656
|
+
|
|
657
|
+
`boolean`
|
|
658
|
+
|
|
659
|
+
`true` if every element passes; otherwise `false`.
|
|
660
|
+
|
|
661
|
+
#### Remarks
|
|
662
|
+
|
|
663
|
+
Time O(n) in the worst case; may exit early when the first failure is found. Space O(1).
|
|
664
|
+
|
|
665
|
+
#### Inherited from
|
|
666
|
+
|
|
667
|
+
[`Heap`](Heap.md).[`every`](Heap.md#every)
|
|
668
|
+
|
|
669
|
+
***
|
|
670
|
+
|
|
671
|
+
### filter()
|
|
672
|
+
|
|
673
|
+
```ts
|
|
674
|
+
filter(callback, thisArg?): this;
|
|
675
|
+
```
|
|
676
|
+
|
|
677
|
+
Defined in: [data-structures/heap/heap.ts:1072](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L1072)
|
|
678
|
+
|
|
679
|
+
Filter elements into a new heap of the same class.
|
|
680
|
+
|
|
681
|
+
#### Parameters
|
|
682
|
+
|
|
683
|
+
##### callback
|
|
684
|
+
|
|
685
|
+
`ElementCallback`\<`E`, `R`, `boolean`\>
|
|
686
|
+
|
|
687
|
+
Predicate (element, index, heap) → boolean to keep element.
|
|
688
|
+
|
|
689
|
+
##### thisArg?
|
|
690
|
+
|
|
691
|
+
`unknown`
|
|
692
|
+
|
|
693
|
+
Value for `this` inside the callback.
|
|
694
|
+
|
|
695
|
+
#### Returns
|
|
696
|
+
|
|
697
|
+
`this`
|
|
698
|
+
|
|
699
|
+
A new heap with the kept elements.
|
|
700
|
+
|
|
701
|
+
*
|
|
702
|
+
|
|
703
|
+
#### Remarks
|
|
704
|
+
|
|
705
|
+
Time O(N log N), Space O(N)
|
|
706
|
+
|
|
707
|
+
#### Example
|
|
708
|
+
|
|
709
|
+
```ts
|
|
710
|
+
// Filter elements
|
|
711
|
+
const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
712
|
+
const evens = heap.filter(x => x % 2 === 0);
|
|
713
|
+
console.log(evens.size); // 2;
|
|
714
|
+
```
|
|
715
|
+
|
|
716
|
+
#### Inherited from
|
|
717
|
+
|
|
718
|
+
[`Heap`](Heap.md).[`filter`](Heap.md#filter)
|
|
719
|
+
|
|
720
|
+
***
|
|
721
|
+
|
|
722
|
+
### find()
|
|
723
|
+
|
|
724
|
+
#### Call Signature
|
|
725
|
+
|
|
726
|
+
```ts
|
|
727
|
+
find<S>(predicate, thisArg?): S | undefined;
|
|
728
|
+
```
|
|
729
|
+
|
|
730
|
+
Defined in: [data-structures/base/iterable-element-base.ts:163](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L163)
|
|
731
|
+
|
|
732
|
+
Finds the first element that satisfies the predicate and returns it.
|
|
733
|
+
|
|
734
|
+
Finds the first element of type `S` (a subtype of `E`) that satisfies the predicate and returns it.
|
|
735
|
+
|
|
736
|
+
##### Type Parameters
|
|
737
|
+
|
|
738
|
+
###### S
|
|
739
|
+
|
|
740
|
+
`S`
|
|
741
|
+
|
|
742
|
+
##### Parameters
|
|
743
|
+
|
|
744
|
+
###### predicate
|
|
745
|
+
|
|
746
|
+
`ElementCallback`\<`E`, `R`, `S`\>
|
|
747
|
+
|
|
748
|
+
Type-guard predicate: `(value, index, self) => value is S`.
|
|
749
|
+
|
|
750
|
+
###### thisArg?
|
|
751
|
+
|
|
752
|
+
`unknown`
|
|
753
|
+
|
|
754
|
+
Optional `this` binding for the predicate.
|
|
755
|
+
|
|
756
|
+
##### Returns
|
|
757
|
+
|
|
758
|
+
`S` \| `undefined`
|
|
759
|
+
|
|
760
|
+
The matched element typed as `S`, or `undefined` if not found.
|
|
761
|
+
|
|
762
|
+
##### Remarks
|
|
763
|
+
|
|
764
|
+
Time O(n) in the worst case; may exit early on the first match. Space O(1).
|
|
765
|
+
|
|
766
|
+
##### Inherited from
|
|
767
|
+
|
|
768
|
+
[`Heap`](Heap.md).[`find`](Heap.md#find)
|
|
769
|
+
|
|
770
|
+
#### Call Signature
|
|
771
|
+
|
|
772
|
+
```ts
|
|
773
|
+
find(predicate, thisArg?): E | undefined;
|
|
774
|
+
```
|
|
775
|
+
|
|
776
|
+
Defined in: [data-structures/base/iterable-element-base.ts:164](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L164)
|
|
777
|
+
|
|
778
|
+
Finds the first element that satisfies the predicate and returns it.
|
|
779
|
+
|
|
780
|
+
Finds the first element of type `S` (a subtype of `E`) that satisfies the predicate and returns it.
|
|
781
|
+
|
|
782
|
+
##### Parameters
|
|
783
|
+
|
|
784
|
+
###### predicate
|
|
785
|
+
|
|
786
|
+
`ElementCallback`\<`E`, `R`, `unknown`\>
|
|
787
|
+
|
|
788
|
+
Type-guard predicate: `(value, index, self) => value is S`.
|
|
789
|
+
|
|
790
|
+
###### thisArg?
|
|
791
|
+
|
|
792
|
+
`unknown`
|
|
793
|
+
|
|
794
|
+
Optional `this` binding for the predicate.
|
|
795
|
+
|
|
796
|
+
##### Returns
|
|
797
|
+
|
|
798
|
+
`E` \| `undefined`
|
|
799
|
+
|
|
800
|
+
The matched element typed as `S`, or `undefined` if not found.
|
|
801
|
+
|
|
802
|
+
##### Remarks
|
|
803
|
+
|
|
804
|
+
Time O(n) in the worst case; may exit early on the first match. Space O(1).
|
|
805
|
+
|
|
806
|
+
##### Inherited from
|
|
807
|
+
|
|
808
|
+
[`Heap`](Heap.md).[`find`](Heap.md#find)
|
|
809
|
+
|
|
810
|
+
***
|
|
811
|
+
|
|
812
|
+
### fix()
|
|
813
|
+
|
|
814
|
+
```ts
|
|
815
|
+
fix(): boolean[];
|
|
816
|
+
```
|
|
817
|
+
|
|
818
|
+
Defined in: [data-structures/heap/heap.ts:909](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L909)
|
|
819
|
+
|
|
820
|
+
Restore heap order bottom-up (heapify in-place).
|
|
821
|
+
|
|
822
|
+
#### Returns
|
|
823
|
+
|
|
824
|
+
`boolean`[]
|
|
825
|
+
|
|
826
|
+
Array of per-node results from fixing steps.
|
|
827
|
+
|
|
828
|
+
#### Remarks
|
|
829
|
+
|
|
830
|
+
Time O(N), Space O(1)
|
|
831
|
+
|
|
832
|
+
#### Inherited from
|
|
833
|
+
|
|
834
|
+
[`Heap`](Heap.md).[`fix`](Heap.md#fix)
|
|
835
|
+
|
|
836
|
+
***
|
|
837
|
+
|
|
838
|
+
### forEach()
|
|
839
|
+
|
|
840
|
+
```ts
|
|
841
|
+
forEach(callbackfn, thisArg?): void;
|
|
842
|
+
```
|
|
843
|
+
|
|
844
|
+
Defined in: [data-structures/base/iterable-element-base.ts:133](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L133)
|
|
845
|
+
|
|
846
|
+
Invokes a callback for each element in iteration order.
|
|
847
|
+
|
|
848
|
+
#### Parameters
|
|
849
|
+
|
|
850
|
+
##### callbackfn
|
|
851
|
+
|
|
852
|
+
`ElementCallback`\<`E`, `R`, `void`\>
|
|
853
|
+
|
|
854
|
+
Function invoked per element with signature `(value, index, self)`.
|
|
855
|
+
|
|
856
|
+
##### thisArg?
|
|
857
|
+
|
|
858
|
+
`unknown`
|
|
859
|
+
|
|
860
|
+
Optional `this` binding for the callback.
|
|
861
|
+
|
|
862
|
+
#### Returns
|
|
863
|
+
|
|
864
|
+
`void`
|
|
865
|
+
|
|
866
|
+
`void`.
|
|
867
|
+
|
|
868
|
+
#### Remarks
|
|
869
|
+
|
|
870
|
+
Time O(n), Space O(1).
|
|
871
|
+
|
|
872
|
+
#### Inherited from
|
|
873
|
+
|
|
874
|
+
[`Heap`](Heap.md).[`forEach`](Heap.md#foreach)
|
|
875
|
+
|
|
876
|
+
***
|
|
877
|
+
|
|
878
|
+
### has()
|
|
879
|
+
|
|
880
|
+
```ts
|
|
881
|
+
has(element): boolean;
|
|
882
|
+
```
|
|
883
|
+
|
|
884
|
+
Defined in: [data-structures/heap/heap.ts:730](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L730)
|
|
885
|
+
|
|
886
|
+
Check if an equal element exists in the heap.
|
|
887
|
+
|
|
888
|
+
#### Parameters
|
|
889
|
+
|
|
890
|
+
##### element
|
|
891
|
+
|
|
892
|
+
`E`
|
|
893
|
+
|
|
894
|
+
Element to search for.
|
|
895
|
+
|
|
896
|
+
#### Returns
|
|
897
|
+
|
|
898
|
+
`boolean`
|
|
899
|
+
|
|
900
|
+
True if found.
|
|
901
|
+
|
|
902
|
+
*
|
|
903
|
+
|
|
904
|
+
#### Remarks
|
|
905
|
+
|
|
906
|
+
Time O(N), Space O(1)
|
|
907
|
+
|
|
908
|
+
#### Example
|
|
909
|
+
|
|
910
|
+
```ts
|
|
911
|
+
// Check element existence
|
|
912
|
+
const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
913
|
+
console.log(heap.has(1)); // true;
|
|
914
|
+
console.log(heap.has(99)); // false;
|
|
915
|
+
```
|
|
916
|
+
|
|
917
|
+
#### Inherited from
|
|
918
|
+
|
|
919
|
+
[`Heap`](Heap.md).[`has`](Heap.md#has)
|
|
920
|
+
|
|
921
|
+
***
|
|
922
|
+
|
|
923
|
+
### isEmpty()
|
|
924
|
+
|
|
925
|
+
```ts
|
|
926
|
+
isEmpty(): boolean;
|
|
927
|
+
```
|
|
928
|
+
|
|
929
|
+
Defined in: [data-structures/heap/heap.ts:628](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L628)
|
|
930
|
+
|
|
931
|
+
Check whether the heap is empty.
|
|
932
|
+
|
|
933
|
+
#### Returns
|
|
934
|
+
|
|
935
|
+
`boolean`
|
|
936
|
+
|
|
937
|
+
True if size is 0.
|
|
938
|
+
|
|
939
|
+
*
|
|
940
|
+
|
|
941
|
+
#### Remarks
|
|
942
|
+
|
|
943
|
+
Time O(1), Space O(1)
|
|
944
|
+
|
|
945
|
+
#### Example
|
|
946
|
+
|
|
947
|
+
```ts
|
|
948
|
+
// Check if heap is empty
|
|
949
|
+
const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
950
|
+
console.log(heap.isEmpty()); // true;
|
|
951
|
+
heap.add(1);
|
|
952
|
+
console.log(heap.isEmpty()); // false;
|
|
953
|
+
```
|
|
954
|
+
|
|
955
|
+
#### Inherited from
|
|
956
|
+
|
|
957
|
+
[`Heap`](Heap.md).[`isEmpty`](Heap.md#isempty)
|
|
958
|
+
|
|
959
|
+
***
|
|
960
|
+
|
|
961
|
+
### map()
|
|
962
|
+
|
|
963
|
+
```ts
|
|
964
|
+
map<EM, RM>(
|
|
965
|
+
callback,
|
|
966
|
+
options,
|
|
967
|
+
thisArg?): Heap<EM, RM>;
|
|
968
|
+
```
|
|
969
|
+
|
|
970
|
+
Defined in: [data-structures/heap/heap.ts:1133](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L1133)
|
|
971
|
+
|
|
972
|
+
Map elements into a new heap of possibly different element type.
|
|
973
|
+
|
|
974
|
+
#### Type Parameters
|
|
975
|
+
|
|
976
|
+
##### EM
|
|
977
|
+
|
|
978
|
+
`EM`
|
|
979
|
+
|
|
980
|
+
##### RM
|
|
981
|
+
|
|
982
|
+
`RM`
|
|
983
|
+
|
|
984
|
+
#### Parameters
|
|
985
|
+
|
|
986
|
+
##### callback
|
|
987
|
+
|
|
988
|
+
`ElementCallback`\<`E`, `R`, `EM`\>
|
|
989
|
+
|
|
990
|
+
Mapping function (element, index, heap) → newElement.
|
|
991
|
+
|
|
992
|
+
##### options
|
|
993
|
+
|
|
994
|
+
`IterableElementBaseOptions`\<`EM`, `RM`\> & `object` & `object`
|
|
995
|
+
|
|
996
|
+
Options for the output heap, including comparator for EM.
|
|
997
|
+
|
|
998
|
+
##### thisArg?
|
|
999
|
+
|
|
1000
|
+
`unknown`
|
|
1001
|
+
|
|
1002
|
+
Value for `this` inside the callback.
|
|
1003
|
+
|
|
1004
|
+
#### Returns
|
|
1005
|
+
|
|
1006
|
+
[`Heap`](Heap.md)\<`EM`, `RM`\>
|
|
1007
|
+
|
|
1008
|
+
A new heap with mapped elements.
|
|
1009
|
+
|
|
1010
|
+
*
|
|
1011
|
+
|
|
1012
|
+
#### Remarks
|
|
1013
|
+
|
|
1014
|
+
Time O(N log N), Space O(N)
|
|
1015
|
+
|
|
1016
|
+
#### Example
|
|
1017
|
+
|
|
1018
|
+
```ts
|
|
1019
|
+
// Transform elements
|
|
1020
|
+
const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
1021
|
+
const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
1022
|
+
console.log(doubled.peek()); // 2;
|
|
1023
|
+
```
|
|
1024
|
+
|
|
1025
|
+
#### Inherited from
|
|
1026
|
+
|
|
1027
|
+
[`Heap`](Heap.md).[`map`](Heap.md#map)
|
|
1028
|
+
|
|
1029
|
+
***
|
|
1030
|
+
|
|
1031
|
+
### mapSame()
|
|
1032
|
+
|
|
1033
|
+
```ts
|
|
1034
|
+
mapSame(callback, thisArg?): this;
|
|
1035
|
+
```
|
|
1036
|
+
|
|
1037
|
+
Defined in: [data-structures/heap/heap.ts:1157](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L1157)
|
|
1038
|
+
|
|
1039
|
+
Map elements into a new heap of the same element type.
|
|
1040
|
+
|
|
1041
|
+
#### Parameters
|
|
1042
|
+
|
|
1043
|
+
##### callback
|
|
1044
|
+
|
|
1045
|
+
`ElementCallback`\<`E`, `R`, `E`\>
|
|
1046
|
+
|
|
1047
|
+
Mapping function (element, index, heap) → element.
|
|
1048
|
+
|
|
1049
|
+
##### thisArg?
|
|
1050
|
+
|
|
1051
|
+
`unknown`
|
|
1052
|
+
|
|
1053
|
+
Value for `this` inside the callback.
|
|
1054
|
+
|
|
1055
|
+
#### Returns
|
|
1056
|
+
|
|
1057
|
+
`this`
|
|
1058
|
+
|
|
1059
|
+
A new heap with mapped elements.
|
|
1060
|
+
|
|
1061
|
+
#### Remarks
|
|
1062
|
+
|
|
1063
|
+
Time O(N log N), Space O(N)
|
|
1064
|
+
|
|
1065
|
+
#### Inherited from
|
|
1066
|
+
|
|
1067
|
+
[`Heap`](Heap.md).[`mapSame`](Heap.md#mapsame)
|
|
1068
|
+
|
|
1069
|
+
***
|
|
1070
|
+
|
|
1071
|
+
### peek()
|
|
1072
|
+
|
|
1073
|
+
```ts
|
|
1074
|
+
peek(): E | undefined;
|
|
1075
|
+
```
|
|
1076
|
+
|
|
1077
|
+
Defined in: [data-structures/heap/heap.ts:579](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L579)
|
|
1078
|
+
|
|
1079
|
+
Get the current top element without removing it.
|
|
1080
|
+
|
|
1081
|
+
#### Returns
|
|
1082
|
+
|
|
1083
|
+
`E` \| `undefined`
|
|
1084
|
+
|
|
1085
|
+
Top element or undefined.
|
|
1086
|
+
|
|
1087
|
+
*
|
|
1088
|
+
|
|
1089
|
+
#### Remarks
|
|
1090
|
+
|
|
1091
|
+
Time O(1), Space O(1)
|
|
1092
|
+
|
|
1093
|
+
#### Example
|
|
1094
|
+
|
|
1095
|
+
```ts
|
|
1096
|
+
// Heap for event processing with priority
|
|
1097
|
+
interface Event {
|
|
1098
|
+
id: number;
|
|
1099
|
+
type: 'critical' | 'warning' | 'info';
|
|
1100
|
+
timestamp: number;
|
|
1101
|
+
message: string;
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
// Custom priority: critical > warning > info
|
|
1105
|
+
const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
1106
|
+
|
|
1107
|
+
const eventHeap = new Heap<Event>([], {
|
|
1108
|
+
comparator: (a: Event, b: Event) => {
|
|
1109
|
+
const priorityA = priorityMap[a.type];
|
|
1110
|
+
const priorityB = priorityMap[b.type];
|
|
1111
|
+
return priorityB - priorityA; // Higher priority first
|
|
1112
|
+
}
|
|
1113
|
+
});
|
|
1114
|
+
|
|
1115
|
+
// Add events in random order
|
|
1116
|
+
eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
1117
|
+
eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
1118
|
+
eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
1119
|
+
eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
1120
|
+
eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
1121
|
+
|
|
1122
|
+
console.log(eventHeap.size); // 5;
|
|
1123
|
+
|
|
1124
|
+
// Process events by priority (critical first)
|
|
1125
|
+
const processedOrder: Event[] = [];
|
|
1126
|
+
while (eventHeap.size > 0) {
|
|
1127
|
+
const event = eventHeap.poll();
|
|
1128
|
+
if (event) {
|
|
1129
|
+
processedOrder.push(event);
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
// Verify critical events came first
|
|
1134
|
+
console.log(processedOrder[0].type); // 'critical';
|
|
1135
|
+
console.log(processedOrder[1].type); // 'critical';
|
|
1136
|
+
console.log(processedOrder[2].type); // 'warning';
|
|
1137
|
+
console.log(processedOrder[3].type); // 'info';
|
|
1138
|
+
console.log(processedOrder[4].type); // 'info';
|
|
1139
|
+
|
|
1140
|
+
// Verify O(log n) operations
|
|
1141
|
+
const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
1142
|
+
|
|
1143
|
+
// Add - O(log n)
|
|
1144
|
+
newHeap.add(2);
|
|
1145
|
+
console.log(newHeap.size); // 5;
|
|
1146
|
+
|
|
1147
|
+
// Poll - O(log n)
|
|
1148
|
+
const removed = newHeap.poll();
|
|
1149
|
+
console.log(removed); // 1;
|
|
1150
|
+
|
|
1151
|
+
// Peek - O(1)
|
|
1152
|
+
const top = newHeap.peek();
|
|
1153
|
+
console.log(top); // 2;
|
|
1154
|
+
```
|
|
1155
|
+
|
|
1156
|
+
#### Inherited from
|
|
1157
|
+
|
|
1158
|
+
[`Heap`](Heap.md).[`peek`](Heap.md#peek)
|
|
1159
|
+
|
|
1160
|
+
***
|
|
1161
|
+
|
|
1162
|
+
### poll()
|
|
1163
|
+
|
|
1164
|
+
```ts
|
|
1165
|
+
poll(): E | undefined;
|
|
1166
|
+
```
|
|
1167
|
+
|
|
1168
|
+
Defined in: [data-structures/heap/heap.ts:468](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L468)
|
|
1169
|
+
|
|
1170
|
+
Remove and return the top element.
|
|
1171
|
+
|
|
1172
|
+
#### Returns
|
|
1173
|
+
|
|
1174
|
+
`E` \| `undefined`
|
|
1175
|
+
|
|
1176
|
+
Top element or undefined.
|
|
1177
|
+
|
|
1178
|
+
*
|
|
1179
|
+
|
|
1180
|
+
#### Remarks
|
|
1181
|
+
|
|
1182
|
+
Time O(log N), Space O(1)
|
|
1183
|
+
|
|
1184
|
+
#### Example
|
|
1185
|
+
|
|
1186
|
+
```ts
|
|
1187
|
+
// Heap with custom comparator (MaxHeap behavior)
|
|
1188
|
+
interface Task {
|
|
1189
|
+
id: number;
|
|
1190
|
+
priority: number;
|
|
1191
|
+
name: string;
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
// Custom comparator for max heap behavior (higher priority first)
|
|
1195
|
+
const tasks: Task[] = [
|
|
1196
|
+
{ id: 1, priority: 5, name: 'Email' },
|
|
1197
|
+
{ id: 2, priority: 3, name: 'Chat' },
|
|
1198
|
+
{ id: 3, priority: 8, name: 'Alert' }
|
|
1199
|
+
];
|
|
1200
|
+
|
|
1201
|
+
const maxHeap = new Heap(tasks, {
|
|
1202
|
+
comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
1203
|
+
});
|
|
1204
|
+
|
|
1205
|
+
console.log(maxHeap.size); // 3;
|
|
1206
|
+
|
|
1207
|
+
// Peek returns highest priority task
|
|
1208
|
+
const topTask = maxHeap.peek();
|
|
1209
|
+
console.log(topTask?.priority); // 8;
|
|
1210
|
+
console.log(topTask?.name); // 'Alert';
|
|
1211
|
+
```
|
|
1212
|
+
|
|
1213
|
+
#### Inherited from
|
|
1214
|
+
|
|
1215
|
+
[`Heap`](Heap.md).[`poll`](Heap.md#poll)
|
|
1216
|
+
|
|
1217
|
+
***
|
|
1218
|
+
|
|
1219
|
+
### print()
|
|
1220
|
+
|
|
1221
|
+
```ts
|
|
1222
|
+
print(): void;
|
|
1223
|
+
```
|
|
1224
|
+
|
|
1225
|
+
Defined in: [data-structures/base/iterable-element-base.ts:269](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L269)
|
|
1226
|
+
|
|
1227
|
+
Prints `toVisual()` to the console. Intended for quick debugging.
|
|
1228
|
+
|
|
1229
|
+
#### Returns
|
|
1230
|
+
|
|
1231
|
+
`void`
|
|
1232
|
+
|
|
1233
|
+
`void`.
|
|
1234
|
+
|
|
1235
|
+
#### Remarks
|
|
1236
|
+
|
|
1237
|
+
Time O(n) due to materialization, Space O(n) for the intermediate representation.
|
|
1238
|
+
|
|
1239
|
+
#### Inherited from
|
|
1240
|
+
|
|
1241
|
+
[`Heap`](Heap.md).[`print`](Heap.md#print)
|
|
1242
|
+
|
|
1243
|
+
***
|
|
1244
|
+
|
|
1245
|
+
### reduce()
|
|
1246
|
+
|
|
1247
|
+
Reduces all elements to a single accumulated value.
|
|
1248
|
+
|
|
1249
|
+
#### Param
|
|
1250
|
+
|
|
1251
|
+
Reducer of signature `(acc, value, index, self) => nextAcc`. The first element is used as the initial accumulator.
|
|
1252
|
+
|
|
1253
|
+
#### Param
|
|
1254
|
+
|
|
1255
|
+
Reducer of signature `(acc, value, index, self) => nextAcc`.
|
|
1256
|
+
|
|
1257
|
+
#### Param
|
|
1258
|
+
|
|
1259
|
+
The initial accumulator value of type `E`.
|
|
1260
|
+
|
|
1261
|
+
#### Template
|
|
1262
|
+
|
|
1263
|
+
The accumulator type when it differs from `E`.
|
|
1264
|
+
|
|
1265
|
+
#### Param
|
|
1266
|
+
|
|
1267
|
+
Reducer of signature `(acc: U, value, index, self) => U`.
|
|
1268
|
+
|
|
1269
|
+
#### Param
|
|
1270
|
+
|
|
1271
|
+
The initial accumulator value of type `U`.
|
|
1272
|
+
|
|
1273
|
+
#### Remarks
|
|
1274
|
+
|
|
1275
|
+
Time O(n), Space O(1). Throws if called on an empty structure without `initialValue`.
|
|
1276
|
+
|
|
1277
|
+
#### Call Signature
|
|
1278
|
+
|
|
1279
|
+
```ts
|
|
1280
|
+
reduce(callbackfn): E;
|
|
1281
|
+
```
|
|
1282
|
+
|
|
1283
|
+
Defined in: [data-structures/base/iterable-element-base.ts:194](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L194)
|
|
1284
|
+
|
|
1285
|
+
##### Parameters
|
|
1286
|
+
|
|
1287
|
+
###### callbackfn
|
|
1288
|
+
|
|
1289
|
+
`ReduceElementCallback`\<`E`, `R`\>
|
|
1290
|
+
|
|
1291
|
+
##### Returns
|
|
1292
|
+
|
|
1293
|
+
`E`
|
|
1294
|
+
|
|
1295
|
+
##### Inherited from
|
|
1296
|
+
|
|
1297
|
+
[`Heap`](Heap.md).[`reduce`](Heap.md#reduce)
|
|
1298
|
+
|
|
1299
|
+
#### Call Signature
|
|
1300
|
+
|
|
1301
|
+
```ts
|
|
1302
|
+
reduce(callbackfn, initialValue): E;
|
|
1303
|
+
```
|
|
1304
|
+
|
|
1305
|
+
Defined in: [data-structures/base/iterable-element-base.ts:195](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L195)
|
|
1306
|
+
|
|
1307
|
+
##### Parameters
|
|
1308
|
+
|
|
1309
|
+
###### callbackfn
|
|
1310
|
+
|
|
1311
|
+
`ReduceElementCallback`\<`E`, `R`\>
|
|
1312
|
+
|
|
1313
|
+
###### initialValue
|
|
1314
|
+
|
|
1315
|
+
`E`
|
|
1316
|
+
|
|
1317
|
+
##### Returns
|
|
1318
|
+
|
|
1319
|
+
`E`
|
|
1320
|
+
|
|
1321
|
+
##### Inherited from
|
|
1322
|
+
|
|
1323
|
+
[`Heap`](Heap.md).[`reduce`](Heap.md#reduce)
|
|
1324
|
+
|
|
1325
|
+
#### Call Signature
|
|
1326
|
+
|
|
1327
|
+
```ts
|
|
1328
|
+
reduce<U>(callbackfn, initialValue): U;
|
|
1329
|
+
```
|
|
1330
|
+
|
|
1331
|
+
Defined in: [data-structures/base/iterable-element-base.ts:196](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L196)
|
|
1332
|
+
|
|
1333
|
+
##### Type Parameters
|
|
1334
|
+
|
|
1335
|
+
###### U
|
|
1336
|
+
|
|
1337
|
+
`U`
|
|
1338
|
+
|
|
1339
|
+
##### Parameters
|
|
1340
|
+
|
|
1341
|
+
###### callbackfn
|
|
1342
|
+
|
|
1343
|
+
`ReduceElementCallback`\<`E`, `R`, `U`\>
|
|
1344
|
+
|
|
1345
|
+
###### initialValue
|
|
1346
|
+
|
|
1347
|
+
`U`
|
|
1348
|
+
|
|
1349
|
+
##### Returns
|
|
1350
|
+
|
|
1351
|
+
`U`
|
|
1352
|
+
|
|
1353
|
+
##### Inherited from
|
|
1354
|
+
|
|
1355
|
+
[`Heap`](Heap.md).[`reduce`](Heap.md#reduce)
|
|
1356
|
+
|
|
1357
|
+
***
|
|
1358
|
+
|
|
1359
|
+
### refill()
|
|
1360
|
+
|
|
1361
|
+
```ts
|
|
1362
|
+
refill(elements): boolean[];
|
|
1363
|
+
```
|
|
1364
|
+
|
|
1365
|
+
Defined in: [data-structures/heap/heap.ts:687](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L687)
|
|
1366
|
+
|
|
1367
|
+
Replace the backing array and rebuild the heap.
|
|
1368
|
+
|
|
1369
|
+
#### Parameters
|
|
1370
|
+
|
|
1371
|
+
##### elements
|
|
1372
|
+
|
|
1373
|
+
`Iterable`\<`E`\>
|
|
1374
|
+
|
|
1375
|
+
Iterable used to refill the heap.
|
|
1376
|
+
|
|
1377
|
+
#### Returns
|
|
1378
|
+
|
|
1379
|
+
`boolean`[]
|
|
1380
|
+
|
|
1381
|
+
Array of per-node results from fixing steps.
|
|
1382
|
+
|
|
1383
|
+
#### Remarks
|
|
1384
|
+
|
|
1385
|
+
Time O(N), Space O(N)
|
|
1386
|
+
|
|
1387
|
+
#### Inherited from
|
|
1388
|
+
|
|
1389
|
+
[`Heap`](Heap.md).[`refill`](Heap.md#refill)
|
|
1390
|
+
|
|
1391
|
+
***
|
|
1392
|
+
|
|
1393
|
+
### setEquality()
|
|
1394
|
+
|
|
1395
|
+
```ts
|
|
1396
|
+
setEquality(equals): this;
|
|
1397
|
+
```
|
|
1398
|
+
|
|
1399
|
+
Defined in: [data-structures/heap/heap.ts:835](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L835)
|
|
1400
|
+
|
|
1401
|
+
Set the equality comparator used by has/delete operations.
|
|
1402
|
+
|
|
1403
|
+
#### Parameters
|
|
1404
|
+
|
|
1405
|
+
##### equals
|
|
1406
|
+
|
|
1407
|
+
(`a`, `b`) => `boolean`
|
|
1408
|
+
|
|
1409
|
+
Equality predicate (a, b) → boolean.
|
|
1410
|
+
|
|
1411
|
+
#### Returns
|
|
1412
|
+
|
|
1413
|
+
`this`
|
|
1414
|
+
|
|
1415
|
+
This heap.
|
|
1416
|
+
|
|
1417
|
+
#### Remarks
|
|
1418
|
+
|
|
1419
|
+
Time O(1), Space O(1)
|
|
1420
|
+
|
|
1421
|
+
#### Inherited from
|
|
1422
|
+
|
|
1423
|
+
[`Heap`](Heap.md).[`setEquality`](Heap.md#setequality)
|
|
1424
|
+
|
|
1425
|
+
***
|
|
1426
|
+
|
|
1427
|
+
### some()
|
|
1428
|
+
|
|
1429
|
+
```ts
|
|
1430
|
+
some(predicate, thisArg?): boolean;
|
|
1431
|
+
```
|
|
1432
|
+
|
|
1433
|
+
Defined in: [data-structures/base/iterable-element-base.ts:110](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L110)
|
|
1434
|
+
|
|
1435
|
+
Tests whether at least one element satisfies the predicate.
|
|
1436
|
+
|
|
1437
|
+
#### Parameters
|
|
1438
|
+
|
|
1439
|
+
##### predicate
|
|
1440
|
+
|
|
1441
|
+
`ElementCallback`\<`E`, `R`, `boolean`\>
|
|
1442
|
+
|
|
1443
|
+
Function invoked for each element with signature `(value, index, self)`.
|
|
1444
|
+
|
|
1445
|
+
##### thisArg?
|
|
1446
|
+
|
|
1447
|
+
`unknown`
|
|
1448
|
+
|
|
1449
|
+
Optional `this` binding for the predicate.
|
|
1450
|
+
|
|
1451
|
+
#### Returns
|
|
1452
|
+
|
|
1453
|
+
`boolean`
|
|
1454
|
+
|
|
1455
|
+
`true` if any element passes; otherwise `false`.
|
|
1456
|
+
|
|
1457
|
+
#### Remarks
|
|
1458
|
+
|
|
1459
|
+
Time O(n) in the worst case; may exit early on first success. Space O(1).
|
|
1460
|
+
|
|
1461
|
+
#### Inherited from
|
|
1462
|
+
|
|
1463
|
+
[`Heap`](Heap.md).[`some`](Heap.md#some)
|
|
1464
|
+
|
|
1465
|
+
***
|
|
1466
|
+
|
|
1467
|
+
### sort()
|
|
1468
|
+
|
|
1469
|
+
```ts
|
|
1470
|
+
sort(): E[];
|
|
1471
|
+
```
|
|
1472
|
+
|
|
1473
|
+
Defined in: [data-structures/heap/heap.ts:963](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L963)
|
|
1474
|
+
|
|
1475
|
+
Return all elements in ascending order by repeatedly polling.
|
|
1476
|
+
|
|
1477
|
+
#### Returns
|
|
1478
|
+
|
|
1479
|
+
`E`[]
|
|
1480
|
+
|
|
1481
|
+
Sorted array of elements.
|
|
1482
|
+
|
|
1483
|
+
*
|
|
1484
|
+
|
|
1485
|
+
#### Remarks
|
|
1486
|
+
|
|
1487
|
+
Time O(N log N), Space O(N)
|
|
1488
|
+
|
|
1489
|
+
#### Example
|
|
1490
|
+
|
|
1491
|
+
```ts
|
|
1492
|
+
// Sort elements using heap
|
|
1493
|
+
const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
1494
|
+
const sorted = heap.sort();
|
|
1495
|
+
console.log(sorted); // [1, 2, 3, 4, 5];
|
|
1496
|
+
```
|
|
1497
|
+
|
|
1498
|
+
#### Inherited from
|
|
1499
|
+
|
|
1500
|
+
[`Heap`](Heap.md).[`sort`](Heap.md#sort)
|
|
1501
|
+
|
|
1502
|
+
***
|
|
1503
|
+
|
|
1504
|
+
### toArray()
|
|
1505
|
+
|
|
1506
|
+
```ts
|
|
1507
|
+
toArray(): E[];
|
|
1508
|
+
```
|
|
1509
|
+
|
|
1510
|
+
Defined in: [data-structures/base/iterable-element-base.ts:246](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L246)
|
|
1511
|
+
|
|
1512
|
+
Materializes the elements into a new array.
|
|
1513
|
+
|
|
1514
|
+
#### Returns
|
|
1515
|
+
|
|
1516
|
+
`E`[]
|
|
1517
|
+
|
|
1518
|
+
A shallow array copy of the iteration order.
|
|
1519
|
+
|
|
1520
|
+
#### Remarks
|
|
1521
|
+
|
|
1522
|
+
Time O(n), Space O(n).
|
|
1523
|
+
|
|
1524
|
+
#### Inherited from
|
|
1525
|
+
|
|
1526
|
+
[`Heap`](Heap.md).[`toArray`](Heap.md#toarray)
|
|
1527
|
+
|
|
1528
|
+
***
|
|
1529
|
+
|
|
1530
|
+
### toVisual()
|
|
1531
|
+
|
|
1532
|
+
```ts
|
|
1533
|
+
toVisual(): E[];
|
|
1534
|
+
```
|
|
1535
|
+
|
|
1536
|
+
Defined in: [data-structures/base/iterable-element-base.ts:258](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L258)
|
|
1537
|
+
|
|
1538
|
+
Returns a representation of the structure suitable for quick visualization.
|
|
1539
|
+
Defaults to an array of elements; subclasses may override to provide richer visuals.
|
|
1540
|
+
|
|
1541
|
+
#### Returns
|
|
1542
|
+
|
|
1543
|
+
`E`[]
|
|
1544
|
+
|
|
1545
|
+
A visual representation (array by default).
|
|
1546
|
+
|
|
1547
|
+
#### Remarks
|
|
1548
|
+
|
|
1549
|
+
Time O(n), Space O(n).
|
|
1550
|
+
|
|
1551
|
+
#### Inherited from
|
|
1552
|
+
|
|
1553
|
+
[`Heap`](Heap.md).[`toVisual`](Heap.md#tovisual)
|
|
1554
|
+
|
|
1555
|
+
***
|
|
1556
|
+
|
|
1557
|
+
### values()
|
|
1558
|
+
|
|
1559
|
+
```ts
|
|
1560
|
+
values(): IterableIterator<E>;
|
|
1561
|
+
```
|
|
1562
|
+
|
|
1563
|
+
Defined in: [data-structures/base/iterable-element-base.ts:72](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L72)
|
|
1564
|
+
|
|
1565
|
+
Returns an iterator over the values (alias of the default iterator).
|
|
1566
|
+
|
|
1567
|
+
#### Returns
|
|
1568
|
+
|
|
1569
|
+
`IterableIterator`\<`E`\>
|
|
1570
|
+
|
|
1571
|
+
An `IterableIterator<E>` over all elements.
|
|
1572
|
+
|
|
1573
|
+
#### Remarks
|
|
1574
|
+
|
|
1575
|
+
Creating the iterator is O(1); full iteration is Time O(n), Space O(1).
|
|
1576
|
+
|
|
1577
|
+
#### Inherited from
|
|
1578
|
+
|
|
1579
|
+
[`Heap`](Heap.md).[`values`](Heap.md#values)
|
|
1580
|
+
|
|
1581
|
+
***
|
|
1582
|
+
|
|
1583
|
+
### from()
|
|
1584
|
+
|
|
1585
|
+
```ts
|
|
1586
|
+
static from<T, R, S>(
|
|
1587
|
+
this,
|
|
1588
|
+
elements?,
|
|
1589
|
+
options?): S;
|
|
1590
|
+
```
|
|
1591
|
+
|
|
1592
|
+
Defined in: [data-structures/heap/heap.ts:259](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L259)
|
|
1593
|
+
|
|
1594
|
+
Create a heap of the same class from an iterable.
|
|
1595
|
+
|
|
1596
|
+
#### Type Parameters
|
|
1597
|
+
|
|
1598
|
+
##### T
|
|
1599
|
+
|
|
1600
|
+
`T`
|
|
1601
|
+
|
|
1602
|
+
##### R
|
|
1603
|
+
|
|
1604
|
+
`R` = `any`
|
|
1605
|
+
|
|
1606
|
+
##### S
|
|
1607
|
+
|
|
1608
|
+
`S` *extends* [`Heap`](Heap.md)\<`T`, `R`\> = [`Heap`](Heap.md)\<`T`, `R`\>
|
|
1609
|
+
|
|
1610
|
+
#### Parameters
|
|
1611
|
+
|
|
1612
|
+
##### this
|
|
1613
|
+
|
|
1614
|
+
`Object`
|
|
1615
|
+
|
|
1616
|
+
##### elements?
|
|
1617
|
+
|
|
1618
|
+
`Iterable`\<`T` \| `R`, `any`, `any`\>
|
|
1619
|
+
|
|
1620
|
+
Iterable of elements or raw records.
|
|
1621
|
+
|
|
1622
|
+
##### options?
|
|
1623
|
+
|
|
1624
|
+
`HeapOptions`\<`T`, `R`\>
|
|
1625
|
+
|
|
1626
|
+
Heap options including comparator.
|
|
1627
|
+
|
|
1628
|
+
#### Returns
|
|
1629
|
+
|
|
1630
|
+
`S`
|
|
1631
|
+
|
|
1632
|
+
A new heap instance of this class.
|
|
1633
|
+
|
|
1634
|
+
#### Remarks
|
|
1635
|
+
|
|
1636
|
+
Time O(N), Space O(N)
|
|
1637
|
+
|
|
1638
|
+
#### Inherited from
|
|
1639
|
+
|
|
1640
|
+
[`Heap`](Heap.md).[`from`](Heap.md#from)
|
|
1641
|
+
|
|
1642
|
+
***
|
|
1643
|
+
|
|
1644
|
+
### heapify()
|
|
1645
|
+
|
|
1646
|
+
```ts
|
|
1647
|
+
static heapify<EE, RR>(elements, options): Heap<EE, RR>;
|
|
1648
|
+
```
|
|
1649
|
+
|
|
1650
|
+
Defined in: [data-structures/heap/heap.ts:277](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L277)
|
|
1651
|
+
|
|
1652
|
+
Build a Heap from an iterable in linear time given a comparator.
|
|
1653
|
+
|
|
1654
|
+
#### Type Parameters
|
|
1655
|
+
|
|
1656
|
+
##### EE
|
|
1657
|
+
|
|
1658
|
+
`EE` = `any`
|
|
1659
|
+
|
|
1660
|
+
##### RR
|
|
1661
|
+
|
|
1662
|
+
`RR` = `any`
|
|
1663
|
+
|
|
1664
|
+
#### Parameters
|
|
1665
|
+
|
|
1666
|
+
##### elements
|
|
1667
|
+
|
|
1668
|
+
`Iterable`\<`EE`\>
|
|
1669
|
+
|
|
1670
|
+
Iterable of elements.
|
|
1671
|
+
|
|
1672
|
+
##### options
|
|
1673
|
+
|
|
1674
|
+
`HeapOptions`\<`EE`, `RR`\>
|
|
1675
|
+
|
|
1676
|
+
Heap options including comparator.
|
|
1677
|
+
|
|
1678
|
+
#### Returns
|
|
1679
|
+
|
|
1680
|
+
[`Heap`](Heap.md)\<`EE`, `RR`\>
|
|
1681
|
+
|
|
1682
|
+
A new Heap built from elements.
|
|
1683
|
+
|
|
1684
|
+
#### Remarks
|
|
1685
|
+
|
|
1686
|
+
Time O(N), Space O(N)
|
|
1687
|
+
|
|
1688
|
+
#### Inherited from
|
|
1689
|
+
|
|
1690
|
+
[`Heap`](Heap.md).[`heapify`](Heap.md#heapify)
|
|
1691
|
+
|
|
1692
|
+
|
|
1693
|
+
---
|
|
1694
|
+
|
|
1695
|
+
## Protected Members
|
|
1696
|
+
|
|
1697
|
+
### \_toElementFn?
|
|
1698
|
+
|
|
1699
|
+
```ts
|
|
1700
|
+
protected optional _toElementFn?: (rawElement) => E;
|
|
1701
|
+
```
|
|
1702
|
+
|
|
1703
|
+
Defined in: [data-structures/base/iterable-element-base.ts:39](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L39)
|
|
1704
|
+
|
|
1705
|
+
The converter used to transform a raw element (`R`) into a public element (`E`).
|
|
1706
|
+
|
|
1707
|
+
#### Parameters
|
|
1708
|
+
|
|
1709
|
+
##### rawElement
|
|
1710
|
+
|
|
1711
|
+
`R`
|
|
1712
|
+
|
|
1713
|
+
#### Returns
|
|
1714
|
+
|
|
1715
|
+
`E`
|
|
1716
|
+
|
|
1717
|
+
#### Remarks
|
|
1718
|
+
|
|
1719
|
+
Time O(1), Space O(1).
|
|
1720
|
+
|
|
1721
|
+
#### Inherited from
|
|
1722
|
+
|
|
1723
|
+
[`Heap`](Heap.md).[`_toElementFn`](Heap.md#_toelementfn)
|
|
1724
|
+
|
|
1725
|
+
## Accessors
|
|
1726
|
+
|
|
1727
|
+
### \_createInstance()
|
|
1728
|
+
|
|
1729
|
+
```ts
|
|
1730
|
+
protected _createInstance(options?): this;
|
|
1731
|
+
```
|
|
1732
|
+
|
|
1733
|
+
Defined in: [data-structures/heap/heap.ts:1230](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L1230)
|
|
1734
|
+
|
|
1735
|
+
(Protected) Create an empty instance of the same concrete class.
|
|
1736
|
+
|
|
1737
|
+
#### Parameters
|
|
1738
|
+
|
|
1739
|
+
##### options?
|
|
1740
|
+
|
|
1741
|
+
`HeapOptions`\<`E`, `R`\>
|
|
1742
|
+
|
|
1743
|
+
Options to override comparator or toElementFn.
|
|
1744
|
+
|
|
1745
|
+
#### Returns
|
|
1746
|
+
|
|
1747
|
+
`this`
|
|
1748
|
+
|
|
1749
|
+
A like-kind empty heap instance.
|
|
1750
|
+
|
|
1751
|
+
#### Remarks
|
|
1752
|
+
|
|
1753
|
+
Time O(1), Space O(1)
|
|
1754
|
+
|
|
1755
|
+
#### Inherited from
|
|
1756
|
+
|
|
1757
|
+
[`Heap`](Heap.md).[`_createInstance`](Heap.md#_createinstance)
|
|
1758
|
+
|
|
1759
|
+
***
|
|
1760
|
+
|
|
1761
|
+
### \_createLike()
|
|
1762
|
+
|
|
1763
|
+
```ts
|
|
1764
|
+
protected _createLike<EM, RM>(elements?, options?): Heap<EM, RM>;
|
|
1765
|
+
```
|
|
1766
|
+
|
|
1767
|
+
Defined in: [data-structures/heap/heap.ts:1248](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L1248)
|
|
1768
|
+
|
|
1769
|
+
(Protected) Create a like-kind instance seeded by elements.
|
|
1770
|
+
|
|
1771
|
+
#### Type Parameters
|
|
1772
|
+
|
|
1773
|
+
##### EM
|
|
1774
|
+
|
|
1775
|
+
`EM`
|
|
1776
|
+
|
|
1777
|
+
##### RM
|
|
1778
|
+
|
|
1779
|
+
`RM`
|
|
1780
|
+
|
|
1781
|
+
#### Parameters
|
|
1782
|
+
|
|
1783
|
+
##### elements?
|
|
1784
|
+
|
|
1785
|
+
`Iterable`\<`EM`, `any`, `any`\> \| `Iterable`\<`RM`, `any`, `any`\>
|
|
1786
|
+
|
|
1787
|
+
Iterable of elements or raw values to seed.
|
|
1788
|
+
|
|
1789
|
+
##### options?
|
|
1790
|
+
|
|
1791
|
+
`HeapOptions`\<`EM`, `RM`\>
|
|
1792
|
+
|
|
1793
|
+
Options forwarded to the constructor.
|
|
1794
|
+
|
|
1795
|
+
#### Returns
|
|
1796
|
+
|
|
1797
|
+
[`Heap`](Heap.md)\<`EM`, `RM`\>
|
|
1798
|
+
|
|
1799
|
+
A like-kind heap instance.
|
|
1800
|
+
|
|
1801
|
+
#### Remarks
|
|
1802
|
+
|
|
1803
|
+
Time O(N log N), Space O(N)
|
|
1804
|
+
|
|
1805
|
+
#### Inherited from
|
|
1806
|
+
|
|
1807
|
+
[`Heap`](Heap.md).[`_createLike`](Heap.md#_createlike)
|
|
1808
|
+
|
|
1809
|
+
***
|
|
1810
|
+
|
|
1811
|
+
### \_getIterator()
|
|
1812
|
+
|
|
1813
|
+
```ts
|
|
1814
|
+
protected _getIterator(): IterableIterator<E>;
|
|
1815
|
+
```
|
|
1816
|
+
|
|
1817
|
+
Defined in: [data-structures/heap/heap.ts:1188](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L1188)
|
|
1818
|
+
|
|
1819
|
+
Internal iterator factory used by the default iterator.
|
|
1820
|
+
|
|
1821
|
+
#### Returns
|
|
1822
|
+
|
|
1823
|
+
`IterableIterator`\<`E`\>
|
|
1824
|
+
|
|
1825
|
+
An iterator over elements.
|
|
1826
|
+
|
|
1827
|
+
#### Remarks
|
|
1828
|
+
|
|
1829
|
+
Implementations should yield in O(1) per element with O(1) extra space when possible.
|
|
1830
|
+
|
|
1831
|
+
#### Inherited from
|
|
1832
|
+
|
|
1833
|
+
[`Heap`](Heap.md).[`_getIterator`](Heap.md#_getiterator)
|
|
1834
|
+
|
|
1835
|
+
***
|
|
1836
|
+
|
|
1837
|
+
### \_spawnLike()
|
|
1838
|
+
|
|
1839
|
+
```ts
|
|
1840
|
+
protected _spawnLike<EM, RM>(options?): Heap<EM, RM>;
|
|
1841
|
+
```
|
|
1842
|
+
|
|
1843
|
+
Defined in: [data-structures/heap/heap.ts:1268](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L1268)
|
|
1844
|
+
|
|
1845
|
+
(Protected) Spawn an empty like-kind heap instance.
|
|
1846
|
+
|
|
1847
|
+
#### Type Parameters
|
|
1848
|
+
|
|
1849
|
+
##### EM
|
|
1850
|
+
|
|
1851
|
+
`EM`
|
|
1852
|
+
|
|
1853
|
+
##### RM
|
|
1854
|
+
|
|
1855
|
+
`RM`
|
|
1856
|
+
|
|
1857
|
+
#### Parameters
|
|
1858
|
+
|
|
1859
|
+
##### options?
|
|
1860
|
+
|
|
1861
|
+
`HeapOptions`\<`EM`, `RM`\>
|
|
1862
|
+
|
|
1863
|
+
Options forwarded to the constructor.
|
|
1864
|
+
|
|
1865
|
+
#### Returns
|
|
1866
|
+
|
|
1867
|
+
[`Heap`](Heap.md)\<`EM`, `RM`\>
|
|
1868
|
+
|
|
1869
|
+
An empty like-kind heap instance.
|
|
1870
|
+
|
|
1871
|
+
#### Remarks
|
|
1872
|
+
|
|
1873
|
+
Time O(1), Space O(1)
|
|
1874
|
+
|
|
1875
|
+
#### Inherited from
|
|
1876
|
+
|
|
1877
|
+
[`Heap`](Heap.md).[`_spawnLike`](Heap.md#_spawnlike)
|
|
1878
|
+
|
|
1879
|
+
***
|