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