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/priority-queue-typed.js +103 -16
- package/dist/umd/priority-queue-typed.js.map +1 -1
- package/dist/umd/priority-queue-typed.min.js +1 -1
- package/dist/umd/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
|
@@ -378,6 +378,10 @@ var priorityQueueTyped = (() => {
|
|
|
378
378
|
|
|
379
379
|
|
|
380
380
|
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
381
385
|
|
|
382
386
|
|
|
383
387
|
|
|
@@ -432,7 +436,7 @@ var priorityQueueTyped = (() => {
|
|
|
432
436
|
}
|
|
433
437
|
/**
|
|
434
438
|
* Insert an element.
|
|
435
|
-
* @remarks Time O(
|
|
439
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
436
440
|
* @param element - Element to insert.
|
|
437
441
|
* @returns True.
|
|
438
442
|
|
|
@@ -465,6 +469,10 @@ var priorityQueueTyped = (() => {
|
|
|
465
469
|
|
|
466
470
|
|
|
467
471
|
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
468
476
|
|
|
469
477
|
|
|
470
478
|
|
|
@@ -522,6 +530,10 @@ var priorityQueueTyped = (() => {
|
|
|
522
530
|
|
|
523
531
|
|
|
524
532
|
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
|
|
525
537
|
|
|
526
538
|
|
|
527
539
|
|
|
@@ -582,10 +594,41 @@ var priorityQueueTyped = (() => {
|
|
|
582
594
|
|
|
583
595
|
|
|
584
596
|
|
|
597
|
+
|
|
598
|
+
|
|
599
|
+
|
|
585
600
|
|
|
586
601
|
|
|
587
602
|
|
|
588
603
|
|
|
604
|
+
* @example
|
|
605
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
606
|
+
* interface Task {
|
|
607
|
+
* id: number;
|
|
608
|
+
* priority: number;
|
|
609
|
+
* name: string;
|
|
610
|
+
* }
|
|
611
|
+
*
|
|
612
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
613
|
+
* const tasks: Task[] = [
|
|
614
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
615
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
616
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
617
|
+
* ];
|
|
618
|
+
*
|
|
619
|
+
* const maxHeap = new Heap(tasks, {
|
|
620
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
621
|
+
* });
|
|
622
|
+
*
|
|
623
|
+
* console.log(maxHeap.size); // 3;
|
|
624
|
+
*
|
|
625
|
+
* // Peek returns highest priority task
|
|
626
|
+
* const topTask = maxHeap.peek();
|
|
627
|
+
* console.log(topTask?.priority); // 8;
|
|
628
|
+
* console.log(topTask?.name); // 'Alert';
|
|
629
|
+
*/
|
|
630
|
+
/**
|
|
631
|
+
* @deprecated Use `pop` instead. Will be removed in a future major version.
|
|
589
632
|
* @example
|
|
590
633
|
* // Heap with custom comparator (MaxHeap behavior)
|
|
591
634
|
* interface Task {
|
|
@@ -613,6 +656,14 @@ var priorityQueueTyped = (() => {
|
|
|
613
656
|
* console.log(topTask?.name); // 'Alert';
|
|
614
657
|
*/
|
|
615
658
|
poll() {
|
|
659
|
+
return this.pop();
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* Remove and return the top element (min or max depending on comparator).
|
|
663
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
664
|
+
* @returns The removed top element, or undefined if empty.
|
|
665
|
+
*/
|
|
666
|
+
pop() {
|
|
616
667
|
if (this.elements.length === 0) return;
|
|
617
668
|
const value = this.elements[0];
|
|
618
669
|
const last = this.elements.pop();
|
|
@@ -656,6 +707,10 @@ var priorityQueueTyped = (() => {
|
|
|
656
707
|
|
|
657
708
|
|
|
658
709
|
|
|
710
|
+
|
|
711
|
+
|
|
712
|
+
|
|
713
|
+
|
|
659
714
|
|
|
660
715
|
|
|
661
716
|
|
|
@@ -756,6 +811,10 @@ var priorityQueueTyped = (() => {
|
|
|
756
811
|
|
|
757
812
|
|
|
758
813
|
|
|
814
|
+
|
|
815
|
+
|
|
816
|
+
|
|
817
|
+
|
|
759
818
|
|
|
760
819
|
|
|
761
820
|
|
|
@@ -803,6 +862,10 @@ var priorityQueueTyped = (() => {
|
|
|
803
862
|
|
|
804
863
|
|
|
805
864
|
|
|
865
|
+
|
|
866
|
+
|
|
867
|
+
|
|
868
|
+
|
|
806
869
|
|
|
807
870
|
|
|
808
871
|
|
|
@@ -817,16 +880,6 @@ var priorityQueueTyped = (() => {
|
|
|
817
880
|
clear() {
|
|
818
881
|
this._elements = [];
|
|
819
882
|
}
|
|
820
|
-
/**
|
|
821
|
-
* Replace the backing array and rebuild the heap.
|
|
822
|
-
* @remarks Time O(N), Space O(N)
|
|
823
|
-
* @param elements - Iterable used to refill the heap.
|
|
824
|
-
* @returns Array of per-node results from fixing steps.
|
|
825
|
-
*/
|
|
826
|
-
refill(elements) {
|
|
827
|
-
this._elements = Array.from(elements);
|
|
828
|
-
return this.fix();
|
|
829
|
-
}
|
|
830
883
|
/**
|
|
831
884
|
* Check if an equal element exists in the heap.
|
|
832
885
|
* @remarks Time O(N), Space O(1)
|
|
@@ -853,6 +906,10 @@ var priorityQueueTyped = (() => {
|
|
|
853
906
|
|
|
854
907
|
|
|
855
908
|
|
|
909
|
+
|
|
910
|
+
|
|
911
|
+
|
|
912
|
+
|
|
856
913
|
|
|
857
914
|
|
|
858
915
|
|
|
@@ -900,6 +957,10 @@ var priorityQueueTyped = (() => {
|
|
|
900
957
|
|
|
901
958
|
|
|
902
959
|
|
|
960
|
+
|
|
961
|
+
|
|
962
|
+
|
|
963
|
+
|
|
903
964
|
|
|
904
965
|
|
|
905
966
|
|
|
@@ -921,7 +982,7 @@ var priorityQueueTyped = (() => {
|
|
|
921
982
|
}
|
|
922
983
|
if (index < 0) return false;
|
|
923
984
|
if (index === 0) {
|
|
924
|
-
this.
|
|
985
|
+
this.pop();
|
|
925
986
|
} else if (index === this.elements.length - 1) {
|
|
926
987
|
this.elements.pop();
|
|
927
988
|
} else {
|
|
@@ -931,13 +992,19 @@ var priorityQueueTyped = (() => {
|
|
|
931
992
|
}
|
|
932
993
|
return true;
|
|
933
994
|
}
|
|
995
|
+
/**
|
|
996
|
+
* @deprecated Use `deleteWhere` instead. Will be removed in a future major version.
|
|
997
|
+
*/
|
|
998
|
+
deleteBy(predicate) {
|
|
999
|
+
return this.deleteWhere(predicate);
|
|
1000
|
+
}
|
|
934
1001
|
/**
|
|
935
1002
|
* Delete the first element that matches a predicate.
|
|
936
1003
|
* @remarks Time O(N), Space O(1)
|
|
937
1004
|
* @param predicate - Function (element, index, heap) → boolean.
|
|
938
1005
|
* @returns True if an element was removed.
|
|
939
1006
|
*/
|
|
940
|
-
|
|
1007
|
+
deleteWhere(predicate) {
|
|
941
1008
|
let idx = -1;
|
|
942
1009
|
for (let i = 0; i < this.elements.length; i++) {
|
|
943
1010
|
if (predicate(this.elements[i], i, this)) {
|
|
@@ -947,7 +1014,7 @@ var priorityQueueTyped = (() => {
|
|
|
947
1014
|
}
|
|
948
1015
|
if (idx < 0) return false;
|
|
949
1016
|
if (idx === 0) {
|
|
950
|
-
this.
|
|
1017
|
+
this.pop();
|
|
951
1018
|
} else if (idx === this.elements.length - 1) {
|
|
952
1019
|
this.elements.pop();
|
|
953
1020
|
} else {
|
|
@@ -993,6 +1060,10 @@ var priorityQueueTyped = (() => {
|
|
|
993
1060
|
|
|
994
1061
|
|
|
995
1062
|
|
|
1063
|
+
|
|
1064
|
+
|
|
1065
|
+
|
|
1066
|
+
|
|
996
1067
|
|
|
997
1068
|
|
|
998
1069
|
|
|
@@ -1073,6 +1144,10 @@ var priorityQueueTyped = (() => {
|
|
|
1073
1144
|
|
|
1074
1145
|
|
|
1075
1146
|
|
|
1147
|
+
|
|
1148
|
+
|
|
1149
|
+
|
|
1150
|
+
|
|
1076
1151
|
|
|
1077
1152
|
|
|
1078
1153
|
|
|
@@ -1126,6 +1201,10 @@ var priorityQueueTyped = (() => {
|
|
|
1126
1201
|
|
|
1127
1202
|
|
|
1128
1203
|
|
|
1204
|
+
|
|
1205
|
+
|
|
1206
|
+
|
|
1207
|
+
|
|
1129
1208
|
|
|
1130
1209
|
|
|
1131
1210
|
|
|
@@ -1178,6 +1257,10 @@ var priorityQueueTyped = (() => {
|
|
|
1178
1257
|
|
|
1179
1258
|
|
|
1180
1259
|
|
|
1260
|
+
|
|
1261
|
+
|
|
1262
|
+
|
|
1263
|
+
|
|
1181
1264
|
|
|
1182
1265
|
|
|
1183
1266
|
|
|
@@ -1237,6 +1320,10 @@ var priorityQueueTyped = (() => {
|
|
|
1237
1320
|
|
|
1238
1321
|
|
|
1239
1322
|
|
|
1323
|
+
|
|
1324
|
+
|
|
1325
|
+
|
|
1326
|
+
|
|
1240
1327
|
|
|
1241
1328
|
|
|
1242
1329
|
|
|
@@ -1415,7 +1502,7 @@ var priorityQueueTyped = (() => {
|
|
|
1415
1502
|
* Push an element into the root list.
|
|
1416
1503
|
* @remarks Time O(1) amortized, Space O(1)
|
|
1417
1504
|
* @param element - Element to insert.
|
|
1418
|
-
* @returns
|
|
1505
|
+
* @returns True when the element is added.
|
|
1419
1506
|
*/
|
|
1420
1507
|
push(element) {
|
|
1421
1508
|
const node = this.createNode(element);
|
|
@@ -1424,7 +1511,7 @@ var priorityQueueTyped = (() => {
|
|
|
1424
1511
|
this.mergeWithRoot(node);
|
|
1425
1512
|
if (!this.min || this.comparator(node.element, this.min.element) <= 0) this._min = node;
|
|
1426
1513
|
this._size++;
|
|
1427
|
-
return
|
|
1514
|
+
return true;
|
|
1428
1515
|
}
|
|
1429
1516
|
peek() {
|
|
1430
1517
|
return this.min ? this.min.element : void 0;
|