data-structure-typed 1.50.1 → 1.50.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (196) hide show
  1. package/CHANGELOG.md +2 -1
  2. package/README.md +30 -28
  3. package/README_zh-CN.md +1 -1
  4. package/SPECIFICATION.md +56 -0
  5. package/SPONSOR-zh-CN.md +90 -0
  6. package/SPONSOR.md +54 -0
  7. package/benchmark/report.html +13 -13
  8. package/benchmark/report.json +149 -161
  9. package/dist/cjs/data-structures/base/iterable-base.d.ts +120 -9
  10. package/dist/cjs/data-structures/base/iterable-base.js +143 -7
  11. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
  12. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +72 -47
  13. package/dist/cjs/data-structures/binary-tree/avl-tree.js +101 -72
  14. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  15. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
  16. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
  17. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
  19. package/dist/cjs/data-structures/binary-tree/binary-tree.js +484 -376
  20. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  21. package/dist/cjs/data-structures/binary-tree/bst.d.ts +92 -79
  22. package/dist/cjs/data-structures/binary-tree/bst.js +68 -76
  23. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  24. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
  25. package/dist/cjs/data-structures/binary-tree/rb-tree.js +152 -99
  26. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  27. package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
  28. package/dist/cjs/data-structures/binary-tree/segment-tree.js +127 -10
  29. package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
  30. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
  31. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +102 -85
  32. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  33. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -78
  34. package/dist/cjs/data-structures/graph/abstract-graph.js +3 -189
  35. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  36. package/dist/cjs/data-structures/graph/directed-graph.d.ts +73 -0
  37. package/dist/cjs/data-structures/graph/directed-graph.js +131 -0
  38. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  39. package/dist/cjs/data-structures/graph/map-graph.d.ts +8 -0
  40. package/dist/cjs/data-structures/graph/map-graph.js +14 -0
  41. package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
  42. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +76 -7
  43. package/dist/cjs/data-structures/graph/undirected-graph.js +151 -18
  44. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  45. package/dist/cjs/data-structures/hash/hash-map.d.ts +254 -28
  46. package/dist/cjs/data-structures/hash/hash-map.js +347 -78
  47. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  48. package/dist/cjs/data-structures/heap/heap.d.ts +95 -25
  49. package/dist/cjs/data-structures/heap/heap.js +95 -26
  50. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  51. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
  52. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +141 -77
  53. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  54. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +154 -106
  55. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +164 -115
  56. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  57. package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
  58. package/dist/cjs/data-structures/linked-list/skip-linked-list.js +63 -36
  59. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
  60. package/dist/cjs/data-structures/matrix/matrix.d.ts +35 -4
  61. package/dist/cjs/data-structures/matrix/matrix.js +50 -11
  62. package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
  63. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
  64. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +10 -0
  65. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  66. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
  67. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +11 -0
  68. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  69. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
  70. package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -0
  71. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  72. package/dist/cjs/data-structures/queue/deque.d.ts +139 -35
  73. package/dist/cjs/data-structures/queue/deque.js +200 -62
  74. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  75. package/dist/cjs/data-structures/queue/queue.d.ts +103 -49
  76. package/dist/cjs/data-structures/queue/queue.js +111 -49
  77. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  78. package/dist/cjs/data-structures/stack/stack.d.ts +51 -21
  79. package/dist/cjs/data-structures/stack/stack.js +58 -22
  80. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  81. package/dist/cjs/data-structures/tree/tree.d.ts +57 -3
  82. package/dist/cjs/data-structures/tree/tree.js +77 -11
  83. package/dist/cjs/data-structures/tree/tree.js.map +1 -1
  84. package/dist/cjs/data-structures/trie/trie.d.ts +135 -34
  85. package/dist/cjs/data-structures/trie/trie.js +153 -33
  86. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  87. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  88. package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -3
  89. package/dist/cjs/types/utils/utils.d.ts +1 -0
  90. package/dist/mjs/data-structures/base/iterable-base.d.ts +120 -9
  91. package/dist/mjs/data-structures/base/iterable-base.js +143 -7
  92. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +72 -47
  93. package/dist/mjs/data-structures/binary-tree/avl-tree.js +102 -73
  94. package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
  95. package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
  96. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
  97. package/dist/mjs/data-structures/binary-tree/binary-tree.js +483 -375
  98. package/dist/mjs/data-structures/binary-tree/bst.d.ts +92 -79
  99. package/dist/mjs/data-structures/binary-tree/bst.js +68 -76
  100. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
  101. package/dist/mjs/data-structures/binary-tree/rb-tree.js +153 -100
  102. package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
  103. package/dist/mjs/data-structures/binary-tree/segment-tree.js +127 -10
  104. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
  105. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +102 -86
  106. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -78
  107. package/dist/mjs/data-structures/graph/abstract-graph.js +3 -188
  108. package/dist/mjs/data-structures/graph/directed-graph.d.ts +73 -0
  109. package/dist/mjs/data-structures/graph/directed-graph.js +131 -0
  110. package/dist/mjs/data-structures/graph/map-graph.d.ts +8 -0
  111. package/dist/mjs/data-structures/graph/map-graph.js +14 -0
  112. package/dist/mjs/data-structures/graph/undirected-graph.d.ts +76 -7
  113. package/dist/mjs/data-structures/graph/undirected-graph.js +152 -19
  114. package/dist/mjs/data-structures/hash/hash-map.d.ts +254 -28
  115. package/dist/mjs/data-structures/hash/hash-map.js +353 -84
  116. package/dist/mjs/data-structures/heap/heap.d.ts +95 -25
  117. package/dist/mjs/data-structures/heap/heap.js +95 -26
  118. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
  119. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +144 -80
  120. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +154 -106
  121. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +166 -117
  122. package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
  123. package/dist/mjs/data-structures/linked-list/skip-linked-list.js +63 -36
  124. package/dist/mjs/data-structures/matrix/matrix.d.ts +35 -4
  125. package/dist/mjs/data-structures/matrix/matrix.js +50 -11
  126. package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
  127. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +10 -0
  128. package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
  129. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +11 -0
  130. package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
  131. package/dist/mjs/data-structures/priority-queue/priority-queue.js +8 -0
  132. package/dist/mjs/data-structures/queue/deque.d.ts +139 -35
  133. package/dist/mjs/data-structures/queue/deque.js +205 -67
  134. package/dist/mjs/data-structures/queue/queue.d.ts +103 -49
  135. package/dist/mjs/data-structures/queue/queue.js +111 -49
  136. package/dist/mjs/data-structures/stack/stack.d.ts +51 -21
  137. package/dist/mjs/data-structures/stack/stack.js +58 -22
  138. package/dist/mjs/data-structures/tree/tree.d.ts +57 -3
  139. package/dist/mjs/data-structures/tree/tree.js +80 -14
  140. package/dist/mjs/data-structures/trie/trie.d.ts +135 -34
  141. package/dist/mjs/data-structures/trie/trie.js +156 -36
  142. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  143. package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -3
  144. package/dist/mjs/types/utils/utils.d.ts +1 -0
  145. package/dist/umd/data-structure-typed.js +3121 -1583
  146. package/dist/umd/data-structure-typed.min.js +3 -3
  147. package/dist/umd/data-structure-typed.min.js.map +1 -1
  148. package/package.json +61 -55
  149. package/src/data-structures/base/iterable-base.ts +184 -19
  150. package/src/data-structures/binary-tree/avl-tree.ts +134 -100
  151. package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
  152. package/src/data-structures/binary-tree/binary-tree.ts +674 -671
  153. package/src/data-structures/binary-tree/bst.ts +127 -136
  154. package/src/data-structures/binary-tree/rb-tree.ts +199 -166
  155. package/src/data-structures/binary-tree/segment-tree.ts +145 -11
  156. package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
  157. package/src/data-structures/graph/abstract-graph.ts +4 -211
  158. package/src/data-structures/graph/directed-graph.ts +152 -0
  159. package/src/data-structures/graph/map-graph.ts +15 -0
  160. package/src/data-structures/graph/undirected-graph.ts +171 -19
  161. package/src/data-structures/hash/hash-map.ts +389 -96
  162. package/src/data-structures/heap/heap.ts +97 -26
  163. package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
  164. package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
  165. package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
  166. package/src/data-structures/matrix/matrix.ts +52 -12
  167. package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
  168. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
  169. package/src/data-structures/priority-queue/priority-queue.ts +8 -0
  170. package/src/data-structures/queue/deque.ts +225 -70
  171. package/src/data-structures/queue/queue.ts +118 -49
  172. package/src/data-structures/stack/stack.ts +63 -23
  173. package/src/data-structures/tree/tree.ts +89 -15
  174. package/src/data-structures/trie/trie.ts +173 -38
  175. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  176. package/src/types/data-structures/hash/hash-map.ts +4 -3
  177. package/src/types/utils/utils.ts +2 -0
  178. package/test/performance/data-structures/graph/directed-graph.test.ts +4 -4
  179. package/test/performance/data-structures/queue/deque.test.ts +26 -25
  180. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +37 -0
  181. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +46 -17
  182. package/test/unit/data-structures/binary-tree/bst.test.ts +65 -1
  183. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +38 -1
  184. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -32
  185. package/test/unit/data-structures/graph/abstract-graph.test.ts +11 -0
  186. package/test/unit/data-structures/graph/directed-graph.test.ts +190 -5
  187. package/test/unit/data-structures/graph/undirected-graph.test.ts +263 -18
  188. package/test/unit/data-structures/hash/hash-map.test.ts +376 -353
  189. package/test/unit/data-structures/heap/heap.test.ts +18 -1
  190. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +24 -5
  191. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +20 -2
  192. package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
  193. package/test/unit/data-structures/queue/deque.test.ts +65 -5
  194. package/test/unit/data-structures/queue/queue.test.ts +24 -5
  195. package/test/unit/data-structures/stack/stack.test.ts +17 -0
  196. package/test/unit/data-structures/trie/trie.test.ts +17 -0
