min-heap-typed 1.38.4 → 1.38.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,6 +8,7 @@
8
8
  import { BST, BSTNode } from './bst';
9
9
  import type { AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeKey } from '../../types';
10
10
  import { IBinaryTree } from '../../interfaces';
11
+ import { MapCallback } from '../../types';
11
12
  export declare class AVLTreeNode<V = any, FAMILY extends AVLTreeNode<V, FAMILY> = AVLTreeNodeNested<V>> extends BSTNode<V, FAMILY> {
12
13
  height: number;
13
14
  constructor(key: BinaryTreeNodeKey, val?: V);
@@ -43,11 +44,16 @@ export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> e
43
44
  /**
44
45
  * The function overrides the delete method of a binary tree and balances the tree after deleting a
45
46
  * node if necessary.
46
- * @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object
47
- * (`N`) or a key value (`BinaryTreeNodeKey`).
47
+ * @param {ReturnType<C>} identifier - The `identifier` parameter is either a
48
+ * `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
49
+ * searching for. It can be a specific key value or any other property of the node.
50
+ * @param callback - The `callback` parameter is a function that takes a node as input and returns a
51
+ * value. This value is compared with the `identifier` parameter to determine if the node should be
52
+ * included in the result. The `callback` parameter has a default value of
53
+ * `this._defaultCallbackByKey`
48
54
  * @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
49
55
  */
50
- delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[];
56
+ delete<C extends MapCallback<N>>(identifier: ReturnType<C>, callback?: C): BinaryTreeDeletedResult<N>[];
51
57
  /**
52
58
  * The function swaps the key, value, and height properties between two nodes in a binary tree.
53
59
  * @param {N} srcNode - The `srcNode` parameter represents the source node that needs to be swapped
@@ -57,12 +57,17 @@ class AVLTree extends bst_1.BST {
57
57
  /**
58
58
  * The function overrides the delete method of a binary tree and balances the tree after deleting a
59
59
  * node if necessary.
60
- * @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object
61
- * (`N`) or a key value (`BinaryTreeNodeKey`).
60
+ * @param {ReturnType<C>} identifier - The `identifier` parameter is either a
61
+ * `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
62
+ * searching for. It can be a specific key value or any other property of the node.
63
+ * @param callback - The `callback` parameter is a function that takes a node as input and returns a
64
+ * value. This value is compared with the `identifier` parameter to determine if the node should be
65
+ * included in the result. The `callback` parameter has a default value of
66
+ * `this._defaultCallbackByKey`
62
67
  * @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
63
68
  */
64
- delete(nodeOrKey) {
65
- const deletedResults = super.delete(nodeOrKey);
69
+ delete(identifier, callback = this._defaultCallbackByKey) {
70
+ const deletedResults = super.delete(identifier, callback);
66
71
  for (const { needBalanced } of deletedResults) {
67
72
  if (needBalanced) {
68
73
  this._balancePath(needBalanced);
@@ -6,7 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import type { BFSCallback, BinaryTreeNodeKey, BinaryTreeNodeNested, BinaryTreeOptions, MapCallback } from '../../types';
9
- import { BinaryTreeDeletedResult, DFSOrderPattern, FamilyPosition, IterationType } from '../../types';
9
+ import { BinaryTreeDeletedResult, DefaultMapCallback, DFSOrderPattern, FamilyPosition, IterationType } from '../../types';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
  /**
12
12
  * Represents a node in a binary tree.
@@ -132,32 +132,25 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
132
132
  * @returns The method is returning a boolean value.
133
133
  */
134
134
  refill(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N[] | Array<N['val']>): boolean;
135
- /**
136
- * The `delete` function removes a node from a binary search tree and returns the deleted node along
137
- * with the parent node that needs to be balanced.
138
- * @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node (`N`) or
139
- * a key (`BinaryTreeNodeKey`). If it is a key, the function will find the corresponding node in the
140
- * binary tree.
141
- * @returns an array of `BinaryTreeDeletedResult<N>` objects.
142
- */
143
- delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[];
135
+ delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N): BinaryTreeDeletedResult<N>[];
136
+ delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): BinaryTreeDeletedResult<N>[];
144
137
  /**
145
138
  * The function `getDepth` calculates the depth of a given node in a binary tree relative to a
146
139
  * specified root node.
147
- * @param {N | BinaryTreeNodeKey | null} distNode - The `distNode` parameter represents the node
140
+ * @param {BinaryTreeNodeKey | N | null} distNode - The `distNode` parameter represents the node
148
141
  * whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value
149
142
  * of the node (`BinaryTreeNodeKey`), or `null`.
150
- * @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the
143
+ * @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter represents the
151
144
  * starting node from which we want to calculate the depth. It can be either a node object or the key
152
145
  * of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root
153
146
  * node of the binary tree.
154
147
  * @returns the depth of the `distNode` relative to the `beginRoot`.
155
148
  */
156
- getDepth(distNode: N | BinaryTreeNodeKey | null, beginRoot?: N | BinaryTreeNodeKey | null): number;
149
+ getDepth(distNode: BinaryTreeNodeKey | N | null, beginRoot?: BinaryTreeNodeKey | N | null): number;
157
150
  /**
158
151
  * The `getHeight` function calculates the maximum height of a binary tree using either recursive or
159
152
  * iterative approach.
160
- * @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the
153
+ * @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter represents the
161
154
  * starting node from which the height of the binary tree is calculated. It can be either a node
162
155
  * object (`N`), a key value of a node in the tree (`BinaryTreeNodeKey`), or `null` if no starting
163
156
  * node is specified. If `
@@ -166,7 +159,7 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
166
159
  * possible values:
167
160
  * @returns the height of the binary tree.
168
161
  */
169
- getHeight(beginRoot?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): number;
162
+ getHeight(beginRoot?: BinaryTreeNodeKey | N | null, iterationType?: IterationType): number;
170
163
  /**
171
164
  * The `getMinHeight` function calculates the minimum height of a binary tree using either a
172
165
  * recursive or iterative approach.
@@ -186,62 +179,21 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
186
179
  * @returns The method is returning a boolean value.
187
180
  */
188
181
  isPerfectlyBalanced(beginRoot?: N | null): boolean;
189
- /**
190
- * The function `getNodes` returns an array of nodes that match a given node property, using either
191
- * recursive or iterative traversal.
192
- * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is either a
193
- * `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
194
- * searching for. It can be a specific key value or any other property of the node.
195
- * @param callback - The `callback` parameter is a function that takes a node as input and returns a
196
- * value. This value is compared with the `nodeProperty` parameter to determine if the node should be
197
- * included in the result. The `callback` parameter has a default value of
198
- * `this._defaultCallbackByKey`, which
199
- * @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the
200
- * first node that matches the nodeProperty. If set to true, the function will return an array with
201
- * only one element (or an empty array if no matching node is found). If set to false (default), the
202
- * function will continue searching for all
203
- * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which the
204
- * traversal of the binary tree will begin. It is optional and defaults to the root of the binary
205
- * tree.
206
- * @param iterationType - The `iterationType` parameter determines the type of iteration used to
207
- * traverse the binary tree. It can have two possible values:
208
- * @returns The function `getNodes` returns an array of nodes (`N[]`).
209
- */
210
- getNodes<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(nodeProperty: BinaryTreeNodeKey | N, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
211
- /**
212
- * The function checks if a binary tree has a node with a given property or key.
213
- * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is the key or value of
214
- * the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or a
215
- * generic type `N`.
216
- * @param callback - The `callback` parameter is a function that is used to determine whether a node
217
- * matches the desired criteria. It takes a node as input and returns a boolean value indicating
218
- * whether the node matches the criteria or not. The default callback function
219
- * `this._defaultCallbackByKey` is used if no callback function is
220
- * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
221
- * the node from which the search should begin. By default, it is set to `this.root`, which means the
222
- * search will start from the root node of the binary tree. However, you can provide a different node
223
- * as
224
- * @param iterationType - The `iterationType` parameter specifies the type of iteration to be
225
- * performed when searching for nodes in the binary tree. It can have one of the following values:
226
- * @returns a boolean value.
227
- */
228
- has<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(nodeProperty: BinaryTreeNodeKey | N, callback?: C, beginRoot?: N | null, iterationType?: IterationType): boolean;
229
- /**
230
- * The function `get` returns the first node in a binary tree that matches the given property or key.
231
- * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is the key or value of
232
- * the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or `N`
233
- * type.
234
- * @param callback - The `callback` parameter is a function that is used to determine whether a node
235
- * matches the desired criteria. It takes a node as input and returns a boolean value indicating
236
- * whether the node matches the criteria or not. The default callback function
237
- * (`this._defaultCallbackByKey`) is used if no callback function is
238
- * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
239
- * the root node from which the search should begin.
240
- * @param iterationType - The `iterationType` parameter specifies the type of iteration to be
241
- * performed when searching for a node in the binary tree. It can have one of the following values:
242
- * @returns either the found node (of type N) or null if no node is found.
243
- */
244
- get<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(nodeProperty: BinaryTreeNodeKey | N, callback?: C, beginRoot?: N | null, iterationType?: IterationType): N | null;
182
+ getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N): N[];
183
+ getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): N[];
184
+ getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, onlyOne: boolean): N[];
185
+ getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, onlyOne: boolean): N[];
186
+ getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, onlyOne: boolean, beginRoot: N | null): N[];
187
+ getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, onlyOne: boolean, beginRoot: N | null, iterationType: IterationType): N[];
188
+ has<C extends MapCallback<N>>(identifier: ReturnType<C> | N): boolean;
189
+ has<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): boolean;
190
+ has<C extends MapCallback<N>>(identifier: ReturnType<C> | N, beginRoot: N | null): boolean;
191
+ has<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, beginRoot: N | null): boolean;
192
+ get<C extends MapCallback<N>>(identifier: ReturnType<C> | N): N | null;
193
+ get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): N | null;
194
+ get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, beginRoot: N | null): N | null;
195
+ get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, beginRoot: N | null): N | null;
196
+ get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, beginRoot: N | null, iterationType: IterationType): N | null;
245
197
  /**
246
198
  * The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
247
199
  * up to the root node, with the option to reverse the order of the nodes.
@@ -256,7 +208,7 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
256
208
  /**
257
209
  * The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
258
210
  * iterative traversal.
259
- * @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter is the starting point
211
+ * @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
260
212
  * for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value
261
213
  * of a node (`BinaryTreeNodeKey`), or `null` if the tree is empty.
262
214
  * @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
@@ -264,7 +216,7 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
264
216
  * @returns The function `getLeftMost` returns the leftmost node (`N`) in a binary tree. If there is
265
217
  * no leftmost node, it returns `null`.
266
218
  */
