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.
Files changed (63) hide show
  1. package/dist/cjs/index.cjs +174 -16
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +174 -16
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +174 -16
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +174 -16
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/data-structures/base/iterable-element-base.d.ts +17 -0
  10. package/dist/types/data-structures/base/linear-base.d.ts +6 -0
  11. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +86 -2
  12. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +98 -0
  13. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +191 -15
  14. package/dist/types/data-structures/binary-tree/bst.d.ts +171 -3
  15. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +136 -8
  16. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +42 -0
  17. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1061 -167
  18. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1232 -355
  19. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +916 -194
  20. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1078 -141
  21. package/dist/types/data-structures/graph/directed-graph.d.ts +70 -0
  22. package/dist/types/data-structures/graph/undirected-graph.d.ts +63 -0
  23. package/dist/types/data-structures/hash/hash-map.d.ts +84 -6
  24. package/dist/types/data-structures/heap/heap.d.ts +140 -12
  25. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +150 -2
  26. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +106 -1
  27. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +126 -0
  28. package/dist/types/data-structures/matrix/matrix.d.ts +56 -0
  29. package/dist/types/data-structures/queue/deque.d.ts +171 -0
  30. package/dist/types/data-structures/queue/queue.d.ts +97 -0
  31. package/dist/types/data-structures/stack/stack.d.ts +72 -2
  32. package/dist/types/data-structures/trie/trie.d.ts +84 -0
  33. package/dist/types/interfaces/binary-tree.d.ts +2 -3
  34. package/dist/umd/max-priority-queue-typed.js +174 -16
  35. package/dist/umd/max-priority-queue-typed.js.map +1 -1
  36. package/dist/umd/max-priority-queue-typed.min.js +1 -1
  37. package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
  38. package/package.json +2 -2
  39. package/src/data-structures/base/iterable-element-base.ts +32 -0
  40. package/src/data-structures/base/linear-base.ts +11 -0
  41. package/src/data-structures/binary-tree/avl-tree.ts +88 -5
  42. package/src/data-structures/binary-tree/binary-indexed-tree.ts +98 -0
  43. package/src/data-structures/binary-tree/binary-tree.ts +242 -81
  44. package/src/data-structures/binary-tree/bst.ts +173 -7
  45. package/src/data-structures/binary-tree/red-black-tree.ts +139 -15
  46. package/src/data-structures/binary-tree/segment-tree.ts +42 -0
  47. package/src/data-structures/binary-tree/tree-map.ts +948 -36
  48. package/src/data-structures/binary-tree/tree-multi-map.ts +893 -13
  49. package/src/data-structures/binary-tree/tree-multi-set.ts +761 -33
  50. package/src/data-structures/binary-tree/tree-set.ts +1260 -251
  51. package/src/data-structures/graph/directed-graph.ts +71 -1
  52. package/src/data-structures/graph/undirected-graph.ts +64 -1
  53. package/src/data-structures/hash/hash-map.ts +100 -12
  54. package/src/data-structures/heap/heap.ts +149 -19
  55. package/src/data-structures/linked-list/doubly-linked-list.ts +178 -2
  56. package/src/data-structures/linked-list/singly-linked-list.ts +106 -1
  57. package/src/data-structures/linked-list/skip-linked-list.ts +126 -0
  58. package/src/data-structures/matrix/matrix.ts +56 -0
  59. package/src/data-structures/queue/deque.ts +187 -0
  60. package/src/data-structures/queue/queue.ts +109 -0
  61. package/src/data-structures/stack/stack.ts +75 -5
  62. package/src/data-structures/trie/trie.ts +84 -0
  63. package/src/interfaces/binary-tree.ts +1 -9
@@ -245,6 +245,13 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
245
245
 
246
246
 
247
247
 
248
+
249
+
250
+
251
+
252
+
253
+
254
+
248
255
 
249
256
 
250
257
 
@@ -299,6 +306,13 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
299
306
 
300
307
 
301
308
 
309
+
310
+
311
+
312
+
313
+
314
+
315
+
302
316
 
303
317
 
304
318
 
@@ -348,6 +362,13 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
348
362
 
349
363
 
350
364
 
365
+
366
+
367
+
368
+
369
+
370
+
371
+
351
372
 
352
373
 
353
374
 
@@ -393,6 +414,13 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
393
414
 
394
415
 
395
416
 
417
+
418
+
419
+
420
+
421
+
422
+
423
+
396
424
 
397
425
 
398
426
 
