data-structure-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 (69) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +1 -1
  3. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +1 -1
  4. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  5. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
  6. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +18 -6
  7. package/dist/cjs/data-structures/binary-tree/binary-tree.js +58 -8
  8. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  9. package/dist/cjs/data-structures/binary-tree/bst.d.ts +1 -1
  10. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +1 -1
  12. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  13. package/dist/cjs/data-structures/binary-tree/tree-multiset.d.ts +1 -1
  14. package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
  15. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  16. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  17. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  18. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  19. package/dist/cjs/data-structures/hash/tree-map.js.map +1 -1
  20. package/dist/cjs/data-structures/hash/tree-set.js.map +1 -1
  21. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  22. package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
  23. package/dist/cjs/data-structures/matrix/vector2d.js.map +1 -1
  24. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  25. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  26. package/dist/cjs/data-structures/trie/trie.d.ts +2 -1
  27. package/dist/cjs/data-structures/trie/trie.js +3 -2
  28. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  29. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +1 -1
  30. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +18 -6
  31. package/dist/mjs/data-structures/binary-tree/binary-tree.js +58 -8
  32. package/dist/mjs/data-structures/binary-tree/bst.d.ts +1 -1
  33. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +1 -1
  34. package/dist/mjs/data-structures/binary-tree/tree-multiset.d.ts +1 -1
  35. package/dist/mjs/data-structures/trie/trie.d.ts +2 -1
  36. package/dist/mjs/data-structures/trie/trie.js +3 -2
  37. package/dist/umd/{index.global.js → data-structure-typed.min.js} +2 -2
  38. package/dist/umd/data-structure-typed.min.js.map +1 -0
  39. package/package.json +6 -6
  40. package/src/data-structures/binary-tree/avl-tree.ts +3 -4
  41. package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
  42. package/src/data-structures/binary-tree/binary-tree.ts +66 -11
  43. package/src/data-structures/binary-tree/bst.ts +3 -1
  44. package/src/data-structures/binary-tree/rb-tree.ts +3 -1
  45. package/src/data-structures/binary-tree/tree-multiset.ts +2 -3
  46. package/src/data-structures/graph/abstract-graph.ts +10 -11
  47. package/src/data-structures/graph/directed-graph.ts +1 -2
  48. package/src/data-structures/graph/undirected-graph.ts +4 -5
  49. package/src/data-structures/hash/hash-map.ts +1 -1
  50. package/src/data-structures/hash/tree-map.ts +2 -1
  51. package/src/data-structures/hash/tree-set.ts +2 -1
  52. package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
  53. package/src/data-structures/matrix/matrix.ts +1 -1
  54. package/src/data-structures/matrix/vector2d.ts +2 -1
  55. package/src/data-structures/queue/deque.ts +5 -4
  56. package/src/data-structures/queue/queue.ts +1 -1
  57. package/src/data-structures/trie/trie.ts +4 -2
  58. package/src/types/data-structures/matrix/navigator.ts +1 -1
  59. package/src/types/utils/utils.ts +1 -1
  60. package/src/types/utils/validate-type.ts +2 -2
  61. package/test/integration/bst.test.ts +1 -1
  62. package/test/integration/index.html +1 -1
  63. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +1 -1
  64. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +25 -1
  65. package/test/unit/data-structures/binary-tree/bst.test.ts +1 -7
  66. package/test/unit/data-structures/binary-tree/overall.test.ts +18 -18
  67. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +1 -1
  68. package/tsup.config.js +11 -4
  69. package/dist/umd/index.global.js.map +0 -1
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.38.7",
3
+ "version": "1.38.9",
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",
7
7
  "types": "dist/mjs/index.d.ts",
