data-structure-typed 1.37.1 → 1.37.3

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 (48) hide show
  1. package/CHANGELOG.md +3 -1
  2. package/dist/data-structures/binary-tree/avl-tree.d.ts +5 -7
  3. package/dist/data-structures/binary-tree/avl-tree.js +7 -9
  4. package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +61 -209
  6. package/dist/data-structures/binary-tree/binary-tree.js +133 -269
  7. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
  8. package/dist/data-structures/binary-tree/bst.d.ts +22 -19
  9. package/dist/data-structures/binary-tree/bst.js +68 -54
  10. package/dist/data-structures/binary-tree/bst.js.map +1 -1
  11. package/dist/data-structures/binary-tree/tree-multiset.d.ts +2 -36
  12. package/dist/data-structures/binary-tree/tree-multiset.js +3 -81
  13. package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
  14. package/dist/data-structures/heap/heap.d.ts +1 -1
  15. package/dist/data-structures/heap/heap.js +1 -1
  16. package/dist/types/data-structures/binary-tree.d.ts +4 -2
  17. package/dist/types/data-structures/binary-tree.js +6 -6
  18. package/dist/types/data-structures/binary-tree.js.map +1 -1
  19. package/dist/types/data-structures/index.d.ts +2 -0
  20. package/lib/data-structures/binary-tree/avl-tree.d.ts +5 -7
  21. package/lib/data-structures/binary-tree/avl-tree.js +7 -9
  22. package/lib/data-structures/binary-tree/binary-tree.d.ts +61 -209
  23. package/lib/data-structures/binary-tree/binary-tree.js +134 -270
  24. package/lib/data-structures/binary-tree/bst.d.ts +22 -19
  25. package/lib/data-structures/binary-tree/bst.js +69 -55
  26. package/lib/data-structures/binary-tree/tree-multiset.d.ts +2 -36
  27. package/lib/data-structures/binary-tree/tree-multiset.js +4 -82
  28. package/lib/data-structures/heap/heap.d.ts +1 -1
  29. package/lib/data-structures/heap/heap.js +1 -1
  30. package/lib/types/data-structures/binary-tree.d.ts +4 -2
  31. package/lib/types/data-structures/binary-tree.js +5 -5
  32. package/lib/types/data-structures/index.d.ts +2 -0
  33. package/package.json +6 -6
  34. package/src/data-structures/binary-tree/avl-tree.ts +7 -9
  35. package/src/data-structures/binary-tree/binary-tree.ts +79 -54
  36. package/src/data-structures/binary-tree/bst.ts +37 -32
  37. package/src/data-structures/binary-tree/tree-multiset.ts +3 -3
  38. package/src/types/data-structures/binary-tree.ts +2 -2
  39. package/test/config.ts +1 -0
  40. package/test/integration/avl-tree.test.ts +23 -21
  41. package/test/integration/bst.test.ts +49 -44
  42. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +50 -0
  43. package/test/unit/data-structures/binary-tree/bst.test.ts +8 -1
  44. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +2 -1
  45. package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
  46. package/test/utils/big-o.ts +2 -1
  47. package/umd/bundle.min.js +1 -1
  48. package/umd/bundle.min.js.map +1 -1
@@ -5,8 +5,8 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BinaryTreeNodeKey, BinaryTreeNodePropertyName, BSTComparator, BSTNodeNested, BSTOptions } from '../../types';
9
- import { CP } from '../../types';
8
+ import type { BinaryTreeNodeKey, BSTComparator, BSTNodeNested, BSTOptions, MapCallback, MapCallbackReturn } 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> {
@@ -45,47 +45,50 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
45
45
  * to the binary search tree.
46
46
  * @param {N['val'][]} data - The values of tree nodes
47
47
  * @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
48
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies whether to use a
48
49
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
49
50
  */
50
- addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N['val'][], isBalanceAdd?: boolean): (N | null | undefined)[];
51
+ addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N['val'][], isBalanceAdd?: boolean, iterationType?: IterationType): (N | null | undefined)[];
51
52
  /**
52
53
  * The function returns the first node in a binary tree that matches the given property name and value.
53
54
  * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
54
55
  * generic type `N`. It represents the property of the binary tree node that you want to search for.
55
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
56
+ * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
56
57
  * specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'key'`.
57
58
  * @returns The method is returning either a BinaryTreeNodeKey or N (generic type) or null.
58
59
  */
