data-structure-typed 1.36.8 → 1.36.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/CHANGELOG.md +2 -1
  2. package/README.md +8 -0
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +5 -5
  4. package/dist/data-structures/binary-tree/avl-tree.js +6 -6
  5. package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
  6. package/dist/data-structures/binary-tree/binary-tree.d.ts +18 -85
  7. package/dist/data-structures/binary-tree/binary-tree.js +76 -128
  8. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
  9. package/dist/data-structures/binary-tree/tree-multiset.d.ts +9 -16
  10. package/dist/data-structures/binary-tree/tree-multiset.js +10 -20
  11. package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
  12. package/dist/data-structures/hash/hash-map.d.ts +1 -1
  13. package/dist/data-structures/hash/hash-map.js +1 -1
  14. package/dist/data-structures/hash/hash-table.d.ts +3 -3
  15. package/dist/data-structures/hash/hash-table.js +3 -3
  16. package/dist/data-structures/heap/heap.js.map +1 -1
  17. package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -3
  18. package/dist/data-structures/linked-list/skip-linked-list.js +3 -3
  19. package/dist/data-structures/queue/deque.d.ts +2 -2
  20. package/dist/data-structures/queue/deque.js +2 -2
  21. package/dist/data-structures/queue/queue.js +1 -1
  22. package/dist/data-structures/trie/trie.d.ts +2 -2
  23. package/dist/data-structures/trie/trie.js +2 -2
  24. package/dist/interfaces/binary-tree.d.ts +1 -1
  25. package/lib/data-structures/binary-tree/avl-tree.d.ts +5 -5
  26. package/lib/data-structures/binary-tree/avl-tree.js +6 -6
  27. package/lib/data-structures/binary-tree/binary-tree.d.ts +18 -85
  28. package/lib/data-structures/binary-tree/binary-tree.js +76 -128
  29. package/lib/data-structures/binary-tree/tree-multiset.d.ts +9 -16
  30. package/lib/data-structures/binary-tree/tree-multiset.js +10 -20
  31. package/lib/data-structures/hash/hash-map.d.ts +1 -1
  32. package/lib/data-structures/hash/hash-map.js +1 -1
  33. package/lib/data-structures/hash/hash-table.d.ts +3 -3
  34. package/lib/data-structures/hash/hash-table.js +3 -3
  35. package/lib/data-structures/linked-list/skip-linked-list.d.ts +3 -3
  36. package/lib/data-structures/linked-list/skip-linked-list.js +3 -3
  37. package/lib/data-structures/queue/deque.d.ts +2 -2
  38. package/lib/data-structures/queue/deque.js +2 -2
  39. package/lib/data-structures/queue/queue.js +1 -1
  40. package/lib/data-structures/trie/trie.d.ts +2 -2
  41. package/lib/data-structures/trie/trie.js +2 -2
  42. package/lib/interfaces/binary-tree.d.ts +1 -1
  43. package/package.json +6 -6
  44. package/src/data-structures/binary-tree/avl-tree.ts +6 -6
  45. package/src/data-structures/binary-tree/binary-tree.ts +79 -214
  46. package/src/data-structures/binary-tree/rb-tree.ts +3 -3
  47. package/src/data-structures/binary-tree/tree-multiset.ts +10 -21
  48. package/src/data-structures/hash/hash-map.ts +1 -1
  49. package/src/data-structures/hash/hash-table.ts +3 -3
  50. package/src/data-structures/heap/heap.ts +5 -2
  51. package/src/data-structures/linked-list/skip-linked-list.ts +3 -3
  52. package/src/data-structures/queue/deque.ts +2 -2
  53. package/src/data-structures/queue/queue.ts +1 -1
  54. package/src/data-structures/trie/trie.ts +2 -2
  55. package/src/interfaces/binary-tree.ts +1 -1
  56. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +14 -14
  57. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +2 -2
  58. package/test/unit/data-structures/binary-tree/bst.test.ts +28 -28
  59. package/test/unit/data-structures/binary-tree/overall.test.ts +3 -3
  60. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +1 -1
  61. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +28 -28
  62. package/test/unit/data-structures/graph/directed-graph.test.ts +1 -1
  63. package/test/unit/data-structures/graph/undirected-graph.test.ts +1 -1
  64. package/test/unit/data-structures/hash/hash-map.test.ts +2 -2
  65. package/test/unit/data-structures/hash/hash-table.test.ts +5 -5
  66. package/test/unit/data-structures/heap/heap.test.ts +15 -12
  67. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +7 -7
  68. package/test/unit/data-structures/linked-list/skip-list.test.ts +2 -2
  69. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +1 -1
  70. package/test/unit/data-structures/queue/deque.test.ts +3 -3
  71. package/test/unit/data-structures/trie/trie.test.ts +5 -5
  72. package/test/utils/big-o.ts +61 -55
  73. package/umd/bundle.min.js +1 -1
  74. package/umd/bundle.min.js.map +1 -1
