data-structure-typed 1.48.1 → 1.48.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.
Files changed (164) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +24 -18
  3. package/benchmark/report.html +16 -16
  4. package/benchmark/report.json +172 -172
  5. package/dist/cjs/data-structures/base/index.d.ts +1 -0
  6. package/dist/cjs/data-structures/base/index.js +18 -0
  7. package/dist/cjs/data-structures/base/index.js.map +1 -0
  8. package/dist/cjs/data-structures/base/iterable-base.d.ts +232 -0
  9. package/dist/cjs/data-structures/base/iterable-base.js +313 -0
  10. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -0
  11. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +16 -16
  12. package/dist/cjs/data-structures/binary-tree/avl-tree.js +7 -7
  13. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +121 -152
  15. package/dist/cjs/data-structures/binary-tree/binary-tree.js +140 -182
  16. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  17. package/dist/cjs/data-structures/binary-tree/bst.d.ts +28 -47
  18. package/dist/cjs/data-structures/binary-tree/bst.js +54 -57
  19. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  20. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +15 -15
  21. package/dist/cjs/data-structures/binary-tree/rb-tree.js +7 -7
  22. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  23. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +22 -22
  24. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +11 -11
  25. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  26. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +44 -6
  27. package/dist/cjs/data-structures/graph/abstract-graph.js +50 -27
  28. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  29. package/dist/cjs/data-structures/hash/hash-map.d.ts +59 -100
  30. package/dist/cjs/data-structures/hash/hash-map.js +69 -173
  31. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  32. package/dist/cjs/data-structures/heap/heap.d.ts +50 -7
  33. package/dist/cjs/data-structures/heap/heap.js +60 -30
  34. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  35. package/dist/cjs/data-structures/index.d.ts +1 -0
  36. package/dist/cjs/data-structures/index.js +1 -0
  37. package/dist/cjs/data-structures/index.js.map +1 -1
  38. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
  39. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +46 -73
  40. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  41. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
  42. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +40 -73
  43. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  44. package/dist/cjs/data-structures/queue/deque.d.ts +29 -51
  45. package/dist/cjs/data-structures/queue/deque.js +36 -71
  46. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  47. package/dist/cjs/data-structures/queue/queue.d.ts +49 -48
  48. package/dist/cjs/data-structures/queue/queue.js +69 -82
  49. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  50. package/dist/cjs/data-structures/stack/stack.d.ts +43 -10
  51. package/dist/cjs/data-structures/stack/stack.js +50 -31
  52. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  53. package/dist/cjs/data-structures/trie/trie.d.ts +41 -6
  54. package/dist/cjs/data-structures/trie/trie.js +53 -32
  55. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  56. package/dist/cjs/interfaces/binary-tree.d.ts +6 -6
  57. package/dist/cjs/types/common.d.ts +11 -8
  58. package/dist/cjs/types/common.js +6 -1
  59. package/dist/cjs/types/common.js.map +1 -1
  60. package/dist/cjs/types/data-structures/base/base.d.ts +5 -0
  61. package/dist/cjs/types/data-structures/base/base.js +3 -0
  62. package/dist/cjs/types/data-structures/base/base.js.map +1 -0
  63. package/dist/cjs/types/data-structures/base/index.d.ts +1 -0
  64. package/dist/cjs/types/data-structures/base/index.js +18 -0
  65. package/dist/cjs/types/data-structures/base/index.js.map +1 -0
  66. package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +3 -3
  67. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
  68. package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +6 -6
  69. package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
  70. package/dist/cjs/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
  71. package/dist/cjs/types/data-structures/index.d.ts +1 -0
  72. package/dist/cjs/types/data-structures/index.js +1 -0
  73. package/dist/cjs/types/data-structures/index.js.map +1 -1
  74. package/dist/mjs/data-structures/base/index.d.ts +1 -0
  75. package/dist/mjs/data-structures/base/index.js +1 -0
  76. package/dist/mjs/data-structures/base/iterable-base.d.ts +232 -0
  77. package/dist/mjs/data-structures/base/iterable-base.js +307 -0
  78. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +16 -16
  79. package/dist/mjs/data-structures/binary-tree/avl-tree.js +7 -7
  80. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +121 -152
  81. package/dist/mjs/data-structures/binary-tree/binary-tree.js +140 -182
  82. package/dist/mjs/data-structures/binary-tree/bst.d.ts +28 -47
  83. package/dist/mjs/data-structures/binary-tree/bst.js +55 -57
  84. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +15 -15
  85. package/dist/mjs/data-structures/binary-tree/rb-tree.js +7 -7
  86. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +22 -22
  87. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +11 -11
  88. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +44 -6
  89. package/dist/mjs/data-structures/graph/abstract-graph.js +52 -27
  90. package/dist/mjs/data-structures/hash/hash-map.d.ts +59 -100
  91. package/dist/mjs/data-structures/hash/hash-map.js +69 -173
  92. package/dist/mjs/data-structures/heap/heap.d.ts +50 -7
  93. package/dist/mjs/data-structures/heap/heap.js +60 -30
  94. package/dist/mjs/data-structures/index.d.ts +1 -0
  95. package/dist/mjs/data-structures/index.js +1 -0
  96. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
  97. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +46 -73
  98. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
  99. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +40 -73
  100. package/dist/mjs/data-structures/queue/deque.d.ts +29 -51
  101. package/dist/mjs/data-structures/queue/deque.js +36 -71
  102. package/dist/mjs/data-structures/queue/queue.d.ts +49 -48
  103. package/dist/mjs/data-structures/queue/queue.js +66 -79
  104. package/dist/mjs/data-structures/stack/stack.d.ts +43 -10
  105. package/dist/mjs/data-structures/stack/stack.js +50 -31
  106. package/dist/mjs/data-structures/trie/trie.d.ts +41 -6
  107. package/dist/mjs/data-structures/trie/trie.js +53 -32
  108. package/dist/mjs/interfaces/binary-tree.d.ts +6 -6
  109. package/dist/mjs/types/common.d.ts +11 -8
  110. package/dist/mjs/types/common.js +5 -0
  111. package/dist/mjs/types/data-structures/base/base.d.ts +5 -0
  112. package/dist/mjs/types/data-structures/base/base.js +1 -0
  113. package/dist/mjs/types/data-structures/base/index.d.ts +1 -0
  114. package/dist/mjs/types/data-structures/base/index.js +1 -0
  115. package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +3 -3
  116. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
  117. package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +6 -6
  118. package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
  119. package/dist/mjs/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
  120. package/dist/mjs/types/data-structures/index.d.ts +1 -0
  121. package/dist/mjs/types/data-structures/index.js +1 -0
  122. package/dist/umd/data-structure-typed.js +991 -848
  123. package/dist/umd/data-structure-typed.min.js +2 -2
  124. package/dist/umd/data-structure-typed.min.js.map +1 -1
  125. package/package.json +2 -2
  126. package/src/data-structures/base/index.ts +1 -0
  127. package/src/data-structures/base/iterable-base.ts +329 -0
  128. package/src/data-structures/binary-tree/avl-tree.ts +20 -21
  129. package/src/data-structures/binary-tree/binary-tree.ts +222 -267
  130. package/src/data-structures/binary-tree/bst.ts +86 -82
  131. package/src/data-structures/binary-tree/rb-tree.ts +25 -26
  132. package/src/data-structures/binary-tree/tree-multimap.ts +30 -35
  133. package/src/data-structures/graph/abstract-graph.ts +55 -28
  134. package/src/data-structures/hash/hash-map.ts +76 -185
  135. package/src/data-structures/heap/heap.ts +63 -36
  136. package/src/data-structures/index.ts +1 -0
  137. package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
  138. package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
  139. package/src/data-structures/queue/deque.ts +40 -82
  140. package/src/data-structures/queue/queue.ts +72 -87
  141. package/src/data-structures/stack/stack.ts +53 -34
  142. package/src/data-structures/trie/trie.ts +58 -35
  143. package/src/interfaces/binary-tree.ts +5 -6
  144. package/src/types/common.ts +11 -8
  145. package/src/types/data-structures/base/base.ts +6 -0
  146. package/src/types/data-structures/base/index.ts +1 -0
  147. package/src/types/data-structures/binary-tree/avl-tree.ts +3 -3
  148. package/src/types/data-structures/binary-tree/binary-tree.ts +6 -5
  149. package/src/types/data-structures/binary-tree/bst.ts +6 -6
  150. package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
  151. package/src/types/data-structures/binary-tree/tree-multimap.ts +3 -3
  152. package/src/types/data-structures/index.ts +1 -0
  153. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +13 -13
  154. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +16 -16
  155. package/test/unit/data-structures/binary-tree/bst.test.ts +20 -19
  156. package/test/unit/data-structures/binary-tree/overall.test.ts +1 -1
  157. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +9 -9
  158. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +15 -15
  159. package/test/unit/data-structures/graph/directed-graph.test.ts +4 -4
  160. package/test/unit/data-structures/hash/hash-map.test.ts +2 -2
  161. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +28 -0
  162. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +25 -0
  163. package/test/unit/data-structures/queue/deque.test.ts +25 -0
  164. package/test/unit/unrestricted-interconversion.test.ts +1 -1
