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
@@ -208,6 +208,35 @@ var _IterableElementBase = class _IterableElementBase {
208
208
  for (const ele of this) if (ele === element) return true;
209
209
  return false;
210
210
  }
211
+ /**
212
+ * Check whether a value exists (Array-compatible alias for `has`).
213
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
214
+ * @param element - Element to search for (uses `===`).
215
+ * @returns `true` if found.
216
+ */
217
+ includes(element) {
218
+ return this.has(element);
219
+ }
220
+ /**
221
+ * Return an iterator of `[index, value]` pairs (Array-compatible).
222
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
223
+ */
224
+ *entries() {
225
+ let index = 0;
226
+ for (const value of this) {
227
+ yield [index++, value];
228
+ }
229
+ }
230
+ /**
231
+ * Return an iterator of numeric indices (Array-compatible).
232
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
233
+ */
234
+ *keys() {
235
+ let index = 0;
236
+ for (const _ of this) {
237
+ yield index++;
238
+ }
239
+ }
211
240
  /**
212
241
  * Reduces all elements to a single accumulated value.
213
242
  *
@@ -348,6 +377,13 @@ var _Heap = class _Heap extends IterableElementBase {
348
377
 
349
378
 
350
379
 
380
+
381
+
382
+
383
+
384
+
385
+
386
+
351
387
 
352
388
 
353
389
 
@@ -405,7 +441,7 @@ var _Heap = class _Heap extends IterableElementBase {
405
441
  }
406
442
  /**
407
443
  * Insert an element.
408
- * @remarks Time O(1) amortized, Space O(1)
444
+ * @remarks Time O(log N) amortized, Space O(1)
409
445
  * @param element - Element to insert.
410
446
  * @returns True.
411
447
 
@@ -435,6 +471,13 @@ var _Heap = class _Heap extends IterableElementBase {
435
471
 
436
472
 
437
473
 
474
+
475
+
476
+
477
+
478
+
479
+
480
+
438
481
 
439
482
 
440
483
 
@@ -492,6 +535,13 @@ var _Heap = class _Heap extends IterableElementBase {
492
535
 
493
536
 
494
537
 
538
+
539
+
540
+
541
+
542
+
543
+
544
+
495
545
 
496
546
 
497
547
 
@@ -556,6 +606,40 @@ var _Heap = class _Heap extends IterableElementBase {
556
606
 
557
607
 
558
608
 
609
+
610
+
611
+
612
+
613
+
614
+
615
+ * @example
616
+ * // Heap with custom comparator (MaxHeap behavior)
617
+ * interface Task {
618
+ * id: number;
619
+ * priority: number;
620
+ * name: string;
621
+ * }
622
+ *
623
+ * // Custom comparator for max heap behavior (higher priority first)
624
+ * const tasks: Task[] = [
625
+ * { id: 1, priority: 5, name: 'Email' },
626
+ * { id: 2, priority: 3, name: 'Chat' },
627
+ * { id: 3, priority: 8, name: 'Alert' }
628
+ * ];
629
+ *
630
+ * const maxHeap = new Heap(tasks, {
631
+ * comparator: (a: Task, b: Task) => b.priority - a.priority
632
+ * });
633
+ *
634
+ * console.log(maxHeap.size); // 3;
635
+ *
636
+ * // Peek returns highest priority task
637
+ * const topTask = maxHeap.peek();
638
+ * console.log(topTask?.priority); // 8;
639
+ * console.log(topTask?.name); // 'Alert';
640
+ */
641
+ /**
642
+ * @deprecated Use `pop` instead. Will be removed in a future major version.
559
643
 
560
644
 
561
645
 
@@ -586,6 +670,14 @@ var _Heap = class _Heap extends IterableElementBase {
586
670
  * console.log(topTask?.name); // 'Alert';
587
671
  */
