data-structure-typed 1.49.5 → 1.49.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (222) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +17 -23
  3. package/README_zh-CN.md +2 -0
  4. package/benchmark/report.html +14 -23
  5. package/benchmark/report.json +158 -251
  6. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
  7. package/dist/cjs/data-structures/binary-tree/avl-tree.js +55 -49
  8. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  9. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +153 -130
  10. package/dist/cjs/data-structures/binary-tree/binary-tree.js +194 -153
  11. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  12. package/dist/cjs/data-structures/binary-tree/bst.d.ts +83 -71
  13. package/dist/cjs/data-structures/binary-tree/bst.js +114 -91
  14. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  15. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
  16. package/dist/cjs/data-structures/binary-tree/rb-tree.js +62 -59
  17. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +46 -39
  19. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +58 -51
  20. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  21. package/dist/cjs/data-structures/hash/hash-map.d.ts +24 -27
  22. package/dist/cjs/data-structures/hash/hash-map.js +35 -35
  23. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  24. package/dist/cjs/data-structures/hash/index.d.ts +0 -1
  25. package/dist/cjs/data-structures/hash/index.js +0 -1
  26. package/dist/cjs/data-structures/hash/index.js.map +1 -1
  27. package/dist/cjs/data-structures/heap/heap.d.ts +2 -1
  28. package/dist/cjs/data-structures/heap/heap.js +13 -13
  29. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  30. package/dist/cjs/data-structures/heap/max-heap.js +1 -1
  31. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  32. package/dist/cjs/data-structures/heap/min-heap.js +1 -1
  33. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  34. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +1 -1
  35. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  36. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +1 -3
  37. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  38. package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +2 -8
  39. package/dist/cjs/data-structures/linked-list/skip-linked-list.js +15 -18
  40. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
  41. package/dist/cjs/data-structures/matrix/matrix.d.ts +2 -7
  42. package/dist/cjs/data-structures/matrix/matrix.js +0 -7
  43. package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
  44. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +1 -1
  45. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  46. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +1 -1
  47. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  48. package/dist/cjs/data-structures/priority-queue/priority-queue.js +1 -1
  49. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  50. package/dist/cjs/data-structures/queue/deque.d.ts +2 -11
  51. package/dist/cjs/data-structures/queue/deque.js +9 -13
  52. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  53. package/dist/cjs/data-structures/queue/queue.d.ts +13 -13
  54. package/dist/cjs/data-structures/queue/queue.js +29 -25
  55. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  56. package/dist/cjs/data-structures/stack/stack.js +2 -3
  57. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  58. package/dist/cjs/data-structures/trie/trie.d.ts +2 -2
  59. package/dist/cjs/data-structures/trie/trie.js +9 -5
  60. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  61. package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
  62. package/dist/cjs/types/common.d.ts +3 -3
  63. package/dist/cjs/types/common.js +2 -2
  64. package/dist/cjs/types/common.js.map +1 -1
  65. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
  66. package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +1 -1
  67. package/dist/cjs/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
  68. package/dist/cjs/types/data-structures/hash/hash-map.d.ts +5 -2
  69. package/dist/cjs/types/data-structures/hash/index.d.ts +0 -1
  70. package/dist/cjs/types/data-structures/hash/index.js +0 -1
  71. package/dist/cjs/types/data-structures/hash/index.js.map +1 -1
  72. package/dist/cjs/types/data-structures/heap/heap.d.ts +1 -1
  73. package/dist/cjs/types/data-structures/linked-list/index.d.ts +1 -0
  74. package/dist/cjs/types/data-structures/linked-list/index.js +1 -0
  75. package/dist/cjs/types/data-structures/linked-list/index.js.map +1 -1
  76. package/dist/cjs/types/data-structures/linked-list/skip-linked-list.d.ts +4 -1
  77. package/dist/cjs/types/data-structures/matrix/index.d.ts +1 -0
  78. package/dist/cjs/types/data-structures/matrix/index.js +1 -0
  79. package/dist/cjs/types/data-structures/matrix/index.js.map +1 -1
  80. package/dist/cjs/types/data-structures/matrix/matrix.d.ts +7 -1
  81. package/dist/cjs/types/data-structures/queue/deque.d.ts +3 -1
  82. package/dist/cjs/types/data-structures/trie/trie.d.ts +3 -1
  83. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
  84. package/dist/mjs/data-structures/binary-tree/avl-tree.js +55 -49
  85. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +153 -130
  86. package/dist/mjs/data-structures/binary-tree/binary-tree.js +194 -153
  87. package/dist/mjs/data-structures/binary-tree/bst.d.ts +83 -71
  88. package/dist/mjs/data-structures/binary-tree/bst.js +114 -91
  89. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
  90. package/dist/mjs/data-structures/binary-tree/rb-tree.js +62 -59
  91. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +46 -39
  92. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +58 -51
  93. package/dist/mjs/data-structures/hash/hash-map.d.ts +24 -27
  94. package/dist/mjs/data-structures/hash/hash-map.js +35 -37
  95. package/dist/mjs/data-structures/hash/index.d.ts +0 -1
  96. package/dist/mjs/data-structures/hash/index.js +0 -1
  97. package/dist/mjs/data-structures/heap/heap.d.ts +2 -1
  98. package/dist/mjs/data-structures/heap/heap.js +19 -20
  99. package/dist/mjs/data-structures/heap/max-heap.js +1 -1
  100. package/dist/mjs/data-structures/heap/min-heap.js +1 -1
  101. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +1 -1
  102. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +2 -5
  103. package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +2 -8
  104. package/dist/mjs/data-structures/linked-list/skip-linked-list.js +16 -23
  105. package/dist/mjs/data-structures/matrix/matrix.d.ts +2 -7
  106. package/dist/mjs/data-structures/matrix/matrix.js +0 -7
  107. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +1 -1
  108. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +1 -1
  109. package/dist/mjs/data-structures/priority-queue/priority-queue.js +1 -1
  110. package/dist/mjs/data-structures/queue/deque.d.ts +2 -11
  111. package/dist/mjs/data-structures/queue/deque.js +9 -14
  112. package/dist/mjs/data-structures/queue/queue.d.ts +13 -13
  113. package/dist/mjs/data-structures/queue/queue.js +30 -28
  114. package/dist/mjs/data-structures/stack/stack.js +3 -5
  115. package/dist/mjs/data-structures/trie/trie.d.ts +2 -2
  116. package/dist/mjs/data-structures/trie/trie.js +10 -9
  117. package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
  118. package/dist/mjs/types/common.d.ts +3 -3
  119. package/dist/mjs/types/common.js +2 -2
  120. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
  121. package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +1 -1
  122. package/dist/mjs/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
  123. package/dist/mjs/types/data-structures/hash/hash-map.d.ts +5 -2
  124. package/dist/mjs/types/data-structures/hash/index.d.ts +0 -1
  125. package/dist/mjs/types/data-structures/hash/index.js +0 -1
  126. package/dist/mjs/types/data-structures/heap/heap.d.ts +1 -1
  127. package/dist/mjs/types/data-structures/linked-list/index.d.ts +1 -0
  128. package/dist/mjs/types/data-structures/linked-list/index.js +1 -0
  129. package/dist/mjs/types/data-structures/linked-list/skip-linked-list.d.ts +4 -1
  130. package/dist/mjs/types/data-structures/matrix/index.d.ts +1 -0
  131. package/dist/mjs/types/data-structures/matrix/index.js +1 -0
  132. package/dist/mjs/types/data-structures/matrix/matrix.d.ts +7 -1
  133. package/dist/mjs/types/data-structures/queue/deque.d.ts +3 -1
  134. package/dist/mjs/types/data-structures/trie/trie.d.ts +3 -1
  135. package/dist/umd/data-structure-typed.js +620 -823
  136. package/dist/umd/data-structure-typed.min.js +2 -2
  137. package/dist/umd/data-structure-typed.min.js.map +1 -1
  138. package/package.json +1 -1
  139. package/src/data-structures/binary-tree/avl-tree.ts +58 -53
  140. package/src/data-structures/binary-tree/binary-tree.ts +255 -211
  141. package/src/data-structures/binary-tree/bst.ts +126 -107
  142. package/src/data-structures/binary-tree/rb-tree.ts +66 -64
  143. package/src/data-structures/binary-tree/tree-multimap.ts +62 -56
  144. package/src/data-structures/hash/hash-map.ts +46 -50
  145. package/src/data-structures/hash/index.ts +0 -1
  146. package/src/data-structures/heap/heap.ts +20 -19
  147. package/src/data-structures/heap/max-heap.ts +1 -1
  148. package/src/data-structures/heap/min-heap.ts +1 -1
  149. package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
  150. package/src/data-structures/linked-list/singly-linked-list.ts +2 -5
  151. package/src/data-structures/linked-list/skip-linked-list.ts +15 -16
  152. package/src/data-structures/matrix/matrix.ts +2 -10
  153. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
  154. package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
  155. package/src/data-structures/priority-queue/priority-queue.ts +1 -1
  156. package/src/data-structures/queue/deque.ts +11 -15
  157. package/src/data-structures/queue/queue.ts +29 -28
  158. package/src/data-structures/stack/stack.ts +3 -6
  159. package/src/data-structures/trie/trie.ts +10 -11
  160. package/src/interfaces/binary-tree.ts +3 -3
  161. package/src/types/common.ts +3 -3
  162. package/src/types/data-structures/binary-tree/binary-tree.ts +2 -2
  163. package/src/types/data-structures/binary-tree/bst.ts +1 -1
  164. package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
  165. package/src/types/data-structures/hash/hash-map.ts +6 -2
  166. package/src/types/data-structures/hash/index.ts +0 -1
  167. package/src/types/data-structures/heap/heap.ts +1 -1
  168. package/src/types/data-structures/linked-list/index.ts +1 -0
  169. package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -1
  170. package/src/types/data-structures/matrix/index.ts +1 -0
  171. package/src/types/data-structures/matrix/matrix.ts +7 -1
  172. package/src/types/data-structures/queue/deque.ts +1 -1
  173. package/src/types/data-structures/trie/trie.ts +1 -1
  174. package/test/performance/data-structures/binary-tree/avl-tree.test.ts +4 -12
  175. package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +37 -0
  176. package/test/performance/data-structures/binary-tree/binary-tree.test.ts +6 -16
  177. package/test/performance/data-structures/binary-tree/bst.test.ts +5 -13
  178. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +5 -15
  179. package/test/performance/data-structures/comparison/comparison.test.ts +13 -36
  180. package/test/performance/data-structures/graph/directed-graph.test.ts +3 -14
  181. package/test/performance/data-structures/hash/hash-map.test.ts +11 -34
  182. package/test/performance/data-structures/heap/heap.test.ts +5 -18
  183. package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +0 -1
  184. package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +0 -2
  185. package/test/performance/data-structures/priority-queue/max-priority-queue.test.ts +2 -4
  186. package/test/performance/data-structures/priority-queue/priority-queue.test.ts +4 -14
  187. package/test/performance/data-structures/queue/queue.test.ts +8 -25
  188. package/test/performance/data-structures/stack/stack.test.ts +6 -18
  189. package/test/performance/data-structures/trie/trie.test.ts +2 -6
  190. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +6 -5
  191. package/test/unit/data-structures/binary-tree/bst.test.ts +17 -1
  192. package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
  193. package/test/unit/data-structures/queue/deque.test.ts +5 -5
  194. package/test/unit/data-structures/trie/trie.test.ts +1 -1
  195. package/dist/cjs/data-structures/hash/hash-table.d.ts +0 -108
  196. package/dist/cjs/data-structures/hash/hash-table.js +0 -282
  197. package/dist/cjs/data-structures/hash/hash-table.js.map +0 -1
  198. package/dist/cjs/types/data-structures/hash/hash-table.d.ts +0 -1
  199. package/dist/cjs/types/data-structures/hash/hash-table.js +0 -3
  200. package/dist/cjs/types/data-structures/hash/hash-table.js.map +0 -1
  201. package/dist/cjs/types/data-structures/matrix/matrix2d.d.ts +0 -1
  202. package/dist/cjs/types/data-structures/matrix/matrix2d.js +0 -3
  203. package/dist/cjs/types/data-structures/matrix/matrix2d.js.map +0 -1
  204. package/dist/cjs/types/data-structures/matrix/vector2d.d.ts +0 -1
  205. package/dist/cjs/types/data-structures/matrix/vector2d.js +0 -3
  206. package/dist/cjs/types/data-structures/matrix/vector2d.js.map +0 -1
  207. package/dist/mjs/data-structures/hash/hash-table.d.ts +0 -108
  208. package/dist/mjs/data-structures/hash/hash-table.js +0 -283
  209. package/dist/mjs/types/data-structures/hash/hash-table.d.ts +0 -1
  210. package/dist/mjs/types/data-structures/hash/hash-table.js +0 -1
  211. package/dist/mjs/types/data-structures/matrix/matrix2d.d.ts +0 -1
  212. package/dist/mjs/types/data-structures/matrix/matrix2d.js +0 -1
  213. package/dist/mjs/types/data-structures/matrix/vector2d.d.ts +0 -1
  214. package/dist/mjs/types/data-structures/matrix/vector2d.js +0 -1
  215. package/src/data-structures/hash/hash-table.ts +0 -318
  216. package/src/types/data-structures/hash/hash-table.ts +0 -1
  217. package/src/types/data-structures/matrix/matrix2d.ts +0 -1
  218. package/src/types/data-structures/matrix/vector2d.ts +0 -1
  219. package/test/performance/data-structures/binary-tree/overall.test.ts +0 -0
  220. package/test/performance/data-structures/matrix/matrix2d.test.ts +0 -0
  221. package/test/performance/data-structures/matrix/vector2d.test.ts +0 -0
  222. package/test/unit/data-structures/hash/hash-table.test.ts +0 -238