@@ -21,13 +21,13 @@ export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> e
21
21
  */
22
22
  constructor(options?: AVLTreeOptions);
23
23
  /**
24
- * The `swapLocation` function swaps the location of two nodes in a binary tree.
25
- * @param {N} srcNode - The source node that you want to swap with the destination node.
24
+ * The `_swap` function swaps the location of two nodes in a binary tree.
25
+ * @param {N} srcNode - The source node that you want to _swap with the destination node.
26
26
  * @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
27
27
  * be swapped to.
28
28
  * @returns The `destNode` is being returned.
29
29
  */
30
- swapLocation(srcNode: N, destNode: N): N;
30
+ protected _swap(srcNode: N, destNode: N): N;
31
31
  /**
32
32
  * The function creates a new AVL tree node with the given key and value.
33
33
  * @param {BinaryTreeNodeKey} key - The `key` parameter is the identifier for the binary tree node. It is used to uniquely
@@ -46,13 +46,13 @@ export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> e
46
46
  */
47
47
  add(key: BinaryTreeNodeKey, val?: N['val']): N | null | undefined;
48
48
  /**
49
- * The function overrides the remove method of a binary tree and performs additional operations to balance the tree after
49
+ * The function overrides the delete method of a binary tree and performs additional operations to balance the tree after
50
50
  * deletion.
51
51
  * @param {BinaryTreeNodeKey} key - The `key` parameter represents the identifier of the binary tree node that needs to be
52
52
  * removed.
53
53
  * @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
54
54
  */
