data-structure-typed 1.37.2 → 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 (42) hide show
  1. package/CHANGELOG.md +3 -1
  2. package/dist/data-structures/binary-tree/avl-tree.d.ts +44 -38
  3. package/dist/data-structures/binary-tree/avl-tree.js +46 -40
  4. package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +305 -192
  6. package/dist/data-structures/binary-tree/binary-tree.js +304 -201
  7. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
  8. package/dist/data-structures/binary-tree/bst.d.ts +111 -64
  9. package/dist/data-structures/binary-tree/bst.js +132 -85
  10. package/dist/data-structures/binary-tree/bst.js.map +1 -1
  11. package/dist/data-structures/binary-tree/tree-multiset.d.ts +49 -41
  12. package/dist/data-structures/binary-tree/tree-multiset.js +49 -41
  13. package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
  14. package/dist/types/data-structures/binary-tree.d.ts +2 -2
  15. package/dist/types/data-structures/binary-tree.js +6 -6
  16. package/dist/types/data-structures/binary-tree.js.map +1 -1
  17. package/lib/data-structures/binary-tree/avl-tree.d.ts +44 -38
  18. package/lib/data-structures/binary-tree/avl-tree.js +46 -40
  19. package/lib/data-structures/binary-tree/binary-tree.d.ts +305 -192
  20. package/lib/data-structures/binary-tree/binary-tree.js +305 -202
  21. package/lib/data-structures/binary-tree/bst.d.ts +111 -64
  22. package/lib/data-structures/binary-tree/bst.js +133 -86
  23. package/lib/data-structures/binary-tree/tree-multiset.d.ts +49 -41
  24. package/lib/data-structures/binary-tree/tree-multiset.js +50 -42
  25. package/lib/types/data-structures/binary-tree.d.ts +2 -2
  26. package/lib/types/data-structures/binary-tree.js +5 -5
  27. package/package.json +6 -6
  28. package/src/data-structures/binary-tree/avl-tree.ts +46 -40
  29. package/src/data-structures/binary-tree/binary-tree.ts +328 -207
  30. package/src/data-structures/binary-tree/bst.ts +135 -88
  31. package/src/data-structures/binary-tree/tree-multiset.ts +50 -42
  32. package/src/types/data-structures/binary-tree.ts +2 -2
  33. package/test/config.ts +1 -0
  34. package/test/integration/avl-tree.test.ts +7 -8
  35. package/test/integration/bst.test.ts +17 -16
  36. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +50 -0
  37. package/test/unit/data-structures/binary-tree/bst.test.ts +8 -1
  38. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +2 -1
  39. package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
  40. package/test/utils/big-o.ts +2 -1
  41. package/umd/bundle.min.js +1 -1
  42. package/umd/bundle.min.js.map +1 -1
@@ -6,7 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import type { BinaryTreeNodeKey, BSTComparator, BSTNodeNested, BSTOptions, MapCallback, MapCallbackReturn } from '../../types';
9
- import { CP } from '../../types';
9
+ import { CP, IterationType } from '../../types';
10
10
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
11
11
  import { IBinaryTree } from '../../interfaces';
12
12
  export declare class BSTNode<V = any, FAMILY extends BSTNode<V, FAMILY> = BSTNodeNested<V>> extends BinaryTreeNode<V, FAMILY> {
@@ -14,79 +14,121 @@ export declare class BSTNode<V = any, FAMILY extends BSTNode<V, FAMILY> = BSTNod
14
14
  }
