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/esm/index.mjs
CHANGED
|
@@ -1,55 +1,6 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
3
|
|
|
4
|
-
// src/common/error.ts
|
|
5
|
-
var ERR = {
|
|
6
|
-
// Range / index
|
|
7
|
-
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
8
|
-
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
9
|
-
// Type / argument
|
|
10
|
-
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
11
|
-
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
12
|
-
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
13
|
-
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
14
|
-
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
15
|
-
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
16
|
-
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
17
|
-
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
18
|
-
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
19
|
-
// State / operation
|
|
20
|
-
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
21
|
-
// Matrix
|
|
22
|
-
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
23
|
-
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
24
|
-
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
25
|
-
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
26
|
-
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
// src/common/index.ts
|
|
30
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
31
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
32
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
33
|
-
return DFSOperation2;
|
|
34
|
-
})(DFSOperation || {});
|
|
35
|
-
var Range = class {
|
|
36
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
37
|
-
this.low = low;
|
|
38
|
-
this.high = high;
|
|
39
|
-
this.includeLow = includeLow;
|
|
40
|
-
this.includeHigh = includeHigh;
|
|
41
|
-
}
|
|
42
|
-
static {
|
|
43
|
-
__name(this, "Range");
|
|
44
|
-
}
|
|
45
|
-
// Determine whether a key is within the range
|
|
46
|
-
isInRange(key, comparator) {
|
|
47
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
48
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
49
|
-
return lowCheck && highCheck;
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
|
|
53
4
|
// src/data-structures/base/iterable-element-base.ts
|
|
54
5
|
var IterableElementBase = class {
|
|
55
6
|
static {
|
|
@@ -68,7 +19,7 @@ var IterableElementBase = class {
|
|
|
68
19
|
if (options) {
|
|
69
20
|
const { toElementFn } = options;
|
|
70
21
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
71
|
-
else if (toElementFn) throw new TypeError(
|
|
22
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
72
23
|
}
|
|
73
24
|
}
|
|
74
25
|
/**
|
|
@@ -231,7 +182,7 @@ var IterableElementBase = class {
|
|
|
231
182
|
acc = initialValue;
|
|
232
183
|
} else {
|
|
233
184
|
const first = iter.next();
|
|
234
|
-
if (first.done) throw new TypeError(
|
|
185
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
235
186
|
acc = first.value;
|
|
236
187
|
index = 1;
|
|
237
188
|
}
|
|
@@ -273,6 +224,55 @@ var IterableElementBase = class {
|
|
|
273
224
|
}
|
|
274
225
|
};
|
|
275
226
|
|
|
227
|
+
// src/common/error.ts
|
|
228
|
+
var ERR = {
|
|
229
|
+
// Range / index
|
|
230
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
231
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
232
|
+
// Type / argument
|
|
233
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
234
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
235
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
236
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
237
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
238
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
239
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
240
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
241
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
242
|
+
// State / operation
|
|
243
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
244
|
+
// Matrix
|
|
245
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
246
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
247
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
248
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
249
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
// src/common/index.ts
|
|
253
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
254
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
255
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
256
|
+
return DFSOperation2;
|
|
257
|
+
})(DFSOperation || {});
|
|
258
|
+
var Range = class {
|
|
259
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
260
|
+
this.low = low;
|
|
261
|
+
this.high = high;
|
|
262
|
+
this.includeLow = includeLow;
|
|
263
|
+
this.includeHigh = includeHigh;
|
|
264
|
+
}
|
|
265
|
+
static {
|
|
266
|
+
__name(this, "Range");
|
|
267
|
+
}
|
|
268
|
+
// Determine whether a key is within the range
|
|
269
|
+
isInRange(key, comparator) {
|
|
270
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
271
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
272
|
+
return lowCheck && highCheck;
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
|
|
276
276
|
// src/data-structures/heap/heap.ts
|
|
277
277
|
var Heap = class _Heap extends IterableElementBase {
|
|
278
278
|
static {
|
|
@@ -304,10 +304,51 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
304
304
|
return this._elements;
|
|
305
305
|
}
|
|
306
306
|
/**
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
307
|
+
* Get the number of elements.
|
|
308
|
+
* @remarks Time O(1), Space O(1)
|
|
309
|
+
* @returns Heap size.
|
|
310
|
+
|
|
311
|
+
|
|
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
|
+
* @example
|
|
343
|
+
* // Track heap capacity
|
|
344
|
+
* const heap = new Heap<number>();
|
|
345
|
+
* console.log(heap.size); // 0;
|
|
346
|
+
* heap.add(10);
|
|
347
|
+
* heap.add(20);
|
|
348
|
+
* console.log(heap.size); // 2;
|
|
349
|
+
* heap.poll();
|
|
350
|
+
* console.log(heap.size); // 1;
|
|
351
|
+
*/
|
|
311
352
|
get size() {
|
|
312
353
|
return this.elements.length;
|
|
313
354
|
}
|
|
@@ -345,21 +386,103 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
345
386
|
return new _Heap(elements, options);
|
|
346
387
|
}
|
|
347
388
|
/**
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
389
|
+
* Insert an element.
|
|
390
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
391
|
+
* @param element - Element to insert.
|
|
392
|
+
* @returns True.
|
|
393
|
+
|
|
394
|
+
|
|
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
|
+
* @example
|
|
426
|
+
* // basic Heap creation and add operation
|
|
427
|
+
* // Create a min heap (default)
|
|
428
|
+
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
429
|
+
*
|
|
430
|
+
* // Verify size
|
|
431
|
+
* console.log(minHeap.size); // 6;
|
|
432
|
+
*
|
|
433
|
+
* // Add new element
|
|
434
|
+
* minHeap.add(4);
|
|
435
|
+
* console.log(minHeap.size); // 7;
|
|
436
|
+
*
|
|
437
|
+
* // Min heap property: smallest element at root
|
|
438
|
+
* const min = minHeap.peek();
|
|
439
|
+
* console.log(min); // 1;
|
|
440
|
+
*/
|
|
353
441
|
add(element) {
|
|
354
442
|
this._elements.push(element);
|
|
355
443
|
return this._bubbleUp(this.elements.length - 1);
|
|
356
444
|
}
|
|
357
445
|
/**
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
446
|
+
* Insert many elements from an iterable.
|
|
447
|
+
* @remarks Time O(N log N), Space O(1)
|
|
448
|
+
* @param elements - Iterable of elements or raw values.
|
|
449
|
+
* @returns Array of per-element success flags.
|
|
450
|
+
|
|
451
|
+
|
|
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
|
+
* @example
|
|
480
|
+
* // Add multiple elements
|
|
481
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
482
|
+
* heap.addMany([5, 3, 7, 1]);
|
|
483
|
+
* console.log(heap.peek()); // 1;
|
|
484
|
+
* console.log(heap.size); // 4;
|
|
485
|
+
*/
|
|
363
486
|
addMany(elements) {
|
|
364
487
|
const flags = [];
|
|
365
488
|
for (const el of elements) {
|
|
@@ -374,10 +497,67 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
374
497
|
return flags;
|
|
375
498
|
}
|
|
376
499
|
/**
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
500
|
+
* Remove and return the top element.
|
|
501
|
+
* @remarks Time O(log N), Space O(1)
|
|
502
|
+
* @returns Top element or undefined.
|
|
503
|
+
|
|
504
|
+
|
|
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
|
+
* @example
|
|
536
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
537
|
+
* interface Task {
|
|
538
|
+
* id: number;
|
|
539
|
+
* priority: number;
|
|
540
|
+
* name: string;
|
|
541
|
+
* }
|
|
542
|
+
*
|
|
543
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
544
|
+
* const tasks: Task[] = [
|
|
545
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
546
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
547
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
548
|
+
* ];
|
|
549
|
+
*
|
|
550
|
+
* const maxHeap = new Heap(tasks, {
|
|
551
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
552
|
+
* });
|
|
553
|
+
*
|
|
554
|
+
* console.log(maxHeap.size); // 3;
|
|
555
|
+
*
|
|
556
|
+
* // Peek returns highest priority task
|
|
557
|
+
* const topTask = maxHeap.peek();
|
|
558
|
+
* console.log(topTask?.priority); // 8;
|
|
559
|
+
* console.log(topTask?.name); // 'Alert';
|
|
560
|
+
*/
|
|
381
561
|
poll() {
|
|
382
562
|
if (this.elements.length === 0) return;
|
|
383
563
|
const value = this.elements[0];
|
|
@@ -389,26 +569,188 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
389
569
|
return value;
|
|
390
570
|
}
|
|
391
571
|
/**
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
572
|
+
* Get the current top element without removing it.
|
|
573
|
+
* @remarks Time O(1), Space O(1)
|
|
574
|
+
* @returns Top element or undefined.
|
|
575
|
+
|
|
576
|
+
|
|
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
|
+
* @example
|
|
608
|
+
* // Heap for event processing with priority
|
|
609
|
+
* interface Event {
|
|
610
|
+
* id: number;
|
|
611
|
+
* type: 'critical' | 'warning' | 'info';
|
|
612
|
+
* timestamp: number;
|
|
613
|
+
* message: string;
|
|
614
|
+
* }
|
|
615
|
+
*
|
|
616
|
+
* // Custom priority: critical > warning > info
|
|
617
|
+
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
618
|
+
*
|
|
619
|
+
* const eventHeap = new Heap<Event>([], {
|
|
620
|
+
* comparator: (a: Event, b: Event) => {
|
|
621
|
+
* const priorityA = priorityMap[a.type];
|
|
622
|
+
* const priorityB = priorityMap[b.type];
|
|
623
|
+
* return priorityB - priorityA; // Higher priority first
|
|
624
|
+
* }
|
|
625
|
+
* });
|
|
626
|
+
*
|
|
627
|
+
* // Add events in random order
|
|
628
|
+
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
629
|
+
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
630
|
+
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
631
|
+
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
632
|
+
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
633
|
+
*
|
|
634
|
+
* console.log(eventHeap.size); // 5;
|
|
635
|
+
*
|
|
636
|
+
* // Process events by priority (critical first)
|
|
637
|
+
* const processedOrder: Event[] = [];
|
|
638
|
+
* while (eventHeap.size > 0) {
|
|
639
|
+
* const event = eventHeap.poll();
|
|
640
|
+
* if (event) {
|
|
641
|
+
* processedOrder.push(event);
|
|
642
|
+
* }
|
|
643
|
+
* }
|
|
644
|
+
*
|
|
645
|
+
* // Verify critical events came first
|
|
646
|
+
* console.log(processedOrder[0].type); // 'critical';
|
|
647
|
+
* console.log(processedOrder[1].type); // 'critical';
|
|
648
|
+
* console.log(processedOrder[2].type); // 'warning';
|
|
649
|
+
* console.log(processedOrder[3].type); // 'info';
|
|
650
|
+
* console.log(processedOrder[4].type); // 'info';
|
|
651
|
+
*
|
|
652
|
+
* // Verify O(log n) operations
|
|
653
|
+
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
654
|
+
*
|
|
655
|
+
* // Add - O(log n)
|
|
656
|
+
* newHeap.add(2);
|
|
657
|
+
* console.log(newHeap.size); // 5;
|
|
658
|
+
*
|
|
659
|
+
* // Poll - O(log n)
|
|
660
|
+
* const removed = newHeap.poll();
|
|
661
|
+
* console.log(removed); // 1;
|
|
662
|
+
*
|
|
663
|
+
* // Peek - O(1)
|
|
664
|
+
* const top = newHeap.peek();
|
|
665
|
+
* console.log(top); // 2;
|
|
666
|
+
*/
|
|
396
667
|
peek() {
|
|
397
668
|
return this.elements[0];
|
|
398
669
|
}
|
|
399
670
|
/**
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
671
|
+
* Check whether the heap is empty.
|
|
672
|
+
* @remarks Time O(1), Space O(1)
|
|
673
|
+
* @returns True if size is 0.
|
|
674
|
+
|
|
675
|
+
|
|
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
|
+
* @example
|
|
705
|
+
* // Check if heap is empty
|
|
706
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
707
|
+
* console.log(heap.isEmpty()); // true;
|
|
708
|
+
* heap.add(1);
|
|
709
|
+
* console.log(heap.isEmpty()); // false;
|
|
710
|
+
*/
|
|
404
711
|
isEmpty() {
|
|
405
712
|
return this.size === 0;
|
|
406
713
|
}
|
|
407
714
|
/**
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
715
|
+
* Remove all elements.
|
|
716
|
+
* @remarks Time O(1), Space O(1)
|
|
717
|
+
* @returns void
|
|
718
|
+
|
|
719
|
+
|
|
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
|
+
* @example
|
|
749
|
+
* // Remove all elements
|
|
750
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
751
|
+
* heap.clear();
|
|
752
|
+
* console.log(heap.isEmpty()); // true;
|
|
753
|
+
*/
|
|
412
754
|
clear() {
|
|
413
755
|
this._elements = [];
|
|
414
756
|
}
|
|
@@ -423,21 +765,83 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
423
765
|
return this.fix();
|
|
424
766
|
}
|
|
425
767
|
/**
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
768
|
+
* Check if an equal element exists in the heap.
|
|
769
|
+
* @remarks Time O(N), Space O(1)
|
|
770
|
+
* @param element - Element to search for.
|
|
771
|
+
* @returns True if found.
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
|
|
775
|
+
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
|
|
781
|
+
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
|
|
785
|
+
|
|
786
|
+
|
|
787
|
+
|
|
788
|
+
|
|
789
|
+
|
|
790
|
+
|
|
791
|
+
|
|
792
|
+
|
|
793
|
+
|
|
794
|
+
|
|
795
|
+
* @example
|
|
796
|
+
* // Check element existence
|
|
797
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
798
|
+
* console.log(heap.has(1)); // true;
|
|
799
|
+
* console.log(heap.has(99)); // false;
|
|
800
|
+
*/
|
|
431
801
|
has(element) {
|
|
432
802
|
for (const el of this.elements) if (this._equals(el, element)) return true;
|
|
433
803
|
return false;
|
|
434
804
|
}
|
|
435
805
|
/**
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
806
|
+
* Delete one occurrence of an element.
|
|
807
|
+
* @remarks Time O(N), Space O(1)
|
|
808
|
+
* @param element - Element to delete.
|
|
809
|
+
* @returns True if an element was removed.
|
|
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
|
+
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
|
|
839
|
+
* @example
|
|
840
|
+
* // Remove specific element
|
|
841
|
+
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
842
|
+
* heap.delete(4);
|
|
843
|
+
* console.log(heap.toArray().includes(4)); // false;
|
|
844
|
+
*/
|
|
441
845
|
delete(element) {
|
|
442
846
|
let index = -1;
|
|
443
847
|
for (let i = 0; i < this.elements.length; i++) {
|
|
@@ -495,11 +899,39 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
495
899
|
return this;
|
|
496
900
|
}
|
|
497
901
|
/**
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
902
|
+
* Traverse the binary heap as a complete binary tree and collect elements.
|
|
903
|
+
* @remarks Time O(N), Space O(H)
|
|
904
|
+
* @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
905
|
+
* @returns Array of visited elements.
|
|
906
|
+
|
|
907
|
+
|
|
908
|
+
|
|
909
|
+
|
|
910
|
+
|
|
911
|
+
|
|
912
|
+
|
|
913
|
+
|
|
914
|
+
|
|
915
|
+
|
|
916
|
+
|
|
917
|
+
|
|
918
|
+
|
|
919
|
+
|
|
920
|
+
|
|
921
|
+
|
|
922
|
+
|
|
923
|
+
|
|
924
|
+
|
|
925
|
+
|
|
926
|
+
|
|
927
|
+
|
|
928
|
+
|
|
929
|
+
* @example
|
|
930
|
+
* // Depth-first traversal
|
|
931
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
932
|
+
* const result = heap.dfs('IN');
|
|
933
|
+
* console.log(result.length); // 3;
|
|
934
|
+
*/
|
|
503
935
|
dfs(order = "PRE") {
|
|
504
936
|
const result = [];
|
|
505
937
|
const _dfs = /* @__PURE__ */ __name((index) => {
|
|
@@ -536,10 +968,47 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
536
968
|
return results;
|
|
537
969
|
}
|
|
538
970
|
/**
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
971
|
+
* Return all elements in ascending order by repeatedly polling.
|
|
972
|
+
* @remarks Time O(N log N), Space O(N)
|
|
973
|
+
* @returns Sorted array of elements.
|
|
974
|
+
|
|
975
|
+
|
|
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
|
+
* @example
|
|
1007
|
+
* // Sort elements using heap
|
|
1008
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
1009
|
+
* const sorted = heap.sort();
|
|
1010
|
+
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
1011
|
+
*/
|
|
543
1012
|
sort() {
|
|
544
1013
|
const visited = [];
|
|
545
1014
|
const cloned = this._createInstance();
|
|
@@ -551,22 +1020,94 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
551
1020
|
return visited;
|
|
552
1021
|
}
|
|
553
1022
|
/**
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
1023
|
+
* Deep clone this heap.
|
|
1024
|
+
* @remarks Time O(N), Space O(N)
|
|
1025
|
+
* @returns A new heap with the same elements.
|
|
1026
|
+
|
|
1027
|
+
|
|
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
|
+
* @example
|
|
1057
|
+
* // Create independent copy
|
|
1058
|
+
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
1059
|
+
* const copy = heap.clone();
|
|
1060
|
+
* copy.poll();
|
|
1061
|
+
* console.log(heap.size); // 3;
|
|
1062
|
+
* console.log(copy.size); // 2;
|
|
1063
|
+
*/
|
|
558
1064
|
clone() {
|
|
559
1065
|
const next = this._createInstance();
|
|
560
1066
|
for (const x of this.elements) next.add(x);
|
|
561
1067
|
return next;
|
|
562
1068
|
}
|
|
563
1069
|
/**
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
1070
|
+
* Filter elements into a new heap of the same class.
|
|
1071
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1072
|
+
* @param callback - Predicate (element, index, heap) → boolean to keep element.
|
|
1073
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1074
|
+
* @returns A new heap with the kept elements.
|
|
1075
|
+
|
|
1076
|
+
|
|
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
|
+
* @example
|
|
1106
|
+
* // Filter elements
|
|
1107
|
+
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
1108
|
+
* const evens = heap.filter(x => x % 2 === 0);
|
|
1109
|
+
* console.log(evens.size); // 2;
|
|
1110
|
+
*/
|
|
570
1111
|
filter(callback, thisArg) {
|
|
571
1112
|
const out = this._createInstance();
|
|
572
1113
|
let i = 0;
|
|
@@ -580,15 +1121,49 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
580
1121
|
return out;
|
|
581
1122
|
}
|
|
582
1123
|
/**
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
1124
|
+
* Map elements into a new heap of possibly different element type.
|
|
1125
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1126
|
+
* @template EM
|
|
1127
|
+
* @template RM
|
|
1128
|
+
* @param callback - Mapping function (element, index, heap) → newElement.
|
|
1129
|
+
* @param options - Options for the output heap, including comparator for EM.
|
|
1130
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1131
|
+
* @returns A new heap with mapped elements.
|
|
1132
|
+
|
|
1133
|
+
|
|
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
|
+
* @example
|
|
1162
|
+
* // Transform elements
|
|
1163
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
1164
|
+
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
1165
|
+
* console.log(doubled.peek()); // 2;
|
|
1166
|
+
*/
|
|
592
1167
|
map(callback, options, thisArg) {
|
|
593
1168
|
const { comparator, toElementFn, ...rest } = options ?? {};
|
|
594
1169
|
if (!comparator) throw new TypeError(ERR.comparatorRequired("Heap.map"));
|