8
- "umd:main": "dist/umd/index.global.js",
8
+ "umd:main": "dist/umd/data-structure-typed.min.js",
9
9
  "exports": {
10
10
  ".": {
11
11
  "import": "./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.38.6",
64
+ "avl-tree-typed": "^1.38.8",
65
65
  "benchmark": "^2.1.4",
66
- "binary-tree-typed": "^1.38.6",
67
- "bst-typed": "^1.38.6",
66
+ "binary-tree-typed": "^1.38.8",
67
+ "bst-typed": "^1.38.8",
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.38.6",
74
+ "heap-typed": "^1.38.8",
75
75
  "istanbul-badges-readme": "^1.8.5",
76
76
  "jest": "^29.7.0",
77
77
  "prettier": "^3.0.3",
@@ -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
 
@@ -183,7 +183,7 @@ describe('Individual package BST operations test', () => {
183
183
  });
184
184
 
185
185
  it('should perform various operations on a Binary Search Tree with object values', () => {
186
- const objBST = new BST<BSTNode<{key: number; keyA: number}>>();
186
+ const objBST = new BST<{key: number; keyA: number}>();
187
187
  expect(objBST).toBeInstanceOf(BST);
188
188
  objBST.add(11, {key: 11, keyA: 11});
189
189
  objBST.add(3, {key: 3, keyA: 3});
@@ -3,7 +3,7 @@
3
3
  <head>
4
4
  <meta charset='UTF-8'>
5
5
  <title>CDN Test</title>
6
- <script src='https://cdn.jsdelivr.net/npm/data-structure-typed/dist/umd/index.global.js'></script>
6
+ <script src='https://cdn.jsdelivr.net/npm/data-structure-typed/dist/umd/data-structure-typed.min.js'></script>
7
7
  </head>
8
8
  <body>
9
9
  <div id='app'>
@@ -123,7 +123,7 @@ describe('AVLTree APIs test', () => {
123
123
  avl.add(node3);
124
124
  avl.add(node3, {id: 3, text: 'text33'});
125
125
 
126
- const bfsRes = avl.bfs(node => node.key, false);
126
+ const bfsRes = avl.bfs(node => node.key);
127
127
  expect(bfsRes[0]).toBe(2);
128
128
  });
129
129
  });
@@ -1,4 +1,8 @@
1
- import {AVLTree, AVLTreeNode, BinaryTree, BinaryTreeNode} from '../../../../src';
1
+ import {AVLTree, AVLTreeNode, BinaryTree, BinaryTreeNode, IterationType} from '../../../../src';
2
+ import {isDebugTest} from '../../../config';
3
+
4
+ const isDebug = isDebugTest;
5
+ // const isDebug = true;
2
6
 
3
7
  describe('BinaryTreeNode', () => {
4
8
  it('should create an instance of BinaryTreeNode', () => {
@@ -213,3 +217,23 @@ describe('BinaryTree APIs test', () => {
213
217
  expect(bfsRes[0]?.key).toBe(2);
214
218
  });
215
219
  });
220
+
221
+ describe('BinaryTree traversals', () => {
222
+ const tree = new BinaryTree<number>();
223
+
224
+ const arr = [35, 20, 40, 15, 29, null, 50, null, 16, 28, 30, 45, 55];
225
+ tree.refill(arr);
226
+ expect(tree.dfs(node => node.key, 'pre')).toEqual([35, 20, 15, 16, 29, 28, 30, 40, 50, 45, 55]);
227
+ expect(tree.dfs(node => node.key, 'in')).toEqual([15, 16, 20, 28, 29, 30, 35, 40, 45, 50, 55]);
228
+ expect(tree.dfs(node => node.key, 'post')).toEqual([16, 15, 28, 30, 29, 20, 45, 55, 50, 40, 35]);
229
+ expect(tree.bfs(node => node.key, tree.root, IterationType.RECURSIVE)).toEqual([
230
+ 35, 20, 40, 15, 29, 50, 16, 28, 30, 45, 55
231
+ ]);
232
+ expect(tree.bfs(node => node.key, tree.root, IterationType.ITERATIVE)).toEqual([
233
+ 35, 20, 40, 15, 29, 50, 16, 28, 30, 45, 55
234
+ ]);
235
+
236
+ const levels = tree.listLevels(node => node.key);
237
+ expect(levels).toEqual([[35], [20, 40], [15, 29, 50], [16, 28, 30, 45, 55]]);
238
+ isDebug && console.log(levels);
239
+ });
@@ -435,13 +435,7 @@ describe('BST Performance test', function () {
435
435
  bst.addMany(nodes);
436
436
  isDebug && console.log('---add', performance.now() - start);
437
437
  const startL = performance.now();
438
- const arr: number[][] = [];
439
- bst.bfs((node, level) => {
440
- if (level !== undefined) {
441
- if (!arr[level]) arr[level] = [];
442
- arr[level].push(node.key);
443
- }
444
- }, true);
438
+ const arr: number[][] = bst.listLevels(node => node.key);
445
439
  isDebug && console.log('---listLevels', arr);
446
440
  isDebug && console.log('---listLevels', performance.now() - startL);
447
441
  });
@@ -6,27 +6,27 @@ describe('Overall BinaryTree Test', () => {
6
6
  bst.add(11);
7
7
  bst.add(3);
8
8
  bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5], undefined, false);
9
- bst.size === 16; // true
10
- expect(bst.size).toBe(16); // true
11
- bst.has(6); // true
12
- expect(bst.has(6)).toBe(true); // true
13
- bst.getHeight(6) === 2; // true
14
- bst.getHeight() === 5; // true
15
- bst.getDepth(6) === 3; // true
16
- expect(bst.getHeight(6)).toBe(2); // true
17
- expect(bst.getHeight()).toBe(5); // true
18
- expect(bst.getDepth(6)).toBe(3); // true
9
+ bst.size === 16; // true
10
+ expect(bst.size).toBe(16); // true
11
+ bst.has(6); // true
12
+ expect(bst.has(6)).toBe(true); // true
13
+ bst.getHeight(6) === 2; // true
14
+ bst.getHeight() === 5; // true
15
+ bst.getDepth(6) === 3; // true
16
+ expect(bst.getHeight(6)).toBe(2); // true
17
+ expect(bst.getHeight()).toBe(5); // true
18
+ expect(bst.getDepth(6)).toBe(3); // true
19
19
  const leftMost = bst.getLeftMost();
20
- leftMost?.key === 1; // true
20
+ leftMost?.key === 1; // true
21
21
  expect(leftMost?.key).toBe(1);
22
22
  bst.delete(6);
23
- bst.get(6); // null
23
+ bst.get(6); // null
24
24
  expect(bst.get(6)).toBeNull();
25
- bst.isAVLBalanced(); // true or false
25
+ bst.isAVLBalanced(); // true or false
26
26
  expect(bst.isAVLBalanced()).toBe(true);
27
27
  const bfsIDs: number[] = [];
28
28
  bst.bfs(node => bfsIDs.push(node.key));
29
- bfsIDs[0] === 11; // true
29
+ bfsIDs[0] === 11; // true
30
30
  expect(bfsIDs[0]).toBe(11);
31
31
 
32
32
  const objBST = new BST<{key: number; keyA: number}>();
@@ -57,10 +57,10 @@ describe('Overall BinaryTree Test', () => {
57
57
 
58
58
  const avlTree = new AVLTree();
59
59
  avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
60
- avlTree.isAVLBalanced(); // true
61
- expect(avlTree.isAVLBalanced()).toBe(true); // true
60
+ avlTree.isAVLBalanced(); // true
61
+ expect(avlTree.isAVLBalanced()).toBe(true); // true
62
62
  avlTree.delete(10);
63
- avlTree.isAVLBalanced(); // true
64
- expect(avlTree.isAVLBalanced()).toBe(true); // true
63
+ avlTree.isAVLBalanced(); // true
64
+ expect(avlTree.isAVLBalanced()).toBe(true); // true
65
65
  });
66
66
  });
@@ -481,7 +481,7 @@ describe('TreeMultiset Performance test', function () {
481
481
  }
482
482
  isDebug && console.log('---add', performance.now() - start);
483
483
  const startL = performance.now();
484
- treeMS.lesserOrGreaterTraverse((node: TreeMultisetNode<number>) => (node.count += 1), CP.lt, inputSize / 2);
484
+ treeMS.lesserOrGreaterTraverse(node => (node.count += 1), CP.lt, inputSize / 2);
485
485
  isDebug && console.log('---lesserOrGreaterTraverse', performance.now() - startL);
486
486
  });
487
487
  });
package/tsup.config.js CHANGED
@@ -1,11 +1,18 @@
1
1
  export default [{
2
- entry: ['src/index.ts'],
2
+ entryPoints: {
3
+ "data-structure-typed": "src/index.ts"
4
+ },
3
5
  format: ["iife"],
4
6
  clean: true,
5
7
  sourcemap: true,
6
8
  minify: true,
7
- outDir: 'dist/umd',
8
- globalName: 'dataStructureTyped',
9
+ outDir: "dist/umd",
10
+ globalName: "dataStructureTyped",
9
11
  platform: "browser",
10
- bundle: true
12
+ bundle: true,
13
+ outExtension() {
14
+ return {
15
+ js: `.min.js`,
16
+ }
17
+ },
11
18
  }];