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
|
@@ -349,6 +349,10 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
349
349
|
|
|
350
350
|
|
|
351
351
|
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
|
|
352
356
|
|
|
353
357
|
|
|
354
358
|
|
|
@@ -403,7 +407,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
403
407
|
}
|
|
404
408
|
/**
|
|
405
409
|
* Insert an element.
|
|
406
|
-
* @remarks Time O(
|
|
410
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
407
411
|
* @param element - Element to insert.
|
|
408
412
|
* @returns True.
|
|
409
413
|
|
|
@@ -436,6 +440,10 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
436
440
|
|
|
437
441
|
|
|
438
442
|
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
|
|
439
447
|
|
|
440
448
|
|
|
441
449
|
|
|
@@ -493,6 +501,10 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
493
501
|
|
|
494
502
|
|
|
495
503
|
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
|
|
496
508
|
|
|
497
509
|
|
|
498
510
|
|
|
@@ -553,10 +565,41 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
553
565
|
|
|
554
566
|
|
|
555
567
|
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
|
|
556
571
|
|
|
557
572
|
|
|
558
573
|
|
|
559
574
|
|
|
575
|
+
* @example
|
|
576
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
577
|
+
* interface Task {
|
|
578
|
+
* id: number;
|
|
579
|
+
* priority: number;
|
|
580
|
+
* name: string;
|
|
581
|
+
* }
|
|
582
|
+
*
|
|
583
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
584
|
+
* const tasks: Task[] = [
|
|
585
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
586
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
587
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
588
|
+
* ];
|
|
589
|
+
*
|
|
590
|
+
* const maxHeap = new Heap(tasks, {
|
|
591
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
592
|
+
* });
|
|
593
|
+
*
|
|
594
|
+
* console.log(maxHeap.size); // 3;
|
|
595
|
+
*
|
|
596
|
+
* // Peek returns highest priority task
|
|
597
|
+
* const topTask = maxHeap.peek();
|
|
598
|
+
* console.log(topTask?.priority); // 8;
|
|
599
|
+
* console.log(topTask?.name); // 'Alert';
|
|
600
|
+
*/
|
|
601
|
+
/**
|
|
602
|
+
* @deprecated Use `pop` instead. Will be removed in a future major version.
|
|
560
603
|
* @example
|
|
561
604
|
* // Heap with custom comparator (MaxHeap behavior)
|
|
562
605
|
* interface Task {
|
|
@@ -584,6 +627,14 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
584
627
|
* console.log(topTask?.name); // 'Alert';
|
|
585
628
|
*/
|
|
586
629
|
poll() {
|
|
630
|
+
return this.pop();
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
633
|
+
* Remove and return the top element (min or max depending on comparator).
|
|
634
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
635
|
+
* @returns The removed top element, or undefined if empty.
|
|
636
|
+
*/
|
|
637
|
+
pop() {
|
|
587
638
|
if (this.elements.length === 0) return;
|
|
588
639
|
const value = this.elements[0];
|
|
589
640
|
const last = this.elements.pop();
|
|
@@ -627,6 +678,10 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
627
678
|
|
|
628
679
|
|
|
629
680
|
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
|
|
630
685
|
|
|
631
686
|
|
|
632
687
|
|
|
@@ -727,6 +782,10 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
727
782
|
|
|
728
783
|
|
|
729
784
|
|
|
785
|
+
|
|
786
|
+
|
|
787
|
+
|
|
788
|
+
|
|
730
789
|
|
|
731
790
|
|
|
732
791
|
|
|
@@ -774,6 +833,10 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
774
833
|
|
|
775
834
|
|
|
776
835
|
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
|
|
839
|
+
|
|
777
840
|
|
|
778
841
|
|
|
779
842
|
|
|
@@ -788,16 +851,6 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
788
851
|
clear() {
|
|
789
852
|
this._elements = [];
|
|
790
853
|
}
|
|
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
854
|
/**
|
|
802
855
|
* Check if an equal element exists in the heap.
|
|
803
856
|
* @remarks Time O(N), Space O(1)
|
|
@@ -824,6 +877,10 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
824
877
|
|
|
825
878
|
|
|
826
879
|
|
|
880
|
+
|
|
881
|
+
|
|
882
|
+
|
|
883
|
+
|
|
827
884
|
|
|
828
885
|
|
|
829
886
|
|
|
@@ -871,6 +928,10 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
871
928
|
|
|
872
929
|
|
|
873
930
|
|
|
931
|
+
|
|
932
|
+
|
|
933
|
+
|
|
934
|
+
|
|
874
935
|
|
|
875
936
|
|
|
876
937
|
|
|
@@ -892,7 +953,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
892
953
|
}
|
|
893
954
|
if (index < 0) return false;
|
|
894
955
|
if (index === 0) {
|
|
895
|
-
this.
|
|
956
|
+
this.pop();
|
|
896
957
|
} else if (index === this.elements.length - 1) {
|
|
897
958
|
this.elements.pop();
|
|
898
959
|
} else {
|
|
@@ -902,13 +963,19 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
902
963
|
}
|
|
903
964
|
return true;
|
|
904
965
|
}
|
|
966
|
+
/**
|
|
967
|
+
* @deprecated Use `deleteWhere` instead. Will be removed in a future major version.
|
|
968
|
+
*/
|
|
969
|
+
deleteBy(predicate) {
|
|
970
|
+
return this.deleteWhere(predicate);
|
|
971
|
+
}
|
|
905
972
|
/**
|
|
906
973
|
* Delete the first element that matches a predicate.
|
|
907
974
|
* @remarks Time O(N), Space O(1)
|
|
908
975
|
* @param predicate - Function (element, index, heap) → boolean.
|
|
909
976
|
* @returns True if an element was removed.
|
|
910
977
|
*/
|
|
911
|
-
|
|
978
|
+
deleteWhere(predicate) {
|
|
912
979
|
let idx = -1;
|
|
913
980
|
for (let i = 0; i < this.elements.length; i++) {
|
|
914
981
|
if (predicate(this.elements[i], i, this)) {
|
|
@@ -918,7 +985,7 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
918
985
|
}
|
|
919
986
|
if (idx < 0) return false;
|
|
920
987
|
if (idx === 0) {
|
|
921
|
-
this.
|
|
988
|
+
this.pop();
|
|
922
989
|
} else if (idx === this.elements.length - 1) {
|
|
923
990
|
this.elements.pop();
|
|
924
991
|
} else {
|
|
@@ -964,6 +1031,10 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
964
1031
|
|
|
965
1032
|
|
|
966
1033
|
|
|
1034
|
+
|
|
1035
|
+
|
|
1036
|
+
|
|
1037
|
+
|
|
967
1038
|
|
|
968
1039
|
|
|
969
1040
|
|
|
@@ -1044,6 +1115,10 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1044
1115
|
|
|
1045
1116
|
|
|
1046
1117
|
|
|
1118
|
+
|
|
1119
|
+
|
|
1120
|
+
|
|
1121
|
+
|
|
1047
1122
|
|
|
1048
1123
|
|
|
1049
1124
|
|
|
@@ -1097,6 +1172,10 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1097
1172
|
|
|
1098
1173
|
|
|
1099
1174
|
|
|
1175
|
+
|
|
1176
|
+
|
|
1177
|
+
|
|
1178
|
+
|
|
1100
1179
|
|
|
1101
1180
|
|
|
1102
1181
|
|
|
@@ -1149,6 +1228,10 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1149
1228
|
|
|
1150
1229
|
|
|
1151
1230
|
|
|
1231
|
+
|
|
1232
|
+
|
|
1233
|
+
|
|
1234
|
+
|
|
1152
1235
|
|
|
1153
1236
|
|
|
1154
1237
|
|
|
@@ -1208,6 +1291,10 @@ var _Heap = class _Heap extends IterableElementBase {
|
|
|
1208
1291
|
|
|
1209
1292
|
|
|
1210
1293
|
|
|
1294
|
+
|
|
1295
|
+
|
|
1296
|
+
|
|
1297
|
+
|
|
1211
1298
|
|
|
1212
1299
|
|
|
1213
1300
|
|
|
@@ -1390,7 +1477,7 @@ var _FibonacciHeap = class _FibonacciHeap {
|
|
|
1390
1477
|
* Push an element into the root list.
|
|
1391
1478
|
* @remarks Time O(1) amortized, Space O(1)
|
|
1392
1479
|
* @param element - Element to insert.
|
|
1393
|
-
* @returns
|
|
1480
|
+
* @returns True when the element is added.
|
|
1394
1481
|
*/
|
|
1395
1482
|
push(element) {
|
|
1396
1483
|
const node = this.createNode(element);
|
|
@@ -1399,7 +1486,7 @@ var _FibonacciHeap = class _FibonacciHeap {
|
|
|
1399
1486
|
this.mergeWithRoot(node);
|
|
1400
1487
|
if (!this.min || this.comparator(node.element, this.min.element) <= 0) this._min = node;
|
|
1401
1488
|
this._size++;
|
|
1402
|
-
return
|
|
1489
|
+
return true;
|
|
1403
1490
|
}
|
|
1404
1491
|
peek() {
|
|
1405
1492
|
return this.min ? this.min.element : void 0;
|