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
|
@@ -5,54 +5,6 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
|
|
|
5
5
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
6
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
7
7
|
|
|
8
|
-
// src/common/error.ts
|
|
9
|
-
var ERR = {
|
|
10
|
-
// Range / index
|
|
11
|
-
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
12
|
-
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
13
|
-
// Type / argument
|
|
14
|
-
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
15
|
-
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
16
|
-
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
17
|
-
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
18
|
-
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
19
|
-
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
20
|
-
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
21
|
-
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
22
|
-
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
23
|
-
// State / operation
|
|
24
|
-
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
25
|
-
// Matrix
|
|
26
|
-
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
27
|
-
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
28
|
-
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
29
|
-
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
30
|
-
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
// src/common/index.ts
|
|
34
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
35
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
36
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
37
|
-
return DFSOperation2;
|
|
38
|
-
})(DFSOperation || {});
|
|
39
|
-
var _Range = class _Range {
|
|
40
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
41
|
-
this.low = low;
|
|
42
|
-
this.high = high;
|
|
43
|
-
this.includeLow = includeLow;
|
|
44
|
-
this.includeHigh = includeHigh;
|
|
45
|
-
}
|
|
46
|
-
// Determine whether a key is within the range
|
|
47
|
-
isInRange(key, comparator) {
|
|
48
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
49
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
50
|
-
return lowCheck && highCheck;
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
__name(_Range, "Range");
|
|
54
|
-
var Range = _Range;
|
|
55
|
-
|
|
56
8
|
// src/data-structures/base/iterable-element-base.ts
|
|
57
9
|
var _IterableElementBase = class _IterableElementBase {
|
|
58
10
|
/**
|
|
@@ -75,7 +27,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
75
27
|
if (options) {
|
|
76
28
|
const { toElementFn } = options;
|
|
77
29
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
78
|
-
else if (toElementFn) throw new TypeError(
|
|
30
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
79
31
|
}
|
|
80
32
|
}
|
|
81
33
|
/**
|
|
@@ -231,7 +183,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
231
183
|
acc = initialValue;
|
|
232
184
|
} else {
|
|
233
185
|
const first = iter.next();
|
|
234
|
-
if (first.done) throw new TypeError(
|
|
186
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
235
187
|
acc = first.value;
|
|
236
188
|
index = 1;
|
|
237
189
|
}
|
|
@@ -275,6 +227,54 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
275
227
|
__name(_IterableElementBase, "IterableElementBase");
|
|
276
228
|
var IterableElementBase = _IterableElementBase;
|
|
277
229
|
|
|
230
|
+
// src/common/error.ts
|
|
231
|
+
var ERR = {
|
|
232
|
+
// Range / index
|
|
233
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
234
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
235
|
+
// Type / argument
|
|
236
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
237
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
238
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
239
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
240
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
241
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
242
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
243
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
244
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
245
|
+
// State / operation
|
|
246
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
247
|
+
// Matrix
|
|
248
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
249
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
250
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
251
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
252
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
// src/common/index.ts
|
|
256
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
257
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
258
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
259
|
+
return DFSOperation2;
|
|
260
|
+
})(DFSOperation || {});
|
|
261
|
+
var _Range = class _Range {
|
|
262
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
263
|
+
this.low = low;
|
|
264
|
+
this.high = high;
|
|
265
|
+
this.includeLow = includeLow;
|
|
266
|
+
this.includeHigh = includeHigh;
|
|
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
|
+
__name(_Range, "Range");
|
|
276
|
+
var Range = _Range;
|
|
277
|
+
|
|
278
278
|
// src/data-structures/heap/heap.ts
|
|
279
279
|
var _Heap = class _Heap extends IterableElementBase {
|
|
280
280
|
/**
|
|
@@ -312,10 +312,30 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
312
312
|
return this._elements;
|
|
313
313
|
}
|
|
314
314
|
/**
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
315
|
+
* Get the number of elements.
|
|
316
|
+
* @remarks Time O(1), Space O(1)
|
|
317
|
+
* @returns Heap size.
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
* @example
|
|
330
|
+
* // Track heap capacity
|
|
331
|
+
* const heap = new Heap<number>();
|
|
332
|
+
* console.log(heap.size); // 0;
|
|
333
|
+
* heap.add(10);
|
|
334
|
+
* heap.add(20);
|
|
335
|
+
* console.log(heap.size); // 2;
|
|
336
|
+
* heap.poll();
|
|
337
|
+
* console.log(heap.size); // 1;
|
|
338
|
+
*/
|
|
319
339
|
get size() {
|
|
320
340
|
return this.elements.length;
|
|
321
341
|
}
|
|
@@ -354,21 +374,61 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
354
374
|
return new _Heap(elements, options);
|
|
355
375
|
}
|
|
356
376
|
/**
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
377
|
+
* Insert an element.
|
|
378
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
379
|
+
* @param element - Element to insert.
|
|
380
|
+
* @returns True.
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
* @example
|
|
393
|
+
* // basic Heap creation and add operation
|
|
394
|
+
* // Create a min heap (default)
|
|
395
|
+
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
396
|
+
*
|
|
397
|
+
* // Verify size
|
|
398
|
+
* console.log(minHeap.size); // 6;
|
|
399
|
+
*
|
|
400
|
+
* // Add new element
|
|
401
|
+
* minHeap.add(4);
|
|
402
|
+
* console.log(minHeap.size); // 7;
|
|
403
|
+
*
|
|
404
|
+
* // Min heap property: smallest element at root
|
|
405
|
+
* const min = minHeap.peek();
|
|
406
|
+
* console.log(min); // 1;
|
|
407
|
+
*/
|
|
362
408
|
add(element) {
|
|
363
409
|
this._elements.push(element);
|
|
364
410
|
return this._bubbleUp(this.elements.length - 1);
|
|
365
411
|
}
|
|
366
412
|
/**
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
413
|
+
* Insert many elements from an iterable.
|
|
414
|
+
* @remarks Time O(N log N), Space O(1)
|
|
415
|
+
* @param elements - Iterable of elements or raw values.
|
|
416
|
+
* @returns Array of per-element success flags.
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
* @example
|
|
426
|
+
* // Add multiple elements
|
|
427
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
428
|
+
* heap.addMany([5, 3, 7, 1]);
|
|
429
|
+
* console.log(heap.peek()); // 1;
|
|
430
|
+
* console.log(heap.size); // 4;
|
|
431
|
+
*/
|
|
372
432
|
addMany(elements) {
|
|
373
433
|
const flags = [];
|
|
374
434
|
for (const el of elements) {
|
|
@@ -383,10 +443,46 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
383
443
|
return flags;
|
|
384
444
|
}
|
|
385
445
|
/**
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
446
|
+
* Remove and return the top element.
|
|
447
|
+
* @remarks Time O(log N), Space O(1)
|
|
448
|
+
* @returns Top element or undefined.
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
* @example
|
|
461
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
462
|
+
* interface Task {
|
|
463
|
+
* id: number;
|
|
464
|
+
* priority: number;
|
|
465
|
+
* name: string;
|
|
466
|
+
* }
|
|
467
|
+
*
|
|
468
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
469
|
+
* const tasks: Task[] = [
|
|
470
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
471
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
472
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
473
|
+
* ];
|
|
474
|
+
*
|
|
475
|
+
* const maxHeap = new Heap(tasks, {
|
|
476
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
477
|
+
* });
|
|
478
|
+
*
|
|
479
|
+
* console.log(maxHeap.size); // 3;
|
|
480
|
+
*
|
|
481
|
+
* // Peek returns highest priority task
|
|
482
|
+
* const topTask = maxHeap.peek();
|
|
483
|
+
* console.log(topTask?.priority); // 8;
|
|
484
|
+
* console.log(topTask?.name); // 'Alert';
|
|
485
|
+
*/
|
|
390
486
|
poll() {
|
|
391
487
|
if (this.elements.length === 0) return;
|
|
392
488
|
const value = this.elements[0];
|
|
@@ -398,26 +494,125 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
398
494
|
return value;
|
|
399
495
|
}
|
|
400
496
|
/**
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
497
|
+
* Get the current top element without removing it.
|
|
498
|
+
* @remarks Time O(1), Space O(1)
|
|
499
|
+
* @returns Top element or undefined.
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
* @example
|
|
512
|
+
* // Heap for event processing with priority
|
|
513
|
+
* interface Event {
|
|
514
|
+
* id: number;
|
|
515
|
+
* type: 'critical' | 'warning' | 'info';
|
|
516
|
+
* timestamp: number;
|
|
517
|
+
* message: string;
|
|
518
|
+
* }
|
|
519
|
+
*
|
|
520
|
+
* // Custom priority: critical > warning > info
|
|
521
|
+
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
522
|
+
*
|
|
523
|
+
* const eventHeap = new Heap<Event>([], {
|
|
524
|
+
* comparator: (a: Event, b: Event) => {
|
|
525
|
+
* const priorityA = priorityMap[a.type];
|
|
526
|
+
* const priorityB = priorityMap[b.type];
|
|
527
|
+
* return priorityB - priorityA; // Higher priority first
|
|
528
|
+
* }
|
|
529
|
+
* });
|
|
530
|
+
*
|
|
531
|
+
* // Add events in random order
|
|
532
|
+
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
533
|
+
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
534
|
+
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
535
|
+
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
536
|
+
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
537
|
+
*
|
|
538
|
+
* console.log(eventHeap.size); // 5;
|
|
539
|
+
*
|
|
540
|
+
* // Process events by priority (critical first)
|
|
541
|
+
* const processedOrder: Event[] = [];
|
|
542
|
+
* while (eventHeap.size > 0) {
|
|
543
|
+
* const event = eventHeap.poll();
|
|
544
|
+
* if (event) {
|
|
545
|
+
* processedOrder.push(event);
|
|
546
|
+
* }
|
|
547
|
+
* }
|
|
548
|
+
*
|
|
549
|
+
* // Verify critical events came first
|
|
550
|
+
* console.log(processedOrder[0].type); // 'critical';
|
|
551
|
+
* console.log(processedOrder[1].type); // 'critical';
|
|
552
|
+
* console.log(processedOrder[2].type); // 'warning';
|
|
553
|
+
* console.log(processedOrder[3].type); // 'info';
|
|
554
|
+
* console.log(processedOrder[4].type); // 'info';
|
|
555
|
+
*
|
|
556
|
+
* // Verify O(log n) operations
|
|
557
|
+
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
558
|
+
*
|
|
559
|
+
* // Add - O(log n)
|
|
560
|
+
* newHeap.add(2);
|
|
561
|
+
* console.log(newHeap.size); // 5;
|
|
562
|
+
*
|
|
563
|
+
* // Poll - O(log n)
|
|
564
|
+
* const removed = newHeap.poll();
|
|
565
|
+
* console.log(removed); // 1;
|
|
566
|
+
*
|
|
567
|
+
* // Peek - O(1)
|
|
568
|
+
* const top = newHeap.peek();
|
|
569
|
+
* console.log(top); // 2;
|
|
570
|
+
*/
|
|
405
571
|
peek() {
|
|
406
572
|
return this.elements[0];
|
|
407
573
|
}
|
|
408
574
|
/**
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
575
|
+
* Check whether the heap is empty.
|
|
576
|
+
* @remarks Time O(1), Space O(1)
|
|
577
|
+
* @returns True if size is 0.
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
* @example
|
|
588
|
+
* // Check if heap is empty
|
|
589
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
590
|
+
* console.log(heap.isEmpty()); // true;
|
|
591
|
+
* heap.add(1);
|
|
592
|
+
* console.log(heap.isEmpty()); // false;
|
|
593
|
+
*/
|
|
413
594
|
isEmpty() {
|
|
414
595
|
return this.size === 0;
|
|
415
596
|
}
|
|
416
597
|
/**
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
598
|
+
* Remove all elements.
|
|
599
|
+
* @remarks Time O(1), Space O(1)
|
|
600
|
+
* @returns void
|
|
601
|
+
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
* @example
|
|
611
|
+
* // Remove all elements
|
|
612
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
613
|
+
* heap.clear();
|
|
614
|
+
* console.log(heap.isEmpty()); // true;
|
|
615
|
+
*/
|
|
421
616
|
clear() {
|
|
422
617
|
this._elements = [];
|
|
423
618
|
}
|
|
@@ -432,21 +627,41 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
432
627
|
return this.fix();
|
|
433
628
|
}
|
|
434
629
|
/**
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
630
|
+
* Check if an equal element exists in the heap.
|
|
631
|
+
* @remarks Time O(N), Space O(1)
|
|
632
|
+
* @param element - Element to search for.
|
|
633
|
+
* @returns True if found.
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
* @example
|
|
637
|
+
* // Check element existence
|
|
638
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
639
|
+
* console.log(heap.has(1)); // true;
|
|
640
|
+
* console.log(heap.has(99)); // false;
|
|
641
|
+
*/
|
|
440
642
|
has(element) {
|
|
441
643
|
for (const el of this.elements) if (this._equals(el, element)) return true;
|
|
442
644
|
return false;
|
|
443
645
|
}
|
|
444
646
|
/**
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
647
|
+
* Delete one occurrence of an element.
|
|
648
|
+
* @remarks Time O(N), Space O(1)
|
|
649
|
+
* @param element - Element to delete.
|
|
650
|
+
* @returns True if an element was removed.
|
|
651
|
+
|
|
652
|
+
|
|
653
|
+
|
|
654
|
+
|
|
655
|
+
|
|
656
|
+
|
|
657
|
+
|
|
658
|
+
|
|
659
|
+
* @example
|
|
660
|
+
* // Remove specific element
|
|
661
|
+
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
662
|
+
* heap.delete(4);
|
|
663
|
+
* console.log(heap.toArray().includes(4)); // false;
|
|
664
|
+
*/
|
|
450
665
|
delete(element) {
|
|
451
666
|
let index = -1;
|
|
452
667
|
for (let i = 0; i < this.elements.length; i++) {
|
|
@@ -504,11 +719,18 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
504
719
|
return this;
|
|
505
720
|
}
|
|
506
721
|
/**
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
722
|
+
* Traverse the binary heap as a complete binary tree and collect elements.
|
|
723
|
+
* @remarks Time O(N), Space O(H)
|
|
724
|
+
* @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
725
|
+
* @returns Array of visited elements.
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
* @example
|
|
729
|
+
* // Depth-first traversal
|
|
730
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
731
|
+
* const result = heap.dfs('IN');
|
|
732
|
+
* console.log(result.length); // 3;
|
|
733
|
+
*/
|
|
512
734
|
dfs(order = "PRE") {
|
|
513
735
|
const result = [];
|
|
514
736
|
const _dfs = /* @__PURE__ */ __name((index) => {
|
|
@@ -545,10 +767,26 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
545
767
|
return results;
|
|
546
768
|
}
|
|
547
769
|
/**
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
770
|
+
* Return all elements in ascending order by repeatedly polling.
|
|
771
|
+
* @remarks Time O(N log N), Space O(N)
|
|
772
|
+
* @returns Sorted array of elements.
|
|
773
|
+
|
|
774
|
+
|
|
775
|
+
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
|
|
781
|
+
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
* @example
|
|
785
|
+
* // Sort elements using heap
|
|
786
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
787
|
+
* const sorted = heap.sort();
|
|
788
|
+
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
789
|
+
*/
|
|
552
790
|
sort() {
|
|
553
791
|
const visited = [];
|
|
554
792
|
const cloned = this._createInstance();
|
|
@@ -560,22 +798,52 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
560
798
|
return visited;
|
|
561
799
|
}
|
|
562
800
|
/**
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
801
|
+
* Deep clone this heap.
|
|
802
|
+
* @remarks Time O(N), Space O(N)
|
|
803
|
+
* @returns A new heap with the same elements.
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
* @example
|
|
814
|
+
* // Create independent copy
|
|
815
|
+
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
816
|
+
* const copy = heap.clone();
|
|
817
|
+
* copy.poll();
|
|
818
|
+
* console.log(heap.size); // 3;
|
|
819
|
+
* console.log(copy.size); // 2;
|
|
820
|
+
*/
|
|
567
821
|
clone() {
|
|
568
822
|
const next = this._createInstance();
|
|
569
823
|
for (const x of this.elements) next.add(x);
|
|
570
824
|
return next;
|
|
571
825
|
}
|
|
572
826
|
/**
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
827
|
+
* Filter elements into a new heap of the same class.
|
|
828
|
+
* @remarks Time O(N log N), Space O(N)
|
|
829
|
+
* @param callback - Predicate (element, index, heap) → boolean to keep element.
|
|
830
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
831
|
+
* @returns A new heap with the kept elements.
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
* @example
|
|
842
|
+
* // Filter elements
|
|
843
|
+
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
844
|
+
* const evens = heap.filter(x => x % 2 === 0);
|
|
845
|
+
* console.log(evens.size); // 2;
|
|
846
|
+
*/
|
|
579
847
|
filter(callback, thisArg) {
|
|
580
848
|
const out = this._createInstance();
|
|
581
849
|
let i = 0;
|
|
@@ -589,15 +857,28 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
589
857
|
return out;
|
|
590
858
|
}
|
|
591
859
|
/**
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
860
|
+
* Map elements into a new heap of possibly different element type.
|
|
861
|
+
* @remarks Time O(N log N), Space O(N)
|
|
862
|
+
* @template EM
|
|
863
|
+
* @template RM
|
|
864
|
+
* @param callback - Mapping function (element, index, heap) → newElement.
|
|
865
|
+
* @param options - Options for the output heap, including comparator for EM.
|
|
866
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
867
|
+
* @returns A new heap with mapped elements.
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
|
|
872
|
+
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
|
|
876
|
+
* @example
|
|
877
|
+
* // Transform elements
|
|
878
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
879
|
+
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
880
|
+
* console.log(doubled.peek()); // 2;
|
|
881
|
+
*/
|
|
601
882
|
map(callback, options, thisArg) {
|
|
602
883
|
const { comparator, toElementFn, ...rest } = options != null ? options : {};
|
|
603
884
|
if (!comparator) throw new TypeError(ERR.comparatorRequired("Heap.map"));
|