@@ -108,6 +108,7 @@ var dataStructureTyped = (() => {
108
108
  AbstractVertex: () => AbstractVertex,
109
109
  BST: () => BST,
110
110
  BSTNode: () => BSTNode,
111
+ BSTVariant: () => BSTVariant,
111
112
  BinaryIndexedTree: () => BinaryIndexedTree,
112
113
  BinaryTree: () => BinaryTree,
113
114
  BinaryTreeNode: () => BinaryTreeNode,
@@ -126,6 +127,8 @@ var dataStructureTyped = (() => {
126
127
  HashTable: () => HashTable,
127
128
  HashTableNode: () => HashTableNode,
128
129
  Heap: () => Heap,
130
+ IterableElementBase: () => IterableElementBase,
131
+ IterablePairBase: () => IterablePairBase,
129
132
  IterationType: () => IterationType,
130
133
  LinkedHashMap: () => LinkedHashMap,
131
134
  LinkedListQueue: () => LinkedListQueue,
@@ -516,8 +519,317 @@ var dataStructureTyped = (() => {
516
519
  };
517
520
  var calcMinUnitsRequired = (totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize);
518
521
 
522
+ // src/data-structures/base/iterable-base.ts
523
+ var IterablePairBase = class {
524
+ /**
525
+ * Time Complexity: O(n)
526
+ * Space Complexity: O(1)
527
+ */
528
+ /**
529
+ * Time Complexity: O(n)
530
+ * Space Complexity: O(1)
531
+ *
532
+ * The function is an implementation of the Symbol.iterator method that returns an iterable iterator.
533
+ * @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
534
+ * allows the function to accept any number of arguments as an array. In this case, the `args`
535
+ * parameter is used to pass any additional arguments to the `_getIterator` method.
536
+ */
537
+ *[Symbol.iterator](...args) {
538
+ yield* __yieldStar(this._getIterator(...args));
539
+ }
540
+ /**
541
+ * Time Complexity: O(n)
542
+ * Space Complexity: O(n)
543
+ */
544
+ /**
545
+ * Time Complexity: O(n)
546
+ * Space Complexity: O(n)
547
+ *
548
+ * The function returns an iterator that yields key-value pairs from the object, where the value can
549
+ * be undefined.
550
+ */
551
+ *entries() {
552
+ for (const item of this) {
553
+ yield item;
554
+ }
555
+ }
556
+ /**
557
+ * Time Complexity: O(n)
558
+ * Space Complexity: O(n)
559
+ */
560
+ /**
561
+ * Time Complexity: O(n)
562
+ * Space Complexity: O(n)
563
+ *
564
+ * The function returns an iterator that yields the keys of a data structure.
565
+ */
566
+ *keys() {
567
+ for (const item of this) {
568
+ yield item[0];
569
+ }
570
+ }
571
+ /**
572
+ * Time Complexity: O(n)
573
+ * Space Complexity: O(n)
574
+ */
575
+ /**
576
+ * Time Complexity: O(n)
577
+ * Space Complexity: O(n)
578
+ *
579
+ * The function returns an iterator that yields the values of a collection.
580
+ */
581
+ *values() {
582
+ for (const item of this) {
583
+ yield item[1];
584
+ }
585
+ }
586
+ /**
587
+ * Time Complexity: O(n)
588
+ * Space Complexity: O(1)
589
+ */
590
+ /**
591
+ * Time Complexity: O(n)
592
+ * Space Complexity: O(1)
593
+ *
594
+ * The `every` function checks if every element in a collection satisfies a given condition.
595
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
596
+ * `value`, `key`, and `index`. It should return a boolean value indicating whether the condition is
597
+ * met for the current element in the iteration.
598
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
599
+ * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
600
+ * passed as the first argument to the `predicate` function. If `thisArg` is not provided
601
+ * @returns The `every` method is returning a boolean value. It returns `true` if every element in
602
+ * the collection satisfies the provided predicate function, and `false` otherwise.
603
+ */
604
+ every(predicate, thisArg) {
605
+ let index = 0;
606
+ for (const item of this) {
607
+ if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
608
+ return false;
609
+ }
610
+ }
611
+ return true;
612
+ }
613
+ /**
614
+ * Time Complexity: O(n)
615
+ * Space Complexity: O(1)
616
+ */
617
+ /**
618
+ * Time Complexity: O(n)
619
+ * Space Complexity: O(1)
620
+ *
621
+ * The "some" function iterates over a collection and returns true if at least one element satisfies
622
+ * a given predicate.
623
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
624
+ * `value`, `key`, and `index`. It should return a boolean value indicating whether the condition is
625
+ * met for the current element in the iteration.
626
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
627
+ * to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
628
+ * it will be passed as the first argument to the `predicate` function. If `thisArg` is
629
+ * @returns a boolean value. It returns true if the predicate function returns true for any pair in
630
+ * the collection, and false otherwise.
631
+ */
632
+ some(predicate, thisArg) {
633
+ let index = 0;
634
+ for (const item of this) {
635
+ if (predicate.call(thisArg, item[1], item[0], index++, this)) {
636
+ return true;
637
+ }
638
+ }
639
+ return false;
640
+ }
641
+ /**
642
+ * Time Complexity: O(n)
643
+ * Space Complexity: O(1)
644
+ */
645
+ /**
646
+ * Time Complexity: O(n)
647
+ * Space Complexity: O(1)
648
+ *
649
+ * The `forEach` function iterates over each key-value pair in a collection and executes a callback
650
+ * function for each pair.
651
+ * @param callbackfn - The callback function that will be called for each element in the collection.
652
+ * It takes four parameters: the value of the current element, the key of the current element, the
653
+ * index of the current element, and the collection itself.
654
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
655
+ * specify the value of `this` within the callback function. If `thisArg` is provided, it will be
656
+ * used as the `this` value when calling the callback function. If `thisArg` is not provided, `
657
+ */
658
+ forEach(callbackfn, thisArg) {
659
+ let index = 0;
660
+ for (const item of this) {
661
+ const [key, value] = item;
662
+ callbackfn.call(thisArg, value, key, index++, this);
663
+ }
664
+ }
665
+ /**
666
+ * Time Complexity: O(n)
667
+ * Space Complexity: O(1)
668
+ */
669
+ /**
670
+ * Time Complexity: O(n)
671
+ * Space Complexity: O(1)
672
+ *
673
+ * The `reduce` function iterates over key-value pairs and applies a callback function to each pair,
674
+ * accumulating a single value.
675
+ * @param callbackfn - The callback function that will be called for each element in the collection.
676
+ * It takes four arguments: the current accumulator value, the current value of the element, the key
677
+ * of the element, and the index of the element in the collection. It should return the updated
678
+ * accumulator value.
679
+ * @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
680
+ * is the value that will be used as the first argument to the `callbackfn` function when reducing
681
+ * the elements of the collection.
682
+ * @returns The `reduce` method is returning the final value of the accumulator after iterating over
683
+ * all the elements in the collection.
684
+ */
685
+ reduce(callbackfn, initialValue) {
686
+ let accumulator = initialValue;
687
+ let index = 0;
688
+ for (const item of this) {
689
+ const [key, value] = item;
690
+ accumulator = callbackfn(accumulator, value, key, index++, this);
691
+ }
692
+ return accumulator;
693
+ }
694
+ };
695
+ var IterableElementBase = class {
696
+ /**
697
+ * Time Complexity: O(n)
698
+ * Space Complexity: O(1)
699
+ */
700
+ /**
701
+ * Time Complexity: O(n)
702
+ * Space Complexity: O(1)
703
+ *
704
+ * The function is an implementation of the Symbol.iterator method that returns an IterableIterator.
705
+ * @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
706
+ * allows the function to accept any number of arguments as an array. In this case, the `args`
707
+ * parameter is used to pass any number of arguments to the `_getIterator` method.
708
+ */
709
+ *[Symbol.iterator](...args) {
710
+ yield* __yieldStar(this._getIterator(...args));
711
+ }
712
+ /**
713
+ * Time Complexity: O(n)
714
+ * Space Complexity: O(n)
715
+ */
716
+ /**
717
+ * Time Complexity: O(n)
718
+ * Space Complexity: O(n)
719
+ *
720
+ * The function returns an iterator that yields all the values in the object.
721
+ */
722
+ *values() {
723
+ for (const item of this) {
724
+ yield item;
725
+ }
726
+ }
727
+ /**
728
+ * Time Complexity: O(n)
729
+ * Space Complexity: O(1)
730
+ */
731
+ /**
732
+ * Time Complexity: O(n)
733
+ * Space Complexity: O(1)
734
+ *
735
+ * The `every` function checks if every element in the array satisfies a given predicate.
736
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
737
+ * the current element being processed, its index, and the array it belongs to. It should return a
738
+ * boolean value indicating whether the element satisfies a certain condition or not.
739
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
740
+ * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
741
+ * passed as the `this` value to the `predicate` function. If `thisArg` is
742
+ * @returns The `every` method is returning a boolean value. It returns `true` if every element in
743
+ * the array satisfies the provided predicate function, and `false` otherwise.
744
+ */
745
+ every(predicate, thisArg) {
746
+ let index = 0;
747
+ for (const item of this) {
748
+ if (!predicate.call(thisArg, item, index++, this)) {
749
+ return false;
750
+ }
751
+ }
752
+ return true;
753
+ }
754
+ /**
755
+ * Time Complexity: O(n)
756
+ * Space Complexity: O(1)
757
+ */
758
+ /**
759
+ * Time Complexity: O(n)
760
+ * Space Complexity: O(1)
761
+ *
762
+ * The "some" function checks if at least one element in a collection satisfies a given predicate.
763
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
764
+ * `value`, `index`, and `array`. It should return a boolean value indicating whether the current
765
+ * element satisfies the condition.
766
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
767
+ * to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
768
+ * it will be passed as the `this` value to the `predicate` function. If `thisArg
769
+ * @returns a boolean value. It returns true if the predicate function returns true for any element
770
+ * in the collection, and false otherwise.
771
+ */
772
+ some(predicate, thisArg) {
773
+ let index = 0;
774
+ for (const item of this) {
775
+ if (predicate.call(thisArg, item, index++, this)) {
776
+ return true;
777
+ }
778
+ }
779
+ return false;
780
+ }
781
+ /**
782
+ * Time Complexity: O(n)
783
+ * Space Complexity: O(1)
784
+ */
785
+ /**
786
+ * Time Complexity: O(n)
787
+ * Space Complexity: O(1)
788
+ *
789
+ * The `forEach` function iterates over each element in an array-like object and calls a callback
790
+ * function for each element.
791
+ * @param callbackfn - The callbackfn parameter is a function that will be called for each element in
792
+ * the array. It takes three arguments: the current element being processed, the index of the current
793
+ * element, and the array that forEach was called upon.
794
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
795
+ * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
796
+ * be passed as the `this` value to the `callbackfn` function. If `thisArg
797
+ */
798
+ forEach(callbackfn, thisArg) {
799
+ let index = 0;
800
+ for (const item of this) {
801
+ callbackfn.call(thisArg, item, index++, this);
802
+ }
803
+ }
804
+ /**
805
+ * Time Complexity: O(n)
806
+ * Space Complexity: O(1)
807
+ */
808
+ /**
809
+ * Time Complexity: O(n)
810
+ * Space Complexity: O(1)
811
+ *
812
+ * The `reduce` function iterates over the elements of an array-like object and applies a callback
813
+ * function to reduce them into a single value.
814
+ * @param callbackfn - The callbackfn parameter is a function that will be called for each element in
815
+ * the array. It takes four arguments:
816
+ * @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
817
+ * is the value that the accumulator starts with before the reduction operation begins.
818
+ * @returns The `reduce` method is returning the final value of the accumulator after iterating over
819
+ * all the elements in the array and applying the callback function to each element.
820
+ */
821
+ reduce(callbackfn, initialValue) {
822
+ let accumulator = initialValue;
823
+ let index = 0;
824
+ for (const item of this) {
825
+ accumulator = callbackfn(accumulator, item, index++, this);
826
+ }
827
+ return accumulator;
828
+ }
829
+ };
830
+
519
831
  // src/data-structures/hash/hash-map.ts
520
- var HashMap = class _HashMap {
832
+ var HashMap = class _HashMap extends IterablePairBase {
521
833
  /**
522
834
  * The constructor function initializes a new instance of a class with optional elements and options.
523
835
  * @param elements - The `elements` parameter is an iterable containing key-value pairs `[K, V]`. It
@@ -527,6 +839,7 @@ var dataStructureTyped = (() => {
527
839
  * configuration options for the constructor. In this case, it has one property:
528
840
  */
529
841
  constructor(elements = [], options) {
842
+ super();
530
843
  __publicField(this, "_store", {});
531
844
  __publicField(this, "_objMap", /* @__PURE__ */ new Map());
532
845
  __publicField(this, "_size", 0);
@@ -639,95 +952,13 @@ var dataStructureTyped = (() => {
639
952
  }
640
953
  }
641
954
  /**
642
- * The function returns an iterator that yields key-value pairs from both an object store and an
643
- * object map.
644
- */
645
- *[Symbol.iterator]() {
646
- for (const node of Object.values(this._store)) {
647
- yield [node.key, node.value];
648
- }
649
- for (const node of this._objMap) {
650
- yield node;
651
- }
652
- }
653
- /**
654
- * The function returns an iterator that yields key-value pairs from the object.
655
- */
656
- *entries() {
657
- for (const item of this) {
658
- yield item;
659
- }
660
- }
661
- /**
662
- * The function `keys()` returns an iterator that yields all the keys of the object.
663
- */
664
- *keys() {
665
- for (const [key] of this) {
666
- yield key;
667
- }
668
- }
669
- *values() {
670
- for (const [, value] of this) {
671
- yield value;
672
- }
673
- }
674
- /**
675
- * The `every` function checks if every element in a HashMap satisfies a given predicate function.
676
- * @param predicate - The predicate parameter is a function that takes four arguments: value, key,
677
- * index, and map. It is used to test each element in the map against a condition. If the predicate
678
- * function returns false for any element, the every() method will return false. If the predicate
679
- * function returns true for all
680
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
681
- * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
682
- * passed as the `this` value to the `predicate` function. If `thisArg` is
683
- * @returns The method is returning a boolean value. It returns true if the predicate function
684
- * returns true for every element in the map, and false otherwise.
685
- */
686
- every(predicate, thisArg) {
687
- let index = 0;
688
- for (const [key, value] of this) {
689
- if (!predicate.call(thisArg, value, key, index++, this)) {
690
- return false;
691
- }
692
- }
693
- return true;
694
- }
695
- /**
696
- * The "some" function checks if at least one element in a HashMap satisfies a given predicate.
697
- * @param predicate - The `predicate` parameter is a function that takes four arguments: `value`,
698
- * `key`, `index`, and `map`. It is used to determine whether a specific condition is met for a given
699
- * key-value pair in the `HashMap`.
700
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
701
- * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
702
- * passed as the `this` value to the `predicate` function. If `thisArg` is
703
- * @returns a boolean value. It returns true if the predicate function returns true for any element
704
- * in the map, and false otherwise.
705
- */
706
- some(predicate, thisArg) {
707
- let index = 0;
708
- for (const [key, value] of this) {
709
- if (predicate.call(thisArg, value, key, index++, this)) {
710
- return true;
711
- }
712
- }
713
- return false;
714
- }
715
- /**
716
- * The `forEach` function iterates over the elements of a HashMap and applies a callback function to
717
- * each element.
718
- * @param callbackfn - A function that will be called for each key-value pair in the HashMap. It
719
- * takes four parameters:
720
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
721
- * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
722
- * be passed as the `this` value inside the `callbackfn` function. If `thisArg
955
+ * Time Complexity: O(n)
956
+ * Space Complexity: O(n)
723
957
  */
724
- forEach(callbackfn, thisArg) {
725
- let index = 0;
726
- for (const [key, value] of this) {
727
- callbackfn.call(thisArg, value, key, index++, this);
728
- }
729
- }
730
958
  /**
959
+ * Time Complexity: O(n)
960
+ * Space Complexity: O(n)
961
+ *
731
962
  * The `map` function in TypeScript creates a new HashMap by applying a callback function to each
732
963
  * key-value pair in the original HashMap.
733
964
  * @param callbackfn - The callback function that will be called for each key-value pair in the
@@ -747,6 +978,13 @@ var dataStructureTyped = (() => {
747
978
  return resultMap;
748
979
  }
749
980
  /**
981
+ * Time Complexity: O(n)
982
+ * Space Complexity: O(n)
983
+ */
984
+ /**
985
+ * Time Complexity: O(n)
986
+ * Space Complexity: O(n)
987
+ *
750
988
  * The `filter` function creates a new HashMap containing key-value pairs from the original HashMap
751
989
  * that satisfy a given predicate function.
752
990
  * @param predicate - The predicate parameter is a function that takes four arguments: value, key,
@@ -769,27 +1007,20 @@ var dataStructureTyped = (() => {
769
1007
  }
770
1008
  return filteredMap;
771
1009
  }
1010
+ print() {
1011
+ console.log([...this.entries()]);
1012
+ }
772
1013
  /**
773
- * The `reduce` function iterates over the elements of a HashMap and applies a callback function to
774
- * each element, accumulating a single value.
775
- * @param callbackfn - The callback function that will be called for each element in the HashMap. It
776
- * takes five parameters:
777
- * @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
778
- * is the value that will be used as the first argument of the callback function when reducing the
779
- * elements of the map.
780
- * @returns The `reduce` method is returning the final value of the accumulator after iterating over
781
- * all the elements in the `HashMap`.
1014
+ * The function returns an iterator that yields key-value pairs from both an object store and an
1015
+ * object map.
782
1016
  */
783
- reduce(callbackfn, initialValue) {
784
- let accumulator = initialValue;
785
- let index = 0;
786
- for (const [key, value] of this) {
787
- accumulator = callbackfn(accumulator, value, key, index++, this);
1017
+ *_getIterator() {
1018
+ for (const node of Object.values(this._store)) {
1019
+ yield [node.key, node.value];
1020
+ }
1021
+ for (const node of this._objMap) {
1022
+ yield node;
788
1023
  }
789
- return accumulator;
790
- }
791
- print() {
792
- console.log([...this.entries()]);
793
1024
  }
794
1025
  _isObjKey(key) {
795
1026
  const keyType = typeof key;
@@ -810,11 +1041,12 @@ var dataStructureTyped = (() => {
810
1041
  return strKey;
811
1042
  }
812
1043
  };
813
- var LinkedHashMap = class _LinkedHashMap {
1044
+ var LinkedHashMap = class _LinkedHashMap extends IterablePairBase {
814
1045
  constructor(elements, options = {
815
1046
  hashFn: (key) => String(key),
816
1047
  objHashFn: (key) => key
817
1048
  }) {
1049
+ super();
818
1050
  __publicField(this, "_noObjMap", {});
819
1051
  __publicField(this, "_objMap", /* @__PURE__ */ new WeakMap());
820
1052
  __publicField(this, "_head");
@@ -946,18 +1178,6 @@ var dataStructureTyped = (() => {
946
1178
  this.set(key, value);
947
1179
  }
948
1180
  }
949
- keys() {
950
- const keys = [];
951
- for (const [key] of this)
952
- keys.push(key);
953
- return keys;
954
- }
955
- values() {
956
- const values = [];
957
- for (const [, value] of this)
958
- values.push(value);
959
- return values;
960
- }
961
1181
  /**
962
1182
  * Time Complexity: O(1)
963
1183
  * Space Complexity: O(1)
@@ -1080,35 +1300,29 @@ var dataStructureTyped = (() => {
1080
1300
  return cloned;
1081
1301
  }
1082
1302
  /**
1083
- * Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
1084
- * Space Complexity: O(1)
1085
- *
1086
- * The `forEach` function iterates over each element in a LinkedHashMap and executes a callback function on
1087
- * each element.
1088
- * @param callback - The callback parameter is a function that will be called for each element in the
1089
- * LinkedHashMap. It takes three arguments:
1303
+ * Time Complexity: O(n)
1304
+ * Space Complexity: O(n)
1090
1305
  */
1091
- forEach(callback) {
1092
- let index = 0;
1093
- let node = this._head;
1094
- while (node !== this._sentinel) {
1095
- callback([node.key, node.value], index++, this);
1096
- node = node.next;
1097
- }
1098
- }
1099
1306
  /**
1100
- * The `filter` function takes a predicate function and returns a new LinkedHashMap containing only the
1101
- * key-value pairs that satisfy the predicate.
1102
- * @param predicate - The `predicate` parameter is a function that takes two arguments: `element` and
1103
- * `map`.
1104
- * @returns a new LinkedHashMap object that contains the key-value pairs from the original LinkedHashMap that
1105
- * satisfy the given predicate function.
1307
+ * Time Complexity: O(n)
1308
+ * Space Complexity: O(n)
1309
+ *
1310
+ * The `filter` function creates a new `LinkedHashMap` containing key-value pairs from the original
1311
+ * map that satisfy a given predicate function.
1312
+ * @param predicate - The `predicate` parameter is a callback function that takes four arguments:
1313
+ * `value`, `key`, `index`, and `this`. It should return a boolean value indicating whether the
1314
+ * current element should be included in the filtered map or not.
1315
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1316
+ * specify the value of `this` within the `predicate` function. It is used when you want to bind a
1317
+ * specific object as the context for the `predicate` function. If `thisArg` is not provided, `this
1318
+ * @returns a new `LinkedHashMap` object that contains the key-value pairs from the original
1319
+ * `LinkedHashMap` object that satisfy the given predicate function.
1106
1320
  */
1107
- filter(predicate) {
1321
+ filter(predicate, thisArg) {
1108
1322
  const filteredMap = new _LinkedHashMap();
1109
1323
  let index = 0;
1110
1324
  for (const [key, value] of this) {
1111
- if (predicate([key, value], index, this)) {
1325
+ if (predicate.call(thisArg, value, key, index, this)) {
1112
1326
  filteredMap.set(key, value);
1113
1327
  }
1114
1328
  index++;
@@ -1116,42 +1330,38 @@ var dataStructureTyped = (() => {
1116
1330
  return filteredMap;
1117
1331
  }
1118
1332
  /**
1119
- * The `map` function takes a callback function and returns a new LinkedHashMap with the values transformed
1120
- * by the callback.
1121
- * @param callback - The `callback` parameter is a function that takes two arguments: `element` and
1122
- * `map`.
1123
- * @returns a new LinkedHashMap object with the values mapped according to the provided callback function.
1333
+ * Time Complexity: O(n)
1334
+ * Space Complexity: O(n)
1124
1335
  */
1125
- map(callback) {
1336
+ /**
1337
+ * Time Complexity: O(n)
1338
+ * Space Complexity: O(n)
1339
+ *
1340
+ * The `map` function in TypeScript creates a new `LinkedHashMap` by applying a callback function to
1341
+ * each key-value pair in the original map.
1342
+ * @param callback - The callback parameter is a function that will be called for each key-value pair
1343
+ * in the map. It takes four arguments: the value of the current key-value pair, the key of the
1344
+ * current key-value pair, the index of the current key-value pair, and the map itself. The callback
1345
+ * function should
1346
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1347
+ * specify the value of `this` within the callback function. If provided, the callback function will
1348
+ * be called with `thisArg` as its `this` value. If not provided, `this` will refer to the current
1349
+ * map
1350
+ * @returns a new `LinkedHashMap` object with the values mapped according to the provided callback
1351
+ * function.
1352
+ */
1353
+ map(callback, thisArg) {
1126
1354
  const mappedMap = new _LinkedHashMap();
1127
1355
  let index = 0;
1128
1356
  for (const [key, value] of this) {
1129
- const newValue = callback([key, value], index, this);
1357
+ const newValue = callback.call(thisArg, value, key, index, this);
1130
1358
  mappedMap.set(key, newValue);
1131
1359
  index++;
1132
1360
  }
1133
1361
  return mappedMap;
1134
1362
  }
1135
- /**
1136
- * The `reduce` function iterates over the elements of a LinkedHashMap and applies a callback function to
1137
- * each element, accumulating a single value.
1138
- * @param callback - The callback parameter is a function that takes three arguments: accumulator,
1139
- * element, and map. It is called for each element in the LinkedHashMap and is used to accumulate a single
1140
- * result.
1141
- * @param {A} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
1142
- * is the value that will be passed as the first argument to the `callback` function when reducing
1143
- * the elements of the map.
1144
- * @returns The `reduce` function is returning the final value of the accumulator after iterating
1145
- * over all the elements in the LinkedHashMap and applying the callback function to each element.
1146
- */
1147
- reduce(callback, initialValue) {
1148
- let accumulator = initialValue;
1149
- let index = 0;
1150
- for (const entry of this) {
1151
- accumulator = callback(accumulator, entry, index, this);
1152
- index++;
1153
- }
1154
- return accumulator;
1363
+ print() {
1364
+ console.log([...this]);
1155
1365
  }
1156
1366
  /**
1157
1367
  * Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
@@ -1159,16 +1369,13 @@ var dataStructureTyped = (() => {
1159
1369
  *
1160
1370
  * The above function is an iterator that yields key-value pairs from a linked list.
1161
1371
  */
1162
- *[Symbol.iterator]() {
1372
+ *_getIterator() {
1163
1373
  let node = this._head;
1164
1374
  while (node !== this._sentinel) {
1165
1375
  yield [node.key, node.value];
1166
1376
  node = node.next;
1167
1377
  }
1168
1378
  }
1169
- print() {
1170
- console.log([...this]);
1171
- }
1172
1379
  /**
1173
1380
  * Time Complexity: O(1)
1174
1381
  * Space Complexity: O(1)
@@ -1207,11 +1414,12 @@ var dataStructureTyped = (() => {
1207
1414
  this.next = void 0;
1208
1415
  }
1209
1416
  };
1210
- var SinglyLinkedList = class _SinglyLinkedList {
1417
+ var SinglyLinkedList = class _SinglyLinkedList extends IterableElementBase {
1211
1418
  /**
1212
1419
  * The constructor initializes the linked list with an empty head, tail, and length.
1213
1420
  */
1214
1421
  constructor(elements) {
1422
+ super();
1215
1423
  __publicField(this, "_head");
1216
1424
  __publicField(this, "_tail");
1217
1425
  __publicField(this, "_length");
@@ -1793,54 +2001,31 @@ var dataStructureTyped = (() => {
1793
2001
  return count;
1794
2002
  }
1795
2003
  /**
1796
- * The function returns an iterator that iterates over the values of a linked list.
1797
- */
1798
- *[Symbol.iterator]() {
1799
- let current = this.head;
1800
- while (current) {
1801
- yield current.value;
1802
- current = current.next;
1803
- }
1804
- }
1805
- /**
1806
- * Time Complexity: O(n), where n is the number of elements in the linked list.
1807
- * Space Complexity: O(1)
1808
- */
1809
- /**
1810
- * Time Complexity: O(n), where n is the number of elements in the linked list.
1811
- * Space Complexity: O(1)
1812
- *
1813
- * The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
1814
- * @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
1815
- * represents the value of the current node in the linked list, and the index argument represents the index of the
1816
- * current node in the linked list.
1817
- */
1818
- forEach(callback) {
1819
- let index = 0;
1820
- for (const el of this) {
1821
- callback(el, index, this);
1822
- index++;
1823
- }
1824
- }
1825
- /**
1826
- * Time Complexity: O(n), where n is the number of elements in the linked list.
2004
+ * Time Complexity: O(n)
1827
2005
  * Space Complexity: O(n)
1828
2006
  */
1829
2007
  /**
1830
- * Time Complexity: O(n), where n is the number of elements in the linked list.
2008
+ * Time Complexity: O(n)
1831
2009
  * Space Complexity: O(n)
1832
2010
  *
1833
- * The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
1834
- * elements that satisfy the given callback function.
1835
- * @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
1836
- * It is used to determine whether a value should be included in the filtered list or not.
1837
- * @returns The filtered list, which is an instance of the SinglyLinkedList class.
2011
+ * The `filter` function creates a new SinglyLinkedList by iterating over the elements of the current
2012
+ * list and applying a callback function to each element to determine if it should be included in the
2013
+ * filtered list.
2014
+ * @param callback - The callback parameter is a function that will be called for each element in the
2015
+ * list. It takes three arguments: the current element, the index of the current element, and the
2016
+ * list itself. The callback function should return a boolean value indicating whether the current
2017
+ * element should be included in the filtered list or not
2018
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
2019
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
2020
+ * passed as the `this` value to the `callback` function. If `thisArg` is
2021
+ * @returns The `filter` method is returning a new `SinglyLinkedList` object that contains the
2022
+ * elements that pass the filter condition specified by the `callback` function.
1838
2023
  */
1839
- filter(callback) {
2024
+ filter(callback, thisArg) {
1840
2025
  const filteredList = new _SinglyLinkedList();
1841
2026
  let index = 0;
1842
2027
  for (const current of this) {
1843
- if (callback(current, index, this)) {
2028
+ if (callback.call(thisArg, current, index, this)) {
1844
2029
  filteredList.push(current);
1845
2030
  }
1846
2031
  index++;
@@ -1852,21 +2037,24 @@ var dataStructureTyped = (() => {
1852
2037
  * Space Complexity: O(n)
1853
2038
  */
1854
2039
  /**
1855
- * Time Complexity: O(n), where n is the number of elements in the linked list.
2040
+ * Time Complexity: O(n)
1856
2041
  * Space Complexity: O(n)
1857
2042
  *
1858
- * The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
1859
- * SinglyLinkedList with the transformed values.
1860
- * @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
1861
- * the original SinglyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
1862
- * SinglyLinkedList).
1863
- * @returns The `map` function is returning a new instance of `SinglyLinkedList<T>` that contains the mapped values.
2043
+ * The `map` function creates a new SinglyLinkedList by applying a callback function to each element
2044
+ * of the original list.
2045
+ * @param callback - The `callback` parameter is a function that will be called for each element in
2046
+ * the linked list. It takes three arguments:
2047
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
2048
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
2049
+ * passed as the `this` value to the `callback` function. If `thisArg` is
2050
+ * @returns The `map` function is returning a new `SinglyLinkedList` object that contains the results
2051
+ * of applying the provided `callback` function to each element in the original list.
1864
2052
  */
1865
- map(callback) {
2053
+ map(callback, thisArg) {
1866
2054
  const mappedList = new _SinglyLinkedList();
1867
2055
  let index = 0;
1868
2056
  for (const current of this) {
1869
- mappedList.push(callback(current, index, this));
2057
+ mappedList.push(callback.call(thisArg, current, index, this));
1870
2058
  index++;
1871
2059
  }
1872
2060
  return mappedList;
@@ -1875,31 +2063,16 @@ var dataStructureTyped = (() => {
1875
2063
  * Time Complexity: O(n), where n is the number of elements in the linked list.
1876
2064
  * Space Complexity: O(n)
1877
2065
  */
1878
- /**
1879
- * Time Complexity: O(n), where n is the number of elements in the linked list.
1880
- * Space Complexity: O(n)
1881
- *
1882
- * The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
1883
- * single value.
1884
- * @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
1885
- * used to perform a specific operation on each element of the linked list.
1886
- * @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
1887
- * point for the reduction operation.
1888
- * @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
1889
- * elements in the linked list.
1890
- */
1891
- reduce(callback, initialValue) {
1892
- let accumulator = initialValue;
1893
- let index = 0;
1894
- for (const current of this) {
1895
- accumulator = callback(accumulator, current, index, this);
1896
- index++;
1897
- }
1898
- return accumulator;
1899
- }
1900
2066
  print() {
1901
2067
  console.log([...this]);
1902
2068
  }
2069
+ *_getIterator() {
2070
+ let current = this.head;
2071
+ while (current) {
2072
+ yield current.value;
2073
+ current = current.next;
2074
+ }
2075
+ }
1903
2076
  };
1904
2077
 
1905
2078
  // src/data-structures/linked-list/doubly-linked-list.ts
@@ -1918,11 +2091,12 @@ var dataStructureTyped = (() => {
1918
2091
  this.prev = void 0;
1919
2092
  }
1920
2093
  };
1921
- var DoublyLinkedList = class _DoublyLinkedList {
2094
+ var DoublyLinkedList = class _DoublyLinkedList extends IterableElementBase {
1922
2095
  /**
1923
2096
  * The constructor initializes the linked list with an empty head, tail, and length.
1924
2097
  */
1925
2098
  constructor(elements) {
2099
+ super();
1926
2100
  __publicField(this, "_head");
1927
2101
  __publicField(this, "_tail");
1928
2102
  __publicField(this, "_length");
@@ -2558,54 +2732,31 @@ var dataStructureTyped = (() => {
2558
2732
  return array;
2559
2733
  }
2560
2734
  /**
2561
- * The function returns an iterator that iterates over the values of a linked list.
2562
- */
2563
- *[Symbol.iterator]() {
2564
- let current = this.head;
2565
- while (current) {
2566
- yield current.value;
2567
- current = current.next;
2568
- }
2569
- }
2570
- /**
2571
- * Time Complexity: O(n), where n is the number of elements in the linked list.
2572
- * Space Complexity: O(1)
2573
- */
2574
- /**
2575
- * Time Complexity: O(n), where n is the number of elements in the linked list.
2576
- * Space Complexity: O(1)
2577
- *
2578
- * The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
2579
- * @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
2580
- * represents the value of the current node in the linked list, and the index argument represents the index of the
2581
- * current node in the linked list.
2582
- */
2583
- forEach(callback) {
2584
- let index = 0;
2585
- for (const el of this) {
2586
- callback(el, index, this);
2587
- index++;
2588
- }
2589
- }
2590
- /**
2591
- * Time Complexity: O(n), where n is the number of elements in the linked list.
2735
+ * Time Complexity: O(n)
2592
2736
  * Space Complexity: O(n)
2593
2737
  */
2594
2738
  /**
2595
- * Time Complexity: O(n), where n is the number of elements in the linked list.
2739
+ * Time Complexity: O(n)
2596
2740
  * Space Complexity: O(n)
2597
2741
  *
2598
- * The `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the
2599
- * elements that satisfy the given callback function.
2600
- * @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
2601
- * It is used to determine whether a value should be included in the filtered list or not.
2602
- * @returns The filtered list, which is an instance of the DoublyLinkedList class.
2742
+ * The `filter` function creates a new DoublyLinkedList by iterating over the elements of the current
2743
+ * list and applying a callback function to each element, returning only the elements for which the
2744
+ * callback function returns true.
2745
+ * @param callback - The `callback` parameter is a function that will be called for each element in
2746
+ * the DoublyLinkedList. It takes three arguments: the current element, the index of the current
2747
+ * element, and the DoublyLinkedList itself. The callback function should return a boolean value
2748
+ * indicating whether the current element should be included
2749
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
2750
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
2751
+ * passed as the `this` value to the `callback` function. If `thisArg` is
2752
+ * @returns The `filter` method is returning a new `DoublyLinkedList` object that contains the
2753
+ * elements that pass the filter condition specified by the `callback` function.
2603
2754
  */
2604
- filter(callback) {
2755
+ filter(callback, thisArg) {
2605
2756
  const filteredList = new _DoublyLinkedList();
2606
2757
  let index = 0;
2607
2758
  for (const current of this) {
2608
- if (callback(current, index, this)) {
2759
+ if (callback.call(thisArg, current, index, this)) {
2609
2760
  filteredList.push(current);
2610
2761
  }
2611
2762
  index++;
@@ -2617,54 +2768,48 @@ var dataStructureTyped = (() => {
2617
2768
  * Space Complexity: O(n)
2618
2769
  */
2619
2770
  /**
2620
- * Time Complexity: O(n), where n is the number of elements in the linked list.
2771
+ * Time Complexity: O(n)
2621
2772
  * Space Complexity: O(n)
2622
2773
  *
2623
- * The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
2624
- * DoublyLinkedList with the transformed values.
2625
- * @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
2626
- * the original DoublyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
2627
- * DoublyLinkedList).
2628
- * @returns The `map` function is returning a new instance of `DoublyLinkedList<T>` that contains the mapped values.
2774
+ * The `map` function creates a new DoublyLinkedList by applying a callback function to each element
2775
+ * in the original list.
2776
+ * @param callback - The callback parameter is a function that will be called for each element in the
2777
+ * DoublyLinkedList. It takes three arguments: the current element, the index of the current element,
2778
+ * and the DoublyLinkedList itself. The callback function should return a value that will be added to
2779
+ * the new DoublyLinkedList that
2780
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
2781
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
2782
+ * passed as the `this` value to the `callback` function. If `thisArg` is
2783
+ * @returns The `map` function is returning a new `DoublyLinkedList` object that contains the results
2784
+ * of applying the provided `callback` function to each element in the original `DoublyLinkedList`
2785
+ * object.
2629
2786
  */
2630
- map(callback) {
2787
+ map(callback, thisArg) {
2631
2788
  const mappedList = new _DoublyLinkedList();
2632
2789
  let index = 0;
2633
2790
  for (const current of this) {
2634
- mappedList.push(callback(current, index, this));
2791
+ mappedList.push(callback.call(thisArg, current, index, this));
2635
2792
  index++;
2636
2793
  }
2637
- return mappedList;
2638
- }
2639
- /**
2640
- * Time Complexity: O(n), where n is the number of elements in the linked list.
2641
- * Space Complexity: O(n)
2642
- */
2643
- /**
2644
- * Time Complexity: O(n), where n is the number of elements in the linked list.
2645
- * Space Complexity: O(n)
2646
- *
2647
- * The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
2648
- * single value.
2649
- * @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
2650
- * used to perform a specific operation on each element of the linked list.
2651
- * @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
2652
- * point for the reduction operation.
2653
- * @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
2654
- * elements in the linked list.
2655
- */
2656
- reduce(callback, initialValue) {
2657
- let accumulator = initialValue;
2658
- let index = 0;
2659
- for (const current of this) {
2660
- accumulator = callback(accumulator, current, index, this);
2661
- index++;
2662
- }
2663
- return accumulator;
2794
+ return mappedList;
2664
2795
  }
2796
+ /**
2797
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
2798
+ * Space Complexity: O(n)
2799
+ */
2665
2800
  print() {
2666
2801
  console.log([...this]);
2667
2802
  }
2803
+ /**
2804
+ * The function returns an iterator that iterates over the values of a linked list.
2805
+ */
2806
+ *_getIterator() {
2807
+ let current = this.head;
2808
+ while (current) {
2809
+ yield current.value;
2810
+ current = current.next;
2811
+ }
2812
+ }
2668
2813
  };
2669
2814
 
2670
2815
  // src/data-structures/linked-list/skip-linked-list.ts
@@ -2916,7 +3061,7 @@ var dataStructureTyped = (() => {
2916
3061
  };
2917
3062
 
2918
3063
  // src/data-structures/stack/stack.ts
2919
- var Stack = class _Stack {
3064
+ var Stack = class _Stack extends IterableElementBase {
2920
3065
  /**
2921
3066
  * The constructor initializes an array of elements, which can be provided as an optional parameter.
2922
3067
  * @param {E[]} [elements] - The `elements` parameter is an optional parameter of type `E[]`, which represents an array
@@ -2924,6 +3069,7 @@ var dataStructureTyped = (() => {
2924
3069
  * is provided and is an array, it is assigned to the `_elements
2925
3070
  */
2926
3071
  constructor(elements) {
3072
+ super();
2927
3073
  __publicField(this, "_elements");
2928
3074
  this._elements = [];
2929
3075
  if (elements) {
@@ -3049,92 +3195,78 @@ var dataStructureTyped = (() => {
3049
3195
  return new _Stack(this.elements.slice());
3050
3196
  }
3051
3197
  /**
3052
- * Custom iterator for the Stack class.
3053
- * @returns An iterator object.
3198
+ * Time Complexity: O(n)
3199
+ * Space Complexity: O(n)
3054
3200
  */
3055
- *[Symbol.iterator]() {
3056
- for (let i = 0; i < this.elements.length; i++) {
3057
- yield this.elements[i];
3058
- }
3059
- }
3060
3201
  /**
3061
- * Applies a function to each element of the stack.
3062
- * @param {function(E): void} callback - A function to apply to each element.
3202
+ * Time Complexity: O(n)
3203
+ * Space Complexity: O(n)
3204
+ *
3205
+ * The `filter` function creates a new stack containing elements from the original stack that satisfy
3206
+ * a given predicate function.
3207
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
3208
+ * the current element being iterated over, the index of the current element, and the stack itself.
3209
+ * It should return a boolean value indicating whether the element should be included in the filtered
3210
+ * stack or not.
3211
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
3212
+ * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
3213
+ * passed as the `this` value to the `predicate` function. If `thisArg` is
3214
+ * @returns The `filter` method is returning a new `Stack` object that contains the elements that
3215
+ * satisfy the given predicate function.
3063
3216
  */
3064
- forEach(callback) {
3065
- let index = 0;
3066
- for (const el of this) {
3067
- callback(el, index, this);
3068
- index++;
3069
- }
3070
- }
3071
- filter(predicate) {
3217
+ filter(predicate, thisArg) {
3072
3218
  const newStack = new _Stack();
3073
3219
  let index = 0;
3074
3220
  for (const el of this) {
3075
- if (predicate(el, index, this)) {
3221
+ if (predicate.call(thisArg, el, index, this)) {
3076
3222
  newStack.push(el);
3077
3223
  }
3078
3224
  index++;
3079
3225
  }
3080
3226
  return newStack;
3081
3227
  }
3082
- map(callback) {
3228
+ /**
3229
+ * Time Complexity: O(n)
3230
+ * Space Complexity: O(n)
3231
+ */
3232
+ /**
3233
+ * Time Complexity: O(n)
3234
+ * Space Complexity: O(n)
3235
+ *
3236
+ * The `map` function takes a callback function and applies it to each element in the stack,
3237
+ * returning a new stack with the results.
3238
+ * @param callback - The `callback` parameter is a function that will be called for each element in
3239
+ * the stack. It takes three arguments:
3240
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
3241
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
3242
+ * passed as the `this` value to the `callback` function. If `thisArg` is
3243
+ * @returns The `map` method is returning a new `Stack` object.
3244
+ */
3245
+ map(callback, thisArg) {
3083
3246
  const newStack = new _Stack();
3084
3247
  let index = 0;
3085
3248
  for (const el of this) {
3086
- newStack.push(callback(el, index, this));
3249
+ newStack.push(callback.call(thisArg, el, index, this));
3087
3250
  index++;
3088
3251
  }
3089
3252
  return newStack;
3090
3253
  }
3091
- reduce(callback, initialValue) {
3092
- let accumulator = initialValue;
3093
- let index = 0;
3094
- for (const el of this) {
3095
- accumulator = callback(accumulator, el, index, this);
3096
- index++;
3097
- }
3098
- return accumulator;
3099
- }
3100
3254
  print() {
3101
3255
  console.log([...this]);
3102
3256
  }
3103
- };
3104
-
3105
- // src/data-structures/queue/queue.ts
3106
- var LinkedListQueue = class extends SinglyLinkedList {
3107
- /**
3108
- * The enqueue function adds a value to the end of an array.
3109
- * @param {E} value - The value parameter represents the value that you want to add to the queue.
3110
- */
3111
- enqueue(value) {
3112
- this.push(value);
3113
- }
3114
- /**
3115
- * The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
3116
- * @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
3117
- */
3118
- dequeue() {
3119
- return this.shift();
3120
- }
3121
- /**
3122
- * The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
3123
- * @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
3124
- */
3125
- getFirst() {
3126
- var _a;
3127
- return (_a = this.head) == null ? void 0 : _a.value;
3128
- }
3129
3257
  /**
3130
- * The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
3131
- * @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
3258
+ * Custom iterator for the Stack class.
3259
+ * @returns An iterator object.
3132
3260
  */
3133
- peek() {
3134
- return this.getFirst();
3261
+ *_getIterator() {
3262
+ for (let i = 0; i < this.elements.length; i++) {
3263
+ yield this.elements[i];
3264
+ }
3135
3265
  }
3136
3266
  };
3137
- var Queue = class _Queue {
3267
+
3268
+ // src/data-structures/queue/queue.ts
3269
+ var Queue = class _Queue extends IterableElementBase {
3138
3270
  /**
3139
3271
  * The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
3140
3272
  * @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it
@@ -3142,6 +3274,7 @@ var dataStructureTyped = (() => {
3142
3274
  * initialized as an empty array.
3143
3275
  */
3144
3276
  constructor(elements) {
3277
+ super();
3145
3278
  __publicField(this, "_nodes");
3146
3279
  __publicField(this, "_offset");
3147
3280
  this._nodes = elements || [];
@@ -3363,31 +3496,6 @@ var dataStructureTyped = (() => {
3363
3496
  print() {
3364
3497
  console.log([...this]);
3365
3498
  }
3366
- *[Symbol.iterator]() {
3367
- for (const item of this.nodes) {
3368
- yield item;
3369
- }
3370
- }
3371
- /**
3372
- * Time Complexity: O(n)
3373
- * Space Complexity: O(1)
3374
- */
3375
- /**
3376
- * Time Complexity: O(n)
3377
- * Space Complexity: O(1)
3378
- *
3379
- * The `forEach` function iterates over each element in a deque and applies a callback function to
3380
- * each element.
3381
- * @param callback - The callback parameter is a function that will be called for each element in the
3382
- * deque. It takes three parameters:
3383
- */
3384
- forEach(callback) {
3385
- let index = 0;
3386
- for (const el of this) {
3387
- callback(el, index, this);
3388
- index++;
3389
- }
3390
- }
3391
3499
  /**
3392
3500
  * Time Complexity: O(n)
3393
3501
  * Space Complexity: O(n)
@@ -3396,18 +3504,23 @@ var dataStructureTyped = (() => {
3396
3504
  * Time Complexity: O(n)
3397
3505
  * Space Complexity: O(n)
3398
3506
  *
3399
- * The `filter` function creates a new deque containing only the elements that satisfy the given
3400
- * predicate function.
3401
- * @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
3402
- * `index`, and `deque`.
3403
- * @returns The `filter` method is returning a new `Queue` object that contains only the elements
3404
- * that satisfy the given `predicate` function.
3507
+ * The `filter` function creates a new `Queue` object containing elements from the original `Queue`
3508
+ * that satisfy a given predicate function.
3509
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
3510
+ * the current element being iterated over, the index of the current element, and the queue itself.
3511
+ * It should return a boolean value indicating whether the element should be included in the filtered
3512
+ * queue or not.
3513
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
3514
+ * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
3515
+ * passed as the `this` value to the `predicate` function. If `thisArg` is
3516
+ * @returns The `filter` method is returning a new `Queue` object that contains the elements that
3517
+ * satisfy the given predicate function.
3405
3518
  */
3406
- filter(predicate) {
3519
+ filter(predicate, thisArg) {
3407
3520
  const newDeque = new _Queue([]);
3408
3521
  let index = 0;
3409
3522
  for (const el of this) {
3410
- if (predicate(el, index, this)) {
3523
+ if (predicate.call(thisArg, el, index, this)) {
3411
3524
  newDeque.push(el);
3412
3525
  }
3413
3526
  index++;
@@ -3422,33 +3535,69 @@ var dataStructureTyped = (() => {
3422
3535
  * Time Complexity: O(n)
3423
3536
  * Space Complexity: O(n)
3424
3537
  *
3425
- * The `map` function takes a callback function and applies it to each element in the deque,
3426
- * returning a new deque with the results.
3427
- * @param callback - The `callback` parameter is a function that takes three arguments:
3428
- * @returns The `map` method is returning a new `Queue` object with the transformed elements.
3538
+ * The `map` function takes a callback function and applies it to each element in the queue,
3539
+ * returning a new queue with the results.
3540
+ * @param callback - The callback parameter is a function that will be called for each element in the
3541
+ * queue. It takes three arguments: the current element, the index of the current element, and the
3542
+ * queue itself. The callback function should return a new value that will be added to the new queue.
3543
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
3544
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
3545
+ * passed as the `this` value to the `callback` function. If `thisArg` is
3546
+ * @returns The `map` function is returning a new `Queue` object with the transformed elements.
3429
3547
  */
3430
- map(callback) {
3548
+ map(callback, thisArg) {
3431
3549
  const newDeque = new _Queue([]);
3432
3550
  let index = 0;
3433
3551
  for (const el of this) {
3434
- newDeque.push(callback(el, index, this));
3552
+ newDeque.push(callback.call(thisArg, el, index, this));
3435
3553
  index++;
3436
3554
  }
3437
3555
  return newDeque;
3438
3556
  }
3439
- reduce(callback, initialValue) {
3440
- let accumulator = initialValue;
3441
- let index = 0;
3442
- for (const el of this) {
3443
- accumulator = callback(accumulator, el, index, this);
3444
- index++;
3557
+ /**
3558
+ * Time Complexity: O(n)
3559
+ * Space Complexity: O(n)
3560
+ */
3561
+ *_getIterator() {
3562
+ for (const item of this.nodes) {
3563
+ yield item;
3445
3564
  }
3446
- return accumulator;
3565
+ }
3566
+ };
3567
+ var LinkedListQueue = class extends SinglyLinkedList {
3568
+ /**
3569
+ * The enqueue function adds a value to the end of an array.
3570
+ * @param {E} value - The value parameter represents the value that you want to add to the queue.
3571
+ */
3572
+ enqueue(value) {
3573
+ this.push(value);
3574
+ }
3575
+ /**
3576
+ * The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
3577
+ * @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
3578
+ */
3579
+ dequeue() {
3580
+ return this.shift();
3581
+ }
3582
+ /**
3583
+ * The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
3584
+ * @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
3585
+ */
3586
+ getFirst() {
3587
+ var _a;
3588
+ return (_a = this.head) == null ? void 0 : _a.value;
3589
+ }
3590
+ /**
3591
+ * The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
3592
+ * @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
3593
+ */
3594
+ peek() {
3595
+ return this.getFirst();
3447
3596
  }
3448
3597
  };
3449
3598
 
3450
3599
  // src/data-structures/queue/deque.ts
3451
- var Deque = class _Deque {
3600
+ var Deque = class _Deque extends IterableElementBase {
3452
3601
  /**
3453
3602
  * The constructor initializes a data structure with a specified bucket size and populates it with
3454
3603
  * elements from an iterable.
@@ -3459,6 +3608,7 @@ var dataStructureTyped = (() => {
3459
3608
  * stored in each bucket. It determines the size of each bucket in the data structure.
3460
3609
  */
3461
3610
  constructor(elements = [], bucketSize = 1 << 12) {
3611
+ super();
3462
3612
  __publicField(this, "_bucketFirst", 0);
3463
3613
  __publicField(this, "_firstInBucket", 0);
3464
3614
  __publicField(this, "_bucketLast", 0);
@@ -4086,42 +4236,6 @@ var dataStructureTyped = (() => {
4086
4236
  }
4087
4237
  return arr;
4088
4238
  }
4089
- /**
4090
- * Time Complexity: O(n)
4091
- * Space Complexity: O(1)
4092
- */
4093
- /**
4094
- * Time Complexity: O(n)
4095
- * Space Complexity: O(1)
4096
- *
4097
- * The above function is an implementation of the iterator protocol in TypeScript, allowing the
4098
- * object to be iterated over using a for...of loop.
4099
- */
4100
- *[Symbol.iterator]() {
4101
- for (let i = 0; i < this.size; ++i) {
4102
- yield this.getAt(i);
4103
- }
4104
- }
4105
- /**
4106
- * Time Complexity: O(n)
4107
- * Space Complexity: O(1)
4108
- */
4109
- /**
4110
- * Time Complexity: O(n)
4111
- * Space Complexity: O(1)
4112
- *
4113
- * The `forEach` function iterates over each element in a deque and applies a callback function to
4114
- * each element.
4115
- * @param callback - The callback parameter is a function that will be called for each element in the
4116
- * deque. It takes three parameters:
4117
- */
4118
- forEach(callback) {
4119
- let index = 0;
4120
- for (const el of this) {
4121
- callback(el, index, this);
4122
- index++;
4123
- }
4124
- }
4125
4239
  /**
4126
4240
  * Time Complexity: O(n)
4127
4241
  * Space Complexity: O(n)
@@ -4130,18 +4244,23 @@ var dataStructureTyped = (() => {
4130
4244
  * Time Complexity: O(n)
4131
4245
  * Space Complexity: O(n)
4132
4246
  *
4133
- * The `filter` function creates a new deque containing only the elements that satisfy the given
4134
- * predicate function.
4135
- * @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
4136
- * `index`, and `deque`.
4137
- * @returns The `filter` method is returning a new `Deque` object that contains only the elements
4138
- * that satisfy the given `predicate` function.
4247
+ * The `filter` function creates a new deque containing elements from the original deque that satisfy
4248
+ * a given predicate function.
4249
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
4250
+ * the current element being iterated over, the index of the current element, and the deque itself.
4251
+ * It should return a boolean value indicating whether the element should be included in the filtered
4252
+ * deque or not.
4253
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
4254
+ * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
4255
+ * passed as the `this` value to the `predicate` function. If `thisArg` is
4256
+ * @returns The `filter` method is returning a new `Deque` object that contains the elements that
4257
+ * satisfy the given predicate function.
4139
4258
  */
4140
- filter(predicate) {
4259
+ filter(predicate, thisArg) {
4141
4260
  const newDeque = new _Deque([], this._bucketSize);
4142
4261
  let index = 0;
4143
4262
  for (const el of this) {
4144
- if (predicate(el, index, this)) {
4263
+ if (predicate.call(thisArg, el, index, this)) {
4145
4264
  newDeque.push(el);
4146
4265
  }
4147
4266
  index++;
@@ -4156,48 +4275,42 @@ var dataStructureTyped = (() => {
4156
4275
  * Time Complexity: O(n)
4157
4276
  * Space Complexity: O(n)
4158
4277
  *
4159
- * The `map` function takes a callback function and applies it to each element in the deque,
4160
- * returning a new deque with the results.
4161
- * @param callback - The `callback` parameter is a function that takes three arguments:
4162
- * @returns The `map` method is returning a new `Deque` object with the transformed elements.
4278
+ * The `map` function creates a new Deque by applying a callback function to each element of the
4279
+ * original Deque.
4280
+ * @param callback - The `callback` parameter is a function that will be called for each element in
4281
+ * the deque. It takes three arguments:
4282
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
4283
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
4284
+ * passed as the `this` value to the `callback` function. If `thisArg` is
4285
+ * @returns a new Deque object with the mapped values.
4163
4286
  */
4164
- map(callback) {
4287
+ map(callback, thisArg) {
4165
4288
  const newDeque = new _Deque([], this._bucketSize);
4166
4289
  let index = 0;
4167
4290
  for (const el of this) {
4168
- newDeque.push(callback(el, index, this));
4291
+ newDeque.push(callback.call(thisArg, el, index, this));
4169
4292
  index++;
4170
4293
  }
4171
4294
  return newDeque;
4172
4295
  }
4173
4296
  /**
4174
4297
  * Time Complexity: O(n)
4175
- * Space Complexity: O(1)
4298
+ * Space Complexity: O(n)
4176
4299
  */
4300
+ print() {
4301
+ console.log([...this]);
4302
+ }
4177
4303
  /**
4178
4304
  * Time Complexity: O(n)
4179
4305
  * Space Complexity: O(1)
4180
4306
  *
4181
- * The `reduce` function iterates over the elements of a deque and applies a callback function to
4182
- * each element, accumulating a single value.
4183
- * @param callback - The `callback` parameter is a function that takes four arguments:
4184
- * @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
4185
- * is the value that will be passed as the first argument to the `callback` function when reducing
4186
- * the elements of the deque.
4187
- * @returns the final value of the accumulator after iterating over all elements in the deque and
4188
- * applying the callback function to each element.
4307
+ * The above function is an implementation of the iterator protocol in TypeScript, allowing the
4308
+ * object to be iterated over using a for...of loop.
4189
4309
  */
4190
- reduce(callback, initialValue) {
4191
- let accumulator = initialValue;
4192
- let index = 0;
4193
- for (const el of this) {
4194
- accumulator = callback(accumulator, el, index, this);
4195
- index++;
4310
+ *_getIterator() {
4311
+ for (let i = 0; i < this.size; ++i) {
4312
+ yield this.getAt(i);
4196
4313
  }
4197
- return accumulator;
4198
- }
4199
- print() {
4200
- console.log([...this]);
4201
4314
  }
4202
4315
  /**
4203
4316
  * Time Complexity: O(n)
@@ -4428,8 +4541,9 @@ var dataStructureTyped = (() => {
4428
4541
  };
4429
4542
 
4430
4543
  // src/data-structures/heap/heap.ts
4431
- var Heap = class _Heap {
4544
+ var Heap = class _Heap extends IterableElementBase {
4432
4545
  constructor(elements, options) {
4546
+ super();
4433
4547
  __publicField(this, "options");
4434
4548
  __publicField(this, "_elements", []);
4435
4549
  const defaultComparator = (a, b) => {
@@ -4724,47 +4838,70 @@ var dataStructureTyped = (() => {
4724
4838
  for (let i = Math.floor(this.size / 2); i >= 0; i--)
4725
4839
  this._sinkDown(i, this.elements.length >> 1);
4726
4840
  }
4727
- *[Symbol.iterator]() {
4728
- for (const element of this.elements) {
4729
- yield element;
4730
- }
4731
- }
4732
- forEach(callback) {
4733
- let index = 0;
4734
- for (const el of this) {
4735
- callback(el, index, this);
4736
- index++;
4737
- }
4738
- }
4739
- filter(predicate) {
4740
- const filteredHeap = new _Heap([], this.options);
4841
+ /**
4842
+ * Time Complexity: O(n)
4843
+ * Space Complexity: O(n)
4844
+ */
4845
+ /**
4846
+ * Time Complexity: O(n)
4847
+ * Space Complexity: O(n)
4848
+ *
4849
+ * The `filter` function creates a new Heap object containing elements that pass a given callback
4850
+ * function.
4851
+ * @param callback - The `callback` parameter is a function that will be called for each element in
4852
+ * the heap. It takes three arguments: the current element, the index of the current element, and the
4853
+ * heap itself. The callback function should return a boolean value indicating whether the current
4854
+ * element should be included in the filtered list
4855
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
4856
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
4857
+ * passed as the `this` value to the `callback` function. If `thisArg` is
4858
+ * @returns The `filter` method is returning a new `Heap` object that contains the elements that pass
4859
+ * the filter condition specified by the `callback` function.
4860
+ */
4861
+ filter(callback, thisArg) {
4862
+ const filteredList = new _Heap();
4741
4863
  let index = 0;
4742
- for (const el of this) {
4743
- if (predicate(el, index, this)) {
4744
- filteredHeap.push(el);
4864
+ for (const current of this) {
4865
+ if (callback.call(thisArg, current, index, this)) {
4866
+ filteredList.push(current);
4745
4867
  }
4746
4868
  index++;
4747
4869
  }
4748
- return filteredHeap;
4870
+ return filteredList;
4749
4871
  }
4750
- map(callback, comparator) {
4872
+ /**
4873
+ * Time Complexity: O(n)
4874
+ * Space Complexity: O(n)
4875
+ */
4876
+ /**
4877
+ * Time Complexity: O(n)
4878
+ * Space Complexity: O(n)
4879
+ *
4880
+ * The `map` function creates a new heap by applying a callback function to each element of the
4881
+ * original heap.
4882
+ * @param callback - The callback parameter is a function that will be called for each element in the
4883
+ * original heap. It takes three arguments: the current element, the index of the current element,
4884
+ * and the original heap itself. The callback function should return a value of type T, which will be
4885
+ * added to the mapped heap.
4886
+ * @param comparator - The `comparator` parameter is a function that is used to compare elements in
4887
+ * the heap. It takes two arguments, `a` and `b`, and returns a negative number if `a` is less than
4888
+ * `b`, a positive number if `a` is greater than `b`, or
4889
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
4890
+ * specify the value of `this` within the callback function. It is used when you want to bind a
4891
+ * specific object as the context for the callback function. If `thisArg` is not provided,
4892
+ * `undefined` is used as
4893
+ * @returns a new instance of the Heap class, which is created using the mapped elements from the
4894
+ * original Heap.
4895
+ */
4896
+ map(callback, comparator, thisArg) {
4751
4897
  const mappedHeap = new _Heap([], { comparator });
4752
4898
  let index = 0;
4753
4899
  for (const el of this) {
4754
- mappedHeap.add(callback(el, index, this));
4900
+ mappedHeap.add(callback.call(thisArg, el, index, this));
4755
4901
  index++;
4756
4902
  }
4757
4903
  return mappedHeap;
4758
4904
  }
4759
- reduce(callback, initialValue) {
4760
- let accumulator = initialValue;
4761
- let index = 0;
4762
- for (const el of this) {
4763
- accumulator = callback(accumulator, el, index, this);
4764
- index++;
4765
- }
4766
- return accumulator;
4767
- }
4768
4905
  /**
4769
4906
  * Time Complexity: O(log n)
4770
4907
  * Space Complexity: O(1)
@@ -4772,6 +4909,11 @@ var dataStructureTyped = (() => {
4772
4909
  print() {
4773
4910
  console.log([...this]);
4774
4911
  }
4912
+ *_getIterator() {
4913
+ for (const element of this.elements) {
4914
+ yield element;
4915
+ }
4916
+ }
4775
4917
  /**
4776
4918
  * Time Complexity: O(n)
4777
4919
  * Space Complexity: O(1)
@@ -5278,8 +5420,9 @@ var dataStructureTyped = (() => {
5278
5420
  * This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
5279
5421
  */
5280
5422
  };
5281
- var AbstractGraph = class {
5423
+ var AbstractGraph = class extends IterablePairBase {
5282
5424
  constructor() {
5425
+ super();
5283
5426
  __publicField(this, "_vertices", /* @__PURE__ */ new Map());
5284
5427
  }
5285
5428
  get vertices() {
@@ -6236,46 +6379,67 @@ var dataStructureTyped = (() => {
6236
6379
  getBridges() {
6237
6380
  return this.tarjan(false, true, false, false).bridges;
6238
6381
  }
6239
- *[Symbol.iterator]() {
6240
- for (const vertex of this._vertices.values()) {
6241
- yield [vertex.key, vertex.value];
6242
- }
6243
- }
6244
- forEach(callback) {
6245
- let index = 0;
6246
- for (const vertex of this) {
6247
- callback(vertex, index, this._vertices);
6248
- index++;
6249
- }
6250
- }
6251
- filter(predicate) {
6382
+ /**
6383
+ * Time Complexity: O(n)
6384
+ * Space Complexity: O(n)
6385
+ */
6386
+ /**
6387
+ * Time Complexity: O(n)
6388
+ * Space Complexity: O(n)
6389
+ *
6390
+ * The `filter` function iterates over key-value pairs in a data structure and returns an array of
6391
+ * pairs that satisfy a given predicate.
6392
+ * @param predicate - The `predicate` parameter is a callback function that takes four arguments:
6393
+ * `value`, `key`, `index`, and `this`. It is used to determine whether an element should be included
6394
+ * in the filtered array. The callback function should return `true` if the element should be
6395
+ * included, and `
6396
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
6397
+ * specify the value of `this` within the `predicate` function. It is used when you want to bind a
6398
+ * specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
6399
+ * @returns The `filter` method returns an array of key-value pairs `[VertexKey, V | undefined][]`
6400
+ * that satisfy the given predicate function.
6401
+ */
6402
+ filter(predicate, thisArg) {
6252
6403
  const filtered = [];
6253
6404
  let index = 0;
6254
- for (const entry of this) {
6255
- if (predicate(entry, index, this._vertices)) {
6256
- filtered.push(entry);
6405
+ for (const [key, value] of this) {
6406
+ if (predicate.call(thisArg, value, key, index, this)) {
6407
+ filtered.push([key, value]);
6257
6408
  }
6258
6409
  index++;
6259
6410
  }
6260
6411
  return filtered;
6261
6412
  }
6262
- map(callback) {
6413
+ /**
6414
+ * Time Complexity: O(n)
6415
+ * Space Complexity: O(n)
6416
+ */
6417
+ /**
6418
+ * Time Complexity: O(n)
6419
+ * Space Complexity: O(n)
6420
+ *
6421
+ * The `map` function iterates over the elements of a collection and applies a callback function to
6422
+ * each element, returning an array of the results.
6423
+ * @param callback - The callback parameter is a function that will be called for each element in the
6424
+ * map. It takes four arguments:
6425
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
6426
+ * specify the value of `this` within the callback function. If `thisArg` is provided, it will be
6427
+ * used as the `this` value when calling the callback function. If `thisArg` is not provided, `
6428
+ * @returns The `map` function is returning an array of type `T[]`.
6429
+ */
6430
+ map(callback, thisArg) {
6263
6431
  const mapped = [];
6264
6432
  let index = 0;
6265
- for (const entry of this) {
6266
- mapped.push(callback(entry, index, this._vertices));
6433
+ for (const [key, value] of this) {
6434
+ mapped.push(callback.call(thisArg, value, key, index, this));
6267
6435
  index++;
6268
6436
  }
6269
6437
  return mapped;
6270
6438
  }
6271
- reduce(callback, initialValue) {
6272
- let accumulator = initialValue;
6273
- let index = 0;
6274
- for (const entry of this) {
6275
- accumulator = callback(accumulator, entry, index, this._vertices);
6276
- index++;
6439
+ *_getIterator() {
6440
+ for (const vertex of this._vertices.values()) {
6441
+ yield [vertex.key, vertex.value];
6277
6442
  }
6278
- return accumulator;
6279
6443
  }
6280
6444
  _addVertexOnly(newVertex) {
6281
6445
  if (this.hasVertex(newVertex)) {
@@ -7206,6 +7370,11 @@ var dataStructureTyped = (() => {
7206
7370
  })(RBTNColor || {});
7207
7371
 
7208
7372
  // src/types/common.ts
7373
+ var BSTVariant = /* @__PURE__ */ ((BSTVariant2) => {
7374
+ BSTVariant2["MIN"] = "MIN";
7375
+ BSTVariant2["MAX"] = "MAX";
7376
+ return BSTVariant2;
7377
+ })(BSTVariant || {});
7209
7378
  var CP = /* @__PURE__ */ ((CP2) => {
7210
7379
  CP2["lt"] = "lt";
7211
7380
  CP2["eq"] = "eq";
@@ -7259,7 +7428,7 @@ var dataStructureTyped = (() => {
7259
7428
  return "MAL_NODE" /* MAL_NODE */;
7260
7429
  }
7261
7430
  };
7262
- var BinaryTree = class _BinaryTree {
7431
+ var BinaryTree = class _BinaryTree extends IterablePairBase {
7263
7432
  /**
7264
7433
  * The constructor function initializes a binary tree object with optional elements and options.
7265
7434
  * @param [elements] - An optional iterable of BTNodeExemplar objects. These objects represent the
@@ -7270,20 +7439,28 @@ var dataStructureTyped = (() => {
7270
7439
  * required.
7271
7440
  */
7272
7441
  constructor(elements, options) {
7442
+ super();
7273
7443
  __publicField(this, "iterationType", "ITERATIVE" /* ITERATIVE */);
7444
+ __publicField(this, "_extractor", (key) => Number(key));
7274
7445
  __publicField(this, "_root");
7275
7446
  __publicField(this, "_size");
7276
7447
  __publicField(this, "_defaultOneParamCallback", (node) => node.key);
7277
7448
  if (options) {
7278
- const { iterationType } = options;
7449
+ const { iterationType, extractor } = options;
7279
7450
  if (iterationType) {
7280
7451
  this.iterationType = iterationType;
7281
7452
  }
7453
+ if (extractor) {
7454
+ this._extractor = extractor;
7455
+ }
7282
7456
  }
7283
7457
  this._size = 0;
7284
7458
  if (elements)
7285
7459
  this.addMany(elements);
7286
7460
  }
7461
+ get extractor() {
7462
+ return this._extractor;
7463
+ }
7287
7464
  get root() {
7288
7465
  return this._root;
7289
7466
  }
@@ -7292,7 +7469,7 @@ var dataStructureTyped = (() => {
7292
7469
  }
7293
7470
  /**
7294
7471
  * Creates a new instance of BinaryTreeNode with the given key and value.
7295
- * @param {BTNKey} key - The key for the new node.
7472
+ * @param {K} key - The key for the new node.
7296
7473
  * @param {V} value - The value for the new node.
7297
7474
  * @returns {N} - The newly created BinaryTreeNode.
7298
7475
  */
@@ -7311,7 +7488,7 @@ var dataStructureTyped = (() => {
7311
7488
  }
7312
7489
  /**
7313
7490
  * The function "isNode" checks if an exemplar is an instance of the BinaryTreeNode class.
7314
- * @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
7491
+ * @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<K, V,N>`.
7315
7492
  * @returns a boolean value indicating whether the exemplar is an instance of the class N.
7316
7493
  */
7317
7494
  isNode(exemplar) {
@@ -7320,7 +7497,7 @@ var dataStructureTyped = (() => {
7320
7497
  /**
7321
7498
  * The function `exemplarToNode` converts an exemplar of a binary tree node into an actual node
7322
7499
  * object.
7323
- * @param exemplar - BTNodeExemplar<V, N> - A generic type representing the exemplar parameter of the
7500
+ * @param exemplar - BTNodeExemplar<K, V,N> - A generic type representing the exemplar parameter of the
7324
7501
  * function. It can be any type.
7325
7502
  * @returns a value of type `N` (which represents a node), or `null`, or `undefined`.
7326
7503
  */
@@ -7341,7 +7518,7 @@ var dataStructureTyped = (() => {
7341
7518
  }
7342
7519
  } else if (this.isNode(exemplar)) {
7343
7520
  node = exemplar;
7344
- } else if (this.isNodeKey(exemplar)) {
7521
+ } else if (this.isNotNodeInstance(exemplar)) {
7345
7522
  node = this.createNode(exemplar);
7346
7523
  } else {
7347
7524
  return;
@@ -7350,7 +7527,7 @@ var dataStructureTyped = (() => {
7350
7527
  }
7351
7528
  /**
7352
7529
  * The function checks if a given value is an entry in a binary tree node.
7353
- * @param kne - BTNodeExemplar<V, N> - A generic type representing a node in a binary tree. It has
7530
+ * @param kne - BTNodeExemplar<K, V,N> - A generic type representing a node in a binary tree. It has
7354
7531
  * two type parameters V and N, representing the value and node type respectively.
7355
7532
  * @returns a boolean value.
7356
7533
  */
@@ -7416,7 +7593,7 @@ var dataStructureTyped = (() => {
7416
7593
  * The function `addMany` takes in an iterable of `BTNodeExemplar` objects, adds each object to the
7417
7594
  * current instance, and returns an array of the inserted nodes.
7418
7595
  * @param nodes - The `nodes` parameter is an iterable (such as an array or a set) of
7419
- * `BTNodeExemplar<V, N>` objects.
7596
+ * `BTNodeExemplar<K, V,N>` objects.
7420
7597
  * @returns The function `addMany` returns an array of values, where each value is either of type
7421
7598
  * `N`, `null`, or `undefined`.
7422
7599
  */
@@ -7511,11 +7688,11 @@ var dataStructureTyped = (() => {
7511
7688
  * Space Complexity: O(1)
7512
7689
  *
7513
7690
  * The function calculates the depth of a given node in a binary tree.
7514
- * @param {BTNKey | N | null | undefined} distNode - The `distNode` parameter represents the node in
7515
- * the binary tree whose depth we want to find. It can be of type `BTNKey`, `N`, `null`, or
7691
+ * @param {K | N | null | undefined} distNode - The `distNode` parameter represents the node in
7692
+ * the binary tree whose depth we want to find. It can be of type `K`, `N`, `null`, or
7516
7693
  * `undefined`.
7517
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
7518
- * from which we want to calculate the depth. It can be either a `BTNKey` (binary tree node key) or
7694
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
7695
+ * from which we want to calculate the depth. It can be either a `K` (binary tree node key) or
7519
7696
  * `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
7520
7697
  * @returns the depth of the `distNode` relative to the `beginRoot`.
7521
7698
  */
@@ -7542,9 +7719,9 @@ var dataStructureTyped = (() => {
7542
7719
  *
7543
7720
  * The function `getHeight` calculates the maximum height of a binary tree using either recursive or
7544
7721
  * iterative traversal.
7545
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
7722
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
7546
7723
  * starting node of the binary tree from which we want to calculate the height. It can be of type
7547
- * `BTNKey`, `N`, `null`, or `undefined`. If not provided, it defaults to `this.root`.
7724
+ * `K`, `N`, `null`, or `undefined`. If not provided, it defaults to `this.root`.
7548
7725
  * @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
7549
7726
  * height of the tree using a recursive approach or an iterative approach. It can have two possible
7550
7727
  * values:
@@ -7588,9 +7765,9 @@ var dataStructureTyped = (() => {
7588
7765
  *
7589
7766
  * The `getMinHeight` function calculates the minimum height of a binary tree using either a
7590
7767
  * recursive or iterative approach.
7591
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
7768
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
7592
7769
  * starting node of the binary tree from which we want to calculate the minimum height. It can be of
7593
- * type `BTNKey`, `N`, `null`, or `undefined`. If no value is provided, it defaults to `this.root`.
7770
+ * type `K`, `N`, `null`, or `undefined`. If no value is provided, it defaults to `this.root`.
7594
7771
  * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
7595
7772
  * to calculate the minimum height of a binary tree. It can have two possible values:
7596
7773
  * @returns The function `getMinHeight` returns the minimum height of a binary tree.
@@ -7648,8 +7825,8 @@ var dataStructureTyped = (() => {
7648
7825
  *
7649
7826
  * The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
7650
7827
  * height of the tree.
7651
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
7652
- * for calculating the height and minimum height of a binary tree. It can be either a `BTNKey` (a key
7828
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
7829
+ * for calculating the height and minimum height of a binary tree. It can be either a `K` (a key
7653
7830
  * value of a binary tree node), `N` (a node of a binary tree), `null`, or `undefined`. If
7654
7831
  * @returns a boolean value.
7655
7832
  */
@@ -7674,7 +7851,7 @@ var dataStructureTyped = (() => {
7674
7851
  * matches the identifier. If set to true, the function will stop iterating once it finds a matching
7675
7852
  * node and return that node. If set to false (default), the function will continue iterating and
7676
7853
  * return all nodes that match the identifier.
7677
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
7854
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
7678
7855
  * starting node for the traversal. It can be either a key, a node object, or `null`/`undefined`. If
7679
7856
  * it is `null` or `undefined`, an empty array will be returned.
7680
7857
  * @param iterationType - The `iterationType` parameter determines the type of iteration used to
@@ -7730,8 +7907,8 @@ var dataStructureTyped = (() => {
7730
7907
  * the binary tree. It is used to filter the nodes based on certain conditions. The `callback`
7731
7908
  * function should return a boolean value indicating whether the node should be included in the
7732
7909
  * result or not.
7733
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
7734
- * for the search in the binary tree. It can be specified as a `BTNKey` (a unique identifier for a
7910
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
7911
+ * for the search in the binary tree. It can be specified as a `K` (a unique identifier for a
7735
7912
  * node in the binary tree), a node object (`N`), or `null`/`undefined` to start the search from
7736
7913
  * @param iterationType - The `iterationType` parameter is a variable that determines the type of
7737
7914
  * iteration to be performed on the binary tree. It is used to specify whether the iteration should
@@ -7756,7 +7933,7 @@ var dataStructureTyped = (() => {
7756
7933
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
7757
7934
  * the binary tree. It is used to determine if a node matches the given identifier. The `callback`
7758
7935
  * function should take a single parameter of type `N` (the type of the nodes in the binary tree) and
7759
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
7936
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
7760
7937
  * for searching the binary tree. It can be either a key value, a node object, or `null`/`undefined`.
7761
7938
  * If `null` or `undefined` is passed, the search will start from the root of the binary tree.
7762
7939
  * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
@@ -7780,7 +7957,7 @@ var dataStructureTyped = (() => {
7780
7957
  *
7781
7958
  * The function `getNodeByKey` searches for a node in a binary tree by its key, using either
7782
7959
  * recursive or iterative iteration.
7783
- * @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
7960
+ * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
7784
7961
  * It is used to find the node with the matching key value.
7785
7962
  * @param iterationType - The `iterationType` parameter is used to determine whether the search for
7786
7963
  * the node with the given key should be performed iteratively or recursively. It has two possible
@@ -7823,7 +8000,7 @@ var dataStructureTyped = (() => {
7823
8000
  /**
7824
8001
  * The function `ensureNode` returns the node corresponding to the given key if it is a valid node
7825
8002
  * key, otherwise it returns the key itself.
7826
- * @param {BTNKey | N | null | undefined} key - The `key` parameter can be of type `BTNKey`, `N`,
8003
+ * @param {K | N | null | undefined} key - The `key` parameter can be of type `K`, `N`,
7827
8004
  * `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
7828
8005
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
7829
8006
  * type of iteration to be used when searching for a node by key. It has a default value of
@@ -7832,7 +8009,7 @@ var dataStructureTyped = (() => {
7832
8009
  * itself if it is not a valid node key.
7833
8010
  */
7834
8011
  ensureNode(key, iterationType = "ITERATIVE" /* ITERATIVE */) {
7835
- return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
8012
+ return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
7836
8013
  }
7837
8014
  /**
7838
8015
  * Time Complexity: O(n)
@@ -7847,8 +8024,8 @@ var dataStructureTyped = (() => {
7847
8024
  * the binary tree. It is used to determine whether a node matches the given identifier. The callback
7848
8025
  * function should return a value that can be compared to the identifier to determine if it is a
7849
8026
  * match.
7850
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
7851
- * for the search in the binary tree. It can be specified as a `BTNKey` (a unique identifier for a
8027
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
8028
+ * for the search in the binary tree. It can be specified as a `K` (a unique identifier for a
7852
8029
  * node), a node object of type `N`, or `null`/`undefined` to start the search from the root of
7853
8030
  * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
7854
8031
  * be performed when searching for a node in the binary tree. It is an optional parameter with a
@@ -7886,8 +8063,8 @@ var dataStructureTyped = (() => {
7886
8063
  *
7887
8064
  * The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
7888
8065
  * structure, with the option to reverse the order of the nodes.
7889
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
7890
- * starting node from which you want to find the path to the root. It can be of type `BTNKey`, `N`,
8066
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
8067
+ * starting node from which you want to find the path to the root. It can be of type `K`, `N`,
7891
8068
  * `null`, or `undefined`.
7892
8069
  * @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
7893
8070
  * resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
@@ -7916,8 +8093,8 @@ var dataStructureTyped = (() => {
7916
8093
  *
7917
8094
  * The function `getLeftMost` returns the leftmost node in a binary tree, either recursively or
7918
8095
  * iteratively.
7919
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
7920
- * for finding the leftmost node in a binary tree. It can be either a `BTNKey` (a key value), `N` (a
8096
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
8097
+ * for finding the leftmost node in a binary tree. It can be either a `K` (a key value), `N` (a
7921
8098
  * node), `null`, or `undefined`. If not provided, it defaults to `this.root`,
7922
8099
  * @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
7923
8100
  * be performed when finding the leftmost node in a binary tree. It can have two possible values:
@@ -7954,8 +8131,8 @@ var dataStructureTyped = (() => {
7954
8131
  *
7955
8132
  * The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
7956
8133
  * iteratively.
7957
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
7958
- * starting node from which we want to find the rightmost node. It can be of type `BTNKey`, `N`,
8134
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
8135
+ * starting node from which we want to find the rightmost node. It can be of type `K`, `N`,
7959
8136
  * `null`, or `undefined`. If not provided, it defaults to `this.root`, which is a property of the
7960
8137
  * current object.
7961
8138
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
@@ -7992,7 +8169,7 @@ var dataStructureTyped = (() => {
7992
8169
  * Space Complexity: O(1)
7993
8170
  *
7994
8171
  * The function `isSubtreeBST` checks if a given binary tree is a valid binary search tree.
7995
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the root
8172
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the root
7996
8173
  * node of the binary search tree (BST) that you want to check if it is a subtree of another BST.
7997
8174
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
7998
8175
  * type of iteration to use when checking if a subtree is a binary search tree (BST). It can have two
@@ -8007,9 +8184,10 @@ var dataStructureTyped = (() => {
8007
8184
  const dfs = (cur, min, max) => {
8008
8185
  if (!cur)
8009
8186
  return true;
8010
- if (cur.key <= min || cur.key >= max)
8187
+ const numKey = this.extractor(cur.key);
8188
+ if (numKey <= min || numKey >= max)
8011
8189
  return false;
8012
- return dfs(cur.left, min, cur.key) && dfs(cur.right, cur.key, max);
8190
+ return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
8013
8191
  };
8014
8192
  return dfs(beginRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
8015
8193
  } else {
@@ -8021,9 +8199,10 @@ var dataStructureTyped = (() => {
8021
8199
  curr = curr.left;
8022
8200
  }
8023
8201
  curr = stack.pop();
8024
- if (!curr || prev >= curr.key)
8202
+ const numKey = this.extractor(curr.key);
8203
+ if (!curr || prev >= numKey)
8025
8204
  return false;
8026
- prev = curr.key;
8205
+ prev = numKey;
8027
8206
  curr = curr.right;
8028
8207
  }
8029
8208
  return true;
@@ -8058,8 +8237,8 @@ var dataStructureTyped = (() => {
8058
8237
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
8059
8238
  * the subtree traversal. It takes a single parameter, which is the current node being traversed, and
8060
8239
  * returns a value of any type.
8061
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
8062
- * starting node or key from which the subtree traversal should begin. It can be of type `BTNKey`,
8240
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
8241
+ * starting node or key from which the subtree traversal should begin. It can be of type `K`,
8063
8242
  * `N`, `null`, or `undefined`. If not provided, the `root` property of the current object is used as
8064
8243
  * the default value.
8065
8244
  * @param iterationType - The `iterationType` parameter determines the type of traversal to be
@@ -8138,13 +8317,13 @@ var dataStructureTyped = (() => {
8138
8317
  return this.isRealNode(node) || node === null;
8139
8318
  }
8140
8319
  /**
8141
- * The function "isNodeKey" checks if a potential key is a number.
8320
+ * The function "isNotNodeInstance" checks if a potential key is a number.
8142
8321
  * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
8143
8322
  * data type.
8144
8323
  * @returns a boolean value indicating whether the potentialKey is of type number or not.
8145
8324
  */
8146
- isNodeKey(potentialKey) {
8147
- return typeof potentialKey === "number";
8325
+ isNotNodeInstance(potentialKey) {
8326
+ return !(potentialKey instanceof BinaryTreeNode);
8148
8327
  }
8149
8328
  /**
8150
8329
  * Time complexity: O(n)
@@ -8158,7 +8337,7 @@ var dataStructureTyped = (() => {
8158
8337
  * `null`, or `undefined`, and returns a value of any type. The default value for this parameter is
8159
8338
  * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
8160
8339
  * nodes are traversed during the depth-first search. It can have one of the following values:
8161
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
8340
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
8162
8341
  * for the depth-first search traversal. It can be specified as a key, a node object, or
8163
8342
  * `null`/`undefined`. If not provided, the `beginRoot` will default to the root node of the tree.
8164
8343
  * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
@@ -8277,7 +8456,7 @@ var dataStructureTyped = (() => {
8277
8456
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
8278
8457
  * the breadth-first search traversal. It takes a single parameter, which is the current node being
8279
8458
  * visited, and returns a value of any type.
8280
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
8459
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
8281
8460
  * starting node for the breadth-first search traversal. It can be specified as a key, a node object,
8282
8461
  * or `null`/`undefined` to indicate the root of the tree. If not provided, the `root` property of
8283
8462
  * the class is used as
@@ -8348,9 +8527,9 @@ var dataStructureTyped = (() => {
8348
8527
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
8349
8528
  * the tree. It takes a single parameter, which can be of type `N`, `null`, or `undefined`, and
8350
8529
  * returns a value of any type.
8351
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
8530
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
8352
8531
  * starting node for traversing the tree. It can be either a node object (`N`), a key value
8353
- * (`BTNKey`), `null`, or `undefined`. If not provided, it defaults to the root node of the tree.
8532
+ * (`K`), `null`, or `undefined`. If not provided, it defaults to the root node of the tree.
8354
8533
  * @param iterationType - The `iterationType` parameter determines the type of iteration to be
8355
8534
  * performed on the tree. It can have two possible values:
8356
8535
  * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
@@ -8407,7 +8586,7 @@ var dataStructureTyped = (() => {
8407
8586
  }
8408
8587
  /**
8409
8588
  * The function `getPredecessor` returns the predecessor node of a given node in a binary tree.
8410
- * @param {BTNKey | N | null | undefined} node - The `node` parameter can be of type `BTNKey`, `N`,
8589
+ * @param {K | N | null | undefined} node - The `node` parameter can be of type `K`, `N`,
8411
8590
  * `null`, or `undefined`.
8412
8591
  * @returns The function `getPredecessor` returns a value of type `N | undefined`.
8413
8592
  */
@@ -8429,7 +8608,7 @@ var dataStructureTyped = (() => {
8429
8608
  }
8430
8609
  /**
8431
8610
  * The function `getSuccessor` returns the next node in a binary tree given a current node.
8432
- * @param {BTNKey | N | null} [x] - The parameter `x` can be of type `BTNKey`, `N`, or `null`.
8611
+ * @param {K | N | null} [x] - The parameter `x` can be of type `K`, `N`, or `null`.
8433
8612
  * @returns the successor of the given node or key. The successor is the node that comes immediately
8434
8613
  * after the given node in the inorder traversal of the binary tree.
8435
8614
  */
@@ -8458,7 +8637,7 @@ var dataStructureTyped = (() => {
8458
8637
  * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
8459
8638
  * determines the order in which the nodes of a binary tree are traversed. It can have one of the
8460
8639
  * following values:
8461
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
8640
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
8462
8641
  * for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
8463
8642
  * the root of the tree. If no value is provided, the default value is the root of the tree.
8464
8643
  * @returns The function `morris` returns an array of values that are the result of invoking the
@@ -8546,43 +8725,6 @@ var dataStructureTyped = (() => {
8546
8725
  }
8547
8726
  return ans;
8548
8727
  }
8549
- /**
8550
- * Time complexity: O(n)
8551
- * Space complexity: O(n)
8552
- */
8553
- /**
8554
- * Time complexity: O(n)
8555
- * Space complexity: O(n)
8556
- *
8557
- * The function "keys" returns an array of keys from a given object.
8558
- * @returns an array of BTNKey objects.
8559
- */
8560
- keys() {
8561
- const keys = [];
8562
- for (const entry of this) {
8563
- keys.push(entry[0]);
8564
- }
8565
- return keys;
8566
- }
8567
- /**
8568
- * Time complexity: O(n)
8569
- * Space complexity: O(n)
8570
- */
8571
- /**
8572
- * Time complexity: O(n)
8573
- * Space complexity: O(n)
8574
- *
8575
- * The function "values" returns an array of values from a map-like object.
8576
- * @returns The `values()` method is returning an array of values (`V`) from the entries in the
8577
- * object.
8578
- */
8579
- values() {
8580
- const values = [];
8581
- for (const entry of this) {
8582
- values.push(entry[1]);
8583
- }
8584
- return values;
8585
- }
8586
8728
  /**
8587
8729
  * Time complexity: O(n)
8588
8730
  * Space complexity: O(n)
@@ -8601,116 +8743,73 @@ var dataStructureTyped = (() => {
8601
8743
  return cloned;
8602
8744
  }
8603
8745
  /**
8604
- * Time complexity: O(n)
8605
- * Space complexity: O(1)
8606
- */
8607
- /**
8608
- * The `forEach` function iterates over each entry in a tree and calls a callback function with the
8609
- * entry and the tree as arguments.
8610
- * @param callback - The callback parameter is a function that will be called for each entry in the
8611
- * tree. It takes two parameters: entry and tree.
8746
+ * Time Complexity: O(n)
8747
+ * Space Complexity: O(n)
8612
8748
  */
8613
- forEach(callback) {
8614
- for (const entry of this) {
8615
- callback(entry, this);
8616
- }
8617
- }
8618
8749
  /**
8619
- * The `filter` function creates a new tree by iterating over the entries of the current tree and
8620
- * adding the entries that satisfy the given predicate.
8621
- * @param predicate - The `predicate` parameter is a function that takes two arguments: `entry` and
8622
- * `tree`.
8623
- * @returns The `filter` method is returning a new tree object that contains only the entries that
8624
- * satisfy the given predicate function.
8750
+ * Time Complexity: O(n)
8751
+ * Space Complexity: O(n)
8752
+ *
8753
+ * The `filter` function creates a new tree by iterating over the elements of the current tree and
8754
+ * adding only the elements that satisfy the given predicate function.
8755
+ * @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
8756
+ * `key`, and `index`. It should return a boolean value indicating whether the pair should be
8757
+ * included in the filtered tree or not.
8758
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
8759
+ * to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
8760
+ * it will be passed as the first argument to the `predicate` function. If `thisArg` is
8761
+ * @returns The `filter` method is returning a new tree object that contains the key-value pairs that
8762
+ * pass the given predicate function.
8625
8763
  */
8626
- filter(predicate) {
8764
+ filter(predicate, thisArg) {
8627
8765
  const newTree = this.createTree();
8766
+ let index = 0;
8628
8767
  for (const [key, value] of this) {
8629
- if (predicate([key, value], this)) {
8768
+ if (predicate.call(thisArg, value, key, index++, this)) {
8630
8769
  newTree.add([key, value]);
8631
8770
  }
8632
8771
  }
8633
8772
  return newTree;
8634
8773
  }
8635
8774
  /**
8636
- * The `map` function creates a new tree by applying a callback function to each entry in the current
8637
- * tree.
8638
- * @param callback - The callback parameter is a function that takes two arguments: entry and tree.
8775
+ * Time Complexity: O(n)
8776
+ * Space Complexity: O(n)
8777
+ */
8778
+ /**
8779
+ * Time Complexity: O(n)
8780
+ * Space Complexity: O(n)
8781
+ *
8782
+ * The `map` function creates a new tree by applying a callback function to each key-value pair in
8783
+ * the original tree.
8784
+ * @param callback - The callback parameter is a function that will be called for each key-value pair
8785
+ * in the tree. It takes four arguments: the value of the current pair, the key of the current pair,
8786
+ * the index of the current pair, and a reference to the tree itself. The callback function should
8787
+ * return a new
8788
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
8789
+ * specify the value of `this` within the callback function. If you pass a value for `thisArg`, it
8790
+ * will be used as the `this` value when the callback function is called. If you don't pass a value
8639
8791
  * @returns The `map` method is returning a new tree object.
8640
8792
  */
8641
- map(callback) {
8793
+ map(callback, thisArg) {
8642
8794
  const newTree = this.createTree();
8795
+ let index = 0;
8643
8796
  for (const [key, value] of this) {
8644
- newTree.add([key, callback([key, value], this)]);
8797
+ newTree.add([key, callback.call(thisArg, value, key, index++, this)]);
8645
8798
  }
8646
8799
  return newTree;
8647
8800
  }
8648
- // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
8649
- // map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
8650
- // const newTree = this.createTree();
8651
- // for (const [key, value] of this) {
8652
- // newTree.add(key, callback([key, value], this));
8653
- // }
8654
- // return newTree;
8655
- // }
8656
- /**
8657
- * The `reduce` function iterates over the entries of a tree and applies a callback function to each
8658
- * entry, accumulating a single value.
8659
- * @param callback - The callback parameter is a function that takes three arguments: accumulator,
8660
- * entry, and tree. It is called for each entry in the tree and is used to accumulate a single value
8661
- * based on the logic defined in the callback function.
8662
- * @param {T} initialValue - The initialValue parameter is the initial value of the accumulator. It
8663
- * is the value that will be passed as the first argument to the callback function when reducing the
8664
- * elements of the tree.
8665
- * @returns The `reduce` method is returning the final value of the accumulator after iterating over
8666
- * all the entries in the tree and applying the callback function to each entry.
8667
- */
8668
- reduce(callback, initialValue) {
8669
- let accumulator = initialValue;
8670
- for (const [key, value] of this) {
8671
- accumulator = callback(accumulator, [key, value], this);
8672
- }
8673
- return accumulator;
8674
- }
8675
- /**
8676
- * The above function is an iterator for a binary tree that can be used to traverse the tree in
8677
- * either an iterative or recursive manner.
8678
- * @param node - The `node` parameter represents the current node in the binary tree from which the
8679
- * iteration starts. It is an optional parameter with a default value of `this.root`, which means
8680
- * that if no node is provided, the iteration will start from the root of the binary tree.
8681
- * @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
8682
- * binary tree nodes in a specific order.
8683
- */
8684
- *[Symbol.iterator](node = this.root) {
8685
- if (!node)
8686
- return;
8687
- if (this.iterationType === "ITERATIVE" /* ITERATIVE */) {
8688
- const stack = [];
8689
- let current = node;
8690
- while (current || stack.length > 0) {
8691
- while (current && !isNaN(current.key)) {
8692
- stack.push(current);
8693
- current = current.left;
8694
- }
8695
- current = stack.pop();
8696
- if (current && !isNaN(current.key)) {
8697
- yield [current.key, current.value];
8698
- current = current.right;
8699
- }
8700
- }
8701
- } else {
8702
- if (node.left && !isNaN(node.key)) {
8703
- yield* __yieldStar(this[Symbol.iterator](node.left));
8704
- }
8705
- yield [node.key, node.value];
8706
- if (node.right && !isNaN(node.key)) {
8707
- yield* __yieldStar(this[Symbol.iterator](node.right));
8708
- }
8709
- }
8710
- }
8801
+ // // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
8802
+ // // map<NV>(callback: (entry: [K, V | undefined], tree: this) => NV) {
8803
+ // // const newTree = this.createTree();
8804
+ // // for (const [key, value] of this) {
8805
+ // // newTree.add(key, callback([key, value], this));
8806
+ // // }
8807
+ // // return newTree;
8808
+ // // }
8809
+ //
8711
8810
  /**
8712
8811
  * The `print` function is used to display a binary tree structure in a visually appealing way.
8713
- * @param {BTNKey | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `BTNKey | N | null |
8812
+ * @param {K | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `K | N | null |
8714
8813
  * undefined`. It represents the root node of a binary tree. The root node can have one of the
8715
8814
  * following types:
8716
8815
  * @param {BinaryTreePrintOptions} [options={ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false}] - Options object that controls printing behavior. You can specify whether to display undefined, null, or sentinel nodes.
@@ -8737,6 +8836,33 @@ var dataStructureTyped = (() => {
8737
8836
  };
8738
8837
  display(beginRoot);
8739
8838
  }
8839
+ *_getIterator(node = this.root) {
8840
+ if (!node)
8841
+ return;
8842
+ if (this.iterationType === "ITERATIVE" /* ITERATIVE */) {
8843
+ const stack = [];
8844
+ let current = node;
8845
+ while (current || stack.length > 0) {
8846
+ while (current && !isNaN(this.extractor(current.key))) {
8847
+ stack.push(current);
8848
+ current = current.left;
8849
+ }
8850
+ current = stack.pop();
8851
+ if (current && !isNaN(this.extractor(current.key))) {
8852
+ yield [current.key, current.value];
8853
+ current = current.right;
8854
+ }
8855
+ }
8856
+ } else {
8857
+ if (node.left && !isNaN(this.extractor(node.key))) {
8858
+ yield* __yieldStar(this[Symbol.iterator](node.left));
8859
+ }
8860
+ yield [node.key, node.value];
8861
+ if (node.right && !isNaN(this.extractor(node.key))) {
8862
+ yield* __yieldStar(this[Symbol.iterator](node.right));
8863
+ }
8864
+ }
8865
+ }
8740
8866
  _displayAux(node, options) {
8741
8867
  const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
8742
8868
  const emptyDisplayLayout = [["\u2500"], 1, 0, 0];
@@ -8744,10 +8870,10 @@ var dataStructureTyped = (() => {
8744
8870
  return emptyDisplayLayout;
8745
8871
  } else if (node === void 0 && !isShowUndefined) {
8746
8872
  return emptyDisplayLayout;
8747
- } else if (node !== null && node !== void 0 && isNaN(node.key) && !isShowRedBlackNIL) {
8873
+ } else if (node !== null && node !== void 0 && isNaN(this.extractor(node.key)) && !isShowRedBlackNIL) {
8748
8874
  return emptyDisplayLayout;
8749
8875
  } else if (node !== null && node !== void 0) {
8750
- const key = node.key, line = isNaN(key) ? "S" : key.toString(), width = line.length;
8876
+ const key = node.key, line = isNaN(this.extractor(key)) ? "S" : this.extractor(key).toString(), width = line.length;
8751
8877
  return _buildNodeDisplay(line, width, this._displayAux(node.left, options), this._displayAux(node.right, options));
8752
8878
  } else {
8753
8879
  const line = node === void 0 ? "U" : "N", width = line.length;
@@ -8825,7 +8951,7 @@ var dataStructureTyped = (() => {
8825
8951
  * If the parent node is null, the function also returns undefined.
8826
8952
  */
8827
8953
  _addTo(newNode, parent) {
8828
- if (this.isNodeKey(parent))
8954
+ if (this.isNotNodeInstance(parent))
8829
8955
  parent = this.getNode(parent);
8830
8956
  if (parent) {
8831
8957
  if (parent.left === void 0) {
@@ -8917,11 +9043,11 @@ var dataStructureTyped = (() => {
8917
9043
  constructor(elements, options) {
8918
9044
  super([], options);
8919
9045
  __publicField(this, "_root");
8920
- __publicField(this, "comparator", (a, b) => a - b);
9046
+ __publicField(this, "_variant", "MIN" /* MIN */);
8921
9047
  if (options) {
8922
- const { comparator } = options;
8923
- if (comparator) {
8924
- this.comparator = comparator;
9048
+ const { variant } = options;
9049
+ if (variant) {
9050
+ this._variant = variant;
8925
9051
  }
8926
9052
  }
8927
9053
  this._root = void 0;
@@ -8931,9 +9057,12 @@ var dataStructureTyped = (() => {
8931
9057
  get root() {
8932
9058
  return this._root;
8933
9059
  }
9060
+ get variant() {
9061
+ return this._variant;
9062
+ }
8934
9063
  /**
8935
9064
  * The function creates a new binary search tree node with the given key and value.
8936
- * @param {BTNKey} key - The key parameter is the key value that will be associated with
9065
+ * @param {K} key - The key parameter is the key value that will be associated with
8937
9066
  * the new node. It is used to determine the position of the node in the binary search tree.
8938
9067
  * @param [value] - The parameter `value` is an optional value that can be assigned to the node. It
8939
9068
  * represents the value associated with the node in a binary search tree.
@@ -8952,12 +9081,12 @@ var dataStructureTyped = (() => {
8952
9081
  createTree(options) {
8953
9082
  return new _BST([], __spreadValues({
8954
9083
  iterationType: this.iterationType,
8955
- comparator: this.comparator
9084
+ variant: this.variant
8956
9085
  }, options));
8957
9086
  }
8958
9087
  /**
8959
9088
  * The function checks if an exemplar is an instance of BSTNode.
8960
- * @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
9089
+ * @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<K, V, N>`.
8961
9090
  * @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
8962
9091
  */
8963
9092
  isNode(exemplar) {
@@ -8966,7 +9095,7 @@ var dataStructureTyped = (() => {
8966
9095
  /**
8967
9096
  * The function `exemplarToNode` takes an exemplar and returns a corresponding node if the exemplar
8968
9097
  * is valid, otherwise it returns undefined.
8969
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
9098
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
8970
9099
  * @returns a variable `node` which is of type `N` or `undefined`.
8971
9100
  */
8972
9101
  exemplarToNode(exemplar) {
@@ -8982,7 +9111,7 @@ var dataStructureTyped = (() => {
8982
9111
  } else {
8983
9112
  node = this.createNode(key, value);
8984
9113
  }
8985
- } else if (this.isNodeKey(exemplar)) {
9114
+ } else if (this.isNotNodeInstance(exemplar)) {
8986
9115
  node = this.createNode(exemplar);
8987
9116
  } else {
8988
9117
  return;
@@ -9079,17 +9208,17 @@ var dataStructureTyped = (() => {
9079
9208
  sorted = realBTNExemplars.sort((a, b) => {
9080
9209
  let aR, bR;
9081
9210
  if (this.isEntry(a))
9082
- aR = a[0];
9211
+ aR = this.extractor(a[0]);
9083
9212
  else if (this.isRealNode(a))
9084
- aR = a.key;
9213
+ aR = this.extractor(a.key);
9085
9214
  else
9086
- aR = a;
9215
+ aR = this.extractor(a);
9087
9216
  if (this.isEntry(b))
9088
- bR = b[0];
9217
+ bR = this.extractor(b[0]);
9089
9218
  else if (this.isRealNode(b))
9090
- bR = b.key;
9219
+ bR = this.extractor(b.key);
9091
9220
  else
9092
- bR = b;
9221
+ bR = this.extractor(b);
9093
9222
  return aR - bR;
9094
9223
  });
9095
9224
  const _dfs = (arr) => {
@@ -9125,34 +9254,31 @@ var dataStructureTyped = (() => {
9125
9254
  }
9126
9255
  return inserted;
9127
9256
  }
9128
- /**
9129
- * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
9130
- * Space Complexity: O(n) - Additional space is required for the sorted array.
9131
- */
9132
- /**
9133
- * Time Complexity: O(log n) - Average case for a balanced tree.
9134
- * Space Complexity: O(1) - Constant space is used.
9135
- *
9136
- * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
9137
- * leftmost node if the comparison result is greater than.
9138
- * @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
9139
- * type `BTNKey`, `N`, or `undefined`. It represents the starting point for finding the last key in
9140
- * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
9141
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
9142
- * be performed. It can have one of the following values:
9143
- * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
9144
- * the key of the leftmost node if the comparison result is greater than, and the key of the
9145
- * rightmost node otherwise. If no node is found, it returns 0.
9146
- */
9147
- lastKey(beginRoot = this.root, iterationType = this.iterationType) {
9148
- var _a, _b, _c, _d, _e, _f;
9149
- if (this._compare(0, 1) === "lt" /* lt */)
9150
- return (_b = (_a = this.getRightMost(beginRoot, iterationType)) == null ? void 0 : _a.key) != null ? _b : 0;
9151
- else if (this._compare(0, 1) === "gt" /* gt */)
9152
- return (_d = (_c = this.getLeftMost(beginRoot, iterationType)) == null ? void 0 : _c.key) != null ? _d : 0;
9153
- else
9154
- return (_f = (_e = this.getRightMost(beginRoot, iterationType)) == null ? void 0 : _e.key) != null ? _f : 0;
9155
- }
9257
+ // /**
9258
+ // * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
9259
+ // * Space Complexity: O(n) - Additional space is required for the sorted array.
9260
+ // */
9261
+ //
9262
+ // /**
9263
+ // * Time Complexity: O(log n) - Average case for a balanced tree.
9264
+ // * Space Complexity: O(1) - Constant space is used.
9265
+ // *
9266
+ // * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
9267
+ // * leftmost node if the comparison result is greater than.
9268
+ // * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
9269
+ // * type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
9270
+ // * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
9271
+ // * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
9272
+ // * be performed. It can have one of the following values:
9273
+ // * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
9274
+ // * the key of the leftmost node if the comparison result is greater than, and the key of the
9275
+ // * rightmost node otherwise. If no node is found, it returns 0.
9276
+ // */
9277
+ // lastKey(beginRoot: BSTNodeKeyOrNode<K,N> = this.root, iterationType = this.iterationType): K {
9278
+ // if (this._compare(0, 1) === CP.lt) return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
9279
+ // else if (this._compare(0, 1) === CP.gt) return this.getLeftMost(beginRoot, iterationType)?.key ?? 0;
9280
+ // else return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
9281
+ // }
9156
9282
  /**
9157
9283
  * Time Complexity: O(log n) - Average case for a balanced tree.
9158
9284
  * Space Complexity: O(1) - Constant space is used.
@@ -9163,7 +9289,7 @@ var dataStructureTyped = (() => {
9163
9289
  *
9164
9290
  * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
9165
9291
  * either recursive or iterative methods.
9166
- * @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
9292
+ * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
9167
9293
  * It is used to identify the node that we want to retrieve.
9168
9294
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
9169
9295
  * type of iteration to use when searching for a node in the binary tree. It can have two possible
@@ -9208,14 +9334,14 @@ var dataStructureTyped = (() => {
9208
9334
  /**
9209
9335
  * The function `ensureNode` returns the node corresponding to the given key if it is a node key,
9210
9336
  * otherwise it returns the key itself.
9211
- * @param {BTNKey | N | undefined} key - The `key` parameter can be of type `BTNKey`, `N`, or
9337
+ * @param {K | N | undefined} key - The `key` parameter can be of type `K`, `N`, or
9212
9338
  * `undefined`.
9213
9339
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
9214
9340
  * type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
9215
9341
  * @returns either a node object (N) or undefined.
9216
9342
  */
9217
9343
  ensureNode(key, iterationType = "ITERATIVE" /* ITERATIVE */) {
9218
- return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
9344
+ return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
9219
9345
  }
9220
9346
  /**
9221
9347
  * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
@@ -9233,7 +9359,7 @@ var dataStructureTyped = (() => {
9233
9359
  * first node that matches the identifier. If set to true, the function will return an array
9234
9360
  * containing only the first matching node. If set to false (default), the function will continue
9235
9361
  * searching for all nodes that match the identifier and return an array containing
9236
- * @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
9362
+ * @param {K | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
9237
9363
  * for the traversal. It can be either a key value or a node object. If it is undefined, the
9238
9364
  * traversal will start from the root of the tree.
9239
9365
  * @param iterationType - The `iterationType` parameter determines the type of iteration to be
@@ -9308,7 +9434,7 @@ var dataStructureTyped = (() => {
9308
9434
  * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
9309
9435
  * `CP`, which is a custom type representing the comparison operator. The possible values for
9310
9436
  * `lesserOrGreater` are
9311
- * @param {BTNKey | N | undefined} targetNode - The `targetNode` parameter represents the node in the
9437
+ * @param {K | N | undefined} targetNode - The `targetNode` parameter represents the node in the
9312
9438
  * binary tree that you want to traverse from. It can be specified either by its key, by the node
9313
9439
  * object itself, or it can be left undefined to start the traversal from the root of the tree.
9314
9440
  * @param iterationType - The `iterationType` parameter determines the type of traversal to be
@@ -9480,19 +9606,16 @@ var dataStructureTyped = (() => {
9480
9606
  /**
9481
9607
  * The function compares two values using a comparator function and returns whether the first value
9482
9608
  * is greater than, less than, or equal to the second value.
9483
- * @param {BTNKey} a - The parameter "a" is of type BTNKey.
9484
- * @param {BTNKey} b - The parameter "b" in the above code represents a BTNKey.
9609
+ * @param {K} a - The parameter "a" is of type K.
9610
+ * @param {K} b - The parameter "b" in the above code represents a K.
9485
9611
  * @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
9486
9612
  * than), CP.lt (less than), or CP.eq (equal).
9487
9613
  */
9488
9614
  _compare(a, b) {
9489
- const compared = this.comparator(a, b);
9490
- if (compared > 0)
9491
- return "gt" /* gt */;
9492
- else if (compared < 0)
9493
- return "lt" /* lt */;
9494
- else
9495
- return "eq" /* eq */;
9615
+ const extractedA = this.extractor(a);
9616
+ const extractedB = this.extractor(b);
9617
+ const compared = this.variant === "MIN" /* MIN */ ? extractedA - extractedB : extractedB - extractedA;
9618
+ return compared > 0 ? "gt" /* gt */ : compared < 0 ? "lt" /* lt */ : "eq" /* eq */;
9496
9619
  }
9497
9620
  };
9498
9621
 
@@ -9933,7 +10056,7 @@ var dataStructureTyped = (() => {
9933
10056
  var AVLTree = class _AVLTree extends BST {
9934
10057
  /**
9935
10058
  * The constructor function initializes an AVLTree object with optional elements and options.
9936
- * @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
10059
+ * @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<K, V, N>`
9937
10060
  * objects. It represents a collection of elements that will be added to the AVL tree during
9938
10061
  * initialization.
9939
10062
  * @param [options] - The `options` parameter is an optional object that allows you to customize the
@@ -9947,7 +10070,7 @@ var dataStructureTyped = (() => {
9947
10070
  }
9948
10071
  /**
9949
10072
  * The function creates a new AVL tree node with the specified key and value.
9950
- * @param {BTNKey} key - The key parameter is the key value that will be associated with
10073
+ * @param {K} key - The key parameter is the key value that will be associated with
9951
10074
  * the new node. It is used to determine the position of the node in the binary search tree.
9952
10075
  * @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
9953
10076
  * type `V`, which means it can be any value that is assignable to the `value` property of the
@@ -9967,12 +10090,12 @@ var dataStructureTyped = (() => {
9967
10090
  createTree(options) {
9968
10091
  return new _AVLTree([], __spreadValues({
9969
10092
  iterationType: this.iterationType,
9970
- comparator: this.comparator
10093
+ variant: this.variant
9971
10094
  }, options));
9972
10095
  }
9973
10096
  /**
9974
10097
  * The function checks if an exemplar is an instance of AVLTreeNode.
9975
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
10098
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
9976
10099
  * @returns a boolean value indicating whether the exemplar is an instance of the AVLTreeNode class.
9977
10100
  */
9978
10101
  isNode(exemplar) {
@@ -10033,9 +10156,9 @@ var dataStructureTyped = (() => {
10033
10156
  /**
10034
10157
  * The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
10035
10158
  * tree.
10036
- * @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node that
10037
- * needs to be swapped with the destination node. It can be of type `BTNKey`, `N`, or `undefined`.
10038
- * @param {BTNKey | N | undefined} destNode - The `destNode` parameter represents the destination
10159
+ * @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node that
10160
+ * needs to be swapped with the destination node. It can be of type `K`, `N`, or `undefined`.
10161
+ * @param {K | N | undefined} destNode - The `destNode` parameter represents the destination
10039
10162
  * node where the values from the source node will be swapped to.
10040
10163
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
10041
10164
  * if either `srcNode` or `destNode` is undefined.
@@ -10347,7 +10470,7 @@ var dataStructureTyped = (() => {
10347
10470
  /**
10348
10471
  * This is the constructor function for a Red-Black Tree data structure in TypeScript, which
10349
10472
  * initializes the tree with optional elements and options.
10350
- * @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
10473
+ * @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<K, V, N>`
10351
10474
  * objects. It represents the initial elements that will be added to the RBTree during its
10352
10475
  * construction. If this parameter is provided, the `addMany` method is called to add all the
10353
10476
  * elements to the
@@ -10372,7 +10495,7 @@ var dataStructureTyped = (() => {
10372
10495
  }
10373
10496
  /**
10374
10497
  * The function creates a new Red-Black Tree node with the specified key, value, and color.
10375
- * @param {BTNKey} key - The key parameter is the key value associated with the node. It is used to
10498
+ * @param {K} key - The key parameter is the key value associated with the node. It is used to
10376
10499
  * identify and compare nodes in the Red-Black Tree.
10377
10500
  * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
10378
10501
  * associated with the node. It is of type `V`, which is a generic type that can be replaced with any
@@ -10395,12 +10518,12 @@ var dataStructureTyped = (() => {
10395
10518
  createTree(options) {
10396
10519
  return new _RedBlackTree([], __spreadValues({
10397
10520
  iterationType: this.iterationType,
10398
- comparator: this.comparator
10521
+ variant: this.variant
10399
10522
  }, options));
10400
10523
  }
10401
10524
  /**
10402
10525
  * The function checks if an exemplar is an instance of the RedBlackTreeNode class.
10403
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
10526
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
10404
10527
  * @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
10405
10528
  * class.
10406
10529
  */
@@ -10410,7 +10533,7 @@ var dataStructureTyped = (() => {
10410
10533
  /**
10411
10534
  * The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
10412
10535
  * otherwise it returns undefined.
10413
- * @param exemplar - BTNodeExemplar<V, N> - A generic type representing an exemplar of a binary tree
10536
+ * @param exemplar - BTNodeExemplar<K, V, N> - A generic type representing an exemplar of a binary tree
10414
10537
  * node. It can be either a node itself, an entry (key-value pair), a node key, or any other value
10415
10538
  * that is not a valid exemplar.
10416
10539
  * @returns a variable `node` which is of type `N | undefined`.
@@ -10428,7 +10551,7 @@ var dataStructureTyped = (() => {
10428
10551
  } else {
10429
10552
  node = this.createNode(key, value, 1 /* RED */);
10430
10553
  }
10431
- } else if (this.isNodeKey(exemplar)) {
10554
+ } else if (this.isNotNodeInstance(exemplar)) {
10432
10555
  node = this.createNode(exemplar, void 0, 1 /* RED */);
10433
10556
  } else {
10434
10557
  return;
@@ -10584,7 +10707,7 @@ var dataStructureTyped = (() => {
10584
10707
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
10585
10708
  * the binary tree. It is used to determine if a node matches the given identifier. The `callback`
10586
10709
  * function should take a single parameter of type `N` (the type of the nodes in the binary tree) and
10587
- * @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter is the starting point for
10710
+ * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is the starting point for
10588
10711
  * searching for a node in a binary tree. It can be either a key value or a node object. If it is not
10589
10712
  * provided, the search will start from the root of the binary tree.
10590
10713
  * @param iterationType - The `iterationType` parameter is a variable that determines the type of
@@ -10891,7 +11014,7 @@ var dataStructureTyped = (() => {
10891
11014
  var TreeMultimapNode = class extends AVLTreeNode {
10892
11015
  /**
10893
11016
  * The constructor function initializes a BinaryTreeNode object with a key, value, and count.
10894
- * @param {BTNKey} key - The `key` parameter is of type `BTNKey` and represents the unique identifier
11017
+ * @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
10895
11018
  * of the binary tree node.
10896
11019
  * @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the value of the binary
10897
11020
  * tree node. If no value is provided, it will be `undefined`.
@@ -10920,7 +11043,7 @@ var dataStructureTyped = (() => {
10920
11043
  }
10921
11044
  /**
10922
11045
  * The function creates a new BSTNode with the given key, value, and count.
10923
- * @param {BTNKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
11046
+ * @param {K} key - The key parameter is the unique identifier for the binary tree node. It is used to
10924
11047
  * distinguish one node from another in the tree.
10925
11048
  * @param {N} value - The `value` parameter represents the value that will be stored in the binary search tree node.
10926
11049
  * @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
@@ -10933,12 +11056,12 @@ var dataStructureTyped = (() => {
10933
11056
  createTree(options) {
10934
11057
  return new _TreeMultimap([], __spreadValues({
10935
11058
  iterationType: this.iterationType,
10936
- comparator: this.comparator
11059
+ variant: this.variant
10937
11060
  }, options));
10938
11061
  }
10939
11062
  /**
10940
11063
  * The function checks if an exemplar is an instance of the TreeMultimapNode class.
10941
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
11064
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
10942
11065
  * @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
10943
11066
  * class.
10944
11067
  */
@@ -10947,7 +11070,7 @@ var dataStructureTyped = (() => {
10947
11070
  }
10948
11071
  /**
10949
11072
  * The function `exemplarToNode` converts an exemplar object into a node object.
10950
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`, where `V` represents
11073
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where `V` represents
10951
11074
  * the value type and `N` represents the node type.
10952
11075
  * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
10953
11076
  * times the node should be created. If not provided, it defaults to 1.
@@ -10966,7 +11089,7 @@ var dataStructureTyped = (() => {
10966
11089
  } else {
10967
11090
  node = this.createNode(key, value, count);
10968
11091
  }
10969
- } else if (this.isNodeKey(exemplar)) {
11092
+ } else if (this.isNotNodeInstance(exemplar)) {
10970
11093
  node = this.createNode(exemplar, void 0, count);
10971
11094
  } else {
10972
11095
  return;
@@ -11178,9 +11301,9 @@ var dataStructureTyped = (() => {
11178
11301
  * @param {N | undefined} newNode - The `newNode` parameter represents the node that needs to be
11179
11302
  * added to the binary tree. It can be of type `N` (which represents a node in the binary tree) or
11180
11303
  * `undefined` if there is no node to add.
11181
- * @param {BTNKey | N | undefined} parent - The `parent` parameter represents the parent node to
11304
+ * @param {K | N | undefined} parent - The `parent` parameter represents the parent node to
11182
11305
  * which the new node will be added as a child. It can be either a node object (`N`) or a key value
11183
- * (`BTNKey`).
11306
+ * (`K`).
11184
11307
  * @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
11185
11308
  * added, or `undefined` if no node was added.
11186
11309
  */
@@ -11210,9 +11333,9 @@ var dataStructureTyped = (() => {
11210
11333
  }
11211
11334
  /**
11212
11335
  * The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
11213
- * @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node from
11214
- * which the values will be swapped. It can be of type `BTNKey`, `N`, or `undefined`.
11215
- * @param {BTNKey | N | undefined} destNode - The `destNode` parameter represents the destination
11336
+ * @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node from
11337
+ * which the values will be swapped. It can be of type `K`, `N`, or `undefined`.
11338
+ * @param {K | N | undefined} destNode - The `destNode` parameter represents the destination
11216
11339
  * node where the values from the source node will be swapped to.
11217
11340
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
11218
11341
  * if either `srcNode` or `destNode` is undefined.
@@ -11890,8 +12013,9 @@ var dataStructureTyped = (() => {
11890
12013
  this.children = /* @__PURE__ */ new Map();
11891
12014
  }
11892
12015
  };
11893
- var Trie = class _Trie {
12016
+ var Trie = class _Trie extends IterableElementBase {
11894
12017
  constructor(words, caseSensitive = true) {
12018
+ super();
11895
12019
  __publicField(this, "_size");
11896
12020
  __publicField(this, "_caseSensitive");
11897
12021
  __publicField(this, "_root");
@@ -12186,56 +12310,75 @@ var dataStructureTyped = (() => {
12186
12310
  dfs(startNode, prefix);
12187
12311
  return words;
12188
12312
  }
12189
- *[Symbol.iterator]() {
12190
- function* _dfs(node, path) {
12191
- if (node.isEnd) {
12192
- yield path;
12193
- }
12194
- for (const [char, childNode] of node.children) {
12195
- yield* __yieldStar(_dfs(childNode, path + char));
12196
- }
12197
- }
12198
- yield* __yieldStar(_dfs(this.root, ""));
12199
- }
12200
- forEach(callback) {
12201
- let index = 0;
12202
- for (const word of this) {
12203
- callback(word, index, this);
12204
- index++;
12205
- }
12206
- }
12207
- filter(predicate) {
12313
+ /**
12314
+ * Time Complexity: O(n)
12315
+ * Space Complexity: O(n)
12316
+ */
12317
+ /**
12318
+ * Time Complexity: O(n)
12319
+ * Space Complexity: O(n)
12320
+ *
12321
+ * The `filter` function takes a predicate function and returns a new array containing all the
12322
+ * elements for which the predicate function returns true.
12323
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
12324
+ * `word`, `index`, and `this`. It should return a boolean value indicating whether the current
12325
+ * element should be included in the filtered results or not.
12326
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
12327
+ * specify the value of `this` within the `predicate` function. It is used when you want to bind a
12328
+ * specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
12329
+ * @returns The `filter` method is returning an array of strings (`string[]`).
12330
+ */
12331
+ filter(predicate, thisArg) {
12208
12332
  const results = [];
12209
12333
  let index = 0;
12210
12334
  for (const word of this) {
12211
- if (predicate(word, index, this)) {
12335
+ if (predicate.call(thisArg, word, index, this)) {
12212
12336
  results.push(word);
12213
12337
  }
12214
12338
  index++;
12215
12339
  }
12216
12340
  return results;
12217
12341
  }
12218
- map(callback) {
12342
+ /**
12343
+ * Time Complexity: O(n)
12344
+ * Space Complexity: O(n)
12345
+ */
12346
+ /**
12347
+ * Time Complexity: O(n)
12348
+ * Space Complexity: O(n)
12349
+ *
12350
+ * The `map` function creates a new Trie by applying a callback function to each element in the Trie.
12351
+ * @param callback - The callback parameter is a function that will be called for each element in the
12352
+ * Trie. It takes three arguments: the current element in the Trie, the index of the current element,
12353
+ * and the Trie itself. The callback function should return a new value for the element.
12354
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
12355
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
12356
+ * passed as the `this` value to the `callback` function. If `thisArg` is
12357
+ * @returns The `map` function is returning a new Trie object.
12358
+ */
12359
+ map(callback, thisArg) {
12219
12360
  const newTrie = new _Trie();
12220
12361
  let index = 0;
12221
12362
  for (const word of this) {
12222
- newTrie.add(callback(word, index, this));
12363
+ newTrie.add(callback.call(thisArg, word, index, this));
12223
12364
  index++;
12224
12365
  }
12225
12366
  return newTrie;
12226
12367
  }
12227
- reduce(callback, initialValue) {
12228
- let accumulator = initialValue;
12229
- let index = 0;
12230
- for (const word of this) {
12231
- accumulator = callback(accumulator, word, index, this);
12232
- index++;
12233
- }
12234
- return accumulator;
12235
- }
12236
12368
  print() {
12237
12369
  console.log([...this]);
12238
12370
  }
12371
+ *_getIterator() {
12372
+ function* _dfs(node, path) {
12373
+ if (node.isEnd) {
12374
+ yield path;
12375
+ }
12376
+ for (const [char, childNode] of node.children) {
12377
+ yield* __yieldStar(_dfs(childNode, path + char));
12378
+ }
12379
+ }
12380
+ yield* __yieldStar(_dfs(this.root, ""));
12381
+ }
12239
12382
  /**
12240
12383
  * Time Complexity: O(M), where M is the length of the input string.
12241
12384
  * Space Complexity: O(1) - Constant space.