15
15
  export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N> implements IBinaryTree<N> {
16
16
  /**
17
- * The constructor function initializes a binary search tree object with an optional comparator function.
18
- * @param {BSTOptions} [options] - An optional object that contains configuration options for the binary search tree.
17
+ * The constructor function initializes a binary search tree object with an optional comparator
18
+ * function.
19
+ * @param {BSTOptions} [options] - An optional object that contains configuration options for the
20
+ * binary search tree.
19
21
  */
20
22
  constructor(options?: BSTOptions);
21
23
  /**
22
24
  * The function creates a new binary search tree node with the given key and value.
23
- * @param {BinaryTreeNodeKey} key - The `key` parameter is the identifier for the binary tree node. It is used to uniquely
24
- * identify each node in the binary tree.
25
- * @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
26
- * that will be stored in the node.
25
+ * @param {BinaryTreeNodeKey} key - The key parameter is the key value that will be associated with
26
+ * the new node. It is used to determine the position of the node in the binary search tree.
27
+ * @param [val] - The parameter `val` is an optional value that can be assigned to the node. It
28
+ * represents the value associated with the node in a binary search tree.
27
29
  * @returns a new instance of the BSTNode class with the specified key and value.
28
30
  */
29
31
  createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
30
32
  /**
31
- * The `add` function adds a new node to a binary search tree, either by creating a new node or by updating an existing
32
- * node with the same ID.
33
- * @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey` or a `N`
34
- * (which represents a binary tree node) or `null`.
35
- * @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
36
- * being added to the binary search tree.
37
- * @returns The function `add` returns the inserted node (`inserted`) which can be of type `N`, `null`, or `undefined`.
33
+ * The `add` function in a binary search tree class inserts a new node with a given key and value
34
+ * into the tree.
35
+ * @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
36
+ * `BinaryTreeNodeKey` (which can be a number or a string), a `BSTNode` object, or `null`.
37
+ * @param [val] - The `val` parameter is the value to be assigned to the new node being added to the
38
+ * binary search tree.
39
+ * @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
40
+ * was not added or if the parameters were invalid, it returns null or undefined.
38
41
  */
39
42
  add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
40
43
  /**
41
- * The `addMany` function overrides the base class method to add multiple nodes to a binary search tree in a balanced
42
- * manner.
43
- * @param {[BinaryTreeNodeKey | N , N['val']][]} keysOrNodes - The `keysOrNodes` parameter in the `addMany` function is an array of
44
- * `BinaryTreeNodeKey` or `N` (node) objects, or `null` values. It represents the nodes or node IDs that need to be added
45
- * to the binary search tree.
44
+ * The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
45
+ * maintaining balance.
46
+ * @param {[BinaryTreeNodeKey | N, N['val']][]} arr - The `arr` parameter in the `addMany` function
47
+ * represents an array of keys or nodes that need to be added to the binary search tree. It can be an
48
+ * array of `BinaryTreeNodeKey` or `N` (which represents the node type in the binary search tree) or
49
+ * `null
46
50
  * @param {N['val'][]} data - The values of tree nodes
47
51
  * @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
48
- * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
52
+ * @param iterationType - The `iterationType` parameter determines the type of iteration to be used.
53
+ * It can have two possible values:
54
+ * @returns The `addMany` function returns an array of `N`, `null`, or `undefined` values.
49
55
  */
50
- addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N['val'][], isBalanceAdd?: boolean): (N | null | undefined)[];
56
+ addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N['val'][], isBalanceAdd?: boolean, iterationType?: IterationType): (N | null | undefined)[];
51
57
  /**
52
- * The function returns the first node in a binary tree that matches the given property name and value.
53
- * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
54
- * generic type `N`. It represents the property of the binary tree node that you want to search for.
55
- * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
56
- * specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'key'`.
57
- * @returns The method is returning either a BinaryTreeNodeKey or N (generic type) or null.
58
+ * The function returns the first node in the binary tree that matches the given node property and
59
+ * callback.
60
+ * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is used to specify the
61
+ * property of the binary tree node that you want to search for. It can be either a specific key
62
+ * value (`BinaryTreeNodeKey`) or a custom callback function (`MapCallback<N>`) that determines
63
+ * whether a node matches the desired property.
64
+ * @param callback - The `callback` parameter is a function that is used to determine whether a node
65
+ * matches the desired property. It takes a node as input and returns a boolean value indicating
66
+ * whether the node matches the property or not. If no callback function is provided, the default
67
+ * callback function `_defaultCallbackByKey` is used
68
+ * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
69
+ * the root node from which the search should begin.
70
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
71
+ * be performed when searching for nodes in the binary tree. It can have one of the following values:
72
+ * @returns either the first node that matches the given nodeProperty and callback, or null if no
73
+ * matching node is found.
58
74
  */