@@ -70,28 +70,26 @@ 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 keysOrNodesOrEntries and options.
74
+ * @param [keysOrNodesOrEntries] - 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(keysOrNodesOrEntries = [], options) {
82
82
  super();
83
83
  if (options) {
84
84
  const { iterationType, extractor } = options;
85
- if (iterationType) {
85
+ if (iterationType)
86
86
  this.iterationType = iterationType;
87
- }
88
- if (extractor) {
87
+ if (extractor)
89
88
  this._extractor = extractor;
90
- }
91
89
  }
92
90
  this._size = 0;
93
- if (elements)
94
- this.addMany(elements);
91
+ if (keysOrNodesOrEntries)
92
+ this.addMany(keysOrNodesOrEntries);
95
93
  }
96
94
  _extractor = (key) => Number(key);
97
95
  get extractor() {
@@ -125,30 +123,22 @@ export class BinaryTree extends IterableEntryBase {
125
123
  return new BinaryTree([], { iterationType: this.iterationType, ...options });
126
124
  }
127
125
  /**
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>`.
126
+ * The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
127
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
138
128
  * @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
129
+ * `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If no value
140
130
  * is provided, it will be `undefined`.
141
131
  * @returns a value of type N (node), or null, or undefined.
142
132
  */
143
- exemplarToNode(exemplar, value) {
144
- if (exemplar === undefined)
133
+ exemplarToNode(keyOrNodeOrEntry, value) {
134
+ if (keyOrNodeOrEntry === undefined)
145
135
  return;
146
136
  let node;
147
- if (exemplar === null) {
137
+ if (keyOrNodeOrEntry === null) {
148
138
  node = null;
149
139
  }
150
- else if (this.isEntry(exemplar)) {
151
- const [key, value] = exemplar;
140
+ else if (this.isEntry(keyOrNodeOrEntry)) {
141
+ const [key, value] = keyOrNodeOrEntry;
152
142
  if (key === undefined) {
153
143
  return;
154
144
  }
@@ -159,25 +149,108 @@ export class BinaryTree extends IterableEntryBase {
159
149
  node = this.createNode(key, value);
160
150
  }
161
151
  }
162
- else if (this.isNode(exemplar)) {
163
- node = exemplar;
152
+ else if (this.isNode(keyOrNodeOrEntry)) {
153
+ node = keyOrNodeOrEntry;
164
154
  }
165
- else if (this.isNotNodeInstance(exemplar)) {
166
- node = this.createNode(exemplar, value);
155
+ else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
156
+ node = this.createNode(keyOrNodeOrEntry, value);
167
157
  }
168
158
  else {
169
159
  return;
170
160
  }
171
161
  return node;
172
162
  }
163
+ /**
164
+ * Time Complexity: O(n)
165
+ * Space Complexity: O(log n)
166
+ */
167
+ /**
168
+ * Time Complexity: O(n)
169
+ * Space Complexity: O(log n)
170
+ *
171
+ * The function `ensureNode` returns the node corresponding to the given key if it is a valid node
172
+ * key, otherwise it returns the key itself.
173
+ * @param {K | N | null | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`,
174
+ * `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
175
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
176
+ * type of iteration to be used when searching for a node by key. It has a default value of
177
+ * `IterationType.ITERATIVE`.
178
+ * @returns either the node corresponding to the given key if it is a valid node key, or the key
179
+ * itself if it is not a valid node key.
180
+ */
181
+ ensureNode(keyOrNodeOrEntry, iterationType = IterationType.ITERATIVE) {
182
+ let res;
183
+ if (this.isRealNode(keyOrNodeOrEntry)) {
184
+ res = keyOrNodeOrEntry;
185
+ }
186
+ else if (this.isEntry(keyOrNodeOrEntry)) {
187
+ if (keyOrNodeOrEntry[0] === null)
188
+ res = null;
189
+ else if (keyOrNodeOrEntry[0] !== undefined)
190
+ res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
191
+ }
192
+ else {
193
+ if (keyOrNodeOrEntry === null)
194
+ res = null;
195
+ else if (keyOrNodeOrEntry !== undefined)
196
+ res = this.getNodeByKey(keyOrNodeOrEntry, iterationType);
197
+ }
198
+ return res;
199
+ }
200
+ /**
201
+ * The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
202
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,N>`.
203
+ * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the class N.
204
+ */
205
+ isNode(keyOrNodeOrEntry) {
206
+ return keyOrNodeOrEntry instanceof BinaryTreeNode;
207
+ }
173
208
  /**
174
209
  * 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
210
+ * @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,N> - A generic type representing a node in a binary tree. It has
176
211
  * two type parameters V and N, representing the value and node type respectively.
177
212
  * @returns a boolean value.
178
213
  */
179
- isEntry(kne) {
180
- return Array.isArray(kne) && kne.length === 2;
214
+ isEntry(keyOrNodeOrEntry) {
215
+ return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
216
+ }
217
+ /**
218
+ * Time complexity: O(n)
219
+ * Space complexity: O(log n)
220
+ */
221
+ /**
222
+ * The function checks if a given node is a real node by verifying if it is an instance of
223
+ * BinaryTreeNode and its key is not NaN.
224
+ * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
225
+ * @returns a boolean value.
226
+ */
227
+ isRealNode(node) {
228
+ return node instanceof BinaryTreeNode && String(node.key) !== 'NaN';
229
+ }
230
+ /**
231
+ * The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
232
+ * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
233
+ * @returns a boolean value.
234
+ */
235
+ isNIL(node) {
236
+ return node instanceof BinaryTreeNode && String(node.key) === 'NaN';
237
+ }
238
+ /**
239
+ * The function checks if a given node is a real node or null.
240
+ * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
241
+ * @returns a boolean value.
242
+ */
243
+ isNodeOrNull(node) {
244
+ return this.isRealNode(node) || node === null;
245
+ }
246
+ /**
247
+ * The function "isNotNodeInstance" checks if a potential key is a K.
248
+ * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
249
+ * data type.
250
+ * @returns a boolean value indicating whether the potentialKey is of type number or not.
251
+ */
252
+ isNotNodeInstance(potentialKey) {
253
+ return !(potentialKey instanceof BinaryTreeNode);
181
254
  }
182
255
  /**
183
256
  * Time Complexity O(log n) - O(n)
@@ -196,12 +269,12 @@ export class BinaryTree extends IterableEntryBase {
196
269
  add(keyOrNodeOrEntry, value) {
197
270
  const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
198
271
  if (newNode === undefined)
199
- return;
272
+ return false;
200
273
  // If the tree is empty, directly set the new node as the root node
201
274
  if (!this.root) {
202
275
  this._root = newNode;
203
276
  this._size = 1;
204
- return newNode;
277
+ return true;
205
278
  }
206
279
  const queue = new Queue([this.root]);
207
280
  let potentialParent; // Record the parent node of the potential insertion location
@@ -212,7 +285,7 @@ export class BinaryTree extends IterableEntryBase {
212
285
  // Check for duplicate keys when newNode is not null
213
286
  if (newNode !== null && cur.key === newNode.key) {
214
287
  this._replaceNode(cur, newNode);
215
- return newNode; // If duplicate keys are found, no insertion is performed
288
+ return true; // If duplicate keys are found, no insertion is performed
216
289
  }
217
290
  // Record the first possible insertion location found
218
291
  if (potentialParent === undefined && (cur.left === undefined || cur.right === undefined)) {
@@ -235,9 +308,9 @@ export class BinaryTree extends IterableEntryBase {
235
308
  potentialParent.right = newNode;
236
309
  }
237
310
  this._size++;
238
- return newNode;
311
+ return true;
239
312
  }
240
- return undefined; // If the insertion position cannot be found, return undefined
313
+ return false; // If the insertion position cannot be found, return undefined
241
314
  }
242
315
  /**
243
316
  * Time Complexity: O(k log n) - O(k * n)
@@ -248,20 +321,20 @@ export class BinaryTree extends IterableEntryBase {
248
321
  * Time Complexity: O(k log n) - O(k * n)
249
322
  * Space Complexity: O(1)
250
323
  *
251
- * The `addMany` function takes in a collection of nodes and an optional collection of values, and
324
+ * The `addMany` function takes in a collection of keysOrNodesOrEntries and an optional collection of values, and
252
325
  * adds each node with its corresponding value to the data structure.
253
- * @param nodes - An iterable collection of BTNExemplar objects.
326
+ * @param keysOrNodesOrEntries - An iterable collection of KeyOrNodeOrEntry objects.
254
327
  * @param [values] - An optional iterable of values that will be assigned to each node being added.
255
328
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
256
329
  */
257
- addMany(nodes, values) {
330
+ addMany(keysOrNodesOrEntries, values) {
258
331
  // TODO not sure addMany not be run multi times
259
332
  const inserted = [];
260
333
  let valuesIterator;
261
334
  if (values) {
262
335
  valuesIterator = values[Symbol.iterator]();
263
336
  }
264
- for (const kne of nodes) {
337
+ for (const keyOrNodeOrEntry of keysOrNodesOrEntries) {
265
338
  let value = undefined;
266
339
  if (valuesIterator) {
267
340
  const valueResult = valuesIterator.next();
@@ -269,17 +342,30 @@ export class BinaryTree extends IterableEntryBase {
269
342
  value = valueResult.value;
270
343
  }
271
344
  }
272
- inserted.push(this.add(kne, value));
345
+ inserted.push(this.add(keyOrNodeOrEntry, value));
273
346
  }
274
347
  return inserted;
275
348
  }
276
349
  /**
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.
350
+ * Time Complexity: O(k * n)
278
351
  * Space Complexity: O(1)
352
+ * "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
279
353
  */
280
- refill(nodesOrKeysOrEntries, values) {
354
+ /**
355
+ * Time Complexity: O(k * n)
356
+ * Space Complexity: O(1)
357
+ *
358
+ * The `refill` function clears the current data and adds new key-value pairs to the data structure.
359
+ * @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries. These can be of type
360
+ * KeyOrNodeOrEntry<K, V, N>.
361
+ * @param [values] - The `values` parameter is an optional iterable that contains the values to be
362
+ * associated with the keys or nodes or entries in the `keysOrNodesOrEntries` parameter. If provided,
363
+ * the values will be associated with the corresponding keys or nodes or entries in the
364
+ * `keysOrNodesOrEntries` iterable
365
+ */
366
+ refill(keysOrNodesOrEntries, values) {
281
367
  this.clear();
282
- this.addMany(nodesOrKeysOrEntries, values);
368
+ this.addMany(keysOrNodesOrEntries, values);
283
369
  }
284
370
  /**
285
371
  * Time Complexity: O(n)
@@ -353,24 +439,24 @@ export class BinaryTree extends IterableEntryBase {
353
439
  * Space Complexity: O(1)
354
440
  *
355
441
  * 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
442
+ * @param {K | N | null | undefined} dist - The `dist` parameter represents the node in
357
443
  * the binary tree whose depth we want to find. It can be of type `K`, `N`, `null`, or
358
444
  * `undefined`.
359
445
  * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
360
446
  * from which we want to calculate the depth. It can be either a `K` (binary tree node key) or
361
447
  * `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`.
448
+ * @returns the depth of the `dist` relative to the `beginRoot`.
363
449
  */
364
- getDepth(distNode, beginRoot = this.root) {
365
- distNode = this.ensureNode(distNode);
450
+ getDepth(dist, beginRoot = this.root) {
451
+ dist = this.ensureNode(dist);
366
452
  beginRoot = this.ensureNode(beginRoot);
367
453
  let depth = 0;
368
- while (distNode?.parent) {
369
- if (distNode === beginRoot) {
454
+ while (dist?.parent) {
455
+ if (dist === beginRoot) {
370
456
  return depth;
371
457
  }
372
458
  depth++;
373
- distNode = distNode.parent;
459
+ dist = dist.parent;
374
460
  }
375
461
  return depth;
376
462
  }
@@ -566,6 +652,7 @@ export class BinaryTree extends IterableEntryBase {
566
652
  }
567
653
  /**
568
654
  * Time Complexity: O(n)
655
+ * Space Complexity: O(log n).
569
656
  *
570
657
  * The function checks if a Binary Tree Node with a specific identifier exists in the tree.
571
658
  * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
@@ -662,24 +749,6 @@ export class BinaryTree extends IterableEntryBase {
662
749
  }
663
750
  }
664
751
  }
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
752
  /**
684
753
  * Time Complexity: O(n)
685
754
  * Space Complexity: O(log n)
@@ -708,10 +777,13 @@ export class BinaryTree extends IterableEntryBase {
708
777
  return this.getNode(identifier, callback, beginRoot, iterationType)?.value ?? undefined;
709
778
  }
710
779
  /**
711
- * Time Complexity: O(n)
712
- * Space Complexity: O(log n)
780
+ * Time Complexity: O(1)
781
+ * Space Complexity: O(1)
713
782
  */
714
783
  /**
784
+ * Time Complexity: O(1)
785
+ * Space Complexity: O(1)
786
+ *
715
787
  * Clear the binary tree, removing all nodes.
716
788
  */
717
789
  clear() {
@@ -719,6 +791,13 @@ export class BinaryTree extends IterableEntryBase {
719
791
  this._size = 0;
720
792
  }
721
793
  /**
794
+ * Time Complexity: O(1)
795
+ * Space Complexity: O(1)
796
+ */
797
+ /**
798
+ * Time Complexity: O(1)
799
+ * Space Complexity: O(1)
800
+ *
722
801
  * Check if the binary tree is empty.
723
802
  * @returns {boolean} - True if the binary tree is empty, false otherwise.
724
803
  */
@@ -756,7 +835,7 @@ export class BinaryTree extends IterableEntryBase {
756
835
  }
757
836
  /**
758
837
  * Time Complexity: O(log n)
759
- * Space Complexity: O(log n)
838
+ * Space Complexity: O(1)
760
839
  */
761
840
  /**
762
841
  * Time Complexity: O(log n)
@@ -852,7 +931,7 @@ export class BinaryTree extends IterableEntryBase {
852
931
  * possible values:
853
932
  * @returns a boolean value.
854
933
  */
855
- isSubtreeBST(beginRoot, iterationType = this.iterationType) {
934
+ isBST(beginRoot = this.root, iterationType = this.iterationType) {
856
935
  // TODO there is a bug
857
936
  beginRoot = this.ensureNode(beginRoot);
858
937
  if (!beginRoot)
@@ -866,46 +945,34 @@ export class BinaryTree extends IterableEntryBase {
866
945
  return false;
867
946
  return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
868
947
  };
869
- return dfs(beginRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
948
+ const isStandardBST = dfs(beginRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
949
+ const isInverseBST = dfs(beginRoot, Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
950
+ return isStandardBST || isInverseBST;
870
951
  }
871
952
  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;
953
+ const checkBST = (checkMax = false) => {
954
+ const stack = [];
955
+ let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
956
+ // @ts-ignore
957
+ let curr = beginRoot;
958
+ while (curr || stack.length > 0) {
959
+ while (curr) {
960
+ stack.push(curr);
961
+ curr = curr.left;
962
+ }
963
+ curr = stack.pop();
964
+ const numKey = this.extractor(curr.key);
965
+ if (!curr || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey))
966
+ return false;
967
+ prev = numKey;
968
+ curr = curr.right;
878
969
  }
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;
970
+ return true;
971
+ };
972
+ const isStandardBST = checkBST(false), isInverseBST = checkBST(true);
973
+ return isStandardBST || isInverseBST;
887
974
  }
888
975
  }
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
976
  /**
910
977
  * Time complexity: O(n)
911
978
  * Space complexity: O(log n)
@@ -925,7 +992,7 @@ export class BinaryTree extends IterableEntryBase {
925
992
  * whether to include null values in the traversal. If `includeNull` is set to `true`, the
926
993
  * traversal will include null values, otherwise it will skip them.
927
994
  * @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
995
+ * the `callback` function on each node in the subtree. The type of the array nodes is determined
929
996
  * by the return type of the `callback` function.
930
997
  */
931
998
  subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
@@ -968,44 +1035,6 @@ export class BinaryTree extends IterableEntryBase {
968
1035
  }
969
1036
  return ans;
970
1037
  }
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
1038
  /**
1010
1039
  * Time complexity: O(n)
1011
1040
  * Space complexity: O(n)
@@ -1327,7 +1356,12 @@ export class BinaryTree extends IterableEntryBase {
1327
1356
  }
1328
1357
  /**
1329
1358
  * Time complexity: O(n)
1330
- * Space complexity: O(1)
1359
+ * Space complexity: O(n)
1360
+ */
1361
+ /**
1362
+ * Time complexity: O(n)
1363
+ * Space complexity: O(n)
1364
+ *
1331
1365
  * The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
1332
1366
  * algorithm.
1333
1367
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
@@ -1340,7 +1374,7 @@ export class BinaryTree extends IterableEntryBase {
1340
1374
  * for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
1341
1375
  * the root of the tree. If no value is provided, the default value is the root of the tree.
1342
1376
  * @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
1377
+ * `callback` function on each node in the binary tree. The type of the array nodes is determined
1344
1378
  * by the return type of the `callback` function.
1345
1379
  */
1346
1380
  morris(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root) {
@@ -1453,8 +1487,8 @@ export class BinaryTree extends IterableEntryBase {
1453
1487
  * Time Complexity: O(n)
1454
1488
  * Space Complexity: O(n)
1455
1489
  *
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.
1490
+ * The `filter` function creates a new tree by iterating over the nodes of the current tree and
1491
+ * adding only the nodes that satisfy the given predicate function.
1458
1492
  * @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
1459
1493
  * `key`, and `index`. It should return a boolean value indicating whether the pair should be
1460
1494
  * included in the filtered tree or not.
@@ -1511,6 +1545,13 @@ export class BinaryTree extends IterableEntryBase {
1511
1545
  // // }
1512
1546
  //
1513
1547
  /**
1548
+ * Time Complexity: O(n)
1549
+ * Space Complexity: O(n)
1550
+ */
1551
+ /**
1552
+ * Time Complexity: O(n)
1553
+ * Space Complexity: O(n)
1554
+ *
1514
1555
  * The `print` function is used to display a binary tree structure in a visually appealing way.
1515
1556
  * @param {K | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `K | N | null |
1516
1557
  * undefined`. It represents the root node of a binary tree. The root node can have one of the