55
- remove(key: BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[];
55
+ delete(key: BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[];
56
56
  /**
57
57
  * The balance factor of a given AVL tree node is calculated by subtracting the height of its left subtree from the
58
58
  * height of its right subtree.
@@ -23,13 +23,13 @@ export class AVLTree extends BST {
23
23
  super(options);
24
24
  }
25
25
  /**
26
- * The `swapLocation` function swaps the location of two nodes in a binary tree.
27
- * @param {N} srcNode - The source node that you want to swap with the destination node.
26
+ * The `_swap` function swaps the location of two nodes in a binary tree.
27
+ * @param {N} srcNode - The source node that you want to _swap with the destination node.
28
28
  * @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
29
29
  * be swapped to.
30
30
  * @returns The `destNode` is being returned.
31
31
  */
32
- swapLocation(srcNode, destNode) {
32
+ _swap(srcNode, destNode) {
33
33
  const { key, val, height } = destNode;
34
34
  const tempNode = this.createNode(key, val);
35
35
  if (tempNode) {
@@ -69,14 +69,14 @@ export class AVLTree extends BST {
69
69
  return inserted;
70
70
  }
71
71
  /**
72
- * The function overrides the remove method of a binary tree and performs additional operations to balance the tree after
72
+ * The function overrides the delete method of a binary tree and performs additional operations to balance the tree after
73
73
  * deletion.
74
74
  * @param {BinaryTreeNodeKey} key - The `key` parameter represents the identifier of the binary tree node that needs to be
75
75
  * removed.
76
76
  * @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
77
77
  */
78
- remove(key) {
79
- const deletedResults = super.remove(key);
78
+ delete(key) {
79
+ const deletedResults = super.delete(key);
80
80
  for (const { needBalanced } of deletedResults) {
81
81
  if (needBalanced) {
82
82
  this._balancePath(needBalanced);
@@ -55,17 +55,18 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
55
55
  get size(): number;
56
56
  private _loopType;
57
57
  get loopType(): LoopType;
58
+ set loopType(v: LoopType);
58
59
  visitedKey: BinaryTreeNodeKey[];
59
60
  visitedVal: N['val'][];
60
61
  visitedNode: N[];
61
62
  /**
62
- * The `swapLocation` function swaps the location of two nodes in a binary tree.
63
- * @param {N} srcNode - The source node that you want to swap with the destination node.
63
+ * The `_swap` function swaps the location of two nodes in a binary tree.
64
+ * @param {N} srcNode - The source node that you want to _swap with the destination node.
64
65
  * @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
65
66
  * be swapped to.
66
67
  * @returns The `destNode` is being returned.
67
68
  */
68
- swapLocation(srcNode: N, destNode: N): N;
69
+ protected _swap(srcNode: N, destNode: N): N;
69
70
  /**
70
71
  * The clear() function resets the root, size, and maxKey properties to their initial values.
71
72
  */
@@ -111,13 +112,13 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
111
112
  */
112
113
  refill(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N[] | Array<N['val']>): boolean;
113
114
  /**
114
- * The `remove` function in TypeScript is used to delete a node from a binary search tree and returns an array of objects
115
+ * The `delete` function in TypeScript is used to delete a node from a binary search tree and returns an array of objects
115
116
  * containing the deleted node and the node that needs to be balanced.
116
117
  * @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object (`N`) or a binary tree
117
118
  * node ID (`BinaryTreeNodeKey`).
118
- * @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
119
+ * @returns The function `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
119
120
  */
120
- remove(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[];
121
+ delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[];
121
122
  /**
122
123
  * The function calculates the depth of a node in a binary tree.
123
124
  * @param {N | BinaryTreeNodeKey | null} distNode - The `distNode` parameter can be any node of the tree
@@ -236,10 +237,10 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
236
237
  getRightMost(beginRoot: N): N;
237
238
  /**
238
239
  * The function checks if a binary search tree is valid by traversing it either recursively or iteratively.
239
- * @param {N | null} node - The `node` parameter represents the root node of a binary search tree (BST).
240
+ * @param {N | null} subTreeRoot - The `node` parameter represents the root node of a binary search tree (BST).
240
241
  * @returns a boolean value.
241
242
  */
242
- isSubtreeBST(node: N | null): boolean;
243
+ isSubtreeBST(subTreeRoot: N | null): boolean;
243
244
  /**
244
245
  * The function isBST checks if the binary tree is valid binary search tree.
245
246
  * @returns The `isBST()` function is returning a boolean value.
@@ -263,16 +264,14 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
263
264
  */
264
265
  subTreeSum(subTreeRoot: N | BinaryTreeNodeKey | null, propertyName?: BinaryTreeNodePropertyName): number;
265
266
  /**
266
- * The function `subTreeAdd` adds a delta value to a specified property of each node in a subtree.
267
+ * The function `subTreeForeach` adds a delta value to a specified property of each node in a subtree.
267
268
  * @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
268
269
  * tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
269
- * @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
270
- * each node in the subtree should be incremented.
271
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
270
+ * @param callBack - The `callBack` parameter is a function that takes a node as a parameter and returns a number.
272
271
  * specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'key'.
273
272
  * @returns a boolean value.
274
273
  */
275
- subTreeAdd(subTreeRoot: N | BinaryTreeNodeKey | null, delta: number, propertyName?: BinaryTreeNodePropertyName): boolean;
274
+ subTreeForeach(subTreeRoot: N | BinaryTreeNodeKey | null, callback: (node: N) => any): boolean;
276
275
  /**
277
276
  * Performs a breadth-first search (bfs) on a binary tree, accumulating properties of each node based on their 'key' property.
278
277
  * @returns An array of binary tree node IDs.
@@ -311,87 +310,26 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
311
310
  * Performs a depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on the specified property name.
312
311
  * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
313
312
  * @param {string} nodeOrPropertyName - The name of the property to accumulate.
313
+ * @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
314
314
  * @returns An array of values corresponding to the specified property.
315
315
  */
316
- dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
316
+ dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'key', loopType?: LoopType): BinaryTreeNodeKey[];
317
317
  /**
318
318
  * Performs a depth-first search (dfs) traversal on a binary tree and accumulates the 'val' property of each node.
319
319
  * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
320
320
  * @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
321
+ * @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
321
322
  * @returns An array of 'val' properties from each node.
322
323
  */
323
- dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'val'): N[];
324
+ dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'val', loopType?: LoopType): N[];
324
325
  /**
325
326
  * Performs a depth-first search (dfs) traversal on a binary tree and accumulates nodes themselves.
326
327
  * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
327
328
  * @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
329
+ * @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
328
330
  * @returns An array of binary tree nodes.
329
331
  */
330
- dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'node'): N[];
331
- /**
332
- * Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on their 'key' property.
333
- * @returns An array of binary tree node IDs.
334
- */
335
- dfsIterative(): BinaryTreeNodeKey[];
336
- /**
337
- * Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on their 'key' property.
338
- * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
339
- * @returns An array of values corresponding to the specified property.
340
- */
341
- dfsIterative(pattern: DFSOrderPattern): BinaryTreeNodeKey[];
342
- /**
343
- * Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on the specified property name.
344
- * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
345
- * @param {string} nodeOrPropertyName - The name of the property to accumulate.
346
- * @returns An array of values corresponding to the specified property.
347
- */
348
- dfsIterative(pattern: DFSOrderPattern, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
349
- /**
350
- * Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates the 'val' property of each node.
351
- * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
352
- * @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
353
- * @returns An array of 'val' properties from each node.
354
- */
355
- dfsIterative(pattern: DFSOrderPattern, nodeOrPropertyName: 'val'): N['val'][];
356
- /**
357
- * Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates nodes themselves.
358
- * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
359
- * @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
360
- * @returns An array of binary tree nodes.
361
- */
362
- dfsIterative(pattern: DFSOrderPattern, nodeOrPropertyName: 'node'): N[];
363
- /**
364
- * Performs a level-order traversal on a binary tree starting from the specified node and accumulates properties of each node based on their 'key' property.
365
- * @returns An array of binary tree node IDs.
366
- */
367
- levelIterative(): BinaryTreeNodeKey[];
368
- /**
369
- * Performs a level-order traversal on a binary tree starting from the specified node and accumulates properties of each node based on their 'key' property.
370
- * @param {N | null} node - The starting node for the level order traversal. If null, the root node of the tree is used as the starting node.
371
- * @returns An array of binary tree node IDs.
372
- */
373
- levelIterative(node: N | null): BinaryTreeNodeKey[];
374
- /**
375
- * Performs a level-order traversal on a binary tree starting from the specified node and accumulates properties of each node based on the specified property name.
376
- * @param {N | null} node - The starting node for the level order traversal. If null, the root node of the tree is used as the starting node.
377
- * @param {string} nodeOrPropertyName - The name of the property to accumulate.
378
- * @returns An array of values corresponding to the specified property.
379
- */
380
- levelIterative(node: N | null, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
381
- /**
382
- * Performs a level-order traversal on a binary tree starting from the specified node and accumulates the 'val' property of each node.
383
- * @param {N | null} node - The starting node for the level order traversal. If null, the root node of the tree is used as the starting node.
384
- * @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
385
- * @returns An array of 'val' properties from each node.
386
- */
387
- levelIterative(node: N | null, nodeOrPropertyName: 'val'): N['val'][];
388
- /**
389
- * Performs a level-order traversal on a binary tree starting from the specified node and accumulates nodes themselves.
390
- * @param {N | null} node - The starting node for the level order traversal. If null, the root node of the tree is used as the starting node.
391
- * @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
392
- * @returns An array of binary tree nodes.
393
- */
394
- levelIterative(node: N | null, nodeOrPropertyName: 'node'): N[];
332
+ dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'node', loopType?: LoopType): N[];
395
333
  /**
396
334
  * Collects nodes from a binary tree by a specified property and organizes them into levels.
397
335
  * @returns A 2D array of AbstractBinaryTreeNodeProperty<N> objects.
@@ -477,11 +415,6 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
477
415
  * undefined.
478
416
  */
479
417
  protected _addTo(newNode: N | null, parent: N): N | null | undefined;
480
- /**
481
- * The function sets the loop type for a protected variable.
482
- * @param {LoopType} value - The value parameter is of type LoopType.
483
- */
484
- protected _setLoopType(value: LoopType): void;
485
418
  /**
486
419
  * The function sets the root property of an object to a given value, and if the value is not null, it also sets the
487
420
  * parent property of the value to undefined.
@@ -114,14 +114,17 @@ export class BinaryTree {
114
114
  get loopType() {
115
115
  return this._loopType;
116
116
  }
117
+ set loopType(v) {
118
+ this._loopType = v;
119
+ }
117
120
  /**
118
- * The `swapLocation` function swaps the location of two nodes in a binary tree.
119
- * @param {N} srcNode - The source node that you want to swap with the destination node.
121
+ * The `_swap` function swaps the location of two nodes in a binary tree.
122
+ * @param {N} srcNode - The source node that you want to _swap with the destination node.
120
123
  * @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
121
124
  * be swapped to.
122
125
  * @returns The `destNode` is being returned.
123
126
  */
124
- swapLocation(srcNode, destNode) {
127
+ _swap(srcNode, destNode) {
125
128
  const { key, val } = destNode;
126
129
  const tempNode = this.createNode(key, val);
127
130
  if (tempNode) {
@@ -258,13 +261,13 @@ export class BinaryTree {
258
261
  return keysOrNodes.length === this.addMany(keysOrNodes, data).length;
259
262
  }
260
263
  /**
261
- * The `remove` function in TypeScript is used to delete a node from a binary search tree and returns an array of objects
264
+ * The `delete` function in TypeScript is used to delete a node from a binary search tree and returns an array of objects
262
265
  * containing the deleted node and the node that needs to be balanced.
263
266
  * @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object (`N`) or a binary tree
264
267
  * node ID (`BinaryTreeNodeKey`).
265
- * @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
268
+ * @returns The function `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
266
269
  */
267
- remove(nodeOrKey) {
270
+ delete(nodeOrKey) {
268
271
  const bstDeletedResult = [];
269
272
  if (!this.root)
270
273
  return bstDeletedResult;
@@ -293,7 +296,7 @@ export class BinaryTree {
293
296
  const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
294
297
  if (leftSubTreeRightMost) {
295
298
  const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
296
- orgCurrent = this.swapLocation(curr, leftSubTreeRightMost);
299
+ orgCurrent = this._swap(curr, leftSubTreeRightMost);
297
300
  if (parentOfLeftSubTreeMax) {
298
301
  if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
299
302
  parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
@@ -589,12 +592,12 @@ export class BinaryTree {
589
592
  }
590
593
  /**
591
594
  * The function checks if a binary search tree is valid by traversing it either recursively or iteratively.
592
- * @param {N | null} node - The `node` parameter represents the root node of a binary search tree (BST).
595
+ * @param {N | null} subTreeRoot - The `node` parameter represents the root node of a binary search tree (BST).
593
596
  * @returns a boolean value.
594
597
  */
595
- isSubtreeBST(node) {
598
+ isSubtreeBST(subTreeRoot) {
596
599
  // TODO there is a bug
597
- if (!node)
600
+ if (!subTreeRoot)
598
601
  return true;
599
602
  if (this._loopType === LoopType.RECURSIVE) {
600
603
  const dfs = (cur, min, max) => {
@@ -604,11 +607,11 @@ export class BinaryTree {
604
607
  return false;
605
608
  return dfs(cur.left, min, cur.key) && dfs(cur.right, cur.key, max);
606
609
  };
607
- return dfs(node, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
610
+ return dfs(subTreeRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
608
611
  }
609
612
  else {
610
613
  const stack = [];
611
- let prev = Number.MIN_SAFE_INTEGER, curr = node;
614
+ let prev = Number.MIN_SAFE_INTEGER, curr = subTreeRoot;
612
615
  while (curr || stack.length > 0) {
613
616
  while (curr) {
614
617
  stack.push(curr);
@@ -711,33 +714,21 @@ export class BinaryTree {
711
714
  return sum;
712
715
  }
713
716
  /**
714
- * The function `subTreeAdd` adds a delta value to a specified property of each node in a subtree.
717
+ * The function `subTreeForeach` adds a delta value to a specified property of each node in a subtree.
715
718
  * @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
716
719
  * tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
717
- * @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
718
- * each node in the subtree should be incremented.
719
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
720
+ * @param callBack - The `callBack` parameter is a function that takes a node as a parameter and returns a number.
720
721
  * specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'key'.
721
722
  * @returns a boolean value.
722
723
  */
723
- subTreeAdd(subTreeRoot, delta, propertyName = 'key') {
724
+ subTreeForeach(subTreeRoot, callback) {
724
725
  if (typeof subTreeRoot === 'number')
725
726
  subTreeRoot = this.get(subTreeRoot, 'key');
726
727
  if (!subTreeRoot)
727
728
  return false;
728
- const _addByProperty = (cur) => {
729
- switch (propertyName) {
730
- case 'key':
731
- cur.key += delta;
732
- break;
733
- default:
734
- cur.key += delta;
735
- break;
736
- }
737
- };
738
729
  if (this._loopType === LoopType.RECURSIVE) {
739
730
  const _traverse = (cur) => {
740
- _addByProperty(cur);
731
+ callback(cur);
741
732
  cur.left && _traverse(cur.left);
742
733
  cur.right && _traverse(cur.right);
743
734
  };
@@ -747,7 +738,7 @@ export class BinaryTree {
747
738
  const stack = [subTreeRoot];
748
739
  while (stack.length > 0) {
749
740
  const cur = stack.pop();
750
- _addByProperty(cur);
741
+ callback(cur);
751
742
  cur.right && stack.push(cur.right);
752
743
  cur.left && stack.push(cur.left);
753
744
  }
@@ -765,6 +756,7 @@ export class BinaryTree {
765
756
  this._clearResults();
766
757
  const queue = [this.root];
767
758
  while (queue.length !== 0) {
759
+ // TODO Array.shift is not efficient, consider using Deque
768
760
  const cur = queue.shift();
769
761
  if (cur) {
770
762
  this._accumulatedByPropertyName(cur, nodeOrPropertyName);
@@ -781,111 +773,74 @@ export class BinaryTree {
781
773
  * each node based on the specified pattern and property name.
782
774
  * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
783
775
  * @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. If no `nodeOrPropertyName` is provided, the default value is `'key'`.
776
+ * @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
784
777
  * @returns an instance of the BinaryTreeNodeProperties class, which contains the accumulated properties of the binary tree nodes based on the specified pattern and node or property name.
785
778
  */
786
- dfs(pattern = 'in', nodeOrPropertyName = 'key') {
779
+ dfs(pattern = 'in', nodeOrPropertyName = 'key', loopType = LoopType.ITERATIVE) {
787
780
  this._clearResults();
788
- const _traverse = (node) => {
789
- switch (pattern) {
790
- case 'in':
791
- if (node.left)
792
- _traverse(node.left);
793
- this._accumulatedByPropertyName(node, nodeOrPropertyName);
794
- if (node.right)
795
- _traverse(node.right);
796
- break;
797
- case 'pre':
798
- this._accumulatedByPropertyName(node, nodeOrPropertyName);
799
- if (node.left)
800
- _traverse(node.left);
801
- if (node.right)
802
- _traverse(node.right);
803
- break;
804
- case 'post':
805
- if (node.left)
806
- _traverse(node.left);
807
- if (node.right)
808
- _traverse(node.right);
809
- this._accumulatedByPropertyName(node, nodeOrPropertyName);
810
- break;
811
- }
812
- };
813
- this.root && _traverse(this.root);
814
- return this._getResultByPropertyName(nodeOrPropertyName);
815
- }
816
- /**
817
- * The dfsIterative function performs an iterative depth-first search traversal on a binary tree, with the option to
818
- * specify the traversal pattern and the property name to accumulate results by.
819
- * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
820
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. By default, it is set to `'key'`.
821
- * @returns An object of type BinaryTreeNodeProperties<N>.
822
- */
823
- dfsIterative(pattern = 'in', nodeOrPropertyName = 'key') {
824
- this._clearResults();
825
- if (!this.root)
826
- return this._getResultByPropertyName(nodeOrPropertyName);
827
- // 0: visit, 1: print
828
- const stack = [{ opt: 0, node: this.root }];
829
- while (stack.length > 0) {
830
- const cur = stack.pop();
831
- if (!cur || !cur.node)
832
- continue;
833
- if (cur.opt === 1) {
834
- this._accumulatedByPropertyName(cur.node, nodeOrPropertyName);
835
- }
836
- else {
781
+ if (loopType === LoopType.RECURSIVE) {
782
+ const _traverse = (node) => {
837
783
  switch (pattern) {
838
784
  case 'in':
839
- stack.push({ opt: 0, node: cur.node.right });
840
- stack.push({ opt: 1, node: cur.node });
841
- stack.push({ opt: 0, node: cur.node.left });
785
+ if (node.left)
786
+ _traverse(node.left);
787
+ this._accumulatedByPropertyName(node, nodeOrPropertyName);
788
+ if (node.right)
789
+ _traverse(node.right);
842
790
  break;
843
791
  case 'pre':
844
- stack.push({ opt: 0, node: cur.node.right });
845
- stack.push({ opt: 0, node: cur.node.left });
846
- stack.push({ opt: 1, node: cur.node });
792
+ this._accumulatedByPropertyName(node, nodeOrPropertyName);
793
+ if (node.left)
794
+ _traverse(node.left);
795
+ if (node.right)
796
+ _traverse(node.right);
847
797
  break;
848
798
  case 'post':
849
- stack.push({ opt: 1, node: cur.node });
850
- stack.push({ opt: 0, node: cur.node.right });
851
- stack.push({ opt: 0, node: cur.node.left });
852
- break;
853
- default:
854
- stack.push({ opt: 0, node: cur.node.right });
855
- stack.push({ opt: 1, node: cur.node });
856
- stack.push({ opt: 0, node: cur.node.left });
799
+ if (node.left)
800
+ _traverse(node.left);
801
+ if (node.right)
802
+ _traverse(node.right);
803
+ this._accumulatedByPropertyName(node, nodeOrPropertyName);
857
804
  break;
858
805
  }
859
- }
806
+ };
807
+ this.root && _traverse(this.root);
860
808
  }
861
- return this._getResultByPropertyName(nodeOrPropertyName);
862
- }
863
- /**
864
- * The `levelIterative` function performs a level-order traversal on a binary tree and returns the values of the nodes
865
- * in an array, based on a specified property name.
866
- * @param {N | null} node - The `node` parameter is a BinaryTreeNode object representing the starting
867
- * node for the level order traversal. It can be null if no specific node is provided, in which case the root node of
868
- * the tree is used as the starting node.
869
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
870
- * can be either a `BinaryTreeNode` property name or the string `'key'`. If a property name is provided, the function
871
- * will accumulate results based on that property. If no property name is provided, the function will default to
872
- * accumulating results based on the 'key' property.
873
- * @returns An object of type `BinaryTreeNodeProperties<N>`.
874
- */
875
- levelIterative(node = this.root, nodeOrPropertyName = 'key') {
876
- if (!node)
877
- return [];
878
- this._clearResults();
879
- const queue = [node];
880
- while (queue.length > 0) {
881
- const cur = queue.shift();
882
- if (cur) {
883
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
884
- if (cur.left) {
885
- queue.push(cur.left);
809
+ else {
810
+ if (!this.root)
811
+ return this._getResultByPropertyName(nodeOrPropertyName);
812
+ // 0: visit, 1: print
813
+ const stack = [{ opt: 0, node: this.root }];
814
+ while (stack.length > 0) {
815
+ const cur = stack.pop();
816
+ if (!cur || !cur.node)
817
+ continue;
818
+ if (cur.opt === 1) {
819
+ this._accumulatedByPropertyName(cur.node, nodeOrPropertyName);
886
820
  }
887
- if (cur.right) {
888
- queue.push(cur.right);
821
+ else {
822
+ switch (pattern) {
823
+ case 'in':
824
+ stack.push({ opt: 0, node: cur.node.right });
825
+ stack.push({ opt: 1, node: cur.node });
826
+ stack.push({ opt: 0, node: cur.node.left });
827
+ break;
828
+ case 'pre':
829
+ stack.push({ opt: 0, node: cur.node.right });
830
+ stack.push({ opt: 0, node: cur.node.left });
831
+ stack.push({ opt: 1, node: cur.node });
832
+ break;
833
+ case 'post':
834
+ stack.push({ opt: 1, node: cur.node });
835
+ stack.push({ opt: 0, node: cur.node.right });
836
+ stack.push({ opt: 0, node: cur.node.left });
837
+ break;
838
+ default:
839
+ stack.push({ opt: 0, node: cur.node.right });
840
+ stack.push({ opt: 1, node: cur.node });
841
+ stack.push({ opt: 0, node: cur.node.left });
842
+ break;
843
+ }
889
844
  }
890
845
  }
891
846
  }
@@ -1090,13 +1045,6 @@ export class BinaryTree {
1090
1045
  return;
1091
1046
  }
1092
1047
  }
1093
- /**
1094
- * The function sets the loop type for a protected variable.
1095
- * @param {LoopType} value - The value parameter is of type LoopType.
1096
- */
1097
- _setLoopType(value) {
1098
- this._loopType = value;
1099
- }
1100
1048
  /**
1101
1049
  * The function sets the root property of an object to a given value, and if the value is not null, it also sets the
1102
1050
  * parent property of the value to undefined.
@@ -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, DFSOrderPattern, LoopType } 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> {
@@ -48,12 +48,12 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
48
48
  createNode(key: BinaryTreeNodeKey, val?: N['val'], count?: number): N;
49
49
  /**
50
50
  * The function swaps the location of two nodes in a tree data structure.
51
- * @param {N} srcNode - The source node that we want to swap with the destination node.
51
+ * @param {N} srcNode - The source node that we want to _swap with the destination node.
52
52
  * @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
53
53
  * be swapped with.
54
54
  * @returns the `destNode` after swapping its values with the `srcNode`.
55
55
  */
56
- swapLocation(srcNode: N, destNode: N): N;
56
+ protected _swap(srcNode: N, destNode: N): N;
57
57
  /**
58
58
  * The `add` function adds a new node to a binary search tree, maintaining the tree's properties and balancing if
59
59
  * necessary.
@@ -93,15 +93,15 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
93
93
  */
94
94
  perfectlyBalance(): boolean;
95
95
  /**
96
- * The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent
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.
98
98
  * @param {N | BinaryTreeNodeKey | null} nodeOrKey - The `nodeOrKey` parameter can be one of the following:
99
99
  * @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
100
100
  * whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
101
101
  * not be taken into account when removing it. If `ignoreCount` is set to `false
102
- * @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
102
+ * @returns The function `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
103
103
  */
104
- remove(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
104
+ delete(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
105
105
  /**
106
106
  * The function `getSubTreeCount` calculates the number of nodes and the sum of their counts in a subtree, using either
107
107
  * recursive or iterative traversal.
@@ -146,7 +146,7 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
146
146
  * @returns The BFSCount() function returns an array of numbers, specifically the count property of each node in the
147
147
  * bfs traversal.
148
148
  */
149
- BFSCount(): number[];
149
+ bfsCount(): number[];
150
150
  /**
151
151
  * The function "listLevelsCount" takes a node and returns an array of arrays, where each inner array contains the
152
152
  * count property of each node at that level.
@@ -169,18 +169,11 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
169
169
  * the specified traversal pattern.
170
170
  * @param {'in' | 'pre' | 'post'} [pattern] - The pattern parameter is a string that specifies the traversal order for
171
171
  * the Depth-First Search (dfs) algorithm. It can have three possible values: 'in', 'pre', or 'post'.
172
+ * @param loopType - The loopType parameter is a string that specifies the type of loop to use when traversing the
172
173
  * @returns The dfsCountIterative function returns an array of numbers, which represents the count property of each node
173
174
  * in the dfs traversal.
174
175
  */
175
- dfsCountIterative(pattern?: DFSOrderPattern): number[];
176
- /**
177
- * The dfsCount function returns an array of counts for each node in a depth-first search traversal.
178
- * @param {DFSOrderPattern} [pattern] - The pattern parameter is an optional parameter that specifies the order in which
179
- * the Depth-First Search (dfs) algorithm should traverse the nodes. It can have one of the following values:
180
- * @returns The dfsCount function returns an array of numbers, specifically the count property of each node in the dfs
181
- * traversal.
182
- */
183
- dfsCount(pattern?: DFSOrderPattern): number[];
176
+ dfsCount(pattern?: DFSOrderPattern, loopType?: LoopType): number[];
184
177
  /**
185
178
  * The `lesserSumCount` function calculates the sum of the counts of all nodes in a binary tree that have a lesser
186
179
  * value than a given node.