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
@@ -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(ERR.notAFunction("toElementFn"));
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(ERR.reduceEmpty());
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
- * Get the number of elements.
316
- * @remarks Time O(1), Space O(1)
317
- * @returns Heap size.
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
- * Insert an element.
358
- * @remarks Time O(1) amortized, Space O(1)
359
- * @param element - Element to insert.
360
- * @returns True.
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
- * Insert many elements from an iterable.
368
- * @remarks Time O(N log N), Space O(1)
369
- * @param elements - Iterable of elements or raw values.
370
- * @returns Array of per-element success flags.
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
- * Remove and return the top element.
387
- * @remarks Time O(log N), Space O(1)
388
- * @returns Top element or undefined.
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
- * Get the current top element without removing it.
402
- * @remarks Time O(1), Space O(1)
403
- * @returns Top element or undefined.
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
- * Check whether the heap is empty.
410
- * @remarks Time O(1), Space O(1)
411
- * @returns True if size is 0.
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
- * Remove all elements.
418
- * @remarks Time O(1), Space O(1)
419
- * @returns void
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
- * Check if an equal element exists in the heap.
436
- * @remarks Time O(N), Space O(1)
437
- * @param element - Element to search for.
438
- * @returns True if found.
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
- * Delete one occurrence of an element.
446
- * @remarks Time O(N), Space O(1)
447
- * @param element - Element to delete.
448
- * @returns True if an element was removed.
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
- * Traverse the binary heap as a complete binary tree and collect elements.
508
- * @remarks Time O(N), Space O(H)
509
- * @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
510
- * @returns Array of visited elements.
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
- * Return all elements in ascending order by repeatedly polling.
549
- * @remarks Time O(N log N), Space O(N)
550
- * @returns Sorted array of elements.
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
- * Deep clone this heap.
564
- * @remarks Time O(N), Space O(N)
565
- * @returns A new heap with the same elements.
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
- * Filter elements into a new heap of the same class.
574
- * @remarks Time O(N log N), Space O(N)
575
- * @param callback - Predicate (element, index, heap) → boolean to keep element.
576
- * @param [thisArg] - Value for `this` inside the callback.
577
- * @returns A new heap with the kept elements.
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
- * Map elements into a new heap of possibly different element type.
593
- * @remarks Time O(N log N), Space O(N)
594
- * @template EM
595
- * @template RM
596
- * @param callback - Mapping function (element, index, heap) → newElement.
597
- * @param options - Options for the output heap, including comparator for EM.
598
- * @param [thisArg] - Value for `this` inside the callback.
599
- * @returns A new heap with mapped elements.
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"));