59
- get(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>): N | null;
75
+ get(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>, beginRoot?: N | null, iterationType?: IterationType): N | null;
60
76
  /**
61
- * The function returns the key of the rightmost node if the comparison between two values is less than, the key of the
62
- * leftmost node if the comparison is greater than, and the key of the rightmost node otherwise.
63
- * @returns The method `lastKey()` returns the key of the rightmost node in the binary tree if the comparison between
64
- * the values at index 0 and 1 is less than, otherwise it returns the key of the leftmost node. If the comparison is
65
- * equal, it returns the key of the rightmost node. If there are no nodes in the tree, it returns 0.
77
+ * The function `lastKey` returns the key of the rightmost node if the comparison result is less
78
+ * than, the key of the leftmost node if the comparison result is greater than, and the key of the
79
+ * rightmost node otherwise.
80
+ * @param {N | null} beginRoot - The `beginRoot` parameter is the starting point for finding the last
81
+ * key in a binary tree. It represents the root node of the subtree from which the search for the
82
+ * last key should begin. If no specific `beginRoot` is provided, the search will start from the root
83
+ * of the entire binary
84
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
85
+ * be performed when finding the last key. It determines whether the iteration should be performed in
86
+ * pre-order, in-order, or post-order.
87
+ * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
88
+ * the key of the leftmost node if the comparison result is greater than, and the key of the
89
+ * rightmost node otherwise. If no node is found, it returns 0.
66
90
  */
67
- lastKey(): BinaryTreeNodeKey;
91
+ lastKey(beginRoot?: N | null, iterationType?: IterationType): BinaryTreeNodeKey;
68
92
  /**
69
- * The function `getNodes` returns an array of nodes in a binary tree that match a given property value.
70
- * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or an
71
- * `N` type. It represents the property of the binary tree node that you want to compare with.
72
- * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
73
- * specifies the property name to use for comparison. If not provided, it defaults to `'key'`.
74
- * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
75
- * return only one node that matches the given `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne`
76
- * is set to `true`, the function will return an array with only one node (if
77
- * @param beginRoot - The `beginRoot` parameter is an optional parameter that specifies the root node from which to
78
- * @returns an array of nodes (type N).
93
+ * The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
94
+ * using either recursive or iterative traversal.
95
+ * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter represents the property
96
+ * of the binary tree node that you want to search for. It can be either a `BinaryTreeNodeKey` or a
97
+ * generic type `N`.
98
+ * @param callback - The `callback` parameter is a function that takes a node as input and returns a
99
+ * value. This value is compared with the `nodeProperty` parameter to determine if the node should be
100
+ * included in the result. The default value for `callback` is `this._defaultCallbackByKey`, which is
101
+ * a
102
+ * @param [onlyOne=false] - A boolean value indicating whether to stop the traversal after finding
103
+ * the first node that matches the nodeProperty. If set to true, the function will return an array
104
+ * containing only that node. If set to false (default), the function will continue the traversal and
105
+ * return an array containing all nodes that match the node
106
+ * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the traversal. It
107
+ * specifies the root node of the binary tree from which the traversal should begin. If `beginRoot`
108
+ * is `null`, an empty array will be returned.
109
+ * @param iterationType - The `iterationType` parameter determines the type of iteration used to
110
+ * traverse the binary tree. It can have one of the following values:
111
+ * @returns an array of nodes (N[]).
79
112
  */
