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 +66 -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/priority-queue-typed.js +397 -116
- package/dist/umd/priority-queue-typed.js.map +1 -1
- package/dist/umd/priority-queue-typed.min.js +1 -1
- package/dist/umd/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
|
@@ -36,52 +36,6 @@ var priorityQueueTyped = (() => {
|
|
|
36
36
|
Range: () => Range
|
|
37
37
|
});
|
|
38
38
|
|
|
39
|
-
// src/common/error.ts
|
|
40
|
-
var ERR = {
|
|
41
|
-
// Range / index
|
|
42
|
-
indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
|
|
43
|
-
invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
|
|
44
|
-
// Type / argument
|
|
45
|
-
invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
46
|
-
comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
|
|
47
|
-
invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
48
|
-
notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
|
|
49
|
-
invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
|
|
50
|
-
invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
|
|
51
|
-
invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
|
|
52
|
-
reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
|
|
53
|
-
callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
|
|
54
|
-
// State / operation
|
|
55
|
-
invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
56
|
-
// Matrix
|
|
57
|
-
matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
|
|
58
|
-
matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
|
|
59
|
-
matrixNotSquare: () => "Matrix: Must be square for inversion.",
|
|
60
|
-
matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
|
|
61
|
-
matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
// src/common/index.ts
|
|
65
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
66
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
67
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
68
|
-
return DFSOperation2;
|
|
69
|
-
})(DFSOperation || {});
|
|
70
|
-
var Range = class {
|
|
71
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
72
|
-
this.low = low;
|
|
73
|
-
this.high = high;
|
|
74
|
-
this.includeLow = includeLow;
|
|
75
|
-
this.includeHigh = includeHigh;
|
|
76
|
-
}
|
|
77
|
-
// Determine whether a key is within the range
|
|
78
|
-
isInRange(key, comparator) {
|
|
79
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
80
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
81
|
-
return lowCheck && highCheck;
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
|
|
85
39
|
// src/data-structures/base/iterable-element-base.ts
|
|
86
40
|
var IterableElementBase = class {
|
|
87
41
|
/**
|
|
@@ -104,7 +58,7 @@ var priorityQueueTyped = (() => {
|
|
|
104
58
|
if (options) {
|
|
105
59
|
const { toElementFn } = options;
|
|
106
60
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
107
|
-
else if (toElementFn) throw new TypeError(
|
|
61
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
108
62
|
}
|
|
109
63
|
}
|
|
110
64
|
/**
|
|
@@ -260,7 +214,7 @@ var priorityQueueTyped = (() => {
|
|
|
260
214
|
acc = initialValue;
|
|
261
215
|
} else {
|
|
262
216
|
const first = iter.next();
|
|
263
|
-
if (first.done) throw new TypeError(
|
|
217
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
264
218
|
acc = first.value;
|
|
265
219
|
index = 1;
|
|
266
220
|
}
|
|
@@ -302,6 +256,52 @@ var priorityQueueTyped = (() => {
|
|
|
302
256
|
}
|
|
303
257
|
};
|
|
304
258
|
|
|
259
|
+
// src/common/error.ts
|
|
260
|
+
var ERR = {
|
|
261
|
+
// Range / index
|
|
262
|
+
indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
|
|
263
|
+
invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
|
|
264
|
+
// Type / argument
|
|
265
|
+
invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
266
|
+
comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
|
|
267
|
+
invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
268
|
+
notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
|
|
269
|
+
invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
|
|
270
|
+
invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
|
|
271
|
+
invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
|
|
272
|
+
reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
|
|
273
|
+
callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
|
|
274
|
+
// State / operation
|
|
275
|
+
invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
276
|
+
// Matrix
|
|
277
|
+
matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
|
|
278
|
+
matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
|
|
279
|
+
matrixNotSquare: () => "Matrix: Must be square for inversion.",
|
|
280
|
+
matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
|
|
281
|
+
matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
// src/common/index.ts
|
|
285
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
286
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
287
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
288
|
+
return DFSOperation2;
|
|
289
|
+
})(DFSOperation || {});
|
|
290
|
+
var Range = class {
|
|
291
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
292
|
+
this.low = low;
|
|
293
|
+
this.high = high;
|
|
294
|
+
this.includeLow = includeLow;
|
|
295
|
+
this.includeHigh = includeHigh;
|
|
296
|
+
}
|
|
297
|
+
// Determine whether a key is within the range
|
|
298
|
+
isInRange(key, comparator) {
|
|
299
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
300
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
301
|
+
return lowCheck && highCheck;
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
|
|
305
305
|
// src/data-structures/heap/heap.ts
|
|
306
306
|
var Heap = class _Heap extends IterableElementBase {
|
|
307
307
|
/**
|
|
@@ -339,10 +339,30 @@ var priorityQueueTyped = (() => {
|
|
|
339
339
|
return this._elements;
|
|
340
340
|
}
|
|
341
341
|
/**
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
342
|
+
* Get the number of elements.
|
|
343
|
+
* @remarks Time O(1), Space O(1)
|
|
344
|
+
* @returns Heap size.
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
* @example
|
|
357
|
+
* // Track heap capacity
|
|
358
|
+
* const heap = new Heap<number>();
|
|
359
|
+
* console.log(heap.size); // 0;
|
|
360
|
+
* heap.add(10);
|
|
361
|
+
* heap.add(20);
|
|
362
|
+
* console.log(heap.size); // 2;
|
|
363
|
+
* heap.poll();
|
|
364
|
+
* console.log(heap.size); // 1;
|
|
365
|
+
*/
|
|
346
366
|
get size() {
|
|
347
367
|
return this.elements.length;
|
|
348
368
|
}
|
|
@@ -381,21 +401,61 @@ var priorityQueueTyped = (() => {
|
|
|
381
401
|
return new _Heap(elements, options);
|
|
382
402
|
}
|
|
383
403
|
/**
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
404
|
+
* Insert an element.
|
|
405
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
406
|
+
* @param element - Element to insert.
|
|
407
|
+
* @returns True.
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
* @example
|
|
420
|
+
* // basic Heap creation and add operation
|
|
421
|
+
* // Create a min heap (default)
|
|
422
|
+
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
423
|
+
*
|
|
424
|
+
* // Verify size
|
|
425
|
+
* console.log(minHeap.size); // 6;
|
|
426
|
+
*
|
|
427
|
+
* // Add new element
|
|
428
|
+
* minHeap.add(4);
|
|
429
|
+
* console.log(minHeap.size); // 7;
|
|
430
|
+
*
|
|
431
|
+
* // Min heap property: smallest element at root
|
|
432
|
+
* const min = minHeap.peek();
|
|
433
|
+
* console.log(min); // 1;
|
|
434
|
+
*/
|
|
389
435
|
add(element) {
|
|
390
436
|
this._elements.push(element);
|
|
391
437
|
return this._bubbleUp(this.elements.length - 1);
|
|
392
438
|
}
|
|
393
439
|
/**
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
440
|
+
* Insert many elements from an iterable.
|
|
441
|
+
* @remarks Time O(N log N), Space O(1)
|
|
442
|
+
* @param elements - Iterable of elements or raw values.
|
|
443
|
+
* @returns Array of per-element success flags.
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
* @example
|
|
453
|
+
* // Add multiple elements
|
|
454
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
455
|
+
* heap.addMany([5, 3, 7, 1]);
|
|
456
|
+
* console.log(heap.peek()); // 1;
|
|
457
|
+
* console.log(heap.size); // 4;
|
|
458
|
+
*/
|
|
399
459
|
addMany(elements) {
|
|
400
460
|
const flags = [];
|
|
401
461
|
for (const el of elements) {
|
|
@@ -410,10 +470,46 @@ var priorityQueueTyped = (() => {
|
|
|
410
470
|
return flags;
|
|
411
471
|
}
|
|
412
472
|
/**
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
473
|
+
* Remove and return the top element.
|
|
474
|
+
* @remarks Time O(log N), Space O(1)
|
|
475
|
+
* @returns Top element or undefined.
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
* @example
|
|
488
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
489
|
+
* interface Task {
|
|
490
|
+
* id: number;
|
|
491
|
+
* priority: number;
|
|
492
|
+
* name: string;
|
|
493
|
+
* }
|
|
494
|
+
*
|
|
495
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
496
|
+
* const tasks: Task[] = [
|
|
497
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
498
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
499
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
500
|
+
* ];
|
|
501
|
+
*
|
|
502
|
+
* const maxHeap = new Heap(tasks, {
|
|
503
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
504
|
+
* });
|
|
505
|
+
*
|
|
506
|
+
* console.log(maxHeap.size); // 3;
|
|
507
|
+
*
|
|
508
|
+
* // Peek returns highest priority task
|
|
509
|
+
* const topTask = maxHeap.peek();
|
|
510
|
+
* console.log(topTask?.priority); // 8;
|
|
511
|
+
* console.log(topTask?.name); // 'Alert';
|
|
512
|
+
*/
|
|
417
513
|
poll() {
|
|
418
514
|
if (this.elements.length === 0) return;
|
|
419
515
|
const value = this.elements[0];
|
|
@@ -425,26 +521,125 @@ var priorityQueueTyped = (() => {
|
|
|
425
521
|
return value;
|
|
426
522
|
}
|
|
427
523
|
/**
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
524
|
+
* Get the current top element without removing it.
|
|
525
|
+
* @remarks Time O(1), Space O(1)
|
|
526
|
+
* @returns Top element or undefined.
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
* @example
|
|
539
|
+
* // Heap for event processing with priority
|
|
540
|
+
* interface Event {
|
|
541
|
+
* id: number;
|
|
542
|
+
* type: 'critical' | 'warning' | 'info';
|
|
543
|
+
* timestamp: number;
|
|
544
|
+
* message: string;
|
|
545
|
+
* }
|
|
546
|
+
*
|
|
547
|
+
* // Custom priority: critical > warning > info
|
|
548
|
+
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
549
|
+
*
|
|
550
|
+
* const eventHeap = new Heap<Event>([], {
|
|
551
|
+
* comparator: (a: Event, b: Event) => {
|
|
552
|
+
* const priorityA = priorityMap[a.type];
|
|
553
|
+
* const priorityB = priorityMap[b.type];
|
|
554
|
+
* return priorityB - priorityA; // Higher priority first
|
|
555
|
+
* }
|
|
556
|
+
* });
|
|
557
|
+
*
|
|
558
|
+
* // Add events in random order
|
|
559
|
+
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
560
|
+
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
561
|
+
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
562
|
+
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
563
|
+
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
564
|
+
*
|
|
565
|
+
* console.log(eventHeap.size); // 5;
|
|
566
|
+
*
|
|
567
|
+
* // Process events by priority (critical first)
|
|
568
|
+
* const processedOrder: Event[] = [];
|
|
569
|
+
* while (eventHeap.size > 0) {
|
|
570
|
+
* const event = eventHeap.poll();
|
|
571
|
+
* if (event) {
|
|
572
|
+
* processedOrder.push(event);
|
|
573
|
+
* }
|
|
574
|
+
* }
|
|
575
|
+
*
|
|
576
|
+
* // Verify critical events came first
|
|
577
|
+
* console.log(processedOrder[0].type); // 'critical';
|
|
578
|
+
* console.log(processedOrder[1].type); // 'critical';
|
|
579
|
+
* console.log(processedOrder[2].type); // 'warning';
|
|
580
|
+
* console.log(processedOrder[3].type); // 'info';
|
|
581
|
+
* console.log(processedOrder[4].type); // 'info';
|
|
582
|
+
*
|
|
583
|
+
* // Verify O(log n) operations
|
|
584
|
+
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
585
|
+
*
|
|
586
|
+
* // Add - O(log n)
|
|
587
|
+
* newHeap.add(2);
|
|
588
|
+
* console.log(newHeap.size); // 5;
|
|
589
|
+
*
|
|
590
|
+
* // Poll - O(log n)
|
|
591
|
+
* const removed = newHeap.poll();
|
|
592
|
+
* console.log(removed); // 1;
|
|
593
|
+
*
|
|
594
|
+
* // Peek - O(1)
|
|
595
|
+
* const top = newHeap.peek();
|
|
596
|
+
* console.log(top); // 2;
|
|
597
|
+
*/
|
|
432
598
|
peek() {
|
|
433
599
|
return this.elements[0];
|
|
434
600
|
}
|
|
435
601
|
/**
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
602
|
+
* Check whether the heap is empty.
|
|
603
|
+
* @remarks Time O(1), Space O(1)
|
|
604
|
+
* @returns True if size is 0.
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
* @example
|
|
615
|
+
* // Check if heap is empty
|
|
616
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
617
|
+
* console.log(heap.isEmpty()); // true;
|
|
618
|
+
* heap.add(1);
|
|
619
|
+
* console.log(heap.isEmpty()); // false;
|
|
620
|
+
*/
|
|
440
621
|
isEmpty() {
|
|
441
622
|
return this.size === 0;
|
|
442
623
|
}
|
|
443
624
|
/**
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
625
|
+
* Remove all elements.
|
|
626
|
+
* @remarks Time O(1), Space O(1)
|
|
627
|
+
* @returns void
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
* @example
|
|
638
|
+
* // Remove all elements
|
|
639
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
640
|
+
* heap.clear();
|
|
641
|
+
* console.log(heap.isEmpty()); // true;
|
|
642
|
+
*/
|
|
448
643
|
clear() {
|
|
449
644
|
this._elements = [];
|
|
450
645
|
}
|
|
@@ -459,21 +654,41 @@ var priorityQueueTyped = (() => {
|
|
|
459
654
|
return this.fix();
|
|
460
655
|
}
|
|
461
656
|
/**
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
657
|
+
* Check if an equal element exists in the heap.
|
|
658
|
+
* @remarks Time O(N), Space O(1)
|
|
659
|
+
* @param element - Element to search for.
|
|
660
|
+
* @returns True if found.
|
|
661
|
+
|
|
662
|
+
|
|
663
|
+
* @example
|
|
664
|
+
* // Check element existence
|
|
665
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
666
|
+
* console.log(heap.has(1)); // true;
|
|
667
|
+
* console.log(heap.has(99)); // false;
|
|
668
|
+
*/
|
|
467
669
|
has(element) {
|
|
468
670
|
for (const el of this.elements) if (this._equals(el, element)) return true;
|
|
469
671
|
return false;
|
|
470
672
|
}
|
|
471
673
|
/**
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
674
|
+
* Delete one occurrence of an element.
|
|
675
|
+
* @remarks Time O(N), Space O(1)
|
|
676
|
+
* @param element - Element to delete.
|
|
677
|
+
* @returns True if an element was removed.
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
* @example
|
|
687
|
+
* // Remove specific element
|
|
688
|
+
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
689
|
+
* heap.delete(4);
|
|
690
|
+
* console.log(heap.toArray().includes(4)); // false;
|
|
691
|
+
*/
|
|
477
692
|
delete(element) {
|
|
478
693
|
let index = -1;
|
|
479
694
|
for (let i = 0; i < this.elements.length; i++) {
|
|
@@ -531,11 +746,18 @@ var priorityQueueTyped = (() => {
|
|
|
531
746
|
return this;
|
|
532
747
|
}
|
|
533
748
|
/**
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
749
|
+
* Traverse the binary heap as a complete binary tree and collect elements.
|
|
750
|
+
* @remarks Time O(N), Space O(H)
|
|
751
|
+
* @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
752
|
+
* @returns Array of visited elements.
|
|
753
|
+
|
|
754
|
+
|
|
755
|
+
* @example
|
|
756
|
+
* // Depth-first traversal
|
|
757
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
758
|
+
* const result = heap.dfs('IN');
|
|
759
|
+
* console.log(result.length); // 3;
|
|
760
|
+
*/
|
|
539
761
|
dfs(order = "PRE") {
|
|
540
762
|
const result = [];
|
|
541
763
|
const _dfs = (index) => {
|
|
@@ -572,10 +794,26 @@ var priorityQueueTyped = (() => {
|
|
|
572
794
|
return results;
|
|
573
795
|
}
|
|
574
796
|
/**
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
797
|
+
* Return all elements in ascending order by repeatedly polling.
|
|
798
|
+
* @remarks Time O(N log N), Space O(N)
|
|
799
|
+
* @returns Sorted array of elements.
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
* @example
|
|
812
|
+
* // Sort elements using heap
|
|
813
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
814
|
+
* const sorted = heap.sort();
|
|
815
|
+
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
816
|
+
*/
|
|
579
817
|
sort() {
|
|
580
818
|
const visited = [];
|
|
581
819
|
const cloned = this._createInstance();
|
|
@@ -587,22 +825,52 @@ var priorityQueueTyped = (() => {
|
|
|
587
825
|
return visited;
|
|
588
826
|
}
|
|
589
827
|
/**
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
828
|
+
* Deep clone this heap.
|
|
829
|
+
* @remarks Time O(N), Space O(N)
|
|
830
|
+
* @returns A new heap with the same elements.
|
|
831
|
+
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
|
|
839
|
+
|
|
840
|
+
* @example
|
|
841
|
+
* // Create independent copy
|
|
842
|
+
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
843
|
+
* const copy = heap.clone();
|
|
844
|
+
* copy.poll();
|
|
845
|
+
* console.log(heap.size); // 3;
|
|
846
|
+
* console.log(copy.size); // 2;
|
|
847
|
+
*/
|
|
594
848
|
clone() {
|
|
595
849
|
const next = this._createInstance();
|
|
596
850
|
for (const x of this.elements) next.add(x);
|
|
597
851
|
return next;
|
|
598
852
|
}
|
|
599
853
|
/**
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
854
|
+
* Filter elements into a new heap of the same class.
|
|
855
|
+
* @remarks Time O(N log N), Space O(N)
|
|
856
|
+
* @param callback - Predicate (element, index, heap) → boolean to keep element.
|
|
857
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
858
|
+
* @returns A new heap with the kept elements.
|
|
859
|
+
|
|
860
|
+
|
|
861
|
+
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
|
|
867
|
+
|
|
868
|
+
* @example
|
|
869
|
+
* // Filter elements
|
|
870
|
+
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
871
|
+
* const evens = heap.filter(x => x % 2 === 0);
|
|
872
|
+
* console.log(evens.size); // 2;
|
|
873
|
+
*/
|
|
606
874
|
filter(callback, thisArg) {
|
|
607
875
|
const out = this._createInstance();
|
|
608
876
|
let i = 0;
|
|
@@ -616,15 +884,28 @@ var priorityQueueTyped = (() => {
|
|
|
616
884
|
return out;
|
|
617
885
|
}
|
|
618
886
|
/**
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
887
|
+
* Map elements into a new heap of possibly different element type.
|
|
888
|
+
* @remarks Time O(N log N), Space O(N)
|
|
889
|
+
* @template EM
|
|
890
|
+
* @template RM
|
|
891
|
+
* @param callback - Mapping function (element, index, heap) → newElement.
|
|
892
|
+
* @param options - Options for the output heap, including comparator for EM.
|
|
893
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
894
|
+
* @returns A new heap with mapped elements.
|
|
895
|
+
|
|
896
|
+
|
|
897
|
+
|
|
898
|
+
|
|
899
|
+
|
|
900
|
+
|
|
901
|
+
|
|
902
|
+
|
|
903
|
+
* @example
|
|
904
|
+
* // Transform elements
|
|
905
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
906
|
+
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
907
|
+
* console.log(doubled.peek()); // 2;
|
|
908
|
+
*/
|
|
628
909
|
map(callback, options, thisArg) {
|
|
629
910
|
const { comparator, toElementFn, ...rest } = options != null ? options : {};
|
|
630
911
|
if (!comparator) throw new TypeError(ERR.comparatorRequired("Heap.map"));
|