data-structure-typed 1.49.4 → 1.49.6

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 (186) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +15 -26
  3. package/README_zh-CN.md +1 -1
  4. package/benchmark/report.html +14 -23
  5. package/benchmark/report.json +163 -256
  6. package/dist/cjs/data-structures/base/iterable-base.d.ts +1 -1
  7. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
  9. package/dist/cjs/data-structures/binary-tree/avl-tree.js +55 -49
  10. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +154 -143
  12. package/dist/cjs/data-structures/binary-tree/binary-tree.js +211 -198
  13. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/bst.d.ts +83 -71
  15. package/dist/cjs/data-structures/binary-tree/bst.js +113 -89
  16. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  17. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
  18. package/dist/cjs/data-structures/binary-tree/rb-tree.js +62 -59
  19. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  20. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +46 -55
  21. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +59 -94
  22. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  23. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -1
  24. package/dist/cjs/data-structures/graph/abstract-graph.js +3 -2
  25. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  26. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  27. package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
  28. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  29. package/dist/cjs/data-structures/hash/hash-map.d.ts +1 -1
  30. package/dist/cjs/data-structures/hash/hash-map.js +2 -2
  31. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  32. package/dist/cjs/data-structures/heap/heap.js +2 -3
  33. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  34. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  35. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  36. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  37. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  38. package/dist/cjs/data-structures/matrix/index.d.ts +0 -2
  39. package/dist/cjs/data-structures/matrix/index.js +0 -2
  40. package/dist/cjs/data-structures/matrix/index.js.map +1 -1
  41. package/dist/cjs/data-structures/matrix/matrix.d.ts +128 -10
  42. package/dist/cjs/data-structures/matrix/matrix.js +400 -15
  43. package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
  44. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  45. package/dist/cjs/data-structures/queue/deque.d.ts +2 -2
  46. package/dist/cjs/data-structures/queue/deque.js +5 -7
  47. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  48. package/dist/cjs/data-structures/queue/queue.d.ts +1 -1
  49. package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
  50. package/dist/cjs/types/common.d.ts +3 -3
  51. package/dist/cjs/types/common.js +2 -2
  52. package/dist/cjs/types/common.js.map +1 -1
  53. package/dist/cjs/types/data-structures/base/base.d.ts +1 -1
  54. package/dist/cjs/types/data-structures/heap/heap.d.ts +1 -1
  55. package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  56. package/dist/cjs/utils/utils.d.ts +1 -0
  57. package/dist/cjs/utils/utils.js +6 -1
  58. package/dist/cjs/utils/utils.js.map +1 -1
  59. package/dist/mjs/data-structures/base/iterable-base.d.ts +1 -1
  60. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
  61. package/dist/mjs/data-structures/binary-tree/avl-tree.js +57 -50
  62. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +154 -143
  63. package/dist/mjs/data-structures/binary-tree/binary-tree.js +212 -199
  64. package/dist/mjs/data-structures/binary-tree/bst.d.ts +83 -71
  65. package/dist/mjs/data-structures/binary-tree/bst.js +115 -90
  66. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
  67. package/dist/mjs/data-structures/binary-tree/rb-tree.js +64 -60
  68. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +46 -55
  69. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +61 -95
  70. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -1
  71. package/dist/mjs/data-structures/graph/abstract-graph.js +4 -3
  72. package/dist/mjs/data-structures/hash/hash-map.d.ts +1 -1
  73. package/dist/mjs/data-structures/hash/hash-map.js +2 -2
  74. package/dist/mjs/data-structures/heap/heap.js +2 -3
  75. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  76. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +1 -1
  77. package/dist/mjs/data-structures/matrix/index.d.ts +0 -2
  78. package/dist/mjs/data-structures/matrix/index.js +0 -2
  79. package/dist/mjs/data-structures/matrix/matrix.d.ts +128 -10
  80. package/dist/mjs/data-structures/matrix/matrix.js +399 -16
  81. package/dist/mjs/data-structures/queue/deque.d.ts +2 -2
  82. package/dist/mjs/data-structures/queue/deque.js +7 -9
  83. package/dist/mjs/data-structures/queue/queue.d.ts +1 -1
  84. package/dist/mjs/data-structures/queue/queue.js +1 -1
  85. package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
  86. package/dist/mjs/types/common.d.ts +3 -3
  87. package/dist/mjs/types/common.js +2 -2
  88. package/dist/mjs/types/data-structures/base/base.d.ts +1 -1
  89. package/dist/mjs/types/data-structures/heap/heap.d.ts +1 -1
  90. package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  91. package/dist/mjs/utils/utils.d.ts +1 -0
  92. package/dist/mjs/utils/utils.js +4 -0
  93. package/dist/umd/data-structure-typed.js +853 -943
  94. package/dist/umd/data-structure-typed.min.js +2 -2
  95. package/dist/umd/data-structure-typed.min.js.map +1 -1
  96. package/package.json +4 -3
  97. package/src/data-structures/base/index.ts +1 -1
  98. package/src/data-structures/base/iterable-base.ts +7 -10
  99. package/src/data-structures/binary-tree/avl-tree.ts +73 -61
  100. package/src/data-structures/binary-tree/binary-tree.ts +301 -270
  101. package/src/data-structures/binary-tree/bst.ts +139 -115
  102. package/src/data-structures/binary-tree/rb-tree.ts +81 -73
  103. package/src/data-structures/binary-tree/tree-multimap.ts +72 -103
  104. package/src/data-structures/graph/abstract-graph.ts +13 -11
  105. package/src/data-structures/graph/directed-graph.ts +1 -3
  106. package/src/data-structures/graph/map-graph.ts +6 -1
  107. package/src/data-structures/graph/undirected-graph.ts +3 -6
  108. package/src/data-structures/hash/hash-map.ts +18 -16
  109. package/src/data-structures/heap/heap.ts +7 -10
  110. package/src/data-structures/heap/max-heap.ts +2 -1
  111. package/src/data-structures/heap/min-heap.ts +2 -1
  112. package/src/data-structures/linked-list/singly-linked-list.ts +2 -3
  113. package/src/data-structures/matrix/index.ts +0 -2
  114. package/src/data-structures/matrix/matrix.ts +442 -13
  115. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -10
  116. package/src/data-structures/queue/deque.ts +18 -39
  117. package/src/data-structures/queue/queue.ts +1 -1
  118. package/src/interfaces/binary-tree.ts +9 -4
  119. package/src/types/common.ts +5 -5
  120. package/src/types/data-structures/base/base.ts +14 -3
  121. package/src/types/data-structures/base/index.ts +1 -1
  122. package/src/types/data-structures/graph/abstract-graph.ts +4 -2
  123. package/src/types/data-structures/hash/hash-map.ts +3 -3
  124. package/src/types/data-structures/heap/heap.ts +2 -2
  125. package/src/types/data-structures/priority-queue/priority-queue.ts +2 -2
  126. package/src/utils/utils.ts +7 -1
  127. package/test/integration/avl-tree.test.ts +18 -1
  128. package/test/integration/bst.test.ts +2 -2
  129. package/test/performance/data-structures/binary-tree/avl-tree.test.ts +4 -12
  130. package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +37 -0
  131. package/test/performance/data-structures/binary-tree/binary-tree.test.ts +6 -16
  132. package/test/performance/data-structures/binary-tree/bst.test.ts +5 -13
  133. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +6 -16
  134. package/test/performance/data-structures/comparison/comparison.test.ts +19 -47
  135. package/test/performance/data-structures/graph/directed-graph.test.ts +3 -14
  136. package/test/performance/data-structures/hash/hash-map.test.ts +14 -38
  137. package/test/performance/data-structures/heap/heap.test.ts +5 -18
  138. package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +0 -2
  139. package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +0 -2
  140. package/test/performance/data-structures/priority-queue/max-priority-queue.test.ts +2 -4
  141. package/test/performance/data-structures/priority-queue/priority-queue.test.ts +4 -14
  142. package/test/performance/data-structures/queue/deque.test.ts +6 -7
  143. package/test/performance/data-structures/queue/queue.test.ts +13 -29
  144. package/test/performance/data-structures/stack/stack.test.ts +6 -18
  145. package/test/performance/data-structures/trie/trie.test.ts +2 -6
  146. package/test/performance/reportor.ts +14 -15
  147. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +14 -3
  148. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +65 -29
  149. package/test/unit/data-structures/binary-tree/bst.test.ts +69 -27
  150. package/test/unit/data-structures/binary-tree/overall.test.ts +16 -18
  151. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +17 -6
  152. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -21
  153. package/test/unit/data-structures/graph/directed-graph.test.ts +104 -45
  154. package/test/unit/data-structures/graph/undirected-graph.test.ts +57 -40
  155. package/test/unit/data-structures/hash/hash-map.test.ts +48 -28
  156. package/test/unit/data-structures/hash/hash-table.test.ts +0 -4
  157. package/test/unit/data-structures/heap/heap.test.ts +0 -1
  158. package/test/unit/data-structures/heap/min-heap.test.ts +6 -5
  159. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +10 -11
  160. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +3 -4
  161. package/test/unit/data-structures/matrix/matrix.test.ts +345 -52
  162. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +1 -1
  163. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +0 -1
  164. package/test/unit/data-structures/queue/deque.test.ts +0 -6
  165. package/test/unit/data-structures/queue/queue.test.ts +1 -1
  166. package/test/unit/data-structures/stack/stack.test.ts +3 -4
  167. package/test/unit/unrestricted-interconversion.test.ts +50 -44
  168. package/test/utils/performanc.ts +1 -1
  169. package/typedoc.json +30 -0
  170. package/dist/cjs/data-structures/matrix/matrix2d.d.ts +0 -107
  171. package/dist/cjs/data-structures/matrix/matrix2d.js +0 -200
  172. package/dist/cjs/data-structures/matrix/matrix2d.js.map +0 -1
  173. package/dist/cjs/data-structures/matrix/vector2d.d.ts +0 -200
  174. package/dist/cjs/data-structures/matrix/vector2d.js +0 -291
  175. package/dist/cjs/data-structures/matrix/vector2d.js.map +0 -1
  176. package/dist/mjs/data-structures/matrix/matrix2d.d.ts +0 -107
  177. package/dist/mjs/data-structures/matrix/matrix2d.js +0 -196
  178. package/dist/mjs/data-structures/matrix/vector2d.d.ts +0 -200
  179. package/dist/mjs/data-structures/matrix/vector2d.js +0 -289
  180. package/src/data-structures/matrix/matrix2d.ts +0 -211
  181. package/src/data-structures/matrix/vector2d.ts +0 -315
  182. package/test/performance/data-structures/binary-tree/overall.test.ts +0 -0
  183. package/test/performance/data-structures/matrix/matrix2d.test.ts +0 -0
  184. package/test/performance/data-structures/matrix/vector2d.test.ts +0 -0
  185. package/test/unit/data-structures/matrix/matrix2d.test.ts +0 -345
  186. package/test/unit/data-structures/matrix/vector2d.test.ts +0 -171
