data-structure-typed 1.50.3 → 1.50.5

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/.github/ISSUE_TEMPLATE/bug_report.md +0 -13
  2. package/CHANGELOG.md +1 -1
  3. package/README.md +122 -61
  4. package/README_zh-CN.md +6 -6
  5. package/SPECIFICATION.md +2 -2
  6. package/SPECIFICATION_zh-CN.md +81 -0
  7. package/{SPONSOR-zh-CN.md → SPONSOR_zh-CN.md} +1 -1
  8. package/benchmark/report.html +24 -24
  9. package/benchmark/report.json +242 -242
  10. package/dist/cjs/data-structures/base/iterable-base.d.ts +10 -8
  11. package/dist/cjs/data-structures/base/iterable-base.js +8 -12
  12. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
  13. package/dist/{mjs/data-structures/binary-tree/tree-multimap.d.ts → cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts} +11 -11
  14. package/dist/cjs/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +15 -15
  15. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -0
  16. package/dist/cjs/data-structures/binary-tree/bst.js +5 -7
  17. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/index.d.ts +2 -1
  19. package/dist/cjs/data-structures/binary-tree/index.js +2 -1
  20. package/dist/cjs/data-structures/binary-tree/index.js.map +1 -1
  21. package/dist/cjs/data-structures/binary-tree/rb-tree.js +17 -9
  22. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  23. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +200 -0
  24. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +400 -0
  25. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -0
  26. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -0
  27. package/dist/cjs/data-structures/graph/abstract-graph.js +3 -0
  28. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  29. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  30. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +14 -76
  31. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +16 -86
  32. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  33. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +27 -69
  34. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +35 -79
  35. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  36. package/dist/cjs/data-structures/queue/deque.d.ts +0 -53
  37. package/dist/cjs/data-structures/queue/deque.js +0 -61
  38. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  39. package/dist/cjs/data-structures/queue/queue.d.ts +0 -70
  40. package/dist/cjs/data-structures/queue/queue.js +0 -87
  41. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  42. package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -0
  43. package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.js +3 -0
  44. package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -0
  45. package/dist/cjs/types/data-structures/binary-tree/index.d.ts +2 -1
  46. package/dist/cjs/types/data-structures/binary-tree/index.js +2 -1
  47. package/dist/cjs/types/data-structures/binary-tree/index.js.map +1 -1
  48. package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -0
  49. package/dist/cjs/types/data-structures/binary-tree/{tree-multimap.js → tree-multi-map.js} +1 -1
  50. package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.js.map +1 -0
  51. package/dist/mjs/data-structures/base/iterable-base.d.ts +10 -8
  52. package/dist/mjs/data-structures/base/iterable-base.js +8 -12
  53. package/dist/{cjs/data-structures/binary-tree/tree-multimap.d.ts → mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts} +11 -11
  54. package/dist/mjs/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +11 -11
  55. package/dist/mjs/data-structures/binary-tree/bst.js +5 -7
  56. package/dist/mjs/data-structures/binary-tree/index.d.ts +2 -1
  57. package/dist/mjs/data-structures/binary-tree/index.js +2 -1
  58. package/dist/mjs/data-structures/binary-tree/rb-tree.js +17 -9
  59. package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +200 -0
  60. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +397 -0
  61. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -0
  62. package/dist/mjs/data-structures/graph/abstract-graph.js +3 -0
  63. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +14 -76
  64. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +16 -86
  65. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +27 -69
  66. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +33 -79
  67. package/dist/mjs/data-structures/queue/deque.d.ts +0 -53
  68. package/dist/mjs/data-structures/queue/deque.js +0 -61
  69. package/dist/mjs/data-structures/queue/queue.d.ts +0 -70
  70. package/dist/mjs/data-structures/queue/queue.js +0 -86
  71. package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -0
  72. package/dist/mjs/types/data-structures/binary-tree/index.d.ts +2 -1
  73. package/dist/mjs/types/data-structures/binary-tree/index.js +2 -1
  74. package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -0
  75. package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.js +1 -0
  76. package/dist/umd/data-structure-typed.js +461 -354
  77. package/dist/umd/data-structure-typed.min.js +2 -2
  78. package/dist/umd/data-structure-typed.min.js.map +1 -1
  79. package/package.json +1 -1
  80. package/src/data-structures/base/iterable-base.ts +14 -10
  81. package/src/data-structures/binary-tree/{tree-multimap.ts → avl-tree-multi-map.ts} +20 -20
  82. package/src/data-structures/binary-tree/bst.ts +5 -6
  83. package/src/data-structures/binary-tree/index.ts +2 -1
  84. package/src/data-structures/binary-tree/rb-tree.ts +20 -10
  85. package/src/data-structures/binary-tree/tree-multi-map.ts +463 -0
  86. package/src/data-structures/graph/abstract-graph.ts +4 -0
  87. package/src/data-structures/heap/heap.ts +1 -1
  88. package/src/data-structures/linked-list/doubly-linked-list.ts +16 -94
  89. package/src/data-structures/linked-list/singly-linked-list.ts +35 -87
  90. package/src/data-structures/queue/deque.ts +0 -67
  91. package/src/data-structures/queue/queue.ts +0 -98
  92. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +8 -0
  93. package/src/types/data-structures/binary-tree/index.ts +2 -1
  94. package/src/types/data-structures/binary-tree/tree-multi-map.ts +8 -0
  95. package/test/integration/index.html +2 -2
  96. package/test/performance/data-structures/binary-tree/avl-tree.test.ts +3 -3
  97. package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +3 -3
  98. package/test/performance/data-structures/hash/hash-map.test.ts +6 -6
  99. package/test/performance/data-structures/heap/heap.test.ts +14 -14
  100. package/test/performance/data-structures/priority-queue/priority-queue.test.ts +11 -6
  101. package/test/performance/data-structures/queue/deque.test.ts +8 -8
  102. package/test/performance/data-structures/queue/queue.test.ts +5 -12
  103. package/test/performance/reportor.ts +43 -1
  104. package/test/unit/data-structures/binary-tree/{tree-multimap.test.ts → avl-tree-multi-map.test.ts} +55 -55
  105. package/test/unit/data-structures/binary-tree/overall.test.ts +24 -25
  106. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +706 -0
  107. package/test/unit/data-structures/graph/undirected-graph.test.ts +4 -4
  108. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +6 -6
  109. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +10 -10
  110. package/test/unit/data-structures/linked-list/skip-list.test.ts +4 -4
  111. package/test/unit/data-structures/queue/deque.test.ts +26 -26
  112. package/test/unit/data-structures/queue/queue.test.ts +20 -20
  113. package/test/unit/unrestricted-interconversion.test.ts +3 -3
  114. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +0 -1
  115. package/dist/cjs/types/data-structures/binary-tree/tree-multimap.d.ts +0 -5
  116. package/dist/cjs/types/data-structures/binary-tree/tree-multimap.js.map +0 -1
  117. package/dist/mjs/types/data-structures/binary-tree/tree-multimap.d.ts +0 -5
  118. package/src/types/data-structures/binary-tree/tree-multimap.ts +0 -8
  119. /package/dist/mjs/types/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +0 -0
