data-structure-typed 1.34.7 → 1.34.9

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 (167) hide show
  1. package/.eslintrc.js +1 -0
  2. package/CHANGELOG.md +1 -1
  3. package/CONTRIBUTING.md +0 -0
  4. package/README.md +8 -8
  5. package/dist/data-structures/binary-tree/aa-tree.js +2 -5
  6. package/dist/data-structures/binary-tree/aa-tree.js.map +1 -1
  7. package/dist/data-structures/binary-tree/abstract-binary-tree.js +314 -457
  8. package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
  9. package/dist/data-structures/binary-tree/avl-tree.js +64 -90
  10. package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
  11. package/dist/data-structures/binary-tree/b-tree.js +2 -5
  12. package/dist/data-structures/binary-tree/b-tree.js.map +1 -1
  13. package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
  14. package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
  15. package/dist/data-structures/binary-tree/binary-tree.js +12 -31
  16. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
  17. package/dist/data-structures/binary-tree/bst.js +157 -244
  18. package/dist/data-structures/binary-tree/bst.js.map +1 -1
  19. package/dist/data-structures/binary-tree/rb-tree.js +20 -44
  20. package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
  21. package/dist/data-structures/binary-tree/segment-tree.js +80 -122
  22. package/dist/data-structures/binary-tree/segment-tree.js.map +1 -1
  23. package/dist/data-structures/binary-tree/splay-tree.js +2 -5
  24. package/dist/data-structures/binary-tree/splay-tree.js.map +1 -1
  25. package/dist/data-structures/binary-tree/tree-multiset.js +168 -239
  26. package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
  27. package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
  28. package/dist/data-structures/binary-tree/two-three-tree.js.map +1 -1
  29. package/dist/data-structures/graph/abstract-graph.js +351 -585
  30. package/dist/data-structures/graph/abstract-graph.js.map +1 -1
  31. package/dist/data-structures/graph/directed-graph.js +152 -282
  32. package/dist/data-structures/graph/directed-graph.js.map +1 -1
  33. package/dist/data-structures/graph/map-graph.js +47 -88
  34. package/dist/data-structures/graph/map-graph.js.map +1 -1
  35. package/dist/data-structures/graph/undirected-graph.js +91 -180
  36. package/dist/data-structures/graph/undirected-graph.js.map +1 -1
  37. package/dist/data-structures/hash/coordinate-map.js +23 -45
  38. package/dist/data-structures/hash/coordinate-map.js.map +1 -1
  39. package/dist/data-structures/hash/coordinate-set.js +20 -42
  40. package/dist/data-structures/hash/coordinate-set.js.map +1 -1
  41. package/dist/data-structures/hash/hash-map.js +85 -247
  42. package/dist/data-structures/hash/hash-map.js.map +1 -1
  43. package/dist/data-structures/hash/hash-table.js +87 -128
  44. package/dist/data-structures/hash/hash-table.js.map +1 -1
  45. package/dist/data-structures/hash/pair.js +2 -5
  46. package/dist/data-structures/hash/pair.js.map +1 -1
  47. package/dist/data-structures/hash/tree-map.js +2 -5
  48. package/dist/data-structures/hash/tree-map.js.map +1 -1
  49. package/dist/data-structures/hash/tree-set.js +2 -5
  50. package/dist/data-structures/hash/tree-set.js.map +1 -1
  51. package/dist/data-structures/heap/heap.js +56 -80
  52. package/dist/data-structures/heap/heap.js.map +1 -1
  53. package/dist/data-structures/heap/max-heap.js +8 -26
  54. package/dist/data-structures/heap/max-heap.js.map +1 -1
  55. package/dist/data-structures/heap/min-heap.js +8 -26
  56. package/dist/data-structures/heap/min-heap.js.map +1 -1
  57. package/dist/data-structures/linked-list/doubly-linked-list.js +149 -218
  58. package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  59. package/dist/data-structures/linked-list/singly-linked-list.js +119 -218
  60. package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -1
  61. package/dist/data-structures/linked-list/skip-linked-list.js +50 -70
  62. package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
  63. package/dist/data-structures/matrix/matrix.js +7 -8
  64. package/dist/data-structures/matrix/matrix.js.map +1 -1
  65. package/dist/data-structures/matrix/matrix2d.js +57 -70
  66. package/dist/data-structures/matrix/matrix2d.js.map +1 -1
  67. package/dist/data-structures/matrix/navigator.js +18 -37
  68. package/dist/data-structures/matrix/navigator.js.map +1 -1
  69. package/dist/data-structures/matrix/vector2d.js +63 -84
  70. package/dist/data-structures/matrix/vector2d.js.map +1 -1
  71. package/dist/data-structures/priority-queue/max-priority-queue.js +13 -41
  72. package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  73. package/dist/data-structures/priority-queue/min-priority-queue.js +13 -41
  74. package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  75. package/dist/data-structures/priority-queue/priority-queue.js +93 -139
  76. package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
  77. package/dist/data-structures/queue/deque.js +82 -128
  78. package/dist/data-structures/queue/deque.js.map +1 -1
  79. package/dist/data-structures/queue/queue.js +57 -157
  80. package/dist/data-structures/queue/queue.js.map +1 -1
  81. package/dist/data-structures/stack/stack.js +21 -22
  82. package/dist/data-structures/stack/stack.js.map +1 -1
  83. package/dist/data-structures/tree/tree.js +33 -46
  84. package/dist/data-structures/tree/tree.js.map +1 -1
  85. package/dist/data-structures/trie/trie.js +93 -200
  86. package/dist/data-structures/trie/trie.js.map +1 -1
  87. package/dist/types/data-structures/abstract-binary-tree.js.map +1 -1
  88. package/dist/utils/utils.js +22 -107
  89. package/dist/utils/utils.js.map +1 -1
  90. package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +124 -123
  91. package/lib/data-structures/binary-tree/abstract-binary-tree.js +136 -216
  92. package/lib/data-structures/binary-tree/avl-tree.d.ts +19 -10
  93. package/lib/data-structures/binary-tree/avl-tree.js +35 -13
  94. package/lib/data-structures/binary-tree/binary-tree.d.ts +6 -6
  95. package/lib/data-structures/binary-tree/binary-tree.js +7 -7
  96. package/lib/data-structures/binary-tree/bst.d.ts +34 -34
  97. package/lib/data-structures/binary-tree/bst.js +86 -89
  98. package/lib/data-structures/binary-tree/rb-tree.d.ts +3 -3
  99. package/lib/data-structures/binary-tree/rb-tree.js +4 -4
  100. package/lib/data-structures/binary-tree/tree-multiset.d.ts +40 -42
  101. package/lib/data-structures/binary-tree/tree-multiset.js +76 -86
  102. package/lib/data-structures/graph/abstract-graph.d.ts +62 -62
  103. package/lib/data-structures/graph/abstract-graph.js +86 -86
  104. package/lib/data-structures/graph/directed-graph.d.ts +51 -51
  105. package/lib/data-structures/graph/directed-graph.js +63 -63
  106. package/lib/data-structures/graph/map-graph.d.ts +13 -13
  107. package/lib/data-structures/graph/map-graph.js +12 -12
  108. package/lib/data-structures/graph/undirected-graph.d.ts +30 -30
  109. package/lib/data-structures/graph/undirected-graph.js +32 -32
  110. package/lib/data-structures/priority-queue/priority-queue.d.ts +3 -3
  111. package/lib/data-structures/priority-queue/priority-queue.js +3 -3
  112. package/lib/data-structures/tree/tree.d.ts +4 -4
  113. package/lib/data-structures/tree/tree.js +6 -6
  114. package/lib/interfaces/abstract-binary-tree.d.ts +37 -42
  115. package/lib/interfaces/abstract-graph.d.ts +13 -13
  116. package/lib/interfaces/avl-tree.d.ts +6 -4
  117. package/lib/interfaces/bst.d.ts +10 -9
  118. package/lib/interfaces/directed-graph.d.ts +5 -5
  119. package/lib/interfaces/rb-tree.d.ts +2 -2
  120. package/lib/interfaces/undirected-graph.d.ts +2 -2
  121. package/lib/types/data-structures/abstract-binary-tree.d.ts +3 -3
  122. package/lib/types/data-structures/abstract-binary-tree.js +0 -1
  123. package/lib/types/data-structures/abstract-graph.d.ts +2 -2
  124. package/lib/types/data-structures/bst.d.ts +2 -2
  125. package/lib/types/data-structures/tree-multiset.d.ts +1 -1
  126. package/lib/types/utils/validate-type.d.ts +8 -8
  127. package/package.json +6 -6
  128. package/scripts/rename_clear_files.sh +29 -0
  129. package/src/data-structures/binary-tree/abstract-binary-tree.ts +241 -293
  130. package/src/data-structures/binary-tree/avl-tree.ts +43 -14
  131. package/src/data-structures/binary-tree/binary-tree.ts +8 -8
  132. package/src/data-structures/binary-tree/bst.ts +101 -96
  133. package/src/data-structures/binary-tree/rb-tree.ts +9 -9
  134. package/src/data-structures/binary-tree/tree-multiset.ts +82 -94
  135. package/src/data-structures/graph/abstract-graph.ts +114 -109
  136. package/src/data-structures/graph/directed-graph.ts +77 -77
  137. package/src/data-structures/graph/map-graph.ts +20 -15
  138. package/src/data-structures/graph/undirected-graph.ts +39 -39
  139. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  140. package/src/data-structures/tree/tree.ts +7 -7
  141. package/src/interfaces/abstract-binary-tree.ts +37 -50
  142. package/src/interfaces/abstract-graph.ts +13 -13
  143. package/src/interfaces/avl-tree.ts +6 -4
  144. package/src/interfaces/bst.ts +9 -9
  145. package/src/interfaces/directed-graph.ts +5 -5
  146. package/src/interfaces/rb-tree.ts +2 -2
  147. package/src/interfaces/undirected-graph.ts +2 -2
  148. package/src/types/data-structures/abstract-binary-tree.ts +3 -4
  149. package/src/types/data-structures/abstract-graph.ts +2 -2
  150. package/src/types/data-structures/bst.ts +2 -2
  151. package/src/types/data-structures/tree-multiset.ts +1 -1
  152. package/src/types/utils/validate-type.ts +10 -10
  153. package/test/integration/avl-tree.test.ts +24 -24
  154. package/test/integration/bst.test.ts +71 -71
  155. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +28 -28
  156. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +4 -12
  157. package/test/unit/data-structures/binary-tree/bst.test.ts +79 -79
  158. package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
  159. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +80 -80
  160. package/test/unit/data-structures/graph/directed-graph.test.ts +8 -8
  161. package/test/unit/data-structures/graph/map-graph.test.ts +4 -4
  162. package/test/unit/data-structures/graph/overall.test.ts +2 -2
  163. package/test/unit/data-structures/graph/undirected-graph.test.ts +1 -1
  164. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +4 -5
  165. package/test/unit/data-structures/tree/tree.test.ts +2 -2
  166. package/umd/bundle.min.js +1 -1
  167. package/umd/bundle.min.js.map +1 -1