@@ -437,6 +465,13 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
437
465
 
438
466
 
439
467
 
468
+
469
+
470
+
471
+
472
+
473
+
474
+
440
475
 
441
476
 
442
477
 
@@ -484,6 +519,13 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
484
519
 
485
520
 
486
521
 
522
+
523
+
524
+
525
+
526
+
527
+
528
+
487
529
 
488
530
 
489
531
 
@@ -556,6 +598,13 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
556
598
 
557
599
 
558
600
 
601
+
602
+
603
+
604
+
605
+
606
+
607
+
559
608
 
560
609
 
561
610
 
@@ -611,6 +660,13 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
611
660
 
612
661
 
613
662
 
663
+
664
+
665
+
666
+
667
+
668
+
669
+
614
670
 
615
671
 
616
672
 
@@ -660,6 +716,13 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
660
716
 
661
717
 
662
718
 
719
+
720
+
721
+
722
+
723
+
724
+
725
+
663
726
 
664
727
 
665
728
 
@@ -709,6 +772,13 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
709
772
 
710
773
 
711
774
 
775
+
776
+
777
+
778
+
779
+
780
+
781
+
712
782
 
713
783
 
714
784
 
@@ -755,6 +825,13 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
755
825
 
756
826
 
757
827
 
828
+
829
+
830
+
831
+
832
+
833
+
834
+
758
835
 
759
836
 
760
837
 
@@ -796,6 +873,13 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
796
873
 
797
874
 
798
875
 
876
+
877
+
878
+
879
+
880
+
881
+
882
+
799
883
 
800
884
 
801
885
 
@@ -1,5 +1,5 @@
1
1
  import { BinaryTreeNode } from '../data-structures';
2
- import type { BinaryTreeDeleteResult, BinaryTreeOptions, BTNRep, DFSOrderPattern, EntryCallback, IterationType, NodeCallback, NodePredicate, OptNodeOrNull, ReduceEntryCallback, ToEntryFn } from '../types';
2
+ import type { BinaryTreeOptions, BTNRep, DFSOrderPattern, EntryCallback, IterationType, NodeCallback, NodePredicate, OptNodeOrNull, ReduceEntryCallback, ToEntryFn } from '../types';
3
3
  /**
4
4
  * Public, implementation-agnostic binary tree API.
5
5
  * K = key, V = value, R = raw/record used with toEntryFn (optional).
@@ -19,7 +19,7 @@ export interface IBinaryTree<K = any, V = any, R = any> {
19
19
  add(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V, count?: number): boolean;
20
20
  set(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V, count?: number): boolean;
21
21
  addMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): boolean[];
22
- delete(keyNodeEntryRawOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V> | null>): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[];
22
+ delete(keyNodeEntryRawOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V> | null>): boolean;
23
23
  clear(): void;
24
24
  isEmpty(): boolean;
25
25
  get(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): V | undefined;
@@ -56,5 +56,4 @@ export interface IBinaryTree<K = any, V = any, R = any> {
56
56
  filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: unknown): this;
57
57
  map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<BinaryTreeOptions<MK, MV, MR>>, thisArg?: unknown): IBinaryTree<MK, MV, MR>;
58
58
  merge(anotherTree: IBinaryTree<K, V, R>): void;
59
- refill(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): void;
60
59
  }
@@ -234,6 +234,35 @@ var maxPriorityQueueTyped = (() => {
234
234
  for (const ele of this) if (ele === element) return true;
235
235
  return false;
236
236
  }
237
+ /**
238
+ * Check whether a value exists (Array-compatible alias for `has`).
239
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
240
+ * @param element - Element to search for (uses `===`).
241
+ * @returns `true` if found.
242
+ */
243
+ includes(element) {
244
+ return this.has(element);
245
+ }
246
+ /**
247
+ * Return an iterator of `[index, value]` pairs (Array-compatible).
248
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
249
+ */
250
+ *entries() {
251
+ let index = 0;
252
+ for (const value of this) {
253
+ yield [index++, value];
254
+ }
255
+ }
256
+ /**
257
+ * Return an iterator of numeric indices (Array-compatible).
258
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
259
+ */
260
+ *keys() {
261
+ let index = 0;
262
+ for (const _ of this) {
263
+ yield index++;
264
+ }
265
+ }
237
266
  /**
238
267
  * Reduces all elements to a single accumulated value.
239
268
  *
@@ -372,6 +401,13 @@ var maxPriorityQueueTyped = (() => {
372
401
 
373
402
 
374
403
 
404
+
405
+
406
+
407
+
408
+
409
+
410
+
375
411
 
376
412
 
377
413
 
@@ -429,7 +465,7 @@ var maxPriorityQueueTyped = (() => {
429
465
  }
430
466
  /**
431
467
  * Insert an element.
432
- * @remarks Time O(1) amortized, Space O(1)
468
+ * @remarks Time O(log N) amortized, Space O(1)
433
469
  * @param element - Element to insert.
434
470
  * @returns True.
435
471
 
@@ -459,6 +495,13 @@ var maxPriorityQueueTyped = (() => {
459
495
 
460
496
 
461
497
 
498
+
499
+
500
+
501
+
502
+
503
+
504
+
462
505
 
463
506
 
464
507
 
@@ -516,6 +559,13 @@ var maxPriorityQueueTyped = (() => {
516
559
 
517
560
 
518
561
 
562
+
563
+
564
+
565
+
566
+
567
+
568
+
519
569
 
520
570
 
521
571
 
@@ -580,6 +630,40 @@ var maxPriorityQueueTyped = (() => {
580
630
 
581
631
 
582
632
 
633
+
634
+
635
+
636
+
637
+
638
+
639
+ * @example
640
+ * // Heap with custom comparator (MaxHeap behavior)
641
+ * interface Task {
642
+ * id: number;
643
+ * priority: number;
644
+ * name: string;
645
+ * }
646
+ *
647
+ * // Custom comparator for max heap behavior (higher priority first)
648
+ * const tasks: Task[] = [
649
+ * { id: 1, priority: 5, name: 'Email' },
650
+ * { id: 2, priority: 3, name: 'Chat' },
651
+ * { id: 3, priority: 8, name: 'Alert' }
652
+ * ];
653
+ *
654
+ * const maxHeap = new Heap(tasks, {
655
+ * comparator: (a: Task, b: Task) => b.priority - a.priority
656
+ * });
657
+ *
658
+ * console.log(maxHeap.size); // 3;
659
+ *
660
+ * // Peek returns highest priority task
661
+ * const topTask = maxHeap.peek();
662
+ * console.log(topTask?.priority); // 8;
663
+ * console.log(topTask?.name); // 'Alert';
664
+ */
665
+ /**
666
+ * @deprecated Use `pop` instead. Will be removed in a future major version.
583
667
 
584
668
 
585
669
 
@@ -610,6 +694,14 @@ var maxPriorityQueueTyped = (() => {
610
694
  * console.log(topTask?.name); // 'Alert';
611
695
  */
