data-structure-typed 1.39.3 → 1.39.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +1 -1
  3. package/dist/cjs/data-structures/binary-tree/avl-tree.js +4 -2
  4. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  5. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +5 -13
  6. package/dist/cjs/data-structures/binary-tree/binary-tree.js +17 -25
  7. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/bst.d.ts +1 -1
  9. package/dist/cjs/data-structures/binary-tree/bst.js +6 -6
  10. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/tree-multiset.d.ts +1 -1
  12. package/dist/cjs/data-structures/binary-tree/tree-multiset.js +2 -2
  13. package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
  14. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +1 -1
  15. package/dist/mjs/data-structures/binary-tree/avl-tree.js +4 -2
  16. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +5 -13
  17. package/dist/mjs/data-structures/binary-tree/binary-tree.js +17 -25
  18. package/dist/mjs/data-structures/binary-tree/bst.d.ts +1 -1
  19. package/dist/mjs/data-structures/binary-tree/bst.js +6 -6
  20. package/dist/mjs/data-structures/binary-tree/tree-multiset.d.ts +1 -1
  21. package/dist/mjs/data-structures/binary-tree/tree-multiset.js +2 -2
  22. package/dist/umd/data-structure-typed.min.js +1 -1
  23. package/dist/umd/data-structure-typed.min.js.map +1 -1
  24. package/package.json +5 -5
  25. package/src/data-structures/binary-tree/avl-tree.ts +3 -2
  26. package/src/data-structures/binary-tree/binary-tree.ts +19 -28
  27. package/src/data-structures/binary-tree/bst.ts +6 -6
  28. package/src/data-structures/binary-tree/tree-multiset.ts +2 -2
  29. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +0 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.39.3",
3
+ "version": "1.39.4",
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/cjs/index.js",
6
6
  "module": "dist/mjs/index.js",
@@ -61,17 +61,17 @@
61
61
  "@typescript-eslint/eslint-plugin": "^6.7.4",
62
62
  "@typescript-eslint/parser": "^6.7.4",
63
63
  "auto-changelog": "^2.4.0",
64
- "avl-tree-typed": "^1.39.2",
64
+ "avl-tree-typed": "^1.39.3",
65
65
  "benchmark": "^2.1.4",
66
- "binary-tree-typed": "^1.39.2",
67
- "bst-typed": "^1.39.2",
66
+ "binary-tree-typed": "^1.39.3",
67
+ "bst-typed": "^1.39.3",
68
68
  "dependency-cruiser": "^14.1.0",
69
69
  "eslint": "^8.50.0",
70
70
  "eslint-config-prettier": "^9.0.0",
71
71
  "eslint-import-resolver-alias": "^1.1.2",
72
72
  "eslint-import-resolver-typescript": "^3.6.1",
73
73
  "eslint-plugin-import": "^2.28.1",
74
- "heap-typed": "^1.39.2",
74
+ "heap-typed": "^1.39.3",
75
75
  "istanbul-badges-readme": "^1.8.5",
76
76
  "jest": "^29.7.0",
77
77
  "prettier": "^3.0.3",
@@ -71,13 +71,14 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
71
71
  * @param callback - The `callback` parameter is a function that takes a node as input and returns a
72
72
  * value. This value is compared with the `identifier` parameter to determine if the node should be
73
73
  * included in the result. The `callback` parameter has a default value of
74
- * `this._defaultCallbackByKey`
74
+ * `((node: N) => node.key)`
75
75
  * @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
76
76
  */
