max-priority-queue-typed 2.4.5 → 2.5.0
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 +400 -119
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +399 -118
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +400 -119
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +399 -118
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +429 -78
- package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +212 -32
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
- package/dist/types/data-structures/graph/directed-graph.d.ts +219 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +204 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
- package/dist/types/data-structures/heap/heap.d.ts +287 -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 +286 -44
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +331 -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 +272 -65
- package/dist/types/data-structures/queue/queue.d.ts +211 -42
- package/dist/types/data-structures/stack/stack.d.ts +174 -32
- package/dist/types/data-structures/trie/trie.d.ts +213 -43
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/umd/max-priority-queue-typed.js +397 -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/iterable-element-base.ts +4 -5
- package/src/data-structures/binary-tree/avl-tree.ts +134 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +302 -247
- package/src/data-structures/binary-tree/binary-tree.ts +429 -79
- package/src/data-structures/binary-tree/bst.ts +335 -34
- package/src/data-structures/binary-tree/red-black-tree.ts +290 -97
- package/src/data-structures/binary-tree/segment-tree.ts +372 -248
- package/src/data-structures/binary-tree/tree-map.ts +1284 -6
- package/src/data-structures/binary-tree/tree-multi-map.ts +1094 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +858 -65
- package/src/data-structures/binary-tree/tree-set.ts +1136 -9
- package/src/data-structures/graph/directed-graph.ts +219 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +204 -59
- package/src/data-structures/hash/hash-map.ts +230 -77
- package/src/data-structures/heap/heap.ts +287 -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 +286 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
- package/src/data-structures/matrix/matrix.ts +416 -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 +272 -65
- package/src/data-structures/queue/queue.ts +211 -42
- package/src/data-structures/stack/stack.ts +174 -32
- package/src/data-structures/trie/trie.ts +213 -43
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
|
@@ -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,30 @@ 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
|
+
* @example
|
|
354
|
+
* // Track heap capacity
|
|
355
|
+
* const heap = new Heap<number>();
|
|
356
|
+
* console.log(heap.size); // 0;
|
|
357
|
+
* heap.add(10);
|
|
358
|
+
* heap.add(20);
|
|
359
|
+
* console.log(heap.size); // 2;
|
|
360
|
+
* heap.poll();
|
|
361
|
+
* console.log(heap.size); // 1;
|
|
362
|
+
*/
|
|
343
363
|
get size() {
|
|
344
364
|
return this.elements.length;
|
|
345
365
|
}
|
|
@@ -378,21 +398,61 @@ var maxPriorityQueueTyped = (() => {
|
|
|
378
398
|
return new _Heap(elements, options);
|
|
379
399
|
}
|
|
380
400
|
/**
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
401
|
+
* Insert an element.
|
|
402
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
403
|
+
* @param element - Element to insert.
|
|
404
|
+
* @returns True.
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
* @example
|
|
417
|
+
* // basic Heap creation and add operation
|
|
418
|
+
* // Create a min heap (default)
|
|
419
|
+
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
420
|
+
*
|
|
421
|
+
* // Verify size
|
|
422
|
+
* console.log(minHeap.size); // 6;
|
|
423
|
+
*
|
|
424
|
+
* // Add new element
|
|
425
|
+
* minHeap.add(4);
|
|
426
|
+
* console.log(minHeap.size); // 7;
|
|
427
|
+
*
|
|
428
|
+
* // Min heap property: smallest element at root
|
|
429
|
+
* const min = minHeap.peek();
|
|
430
|
+
* console.log(min); // 1;
|
|
431
|
+
*/
|
|
386
432
|
add(element) {
|
|
387
433
|
this._elements.push(element);
|
|
388
434
|
return this._bubbleUp(this.elements.length - 1);
|
|
389
435
|
}
|
|
390
436
|
/**
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
437
|
+
* Insert many elements from an iterable.
|
|
438
|
+
* @remarks Time O(N log N), Space O(1)
|
|
439
|
+
* @param elements - Iterable of elements or raw values.
|
|
440
|
+
* @returns Array of per-element success flags.
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
* @example
|
|
450
|
+
* // Add multiple elements
|
|
451
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
452
|
+
* heap.addMany([5, 3, 7, 1]);
|
|
453
|
+
* console.log(heap.peek()); // 1;
|
|
454
|
+
* console.log(heap.size); // 4;
|
|
455
|
+
*/
|
|
396
456
|
addMany(elements) {
|
|
397
457
|
const flags = [];
|
|
398
458
|
for (const el of elements) {
|
|
@@ -407,10 +467,46 @@ var maxPriorityQueueTyped = (() => {
|
|
|
407
467
|
return flags;
|
|
408
468
|
}
|
|
409
469
|
/**
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
470
|
+
* Remove and return the top element.
|
|
471
|
+
* @remarks Time O(log N), Space O(1)
|
|
472
|
+
* @returns Top element or undefined.
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
* @example
|
|
485
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
486
|
+
* interface Task {
|
|
487
|
+
* id: number;
|
|
488
|
+
* priority: number;
|
|
489
|
+
* name: string;
|
|
490
|
+
* }
|
|
491
|
+
*
|
|
492
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
493
|
+
* const tasks: Task[] = [
|
|
494
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
495
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
496
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
497
|
+
* ];
|
|
498
|
+
*
|
|
499
|
+
* const maxHeap = new Heap(tasks, {
|
|
500
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
501
|
+
* });
|
|
502
|
+
*
|
|
503
|
+
* console.log(maxHeap.size); // 3;
|
|
504
|
+
*
|
|
505
|
+
* // Peek returns highest priority task
|
|
506
|
+
* const topTask = maxHeap.peek();
|
|
507
|
+
* console.log(topTask?.priority); // 8;
|
|
508
|
+
* console.log(topTask?.name); // 'Alert';
|
|
509
|
+
*/
|
|
414
510
|
poll() {
|
|
415
511
|
if (this.elements.length === 0) return;
|
|
416
512
|
const value = this.elements[0];
|
|
@@ -422,26 +518,125 @@ var maxPriorityQueueTyped = (() => {
|
|
|
422
518
|
return value;
|
|
423
519
|
}
|
|
424
520
|
/**
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
521
|
+
* Get the current top element without removing it.
|
|
522
|
+
* @remarks Time O(1), Space O(1)
|
|
523
|
+
* @returns Top element or undefined.
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
* @example
|
|
536
|
+
* // Heap for event processing with priority
|
|
537
|
+
* interface Event {
|
|
538
|
+
* id: number;
|
|
539
|
+
* type: 'critical' | 'warning' | 'info';
|
|
540
|
+
* timestamp: number;
|
|
541
|
+
* message: string;
|
|
542
|
+
* }
|
|
543
|
+
*
|
|
544
|
+
* // Custom priority: critical > warning > info
|
|
545
|
+
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
546
|
+
*
|
|
547
|
+
* const eventHeap = new Heap<Event>([], {
|
|
548
|
+
* comparator: (a: Event, b: Event) => {
|
|
549
|
+
* const priorityA = priorityMap[a.type];
|
|
550
|
+
* const priorityB = priorityMap[b.type];
|
|
551
|
+
* return priorityB - priorityA; // Higher priority first
|
|
552
|
+
* }
|
|
553
|
+
* });
|
|
554
|
+
*
|
|
555
|
+
* // Add events in random order
|
|
556
|
+
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
557
|
+
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
558
|
+
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
559
|
+
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
560
|
+
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
561
|
+
*
|
|
562
|
+
* console.log(eventHeap.size); // 5;
|
|
563
|
+
*
|
|
564
|
+
* // Process events by priority (critical first)
|
|
565
|
+
* const processedOrder: Event[] = [];
|
|
566
|
+
* while (eventHeap.size > 0) {
|
|
567
|
+
* const event = eventHeap.poll();
|
|
568
|
+
* if (event) {
|
|
569
|
+
* processedOrder.push(event);
|
|
570
|
+
* }
|
|
571
|
+
* }
|
|
572
|
+
*
|
|
573
|
+
* // Verify critical events came first
|
|
574
|
+
* console.log(processedOrder[0].type); // 'critical';
|
|
575
|
+
* console.log(processedOrder[1].type); // 'critical';
|
|
576
|
+
* console.log(processedOrder[2].type); // 'warning';
|
|
577
|
+
* console.log(processedOrder[3].type); // 'info';
|
|
578
|
+
* console.log(processedOrder[4].type); // 'info';
|
|
579
|
+
*
|
|
580
|
+
* // Verify O(log n) operations
|
|
581
|
+
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
582
|
+
*
|
|
583
|
+
* // Add - O(log n)
|
|
584
|
+
* newHeap.add(2);
|
|
585
|
+
* console.log(newHeap.size); // 5;
|
|
586
|
+
*
|
|
587
|
+
* // Poll - O(log n)
|
|
588
|
+
* const removed = newHeap.poll();
|
|
589
|
+
* console.log(removed); // 1;
|
|
590
|
+
*
|
|
591
|
+
* // Peek - O(1)
|
|
592
|
+
* const top = newHeap.peek();
|
|
593
|
+
* console.log(top); // 2;
|
|
594
|
+
*/
|
|
429
595
|
peek() {
|
|
430
596
|
return this.elements[0];
|
|
431
597
|
}
|
|
432
598
|
/**
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
599
|
+
* Check whether the heap is empty.
|
|
600
|
+
* @remarks Time O(1), Space O(1)
|
|
601
|
+
* @returns True if size is 0.
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
* @example
|
|
612
|
+
* // Check if heap is empty
|
|
613
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
614
|
+
* console.log(heap.isEmpty()); // true;
|
|
615
|
+
* heap.add(1);
|
|
616
|
+
* console.log(heap.isEmpty()); // false;
|
|
617
|
+
*/
|
|
437
618
|
isEmpty() {
|
|
438
619
|
return this.size === 0;
|
|
439
620
|
}
|
|
440
621
|
/**
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
622
|
+
* Remove all elements.
|
|
623
|
+
* @remarks Time O(1), Space O(1)
|
|
624
|
+
* @returns void
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
* @example
|
|
635
|
+
* // Remove all elements
|
|
636
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
637
|
+
* heap.clear();
|
|
638
|
+
* console.log(heap.isEmpty()); // true;
|
|
639
|
+
*/
|
|
445
640
|
clear() {
|
|
446
641
|
this._elements = [];
|
|
447
642
|
}
|
|
@@ -456,21 +651,41 @@ var maxPriorityQueueTyped = (() => {
|
|
|
456
651
|
return this.fix();
|
|
457
652
|
}
|
|
458
653
|
/**
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
654
|
+
* Check if an equal element exists in the heap.
|
|
655
|
+
* @remarks Time O(N), Space O(1)
|
|
656
|
+
* @param element - Element to search for.
|
|
657
|
+
* @returns True if found.
|
|
658
|
+
|
|
659
|
+
|
|
660
|
+
* @example
|
|
661
|
+
* // Check element existence
|
|
662
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
663
|
+
* console.log(heap.has(1)); // true;
|
|
664
|
+
* console.log(heap.has(99)); // false;
|
|
665
|
+
*/
|
|
464
666
|
has(element) {
|
|
465
667
|
for (const el of this.elements) if (this._equals(el, element)) return true;
|
|
466
668
|
return false;
|
|
467
669
|
}
|
|
468
670
|
/**
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
671
|
+
* Delete one occurrence of an element.
|
|
672
|
+
* @remarks Time O(N), Space O(1)
|
|
673
|
+
* @param element - Element to delete.
|
|
674
|
+
* @returns True if an element was removed.
|
|
675
|
+
|
|
676
|
+
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
* @example
|
|
684
|
+
* // Remove specific element
|
|
685
|
+
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
686
|
+
* heap.delete(4);
|
|
687
|
+
* console.log(heap.toArray().includes(4)); // false;
|
|
688
|
+
*/
|
|
474
689
|
delete(element) {
|
|
475
690
|
let index = -1;
|
|
476
691
|
for (let i = 0; i < this.elements.length; i++) {
|
|
@@ -528,11 +743,18 @@ var maxPriorityQueueTyped = (() => {
|
|
|
528
743
|
return this;
|
|
529
744
|
}
|
|
530
745
|
/**
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
746
|
+
* Traverse the binary heap as a complete binary tree and collect elements.
|
|
747
|
+
* @remarks Time O(N), Space O(H)
|
|
748
|
+
* @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
749
|
+
* @returns Array of visited elements.
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
* @example
|
|
753
|
+
* // Depth-first traversal
|
|
754
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
755
|
+
* const result = heap.dfs('IN');
|
|
756
|
+
* console.log(result.length); // 3;
|
|
757
|
+
*/
|
|
536
758
|
dfs(order = "PRE") {
|
|
537
759
|
const result = [];
|
|
538
760
|
const _dfs = (index) => {
|
|
@@ -569,10 +791,26 @@ var maxPriorityQueueTyped = (() => {
|
|
|
569
791
|
return results;
|
|
570
792
|
}
|
|
571
793
|
/**
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
794
|
+
* Return all elements in ascending order by repeatedly polling.
|
|
795
|
+
* @remarks Time O(N log N), Space O(N)
|
|
796
|
+
* @returns Sorted array of elements.
|
|
797
|
+
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
* @example
|
|
809
|
+
* // Sort elements using heap
|
|
810
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
811
|
+
* const sorted = heap.sort();
|
|
812
|
+
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
813
|
+
*/
|
|
576
814
|
sort() {
|
|
577
815
|
const visited = [];
|
|
578
816
|
const cloned = this._createInstance();
|
|
@@ -584,22 +822,52 @@ var maxPriorityQueueTyped = (() => {
|
|
|
584
822
|
return visited;
|
|
585
823
|
}
|
|
586
824
|
/**
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
825
|
+
* Deep clone this heap.
|
|
826
|
+
* @remarks Time O(N), Space O(N)
|
|
827
|
+
* @returns A new heap with the same elements.
|
|
828
|
+
|
|
829
|
+
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
* @example
|
|
838
|
+
* // Create independent copy
|
|
839
|
+
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
840
|
+
* const copy = heap.clone();
|
|
841
|
+
* copy.poll();
|
|
842
|
+
* console.log(heap.size); // 3;
|
|
843
|
+
* console.log(copy.size); // 2;
|
|
844
|
+
*/
|
|
591
845
|
clone() {
|
|
592
846
|
const next = this._createInstance();
|
|
593
847
|
for (const x of this.elements) next.add(x);
|
|
594
848
|
return next;
|
|
595
849
|
}
|
|
596
850
|
/**
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
851
|
+
* Filter elements into a new heap of the same class.
|
|
852
|
+
* @remarks Time O(N log N), Space O(N)
|
|
853
|
+
* @param callback - Predicate (element, index, heap) → boolean to keep element.
|
|
854
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
855
|
+
* @returns A new heap with the kept elements.
|
|
856
|
+
|
|
857
|
+
|
|
858
|
+
|
|
859
|
+
|
|
860
|
+
|
|
861
|
+
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
* @example
|
|
866
|
+
* // Filter elements
|
|
867
|
+
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
868
|
+
* const evens = heap.filter(x => x % 2 === 0);
|
|
869
|
+
* console.log(evens.size); // 2;
|
|
870
|
+
*/
|
|
603
871
|
filter(callback, thisArg) {
|
|
604
872
|
const out = this._createInstance();
|
|
605
873
|
let i = 0;
|
|
@@ -613,15 +881,28 @@ var maxPriorityQueueTyped = (() => {
|
|
|
613
881
|
return out;
|
|
614
882
|
}
|
|
615
883
|
/**
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
884
|
+
* Map elements into a new heap of possibly different element type.
|
|
885
|
+
* @remarks Time O(N log N), Space O(N)
|
|
886
|
+
* @template EM
|
|
887
|
+
* @template RM
|
|
888
|
+
* @param callback - Mapping function (element, index, heap) → newElement.
|
|
889
|
+
* @param options - Options for the output heap, including comparator for EM.
|
|
890
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
891
|
+
* @returns A new heap with mapped elements.
|
|
892
|
+
|
|
893
|
+
|
|
894
|
+
|
|
895
|
+
|
|
896
|
+
|
|
897
|
+
|
|
898
|
+
|
|
899
|
+
|
|
900
|
+
* @example
|
|
901
|
+
* // Transform elements
|
|
902
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
903
|
+
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
904
|
+
* console.log(doubled.peek()); // 2;
|
|
905
|
+
*/
|
|
625
906
|
map(callback, options, thisArg) {
|
|
626
907
|
const { comparator, toElementFn, ...rest } = options != null ? options : {};
|
|
627
908
|
if (!comparator) throw new TypeError(ERR.comparatorRequired("Heap.map"));
|