@@ -8,7 +8,7 @@
8
8
  import { FamilyPosition, IterationType } from '../../types';
9
9
  import { trampoline } from '../../utils';
10
10
  import { Queue } from '../queue';
11
- import { IterableEntryBase } from "../base";
11
+ import { IterableEntryBase } from '../base';
12
12
  /**
13
13
  * Represents a node in a binary tree.
14
14
  * @template V - The type of data stored in the node.
@@ -70,15 +70,15 @@ export class BinaryTreeNode {
70
70
  export class BinaryTree extends IterableEntryBase {
71
71
  iterationType = IterationType.ITERATIVE;
72
72
  /**
73
- * The constructor function initializes a binary tree object with optional elements and options.
74
- * @param [elements] - An optional iterable of BTNExemplar objects. These objects represent the
75
- * elements to be added to the binary tree.
73
+ * The constructor function initializes a binary tree object with optional nodes and options.
74
+ * @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the
75
+ * nodes to be added to the binary tree.
76
76
  * @param [options] - The `options` parameter is an optional object that can contain additional
77
77
  * configuration options for the binary tree. In this case, it is of type
78
78
  * `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
79
79
  * required.
80
80
  */
81
- constructor(elements, options) {
81
+ constructor(nodes, options) {
82
82
  super();
83
83
  if (options) {
84
84
  const { iterationType, extractor } = options;
@@ -90,8 +90,8 @@ export class BinaryTree extends IterableEntryBase {
90
90
  }
91
91
  }
92
92
  this._size = 0;
93
- if (elements)
94
- this.addMany(elements);
93
+ if (nodes)
94
+ this.addMany(nodes);
95
95
  }
96
96
  _extractor = (key) => Number(key);
97
97
  get extractor() {
@@ -125,30 +125,22 @@ export class BinaryTree extends IterableEntryBase {
125
125
  return new BinaryTree([], { iterationType: this.iterationType, ...options });
126
126
  }
127
127
  /**
128
- * The function "isNode" checks if an exemplar is an instance of the BinaryTreeNode class.
129
- * @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V,N>`.
130
- * @returns a boolean value indicating whether the exemplar is an instance of the class N.
131
- */
132
- isNode(exemplar) {
133
- return exemplar instanceof BinaryTreeNode;
134
- }
135
- /**
136
- * The function `exemplarToNode` converts an exemplar object into a node object.
137
- * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
128
+ * The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
129
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
138
130
  * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
139
- * `exemplarToNode` function. It represents the value associated with the exemplar node. If no value
131
+ * `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If no value
140
132
  * is provided, it will be `undefined`.
141
133
  * @returns a value of type N (node), or null, or undefined.
142
134
  */
143
- exemplarToNode(exemplar, value) {
144
- if (exemplar === undefined)
135
+ exemplarToNode(keyOrNodeOrEntry, value) {
136
+ if (keyOrNodeOrEntry === undefined)
145
137
  return;
146
138
  let node;
147
- if (exemplar === null) {
139
+ if (keyOrNodeOrEntry === null) {
148
140
  node = null;
149
141
  }
150
- else if (this.isEntry(exemplar)) {
151
- const [key, value] = exemplar;
142
+ else if (this.isEntry(keyOrNodeOrEntry)) {
143
+ const [key, value] = keyOrNodeOrEntry;
152
144
  if (key === undefined) {
153
145
  return;
154
146
  }
@@ -159,25 +151,108 @@ export class BinaryTree extends IterableEntryBase {
159
151
  node = this.createNode(key, value);
160
152
  }
161
153
  }
162
- else if (this.isNode(exemplar)) {
163
- node = exemplar;
154
+ else if (this.isNode(keyOrNodeOrEntry)) {
155
+ node = keyOrNodeOrEntry;
164
156
  }
165
- else if (this.isNotNodeInstance(exemplar)) {
166
- node = this.createNode(exemplar, value);
157
+ else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
158
+ node = this.createNode(keyOrNodeOrEntry, value);
167
159
  }
168
160
  else {
169
161
  return;
170
162
  }
171
163
  return node;
172
164
  }
165
+ /**
166
+ * Time Complexity: O(n)
167
+ * Space Complexity: O(log n)
168
+ */
169
+ /**
170
+ * Time Complexity: O(n)
171
+ * Space Complexity: O(log n)
172
+ *
173
+ * The function `ensureNode` returns the node corresponding to the given key if it is a valid node
174
+ * key, otherwise it returns the key itself.
175
+ * @param {K | N | null | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`,
176
+ * `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
177
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
178
+ * type of iteration to be used when searching for a node by key. It has a default value of
179
+ * `IterationType.ITERATIVE`.
180
+ * @returns either the node corresponding to the given key if it is a valid node key, or the key
181
+ * itself if it is not a valid node key.
182
+ */
183
+ ensureNode(keyOrNodeOrEntry, iterationType = IterationType.ITERATIVE) {
184
+ let res;
185
+ if (this.isRealNode(keyOrNodeOrEntry)) {
186
+ res = keyOrNodeOrEntry;
187
+ }
188
+ else if (this.isEntry(keyOrNodeOrEntry)) {
189
+ if (keyOrNodeOrEntry[0] === null)
190
+ res = null;
191
+ else if (keyOrNodeOrEntry[0] !== undefined)
192
+ res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
193
+ }
194
+ else {
195
+ if (keyOrNodeOrEntry === null)
196
+ res = null;
197
+ else if (keyOrNodeOrEntry !== undefined)
198
+ res = this.getNodeByKey(keyOrNodeOrEntry, iterationType);
199
+ }
200
+ return res;
201
+ }
202
+ /**
203
+ * The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
204
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,N>`.
205
+ * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the class N.
206
+ */
207
+ isNode(keyOrNodeOrEntry) {
208
+ return keyOrNodeOrEntry instanceof BinaryTreeNode;
209
+ }
173
210
  /**
174
211
  * The function checks if a given value is an entry in a binary tree node.
175
- * @param kne - BTNExemplar<K, V,N> - A generic type representing a node in a binary tree. It has
212
+ * @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,N> - A generic type representing a node in a binary tree. It has
176
213
  * two type parameters V and N, representing the value and node type respectively.
177
214
  * @returns a boolean value.
178
215
  */
179
- isEntry(kne) {
180
- return Array.isArray(kne) && kne.length === 2;
216
+ isEntry(keyOrNodeOrEntry) {
217
+ return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
218
+ }
219
+ /**
220
+ * Time complexity: O(n)
221
+ * Space complexity: O(log n)
222
+ */
223
+ /**
224
+ * The function checks if a given node is a real node by verifying if it is an instance of
225
+ * BinaryTreeNode and its key is not NaN.
226
+ * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
227
+ * @returns a boolean value.
228
+ */
229
+ isRealNode(node) {
230
+ return node instanceof BinaryTreeNode && String(node.key) !== 'NaN';
231
+ }
232
+ /**
233
+ * The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
234
+ * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
235
+ * @returns a boolean value.
236
+ */
237
+ isNIL(node) {
238
+ return node instanceof BinaryTreeNode && String(node.key) === 'NaN';
239
+ }
240
+ /**
241
+ * The function checks if a given node is a real node or null.
242
+ * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
243
+ * @returns a boolean value.
244
+ */
245
+ isNodeOrNull(node) {
246
+ return this.isRealNode(node) || node === null;
247
+ }
248
+ /**
249
+ * The function "isNotNodeInstance" checks if a potential key is a K.
250
+ * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
251
+ * data type.
252
+ * @returns a boolean value indicating whether the potentialKey is of type number or not.
253
+ */
254
+ isNotNodeInstance(potentialKey) {
255
+ return !(potentialKey instanceof BinaryTreeNode);
181
256
  }
182
257
  /**
183
258
  * Time Complexity O(log n) - O(n)
@@ -196,12 +271,12 @@ export class BinaryTree extends IterableEntryBase {
196
271
  add(keyOrNodeOrEntry, value) {
197
272
  const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
198
273
  if (newNode === undefined)
199
- return;
274
+ return false;
200
275
  // If the tree is empty, directly set the new node as the root node
201
276
  if (!this.root) {
202
277
  this._root = newNode;
203
278
  this._size = 1;
204
- return newNode;
279
+ return true;
205
280
  }
206
281
  const queue = new Queue([this.root]);
207
282
  let potentialParent; // Record the parent node of the potential insertion location
@@ -212,7 +287,7 @@ export class BinaryTree extends IterableEntryBase {
212
287
  // Check for duplicate keys when newNode is not null
213
288
  if (newNode !== null && cur.key === newNode.key) {
214
289
  this._replaceNode(cur, newNode);
215
- return newNode; // If duplicate keys are found, no insertion is performed
290
+ return true; // If duplicate keys are found, no insertion is performed
216
291
  }
217
292
  // Record the first possible insertion location found
218
293
  if (potentialParent === undefined && (cur.left === undefined || cur.right === undefined)) {
@@ -235,9 +310,9 @@ export class BinaryTree extends IterableEntryBase {
235
310
  potentialParent.right = newNode;
236
311
  }
237
312
  this._size++;
238
- return newNode;
313
+ return true;
239
314
  }
240
- return undefined; // If the insertion position cannot be found, return undefined
315
+ return false; // If the insertion position cannot be found, return undefined
241
316
  }
242
317
  /**
243
318
  * Time Complexity: O(k log n) - O(k * n)
@@ -248,20 +323,20 @@ export class BinaryTree extends IterableEntryBase {
248
323
  * Time Complexity: O(k log n) - O(k * n)
249
324
  * Space Complexity: O(1)
250
325
  *
251
- * The `addMany` function takes in a collection of nodes and an optional collection of values, and
326
+ * The `addMany` function takes in a collection of keysOrNodesOrEntries and an optional collection of values, and
252
327
  * adds each node with its corresponding value to the data structure.
253
- * @param nodes - An iterable collection of BTNExemplar objects.
328
+ * @param keysOrNodesOrEntries - An iterable collection of KeyOrNodeOrEntry objects.
254
329
  * @param [values] - An optional iterable of values that will be assigned to each node being added.
255
330
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
256
331
  */
257
- addMany(nodes, values) {
332
+ addMany(keysOrNodesOrEntries, values) {
258
333
  // TODO not sure addMany not be run multi times
259
334
  const inserted = [];
260
335
  let valuesIterator;
261
336
  if (values) {
262
337
  valuesIterator = values[Symbol.iterator]();
263
338
  }
264
- for (const kne of nodes) {
339
+ for (const keyOrNodeOrEntry of keysOrNodesOrEntries) {
265
340
  let value = undefined;
266
341
  if (valuesIterator) {
267
342
  const valueResult = valuesIterator.next();
@@ -269,17 +344,30 @@ export class BinaryTree extends IterableEntryBase {
269
344
  value = valueResult.value;
270
345
  }
271
346
  }
272
- inserted.push(this.add(kne, value));
347
+ inserted.push(this.add(keyOrNodeOrEntry, value));
273
348
  }
274
349
  return inserted;
275
350
  }
276
351
  /**
277
- * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
352
+ * Time Complexity: O(k * n)
278
353
  * Space Complexity: O(1)
354
+ * "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
279
355
  */
280
- refill(nodesOrKeysOrEntries, values) {
356
+ /**
357
+ * Time Complexity: O(k * n)
358
+ * Space Complexity: O(1)
359
+ *
360
+ * The `refill` function clears the current data and adds new key-value pairs to the data structure.
361
+ * @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries. These can be of type
362
+ * KeyOrNodeOrEntry<K, V, N>.
363
+ * @param [values] - The `values` parameter is an optional iterable that contains the values to be
364
+ * associated with the keys or nodes or entries in the `keysOrNodesOrEntries` parameter. If provided,
365
+ * the values will be associated with the corresponding keys or nodes or entries in the
366
+ * `keysOrNodesOrEntries` iterable
367
+ */
368
+ refill(keysOrNodesOrEntries, values) {
281
369
  this.clear();
282
- this.addMany(nodesOrKeysOrEntries, values);
370
+ this.addMany(keysOrNodesOrEntries, values);
283
371
  }
284
372
  /**
285
373
  * Time Complexity: O(n)
@@ -353,24 +441,24 @@ export class BinaryTree extends IterableEntryBase {
353
441
  * Space Complexity: O(1)
354
442
  *
355
443
  * The function calculates the depth of a given node in a binary tree.
356
- * @param {K | N | null | undefined} distNode - The `distNode` parameter represents the node in
444
+ * @param {K | N | null | undefined} dist - The `dist` parameter represents the node in
357
445
  * the binary tree whose depth we want to find. It can be of type `K`, `N`, `null`, or
358
446
  * `undefined`.
359
447
  * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
360
448
  * from which we want to calculate the depth. It can be either a `K` (binary tree node key) or
361
449
  * `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
362
- * @returns the depth of the `distNode` relative to the `beginRoot`.
450
+ * @returns the depth of the `dist` relative to the `beginRoot`.
363
451
  */
364
- getDepth(distNode, beginRoot = this.root) {
365
- distNode = this.ensureNode(distNode);
452
+ getDepth(dist, beginRoot = this.root) {
453
+ dist = this.ensureNode(dist);
366
454
  beginRoot = this.ensureNode(beginRoot);
367
455
  let depth = 0;
368
- while (distNode?.parent) {
369
- if (distNode === beginRoot) {
456
+ while (dist?.parent) {
457
+ if (dist === beginRoot) {
370
458
  return depth;
371
459
  }
372
460
  depth++;
373
- distNode = distNode.parent;
461
+ dist = dist.parent;
374
462
  }
375
463
  return depth;
376
464
  }
@@ -566,6 +654,7 @@ export class BinaryTree extends IterableEntryBase {
566
654
  }
567
655
  /**
568
656
  * Time Complexity: O(n)
657
+ * Space Complexity: O(log n).
569
658
  *
570
659
  * The function checks if a Binary Tree Node with a specific identifier exists in the tree.
571
660
  * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
@@ -662,24 +751,6 @@ export class BinaryTree extends IterableEntryBase {
662
751
  }
663
752
  }
664
753
  }
665
- /**
666
- * Time Complexity: O(n)
667
- * Space Complexity: O(log n)
668
- */
669
- /**
670
- * The function `ensureNode` returns the node corresponding to the given key if it is a valid node
671
- * key, otherwise it returns the key itself.
672
- * @param {K | N | null | undefined} key - The `key` parameter can be of type `K`, `N`,
673
- * `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
674
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
675
- * type of iteration to be used when searching for a node by key. It has a default value of
676
- * `IterationType.ITERATIVE`.
677
- * @returns either the node corresponding to the given key if it is a valid node key, or the key
678
- * itself if it is not a valid node key.
679
- */
680
- ensureNode(key, iterationType = IterationType.ITERATIVE) {
681
- return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
682
- }
683
754
  /**
684
755
  * Time Complexity: O(n)
685
756
  * Space Complexity: O(log n)
@@ -708,10 +779,13 @@ export class BinaryTree extends IterableEntryBase {
708
779
  return this.getNode(identifier, callback, beginRoot, iterationType)?.value ?? undefined;
709
780
  }
710
781
  /**
711
- * Time Complexity: O(n)
712
- * Space Complexity: O(log n)
782
+ * Time Complexity: O(1)
783
+ * Space Complexity: O(1)
713
784
  */
714
785
  /**
786
+ * Time Complexity: O(1)
787
+ * Space Complexity: O(1)
788
+ *
715
789
  * Clear the binary tree, removing all nodes.
716
790
  */
717
791
  clear() {
@@ -719,6 +793,13 @@ export class BinaryTree extends IterableEntryBase {
719
793
  this._size = 0;
720
794
  }
721
795
  /**
796
+ * Time Complexity: O(1)
797
+ * Space Complexity: O(1)
798
+ */
799
+ /**
800
+ * Time Complexity: O(1)
801
+ * Space Complexity: O(1)
802
+ *
722
803
  * Check if the binary tree is empty.
723
804
  * @returns {boolean} - True if the binary tree is empty, false otherwise.
724
805
  */
@@ -756,7 +837,7 @@ export class BinaryTree extends IterableEntryBase {
756
837
  }
757
838
  /**
758
839
  * Time Complexity: O(log n)
759
- * Space Complexity: O(log n)
840
+ * Space Complexity: O(1)
760
841
  */
761
842
  /**
762
843
  * Time Complexity: O(log n)
@@ -852,7 +933,7 @@ export class BinaryTree extends IterableEntryBase {
852
933
  * possible values:
853
934
  * @returns a boolean value.
854
935
  */
855
- isSubtreeBST(beginRoot, iterationType = this.iterationType) {
936
+ isBST(beginRoot = this.root, iterationType = this.iterationType) {
856
937
  // TODO there is a bug
857
938
  beginRoot = this.ensureNode(beginRoot);
858
939
  if (!beginRoot)
@@ -866,46 +947,34 @@ export class BinaryTree extends IterableEntryBase {
866
947
  return false;
867
948
  return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
868
949
  };
869
- return dfs(beginRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
950
+ const isStandardBST = dfs(beginRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
951
+ const isInverseBST = dfs(beginRoot, Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
952
+ return isStandardBST || isInverseBST;
870
953
  }
871
954
  else {
872
- const stack = [];
873
- let prev = Number.MIN_SAFE_INTEGER, curr = beginRoot;
874
- while (curr || stack.length > 0) {
875
- while (curr) {
876
- stack.push(curr);
877
- curr = curr.left;
955
+ const checkBST = (checkMax = false) => {
956
+ const stack = [];
957
+ let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
958
+ // @ts-ignore
959
+ let curr = beginRoot;
960
+ while (curr || stack.length > 0) {
961
+ while (curr) {
962
+ stack.push(curr);
963
+ curr = curr.left;
964
+ }
965
+ curr = stack.pop();
966
+ const numKey = this.extractor(curr.key);
967
+ if (!curr || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey))
968
+ return false;
969
+ prev = numKey;
970
+ curr = curr.right;
878
971
  }
879
- curr = stack.pop();
880
- const numKey = this.extractor(curr.key);
881
- if (!curr || prev >= numKey)
882
- return false;
883
- prev = numKey;
884
- curr = curr.right;
885
- }
886
- return true;
972
+ return true;
973
+ };
974
+ const isStandardBST = checkBST(false), isInverseBST = checkBST(true);
975
+ return isStandardBST || isInverseBST;
887
976
  }
888
977
  }
889
- /**
890
- * Time Complexity: O(n)
891
- * Space Complexity: O(1)
892
- */
893
- /**
894
- * Time Complexity: O(n)
895
- * Space Complexity: O(1)
896
- *
897
- * The function checks if a binary tree is a binary search tree.
898
- * @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
899
- * be used when checking if the binary tree is a binary search tree (BST). It is an optional
900
- * parameter with a default value of "this.iterationType". The value of "this.iterationType" is
901
- * expected to be
902
- * @returns a boolean value.
903
- */
904
- isBST(iterationType = this.iterationType) {
905
- if (this.root === null)
906
- return true;
907
- return this.isSubtreeBST(this.root, iterationType);
908
- }
909
978
  /**
910
979
  * Time complexity: O(n)
911
980
  * Space complexity: O(log n)
@@ -925,7 +994,7 @@ export class BinaryTree extends IterableEntryBase {
925
994
  * whether to include null values in the traversal. If `includeNull` is set to `true`, the
926
995
  * traversal will include null values, otherwise it will skip them.
927
996
  * @returns The function `subTreeTraverse` returns an array of values that are the result of invoking
928
- * the `callback` function on each node in the subtree. The type of the array elements is determined
997
+ * the `callback` function on each node in the subtree. The type of the array nodes is determined
929
998
  * by the return type of the `callback` function.
930
999
  */
931
1000
  subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
@@ -968,44 +1037,6 @@ export class BinaryTree extends IterableEntryBase {
968
1037
  }
969
1038
  return ans;
970
1039
  }
971
- /**
972
- * Time complexity: O(n)
973
- * Space complexity: O(log n)
974
- */
975
- /**
976
- * The function checks if a given node is a real node by verifying if it is an instance of
977
- * BinaryTreeNode and its key is not NaN.
978
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
979
- * @returns a boolean value.
980
- */
981
- isRealNode(node) {
982
- return node instanceof BinaryTreeNode && String(node.key) !== 'NaN';
983
- }
984
- /**
985
- * The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
986
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
987
- * @returns a boolean value.
988
- */
989
- isNIL(node) {
990
- return node instanceof BinaryTreeNode && String(node.key) === 'NaN';
991
- }
992
- /**
993
- * The function checks if a given node is a real node or null.
994
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
995
- * @returns a boolean value.
996
- */
997
- isNodeOrNull(node) {
998
- return this.isRealNode(node) || node === null;
999
- }
1000
- /**
1001
- * The function "isNotNodeInstance" checks if a potential key is a K.
1002
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
1003
- * data type.
1004
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
1005
- */
1006
- isNotNodeInstance(potentialKey) {
1007
- return !(potentialKey instanceof BinaryTreeNode);
1008
- }
1009
1040
  /**
1010
1041
  * Time complexity: O(n)
1011
1042
  * Space complexity: O(n)
@@ -1327,7 +1358,12 @@ export class BinaryTree extends IterableEntryBase {
1327
1358
  }
1328
1359
  /**
1329
1360
  * Time complexity: O(n)
1330
- * Space complexity: O(1)
1361
+ * Space complexity: O(n)
1362
+ */
1363
+ /**
1364
+ * Time complexity: O(n)
1365
+ * Space complexity: O(n)
1366
+ *
1331
1367
  * The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
1332
1368
  * algorithm.
1333
1369
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
@@ -1340,7 +1376,7 @@ export class BinaryTree extends IterableEntryBase {
1340
1376
  * for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
1341
1377
  * the root of the tree. If no value is provided, the default value is the root of the tree.
1342
1378
  * @returns The function `morris` returns an array of values that are the result of invoking the
1343
- * `callback` function on each node in the binary tree. The type of the array elements is determined
1379
+ * `callback` function on each node in the binary tree. The type of the array nodes is determined
1344
1380
  * by the return type of the `callback` function.
1345
1381
  */
1346
1382
  morris(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root) {
@@ -1453,8 +1489,8 @@ export class BinaryTree extends IterableEntryBase {
1453
1489
  * Time Complexity: O(n)
1454
1490
  * Space Complexity: O(n)
1455
1491
  *
1456
- * The `filter` function creates a new tree by iterating over the elements of the current tree and
1457
- * adding only the elements that satisfy the given predicate function.
1492
+ * The `filter` function creates a new tree by iterating over the nodes of the current tree and
1493
+ * adding only the nodes that satisfy the given predicate function.
1458
1494
  * @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
1459
1495
  * `key`, and `index`. It should return a boolean value indicating whether the pair should be
1460
1496
  * included in the filtered tree or not.
@@ -1511,6 +1547,13 @@ export class BinaryTree extends IterableEntryBase {
1511
1547
  // // }
1512
1548
  //
1513
1549
  /**
1550
+ * Time Complexity: O(n)
1551
+ * Space Complexity: O(n)
1552
+ */
1553
+ /**
1554
+ * Time Complexity: O(n)
1555
+ * Space Complexity: O(n)
1556
+ *
1514
1557
  * The `print` function is used to display a binary tree structure in a visually appealing way.
1515
1558
  * @param {K | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `K | N | null |
1516
1559
  * undefined`. It represents the root node of a binary tree. The root node can have one of the
@@ -1593,24 +1636,33 @@ export class BinaryTree extends IterableEntryBase {
1593
1636
  function _buildNodeDisplay(line, width, left, right) {
1594
1637
  const [leftLines, leftWidth, leftHeight, leftMiddle] = left;
1595
1638
  const [rightLines, rightWidth, rightHeight, rightMiddle] = right;
1596
- const firstLine = ' '.repeat(Math.max(0, leftMiddle + 1))
1597
- + '_'.repeat(Math.max(0, leftWidth - leftMiddle - 1))
1598
- + line
1599
- + '_'.repeat(Math.max(0, rightMiddle))
1600
- + ' '.repeat(Math.max(0, rightWidth - rightMiddle));
1601
- const secondLine = (leftHeight > 0 ? ' '.repeat(leftMiddle) + '/' + ' '.repeat(leftWidth - leftMiddle - 1) : ' '.repeat(leftWidth))
1602
- + ' '.repeat(width)
1603
- + (rightHeight > 0 ? ' '.repeat(rightMiddle) + '\\' + ' '.repeat(rightWidth - rightMiddle - 1) : ' '.repeat(rightWidth));
1639
+ const firstLine = ' '.repeat(Math.max(0, leftMiddle + 1)) +
1640
+ '_'.repeat(Math.max(0, leftWidth - leftMiddle - 1)) +
1641
+ line +
1642
+ '_'.repeat(Math.max(0, rightMiddle)) +
1643
+ ' '.repeat(Math.max(0, rightWidth - rightMiddle));
1644
+ const secondLine = (leftHeight > 0
1645
+ ? ' '.repeat(leftMiddle) + '/' + ' '.repeat(leftWidth - leftMiddle - 1)
1646
+ : ' '.repeat(leftWidth)) +
1647
+ ' '.repeat(width) +
1648
+ (rightHeight > 0
1649
+ ? ' '.repeat(rightMiddle) + '\\' + ' '.repeat(rightWidth - rightMiddle - 1)
1650
+ : ' '.repeat(rightWidth));
1604
1651
  const mergedLines = [firstLine, secondLine];
1605
1652
  for (let i = 0; i < Math.max(leftHeight, rightHeight); i++) {
1606
1653
  const leftLine = i < leftHeight ? leftLines[i] : ' '.repeat(leftWidth);
1607
1654
  const rightLine = i < rightHeight ? rightLines[i] : ' '.repeat(rightWidth);
1608
1655
  mergedLines.push(leftLine + ' '.repeat(width) + rightLine);
1609
1656
  }
1610
- return [mergedLines, leftWidth + width + rightWidth, Math.max(leftHeight, rightHeight) + 2, leftWidth + Math.floor(width / 2)];
1657
+ return [
1658
+ mergedLines,
1659
+ leftWidth + width + rightWidth,
1660
+ Math.max(leftHeight, rightHeight) + 2,
1661
+ leftWidth + Math.floor(width / 2)
1662
+ ];
1611
1663
  }
1612
1664
  }
1613
- _defaultOneParamCallback = (node) => node ? node.key : undefined;
1665
+ _defaultOneParamCallback = (node) => (node ? node.key : undefined);
1614
1666
  /**
1615
1667
  * Swap the data of two nodes in the binary tree.
1616
1668
  * @param {N} srcNode - The source node to swap.
@@ -1658,45 +1710,6 @@ export class BinaryTree extends IterableEntryBase {
1658
1710
  }
1659
1711
  return newNode;
1660
1712
  }
1661
- /**
1662
- * The function `_addTo` adds a new node to a binary tree if there is an available position.
1663
- * @param {N | null | undefined} newNode - The `newNode` parameter represents the node that you want to add to
1664
- * the binary tree. It can be either a node object or `null`.
1665
- * @param {N} parent - The `parent` parameter represents the parent node to which the new node will
1666
- * be added as a child.
1667
- * @returns either the left or right child node of the parent node, depending on which child is
1668
- * available for adding the new node. If a new node is added, the function also updates the size of
1669
- * the binary tree. If neither the left nor right child is available, the function returns undefined.
1670
- * If the parent node is null, the function also returns undefined.
1671
- */
1672
- _addTo(newNode, parent) {
1673
- if (this.isNotNodeInstance(parent))
1674
- parent = this.getNode(parent);
1675
- if (parent) {
1676
- // When all leaf nodes are null, it will no longer be possible to add new entity nodes to this binary tree.
1677
- // In this scenario, null nodes serve as "sentinel nodes," "virtual nodes," or "placeholder nodes."
1678
- if (parent.left === undefined) {
1679
- parent.left = newNode;
1680
- if (newNode) {
1681
- this._size = this.size + 1;
1682
- }
1683
- return parent.left;
1684
- }
1685
- else if (parent.right === undefined) {
1686
- parent.right = newNode;
1687
- if (newNode) {
1688
- this._size = this.size + 1;
1689
- }
1690
- return parent.right;
1691
- }
1692
- else {
1693
- return;
1694
- }
1695
- }
1696
- else {
1697
- return;
1698
- }
1699
- }
1700
1713
  /**
1701
1714
  * The function sets the root property of an object to a given value, and if the value is not null,
1702
1715
  * it also sets the parent property of the value to undefined.