@@ -16,8 +16,24 @@ class TreeMultimapNode extends avl_tree_1.AVLTreeNode {
16
16
  */
17
17
  constructor(key, value, count = 1) {
18
18
  super(key, value);
19
+ this._count = 1;
19
20
  this.count = count;
20
21
  }
22
+ /**
23
+ * The function returns the value of the protected variable _count.
24
+ * @returns The count property of the object, which is of type number.
25
+ */
26
+ get count() {
27
+ return this._count;
28
+ }
29
+ /**
30
+ * The above function sets the value of the count property.
31
+ * @param {number} value - The value parameter is of type number, which means it can accept any
32
+ * numeric value.
33
+ */
34
+ set count(value) {
35
+ this._count = value;
36
+ }
21
37
  }
22
38
  exports.TreeMultimapNode = TreeMultimapNode;
23
39
  /**
@@ -31,6 +47,11 @@ class TreeMultimap extends avl_tree_1.AVLTree {
31
47
  this.addMany(keysOrNodesOrEntries);
32
48
  }
33
49
  // TODO the _count is not accurate after nodes count modified
50
+ /**
51
+ * The function calculates the sum of the count property of all nodes in a tree using depth-first
52
+ * search.
53
+ * @returns the sum of the count property of all nodes in the tree.
54
+ */
34
55
  get count() {
35
56
  let sum = 0;
36
57
  this.dfs(node => (sum += node.count));
@@ -40,7 +61,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
40
61
  * The function creates a new BSTNode with the given key, value, and count.
41
62
  * @param {K} key - The key parameter is the unique identifier for the binary tree node. It is used to
42
63
  * distinguish one node from another in the tree.
43
- * @param {N} value - The `value` parameter represents the value that will be stored in the binary search tree node.
64
+ * @param {NODE} value - The `value` parameter represents the value that will be stored in the binary search tree node.
44
65
  * @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
45
66
  * occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
46
67
  * @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
@@ -48,21 +69,30 @@ class TreeMultimap extends avl_tree_1.AVLTree {
48
69
  createNode(key, value, count) {
49
70
  return new TreeMultimapNode(key, value, count);
50
71
  }
72
+ /**
73
+ * The function creates a new TreeMultimap object with the specified options and returns it.
74
+ * @param [options] - The `options` parameter is an optional object that contains additional
75
+ * configuration options for creating the `TreeMultimap` object. It can include properties such as
76
+ * `iterationType` and `variant`, which are used to specify the type of iteration and the variant of
77
+ * the tree, respectively. These properties can be
78
+ * @returns a new instance of the `TreeMultimap` class, with the provided options merged with the
79
+ * default options. The returned value is casted as `TREE`.
80
+ */
51
81
  createTree(options) {
52
82
  return new TreeMultimap([], Object.assign({ iterationType: this.iterationType, variant: this.variant }, options));
53
83
  }
54
84
  /**
55
- * The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
56
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, which means it
85
+ * The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object.
86
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, which means it
57
87
  * can be one of the following:
58
88
  * @param {V} [value] - The `value` parameter is an optional argument that represents the value
59
89
  * associated with the node. It is of type `V`, which can be any data type. If no value is provided,
60
90
  * it defaults to `undefined`.
61
91
  * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
62
92
  * times the value should be added to the node. If not provided, it defaults to 1.
63
- * @returns a node of type `N` or `undefined`.
93
+ * @returns a node of type `NODE` or `undefined`.
64
94
  */
65
- exemplarToNode(keyOrNodeOrEntry, value, count = 1) {
95
+ keyValueOrEntryToNode(keyOrNodeOrEntry, value, count = 1) {
66
96
  let node;
67
97
  if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
68
98
  return;
@@ -89,7 +119,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
89
119
  }
90
120
  /**
91
121
  * The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class.
92
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
122
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
93
123
  * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode
94
124
  * class.
95
125
  */
@@ -99,7 +129,6 @@ class TreeMultimap extends avl_tree_1.AVLTree {
99
129
  /**
100
130
  * Time Complexity: O(log n)
101
131
  * Space Complexity: O(1)
102
- * logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
103
132
  */
104
133
  /**
105
134
  * Time Complexity: O(log n)
@@ -118,7 +147,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
118
147
  * was not successful.
119
148
  */
120
149
  add(keyOrNodeOrEntry, value, count = 1) {
121
- const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count);
150
+ const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value, count);
122
151
  if (newNode === undefined)
123
152
  return false;
124
153
  const orgNodeCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
@@ -129,82 +158,11 @@ class TreeMultimap extends avl_tree_1.AVLTree {
129
158
  return true;
130
159
  }
131
160
  /**
132
- * Time Complexity: O(k log n)
133
- * Space Complexity: O(1)
134
- * logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
135
- */
136
- /**
137
- * Time Complexity: O(k log n)
138
- * Space Complexity: O(1)
139
- *
140
- * The function overrides the addMany method to add multiple keys, nodes, or entries to a data
141
- * structure.
142
- * @param keysOrNodesOrEntries - The parameter `keysOrNodesOrEntries` is an iterable that can contain
143
- * either keys, nodes, or entries.
144
- * @returns The method is returning an array of type `N | undefined`.
145
- */
146
- addMany(keysOrNodesOrEntries) {
147
- return super.addMany(keysOrNodesOrEntries);
148
- }
149
- /**
150
- * Time Complexity: O(n log n)
151
- * Space Complexity: O(n)
152
- * logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node. linear space, as it creates an array to store the sorted nodes.
153
- */
154
- /**
155
- * Time Complexity: O(n log n)
156
- * Space Complexity: O(n)
157
- *
158
- * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
159
- * tree using either a recursive or iterative approach.
160
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
161
- * type of iteration to use when building the balanced binary search tree. It can have two possible
162
- * values:
163
- * @returns a boolean value.
164
- */
165
- perfectlyBalance(iterationType = this.iterationType) {
166
- const sorted = this.dfs(node => node, 'in'), n = sorted.length;
167
- if (sorted.length < 1)
168
- return false;
169
- this.clear();
170
- if (iterationType === types_1.IterationType.RECURSIVE) {
171
- const buildBalanceBST = (l, r) => {
172
- if (l > r)
173
- return;
174
- const m = l + Math.floor((r - l) / 2);
175
- const midNode = sorted[m];
176
- this.add(midNode.key, midNode.value, midNode.count);
177
- buildBalanceBST(l, m - 1);
178
- buildBalanceBST(m + 1, r);
179
- };
180
- buildBalanceBST(0, n - 1);
181
- return true;
182
- }
183
- else {
184
- const stack = [[0, n - 1]];
185
- while (stack.length > 0) {
186
- const popped = stack.pop();
187
- if (popped) {
188
- const [l, r] = popped;
189
- if (l <= r) {
190
- const m = l + Math.floor((r - l) / 2);
191
- const midNode = sorted[m];
192
- this.add(midNode.key, midNode.value, midNode.count);
193
- stack.push([m + 1, r]);
194
- stack.push([l, m - 1]);
195
- }
196
- }
197
- }
198
- return true;
199
- }
200
- }
201
- /**
202
- * Time Complexity: O(k log n)
161
+ * Time Complexity: O(log n)
203
162
  * Space Complexity: O(1)
204
- * logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each. constant space, as it doesn't use additional data structures that scale with input size.
205
163
  */
206
164
  /**
207
- * Time Complexity: O(k log n)
165
+ * Time Complexity: O(log n)
208
166
  * Space Complexity: O(1)
209
167
  *
210
168
  * The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
@@ -220,7 +178,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
220
178
  * being deleted. If set to true, the count of the node will not be considered and the node will be
221
179
  * deleted regardless of its count. If set to false (default), the count of the node will be
222
180
  * decremented by 1 and
223
- * @returns an array of `BinaryTreeDeleteResult<N>`.
181
+ * @returns an array of `BinaryTreeDeleteResult<NODE>`.
224
182
  */
225
183
  delete(identifier, callback = this._defaultOneParamCallback, ignoreCount = false) {
226
184
  var _a;
@@ -294,6 +252,57 @@ class TreeMultimap extends avl_tree_1.AVLTree {
294
252
  super.clear();
295
253
  this._count = 0;
296
254
  }
255
+ /**
256
+ * Time Complexity: O(n log n)
257
+ * Space Complexity: O(log n)
258
+ */
259
+ /**
260
+ * Time Complexity: O(n log n)
261
+ * Space Complexity: O(log n)
262
+ *
263
+ * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
264
+ * tree using either a recursive or iterative approach.
265
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
266
+ * type of iteration to use when building the balanced binary search tree. It can have two possible
267
+ * values:
268
+ * @returns a boolean value.
269
+ */
270
+ perfectlyBalance(iterationType = this.iterationType) {
271
+ const sorted = this.dfs(node => node, 'in'), n = sorted.length;
272
+ if (sorted.length < 1)
273
+ return false;
274
+ this.clear();
275
+ if (iterationType === types_1.IterationType.RECURSIVE) {
276
+ const buildBalanceBST = (l, r) => {
277
+ if (l > r)
278
+ return;
279
+ const m = l + Math.floor((r - l) / 2);
280
+ const midNode = sorted[m];
281
+ this.add(midNode.key, midNode.value, midNode.count);
282
+ buildBalanceBST(l, m - 1);
283
+ buildBalanceBST(m + 1, r);
284
+ };
285
+ buildBalanceBST(0, n - 1);
286
+ return true;
287
+ }
288
+ else {
289
+ const stack = [[0, n - 1]];
290
+ while (stack.length > 0) {
291
+ const popped = stack.pop();
292
+ if (popped) {
293
+ const [l, r] = popped;
294
+ if (l <= r) {
295
+ const m = l + Math.floor((r - l) / 2);
296
+ const midNode = sorted[m];
297
+ this.add(midNode.key, midNode.value, midNode.count);
298
+ stack.push([m + 1, r]);
299
+ stack.push([l, m - 1]);
300
+ }
301
+ }
302
+ }
303
+ return true;
304
+ }
305
+ }
297
306
  /**
298
307
  * Time complexity: O(n)
299
308
  * Space complexity: O(n)
@@ -312,9 +321,9 @@ class TreeMultimap extends avl_tree_1.AVLTree {
312
321
  }
313
322
  /**
314
323
  * The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
315
- * @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node from
316
- * which the values will be swapped. It can be of type `K`, `N`, or `undefined`.
317
- * @param {K | N | undefined} destNode - The `destNode` parameter represents the destination
324
+ * @param {K | NODE | undefined} srcNode - The `srcNode` parameter represents the source node from
325
+ * which the values will be swapped. It can be of type `K`, `NODE`, or `undefined`.
326
+ * @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
318
327
  * node where the values from the source node will be swapped to.
319
328
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
320
329
  * if either `srcNode` or `destNode` is undefined.
@@ -340,6 +349,14 @@ class TreeMultimap extends avl_tree_1.AVLTree {
340
349
  }
341
350
  return undefined;
342
351
  }
352
+ /**
353
+ * The function replaces an old node with a new node and updates the count property of the new node.
354
+ * @param {NODE} oldNode - The `oldNode` parameter is of type `NODE` and represents the node that
355
+ * needs to be replaced in a data structure.
356
+ * @param {NODE} newNode - The `newNode` parameter is an object of type `NODE`.
357
+ * @returns The method is returning the result of calling the `_replaceNode` method from the
358
+ * superclass, after updating the `count` property of the `newNode` object.
359
+ */
343
360
  _replaceNode(oldNode, newNode) {
344
361
  newNode.count = oldNode.count + newNode.count;
345
362
  return super._replaceNode(oldNode, newNode);
@@ -1 +1 @@
1
- {"version":3,"file":"tree-multimap.js","sourceRoot":"","sources":["../../../../src/data-structures/binary-tree/tree-multimap.ts"],"names":[],"mappings":";;;AAgBA,uCAA4D;AAE5D,yCAAkD;AAElD,MAAa,gBAIX,SAAQ,sBAAoB;IAG5B;;;;;;;;;OASG;IACH,YAAY,GAAM,EAAE,KAAS,EAAE,KAAK,GAAG,CAAC;QACtC,KAAK,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAClB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;CACF;AArBD,4CAqBC;AAED;;GAEG;AACH,MAAa,YAMX,SAAQ,kBAAsB;IAE9B,YAAY,uBAA4D,EAAE,EAAE,OAAgC;QAC1G,KAAK,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC;QAIb,WAAM,GAAG,CAAC,CAAC;QAHjB,IAAI,oBAAoB;YAAE,IAAI,CAAC,OAAO,CAAC,oBAAoB,CAAC,CAAC;IAC/D,CAAC;IAID,6DAA6D;IAC7D,IAAI,KAAK;QACP,IAAI,GAAG,GAAG,CAAC,CAAC;QACZ,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,GA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AC,UAAU,CAAC,GAAG,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;YACpD,IAAI,QAAQ,EAAE,CAAC;gBACb,QAAQ,CAAC,MAAM,GAAG,MAAM,CAAC;gBAEzB,QAAQ,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;gBAC3B,QAAQ,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;gBAC/B,QAAQ,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;gBAC/B,QAAQ,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;gBAEjC,OAAO,CAAC,GAAG,GAAG,QAAQ,CAAC,GAAG,CAAC;gBAC3B,OAAO,CAAC,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC;gBAC/B,OAAO,CAAC,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC;gBAC/B,OAAO,CAAC,MAAM,GAAG,QAAQ,CAAC,MAAM,CAAC;YACnC,CAAC;YAED,OAAO,QAAQ,CAAC;QAClB,CAAC;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;IAED;;;;;;;OAOG;IACO,YAAY,CAAC,OAAa,EAAE,OAAa;QACjD,OAAO,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;QAC9C,OAAO,KAAK,CAAC,YAAY,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAC9C,CAAC;CACF;AArWD,oCAqWC"}
@@ -40,6 +40,7 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
40
40
  constructor();
41
41
  protected _vertexMap: Map<VertexKey, VO>;
42
42
  get vertexMap(): Map<VertexKey, VO>;
43
+ set vertexMap(v: Map<VertexKey, VO>);
43
44
  /**
44
45
  * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
45
46
  * This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
@@ -361,84 +362,6 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
361
362
  costs: number[][];
362
363
  predecessor: (VO | undefined)[][];
363
364
  };
364
- /**
365
- * Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
366
- * Space Complexity: O(V) - Linear space (Tarjan's algorithm).
367
- * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
368
- * Tarjan can find cycles in directed or undirected graph
369
- * Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
370
- * Tarjan solve the bi-connected components of undirected graphs;
371
- * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
372
- * /
373
-
374
- /**
375
- * Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
376
- * Space Complexity: O(V) - Linear space (Tarjan's algorithm).
377
- *
378
- * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
379
- * Tarjan can find cycles in directed or undirected graph
380
- * Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
381
- * Tarjan solve the bi-connected components of undirected graphs;
382
- * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
383
- * The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
384
- * strongly connected components (SCCs), and cycles in a graph.
385
- * @param {boolean} [needCutVertexes] - A boolean value indicating whether or not to calculate and return the
386
- * articulation points in the graph. Articulation points are the vertexMap in a graph whose removal would increase the
387
- * number of connected components in the graph.
388
- * @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
389
- * (edgeMap whose removal would increase the number of connected components in the graph).
390
- * @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
391
- * graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
392
- * SCCs will not be calculated or returned.
393
- * @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
394
- * set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values
395
- * are arrays of vertexMap that form cycles within the SCCs.
396
- * @returns The function `tarjan` returns an object with the following properties:
397
- */
398
- tarjan(needCutVertexes?: boolean, needBridges?: boolean, needSCCs?: boolean, needCycles?: boolean): {
399
- dfnMap: Map<VO, number>;
400
- lowMap: Map<VO, number>;
401
- bridges: EO[];
402
- cutVertexes: VO[];
403
- SCCs: Map<number, VO[]>;
404
- cycles: Map<number, VO[]>;
405
- };
406
- /**
407
- * Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
408
- * Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
409
- */
410
- /**
411
- * Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
412
- * Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
413
- *
414
- * The function returns a map that associates each vertex object with its corresponding depth-first
415
- * number.
416
- * @returns A Map object with keys of type VO and values of type number.
417
- */
418
- getDFNMap(): Map<VO, number>;
419
- /**
420
- * The function returns a Map object that contains the low values of each vertex in a Tarjan
421
- * algorithm.
422
- * @returns The method `getLowMap()` is returning a `Map` object with keys of type `VO` and values of
423
- * type `number`.
424
- */
425
- getLowMap(): Map<VO, number>;
426
- /**
427
- * The function "getCutVertexes" returns an array of cut vertexes using the Tarjan algorithm.
428
- * @returns an array of VO objects, specifically the cut vertexes.
429
- */
430
- getCutVertexes(): VO[];
431
- /**
432
- * The function "getSCCs" returns a map of strongly connected components (SCCs) using the Tarjan
433
- * algorithm.
434
- * @returns a map where the keys are numbers and the values are arrays of VO objects.
435
- */
436
- getSCCs(): Map<number, VO[]>;
437
- /**
438
- * The function "getBridges" returns an array of bridges using the Tarjan algorithm.
439
- * @returns the bridges found using the Tarjan algorithm.
440
- */
441
- getBridges(): EO[];
442
365
  /**
443
366
  * O(V+E+C)
444
367
  * O(V+C)
@@ -47,6 +47,9 @@ class AbstractGraph extends base_1.IterableEntryBase {
47
47
  get vertexMap() {
48
48
  return this._vertexMap;
49
49
  }
50
+ set vertexMap(v) {
51
+ this._vertexMap = v;
52
+ }
50
53
  /**
51
54
  * Time Complexity: O(1) - Constant time for Map lookup.
52
55
  * Space Complexity: O(1) - Constant space, as it creates only a few variables.
@@ -817,195 +820,6 @@ class AbstractGraph extends base_1.IterableEntryBase {
817
820
  }
818
821
  return { costs, predecessor };
819
822
  }
820
- /**
821
- * Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
822
- * Space Complexity: O(V) - Linear space (Tarjan's algorithm).
823
- * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
824
- * Tarjan can find cycles in directed or undirected graph
825
- * Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
826
- * Tarjan solve the bi-connected components of undirected graphs;
827
- * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
828
- * /
829
-
830
- /**
831
- * Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
832
- * Space Complexity: O(V) - Linear space (Tarjan's algorithm).
833
- *
834
- * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
835
- * Tarjan can find cycles in directed or undirected graph
836
- * Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
837
- * Tarjan solve the bi-connected components of undirected graphs;
838
- * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
839
- * The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
840
- * strongly connected components (SCCs), and cycles in a graph.
841
- * @param {boolean} [needCutVertexes] - A boolean value indicating whether or not to calculate and return the
842
- * articulation points in the graph. Articulation points are the vertexMap in a graph whose removal would increase the
843
- * number of connected components in the graph.
844
- * @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
845
- * (edgeMap whose removal would increase the number of connected components in the graph).
846
- * @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
847
- * graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
848
- * SCCs will not be calculated or returned.
849
- * @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
850
- * set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values
851
- * are arrays of vertexMap that form cycles within the SCCs.
852
- * @returns The function `tarjan` returns an object with the following properties:
853
- */
854
- tarjan(needCutVertexes = false, needBridges = false, needSCCs = true, needCycles = false) {
855
- // !! in undirected graph we will not let child visit parent when dfs
856
- // !! articulation point(in dfs search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2)
857
- // !! bridge: low(child) > dfn(cur)
858
- const defaultConfig = false;
859
- if (needCutVertexes === undefined)
860
- needCutVertexes = defaultConfig;
861
- if (needBridges === undefined)
862
- needBridges = defaultConfig;
863
- if (needSCCs === undefined)
864
- needSCCs = defaultConfig;
865
- if (needCycles === undefined)
866
- needCycles = defaultConfig;
867
- const dfnMap = new Map();
868
- const lowMap = new Map();
869
- const vertexMap = this._vertexMap;
870
- vertexMap.forEach(v => {
871
- dfnMap.set(v, -1);
872
- lowMap.set(v, Infinity);
873
- });
874
- const [root] = vertexMap.values();
875
- const cutVertexes = [];
876
- const bridges = [];
877
- let dfn = 0;
878
- const dfs = (cur, parent) => {
879
- dfn++;
880
- dfnMap.set(cur, dfn);
881
- lowMap.set(cur, dfn);
882
- const neighbors = this.getNeighbors(cur);
883
- let childCount = 0; // child in dfs tree not child in graph
884
- for (const neighbor of neighbors) {
885
- if (neighbor !== parent) {
886
- if (dfnMap.get(neighbor) === -1) {
887
- childCount++;
888
- dfs(neighbor, cur);
889
- }
890
- const childLow = lowMap.get(neighbor);
891
- const curLow = lowMap.get(cur);
892
- // TODO after no-non-undefined-assertion not ensure the logic
893
- if (curLow !== undefined && childLow !== undefined) {
894
- lowMap.set(cur, Math.min(curLow, childLow));
895
- }
896
- const curFromMap = dfnMap.get(cur);
897
- if (childLow !== undefined && curFromMap !== undefined) {
898
- if (needCutVertexes) {
899
- if ((cur === root && childCount >= 2) || (cur !== root && childLow >= curFromMap)) {
900
- // todo not ensure the logic if (cur === root && childCount >= 2 || ((cur !== root) && (childLow >= curFromMap))) {
901
- cutVertexes.push(cur);
902
- }
903
- }
904
- if (needBridges) {
905
- if (childLow > curFromMap) {
906
- const edgeCurToNeighbor = this.getEdge(cur, neighbor);
907
- if (edgeCurToNeighbor) {
908
- bridges.push(edgeCurToNeighbor);
909
- }
910
- }
911
- }
912
- }
913
- }
914
- }
915
- };
916
- dfs(root, undefined);
917
- let SCCs = new Map();
918
- const getSCCs = () => {
919
- const SCCs = new Map();
920
- lowMap.forEach((low, vertex) => {
921
- var _a;
922
- if (!SCCs.has(low)) {
923
- SCCs.set(low, [vertex]);
924
- }
925
- else {
926
- (_a = SCCs.get(low)) === null || _a === void 0 ? void 0 : _a.push(vertex);
927
- }
928
- });
929
- return SCCs;
930
- };
931
- if (needSCCs) {
932
- SCCs = getSCCs();
933
- }
934
- const cycles = new Map();
935
- if (needCycles) {
936
- const visitedMap = new Map();
937
- const stack = [];
938
- const findCyclesDFS = (cur, parent) => {
939
- visitedMap.set(cur, true);
940
- stack.push(cur);
941
- const neighbors = this.getNeighbors(cur);
942
- for (const neighbor of neighbors) {
943
- if (!visitedMap.get(neighbor)) {
944
- findCyclesDFS(neighbor, cur);
945
- }
946
- else if (stack.includes(neighbor) && neighbor !== parent) {
947
- const cycleStartIndex = stack.indexOf(neighbor);
948
- const cycle = stack.slice(cycleStartIndex);
949
- const cycleLow = Math.min(...cycle.map(v => dfnMap.get(v) || Infinity));
950
- cycles.set(cycleLow, cycle);
951
- }
952
- }
953
- stack.pop();
954
- };
955
- vertexMap.forEach(v => {
956
- if (!visitedMap.get(v)) {
957
- findCyclesDFS(v, undefined);
958
- }
959
- });
960
- }
961
- return { dfnMap, lowMap, bridges, cutVertexes, SCCs, cycles };
962
- }
963
- /**
964
- * Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
965
- * Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
966
- */
967
- /**
968
- * Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
969
- * Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
970
- *
971
- * The function returns a map that associates each vertex object with its corresponding depth-first
972
- * number.
973
- * @returns A Map object with keys of type VO and values of type number.
974
- */
975
- getDFNMap() {
976
- return this.tarjan(false, false, false, false).dfnMap;
977
- }
978
- /**
979
- * The function returns a Map object that contains the low values of each vertex in a Tarjan
980
- * algorithm.
981
- * @returns The method `getLowMap()` is returning a `Map` object with keys of type `VO` and values of
982
- * type `number`.
983
- */
984
- getLowMap() {
985
- return this.tarjan(false, false, false, false).lowMap;
986
- }
987
- /**
988
- * The function "getCutVertexes" returns an array of cut vertexes using the Tarjan algorithm.
989
- * @returns an array of VO objects, specifically the cut vertexes.
990
- */
991
- getCutVertexes() {
992
- return this.tarjan(true, false, false, false).cutVertexes;
993
- }
994
- /**
995
- * The function "getSCCs" returns a map of strongly connected components (SCCs) using the Tarjan
996
- * algorithm.
997
- * @returns a map where the keys are numbers and the values are arrays of VO objects.
998
- */
999
- getSCCs() {
1000
- return this.tarjan(false, false, true, false).SCCs;
1001
- }
1002
- /**
1003
- * The function "getBridges" returns an array of bridges using the Tarjan algorithm.
1004
- * @returns the bridges found using the Tarjan algorithm.
1005
- */
1006
- getBridges() {
1007
- return this.tarjan(false, true, false, false).bridges;
1008
- }
1009
823
  /**
1010
824
  * O(V+E+C)
1011
825
  * O(V+C)