@@ -12,7 +12,7 @@ import type {
12
12
  AbstractBinaryTreeNodeProperties,
13
13
  AbstractBinaryTreeNodeProperty,
14
14
  BinaryTreeDeletedResult,
15
- BinaryTreeNodeId,
15
+ BinaryTreeNodeKey,
16
16
  BinaryTreeNodePropertyName,
17
17
  DFSOrderPattern,
18
18
  NodeOrPropertyName
@@ -26,36 +26,20 @@ export abstract class AbstractBinaryTreeNode<
26
26
  > implements IAbstractBinaryTreeNode<V, NEIGHBOR>
27
27
  {
28
28
  /**
29
- * The constructor function initializes a BinaryTreeNode object with an id and an optional value.
30
- * @param {BinaryTreeNodeId} id - The `id` parameter is of type `BinaryTreeNodeId` and represents the unique identifier
29
+ * The constructor function initializes a BinaryTreeNode object with a key and an optional value.
30
+ * @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
31
31
  * of the binary tree node. It is used to distinguish one node from another in the binary tree.
32
32
  * @param {V} [val] - The "val" parameter is an optional parameter of type V. It represents the value that will be
33
33
  * stored in the binary tree node. If no value is provided, it will be set to undefined.
34
34
  */
35
- protected constructor(id: BinaryTreeNodeId, val?: V) {
36
- this._id = id;
37
- this._val = val;
35
+ protected constructor(key: BinaryTreeNodeKey, val?: V) {
36
+ this.key = key;
37
+ this.val = val;
38
38
  }
39
39
 
40
- private _id: BinaryTreeNodeId;
40
+ key: BinaryTreeNodeKey;
41
41
 
42
- get id(): BinaryTreeNodeId {
43
- return this._id;
44
- }
45
-
46
- set id(v: BinaryTreeNodeId) {
47
- this._id = v;
48
- }
49
-
50
- private _val: V | undefined;
51
-
52
- get val(): V | undefined {
53
- return this._val;
54
- }
55
-
56
- set val(value: V | undefined) {
57
- this._val = value;
58
- }
42
+ val: V | undefined;
59
43
 
60
44
  private _left: NEIGHBOR | null | undefined;
61
45
 
@@ -83,25 +67,7 @@ export abstract class AbstractBinaryTreeNode<
83
67
  this._right = v;
84
68
  }
85
69
 
86
- private _parent: NEIGHBOR | null | undefined;
87
-
88
- get parent(): NEIGHBOR | null | undefined {
89
- return this._parent;
90
- }
91
-
92
- set parent(v: NEIGHBOR | null | undefined) {
93
- this._parent = v;
94
- }
95
-
96
- private _height = 0;
97
-
98
- get height(): number {
99
- return this._height;
100
- }
101
-
102
- set height(v: number) {
103
- this._height = v;
104
- }
70
+ parent: NEIGHBOR | null | undefined;
105
71
 
106
72
  /**
107
73
  * The function determines the position of a node in a family tree structure.
@@ -169,25 +135,13 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
169
135
  return this._loopType;
170
136
  }
171
137
 
172
- private _visitedId: BinaryTreeNodeId[] = [];
173
-
174
- get visitedId(): BinaryTreeNodeId[] {
175
- return this._visitedId;
176
- }
177
-
178
- private _visitedVal: N['val'][] = [];
138
+ visitedKey: BinaryTreeNodeKey[] = [];
179
139
 
180
- get visitedVal(): N['val'][] {
181
- return this._visitedVal;
182
- }
140
+ visitedVal: N['val'][] = [];
183
141
 
184
- private _visitedNode: N[] = [];
185
-
186
- get visitedNode(): N[] {
187
- return this._visitedNode;
188
- }
142
+ visitedNode: N[] = [];
189
143
 
190
- abstract createNode(id: BinaryTreeNodeId, val?: N['val']): N | null;
144
+ abstract createNode(key: BinaryTreeNodeKey, val?: N['val']): N | null;
191
145
 
192
146
  /**
193
147
  * The `swapLocation` function swaps the location of two nodes in a binary tree.
@@ -197,26 +151,22 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
197
151
  * @returns The `destNode` is being returned.
198
152
  */
199
153
  swapLocation(srcNode: N, destNode: N): N {
200
- const {id, val, height} = destNode;
201
- const tempNode = this.createNode(id, val);
154
+ const {key, val} = destNode;
155
+ const tempNode = this.createNode(key, val);
202
156
 
203
157
  if (tempNode) {
204
- tempNode.height = height;
205
-
206
- destNode.id = srcNode.id;
158
+ destNode.key = srcNode.key;
207
159
  destNode.val = srcNode.val;
208
- destNode.height = srcNode.height;
209
160
 
210
- srcNode.id = tempNode.id;
161
+ srcNode.key = tempNode.key;
211
162
  srcNode.val = tempNode.val;
212
- srcNode.height = tempNode.height;
213
163
  }
214
164
 
215
165
  return destNode;
216
166
  }
217
167
 
218
168
  /**
219
- * The clear() function resets the root, size, and maxId properties to their initial values.
169
+ * The clear() function resets the root, size, and maxKey properties to their initial values.
220
170
  */
221
171
  clear() {
222
172
  this._root = null;
@@ -239,20 +189,20 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
239
189
 
240
190
  /**
241
191
  * The `add` function adds a new node to a binary tree, either by ID or by creating a new node with a given value.
242
- * @param {BinaryTreeNodeId | N | null} idOrNode - The `idOrNode` parameter can be either a `BinaryTreeNodeId`, which
192
+ * @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey`, which
243
193
  * is a number representing the ID of a binary tree node, or it can be a `N` object, which represents a binary tree
244
194
  * node itself. It can also be `null` if no node is specified.
245
195
  * @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
246
196
  * being added to the binary tree.
247
197
  * @returns The function `add` returns either the inserted node (`N`), `null`, or `undefined`.
248
198
  */
249
- add(idOrNode: BinaryTreeNodeId | N | null, val?: N['val']): N | null | undefined {
199
+ add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
250
200
  const _bfs = (root: N, newNode: N | null): N | undefined | null => {
251
201
  const queue: Array<N | null> = [root];
252
202
  while (queue.length > 0) {
253
203
  const cur = queue.shift();
254
204
  if (cur) {
255
- if (newNode && cur.id === newNode.id) return;
205
+ if (newNode && cur.key === newNode.key) return;
256
206
  const inserted = this._addTo(newNode, cur);
257
207
  if (inserted !== undefined) return inserted;
258
208
  if (cur.left) queue.push(cur.left);
@@ -264,17 +214,17 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
264
214
 
265
215
  let inserted: N | null | undefined, needInsert: N | null;
266
216
 
267
- if (idOrNode === null) {
217
+ if (keyOrNode === null) {
268
218
  needInsert = null;
269
- } else if (typeof idOrNode === 'number') {
270
- needInsert = this.createNode(idOrNode, val);
271
- } else if (idOrNode instanceof AbstractBinaryTreeNode) {
272
- needInsert = idOrNode;
219
+ } else if (typeof keyOrNode === 'number') {
220
+ needInsert = this.createNode(keyOrNode, val);
221
+ } else if (keyOrNode instanceof AbstractBinaryTreeNode) {
222
+ needInsert = keyOrNode;
273
223
  } else {
274
224
  return;
275
225
  }
276
226
 
277
- const existNode = idOrNode ? this.get(idOrNode, 'id') : undefined;
227
+ const existNode = keyOrNode ? this.get(keyOrNode, 'key') : undefined;
278
228
 
279
229
  if (this.root) {
280
230
  if (existNode) {
@@ -298,61 +248,61 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
298
248
  /**
299
249
  * The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
300
250
  * values, and adds them to the binary tree.
301
- * @param {(BinaryTreeNodeId | null)[] | (N | null)[]} idsOrNodes - An array of BinaryTreeNodeId or BinaryTreeNode
251
+ * @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
302
252
  * objects, or null values.
303
253
  * @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
304
254
  * the nodes or node IDs being added. It is used to set the value of each node being added. If `data` is not provided,
305
255
  * the value of the nodes will be `undefined`.
306
256
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
307
257
  */
308
- addMany(idsOrNodes: (BinaryTreeNodeId | null)[] | (N | null)[], data?: N['val'][]): (N | null | undefined)[] {
258
+ addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N['val'][]): (N | null | undefined)[] {
309
259
  // TODO not sure addMany not be run multi times
310
260
  const inserted: (N | null | undefined)[] = [];
311
261
 
312
- for (let i = 0; i < idsOrNodes.length; i++) {
313
- const idOrNode = idsOrNodes[i];
314
- if (idOrNode instanceof AbstractBinaryTreeNode) {
315
- inserted.push(this.add(idOrNode.id, idOrNode.val));
262
+ for (let i = 0; i < keysOrNodes.length; i++) {
263
+ const keyOrNode = keysOrNodes[i];
264
+ if (keyOrNode instanceof AbstractBinaryTreeNode) {
265
+ inserted.push(this.add(keyOrNode.key, keyOrNode.val));
316
266
  continue;
317
267
  }
318
268
 
319
- if (idOrNode === null) {
269
+ if (keyOrNode === null) {
320
270
  inserted.push(this.add(null));
321
271
  continue;
322
272
  }
323
273
 
324
274
  const val = data?.[i];
325
- inserted.push(this.add(idOrNode, val));
275
+ inserted.push(this.add(keyOrNode, val));
326
276
  }
327
277
  return inserted;
328
278
  }
329
279
 
330
280
  /**
331
- * The `fill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
332
- * @param {(BinaryTreeNodeId | N)[]} idsOrNodes - The `idsOrNodes` parameter is an array that can contain either
333
- * `BinaryTreeNodeId` or `N` values.
281
+ * The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
282
+ * @param {(BinaryTreeNodeKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
283
+ * `BinaryTreeNodeKey` or `N` values.
334
284
  * @param {N[] | Array<N['val']>} [data] - The `data` parameter is an optional array of values that will be assigned to
335
- * the nodes being added. If provided, the length of the `data` array should be equal to the length of the `idsOrNodes`
285
+ * the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
336
286
  * array. Each value in the `data` array will be assigned to the
337
287
  * @returns The method is returning a boolean value.
338
288
  */
339
- fill(idsOrNodes: (BinaryTreeNodeId | null)[] | (N | null)[], data?: N[] | Array<N['val']>): boolean {
289
+ refill(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N[] | Array<N['val']>): boolean {
340
290
  this.clear();
341
- return idsOrNodes.length === this.addMany(idsOrNodes, data).length;
291
+ return keysOrNodes.length === this.addMany(keysOrNodes, data).length;
342
292
  }
343
293
 
344
294
  /**
345
295
  * The `remove` function in TypeScript is used to delete a node from a binary search tree and returns an array of objects
346
296
  * containing the deleted node and the node that needs to be balanced.
347
- * @param {N | BinaryTreeNodeId} nodeOrId - The `nodeOrId` parameter can be either a node object (`N`) or a binary tree
348
- * node ID (`BinaryTreeNodeId`).
297
+ * @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object (`N`) or a binary tree
298
+ * node ID (`BinaryTreeNodeKey`).
349
299
  * @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
350
300
  */
351
- remove(nodeOrId: N | BinaryTreeNodeId): BinaryTreeDeletedResult<N>[] {
301
+ remove(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[] {
352
302
  const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
353
303
  if (!this.root) return bstDeletedResult;
354
304
 
355
- const curr: N | null = typeof nodeOrId === 'number' ? this.get(nodeOrId) : nodeOrId;
305
+ const curr: N | null = typeof nodeOrKey === 'number' ? this.get(nodeOrKey) : nodeOrKey;
356
306
  if (!curr) return bstDeletedResult;
357
307
 
358
308
  const parent: N | null = curr?.parent ? curr.parent : null;
@@ -392,11 +342,11 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
392
342
 
393
343
  /**
394
344
  * The function calculates the depth of a node in a binary tree.
395
- * @param {N | BinaryTreeNodeId | null} beginRoot - The `beginRoot` parameter can be one of the following:
345
+ * @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter can be one of the following:
396
346
  * @returns the depth of the given node or binary tree.
397
347
  */
398
- getDepth(beginRoot: N | BinaryTreeNodeId | null): number {
399
- if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'id');
348
+ getDepth(beginRoot: N | BinaryTreeNodeKey | null = this.root): number {
349
+ if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'key');
400
350
 
401
351
  let depth = 0;
402
352
  while (beginRoot?.parent) {
@@ -408,15 +358,13 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
408
358
 
409
359
  /**
410
360
  * The `getHeight` function calculates the maximum height of a binary tree, either recursively or iteratively.
411
- * @param {N | BinaryTreeNodeId | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
412
- * generic type representing a node in a binary tree), `BinaryTreeNodeId` (a type representing the ID of a binary tree
361
+ * @param {N | BinaryTreeNodeKey | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
362
+ * generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
413
363
  * node), or `null`.
414
364
  * @returns the height of the binary tree.
415
365
  */
416
- getHeight(beginRoot?: N | BinaryTreeNodeId | null): number {
417
- beginRoot = beginRoot ?? this.root;
418
-
419
- if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'id');
366
+ getHeight(beginRoot: N | BinaryTreeNodeKey | null = this.root): number {
367
+ if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'key');
420
368
  if (!beginRoot) return -1;
421
369
 
422
370
  if (this._loopType === LoopType.RECURSIVE) {
@@ -462,8 +410,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
462
410
  * for `beginRoot`, the `this.root` property is used as the default value.
463
411
  * @returns The function `getMinHeight` returns the minimum height of the binary tree.
464
412
  */
465
- getMinHeight(beginRoot?: N | null): number {
466
- beginRoot = beginRoot || this.root;
413
+ getMinHeight(beginRoot: N | null = this.root): number {
467
414
  if (!beginRoot) return -1;
468
415
 
469
416
  if (this._loopType === LoopType.RECURSIVE) {
@@ -512,24 +459,27 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
512
459
  * tree or null if the tree is empty.
513
460
  * @returns The method is returning a boolean value.
514
461
  */
515
- isPerfectlyBalanced(beginRoot?: N | null): boolean {
462
+ isPerfectlyBalanced(beginRoot: N | null = this.root): boolean {
516
463
  return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
517
464
  }
518
465
 
519
466
  /**
520
467
  * The function `getNodes` returns an array of nodes that match a given property name and value in a binary tree.
521
- * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
468
+ * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
522
469
  * generic type `N`. It represents the property of the binary tree node that you want to search for.
523
470
  * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
524
- * specifies the property name to use when searching for nodes. If not provided, it defaults to 'id'.
471
+ * specifies the property name to use when searching for nodes. If not provided, it defaults to 'key'.
525
472
  * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
526
473
  * return only one node that matches the given `nodeProperty` or `propertyName`. If `onlyOne` is set to `true`, the
527
474
  * function will stop traversing the tree and return the first matching node. If `only
528
475
  * @returns an array of nodes (type N).
529
476
  */
530
- getNodes(nodeProperty: BinaryTreeNodeId | N, propertyName?: BinaryTreeNodePropertyName, onlyOne?: boolean): N[] {
477
+ getNodes(
478
+ nodeProperty: BinaryTreeNodeKey | N,
479
+ propertyName: BinaryTreeNodePropertyName = 'key',
480
+ onlyOne = false
481
+ ): N[] {
531
482
  if (!this.root) return [];
532
- propertyName = propertyName ?? 'id';
533
483
 
534
484
  const result: N[] = [];
535
485
 
@@ -559,14 +509,13 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
559
509
 
560
510
  /**
561
511
  * The function checks if a binary tree node has a specific property.
562
- * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or `N`.
512
+ * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`.
563
513
  * It represents the property of the binary tree node that you want to check.
564
514
  * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
565
- * specifies the name of the property to be checked in the nodes. If not provided, it defaults to 'id'.
515
+ * specifies the name of the property to be checked in the nodes. If not provided, it defaults to 'key'.
566
516
  * @returns a boolean value.
567
517
  */
568
- has(nodeProperty: BinaryTreeNodeId | N, propertyName?: BinaryTreeNodePropertyName): boolean {
569
- propertyName = propertyName ?? 'id';
518
+ has(nodeProperty: BinaryTreeNodeKey | N, propertyName: BinaryTreeNodePropertyName = 'key'): boolean {
570
519
  // TODO may support finding node by value equal
571
520
  return this.getNodes(nodeProperty, propertyName).length > 0;
572
521
  }
@@ -574,16 +523,15 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
574
523
  /**
575
524
  * The function returns the first node that matches the given property name and value, or null if no matching node is
576
525
  * found.
577
- * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or `N`.
526
+ * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`.
578
527
  * It represents the property of the binary tree node that you want to search for.
579
528
  * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
580
529
  * specifies the property name to be used for searching the binary tree nodes. If this parameter is not provided, the
581
- * default value is set to `'id'`.
530
+ * default value is set to `'key'`.
582
531
  * @returns either the value of the specified property of the node, or the node itself if no property name is provided.
583
532
  * If no matching node is found, it returns null.
584
533
  */
585
- get(nodeProperty: BinaryTreeNodeId | N, propertyName?: BinaryTreeNodePropertyName): N | null {
586
- propertyName = propertyName ?? 'id';
534
+ get(nodeProperty: BinaryTreeNodeKey | N, propertyName: BinaryTreeNodePropertyName = 'key'): N | null {
587
535
  // TODO may support finding node by value equal
588
536
  return this.getNodes(nodeProperty, propertyName, true)[0] ?? null;
589
537
  }
@@ -599,7 +547,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
599
547
  * @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
600
548
  */
601
549
  getPathToRoot(node: N, isReverse = true): N[] {
602
- // TODO to support get path through passing id
550
+ // TODO to support get path through passing key
603
551
  const result: N[] = [];
604
552
  while (node.parent) {
605
553
  // Array.push + Array.reverse is more efficient than Array.unshift
@@ -614,7 +562,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
614
562
  /**
615
563
  * The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if
616
564
  * no node is specified.
617
- * generic type representing a node in a binary tree), `BinaryTreeNodeId` (a type representing the ID of a binary tree
565
+ * generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
618
566
  * node), or `null`.
619
567
  * @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
620
568
  * provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal
@@ -626,8 +574,8 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
626
574
  /**
627
575
  * The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if
628
576
  * no node is specified.
629
- * @param {N | BinaryTreeNodeId | null} [node] - The `beginRoot` parameter is optional and can be of type `N` (a
630
- * generic type representing a node in a binary tree), `BinaryTreeNodeId` (a type representing the ID of a binary tree
577
+ * @param {N | BinaryTreeNodeKey | null} [node] - The `beginRoot` parameter is optional and can be of type `N` (a
578
+ * generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
631
579
  * node).
632
580
  * @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
633
581
  * provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal
@@ -639,18 +587,17 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
639
587
  /**
640
588
  * The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if
641
589
  * no node is specified.
642
- * @param {N | BinaryTreeNodeId | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
643
- * generic type representing a node in a binary tree), `BinaryTreeNodeId` (a type representing the ID of a binary tree
590
+ * @param {N | BinaryTreeNodeKey | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
591
+ * generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
644
592
  * node), or `null`.
645
593
  * @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
646
594
  * provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal
647
595
  * from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost
648
596
  * node is found (
649
597
  */
650
- getLeftMost(beginRoot?: N | BinaryTreeNodeId | null): N | null {
651
- if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'id');
598
+ getLeftMost(beginRoot: N | BinaryTreeNodeKey | null = this.root): N | null {
599
+ if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'key');
652
600
 
653
- beginRoot = beginRoot ?? this.root;
654
601
  if (!beginRoot) return beginRoot;
655
602
 
656
603
  if (this._loopType === LoopType.RECURSIVE) {
@@ -682,28 +629,27 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
682
629
  /**
683
630
  * The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using tail
684
631
  * recursion optimization.
685
- * @param {N | null} [node] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
632
+ * @param {N | null} [beginRoot] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
686
633
  * starting node from which we want to find the rightmost node. If no node is provided, the function will default to
687
634
  * using the root node of the data structure.
688
635
  * @returns The `getRightMost` function returns the rightmost node in a binary tree. It returns the rightmost node
689
636
  * starting from that node.
690
637
  */
691
- getRightMost(node: N): N;
638
+ getRightMost(beginRoot: N): N;
692
639
 
693
640
  /**
694
641
  * The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using tail
695
642
  * recursion optimization.
696
- * @param {N | null} [node] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
643
+ * @param {N | null} [beginRoot] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
697
644
  * starting node from which we want to find the rightmost node. If no node is provided, the function will default to
698
645
  * using the root node of the data structure.
699
646
  * @returns The `getRightMost` function returns the rightmost node in a binary tree. If the `node` parameter is provided,
700
647
  * it returns the rightmost node starting from that node. If the `node` parameter is not provided, it returns the
701
648
  * rightmost node starting from the root of the binary tree.
702
649
  */
703
- getRightMost(node?: N | null): N | null {
704
- // TODO support get right most by passing id in
705
- node = node ?? this.root;
706
- if (!node) return node;
650
+ getRightMost(beginRoot: N | null = this.root): N | null {
651
+ // TODO support get right most by passing key in
652
+ if (!beginRoot) return beginRoot;
707
653
 
708
654
  if (this._loopType === LoopType.RECURSIVE) {
709
655
  const _traverse = (cur: N): N => {
@@ -711,7 +657,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
711
657
  return _traverse(cur.right);
712
658
  };
713
659
 
714
- return _traverse(node);
660
+ return _traverse(beginRoot);
715
661
  } else {
716
662
  // Indirect implementation of iteration using tail recursion optimization
717
663
  const _traverse = trampoline((cur: N) => {
@@ -719,7 +665,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
719
665
  return _traverse.cont(cur.right);
720
666
  });
721
667
 
722
- return _traverse(node);
668
+ return _traverse(beginRoot);
723
669
  }
724
670
  }
725
671
 
@@ -733,10 +679,10 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
733
679
  if (!node) return true;
734
680
 
735
681
  if (this._loopType === LoopType.RECURSIVE) {
736
- const dfs = (cur: N | null | undefined, min: BinaryTreeNodeId, max: BinaryTreeNodeId): boolean => {
682
+ const dfs = (cur: N | null | undefined, min: BinaryTreeNodeKey, max: BinaryTreeNodeKey): boolean => {
737
683
  if (!cur) return true;
738
- if (cur.id <= min || cur.id >= max) return false;
739
- return dfs(cur.left, min, cur.id) && dfs(cur.right, cur.id, max);
684
+ if (cur.key <= min || cur.key >= max) return false;
685
+ return dfs(cur.left, min, cur.key) && dfs(cur.right, cur.key, max);
740
686
  };
741
687
 
742
688
  return dfs(node, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
@@ -750,8 +696,8 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
750
696
  curr = curr.left;
751
697
  }
752
698
  curr = stack.pop()!;
753
- if (!curr || prev >= curr.id) return false;
754
- prev = curr.id;
699
+ if (!curr || prev >= curr.key) return false;
700
+ prev = curr.key;
755
701
  curr = curr.right;
756
702
  }
757
703
  return true;
@@ -773,7 +719,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
773
719
  * @returns the size of the subtree rooted at `subTreeRoot`.
774
720
  */
775
721
  getSubTreeSize(subTreeRoot: N | null | undefined) {
776
- // TODO support id passed in
722
+ // TODO support key passed in
777
723
  let size = 0;
778
724
  if (!subTreeRoot) return size;
779
725
 
@@ -802,16 +748,15 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
802
748
 
803
749
  /**
804
750
  * The function `subTreeSum` calculates the sum of a specified property in a binary tree or subtree.
805
- * @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
751
+ * @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
806
752
  * tree or the ID of a binary tree node. It can also be `null` if there is no subtree.
807
753
  * @param {BinaryTreeNodePropertyName} [propertyName] - propertyName is an optional parameter that specifies the
808
- * property of the binary tree node to use for calculating the sum. It can be either 'id' or 'val'. If propertyName is
809
- * not provided, it defaults to 'id'.
754
+ * property of the binary tree node to use for calculating the sum. It can be either 'key' or 'val'. If propertyName is
755
+ * not provided, it defaults to 'key'.
810
756
  * @returns a number, which is the sum of the values of the specified property in the subtree rooted at `subTreeRoot`.
811
757
  */
812
- subTreeSum(subTreeRoot: N | BinaryTreeNodeId | null, propertyName?: BinaryTreeNodePropertyName): number {
813
- propertyName = propertyName ?? 'id';
814
- if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'id');
758
+ subTreeSum(subTreeRoot: N | BinaryTreeNodeKey | null, propertyName: BinaryTreeNodePropertyName = 'key'): number {
759
+ if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'key');
815
760
 
816
761
  if (!subTreeRoot) return 0;
817
762
 
@@ -820,14 +765,14 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
820
765
  const _sumByProperty = (cur: N) => {
821
766
  let needSum: number;
822
767
  switch (propertyName) {
823
- case 'id':
824
- needSum = cur.id;
768
+ case 'key':
769
+ needSum = cur.key;
825
770
  break;
826
771
  case 'val':
827
772
  needSum = typeof cur.val === 'number' ? cur.val : 0;
828
773
  break;
829
774
  default:
830
- needSum = cur.id;
775
+ needSum = cur.key;
831
776
  break;
832
777
  }
833
778
  return needSum;
@@ -857,31 +802,30 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
857
802
 
858
803
  /**
859
804
  * The function `subTreeAdd` adds a delta value to a specified property of each node in a subtree.
860
- * @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
805
+ * @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
861
806
  * tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
862
807
  * @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
863
808
  * each node in the subtree should be incremented.
864
809
  * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
865
- * specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'id'.
810
+ * specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'key'.
866
811
  * @returns a boolean value.
867
812
  */
868
813
  subTreeAdd(
869
- subTreeRoot: N | BinaryTreeNodeId | null,
814
+ subTreeRoot: N | BinaryTreeNodeKey | null,
870
815
  delta: number,
871
- propertyName?: BinaryTreeNodePropertyName
816
+ propertyName: BinaryTreeNodePropertyName = 'key'
872
817
  ): boolean {
873
- propertyName = propertyName ?? 'id';
874
- if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'id');
818
+ if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'key');
875
819
 
876
820
  if (!subTreeRoot) return false;
877
821
 
878
822
  const _addByProperty = (cur: N) => {
879
823
  switch (propertyName) {
880
- case 'id':
881
- cur.id += delta;
824
+ case 'key':
825
+ cur.key += delta;
882
826
  break;
883
827
  default:
884
- cur.id += delta;
828
+ cur.key += delta;
885
829
  break;
886
830
  }
887
831
  };
@@ -909,41 +853,40 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
909
853
  }
910
854
 
911
855
  /**
912
- * Performs a breadth-first search (BFS) on a binary tree, accumulating properties of each node based on their 'id' property.
856
+ * Performs a breadth-first search (bfs) on a binary tree, accumulating properties of each node based on their 'key' property.
913
857
  * @returns An array of binary tree node IDs.
914
858
  */
915
- BFS(): BinaryTreeNodeId[];
859
+ bfs(): BinaryTreeNodeKey[];
916
860
 
917
861
  /**
918
- * Performs a breadth-first search (BFS) on a binary tree, accumulating properties of each node based on the specified property name.
919
- * @param {'id'} nodeOrPropertyName - The name of the property to accumulate.
862
+ * Performs a breadth-first search (bfs) on a binary tree, accumulating properties of each node based on the specified property name.
863
+ * @param {'key'} nodeOrPropertyName - The name of the property to accumulate.
920
864
  * @returns An array of values corresponding to the specified property.
921
865
  */
922
- BFS(nodeOrPropertyName: 'id'): BinaryTreeNodeId[];
866
+ bfs(nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
923
867
 
924
868
  /**
925
- * Performs a breadth-first search (BFS) on a binary tree, accumulating the 'val' property of each node.
869
+ * Performs a breadth-first search (bfs) on a binary tree, accumulating the 'val' property of each node.
926
870
  * @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
927
871
  * @returns An array of 'val' properties from each node.
928
872
  */
929
- BFS(nodeOrPropertyName: 'val'): N['val'][];
873
+ bfs(nodeOrPropertyName: 'val'): N['val'][];
930
874
 
931
875
  /**
932
- * Performs a breadth-first search (BFS) on a binary tree, accumulating nodes themselves.
876
+ * Performs a breadth-first search (bfs) on a binary tree, accumulating nodes themselves.
933
877
  * @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
934
878
  * @returns An array of binary tree nodes.
935
879
  */
936
- BFS(nodeOrPropertyName: 'node'): N[];
880
+ bfs(nodeOrPropertyName: 'node'): N[];
937
881
 
938
882
  /**
939
- * The BFS function performs a breadth-first search on a binary tree, accumulating properties of each node based on a specified property name.
883
+ * The bfs function performs a breadth-first search on a binary tree, accumulating properties of each node based on a specified property name.
940
884
  * @param {NodeOrPropertyName} [nodeOrPropertyName] - An optional parameter that represents either a node or a property name.
941
- * If a node is provided, the BFS algorithm will be performed starting from that node.
942
- * If a property name is provided, the BFS algorithm will be performed starting from the root node, accumulating the specified property.
885
+ * If a node is provided, the bfs algorithm will be performed starting from that node.
886
+ * If a property name is provided, the bfs algorithm will be performed starting from the root node, accumulating the specified property.
943
887
  * @returns An instance of the `AbstractBinaryTreeNodeProperties` class with generic type `N`.
944
888
  */
945
- BFS(nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
946
- nodeOrPropertyName = nodeOrPropertyName ?? 'id';
889
+ bfs(nodeOrPropertyName: NodeOrPropertyName = 'key'): AbstractBinaryTreeNodeProperties<N> {
947
890
  this._clearResults();
948
891
  const queue: Array<N | null | undefined> = [this.root];
949
892
 
@@ -960,45 +903,53 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
960
903
  }
961
904
 
962
905
  /**
963
- * Performs a depth-first search (DFS) traversal on a binary tree and accumulates properties of each node based on their 'id' property.
906
+ * Performs a depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on their 'key' property.
964
907
  * @returns An array of binary tree node IDs.
965
908
  */
966
- DFS(): BinaryTreeNodeId[];
909
+ dfs(): BinaryTreeNodeKey[];
967
910
 
968
911
  /**
969
- * Performs a depth-first search (DFS) traversal on a binary tree and accumulates properties of each node based on the specified property name.
912
+ * Performs a depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on the specified property name.
913
+ * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
914
+ * @returns An array of values corresponding to the specified property.
915
+ */
916
+ dfs(pattern: DFSOrderPattern): BinaryTreeNodeKey[];
917
+
918
+ /**
919
+ * Performs a depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on the specified property name.
970
920
  * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
971
921
  * @param {string} nodeOrPropertyName - The name of the property to accumulate.
972
922
  * @returns An array of values corresponding to the specified property.
973
923
  */
974
- DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
924
+ dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
975
925
 
976
926
  /**
977
- * Performs a depth-first search (DFS) traversal on a binary tree and accumulates the 'val' property of each node.
927
+ * Performs a depth-first search (dfs) traversal on a binary tree and accumulates the 'val' property of each node.
978
928
  * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
979
929
  * @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
980
930
  * @returns An array of 'val' properties from each node.
981
931
  */
982
- DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): N[];
932
+ dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'val'): N[];
983
933
 
984
934
  /**
985
- * Performs a depth-first search (DFS) traversal on a binary tree and accumulates nodes themselves.
935
+ * Performs a depth-first search (dfs) traversal on a binary tree and accumulates nodes themselves.
986
936
  * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
987
937
  * @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
988
938
  * @returns An array of binary tree nodes.
989
939
  */
990
- DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): N[];
940
+ dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'node'): N[];
991
941
 
992
942
  /**
993
- * The DFS function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
943
+ * The dfs function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
994
944
  * each node based on the specified pattern and property name.
995
945
  * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
996
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. If no `nodeOrPropertyName` is provided, the default value is `'id'`.
946
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. If no `nodeOrPropertyName` is provided, the default value is `'key'`.
997
947
  * @returns an instance of the AbstractBinaryTreeNodeProperties class, which contains the accumulated properties of the binary tree nodes based on the specified pattern and node or property name.
998
948
  */
999
- DFS(pattern?: 'in' | 'pre' | 'post', nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
1000
- pattern = pattern ?? 'in';
1001
- nodeOrPropertyName = nodeOrPropertyName ?? 'id';
949
+ dfs(
950
+ pattern: DFSOrderPattern = 'in',
951
+ nodeOrPropertyName: NodeOrPropertyName = 'key'
952
+ ): AbstractBinaryTreeNodeProperties<N> {
1002
953
  this._clearResults();
1003
954
  const _traverse = (node: N) => {
1004
955
  switch (pattern) {
@@ -1027,48 +978,53 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
1027
978
  // --- start additional methods ---
1028
979
 
1029
980
  /**
1030
- * Performs an iterative depth-first search (DFS) traversal on a binary tree and accumulates properties of each node based on their 'id' property.
981
+ * Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on their 'key' property.
1031
982
  * @returns An array of binary tree node IDs.
1032
983
  */
1033
- DFSIterative(): BinaryTreeNodeId[];
984
+ dfsIterative(): BinaryTreeNodeKey[];
1034
985
 
1035
986
  /**
1036
- * Performs an iterative depth-first search (DFS) traversal on a binary tree and accumulates properties of each node based on the specified property name.
987
+ * Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on their 'key' property.
988
+ * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
989
+ * @returns An array of values corresponding to the specified property.
990
+ */
991
+ dfsIterative(pattern: DFSOrderPattern): BinaryTreeNodeKey[];
992
+
993
+ /**
994
+ * Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on the specified property name.
1037
995
  * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
1038
996
  * @param {string} nodeOrPropertyName - The name of the property to accumulate.
1039
997
  * @returns An array of values corresponding to the specified property.
1040
998
  */
1041
- DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
999
+ dfsIterative(pattern: DFSOrderPattern, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
1042
1000
 
1043
1001
  /**
1044
- * Performs an iterative depth-first search (DFS) traversal on a binary tree and accumulates the 'val' property of each node.
1002
+ * Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates the 'val' property of each node.
1045
1003
  * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
1046
1004
  * @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
1047
1005
  * @returns An array of 'val' properties from each node.
1048
1006
  */
1049
- DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): N[];
1007
+ dfsIterative(pattern: DFSOrderPattern, nodeOrPropertyName: 'val'): N['val'][];
1050
1008
 
1051
1009
  /**
1052
- * Performs an iterative depth-first search (DFS) traversal on a binary tree and accumulates nodes themselves.
1010
+ * Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates nodes themselves.
1053
1011
  * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
1054
1012
  * @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
1055
1013
  * @returns An array of binary tree nodes.
1056
1014
  */
1057
- DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): N[];
1015
+ dfsIterative(pattern: DFSOrderPattern, nodeOrPropertyName: 'node'): N[];
1058
1016
 
1059
1017
  /**
1060
- * The DFSIterative function performs an iterative depth-first search traversal on a binary tree, with the option to
1018
+ * The dfsIterative function performs an iterative depth-first search traversal on a binary tree, with the option to
1061
1019
  * specify the traversal pattern and the property name to accumulate results by.
1062
1020
  * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
1063
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. By default, it is set to `'id'`.
1021
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. By default, it is set to `'key'`.
1064
1022
  * @returns An object of type AbstractBinaryTreeNodeProperties<N>.
1065
1023
  */
1066
- DFSIterative(
1067
- pattern?: 'in' | 'pre' | 'post',
1068
- nodeOrPropertyName?: NodeOrPropertyName
1024
+ dfsIterative(
1025
+ pattern: DFSOrderPattern = 'in',
1026
+ nodeOrPropertyName: NodeOrPropertyName = 'key'
1069
1027
  ): AbstractBinaryTreeNodeProperties<N> {
1070
- pattern = pattern || 'in';
1071
- nodeOrPropertyName = nodeOrPropertyName || 'id';
1072
1028
  this._clearResults();
1073
1029
  if (!this.root) return this._getResultByPropertyName(nodeOrPropertyName);
1074
1030
  // 0: visit, 1: print
@@ -1109,11 +1065,17 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
1109
1065
  }
1110
1066
 
1111
1067
  /**
1112
- * Performs a level-order traversal on a binary tree starting from the specified node and accumulates properties of each node based on their 'id' property.
1068
+ * Performs a level-order traversal on a binary tree starting from the specified node and accumulates properties of each node based on their 'key' property.
1069
+ * @returns An array of binary tree node IDs.
1070
+ */
1071
+ levelIterative(): BinaryTreeNodeKey[];
1072
+
1073
+ /**
1074
+ * Performs a level-order traversal on a binary tree starting from the specified node and accumulates properties of each node based on their 'key' property.
1113
1075
  * @param {N | null} node - The starting node for the level order traversal. If null, the root node of the tree is used as the starting node.
1114
1076
  * @returns An array of binary tree node IDs.
1115
1077
  */
1116
- levelIterative(node: N | null): BinaryTreeNodeId[];
1078
+ levelIterative(node: N | null): BinaryTreeNodeKey[];
1117
1079
 
1118
1080
  /**
1119
1081
  * Performs a level-order traversal on a binary tree starting from the specified node and accumulates properties of each node based on the specified property name.
@@ -1121,7 +1083,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
1121
1083
  * @param {string} nodeOrPropertyName - The name of the property to accumulate.
1122
1084
  * @returns An array of values corresponding to the specified property.
1123
1085
  */
1124
- levelIterative(node: N | null, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
1086
+ levelIterative(node: N | null, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
1125
1087
 
1126
1088
  /**
1127
1089
  * Performs a level-order traversal on a binary tree starting from the specified node and accumulates the 'val' property of each node.
@@ -1129,7 +1091,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
1129
1091
  * @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
1130
1092
  * @returns An array of 'val' properties from each node.
1131
1093
  */
1132
- levelIterative(node: N | null, nodeOrPropertyName?: 'val'): N['val'][];
1094
+ levelIterative(node: N | null, nodeOrPropertyName: 'val'): N['val'][];
1133
1095
 
1134
1096
  /**
1135
1097
  * Performs a level-order traversal on a binary tree starting from the specified node and accumulates nodes themselves.
@@ -1137,7 +1099,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
1137
1099
  * @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
1138
1100
  * @returns An array of binary tree nodes.
1139
1101
  */
1140
- levelIterative(node: N | null, nodeOrPropertyName?: 'node'): N[];
1102
+ levelIterative(node: N | null, nodeOrPropertyName: 'node'): N[];
1141
1103
 
1142
1104
  /**
1143
1105
  * The `levelIterative` function performs a level-order traversal on a binary tree and returns the values of the nodes
@@ -1146,14 +1108,15 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
1146
1108
  * node for the level order traversal. It can be null if no specific node is provided, in which case the root node of
1147
1109
  * the tree is used as the starting node.
1148
1110
  * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
1149
- * can be either a `BinaryTreeNode` property name or the string `'id'`. If a property name is provided, the function
1111
+ * can be either a `BinaryTreeNode` property name or the string `'key'`. If a property name is provided, the function
1150
1112
  * will accumulate results based on that property. If no property name is provided, the function will default to
1151
- * accumulating results based on the 'id' property.
1113
+ * accumulating results based on the 'key' property.
1152
1114
  * @returns An object of type `AbstractBinaryTreeNodeProperties<N>`.
1153
1115
  */
1154
- levelIterative(node: N | null, nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
1155
- nodeOrPropertyName = nodeOrPropertyName || 'id';
1156
- node = node || this.root;
1116
+ levelIterative(
1117
+ node: N | null = this.root,
1118
+ nodeOrPropertyName: NodeOrPropertyName = 'key'
1119
+ ): AbstractBinaryTreeNodeProperties<N> {
1157
1120
  if (!node) return [];
1158
1121
 
1159
1122
  this._clearResults();
@@ -1175,20 +1138,26 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
1175
1138
  return this._getResultByPropertyName(nodeOrPropertyName);
1176
1139
  }
1177
1140
 
1141
+ /**
1142
+ * Collects nodes from a binary tree by a specified property and organizes them into levels.
1143
+ * @returns A 2D array of AbstractBinaryTreeNodeProperty<N> objects.
1144
+ */
1145
+ listLevels(): BinaryTreeNodeKey[][];
1146
+
1178
1147
  /**
1179
1148
  * Collects nodes from a binary tree by a specified property and organizes them into levels.
1180
1149
  * @param {N | null} node - The root node of the binary tree or null. If null, the function will use the root node of the current binary tree instance.
1181
1150
  * @returns A 2D array of AbstractBinaryTreeNodeProperty<N> objects.
1182
1151
  */
1183
- listLevels(node: N | null): BinaryTreeNodeId[][];
1152
+ listLevels(node: N | null): BinaryTreeNodeKey[][];
1184
1153
 
1185
1154
  /**
1186
1155
  * Collects nodes from a binary tree by a specified property and organizes them into levels.
1187
1156
  * @param {N | null} node - The root node of the binary tree or null. If null, the function will use the root node of the current binary tree instance.
1188
- * @param {'id} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
1157
+ * @param {'key} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
1189
1158
  * @returns A 2D array of values corresponding to the specified property.
1190
1159
  */
1191
- listLevels(node: N | null, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[][];
1160
+ listLevels(node: N | null, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[][];
1192
1161
 
1193
1162
  /**
1194
1163
  * Collects nodes from a binary tree by a specified property and organizes them into levels.
@@ -1196,7 +1165,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
1196
1165
  * @param {'val'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
1197
1166
  * @returns A 2D array of 'val' properties from each node.
1198
1167
  */
1199
- listLevels(node: N | null, nodeOrPropertyName?: 'val'): N['val'][][];
1168
+ listLevels(node: N | null, nodeOrPropertyName: 'val'): N['val'][][];
1200
1169
 
1201
1170
  /**
1202
1171
  * Collects nodes from a binary tree by a specified property and organizes them into levels.
@@ -1204,25 +1173,26 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
1204
1173
  * @param {'node'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
1205
1174
  * @returns A 2D array of binary tree nodes.
1206
1175
  */
1207
- listLevels(node: N | null, nodeOrPropertyName?: 'node'): N[][];
1176
+ listLevels(node: N | null, nodeOrPropertyName: 'node'): N[][];
1208
1177
 
1209
1178
  /**
1210
1179
  * The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
1211
1180
  * @param {N | null} node - The `node` parameter is a BinaryTreeNode object or null. It represents the root node of a binary tree. If it is null, the function will use the root node of the current binary tree instance.
1212
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that specifies the property of the `BinaryTreeNode` object to collect at each level. It can be one of the following values: 'id', 'val', or 'node'. If not provided, it defaults to 'id'.
1181
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that specifies the property of the `BinaryTreeNode` object to collect at each level. It can be one of the following values: 'key', 'val', or 'node'. If not provided, it defaults to 'key'.
1213
1182
  * @returns A 2D array of `AbstractBinaryTreeNodeProperty<N>` objects.
1214
1183
  */
1215
- listLevels(node: N | null, nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperty<N>[][] {
1216
- nodeOrPropertyName = nodeOrPropertyName || 'id';
1217
- node = node || this.root;
1184
+ listLevels(
1185
+ node: N | null = this.root,
1186
+ nodeOrPropertyName: NodeOrPropertyName = 'key'
1187
+ ): AbstractBinaryTreeNodeProperty<N>[][] {
1218
1188
  if (!node) return [];
1219
1189
 
1220
1190
  const levelsNodes: AbstractBinaryTreeNodeProperty<N>[][] = [];
1221
1191
 
1222
1192
  const collectByProperty = (node: N, level: number) => {
1223
1193
  switch (nodeOrPropertyName) {
1224
- case 'id':
1225
- levelsNodes[level].push(node.id);
1194
+ case 'key':
1195
+ levelsNodes[level].push(node.key);
1226
1196
  break;
1227
1197
  case 'val':
1228
1198
  levelsNodes[level].push(node.val);
@@ -1231,7 +1201,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
1231
1201
  levelsNodes[level].push(node);
1232
1202
  break;
1233
1203
  default:
1234
- levelsNodes[level].push(node.id);
1204
+ levelsNodes[level].push(node.key);
1235
1205
  break;
1236
1206
  }
1237
1207
  };
@@ -1283,22 +1253,29 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
1283
1253
 
1284
1254
  /**
1285
1255
  * Time complexity is O(n)
1286
- * Space complexity of Iterative DFS equals to recursive DFS which is O(n) because of the stack
1256
+ * Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
1287
1257
  */
1288
1258
 
1289
1259
  /**
1290
1260
  * Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm.
1291
1261
  * @returns An array of binary tree node IDs.
1292
1262
  */
1293
- morris(): BinaryTreeNodeId[];
1263
+ morris(): BinaryTreeNodeKey[];
1294
1264
 
1295
1265
  /**
1296
1266
  * Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm and accumulates properties of each node based on the specified property name.
1297
1267
  * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
1298
- * @param {'id'} nodeOrPropertyName - The name of the property to accumulate.
1268
+ * @param {'key'} nodeOrPropertyName - The name of the property to accumulate.
1299
1269
  * @returns An array of values corresponding to the specified property.
1300
1270
  */
1301
- morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
1271
+ morris(pattern: DFSOrderPattern, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
1272
+
1273
+ /**
1274
+ * Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm and accumulates properties of each node based on the specified property name.
1275
+ * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
1276
+ * @returns An array of values corresponding to the specified property.
1277
+ */
1278
+ morris(pattern: DFSOrderPattern): BinaryTreeNodeKey[];
1302
1279
 
1303
1280
  /**
1304
1281
  * Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm and accumulates the 'val' property of each node.
@@ -1306,7 +1283,7 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
1306
1283
  * @param {'val'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
1307
1284
  * @returns An array of 'val' properties from each node.
1308
1285
  */
1309
- morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): N[];
1286
+ morris(pattern: DFSOrderPattern, nodeOrPropertyName: 'val'): N[];
1310
1287
 
1311
1288
  /**
1312
1289
  * Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm and accumulates nodes themselves.
@@ -1314,23 +1291,20 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
1314
1291
  * @param {'node'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
1315
1292
  * @returns An array of binary tree nodes.
1316
1293
  */
1317
- morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): N[];
1294
+ morris(pattern: DFSOrderPattern, nodeOrPropertyName: 'node'): N[];
1318
1295
 
1319
1296
  /**
1320
1297
  * The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm.
1321
1298
  * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
1322
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - The property name of the nodes to retrieve or perform operations on during the traversal. It can be any valid property name of the nodes in the binary tree. If not provided, it defaults to 'id'.
1299
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The property name of the nodes to retrieve or perform operations on during the traversal. It can be any valid property name of the nodes in the binary tree. If not provided, it defaults to 'key'.
1323
1300
  * @returns An array of AbstractBinaryTreeNodeProperties<N> objects.
1324
1301
  */
1325
1302
  morris(
1326
- pattern?: 'in' | 'pre' | 'post',
1327
- nodeOrPropertyName?: NodeOrPropertyName
1303
+ pattern: DFSOrderPattern = 'in',
1304
+ nodeOrPropertyName: NodeOrPropertyName = 'key'
1328
1305
  ): AbstractBinaryTreeNodeProperties<N> {
1329
1306
  if (this.root === null) return [];
1330
1307
 
1331
- pattern = pattern || 'in';
1332
- nodeOrPropertyName = nodeOrPropertyName || 'id';
1333
-
1334
1308
  this._clearResults();
1335
1309
 
1336
1310
  let cur: N | null | undefined = this.root;
@@ -1453,30 +1427,6 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
1453
1427
  this._loopType = value;
1454
1428
  }
1455
1429
 
1456
- /**
1457
- * The function sets the value of the `_visitedId` property in a protected manner.
1458
- * @param {BinaryTreeNodeId[]} value - value is an array of BinaryTreeNodeId values.
1459
- */
1460
- protected _setVisitedId(value: BinaryTreeNodeId[]) {
1461
- this._visitedId = value;
1462
- }
1463
-
1464
- /**
1465
- * The function sets the value of the "_visitedVal" property to the given array.
1466
- * @param value - An array of type N.
1467
- */
1468
- protected _setVisitedVal(value: Array<N>) {
1469
- this._visitedVal = value;
1470
- }
1471
-
1472
- /**
1473
- * The function sets the value of the _visitedNode property.
1474
- * @param {N[]} value - N[] is an array of elements of type N.
1475
- */
1476
- protected _setVisitedNode(value: N[]) {
1477
- this._visitedNode = value;
1478
- }
1479
-
1480
1430
  /**
1481
1431
  * The function sets the root property of an object to a given value, and if the value is not null, it also sets the
1482
1432
  * parent property of the value to undefined.
@@ -1502,9 +1452,9 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
1502
1452
  * properties.
1503
1453
  */
1504
1454
  protected _clearResults() {
1505
- this._visitedId = [];
1506
- this._visitedVal = [];
1507
- this._visitedNode = [];
1455
+ this.visitedKey = [];
1456
+ this.visitedVal = [];
1457
+ this.visitedNode = [];
1508
1458
  }
1509
1459
 
1510
1460
  /**
@@ -1512,11 +1462,11 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
1512
1462
  * a result array.
1513
1463
  * @param {N} cur - The current node being processed.
1514
1464
  * @param {(N | null | undefined)[]} result - An array that stores the matching nodes.
1515
- * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter is either a `BinaryTreeNodeId` or a `N`
1465
+ * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is either a `BinaryTreeNodeKey` or a `N`
1516
1466
  * type. It represents the property value that we are comparing against in the switch statement.
1517
1467
  * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
1518
- * specifies the property name to compare against when pushing nodes into the `result` array. It can be either `'id'`
1519
- * or `'val'`. If it is not provided or is not equal to `'id'` or `'val'`, the
1468
+ * specifies the property name to compare against when pushing nodes into the `result` array. It can be either `'key'`
1469
+ * or `'val'`. If it is not provided or is not equal to `'key'` or `'val'`, the
1520
1470
  * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
1521
1471
  * stop after finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set to
1522
1472
  * `true`, the function will stop after finding the first matching node and return `true`. If `onlyOne
@@ -1525,27 +1475,27 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
1525
1475
  protected _pushByPropertyNameStopOrNot(
1526
1476
  cur: N,
1527
1477
  result: (N | null | undefined)[],
1528
- nodeProperty: BinaryTreeNodeId | N,
1529
- propertyName?: BinaryTreeNodePropertyName,
1530
- onlyOne?: boolean
1478
+ nodeProperty: BinaryTreeNodeKey | N,
1479
+ propertyName: BinaryTreeNodePropertyName = 'key',
1480
+ onlyOne = false
1531
1481
  ) {
1532
1482
  switch (propertyName) {
1533
- case 'id':
1534
- if (cur.id === nodeProperty) {
1483
+ case 'key':
1484
+ if (cur.key === nodeProperty) {
1535
1485
  result.push(cur);
1536
- return !!onlyOne;
1486
+ return onlyOne;
1537
1487
  }
1538
1488
  break;
1539
1489
  case 'val':
1540
1490
  if (cur.val === nodeProperty) {
1541
1491
  result.push(cur);
1542
- return !!onlyOne;
1492
+ return onlyOne;
1543
1493
  }
1544
1494
  break;
1545
1495
  default:
1546
- if (cur.id === nodeProperty) {
1496
+ if (cur.key === nodeProperty) {
1547
1497
  result.push(cur);
1548
- return !!onlyOne;
1498
+ return onlyOne;
1549
1499
  }
1550
1500
  break;
1551
1501
  }
@@ -1558,27 +1508,25 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
1558
1508
  * can be either a string representing a property name or a reference to a `Node` object. If it is a string, it
1559
1509
  * specifies the property name to be used for accumulating values. If it is a `Node` object, it specifies
1560
1510
  */
1561
- protected _accumulatedByPropertyName(node: N, nodeOrPropertyName?: NodeOrPropertyName) {
1562
- nodeOrPropertyName = nodeOrPropertyName ?? 'id';
1563
-
1511
+ protected _accumulatedByPropertyName(node: N, nodeOrPropertyName: NodeOrPropertyName = 'key') {
1564
1512
  switch (nodeOrPropertyName) {
1565
- case 'id':
1566
- this._visitedId.push(node.id);
1513
+ case 'key':
1514
+ this.visitedKey.push(node.key);
1567
1515
  break;
1568
1516
  case 'val':
1569
- this._visitedVal.push(node.val);
1517
+ this.visitedVal.push(node.val);
1570
1518
  break;
1571
1519
  case 'node':
1572
- this._visitedNode.push(node);
1520
+ this.visitedNode.push(node);
1573
1521
  break;
1574
1522
  default:
1575
- this._visitedId.push(node.id);
1523
+ this.visitedKey.push(node.key);
1576
1524
  break;
1577
1525
  }
1578
1526
  }
1579
1527
 
1580
1528
  /**
1581
- * The time complexity of Morris traversal is O(n), it's may slower than others
1529
+ * The time complexity of Morris traversal is O(n), it may slower than others
1582
1530
  * The space complexity Morris traversal is O(1) because no using stack
1583
1531
  */
1584
1532
 
@@ -1589,18 +1537,18 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
1589
1537
  * can accept either a `NodeOrPropertyName` type or be undefined.
1590
1538
  * @returns The method `_getResultByPropertyName` returns an instance of `AbstractBinaryTreeNodeProperties<N>`.
1591
1539
  */
1592
- protected _getResultByPropertyName(nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
1593
- nodeOrPropertyName = nodeOrPropertyName ?? 'id';
1594
-
1540
+ protected _getResultByPropertyName(
1541
+ nodeOrPropertyName: NodeOrPropertyName = 'key'
1542
+ ): AbstractBinaryTreeNodeProperties<N> {
1595
1543
  switch (nodeOrPropertyName) {
1596
- case 'id':
1597
- return this._visitedId;
1544
+ case 'key':
1545
+ return this.visitedKey;
1598
1546
  case 'val':
1599
- return this._visitedVal;
1547
+ return this.visitedVal;
1600
1548
  case 'node':
1601
- return this._visitedNode;
1549
+ return this.visitedNode;
1602
1550
  default:
1603
- return this._visitedId;
1551
+ return this.visitedKey;
1604
1552
  }
1605
1553
  }
1606
1554