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
|
@@ -33,52 +33,6 @@ var maxPriorityQueueTyped = (() => {
|
|
|
33
33
|
Range: () => Range
|
|
34
34
|
});
|
|
35
35
|
|
|
36
|
-
// src/common/error.ts
|
|
37
|
-
var ERR = {
|
|
38
|
-
// Range / index
|
|
39
|
-
indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
|
|
40
|
-
invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
|
|
41
|
-
// Type / argument
|
|
42
|
-
invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
43
|
-
comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
|
|
44
|
-
invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
45
|
-
notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
|
|
46
|
-
invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
|
|
47
|
-
invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
|
|
48
|
-
invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
|
|
49
|
-
reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
|
|
50
|
-
callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
|
|
51
|
-
// State / operation
|
|
52
|
-
invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
53
|
-
// Matrix
|
|
54
|
-
matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
|
|
55
|
-
matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
|
|
56
|
-
matrixNotSquare: () => "Matrix: Must be square for inversion.",
|
|
57
|
-
matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
|
|
58
|
-
matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
// src/common/index.ts
|
|
62
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
63
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
64
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
65
|
-
return DFSOperation2;
|
|
66
|
-
})(DFSOperation || {});
|
|
67
|
-
var Range = class {
|
|
68
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
69
|
-
this.low = low;
|
|
70
|
-
this.high = high;
|
|
71
|
-
this.includeLow = includeLow;
|
|
72
|
-
this.includeHigh = includeHigh;
|
|
73
|
-
}
|
|
74
|
-
// Determine whether a key is within the range
|
|
75
|
-
isInRange(key, comparator) {
|
|
76
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
77
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
78
|
-
return lowCheck && highCheck;
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
|
|
82
36
|
// src/data-structures/base/iterable-element-base.ts
|
|
83
37
|
var IterableElementBase = class {
|
|
84
38
|
/**
|
|
@@ -101,7 +55,7 @@ var maxPriorityQueueTyped = (() => {
|
|
|
101
55
|
if (options) {
|
|
102
56
|
const { toElementFn } = options;
|
|
103
57
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
104
|
-
else if (toElementFn) throw new TypeError(
|
|
58
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
105
59
|
}
|
|
106
60
|
}
|
|
107
61
|
/**
|
|
@@ -257,7 +211,7 @@ var maxPriorityQueueTyped = (() => {
|
|
|
257
211
|
acc = initialValue;
|
|
258
212
|
} else {
|
|
259
213
|
const first = iter.next();
|
|
260
|
-
if (first.done) throw new TypeError(
|
|
214
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
261
215
|
acc = first.value;
|
|
262
216
|
index = 1;
|
|
263
217
|
}
|
|
@@ -299,6 +253,52 @@ var maxPriorityQueueTyped = (() => {
|
|
|
299
253
|
}
|
|
300
254
|
};
|
|
301
255
|
|
|
256
|
+
// src/common/error.ts
|
|
257
|
+
var ERR = {
|
|
258
|
+
// Range / index
|
|
259
|
+
indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
|
|
260
|
+
invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
|
|
261
|
+
// Type / argument
|
|
262
|
+
invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
263
|
+
comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
|
|
264
|
+
invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
265
|
+
notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
|
|
266
|
+
invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
|
|
267
|
+
invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
|
|
268
|
+
invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
|
|
269
|
+
reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
|
|
270
|
+
callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
|
|
271
|
+
// State / operation
|
|
272
|
+
invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
273
|
+
// Matrix
|
|
274
|
+
matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
|
|
275
|
+
matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
|
|
276
|
+
matrixNotSquare: () => "Matrix: Must be square for inversion.",
|
|
277
|
+
matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
|
|
278
|
+
matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
// src/common/index.ts
|
|
282
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
283
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
284
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
285
|
+
return DFSOperation2;
|
|
286
|
+
})(DFSOperation || {});
|
|
287
|
+
var Range = class {
|
|
288
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
289
|
+
this.low = low;
|
|
290
|
+
this.high = high;
|
|
291
|
+
this.includeLow = includeLow;
|
|
292
|
+
this.includeHigh = includeHigh;
|
|
293
|
+
}
|
|
294
|
+
// Determine whether a key is within the range
|
|
295
|
+
isInRange(key, comparator) {
|
|
296
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
297
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
298
|
+
return lowCheck && highCheck;
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
|
|
302
302
|
// src/data-structures/heap/heap.ts
|
|
303
303
|
var Heap = class _Heap extends IterableElementBase {
|
|
304
304
|
/**
|
|
@@ -336,10 +336,51 @@ var maxPriorityQueueTyped = (() => {
|
|
|
336
336
|
return this._elements;
|
|
337
337
|
}
|
|
338
338
|
/**
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
339
|
+
* Get the number of elements.
|
|
340
|
+
* @remarks Time O(1), Space O(1)
|
|
341
|
+
* @returns Heap size.
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
* @example
|
|
375
|
+
* // Track heap capacity
|
|
376
|
+
* const heap = new Heap<number>();
|
|
377
|
+
* console.log(heap.size); // 0;
|
|
378
|
+
* heap.add(10);
|
|
379
|
+
* heap.add(20);
|
|
380
|
+
* console.log(heap.size); // 2;
|
|
381
|
+
* heap.poll();
|
|
382
|
+
* console.log(heap.size); // 1;
|
|
383
|
+
*/
|
|
343
384
|
get size() {
|
|
344
385
|
return this.elements.length;
|
|
345
386
|
}
|
|
@@ -378,21 +419,103 @@ var maxPriorityQueueTyped = (() => {
|
|
|
378
419
|
return new _Heap(elements, options);
|
|
379
420
|
}
|
|
380
421
|
/**
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
422
|
+
* Insert an element.
|
|
423
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
424
|
+
* @param element - Element to insert.
|
|
425
|
+
* @returns True.
|
|
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
|
+
|
|
457
|
+
|
|
458
|
+
* @example
|
|
459
|
+
* // basic Heap creation and add operation
|
|
460
|
+
* // Create a min heap (default)
|
|
461
|
+
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
462
|
+
*
|
|
463
|
+
* // Verify size
|
|
464
|
+
* console.log(minHeap.size); // 6;
|
|
465
|
+
*
|
|
466
|
+
* // Add new element
|
|
467
|
+
* minHeap.add(4);
|
|
468
|
+
* console.log(minHeap.size); // 7;
|
|
469
|
+
*
|
|
470
|
+
* // Min heap property: smallest element at root
|
|
471
|
+
* const min = minHeap.peek();
|
|
472
|
+
* console.log(min); // 1;
|
|
473
|
+
*/
|
|
386
474
|
add(element) {
|
|
387
475
|
this._elements.push(element);
|
|
388
476
|
return this._bubbleUp(this.elements.length - 1);
|
|
389
477
|
}
|
|
390
478
|
/**
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
479
|
+
* Insert many elements from an iterable.
|
|
480
|
+
* @remarks Time O(N log N), Space O(1)
|
|
481
|
+
* @param elements - Iterable of elements or raw values.
|
|
482
|
+
* @returns Array of per-element success flags.
|
|
483
|
+
|
|
484
|
+
|
|
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
|
+
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
* @example
|
|
513
|
+
* // Add multiple elements
|
|
514
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
515
|
+
* heap.addMany([5, 3, 7, 1]);
|
|
516
|
+
* console.log(heap.peek()); // 1;
|
|
517
|
+
* console.log(heap.size); // 4;
|
|
518
|
+
*/
|
|
396
519
|
addMany(elements) {
|
|
397
520
|
const flags = [];
|
|
398
521
|
for (const el of elements) {
|
|
@@ -407,10 +530,67 @@ var maxPriorityQueueTyped = (() => {
|
|
|
407
530
|
return flags;
|
|
408
531
|
}
|
|
409
532
|
/**
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
533
|
+
* Remove and return the top element.
|
|
534
|
+
* @remarks Time O(log N), Space O(1)
|
|
535
|
+
* @returns Top element or undefined.
|
|
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
|
+
* @example
|
|
569
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
570
|
+
* interface Task {
|
|
571
|
+
* id: number;
|
|
572
|
+
* priority: number;
|
|
573
|
+
* name: string;
|
|
574
|
+
* }
|
|
575
|
+
*
|
|
576
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
577
|
+
* const tasks: Task[] = [
|
|
578
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
579
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
580
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
581
|
+
* ];
|
|
582
|
+
*
|
|
583
|
+
* const maxHeap = new Heap(tasks, {
|
|
584
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
585
|
+
* });
|
|
586
|
+
*
|
|
587
|
+
* console.log(maxHeap.size); // 3;
|
|
588
|
+
*
|
|
589
|
+
* // Peek returns highest priority task
|
|
590
|
+
* const topTask = maxHeap.peek();
|
|
591
|
+
* console.log(topTask?.priority); // 8;
|
|
592
|
+
* console.log(topTask?.name); // 'Alert';
|
|
593
|
+
*/
|
|
414
594
|
poll() {
|
|
415
595
|
if (this.elements.length === 0) return;
|
|
416
596
|
const value = this.elements[0];
|
|
@@ -422,26 +602,188 @@ var maxPriorityQueueTyped = (() => {
|
|
|
422
602
|
return value;
|
|
423
603
|
}
|
|
424
604
|
/**
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
605
|
+
* Get the current top element without removing it.
|
|
606
|
+
* @remarks Time O(1), Space O(1)
|
|
607
|
+
* @returns Top element or undefined.
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
* @example
|
|
641
|
+
* // Heap for event processing with priority
|
|
642
|
+
* interface Event {
|
|
643
|
+
* id: number;
|
|
644
|
+
* type: 'critical' | 'warning' | 'info';
|
|
645
|
+
* timestamp: number;
|
|
646
|
+
* message: string;
|
|
647
|
+
* }
|
|
648
|
+
*
|
|
649
|
+
* // Custom priority: critical > warning > info
|
|
650
|
+
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
651
|
+
*
|
|
652
|
+
* const eventHeap = new Heap<Event>([], {
|
|
653
|
+
* comparator: (a: Event, b: Event) => {
|
|
654
|
+
* const priorityA = priorityMap[a.type];
|
|
655
|
+
* const priorityB = priorityMap[b.type];
|
|
656
|
+
* return priorityB - priorityA; // Higher priority first
|
|
657
|
+
* }
|
|
658
|
+
* });
|
|
659
|
+
*
|
|
660
|
+
* // Add events in random order
|
|
661
|
+
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
662
|
+
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
663
|
+
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
664
|
+
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
665
|
+
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
666
|
+
*
|
|
667
|
+
* console.log(eventHeap.size); // 5;
|
|
668
|
+
*
|
|
669
|
+
* // Process events by priority (critical first)
|
|
670
|
+
* const processedOrder: Event[] = [];
|
|
671
|
+
* while (eventHeap.size > 0) {
|
|
672
|
+
* const event = eventHeap.poll();
|
|
673
|
+
* if (event) {
|
|
674
|
+
* processedOrder.push(event);
|
|
675
|
+
* }
|
|
676
|
+
* }
|
|
677
|
+
*
|
|
678
|
+
* // Verify critical events came first
|
|
679
|
+
* console.log(processedOrder[0].type); // 'critical';
|
|
680
|
+
* console.log(processedOrder[1].type); // 'critical';
|
|
681
|
+
* console.log(processedOrder[2].type); // 'warning';
|
|
682
|
+
* console.log(processedOrder[3].type); // 'info';
|
|
683
|
+
* console.log(processedOrder[4].type); // 'info';
|
|
684
|
+
*
|
|
685
|
+
* // Verify O(log n) operations
|
|
686
|
+
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
687
|
+
*
|
|
688
|
+
* // Add - O(log n)
|
|
689
|
+
* newHeap.add(2);
|
|
690
|
+
* console.log(newHeap.size); // 5;
|
|
691
|
+
*
|
|
692
|
+
* // Poll - O(log n)
|
|
693
|
+
* const removed = newHeap.poll();
|
|
694
|
+
* console.log(removed); // 1;
|
|
695
|
+
*
|
|
696
|
+
* // Peek - O(1)
|
|
697
|
+
* const top = newHeap.peek();
|
|
698
|
+
* console.log(top); // 2;
|
|
699
|
+
*/
|
|
429
700
|
peek() {
|
|
430
701
|
return this.elements[0];
|
|
431
702
|
}
|
|
432
703
|
/**
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
704
|
+
* Check whether the heap is empty.
|
|
705
|
+
* @remarks Time O(1), Space O(1)
|
|
706
|
+
* @returns True if size is 0.
|
|
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
|
+
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
* @example
|
|
738
|
+
* // Check if heap is empty
|
|
739
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
740
|
+
* console.log(heap.isEmpty()); // true;
|
|
741
|
+
* heap.add(1);
|
|
742
|
+
* console.log(heap.isEmpty()); // false;
|
|
743
|
+
*/
|
|
437
744
|
isEmpty() {
|
|
438
745
|
return this.size === 0;
|
|
439
746
|
}
|
|
440
747
|
/**
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
748
|
+
* Remove all elements.
|
|
749
|
+
* @remarks Time O(1), Space O(1)
|
|
750
|
+
* @returns void
|
|
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
|
+
* // Remove all elements
|
|
783
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
784
|
+
* heap.clear();
|
|
785
|
+
* console.log(heap.isEmpty()); // true;
|
|
786
|
+
*/
|
|
445
787
|
clear() {
|
|
446
788
|
this._elements = [];
|
|
447
789
|
}
|
|
@@ -456,21 +798,83 @@ var maxPriorityQueueTyped = (() => {
|
|
|
456
798
|
return this.fix();
|
|
457
799
|
}
|
|
458
800
|
/**
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
801
|
+
* Check if an equal element exists in the heap.
|
|
802
|
+
* @remarks Time O(N), Space O(1)
|
|
803
|
+
* @param element - Element to search for.
|
|
804
|
+
* @returns True if found.
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
|
|
814
|
+
|
|
815
|
+
|
|
816
|
+
|
|
817
|
+
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
* @example
|
|
829
|
+
* // Check element existence
|
|
830
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
831
|
+
* console.log(heap.has(1)); // true;
|
|
832
|
+
* console.log(heap.has(99)); // false;
|
|
833
|
+
*/
|
|
464
834
|
has(element) {
|
|
465
835
|
for (const el of this.elements) if (this._equals(el, element)) return true;
|
|
466
836
|
return false;
|
|
467
837
|
}
|
|
468
838
|
/**
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
839
|
+
* Delete one occurrence of an element.
|
|
840
|
+
* @remarks Time O(N), Space O(1)
|
|
841
|
+
* @param element - Element to delete.
|
|
842
|
+
* @returns True if an element was removed.
|
|
843
|
+
|
|
844
|
+
|
|
845
|
+
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
|
|
849
|
+
|
|
850
|
+
|
|
851
|
+
|
|
852
|
+
|
|
853
|
+
|
|
854
|
+
|
|
855
|
+
|
|
856
|
+
|
|
857
|
+
|
|
858
|
+
|
|
859
|
+
|
|
860
|
+
|
|
861
|
+
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
|
|
872
|
+
* @example
|
|
873
|
+
* // Remove specific element
|
|
874
|
+
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
875
|
+
* heap.delete(4);
|
|
876
|
+
* console.log(heap.toArray().includes(4)); // false;
|
|
877
|
+
*/
|
|
474
878
|
delete(element) {
|
|
475
879
|
let index = -1;
|
|
476
880
|
for (let i = 0; i < this.elements.length; i++) {
|
|
@@ -528,11 +932,39 @@ var maxPriorityQueueTyped = (() => {
|
|
|
528
932
|
return this;
|
|
529
933
|
}
|
|
530
934
|
/**
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
935
|
+
* Traverse the binary heap as a complete binary tree and collect elements.
|
|
936
|
+
* @remarks Time O(N), Space O(H)
|
|
937
|
+
* @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
938
|
+
* @returns Array of visited elements.
|
|
939
|
+
|
|
940
|
+
|
|
941
|
+
|
|
942
|
+
|
|
943
|
+
|
|
944
|
+
|
|
945
|
+
|
|
946
|
+
|
|
947
|
+
|
|
948
|
+
|
|
949
|
+
|
|
950
|
+
|
|
951
|
+
|
|
952
|
+
|
|
953
|
+
|
|
954
|
+
|
|
955
|
+
|
|
956
|
+
|
|
957
|
+
|
|
958
|
+
|
|
959
|
+
|
|
960
|
+
|
|
961
|
+
|
|
962
|
+
* @example
|
|
963
|
+
* // Depth-first traversal
|
|
964
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
965
|
+
* const result = heap.dfs('IN');
|
|
966
|
+
* console.log(result.length); // 3;
|
|
967
|
+
*/
|
|
536
968
|
dfs(order = "PRE") {
|
|
537
969
|
const result = [];
|
|
538
970
|
const _dfs = (index) => {
|
|
@@ -569,10 +1001,47 @@ var maxPriorityQueueTyped = (() => {
|
|
|
569
1001
|
return results;
|
|
570
1002
|
}
|
|
571
1003
|
/**
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
1004
|
+
* Return all elements in ascending order by repeatedly polling.
|
|
1005
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1006
|
+
* @returns Sorted array of elements.
|
|
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
|
+
|
|
1033
|
+
|
|
1034
|
+
|
|
1035
|
+
|
|
1036
|
+
|
|
1037
|
+
|
|
1038
|
+
|
|
1039
|
+
* @example
|
|
1040
|
+
* // Sort elements using heap
|
|
1041
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
1042
|
+
* const sorted = heap.sort();
|
|
1043
|
+
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
1044
|
+
*/
|
|
576
1045
|
sort() {
|
|
577
1046
|
const visited = [];
|
|
578
1047
|
const cloned = this._createInstance();
|
|
@@ -584,22 +1053,94 @@ var maxPriorityQueueTyped = (() => {
|
|
|
584
1053
|
return visited;
|
|
585
1054
|
}
|
|
586
1055
|
/**
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
1056
|
+
* Deep clone this heap.
|
|
1057
|
+
* @remarks Time O(N), Space O(N)
|
|
1058
|
+
* @returns A new heap with the same elements.
|
|
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
|
+
* @example
|
|
1090
|
+
* // Create independent copy
|
|
1091
|
+
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
1092
|
+
* const copy = heap.clone();
|
|
1093
|
+
* copy.poll();
|
|
1094
|
+
* console.log(heap.size); // 3;
|
|
1095
|
+
* console.log(copy.size); // 2;
|
|
1096
|
+
*/
|
|
591
1097
|
clone() {
|
|
592
1098
|
const next = this._createInstance();
|
|
593
1099
|
for (const x of this.elements) next.add(x);
|
|
594
1100
|
return next;
|
|
595
1101
|
}
|
|
596
1102
|
/**
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
1103
|
+
* Filter elements into a new heap of the same class.
|
|
1104
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1105
|
+
* @param callback - Predicate (element, index, heap) → boolean to keep element.
|
|
1106
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1107
|
+
* @returns A new heap with the kept elements.
|
|
1108
|
+
|
|
1109
|
+
|
|
1110
|
+
|
|
1111
|
+
|
|
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
|
+
* @example
|
|
1139
|
+
* // Filter elements
|
|
1140
|
+
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
1141
|
+
* const evens = heap.filter(x => x % 2 === 0);
|
|
1142
|
+
* console.log(evens.size); // 2;
|
|
1143
|
+
*/
|
|
603
1144
|
filter(callback, thisArg) {
|
|
604
1145
|
const out = this._createInstance();
|
|
605
1146
|
let i = 0;
|
|
@@ -613,15 +1154,49 @@ var maxPriorityQueueTyped = (() => {
|
|
|
613
1154
|
return out;
|
|
614
1155
|
}
|
|
615
1156
|
/**
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
1157
|
+
* Map elements into a new heap of possibly different element type.
|
|
1158
|
+
* @remarks Time O(N log N), Space O(N)
|
|
1159
|
+
* @template EM
|
|
1160
|
+
* @template RM
|
|
1161
|
+
* @param callback - Mapping function (element, index, heap) → newElement.
|
|
1162
|
+
* @param options - Options for the output heap, including comparator for EM.
|
|
1163
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1164
|
+
* @returns A new heap with mapped elements.
|
|
1165
|
+
|
|
1166
|
+
|
|
1167
|
+
|
|
1168
|
+
|
|
1169
|
+
|
|
1170
|
+
|
|
1171
|
+
|
|
1172
|
+
|
|
1173
|
+
|
|
1174
|
+
|
|
1175
|
+
|
|
1176
|
+
|
|
1177
|
+
|
|
1178
|
+
|
|
1179
|
+
|
|
1180
|
+
|
|
1181
|
+
|
|
1182
|
+
|
|
1183
|
+
|
|
1184
|
+
|
|
1185
|
+
|
|
1186
|
+
|
|
1187
|
+
|
|
1188
|
+
|
|
1189
|
+
|
|
1190
|
+
|
|
1191
|
+
|
|
1192
|
+
|
|
1193
|
+
|
|
1194
|
+
* @example
|
|
1195
|
+
* // Transform elements
|
|
1196
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
1197
|
+
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
1198
|
+
* console.log(doubled.peek()); // 2;
|
|
1199
|
+
*/
|
|
625
1200
|
map(callback, options, thisArg) {
|
|
626
1201
|
const { comparator, toElementFn, ...rest } = options != null ? options : {};
|
|
627
1202
|
if (!comparator) throw new TypeError(ERR.comparatorRequired("Heap.map"));
|