@@ -102,6 +102,8 @@ var dataStructureTyped = (() => {
102
102
  var src_exports = {};
103
103
  __export(src_exports, {
104
104
  AVLTree: () => AVLTree,
105
+ AVLTreeMultiMap: () => AVLTreeMultiMap,
106
+ AVLTreeMultiMapNode: () => AVLTreeMultiMapNode,
105
107
  AVLTreeNode: () => AVLTreeNode,
106
108
  AbstractEdge: () => AbstractEdge,
107
109
  AbstractGraph: () => AbstractGraph,
@@ -152,8 +154,8 @@ var dataStructureTyped = (() => {
152
154
  SkipListNode: () => SkipListNode,
153
155
  Stack: () => Stack,
154
156
  THUNK_SYMBOL: () => THUNK_SYMBOL,
155
- TreeMultimap: () => TreeMultimap,
156
- TreeMultimapNode: () => TreeMultimapNode,
157
+ TreeMultiMap: () => TreeMultiMap,
158
+ TreeMultiMapNode: () => TreeMultiMapNode,
157
159
  TreeNode: () => TreeNode,
158
160
  Trie: () => Trie,
159
161
  TrieNode: () => TrieNode,
@@ -176,10 +178,6 @@ var dataStructureTyped = (() => {
176
178
 
177
179
  // src/data-structures/base/iterable-base.ts
178
180
  var IterableEntryBase = class {
179
- /**
180
- * Time Complexity: O(n)
181
- * Space Complexity: O(1)
182
- */
183
181
  /**
184
182
  * Time Complexity: O(n)
185
183
  * Space Complexity: O(1)
@@ -297,6 +295,10 @@ var dataStructureTyped = (() => {
297
295
  * Time Complexity: O(n)
298
296
  * Space Complexity: O(1)
299
297
  */
298
+ /**
299
+ * Time Complexity: O(n)
300
+ * Space Complexity: O(1)
301
+ */
300
302
  /**
301
303
  * Time Complexity: O(n)
302
304
  * Space Complexity: O(1)
@@ -411,10 +413,6 @@ var dataStructureTyped = (() => {
411
413
  }
412
414
  return;
413
415
  }
414
- /**
415
- * Time Complexity: O(n)
416
- * Space Complexity: O(1)
417
- */
418
416
  /**
419
417
  * Time Complexity: O(n)
420
418
  * Space Complexity: O(1)
@@ -511,6 +509,10 @@ var dataStructureTyped = (() => {
511
509
  * Time Complexity: O(n)
512
510
  * Space Complexity: O(1)
513
511
  */
512
+ /**
513
+ * Time Complexity: O(n)
514
+ * Space Complexity: O(1)
515
+ */
514
516
  /**
515
517
  * Time Complexity: O(n)
516
518
  * Space Complexity: O(1)
@@ -585,10 +587,6 @@ var dataStructureTyped = (() => {
585
587
  }
586
588
  return;
587
589
  }
588
- /**
589
- * Time Complexity: O(n)
590
- * Space Complexity: O(1)
591
- */
592
590
  /**
593
591
  * Time Complexity: O(n)
594
592
  * Space Complexity: O(1)
@@ -1661,6 +1659,24 @@ var dataStructureTyped = (() => {
1661
1659
  get tail() {
1662
1660
  return this._tail;
1663
1661
  }
1662
+ /**
1663
+ * The above function returns the value of the first element in a linked list, or undefined if the
1664
+ * list is empty.
1665
+ * @returns The value of the first node in the linked list, or undefined if the linked list is empty.
1666
+ */
1667
+ get first() {
1668
+ var _a;
1669
+ return (_a = this.head) == null ? void 0 : _a.value;
1670
+ }
1671
+ /**
1672
+ * The function returns the value of the last element in a linked list, or undefined if the list is
1673
+ * empty.
1674
+ * @returns The value of the last node in the linked list, or undefined if the linked list is empty.
1675
+ */
1676
+ get last() {
1677
+ var _a;
1678
+ return (_a = this.tail) == null ? void 0 : _a.value;
1679
+ }
1664
1680
  /**
1665
1681
  * The function returns the size of an object.
1666
1682
  * @returns The size of the object, which is a number.
@@ -1693,19 +1709,18 @@ var dataStructureTyped = (() => {
1693
1709
  /**
1694
1710
  * Time Complexity: O(1)
1695
1711
  * Space Complexity: O(1)
1696
- * Constant time, as it involves basic pointer adjustments.
1697
- * Constant space, as it only creates a new node.
1698
1712
  */
1699
1713
  /**
1700
1714
  * Time Complexity: O(1)
1701
1715
  * Space Complexity: O(1)
1702
1716
  *
1703
- * The `push` function adds a new node with the given value to the end of a singly linked list.
1704
- * @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
1705
- * any type (E) as specified in the generic type declaration of the class or function.
1717
+ * The push function adds a new element to the end of a singly linked list.
1718
+ * @param {E} element - The "element" parameter represents the value of the element that you want to
1719
+ * add to the linked list.
1720
+ * @returns The `push` method is returning a boolean value, `true`.
1706
1721
  */
1707
- push(value) {
1708
- const newNode = new SinglyLinkedListNode(value);
1722
+ push(element) {
1723
+ const newNode = new SinglyLinkedListNode(element);
1709
1724
  if (!this.head) {
1710
1725
  this._head = newNode;
1711
1726
  this._tail = newNode;
@@ -1716,21 +1731,6 @@ var dataStructureTyped = (() => {
1716
1731
  this._size++;
1717
1732
  return true;
1718
1733
  }
1719
- /**
1720
- * Time Complexity: O(1)
1721
- * Space Complexity: O(1)
1722
- */
1723
- /**
1724
- * Time Complexity: O(1)
1725
- * Space Complexity: O(1)
1726
- *
1727
- * The `push` function adds a new node with the given value to the end of a singly linked list.
1728
- * @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
1729
- * any type (E) as specified in the generic type declaration of the class or function.
1730
- */
1731
- addLast(value) {
1732
- return this.push(value);
1733
- }
1734
1734
  /**
1735
1735
  * Time Complexity: O(n)
1736
1736
  * Space Complexity: O(1)
@@ -1740,10 +1740,9 @@ var dataStructureTyped = (() => {
1740
1740
  * Time Complexity: O(n)
1741
1741
  * Space Complexity: O(1)
1742
1742
  *
1743
- * The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
1744
- * pointers accordingly.
1745
- * @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
1746
- * the linked list is empty, it returns `undefined`.
1743
+ * The `pop` function removes and returns the value of the last element in a linked list.
1744
+ * @returns The method is returning the value of the element that is being popped from the end of the
1745
+ * list.
1747
1746
  */
1748
1747
  pop() {
1749
1748
  if (!this.head)
@@ -1765,22 +1764,6 @@ var dataStructureTyped = (() => {
1765
1764
  this._size--;
1766
1765
  return value;
1767
1766
  }
1768
- /**
1769
- * Time Complexity: O(n)
1770
- * Space Complexity: O(1)
1771
- */
1772
- /**
1773
- * Time Complexity: O(n)
1774
- * Space Complexity: O(1)
1775
- *
1776
- * The `pollLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
1777
- * pointers accordingly.
1778
- * @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
1779
- * the linked list is empty, it returns `undefined`.
1780
- */
1781
- pollLast() {
1782
- return this.pop();
1783
- }
1784
1767
  /**
1785
1768
  * Time Complexity: O(1)
1786
1769
  * Space Complexity: O(1)
@@ -1789,8 +1772,8 @@ var dataStructureTyped = (() => {
1789
1772
  * Time Complexity: O(1)
1790
1773
  * Space Complexity: O(1)
1791
1774
  *
1792
- * The `shift()` function removes and returns the value of the first node in a linked list.
1793
- * @returns The value of the node that is being removed from the beginning of the linked list.
1775
+ * The `shift()` function removes and returns the value of the first element in a linked list.
1776
+ * @returns The value of the removed node.
1794
1777
  */
1795
1778
  shift() {
1796
1779
  if (!this.head)
@@ -1808,26 +1791,13 @@ var dataStructureTyped = (() => {
1808
1791
  * Time Complexity: O(1)
1809
1792
  * Space Complexity: O(1)
1810
1793
  *
1811
- * The `pollFirst()` function removes and returns the value of the first node in a linked list.
1812
- * @returns The value of the node that is being removed from the beginning of the linked list.
1813
- */
1814
- pollFirst() {
1815
- return this.shift();
1816
- }
1817
- /**
1818
- * Time Complexity: O(1)
1819
- * Space Complexity: O(1)
1820
- */
1821
- /**
1822
- * Time Complexity: O(1)
1823
- * Space Complexity: O(1)
1824
- *
1825
- * The unshift function adds a new node with the given value to the beginning of a singly linked list.
1826
- * @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
1827
- * linked list.
1794
+ * The unshift function adds a new element to the beginning of a singly linked list.
1795
+ * @param {E} element - The "element" parameter represents the value of the element that you want to
1796
+ * add to the beginning of the singly linked list.
1797
+ * @returns The `unshift` method is returning a boolean value, `true`.
1828
1798
  */
1829
- unshift(value) {
1830
- const newNode = new SinglyLinkedListNode(value);
1799
+ unshift(element) {
1800
+ const newNode = new SinglyLinkedListNode(element);
1831
1801
  if (!this.head) {
1832
1802
  this._head = newNode;
1833
1803
  this._tail = newNode;
@@ -1838,25 +1808,9 @@ var dataStructureTyped = (() => {
1838
1808
  this._size++;
1839
1809
  return true;
1840
1810
  }
1841
- /**
1842
- * Time Complexity: O(1)
1843
- * Space Complexity: O(1)
1844
- */
1845
- /**
1846
- * Time Complexity: O(1)
1847
- * Space Complexity: O(1)
1848
- *
1849
- * The addFirst function adds a new node with the given value to the beginning of a singly linked list.
1850
- * @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
1851
- * linked list.
1852
- */
1853
- addFirst(value) {
1854
- return this.unshift(value);
1855
- }
1856
1811
  /**
1857
1812
  * Time Complexity: O(n)
1858
1813
  * Space Complexity: O(1)
1859
- * Linear time, where n is the index, as it may need to traverse the list to find the desired node.
1860
1814
  */
1861
1815
  /**
1862
1816
  * Time Complexity: O(n)
@@ -2465,14 +2419,13 @@ var dataStructureTyped = (() => {
2465
2419
  * Space Complexity: O(1)
2466
2420
  */
2467
2421
  /**
2468
- * Time Complexity: O(1)
2469
- * Space Complexity: O(1)
2470
- *
2471
- * The push function adds a new node with the given value to the end of the doubly linked list.
2472
- * @param {E} value - The value to be added to the linked list.
2422
+ * The push function adds a new element to the end of a doubly linked list.
2423
+ * @param {E} element - The "element" parameter represents the value that you want to add to the
2424
+ * doubly linked list.
2425
+ * @returns The `push` method is returning a boolean value, `true`.
2473
2426
  */
2474
- push(value) {
2475
- const newNode = new DoublyLinkedListNode(value);
2427
+ push(element) {
2428
+ const newNode = new DoublyLinkedListNode(element);
2476
2429
  if (!this.head) {
2477
2430
  this._head = newNode;
2478
2431
  this._tail = newNode;
@@ -2489,12 +2442,8 @@ var dataStructureTyped = (() => {
2489
2442
  * Space Complexity: O(1)
2490
2443
  */
2491
2444
  /**
2492
- * Time Complexity: O(1)
2493
- * Space Complexity: O(1)
2494
- *
2495
- * The `pop()` function removes and returns the value of the last node in a doubly linked list.
2496
- * @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
2497
- * list is empty, it returns undefined.
2445
+ * The `pop()` function removes and returns the value of the last element in a linked list.
2446
+ * @returns The method is returning the value of the removed node.
2498
2447
  */
2499
2448
  pop() {
2500
2449
  if (!this.tail)
@@ -2515,12 +2464,8 @@ var dataStructureTyped = (() => {
2515
2464
  * Space Complexity: O(1)
2516
2465
  */
2517
2466
  /**
2518
- * Time Complexity: O(1)
2519
- * Space Complexity: O(1)
2520
- *
2521
- * The `shift()` function removes and returns the value of the first node in a doubly linked list.
2522
- * @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
2523
- * list.
2467
+ * The `shift()` function removes and returns the value of the first element in a doubly linked list.
2468
+ * @returns The value of the removed node.
2524
2469
  */
2525
2470
  shift() {
2526
2471
  if (!this.head)
@@ -2541,15 +2486,13 @@ var dataStructureTyped = (() => {
2541
2486
  * Space Complexity: O(1)
2542
2487
  */
2543
2488
  /**
2544
- * Time Complexity: O(1)
2545
- * Space Complexity: O(1)
2546
- *
2547
- * The unshift function adds a new node with the given value to the beginning of a doubly linked list.
2548
- * @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
2549
- * doubly linked list.
2489
+ * The unshift function adds a new element to the beginning of a doubly linked list.
2490
+ * @param {E} element - The "element" parameter represents the value of the element that you want to
2491
+ * add to the beginning of the doubly linked list.
2492
+ * @returns The `unshift` method is returning a boolean value, `true`.
2550
2493
  */
2551
- unshift(value) {
2552
- const newNode = new DoublyLinkedListNode(value);
2494
+ unshift(element) {
2495
+ const newNode = new DoublyLinkedListNode(element);
2553
2496
  if (!this.head) {
2554
2497
  this._head = newNode;
2555
2498
  this._tail = newNode;
@@ -3033,65 +2976,6 @@ var dataStructureTyped = (() => {
3033
2976
  }
3034
2977
  return mappedList;
3035
2978
  }
3036
- /**
3037
- * Time Complexity: O(1)
3038
- * Space Complexity: O(1)
3039
- */
3040
- /**
3041
- * Time Complexity: O(1)
3042
- * Space Complexity: O(1)
3043
- *
3044
- * The addLast function adds a new node with the given value to the end of the doubly linked list.
3045
- * @param {E} value - The value to be added to the linked list.
3046
- */
3047
- addLast(value) {
3048
- return this.push(value);
3049
- }
3050
- /**
3051
- * Time Complexity: O(1)
3052
- * Space Complexity: O(1)
3053
- */
3054
- /**
3055
- * Time Complexity: O(1)
3056
- * Space Complexity: O(1)
3057
- *
3058
- * The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
3059
- * @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
3060
- * list is empty, it returns undefined.
3061
- */
3062
- pollLast() {
3063
- return this.pop();
3064
- }
3065
- /**
3066
- * Time Complexity: O(1)
3067
- * Space Complexity: O(1)
3068
- */
3069
- /**
3070
- * Time Complexity: O(1)
3071
- * Space Complexity: O(1)
3072
- *
3073
- * The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
3074
- * @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
3075
- * list.
3076
- */
3077
- pollFirst() {
3078
- return this.shift();
3079
- }
3080
- /**
3081
- * Time Complexity: O(1)
3082
- * Space Complexity: O(1)
3083
- */
3084
- /**
3085
- * Time Complexity: O(1)
3086
- * Space Complexity: O(1)
3087
- *
3088
- * The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
3089
- * @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
3090
- * doubly linked list.
3091
- */
3092
- addFirst(value) {
3093
- this.unshift(value);
3094
- }
3095
2979
  /**
3096
2980
  * The function returns an iterator that iterates over the values of a linked list.
3097
2981
  */
@@ -3758,64 +3642,6 @@ var dataStructureTyped = (() => {
3758
3642
  const spliced = this.elements.splice(index, 1);
3759
3643
  return spliced.length === 1;
3760
3644
  }
3761
- /**
3762
- * Time Complexity: O(1)
3763
- * Space Complexity: O(1)
3764
- */
3765
- /**
3766
- * Time Complexity: O(1)
3767
- * Space Complexity: O(1)
3768
- *
3769
- * The `peek` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
3770
- * @returns The `peek()` method returns the first element of the data structure, represented by the `_elements` array at
3771
- * the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
3772
- */
3773
- peek() {
3774
- return this.first;
3775
- }
3776
- /**
3777
- * Time Complexity: O(1)
3778
- * Space Complexity: O(1)
3779
- */
3780
- /**
3781
- * Time Complexity: O(1)
3782
- * Space Complexity: O(1)
3783
- *
3784
- * The `peekLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
3785
- * @returns The method `peekLast()` returns the last element of the `_elements` array if the array is not empty. If the
3786
- * array is empty, it returns `undefined`.
3787
- */
3788
- peekLast() {
3789
- return this.last;
3790
- }
3791
- /**
3792
- * Time Complexity: O(1)
3793
- * Space Complexity: O(1)
3794
- */
3795
- /**
3796
- * Time Complexity: O(1)
3797
- * Space Complexity: O(1)
3798
- *
3799
- * The enqueue function adds a value to the end of a queue.
3800
- * @param {E} value - The value parameter represents the value that you want to add to the queue.
3801
- */
3802
- enqueue(value) {
3803
- return this.push(value);
3804
- }
3805
- /**
3806
- * Time Complexity: O(1)
3807
- * Space Complexity: O(1)
3808
- */
3809
- /**
3810
- * Time Complexity: O(1)
3811
- * Space Complexity: O(1)
3812
- *
3813
- * The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
3814
- * @returns The method is returning a value of type E or undefined.
3815
- */
3816
- dequeue() {
3817
- return this.shift();
3818
- }
3819
3645
  /**
3820
3646
  * Time Complexity: O(1)
3821
3647
  * Space Complexity: O(1)
@@ -3961,35 +3787,6 @@ var dataStructureTyped = (() => {
3961
3787
  }
3962
3788
  };
3963
3789
  var LinkedListQueue = class _LinkedListQueue extends SinglyLinkedList {
3964
- /**
3965
- * The `get first` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
3966
- * @returns The `get first()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
3967
- */
3968
- get first() {
3969
- var _a;
3970
- return (_a = this.head) == null ? void 0 : _a.value;
3971
- }
3972
- /**
3973
- * The enqueue function adds a value to the end of an array.
3974
- * @param {E} value - The value parameter represents the value that you want to add to the queue.
3975
- */
3976
- enqueue(value) {
3977
- return this.push(value);
3978
- }
3979
- /**
3980
- * The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
3981
- * @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
3982
- */
3983
- dequeue() {
3984
- return this.shift();
3985
- }
3986
- /**
3987
- * The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
3988
- * @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
3989
- */
3990
- peek() {
3991
- return this.first;
3992
- }
3993
3790
  /**
3994
3791
  * Time Complexity: O(n)
3995
3792
  * Space Complexity: O(n)
@@ -4751,67 +4548,6 @@ var dataStructureTyped = (() => {
4751
4548
  }
4752
4549
  return newDeque;
4753
4550
  }
4754
- /**
4755
- * Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
4756
- * Space Complexity: O(n) - Due to potential resizing.
4757
- */
4758
- /**
4759
- * Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
4760
- * Space Complexity: O(n) - Due to potential resizing.
4761
- *
4762
- * The addLast function adds an element to the end of an array.
4763
- * @param {E} element - The element parameter represents the element that you want to add to the end of the
4764
- * data structure.
4765
- */
4766
- addLast(element) {
4767
- return this.push(element);
4768
- }
4769
- /**
4770
- * Time Complexity: O(1)
4771
- * Space Complexity: O(1)
4772
- */
4773
- /**
4774
- * Time Complexity: O(1)
4775
- * Space Complexity: O(1)
4776
- *
4777
- * The function "pollLast" removes and returns the last element of an array.
4778
- * @returns The last element of the array is being returned.
4779
- */
4780
- pollLast() {
4781
- return this.pop();
4782
- }
4783
- /**
4784
- * Time Complexity: O(1)
4785
- * Space Complexity: O(1)
4786
- * /
4787
-
4788
- /**
4789
- * Time Complexity: O(1)
4790
- * Space Complexity: O(1)
4791
- *
4792
- * The "addFirst" function adds an element to the beginning of an array.
4793
- * @param {E} element - The parameter "element" represents the element that you want to add to the
4794
- * beginning of the data structure.
4795
- */
4796
- addFirst(element) {
4797
- return this.unshift(element);
4798
- }
4799
- /**
4800
- * Time Complexity: O(1)
4801
- * Space Complexity: O(1)
4802
- * /
4803
-
4804
- /**
4805
- * Time Complexity: O(1)
4806
- * Space Complexity: O(1)
4807
- *
4808
- * The function "pollFirst" removes and returns the first element of an array.
4809
- * @returns The method `pollFirst()` is returning the first element of the array after removing it
4810
- * from the beginning. If the array is empty, it will return `undefined`.
4811
- */
4812
- pollFirst() {
4813
- return this.shift();
4814
- }
4815
4551
  /**
4816
4552
  * Time Complexity: O(n)
4817
4553
  * Space Complexity: O(1)
@@ -5784,6 +5520,9 @@ var dataStructureTyped = (() => {
5784
5520
  set vertexMap(v) {
5785
5521
  this._vertexMap = v;
5786
5522
  }
5523
+ get size() {
5524
+ return this._vertexMap.size;
5525
+ }
5787
5526
  /**
5788
5527
  * Time Complexity: O(1) - Constant time for Map lookup.
5789
5528
  * Space Complexity: O(1) - Constant space, as it creates only a few variables.
@@ -10221,11 +9960,9 @@ var dataStructureTyped = (() => {
10221
9960
  const compared = this._compare(cur.key, targetKey);
10222
9961
  if (compared === lesserOrGreater)
10223
9962
  ans.push(callback(cur));
10224
- if (!cur.left && !cur.right)
10225
- return;
10226
- if (cur.left && this._compare(cur.left.key, targetKey) === lesserOrGreater)
9963
+ if (this.isRealNode(cur.left))
10227
9964
  _traverse(cur.left);
10228
- if (cur.right && this._compare(cur.right.key, targetKey) === lesserOrGreater)
9965
+ if (this.isRealNode(cur.right))
10229
9966
  _traverse(cur.right);
10230
9967
  };
10231
9968
  _traverse(this.root);
@@ -10234,13 +9971,13 @@ var dataStructureTyped = (() => {
10234
9971
  const queue = new Queue([this.root]);
10235
9972
  while (queue.size > 0) {
10236
9973
  const cur = queue.shift();
10237
- if (cur) {
9974
+ if (this.isRealNode(cur)) {
10238
9975
  const compared = this._compare(cur.key, targetKey);
10239
9976
  if (compared === lesserOrGreater)
10240
9977
  ans.push(callback(cur));
10241
- if (cur.left && this._compare(cur.left.key, targetKey) === lesserOrGreater)
9978
+ if (this.isRealNode(cur.left))
10242
9979
  queue.push(cur.left);
10243
- if (cur.right && this._compare(cur.right.key, targetKey) === lesserOrGreater)
9980
+ if (this.isRealNode(cur.right))
10244
9981
  queue.push(cur.right);
10245
9982
  }
10246
9983
  }
@@ -11847,12 +11584,12 @@ var dataStructureTyped = (() => {
11847
11584
  */
11848
11585
  _fixInsert(k) {
11849
11586
  let u;
11850
- while (k.parent && k.parent.color === 1) {
11587
+ while (k.parent && k.parent.color === 1 /* RED */) {
11851
11588
  if (k.parent.parent && k.parent === k.parent.parent.right) {
11852
11589
  u = k.parent.parent.left;
11853
- if (u && u.color === 1) {
11854
- u.color = 0 /* BLACK */;
11590
+ if (u && u.color === 1 /* RED */) {
11855
11591
  k.parent.color = 0 /* BLACK */;
11592
+ u.color = 0 /* BLACK */;
11856
11593
  k.parent.parent.color = 1 /* RED */;
11857
11594
  k = k.parent.parent;
11858
11595
  } else {
@@ -11860,15 +11597,17 @@ var dataStructureTyped = (() => {
11860
11597
  k = k.parent;
11861
11598
  this._rightRotate(k);
11862
11599
  }
11863
- k.parent.color = 0 /* BLACK */;
11864
- k.parent.parent.color = 1 /* RED */;
11600
+ if (k.parent.color === 1 /* RED */) {
11601
+ k.parent.color = 0 /* BLACK */;
11602
+ k.parent.parent.color = 1 /* RED */;
11603
+ }
11865
11604
  this._leftRotate(k.parent.parent);
11866
11605
  }
11867
11606
  } else {
11868
11607
  u = k.parent.parent.right;
11869
- if (u && u.color === 1) {
11870
- u.color = 0 /* BLACK */;
11608
+ if (u && u.color === 1 /* RED */) {
11871
11609
  k.parent.color = 0 /* BLACK */;
11610
+ u.color = 0 /* BLACK */;
11872
11611
  k.parent.parent.color = 1 /* RED */;
11873
11612
  k = k.parent.parent;
11874
11613
  } else {
@@ -11876,8 +11615,10 @@ var dataStructureTyped = (() => {
11876
11615
  k = k.parent;
11877
11616
  this._leftRotate(k);
11878
11617
  }
11879
- k.parent.color = 0 /* BLACK */;
11880
- k.parent.parent.color = 1 /* RED */;
11618
+ if (k.parent.color === 1 /* RED */) {
11619
+ k.parent.color = 0 /* BLACK */;
11620
+ k.parent.parent.color = 1 /* RED */;
11621
+ }
11881
11622
  this._rightRotate(k.parent.parent);
11882
11623
  }
11883
11624
  }
@@ -11996,8 +11737,8 @@ var dataStructureTyped = (() => {
11996
11737
  }
11997
11738
  };
11998
11739
 
11999
- // src/data-structures/binary-tree/tree-multimap.ts
12000
- var TreeMultimapNode = class extends AVLTreeNode {
11740
+ // src/data-structures/binary-tree/avl-tree-multi-map.ts
11741
+ var AVLTreeMultiMapNode = class extends AVLTreeNode {
12001
11742
  /**
12002
11743
  * The constructor function initializes a BinaryTreeNode object with a key, value, and count.
12003
11744
  * @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
@@ -12029,7 +11770,7 @@ var dataStructureTyped = (() => {
12029
11770
  this._count = value;
12030
11771
  }
12031
11772
  };
12032
- var TreeMultimap = class _TreeMultimap extends AVLTree {
11773
+ var AVLTreeMultiMap = class _AVLTreeMultiMap extends AVLTree {
12033
11774
  constructor(keysOrNodesOrEntries = [], options) {
12034
11775
  super([], options);
12035
11776
  __publicField(this, "_count", 0);
@@ -12057,19 +11798,19 @@ var dataStructureTyped = (() => {
12057
11798
  * @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
12058
11799
  */
12059
11800
  createNode(key, value, count) {
12060
- return new TreeMultimapNode(key, value, count);
11801
+ return new AVLTreeMultiMapNode(key, value, count);
12061
11802
  }
12062
11803
  /**
12063
- * The function creates a new TreeMultimap object with the specified options and returns it.
11804
+ * The function creates a new AVLTreeMultiMap object with the specified options and returns it.
12064
11805
  * @param [options] - The `options` parameter is an optional object that contains additional
12065
- * configuration options for creating the `TreeMultimap` object. It can include properties such as
11806
+ * configuration options for creating the `AVLTreeMultiMap` object. It can include properties such as
12066
11807
  * `iterationType` and `variant`, which are used to specify the type of iteration and the variant of
12067
11808
  * the tree, respectively. These properties can be
12068
- * @returns a new instance of the `TreeMultimap` class, with the provided options merged with the
11809
+ * @returns a new instance of the `AVLTreeMultiMap` class, with the provided options merged with the
12069
11810
  * default options. The returned value is casted as `TREE`.
12070
11811
  */
12071
11812
  createTree(options) {
12072
- return new _TreeMultimap([], __spreadValues({
11813
+ return new _AVLTreeMultiMap([], __spreadValues({
12073
11814
  iterationType: this.iterationType,
12074
11815
  variant: this.variant
12075
11816
  }, options));
@@ -12106,13 +11847,13 @@ var dataStructureTyped = (() => {
12106
11847
  return node;
12107
11848
  }
12108
11849
  /**
12109
- * The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class.
11850
+ * The function checks if an keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode class.
12110
11851
  * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
12111
- * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode
11852
+ * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode
12112
11853
  * class.
12113
11854
  */
12114
11855
  isNode(keyOrNodeOrEntry) {
12115
- return keyOrNodeOrEntry instanceof TreeMultimapNode;
11856
+ return keyOrNodeOrEntry instanceof AVLTreeMultiMapNode;
12116
11857
  }
12117
11858
  /**
12118
11859
  * Time Complexity: O(log n)
@@ -12344,6 +12085,372 @@ var dataStructureTyped = (() => {
12344
12085
  }
12345
12086
  };
12346
12087
 
12088
+ // src/data-structures/binary-tree/tree-multi-map.ts
12089
+ var TreeMultiMapNode = class extends RedBlackTreeNode {
12090
+ /**
12091
+ * The constructor function initializes an instance of a class with a key, value, and count.
12092
+ * @param {K} key - The key parameter is of type K, which represents the type of the key for the
12093
+ * constructor. It is required and must be provided when creating an instance of the class.
12094
+ * @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
12095
+ * value associated with the key in the constructor. If no value is provided, it will be `undefined`.
12096
+ * @param [count=1] - The "count" parameter is an optional parameter that specifies the number of
12097
+ * times the key-value pair should be repeated. If no value is provided for "count", it defaults to
12098
+ * 1.
12099
+ */
12100
+ constructor(key, value, count = 1) {
12101
+ super(key, value);
12102
+ __publicField(this, "_count", 1);
12103
+ this.count = count;
12104
+ }
12105
+ /**
12106
+ * The function returns the value of the private variable _count.
12107
+ * @returns The count property of the object, which is of type number.
12108
+ */
12109
+ get count() {
12110
+ return this._count;
12111
+ }
12112
+ /**
12113
+ * The above function sets the value of the count property.
12114
+ * @param {number} value - The value parameter is of type number, which means it can accept any
12115
+ * numeric value.
12116
+ */
12117
+ set count(value) {
12118
+ this._count = value;
12119
+ }
12120
+ };
12121
+ var TreeMultiMap = class _TreeMultiMap extends RedBlackTree {
12122
+ /**
12123
+ * The constructor function initializes a new instance of the TreeMultiMap class with optional
12124
+ * initial keys, nodes, or entries.
12125
+ * @param keysOrNodesOrEntries - The `keysOrNodesOrEntries` parameter is an iterable object that can
12126
+ * contain keys, nodes, or entries. It is used to initialize the TreeMultiMap with the provided keys,
12127
+ * nodes, or entries.
12128
+ * @param [options] - The `options` parameter is an optional object that can be passed to the
12129
+ * constructor. It allows you to customize the behavior of the `TreeMultiMap` instance.
12130
+ */
12131
+ constructor(keysOrNodesOrEntries = [], options) {
12132
+ super([], options);
12133
+ __publicField(this, "_count", 0);
12134
+ if (keysOrNodesOrEntries)
12135
+ this.addMany(keysOrNodesOrEntries);
12136
+ }
12137
+ // TODO the _count is not accurate after nodes count modified
12138
+ /**
12139
+ * The function calculates the sum of the count property of all nodes in a tree structure.
12140
+ * @returns the sum of the count property of all nodes in the tree.
12141
+ */
12142
+ get count() {
12143
+ let sum = 0;
12144
+ this.dfs((node) => sum += node.count);
12145
+ return sum;
12146
+ }
12147
+ /**
12148
+ * The function creates a new TreeMultiMapNode object with the specified key, value, and count.
12149
+ * @param {K} key - The key parameter represents the key of the node being created. It is of type K,
12150
+ * which is a generic type that can be replaced with any specific type when using the function.
12151
+ * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
12152
+ * associated with the key in the node. It is of type `V`, which can be any data type.
12153
+ * @param {number} [count] - The `count` parameter represents the number of occurrences of a
12154
+ * key-value pair in the TreeMultiMap. It is an optional parameter, so if it is not provided, it will
12155
+ * default to 1.
12156
+ * @returns a new instance of the TreeMultiMapNode class, casted as NODE.
12157
+ */
12158
+ createNode(key, value, count) {
12159
+ return new TreeMultiMapNode(key, value, count);
12160
+ }
12161
+ /**
12162
+ * The function creates a new instance of a TreeMultiMap with the specified options and returns it.
12163
+ * @param [options] - The `options` parameter is an optional object that contains additional
12164
+ * configuration options for creating the `TreeMultiMap`. It can include properties such as
12165
+ * `keyComparator`, `valueComparator`, `allowDuplicates`, etc.
12166
+ * @returns a new instance of the `TreeMultiMap` class, with the provided options merged with the
12167
+ * existing `iterationType` option. The returned value is casted as `TREE`.
12168
+ */
12169
+ createTree(options) {
12170
+ return new _TreeMultiMap([], __spreadValues({
12171
+ iterationType: this.iterationType
12172
+ }, options));
12173
+ }
12174
+ /**
12175
+ * The function `keyValueOrEntryToNode` takes a key, value, and count and returns a node if the input
12176
+ * is valid.
12177
+ * @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be of type `KeyOrNodeOrEntry<K, V,
12178
+ * NODE>`. It can accept three types of values:
12179
+ * @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
12180
+ * value associated with a key in a key-value pair.
12181
+ * @param [count=1] - The count parameter is an optional parameter that specifies the number of times
12182
+ * the key-value pair should be added to the node. If not provided, it defaults to 1.
12183
+ * @returns a NODE object or undefined.
12184
+ */
12185
+ keyValueOrEntryToNode(keyOrNodeOrEntry, value, count = 1) {
12186
+ let node;
12187
+ if (keyOrNodeOrEntry === void 0 || keyOrNodeOrEntry === null) {
12188
+ return;
12189
+ } else if (this.isNode(keyOrNodeOrEntry)) {
12190
+ node = keyOrNodeOrEntry;
12191
+ } else if (this.isEntry(keyOrNodeOrEntry)) {
12192
+ const [key, value2] = keyOrNodeOrEntry;
12193
+ if (key === void 0 || key === null) {
12194
+ return;
12195
+ } else {
12196
+ node = this.createNode(key, value2, count);
12197
+ }
12198
+ } else if (!this.isNode(keyOrNodeOrEntry)) {
12199
+ node = this.createNode(keyOrNodeOrEntry, value, count);
12200
+ } else {
12201
+ return;
12202
+ }
12203
+ return node;
12204
+ }
12205
+ /**
12206
+ * The function "isNode" checks if a given key, node, or entry is an instance of the TreeMultiMapNode
12207
+ * class.
12208
+ * @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be of type `KeyOrNodeOrEntry<K, V,
12209
+ * NODE>`.
12210
+ * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntry` is an instance
12211
+ * of the `TreeMultiMapNode` class.
12212
+ */
12213
+ isNode(keyOrNodeOrEntry) {
12214
+ return keyOrNodeOrEntry instanceof TreeMultiMapNode;
12215
+ }
12216
+ /**
12217
+ * Time Complexity: O(log n)
12218
+ * Space Complexity: O(1)
12219
+ */
12220
+ /**
12221
+ * Time Complexity: O(log n)
12222
+ * Space Complexity: O(1)
12223
+ *
12224
+ * The function overrides the add method in TypeScript and adds a new node to the data structure.
12225
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can accept three types of values:
12226
+ * @param {V} [value] - The `value` parameter represents the value associated with the key in the
12227
+ * data structure.
12228
+ * @param [count=1] - The `count` parameter represents the number of times the key-value pair should
12229
+ * be added to the data structure. By default, it is set to 1, meaning that the key-value pair will
12230
+ * be added once. However, you can specify a different value for `count` if you want to add
12231
+ * @returns a boolean value.
12232
+ */
12233
+ add(keyOrNodeOrEntry, value, count = 1) {
12234
+ const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value, count);
12235
+ if (newNode === void 0)
12236
+ return false;
12237
+ const orgNodeCount = (newNode == null ? void 0 : newNode.count) || 0;
12238
+ const inserted = super.add(newNode);
12239
+ if (inserted) {
12240
+ this._count += orgNodeCount;
12241
+ }
12242
+ return true;
12243
+ }
12244
+ /**
12245
+ * Time Complexity: O(log n)
12246
+ * Space Complexity: O(1)
12247
+ */
12248
+ /**
12249
+ * Time Complexity: O(log n)
12250
+ * Space Complexity: O(1)
12251
+ *
12252
+ * The `delete` function in a TypeScript class is used to delete nodes from a binary tree based on a
12253
+ * given identifier, and it returns an array of results containing information about the deleted
12254
+ * nodes.
12255
+ * @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value used
12256
+ * to identify the node to be deleted. It can be of any type that is returned by the callback
12257
+ * function. It can also be null or undefined if no node needs to be deleted.
12258
+ * @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
12259
+ * input and returns a value of type `ReturnType<C>`. It is used to determine if a node matches the
12260
+ * identifier for deletion. If no callback is provided, the `_defaultOneParamCallback` function is
12261
+ * used
12262
+ * @param [ignoreCount=false] - A boolean value indicating whether to ignore the count of the target
12263
+ * node when performing deletion. If set to true, the count of the target node will not be considered
12264
+ * and the node will be deleted regardless of its count. If set to false (default), the count of the
12265
+ * target node will be decremented
12266
+ * @returns an array of BinaryTreeDeleteResult<NODE> objects.
12267
+ */
12268
+ delete(identifier, callback = this._defaultOneParamCallback, ignoreCount = false) {
12269
+ const deleteResults = [];
12270
+ if (identifier === null)
12271
+ return deleteResults;
12272
+ const deleteHelper = (node) => {
12273
+ let targetNode = this._Sentinel;
12274
+ let currentNode;
12275
+ while (node !== this._Sentinel) {
12276
+ if (node && callback(node) === identifier) {
12277
+ targetNode = node;
12278
+ }
12279
+ if (node && identifier && callback(node) <= identifier) {
12280
+ node = node.right;
12281
+ } else {
12282
+ node = node == null ? void 0 : node.left;
12283
+ }
12284
+ }
12285
+ if (targetNode === this._Sentinel) {
12286
+ return;
12287
+ }
12288
+ if (ignoreCount || targetNode.count <= 1) {
12289
+ let parentNode = targetNode;
12290
+ let parentNodeOriginalColor = parentNode.color;
12291
+ if (targetNode.left === this._Sentinel) {
12292
+ currentNode = targetNode.right;
12293
+ this._rbTransplant(targetNode, targetNode.right);
12294
+ } else if (targetNode.right === this._Sentinel) {
12295
+ currentNode = targetNode.left;
12296
+ this._rbTransplant(targetNode, targetNode.left);
12297
+ } else {
12298
+ parentNode = this.getLeftMost(targetNode.right);
12299
+ parentNodeOriginalColor = parentNode.color;
12300
+ currentNode = parentNode.right;
12301
+ if (parentNode.parent === targetNode) {
12302
+ currentNode.parent = parentNode;
12303
+ } else {
12304
+ this._rbTransplant(parentNode, parentNode.right);
12305
+ parentNode.right = targetNode.right;
12306
+ parentNode.right.parent = parentNode;
12307
+ }
12308
+ this._rbTransplant(targetNode, parentNode);
12309
+ parentNode.left = targetNode.left;
12310
+ parentNode.left.parent = parentNode;
12311
+ parentNode.color = targetNode.color;
12312
+ }
12313
+ if (parentNodeOriginalColor === 0 /* BLACK */) {
12314
+ this._fixDelete(currentNode);
12315
+ }
12316
+ this._size--;
12317
+ this._count -= targetNode.count;
12318
+ deleteResults.push({ deleted: targetNode, needBalanced: void 0 });
12319
+ } else {
12320
+ targetNode.count--;
12321
+ this._count--;
12322
+ }
12323
+ };
12324
+ deleteHelper(this.root);
12325
+ return deleteResults;
12326
+ }
12327
+ /**
12328
+ * Time Complexity: O(1)
12329
+ * Space Complexity: O(1)
12330
+ */
12331
+ /**
12332
+ * Time Complexity: O(1)
12333
+ * Space Complexity: O(1)
12334
+ *
12335
+ * The "clear" function overrides the parent class's "clear" function and also resets the count to
12336
+ * zero.
12337
+ */
12338
+ clear() {
12339
+ super.clear();
12340
+ this._count = 0;
12341
+ }
12342
+ /**
12343
+ * Time Complexity: O(n log n)
12344
+ * Space Complexity: O(log n)
12345
+ */
12346
+ /**
12347
+ * Time Complexity: O(n log n)
12348
+ * Space Complexity: O(log n)
12349
+ *
12350
+ * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
12351
+ * tree using either a recursive or iterative approach.
12352
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
12353
+ * type of iteration to use when building the balanced binary search tree. It can have two possible
12354
+ * values:
12355
+ * @returns a boolean value.
12356
+ */
12357
+ perfectlyBalance(iterationType = this.iterationType) {
12358
+ const sorted = this.dfs((node) => node, "in"), n = sorted.length;
12359
+ if (sorted.length < 1)
12360
+ return false;
12361
+ this.clear();
12362
+ if (iterationType === "RECURSIVE" /* RECURSIVE */) {
12363
+ const buildBalanceBST = (l, r) => {
12364
+ if (l > r)
12365
+ return;
12366
+ const m = l + Math.floor((r - l) / 2);
12367
+ const midNode = sorted[m];
12368
+ this.add(midNode.key, midNode.value, midNode.count);
12369
+ buildBalanceBST(l, m - 1);
12370
+ buildBalanceBST(m + 1, r);
12371
+ };
12372
+ buildBalanceBST(0, n - 1);
12373
+ return true;
12374
+ } else {
12375
+ const stack = [[0, n - 1]];
12376
+ while (stack.length > 0) {
12377
+ const popped = stack.pop();
12378
+ if (popped) {
12379
+ const [l, r] = popped;
12380
+ if (l <= r) {
12381
+ const m = l + Math.floor((r - l) / 2);
12382
+ const midNode = sorted[m];
12383
+ this.add(midNode.key, midNode.value, midNode.count);
12384
+ stack.push([m + 1, r]);
12385
+ stack.push([l, m - 1]);
12386
+ }
12387
+ }
12388
+ }
12389
+ return true;
12390
+ }
12391
+ }
12392
+ /**
12393
+ * Time complexity: O(n)
12394
+ * Space complexity: O(n)
12395
+ */
12396
+ /**
12397
+ * Time complexity: O(n)
12398
+ * Space complexity: O(n)
12399
+ *
12400
+ * The function overrides the clone method to create a deep copy of a tree object.
12401
+ * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
12402
+ */
12403
+ clone() {
12404
+ const cloned = this.createTree();
12405
+ this.bfs((node) => cloned.add(node.key, node.value, node.count));
12406
+ return cloned;
12407
+ }
12408
+ /**
12409
+ * The function swaps the properties of two nodes in a binary search tree.
12410
+ * @param srcNode - The source node that needs to be swapped with the destination node. It can be
12411
+ * either a key or a node object.
12412
+ * @param destNode - The `destNode` parameter is the node in the binary search tree where the
12413
+ * properties will be swapped with the `srcNode`.
12414
+ * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
12415
+ * If both `srcNode` and `destNode` are valid nodes, the method swaps their `key`, `value`, `count`,
12416
+ * and `color` properties. If the swapping is successful, the method returns the modified `destNode`.
12417
+ * If either `srcNode` or `destNode` is
12418
+ */
12419
+ _swapProperties(srcNode, destNode) {
12420
+ srcNode = this.ensureNode(srcNode);
12421
+ destNode = this.ensureNode(destNode);
12422
+ if (srcNode && destNode) {
12423
+ const { key, value, count, color } = destNode;
12424
+ const tempNode = this.createNode(key, value, count);
12425
+ if (tempNode) {
12426
+ tempNode.color = color;
12427
+ destNode.key = srcNode.key;
12428
+ destNode.value = srcNode.value;
12429
+ destNode.count = srcNode.count;
12430
+ destNode.color = srcNode.color;
12431
+ srcNode.key = tempNode.key;
12432
+ srcNode.value = tempNode.value;
12433
+ srcNode.count = tempNode.count;
12434
+ srcNode.color = tempNode.color;
12435
+ }
12436
+ return destNode;
12437
+ }
12438
+ return void 0;
12439
+ }
12440
+ /**
12441
+ * The function replaces an old node with a new node and updates the count property of the new node.
12442
+ * @param {NODE} oldNode - The `oldNode` parameter is of type `NODE` and represents the node that
12443
+ * needs to be replaced in the data structure.
12444
+ * @param {NODE} newNode - The `newNode` parameter is an object of type `NODE`.
12445
+ * @returns The method is returning the result of calling the `_replaceNode` method from the
12446
+ * superclass, after updating the `count` property of the `newNode` object.
12447
+ */
12448
+ _replaceNode(oldNode, newNode) {
12449
+ newNode.count = oldNode.count + newNode.count;
12450
+ return super._replaceNode(oldNode, newNode);
12451
+ }
12452
+ };
12453
+
12347
12454
  // src/data-structures/tree/tree.ts
12348
12455
  var TreeNode = class _TreeNode {
12349
12456
  /**