267
- getLeftMost(beginRoot?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): N | null;
219
+ getLeftMost(beginRoot?: BinaryTreeNodeKey | N | null, iterationType?: IterationType): N | null;
268
220
  /**
269
221
  * The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
270
222
  * iteratively.
@@ -303,14 +255,14 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
303
255
  * subtree traversal. It takes a single argument, which is the current node being traversed, and
304
256
  * returns a value. The return values from each callback invocation will be collected and returned as
305
257
  * an array.
306
- * @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter is the starting point
258
+ * @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
307
259
  * for traversing the subtree. It can be either a node object, a key value of a node, or `null` to
308
260
  * start from the root of the tree.
309
261
  * @param iterationType - The `iterationType` parameter determines the type of traversal to be
310
262
  * performed on the binary tree. It can have two possible values:
311
263
  * @returns The function `subTreeTraverse` returns an array of `MapCallbackReturn<N>`.
312
264
  */
313
- subTreeTraverse<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(callback?: C, beginRoot?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): ReturnType<C>[];
265
+ subTreeTraverse<C extends MapCallback<N>>(callback?: C, beginRoot?: BinaryTreeNodeKey | N | null, iterationType?: IterationType): ReturnType<C>[];
314
266
  /**
315
267
  * The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
316
268
  * function on each node according to a specified order pattern.
@@ -326,7 +278,7 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
326
278
  * iteration used in the depth-first search algorithm. It can have two possible values:
327
279
  * @returns The function `dfs` returns an array of `MapCallbackReturn<N>` values.
328
280
  */
