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/priority-queue-typed.js +174 -16
  35. package/dist/umd/priority-queue-typed.js.map +1 -1
  36. package/dist/umd/priority-queue-typed.min.js +1 -1
  37. package/dist/umd/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
  }
@@ -237,6 +237,35 @@ var priorityQueueTyped = (() => {
237
237
  for (const ele of this) if (ele === element) return true;
238
238
  return false;
239
239
  }
240
+ /**
241
+ * Check whether a value exists (Array-compatible alias for `has`).
242
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
243
+ * @param element - Element to search for (uses `===`).
244
+ * @returns `true` if found.
245
+ */
246
+ includes(element) {
247
+ return this.has(element);
248
+ }
249
+ /**
250
+ * Return an iterator of `[index, value]` pairs (Array-compatible).
251
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
252
+ */
253
+ *entries() {
254
+ let index = 0;
255
+ for (const value of this) {
256
+ yield [index++, value];
257
+ }
258
+ }
259
+ /**
260
+ * Return an iterator of numeric indices (Array-compatible).
261
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1) per step.
262
+ */
263
+ *keys() {
264
+ let index = 0;
265
+ for (const _ of this) {
266
+ yield index++;
267
+ }
268
+ }
240
269
  /**
241
270
  * Reduces all elements to a single accumulated value.
242
271
  *
@@ -375,6 +404,13 @@ var priorityQueueTyped = (() => {
375
404
 
376
405
 
377
406
 
407
+
408
+
409
+
410
+
411
+
412
+
413
+
378
414
 
379
415
 
380
416
 
@@ -432,7 +468,7 @@ var priorityQueueTyped = (() => {
432
468
  }
433
469
  /**
434
470
  * Insert an element.
435
- * @remarks Time O(1) amortized, Space O(1)
471
+ * @remarks Time O(log N) amortized, Space O(1)
436
472
  * @param element - Element to insert.
437
473
  * @returns True.
438
474
 
@@ -462,6 +498,13 @@ var priorityQueueTyped = (() => {
462
498
 
463
499
 
464
500
 
501
+
502
+
503
+
504
+
505
+
506
+
507
+
465
508
 
466
509
 
467
510
 
@@ -519,6 +562,13 @@ var priorityQueueTyped = (() => {
519
562
 
520
563
 
521
564
 
565
+
566
+
567
+
568
+
569
+
570
+
571
+
522
572
 
523
573
 
524
574
 
@@ -583,6 +633,40 @@ var priorityQueueTyped = (() => {
583
633
 
584
634
 
585
635
 
636
+
637
+
638
+
639
+
640
+
641
+
642
+ * @example
643
+ * // Heap with custom comparator (MaxHeap behavior)
644
+ * interface Task {
645
+ * id: number;
646
+ * priority: number;
647
+ * name: string;
648
+ * }
649
+ *
650
+ * // Custom comparator for max heap behavior (higher priority first)
651
+ * const tasks: Task[] = [
652
+ * { id: 1, priority: 5, name: 'Email' },
653
+ * { id: 2, priority: 3, name: 'Chat' },
654
+ * { id: 3, priority: 8, name: 'Alert' }
655
+ * ];
656
+ *
657
+ * const maxHeap = new Heap(tasks, {
658
+ * comparator: (a: Task, b: Task) => b.priority - a.priority
659
+ * });
660
+ *
661
+ * console.log(maxHeap.size); // 3;
662
+ *
663
+ * // Peek returns highest priority task
664
+ * const topTask = maxHeap.peek();
665
+ * console.log(topTask?.priority); // 8;
666
+ * console.log(topTask?.name); // 'Alert';
667
+ */
668
+ /**
669
+ * @deprecated Use `pop` instead. Will be removed in a future major version.
586
670
 
587
671
 
588
672
 
@@ -613,6 +697,14 @@ var priorityQueueTyped = (() => {
613
697
  * console.log(topTask?.name); // 'Alert';
614
698
  */
615
699
  poll() {
700
+ return this.pop();
701
+ }
702
+ /**
703
+ * Remove and return the top element (min or max depending on comparator).
704
+ * @remarks Time O(log N) amortized, Space O(1)
705
+ * @returns The removed top element, or undefined if empty.
706
+ */
707
+ pop() {
616
708
  if (this.elements.length === 0) return;
617
709
  const value = this.elements[0];
618
710
  const last = this.elements.pop();
@@ -653,6 +745,13 @@ var priorityQueueTyped = (() => {
653
745
 
654
746
 
655
747
 
748
+
749
+
750
+
751
+
752
+
753
+
754
+
656
755
 
657
756
 
658
757
 
@@ -753,6 +852,13 @@ var priorityQueueTyped = (() => {
753
852
 
754
853
 
755
854
 
855
+
856
+
857
+
858
+
859
+
860
+
861
+
756
862
 
757
863
 
758
864
 
@@ -800,6 +906,13 @@ var priorityQueueTyped = (() => {
800
906
 
801
907
 
802
908
 
909
+
910
+
911
+
912
+
913
+
914
+
915
+
803
916
 
804
917
 
805
918
 
@@ -817,16 +930,6 @@ var priorityQueueTyped = (() => {
817
930
  clear() {
818
931
  this._elements = [];
819
932
  }
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
933
  /**
831
934
  * Check if an equal element exists in the heap.
832
935
  * @remarks Time O(N), Space O(1)
@@ -850,6 +953,13 @@ var priorityQueueTyped = (() => {
850
953
 
851
954
 
852
955
 
956
+
957
+
958
+
959
+
960
+
961
+
962
+
853
963
 
854
964
 
855
965
 
@@ -897,6 +1007,13 @@ var priorityQueueTyped = (() => {
897
1007
 
898
1008
 
899
1009
 
1010
+
1011
+
1012
+
1013
+
1014
+
1015
+
1016
+
900
1017
 
901
1018
 
902
1019
 
@@ -921,7 +1038,7 @@ var priorityQueueTyped = (() => {
921
1038
  }
922
1039
  if (index < 0) return false;
923
1040
  if (index === 0) {
924
- this.poll();
1041
+ this.pop();
925
1042
  } else if (index === this.elements.length - 1) {
926
1043
  this.elements.pop();
927
1044
  } else {
@@ -931,13 +1048,19 @@ var priorityQueueTyped = (() => {
931
1048
  }
932
1049
  return true;
933
1050
  }
1051
+ /**
1052
+ * @deprecated Use `deleteWhere` instead. Will be removed in a future major version.
1053
+ */
1054
+ deleteBy(predicate) {
1055
+ return this.deleteWhere(predicate);
1056
+ }
934
1057
  /**
935
1058
  * Delete the first element that matches a predicate.
936
1059
  * @remarks Time O(N), Space O(1)
937
1060
  * @param predicate - Function (element, index, heap) → boolean.
938
1061
  * @returns True if an element was removed.
939
1062
  */
940
- deleteBy(predicate) {
1063
+ deleteWhere(predicate) {
941
1064
  let idx = -1;
942
1065
  for (let i = 0; i < this.elements.length; i++) {
943
1066
  if (predicate(this.elements[i], i, this)) {
@@ -947,7 +1070,7 @@ var priorityQueueTyped = (() => {
947
1070
  }
948
1071
  if (idx < 0) return false;
949
1072
  if (idx === 0) {
950
- this.poll();
1073
+ this.pop();
951
1074
  } else if (idx === this.elements.length - 1) {
952
1075
  this.elements.pop();
953
1076
  } else {
@@ -990,6 +1113,13 @@ var priorityQueueTyped = (() => {
990
1113
 
991
1114
 
992
1115
 
1116
+
1117
+
1118
+
1119
+
1120
+
1121
+
1122
+
993
1123
 
994
1124
 
995
1125
 
@@ -1070,6 +1200,13 @@ var priorityQueueTyped = (() => {
1070
1200
 
1071
1201
 
1072
1202
 
1203
+
1204
+
1205
+
1206
+
1207
+
1208
+
1209
+
1073
1210
 
1074
1211
 
1075
1212
 
@@ -1123,6 +1260,13 @@ var priorityQueueTyped = (() => {
1123
1260
 
1124
1261
 
1125
1262
 
1263
+
1264
+
1265
+
1266
+
1267
+
1268
+
1269
+
1126
1270
 
1127
1271
 
1128
1272
 
@@ -1175,6 +1319,13 @@ var priorityQueueTyped = (() => {
1175
1319
 
1176
1320
 
1177
1321
 
1322
+
1323
+
1324
+
1325
+
1326
+
1327
+
1328
+
1178
1329
 
1179
1330
 
1180
1331
 
@@ -1234,6 +1385,13 @@ var priorityQueueTyped = (() => {
1234
1385
 
1235
1386
 
1236
1387
 
1388
+
1389
+
1390
+
1391
+
1392
+
1393
+
1394
+
1237
1395
 
1238
1396
 
1239
1397
 
@@ -1415,7 +1573,7 @@ var priorityQueueTyped = (() => {
1415
1573
  * Push an element into the root list.
1416
1574
  * @remarks Time O(1) amortized, Space O(1)
1417
1575
  * @param element - Element to insert.
1418
- * @returns This heap.
1576
+ * @returns True when the element is added.
1419
1577
  */
1420
1578
  push(element) {
1421
1579
  const node = this.createNode(element);
@@ -1424,7 +1582,7 @@ var priorityQueueTyped = (() => {
1424
1582
  this.mergeWithRoot(node);
1425
1583
  if (!this.min || this.comparator(node.element, this.min.element) <= 0) this._min = node;
1426
1584
  this._size++;
1427
- return this;
1585
+ return true;
1428
1586
  }
1429
1587
  peek() {
1430
1588
  return this.min ? this.min.element : void 0;