max-priority-queue-typed 2.4.5 → 2.5.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/README.md +63 -0
- package/dist/cjs/index.cjs +694 -119
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +693 -118
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +694 -119
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +693 -118
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- 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 +380 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +487 -147
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +956 -80
- package/dist/types/data-structures/binary-tree/bst.d.ts +816 -29
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +610 -31
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +326 -135
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3781 -6
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3607 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2874 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3528 -6
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +429 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +393 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +473 -89
- package/dist/types/data-structures/heap/heap.d.ts +581 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +646 -47
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +596 -68
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +793 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +499 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +593 -71
- package/dist/types/data-structures/queue/queue.d.ts +463 -42
- package/dist/types/data-structures/stack/stack.d.ts +384 -32
- package/dist/types/data-structures/trie/trie.d.ts +470 -48
- 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/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- 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/max-priority-queue-typed.js +691 -116
- package/dist/umd/max-priority-queue-typed.js.map +1 -1
- package/dist/umd/max-priority-queue-typed.min.js +1 -1
- package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- 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 +386 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
- package/src/data-structures/binary-tree/binary-tree.ts +956 -81
- package/src/data-structures/binary-tree/bst.ts +840 -35
- package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
- package/src/data-structures/binary-tree/segment-tree.ts +498 -249
- package/src/data-structures/binary-tree/tree-map.ts +3784 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
- package/src/data-structures/binary-tree/tree-set.ts +3531 -10
- package/src/data-structures/graph/abstract-graph.ts +4 -4
- package/src/data-structures/graph/directed-graph.ts +429 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +393 -59
- package/src/data-structures/hash/hash-map.ts +476 -92
- package/src/data-structures/heap/heap.ts +581 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +646 -47
- package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
- package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
- package/src/data-structures/matrix/matrix.ts +584 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +592 -70
- package/src/data-structures/queue/queue.ts +463 -42
- package/src/data-structures/stack/stack.ts +384 -32
- package/src/data-structures/trie/trie.ts +470 -48
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
package/dist/cjs/index.cjs
CHANGED
|
@@ -3,55 +3,6 @@
|
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
4
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
5
5
|
|
|
6
|
-
// src/common/error.ts
|
|
7
|
-
var ERR = {
|
|
8
|
-
// Range / index
|
|
9
|
-
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
10
|
-
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
11
|
-
// Type / argument
|
|
12
|
-
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
13
|
-
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
14
|
-
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
15
|
-
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
16
|
-
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
17
|
-
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
18
|
-
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
19
|
-
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
20
|
-
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
21
|
-
// State / operation
|
|
22
|
-
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
23
|
-
// Matrix
|
|
24
|
-
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
25
|
-
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
26
|
-
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
27
|
-
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
28
|
-
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
// src/common/index.ts
|
|
32
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
33
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
34
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
35
|
-
return DFSOperation2;
|
|
36
|
-
})(DFSOperation || {});
|
|
37
|
-
var Range = class {
|
|
38
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
39
|
-
this.low = low;
|
|
40
|
-
this.high = high;
|
|
41
|
-
this.includeLow = includeLow;
|
|
42
|
-
this.includeHigh = includeHigh;
|
|
43
|
-
}
|
|
44
|
-
static {
|
|
45
|
-
__name(this, "Range");
|
|
46
|
-
}
|
|
47
|
-
// Determine whether a key is within the range
|
|
48
|
-
isInRange(key, comparator) {
|
|
49
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
50
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
51
|
-
return lowCheck && highCheck;
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
|
|
55
6
|
// src/data-structures/base/iterable-element-base.ts
|
|
56
7
|
var IterableElementBase = class {
|
|
57
8
|
static {
|
|
@@ -70,7 +21,7 @@ var IterableElementBase = class {
|
|
|
70
21
|
if (options) {
|
|
71
22
|
const { toElementFn } = options;
|
|
72
23
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
73
|
-
else if (toElementFn) throw new TypeError(
|
|
24
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
74
25
|
}
|
|
75
26
|
}
|
|
76
27
|
/**
|
|
@@ -233,7 +184,7 @@ var IterableElementBase = class {
|
|
|
233
184
|
acc = initialValue;
|
|
234
185
|
} else {
|
|
235
186
|
const first = iter.next();
|
|
236
|
-
if (first.done) throw new TypeError(
|
|
187
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
237
188
|
acc = first.value;
|
|
238
189
|
index = 1;
|
|
239
190
|
}
|
|
@@ -275,6 +226,55 @@ var IterableElementBase = class {
|
|
|
275
226
|
}
|
|
276
227
|
};
|
|
277
228
|
|
|
229
|
+
// src/common/error.ts
|
|
230
|
+
var ERR = {
|
|
231
|
+
// Range / index
|
|
232
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
233
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
234
|
+
// Type / argument
|
|
235
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
236
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
237
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
238
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
239
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
240
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
241
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
242
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
243
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
244
|
+
// State / operation
|
|
245
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
246
|
+
// Matrix
|
|
247
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
248
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
249
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
250
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
251
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
// src/common/index.ts
|
|
255
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
256
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
257
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
258
|
+
return DFSOperation2;
|
|
259
|
+
})(DFSOperation || {});
|
|
260
|
+
var Range = class {
|
|
261
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
262
|
+
this.low = low;
|
|
263
|
+
this.high = high;
|
|
264
|
+
this.includeLow = includeLow;
|
|
265
|
+
this.includeHigh = includeHigh;
|
|
266
|
+
}
|
|
267
|
+
static {
|
|
268
|
+
__name(this, "Range");
|
|
269
|
+
}
|
|
270
|
+
// Determine whether a key is within the range
|
|
271
|
+
isInRange(key, comparator) {
|
|
272
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
273
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
274
|
+
return lowCheck && highCheck;
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
|
|
278
278
|
// src/data-structures/heap/heap.ts
|
|
279
279
|
var Heap = class _Heap extends IterableElementBase {
|
|
280
280
|
static {
|
|
@@ -306,10 +306,51 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
306
306
|
return this._elements;
|
|
307
307
|
}
|
|
308
308
|
/**
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
309
|
+
* Get the number of elements.
|
|
310
|
+
* @remarks Time O(1), Space O(1)
|
|
311
|
+
* @returns Heap size.
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
* @example
|
|
345
|
+
* // Track heap capacity
|
|
346
|
+
* const heap = new Heap<number>();
|
|
347
|
+
* console.log(heap.size); // 0;
|
|
348
|
+
* heap.add(10);
|
|
349
|
+
* heap.add(20);
|
|
350
|
+
* console.log(heap.size); // 2;
|
|
351
|
+
* heap.poll();
|
|
352
|
+
* console.log(heap.size); // 1;
|
|
353
|
+
*/
|
|
313
354
|
get size() {
|
|
314
355
|
return this.elements.length;
|
|
315
356
|
}
|
|
@@ -347,21 +388,103 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
347
388
|
return new _Heap(elements, options);
|
|
348
389
|
}
|
|
349
390
|
/**
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
391
|
+
* Insert an element.
|
|
392
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
393
|
+
* @param element - Element to insert.
|
|
394
|
+
* @returns True.
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
* @example
|
|
428
|
+
* // basic Heap creation and add operation
|
|
429
|
+
* // Create a min heap (default)
|
|
430
|
+
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
431
|
+
*
|
|
432
|
+
* // Verify size
|
|
433
|
+
* console.log(minHeap.size); // 6;
|
|
434
|
+
*
|
|
435
|
+
* // Add new element
|
|
436
|
+
* minHeap.add(4);
|
|
437
|
+
* console.log(minHeap.size); // 7;
|
|
438
|
+
*
|
|
439
|
+
* // Min heap property: smallest element at root
|
|
440
|
+
* const min = minHeap.peek();
|
|
441
|
+
* console.log(min); // 1;
|
|
442
|
+
*/
|
|
355
443
|
add(element) {
|
|
356
444
|
this._elements.push(element);
|
|
357
445
|
return this._bubbleUp(this.elements.length - 1);
|
|
358
446
|
}
|
|
359
447
|
/**
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
448
|
+
* Insert many elements from an iterable.
|
|
449
|
+
* @remarks Time O(N log N), Space O(1)
|
|
450
|
+
* @param elements - Iterable of elements or raw values.
|
|
451
|
+
* @returns Array of per-element success flags.
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
* @example
|
|
482
|
+
* // Add multiple elements
|
|
483
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
484
|
+
* heap.addMany([5, 3, 7, 1]);
|
|
485
|
+
* console.log(heap.peek()); // 1;
|
|
486
|
+
* console.log(heap.size); // 4;
|
|
487
|
+
*/
|
|
365
488
|
addMany(elements) {
|
|
366
489
|
const flags = [];
|
|
367
490
|
for (const el of elements) {
|
|
@@ -376,10 +499,67 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
376
499
|
return flags;
|
|
377
500
|
}
|
|
378
501
|
/**
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
502
|
+
* Remove and return the top element.
|
|
503
|
+
* @remarks Time O(log N), Space O(1)
|
|
504
|
+
* @returns Top element or undefined.
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
* @example
|
|
538
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
539
|
+
* interface Task {
|
|
540
|
+
* id: number;
|
|
541
|
+
* priority: number;
|
|
542
|
+
* name: string;
|
|
543
|
+
* }
|
|
544
|
+
*
|
|
545
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
546
|
+
* const tasks: Task[] = [
|
|
547
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
548
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
549
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
550
|
+
* ];
|
|
551
|
+
*
|
|
552
|
+
* const maxHeap = new Heap(tasks, {
|
|
553
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
554
|
+
* });
|
|
555
|
+
*
|
|
556
|
+
* console.log(maxHeap.size); // 3;
|
|
557
|
+
*
|
|
558
|
+
* // Peek returns highest priority task
|
|
559
|
+
* const topTask = maxHeap.peek();
|
|
560
|
+
* console.log(topTask?.priority); // 8;
|
|
561
|
+
* console.log(topTask?.name); // 'Alert';
|
|
562
|
+
*/
|
|
383
563
|
poll() {
|
|
384
564
|
if (this.elements.length === 0) return;
|
|
385
565
|
const value = this.elements[0];
|
|
@@ -391,26 +571,188 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
391
571
|
return value;
|
|
392
572
|
}
|
|
393
573
|
/**
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
574
|
+
* Get the current top element without removing it.
|
|
575
|
+
* @remarks Time O(1), Space O(1)
|
|
576
|
+
* @returns Top element or undefined.
|
|
577
|
+
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
* @example
|
|
610
|
+
* // Heap for event processing with priority
|
|
611
|
+
* interface Event {
|
|
612
|
+
* id: number;
|
|
613
|
+
* type: 'critical' | 'warning' | 'info';
|
|
614
|
+
* timestamp: number;
|
|
615
|
+
* message: string;
|
|
616
|
+
* }
|
|
617
|
+
*
|
|
618
|
+
* // Custom priority: critical > warning > info
|
|
619
|
+
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
620
|
+
*
|
|
621
|
+
* const eventHeap = new Heap<Event>([], {
|
|
622
|
+
* comparator: (a: Event, b: Event) => {
|
|
623
|
+
* const priorityA = priorityMap[a.type];
|
|
624
|
+
* const priorityB = priorityMap[b.type];
|
|
625
|
+
* return priorityB - priorityA; // Higher priority first
|
|
626
|
+
* }
|
|
627
|
+
* });
|
|
628
|
+
*
|
|
629
|
+
* // Add events in random order
|
|
630
|
+
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
631
|
+
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
632
|
+
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
633
|
+
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
634
|
+
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
635
|
+
*
|
|
636
|
+
* console.log(eventHeap.size); // 5;
|
|
637
|
+
*
|
|
638
|
+
* // Process events by priority (critical first)
|
|
639
|
+
* const processedOrder: Event[] = [];
|
|
640
|
+
* while (eventHeap.size > 0) {
|
|
641
|
+
* const event = eventHeap.poll();
|
|
642
|
+
* if (event) {
|
|
643
|
+
* processedOrder.push(event);
|
|
644
|
+
* }
|
|
645
|
+
* }
|
|
646
|
+
*
|
|
647
|
+
* // Verify critical events came first
|
|
648
|
+
* console.log(processedOrder[0].type); // 'critical';
|
|
649
|
+
* console.log(processedOrder[1].type); // 'critical';
|
|
650
|
+
* console.log(processedOrder[2].type); // 'warning';
|
|
651
|
+
* console.log(processedOrder[3].type); // 'info';
|
|
652
|
+
* console.log(processedOrder[4].type); // 'info';
|
|
653
|
+
*
|
|
654
|
+
* // Verify O(log n) operations
|
|
655
|
+
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
656
|
+
*
|
|
657
|
+
* // Add - O(log n)
|
|
658
|
+
* newHeap.add(2);
|
|
659
|
+
* console.log(newHeap.size); // 5;
|
|
660
|
+
*
|
|
661
|
+
* // Poll - O(log n)
|
|
662
|
+
* const removed = newHeap.poll();
|
|
663
|
+
* console.log(removed); // 1;
|
|
664
|
+
*
|
|
665
|
+
* // Peek - O(1)
|
|
666
|
+
* const top = newHeap.peek();
|
|
667
|
+
* console.log(top); // 2;
|
|
668
|
+
*/
|
|
398
669
|
peek() {
|
|
399
670
|
return this.elements[0];
|
|
400
671
|
}
|
|
401
672
|
/**
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
673
|
+
* Check whether the heap is empty.
|
|
674
|
+
* @remarks Time O(1), Space O(1)
|
|
675
|
+
* @returns True if size is 0.
|
|
676
|
+
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
|
|
695
|
+
|
|
696
|
+
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
|
|
706
|
+
* @example
|
|
707
|
+
* // Check if heap is empty
|
|
708
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
709
|
+
* console.log(heap.isEmpty()); // true;
|
|
710
|
+
* heap.add(1);
|
|
711
|
+
* console.log(heap.isEmpty()); // false;
|
|
712
|
+
*/
|
|
406
713
|
isEmpty() {
|
|
407
714
|
return this.size === 0;
|
|
408
715
|
}
|
|
409
716
|
/**
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
717
|
+
* Remove all elements.
|
|
718
|
+
* @remarks Time O(1), Space O(1)
|
|
719
|
+
* @returns void
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
|
|
729
|
+
|
|
730
|
+
|
|
731
|
+
|
|
732
|
+
|
|
733
|
+
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
|
|
746
|
+
|
|
747
|
+
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
* @example
|
|
751
|
+
* // Remove all elements
|
|
752
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
753
|
+
* heap.clear();
|
|
754
|
+
* console.log(heap.isEmpty()); // true;
|
|
755
|
+
*/
|
|
414
756
|
clear() {
|
|
415
757
|
this._elements = [];
|
|
416
758
|
}
|
|
@@ -425,21 +767,83 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
425
767
|
return this.fix();
|
|
426
768
|
}
|
|
427
769
|
/**
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
770
|
+
* Check if an equal element exists in the heap.
|
|
771
|
+
* @remarks Time O(N), Space O(1)
|
|
772
|
+
* @param element - Element to search for.
|
|
773
|
+
* @returns True if found.
|
|
774
|
+
|
|
775
|
+
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
|
|
781
|
+
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
|
|
785
|
+
|
|
786
|
+
|
|
787
|
+
|
|
788
|
+
|
|
789
|
+
|
|
790
|
+
|
|
791
|
+
|
|
792
|
+
|
|
793
|
+
|
|
794
|
+
|
|
795
|
+
|
|
796
|
+
|
|
797
|
+
* @example
|
|
798
|
+
* // Check element existence
|
|
799
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
800
|
+
* console.log(heap.has(1)); // true;
|
|
801
|
+
* console.log(heap.has(99)); // false;
|
|
802
|
+
*/
|
|
433
803
|
has(element) {
|
|
434
804
|
for (const el of this.elements) if (this._equals(el, element)) return true;
|
|
435
805
|
return false;
|
|
436
806
|
}
|
|
437
807
|
/**
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
808
|
+
* Delete one occurrence of an element.
|
|
809
|
+
* @remarks Time O(N), Space O(1)
|
|
810
|
+
* @param element - Element to delete.
|
|
811
|
+
* @returns True if an element was removed.
|
|
812
|
+
|
|
813
|
+
|
|
814
|
+
|
|
815
|
+
|
|
816
|
+
|
|
817
|
+
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
|
|
829
|
+
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
* @example
|
|
842
|
+
* // Remove specific element
|
|
843
|
+
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
844
|
+
* heap.delete(4);
|
|
845
|
+
* console.log(heap.toArray().includes(4)); // false;
|
|
846
|
+
*/
|
|
443
847
|
delete(element) {
|
|
444
848
|
let index = -1;
|
|
445
849
|
for (let i = 0; i < this.elements.length; i++) {
|
|
@@ -497,11 +901,39 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
497
901
|
return this;
|
|
498
902
|
}
|
|
499
903
|
/**
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
904
|
+
* Traverse the binary heap as a complete binary tree and collect elements.
|
|
905
|
+
* @remarks Time O(N), Space O(H)
|
|
906
|
+
* @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
907
|
+
* @returns Array of visited elements.
|
|
908
|
+
|
|
909
|
+
|
|
910
|
+
|
|
911
|
+
|
|
912
|
+
|
|
913
|
+
|
|
914
|
+
|
|
915
|
+
|
|
916
|
+
|
|
917
|
+
|
|
918
|
+
|
|
919
|
+
|
|
920
|
+
|
|
921
|
+
|
|
922
|
+
|
|
923
|
+
|
|
924
|
+
|
|
925
|
+
|
|
926
|
+
|
|
927
|
+
|
|
928
|
+
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
* @example
|
|
932
|
+
* // Depth-first traversal
|
|
933
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
934
|
+
* const result = heap.dfs('IN');
|
|
935
|
+
* console.log(result.length); // 3;
|
|
936
|
+
*/
|
|
505
937
|
dfs(order = "PRE") {
|
|
506
938
|
const result = [];
|
|
507
939
|
const _dfs = /* @__PURE__ */ __name((index) => {
|
|
@@ -538,10 +970,47 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
538
970
|
return results;
|
|
539
971
|
}
|
|
540
972
|
/**
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
973
|
+
* Return all elements in ascending order by repeatedly polling.
|
|
974
|
+
* @remarks Time O(N log N), Space O(N)
|
|
975
|
+
* @returns Sorted array of elements.
|
|
976
|
+
|
|
977
|
+
|
|
978
|
+
|
|
979
|
+
|
|
980
|
+
|
|
981
|
+
|
|
982
|
+
|
|
983
|
+
|
|
984
|
+
|
|
985
|
+
|
|
986
|
+
|
|
987
|
+
|
|
988
|
+
|
|
989
|
+
|
|
990
|
+
|
|
991
|
+
|
|
992
|
+
|
|
993
|
+
|
|
994
|
+
|
|
995
|
+
|
|
996
|
+
|
|
997
|
+
|
|
998
|
+
|
|
999
|
+
|
|
1000
|
+
|
|
1001
|
+
|
|
1002
|
+
|
|
1003
|
+
|
|
1004
|
+
|
|
1005
|
+
|
|
1006
|
+
|
|
1007
|
+
|
|
1008
|
+
* @example
|
|
1009
|
+
* // Sort elements using heap
|
|
1010
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
1011
|
+
* const sorted = heap.sort();
|
|
1012
|
+
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
1013
|
+
*/
|
|
545
1014
|
sort() {
|
|
546
1015
|
const visited = [];
|
|
547
1016
|
const cloned = this._createInstance();
|
|
@@ -553,22 +1022,94 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
553
1022
|
return visited;
|
|
554
1023
|
}
|
|
555
1024
|
/**
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
1025
|
+
* Deep clone this heap.
|
|
1026
|
+
* @remarks Time O(N), Space O(N)
|
|
1027
|
+
* @returns A new heap with the same elements.
|
|
1028
|
+
|
|
1029
|
+
|
|
1030
|
+
|
|
1031
|
+
|
|
1032
|
+
|
|
1033
|
+
|
|
1034
|
+
|
|
1035
|
+
|
|
1036
|
+
|
|
1037
|
+
|
|
1038
|
+
|
|
1039
|
+
|
|
1040
|
+
|
|
1041
|
+
|
|
1042
|
+
|
|
1043
|
+
|
|
1044
|
+
|
|
1045
|
+
|
|
1046
|
+
|
|
1047
|
+
|
|
1048
|
+
|
|
1049
|
+
|
|
1050
|
+
|
|
1051
|
+
|
|
1052
|
+
|
|
1053
|
+
|
|
1054
|
+
|
|
1055
|
+
|
|
1056
|
+
|
|
1057
|
+
|
|
1058
|
+
* @example
|
|
1059
|
+
* // Create independent copy
|
|
1060
|
+
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
1061
|
+
* const copy = heap.clone();
|
|
1062
|
+
* copy.poll();
|
|
1063
|
+
* console.log(heap.size); // 3;
|
|
1064
|
+
* console.log(copy.size); // 2;
|
|
1065
|
+
*/
|
|
560
1066
|
clone() {
|
|
561
1067
|
const next = this._createInstance();
|
|
562
1068
|
for (const x of this.elements) next.add(x);
|
|
563
1069
|
return next;
|
|
564
1070
|
}
|
|
565
1071
|
/**
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
1072
|
+
* Filter elements into a new heap of the same class.
|
|
1073
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1074
|
+
* @param callback - Predicate (element, index, heap) → boolean to keep element.
|
|
1075
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1076
|
+
* @returns A new heap with the kept elements.
|
|
1077
|
+
|
|
1078
|
+
|
|
1079
|
+
|
|
1080
|
+
|
|
1081
|
+
|
|
1082
|
+
|
|
1083
|
+
|
|
1084
|
+
|
|
1085
|
+
|
|
1086
|
+
|
|
1087
|
+
|
|
1088
|
+
|
|
1089
|
+
|
|
1090
|
+
|
|
1091
|
+
|
|
1092
|
+
|
|
1093
|
+
|
|
1094
|
+
|
|
1095
|
+
|
|
1096
|
+
|
|
1097
|
+
|
|
1098
|
+
|
|
1099
|
+
|
|
1100
|
+
|
|
1101
|
+
|
|
1102
|
+
|
|
1103
|
+
|
|
1104
|
+
|
|
1105
|
+
|
|
1106
|
+
|
|
1107
|
+
* @example
|
|
1108
|
+
* // Filter elements
|
|
1109
|
+
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
1110
|
+
* const evens = heap.filter(x => x % 2 === 0);
|
|
1111
|
+
* console.log(evens.size); // 2;
|
|
1112
|
+
*/
|
|
572
1113
|
filter(callback, thisArg) {
|
|
573
1114
|
const out = this._createInstance();
|
|
574
1115
|
let i = 0;
|
|
@@ -582,15 +1123,49 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
582
1123
|
return out;
|
|
583
1124
|
}
|
|
584
1125
|
/**
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
1126
|
+
* Map elements into a new heap of possibly different element type.
|
|
1127
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1128
|
+
* @template EM
|
|
1129
|
+
* @template RM
|
|
1130
|
+
* @param callback - Mapping function (element, index, heap) → newElement.
|
|
1131
|
+
* @param options - Options for the output heap, including comparator for EM.
|
|
1132
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1133
|
+
* @returns A new heap with mapped elements.
|
|
1134
|
+
|
|
1135
|
+
|
|
1136
|
+
|
|
1137
|
+
|
|
1138
|
+
|
|
1139
|
+
|
|
1140
|
+
|
|
1141
|
+
|
|
1142
|
+
|
|
1143
|
+
|
|
1144
|
+
|
|
1145
|
+
|
|
1146
|
+
|
|
1147
|
+
|
|
1148
|
+
|
|
1149
|
+
|
|
1150
|
+
|
|
1151
|
+
|
|
1152
|
+
|
|
1153
|
+
|
|
1154
|
+
|
|
1155
|
+
|
|
1156
|
+
|
|
1157
|
+
|
|
1158
|
+
|
|
1159
|
+
|
|
1160
|
+
|
|
1161
|
+
|
|
1162
|
+
|
|
1163
|
+
* @example
|
|
1164
|
+
* // Transform elements
|
|
1165
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
1166
|
+
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
1167
|
+
* console.log(doubled.peek()); // 2;
|
|
1168
|
+
*/
|
|
594
1169
|
map(callback, options, thisArg) {
|
|
595
1170
|
const { comparator, toElementFn, ...rest } = options ?? {};
|
|
596
1171
|
if (!comparator) throw new TypeError(ERR.comparatorRequired("Heap.map"));
|