77
77
  override delete<C extends BTNCallback<N>>(
78
78
  identifier: ReturnType<C>,
79
- callback: C = this._defaultCallbackByKey as C
79
+ callback: C = ((node: N) => node.key) as C
80
80
  ): BinaryTreeDeletedResult<N>[] {
81
+ if ((identifier as any) instanceof AVLTreeNode) callback = (node => node) as C;
81
82
  const deletedResults = super.delete(identifier, callback);
82
83
  for (const {needBalanced} of deletedResults) {
83
84
  if (needBalanced) {
@@ -217,7 +217,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
217
217
  }
218
218
 
219
219
  const key = typeof keyOrNode === 'number' ? keyOrNode : keyOrNode ? keyOrNode.key : undefined;
220
- const existNode = key !== undefined ? this.get(key, this._defaultCallbackByKey) : undefined;
220
+ const existNode = key !== undefined ? this.get(key, (node: N) => node.key) : undefined;
221
221
 
222
222
  if (this.root) {
223
223
  if (existNode) {
@@ -296,11 +296,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
296
296
  * @param callback - The `callback` parameter is a function that takes a node as input and returns a
297
297
  * value. This value is compared with the `identifier` parameter to determine if the node should be
298
298
  * included in the result. The `callback` parameter has a default value of
299
- * `this._defaultCallbackByKey`, which
299
+ * `((node: N) => node.key)`, which
300
300
  */
301
301
  delete<C extends BTNCallback<N>>(
302
302
  identifier: ReturnType<C> | null,
303
- callback: C = this._defaultCallbackByKey as C
303
+ callback: C = ((node: N) => node.key) as C
304
304
  ): BinaryTreeDeletedResult<N>[] {
305
305
  const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
306
306
  if (!this.root) return bstDeletedResult;
@@ -487,7 +487,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
487
487
 
488
488
  getNodes<C extends BTNCallback<N, BTNKey>>(
489
489
  identifier: BTNKey,
490
- callback: C,
490
+ callback?: C,
491
491
  onlyOne?: boolean,
492
492
  beginRoot?: N | null,
493
493
  iterationType?: IterationType
@@ -495,7 +495,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
495
495
 
496
496
  getNodes<C extends BTNCallback<N, N>>(
497
497
  identifier: N | null,
498
- callback: C,
498
+ callback?: C,
499
499
  onlyOne?: boolean,
500
500
  beginRoot?: N | null,
501
501
  iterationType?: IterationType
@@ -518,7 +518,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
518
518
  * @param callback - The `callback` parameter is a function that takes a node as input and returns a
519
519
  * value. This value is compared with the `identifier` parameter to determine if the node should be
520
520
  * included in the result. The `callback` parameter has a default value of
521
- * `this._defaultCallbackByKey`, which
521
+ * `((node: N) => node.key)`, which
522
522
  * @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the
523
523
  * first node that matches the identifier. If set to true, the function will return an array with
524
524
  * only one element (or an empty array if no matching node is found). If set to false (default), the
@@ -532,7 +532,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
532
532
  */
533
533
  getNodes<C extends BTNCallback<N>>(
534
534
  identifier: ReturnType<C> | null,
535
- callback: C = this._defaultCallbackByKey as C,
535
+ callback: C = ((node: N) => node.key) as C,
536
536
  onlyOne = false,
537
537
  beginRoot: N | null = this.root,
538
538
  iterationType = this.iterationType
@@ -600,7 +600,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
600
600
  * @param callback - The `callback` parameter is a function that is used to determine whether a node
601
601
  * matches the desired criteria. It takes a node as input and returns a boolean value indicating
602
602
  * whether the node matches the criteria or not. The default callback function
603
- * `this._defaultCallbackByKey` is used if no callback function is
603
+ * `((node: N) => node.key)` is used if no callback function is
604
604
  * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
605
605
  * the node from which the search should begin. By default, it is set to `this.root`, which means the
606
606
  * search will start from the root node of the binary tree. However, you can provide a different node
@@ -611,7 +611,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
611
611
  */
612
612
  has<C extends BTNCallback<N>>(
613
613
  identifier: ReturnType<C> | null,
614
- callback: C = this._defaultCallbackByKey as C,
614
+ callback: C = ((node: N) => node.key) as C,
615
615
  beginRoot = this.root,
616
616
  iterationType = this.iterationType
617
617
  ): boolean {
@@ -649,7 +649,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
649
649
  * @param callback - The `callback` parameter is a function that is used to determine whether a node
650
650
  * matches the desired criteria. It takes a node as input and returns a boolean value indicating
651
651
  * whether the node matches the criteria or not. The default callback function
652
- * (`this._defaultCallbackByKey`) is used if no callback function is
652
+ * (`((node: N) => node.key)`) is used if no callback function is
653
653
  * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
654
654
  * the root node from which the search should begin.
655
655
  * @param iterationType - The `iterationType` parameter specifies the type of iteration to be
@@ -658,7 +658,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
658
658
  */
659
659
  get<C extends BTNCallback<N>>(
660
660
  identifier: ReturnType<C> | null,
661
- callback: C = this._defaultCallbackByKey as C,
661
+ callback: C = ((node: N) => node.key) as C,
662
662
  beginRoot = this.root,
663
663
  iterationType = this.iterationType
664
664
  ): N | null {
@@ -824,7 +824,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
824
824
  * @returns The function `subTreeTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
825
825
  */
826
826
  subTreeTraverse<C extends BTNCallback<N>>(
827
- callback: C = this._defaultCallbackByKey as C,
827
+ callback: C = ((node: N) => node.key) as C,
828
828
  beginRoot: BTNKey | N | null = this.root,
829
829
  iterationType = this.iterationType
830
830
  ): ReturnType<C>[] {
@@ -860,7 +860,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
860
860
  * function on each node according to a specified order pattern.
861
861
  * @param callback - The `callback` parameter is a function that will be called on each node during
862
862
  * the depth-first search traversal. It takes a node as input and returns a value. The default value
863
- * is `this._defaultCallbackByKey`, which is a callback function defined elsewhere in the code.
863
+ * is `((node: N) => node.key)`, which is a callback function defined elsewhere in the code.
864
864
  * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
865
865
  * nodes are visited during the depth-first search. There are three possible values for `pattern`:
866
866
  * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
@@ -871,7 +871,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
871
871
  * @returns The function `dfs` returns an array of `ReturnType<BTNCallback<N>>` values.
872
872
  */
873
873
  dfs<C extends BTNCallback<N>>(
874
- callback: C = this._defaultCallbackByKey as C,
874
+ callback: C = ((node: N) => node.key) as C,
875
875
  pattern: DFSOrderPattern = 'in',
876
876
  beginRoot: N | null = this.root,
877
877
  iterationType: IterationType = IterationType.ITERATIVE
@@ -946,7 +946,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
946
946
  * function on each node.
947
947
  * @param callback - The `callback` parameter is a function that will be called for each node in the
948
948
  * breadth-first search. It takes a node of type `N` as its argument and returns a value of type
949
- * `ReturnType<BTNCallback<N>>`. The default value for this parameter is `this._defaultCallbackByKey
949
+ * `ReturnType<BTNCallback<N>>`. The default value for this parameter is `((node: N) => node.key)
950
950
  * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
951
951
  * search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
952
952
  * will not be performed and an empty array will be returned.
@@ -955,7 +955,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
955
955
  * @returns The function `bfs` returns an array of `ReturnType<BTNCallback<N>>[]`.
956
956
  */
957
957
  bfs<C extends BTNCallback<N>>(
958
- callback: C = this._defaultCallbackByKey as C,
958
+ callback: C = ((node: N) => node.key) as C,
959
959
  beginRoot: N | null = this.root,
960
960
  iterationType = this.iterationType
961
961
  ): ReturnType<C>[] {
@@ -1012,7 +1012,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1012
1012
  * function `C` applied to the nodes at that level.
1013
1013
  */
1014
1014
  listLevels<C extends BTNCallback<N>>(
1015
- callback: C = this._defaultCallbackByKey as C,
1015
+ callback: C = ((node: N) => node.key) as C,
1016
1016
  beginRoot: N | null = this.root,
1017
1017
  iterationType = this.iterationType
1018
1018
  ): ReturnType<C>[][] {
@@ -1071,7 +1071,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1071
1071
  * algorithm and returns an array of values obtained by applying a callback function to each node.
1072
1072
  * @param callback - The `callback` parameter is a function that will be called on each node in the
1073
1073
  * tree. It takes a node of type `N` as input and returns a value of type `ReturnType<BTNCallback<N>>`. The
1074
- * default value for this parameter is `this._defaultCallbackByKey`.
1074
+ * default value for this parameter is `((node: N) => node.key)`.
1075
1075
  * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
1076
1076
  * determines the order in which the nodes of a binary tree are traversed. It can have one of the
1077
1077
  * following values:
@@ -1081,7 +1081,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1081
1081
  * @returns The `morris` function returns an array of `ReturnType<BTNCallback<N>>` values.
1082
1082
  */
1083
1083
  morris<C extends BTNCallback<N>>(
1084
- callback: C = this._defaultCallbackByKey as C,
1084
+ callback: C = ((node: N) => node.key) as C,
1085
1085
  pattern: DFSOrderPattern = 'in',
1086
1086
  beginRoot: N | null = this.root
1087
1087
  ): ReturnType<C>[] {
@@ -1226,15 +1226,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1226
1226
  return destNode;
1227
1227
  }
1228
1228
 
1229
- /**
1230
- * Time complexity is O(n)
1231
- * Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
1232
- * The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
1233
- * the tree's structure should be restored to its original state to maintain the tree's integrity.
1234
- * This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
1235
- */
1236
- protected _defaultCallbackByKey: BTNCallback<N, BTNKey> = node => node.key;
1237
-
1238
1229
  /**
1239
1230
  * The function `_addTo` adds a new node to a binary tree if there is an available position.
1240
1231
  * @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
@@ -234,7 +234,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
234
234
  */
235
235
  override get<C extends BTNCallback<N>>(
236
236
  identifier: ReturnType<C> | null,
237
- callback: C = this._defaultCallbackByKey as C,
237
+ callback: C = ((node: N) => node.key) as C,
238
238
  beginRoot = this.root,
239
239
  iterationType = this.iterationType
240
240
  ): N | null {
@@ -270,7 +270,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
270
270
  * generic type `N`.
271
271
  * @param callback - The `callback` parameter is a function that takes a node as input and returns a
272
272
  * value. This value is compared with the `nodeProperty` parameter to determine if the node should be
273
- * included in the result. The default value for `callback` is `this._defaultCallbackByKey`, which is
273
+ * included in the result. The default value for `callback` is `((node: N) => node.key)`, which is
274
274
  * a
275
275
  * @param [onlyOne=false] - A boolean value indicating whether to stop the traversal after finding
276
276
  * the first node that matches the nodeProperty. If set to true, the function will return an array
@@ -285,7 +285,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
285
285
  */
286
286
  override getNodes<C extends BTNCallback<N>>(
287
287
  identifier: ReturnType<C> | null,
288
- callback: C = this._defaultCallbackByKey as C,
288
+ callback: C = ((node: N) => node.key) as C,
289
289
  onlyOne = false,
290
290
  beginRoot: N | null = this.root,
291
291
  iterationType = this.iterationType
@@ -303,7 +303,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
303
303
 
304
304
  if (!cur.left && !cur.right) return;
305
305
  // TODO potential bug
306
- if (callback === this._defaultCallbackByKey) {
306
+ if (callback === ((node: N) => node.key)) {
307
307
  if (this._compare(cur.key, identifier as number) === CP.gt) cur.left && _traverse(cur.left);
308
308
  if (this._compare(cur.key, identifier as number) === CP.lt) cur.right && _traverse(cur.right);
309
309
  } else {
@@ -324,7 +324,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
324
324
  if (onlyOne) return ans;
325
325
  }
326
326
  // TODO potential bug
327
- if (callback === this._defaultCallbackByKey) {
327
+ if (callback === ((node: N) => node.key)) {
328
328
  if (this._compare(cur.key, identifier as number) === CP.gt) cur.left && queue.push(cur.left);
329
329
  if (this._compare(cur.key, identifier as number) === CP.lt) cur.right && queue.push(cur.right);
330
330
  } else {
@@ -358,7 +358,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
358
358
  * @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
359
359
  */
360
360
  lesserOrGreaterTraverse<C extends BTNCallback<N>>(
361
- callback: C = this._defaultCallbackByKey as C,
361
+ callback: C = ((node: N) => node.key) as C,
362
362
  lesserOrGreater: CP = CP.lt,
363
363
  targetNode: BTNKey | N | null = this.root,
364
364
  iterationType = this.iterationType
@@ -268,7 +268,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
268
268
  * @param callback - The `callback` parameter is a function that takes a node as input and returns a
269
269
  * value. This value is compared with the `identifier` parameter to determine if the node should be
270
270
  * included in the result. The `callback` parameter has a default value of
271
- * `this._defaultCallbackByKey`
271
+ * `((node: N) => node.key)`
272
272
  * @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
273
273
  * being deleted. If set to true, the count of the node will not be considered and the node will be
274
274
  * deleted regardless of its count. If set to false (default), the count of the node will be
@@ -277,7 +277,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
277
277
  */
278
278
  override delete<C extends BTNCallback<N>>(
279
279
  identifier: ReturnType<C>,
280
- callback: C = this._defaultCallbackByKey as C,
280
+ callback: C = ((node: N) => node.key) as C,
281
281
  ignoreCount = false
282
282
  ): BinaryTreeDeletedResult<N>[] {
283
283
  const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
@@ -480,7 +480,6 @@ describe('BinaryTree', () => {
480
480
  tree.delete(5);
481
481
  tree.delete(7);
482
482
  tree.delete(3);
483
-
484
483
  expect(tree.root).toBe(null);
485
484
  expect(tree.getHeight()).toBe(-1);
486
485
  });