max-priority-queue-typed 2.5.2 → 2.6.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 (63) hide show
  1. package/dist/cjs/index.cjs +174 -16
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +174 -16
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +174 -16
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +174 -16
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/data-structures/base/iterable-element-base.d.ts +17 -0
  10. package/dist/types/data-structures/base/linear-base.d.ts +6 -0
  11. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +86 -2
  12. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +98 -0
  13. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +191 -15
  14. package/dist/types/data-structures/binary-tree/bst.d.ts +171 -3
  15. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +136 -8
  16. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +42 -0
  17. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1061 -167
  18. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1232 -355
  19. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +916 -194
  20. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1078 -141
  21. package/dist/types/data-structures/graph/directed-graph.d.ts +70 -0
  22. package/dist/types/data-structures/graph/undirected-graph.d.ts +63 -0
  23. package/dist/types/data-structures/hash/hash-map.d.ts +84 -6
  24. package/dist/types/data-structures/heap/heap.d.ts +140 -12
  25. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +150 -2
  26. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +106 -1
  27. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +126 -0
  28. package/dist/types/data-structures/matrix/matrix.d.ts +56 -0
  29. package/dist/types/data-structures/queue/deque.d.ts +171 -0
  30. package/dist/types/data-structures/queue/queue.d.ts +97 -0
  31. package/dist/types/data-structures/stack/stack.d.ts +72 -2
  32. package/dist/types/data-structures/trie/trie.d.ts +84 -0
  33. package/dist/types/interfaces/binary-tree.d.ts +2 -3
  34. package/dist/umd/max-priority-queue-typed.js +174 -16
  35. package/dist/umd/max-priority-queue-typed.js.map +1 -1
  36. package/dist/umd/max-priority-queue-typed.min.js +1 -1
  37. package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
  38. package/package.json +2 -2
  39. package/src/data-structures/base/iterable-element-base.ts +32 -0
  40. package/src/data-structures/base/linear-base.ts +11 -0
  41. package/src/data-structures/binary-tree/avl-tree.ts +88 -5
  42. package/src/data-structures/binary-tree/binary-indexed-tree.ts +98 -0
  43. package/src/data-structures/binary-tree/binary-tree.ts +242 -81
  44. package/src/data-structures/binary-tree/bst.ts +173 -7
  45. package/src/data-structures/binary-tree/red-black-tree.ts +139 -15
  46. package/src/data-structures/binary-tree/segment-tree.ts +42 -0
  47. package/src/data-structures/binary-tree/tree-map.ts +948 -36
  48. package/src/data-structures/binary-tree/tree-multi-map.ts +893 -13
  49. package/src/data-structures/binary-tree/tree-multi-set.ts +761 -33
  50. package/src/data-structures/binary-tree/tree-set.ts +1260 -251
  51. package/src/data-structures/graph/directed-graph.ts +71 -1
  52. package/src/data-structures/graph/undirected-graph.ts +64 -1
  53. package/src/data-structures/hash/hash-map.ts +100 -12
  54. package/src/data-structures/heap/heap.ts +149 -19
  55. package/src/data-structures/linked-list/doubly-linked-list.ts +178 -2
  56. package/src/data-structures/linked-list/singly-linked-list.ts +106 -1
  57. package/src/data-structures/linked-list/skip-linked-list.ts +126 -0
  58. package/src/data-structures/matrix/matrix.ts +56 -0
  59. package/src/data-structures/queue/deque.ts +187 -0
  60. package/src/data-structures/queue/queue.ts +109 -0
  61. package/src/data-structures/stack/stack.ts +75 -5
  62. package/src/data-structures/trie/trie.ts +84 -0
  63. package/src/interfaces/binary-tree.ts +1 -9
@@ -210,6 +210,35 @@ var IterableElementBase = class {
210
210
  for (const ele of this) if (ele === element) return true;
211
211
  return false;
212
212
  }
