data-structure-typed 1.37.3 → 1.37.4

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 (28) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/dist/data-structures/binary-tree/avl-tree.d.ts +42 -34
  3. package/dist/data-structures/binary-tree/avl-tree.js +42 -34
  4. package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +265 -168
  6. package/dist/data-structures/binary-tree/binary-tree.js +257 -170
  7. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
  8. package/dist/data-structures/binary-tree/bst.d.ts +104 -59
  9. package/dist/data-structures/binary-tree/bst.js +105 -60
  10. package/dist/data-structures/binary-tree/bst.js.map +1 -1
  11. package/dist/data-structures/binary-tree/tree-multiset.d.ts +47 -39
  12. package/dist/data-structures/binary-tree/tree-multiset.js +47 -39
  13. package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
  14. package/lib/data-structures/binary-tree/avl-tree.d.ts +42 -34
  15. package/lib/data-structures/binary-tree/avl-tree.js +42 -34
  16. package/lib/data-structures/binary-tree/binary-tree.d.ts +265 -168
  17. package/lib/data-structures/binary-tree/binary-tree.js +257 -170
  18. package/lib/data-structures/binary-tree/bst.d.ts +104 -59
  19. package/lib/data-structures/binary-tree/bst.js +105 -60
  20. package/lib/data-structures/binary-tree/tree-multiset.d.ts +47 -39
  21. package/lib/data-structures/binary-tree/tree-multiset.js +47 -39
  22. package/package.json +5 -5
  23. package/src/data-structures/binary-tree/avl-tree.ts +42 -34
  24. package/src/data-structures/binary-tree/binary-tree.ts +270 -174
  25. package/src/data-structures/binary-tree/bst.ts +108 -66
  26. package/src/data-structures/binary-tree/tree-multiset.ts +47 -39
  27. package/umd/bundle.min.js +1 -1
  28. package/umd/bundle.min.js.map +1 -1
@@ -20,29 +20,45 @@ import {IBinaryTree} from '../../interfaces';
20
20
  import {trampoline} from '../../utils';
21
21
  import {Queue} from '../queue';
22
22
 
