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