max-priority-queue-typed 2.5.2 → 2.5.3
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 +103 -16
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +103 -16
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +103 -16
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +103 -16
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +50 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +56 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +116 -15
- package/dist/types/data-structures/binary-tree/bst.d.ts +99 -3
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +79 -8
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +24 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +520 -1
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +489 -1
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +393 -1
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +500 -1
- package/dist/types/data-structures/graph/directed-graph.d.ts +40 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +36 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +51 -6
- package/dist/types/data-structures/heap/heap.d.ts +98 -12
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +75 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +61 -1
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +72 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +32 -0
- package/dist/types/data-structures/queue/deque.d.ts +82 -0
- package/dist/types/data-structures/queue/queue.d.ts +61 -0
- package/dist/types/data-structures/stack/stack.d.ts +42 -2
- package/dist/types/data-structures/trie/trie.d.ts +48 -0
- package/dist/types/interfaces/binary-tree.d.ts +2 -3
- package/dist/umd/max-priority-queue-typed.js +103 -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/binary-tree/avl-tree.ts +52 -5
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +56 -0
- package/src/data-structures/binary-tree/binary-tree.ts +167 -81
- package/src/data-structures/binary-tree/bst.ts +101 -7
- package/src/data-structures/binary-tree/red-black-tree.ts +82 -15
- package/src/data-structures/binary-tree/segment-tree.ts +24 -0
- package/src/data-structures/binary-tree/tree-map.ts +540 -3
- package/src/data-structures/binary-tree/tree-multi-map.ts +490 -2
- package/src/data-structures/binary-tree/tree-multi-set.ts +393 -1
- package/src/data-structures/binary-tree/tree-set.ts +520 -3
- package/src/data-structures/graph/directed-graph.ts +41 -1
- package/src/data-structures/graph/undirected-graph.ts +37 -1
- package/src/data-structures/hash/hash-map.ts +67 -12
- package/src/data-structures/heap/heap.ts +107 -19
- package/src/data-structures/linked-list/doubly-linked-list.ts +88 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +61 -1
- package/src/data-structures/linked-list/skip-linked-list.ts +72 -0
- package/src/data-structures/matrix/matrix.ts +32 -0
- package/src/data-structures/queue/deque.ts +85 -0
- package/src/data-structures/queue/queue.ts +73 -0
- package/src/data-structures/stack/stack.ts +45 -5
- package/src/data-structures/trie/trie.ts +48 -0
- package/src/interfaces/binary-tree.ts +1 -9
package/dist/cjs/index.cjs
CHANGED
|
@@ -345,6 +345,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
345
345
|
|
|
346
346
|
|
|
347
347
|
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
348
352
|
|
|
349
353
|
|
|
350
354
|
|
|
@@ -398,7 +402,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
398
402
|
}
|
|
399
403
|
/**
|
|
400
404
|
* Insert an element.
|
|
401
|
-
* @remarks Time O(
|
|
405
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
402
406
|
* @param element - Element to insert.
|
|
403
407
|
* @returns True.
|
|
404
408
|
|
|
@@ -431,6 +435,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
431
435
|
|
|
432
436
|
|
|
433
437
|
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
|
|
434
442
|
|
|
435
443
|
|
|
436
444
|
|
|
@@ -488,6 +496,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
488
496
|
|
|
489
497
|
|
|
490
498
|
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
491
503
|
|
|
492
504
|
|
|
493
505
|
|
|
@@ -548,10 +560,41 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
548
560
|
|
|
549
561
|
|
|
550
562
|
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
|
|
551
566
|
|
|
552
567
|
|
|
553
568
|
|
|
554
569
|
|
|
570
|
+
* @example
|
|
571
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
572
|
+
* interface Task {
|
|
573
|
+
* id: number;
|
|
574
|
+
* priority: number;
|
|
575
|
+
* name: string;
|
|
576
|
+
* }
|
|
577
|
+
*
|
|
578
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
579
|
+
* const tasks: Task[] = [
|
|
580
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
581
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
582
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
583
|
+
* ];
|
|
584
|
+
*
|
|
585
|
+
* const maxHeap = new Heap(tasks, {
|
|
586
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
587
|
+
* });
|
|
588
|
+
*
|
|
589
|
+
* console.log(maxHeap.size); // 3;
|
|
590
|
+
*
|
|
591
|
+
* // Peek returns highest priority task
|
|
592
|
+
* const topTask = maxHeap.peek();
|
|
593
|
+
* console.log(topTask?.priority); // 8;
|
|
594
|
+
* console.log(topTask?.name); // 'Alert';
|
|
595
|
+
*/
|
|
596
|
+
/**
|
|
597
|
+
* @deprecated Use `pop` instead. Will be removed in a future major version.
|
|
555
598
|
* @example
|
|
556
599
|
* // Heap with custom comparator (MaxHeap behavior)
|
|
557
600
|
* interface Task {
|
|
@@ -579,6 +622,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
579
622
|
* console.log(topTask?.name); // 'Alert';
|
|
580
623
|
*/
|
|
581
624
|
poll() {
|
|
625
|
+
return this.pop();
|
|
626
|
+
}
|
|
627
|
+
/**
|
|
628
|
+
* Remove and return the top element (min or max depending on comparator).
|
|
629
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
630
|
+
* @returns The removed top element, or undefined if empty.
|
|
631
|
+
*/
|
|
632
|
+
pop() {
|
|
582
633
|
if (this.elements.length === 0) return;
|
|
583
634
|
const value = this.elements[0];
|
|
584
635
|
const last = this.elements.pop();
|
|
@@ -622,6 +673,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
622
673
|
|
|
623
674
|
|
|
624
675
|
|
|
676
|
+
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
|
|
625
680
|
|
|
626
681
|
|
|
627
682
|
|
|
@@ -722,6 +777,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
722
777
|
|
|
723
778
|
|
|
724
779
|
|
|
780
|
+
|
|
781
|
+
|
|
782
|
+
|
|
783
|
+
|
|
725
784
|
|
|
726
785
|
|
|
727
786
|
|
|
@@ -769,6 +828,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
769
828
|
|
|
770
829
|
|
|
771
830
|
|
|
831
|
+
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
|
|
772
835
|
|
|
773
836
|
|
|
774
837
|
|
|
@@ -783,16 +846,6 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
783
846
|
clear() {
|
|
784
847
|
this._elements = [];
|
|
785
848
|
}
|
|
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
849
|
/**
|
|
797
850
|
* Check if an equal element exists in the heap.
|
|
798
851
|
* @remarks Time O(N), Space O(1)
|
|
@@ -819,6 +872,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
819
872
|
|
|
820
873
|
|
|
821
874
|
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
|
|
878
|
+
|
|
822
879
|
|
|
823
880
|
|
|
824
881
|
|
|
@@ -866,6 +923,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
866
923
|
|
|
867
924
|
|
|
868
925
|
|
|
926
|
+
|
|
927
|
+
|
|
928
|
+
|
|
929
|
+
|
|
869
930
|
|
|
870
931
|
|
|
871
932
|
|
|
@@ -887,7 +948,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
887
948
|
}
|
|
888
949
|
if (index < 0) return false;
|
|
889
950
|
if (index === 0) {
|
|
890
|
-
this.
|
|
951
|
+
this.pop();
|
|
891
952
|
} else if (index === this.elements.length - 1) {
|
|
892
953
|
this.elements.pop();
|
|
893
954
|
} else {
|
|
@@ -897,13 +958,19 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
897
958
|
}
|
|
898
959
|
return true;
|
|
899
960
|
}
|
|
961
|
+
/**
|
|
962
|
+
* @deprecated Use `deleteWhere` instead. Will be removed in a future major version.
|
|
963
|
+
*/
|
|
964
|
+
deleteBy(predicate) {
|
|
965
|
+
return this.deleteWhere(predicate);
|
|
966
|
+
}
|
|
900
967
|
/**
|
|
901
968
|
* Delete the first element that matches a predicate.
|
|
902
969
|
* @remarks Time O(N), Space O(1)
|
|
903
970
|
* @param predicate - Function (element, index, heap) → boolean.
|
|
904
971
|
* @returns True if an element was removed.
|
|
905
972
|
*/
|
|
906
|
-
|
|
973
|
+
deleteWhere(predicate) {
|
|
907
974
|
let idx = -1;
|
|
908
975
|
for (let i = 0; i < this.elements.length; i++) {
|
|
909
976
|
if (predicate(this.elements[i], i, this)) {
|
|
@@ -913,7 +980,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
913
980
|
}
|
|
914
981
|
if (idx < 0) return false;
|
|
915
982
|
if (idx === 0) {
|
|
916
|
-
this.
|
|
983
|
+
this.pop();
|
|
917
984
|
} else if (idx === this.elements.length - 1) {
|
|
918
985
|
this.elements.pop();
|
|
919
986
|
} else {
|
|
@@ -959,6 +1026,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
959
1026
|
|
|
960
1027
|
|
|
961
1028
|
|
|
1029
|
+
|
|
1030
|
+
|
|
1031
|
+
|
|
1032
|
+
|
|
962
1033
|
|
|
963
1034
|
|
|
964
1035
|
|
|
@@ -1039,6 +1110,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1039
1110
|
|
|
1040
1111
|
|
|
1041
1112
|
|
|
1113
|
+
|
|
1114
|
+
|
|
1115
|
+
|
|
1116
|
+
|
|
1042
1117
|
|
|
1043
1118
|
|
|
1044
1119
|
|
|
@@ -1092,6 +1167,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1092
1167
|
|
|
1093
1168
|
|
|
1094
1169
|
|
|
1170
|
+
|
|
1171
|
+
|
|
1172
|
+
|
|
1173
|
+
|
|
1095
1174
|
|
|
1096
1175
|
|
|
1097
1176
|
|
|
@@ -1144,6 +1223,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1144
1223
|
|
|
1145
1224
|
|
|
1146
1225
|
|
|
1226
|
+
|
|
1227
|
+
|
|
1228
|
+
|
|
1229
|
+
|
|
1147
1230
|
|
|
1148
1231
|
|
|
1149
1232
|
|
|
@@ -1203,6 +1286,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1203
1286
|
|
|
1204
1287
|
|
|
1205
1288
|
|
|
1289
|
+
|
|
1290
|
+
|
|
1291
|
+
|
|
1292
|
+
|
|
1206
1293
|
|
|
1207
1294
|
|
|
1208
1295
|
|
|
@@ -1396,7 +1483,7 @@ var FibonacciHeap = class {
|
|
|
1396
1483
|
* Push an element into the root list.
|
|
1397
1484
|
* @remarks Time O(1) amortized, Space O(1)
|
|
1398
1485
|
* @param element - Element to insert.
|
|
1399
|
-
* @returns
|
|
1486
|
+
* @returns True when the element is added.
|
|
1400
1487
|
*/
|
|
1401
1488
|
push(element) {
|
|
1402
1489
|
const node = this.createNode(element);
|
|
@@ -1405,7 +1492,7 @@ var FibonacciHeap = class {
|
|
|
1405
1492
|
this.mergeWithRoot(node);
|
|
1406
1493
|
if (!this.min || this.comparator(node.element, this.min.element) <= 0) this._min = node;
|
|
1407
1494
|
this._size++;
|
|
1408
|
-
return
|
|
1495
|
+
return true;
|
|
1409
1496
|
}
|
|
1410
1497
|
peek() {
|
|
1411
1498
|
return this.min ? this.min.element : void 0;
|