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.
- package/dist/cjs/index.cjs +174 -16
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +174 -16
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +174 -16
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +174 -16
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/iterable-element-base.d.ts +17 -0
- package/dist/types/data-structures/base/linear-base.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +86 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +98 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +191 -15
- package/dist/types/data-structures/binary-tree/bst.d.ts +171 -3
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +136 -8
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +42 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1061 -167
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1232 -355
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +916 -194
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1078 -141
- package/dist/types/data-structures/graph/directed-graph.d.ts +70 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +63 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +84 -6
- package/dist/types/data-structures/heap/heap.d.ts +140 -12
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +150 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +106 -1
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +126 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +56 -0
- package/dist/types/data-structures/queue/deque.d.ts +171 -0
- package/dist/types/data-structures/queue/queue.d.ts +97 -0
- package/dist/types/data-structures/stack/stack.d.ts +72 -2
- package/dist/types/data-structures/trie/trie.d.ts +84 -0
- package/dist/types/interfaces/binary-tree.d.ts +2 -3
- package/dist/umd/max-priority-queue-typed.js +174 -16
- 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 +32 -0
- package/src/data-structures/base/linear-base.ts +11 -0
- package/src/data-structures/binary-tree/avl-tree.ts +88 -5
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +98 -0
- package/src/data-structures/binary-tree/binary-tree.ts +242 -81
- package/src/data-structures/binary-tree/bst.ts +173 -7
- package/src/data-structures/binary-tree/red-black-tree.ts +139 -15
- package/src/data-structures/binary-tree/segment-tree.ts +42 -0
- package/src/data-structures/binary-tree/tree-map.ts +948 -36
- package/src/data-structures/binary-tree/tree-multi-map.ts +893 -13
- package/src/data-structures/binary-tree/tree-multi-set.ts +761 -33
- package/src/data-structures/binary-tree/tree-set.ts +1260 -251
- package/src/data-structures/graph/directed-graph.ts +71 -1
- package/src/data-structures/graph/undirected-graph.ts +64 -1
- package/src/data-structures/hash/hash-map.ts +100 -12
- package/src/data-structures/heap/heap.ts +149 -19
- package/src/data-structures/linked-list/doubly-linked-list.ts +178 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +106 -1
- package/src/data-structures/linked-list/skip-linked-list.ts +126 -0
- package/src/data-structures/matrix/matrix.ts +56 -0
- package/src/data-structures/queue/deque.ts +187 -0
- package/src/data-structures/queue/queue.ts +109 -0
- package/src/data-structures/stack/stack.ts +75 -5
- package/src/data-structures/trie/trie.ts +84 -0
- package/src/interfaces/binary-tree.ts +1 -9
|
@@ -206,6 +206,35 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
206
206
|
for (const ele of this) if (ele === element) return true;
|
|
207
207
|
return false;
|
|
208
208
|
}
|
|
209
|
+
/**
|
|
210
|
+
* Check whether a value exists (Array-compatible alias for `has`).
|
|
211
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
|
|
212
|
+
* @param element - Element to search for (uses `===`).
|
|
213
|
+
* @returns `true` if found.
|
|
214
|
+
*/
|
|
215
|
+
includes(element) {
|
|
216
|
+
return this.has(element);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Return an iterator of `[index, value]` pairs (Array-compatible).
|
|
220
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
|
|
221
|
+
*/
|
|
222
|
+
*entries() {
|
|
223
|
+
let index = 0;
|
|
224
|
+
for (const value of this) {
|
|
225
|
+
yield [index++, value];
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Return an iterator of numeric indices (Array-compatible).
|
|
230
|
+
* @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
|
|
231
|
+
*/
|
|
232
|
+
*keys() {
|
|
233
|
+
let index = 0;
|
|
234
|
+
for (const _ of this) {
|
|
235
|
+
yield index++;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
209
238
|
/**
|
|
210
239
|
* Reduces all elements to a single accumulated value.
|
|
211
240
|
*
|
|
@@ -346,6 +375,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
346
375
|
|
|
347
376
|
|
|
348
377
|
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
349
385
|
|
|
350
386
|
|
|
351
387
|
|
|
@@ -403,7 +439,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
403
439
|
}
|
|
404
440
|
/**
|
|
405
441
|
* Insert an element.
|
|
406
|
-
* @remarks Time O(
|
|
442
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
407
443
|
* @param element - Element to insert.
|
|
408
444
|
* @returns True.
|
|
409
445
|
|
|
@@ -433,6 +469,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
433
469
|
|
|
434
470
|
|
|
435
471
|
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
|
|
436
479
|
|
|
437
480
|
|
|
438
481
|
|
|
@@ -490,6 +533,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
490
533
|
|
|
491
534
|
|
|
492
535
|
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
|
|
542
|
+
|
|
493
543
|
|
|
494
544
|
|
|
495
545
|
|
|
@@ -554,6 +604,40 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
554
604
|
|
|
555
605
|
|
|
556
606
|
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
* @example
|
|
614
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
615
|
+
* interface Task {
|
|
616
|
+
* id: number;
|
|
617
|
+
* priority: number;
|
|
618
|
+
* name: string;
|
|
619
|
+
* }
|
|
620
|
+
*
|
|
621
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
622
|
+
* const tasks: Task[] = [
|
|
623
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
624
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
625
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
626
|
+
* ];
|
|
627
|
+
*
|
|
628
|
+
* const maxHeap = new Heap(tasks, {
|
|
629
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
630
|
+
* });
|
|
631
|
+
*
|
|
632
|
+
* console.log(maxHeap.size); // 3;
|
|
633
|
+
*
|
|
634
|
+
* // Peek returns highest priority task
|
|
635
|
+
* const topTask = maxHeap.peek();
|
|
636
|
+
* console.log(topTask?.priority); // 8;
|
|
637
|
+
* console.log(topTask?.name); // 'Alert';
|
|
638
|
+
*/
|
|
639
|
+
/**
|
|
640
|
+
* @deprecated Use `pop` instead. Will be removed in a future major version.
|
|
557
641
|
|
|
558
642
|
|
|
559
643
|
|
|
@@ -584,6 +668,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
584
668
|
* console.log(topTask?.name); // 'Alert';
|
|
585
669
|
*/
|
|
586
670
|
poll() {
|
|
671
|
+
return this.pop();
|
|
672
|
+
}
|
|
673
|
+
/**
|
|
674
|
+
* Remove and return the top element (min or max depending on comparator).
|
|
675
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
676
|
+
* @returns The removed top element, or undefined if empty.
|
|
677
|
+
*/
|
|
678
|
+
pop() {
|
|
587
679
|
if (this.elements.length === 0) return;
|
|
588
680
|
const value = this.elements[0];
|
|
589
681
|
const last = this.elements.pop();
|
|
@@ -624,6 +716,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
624
716
|
|
|
625
717
|
|
|
626
718
|
|
|
719
|
+
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
|
|
627
726
|
|
|
628
727
|
|
|
629
728
|
|
|
@@ -724,6 +823,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
724
823
|
|
|
725
824
|
|
|
726
825
|
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
|
|
829
|
+
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
|
|
727
833
|
|
|
728
834
|
|
|
729
835
|
|
|
@@ -771,6 +877,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
771
877
|
|
|
772
878
|
|
|
773
879
|
|
|
880
|
+
|
|
881
|
+
|
|
882
|
+
|
|
883
|
+
|
|
884
|
+
|
|
885
|
+
|
|
886
|
+
|
|
774
887
|
|
|
775
888
|
|
|
776
889
|
|
|
@@ -788,16 +901,6 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
788
901
|
clear() {
|
|
789
902
|
this._elements = [];
|
|
790
903
|
}
|
|
791
|
-
/**
|
|
792
|
-
* Replace the backing array and rebuild the heap.
|
|
793
|
-
* @remarks Time O(N), Space O(N)
|
|
794
|
-
* @param elements - Iterable used to refill the heap.
|
|
795
|
-
* @returns Array of per-node results from fixing steps.
|
|
796
|
-
*/
|
|
797
|
-
refill(elements) {
|
|
798
|
-
this._elements = Array.from(elements);
|
|
799
|
-
return this.fix();
|
|
800
|
-
}
|
|
801
904
|
/**
|
|
802
905
|
* Check if an equal element exists in the heap.
|
|
803
906
|
* @remarks Time O(N), Space O(1)
|
|
@@ -821,6 +924,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
821
924
|
|
|
822
925
|
|
|
823
926
|
|
|
927
|
+
|
|
928
|
+
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
|
|
932
|
+
|
|
933
|
+
|
|
824
934
|
|
|
825
935
|
|
|
826
936
|
|
|
@@ -868,6 +978,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
868
978
|
|
|
869
979
|
|
|
870
980
|
|
|
981
|
+
|
|
982
|
+
|
|
983
|
+
|
|
984
|
+
|
|
985
|
+
|
|
986
|
+
|
|
987
|
+
|
|
871
988
|
|
|
872
989
|
|
|
873
990
|
|
|
@@ -892,7 +1009,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
892
1009
|
}
|
|
893
1010
|
if (index < 0) return false;
|
|
894
1011
|
if (index === 0) {
|
|
895
|
-
this.
|
|
1012
|
+
this.pop();
|
|
896
1013
|
} else if (index === this.elements.length - 1) {
|
|
897
1014
|
this.elements.pop();
|
|
898
1015
|
} else {
|
|
@@ -902,13 +1019,19 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
902
1019
|
}
|
|
903
1020
|
return true;
|
|
904
1021
|
}
|
|
1022
|
+
/**
|
|
1023
|
+
* @deprecated Use `deleteWhere` instead. Will be removed in a future major version.
|
|
1024
|
+
*/
|
|
1025
|
+
deleteBy(predicate) {
|
|
1026
|
+
return this.deleteWhere(predicate);
|
|
1027
|
+
}
|
|
905
1028
|
/**
|
|
906
1029
|
* Delete the first element that matches a predicate.
|
|
907
1030
|
* @remarks Time O(N), Space O(1)
|
|
908
1031
|
* @param predicate - Function (element, index, heap) → boolean.
|
|
909
1032
|
* @returns True if an element was removed.
|
|
910
1033
|
*/
|
|
911
|
-
|
|
1034
|
+
deleteWhere(predicate) {
|
|
912
1035
|
let idx = -1;
|
|
913
1036
|
for (let i = 0; i < this.elements.length; i++) {
|
|
914
1037
|
if (predicate(this.elements[i], i, this)) {
|
|
@@ -918,7 +1041,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
918
1041
|
}
|
|
919
1042
|
if (idx < 0) return false;
|
|
920
1043
|
if (idx === 0) {
|
|
921
|
-
this.
|
|
1044
|
+
this.pop();
|
|
922
1045
|
} else if (idx === this.elements.length - 1) {
|
|
923
1046
|
this.elements.pop();
|
|
924
1047
|
} else {
|
|
@@ -961,6 +1084,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
961
1084
|
|
|
962
1085
|
|
|
963
1086
|
|
|
1087
|
+
|
|
1088
|
+
|
|
1089
|
+
|
|
1090
|
+
|
|
1091
|
+
|
|
1092
|
+
|
|
1093
|
+
|
|
964
1094
|
|
|
965
1095
|
|
|
966
1096
|
|
|
@@ -1041,6 +1171,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1041
1171
|
|
|
1042
1172
|
|
|
1043
1173
|
|
|
1174
|
+
|
|
1175
|
+
|
|
1176
|
+
|
|
1177
|
+
|
|
1178
|
+
|
|
1179
|
+
|
|
1180
|
+
|
|
1044
1181
|
|
|
1045
1182
|
|
|
1046
1183
|
|
|
@@ -1094,6 +1231,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1094
1231
|
|
|
1095
1232
|
|
|
1096
1233
|
|
|
1234
|
+
|
|
1235
|
+
|
|
1236
|
+
|
|
1237
|
+
|
|
1238
|
+
|
|
1239
|
+
|
|
1240
|
+
|
|
1097
1241
|
|
|
1098
1242
|
|
|
1099
1243
|
|
|
@@ -1146,6 +1290,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1146
1290
|
|
|
1147
1291
|
|
|
1148
1292
|
|
|
1293
|
+
|
|
1294
|
+
|
|
1295
|
+
|
|
1296
|
+
|
|
1297
|
+
|
|
1298
|
+
|
|
1299
|
+
|
|
1149
1300
|
|
|
1150
1301
|
|
|
1151
1302
|
|
|
@@ -1205,6 +1356,13 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1205
1356
|
|
|
1206
1357
|
|
|
1207
1358
|
|
|
1359
|
+
|
|
1360
|
+
|
|
1361
|
+
|
|
1362
|
+
|
|
1363
|
+
|
|
1364
|
+
|
|
1365
|
+
|
|
1208
1366
|
|
|
1209
1367
|
|
|
1210
1368
|
|
|
@@ -1390,7 +1548,7 @@ var _FibonacciHeap = class _FibonacciHeap {
|
|
|
1390
1548
|
* Push an element into the root list.
|
|
1391
1549
|
* @remarks Time O(1) amortized, Space O(1)
|
|
1392
1550
|
* @param element - Element to insert.
|
|
1393
|
-
* @returns
|
|
1551
|
+
* @returns True when the element is added.
|
|
1394
1552
|
*/
|
|
1395
1553
|
push(element) {
|
|
1396
1554
|
const node = this.createNode(element);
|
|
@@ -1399,7 +1557,7 @@ var _FibonacciHeap = class _FibonacciHeap {
|
|
|
1399
1557
|
this.mergeWithRoot(node);
|
|
1400
1558
|
if (!this.min || this.comparator(node.element, this.min.element) <= 0) this._min = node;
|
|
1401
1559
|
this._size++;
|
|
1402
|
-
return
|
|
1560
|
+
return true;
|
|
1403
1561
|
}
|
|
1404
1562
|
peek() {
|
|
1405
1563
|
return this.min ? this.min.element : void 0;
|