min-heap-typed 1.38.7 → 1.38.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 (30) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +1 -1
  2. package/dist/data-structures/binary-tree/binary-tree.d.ts +18 -6
  3. package/dist/data-structures/binary-tree/binary-tree.js +58 -8
  4. package/dist/data-structures/binary-tree/bst.d.ts +1 -1
  5. package/dist/data-structures/binary-tree/rb-tree.d.ts +1 -1
  6. package/dist/data-structures/binary-tree/tree-multiset.d.ts +1 -1
  7. package/dist/data-structures/trie/trie.d.ts +2 -1
  8. package/dist/data-structures/trie/trie.js +3 -2
  9. package/package.json +2 -2
  10. package/src/data-structures/binary-tree/avl-tree.ts +3 -4
  11. package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
  12. package/src/data-structures/binary-tree/binary-tree.ts +66 -11
  13. package/src/data-structures/binary-tree/bst.ts +3 -1
  14. package/src/data-structures/binary-tree/rb-tree.ts +3 -1
  15. package/src/data-structures/binary-tree/tree-multiset.ts +2 -3
  16. package/src/data-structures/graph/abstract-graph.ts +10 -11
  17. package/src/data-structures/graph/directed-graph.ts +1 -2
  18. package/src/data-structures/graph/undirected-graph.ts +4 -5
  19. package/src/data-structures/hash/hash-map.ts +1 -1
  20. package/src/data-structures/hash/tree-map.ts +2 -1
  21. package/src/data-structures/hash/tree-set.ts +2 -1
  22. package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
  23. package/src/data-structures/matrix/matrix.ts +1 -1
  24. package/src/data-structures/matrix/vector2d.ts +2 -1
  25. package/src/data-structures/queue/deque.ts +5 -4
  26. package/src/data-structures/queue/queue.ts +1 -1
  27. package/src/data-structures/trie/trie.ts +4 -2
  28. package/src/types/data-structures/matrix/navigator.ts +1 -1
  29. package/src/types/utils/utils.ts +1 -1
  30. package/src/types/utils/validate-type.ts +2 -2
@@ -13,7 +13,7 @@ export declare class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeN
13
13
  height: number;
14
14
  constructor(key: BinaryTreeNodeKey, val?: V);
15
15
  }