329
- dfs<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[];
281
+ dfs<C extends MapCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[];
330
282
  /**
331
283
  * The bfs function performs a breadth-first search traversal on a binary tree, executing a callback
332
284
  * function on each node.
@@ -334,7 +286,7 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
334
286
  * breadth-first search. It takes a node of type `N` as its argument and returns a value of type
335
287
  * `BFSCallbackReturn<N>`. The default value for this parameter is `this._defaultCallbackByKey
336
288
  * @param {boolean} [withLevel=false] - The `withLevel` parameter is a boolean flag that determines
337
- * whether or not to include the level of each node in the callback function. If `withLevel` is set
289
+ * whether to include the level of each node in the callback function. If `withLevel` is set
338
290
  * to `true`, the level of each node will be passed as an argument to the callback function. If
339
291
  * `withLevel` is
340
292
  * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
@@ -365,7 +317,7 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
365
317
  * `beginRoot` is `null`, an empty array will be returned.
366
318
  * @returns The `morris` function returns an array of `MapCallbackReturn<N>` values.
367
319
  */
368
- morris<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null): ReturnType<C>[];
320
+ morris<C extends MapCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null): ReturnType<C>[];
369
321
  /**
370
322
  * Swap the data of two nodes in the binary tree.
371
323
  * @param {N} srcNode - The source node to swap.
@@ -380,7 +332,7 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
380
332
  * the tree's structure should be restored to its original state to maintain the tree's integrity.
381
333
  * This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
382
334
  */
