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/esm/index.mjs
CHANGED
|
@@ -343,6 +343,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
343
343
|
|
|
344
344
|
|
|
345
345
|
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
346
350
|
|
|
347
351
|
|
|
348
352
|
|
|
@@ -396,7 +400,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
396
400
|
}
|
|
397
401
|
/**
|
|
398
402
|
* Insert an element.
|
|
399
|
-
* @remarks Time O(
|
|
403
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
400
404
|
* @param element - Element to insert.
|
|
401
405
|
* @returns True.
|
|
402
406
|
|
|
@@ -429,6 +433,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
429
433
|
|
|
430
434
|
|
|
431
435
|
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
|
|
432
440
|
|
|
433
441
|
|
|
434
442
|
|
|
@@ -486,6 +494,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
486
494
|
|
|
487
495
|
|
|
488
496
|
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
|
|
489
501
|
|
|
490
502
|
|
|
491
503
|
|
|
@@ -546,10 +558,41 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
546
558
|
|
|
547
559
|
|
|
548
560
|
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
|
|
549
564
|
|
|
550
565
|
|
|
551
566
|
|
|
552
567
|
|
|
568
|
+
* @example
|
|
569
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
570
|
+
* interface Task {
|
|
571
|
+
* id: number;
|
|
572
|
+
* priority: number;
|
|
573
|
+
* name: string;
|
|
574
|
+
* }
|
|
575
|
+
*
|
|
576
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
577
|
+
* const tasks: Task[] = [
|
|
578
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
579
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
580
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
581
|
+
* ];
|
|
582
|
+
*
|
|
583
|
+
* const maxHeap = new Heap(tasks, {
|
|
584
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
585
|
+
* });
|
|
586
|
+
*
|
|
587
|
+
* console.log(maxHeap.size); // 3;
|
|
588
|
+
*
|
|
589
|
+
* // Peek returns highest priority task
|
|
590
|
+
* const topTask = maxHeap.peek();
|
|
591
|
+
* console.log(topTask?.priority); // 8;
|
|
592
|
+
* console.log(topTask?.name); // 'Alert';
|
|
593
|
+
*/
|
|
594
|
+
/**
|
|
595
|
+
* @deprecated Use `pop` instead. Will be removed in a future major version.
|
|
553
596
|
* @example
|
|
554
597
|
* // Heap with custom comparator (MaxHeap behavior)
|
|
555
598
|
* interface Task {
|
|
@@ -577,6 +620,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
577
620
|
* console.log(topTask?.name); // 'Alert';
|
|
578
621
|
*/
|
|
579
622
|
poll() {
|
|
623
|
+
return this.pop();
|
|
624
|
+
}
|
|
625
|
+
/**
|
|
626
|
+
* Remove and return the top element (min or max depending on comparator).
|
|
627
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
628
|
+
* @returns The removed top element, or undefined if empty.
|
|
629
|
+
*/
|
|
630
|
+
pop() {
|
|
580
631
|
if (this.elements.length === 0) return;
|
|
581
632
|
const value = this.elements[0];
|
|
582
633
|
const last = this.elements.pop();
|
|
@@ -620,6 +671,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
620
671
|
|
|
621
672
|
|
|
622
673
|
|
|
674
|
+
|
|
675
|
+
|
|
676
|
+
|
|
677
|
+
|
|
623
678
|
|
|
624
679
|
|
|
625
680
|
|
|
@@ -720,6 +775,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
720
775
|
|
|
721
776
|
|
|
722
777
|
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
|
|
781
|
+
|
|
723
782
|
|
|
724
783
|
|
|
725
784
|
|
|
@@ -767,6 +826,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
767
826
|
|
|
768
827
|
|
|
769
828
|
|
|
829
|
+
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
|
|
770
833
|
|
|
771
834
|
|
|
772
835
|
|
|
@@ -781,16 +844,6 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
781
844
|
clear() {
|
|
782
845
|
this._elements = [];
|
|
783
846
|
}
|
|
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
847
|
/**
|
|
795
848
|
* Check if an equal element exists in the heap.
|
|
796
849
|
* @remarks Time O(N), Space O(1)
|
|
@@ -817,6 +870,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
817
870
|
|
|
818
871
|
|
|
819
872
|
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
|
|
876
|
+
|
|
820
877
|
|
|
821
878
|
|
|
822
879
|
|
|
@@ -864,6 +921,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
864
921
|
|
|
865
922
|
|
|
866
923
|
|
|
924
|
+
|
|
925
|
+
|
|
926
|
+
|
|
927
|
+
|
|
867
928
|
|
|
868
929
|
|
|
869
930
|
|
|
@@ -885,7 +946,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
885
946
|
}
|
|
886
947
|
if (index < 0) return false;
|
|
887
948
|
if (index === 0) {
|
|
888
|
-
this.
|
|
949
|
+
this.pop();
|
|
889
950
|
} else if (index === this.elements.length - 1) {
|
|
890
951
|
this.elements.pop();
|
|
891
952
|
} else {
|
|
@@ -895,13 +956,19 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
895
956
|
}
|
|
896
957
|
return true;
|
|
897
958
|
}
|
|
959
|
+
/**
|
|
960
|
+
* @deprecated Use `deleteWhere` instead. Will be removed in a future major version.
|
|
961
|
+
*/
|
|
962
|
+
deleteBy(predicate) {
|
|
963
|
+
return this.deleteWhere(predicate);
|
|
964
|
+
}
|
|
898
965
|
/**
|
|
899
966
|
* Delete the first element that matches a predicate.
|
|
900
967
|
* @remarks Time O(N), Space O(1)
|
|
901
968
|
* @param predicate - Function (element, index, heap) → boolean.
|
|
902
969
|
* @returns True if an element was removed.
|
|
903
970
|
*/
|
|
904
|
-
|
|
971
|
+
deleteWhere(predicate) {
|
|
905
972
|
let idx = -1;
|
|
906
973
|
for (let i = 0; i < this.elements.length; i++) {
|
|
907
974
|
if (predicate(this.elements[i], i, this)) {
|
|
@@ -911,7 +978,7 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
911
978
|
}
|
|
912
979
|
if (idx < 0) return false;
|
|
913
980
|
if (idx === 0) {
|
|
914
|
-
this.
|
|
981
|
+
this.pop();
|
|
915
982
|
} else if (idx === this.elements.length - 1) {
|
|
916
983
|
this.elements.pop();
|
|
917
984
|
} else {
|
|
@@ -957,6 +1024,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
957
1024
|
|
|
958
1025
|
|
|
959
1026
|
|
|
1027
|
+
|
|
1028
|
+
|
|
1029
|
+
|
|
1030
|
+
|
|
960
1031
|
|
|
961
1032
|
|
|
962
1033
|
|
|
@@ -1037,6 +1108,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1037
1108
|
|
|
1038
1109
|
|
|
1039
1110
|
|
|
1111
|
+
|
|
1112
|
+
|
|
1113
|
+
|
|
1114
|
+
|
|
1040
1115
|
|
|
1041
1116
|
|
|
1042
1117
|
|
|
@@ -1090,6 +1165,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1090
1165
|
|
|
1091
1166
|
|
|
1092
1167
|
|
|
1168
|
+
|
|
1169
|
+
|
|
1170
|
+
|
|
1171
|
+
|
|
1093
1172
|
|
|
1094
1173
|
|
|
1095
1174
|
|
|
@@ -1142,6 +1221,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1142
1221
|
|
|
1143
1222
|
|
|
1144
1223
|
|
|
1224
|
+
|
|
1225
|
+
|
|
1226
|
+
|
|
1227
|
+
|
|
1145
1228
|
|
|
1146
1229
|
|
|
1147
1230
|
|
|
@@ -1201,6 +1284,10 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1201
1284
|
|
|
1202
1285
|
|
|
1203
1286
|
|
|
1287
|
+
|
|
1288
|
+
|
|
1289
|
+
|
|
1290
|
+
|
|
1204
1291
|
|
|
1205
1292
|
|
|
1206
1293
|
|
|
@@ -1394,7 +1481,7 @@ var FibonacciHeap = class {
|
|
|
1394
1481
|
* Push an element into the root list.
|
|
1395
1482
|
* @remarks Time O(1) amortized, Space O(1)
|
|
1396
1483
|
* @param element - Element to insert.
|
|
1397
|
-
* @returns
|
|
1484
|
+
* @returns True when the element is added.
|
|
1398
1485
|
*/
|
|
1399
1486
|
push(element) {
|
|
1400
1487
|
const node = this.createNode(element);
|
|
@@ -1403,7 +1490,7 @@ var FibonacciHeap = class {
|
|
|
1403
1490
|
this.mergeWithRoot(node);
|
|
1404
1491
|
if (!this.min || this.comparator(node.element, this.min.element) <= 0) this._min = node;
|
|
1405
1492
|
this._size++;
|
|
1406
|
-
return
|
|
1493
|
+
return true;
|
|
1407
1494
|
}
|
|
1408
1495
|
peek() {
|
|
1409
1496
|
return this.min ? this.min.element : void 0;
|