213
+ /**
214
+ * Check whether a value exists (Array-compatible alias for `has`).
215
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
216
+ * @param element - Element to search for (uses `===`).
217
+ * @returns `true` if found.
218
+ */
219
+ includes(element) {
220
+ return this.has(element);
221
+ }
222
+ /**
223
+ * Return an iterator of `[index, value]` pairs (Array-compatible).
224
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
225
+ */
226
+ *entries() {
227
+ let index = 0;
228
+ for (const value of this) {
229
+ yield [index++, value];
230
+ }
231
+ }
232
+ /**
233
+ * Return an iterator of numeric indices (Array-compatible).
234
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
235
+ */
236
+ *keys() {
237
+ let index = 0;
238
+ for (const _ of this) {
239
+ yield index++;
240
+ }
241
+ }
213
242
  /**
214
243
  * Reduces all elements to a single accumulated value.
215
244
  *
@@ -342,6 +371,13 @@ var Heap = class _Heap extends IterableElementBase {
342
371
 
343
372
 
344
373
 
374
+
375
+
376
+
377
+
378
+
379
+
380
+
345
381
 
346
382
 
347
383
 
@@ -398,7 +434,7 @@ var Heap = class _Heap extends IterableElementBase {
398
434
  }
399
435
  /**
400
436
  * Insert an element.
401
- * @remarks Time O(1) amortized, Space O(1)
437
+ * @remarks Time O(log N) amortized, Space O(1)
402
438
  * @param element - Element to insert.
403
439
  * @returns True.
404
440
 
@@ -428,6 +464,13 @@ var Heap = class _Heap extends IterableElementBase {
428
464
 
429
465
 
430
466
 
467
+
468
+
469
+
470
+
471
+
472
+
473
+
431
474
 
432
475
 
433
476
 
@@ -485,6 +528,13 @@ var Heap = class _Heap extends IterableElementBase {
485
528
 
486
529
 
487
530
 
531
+
532
+
533
+
534
+
535
+
536
+
537
+
488
538
 
489
539
 
490
540
 
@@ -549,6 +599,40 @@ var Heap = class _Heap extends IterableElementBase {
549
599
 
550
600
 
551
601
 
602
+
603
+
604
+
605
+
606
+
607
+
608
+ * @example
609
+ * // Heap with custom comparator (MaxHeap behavior)
610
+ * interface Task {
611
+ * id: number;
612
+ * priority: number;
613
+ * name: string;
614
+ * }
615
+ *
616
+ * // Custom comparator for max heap behavior (higher priority first)
617
+ * const tasks: Task[] = [
618
+ * { id: 1, priority: 5, name: 'Email' },
619
+ * { id: 2, priority: 3, name: 'Chat' },
620
+ * { id: 3, priority: 8, name: 'Alert' }
621
+ * ];
622
+ *
623
+ * const maxHeap = new Heap(tasks, {
624
+ * comparator: (a: Task, b: Task) => b.priority - a.priority
625
+ * });
626
+ *
627
+ * console.log(maxHeap.size); // 3;
628
+ *
629
+ * // Peek returns highest priority task
630
+ * const topTask = maxHeap.peek();
631
+ * console.log(topTask?.priority); // 8;
632
+ * console.log(topTask?.name); // 'Alert';
633
+ */
634
+ /**
635
+ * @deprecated Use `pop` instead. Will be removed in a future major version.
552
636
 
553
637
 
554
638
 
@@ -579,6 +663,14 @@ var Heap = class _Heap extends IterableElementBase {
579
663
  * console.log(topTask?.name); // 'Alert';
580
664
  */