16
- export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode> extends BST<V, N> implements IBinaryTree<V, N> {
16
+ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>> extends BST<V, N> implements IBinaryTree<V, N> {
17
17
  /**
18
18
  * This is a constructor function for an AVL tree data structure in TypeScript.
19
19
  * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
@@ -62,7 +62,7 @@ export declare class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = Bi
62
62
  * Represents a binary tree data structure.
63
63
  * @template N - The type of the binary tree's nodes.
64
64
  */
65
- export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode> implements IBinaryTree<V, N> {
65
+ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>> implements IBinaryTree<V, N> {
66
66
  /**
67
67
  * Creates a new instance of BinaryTree.
68
68
  * @param {BinaryTreeOptions} [options] - The options for the binary tree.
@@ -285,10 +285,6 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
285
285
  * @param callback - The `callback` parameter is a function that will be called for each node in the
286
286
  * breadth-first search. It takes a node of type `N` as its argument and returns a value of type
287
287
  * `BFSCallbackReturn<N>`. The default value for this parameter is `this._defaultCallbackByKey
288
- * @param {boolean} [withLevel=false] - The `withLevel` parameter is a boolean flag that determines
289
- * whether to include the level of each node in the callback function. If `withLevel` is set
290
- * to `true`, the level of each node will be passed as an argument to the callback function. If
291
- * `withLevel` is
292
288
  * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
293
289
  * search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
294
290
  * will not be performed and an empty array will be returned.
@@ -296,7 +292,23 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
296
292
  * in the breadth-first search (BFS) algorithm. It can have two possible values:
297
293
  * @returns The function `bfs` returns an array of `BFSCallbackReturn<N>[]`.
298
294
  */
299
- bfs<C extends BFSCallback<N> = BFSCallback<N, BinaryTreeNodeKey>>(callback?: C, withLevel?: boolean, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[];
295
+ bfs<C extends BFSCallback<N> = BFSCallback<N, BinaryTreeNodeKey>>(callback?: C, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[];
296
+ /**
297
+ * The `listLevels` function takes a binary tree node and a callback function, and returns an array
298
+ * of arrays representing the levels of the tree.
299
+ * @param {C} callback - The `callback` parameter is a function that will be called on each node in
300
+ * the tree. It takes a node as input and returns a value. The return type of the callback function
301
+ * is determined by the generic type `C`.
302
+ * @param {N | null} beginRoot - The `beginRoot` parameter represents the starting node of the binary tree
303
+ * traversal. It can be any node in the binary tree. If no node is provided, the traversal will start
304
+ * from the root node of the binary tree.
305
+ * @param iterationType - The `iterationType` parameter determines whether the tree traversal is done
306
+ * recursively or iteratively. It can have two possible values:
307
+ * @returns The function `listLevels` returns an array of arrays, where each inner array represents a
308
+ * level in a binary tree. Each inner array contains the return type of the provided callback
309
+ * function `C` applied to the nodes at that level.
310
+ */
311
+ listLevels<C extends BFSCallback<N> = BFSCallback<N, BinaryTreeNodeKey>>(callback?: C, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[][];
300
312
  /**
301
313
  * The function returns the predecessor node of a given node in a binary tree.
302
314
  * @param {N} node - The parameter "node" represents a node in a binary tree.
@@ -821,10 +821,6 @@ class BinaryTree {
821
821
  * @param callback - The `callback` parameter is a function that will be called for each node in the
822
822
  * breadth-first search. It takes a node of type `N` as its argument and returns a value of type
823
823
  * `BFSCallbackReturn<N>`. The default value for this parameter is `this._defaultCallbackByKey
824
- * @param {boolean} [withLevel=false] - The `withLevel` parameter is a boolean flag that determines
825
- * whether to include the level of each node in the callback function. If `withLevel` is set
826
- * to `true`, the level of each node will be passed as an argument to the callback function. If
827
- * `withLevel` is
828
824
  * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
829
825
  * search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
830
826
  * will not be performed and an empty array will be returned.
@@ -832,13 +828,65 @@ class BinaryTree {
832
828
  * in the breadth-first search (BFS) algorithm. It can have two possible values:
833
829
  * @returns The function `bfs` returns an array of `BFSCallbackReturn<N>[]`.
834
830
  */
835
- bfs(callback = this._defaultCallbackByKey, withLevel = false, beginRoot = this.root, iterationType = this.iterationType) {
831
+ bfs(callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
836
832
  if (!beginRoot)
837
833
  return [];
838
834
  const ans = [];
835
+ if (iterationType === types_1.IterationType.RECURSIVE) {
836
+ const queue = new queue_1.Queue([beginRoot]);
837
+ function traverse(level) {
838
+ if (queue.size === 0)
839
+ return;
840
+ const current = queue.shift();
841
+ ans.push(callback(current));
842
+ if (current.left)
843
+ queue.push(current.left);
844
+ if (current.right)
845
+ queue.push(current.right);
846
+ traverse(level + 1);
847
+ }
848
+ traverse(0);
849
+ }
850
+ else {
851
+ const queue = new queue_1.Queue([beginRoot]);
852
+ while (queue.size > 0) {
853
+ const levelSize = queue.size;
854
+ for (let i = 0; i < levelSize; i++) {
855
+ const current = queue.shift();
856
+ ans.push(callback(current));
857
+ if (current.left)
858
+ queue.push(current.left);
859
+ if (current.right)
860
+ queue.push(current.right);
861
+ }
862
+ }
863
+ }
864
+ return ans;
865
+ }
866
+ /**
867
+ * The `listLevels` function takes a binary tree node and a callback function, and returns an array
868
+ * of arrays representing the levels of the tree.
869
+ * @param {C} callback - The `callback` parameter is a function that will be called on each node in
870
+ * the tree. It takes a node as input and returns a value. The return type of the callback function
871
+ * is determined by the generic type `C`.
872
+ * @param {N | null} beginRoot - The `beginRoot` parameter represents the starting node of the binary tree
873
+ * traversal. It can be any node in the binary tree. If no node is provided, the traversal will start
874
+ * from the root node of the binary tree.
875
+ * @param iterationType - The `iterationType` parameter determines whether the tree traversal is done
876
+ * recursively or iteratively. It can have two possible values:
877
+ * @returns The function `listLevels` returns an array of arrays, where each inner array represents a
878
+ * level in a binary tree. Each inner array contains the return type of the provided callback
879
+ * function `C` applied to the nodes at that level.
880
+ */
881
+ listLevels(callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
882
+ if (!beginRoot)
883
+ return [];
884
+ const levelsNodes = [];
839
885
  if (iterationType === types_1.IterationType.RECURSIVE) {
840
886
  const _recursive = (node, level) => {
841
- callback && ans.push(callback(node, withLevel ? level : undefined));
887
+ if (!levelsNodes[level])
888
+ levelsNodes[level] = [];
889
+ levelsNodes[level].push(callback(node));
842
890
  if (node.left)
843
891
  _recursive(node.left, level + 1);
844
892
  if (node.right)
@@ -851,14 +899,16 @@ class BinaryTree {
851
899
  while (stack.length > 0) {
852
900
  const head = stack.pop();
853
901
  const [node, level] = head;
854
- callback && ans.push(callback(node, withLevel ? level : undefined));
902
+ if (!levelsNodes[level])
903
+ levelsNodes[level] = [];
904
+ levelsNodes[level].push(callback(node));
855
905
  if (node.right)
856
906
  stack.push([node.right, level + 1]);
857
907
  if (node.left)
858
908
  stack.push([node.left, level + 1]);
859
909
  }
860
910
  }
861
- return ans;
911
+ return levelsNodes;
862
912
  }
863
913
  /**
864
914
  * The function returns the predecessor node of a given node in a binary tree.
@@ -12,7 +12,7 @@ import { IBinaryTree } from '../../interfaces';
12
12
  export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
13
13
  constructor(key: BinaryTreeNodeKey, val?: V);
14
14
  }
15
- export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode> extends BinaryTree<V, N> implements IBinaryTree<V, N> {
15
+ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>> extends BinaryTree<V, N> implements IBinaryTree<V, N> {
16
16
  /**
17
17
  * The constructor function initializes a binary search tree object with an optional comparator
18
18
  * function.
@@ -7,7 +7,7 @@ export declare class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNode
7
7
  get color(): RBColor;
8
8
  set color(value: RBColor);
9
9
  }
10
- export declare class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode> extends BST<V, N> implements IBinaryTree<V, N> {
10
+ export declare class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNested<V>>> extends BST<V, N> implements IBinaryTree<V, N> {
11
11
  constructor(options?: RBTreeOptions);
12
12
  createNode(key: BinaryTreeNodeKey, val?: V): N;
13
13
  }
@@ -26,7 +26,7 @@ export declare class TreeMultisetNode<V = any, N extends TreeMultisetNode<V, N>
26
26
  /**
27
27
  * The only distinction between a TreeMultiset and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
28
28
  */
29
- export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNode> extends AVLTree<V, N> implements IBinaryTree<V, N> {
29
+ export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNode<V, TreeMultisetNodeNested<V>>> extends AVLTree<V, N> implements IBinaryTree<V, N> {
30
30
  /**
31
31
  * The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
32
32
  * merge duplicated values.
@@ -77,8 +77,9 @@ export declare class Trie {
77
77
  * @param {string} prefix - The `prefix` parameter is a string that represents the prefix that we want to search for in the
78
78
  * trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
79
79
  * @param {number} max - The max count of words will be found
80
+ * @param isAllWhenEmptyPrefix - If true, when the prefix provided as '', returns all the words in the trie.
80
81
  * @returns {string[]} an array of strings.
81
82
  */
82
- getWords(prefix?: string, max?: number): string[];
83
+ getWords(prefix?: string, max?: number, isAllWhenEmptyPrefix?: boolean): string[];
83
84
  private _caseProcess;
84
85
  }
@@ -226,9 +226,10 @@ class Trie {
226
226
  * @param {string} prefix - The `prefix` parameter is a string that represents the prefix that we want to search for in the
227
227
  * trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
228
228
  * @param {number} max - The max count of words will be found
229
+ * @param isAllWhenEmptyPrefix - If true, when the prefix provided as '', returns all the words in the trie.
229
230
  * @returns {string[]} an array of strings.
230
231
  */
231
- getWords(prefix = '', max = Number.MAX_SAFE_INTEGER) {
232
+ getWords(prefix = '', max = Number.MAX_SAFE_INTEGER, isAllWhenEmptyPrefix = false) {
232
233
  prefix = this._caseProcess(prefix);
233
234
  const words = [];
234
235
  let found = 0;
@@ -254,7 +255,7 @@ class Trie {
254
255
  startNode = nodeC;
255
256
  }
256
257
  }
257
- if (startNode !== this.root)
258
+ if (isAllWhenEmptyPrefix || startNode !== this.root)
258
259
  dfs(startNode, prefix);
259
260
  return words;
260
261
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "min-heap-typed",
3
- "version": "1.38.7",
3
+ "version": "1.38.9",
4
4
  "description": "Min Heap. Javascript & Typescript Data Structure.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -131,6 +131,6 @@
131
131
  "typescript": "^4.9.5"
132
132
  },
133
133
  "dependencies": {
134
- "data-structure-typed": "^1.38.7"
134
+ "data-structure-typed": "^1.38.9"
135
135
  }
136
136
  }
@@ -19,10 +19,9 @@ export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNeste
19
19
  }
20
20
  }
21
21
 
22
- export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode>
22
+ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>>
23
23
  extends BST<V, N>
24
- implements IBinaryTree<V, N>
25
- {
24
+ implements IBinaryTree<V, N> {
26
25
  /**
27
26
  * This is a constructor function for an AVL tree data structure in TypeScript.
28
27
  * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
@@ -161,7 +160,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode>
161
160
  // Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
162
161
  switch (
163
162
  this._balanceFactor(A) // second O(1)
164
- ) {
163
+ ) {
165
164
  case -2:
166
165
  if (A && A.left) {
167
166
  if (this._balanceFactor(A.left) <= 0) {
@@ -17,7 +17,7 @@ export class BinaryIndexedTree {
17
17
  * @param - - `frequency`: The default frequency value. It is optional and has a default
18
18
  * value of 0.
19
19
  */
20
- constructor({frequency = 0, max}: {frequency?: number; max: number}) {
20
+ constructor({frequency = 0, max}: { frequency?: number; max: number }) {
21
21
  this._freq = frequency;
22
22
  this._max = max;
23
23
  this._freqMap = {0: 0};
@@ -115,7 +115,8 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
115
115
  * Represents a binary tree data structure.
116
116
  * @template N - The type of the binary tree's nodes.
117
117
  */
118
- export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode> implements IBinaryTree<V, N> {
118
+ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
119
+ implements IBinaryTree<V, N> {
119
120
  /**
120
121
  * Creates a new instance of BinaryTree.
121
122
  * @param {BinaryTreeOptions} [options] - The options for the binary tree.
@@ -404,7 +405,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
404
405
  return -1;
405
406
  }
406
407
 
407
- const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
408
+ const stack: { node: N; depth: number }[] = [{node: beginRoot, depth: 0}];
408
409
  let maxHeight = 0;
409
410
 
410
411
  while (stack.length > 0) {
@@ -887,7 +888,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
887
888
  _traverse(beginRoot);
888
889
  } else {
889
890
  // 0: visit, 1: print
890
- const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];
891
+ const stack: { opt: 0 | 1; node: N | null | undefined }[] = [{opt: 0, node: beginRoot}];
891
892
 
892
893
  while (stack.length > 0) {
893
894
  const cur = stack.pop();
@@ -930,10 +931,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
930
931
  * @param callback - The `callback` parameter is a function that will be called for each node in the
931
932
  * breadth-first search. It takes a node of type `N` as its argument and returns a value of type
932
933
  * `BFSCallbackReturn<N>`. The default value for this parameter is `this._defaultCallbackByKey
933
- * @param {boolean} [withLevel=false] - The `withLevel` parameter is a boolean flag that determines
934
- * whether to include the level of each node in the callback function. If `withLevel` is set
935
- * to `true`, the level of each node will be passed as an argument to the callback function. If
936
- * `withLevel` is
937
934
  * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
938
935
  * search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
939
936
  * will not be performed and an empty array will be returned.
@@ -943,7 +940,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
943
940
  */
944
941
  bfs<C extends BFSCallback<N> = BFSCallback<N, BinaryTreeNodeKey>>(
945
942
  callback: C = this._defaultCallbackByKey as C,
946
- withLevel: boolean = false,
947
943
  beginRoot: N | null = this.root,
948
944
  iterationType = this.iterationType
949
945
  ): ReturnType<C>[] {
@@ -951,9 +947,66 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
951
947
 
952
948
  const ans: BFSCallbackReturn<N>[] = [];
953
949
 
950
+ if (iterationType === IterationType.RECURSIVE) {
951
+ const queue = new Queue<N>([beginRoot]);
952
+
953
+ function traverse(level: number) {
954
+ if (queue.size === 0) return;
955
+
956
+ const current = queue.shift()!;
957
+ ans.push(callback(current));
958
+
959
+ if (current.left) queue.push(current.left);
960
+ if (current.right) queue.push(current.right);
961
+
962
+ traverse(level + 1);
963
+ }
964
+
965
+ traverse(0);
966
+ } else {
967
+ const queue = new Queue<N>([beginRoot]);
968
+ while (queue.size > 0) {
969
+ const levelSize = queue.size;
970
+
971
+ for (let i = 0; i < levelSize; i++) {
972
+ const current = queue.shift()!;
973
+ ans.push(callback(current));
974
+
975
+ if (current.left) queue.push(current.left);
976
+ if (current.right) queue.push(current.right);
977
+ }
978
+ }
979
+ }
980
+ return ans;
981
+ }
982
+
983
+ /**
984
+ * The `listLevels` function takes a binary tree node and a callback function, and returns an array
985
+ * of arrays representing the levels of the tree.
986
+ * @param {C} callback - The `callback` parameter is a function that will be called on each node in
987
+ * the tree. It takes a node as input and returns a value. The return type of the callback function
988
+ * is determined by the generic type `C`.
989
+ * @param {N | null} beginRoot - The `beginRoot` parameter represents the starting node of the binary tree
990
+ * traversal. It can be any node in the binary tree. If no node is provided, the traversal will start
991
+ * from the root node of the binary tree.
992
+ * @param iterationType - The `iterationType` parameter determines whether the tree traversal is done
993
+ * recursively or iteratively. It can have two possible values:
994
+ * @returns The function `listLevels` returns an array of arrays, where each inner array represents a
995
+ * level in a binary tree. Each inner array contains the return type of the provided callback
996
+ * function `C` applied to the nodes at that level.
997
+ */
998
+ listLevels<C extends BFSCallback<N> = BFSCallback<N, BinaryTreeNodeKey>>(
999
+ callback: C = this._defaultCallbackByKey as C,
1000
+ beginRoot: N | null = this.root,
1001
+ iterationType = this.iterationType
1002
+ ): ReturnType<C>[][] {
1003
+ if (!beginRoot) return [];
1004
+ const levelsNodes: ReturnType<C>[][] = [];
1005
+
954
1006
  if (iterationType === IterationType.RECURSIVE) {
955
1007
  const _recursive = (node: N, level: number) => {
956
- callback && ans.push(callback(node, withLevel ? level : undefined));
1008
+ if (!levelsNodes[level]) levelsNodes[level] = [];
1009
+ levelsNodes[level].push(callback(node));
957
1010
  if (node.left) _recursive(node.left, level + 1);
958
1011
  if (node.right) _recursive(node.right, level + 1);
959
1012
  };
@@ -966,12 +1019,14 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
966
1019
  const head = stack.pop()!;
967
1020
  const [node, level] = head;
968
1021
 
969
- callback && ans.push(callback(node, withLevel ? level : undefined));
1022
+ if (!levelsNodes[level]) levelsNodes[level] = [];
1023
+ levelsNodes[level].push(callback(node));
970
1024
  if (node.right) stack.push([node.right, level + 1]);
971
1025
  if (node.left) stack.push([node.left, level + 1]);
972
1026
  }
973
1027
  }
974
- return ans;
1028
+
1029
+ return levelsNodes;
975
1030
  }
976
1031
 
977
1032
  /**
@@ -24,7 +24,9 @@ export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extend
24
24
  }
25
25
  }
26
26
 
27
- export class BST<V = any, N extends BSTNode<V, N> = BSTNode> extends BinaryTree<V, N> implements IBinaryTree<V, N> {
27
+ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>>
28
+ extends BinaryTree<V, N>
29
+ implements IBinaryTree<V, N> {
28
30
  /**
29
31
  * The constructor function initializes a binary search tree object with an optional comparator
30
32
  * function.
@@ -19,7 +19,9 @@ export class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V
19
19
  }
20
20
  }
21
21
 
22
- export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode> extends BST<V, N> implements IBinaryTree<V, N> {
22
+ export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNested<V>>>
23
+ extends BST<V, N>
24
+ implements IBinaryTree<V, N> {
23
25
  constructor(options?: RBTreeOptions) {
24
26
  super(options);
25
27
  }
@@ -35,10 +35,9 @@ export class TreeMultisetNode<
35
35
  /**
36
36
  * The only distinction between a TreeMultiset and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
37
37
  */
38
- export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNode>
38
+ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNode<V, TreeMultisetNodeNested<V>>>
39
39
  extends AVLTree<V, N>
40
- implements IBinaryTree<V, N>
41
- {
40
+ implements IBinaryTree<V, N> {
42
41
  /**
43
42
  * The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
44
43
  * merge duplicated values.
@@ -105,8 +105,7 @@ export abstract class AbstractEdge<V = any> {
105
105
  export abstract class AbstractGraph<
106
106
  V extends AbstractVertex<any> = AbstractVertex<any>,
107
107
  E extends AbstractEdge<any> = AbstractEdge<any>
108
- > implements IGraph<V, E>
109
- {
108
+ > implements IGraph<V, E> {
110
109
  private _vertices: Map<VertexKey, V> = new Map<VertexKey, V>();
111
110
 
112
111
  get vertices(): Map<VertexKey, V> {
@@ -554,14 +553,14 @@ export abstract class AbstractGraph<
554
553
  }
555
554
 
556
555
  getMinDist &&
557
- distMap.forEach((d, v) => {
558
- if (v !== srcVertex) {
559
- if (d < minDist) {
560
- minDist = d;
561
- if (genPaths) minDest = v;
562
- }
556
+ distMap.forEach((d, v) => {
557
+ if (v !== srcVertex) {
558
+ if (d < minDist) {
559
+ minDist = d;
560
+ if (genPaths) minDest = v;
563
561
  }
564
- });
562
+ }
563
+ });
565
564
 
566
565
  genPaths && getPaths(minDest);
567
566
 
@@ -623,7 +622,7 @@ export abstract class AbstractGraph<
623
622
  if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
624
623
  }
625
624
 
626
- const heap = new PriorityQueue<{key: number; val: V}>((a, b) => a.key - b.key);
625
+ const heap = new PriorityQueue<{ key: number; val: V }>((a, b) => a.key - b.key);
627
626
  heap.add({key: 0, val: srcVertex});
628
627
 
629
628
  distMap.set(srcVertex, 0);
@@ -852,7 +851,7 @@ export abstract class AbstractGraph<
852
851
  * `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
853
852
  * path between vertices in the
854
853
  */
855
- floyd(): {costs: number[][]; predecessor: (V | null)[][]} {
854
+ floyd(): { costs: number[][]; predecessor: (V | null)[][] } {
856
855
  const idAndVertices = [...this._vertices];
857
856
  const n = idAndVertices.length;
858
857
 
@@ -64,8 +64,7 @@ export class DirectedEdge<V = any> extends AbstractEdge<V> {
64
64
 
65
65
  export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E extends DirectedEdge<any> = DirectedEdge>
66
66
  extends AbstractGraph<V, E>
67
- implements IGraph<V, E>
68
- {
67
+ implements IGraph<V, E> {
69
68
  /**
70
69
  * The constructor function initializes an instance of a class.
71
70
  */
@@ -51,12 +51,11 @@ export class UndirectedEdge<V = number> extends AbstractEdge<V> {
51
51
  }
52
52
 
53
53
  export class UndirectedGraph<
54
- V extends UndirectedVertex<any> = UndirectedVertex,
55
- E extends UndirectedEdge<any> = UndirectedEdge
56
- >
54
+ V extends UndirectedVertex<any> = UndirectedVertex,
55
+ E extends UndirectedEdge<any> = UndirectedEdge
56
+ >
57
57
  extends AbstractGraph<V, E>
58
- implements IGraph<V, E>
59
- {
58
+ implements IGraph<V, E> {
60
59
  /**
61
60
  * The constructor initializes a new Map object to store edges.
62
61
  */
@@ -157,7 +157,7 @@ export class HashMap<K, V> {
157
157
  }
158
158
  }
159
159
 
160
- *entries(): IterableIterator<[K, V]> {
160
+ * entries(): IterableIterator<[K, V]> {
161
161
  for (const bucket of this.table) {
162
162
  if (bucket) {
163
163
  for (const [key, value] of bucket) {
@@ -1 +1,2 @@
1
- export class TreeMap {}
1
+ export class TreeMap {
2
+ }
@@ -1 +1,2 @@
1
- export class TreeSet {}
1
+ export class TreeSet {
2
+ }
@@ -485,7 +485,7 @@ export class SinglyLinkedList<E = any> {
485
485
  return count;
486
486
  }
487
487
 
488
- *[Symbol.iterator]() {
488
+ * [Symbol.iterator]() {
489
489
  let current = this.head;
490
490
 
491
491
  while (current) {
@@ -14,7 +14,7 @@ export class MatrixNTI2D<V = any> {
14
14
  * given initial value or 0 if not provided.
15
15
  * @param options - An object containing the following properties:
16
16
  */
17
- constructor(options: {row: number; col: number; initialVal?: V}) {
17
+ constructor(options: { row: number; col: number; initialVal?: V }) {
18
18
  const {row, col, initialVal} = options;
19
19
  this._matrix = new Array(row).fill(undefined).map(() => new Array(col).fill(initialVal || 0));
20
20
  }
@@ -10,7 +10,8 @@ export class Vector2D {
10
10
  public x: number = 0,
11
11
  public y: number = 0,
12
12
  public w: number = 1 // needed for matrix multiplication
13
- ) {}
13
+ ) {
14
+ }
14
15
 
15
16
  /**
16
17
  * The function checks if the x and y values of a point are both zero.
@@ -9,7 +9,8 @@ import {DoublyLinkedList} from '../linked-list';
9
9
 
10
10
  // O(n) time complexity of obtaining the value
11
11
  // O(1) time complexity of adding at the beginning and the end
12
- export class Deque<E = any> extends DoublyLinkedList<E> {}
12
+ export class Deque<E = any> extends DoublyLinkedList<E> {
13
+ }
13
14
 
14
15
  // O(1) time complexity of obtaining the value
15
16
  // O(n) time complexity of adding at the beginning and the end
@@ -19,9 +20,9 @@ export class ObjectDeque<E = number> {
19
20
  if (capacity !== undefined) this._capacity = capacity;
20
21
  }
21
22
 
22
- private _nodes: {[key: number]: E} = {};
23
+ private _nodes: { [key: number]: E } = {};
23
24
 
24
- get nodes(): {[p: number]: E} {
25
+ get nodes(): { [p: number]: E } {
25
26
  return this._nodes;
26
27
  }
27
28
 
@@ -156,7 +157,7 @@ export class ObjectDeque<E = number> {
156
157
  return this._size <= 0;
157
158
  }
158
159
 
159
- protected _seNodes(value: {[p: number]: E}) {
160
+ protected _seNodes(value: { [p: number]: E }) {
160
161
  this._nodes = value;
161
162
  }
162
163
 
@@ -183,7 +183,7 @@ export class Queue<E = any> {
183
183
  return new Queue(this.nodes.slice(this.offset));
184
184
  }
185
185
 
186
- *[Symbol.iterator]() {
186
+ * [Symbol.iterator]() {
187
187
  for (const item of this.nodes) {
188
188
  yield item;
189
189
  }
@@ -241,9 +241,10 @@ export class Trie {
241
241
  * @param {string} prefix - The `prefix` parameter is a string that represents the prefix that we want to search for in the
242
242
  * trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
243
243
  * @param {number} max - The max count of words will be found
244
+ * @param isAllWhenEmptyPrefix - If true, when the prefix provided as '', returns all the words in the trie.
244
245
  * @returns {string[]} an array of strings.
245
246
  */
246
- getWords(prefix = '', max = Number.MAX_SAFE_INTEGER): string[] {
247
+ getWords(prefix = '', max = Number.MAX_SAFE_INTEGER, isAllWhenEmptyPrefix = false): string[] {
247
248
  prefix = this._caseProcess(prefix);
248
249
  const words: string[] = [];
249
250
  let found = 0;
@@ -270,7 +271,8 @@ export class Trie {
270
271
  if (nodeC) startNode = nodeC;
271
272
  }
272
273
  }
273
- if (startNode !== this.root) dfs(startNode, prefix);
274
+
275
+ if (isAllWhenEmptyPrefix || startNode !== this.root) dfs(startNode, prefix);
274
276
 
275
277
  return words;
276
278
  }
@@ -1,6 +1,6 @@
1
1
  export type Direction = 'up' | 'right' | 'down' | 'left';
2
2
 
3
- export type Turning = {[key in Direction]: Direction};
3
+ export type Turning = { [key in Direction]: Direction };
4
4
 
5
5
  export type NavigatorParams<T = any> = {
6
6
  matrix: T[][];
@@ -1,5 +1,5 @@
1
1
  export type ToThunkFn = () => ReturnType<TrlFn>;
2
- export type Thunk = () => ReturnType<ToThunkFn> & {__THUNK__: symbol};
2
+ export type Thunk = () => ReturnType<ToThunkFn> & { __THUNK__: symbol };
3
3
  export type TrlFn = (...args: any[]) => any;
4
4
  export type TrlAsyncFn = (...args: any[]) => any;
5
5
 
@@ -1,6 +1,6 @@
1
- export type KeyValueObject = {[key: string]: any};
1
+ export type KeyValueObject = { [key: string]: any };
2
2
 
3
- export type KeyValueObjectWithKey = {[key: string]: any; key: string | number | symbol};
3
+ export type KeyValueObjectWithKey = { [key: string]: any; key: string | number | symbol };
4
4
 
5
5
  export type NonNumberNonObjectButDefined = string | boolean | symbol | null;
6
6