data-structure-typed 1.53.5 → 1.53.7

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 (119) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +16 -25
  3. package/benchmark/report.html +32 -5
  4. package/benchmark/report.json +326 -23
  5. package/dist/cjs/common/index.d.ts +12 -0
  6. package/dist/cjs/common/index.js +24 -0
  7. package/dist/cjs/common/index.js.map +1 -0
  8. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +7 -10
  9. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  10. package/dist/cjs/data-structures/binary-tree/avl-tree.js +2 -2
  11. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  12. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +54 -19
  13. package/dist/cjs/data-structures/binary-tree/binary-tree.js +100 -66
  14. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  15. package/dist/cjs/data-structures/binary-tree/bst.d.ts +100 -36
  16. package/dist/cjs/data-structures/binary-tree/bst.js +185 -66
  17. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +4 -0
  19. package/dist/cjs/data-structures/binary-tree/rb-tree.js +6 -2
  20. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  21. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +2 -2
  22. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  23. package/dist/cjs/data-structures/heap/heap.d.ts +6 -6
  24. package/dist/cjs/data-structures/heap/heap.js +6 -6
  25. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +31 -19
  26. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +49 -34
  27. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  28. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +144 -62
  29. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +201 -97
  30. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  31. package/dist/cjs/data-structures/trie/trie.d.ts +110 -4
  32. package/dist/cjs/data-structures/trie/trie.js +122 -12
  33. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  34. package/dist/cjs/index.d.ts +1 -1
  35. package/dist/cjs/index.js +1 -1
  36. package/dist/cjs/index.js.map +1 -1
  37. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  38. package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +3 -2
  39. package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
  40. package/dist/cjs/types/utils/utils.d.ts +10 -6
  41. package/dist/cjs/utils/utils.js +4 -2
  42. package/dist/cjs/utils/utils.js.map +1 -1
  43. package/dist/mjs/common/index.d.ts +12 -0
  44. package/dist/mjs/common/index.js +24 -0
  45. package/dist/mjs/common/index.js.map +1 -0
  46. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +8 -10
  47. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  48. package/dist/mjs/data-structures/binary-tree/avl-tree.js +3 -2
  49. package/dist/mjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  50. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +54 -19
  51. package/dist/mjs/data-structures/binary-tree/binary-tree.js +95 -61
  52. package/dist/mjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  53. package/dist/mjs/data-structures/binary-tree/bst.d.ts +100 -36
  54. package/dist/mjs/data-structures/binary-tree/bst.js +187 -66
  55. package/dist/mjs/data-structures/binary-tree/bst.js.map +1 -1
  56. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +4 -0
  57. package/dist/mjs/data-structures/binary-tree/rb-tree.js +6 -2
  58. package/dist/mjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  59. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +2 -2
  60. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  61. package/dist/mjs/data-structures/heap/heap.d.ts +6 -6
  62. package/dist/mjs/data-structures/heap/heap.js +6 -6
  63. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +31 -19
  64. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +49 -34
  65. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  66. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +144 -62
  67. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +201 -97
  68. package/dist/mjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  69. package/dist/mjs/data-structures/trie/trie.d.ts +110 -4
  70. package/dist/mjs/data-structures/trie/trie.js +122 -12
  71. package/dist/mjs/data-structures/trie/trie.js.map +1 -1
  72. package/dist/mjs/index.d.ts +1 -1
  73. package/dist/mjs/index.js +1 -1
  74. package/dist/mjs/index.js.map +1 -1
  75. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  76. package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +3 -2
  77. package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
  78. package/dist/mjs/types/utils/utils.d.ts +10 -6
  79. package/dist/mjs/utils/utils.js +4 -2
  80. package/dist/mjs/utils/utils.js.map +1 -1
  81. package/dist/umd/data-structure-typed.js +512 -266
  82. package/dist/umd/data-structure-typed.min.js +2 -2
  83. package/dist/umd/data-structure-typed.min.js.map +1 -1
  84. package/package.json +8 -8
  85. package/src/common/index.ts +19 -0
  86. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -9
  87. package/src/data-structures/binary-tree/avl-tree.ts +3 -2
  88. package/src/data-structures/binary-tree/binary-tree.ts +108 -64
  89. package/src/data-structures/binary-tree/bst.ts +190 -69
  90. package/src/data-structures/binary-tree/rb-tree.ts +6 -2
  91. package/src/data-structures/binary-tree/tree-multi-map.ts +3 -3
  92. package/src/data-structures/heap/heap.ts +39 -39
  93. package/src/data-structures/linked-list/doubly-linked-list.ts +139 -121
  94. package/src/data-structures/linked-list/singly-linked-list.ts +219 -98
  95. package/src/data-structures/trie/trie.ts +116 -11
  96. package/src/index.ts +1 -1
  97. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  98. package/src/types/data-structures/binary-tree/bst.ts +3 -2
  99. package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
  100. package/src/types/utils/utils.ts +16 -10
  101. package/src/utils/utils.ts +4 -2
  102. package/test/performance/data-structures/binary-tree/avl-tree.test.ts +3 -0
  103. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +4 -1
  104. package/test/performance/reportor.ts +38 -33
  105. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +2 -2
  106. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +12 -12
  107. package/test/unit/data-structures/binary-tree/bst.test.ts +79 -3
  108. package/test/unit/data-structures/binary-tree/overall.test.ts +14 -22
  109. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +100 -1
  110. package/test/unit/data-structures/trie/trie.test.ts +151 -0
  111. package/test/unit/utils/utils.test.ts +6 -6
  112. package/dist/cjs/constants/index.d.ts +0 -4
  113. package/dist/cjs/constants/index.js +0 -9
  114. package/dist/cjs/constants/index.js.map +0 -1
  115. package/dist/mjs/constants/index.d.ts +0 -4
  116. package/dist/mjs/constants/index.js +0 -6
  117. package/dist/mjs/constants/index.js.map +0 -1
  118. package/src/constants/index.ts +0 -4
  119. package/testToExample.ts +0 -215