80
- getNodes(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>, onlyOne?: boolean, beginRoot?: N | null): N[];
113
+ getNodes(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
81
114
  /**
82
- * The `lesserOrGreaterTraverse` function adds a delta value to the specified property of all nodes in a binary tree that
83
- * have a greater value than a given node.
84
- * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
85
- * @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be either of type `N` (a generic type), `BinaryTreeNodeKey`, or `null`. It
86
- * represents the node in the binary tree to which the delta value will be added.
87
- * @param lesserOrGreater - The `lesserOrGreater` parameter is an optional parameter that specifies whether the delta
115
+ * The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
116
+ * nodes that have a key value lesser or greater than a target key value.
117
+ * @param callback - The `callback` parameter is a function that will be called for each node that
118
+ * meets the condition specified by the `lesserOrGreater` parameter. It takes a node as an argument
119
+ * and returns a value.
120
+ * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
121
+ * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
122
+ * of the following values:
123
+ * @param {N | BinaryTreeNodeKey | null} targetNode - The `targetNode` parameter in the
124
+ * `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
125
+ * start. It can be either a reference to a specific node (`N`), the key of a node
126
+ * (`BinaryTreeNodeKey`), or `null` to
127
+ * @param iterationType - The `iterationType` parameter determines whether the traversal should be
128
+ * done recursively or iteratively. It can have two possible values:
129
+ * @returns The function `lesserOrGreaterTraverse` returns an array of `MapCallbackReturn<N>`.
88
130
  */
89
- lesserOrGreaterTraverse(callback: MapCallback<N> | undefined, lesserOrGreater: CP | undefined, node: N | BinaryTreeNodeKey | null): MapCallbackReturn<N>;
131
+ lesserOrGreaterTraverse(callback?: MapCallback<N>, lesserOrGreater?: CP, targetNode?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): MapCallbackReturn<N>;
90
132
  /**
91
133
  * Balancing Adjustment:
92
134
  * Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
@@ -97,24 +139,29 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
97
139
  * AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
98
140
  */
99
141
  /**
100
- * The `perfectlyBalance` function takes a binary tree, performs a depth-first search to sort the nodes, and then
101
- * constructs a balanced binary search tree using either a recursive or iterative approach.
102
- * @returns The function `perfectlyBalance()` returns a boolean value.
142
+ * The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
143
+ * ensures the tree is perfectly balanced.
144
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
145
+ * type of iteration to use when building a balanced binary search tree. It can have two possible
146
+ * values:
147
+ * @returns The function `perfectlyBalance` returns a boolean value.
103
148
  */
104
- perfectlyBalance(): boolean;
149
+ perfectlyBalance(iterationType?: IterationType): boolean;
105
150
  /**
106
- * The function `isAVLBalanced` checks if a binary tree is balanced according to the AVL tree property.
151
+ * The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
152
+ * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
153
+ * to check if the AVL tree is balanced. It can have two possible values:
107
154
  * @returns a boolean value.
108
155
  */
109
- isAVLBalanced(): boolean;
156
+ isAVLBalanced(iterationType?: IterationType): boolean;
110
157
  protected _comparator: BSTComparator;
111
158
  /**
112
- * The function compares two binary tree node IDs using a comparator function and returns whether the first ID is
113
- * greater than, less than, or equal to the second ID.
114
- * @param {BinaryTreeNodeKey} a - "a" is a BinaryTreeNodeKey, which represents the identifier of a binary tree node.
115
- * @param {BinaryTreeNodeKey} b - The parameter "b" in the above code refers to a BinaryTreeNodeKey.
116
- * @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater than), CP.lt (less
117
- * than), or CP.eq (equal).
159
+ * The function compares two values using a comparator function and returns whether the first value
160
+ * is greater than, less than, or equal to the second value.
161
+ * @param {BinaryTreeNodeKey} a - The parameter "a" is of type BinaryTreeNodeKey.
162
+ * @param {BinaryTreeNodeKey} b - The parameter "b" in the above code represents a BinaryTreeNodeKey.
163
+ * @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
164
+ * than), CP.lt (less than), or CP.eq (equal).
118
165
  */
119
166
  protected _compare(a: BinaryTreeNodeKey, b: BinaryTreeNodeKey): CP;
120
167
  }
@@ -1,4 +1,4 @@
1
- import { CP, LoopType } from '../../types';
1
+ import { CP, IterationType } from '../../types';
2
2
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
3
3
  import { Queue } from '../queue';