612
696
  poll() {
697
+ return this.pop();
698
+ }
699
+ /**
700
+ * Remove and return the top element (min or max depending on comparator).
701
+ * @remarks Time O(log N) amortized, Space O(1)
702
+ * @returns The removed top element, or undefined if empty.
703
+ */
704
+ pop() {
613
705
  if (this.elements.length === 0) return;
614
706
  const value = this.elements[0];
615
707
  const last = this.elements.pop();
@@ -650,6 +742,13 @@ var maxPriorityQueueTyped = (() => {
650
742
 
651
743
 
652
744
 
745
+
746
+
747
+
748
+
749
+
750
+
751
+
653
752
 
654
753
 
655
754
 
@@ -750,6 +849,13 @@ var maxPriorityQueueTyped = (() => {
750
849
 
751
850
 
752
851
 
852
+
853
+
854
+
855
+
856
+
857
+
858
+
753
859
 
754
860
 
755
861
 
@@ -797,6 +903,13 @@ var maxPriorityQueueTyped = (() => {
797
903
 
798
904
 
799
905
 
906
+
907
+
908
+
909
+
910
+
911
+
912
+
800
913
 
801
914
 
802
915
 
@@ -814,16 +927,6 @@ var maxPriorityQueueTyped = (() => {
814
927
  clear() {
815
928
  this._elements = [];
816
929
  }
817
- /**
818
- * Replace the backing array and rebuild the heap.
819
- * @remarks Time O(N), Space O(N)
820
- * @param elements - Iterable used to refill the heap.
821
- * @returns Array of per-node results from fixing steps.
822
- */
823
- refill(elements) {
824
- this._elements = Array.from(elements);
825
- return this.fix();
826
- }
827
930
  /**
828
931
  * Check if an equal element exists in the heap.
829
932
  * @remarks Time O(N), Space O(1)
@@ -847,6 +950,13 @@ var maxPriorityQueueTyped = (() => {
847
950
 
848
951
 
849
952
 
953
+
954
+
955
+
956
+
957
+
958
+
959
+
850
960
 
851
961
 
852
962
 
@@ -894,6 +1004,13 @@ var maxPriorityQueueTyped = (() => {
894
1004
 
895
1005
 
896
1006
 
1007
+
1008
+
1009
+
1010
+
1011
+
1012
+
1013
+
897
1014
 
898
1015
 
899
1016
 
@@ -918,7 +1035,7 @@ var maxPriorityQueueTyped = (() => {
918
1035
  }
919
1036
  if (index < 0) return false;
920
1037
  if (index === 0) {
921
- this.poll();
1038
+ this.pop();
922
1039
  } else if (index === this.elements.length - 1) {
923
1040
  this.elements.pop();
924
1041
  } else {
@@ -928,13 +1045,19 @@ var maxPriorityQueueTyped = (() => {
928
1045
  }
929
1046
  return true;
930
1047
  }
1048
+ /**
1049
+ * @deprecated Use `deleteWhere` instead. Will be removed in a future major version.
1050
+ */
1051
+ deleteBy(predicate) {
1052
+ return this.deleteWhere(predicate);
1053
+ }
931
1054
  /**
932
1055
  * Delete the first element that matches a predicate.
933
1056
  * @remarks Time O(N), Space O(1)
934
1057
  * @param predicate - Function (element, index, heap) → boolean.
935
1058
  * @returns True if an element was removed.
936
1059
  */
937
- deleteBy(predicate) {
1060
+ deleteWhere(predicate) {
938
1061
  let idx = -1;
939
1062
  for (let i = 0; i < this.elements.length; i++) {
940
1063
  if (predicate(this.elements[i], i, this)) {
@@ -944,7 +1067,7 @@ var maxPriorityQueueTyped = (() => {
944
1067
  }
945
1068
  if (idx < 0) return false;
946
1069
  if (idx === 0) {
947
- this.poll();
1070
+ this.pop();
948
1071
  } else if (idx === this.elements.length - 1) {
949
1072
  this.elements.pop();
950
1073
  } else {
@@ -987,6 +1110,13 @@ var maxPriorityQueueTyped = (() => {
987
1110
 
988
1111
 
989
1112
 
1113
+
1114
+
1115
+
1116
+
1117
+
1118
+
1119
+
990
1120
 
991
1121
 
992
1122
 
@@ -1067,6 +1197,13 @@ var maxPriorityQueueTyped = (() => {
1067
1197
 
1068
1198
 
1069
1199
 
1200
+
1201
+
1202
+
1203
+
1204
+
1205
+
1206
+
1070
1207
 
1071
1208
 
1072
1209
 
@@ -1120,6 +1257,13 @@ var maxPriorityQueueTyped = (() => {
1120
1257
 
1121
1258
 
1122
1259
 
1260
+
1261
+
1262
+
1263
+
1264
+
1265
+
1266
+
1123
1267
 
1124
1268
 
1125
1269
 
@@ -1172,6 +1316,13 @@ var maxPriorityQueueTyped = (() => {
1172
1316
 
1173
1317
 
1174
1318
 
1319
+
1320
+
1321
+
1322
+
1323
+
1324
+
1325
+
1175
1326
 
1176
1327
 
1177
1328
 
@@ -1231,6 +1382,13 @@ var maxPriorityQueueTyped = (() => {
1231
1382
 
1232
1383
 
1233
1384
 
1385
+
1386
+
1387
+
1388
+
1389
+
1390
+
1391
+
1234
1392
 
1235
1393
 
1236
1394
 
@@ -1412,7 +1570,7 @@ var maxPriorityQueueTyped = (() => {
1412
1570
  * Push an element into the root list.
1413
1571
  * @remarks Time O(1) amortized, Space O(1)
1414
1572
  * @param element - Element to insert.
1415
- * @returns This heap.
1573
+ * @returns True when the element is added.
1416
1574
  */
1417
1575
  push(element) {
1418
1576
  const node = this.createNode(element);
@@ -1421,7 +1579,7 @@ var maxPriorityQueueTyped = (() => {
1421
1579
  this.mergeWithRoot(node);
1422
1580
  if (!this.min || this.comparator(node.element, this.min.element) <= 0) this._min = node;
1423
1581
  this._size++;
1424
- return this;
1582
+ return true;
1425
1583
  }
1426
1584
  peek() {
1427
1585
  return this.min ? this.min.element : void 0;