59
- get(nodeProperty: BinaryTreeNodeKey | N, propertyName?: BinaryTreeNodePropertyName): N | null;
60
+ get(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>): N | null;
60
61
  /**
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.
62
+ * lastKey returns the last key in a binary tree. If the binary tree is empty, it returns 0.
63
+ * @param beginRoot - The `beginRoot` parameter is an optional parameter that specifies the root node from which to begin
64
+ * the search for the last key.
65
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies whether to use a recursive or iterative approach to search for the last key.
66
66
  */
67
- lastKey(): BinaryTreeNodeKey;
67
+ lastKey(beginRoot?: N | null, iterationType?: IterationType): BinaryTreeNodeKey;
68
68
  /**
69
69
  * The function `getNodes` returns an array of nodes in a binary tree that match a given property value.
70
70
  * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or an
71
71
  * `N` type. It represents the property of the binary tree node that you want to compare with.
72
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
72
+ * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
73
73
  * specifies the property name to use for comparison. If not provided, it defaults to `'key'`.
74
74
  * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
75
75
  * return only one node that matches the given `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne`
76
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
+ * @param iterationType
77
79
  * @returns an array of nodes (type N).
78
80
  */
79
- getNodes(nodeProperty: BinaryTreeNodeKey | N, propertyName?: BinaryTreeNodePropertyName, onlyOne?: boolean): N[];
81
+ getNodes(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
80
82
  /**
81
- * The `lesserOrGreaterForeach` function adds a delta value to the specified property of all nodes in a binary tree that
83
+ * The `lesserOrGreaterTraverse` function adds a delta value to the specified property of all nodes in a binary tree that
82
84
  * have a greater value than a given node.
83
- * @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be either of type `N` (a generic type), `BinaryTreeNodeKey`, or `null`. It
85
+ * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
84
86
  * represents the node in the binary tree to which the delta value will be added.
85
87
  * @param lesserOrGreater - The `lesserOrGreater` parameter is an optional parameter that specifies whether the delta
86
- * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a boolean
88
+ * @param targetNode - The `targetNode` parameter is an optional parameter that specifies the node in the binary tree
89
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies whether to use a
87
90
  */
88
- lesserOrGreaterForeach(node: N | BinaryTreeNodeKey | null, lesserOrGreater: CP | undefined, callback: (node: N) => void): boolean;
91
+ lesserOrGreaterTraverse(callback?: MapCallback<N>, lesserOrGreater?: CP, targetNode?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): MapCallbackReturn<N>;
89
92
  /**
90
93
  * Balancing Adjustment:
91
94
  * 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.
@@ -100,12 +103,12 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
100
103
  * constructs a balanced binary search tree using either a recursive or iterative approach.
101
104
  * @returns The function `perfectlyBalance()` returns a boolean value.
102
105
  */
103
- perfectlyBalance(): boolean;
106
+ perfectlyBalance(iterationType?: IterationType): boolean;
104
107
  /**
105
108
  * The function `isAVLBalanced` checks if a binary tree is balanced according to the AVL tree property.
106
109
  * @returns a boolean value.
107
110
  */
108
- isAVLBalanced(): boolean;
111
+ isAVLBalanced(iterationType?: IterationType): boolean;
109
112
  protected _comparator: BSTComparator;
110
113
  /**
111
114
  * The function compares two binary tree node IDs using a comparator function and returns whether the first ID is
@@ -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 {
@@ -124,9 +124,10 @@ export class BST extends BinaryTree {
124
124
  * to the binary search tree.
125
125
  * @param {N['val'][]} data - The values of tree nodes
126
126
  * @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
127
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies whether to use a
127
128
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
128
129
  */
129
- addMany(keysOrNodes, data, isBalanceAdd = true) {
130
+ addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.iterationType) {
130
131
  // TODO this addMany function is inefficient, it should be optimized
131
132
  function hasNoNull(arr) {
132
133
  return arr.indexOf(null) === -1;
@@ -187,7 +188,7 @@ export class BST extends BinaryTree {
187
188
  }
188
189
  }
189
190
  };
190
- if (this.loopType === LoopType.RECURSIVE) {
191
+ if (iterationType === IterationType.RECURSIVE) {
191
192
  recursive(sortedKeysOrNodes, sortedData);
192
193
  }
193
194
  else {
@@ -199,52 +200,58 @@ export class BST extends BinaryTree {
199
200
  * The function returns the first node in a binary tree that matches the given property name and value.
200
201
  * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
201
202
  * generic type `N`. It represents the property of the binary tree node that you want to search for.
202
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
203
+ * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
203
204
  * specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'key'`.
204
205
  * @returns The method is returning either a BinaryTreeNodeKey or N (generic type) or null.
205
206
  */
206
- get(nodeProperty, propertyName = 'key') {
207
+ get(nodeProperty, callback = this._defaultCallbackByKey) {
207
208
  var _a;
208
- return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
209
+ return (_a = this.getNodes(nodeProperty, callback, true)[0]) !== null && _a !== void 0 ? _a : null;
209
210
  }
210
211
  /**
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.
212
+ * lastKey returns the last key in a binary tree. If the binary tree is empty, it returns 0.
213
+ * @param beginRoot - The `beginRoot` parameter is an optional parameter that specifies the root node from which to begin
214
+ * the search for the last key.
215
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies whether to use a recursive or iterative approach to search for the last key.
216
216
  */
217
- lastKey() {
217
+ lastKey(beginRoot = this.root, iterationType = this.iterationType) {
218
218
  var _a, _b, _c, _d, _e, _f;
219
219
  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;
220
+ return (_b = (_a = this.getRightMost(beginRoot, iterationType)) === null || _a === void 0 ? void 0 : _a.key) !== null && _b !== void 0 ? _b : 0;
221
221
  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;
222
+ return (_d = (_c = this.getLeftMost(beginRoot, iterationType)) === null || _c === void 0 ? void 0 : _c.key) !== null && _d !== void 0 ? _d : 0;
223
223
  else
224
- return (_f = (_e = this.getRightMost()) === null || _e === void 0 ? void 0 : _e.key) !== null && _f !== void 0 ? _f : 0;
224
+ return (_f = (_e = this.getRightMost(beginRoot, iterationType)) === null || _e === void 0 ? void 0 : _e.key) !== null && _f !== void 0 ? _f : 0;
225
225
  }
226
226
  /**
227
227
  * The function `getNodes` returns an array of nodes in a binary tree that match a given property value.
228
228
  * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or an
229
229
  * `N` type. It represents the property of the binary tree node that you want to compare with.
230
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
230
+ * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
231
231
  * specifies the property name to use for comparison. If not provided, it defaults to `'key'`.
232
232
  * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
233
233
  * return only one node that matches the given `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne`
234
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
+ * @param iterationType
235
237
  * @returns an array of nodes (type N).
236
238
  */
237
- getNodes(nodeProperty, propertyName = 'key', onlyOne = false) {
238
- if (!this.root)
239
+ getNodes(nodeProperty, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
240
+ if (!beginRoot)
239
241
  return [];
240
- const result = [];
241
- if (this.loopType === LoopType.RECURSIVE) {
242
+ const ans = [];
243
+ if (iterationType === IterationType.RECURSIVE) {
242
244
  const _traverse = (cur) => {
243
- if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
244
- return;
245
+ const callbackResult = callback(cur);
246
+ if (callbackResult === nodeProperty) {
247
+ ans.push(cur);
248
+ if (onlyOne)
249
+ return;
250
+ }
245
251
  if (!cur.left && !cur.right)
246
252
  return;
247
- if (propertyName === 'key') {
253
+ // TODO potential bug
254
+ if (callback === this._defaultCallbackByKey) {
248
255
  if (this._compare(cur.key, nodeProperty) === CP.gt)
249
256
  cur.left && _traverse(cur.left);
250
257
  if (this._compare(cur.key, nodeProperty) === CP.lt)
@@ -255,16 +262,21 @@ export class BST extends BinaryTree {
255
262
  cur.right && _traverse(cur.right);
256
263
  }
257
264
  };
258
- _traverse(this.root);
265
+ _traverse(beginRoot);
259
266
  }
260
267
  else {
261
- const queue = new Queue([this.root]);
268
+ const queue = new Queue([beginRoot]);
262
269
  while (queue.size > 0) {
263
270
  const cur = queue.shift();
264
271
  if (cur) {
265
- if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
266
- return result;
267
- if (propertyName === 'key') {
272
+ const callbackResult = callback(cur);
273
+ if (callbackResult === nodeProperty) {
274
+ ans.push(cur);
275
+ if (onlyOne)
276
+ return ans;
277
+ }
278
+ // TODO potential bug
279
+ if (callback === this._defaultCallbackByKey) {
268
280
  if (this._compare(cur.key, nodeProperty) === CP.gt)
269
281
  cur.left && queue.push(cur.left);
270
282
  if (this._compare(cur.key, nodeProperty) === CP.lt)
@@ -277,55 +289,57 @@ export class BST extends BinaryTree {
277
289
  }
278
290
  }
279
291
  }
280
- return result;
292
+ return ans;
281
293
  }
282
294
  // --- start additional functions
283
295
  /**
284
- * The `lesserOrGreaterForeach` function adds a delta value to the specified property of all nodes in a binary tree that
296
+ * The `lesserOrGreaterTraverse` function adds a delta value to the specified property of all nodes in a binary tree that
285
297
  * have a greater value than a given node.
286
- * @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be either of type `N` (a generic type), `BinaryTreeNodeKey`, or `null`. It
298
+ * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
287
299
  * represents the node in the binary tree to which the delta value will be added.
288
300
  * @param lesserOrGreater - The `lesserOrGreater` parameter is an optional parameter that specifies whether the delta
289
- * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a boolean
301
+ * @param targetNode - The `targetNode` parameter is an optional parameter that specifies the node in the binary tree
302
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies whether to use a
290
303
  */
291
- lesserOrGreaterForeach(node, lesserOrGreater = CP.lt, callback) {
292
- if (typeof node === 'number')
293
- node = this.get(node, 'key');
294
- if (!node)
295
- return false;
296
- const key = node.key;
304
+ lesserOrGreaterTraverse(callback = this._defaultCallbackByKey, lesserOrGreater = CP.lt, targetNode = this.root, iterationType = this.iterationType) {
305
+ if (typeof targetNode === 'number')
306
+ targetNode = this.get(targetNode);
307
+ const ans = [];
308
+ if (!targetNode)
309
+ return ans;
310
+ const targetKey = targetNode.key;
297
311
  if (!this.root)
298
- return false;
299
- if (this.loopType === LoopType.RECURSIVE) {
312
+ return ans;
313
+ if (iterationType === IterationType.RECURSIVE) {
300
314
  const _traverse = (cur) => {
301
- const compared = this._compare(cur.key, key);
315
+ const compared = this._compare(cur.key, targetKey);
302
316
  if (compared === lesserOrGreater)
303
- callback(cur);
317
+ ans.push(callback(cur));
304
318
  if (!cur.left && !cur.right)
305
319
  return;
306
- if (cur.left && this._compare(cur.left.key, key) === lesserOrGreater)
320
+ if (cur.left && this._compare(cur.left.key, targetKey) === lesserOrGreater)
307
321
  _traverse(cur.left);
308
- if (cur.right && this._compare(cur.right.key, key) === lesserOrGreater)
322
+ if (cur.right && this._compare(cur.right.key, targetKey) === lesserOrGreater)
309
323
  _traverse(cur.right);
310
324
  };
311
325
  _traverse(this.root);
312
- return true;
326
+ return ans;
313
327
  }
314
328
  else {
315
329
  const queue = new Queue([this.root]);
316
330
  while (queue.size > 0) {
317
331
  const cur = queue.shift();
318
332
  if (cur) {
319
- const compared = this._compare(cur.key, key);
333
+ const compared = this._compare(cur.key, targetKey);
320
334
  if (compared === lesserOrGreater)
321
- callback(cur);
322
- if (cur.left && this._compare(cur.left.key, key) === lesserOrGreater)
335
+ ans.push(callback(cur));
336
+ if (cur.left && this._compare(cur.left.key, targetKey) === lesserOrGreater)
323
337
  queue.push(cur.left);
324
- if (cur.right && this._compare(cur.right.key, key) === lesserOrGreater)
338
+ if (cur.right && this._compare(cur.right.key, targetKey) === lesserOrGreater)
325
339
  queue.push(cur.right);
326
340
  }
327
341
  }
328
- return true;
342
+ return ans;
329
343
  }
330
344
  }
331
345
  /**
@@ -342,12 +356,12 @@ export class BST extends BinaryTree {
342
356
  * constructs a balanced binary search tree using either a recursive or iterative approach.
343
357
  * @returns The function `perfectlyBalance()` returns a boolean value.
344
358
  */
345
- perfectlyBalance() {
346
- const sorted = this.dfs('in', 'node'), n = sorted.length;
359
+ perfectlyBalance(iterationType = this.iterationType) {
360
+ const sorted = this.dfs(node => node, 'in'), n = sorted.length;
347
361
  this.clear();
348
362
  if (sorted.length < 1)
349
363
  return false;
350
- if (this.loopType === LoopType.RECURSIVE) {
364
+ if (iterationType === IterationType.RECURSIVE) {
351
365
  const buildBalanceBST = (l, r) => {
352
366
  if (l > r)
353
367
  return;
@@ -382,12 +396,12 @@ export class BST extends BinaryTree {
382
396
  * The function `isAVLBalanced` checks if a binary tree is balanced according to the AVL tree property.
383
397
  * @returns a boolean value.
384
398
  */
385
- isAVLBalanced() {
399
+ isAVLBalanced(iterationType = this.iterationType) {
386
400
  var _a, _b;
387
401
  if (!this.root)
388
402
  return true;
389
403
  let balanced = true;
390
- if (this.loopType === LoopType.RECURSIVE) {
404
+ if (iterationType === IterationType.RECURSIVE) {
391
405
  const _height = (cur) => {
392
406
  if (!cur)
393
407
  return 0;
@@ -6,7 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import type { BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions } from '../../types';
9
- import { BinaryTreeDeletedResult, DFSOrderPattern } from '../../types';
9
+ import { BinaryTreeDeletedResult, IterationType } from '../../types';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
  import { AVLTree, AVLTreeNode } from './avl-tree';
12
12
  export declare class TreeMultisetNode<V = any, FAMILY extends TreeMultisetNode<V, FAMILY> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, FAMILY> {
@@ -91,7 +91,7 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
91
91
  * constructs a balanced binary search tree using either a recursive or iterative approach.
92
92
  * @returns The function `perfectlyBalance()` returns a boolean value.
93
93
  */
94
- perfectlyBalance(): boolean;
94
+ perfectlyBalance(iterationType?: IterationType): boolean;
95
95
  /**
96
96
  * The `delete` function removes a node from a binary search tree and returns the deleted node along with the parent
97
97
  * node that needs to be balanced.
@@ -102,40 +102,6 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
102
102
  * @returns The function `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
103
103
  */
104
104
  delete(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
105
- /**
106
- * The function `getNodesByCount` returns an array of nodes that have a specific count property, either recursively or
107
- * using a queue.
108
- * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
109
- * `N`. It represents the property of the nodes that you want to search for.
110
- * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
111
- * return only one node that matches the `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne` is set
112
- * to `true`, the function will return only one node. If `onlyOne`
113
- * @returns an array of nodes that match the given nodeProperty.
114
- */
115
- getNodesByCount(nodeProperty: BinaryTreeNodeKey | N, onlyOne?: boolean): N[];
116
- /**
117
- * The BFSCount function returns an array of counts from a breadth-first search of nodes.
118
- * @returns The BFSCount() function returns an array of numbers, specifically the count property of each node in the
119
- * bfs traversal.
120
- */
121
- bfsCount(): number[];
122
- /**
123
- * The function "listLevelsCount" takes a node and returns an array of arrays, where each inner array contains the
124
- * count property of each node at that level.
125
- * @param {N | null} node - The parameter `node` is of type `N | null`. This means that it can either be an instance of
126
- * the class `N` or `null`.
127
- * @returns a 2D array of numbers. Each inner array represents a level in the binary tree, and each number in the inner
128
- * array represents the count property of a node in that level.
129
- */
130
- listLevelsCount(node: N | null): number[][];
131
- /**
132
- * The `morrisCount` function returns an array of counts for each node in a binary tree, based on a specified traversal
133
- * pattern.
134
- * @param {'in' | 'pre' | 'post'} [pattern] - The `pattern` parameter is an optional parameter that specifies the
135
- * traversal pattern for the Morris traversal algorithm. It can have one of three values: 'in', 'pre', or 'post'.
136
- * @returns The function `morrisCount` returns an array of numbers.
137
- */
138
- morrisCount(pattern?: DFSOrderPattern): number[];
139
105
  /**
140
106
  * The clear() function clears the data and sets the count to 0.
141
107
  */
@@ -1,6 +1,5 @@
1
- import { CP, FamilyPosition, LoopType } from '../../types';
1
+ import { CP, FamilyPosition, IterationType } from '../../types';
2
2
  import { AVLTree, AVLTreeNode } from './avl-tree';
3
- import { Queue } from '../queue';
4
3
  export class TreeMultisetNode extends AVLTreeNode {
5
4
  /**
6
5
  * The constructor function initializes a BinaryTreeNode object with a key, value, and count.
@@ -221,12 +220,12 @@ export class TreeMultiset extends AVLTree {
221
220
  * constructs a balanced binary search tree using either a recursive or iterative approach.
222
221
  * @returns The function `perfectlyBalance()` returns a boolean value.
223
222
  */
224
- perfectlyBalance() {
225
- const sorted = this.dfs('in', 'node'), n = sorted.length;
223
+ perfectlyBalance(iterationType = this.iterationType) {
224
+ const sorted = this.dfs(node => node, 'in'), n = sorted.length;
226
225
  if (sorted.length < 1)
227
226
  return false;
228
227
  this.clear();
229
- if (this.loopType === LoopType.RECURSIVE) {
228
+ if (iterationType === IterationType.RECURSIVE) {
230
229
  const buildBalanceBST = (l, r) => {
231
230
  if (l > r)
232
231
  return;
@@ -322,83 +321,6 @@ export class TreeMultiset extends AVLTree {
322
321
  }
323
322
  return bstDeletedResult;
324
323
  }
325
- /**
326
- * The function `getNodesByCount` returns an array of nodes that have a specific count property, either recursively or
327
- * using a queue.
328
- * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
329
- * `N`. It represents the property of the nodes that you want to search for.
330
- * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
331
- * return only one node that matches the `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne` is set
332
- * to `true`, the function will return only one node. If `onlyOne`
333
- * @returns an array of nodes that match the given nodeProperty.
334
- */
335
- getNodesByCount(nodeProperty, onlyOne = false) {
336
- if (!this.root)
337
- return [];
338
- const result = [];
339
- if (this.loopType === LoopType.RECURSIVE) {
340
- const _traverse = (cur) => {
341
- if (cur.count === nodeProperty) {
342
- result.push(cur);
343
- if (onlyOne)
344
- return;
345
- }
346
- if (!cur.left && !cur.right)
347
- return;
348
- cur.left && _traverse(cur.left);
349
- cur.right && _traverse(cur.right);
350
- };
351
- _traverse(this.root);
352
- }
353
- else {
354
- const queue = new Queue([this.root]);
355
- while (queue.size > 0) {
356
- const cur = queue.shift();
357
- if (cur) {
358
- if (cur.count === nodeProperty) {
359
- result.push(cur);
360
- if (onlyOne)
361
- return result;
362
- }
363
- cur.left && queue.push(cur.left);
364
- cur.right && queue.push(cur.right);
365
- }
366
- }
367
- }
368
- return result;
369
- }
370
- /**
371
- * The BFSCount function returns an array of counts from a breadth-first search of nodes.
372
- * @returns The BFSCount() function returns an array of numbers, specifically the count property of each node in the
373
- * bfs traversal.
374
- */
375
- bfsCount() {
376
- const nodes = super.bfs('node');
377
- return nodes.map(node => node.count);
378
- }
379
- /**
380
- * The function "listLevelsCount" takes a node and returns an array of arrays, where each inner array contains the
381
- * count property of each node at that level.
382
- * @param {N | null} node - The parameter `node` is of type `N | null`. This means that it can either be an instance of
383
- * the class `N` or `null`.
384
- * @returns a 2D array of numbers. Each inner array represents a level in the binary tree, and each number in the inner
385
- * array represents the count property of a node in that level.
386
- */
387
- listLevelsCount(node) {
388
- const levels = super.listLevels(node, 'node');
389
- return levels.map(level => level.map(node => node.count));
390
- }
391
- /**
392
- * The `morrisCount` function returns an array of counts for each node in a binary tree, based on a specified traversal
393
- * pattern.
394
- * @param {'in' | 'pre' | 'post'} [pattern] - The `pattern` parameter is an optional parameter that specifies the
395
- * traversal pattern for the Morris traversal algorithm. It can have one of three values: 'in', 'pre', or 'post'.
396
- * @returns The function `morrisCount` returns an array of numbers.
397
- */
398
- morrisCount(pattern = 'in') {
399
- const nodes = super.morris(pattern, 'node');
400
- return nodes.map(node => node.count);
401
- }
402
324
  /**
403
325
  * The clear() function clears the data and sets the count to 0.
404
326
  */
@@ -80,7 +80,7 @@ export declare class Heap<E> {
80
80
  has(element: E): boolean;
81
81
  /**
82
82
  * Depth-first search (DFS) method, different traversal orders can be selected。
83
- * @param order - Traversal order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
83
+ * @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
84
84
  * @returns An array containing elements traversed in the specified order.
85
85
  */
86
86
  dfs(order: DFSOrderPattern): E[];
@@ -151,7 +151,7 @@ export class Heap {
151
151
  }
152
152
  /**
153
153
  * Depth-first search (DFS) method, different traversal orders can be selected。
154
- * @param order - Traversal order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
154
+ * @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
155
155
  * @returns An array containing elements traversed in the specified order.
156
156
  */
157
157
  dfs(order) {
@@ -5,7 +5,7 @@ import { BinaryTreeNode } from '../../data-structures/binary-tree';
5
5
  * - `iterative`: Indicates the iterative loop type (with loops that use iterations).
6
6
  * - `recursive`: Indicates the recursive loop type (with loops that call themselves).
7
7
  */
8
- export declare enum LoopType {
8
+ export declare enum IterationType {
9
9
  ITERATIVE = "ITERATIVE",
10
10
  RECURSIVE = "RECURSIVE"
11
11
  }
@@ -21,6 +21,8 @@ export declare enum FamilyPosition {
21
21
  export type BinaryTreeNodePropertyName = 'key' | 'val';
22
22
  export type NodeOrPropertyName = 'node' | BinaryTreeNodePropertyName;
23
23
  export type BinaryTreeNodeKey = number;
24
+ export type BFSCallback<N> = (node: N, level?: number) => any;
25
+ export type BFSCallbackReturn<N> = ReturnType<BFSCallback<N>>;
24
26
  export type BinaryTreeNodeProperty<N extends BinaryTreeNode<N['val'], N>> = N['val'] | N | number | BinaryTreeNodeKey;
25
27
  export type BinaryTreeDeletedResult<N> = {
26
28
  deleted: N | null | undefined;
@@ -29,5 +31,5 @@ export type BinaryTreeDeletedResult<N> = {
29
31
  export type BinaryTreeNodeProperties<N extends BinaryTreeNode<N['val'], N>> = BinaryTreeNodeProperty<N>[];
30
32
  export type BinaryTreeNodeNested<T> = BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
31
33
  export type BinaryTreeOptions = {
32
- loopType?: LoopType;
34
+ iterationType?: IterationType;
33
35
  };
@@ -4,11 +4,11 @@
4
4
  * - `iterative`: Indicates the iterative loop type (with loops that use iterations).
5
5
  * - `recursive`: Indicates the recursive loop type (with loops that call themselves).
6
6
  */
7
- export var LoopType;
8
- (function (LoopType) {
9
- LoopType["ITERATIVE"] = "ITERATIVE";
10
- LoopType["RECURSIVE"] = "RECURSIVE";
11
- })(LoopType || (LoopType = {}));
7
+ export var IterationType;
8
+ (function (IterationType) {
9
+ IterationType["ITERATIVE"] = "ITERATIVE";
10
+ IterationType["RECURSIVE"] = "RECURSIVE";
11
+ })(IterationType || (IterationType = {}));
12
12
  export var FamilyPosition;
13
13
  (function (FamilyPosition) {
14
14
  FamilyPosition["ROOT"] = "ROOT";
@@ -12,3 +12,5 @@ export * from './singly-linked-list';
12
12
  export * from './doubly-linked-list';
13
13
  export * from './navigator';
14
14
  export * from './hash';
15
+ export type MapCallback<N> = (node: N) => any;
16
+ export type MapCallbackReturn<N> = ReturnType<MapCallback<N>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.37.1",
3
+ "version": "1.37.3",
4
4
  "description": "Data Structures of Javascript & TypeScript. Binary Tree, BST, Graph, Heap, Priority Queue, Linked List, Queue, Deque, Stack, AVL Tree, Tree Multiset, Trie, Directed Graph, Undirected Graph, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue.",
5
5
  "main": "dist/index.js",
6
6
  "module": "lib/index.js",
@@ -28,7 +28,7 @@
28
28
  "fix:test": "npm run lint:test && npm run format:test",
29
29
  "fix": "npm run fix:src && npm run fix:test",
30
30
  "update:individuals": "npm i avl-tree-typed binary-tree-typed bst-typed heap-typed --save-dev",
31
- "install:individuals": "npm i avl-tree-typed binary-tree-typed bst-typed deque-typed directed-graph-typed doubly-linked-list-typed graph-typed heap-typed linked-list-typed max-heap-typed max-priority-queue-typed min-heap-typed min-priority-queue-typed priority-queue-typed singly-linked-list-typed stack-typed tree-multiset-typed trie-typed undirected-graph-typed queue-typed --save-dev",
31
+ "install:all-individuals": "npm i avl-tree-typed binary-tree-typed bst-typed deque-typed directed-graph-typed doubly-linked-list-typed graph-typed heap-typed linked-list-typed max-heap-typed max-priority-queue-typed min-heap-typed min-priority-queue-typed priority-queue-typed singly-linked-list-typed stack-typed tree-multiset-typed trie-typed undirected-graph-typed queue-typed --save-dev",
32
32
  "test": "jest",
33
33
  "check:deps": "dependency-cruiser src",
34
34
  "changelog": "auto-changelog",
@@ -58,17 +58,17 @@
58
58
  "@typescript-eslint/eslint-plugin": "^6.7.4",
59
59
  "@typescript-eslint/parser": "^6.7.4",
60
60
  "auto-changelog": "^2.4.0",
61
- "avl-tree-typed": "^1.36.9",
61
+ "avl-tree-typed": "^1.37.2",
62
62
  "benchmark": "^2.1.4",
63
- "binary-tree-typed": "^1.36.9",
64
- "bst-typed": "^1.36.9",
63
+ "binary-tree-typed": "^1.37.2",
64
+ "bst-typed": "^1.37.2",
65
65
  "dependency-cruiser": "^14.1.0",
66
66
  "eslint": "^8.50.0",
67
67
  "eslint-config-prettier": "^9.0.0",
68
68
  "eslint-import-resolver-alias": "^1.1.2",
69
69
  "eslint-import-resolver-typescript": "^3.6.1",
70
70
  "eslint-plugin-import": "^2.28.1",
71
- "heap-typed": "^1.36.9",
71
+ "heap-typed": "^1.37.2",
72
72
  "istanbul-badges-readme": "^1.8.5",
73
73
  "jest": "^29.7.0",
74
74
  "prettier": "^3.0.3",