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
package/dist/esm/index.mjs
CHANGED
|
@@ -208,6 +208,35 @@ var IterableElementBase = class {
|
|
|
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
|
*
|
|
@@ -340,6 +369,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
340
369
|
|
|
341
370
|
|
|
342
371
|
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
343
379
|
|
|
344
380
|
|
|
345
381
|
|
|
@@ -396,7 +432,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
396
432
|
}
|
|
397
433
|
/**
|
|
398
434
|
* Insert an element.
|
|
399
|
-
* @remarks Time O(
|
|
435
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
400
436
|
* @param element - Element to insert.
|
|
401
437
|
* @returns True.
|
|
402
438
|
|
|
@@ -426,6 +462,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
426
462
|
|
|
427
463
|
|
|
428
464
|
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
|
|
429
472
|
|
|
430
473
|
|
|
431
474
|
|
|
@@ -483,6 +526,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
483
526
|
|
|
484
527
|
|
|
485
528
|
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
|
|
486
536
|
|
|
487
537
|
|
|
488
538
|
|
|
@@ -547,6 +597,40 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
547
597
|
|
|
548
598
|
|
|
549
599
|
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
* @example
|
|
607
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
608
|
+
* interface Task {
|
|
609
|
+
* id: number;
|
|
610
|
+
* priority: number;
|
|
611
|
+
* name: string;
|
|
612
|
+
* }
|
|
613
|
+
*
|
|
614
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
615
|
+
* const tasks: Task[] = [
|
|
616
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
617
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
618
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
619
|
+
* ];
|
|
620
|
+
*
|
|
621
|
+
* const maxHeap = new Heap(tasks, {
|
|
622
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
623
|
+
* });
|
|
624
|
+
*
|
|
625
|
+
* console.log(maxHeap.size); // 3;
|
|
626
|
+
*
|
|
627
|
+
* // Peek returns highest priority task
|
|
628
|
+
* const topTask = maxHeap.peek();
|
|
629
|
+
* console.log(topTask?.priority); // 8;
|
|
630
|
+
* console.log(topTask?.name); // 'Alert';
|
|
631
|
+
*/
|
|
632
|
+
/**
|
|
633
|
+
* @deprecated Use `pop` instead. Will be removed in a future major version.
|
|
550
634
|
|
|
551
635
|
|
|
552
636
|
|
|
@@ -577,6 +661,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
577
661
|
* console.log(topTask?.name); // 'Alert';
|
|
578
662
|
*/
|
|
579
663
|
poll() {
|
|
664
|
+
return this.pop();
|
|
665
|
+
}
|
|
666
|
+
/**
|
|
667
|
+
* Remove and return the top element (min or max depending on comparator).
|
|
668
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
669
|
+
* @returns The removed top element, or undefined if empty.
|
|
670
|
+
*/
|
|
671
|
+
pop() {
|
|
580
672
|
if (this.elements.length === 0) return;
|
|
581
673
|
const value = this.elements[0];
|
|
582
674
|
const last = this.elements.pop();
|
|
@@ -617,6 +709,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
617
709
|
|
|
618
710
|
|
|
619
711
|
|
|
712
|
+
|
|
713
|
+
|
|
714
|
+
|
|
715
|
+
|
|
716
|
+
|
|
717
|
+
|
|
718
|
+
|
|
620
719
|
|
|
621
720
|
|
|
622
721
|
|
|
@@ -717,6 +816,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
717
816
|
|
|
718
817
|
|
|
719
818
|
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
|
|
825
|
+
|
|
720
826
|
|
|
721
827
|
|
|
722
828
|
|
|
@@ -764,6 +870,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
764
870
|
|
|
765
871
|
|
|
766
872
|
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
|
|
878
|
+
|
|
879
|
+
|
|
767
880
|
|
|
768
881
|
|
|
769
882
|
|
|
@@ -781,16 +894,6 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
781
894
|
clear() {
|
|
782
895
|
this._elements = [];
|
|
783
896
|
}
|
|
784
|
-
/**
|
|
785
|
-
* Replace the backing array and rebuild the heap.
|
|
786
|
-
* @remarks Time O(N), Space O(N)
|
|
787
|
-
* @param elements - Iterable used to refill the heap.
|
|
788
|
-
* @returns Array of per-node results from fixing steps.
|
|
789
|
-
*/
|
|
790
|
-
refill(elements) {
|
|
791
|
-
this._elements = Array.from(elements);
|
|
792
|
-
return this.fix();
|
|
793
|
-
}
|
|
794
897
|
/**
|
|
795
898
|
* Check if an equal element exists in the heap.
|
|
796
899
|
* @remarks Time O(N), Space O(1)
|
|
@@ -814,6 +917,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
814
917
|
|
|
815
918
|
|
|
816
919
|
|
|
920
|
+
|
|
921
|
+
|
|
922
|
+
|
|
923
|
+
|
|
924
|
+
|
|
925
|
+
|
|
926
|
+
|
|
817
927
|
|
|
818
928
|
|
|
819
929
|
|
|
@@ -861,6 +971,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
861
971
|
|
|
862
972
|
|
|
863
973
|
|
|
974
|
+
|
|
975
|
+
|
|
976
|
+
|
|
977
|
+
|
|
978
|
+
|
|
979
|
+
|
|
980
|
+
|
|
864
981
|
|
|
865
982
|
|
|
866
983
|
|
|
@@ -885,7 +1002,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
885
1002
|
}
|
|
886
1003
|
if (index < 0) return false;
|
|
887
1004
|
if (index === 0) {
|
|
888
|
-
this.
|
|
1005
|
+
this.pop();
|
|
889
1006
|
} else if (index === this.elements.length - 1) {
|
|
890
1007
|
this.elements.pop();
|
|
891
1008
|
} else {
|
|
@@ -895,13 +1012,19 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
895
1012
|
}
|
|
896
1013
|
return true;
|
|
897
1014
|
}
|
|
1015
|
+
/**
|
|
1016
|
+
* @deprecated Use `deleteWhere` instead. Will be removed in a future major version.
|
|
1017
|
+
*/
|
|
1018
|
+
deleteBy(predicate) {
|
|
1019
|
+
return this.deleteWhere(predicate);
|
|
1020
|
+
}
|
|
898
1021
|
/**
|
|
899
1022
|
* Delete the first element that matches a predicate.
|
|
900
1023
|
* @remarks Time O(N), Space O(1)
|
|
901
1024
|
* @param predicate - Function (element, index, heap) → boolean.
|
|
902
1025
|
* @returns True if an element was removed.
|
|
903
1026
|
*/
|
|
904
|
-
|
|
1027
|
+
deleteWhere(predicate) {
|
|
905
1028
|
let idx = -1;
|
|
906
1029
|
for (let i = 0; i < this.elements.length; i++) {
|
|
907
1030
|
if (predicate(this.elements[i], i, this)) {
|
|
@@ -911,7 +1034,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
911
1034
|
}
|
|
912
1035
|
if (idx < 0) return false;
|
|
913
1036
|
if (idx === 0) {
|
|
914
|
-
this.
|
|
1037
|
+
this.pop();
|
|
915
1038
|
} else if (idx === this.elements.length - 1) {
|
|
916
1039
|
this.elements.pop();
|
|
917
1040
|
} else {
|
|
@@ -954,6 +1077,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
954
1077
|
|
|
955
1078
|
|
|
956
1079
|
|
|
1080
|
+
|
|
1081
|
+
|
|
1082
|
+
|
|
1083
|
+
|
|
1084
|
+
|
|
1085
|
+
|
|
1086
|
+
|
|
957
1087
|
|
|
958
1088
|
|
|
959
1089
|
|
|
@@ -1034,6 +1164,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1034
1164
|
|
|
1035
1165
|
|
|
1036
1166
|
|
|
1167
|
+
|
|
1168
|
+
|
|
1169
|
+
|
|
1170
|
+
|
|
1171
|
+
|
|
1172
|
+
|
|
1173
|
+
|
|
1037
1174
|
|
|
1038
1175
|
|
|
1039
1176
|
|
|
@@ -1087,6 +1224,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1087
1224
|
|
|
1088
1225
|
|
|
1089
1226
|
|
|
1227
|
+
|
|
1228
|
+
|
|
1229
|
+
|
|
1230
|
+
|
|
1231
|
+
|
|
1232
|
+
|
|
1233
|
+
|
|
1090
1234
|
|
|
1091
1235
|
|
|
1092
1236
|
|
|
@@ -1139,6 +1283,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1139
1283
|
|
|
1140
1284
|
|
|
1141
1285
|
|
|
1286
|
+
|
|
1287
|
+
|
|
1288
|
+
|
|
1289
|
+
|
|
1290
|
+
|
|
1291
|
+
|
|
1292
|
+
|
|
1142
1293
|
|
|
1143
1294
|
|
|
1144
1295
|
|
|
@@ -1198,6 +1349,13 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1198
1349
|
|
|
1199
1350
|
|
|
1200
1351
|
|
|
1352
|
+
|
|
1353
|
+
|
|
1354
|
+
|
|
1355
|
+
|
|
1356
|
+
|
|
1357
|
+
|
|
1358
|
+
|
|
1201
1359
|
|
|
1202
1360
|
|
|
1203
1361
|
|
|
@@ -1394,7 +1552,7 @@ var FibonacciHeap = class {
|
|
|
1394
1552
|
* Push an element into the root list.
|
|
1395
1553
|
* @remarks Time O(1) amortized, Space O(1)
|
|
1396
1554
|
* @param element - Element to insert.
|
|
1397
|
-
* @returns
|
|
1555
|
+
* @returns True when the element is added.
|
|
1398
1556
|
*/
|
|
1399
1557
|
push(element) {
|
|
1400
1558
|
const node = this.createNode(element);
|
|
@@ -1403,7 +1561,7 @@ var FibonacciHeap = class {
|
|
|
1403
1561
|
this.mergeWithRoot(node);
|
|
1404
1562
|
if (!this.min || this.comparator(node.element, this.min.element) <= 0) this._min = node;
|
|
1405
1563
|
this._size++;
|
|
1406
|
-
return
|
|
1564
|
+
return true;
|
|
1407
1565
|
}
|
|
1408
1566
|
peek() {
|
|
1409
1567
|
return this.min ? this.min.element : void 0;
|