data-structure-typed 2.5.3 → 2.6.1
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/.github/workflows/ci.yml +7 -2
- package/.github/workflows/release-package.yml +9 -2
- package/.husky/pre-commit +3 -0
- package/CHANGELOG.md +1 -1
- package/MIGRATION.md +48 -0
- package/README.md +20 -2
- package/README_CN.md +20 -2
- package/SPECIFICATION.md +24 -0
- package/SPECIFICATION.zh-CN.md +24 -0
- package/dist/cjs/binary-tree.cjs +1897 -19
- package/dist/cjs/graph.cjs +174 -0
- package/dist/cjs/hash.cjs +33 -0
- package/dist/cjs/heap.cjs +71 -0
- package/dist/cjs/index.cjs +2383 -3
- package/dist/cjs/linked-list.cjs +224 -2
- package/dist/cjs/matrix.cjs +24 -0
- package/dist/cjs/priority-queue.cjs +71 -0
- package/dist/cjs/queue.cjs +221 -1
- package/dist/cjs/stack.cjs +59 -0
- package/dist/cjs/trie.cjs +62 -0
- package/dist/cjs-legacy/binary-tree.cjs +1897 -19
- package/dist/cjs-legacy/graph.cjs +174 -0
- package/dist/cjs-legacy/hash.cjs +33 -0
- package/dist/cjs-legacy/heap.cjs +71 -0
- package/dist/cjs-legacy/index.cjs +2383 -3
- package/dist/cjs-legacy/linked-list.cjs +224 -2
- package/dist/cjs-legacy/matrix.cjs +24 -0
- package/dist/cjs-legacy/priority-queue.cjs +71 -0
- package/dist/cjs-legacy/queue.cjs +221 -1
- package/dist/cjs-legacy/stack.cjs +59 -0
- package/dist/cjs-legacy/trie.cjs +62 -0
- package/dist/esm/binary-tree.mjs +1897 -19
- package/dist/esm/graph.mjs +174 -0
- package/dist/esm/hash.mjs +33 -0
- package/dist/esm/heap.mjs +71 -0
- package/dist/esm/index.mjs +2383 -3
- package/dist/esm/linked-list.mjs +224 -2
- package/dist/esm/matrix.mjs +24 -0
- package/dist/esm/priority-queue.mjs +71 -0
- package/dist/esm/queue.mjs +221 -1
- package/dist/esm/stack.mjs +59 -0
- package/dist/esm/trie.mjs +62 -0
- package/dist/esm-legacy/binary-tree.mjs +1897 -19
- package/dist/esm-legacy/graph.mjs +174 -0
- package/dist/esm-legacy/hash.mjs +33 -0
- package/dist/esm-legacy/heap.mjs +71 -0
- package/dist/esm-legacy/index.mjs +2383 -3
- package/dist/esm-legacy/linked-list.mjs +224 -2
- package/dist/esm-legacy/matrix.mjs +24 -0
- package/dist/esm-legacy/priority-queue.mjs +71 -0
- package/dist/esm-legacy/queue.mjs +221 -1
- package/dist/esm-legacy/stack.mjs +59 -0
- package/dist/esm-legacy/trie.mjs +62 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +17 -0
- package/dist/types/data-structures/base/linear-base.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +36 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +42 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +75 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +72 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +57 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +18 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +375 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +389 -0
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +330 -0
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +438 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +30 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +27 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +33 -0
- package/dist/types/data-structures/heap/heap.d.ts +42 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +75 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +45 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +54 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +24 -0
- package/dist/types/data-structures/queue/deque.d.ts +90 -1
- package/dist/types/data-structures/queue/queue.d.ts +36 -0
- package/dist/types/data-structures/stack/stack.d.ts +30 -0
- package/dist/types/data-structures/trie/trie.d.ts +36 -0
- package/dist/umd/data-structure-typed.js +2383 -3
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +108 -108
- package/docs-site-docusaurus/docs/api/classes/BST.md +101 -101
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +66 -66
- package/docs-site-docusaurus/docs/api/classes/Deque.md +235 -51
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +21 -21
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +231 -67
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +9 -9
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +14 -14
- package/docs-site-docusaurus/docs/api/classes/Heap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +83 -13
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +124 -20
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +140 -32
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +30 -26
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +159 -51
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +20 -20
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +23 -23
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/Queue.md +142 -34
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +117 -117
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +158 -50
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +21 -21
- package/docs-site-docusaurus/docs/api/classes/Stack.md +108 -26
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +33 -33
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +75 -39
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +301 -39
- package/docs-site-docusaurus/docs/api/classes/Trie.md +110 -28
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +20 -20
- package/jest.integration.config.js +1 -2
- package/package.json +51 -50
- package/src/common/error.ts +15 -32
- package/src/common/index.ts +0 -3
- package/src/data-structures/base/iterable-element-base.ts +32 -3
- package/src/data-structures/base/linear-base.ts +13 -36
- package/src/data-structures/binary-tree/avl-tree.ts +31 -493
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +47 -530
- package/src/data-structures/binary-tree/binary-tree.ts +326 -1236
- package/src/data-structures/binary-tree/bst.ts +158 -1010
- package/src/data-structures/binary-tree/red-black-tree.ts +451 -1233
- package/src/data-structures/binary-tree/segment-tree.ts +73 -333
- package/src/data-structures/binary-tree/tree-map.ts +462 -4749
- package/src/data-structures/binary-tree/tree-multi-map.ts +310 -4530
- package/src/data-structures/binary-tree/tree-multi-set.ts +300 -3652
- package/src/data-structures/binary-tree/tree-set.ts +437 -4443
- package/src/data-structures/graph/abstract-graph.ts +98 -167
- package/src/data-structures/graph/directed-graph.ts +137 -532
- package/src/data-structures/graph/map-graph.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +132 -484
- package/src/data-structures/hash/hash-map.ts +154 -549
- package/src/data-structures/heap/heap.ts +200 -753
- package/src/data-structures/linked-list/doubly-linked-list.ts +153 -809
- package/src/data-structures/linked-list/singly-linked-list.ts +122 -749
- package/src/data-structures/linked-list/skip-linked-list.ts +211 -864
- package/src/data-structures/matrix/matrix.ts +179 -494
- package/src/data-structures/matrix/navigator.ts +0 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -6
- package/src/data-structures/priority-queue/min-priority-queue.ts +6 -11
- package/src/data-structures/priority-queue/priority-queue.ts +1 -2
- package/src/data-structures/queue/deque.ts +241 -807
- package/src/data-structures/queue/queue.ts +102 -589
- package/src/data-structures/stack/stack.ts +76 -475
- package/src/data-structures/trie/trie.ts +98 -592
- package/src/types/common.ts +0 -10
- package/src/types/data-structures/binary-tree/bst.ts +0 -7
- package/src/types/data-structures/binary-tree/red-black-tree.ts +0 -1
- package/src/types/data-structures/graph/abstract-graph.ts +0 -2
- package/src/types/data-structures/hash/hash-map.ts +0 -3
- package/src/types/data-structures/hash/index.ts +0 -1
- package/src/types/data-structures/matrix/navigator.ts +0 -2
- package/src/types/utils/utils.ts +0 -7
- package/src/types/utils/validate-type.ts +0 -7
- package/src/utils/number.ts +0 -2
- package/src/utils/utils.ts +0 -5
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
|
|
9
8
|
import type { Comparator, DFSOrderPattern, ElementCallback, HeapOptions } from '../../types';
|
|
10
9
|
import { IterableElementBase } from '../base';
|
|
11
10
|
import { ERR, raise } from '../../common';
|
|
@@ -157,15 +156,12 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
157
156
|
* @param [options] - Options such as comparator and toElementFn.
|
|
158
157
|
* @returns New Heap instance.
|
|
159
158
|
*/
|
|
160
|
-
|
|
161
159
|
constructor(elements: Iterable<E | R> = [], options?: HeapOptions<E, R>) {
|
|
162
160
|
super(options);
|
|
163
|
-
|
|
164
161
|
if (options) {
|
|
165
162
|
const { comparator } = options;
|
|
166
163
|
if (comparator) this._comparator = comparator;
|
|
167
164
|
}
|
|
168
|
-
|
|
169
165
|
this.addMany(elements as Iterable<E | R>);
|
|
170
166
|
}
|
|
171
167
|
|
|
@@ -176,7 +172,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
176
172
|
* @remarks Time O(1), Space O(1)
|
|
177
173
|
* @returns Internal elements array.
|
|
178
174
|
*/
|
|
179
|
-
|
|
180
175
|
get elements(): E[] {
|
|
181
176
|
return this._elements;
|
|
182
177
|
}
|
|
@@ -185,56 +180,16 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
185
180
|
* Get the number of elements.
|
|
186
181
|
* @remarks Time O(1), Space O(1)
|
|
187
182
|
* @returns Heap size.
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
* @example
|
|
228
|
-
* // Track heap capacity
|
|
229
|
-
* const heap = new Heap<number>();
|
|
230
|
-
* console.log(heap.size); // 0;
|
|
231
|
-
* heap.add(10);
|
|
232
|
-
* heap.add(20);
|
|
233
|
-
* console.log(heap.size); // 2;
|
|
234
|
-
* heap.poll();
|
|
235
|
-
* console.log(heap.size); // 1;
|
|
183
|
+
* @example
|
|
184
|
+
* // Track heap capacity
|
|
185
|
+
* const heap = new Heap<number>();
|
|
186
|
+
* console.log(heap.size); // 0;
|
|
187
|
+
* heap.add(10);
|
|
188
|
+
* heap.add(20);
|
|
189
|
+
* console.log(heap.size); // 2;
|
|
190
|
+
* heap.poll();
|
|
191
|
+
* console.log(heap.size); // 1;
|
|
236
192
|
*/
|
|
237
|
-
|
|
238
193
|
get size(): number {
|
|
239
194
|
return this.elements.length;
|
|
240
195
|
}
|
|
@@ -244,11 +199,19 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
244
199
|
* @remarks Time O(1), Space O(1)
|
|
245
200
|
* @returns Last element or undefined.
|
|
246
201
|
*/
|
|
247
|
-
|
|
248
202
|
get leaf(): E | undefined {
|
|
249
203
|
return this.elements[this.size - 1] ?? undefined;
|
|
250
204
|
}
|
|
251
205
|
|
|
206
|
+
/**
|
|
207
|
+
* Get the comparator used to order elements.
|
|
208
|
+
* @remarks Time O(1), Space O(1)
|
|
209
|
+
* @returns Comparator function.
|
|
210
|
+
*/
|
|
211
|
+
get comparator() {
|
|
212
|
+
return this._comparator;
|
|
213
|
+
}
|
|
214
|
+
|
|
252
215
|
/**
|
|
253
216
|
* Create a heap of the same class from an iterable.
|
|
254
217
|
* @remarks Time O(N), Space O(N)
|
|
@@ -259,7 +222,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
259
222
|
* @param [options] - Heap options including comparator.
|
|
260
223
|
* @returns A new heap instance of this class.
|
|
261
224
|
*/
|
|
262
|
-
|
|
263
225
|
static from<T, R = any, S extends Heap<T, R> = Heap<T, R>>(
|
|
264
226
|
this: new (elements?: Iterable<T | R>, options?: HeapOptions<T, R>) => S,
|
|
265
227
|
elements?: Iterable<T | R>,
|
|
@@ -277,7 +239,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
277
239
|
* @param options - Heap options including comparator.
|
|
278
240
|
* @returns A new Heap built from elements.
|
|
279
241
|
*/
|
|
280
|
-
|
|
281
242
|
static heapify<EE = any, RR = any>(elements: Iterable<EE>, options: HeapOptions<EE, RR>): Heap<EE, RR> {
|
|
282
243
|
return new Heap<EE, RR>(elements, options);
|
|
283
244
|
}
|
|
@@ -287,62 +248,22 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
287
248
|
* @remarks Time O(log N) amortized, Space O(1)
|
|
288
249
|
* @param element - Element to insert.
|
|
289
250
|
* @returns True.
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
* @example
|
|
330
|
-
* // basic Heap creation and add operation
|
|
331
|
-
* // Create a min heap (default)
|
|
332
|
-
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
333
|
-
*
|
|
334
|
-
* // Verify size
|
|
335
|
-
* console.log(minHeap.size); // 6;
|
|
336
|
-
*
|
|
337
|
-
* // Add new element
|
|
338
|
-
* minHeap.add(4);
|
|
339
|
-
* console.log(minHeap.size); // 7;
|
|
340
|
-
*
|
|
341
|
-
* // Min heap property: smallest element at root
|
|
342
|
-
* const min = minHeap.peek();
|
|
343
|
-
* console.log(min); // 1;
|
|
251
|
+
* @example
|
|
252
|
+
* // basic Heap creation and add operation
|
|
253
|
+
* // Create a min heap (default)
|
|
254
|
+
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
255
|
+
*
|
|
256
|
+
* // Verify size
|
|
257
|
+
* console.log(minHeap.size); // 6;
|
|
258
|
+
*
|
|
259
|
+
* // Add new element
|
|
260
|
+
* minHeap.add(4);
|
|
261
|
+
* console.log(minHeap.size); // 7;
|
|
262
|
+
*
|
|
263
|
+
* // Min heap property: smallest element at root
|
|
264
|
+
* const min = minHeap.peek();
|
|
265
|
+
* console.log(min); // 1;
|
|
344
266
|
*/
|
|
345
|
-
|
|
346
267
|
add(element: E): boolean {
|
|
347
268
|
this._elements.push(element);
|
|
348
269
|
return this._bubbleUp(this.elements.length - 1);
|
|
@@ -353,50 +274,13 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
353
274
|
* @remarks Time O(N log N), Space O(1)
|
|
354
275
|
* @param elements - Iterable of elements or raw values.
|
|
355
276
|
* @returns Array of per-element success flags.
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
* @example
|
|
393
|
-
* // Add multiple elements
|
|
394
|
-
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
395
|
-
* heap.addMany([5, 3, 7, 1]);
|
|
396
|
-
* console.log(heap.peek()); // 1;
|
|
397
|
-
* console.log(heap.size); // 4;
|
|
277
|
+
* @example
|
|
278
|
+
* // Add multiple elements
|
|
279
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
280
|
+
* heap.addMany([5, 3, 7, 1]);
|
|
281
|
+
* console.log(heap.peek()); // 1;
|
|
282
|
+
* console.log(heap.size); // 4;
|
|
398
283
|
*/
|
|
399
|
-
|
|
400
284
|
addMany(elements: Iterable<E | R>): boolean[] {
|
|
401
285
|
const flags: boolean[] = [];
|
|
402
286
|
for (const el of elements) {
|
|
@@ -415,98 +299,59 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
415
299
|
* Remove and return the top element.
|
|
416
300
|
* @remarks Time O(log N), Space O(1)
|
|
417
301
|
* @returns Top element or undefined.
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
* @example
|
|
457
|
-
* // Heap with custom comparator (MaxHeap behavior)
|
|
458
|
-
* interface Task {
|
|
459
|
-
* id: number;
|
|
460
|
-
* priority: number;
|
|
461
|
-
* name: string;
|
|
462
|
-
* }
|
|
463
|
-
*
|
|
464
|
-
* // Custom comparator for max heap behavior (higher priority first)
|
|
465
|
-
* const tasks: Task[] = [
|
|
466
|
-
* { id: 1, priority: 5, name: 'Email' },
|
|
467
|
-
* { id: 2, priority: 3, name: 'Chat' },
|
|
468
|
-
* { id: 3, priority: 8, name: 'Alert' }
|
|
469
|
-
* ];
|
|
470
|
-
*
|
|
471
|
-
* const maxHeap = new Heap(tasks, {
|
|
472
|
-
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
473
|
-
* });
|
|
474
|
-
*
|
|
475
|
-
* console.log(maxHeap.size); // 3;
|
|
476
|
-
*
|
|
477
|
-
* // Peek returns highest priority task
|
|
478
|
-
* const topTask = maxHeap.peek();
|
|
479
|
-
* console.log(topTask?.priority); // 8;
|
|
480
|
-
* console.log(topTask?.name); // 'Alert';
|
|
302
|
+
* @example
|
|
303
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
304
|
+
* interface Task {
|
|
305
|
+
* id: number;
|
|
306
|
+
* priority: number;
|
|
307
|
+
* name: string;
|
|
308
|
+
* }
|
|
309
|
+
*
|
|
310
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
311
|
+
* const tasks: Task[] = [
|
|
312
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
313
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
314
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
315
|
+
* ];
|
|
316
|
+
*
|
|
317
|
+
* const maxHeap = new Heap(tasks, {
|
|
318
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
319
|
+
* });
|
|
320
|
+
*
|
|
321
|
+
* console.log(maxHeap.size); // 3;
|
|
322
|
+
*
|
|
323
|
+
* // Peek returns highest priority task
|
|
324
|
+
* const topTask = maxHeap.peek();
|
|
325
|
+
* console.log(topTask?.priority); // 8;
|
|
326
|
+
* console.log(topTask?.name); // 'Alert';
|
|
481
327
|
*/
|
|
482
|
-
|
|
483
328
|
/**
|
|
484
329
|
* @deprecated Use `pop` instead. Will be removed in a future major version.
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
330
|
+
* @example
|
|
331
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
332
|
+
* interface Task {
|
|
333
|
+
* id: number;
|
|
334
|
+
* priority: number;
|
|
335
|
+
* name: string;
|
|
336
|
+
* }
|
|
337
|
+
*
|
|
338
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
339
|
+
* const tasks: Task[] = [
|
|
340
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
341
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
342
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
343
|
+
* ];
|
|
344
|
+
*
|
|
345
|
+
* const maxHeap = new Heap(tasks, {
|
|
346
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
347
|
+
* });
|
|
348
|
+
*
|
|
349
|
+
* console.log(maxHeap.size); // 3;
|
|
350
|
+
*
|
|
351
|
+
* // Peek returns highest priority task
|
|
352
|
+
* const topTask = maxHeap.peek();
|
|
353
|
+
* console.log(topTask?.priority); // 8;
|
|
354
|
+
* console.log(topTask?.name); // 'Alert';
|
|
510
355
|
*/
|
|
511
356
|
poll(): E | undefined {
|
|
512
357
|
return this.pop();
|
|
@@ -532,106 +377,66 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
532
377
|
* Get the current top element without removing it.
|
|
533
378
|
* @remarks Time O(1), Space O(1)
|
|
534
379
|
* @returns Top element or undefined.
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
* // Add events in random order
|
|
595
|
-
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
596
|
-
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
597
|
-
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
598
|
-
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
599
|
-
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
600
|
-
*
|
|
601
|
-
* console.log(eventHeap.size); // 5;
|
|
602
|
-
*
|
|
603
|
-
* // Process events by priority (critical first)
|
|
604
|
-
* const processedOrder: Event[] = [];
|
|
605
|
-
* while (eventHeap.size > 0) {
|
|
606
|
-
* const event = eventHeap.poll();
|
|
607
|
-
* if (event) {
|
|
608
|
-
* processedOrder.push(event);
|
|
609
|
-
* }
|
|
610
|
-
* }
|
|
611
|
-
*
|
|
612
|
-
* // Verify critical events came first
|
|
613
|
-
* console.log(processedOrder[0].type); // 'critical';
|
|
614
|
-
* console.log(processedOrder[1].type); // 'critical';
|
|
615
|
-
* console.log(processedOrder[2].type); // 'warning';
|
|
616
|
-
* console.log(processedOrder[3].type); // 'info';
|
|
617
|
-
* console.log(processedOrder[4].type); // 'info';
|
|
618
|
-
*
|
|
619
|
-
* // Verify O(log n) operations
|
|
620
|
-
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
621
|
-
*
|
|
622
|
-
* // Add - O(log n)
|
|
623
|
-
* newHeap.add(2);
|
|
624
|
-
* console.log(newHeap.size); // 5;
|
|
625
|
-
*
|
|
626
|
-
* // Poll - O(log n)
|
|
627
|
-
* const removed = newHeap.poll();
|
|
628
|
-
* console.log(removed); // 1;
|
|
629
|
-
*
|
|
630
|
-
* // Peek - O(1)
|
|
631
|
-
* const top = newHeap.peek();
|
|
632
|
-
* console.log(top); // 2;
|
|
380
|
+
* @example
|
|
381
|
+
* // Heap for event processing with priority
|
|
382
|
+
* interface Event {
|
|
383
|
+
* id: number;
|
|
384
|
+
* type: 'critical' | 'warning' | 'info';
|
|
385
|
+
* timestamp: number;
|
|
386
|
+
* message: string;
|
|
387
|
+
* }
|
|
388
|
+
*
|
|
389
|
+
* // Custom priority: critical > warning > info
|
|
390
|
+
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
391
|
+
*
|
|
392
|
+
* const eventHeap = new Heap<Event>([], {
|
|
393
|
+
* comparator: (a: Event, b: Event) => {
|
|
394
|
+
* const priorityA = priorityMap[a.type];
|
|
395
|
+
* const priorityB = priorityMap[b.type];
|
|
396
|
+
* return priorityB - priorityA; // Higher priority first
|
|
397
|
+
* }
|
|
398
|
+
* });
|
|
399
|
+
*
|
|
400
|
+
* // Add events in random order
|
|
401
|
+
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
402
|
+
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
403
|
+
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
404
|
+
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
405
|
+
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
406
|
+
*
|
|
407
|
+
* console.log(eventHeap.size); // 5;
|
|
408
|
+
*
|
|
409
|
+
* // Process events by priority (critical first)
|
|
410
|
+
* const processedOrder: Event[] = [];
|
|
411
|
+
* while (eventHeap.size > 0) {
|
|
412
|
+
* const event = eventHeap.poll();
|
|
413
|
+
* if (event) {
|
|
414
|
+
* processedOrder.push(event);
|
|
415
|
+
* }
|
|
416
|
+
* }
|
|
417
|
+
*
|
|
418
|
+
* // Verify critical events came first
|
|
419
|
+
* console.log(processedOrder[0].type); // 'critical';
|
|
420
|
+
* console.log(processedOrder[1].type); // 'critical';
|
|
421
|
+
* console.log(processedOrder[2].type); // 'warning';
|
|
422
|
+
* console.log(processedOrder[3].type); // 'info';
|
|
423
|
+
* console.log(processedOrder[4].type); // 'info';
|
|
424
|
+
*
|
|
425
|
+
* // Verify O(log n) operations
|
|
426
|
+
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
427
|
+
*
|
|
428
|
+
* // Add - O(log n)
|
|
429
|
+
* newHeap.add(2);
|
|
430
|
+
* console.log(newHeap.size); // 5;
|
|
431
|
+
*
|
|
432
|
+
* // Poll - O(log n)
|
|
433
|
+
* const removed = newHeap.poll();
|
|
434
|
+
* console.log(removed); // 1;
|
|
435
|
+
*
|
|
436
|
+
* // Peek - O(1)
|
|
437
|
+
* const top = newHeap.peek();
|
|
438
|
+
* console.log(top); // 2;
|
|
633
439
|
*/
|
|
634
|
-
|
|
635
440
|
peek(): E | undefined {
|
|
636
441
|
return this.elements[0];
|
|
637
442
|
}
|
|
@@ -640,51 +445,13 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
640
445
|
* Check whether the heap is empty.
|
|
641
446
|
* @remarks Time O(1), Space O(1)
|
|
642
447
|
* @returns True if size is 0.
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
* @example
|
|
681
|
-
* // Check if heap is empty
|
|
682
|
-
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
683
|
-
* console.log(heap.isEmpty()); // true;
|
|
684
|
-
* heap.add(1);
|
|
685
|
-
* console.log(heap.isEmpty()); // false;
|
|
448
|
+
* @example
|
|
449
|
+
* // Check if heap is empty
|
|
450
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
451
|
+
* console.log(heap.isEmpty()); // true;
|
|
452
|
+
* heap.add(1);
|
|
453
|
+
* console.log(heap.isEmpty()); // false;
|
|
686
454
|
*/
|
|
687
|
-
|
|
688
455
|
isEmpty(): boolean {
|
|
689
456
|
return this.size === 0;
|
|
690
457
|
}
|
|
@@ -693,98 +460,27 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
693
460
|
* Remove all elements.
|
|
694
461
|
* @remarks Time O(1), Space O(1)
|
|
695
462
|
* @returns void
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
* @example
|
|
734
|
-
* // Remove all elements
|
|
735
|
-
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
736
|
-
* heap.clear();
|
|
737
|
-
* console.log(heap.isEmpty()); // true;
|
|
463
|
+
* @example
|
|
464
|
+
* // Remove all elements
|
|
465
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
466
|
+
* heap.clear();
|
|
467
|
+
* console.log(heap.isEmpty()); // true;
|
|
738
468
|
*/
|
|
739
|
-
|
|
740
469
|
clear(): void {
|
|
741
470
|
this._elements = [];
|
|
742
471
|
}
|
|
743
472
|
|
|
744
|
-
|
|
745
|
-
|
|
746
473
|
/**
|
|
747
474
|
* Check if an equal element exists in the heap.
|
|
748
475
|
* @remarks Time O(N), Space O(1)
|
|
749
476
|
* @param element - Element to search for.
|
|
750
477
|
* @returns True if found.
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
* @example
|
|
782
|
-
* // Check element existence
|
|
783
|
-
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
784
|
-
* console.log(heap.has(1)); // true;
|
|
785
|
-
* console.log(heap.has(99)); // false;
|
|
478
|
+
* @example
|
|
479
|
+
* // Check element existence
|
|
480
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
481
|
+
* console.log(heap.has(1)); // true;
|
|
482
|
+
* console.log(heap.has(99)); // false;
|
|
786
483
|
*/
|
|
787
|
-
|
|
788
484
|
override has(element: E): boolean {
|
|
789
485
|
for (const el of this.elements) if (this._equals(el, element)) return true;
|
|
790
486
|
return false;
|
|
@@ -795,49 +491,12 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
795
491
|
* @remarks Time O(N), Space O(1)
|
|
796
492
|
* @param element - Element to delete.
|
|
797
493
|
* @returns True if an element was removed.
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
* @example
|
|
835
|
-
* // Remove specific element
|
|
836
|
-
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
837
|
-
* heap.delete(4);
|
|
838
|
-
* console.log(heap.toArray().includes(4)); // false;
|
|
494
|
+
* @example
|
|
495
|
+
* // Remove specific element
|
|
496
|
+
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
497
|
+
* heap.delete(4);
|
|
498
|
+
* console.log(heap.toArray().includes(4)); // false;
|
|
839
499
|
*/
|
|
840
|
-
|
|
841
500
|
delete(element: E): boolean {
|
|
842
501
|
let index = -1;
|
|
843
502
|
for (let i = 0; i < this.elements.length; i++) {
|
|
@@ -899,7 +558,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
899
558
|
* @param equals - Equality predicate (a, b) → boolean.
|
|
900
559
|
* @returns This heap.
|
|
901
560
|
*/
|
|
902
|
-
|
|
903
561
|
setEquality(equals: (a: E, b: E) => boolean): this {
|
|
904
562
|
this._equals = equals;
|
|
905
563
|
return this;
|
|
@@ -910,43 +568,12 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
910
568
|
* @remarks Time O(N), Space O(H)
|
|
911
569
|
* @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
912
570
|
* @returns Array of visited elements.
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
* @example
|
|
944
|
-
* // Depth-first traversal
|
|
945
|
-
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
946
|
-
* const result = heap.dfs('IN');
|
|
947
|
-
* console.log(result.length); // 3;
|
|
571
|
+
* @example
|
|
572
|
+
* // Depth-first traversal
|
|
573
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
574
|
+
* const result = heap.dfs('IN');
|
|
575
|
+
* console.log(result.length); // 3;
|
|
948
576
|
*/
|
|
949
|
-
|
|
950
577
|
dfs(order: DFSOrderPattern = 'PRE'): E[] {
|
|
951
578
|
const result: E[] = [];
|
|
952
579
|
const _dfs = (index: number) => {
|
|
@@ -977,7 +604,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
977
604
|
* @remarks Time O(N), Space O(1)
|
|
978
605
|
* @returns Array of per-node results from fixing steps.
|
|
979
606
|
*/
|
|
980
|
-
|
|
981
607
|
fix(): boolean[] {
|
|
982
608
|
const results: boolean[] = [];
|
|
983
609
|
for (let i = Math.floor(this.size / 2) - 1; i >= 0; i--) {
|
|
@@ -990,52 +616,12 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
990
616
|
* Return all elements in ascending order by repeatedly polling.
|
|
991
617
|
* @remarks Time O(N log N), Space O(N)
|
|
992
618
|
* @returns Sorted array of elements.
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
* @example
|
|
1033
|
-
* // Sort elements using heap
|
|
1034
|
-
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
1035
|
-
* const sorted = heap.sort();
|
|
1036
|
-
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
619
|
+
* @example
|
|
620
|
+
* // Sort elements using heap
|
|
621
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
622
|
+
* const sorted = heap.sort();
|
|
623
|
+
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
1037
624
|
*/
|
|
1038
|
-
|
|
1039
625
|
sort(): E[] {
|
|
1040
626
|
const visited: E[] = [];
|
|
1041
627
|
const cloned = this._createInstance();
|
|
@@ -1051,52 +637,14 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
1051
637
|
* Deep clone this heap.
|
|
1052
638
|
* @remarks Time O(N), Space O(N)
|
|
1053
639
|
* @returns A new heap with the same elements.
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
* @example
|
|
1092
|
-
* // Create independent copy
|
|
1093
|
-
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
1094
|
-
* const copy = heap.clone();
|
|
1095
|
-
* copy.poll();
|
|
1096
|
-
* console.log(heap.size); // 3;
|
|
1097
|
-
* console.log(copy.size); // 2;
|
|
640
|
+
* @example
|
|
641
|
+
* // Create independent copy
|
|
642
|
+
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
643
|
+
* const copy = heap.clone();
|
|
644
|
+
* copy.poll();
|
|
645
|
+
* console.log(heap.size); // 3;
|
|
646
|
+
* console.log(copy.size); // 2;
|
|
1098
647
|
*/
|
|
1099
|
-
|
|
1100
648
|
clone(): this {
|
|
1101
649
|
const next = this._createInstance();
|
|
1102
650
|
for (const x of this.elements) next.add(x);
|
|
@@ -1109,50 +657,12 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
1109
657
|
* @param callback - Predicate (element, index, heap) → boolean to keep element.
|
|
1110
658
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
1111
659
|
* @returns A new heap with the kept elements.
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
* @example
|
|
1150
|
-
* // Filter elements
|
|
1151
|
-
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
1152
|
-
* const evens = heap.filter(x => x % 2 === 0);
|
|
1153
|
-
* console.log(evens.size); // 2;
|
|
660
|
+
* @example
|
|
661
|
+
* // Filter elements
|
|
662
|
+
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
663
|
+
* const evens = heap.filter(x => x % 2 === 0);
|
|
664
|
+
* console.log(evens.size); // 2;
|
|
1154
665
|
*/
|
|
1155
|
-
|
|
1156
666
|
filter(callback: ElementCallback<E, R, boolean>, thisArg?: unknown): this {
|
|
1157
667
|
const out = this._createInstance();
|
|
1158
668
|
let i = 0;
|
|
@@ -1175,49 +685,12 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
1175
685
|
* @param options - Options for the output heap, including comparator for EM.
|
|
1176
686
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
1177
687
|
* @returns A new heap with mapped elements.
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
* @example
|
|
1215
|
-
* // Transform elements
|
|
1216
|
-
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
1217
|
-
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
1218
|
-
* console.log(doubled.peek()); // 2;
|
|
688
|
+
* @example
|
|
689
|
+
* // Transform elements
|
|
690
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
691
|
+
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
692
|
+
* console.log(doubled.peek()); // 2;
|
|
1219
693
|
*/
|
|
1220
|
-
|
|
1221
694
|
map<EM, RM>(
|
|
1222
695
|
callback: ElementCallback<E, R, EM>,
|
|
1223
696
|
options: HeapOptions<EM, RM> & { comparator: Comparator<EM> },
|
|
@@ -1241,7 +714,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
1241
714
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
1242
715
|
* @returns A new heap with mapped elements.
|
|
1243
716
|
*/
|
|
1244
|
-
|
|
1245
717
|
mapSame(callback: ElementCallback<E, R, E>, thisArg?: unknown): this {
|
|
1246
718
|
const out = this._createInstance();
|
|
1247
719
|
let i = 0;
|
|
@@ -1263,16 +735,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
1263
735
|
|
|
1264
736
|
protected readonly _comparator: Comparator<E> = this._DEFAULT_COMPARATOR;
|
|
1265
737
|
|
|
1266
|
-
/**
|
|
1267
|
-
* Get the comparator used to order elements.
|
|
1268
|
-
* @remarks Time O(1), Space O(1)
|
|
1269
|
-
* @returns Comparator function.
|
|
1270
|
-
*/
|
|
1271
|
-
|
|
1272
|
-
get comparator() {
|
|
1273
|
-
return this._comparator;
|
|
1274
|
-
}
|
|
1275
|
-
|
|
1276
738
|
protected *_getIterator(): IterableIterator<E> {
|
|
1277
739
|
for (const element of this.elements) yield element;
|
|
1278
740
|
}
|
|
@@ -1314,12 +776,8 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
1314
776
|
* @param [options] - Options to override comparator or toElementFn.
|
|
1315
777
|
* @returns A like-kind empty heap instance.
|
|
1316
778
|
*/
|
|
1317
|
-
|
|
1318
779
|
protected _createInstance(options?: HeapOptions<E, R>): this {
|
|
1319
|
-
const Ctor = this.constructor as new (
|
|
1320
|
-
elements?: Iterable<E> | Iterable<R>,
|
|
1321
|
-
options?: HeapOptions<E, R>
|
|
1322
|
-
) => this;
|
|
780
|
+
const Ctor = this.constructor as new (elements?: Iterable<E> | Iterable<R>, options?: HeapOptions<E, R>) => this;
|
|
1323
781
|
return new Ctor([], { comparator: this.comparator, toElementFn: this.toElementFn, ...(options ?? {}) });
|
|
1324
782
|
}
|
|
1325
783
|
|
|
@@ -1332,7 +790,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
1332
790
|
* @param [options] - Options forwarded to the constructor.
|
|
1333
791
|
* @returns A like-kind heap instance.
|
|
1334
792
|
*/
|
|
1335
|
-
|
|
1336
793
|
protected _createLike<EM, RM>(
|
|
1337
794
|
elements: Iterable<EM> | Iterable<RM> = [],
|
|
1338
795
|
options?: HeapOptions<EM, RM>
|
|
@@ -1352,7 +809,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
1352
809
|
* @param [options] - Options forwarded to the constructor.
|
|
1353
810
|
* @returns An empty like-kind heap instance.
|
|
1354
811
|
*/
|
|
1355
|
-
|
|
1356
812
|
protected _spawnLike<EM, RM>(options?: HeapOptions<EM, RM>): Heap<EM, RM> {
|
|
1357
813
|
return this._createLike<EM, RM>([], options);
|
|
1358
814
|
}
|
|
@@ -1386,13 +842,14 @@ export class FibonacciHeapNode<E> {
|
|
|
1386
842
|
* @example examples will be generated by unit test
|
|
1387
843
|
*/
|
|
1388
844
|
export class FibonacciHeap<E> {
|
|
845
|
+
protected readonly _comparator: Comparator<E>;
|
|
846
|
+
|
|
1389
847
|
/**
|
|
1390
848
|
* Create a FibonacciHeap.
|
|
1391
849
|
* @remarks Time O(1), Space O(1)
|
|
1392
850
|
* @param [comparator] - Comparator to order elements (min-heap by default).
|
|
1393
851
|
* @returns New FibonacciHeap instance.
|
|
1394
852
|
*/
|
|
1395
|
-
|
|
1396
853
|
constructor(comparator?: Comparator<E>) {
|
|
1397
854
|
this.clear();
|
|
1398
855
|
this._comparator = comparator || this._defaultComparator;
|
|
@@ -1406,12 +863,12 @@ export class FibonacciHeap<E> {
|
|
|
1406
863
|
* @remarks Time O(1), Space O(1)
|
|
1407
864
|
* @returns Root node or undefined.
|
|
1408
865
|
*/
|
|
1409
|
-
|
|
1410
866
|
get root(): FibonacciHeapNode<E> | undefined {
|
|
1411
867
|
return this._root;
|
|
1412
868
|
}
|
|
1413
869
|
|
|
1414
870
|
protected _size = 0;
|
|
871
|
+
|
|
1415
872
|
get size(): number {
|
|
1416
873
|
return this._size;
|
|
1417
874
|
}
|
|
@@ -1423,13 +880,10 @@ export class FibonacciHeap<E> {
|
|
|
1423
880
|
* @remarks Time O(1), Space O(1)
|
|
1424
881
|
* @returns Min node or undefined.
|
|
1425
882
|
*/
|
|
1426
|
-
|
|
1427
883
|
get min(): FibonacciHeapNode<E> | undefined {
|
|
1428
884
|
return this._min;
|
|
1429
885
|
}
|
|
1430
886
|
|
|
1431
|
-
protected readonly _comparator: Comparator<E>;
|
|
1432
|
-
|
|
1433
887
|
get comparator(): Comparator<E> {
|
|
1434
888
|
return this._comparator;
|
|
1435
889
|
}
|
|
@@ -1451,7 +905,6 @@ export class FibonacciHeap<E> {
|
|
|
1451
905
|
* @param element - Element to insert.
|
|
1452
906
|
* @returns True when the element is added.
|
|
1453
907
|
*/
|
|
1454
|
-
|
|
1455
908
|
push(element: E): boolean {
|
|
1456
909
|
const node = this.createNode(element);
|
|
1457
910
|
node.left = node;
|
|
@@ -1472,7 +925,6 @@ export class FibonacciHeap<E> {
|
|
|
1472
925
|
* @param [head] - Start node of the circular list.
|
|
1473
926
|
* @returns Array of nodes from the list.
|
|
1474
927
|
*/
|
|
1475
|
-
|
|
1476
928
|
consumeLinkedList(head?: FibonacciHeapNode<E>): FibonacciHeapNode<E>[] {
|
|
1477
929
|
const elements: FibonacciHeapNode<E>[] = [];
|
|
1478
930
|
if (!head) return elements;
|
|
@@ -1494,7 +946,6 @@ export class FibonacciHeap<E> {
|
|
|
1494
946
|
* @param node - Child node to insert.
|
|
1495
947
|
* @returns void
|
|
1496
948
|
*/
|
|
1497
|
-
|
|
1498
949
|
mergeWithChild(parent: FibonacciHeapNode<E>, node: FibonacciHeapNode<E>): void {
|
|
1499
950
|
if (!parent.child) parent.child = node;
|
|
1500
951
|
else {
|
|
@@ -1514,7 +965,6 @@ export class FibonacciHeap<E> {
|
|
|
1514
965
|
* @remarks Time O(log N) amortized, Space O(1)
|
|
1515
966
|
* @returns Minimum element or undefined.
|
|
1516
967
|
*/
|
|
1517
|
-
|
|
1518
968
|
pop(): E | undefined {
|
|
1519
969
|
if (this._size === 0) return undefined;
|
|
1520
970
|
const z = this.min!;
|
|
@@ -1543,7 +993,6 @@ export class FibonacciHeap<E> {
|
|
|
1543
993
|
* @param heapToMerge - Another FibonacciHeap to meld into this one.
|
|
1544
994
|
* @returns void
|
|
1545
995
|
*/
|
|
1546
|
-
|
|
1547
996
|
merge(heapToMerge: FibonacciHeap<E>): void {
|
|
1548
997
|
if (heapToMerge.size === 0) return;
|
|
1549
998
|
if (this.root && heapToMerge.root) {
|
|
@@ -1611,7 +1060,6 @@ export class FibonacciHeap<E> {
|
|
|
1611
1060
|
y: FibonacciHeapNode<E> | undefined,
|
|
1612
1061
|
d: number,
|
|
1613
1062
|
t: FibonacciHeapNode<E> | undefined;
|
|
1614
|
-
|
|
1615
1063
|
for (const node of elements) {
|
|
1616
1064
|
x = node;
|
|
1617
1065
|
d = x.degree;
|
|
@@ -1628,7 +1076,6 @@ export class FibonacciHeap<E> {
|
|
|
1628
1076
|
}
|
|
1629
1077
|
A[d] = x;
|
|
1630
1078
|
}
|
|
1631
|
-
|
|
1632
1079
|
for (let i = 0; i < A.length; i++) {
|
|
1633
1080
|
if (A[i] && (!this.min || this.comparator(A[i]!.element, this.min.element) <= 0)) this._min = A[i]!;
|
|
1634
1081
|
}
|