23
+ /**
24
+ * Represents a node in a binary tree.
25
+ * @template V - The type of data stored in the node.
26
+ * @template FAMILY - The type of the family relationship in the binary tree.
27
+ */
23
28
  export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> = BinaryTreeNodeNested<V>> {
24
29
  /**
25
- * The constructor function initializes a BinaryTreeNode object with a key and an optional value.
26
- * @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
27
- * of the binary tree node. It is used to distinguish one node from another in the binary tree.
28
- * @param {V} [val] - The "val" parameter is an optional parameter of type V. It represents the value that will be
29
- * stored in the binary tree node. If no value is provided, it will be set to undefined.
30
+ * Creates a new instance of BinaryTreeNode.
31
+ * @param {BinaryTreeNodeKey} key - The key associated with the node.
32
+ * @param {V} val - The value stored in the node.
30
33
  */
31
34
  constructor(key: BinaryTreeNodeKey, val?: V) {
32
35
  this.key = key;
33
36
  this.val = val;
34
37
  }
35
38
 
39
+ /**
40
+ * The key associated with the node.
41
+ */
36
42
  key: BinaryTreeNodeKey;
37
43
 
44
+ /**
45
+ * The value stored in the node.
46
+ */
38
47
  val: V | undefined;
39
48
 
40
49
  private _left: FAMILY | null | undefined;
41
50
 
51
+ /**
52
+ * Get the left child node.
53
+ */
42
54
  get left(): FAMILY | null | undefined {
43
55
  return this._left;
44
56
  }
45
57
 
58
+ /**
59
+ * Set the left child node.
60
+ * @param {FAMILY | null | undefined} v - The left child node.
61
+ */
46
62
  set left(v: FAMILY | null | undefined) {
47
63
  if (v) {
48
64
  v.parent = this as unknown as FAMILY;
@@ -52,10 +68,17 @@ export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> =
52
68
 
53
69
  private _right: FAMILY | null | undefined;
54
70
 
71
+ /**
72
+ * Get the right child node.
73
+ */
55
74
  get right(): FAMILY | null | undefined {
56
75
  return this._right;
57
76
  }
58
77
 
78
+ /**
79
+ * Set the right child node.
80
+ * @param {FAMILY | null | undefined} v - The right child node.
81
+ */
59
82
  set right(v: FAMILY | null | undefined) {
60
83
  if (v) {
61
84
  v.parent = this as unknown as FAMILY;
@@ -63,11 +86,14 @@ export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> =
63
86
  this._right = v;
64
87
  }
65
88
 
89
+ /**
90
+ * The parent node of the current node.
91
+ */
66
92
  parent: FAMILY | null | undefined;
67
93
 
68
94
  /**
69
- * The function determines the position of a node in a family tree structure.
70
- * @returns a value of type `FamilyPosition`.
95
+ * Get the position of the node within its family.
96
+ * @returns {FamilyPosition} - The family position of the node.
71
97
  */
72
98
  get familyPosition(): FamilyPosition {
73
99
  const that = this as unknown as FAMILY;
@@ -97,64 +123,75 @@ export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> =
97
123
  }
98
124
  }
99
125
 
126
+ /**
127
+ * Represents a binary tree data structure.
128
+ * @template N - The type of the binary tree's nodes.
129
+ */
100
130
  export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode> implements IBinaryTree<N> {
101
131
  /**
102
- * This is a constructor function for a binary tree class that takes an optional options parameter.
103
- * @param {BinaryTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
104
- * constructor of the `BinaryTree` class. It allows you to customize the behavior of the binary tree by providing
105
- * different configuration options.
132
+ * Creates a new instance of BinaryTree.
133
+ * @param {BinaryTreeOptions} [options] - The options for the binary tree.
106
134
  */
107
135
  constructor(options?: BinaryTreeOptions) {
108
136
  if (options !== undefined) {
109
- const {iterationType = IterationType.ITERATIVE} = options;
137
+ const { iterationType = IterationType.ITERATIVE } = options;
110
138
  this._loopType = iterationType;
111
139
  }
112
140
  }
113
141
 
114
142
  /**
115
- * The function creates a new binary tree node with an optional value.
116
- * @param {BinaryTreeNodeKey} key - The `key` parameter is the identifier for the binary tree node. It is of type
117
- * `BinaryTreeNodeKey`, which represents the unique identifier for each node in the binary tree.
118
- * @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
119
- * stored in the node.
120
- * @returns a new instance of a BinaryTreeNode with the specified key and value.
143
+ * Creates a new instance of BinaryTreeNode with the given key and value.
144
+ * @param {BinaryTreeNodeKey} key - The key for the new node.
145
+ * @param {N['val']} val - The value for the new node.
146
+ * @returns {N} - The newly created BinaryTreeNode.
121
147
  */
122
148
  createNode(key: BinaryTreeNodeKey, val?: N['val']): N {
123
149
  return new BinaryTreeNode<N['val'], N>(key, val) as N;
124
150
  }
125
151
 
126
- // TODO placeholder node may need redesigned
127
152
  private _root: N | null = null;
128
153
 
154
+ /**
155
+ * Get the root node of the binary tree.
156
+ */
129
157
  get root(): N | null {
130
158
  return this._root;
131
159
  }
132
160
 
133
161
  private _size = 0;
134
162
 
163
+ /**
164
+ * Get the number of nodes in the binary tree.
165
+ */
135
166
  get size(): number {
136
167
  return this._size;
137
168
  }
138
169
 
139
170
  private _loopType: IterationType = IterationType.ITERATIVE;
140
171
 
172
+ /**
173
+ * Get the iteration type used in the binary tree.
174
+ */
141
175
  get iterationType(): IterationType {
142
176
  return this._loopType;
143
177
  }
144
178
 
179
+ /**
180
+ * Set the iteration type for the binary tree.
181
+ * @param {IterationType} v - The new iteration type to set.
182
+ */
145
183
  set iterationType(v: IterationType) {
146
184
  this._loopType = v;
147
185
  }
148
186
 
149
187
  /**
150
- * The `_swap` function swaps the location of two nodes in a binary tree.
151
- * @param {N} srcNode - The source node that you want to _swap with the destination node.
152
- * @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
153
- * be swapped to.
154
- * @returns The `destNode` is being returned.
188
+ * Swap the data of two nodes in the binary tree.
189
+ * @param {N} srcNode - The source node to swap.
190
+ * @param {N} destNode - The destination node to swap.
191
+ * @returns {N} - The destination node after the swap.
155
192
  */
156
193
  protected _swap(srcNode: N, destNode: N): N {
157
- const {key, val} = destNode;
194
+ const { key, val } = destNode;
158
195
  const tempNode = this.createNode(key, val);
159
196
 
160
197
  if (tempNode) {
@@ -169,7 +206,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
169
206
  }
170
207
 
171
208
  /**
172
- * The clear() function resets the root, size, and maxKey properties to their initial values.
209
+ * Clear the binary tree, removing all nodes.
173
210
  */
174
211
  clear() {
175
212
  this._root = null;
@@ -177,26 +214,18 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
177
214
  }
178
215
 
179
216
  /**
180
- * The function checks if the size of an object is equal to zero and returns a boolean value.
181
- * @returns A boolean value indicating whether the size of the object is 0 or not.
217
+ * Check if the binary tree is empty.
218
+ * @returns {boolean} - True if the binary tree is empty, false otherwise.
182
219
  */
183
220
  isEmpty(): boolean {
184
221
  return this.size === 0;
185
222
  }
186
223
 
187
224
  /**
188
- * When all leaf nodes are null, it will no longer be possible to add new entity nodes to this binary tree.
189
- * In this scenario, null nodes serve as "sentinel nodes," "virtual nodes," or "placeholder nodes."
190
- */
191
-
192
- /**
193
- * The `add` function adds a new node to a binary tree, either by ID or by creating a new node with a given value.
194
- * @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey`, which
195
- * is a number representing the ID of a binary tree node, or it can be a `N` object, which represents a binary tree
196
- * node itself. It can also be `null` if no node is specified.
197
- * @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
198
- * being added to the binary tree.
199
- * @returns The function `add` returns either the inserted node (`N`), `null`, or `undefined`.
225
+ * Add a node with the given key and value to the binary tree.
226
+ * @param {BinaryTreeNodeKey | N | null} keyOrNode - The key or node to add to the binary tree.
227
+ * @param {N['val']} val - The value for the new node (optional).
228
+ * @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
200
229
  */
201
230
  add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
202
231
  const _bfs = (root: N, newNode: N | null): N | undefined | null => {
@@ -252,12 +281,12 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
252
281
  * values, and adds them to the binary tree.
253
282
  * @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
254
283
  * objects, or null values.
255
- * @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
256
- * the nodes or node IDs being added. It is used to set the value of each node being added. If `data` is not provided,
284
+ * @param {N['val'][]} [values] - The `values` parameter is an optional array of values (`N['val'][]`) that corresponds to
285
+ * the nodes or node IDs being added. It is used to set the value of each node being added. If `values` is not provided,
257
286
  * the value of the nodes will be `undefined`.
258
287
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
259
288
  */
260
- addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N['val'][]): (N | null | undefined)[] {
289
+ addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], values?: N['val'][]): (N | null | undefined)[] {
261
290
  // TODO not sure addMany not be run multi times
262
291
  const inserted: (N | null | undefined)[] = [];
263
292
 
@@ -273,7 +302,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
273
302
  continue;
274
303
  }
275
304
 
276
- const val = data?.[i];
305
+ const val = values?.[i];
277
306
  inserted.push(this.add(keyOrNode, val));
278
307
  }
279
308
  return inserted;
@@ -293,12 +322,14 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
293
322
  return keysOrNodes.length === this.addMany(keysOrNodes, data).length;
294
323
  }
295
324
 
325
+
296
326
  /**
297
- * The `delete` function in TypeScript is used to delete a node from a binary search tree and returns an array of objects
298
- * containing the deleted node and the node that needs to be balanced.
299
- * @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object (`N`) or a binary tree
300
- * node ID (`BinaryTreeNodeKey`).
301
- * @returns The function `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
327
+ * The `delete` function removes a node from a binary search tree and returns the deleted node along
328
+ * with the parent node that needs to be balanced.
329
+ * @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node (`N`) or
330
+ * a key (`BinaryTreeNodeKey`). If it is a key, the function will find the corresponding node in the
331
+ * binary tree.
332
+ * @returns an array of `BinaryTreeDeletedResult<N>` objects.
302
333
  */
303
334
  delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[] {
304
335
  const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
@@ -343,10 +374,16 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
343
374
  }
344
375
 
345
376
  /**
346
- * The function calculates the depth of a node in a binary tree.
347
- * @param {N | BinaryTreeNodeKey | null} distNode - The `distNode` parameter can be any node of the tree
348
- * @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter can be the predecessor node of distNode
349
- * @returns the depth of the given node or binary tree.
377
+ * The function `getDepth` calculates the depth of a given node in a binary tree relative to a
378
+ * specified root node.
379
+ * @param {N | BinaryTreeNodeKey | null} distNode - The `distNode` parameter represents the node
380
+ * whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value
381
+ * of the node (`BinaryTreeNodeKey`), or `null`.
382
+ * @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the
383
+ * starting node from which we want to calculate the depth. It can be either a node object or the key
384
+ * of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root
385
+ * node of the binary tree.
386
+ * @returns the depth of the `distNode` relative to the `beginRoot`.
350
387
  */
351
388
  getDepth(distNode: N | BinaryTreeNodeKey | null, beginRoot: N | BinaryTreeNodeKey | null = this.root): number {
352
389
  if (typeof distNode === 'number') distNode = this.get(distNode);
@@ -363,11 +400,15 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
363
400
  }
364
401
 
365
402
  /**
366
- * The `getHeight` function calculates the maximum height of a binary tree, either recursively or iteratively.
367
- * @param {N | BinaryTreeNodeKey | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
368
- * generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
369
- * node), or `null`.
370
- * @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of
403
+ * The `getHeight` function calculates the maximum height of a binary tree using either recursive or
404
+ * iterative approach.
405
+ * @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the
406
+ * starting node from which the height of the binary tree is calculated. It can be either a node
407
+ * object (`N`), a key value of a node in the tree (`BinaryTreeNodeKey`), or `null` if no starting
408
+ * node is specified. If `
409
+ * @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
410
+ * height of the binary tree using a recursive approach or an iterative approach. It can have two
411
+ * possible values:
371
412
  * @returns the height of the binary tree.
372
413
  */
373
414
  getHeight(beginRoot: N | BinaryTreeNodeKey | null = this.root, iterationType = this.iterationType): number {
@@ -412,13 +453,14 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
412
453
  protected _defaultCallbackByKey: MapCallback<N> = node => node.key;
413
454
 
414
455
  /**
415
- * The `getMinHeight` function calculates the minimum height of a binary tree using either a recursive or iterative
416
- * approach.
417
- * @param {N | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It
418
- * represents the starting node from which to calculate the minimum height of a binary tree. If no value is provided
419
- * for `beginRoot`, the `this.root` property is used as the default value.
420
- * @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
421
- * @returns The function `getMinHeight` returns the minimum height of the binary tree.
456
+ * The `getMinHeight` function calculates the minimum height of a binary tree using either a
457
+ * recursive or iterative approach.
458
+ * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which we want to
459
+ * calculate the minimum height of the tree. It is optional and defaults to the root of the tree if
460
+ * not provided.
461
+ * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
462
+ * to calculate the minimum height of a binary tree. It can have two possible values:
463
+ * @returns The function `getMinHeight` returns the minimum height of a binary tree.
422
464
  */
423
465
  getMinHeight(beginRoot: N | null = this.root, iterationType = this.iterationType): number {
424
466
  if (!beginRoot) return -1;
@@ -463,10 +505,10 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
463
505
  }
464
506
 
465
507
  /**
466
- * The function checks if a binary tree is perfectly balanced by comparing the minimum height and the height of the
467
- * tree.
468
- * @param {N | null} [beginRoot] - The parameter `beginRoot` is of type `N` or `null`. It represents the root node of a
469
- * tree or null if the tree is empty.
508
+ * The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
509
+ * height of the tree.
510
+ * @param {N | null} beginRoot - The parameter `beginRoot` is of type `N | null`, which means it can
511
+ * either be of type `N` (representing a node in a tree) or `null` (representing an empty tree).
470
512
  * @returns The method is returning a boolean value.
471
513
  */
472
514
  isPerfectlyBalanced(beginRoot: N | null = this.root): boolean {
@@ -474,17 +516,25 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
474
516
  }
475
517
 
476
518
  /**
477
- * The function `getNodes` returns an array of nodes that match a given property name and value in a binary tree.
478
- * @param callback
479
- * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
480
- * generic type `N`. It represents the property of the binary tree node that you want to search for.
481
- * specifies the property name to use when searching for nodes. If not provided, it defaults to 'key'.
482
- * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
483
- * return only one node that matches the given `nodeProperty` or `propertyName`. If `onlyOne` is set to `true`, the
484
- * function will stop traversing the tree and return the first matching node. If `only
485
- * @param beginRoot
486
- * @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
487
- * @returns an array of nodes (type N).
519
+ * The function `getNodes` returns an array of nodes that match a given node property, using either
520
+ * recursive or iterative traversal.
521
+ * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is either a
522
+ * `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
523
+ * searching for. It can be a specific key value or any other property of the node.
524
+ * @param callback - The `callback` parameter is a function that takes a node as input and returns a
525
+ * value. This value is compared with the `nodeProperty` parameter to determine if the node should be
526
+ * included in the result. The `callback` parameter has a default value of
527
+ * `this._defaultCallbackByKey`, which
528
+ * @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the
529
+ * first node that matches the nodeProperty. If set to true, the function will return an array with
530
+ * only one element (or an empty array if no matching node is found). If set to false (default), the
531
+ * function will continue searching for all
532
+ * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which the
533
+ * traversal of the binary tree will begin. It is optional and defaults to the root of the binary
534
+ * tree.
535
+ * @param iterationType - The `iterationType` parameter determines the type of iteration used to
536
+ * traverse the binary tree. It can have two possible values:
537
+ * @returns The function `getNodes` returns an array of nodes (`N[]`).
488
538
  */
489
539
  getNodes(
490
540
  nodeProperty: BinaryTreeNodeKey | N,
@@ -528,13 +578,20 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
528
578
  }
529
579
 
530
580
  /**
531
- * The function checks if a binary tree node has a specific property.
532
- * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
533
- * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`.
534
- * It represents the property of the binary tree node that you want to check.
535
- * specifies the name of the property to be checked in the nodes. If not provided, it defaults to 'key'.
536
- * @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the root node of a tree or null if the tree is empty.
537
- * @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
581
+ * The function checks if a binary tree has a node with a given property or key.
582
+ * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is the key or value of
583
+ * the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or a
584
+ * generic type `N`.
585
+ * @param callback - The `callback` parameter is a function that is used to determine whether a node
586
+ * matches the desired criteria. It takes a node as input and returns a boolean value indicating
587
+ * whether the node matches the criteria or not. The default callback function
588
+ * `this._defaultCallbackByKey` is used if no callback function is
589
+ * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
590
+ * the node from which the search should begin. By default, it is set to `this.root`, which means the
591
+ * search will start from the root node of the binary tree. However, you can provide a different node
592
+ * as
593
+ * @param iterationType - The `iterationType` parameter specifies the type of iteration to be
594
+ * performed when searching for nodes in the binary tree. It can have one of the following values:
538
595
  * @returns a boolean value.
539
596
  */
540
597
  has(
@@ -548,17 +605,19 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
548
605
  }
549
606
 
550
607
  /**
551
- * The function returns the first node that matches the given property name and value, or null if no matching node is
552
- * found.
553
- * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
554
- * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`.
555
- * It represents the property of the binary tree node that you want to search for.
556
- * specifies the property name to be used for searching the binary tree nodes. If this parameter is not provided, the
557
- * default value is set to `'key'`.
558
- * @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the root node of a tree or null if the tree is empty.
559
- * @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop used to traverse the binary tree.
560
- * @returns either the value of the specified property of the node, or the node itself if no property name is provided.
561
- * If no matching node is found, it returns null.
608
+ * The function `get` returns the first node in a binary tree that matches the given property or key.
609
+ * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is the key or value of
610
+ * the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or `N`
611
+ * type.
612
+ * @param callback - The `callback` parameter is a function that is used to determine whether a node
613
+ * matches the desired criteria. It takes a node as input and returns a boolean value indicating
614
+ * whether the node matches the criteria or not. The default callback function
615
+ * (`this._defaultCallbackByKey`) is used if no callback function is
616
+ * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
617
+ * the root node from which the search should begin.
618
+ * @param iterationType - The `iterationType` parameter specifies the type of iteration to be
619
+ * performed when searching for a node in the binary tree. It can have one of the following values:
620
+ * @returns either the found node (of type N) or null if no node is found.
562
621
  */
563
622
  get(
564
623
  nodeProperty: BinaryTreeNodeKey | N,
@@ -571,14 +630,14 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
571
630
  }
572
631
 
573
632
  /**
574
- * The function `getPathToRoot` returns an array of nodes representing the path from a given node to the root node, with
575
- * an option to reverse the order of the nodes.
576
- * type that represents a node in your specific implementation.
577
- * @param beginRoot - The `beginRoot` parameter is of type `N` and represents the starting node from which you want to
578
- * @param {boolean} [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the resulting
579
- * path should be reversed or not. If `isReverse` is set to `true`, the path will be reversed before returning it. If
580
- * `isReverse` is set to `false` or not provided, the path will
581
- * @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
633
+ * The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
634
+ * up to the root node, with the option to reverse the order of the nodes.
635
+ * @param {N} beginRoot - The `beginRoot` parameter represents the starting node from which you want
636
+ * to find the path to the root node.
637
+ * @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
638
+ * resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
639
+ * reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
640
+ * @returns The function `getPathToRoot` returns an array of type `N[]`.
582
641
  */
583
642
  getPathToRoot(beginRoot: N, isReverse = true): N[] {
584
643
  // TODO to support get path through passing key
@@ -594,16 +653,15 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
594
653
  }
595
654
 
596
655
  /**
597
- * The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if
598
- * no node is specified.
599
- * @param {N | BinaryTreeNodeKey | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
600
- * generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
601
- * node), or `null`.
602
- * @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop used to traverse the binary tree.
603
- * @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
604
- * provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal
605
- * from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost
606
- * node is found (
656
+ * The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
657
+ * iterative traversal.
658
+ * @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter is the starting point
659
+ * for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value
660
+ * of a node (`BinaryTreeNodeKey`), or `null` if the tree is empty.
661
+ * @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
662
+ * be performed when finding the leftmost node in a binary tree. It can have two possible values:
663
+ * @returns The function `getLeftMost` returns the leftmost node (`N`) in a binary tree. If there is
664
+ * no leftmost node, it returns `null`.
607
665
  */
608
666
  getLeftMost(beginRoot: N | BinaryTreeNodeKey | null = this.root, iterationType = this.iterationType): N | null {
609
667
  if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot);
@@ -629,15 +687,15 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
629
687
  }
630
688
 
631
689
  /**
632
- * The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using tail
633
- * recursion optimization.
634
- * @param {N | null} [beginRoot] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
635
- * starting node from which we want to find the rightmost node. If no node is provided, the function will default to
636
- * using the root node of the data structure.
637
- * @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
638
- * @returns The `getRightMost` function returns the rightmost node in a binary tree. If the `node` parameter is provided,
639
- * it returns the rightmost node starting from that node. If the `node` parameter is not provided, it returns the
640
- * rightmost node starting from the root of the binary tree.
690
+ * The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
691
+ * iteratively.
692
+ * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which we want to
693
+ * find the rightmost node. It is of type `N | null`, which means it can either be a node of type `N`
694
+ * or `null`. If it is `null`, it means there is no starting node
695
+ * @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
696
+ * be performed when finding the rightmost node in a binary tree. It can have two possible values:
697
+ * @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If the
698
+ * `beginRoot` parameter is `null`, it returns `null`.
641
699
  */
642
700
  getRightMost(beginRoot: N | null = this.root, iterationType = this.iterationType): N | null {
643
701
  // TODO support get right most by passing key in
@@ -662,10 +720,13 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
662
720
  }
663
721
 
664
722
  /**
665
- * The function checks if a binary search tree is valid by traversing it either recursively or iteratively.
666
- * @param {N | null} beginRoot - The `node` parameter represents the root node of a binary search tree (BST).
667
- * @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
668
- * @returns a boolean value.
723
+ * The function `isSubtreeBST` checks if a given binary tree is a valid binary search tree.
724
+ * @param {N} beginRoot - The `beginRoot` parameter is the root node of the binary tree that you want
725
+ * to check if it is a binary search tree (BST) subtree.
726
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
727
+ * type of iteration to use when checking if a subtree is a binary search tree (BST). It can have two
728
+ * possible values:
729
+ * @returns The function `isSubtreeBST` returns a boolean value.
669
730
  */
670
731
  isSubtreeBST(beginRoot: N, iterationType = this.iterationType): boolean {
671
732
  // TODO there is a bug
@@ -698,8 +759,12 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
698
759
  }
699
760
 
700
761
  /**
701
- * The function isBST checks if the binary tree is valid binary search tree.
702
- * @returns The `isBST()` function is returning a boolean value.
762
+ * The function checks if a binary tree is a binary search tree.
763
+ * @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
764
+ * be used when checking if the binary tree is a binary search tree (BST). It is an optional
765
+ * parameter with a default value of "this.iterationType". The value of "this.iterationType" is not
766
+ * provided in
767
+ * @returns a boolean value.
703
768
  */
704
769
  isBST(iterationType = this.iterationType): boolean {
705
770
  if (this.root === null) return true;
@@ -707,13 +772,18 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
707
772
  }
708
773
 
709
774
  /**
710
- * The function `subTreeTraverse` adds a delta value to a specified property of each node in a subtree.
711
- * @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the root node of a binary
712
- * tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
713
- * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
714
- * specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'key'.
715
- * @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
716
- * @returns a boolean value.
775
+ * The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
776
+ * node, either recursively or iteratively.
777
+ * @param callback - The `callback` parameter is a function that will be called on each node in the
778
+ * subtree traversal. It takes a single argument, which is the current node being traversed, and
779
+ * returns a value. The return values from each callback invocation will be collected and returned as
780
+ * an array.
781
+ * @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter is the starting point
782
+ * for traversing the subtree. It can be either a node object, a key value of a node, or `null` to
783
+ * start from the root of the tree.
784
+ * @param iterationType - The `iterationType` parameter determines the type of traversal to be
785
+ * performed on the binary tree. It can have two possible values:
786
+ * @returns The function `subTreeTraverse` returns an array of `MapCallbackReturn<N>`.
717
787
  */
718
788
  subTreeTraverse(
719
789
  callback: MapCallback<N> = this._defaultCallbackByKey,
@@ -748,13 +818,19 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
748
818
  }
749
819
 
750
820
  /**
751
- * The dfs function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
752
- * each node based on the specified pattern and property name.
753
- * @param callback
754
- * @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the
755
- * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
756
- * @param iterationType - The type of loop to use for the depth-first search traversal. The default value is `IterationType.ITERATIVE`.
757
- * @returns an instance of the BinaryTreeNodeProperties class, which contains the accumulated properties of the binary tree nodes based on the specified pattern and node or property name.
821
+ * The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
822
+ * function on each node according to a specified order pattern.
823
+ * @param callback - The `callback` parameter is a function that will be called on each node during
824
+ * the depth-first search traversal. It takes a node as input and returns a value. The default value
825
+ * is `this._defaultCallbackByKey`, which is a callback function defined elsewhere in the code.
826
+ * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
827
+ * nodes are visited during the depth-first search. There are three possible values for `pattern`:
828
+ * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
829
+ * search. It determines where the search will begin in the tree or graph structure. If `beginRoot`
830
+ * is `null`, an empty array will be returned.
831
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
832
+ * iteration used in the depth-first search algorithm. It can have two possible values:
833
+ * @returns The function `dfs` returns an array of `MapCallbackReturn<N>` values.
758
834
  */
759
835
  dfs(
760
836
  callback: MapCallback<N> = this._defaultCallbackByKey,
@@ -830,11 +906,21 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
830
906
  // --- start additional methods ---
831
907
 
832
908
  /**
833
- * The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
834
- * @param callback - The `callback` parameter is a function that takes a node and a level as parameters and returns a value.
835
- * @param withLevel - The `withLevel` parameter is a boolean flag that determines whether to include the level of each node in the result. If `withLevel` is set to `true`, the function will include the level of each node in the result. If `withLevel` is set to `false` or not provided, the function will not include the level of each node in the result.
836
- * @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the root node of a tree or null if the tree is empty.
837
- * @param iterationType
909
+ * The bfs function performs a breadth-first search traversal on a binary tree, executing a callback
910
+ * function on each node.
911
+ * @param callback - The `callback` parameter is a function that will be called for each node in the
912
+ * breadth-first search. It takes a node of type `N` as its argument and returns a value of type
913
+ * `BFSCallbackReturn<N>`. The default value for this parameter is `this._defaultCallbackByKey
914
+ * @param {boolean} [withLevel=false] - The `withLevel` parameter is a boolean flag that determines
915
+ * whether or not to include the level of each node in the callback function. If `withLevel` is set
916
+ * to `true`, the level of each node will be passed as an argument to the callback function. If
917
+ * `withLevel` is
918
+ * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
919
+ * search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
920
+ * will not be performed and an empty array will be returned.
921
+ * @param iterationType - The `iterationType` parameter determines the type of iteration to be used
922
+ * in the breadth-first search (BFS) algorithm. It can have two possible values:
923
+ * @returns The function `bfs` returns an array of `BFSCallbackReturn<N>[]`.
838
924
  */
839
925
  bfs(
840
926
  callback: BFSCallback<N> = this._defaultCallbackByKey,
@@ -870,9 +956,9 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
870
956
  }
871
957
 
872
958
  /**
873
- * The function returns the predecessor of a given node in a binary tree.
874
- * @param node - The parameter `node` is a BinaryTreeNode object, representing a node in a binary tree.
875
- * @returns the predecessor of the given node in a binary tree.
959
+ * The function returns the predecessor node of a given node in a binary tree.
960
+ * @param {N} node - The parameter "node" represents a node in a binary tree.
961
+ * @returns The function `getPredecessor` returns the predecessor node of the given node `node`.
876
962
  */
877
963
  getPredecessor(node: N): N {
878
964
  if (node.left) {
@@ -891,17 +977,24 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
891
977
  /**
892
978
  * Time complexity is O(n)
893
979
  * Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
894
- */
895
-
896
- /**
897
- * The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm.
898
980
  * The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
899
981
  * the tree's structure should be restored to its original state to maintain the tree's integrity.
900
982
  * This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
901
- * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
902
- * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
903
- * @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the
904
- * @returns An array of BinaryTreeNodeProperties<N> objects.
983
+ */
984
+
985
+ /**
986
+ * The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
987
+ * algorithm and returns an array of values obtained by applying a callback function to each node.
988
+ * @param callback - The `callback` parameter is a function that will be called on each node in the
989
+ * tree. It takes a node of type `N` as input and returns a value of type `MapCallbackReturn<N>`. The
990
+ * default value for this parameter is `this._defaultCallbackByKey`.
991
+ * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
992
+ * determines the order in which the nodes of a binary tree are traversed. It can have one of the
993
+ * following values:
994
+ * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the Morris
995
+ * traversal. It specifies the root node of the tree from which the traversal should begin. If
996
+ * `beginRoot` is `null`, an empty array will be returned.
997
+ * @returns The `morris` function returns an array of `MapCallbackReturn<N>` values.
905
998
  */
906
999
  morris(
907
1000
  callback: MapCallback<N> = this._defaultCallbackByKey,
@@ -989,14 +1082,15 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
989
1082
  }
990
1083
 
991
1084
  /**
992
- * The function adds a new node to a binary tree if there is an available position.
993
- * @param {N | null} newNode - The `newNode` parameter is of type `N | null`, which means it can either be a node of
994
- * type `N` or `null`. It represents the node that you want to add to the binary tree.
995
- * @param {N} parent - The parent parameter is of type N, which represents a node in a binary tree.
996
- * @returns either the left or right child node of the parent node, depending on which child is available for adding
997
- * the new node. If a new node is added, the function also updates the size of the binary tree. If neither the left nor
998
- * right child is available, the function returns undefined. If the parent node is null, the function also returns
999
- * undefined.
1085
+ * The function `_addTo` adds a new node to a binary tree if there is an available position.
1086
+ * @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
1087
+ * the binary tree. It can be either a node object or `null`.
1088
+ * @param {N} parent - The `parent` parameter represents the parent node to which the new node will
1089
+ * be added as a child.
1090
+ * @returns either the left or right child node of the parent node, depending on which child is
1091
+ * available for adding the new node. If a new node is added, the function also updates the size of
1092
+ * the binary tree. If neither the left nor right child is available, the function returns undefined.
1093
+ * If the parent node is null, the function also returns undefined.
1000
1094
  */
1001
1095
  protected _addTo(newNode: N | null, parent: N): N | null | undefined {
1002
1096
  if (parent) {
@@ -1023,9 +1117,10 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
1023
1117
  }
1024
1118
 
1025
1119
  /**
1026
- * The function sets the root property of an object to a given value, and if the value is not null, it also sets the
1027
- * parent property of the value to undefined.
1028
- * @param {N | null} v - The parameter `v` is of type `N | null`, which means it can either be of type `N` or `null`.
1120
+ * The function sets the root property of an object to a given value, and if the value is not null,
1121
+ * it also sets the parent property of the value to undefined.
1122
+ * @param {N | null} v - The parameter `v` is of type `N | null`, which means it can either be of
1123
+ * type `N` or `null`.
1029
1124
  */
1030
1125
  protected _setRoot(v: N | null) {
1031
1126
  if (v) {
@@ -1035,8 +1130,9 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
1035
1130
  }
1036
1131
 
1037
1132
  /**
1038
- * The function sets the size of a protected variable.
1039
- * @param {number} v - number
1133
+ * The function sets the value of the protected property "_size" to the given number.
1134
+ * @param {number} v - The parameter "v" is a number that represents the size value that we want to
1135
+ * set.
1040
1136
  */
1041
1137
  protected _setSize(v: number) {
1042
1138
  this._size = v;