588
672
  poll() {
673
+ return this.pop();
674
+ }
675
+ /**
676
+ * Remove and return the top element (min or max depending on comparator).
677
+ * @remarks Time O(log N) amortized, Space O(1)
678
+ * @returns The removed top element, or undefined if empty.
679
+ */
680
+ pop() {
589
681
  if (this.elements.length === 0) return;
590
682
  const value = this.elements[0];
591
683
  const last = this.elements.pop();
@@ -626,6 +718,13 @@ var _Heap = class _Heap extends IterableElementBase {
626
718
 
627
719
 
628
720
 
721
+
722
+
723
+
724
+
725
+
726
+
727
+
629
728
 
630
729
 
631
730
 
@@ -726,6 +825,13 @@ var _Heap = class _Heap extends IterableElementBase {
726
825
 
727
826
 
728
827
 
828
+
829
+
830
+
831
+
832
+
833
+
834
+
729
835
 
730
836
 
731
837
 
@@ -773,6 +879,13 @@ var _Heap = class _Heap extends IterableElementBase {
773
879
 
774
880
 
775
881
 
882
+
883
+
884
+
885
+
886
+
887
+
888
+
776
889
 
777
890
 
778
891
 
@@ -790,16 +903,6 @@ var _Heap = class _Heap extends IterableElementBase {
790
903
  clear() {
791
904
  this._elements = [];
792
905
  }
793
- /**
794
- * Replace the backing array and rebuild the heap.
795
- * @remarks Time O(N), Space O(N)
796
- * @param elements - Iterable used to refill the heap.
797
- * @returns Array of per-node results from fixing steps.
798
- */
799
- refill(elements) {
800
- this._elements = Array.from(elements);
801
- return this.fix();
802
- }
803
906
  /**
804
907
  * Check if an equal element exists in the heap.
805
908
  * @remarks Time O(N), Space O(1)
@@ -823,6 +926,13 @@ var _Heap = class _Heap extends IterableElementBase {
823
926
 
824
927
 
825
928
 
929
+
930
+
931
+
932
+
933
+
934
+
935
+
826
936
 
827
937
 
828
938
 
@@ -870,6 +980,13 @@ var _Heap = class _Heap extends IterableElementBase {
870
980
 
871
981
 
872
982
 
983
+
984
+
985
+
986
+
987
+
988
+
989
+
873
990
 
874
991
 
875
992
 
@@ -894,7 +1011,7 @@ var _Heap = class _Heap extends IterableElementBase {
894
1011
  }
895
1012
  if (index < 0) return false;
896
1013
  if (index === 0) {
897
- this.poll();
1014
+ this.pop();
898
1015
  } else if (index === this.elements.length - 1) {
899
1016
  this.elements.pop();
900
1017
  } else {
@@ -904,13 +1021,19 @@ var _Heap = class _Heap extends IterableElementBase {
904
1021
  }
905
1022
  return true;
906
1023
  }
1024
+ /**
1025
+ * @deprecated Use `deleteWhere` instead. Will be removed in a future major version.
1026
+ */
1027
+ deleteBy(predicate) {
1028
+ return this.deleteWhere(predicate);
1029
+ }
907
1030
  /**
908
1031
  * Delete the first element that matches a predicate.
909
1032
  * @remarks Time O(N), Space O(1)
910
1033
  * @param predicate - Function (element, index, heap) → boolean.
911
1034
  * @returns True if an element was removed.
912
1035
  */
913
- deleteBy(predicate) {
1036
+ deleteWhere(predicate) {
914
1037
  let idx = -1;
915
1038
  for (let i = 0; i < this.elements.length; i++) {
916
1039
  if (predicate(this.elements[i], i, this)) {
@@ -920,7 +1043,7 @@ var _Heap = class _Heap extends IterableElementBase {
920
1043
  }
921
1044
  if (idx < 0) return false;
922
1045
  if (idx === 0) {
923
- this.poll();
1046
+ this.pop();
924
1047
  } else if (idx === this.elements.length - 1) {
925
1048
  this.elements.pop();
926
1049
  } else {
@@ -963,6 +1086,13 @@ var _Heap = class _Heap extends IterableElementBase {
963
1086
 
964
1087
 
965
1088
 
1089
+
1090
+
1091
+
1092
+
1093
+
1094
+
1095
+
966
1096
 
967
1097
 
968
1098
 
@@ -1043,6 +1173,13 @@ var _Heap = class _Heap extends IterableElementBase {
1043
1173
 
1044
1174
 
1045
1175
 
1176
+
1177
+
1178
+
1179
+
1180
+
1181
+
1182
+
1046
1183
 
1047
1184
 
1048
1185
 
@@ -1096,6 +1233,13 @@ var _Heap = class _Heap extends IterableElementBase {
1096
1233
 
1097
1234
 
1098
1235
 
1236
+
1237
+
1238
+
1239
+
1240
+
1241
+
1242
+
1099
1243
 
1100
1244
 
1101
1245
 
@@ -1148,6 +1292,13 @@ var _Heap = class _Heap extends IterableElementBase {
1148
1292
 
1149
1293
 
1150
1294
 
1295
+
1296
+
1297
+
1298
+
1299
+
1300
+
1301
+
1151
1302
 
1152
1303
 
1153
1304
 
@@ -1207,6 +1358,13 @@ var _Heap = class _Heap extends IterableElementBase {
1207
1358
 
1208
1359
 
1209
1360
 
1361
+
1362
+
1363
+
1364
+
1365
+
1366
+
1367
+
1210
1368
 
1211
1369
 
1212
1370
 
@@ -1392,7 +1550,7 @@ var _FibonacciHeap = class _FibonacciHeap {
1392
1550
  * Push an element into the root list.
1393
1551
  * @remarks Time O(1) amortized, Space O(1)
1394
1552
  * @param element - Element to insert.
1395
- * @returns This heap.
1553
+ * @returns True when the element is added.
1396
1554
  */
1397
1555
  push(element) {
1398
1556
  const node = this.createNode(element);
@@ -1401,7 +1559,7 @@ var _FibonacciHeap = class _FibonacciHeap {
1401
1559
  this.mergeWithRoot(node);
1402
1560
  if (!this.min || this.comparator(node.element, this.min.element) <= 0) this._min = node;
1403
1561
  this._size++;
1404
- return this;
1562
+ return true;
1405
1563
  }
1406
1564
  peek() {
1407
1565
  return this.min ? this.min.element : void 0;