@@ -131,6 +131,7 @@ var dataStructureTyped = (() => {
131
131
  Navigator: () => Navigator,
132
132
  PriorityQueue: () => PriorityQueue,
133
133
  Queue: () => Queue,
134
+ Range: () => Range,
134
135
  RedBlackTree: () => RedBlackTree,
135
136
  RedBlackTreeNode: () => RedBlackTreeNode,
136
137
  SegmentTree: () => SegmentTree,
@@ -670,7 +671,7 @@ var dataStructureTyped = (() => {
670
671
  };
671
672
  function isPrimitiveComparable(value) {
672
673
  const valueType = typeof value;
673
- if (valueType === "number") return !Number.isNaN(value);
674
+ if (valueType === "number") return true;
674
675
  return valueType === "bigint" || valueType === "string" || valueType === "boolean";
675
676
  }
676
677
  function tryObjectToPrimitive(obj) {
@@ -691,7 +692,7 @@ var dataStructureTyped = (() => {
691
692
  if (value === null || value === void 0) return false;
692
693
  if (isPrimitiveComparable(value)) return true;
693
694
  if (typeof value !== "object") return false;
694
- if (value instanceof Date) return !Number.isNaN(value.getTime());
695
+ if (value instanceof Date) return true;
695
696
  if (isForceObjectComparable) return true;
696
697
  const comparableValue = tryObjectToPrimitive(value);
697
698
  if (comparableValue === null || comparableValue === void 0) return false;
@@ -1562,33 +1563,17 @@ var dataStructureTyped = (() => {
1562
1563
  get size() {
1563
1564
  return this._size;
1564
1565
  }
1565
- /**
1566
- * Time Complexity: O(n)
1567
- * Space Complexity: O(n)
1568
- *
1569
- * The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
1570
- * array.
1571
- * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
1572
- * @returns The `fromArray` function returns a `SinglyLinkedList` object.
1573
- */
1574
- static fromArray(data) {
1575
- const singlyLinkedList = new _SinglyLinkedList();
1576
- for (const item of data) {
1577
- singlyLinkedList.push(item);
1578
- }
1579
- return singlyLinkedList;
1580
- }
1581
1566
  /**
1582
1567
  * Time Complexity: O(1)
1583
1568
  * Space Complexity: O(1)
1584
1569
  *
1585
- * The push function adds a new element to the end of a singly linked list.
1586
- * @param {E} element - The "element" parameter represents the value of the element that you want to
1587
- * add to the linked list.
1588
- * @returns The `push` method is returning a boolean value, `true`.
1570
+ * The `push` function adds a new element or node to the end of a singly linked list.
1571
+ * @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the `push`
1572
+ * method can accept either an element of type `E` or a `SinglyLinkedListNode<E>` object.
1573
+ * @returns The `push` method is returning a boolean value, specifically `true`.
1589
1574
  */
1590
- push(element) {
1591
- const newNode = new SinglyLinkedListNode(element);
1575
+ push(elementOrNode) {
1576
+ const newNode = this._ensureNode(elementOrNode);
1592
1577
  if (!this.head) {
1593
1578
  this._head = newNode;
1594
1579
  this._tail = newNode;
@@ -1644,13 +1629,15 @@ var dataStructureTyped = (() => {
1644
1629
  * Time Complexity: O(1)
1645
1630
  * Space Complexity: O(1)
1646
1631
  *
1647
- * The unshift function adds a new element to the beginning of a singly linked list.
1648
- * @param {E} element - The "element" parameter represents the value of the element that you want to
1649
- * add to the beginning of the singly linked list.
1650
- * @returns The `unshift` method is returning a boolean value, `true`.
1632
+ * The unshift function adds a new element or node to the beginning of a singly linked list in
1633
+ * TypeScript.
1634
+ * @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the
1635
+ * `unshift` method can be either an element of type `E` or a `SinglyLinkedListNode` containing an
1636
+ * element of type `E`.
1637
+ * @returns The `unshift` method is returning a boolean value, specifically `true`.
1651
1638
  */
1652
- unshift(element) {
1653
- const newNode = new SinglyLinkedListNode(element);
1639
+ unshift(elementOrNode) {
1640
+ const newNode = this._ensureNode(elementOrNode);
1654
1641
  if (!this.head) {
1655
1642
  this._head = newNode;
1656
1643
  this._tail = newNode;
@@ -1661,6 +1648,27 @@ var dataStructureTyped = (() => {
1661
1648
  this._size++;
1662
1649
  return true;
1663
1650
  }
1651
+ /**
1652
+ * Time Complexity: O(n)
1653
+ * Space Complexity: O(1)
1654
+ *
1655
+ * This function searches for a specific element in a singly linked list based on a given node or
1656
+ * predicate.
1657
+ * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
1658
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `get` method can be one of
1659
+ * the following types:
1660
+ * @returns The `get` method returns the value of the first node in the singly linked list that
1661
+ * satisfies the provided predicate function. If no such node is found, it returns `undefined`.
1662
+ */
1663
+ search(elementNodeOrPredicate) {
1664
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
1665
+ let current = this.head;
1666
+ while (current) {
1667
+ if (predicate(current)) return current.value;
1668
+ current = current.next;
1669
+ }
1670
+ return void 0;
1671
+ }
1664
1672
  /**
1665
1673
  * Time Complexity: O(n)
1666
1674
  * Space Complexity: O(1)
@@ -1679,6 +1687,22 @@ var dataStructureTyped = (() => {
1679
1687
  }
1680
1688
  return current.value;
1681
1689
  }
1690
+ /**
1691
+ * Time Complexity: O(1)
1692
+ * Space Complexity: O(1)
1693
+ *
1694
+ * The function `isNode` in TypeScript checks if the input is an instance of `SinglyLinkedListNode`.
1695
+ * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
1696
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `isNode` function can be
1697
+ * one of the following types:
1698
+ * @returns The `isNode` function is checking if the `elementNodeOrPredicate` parameter is an
1699
+ * instance of `SinglyLinkedListNode<E>`. If it is, the function returns `true`, indicating that the
1700
+ * parameter is a `SinglyLinkedListNode<E>`. If it is not an instance of `SinglyLinkedListNode<E>`,
1701
+ * the function returns `false`.
1702
+ */
1703
+ isNode(elementNodeOrPredicate) {
1704
+ return elementNodeOrPredicate instanceof SinglyLinkedListNode;
1705
+ }
1682
1706
  /**
1683
1707
  * Time Complexity: O(n)
1684
1708
  * Space Complexity: O(1)
@@ -1727,18 +1751,18 @@ var dataStructureTyped = (() => {
1727
1751
  * Space Complexity: O(1)
1728
1752
  *
1729
1753
  * The delete function removes a node with a specific value from a singly linked list.
1730
- * @param {E | SinglyLinkedListNode<E>} valueOrNode - The `valueOrNode` parameter can accept either a value of type `E`
1754
+ * @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter can accept either a value of type `E`
1731
1755
  * or a `SinglyLinkedListNode<E>` object.
1732
1756
  * @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
1733
1757
  * successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
1734
1758
  */
1735
- delete(valueOrNode) {
1736
- if (valueOrNode === void 0) return false;
1759
+ delete(elementOrNode) {
1760
+ if (elementOrNode === void 0) return false;
1737
1761
  let value;
1738
- if (valueOrNode instanceof SinglyLinkedListNode) {
1739
- value = valueOrNode.value;
1762
+ if (elementOrNode instanceof SinglyLinkedListNode) {
1763
+ value = elementOrNode.value;
1740
1764
  } else {
1741
- value = valueOrNode;
1765
+ value = elementOrNode;
1742
1766
  }
1743
1767
  let current = this.head, prev = void 0;
1744
1768
  while (current) {
@@ -1766,25 +1790,28 @@ var dataStructureTyped = (() => {
1766
1790
  * Time Complexity: O(n)
1767
1791
  * Space Complexity: O(1)
1768
1792
  *
1769
- * The `addAt` function inserts a value at a specified index in a singly linked list.
1770
- * @param {number} index - The index parameter represents the position at which the new value should be inserted in the
1771
- * linked list. It is of type number.
1772
- * @param {E} value - The `value` parameter represents the value that you want to insert into the linked list at the
1773
- * specified index.
1774
- * @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
1775
- * if the index is out of bounds.
1793
+ * The `addAt` function inserts a new element or node at a specified index in a singly linked list.
1794
+ * @param {number} index - The `index` parameter represents the position at which you want to add a
1795
+ * new element or node in the linked list. It is a number that indicates the index where the new
1796
+ * element or node should be inserted.
1797
+ * @param {E | SinglyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
1798
+ * `addAt` method can be either a value of type `E` or a `SinglyLinkedListNode<E>` object. This
1799
+ * parameter represents the element or node that you want to add to the linked list at the specified
1800
+ * index.
1801
+ * @returns The `addAt` method returns a boolean value - `true` if the element or node was
1802
+ * successfully added at the specified index, and `false` if the index is out of bounds.
1776
1803
  */
1777
- addAt(index, value) {
1804
+ addAt(index, newElementOrNode) {
1778
1805
  if (index < 0 || index > this._size) return false;
1779
1806
  if (index === 0) {
1780
- this.unshift(value);
1807
+ this.unshift(newElementOrNode);
1781
1808
  return true;
1782
1809
  }
1783
1810
  if (index === this._size) {
1784
- this.push(value);
1811
+ this.push(newElementOrNode);
1785
1812
  return true;
1786
1813
  }
1787
- const newNode = new SinglyLinkedListNode(value);
1814
+ const newNode = this._ensureNode(newElementOrNode);
1788
1815
  const prevNode = this.getNodeAt(index - 1);
1789
1816
  newNode.next = prevNode.next;
1790
1817
  prevNode.next = newNode;
@@ -1848,16 +1875,21 @@ var dataStructureTyped = (() => {
1848
1875
  * Time Complexity: O(n)
1849
1876
  * Space Complexity: O(1)
1850
1877
  *
1851
- * The `indexOf` function returns the index of the first occurrence of a given value in a linked list.
1852
- * @param {E} value - The value parameter is the value that you want to find the index of in the linked list.
1853
- * @returns The method is returning the index of the first occurrence of the specified value in the linked list. If the
1854
- * value is not found, it returns -1.
1878
+ * The `indexOf` function in TypeScript searches for a specific element or node in a singly linked
1879
+ * list and returns its index if found.
1880
+ * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
1881
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `indexOf` method can be one
1882
+ * of the following types:
1883
+ * @returns The `indexOf` method returns the index of the first occurrence of the element that
1884
+ * matches the provided predicate in the singly linked list. If no matching element is found, it
1885
+ * returns -1.
1855
1886
  */
1856
- indexOf(value) {
1887
+ indexOf(elementNodeOrPredicate) {
1888
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
1857
1889
  let index = 0;
1858
1890
  let current = this.head;
1859
1891
  while (current) {
1860
- if (current.value === value) {
1892
+ if (predicate(current)) {
1861
1893
  return index;
1862
1894
  }
1863
1895
  index++;
@@ -1869,16 +1901,21 @@ var dataStructureTyped = (() => {
1869
1901
  * Time Complexity: O(n)
1870
1902
  * Space Complexity: O(1)
1871
1903
  *
1872
- * The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns
1873
- * undefined.
1874
- * @param {E} value - The value parameter is the value that we want to search for in the linked list.
1875
- * @returns a `SinglyLinkedListNode<E>` if a node with the specified value is found in the linked list. If no node with
1876
- * the specified value is found, the function returns `undefined`.
1904
+ * The function `getNode` in TypeScript searches for a node in a singly linked list based on a given
1905
+ * element, node, or predicate.
1906
+ * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean) | undefined} elementNodeOrPredicate
1907
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `getNode` method can be one
1908
+ * of the following types:
1909
+ * @returns The `getNode` method returns either a `SinglyLinkedListNode<E>` if a matching node is
1910
+ * found based on the provided predicate, or it returns `undefined` if no matching node is found or
1911
+ * if the input parameter is `undefined`.
1877
1912
  */
1878
- getNode(value) {
1913
+ getNode(elementNodeOrPredicate) {
1914
+ if (elementNodeOrPredicate === void 0) return;
1915
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
1879
1916
  let current = this.head;
1880
1917
  while (current) {
1881
- if (current.value === value) {
1918
+ if (predicate(current)) {
1882
1919
  return current;
1883
1920
  }
1884
1921
  current = current.next;
@@ -1889,29 +1926,34 @@ var dataStructureTyped = (() => {
1889
1926
  * Time Complexity: O(n)
1890
1927
  * Space Complexity: O(1)
1891
1928
  *
1892
- * The `addBefore` function inserts a new value before an existing value in a singly linked list.
1893
- * @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node that you want to insert the
1894
- * new value before. It can be either the value itself or a node containing the value in the linked list.
1895
- * @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the linked list.
1896
- * @returns The method `addBefore` returns a boolean value. It returns `true` if the new value was successfully
1897
- * inserted before the existing value, and `false` otherwise.
1929
+ * The function `addBefore` in TypeScript adds a new element or node before an existing element or
1930
+ * node in a singly linked list.
1931
+ * @param {E | SinglyLinkedListNode<E>} existingElementOrNode - existingElementOrNode represents the
1932
+ * element or node in the linked list before which you want to add a new element or node.
1933
+ * @param {E | SinglyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
1934
+ * `addBefore` method represents the element or node that you want to insert before the existing
1935
+ * element or node in the linked list. This new element can be of type `E` or a
1936
+ * `SinglyLinkedListNode<E>`.
1937
+ * @returns The `addBefore` method returns a boolean value - `true` if the new element or node was
1938
+ * successfully added before the existing element or node, and `false` if the operation was
1939
+ * unsuccessful.
1898
1940
  */
1899
- addBefore(existingValueOrNode, newValue) {
1941
+ addBefore(existingElementOrNode, newElementOrNode) {
1900
1942
  if (!this.head) return false;
1901
1943
  let existingValue;
1902
- if (existingValueOrNode instanceof SinglyLinkedListNode) {
1903
- existingValue = existingValueOrNode.value;
1944
+ if (this.isNode(existingElementOrNode)) {
1945
+ existingValue = existingElementOrNode.value;
1904
1946
  } else {
1905
- existingValue = existingValueOrNode;
1947
+ existingValue = existingElementOrNode;
1906
1948
  }
1907
1949
  if (this.head.value === existingValue) {
1908
- this.unshift(newValue);
1950
+ this.unshift(newElementOrNode);
1909
1951
  return true;
1910
1952
  }
1911
1953
  let current = this.head;
1912
1954
  while (current.next) {
1913
1955
  if (current.next.value === existingValue) {
1914
- const newNode = new SinglyLinkedListNode(newValue);
1956
+ const newNode = this._ensureNode(newElementOrNode);
1915
1957
  newNode.next = current.next;
1916
1958
  current.next = newNode;
1917
1959
  this._size++;
@@ -1925,22 +1967,22 @@ var dataStructureTyped = (() => {
1925
1967
  * Time Complexity: O(n)
1926
1968
  * Space Complexity: O(1)
1927
1969
  *
1928
- * The `addAfter` function inserts a new node with a given value after an existing node in a singly linked list.
1929
- * @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node in the linked list after which
1930
- * the new value will be inserted. It can be either the value of the existing node or the existing node itself.
1931
- * @param {E} newValue - The value that you want to insert into the linked list after the existing value or node.
1932
- * @returns The method returns a boolean value. It returns true if the new value was successfully inserted after the
1933
- * existing value or node, and false if the existing value or node was not found in the linked list.
1970
+ * The `addAfter` function in TypeScript adds a new element or node after an existing element or node
1971
+ * in a singly linked list.
1972
+ * @param {E | SinglyLinkedListNode<E>} existingElementOrNode - existingElementOrNode can be either
1973
+ * an element of type E or a SinglyLinkedListNode of type E.
1974
+ * @param {E | SinglyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
1975
+ * `addAfter` method represents the element or node that you want to add after the existing element
1976
+ * or node in a singly linked list. This parameter can be either the value of the new element or a
1977
+ * reference to a `SinglyLinkedListNode` containing
1978
+ * @returns The `addAfter` method returns a boolean value - `true` if the new element or node was
1979
+ * successfully added after the existing element or node, and `false` if the existing element or node
1980
+ * was not found.
1934
1981
  */
1935
- addAfter(existingValueOrNode, newValue) {
1936
- let existingNode;
1937
- if (existingValueOrNode instanceof SinglyLinkedListNode) {
1938
- existingNode = existingValueOrNode;
1939
- } else {
1940
- existingNode = this.getNode(existingValueOrNode);
1941
- }
1982
+ addAfter(existingElementOrNode, newElementOrNode) {
1983
+ const existingNode = this.getNode(existingElementOrNode);
1942
1984
  if (existingNode) {
1943
- const newNode = new SinglyLinkedListNode(newValue);
1985
+ const newNode = this._ensureNode(newElementOrNode);
1944
1986
  newNode.next = existingNode.next;
1945
1987
  existingNode.next = newNode;
1946
1988
  if (existingNode === this.tail) {
@@ -1955,15 +1997,19 @@ var dataStructureTyped = (() => {
1955
1997
  * Time Complexity: O(n)
1956
1998
  * Space Complexity: O(1)
1957
1999
  *
1958
- * The function counts the number of occurrences of a given value in a linked list.
1959
- * @param {E} value - The value parameter is the value that you want to count the occurrences of in the linked list.
1960
- * @returns The count of occurrences of the given value in the linked list.
2000
+ * The function `countOccurrences` iterates through a singly linked list and counts the occurrences
2001
+ * of a specified element or nodes that satisfy a given predicate.
2002
+ * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementOrNode
2003
+ * - The `elementOrNode` parameter in the `countOccurrences` method can accept three types of values:
2004
+ * @returns The `countOccurrences` method returns the number of occurrences of the specified element,
2005
+ * node, or predicate function in the singly linked list.
1961
2006
  */
1962
- countOccurrences(value) {
2007
+ countOccurrences(elementOrNode) {
2008
+ const predicate = this._ensurePredicate(elementOrNode);
1963
2009
  let count = 0;
1964
2010
  let current = this.head;
1965
2011
  while (current) {
1966
- if (current.value === value) {
2012
+ if (predicate(current)) {
1967
2013
  count++;
1968
2014
  }
1969
2015
  current = current.next;
@@ -2049,6 +2095,61 @@ var dataStructureTyped = (() => {
2049
2095
  current = current.next;
2050
2096
  }
2051
2097
  }
2098
+ /**
2099
+ * Time Complexity: O(n)
2100
+ * Space Complexity: O(n)
2101
+ *
2102
+ * The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
2103
+ * array.
2104
+ * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
2105
+ * @returns The `fromArray` function returns a `SinglyLinkedList` object.
2106
+ */
2107
+ static fromArray(data) {
2108
+ const singlyLinkedList = new _SinglyLinkedList();
2109
+ for (const item of data) {
2110
+ singlyLinkedList.push(item);
2111
+ }
2112
+ return singlyLinkedList;
2113
+ }
2114
+ /**
2115
+ * The _isPredicate function in TypeScript checks if the input is a function that takes a
2116
+ * SinglyLinkedListNode as an argument and returns a boolean.
2117
+ * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
2118
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following types:
2119
+ * @returns The _isPredicate method is returning a boolean value based on whether the
2120
+ * elementNodeOrPredicate parameter is a function or not. If the elementNodeOrPredicate is a
2121
+ * function, the method will return true, indicating that it is a predicate function. If it is not a
2122
+ * function, the method will return false.
2123
+ */
2124
+ _isPredicate(elementNodeOrPredicate) {
2125
+ return typeof elementNodeOrPredicate === "function";
2126
+ }
2127
+ /**
2128
+ * The function `_ensureNode` ensures that the input is a valid node and returns it, creating a new
2129
+ * node if necessary.
2130
+ * @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter can be either
2131
+ * an element of type `E` or a `SinglyLinkedListNode` containing an element of type `E`.
2132
+ * @returns A SinglyLinkedListNode<E> object is being returned.
2133
+ */
2134
+ _ensureNode(elementOrNode) {
2135
+ if (this.isNode(elementOrNode)) return elementOrNode;
2136
+ return new SinglyLinkedListNode(elementOrNode);
2137
+ }
2138
+ /**
2139
+ * The function `_ensurePredicate` in TypeScript ensures that the input is either a node, a predicate
2140
+ * function, or a value to compare with the node's value.
2141
+ * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
2142
+ * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following types:
2143
+ * @returns A function is being returned. If the input `elementNodeOrPredicate` is already a node, a
2144
+ * function is returned that checks if a given node is equal to the input node. If the input is a
2145
+ * predicate function, it is returned as is. If the input is neither a node nor a predicate function,
2146
+ * a function is returned that checks if a given node's value is equal to the input
2147
+ */
2148
+ _ensurePredicate(elementNodeOrPredicate) {
2149
+ if (this.isNode(elementNodeOrPredicate)) return (node) => node === elementNodeOrPredicate;
2150
+ if (this._isPredicate(elementNodeOrPredicate)) return elementNodeOrPredicate;
2151
+ return (node) => node.value === elementNodeOrPredicate;
2152
+ }
2052
2153
  };
2053
2154
 
2054
2155
  // src/data-structures/linked-list/doubly-linked-list.ts
@@ -2116,6 +2217,16 @@ var dataStructureTyped = (() => {
2116
2217
  }
2117
2218
  };
2118
2219
  var DoublyLinkedList = class _DoublyLinkedList extends IterableElementBase {
2220
+ /**
2221
+ * This TypeScript constructor initializes a DoublyLinkedList with optional elements and options.
2222
+ * @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the constructor is an
2223
+ * iterable collection of elements of type `E` or `R`. It is used to initialize the DoublyLinkedList
2224
+ * with the elements provided in the iterable. If no elements are provided, the default value is an
2225
+ * empty iterable.
2226
+ * @param [options] - The `options` parameter in the constructor is of type
2227
+ * `DoublyLinkedListOptions<E, R>`. It is an optional parameter that allows you to pass additional
2228
+ * configuration options to customize the behavior of the DoublyLinkedList.
2229
+ */
2119
2230
  constructor(elements = [], options) {
2120
2231
  super(options);
2121
2232
  __publicField(this, "_head");
@@ -2395,12 +2506,7 @@ var dataStructureTyped = (() => {
2395
2506
  * node was not found.
2396
2507
  */
2397
2508
  addBefore(existingElementOrNode, newElementOrNode) {
2398
- let existingNode;
2399
- if (existingElementOrNode instanceof DoublyLinkedListNode) {
2400
- existingNode = existingElementOrNode;
2401
- } else {
2402
- existingNode = this.getNode(existingElementOrNode);
2403
- }
2509
+ const existingNode = this.isNode(existingElementOrNode) ? existingElementOrNode : this.getNode(existingElementOrNode);
2404
2510
  if (existingNode) {
2405
2511
  const newNode = this._ensureNode(newElementOrNode);
2406
2512
  newNode.prev = existingNode.prev;
@@ -2434,12 +2540,7 @@ var dataStructureTyped = (() => {
2434
2540
  * was not found in the linked list.
2435
2541
  */
2436
2542
  addAfter(existingElementOrNode, newElementOrNode) {
2437
- let existingNode;
2438
- if (existingElementOrNode instanceof DoublyLinkedListNode) {
2439
- existingNode = existingElementOrNode;
2440
- } else {
2441
- existingNode = this.getNode(existingElementOrNode);
2442
- }
2543
+ const existingNode = this.isNode(existingElementOrNode) ? existingElementOrNode : this.getNode(existingElementOrNode);
2443
2544
  if (existingNode) {
2444
2545
  const newNode = this._ensureNode(newElementOrNode);
2445
2546
  newNode.next = existingNode.next;
@@ -2540,17 +2641,15 @@ var dataStructureTyped = (() => {
2540
2641
  * Time Complexity: O(n)
2541
2642
  * Space Complexity: O(1)
2542
2643
  *
2543
- * The indexOf function in TypeScript returns the index of a specified element or node in a Doubly
2544
- * Linked List.
2545
- * @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the
2546
- * `indexOf` method can be either an element of type `E` or a `DoublyLinkedListNode` containing an
2547
- * element of type `E`.
2548
- * @returns The `indexOf` method is returning the index of the element or node in the doubly linked
2549
- * list. If the element or node is found in the list, the method returns the index of that element or
2550
- * node. If the element or node is not found in the list, the method returns -1.
2644
+ * This function finds the index of a specified element, node, or predicate in a doubly linked list.
2645
+ * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
2646
+ * elementNodeOrPredicate - The `indexOf` method takes in a parameter `elementNodeOrPredicate`, which
2647
+ * can be one of the following:
2648
+ * @returns The `indexOf` method returns the index of the element in the doubly linked list that
2649
+ * matches the provided element, node, or predicate. If no match is found, it returns -1.
2551
2650
  */
2552
- indexOf(elementOrNode) {
2553
- const predicate = this._ensurePredicate(elementOrNode);
2651
+ indexOf(elementNodeOrPredicate) {
2652
+ const predicate = this._ensurePredicate(elementNodeOrPredicate);
2554
2653
  let index = 0;
2555
2654
  let current = this.head;
2556
2655
  while (current) {
@@ -2566,8 +2665,6 @@ var dataStructureTyped = (() => {
2566
2665
  * Time Complexity: O(n)
2567
2666
  * Space Complexity: O(1)
2568
2667
  *
2569
- */
2570
- /**
2571
2668
  * This function retrieves an element from a doubly linked list based on a given element
2572
2669
  * node or predicate.
2573
2670
  * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
@@ -2576,7 +2673,7 @@ var dataStructureTyped = (() => {
2576
2673
  * @returns The `get` method returns the value of the first node in the doubly linked list that
2577
2674
  * satisfies the provided predicate function. If no such node is found, it returns `undefined`.
2578
2675
  */
2579
- get(elementNodeOrPredicate) {
2676
+ search(elementNodeOrPredicate) {
2580
2677
  const predicate = this._ensurePredicate(elementNodeOrPredicate);
2581
2678
  let current = this.head;
2582
2679
  while (current) {
@@ -2724,6 +2821,23 @@ var dataStructureTyped = (() => {
2724
2821
  }
2725
2822
  return mappedList;
2726
2823
  }
2824
+ /**
2825
+ * Time Complexity: O(n)
2826
+ * Space Complexity: O(1)
2827
+ *
2828
+ */
2829
+ countOccurrences(elementOrNode) {
2830
+ const predicate = this._ensurePredicate(elementOrNode);
2831
+ let count = 0;
2832
+ let current = this.head;
2833
+ while (current) {
2834
+ if (predicate(current)) {
2835
+ count++;
2836
+ }
2837
+ current = current.next;
2838
+ }
2839
+ return count;
2840
+ }
2727
2841
  /**
2728
2842
  * Time Complexity: O(n)
2729
2843
  * Space Complexity: O(n)
@@ -6994,12 +7108,26 @@ var dataStructureTyped = (() => {
6994
7108
  }
6995
7109
  };
6996
7110
 
6997
- // src/constants/index.ts
7111
+ // src/common/index.ts
6998
7112
  var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
6999
7113
  DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
7000
7114
  DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
7001
7115
  return DFSOperation2;
7002
7116
  })(DFSOperation || {});
7117
+ var Range = class {
7118
+ constructor(low, high, includeLow = true, includeHigh = true) {
7119
+ this.low = low;
7120
+ this.high = high;
7121
+ this.includeLow = includeLow;
7122
+ this.includeHigh = includeHigh;
7123
+ }
7124
+ // Determine whether a key is within the range
7125
+ isInRange(key, comparator) {
7126
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
7127
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
7128
+ return lowCheck && highCheck;
7129
+ }
7130
+ };
7003
7131
 
7004
7132
  // src/data-structures/binary-tree/binary-tree.ts
7005
7133
  var BinaryTreeNode = class {
@@ -7143,15 +7271,12 @@ var dataStructureTyped = (() => {
7143
7271
  const finalValue = value != null ? value : entryValue;
7144
7272
  return [this.createNode(key, finalValue), finalValue];
7145
7273
  }
7146
- if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value), value];
7147
7274
  if (this.isRaw(keyNodeEntryOrRaw)) {
7148
- if (this._toEntryFn) {
7149
- const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
7150
- const finalValue = value != null ? value : entryValue;
7151
- if (this.isKey(key)) return [this.createNode(key, finalValue), finalValue];
7152
- }
7153
- return [void 0, void 0];
7275
+ const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
7276
+ const finalValue = value != null ? value : entryValue;
7277
+ if (this.isKey(key)) return [this.createNode(key, finalValue), finalValue];
7154
7278
  }
7279
+ if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value), value];
7155
7280
  return [void 0, void 0];
7156
7281
  }
7157
7282
  /**
@@ -7178,13 +7303,13 @@ var dataStructureTyped = (() => {
7178
7303
  const key = keyNodeEntryOrRaw[0];
7179
7304
  if (key === null) return null;
7180
7305
  if (key === void 0) return;
7181
- return this.getNodeByKey(key, iterationType);
7306
+ return this.getNode(key, this._root, iterationType);
7182
7307
  }
7183
7308
  if (this._toEntryFn) {
7184
7309
  const [key] = this._toEntryFn(keyNodeEntryOrRaw);
7185
- if (this.isKey(key)) return this.getNodeByKey(key);
7310
+ if (this.isKey(key)) return this.getNode(key);
7186
7311
  }
7187
- if (this.isKey(keyNodeEntryOrRaw)) return this.getNodeByKey(keyNodeEntryOrRaw, iterationType);
7312
+ if (this.isKey(keyNodeEntryOrRaw)) return this.getNode(keyNodeEntryOrRaw, this._root, iterationType);
7188
7313
  return;
7189
7314
  }
7190
7315
  /**
@@ -7201,8 +7326,15 @@ var dataStructureTyped = (() => {
7201
7326
  isNode(keyNodeEntryOrRaw) {
7202
7327
  return keyNodeEntryOrRaw instanceof BinaryTreeNode;
7203
7328
  }
7329
+ /**
7330
+ * The function `isRaw` checks if the input parameter is of type `R` by verifying if it is an object.
7331
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - BTNRep<K, V, NODE> | R
7332
+ * @returns The function `isRaw` is checking if the `keyNodeEntryOrRaw` parameter is of type `R` by
7333
+ * checking if it is an object. If the parameter is an object, the function will return `true`,
7334
+ * indicating that it is of type `R`.
7335
+ */
7204
7336
  isRaw(keyNodeEntryOrRaw) {
7205
- return typeof keyNodeEntryOrRaw === "object";
7337
+ return this._toEntryFn !== void 0 && typeof keyNodeEntryOrRaw === "object";
7206
7338
  }
7207
7339
  /**
7208
7340
  * The function `isRealNode` checks if a given input is a valid node in a binary tree.
@@ -7240,6 +7372,9 @@ var dataStructureTyped = (() => {
7240
7372
  isNIL(keyNodeEntryOrRaw) {
7241
7373
  return keyNodeEntryOrRaw === this._NIL;
7242
7374
  }
7375
+ isRange(keyNodeEntryRawOrPredicate) {
7376
+ return keyNodeEntryRawOrPredicate instanceof Range;
7377
+ }
7243
7378
  /**
7244
7379
  * The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
7245
7380
  * tree.
@@ -7378,6 +7513,17 @@ var dataStructureTyped = (() => {
7378
7513
  }
7379
7514
  return inserted;
7380
7515
  }
7516
+ /**
7517
+ * Time Complexity: O(k * n)
7518
+ * Space Complexity: O(1)
7519
+ *
7520
+ * The `merge` function in TypeScript merges another binary tree into the current tree by adding all
7521
+ * elements from the other tree.
7522
+ * @param anotherTree - `BinaryTree<K, V, R, NODE, TREE>`
7523
+ */
7524
+ merge(anotherTree) {
7525
+ this.addMany(anotherTree, []);
7526
+ }
7381
7527
  /**
7382
7528
  * Time Complexity: O(k * n)
7383
7529
  * Space Complexity: O(1)
@@ -7452,34 +7598,37 @@ var dataStructureTyped = (() => {
7452
7598
  * Time Complexity: O(n)
7453
7599
  * Space Complexity: O(k + log n)
7454
7600
  *
7455
- * The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
7456
- * or predicate, with options for recursive or iterative traversal.
7457
- * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
7458
- * - The `getNodes` function you provided takes several parameters:
7459
- * @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
7460
- * determines whether to return only the first node that matches the criteria specified by the
7461
- * `keyNodeEntryRawOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
7462
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
7463
- * `getNodes` function is used to specify the starting point for traversing the binary tree. It
7464
- * represents the root node of the binary tree or the node from which the traversal should begin. If
7465
- * not provided, the default value is set to `this._root
7466
- * @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` function
7467
- * determines the type of iteration to be performed when traversing the nodes of a binary tree. It
7468
- * can have two possible values:
7469
- * @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
7470
- * based on the input parameters and the iteration type specified.
7471
- */
7472
- getNodes(keyNodeEntryRawOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
7601
+ * The `search` function in TypeScript performs a depth-first or breadth-first search on a tree
7602
+ * structure based on a given predicate or key, with options to return multiple results or just one.
7603
+ * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The
7604
+ * `keyNodeEntryRawOrPredicate` parameter in the `search` function can accept three types of values:
7605
+ * @param [onlyOne=false] - The `onlyOne` parameter in the `search` function is a boolean flag that
7606
+ * determines whether the search should stop after finding the first matching node. If `onlyOne` is
7607
+ * set to `true`, the search will return as soon as a matching node is found. If `onlyOne` is
7608
+ * @param {C} callback - The `callback` parameter in the `search` function is a callback function
7609
+ * that will be called on each node that matches the search criteria. It is of type `C`, which
7610
+ * extends `NodeCallback<NODE>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if
7611
+ * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `search` function is
7612
+ * used to specify the node from which the search operation should begin. It represents the starting
7613
+ * point in the binary tree where the search will be performed. If no specific `startNode` is
7614
+ * provided, the search operation will start from the root
7615
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `search` function
7616
+ * specifies the type of iteration to be used when searching for nodes in a binary tree. It can have
7617
+ * two possible values:
7618
+ * @returns The `search` function returns an array of values that match the provided criteria based
7619
+ * on the search algorithm implemented within the function.
7620
+ */
7621
+ search(keyNodeEntryRawOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
7473
7622
  if (keyNodeEntryRawOrPredicate === void 0) return [];
7474
7623
  if (keyNodeEntryRawOrPredicate === null) return [];
7475
7624
  startNode = this.ensureNode(startNode);
7476
7625
  if (!startNode) return [];
7477
- const callback = this._ensurePredicate(keyNodeEntryRawOrPredicate);
7626
+ const predicate = this._ensurePredicate(keyNodeEntryRawOrPredicate);
7478
7627
  const ans = [];
7479
7628
  if (iterationType === "RECURSIVE") {
7480
7629
  const dfs = (cur) => {
7481
- if (callback(cur)) {
7482
- ans.push(cur);
7630
+ if (predicate(cur)) {
7631
+ ans.push(callback(cur));
7483
7632
  if (onlyOne) return;
7484
7633
  }
7485
7634
  if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
@@ -7492,8 +7641,8 @@ var dataStructureTyped = (() => {
7492
7641
  while (stack.length > 0) {
7493
7642
  const cur = stack.pop();
7494
7643
  if (this.isRealNode(cur)) {
7495
- if (callback(cur)) {
7496
- ans.push(cur);
7644
+ if (predicate(cur)) {
7645
+ ans.push(callback(cur));
7497
7646
  if (onlyOne) return ans;
7498
7647
  }
7499
7648
  if (this.isRealNode(cur.left)) stack.push(cur.left);
@@ -7503,6 +7652,30 @@ var dataStructureTyped = (() => {
7503
7652
  }
7504
7653
  return ans;
7505
7654
  }
7655
+ /**
7656
+ * Time Complexity: O(n)
7657
+ * Space Complexity: O(k + log n)
7658
+ *
7659
+ * The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
7660
+ * or predicate, with options for recursive or iterative traversal.
7661
+ * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
7662
+ * - The `getNodes` function you provided takes several parameters:
7663
+ * @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
7664
+ * determines whether to return only the first node that matches the criteria specified by the
7665
+ * `keyNodeEntryRawOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
7666
+ * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
7667
+ * `getNodes` function is used to specify the starting point for traversing the binary tree. It
7668
+ * represents the root node of the binary tree or the node from which the traversal should begin. If
7669
+ * not provided, the default value is set to `this._root
7670
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` function
7671
+ * determines the type of iteration to be performed when traversing the nodes of a binary tree. It
7672
+ * can have two possible values:
7673
+ * @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
7674
+ * based on the input parameters and the iteration type specified.
7675
+ */
7676
+ getNodes(keyNodeEntryRawOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
7677
+ return this.search(keyNodeEntryRawOrPredicate, onlyOne, (node) => node, startNode, iterationType);
7678
+ }
7506
7679
  /**
7507
7680
  * Time Complexity: O(n)
7508
7681
  * Space Complexity: O(log n).
@@ -7525,23 +7698,7 @@ var dataStructureTyped = (() => {
7525
7698
  */
7526
7699
  getNode(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
7527
7700
  var _a;
7528
- return (_a = this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType)[0]) != null ? _a : null;
7529
- }
7530
- /**
7531
- * Time Complexity: O(n)
7532
- * Space Complexity: O(log n)
7533
- *
7534
- * The function `getNodeByKey` retrieves a node by its key from a binary tree structure.
7535
- * @param {K} key - The `key` parameter is the value used to search for a specific node in a data
7536
- * structure.
7537
- * @param {IterationType} iterationType - The `iterationType` parameter is a type of iteration that
7538
- * specifies how the tree nodes should be traversed when searching for a node with the given key. It
7539
- * is an optional parameter with a default value of `this.iterationType`.
7540
- * @returns The `getNodeByKey` function is returning an optional binary tree node
7541
- * (`OptNodeOrNull<NODE>`).
7542
- */
7543
- getNodeByKey(key, iterationType = this.iterationType) {
7544
- return this.getNode(key, this._root, iterationType);
7701
+ return (_a = this.search(keyNodeEntryRawOrPredicate, true, (node) => node, startNode, iterationType)[0]) != null ? _a : null;
7545
7702
  }
7546
7703
  /**
7547
7704
  * Time Complexity: O(n)
@@ -7568,7 +7725,7 @@ var dataStructureTyped = (() => {
7568
7725
  get(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
7569
7726
  var _a;
7570
7727
  if (this._isMapMode) {
7571
- const key = this._getKey(keyNodeEntryRawOrPredicate);
7728
+ const key = this._extractKey(keyNodeEntryRawOrPredicate);
7572
7729
  if (key === null || key === void 0) return;
7573
7730
  return this._store.get(key);
7574
7731
  }
@@ -7596,7 +7753,7 @@ var dataStructureTyped = (() => {
7596
7753
  * Otherwise, it returns `false`.
7597
7754
  */
7598
7755
  has(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
7599
- return this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType).length > 0;
7756
+ return this.search(keyNodeEntryRawOrPredicate, true, (node) => node, startNode, iterationType).length > 0;
7600
7757
  }
7601
7758
  /**
7602
7759
  * Time Complexity: O(1)
@@ -7835,7 +7992,7 @@ var dataStructureTyped = (() => {
7835
7992
  * array is either in reverse order or in the original order based on the value of the `isReverse`
7836
7993
  * parameter.
7837
7994
  */
7838
- getPathToRoot(callback = this._DEFAULT_NODE_CALLBACK, beginNode, isReverse = true) {
7995
+ getPathToRoot(beginNode, callback = this._DEFAULT_NODE_CALLBACK, isReverse = false) {
7839
7996
  const result = [];
7840
7997
  let beginNodeEnsured = this.ensureNode(beginNode);
7841
7998
  if (!beginNodeEnsured) return result;
@@ -8760,16 +8917,16 @@ var dataStructureTyped = (() => {
8760
8917
  * Time Complexity: O(1)
8761
8918
  * Space Complexity: O(1)
8762
8919
  *
8763
- * The function `_getKey` in TypeScript returns the key from a given input, which can be a node,
8920
+ * The function `_extractKey` in TypeScript returns the key from a given input, which can be a node,
8764
8921
  * entry, raw data, or null/undefined.
8765
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `_getKey` method you provided is a
8922
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `_extractKey` method you provided is a
8766
8923
  * TypeScript method that takes in a parameter `keyNodeEntryOrRaw` of type `BTNRep<K, V, NODE> | R`,
8767
8924
  * where `BTNRep` is a generic type with keys `K`, `V`, and `NODE`, and `
8768
- * @returns The `_getKey` method returns the key value extracted from the `keyNodeEntryOrRaw`
8925
+ * @returns The `_extractKey` method returns the key value extracted from the `keyNodeEntryOrRaw`
8769
8926
  * parameter. The return value can be a key value of type `K`, `null`, or `undefined`, depending on
8770
8927
  * the conditions checked in the method.
8771
8928
  */
8772
- _getKey(keyNodeEntryOrRaw) {
8929
+ _extractKey(keyNodeEntryOrRaw) {
8773
8930
  if (keyNodeEntryOrRaw === null) return null;
8774
8931
  if (keyNodeEntryOrRaw === void 0) return;
8775
8932
  if (keyNodeEntryOrRaw === this._NIL) return;
@@ -8804,6 +8961,9 @@ var dataStructureTyped = (() => {
8804
8961
  return this._store.set(key, value);
8805
8962
  }
8806
8963
  /**
8964
+ * Time Complexity: O(1)
8965
+ * Space Complexity: O(1)
8966
+ *
8807
8967
  * The _clearNodes function sets the root node to undefined and resets the size to 0.
8808
8968
  */
8809
8969
  _clearNodes() {
@@ -8811,6 +8971,9 @@ var dataStructureTyped = (() => {
8811
8971
  this._size = 0;
8812
8972
  }
8813
8973
  /**
8974
+ * Time Complexity: O(1)
8975
+ * Space Complexity: O(1)
8976
+ *
8814
8977
  * The _clearValues function clears all values stored in the _store object.
8815
8978
  */
8816
8979
  _clearValues() {
@@ -8879,20 +9042,30 @@ var dataStructureTyped = (() => {
8879
9042
  constructor(keysNodesEntriesOrRaws = [], options) {
8880
9043
  super([], options);
8881
9044
  __publicField(this, "_root");
8882
- __publicField(this, "_DEFAULT_COMPARATOR", (a, b) => {
9045
+ __publicField(this, "_isReverse", false);
9046
+ __publicField(this, "_comparator", (a, b) => {
9047
+ if (isComparable(a) && isComparable(b)) {
9048
+ if (a > b) return 1;
9049
+ if (a < b) return -1;
9050
+ return 0;
9051
+ }
9052
+ if (this._extractComparable) {
9053
+ if (this._extractComparable(a) > this._extractComparable(b)) return 1;
9054
+ if (this._extractComparable(a) < this._extractComparable(b)) return -1;
9055
+ return 0;
9056
+ }
8883
9057
  if (typeof a === "object" || typeof b === "object") {
8884
9058
  throw TypeError(
8885
- `When comparing object types, a custom comparator must be defined in the constructor's options parameter.`
9059
+ `When comparing object types, a custom extractComparable must be defined in the constructor's options parameter.`
8886
9060
  );
8887
9061
  }
8888
- if (a > b) return 1;
8889
- if (a < b) return -1;
8890
9062
  return 0;
8891
9063
  });
8892
- __publicField(this, "_comparator", this._DEFAULT_COMPARATOR);
9064
+ __publicField(this, "_extractComparable");
8893
9065
  if (options) {
8894
- const { comparator } = options;
8895
- if (comparator) this._comparator = comparator;
9066
+ const { extractComparable, isReverse } = options;
9067
+ if (typeof extractComparable === "function") this._extractComparable = extractComparable;
9068
+ if (isReverse !== void 0) this._isReverse = isReverse;
8896
9069
  }
8897
9070
  if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
8898
9071
  }
@@ -8903,6 +9076,14 @@ var dataStructureTyped = (() => {
8903
9076
  get root() {
8904
9077
  return this._root;
8905
9078
  }
9079
+ /**
9080
+ * The above function is a getter method in TypeScript that returns the value of the private property
9081
+ * `_isReverse`.
9082
+ * @returns The `isReverse` property of the object, which is a boolean value.
9083
+ */
9084
+ get isReverse() {
9085
+ return this._isReverse;
9086
+ }
8906
9087
  /**
8907
9088
  * The function creates a new BSTNode with the given key and value and returns it.
8908
9089
  * @param {K} key - The key parameter is of type K, which represents the type of the key for the node
@@ -8925,8 +9106,9 @@ var dataStructureTyped = (() => {
8925
9106
  return new _BST([], __spreadValues({
8926
9107
  iterationType: this.iterationType,
8927
9108
  isMapMode: this._isMapMode,
8928
- comparator: this._comparator,
8929
- toEntryFn: this._toEntryFn
9109
+ extractComparable: this._extractComparable,
9110
+ toEntryFn: this._toEntryFn,
9111
+ isReverse: this._isReverse
8930
9112
  }, options));
8931
9113
  }
8932
9114
  /**
@@ -8977,11 +9159,11 @@ var dataStructureTyped = (() => {
8977
9159
  * @param {any} key - The `key` parameter is a value that will be checked to determine if it is of
8978
9160
  * type `K`.
8979
9161
  * @returns The `override isKey(key: any): key is K` function is returning a boolean value based on
8980
- * the result of the `isComparable` function with the condition `this.comparator !==
9162
+ * the result of the `isComparable` function with the condition `this._compare !==
8981
9163
  * this._DEFAULT_COMPARATOR`.
8982
9164
  */
8983
9165
  isKey(key) {
8984
- return isComparable(key, this.comparator !== this._DEFAULT_COMPARATOR);
9166
+ return isComparable(key, this._extractComparable !== void 0);
8985
9167
  }
8986
9168
  /**
8987
9169
  * Time Complexity: O(log n)
@@ -9005,11 +9187,11 @@ var dataStructureTyped = (() => {
9005
9187
  }
9006
9188
  let current = this._root;
9007
9189
  while (current !== void 0) {
9008
- if (this.comparator(current.key, newNode.key) === 0) {
9190
+ if (this._compare(current.key, newNode.key) === 0) {
9009
9191
  this._replaceNode(current, newNode);
9010
9192
  if (this._isMapMode) this._setValue(current.key, newValue);
9011
9193
  return true;
9012
- } else if (this.comparator(current.key, newNode.key) > 0) {
9194
+ } else if (this._compare(current.key, newNode.key) > 0) {
9013
9195
  if (current.left === void 0) {
9014
9196
  current.left = newNode;
9015
9197
  if (this._isMapMode) this._setValue(newNode == null ? void 0 : newNode.key, newValue);
@@ -9087,7 +9269,7 @@ var dataStructureTyped = (() => {
9087
9269
  keyB = b;
9088
9270
  }
9089
9271
  if (keyA !== void 0 && keyA !== null && keyB !== void 0 && keyB !== null) {
9090
- return this.comparator(keyA, keyB);
9272
+ return this._compare(keyA, keyB);
9091
9273
  }
9092
9274
  return 0;
9093
9275
  });
@@ -9123,48 +9305,92 @@ var dataStructureTyped = (() => {
9123
9305
  }
9124
9306
  return inserted;
9125
9307
  }
9308
+ /**
9309
+ * Time Complexity: O(n)
9310
+ * Space Complexity: O(1)
9311
+ *
9312
+ * The `merge` function overrides the base class method by adding elements from another
9313
+ * binary search tree.
9314
+ * @param anotherTree - `anotherTree` is an instance of a Binary Search Tree (BST) with key type `K`,
9315
+ * value type `V`, return type `R`, node type `NODE`, and tree type `TREE`.
9316
+ */
9317
+ merge(anotherTree) {
9318
+ this.addMany(anotherTree, [], false);
9319
+ }
9126
9320
  /**
9127
9321
  * Time Complexity: O(log n)
9128
9322
  * Space Complexity: O(k + log n)
9129
9323
  *
9130
- * The function `getNodes` in TypeScript overrides the base class method to retrieve nodes based on a
9131
- * given keyNodeEntryRawOrPredicate and iteration type.
9132
- * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate`
9133
- * parameter in the `getNodes` method is used to filter the nodes that will be returned. It can be a
9134
- * key, a node, an entry, or a custom keyNodeEntryRawOrPredicate function that determines whether a node should be
9135
- * included in the result.
9136
- * @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` method is a boolean flag that
9137
- * determines whether to return only the first node that matches the keyNodeEntryRawOrPredicate (`true`) or all nodes
9138
- * that match the keyNodeEntryRawOrPredicate (`false`). If `onlyOne` is set to `true`, the method will stop iterating
9139
- * and
9140
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
9141
- * `getNodes` method is used to specify the starting point for traversing the tree when searching for
9142
- * nodes that match a given keyNodeEntryRawOrPredicate. It represents the root node of the subtree where the search
9143
- * should begin. If not explicitly provided, the default value for `begin
9144
- * @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` method
9145
- * specifies the type of iteration to be performed when traversing the nodes of a binary tree. It can
9146
- * have two possible values:
9147
- * @returns The `getNodes` method returns an array of nodes that satisfy the given keyNodeEntryRawOrPredicate.
9324
+ * The function `search` in TypeScript overrides the search behavior in a binary tree structure based
9325
+ * on specified criteria.
9326
+ * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The
9327
+ * `keyNodeEntryRawOrPredicate` parameter in the `override search` method can accept one of the
9328
+ * following types:
9329
+ * @param [onlyOne=false] - The `onlyOne` parameter is a boolean flag that determines whether the
9330
+ * search should stop after finding the first matching node. If `onlyOne` is set to `true`, the
9331
+ * search will return as soon as a matching node is found. If `onlyOne` is set to `false`, the
9332
+ * @param {C} callback - The `callback` parameter in the `override search` function is a function
9333
+ * that will be called on each node that matches the search criteria. It is of type `C`, which
9334
+ * extends `NodeCallback<NODE>`. The callback function should accept a node of type `NODE` as its
9335
+ * argument and
9336
+ * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `override search`
9337
+ * method represents the node from which the search operation will begin. It is the starting point
9338
+ * for searching within the tree data structure. The method ensures that the `startNode` is a valid
9339
+ * node before proceeding with the search operation. If the `
9340
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `override search`
9341
+ * function determines the type of iteration to be used during the search operation. It can have two
9342
+ * possible values:
9343
+ * @returns The `override search` method returns an array of values that match the search criteria
9344
+ * specified by the input parameters. The method performs a search operation on a binary tree
9345
+ * structure based on the provided key, predicate, and other options. The search results are
9346
+ * collected in an array and returned as the output of the method.
9148
9347
  */
9149
- getNodes(keyNodeEntryRawOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
9348
+ search(keyNodeEntryRawOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
9150
9349
  if (keyNodeEntryRawOrPredicate === void 0) return [];
9151
9350
  if (keyNodeEntryRawOrPredicate === null) return [];
9152
9351
  startNode = this.ensureNode(startNode);
9153
9352
  if (!startNode) return [];
9154
- const callback = this._ensurePredicate(keyNodeEntryRawOrPredicate);
9353
+ let predicate;
9354
+ const isRange = this.isRange(keyNodeEntryRawOrPredicate);
9355
+ if (isRange) {
9356
+ predicate = (node) => keyNodeEntryRawOrPredicate.isInRange(node.key, this._comparator);
9357
+ } else {
9358
+ predicate = this._ensurePredicate(keyNodeEntryRawOrPredicate);
9359
+ }
9360
+ const isToLeftByRange = (cur) => {
9361
+ if (isRange) {
9362
+ const range = keyNodeEntryRawOrPredicate;
9363
+ const leftS = this.isReverse ? range.high : range.low;
9364
+ const leftI = this.isReverse ? range.includeHigh : range.includeLow;
9365
+ return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
9366
+ }
9367
+ return false;
9368
+ };
9369
+ const isToRightByRange = (cur) => {
9370
+ if (isRange) {
9371
+ const range = keyNodeEntryRawOrPredicate;
9372
+ const rightS = this.isReverse ? range.low : range.high;
9373
+ const rightI = this.isReverse ? range.includeLow : range.includeLow;
9374
+ return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
9375
+ }
9376
+ return false;
9377
+ };
9155
9378
  const ans = [];
9156
9379
  if (iterationType === "RECURSIVE") {
9157
9380
  const dfs = (cur) => {
9158
- if (callback(cur)) {
9159
- ans.push(cur);
9381
+ if (predicate(cur)) {
9382
+ ans.push(callback(cur));
9160
9383
  if (onlyOne) return;
9161
9384
  }
9162
9385
  if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
9163
- if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
9164
- const benchmarkKey = this._getKey(keyNodeEntryRawOrPredicate);
9165
- if (this.isRealNode(cur.left) && benchmarkKey !== null && benchmarkKey !== void 0 && this.comparator(cur.key, benchmarkKey) > 0)
9386
+ if (isRange) {
9387
+ if (this.isRealNode(cur.left) && isToLeftByRange(cur)) dfs(cur.left);
9388
+ if (this.isRealNode(cur.right) && isToRightByRange(cur)) dfs(cur.right);
9389
+ } else if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
9390
+ const benchmarkKey = this._extractKey(keyNodeEntryRawOrPredicate);
9391
+ if (this.isRealNode(cur.left) && benchmarkKey !== null && benchmarkKey !== void 0 && this._compare(cur.key, benchmarkKey) > 0)
9166
9392
  dfs(cur.left);
9167
- if (this.isRealNode(cur.right) && benchmarkKey !== null && benchmarkKey !== void 0 && this.comparator(cur.key, benchmarkKey) < 0)
9393
+ if (this.isRealNode(cur.right) && benchmarkKey !== null && benchmarkKey !== void 0 && this._compare(cur.key, benchmarkKey) < 0)
9168
9394
  dfs(cur.right);
9169
9395
  } else {
9170
9396
  if (this.isRealNode(cur.left)) dfs(cur.left);
@@ -9176,15 +9402,18 @@ var dataStructureTyped = (() => {
9176
9402
  const stack = [startNode];
9177
9403
  while (stack.length > 0) {
9178
9404
  const cur = stack.pop();
9179
- if (callback(cur)) {
9180
- ans.push(cur);
9405
+ if (predicate(cur)) {
9406
+ ans.push(callback(cur));
9181
9407
  if (onlyOne) return ans;
9182
9408
  }
9183
- if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
9184
- const benchmarkKey = this._getKey(keyNodeEntryRawOrPredicate);
9185
- if (this.isRealNode(cur.right) && benchmarkKey !== null && benchmarkKey !== void 0 && this.comparator(cur.key, benchmarkKey) < 0)
9409
+ if (isRange) {
9410
+ if (this.isRealNode(cur.left) && isToLeftByRange(cur)) stack.push(cur.left);
9411
+ if (this.isRealNode(cur.right) && isToRightByRange(cur)) stack.push(cur.right);
9412
+ } else if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
9413
+ const benchmarkKey = this._extractKey(keyNodeEntryRawOrPredicate);
9414
+ if (this.isRealNode(cur.right) && benchmarkKey !== null && benchmarkKey !== void 0 && this._compare(cur.key, benchmarkKey) < 0)
9186
9415
  stack.push(cur.right);
9187
- if (this.isRealNode(cur.left) && benchmarkKey !== null && benchmarkKey !== void 0 && this.comparator(cur.key, benchmarkKey) > 0)
9416
+ if (this.isRealNode(cur.left) && benchmarkKey !== null && benchmarkKey !== void 0 && this._compare(cur.key, benchmarkKey) > 0)
9188
9417
  stack.push(cur.left);
9189
9418
  } else {
9190
9419
  if (this.isRealNode(cur.right)) stack.push(cur.right);
@@ -9218,22 +9447,6 @@ var dataStructureTyped = (() => {
9218
9447
  var _a;
9219
9448
  return (_a = this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType)[0]) != null ? _a : void 0;
9220
9449
  }
9221
- /**
9222
- * Time Complexity: O(log n)
9223
- * Space Complexity: O(1)
9224
- *
9225
- * The function `getNodeByKey` returns a node with a specific key from a tree data structure.
9226
- * @param {K} key - The key parameter is the value used to search for a specific node in the tree. It
9227
- * is typically a unique identifier or a value that can be used to determine the position of the node
9228
- * in the tree structure.
9229
- * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
9230
- * parameter that specifies the type of iteration to be used when searching for a node in the tree.
9231
- * It has a default value of `'ITERATIVE'`.
9232
- * @returns The method is returning a NODE object or undefined.
9233
- */
9234
- getNodeByKey(key, iterationType = this.iterationType) {
9235
- return this.getNode(key, this._root, iterationType);
9236
- }
9237
9450
  /**
9238
9451
  * Time complexity: O(n)
9239
9452
  * Space complexity: O(n)
@@ -9327,7 +9540,7 @@ var dataStructureTyped = (() => {
9327
9540
  const targetKey = targetNodeEnsured.key;
9328
9541
  if (iterationType === "RECURSIVE") {
9329
9542
  const dfs = (cur) => {
9330
- const compared = this.comparator(cur.key, targetKey);
9543
+ const compared = this._compare(cur.key, targetKey);
9331
9544
  if (Math.sign(compared) === lesserOrGreater) ans.push(callback(cur));
9332
9545
  if (this.isRealNode(cur.left)) dfs(cur.left);
9333
9546
  if (this.isRealNode(cur.right)) dfs(cur.right);
@@ -9339,7 +9552,7 @@ var dataStructureTyped = (() => {
9339
9552
  while (queue.size > 0) {
9340
9553
  const cur = queue.shift();
9341
9554
  if (this.isRealNode(cur)) {
9342
- const compared = this.comparator(cur.key, targetKey);
9555
+ const compared = this._compare(cur.key, targetKey);
9343
9556
  if (Math.sign(compared) === lesserOrGreater) ans.push(callback(cur));
9344
9557
  if (this.isRealNode(cur.left)) queue.push(cur.left);
9345
9558
  if (this.isRealNode(cur.right)) queue.push(cur.right);
@@ -9451,6 +9664,14 @@ var dataStructureTyped = (() => {
9451
9664
  get comparator() {
9452
9665
  return this._comparator;
9453
9666
  }
9667
+ /**
9668
+ * This function returns the value of the `_extractComparable` property.
9669
+ * @returns The method `extractComparable()` is being returned, which is a getter method for the
9670
+ * `_extractComparable` property.
9671
+ */
9672
+ get extractComparable() {
9673
+ return this._extractComparable;
9674
+ }
9454
9675
  /**
9455
9676
  * The function sets the root of a tree-like structure and updates the parent property of the new
9456
9677
  * root.
@@ -9462,6 +9683,9 @@ var dataStructureTyped = (() => {
9462
9683
  }
9463
9684
  this._root = v;
9464
9685
  }
9686
+ _compare(a, b) {
9687
+ return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
9688
+ }
9465
9689
  };
9466
9690
 
9467
9691
  // src/data-structures/binary-tree/binary-indexed-tree.ts
@@ -10097,8 +10321,9 @@ var dataStructureTyped = (() => {
10097
10321
  return new _AVLTree([], __spreadValues({
10098
10322
  iterationType: this.iterationType,
10099
10323
  isMapMode: this._isMapMode,
10100
- comparator: this._comparator,
10101
- toEntryFn: this._toEntryFn
10324
+ extractComparable: this._extractComparable,
10325
+ toEntryFn: this._toEntryFn,
10326
+ isReverse: this._isReverse
10102
10327
  }, options));
10103
10328
  }
10104
10329
  /**
@@ -10384,7 +10609,7 @@ var dataStructureTyped = (() => {
10384
10609
  */
10385
10610
  _balancePath(node) {
10386
10611
  node = this.ensureNode(node);
10387
- const path = this.getPathToRoot((node2) => node2, node, false);
10612
+ const path = this.getPathToRoot(node, (node2) => node2, false);
10388
10613
  for (let i = 0; i < path.length; i++) {
10389
10614
  const A = path[i];
10390
10615
  if (A) {
@@ -10516,7 +10741,7 @@ var dataStructureTyped = (() => {
10516
10741
  return new _RedBlackTree([], __spreadValues({
10517
10742
  iterationType: this.iterationType,
10518
10743
  isMapMode: this._isMapMode,
10519
- comparator: this._comparator,
10744
+ extractComparable: this._extractComparable,
10520
10745
  toEntryFn: this._toEntryFn
10521
10746
  }, options));
10522
10747
  }
@@ -10725,7 +10950,7 @@ var dataStructureTyped = (() => {
10725
10950
  let parent = void 0;
10726
10951
  while (this.isRealNode(current)) {
10727
10952
  parent = current;
10728
- const compared = this.comparator(node.key, current.key);
10953
+ const compared = this._compare(node.key, current.key);
10729
10954
  if (compared < 0) {
10730
10955
  current = (_a = current.left) != null ? _a : this.NIL;
10731
10956
  } else if (compared > 0) {
@@ -11040,8 +11265,9 @@ var dataStructureTyped = (() => {
11040
11265
  return new _AVLTreeMultiMap([], __spreadValues({
11041
11266
  iterationType: this.iterationType,
11042
11267
  isMapMode: this._isMapMode,
11043
- comparator: this._comparator,
11044
- toEntryFn: this._toEntryFn
11268
+ extractComparable: this._extractComparable,
11269
+ toEntryFn: this._toEntryFn,
11270
+ isReverse: this._isReverse
11045
11271
  }, options));
11046
11272
  }
11047
11273
  /**
@@ -11075,15 +11301,12 @@ var dataStructureTyped = (() => {
11075
11301
  const finalValue = value != null ? value : entryValue;
11076
11302
  return [this.createNode(key, finalValue, count), finalValue];
11077
11303
  }
11078
- if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, count), value];
11079
11304
  if (this.isRaw(keyNodeEntryOrRaw)) {
11080
- if (this._toEntryFn) {
11081
- const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
11082
- const finalValue = value != null ? value : entryValue;
11083
- if (this.isKey(key)) return [this.createNode(key, finalValue, count), finalValue];
11084
- }
11085
- return [void 0, void 0];
11305
+ const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
11306
+ const finalValue = value != null ? value : entryValue;
11307
+ if (this.isKey(key)) return [this.createNode(key, finalValue, count), finalValue];
11086
11308
  }
11309
+ if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, count), value];
11087
11310
  return [void 0, void 0];
11088
11311
  }
11089
11312
  /**
@@ -11402,7 +11625,7 @@ var dataStructureTyped = (() => {
11402
11625
  return new _TreeMultiMap([], __spreadValues({
11403
11626
  iterationType: this.iterationType,
11404
11627
  isMapMode: this._isMapMode,
11405
- comparator: this._comparator,
11628
+ extractComparable: this._extractComparable,
11406
11629
  toEntryFn: this._toEntryFn
11407
11630
  }, options));
11408
11631
  }
@@ -11427,7 +11650,7 @@ var dataStructureTyped = (() => {
11427
11650
  const finalValue = value != null ? value : entryValue;
11428
11651
  if (this.isKey(key)) return [this.createNode(key, finalValue, "BLACK", count), finalValue];
11429
11652
  }
11430
- if (this._toEntryFn) {
11653
+ if (this.isRaw(keyNodeEntryOrRaw)) {
11431
11654
  const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
11432
11655
  const finalValue = value != null ? value : entryValue;
11433
11656
  if (this.isKey(key)) return [this.createNode(key, finalValue, "BLACK", count), finalValue];
@@ -12546,10 +12769,16 @@ var dataStructureTyped = (() => {
12546
12769
  };
12547
12770
  var Trie = class _Trie extends IterableElementBase {
12548
12771
  /**
12549
- * The constructor function for the Trie class.
12550
- * @param words: Iterable string Initialize the trie with a set of words
12551
- * @param options?: TrieOptions Allow the user to pass in options for the trie
12552
- * @return This
12772
+ * The constructor initializes a Trie data structure with optional options and words provided as
12773
+ * input.
12774
+ * @param {Iterable<string> | Iterable<R>} words - The `words` parameter in the constructor is an
12775
+ * iterable containing either strings or elements of type `R`. It is used to initialize the Trie with
12776
+ * a list of words or elements. If no `words` are provided, an empty iterable is used as the default
12777
+ * value.
12778
+ * @param [options] - The `options` parameter in the constructor is an optional object that can
12779
+ * contain configuration options for the Trie data structure. One of the options it can have is
12780
+ * `caseSensitive`, which is a boolean value indicating whether the Trie should be case-sensitive or
12781
+ * not. If `caseSensitive` is set to `
12553
12782
  */
12554
12783
  constructor(words = [], options) {
12555
12784
  super(options);
@@ -12561,13 +12790,7 @@ var dataStructureTyped = (() => {
12561
12790
  if (caseSensitive !== void 0) this._caseSensitive = caseSensitive;
12562
12791
  }
12563
12792
  if (words) {
12564
- for (const word of words) {
12565
- if (this.toElementFn) {
12566
- this.add(this.toElementFn(word));
12567
- } else {
12568
- this.add(word);
12569
- }
12570
- }
12793
+ this.addMany(words);
12571
12794
  }
12572
12795
  }
12573
12796
  /**
@@ -12618,6 +12841,29 @@ var dataStructureTyped = (() => {
12618
12841
  }
12619
12842
  return isNewWord;
12620
12843
  }
12844
+ /**
12845
+ * Time Complexity: O(n * l)
12846
+ * Space Complexity: O(1)
12847
+ *
12848
+ * The `addMany` function in TypeScript takes an iterable of strings or elements of type R, converts
12849
+ * them using a provided function if available, and adds them to a data structure while returning an
12850
+ * array of boolean values indicating success.
12851
+ * @param {Iterable<string> | Iterable<R>} words - The `words` parameter in the `addMany` function is
12852
+ * an iterable that contains either strings or elements of type `R`.
12853
+ * @returns The `addMany` method returns an array of boolean values indicating whether each word in
12854
+ * the input iterable was successfully added to the data structure.
12855
+ */
12856
+ addMany(words = []) {
12857
+ const ans = [];
12858
+ for (const word of words) {
12859
+ if (this.toElementFn) {
12860
+ ans.push(this.add(this.toElementFn(word)));
12861
+ } else {
12862
+ ans.push(this.add(word));
12863
+ }
12864
+ }
12865
+ return ans;
12866
+ }
12621
12867
  /**
12622
12868
  * Time Complexity: O(l), where l is the length of the input word.
12623
12869
  * Space Complexity: O(1) - Constant space.