4
4
  export class BSTNode extends BinaryTreeNode {
@@ -8,8 +8,10 @@ export class BSTNode extends BinaryTreeNode {
8
8
  }
9
9
  export class BST extends BinaryTree {
10
10
  /**
11
- * The constructor function initializes a binary search tree object with an optional comparator function.
12
- * @param {BSTOptions} [options] - An optional object that contains configuration options for the binary search tree.
11
+ * The constructor function initializes a binary search tree object with an optional comparator
12
+ * function.
13
+ * @param {BSTOptions} [options] - An optional object that contains configuration options for the
14
+ * binary search tree.
13
15
  */
14
16
  constructor(options) {
15
17
  super(options);
@@ -23,23 +25,24 @@ export class BST extends BinaryTree {
23
25
  }
24
26
  /**
25
27
  * The function creates a new binary search tree node with the given key and value.
26
- * @param {BinaryTreeNodeKey} key - The `key` parameter is the identifier for the binary tree node. It is used to uniquely
27
- * identify each node in the binary tree.
28
- * @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
29
- * that will be stored in the node.
28
+ * @param {BinaryTreeNodeKey} key - The key parameter is the key value that will be associated with
29
+ * the new node. It is used to determine the position of the node in the binary search tree.
30
+ * @param [val] - The parameter `val` is an optional value that can be assigned to the node. It
31
+ * represents the value associated with the node in a binary search tree.
30
32
  * @returns a new instance of the BSTNode class with the specified key and value.
31
33
  */
32
34
  createNode(key, val) {
33
35
  return new BSTNode(key, val);
34
36
  }
35
37
  /**
36
- * The `add` function adds a new node to a binary search tree, either by creating a new node or by updating an existing
37
- * node with the same ID.
38
- * @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey` or a `N`
39
- * (which represents a binary tree node) or `null`.
40
- * @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
41
- * being added to the binary search tree.
42
- * @returns The function `add` returns the inserted node (`inserted`) which can be of type `N`, `null`, or `undefined`.
38
+ * The `add` function in a binary search tree class inserts a new node with a given key and value
39
+ * into the tree.
40
+ * @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
41
+ * `BinaryTreeNodeKey` (which can be a number or a string), a `BSTNode` object, or `null`.
42
+ * @param [val] - The `val` parameter is the value to be assigned to the new node being added to the
43
+ * binary search tree.
44
+ * @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
45
+ * was not added or if the parameters were invalid, it returns null or undefined.
43
46
  */
44
47
  add(keyOrNode, val) {
45
48
  // TODO support node as a parameter
@@ -117,16 +120,19 @@ export class BST extends BinaryTree {
117
120
  return inserted;
118
121
  }
119
122
  /**
120
- * The `addMany` function overrides the base class method to add multiple nodes to a binary search tree in a balanced
121
- * manner.
122
- * @param {[BinaryTreeNodeKey | N , N['val']][]} keysOrNodes - The `keysOrNodes` parameter in the `addMany` function is an array of
123
- * `BinaryTreeNodeKey` or `N` (node) objects, or `null` values. It represents the nodes or node IDs that need to be added
124
- * to the binary search tree.
123
+ * The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
124
+ * maintaining balance.
125
+ * @param {[BinaryTreeNodeKey | N, N['val']][]} arr - The `arr` parameter in the `addMany` function
126
+ * represents an array of keys or nodes that need to be added to the binary search tree. It can be an
127
+ * array of `BinaryTreeNodeKey` or `N` (which represents the node type in the binary search tree) or
128
+ * `null
125
129
  * @param {N['val'][]} data - The values of tree nodes
126
130
  * @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
127
- * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
131
+ * @param iterationType - The `iterationType` parameter determines the type of iteration to be used.
132
+ * It can have two possible values:
133
+ * @returns The `addMany` function returns an array of `N`, `null`, or `undefined` values.
128
134
  */
129
- addMany(keysOrNodes, data, isBalanceAdd = true) {
135
+ addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.iterationType) {
130
136
  // TODO this addMany function is inefficient, it should be optimized
131
137
  function hasNoNull(arr) {
132
138
  return arr.indexOf(null) === -1;
@@ -187,7 +193,7 @@ export class BST extends BinaryTree {
187
193
  }
188
194
  }
189
195
  };
190
- if (this.loopType === LoopType.RECURSIVE) {
196
+ if (iterationType === IterationType.RECURSIVE) {
191
197
  recursive(sortedKeysOrNodes, sortedData);
192
198
  }
193
199
  else {
@@ -196,50 +202,77 @@ export class BST extends BinaryTree {
196
202
  return inserted;
197
203
  }
198
204
  /**
199
- * The function returns the first node in a binary tree that matches the given property name and value.
200
- * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
201
- * generic type `N`. It represents the property of the binary tree node that you want to search for.
202
- * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
203
- * specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'key'`.
204
- * @returns The method is returning either a BinaryTreeNodeKey or N (generic type) or null.
205
+ * The function returns the first node in the binary tree that matches the given node property and
206
+ * callback.
207
+ * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is used to specify the
208
+ * property of the binary tree node that you want to search for. It can be either a specific key
209
+ * value (`BinaryTreeNodeKey`) or a custom callback function (`MapCallback<N>`) that determines
210
+ * whether a node matches the desired property.
211
+ * @param callback - The `callback` parameter is a function that is used to determine whether a node
212
+ * matches the desired property. It takes a node as input and returns a boolean value indicating
213
+ * whether the node matches the property or not. If no callback function is provided, the default
214
+ * callback function `_defaultCallbackByKey` is used
215
+ * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
216
+ * the root node from which the search should begin.
217
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
218
+ * be performed when searching for nodes in the binary tree. It can have one of the following values:
219
+ * @returns either the first node that matches the given nodeProperty and callback, or null if no
220
+ * matching node is found.
205
221
  */
206
- get(nodeProperty, callback = this._defaultCallbackByKey) {
222
+ get(nodeProperty, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
207
223
  var _a;
208
- return (_a = this.getNodes(nodeProperty, callback, true)[0]) !== null && _a !== void 0 ? _a : null;
224
+ return (_a = this.getNodes(nodeProperty, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
209
225
  }
210
226
  /**
211
- * The function returns the key of the rightmost node if the comparison between two values is less than, the key of the
212
- * leftmost node if the comparison is greater than, and the key of the rightmost node otherwise.
213
- * @returns The method `lastKey()` returns the key of the rightmost node in the binary tree if the comparison between
214
- * the values at index 0 and 1 is less than, otherwise it returns the key of the leftmost node. If the comparison is
215
- * equal, it returns the key of the rightmost node. If there are no nodes in the tree, it returns 0.
227
+ * The function `lastKey` returns the key of the rightmost node if the comparison result is less
228
+ * than, the key of the leftmost node if the comparison result is greater than, and the key of the
229
+ * rightmost node otherwise.
230
+ * @param {N | null} beginRoot - The `beginRoot` parameter is the starting point for finding the last
231
+ * key in a binary tree. It represents the root node of the subtree from which the search for the
232
+ * last key should begin. If no specific `beginRoot` is provided, the search will start from the root
233
+ * of the entire binary
234
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
235
+ * be performed when finding the last key. It determines whether the iteration should be performed in
236
+ * pre-order, in-order, or post-order.
237
+ * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
238
+ * the key of the leftmost node if the comparison result is greater than, and the key of the
239
+ * rightmost node otherwise. If no node is found, it returns 0.
216
240
  */
217
- lastKey() {
241
+ lastKey(beginRoot = this.root, iterationType = this.iterationType) {
218
242
  var _a, _b, _c, _d, _e, _f;
219
243
  if (this._compare(0, 1) === CP.lt)
220
- return (_b = (_a = this.getRightMost()) === null || _a === void 0 ? void 0 : _a.key) !== null && _b !== void 0 ? _b : 0;
244
+ return (_b = (_a = this.getRightMost(beginRoot, iterationType)) === null || _a === void 0 ? void 0 : _a.key) !== null && _b !== void 0 ? _b : 0;
221
245
  else if (this._compare(0, 1) === CP.gt)
222
- return (_d = (_c = this.getLeftMost()) === null || _c === void 0 ? void 0 : _c.key) !== null && _d !== void 0 ? _d : 0;
246
+ return (_d = (_c = this.getLeftMost(beginRoot, iterationType)) === null || _c === void 0 ? void 0 : _c.key) !== null && _d !== void 0 ? _d : 0;
223
247
  else
224
- return (_f = (_e = this.getRightMost()) === null || _e === void 0 ? void 0 : _e.key) !== null && _f !== void 0 ? _f : 0;
248
+ return (_f = (_e = this.getRightMost(beginRoot, iterationType)) === null || _e === void 0 ? void 0 : _e.key) !== null && _f !== void 0 ? _f : 0;
225
249
  }
226
250
  /**
227
- * The function `getNodes` returns an array of nodes in a binary tree that match a given property value.
228
- * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or an
229
- * `N` type. It represents the property of the binary tree node that you want to compare with.
230
- * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
231
- * specifies the property name to use for comparison. If not provided, it defaults to `'key'`.
232
- * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
233
- * return only one node that matches the given `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne`
234
- * is set to `true`, the function will return an array with only one node (if
235
- * @param beginRoot - The `beginRoot` parameter is an optional parameter that specifies the root node from which to
236
- * @returns an array of nodes (type N).
251
+ * The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
252
+ * using either recursive or iterative traversal.
253
+ * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter represents the property
254
+ * of the binary tree node that you want to search for. It can be either a `BinaryTreeNodeKey` or a
255
+ * generic type `N`.
256
+ * @param callback - The `callback` parameter is a function that takes a node as input and returns a
257
+ * value. This value is compared with the `nodeProperty` parameter to determine if the node should be
258
+ * included in the result. The default value for `callback` is `this._defaultCallbackByKey`, which is
259
+ * a
260
+ * @param [onlyOne=false] - A boolean value indicating whether to stop the traversal after finding
261
+ * the first node that matches the nodeProperty. If set to true, the function will return an array
262
+ * containing only that node. If set to false (default), the function will continue the traversal and
263
+ * return an array containing all nodes that match the node
264
+ * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the traversal. It
265
+ * specifies the root node of the binary tree from which the traversal should begin. If `beginRoot`
266
+ * is `null`, an empty array will be returned.
267
+ * @param iterationType - The `iterationType` parameter determines the type of iteration used to
268
+ * traverse the binary tree. It can have one of the following values:
269
+ * @returns an array of nodes (N[]).
237
270
  */
238
- getNodes(nodeProperty, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root) {
271
+ getNodes(nodeProperty, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
239
272
  if (!beginRoot)
240
273
  return [];
241
274
  const ans = [];
242
- if (this.loopType === LoopType.RECURSIVE) {
275
+ if (iterationType === IterationType.RECURSIVE) {
243
276
  const _traverse = (cur) => {
244
277
  const callbackResult = callback(cur);
245
278
  if (callbackResult === nodeProperty) {
@@ -292,48 +325,57 @@ export class BST extends BinaryTree {
292
325
  }
293
326
  // --- start additional functions
294
327
  /**
295
- * The `lesserOrGreaterTraverse` function adds a delta value to the specified property of all nodes in a binary tree that
296
- * have a greater value than a given node.
297
- * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
298
- * @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be either of type `N` (a generic type), `BinaryTreeNodeKey`, or `null`. It
299
- * represents the node in the binary tree to which the delta value will be added.
300
- * @param lesserOrGreater - The `lesserOrGreater` parameter is an optional parameter that specifies whether the delta
328
+ * The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
329
+ * nodes that have a key value lesser or greater than a target key value.
330
+ * @param callback - The `callback` parameter is a function that will be called for each node that
331
+ * meets the condition specified by the `lesserOrGreater` parameter. It takes a node as an argument
332
+ * and returns a value.
333
+ * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
334
+ * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
335
+ * of the following values:
336
+ * @param {N | BinaryTreeNodeKey | null} targetNode - The `targetNode` parameter in the
337
+ * `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
338
+ * start. It can be either a reference to a specific node (`N`), the key of a node
339
+ * (`BinaryTreeNodeKey`), or `null` to
340
+ * @param iterationType - The `iterationType` parameter determines whether the traversal should be
341
+ * done recursively or iteratively. It can have two possible values:
342
+ * @returns The function `lesserOrGreaterTraverse` returns an array of `MapCallbackReturn<N>`.
301
343
  */
302
- lesserOrGreaterTraverse(callback = this._defaultCallbackByKey, lesserOrGreater = CP.lt, node) {
303
- if (typeof node === 'number')
304
- node = this.get(node);
344
+ lesserOrGreaterTraverse(callback = this._defaultCallbackByKey, lesserOrGreater = CP.lt, targetNode = this.root, iterationType = this.iterationType) {
345
+ if (typeof targetNode === 'number')
346
+ targetNode = this.get(targetNode);
305
347
  const ans = [];
306
- if (!node)
307
- return [];
308
- const key = node.key;
348
+ if (!targetNode)
349
+ return ans;
350
+ const targetKey = targetNode.key;
309
351
  if (!this.root)
310
- return false;
311
- if (this.loopType === LoopType.RECURSIVE) {
352
+ return ans;
353
+ if (iterationType === IterationType.RECURSIVE) {
312
354
  const _traverse = (cur) => {
313
- const compared = this._compare(cur.key, key);
355
+ const compared = this._compare(cur.key, targetKey);
314
356
  if (compared === lesserOrGreater)
315
357
  ans.push(callback(cur));
316
358
  if (!cur.left && !cur.right)
317
359
  return;
318
- if (cur.left && this._compare(cur.left.key, key) === lesserOrGreater)
360
+ if (cur.left && this._compare(cur.left.key, targetKey) === lesserOrGreater)
319
361
  _traverse(cur.left);
320
- if (cur.right && this._compare(cur.right.key, key) === lesserOrGreater)
362
+ if (cur.right && this._compare(cur.right.key, targetKey) === lesserOrGreater)
321
363
  _traverse(cur.right);
322
364
  };
323
365
  _traverse(this.root);
324
- return true;
366
+ return ans;
325
367
  }
326
368
  else {
327
369
  const queue = new Queue([this.root]);
328
370
  while (queue.size > 0) {
329
371
  const cur = queue.shift();
330
372
  if (cur) {
331
- const compared = this._compare(cur.key, key);
373
+ const compared = this._compare(cur.key, targetKey);
332
374
  if (compared === lesserOrGreater)
333
375
  ans.push(callback(cur));
334
- if (cur.left && this._compare(cur.left.key, key) === lesserOrGreater)
376
+ if (cur.left && this._compare(cur.left.key, targetKey) === lesserOrGreater)
335
377
  queue.push(cur.left);
336
- if (cur.right && this._compare(cur.right.key, key) === lesserOrGreater)
378
+ if (cur.right && this._compare(cur.right.key, targetKey) === lesserOrGreater)
337
379
  queue.push(cur.right);
338
380
  }
339
381
  }
@@ -350,16 +392,19 @@ export class BST extends BinaryTree {
350
392
  * AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
351
393
  */
352
394
  /**
353
- * The `perfectlyBalance` function takes a binary tree, performs a depth-first search to sort the nodes, and then
354
- * constructs a balanced binary search tree using either a recursive or iterative approach.
355
- * @returns The function `perfectlyBalance()` returns a boolean value.
395
+ * The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
396
+ * ensures the tree is perfectly balanced.
397
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
398
+ * type of iteration to use when building a balanced binary search tree. It can have two possible
399
+ * values:
400
+ * @returns The function `perfectlyBalance` returns a boolean value.
356
401
  */
357
- perfectlyBalance() {
402
+ perfectlyBalance(iterationType = this.iterationType) {
358
403
  const sorted = this.dfs(node => node, 'in'), n = sorted.length;
359
404
  this.clear();
360
405
  if (sorted.length < 1)
361
406
  return false;
362
- if (this.loopType === LoopType.RECURSIVE) {
407
+ if (iterationType === IterationType.RECURSIVE) {
363
408
  const buildBalanceBST = (l, r) => {
364
409
  if (l > r)
365
410
  return;
@@ -391,15 +436,17 @@ export class BST extends BinaryTree {
391
436
  }
392
437
  }
393
438
  /**
394
- * The function `isAVLBalanced` checks if a binary tree is balanced according to the AVL tree property.
439
+ * The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
440
+ * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
441
+ * to check if the AVL tree is balanced. It can have two possible values:
395
442
  * @returns a boolean value.
396
443
  */
397
- isAVLBalanced() {
444
+ isAVLBalanced(iterationType = this.iterationType) {
398
445
  var _a, _b;
399
446
  if (!this.root)
400
447
  return true;
401
448
  let balanced = true;
402
- if (this.loopType === LoopType.RECURSIVE) {
449
+ if (iterationType === IterationType.RECURSIVE) {
403
450
  const _height = (cur) => {
404
451
  if (!cur)
405
452
  return 0;
@@ -441,12 +488,12 @@ export class BST extends BinaryTree {
441
488
  return balanced;
442
489
  }
443
490
  /**
444
- * The function compares two binary tree node IDs using a comparator function and returns whether the first ID is
445
- * greater than, less than, or equal to the second ID.
446
- * @param {BinaryTreeNodeKey} a - "a" is a BinaryTreeNodeKey, which represents the identifier of a binary tree node.
447
- * @param {BinaryTreeNodeKey} b - The parameter "b" in the above code refers to a BinaryTreeNodeKey.
448
- * @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater than), CP.lt (less
449
- * than), or CP.eq (equal).
491
+ * The function compares two values using a comparator function and returns whether the first value
492
+ * is greater than, less than, or equal to the second value.
493
+ * @param {BinaryTreeNodeKey} a - The parameter "a" is of type BinaryTreeNodeKey.
494
+ * @param {BinaryTreeNodeKey} b - The parameter "b" in the above code represents a BinaryTreeNodeKey.
495
+ * @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
496
+ * than), CP.lt (less than), or CP.eq (equal).
450
497
  */
451
498
  _compare(a, b) {
452
499
  const compared = this._comparator(a, b);