581
665
  poll() {
666
+ return this.pop();
667
+ }
668
+ /**
669
+ * Remove and return the top element (min or max depending on comparator).
670
+ * @remarks Time O(log N) amortized, Space O(1)
671
+ * @returns The removed top element, or undefined if empty.
672
+ */
673
+ pop() {
582
674
  if (this.elements.length === 0) return;
583
675
  const value = this.elements[0];
584
676
  const last = this.elements.pop();
@@ -619,6 +711,13 @@ var Heap = class _Heap extends IterableElementBase {
619
711
 
620
712
 
621
713
 
714
+
715
+
716
+
717
+
718
+
719
+
720
+
622
721
 
623
722
 
624
723
 
@@ -719,6 +818,13 @@ var Heap = class _Heap extends IterableElementBase {
719
818
 
720
819
 
721
820
 
821
+
822
+
823
+
824
+
825
+
826
+
827
+
722
828
 
723
829
 
724
830
 
@@ -766,6 +872,13 @@ var Heap = class _Heap extends IterableElementBase {
766
872
 
767
873
 
768
874
 
875
+
876
+
877
+
878
+
879
+
880
+
881
+
769
882
 
770
883
 
771
884
 
@@ -783,16 +896,6 @@ var Heap = class _Heap extends IterableElementBase {
783
896
  clear() {
784
897
  this._elements = [];
785
898
  }
786
- /**
787
- * Replace the backing array and rebuild the heap.
788
- * @remarks Time O(N), Space O(N)
789
- * @param elements - Iterable used to refill the heap.
790
- * @returns Array of per-node results from fixing steps.
791
- */
792
- refill(elements) {
793
- this._elements = Array.from(elements);
794
- return this.fix();
795
- }
796
899
  /**
797
900
  * Check if an equal element exists in the heap.
798
901
  * @remarks Time O(N), Space O(1)
@@ -816,6 +919,13 @@ var Heap = class _Heap extends IterableElementBase {
816
919
 
817
920
 
818
921
 
922
+
923
+
924
+
925
+
926
+
927
+
928
+
819
929
 
820
930
 
821
931
 
@@ -863,6 +973,13 @@ var Heap = class _Heap extends IterableElementBase {
863
973
 
864
974
 
865
975
 
976
+
977
+
978
+
979
+
980
+
981
+
982
+
866
983
 
867
984
 
868
985
 
@@ -887,7 +1004,7 @@ var Heap = class _Heap extends IterableElementBase {
887
1004
  }
888
1005
  if (index < 0) return false;
889
1006
  if (index === 0) {
890
- this.poll();
1007
+ this.pop();
891
1008
  } else if (index === this.elements.length - 1) {
892
1009
  this.elements.pop();
893
1010
  } else {
@@ -897,13 +1014,19 @@ var Heap = class _Heap extends IterableElementBase {
897
1014
  }
898
1015
  return true;
899
1016
  }
1017
+ /**
1018
+ * @deprecated Use `deleteWhere` instead. Will be removed in a future major version.
1019
+ */
1020
+ deleteBy(predicate) {
1021
+ return this.deleteWhere(predicate);
1022
+ }
900
1023
  /**
901
1024
  * Delete the first element that matches a predicate.
902
1025
  * @remarks Time O(N), Space O(1)
903
1026
  * @param predicate - Function (element, index, heap) → boolean.
904
1027
  * @returns True if an element was removed.
905
1028
  */
906
- deleteBy(predicate) {
1029
+ deleteWhere(predicate) {
907
1030
  let idx = -1;
908
1031
  for (let i = 0; i < this.elements.length; i++) {
909
1032
  if (predicate(this.elements[i], i, this)) {
@@ -913,7 +1036,7 @@ var Heap = class _Heap extends IterableElementBase {
913
1036
  }
914
1037
  if (idx < 0) return false;
915
1038
  if (idx === 0) {
916
- this.poll();
1039
+ this.pop();
917
1040
  } else if (idx === this.elements.length - 1) {
918
1041
  this.elements.pop();
919
1042
  } else {
@@ -956,6 +1079,13 @@ var Heap = class _Heap extends IterableElementBase {
956
1079
 
957
1080
 
958
1081
 
1082
+
1083
+
1084
+
1085
+
1086
+
1087
+
1088
+
959
1089
 
960
1090
 
961
1091
 
@@ -1036,6 +1166,13 @@ var Heap = class _Heap extends IterableElementBase {
1036
1166
 
1037
1167
 
1038
1168
 
1169
+
1170
+
1171
+
1172
+
1173
+
1174
+
1175
+
1039
1176
 
1040
1177
 
1041
1178
 
@@ -1089,6 +1226,13 @@ var Heap = class _Heap extends IterableElementBase {
1089
1226
 
1090
1227
 
1091
1228
 
1229
+
1230
+
1231
+
1232
+
1233
+
1234
+
1235
+
1092
1236
 
1093
1237
 
1094
1238
 
@@ -1141,6 +1285,13 @@ var Heap = class _Heap extends IterableElementBase {
1141
1285
 
1142
1286
 
1143
1287
 
1288
+
1289
+
1290
+
1291
+
1292
+
1293
+
1294
+
1144
1295
 
1145
1296
 
1146
1297
 
@@ -1200,6 +1351,13 @@ var Heap = class _Heap extends IterableElementBase {
1200
1351
 
1201
1352
 
1202
1353
 
1354
+
1355
+
1356
+
1357
+
1358
+
1359
+
1360
+
1203
1361
 
1204
1362
 
1205
1363
 
@@ -1396,7 +1554,7 @@ var FibonacciHeap = class {
1396
1554
  * Push an element into the root list.
1397
1555
  * @remarks Time O(1) amortized, Space O(1)
1398
1556
  * @param element - Element to insert.
1399
- * @returns This heap.
1557
+ * @returns True when the element is added.
1400
1558
  */
1401
1559
  push(element) {
1402
1560
  const node = this.createNode(element);
@@ -1405,7 +1563,7 @@ var FibonacciHeap = class {
1405
1563
  this.mergeWithRoot(node);
1406
1564
  if (!this.min || this.comparator(node.element, this.min.element) <= 0) this._min = node;
1407
1565
  this._size++;
1408
- return this;
1566
+ return true;
1409
1567
  }
1410
1568
  peek() {
1411
1569
  return this.min ? this.min.element : void 0;