383
- protected _defaultCallbackByKey: (node: N) => number;
335
+ protected _defaultCallbackByKey: DefaultMapCallback<N>;
384
336
  /**
385
337
  * The function `_addTo` adds a new node to a binary tree if there is an available position.
386
338
  * @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
@@ -64,35 +64,16 @@ class BinaryTreeNode {
64
64
  */
65
65
  get familyPosition() {
66
66
  const that = this;
67
- if (that.parent) {
68
- if (that.parent.left === that) {
69
- if (that.left || that.right) {
70
- return types_1.FamilyPosition.ROOT_LEFT;
71
- }
72
- else {
73
- return types_1.FamilyPosition.LEFT;
74
- }
75
- }
76
- else if (that.parent.right === that) {
77
- if (that.left || that.right) {
78
- return types_1.FamilyPosition.ROOT_RIGHT;
79
- }
80
- else {
81
- return types_1.FamilyPosition.RIGHT;
82
- }
83
- }
84
- else {
85
- return types_1.FamilyPosition.MAL_NODE;
86
- }
67
+ if (!this.parent) {
68
+ return this.left || this.right ? types_1.FamilyPosition.ROOT : types_1.FamilyPosition.ISOLATED;
87
69
  }
88
- else {
89
- if (that.left || that.right) {
90
- return types_1.FamilyPosition.ROOT;
91
- }
92
- else {
93
- return types_1.FamilyPosition.ISOLATED;
94
- }
70
+ if (this.parent.left === that) {
71
+ return this.left || this.right ? types_1.FamilyPosition.ROOT_LEFT : types_1.FamilyPosition.LEFT;
95
72
  }
73
+ else if (this.parent.right === that) {
74
+ return this.left || this.right ? types_1.FamilyPosition.ROOT_RIGHT : types_1.FamilyPosition.RIGHT;
75
+ }
76
+ return types_1.FamilyPosition.MAL_NODE;
96
77
  }
97
78
  }
98
79
  exports.BinaryTreeNode = BinaryTreeNode;
@@ -210,7 +191,8 @@ class BinaryTree {
210
191
  else {
211
192
  return;
212
193
  }
213
- const existNode = keyOrNode ? this.get(keyOrNode, this._defaultCallbackByKey) : undefined;
194
+ const key = typeof keyOrNode === 'number' ? keyOrNode : keyOrNode ? keyOrNode.key : undefined;
195
+ const existNode = key !== undefined ? this.get(key, this._defaultCallbackByKey) : undefined;
214
196
  if (this.root) {
215
197
  if (existNode) {
216
198
  existNode.val = val;
@@ -244,21 +226,16 @@ class BinaryTree {
244
226
  */
245
227
  addMany(keysOrNodes, values) {
246
228
  // TODO not sure addMany not be run multi times
247
- const inserted = [];
248
- for (let i = 0; i < keysOrNodes.length; i++) {
249
- const keyOrNode = keysOrNodes[i];
229
+ return keysOrNodes.map((keyOrNode, i) => {
250
230
  if (keyOrNode instanceof BinaryTreeNode) {
251
- inserted.push(this.add(keyOrNode.key, keyOrNode.val));
252
- continue;
231
+ return this.add(keyOrNode.key, keyOrNode.val);
253
232
  }
254
233
  if (keyOrNode === null) {
255
- inserted.push(this.add(null));
256
- continue;
234
+ return this.add(null);
257
235
  }
258
236
  const val = values === null || values === void 0 ? void 0 : values[i];
259
- inserted.push(this.add(keyOrNode, val));
260
- }
261
- return inserted;
237
+ return this.add(keyOrNode, val);
238
+ });
262
239
  }
263
240
  /**
264
241
  * The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
@@ -276,16 +253,24 @@ class BinaryTree {
276
253
  /**
277
254
  * The `delete` function removes a node from a binary search tree and returns the deleted node along
278
255
  * with the parent node that needs to be balanced.
279
- * @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node (`N`) or
280
256
  * a key (`BinaryTreeNodeKey`). If it is a key, the function will find the corresponding node in the
281
257
  * binary tree.
282
258
  * @returns an array of `BinaryTreeDeletedResult<N>` objects.
259
+ * @param {ReturnType<C>} identifier - The `identifier` parameter is either a
260
+ * `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
261
+ * searching for. It can be a specific key value or any other property of the node.
262
+ * @param callback - The `callback` parameter is a function that takes a node as input and returns a
263
+ * value. This value is compared with the `identifier` parameter to determine if the node should be
264
+ * included in the result. The `callback` parameter has a default value of
265
+ * `this._defaultCallbackByKey`, which
283
266
  */
284
- delete(nodeOrKey) {
267
+ delete(identifier, callback = this._defaultCallbackByKey) {
285
268
  const bstDeletedResult = [];
286
269
  if (!this.root)
287
270
  return bstDeletedResult;
288
- const curr = typeof nodeOrKey === 'number' ? this.get(nodeOrKey) : nodeOrKey;
271
+ if (identifier instanceof BinaryTreeNode)
272
+ callback = (node => node);
273
+ const curr = this.get(identifier, callback);
289
274
  if (!curr)
290
275
  return bstDeletedResult;
291
276
  const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
@@ -327,10 +312,10 @@ class BinaryTree {
327
312
  /**
328
313
  * The function `getDepth` calculates the depth of a given node in a binary tree relative to a
329
314
  * specified root node.
330
- * @param {N | BinaryTreeNodeKey | null} distNode - The `distNode` parameter represents the node
315
+ * @param {BinaryTreeNodeKey | N | null} distNode - The `distNode` parameter represents the node
331
316
  * whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value
332
317
  * of the node (`BinaryTreeNodeKey`), or `null`.
333
- * @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the
318
+ * @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter represents the
334
319
  * starting node from which we want to calculate the depth. It can be either a node object or the key
335
320
  * of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root
336
321
  * node of the binary tree.
@@ -354,7 +339,7 @@ class BinaryTree {
354
339
  /**
355
340
  * The `getHeight` function calculates the maximum height of a binary tree using either recursive or
356
341
  * iterative approach.
357
- * @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the
342
+ * @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter represents the
358
343
  * starting node from which the height of the binary tree is calculated. It can be either a node
359
344
  * object (`N`), a key value of a node in the tree (`BinaryTreeNodeKey`), or `null` if no starting
360
345
  * node is specified. If `
@@ -464,15 +449,15 @@ class BinaryTree {
464
449
  /**
465
450
  * The function `getNodes` returns an array of nodes that match a given node property, using either
466
451
  * recursive or iterative traversal.
467
- * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is either a
452
+ * @param {ReturnType<C>} identifier - The `identifier` parameter is either a
468
453
  * `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
469
454
  * searching for. It can be a specific key value or any other property of the node.
470
455
  * @param callback - The `callback` parameter is a function that takes a node as input and returns a
471
- * value. This value is compared with the `nodeProperty` parameter to determine if the node should be
456
+ * value. This value is compared with the `identifier` parameter to determine if the node should be
472
457
  * included in the result. The `callback` parameter has a default value of
473
458
  * `this._defaultCallbackByKey`, which
474
459
  * @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the
475
- * first node that matches the nodeProperty. If set to true, the function will return an array with
460
+ * first node that matches the identifier. If set to true, the function will return an array with
476
461
  * only one element (or an empty array if no matching node is found). If set to false (default), the
477
462
  * function will continue searching for all
478
463
  * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which the
@@ -482,13 +467,15 @@ class BinaryTree {
482
467
  * traverse the binary tree. It can have two possible values:
483
468
  * @returns The function `getNodes` returns an array of nodes (`N[]`).
484
469
  */
485
- getNodes(nodeProperty, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
470
+ getNodes(identifier, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
486
471
  if (!beginRoot)
487
472
  return [];
473
+ if (identifier instanceof BinaryTreeNode)
474
+ callback = (node => node);
488
475
  const ans = [];
489
476
  if (iterationType === types_1.IterationType.RECURSIVE) {
490
477
  const _traverse = (cur) => {
491
- if (callback(cur) === nodeProperty) {
478
+ if (callback(cur) === identifier) {
492
479
  ans.push(cur);
493
480
  if (onlyOne)
494
481
  return;
@@ -505,7 +492,7 @@ class BinaryTree {
505
492
  while (queue.size > 0) {
506
493
  const cur = queue.shift();
507
494
  if (cur) {
508
- if (callback(cur) === nodeProperty) {
495
+ if (callback(cur) === identifier) {
509
496
  ans.push(cur);
510
497
  if (onlyOne)
511
498
  return ans;
@@ -519,7 +506,7 @@ class BinaryTree {
519
506
  }
520
507
  /**
521
508
  * The function checks if a binary tree has a node with a given property or key.
522
- * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is the key or value of
509
+ * @param {BinaryTreeNodeKey | N} identifier - The `identifier` parameter is the key or value of
523
510
  * the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or a
524
511
  * generic type `N`.
525
512
  * @param callback - The `callback` parameter is a function that is used to determine whether a node
@@ -534,13 +521,15 @@ class BinaryTree {
534
521
  * performed when searching for nodes in the binary tree. It can have one of the following values:
535
522
  * @returns a boolean value.
536
523
  */
537
- has(nodeProperty, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
524
+ has(identifier, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
525
+ if (identifier instanceof BinaryTreeNode)
526
+ callback = (node => node);
538
527
  // TODO may support finding node by value equal
539
- return this.getNodes(nodeProperty, callback, true, beginRoot, iterationType).length > 0;
528
+ return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
540
529
  }
541
530
  /**
542
531
  * The function `get` returns the first node in a binary tree that matches the given property or key.
543
- * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is the key or value of
532
+ * @param {BinaryTreeNodeKey | N} identifier - The `identifier` parameter is the key or value of
544
533
  * the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or `N`
545
534
  * type.
546
535
  * @param callback - The `callback` parameter is a function that is used to determine whether a node
@@ -553,10 +542,12 @@ class BinaryTree {
553
542
  * performed when searching for a node in the binary tree. It can have one of the following values:
554
543
  * @returns either the found node (of type N) or null if no node is found.
555
544
  */
556
- get(nodeProperty, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
545
+ get(identifier, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
557
546
  var _a;
547
+ if (identifier instanceof BinaryTreeNode)
548
+ callback = (node => node);
558
549
  // TODO may support finding node by value equal
559
- return (_a = this.getNodes(nodeProperty, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
550
+ return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
560
551
  }
561
552
  /**
562
553
  * The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
@@ -583,7 +574,7 @@ class BinaryTree {
583
574
  /**
584
575
  * The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
585
576
  * iterative traversal.
586
- * @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter is the starting point
577
+ * @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
587
578
  * for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value
588
579
  * of a node (`BinaryTreeNodeKey`), or `null` if the tree is empty.
589
580
  * @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
@@ -707,7 +698,7 @@ class BinaryTree {
707
698
  * subtree traversal. It takes a single argument, which is the current node being traversed, and
708
699
  * returns a value. The return values from each callback invocation will be collected and returned as
709
700
  * an array.
710
- * @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter is the starting point
701
+ * @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
711
702
  * for traversing the subtree. It can be either a node object, a key value of a node, or `null` to
712
703
  * start from the root of the tree.
713
704
  * @param iterationType - The `iterationType` parameter determines the type of traversal to be
@@ -831,7 +822,7 @@ class BinaryTree {
831
822
  * breadth-first search. It takes a node of type `N` as its argument and returns a value of type
832
823
  * `BFSCallbackReturn<N>`. The default value for this parameter is `this._defaultCallbackByKey
833
824
  * @param {boolean} [withLevel=false] - The `withLevel` parameter is a boolean flag that determines
834
- * whether or not to include the level of each node in the callback function. If `withLevel` is set
825
+ * whether to include the level of each node in the callback function. If `withLevel` is set
835
826
  * to `true`, the level of each node will be passed as an argument to the callback function. If
836
827
  * `withLevel` is
837
828
  * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
@@ -43,7 +43,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
43
43
  /**
44
44
  * The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
45
45
  * maintaining balance.
46
- * @param {[BinaryTreeNodeKey | N, N['val']][]} arr - The `arr` parameter in the `addMany` function
46
+ * @param {[BinaryTreeNodeKey | N, N['val']][]} keysOrNodes - The `arr` parameter in the `addMany` function
47
47
  * represents an array of keys or nodes that need to be added to the binary search tree. It can be an
48
48
  * array of `BinaryTreeNodeKey` or `N` (which represents the node type in the binary search tree) or
49
49
  * `null
@@ -57,7 +57,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
57
57
  /**
58
58
  * The function returns the first node in the binary tree that matches the given node property and
59
59
  * callback.
60
- * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is used to specify the
60
+ * @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter is used to specify the
61
61
  * property of the binary tree node that you want to search for. It can be either a specific key
62
62
  * value (`BinaryTreeNodeKey`) or a custom callback function (`MapCallback<N>`) that determines
63
63
  * whether a node matches the desired property.
@@ -72,7 +72,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
72
72
  * @returns either the first node that matches the given nodeProperty and callback, or null if no
73
73
  * matching node is found.
74
74
  */
75
- get<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(nodeProperty: BinaryTreeNodeKey | N, callback?: C, beginRoot?: N | null, iterationType?: IterationType): N | null;
75
+ get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback?: C, beginRoot?: N | null, iterationType?: IterationType): N | null;
76
76
  /**
77
77
  * The function `lastKey` returns the key of the rightmost node if the comparison result is less
78
78
  * than, the key of the leftmost node if the comparison result is greater than, and the key of the
@@ -92,7 +92,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
92
92
  /**
93
93
  * The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
94
94
  * using either recursive or iterative traversal.
95
- * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter represents the property
95
+ * @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter represents the property
96
96
  * of the binary tree node that you want to search for. It can be either a `BinaryTreeNodeKey` or a
97
97
  * generic type `N`.
98
98
  * @param callback - The `callback` parameter is a function that takes a node as input and returns a
@@ -110,7 +110,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
110
110
  * traverse the binary tree. It can have one of the following values:
111
111
  * @returns an array of nodes (N[]).
112
112
  */
113
- getNodes<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(nodeProperty: BinaryTreeNodeKey | N, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
113
+ getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
114
114
  /**
115
115
  * The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
116
116
  * nodes that have a key value lesser or greater than a target key value.
@@ -120,7 +120,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
120
120
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
121
121
  * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
122
122
  * of the following values:
123
- * @param {N | BinaryTreeNodeKey | null} targetNode - The `targetNode` parameter in the
123
+ * @param {BinaryTreeNodeKey | N | null} targetNode - The `targetNode` parameter in the
124
124
  * `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
125
125
  * start. It can be either a reference to a specific node (`N`), the key of a node
126
126
  * (`BinaryTreeNodeKey`), or `null` to
@@ -128,7 +128,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
128
128
  * done recursively or iteratively. It can have two possible values:
129
129
  * @returns The function `lesserOrGreaterTraverse` returns an array of `MapCallbackReturn<N>`.
130
130
  */
131
- lesserOrGreaterTraverse<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(callback?: C, lesserOrGreater?: CP, targetNode?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): ReturnType<C>[];
131
+ lesserOrGreaterTraverse<C extends MapCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BinaryTreeNodeKey | N | null, iterationType?: IterationType): ReturnType<C>[];
132
132
  /**
133
133
  * Balancing Adjustment:
134
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.