data-structure-typed 1.38.8 → 1.39.0

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 (88) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +9 -9
  3. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.d.ts +8 -0
  4. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +17 -0
  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.js.map +1 -1
  7. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  9. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +3 -3
  10. package/dist/cjs/data-structures/graph/abstract-graph.js +4 -4
  11. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  12. package/dist/cjs/data-structures/graph/directed-graph.d.ts +4 -4
  13. package/dist/cjs/data-structures/graph/directed-graph.js +6 -6
  14. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +3 -3
  15. package/dist/cjs/data-structures/graph/undirected-graph.js +4 -4
  16. package/dist/cjs/data-structures/heap/heap.d.ts +10 -5
  17. package/dist/cjs/data-structures/heap/heap.js +10 -10
  18. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  19. package/dist/cjs/data-structures/heap/max-heap.d.ts +4 -1
  20. package/dist/cjs/data-structures/heap/max-heap.js +9 -7
  21. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  22. package/dist/cjs/data-structures/heap/min-heap.d.ts +4 -1
  23. package/dist/cjs/data-structures/heap/min-heap.js +9 -7
  24. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  25. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +4 -1
  26. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +9 -7
  27. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  28. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +4 -1
  29. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +9 -7
  30. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  31. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +5 -2
  32. package/dist/cjs/data-structures/priority-queue/priority-queue.js +2 -2
  33. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  34. package/dist/cjs/data-structures/trie/trie.d.ts +2 -1
  35. package/dist/cjs/data-structures/trie/trie.js +3 -2
  36. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  37. package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.d.ts +8 -0
  38. package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +17 -0
  39. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +3 -3
  40. package/dist/mjs/data-structures/graph/abstract-graph.js +4 -4
  41. package/dist/mjs/data-structures/graph/directed-graph.d.ts +4 -4
  42. package/dist/mjs/data-structures/graph/directed-graph.js +6 -6
  43. package/dist/mjs/data-structures/graph/undirected-graph.d.ts +3 -3
  44. package/dist/mjs/data-structures/graph/undirected-graph.js +4 -4
  45. package/dist/mjs/data-structures/heap/heap.d.ts +10 -5
  46. package/dist/mjs/data-structures/heap/heap.js +10 -10
  47. package/dist/mjs/data-structures/heap/max-heap.d.ts +4 -1
  48. package/dist/mjs/data-structures/heap/max-heap.js +9 -7
  49. package/dist/mjs/data-structures/heap/min-heap.d.ts +4 -1
  50. package/dist/mjs/data-structures/heap/min-heap.js +9 -7
  51. package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +4 -1
  52. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +9 -7
  53. package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +4 -1
  54. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +9 -7
  55. package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +5 -2
  56. package/dist/mjs/data-structures/priority-queue/priority-queue.js +2 -2
  57. package/dist/mjs/data-structures/trie/trie.d.ts +2 -1
  58. package/dist/mjs/data-structures/trie/trie.js +3 -2
  59. package/dist/umd/data-structure-typed.min.js +1 -1
  60. package/dist/umd/data-structure-typed.min.js.map +1 -1
  61. package/package.json +5 -5
  62. package/src/data-structures/binary-tree/binary-indexed-tree.ts +20 -0
  63. package/src/data-structures/binary-tree/binary-tree.ts +2 -2
  64. package/src/data-structures/binary-tree/bst.ts +3 -1
  65. package/src/data-structures/binary-tree/rb-tree.ts +3 -1
  66. package/src/data-structures/graph/abstract-graph.ts +5 -5
  67. package/src/data-structures/graph/directed-graph.ts +6 -6
  68. package/src/data-structures/graph/undirected-graph.ts +4 -4
  69. package/src/data-structures/heap/heap.ts +12 -12
  70. package/src/data-structures/heap/max-heap.ts +8 -6
  71. package/src/data-structures/heap/min-heap.ts +8 -6
  72. package/src/data-structures/priority-queue/max-priority-queue.ts +8 -6
  73. package/src/data-structures/priority-queue/min-priority-queue.ts +8 -6
  74. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  75. package/src/data-structures/trie/trie.ts +4 -2
  76. package/test/unit/data-structures/binary-tree/binary-index-tree.test.ts +35 -0
  77. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +8 -5
  78. package/test/unit/data-structures/binary-tree/bst.test.ts +1 -1
  79. package/test/unit/data-structures/binary-tree/overall.test.ts +18 -18
  80. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +1 -1
  81. package/test/unit/data-structures/graph/directed-graph.test.ts +3 -3
  82. package/test/unit/data-structures/graph/overall.test.ts +2 -2
  83. package/test/unit/data-structures/graph/undirected-graph.test.ts +2 -2
  84. package/test/unit/data-structures/heap/heap.test.ts +2 -2
  85. package/test/unit/data-structures/heap/max-heap.test.ts +1 -1
  86. package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
  87. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +3 -3
  88. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +10 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.38.8",
3
+ "version": "1.39.0",
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.38.7",
64
+ "avl-tree-typed": "^1.38.9",
65
65
  "benchmark": "^2.1.4",
66
- "binary-tree-typed": "^1.38.7",
67
- "bst-typed": "^1.38.7",
66
+ "binary-tree-typed": "^1.38.9",
67
+ "bst-typed": "^1.38.9",
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.7",
74
+ "heap-typed": "^1.38.9",
75
75
  "istanbul-badges-readme": "^1.8.5",
76
76
  "jest": "^29.7.0",
77
77
  "prettier": "^3.0.3",
@@ -143,6 +143,26 @@ export class BinaryIndexedTree {
143
143
  return this._binarySearch(sum, (x, y) => x <= y);
144
144
  }
145
145
 
146
+ /**
147
+ * The function calculates the prefix sum of an array using a binary indexed tree.
148
+ * @param {number} i - The parameter "i" in the function "getPrefixSum" represents the index of the element in the
149
+ * array for which we want to calculate the prefix sum.
150
+ * @returns The function `getPrefixSum` returns the prefix sum of the elements in the binary indexed tree up to index
151
+ * `i`.
152
+ */
153
+ getPrefixSum(i: number): number {
154
+ this._checkIndex(i);
155
+ i++; // Convert to 1-based index
156
+
157
+ let sum = 0;
158
+ while (i > 0) {
159
+ sum += this._getFrequency(i);
160
+ i -= i & -i;
161
+ }
162
+
163
+ return sum;
164
+ }
165
+
146
166
  /**
147
167
  * The function returns the value of a specific index in a freqMap data structure, or a default value if
148
168
  * the index is not found.
@@ -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<V, BinaryTreeNodeNested<V>>> 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.
@@ -974,7 +975,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
974
975
  if (current.left) queue.push(current.left);
975
976
  if (current.right) queue.push(current.right);
976
977
  }
977
-
978
978
  }
979
979
  }
980
980
  return ans;
@@ -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<V, BSTNodeNested<V>>> 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<V, RBTreeNodeNested<V>>> 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
  }
@@ -130,7 +130,7 @@ export abstract class AbstractGraph<
130
130
  */
131
131
  abstract createEdge(srcOrV1: VertexKey | string, destOrV2: VertexKey | string, weight?: number, val?: E): E;
132
132
 
133
- abstract removeEdge(edge: E): E | null;
133
+ abstract deleteEdge(edge: E): E | null;
134
134
 
135
135
  abstract getEdge(srcOrKey: V | VertexKey, destOrKey: V | VertexKey): E | null;
136
136
 
@@ -179,12 +179,12 @@ export abstract class AbstractGraph<
179
179
  }
180
180
 
181
181
  /**
182
- * The `removeVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
182
+ * The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
183
183
  * @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`V`) or a vertex ID
184
184
  * (`VertexKey`).
185
185
  * @returns The method is returning a boolean value.
186
186
  */
187
- removeVertex(vertexOrKey: V | VertexKey): boolean {
187
+ deleteVertex(vertexOrKey: V | VertexKey): boolean {
188
188
  const vertexKey = this._getVertexKey(vertexOrKey);
189
189
  return this._vertices.delete(vertexKey);
190
190
  }
@@ -199,7 +199,7 @@ export abstract class AbstractGraph<
199
199
  removeAllVertices(vertices: V[] | VertexKey[]): boolean {
200
200
  const removed: boolean[] = [];
201
201
  for (const v of vertices) {
202
- removed.push(this.removeVertex(v));
202
+ removed.push(this.deleteVertex(v));
203
203
  }
204
204
  return removed.length > 0;
205
205
  }
@@ -622,7 +622,7 @@ export abstract class AbstractGraph<
622
622
  if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
623
623
  }
624
624
 
625
- const heap = new PriorityQueue<{ key: number; val: V }>((a, b) => a.key - b.key);
625
+ const heap = new PriorityQueue<{ key: number; val: V }>({comparator: (a, b) => a.key - b.key});
626
626
  heap.add({key: 0, val: srcVertex});
627
627
 
628
628
  distMap.set(srcVertex, 0);
@@ -153,7 +153,7 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
153
153
  * @param {V | VertexKey} destOrKey - The `destOrKey` parameter represents the destination vertex or its ID.
154
154
  * @returns the removed edge (E) if it exists, or null if either the source or destination vertex does not exist.
155
155
  */
156
- removeEdgeSrcToDest(srcOrKey: V | VertexKey, destOrKey: V | VertexKey): E | null {
156
+ deleteEdgeSrcToDest(srcOrKey: V | VertexKey, destOrKey: V | VertexKey): E | null {
157
157
  const src: V | null = this._getVertex(srcOrKey);
158
158
  const dest: V | null = this._getVertex(destOrKey);
159
159
  let removed: E | null = null;
@@ -177,9 +177,9 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
177
177
  * The function removes an edge from a graph and returns the removed edge, or null if the edge was not found.
178
178
  * @param {E} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
179
179
  * and `dest`, which represent the source and destination vertices of the edge, respectively.
180
- * @returns The method `removeEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
180
+ * @returns The method `deleteEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
181
181
  */
182
- removeEdge(edge: E): E | null {
182
+ deleteEdge(edge: E): E | null {
183
183
  let removed: E | null = null;
184
184
  const src = this._getVertex(edge.src);
185
185
  const dest = this._getVertex(edge.dest);
@@ -206,12 +206,12 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
206
206
  * the second vertex in the edge that needs to be removed.
207
207
  * @returns an array of removed edges (E[]).
208
208
  */
209
- removeEdgesBetween(v1: VertexKey | V, v2: VertexKey | V): E[] {
209
+ deleteEdgesBetween(v1: VertexKey | V, v2: VertexKey | V): E[] {
210
210
  const removed: E[] = [];
211
211
 
212
212
  if (v1 && v2) {
213
- const v1ToV2 = this.removeEdgeSrcToDest(v1, v2);
214
- const v2ToV1 = this.removeEdgeSrcToDest(v2, v1);
213
+ const v1ToV2 = this.deleteEdgeSrcToDest(v1, v2);
214
+ const v2ToV1 = this.deleteEdgeSrcToDest(v2, v1);
215
215
 
216
216
  v1ToV2 && removed.push(v1ToV2);
217
217
  v2ToV1 && removed.push(v2ToV1);
@@ -127,7 +127,7 @@ export class UndirectedGraph<
127
127
  * (VertexKey). It represents the second vertex of the edge that needs to be removed.
128
128
  * @returns the removed edge (E) if it exists, or null if either of the vertices (V) does not exist.
129
129
  */
130
- removeEdgeBetween(v1: V | VertexKey, v2: V | VertexKey): E | null {
130
+ deleteEdgeBetween(v1: V | VertexKey, v2: V | VertexKey): E | null {
131
131
  const vertex1: V | null = this._getVertex(v1);
132
132
  const vertex2: V | null = this._getVertex(v2);
133
133
 
@@ -148,12 +148,12 @@ export class UndirectedGraph<
148
148
  }
149
149
 
150
150
  /**
151
- * The removeEdge function removes an edge between two vertices in a graph.
151
+ * The deleteEdge function removes an edge between two vertices in a graph.
152
152
  * @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
153
153
  * @returns The method is returning either the removed edge (of type E) or null if the edge was not found.
154
154
  */
155
- removeEdge(edge: E): E | null {
156
- return this.removeEdgeBetween(edge.vertices[0], edge.vertices[1]);
155
+ deleteEdge(edge: E): E | null {
156
+ return this.deleteEdgeBetween(edge.vertices[0], edge.vertices[1]);
157
157
  }
158
158
 
159
159
  /**
@@ -7,12 +7,16 @@
7
7
 
8
8
  import type {Comparator, DFSOrderPattern} from '../../types';
9
9
 
10
- export class Heap<E> {
10
+ export class Heap<E = any> {
11
11
  protected nodes: E[] = [];
12
12
  protected readonly comparator: Comparator<E>;
13
13
 
14
- constructor(comparator: Comparator<E>) {
15
- this.comparator = comparator;
14
+ constructor(options: { comparator: Comparator<E>; nodes?: E[] }) {
15
+ this.comparator = options.comparator;
16
+ if (options.nodes && options.nodes.length > 0) {
17
+ this.nodes = options.nodes;
18
+ this.fix();
19
+ }
16
20
  }
17
21
 
18
22
  /**
@@ -32,15 +36,11 @@ export class Heap<E> {
32
36
 
33
37
  /**
34
38
  * Static method that creates a binary heap from an array of nodes and a comparison function.
35
- * @param nodes
36
- * @param comparator - Comparison function.
37
39
  * @returns A new Heap instance.
40
+ * @param options
38
41
  */
39
- static heapify<E>(nodes: E[], comparator: Comparator<E>): Heap<E> {
40
- const binaryHeap = new Heap<E>(comparator);
41
- binaryHeap.nodes = [...nodes];
42
- binaryHeap.fix(); // Fix heap properties
43
- return binaryHeap;
42
+ static heapify<E>(options: { nodes: E[]; comparator: Comparator<E> }): Heap<E> {
43
+ return new Heap<E>(options);
44
44
  }
45
45
 
46
46
  /**
@@ -180,7 +180,7 @@ export class Heap<E> {
180
180
  * @returns A new Heap instance containing the same elements.
181
181
  */
182
182
  clone(): Heap<E> {
183
- const clonedHeap = new Heap<E>(this.comparator);
183
+ const clonedHeap = new Heap<E>({comparator: this.comparator});
184
184
  clonedHeap.nodes = [...this.nodes];
185
185
  return clonedHeap;
186
186
  }
@@ -269,7 +269,7 @@ export class FibonacciHeapNode<E> {
269
269
 
270
270
  export class FibonacciHeap<E> {
271
271
  root?: FibonacciHeapNode<E>;
272
- size: number = 0;
272
+ size = 0;
273
273
  protected min?: FibonacciHeapNode<E>;
274
274
  protected readonly comparator: Comparator<E>;
275
275
 
@@ -11,14 +11,16 @@ import type {Comparator} from '../../types';
11
11
 
12
12
  export class MaxHeap<E = any> extends Heap<E> {
13
13
  constructor(
14
- comparator: Comparator<E> = (a: E, b: E) => {
15
- if (!(typeof a === 'number' && typeof b === 'number')) {
16
- throw new Error('The a, b params of compare function must be number');
17
- } else {
18
- return b - a;
14
+ options: { comparator: Comparator<E>; nodes?: E[] } = {
15
+ comparator: (a: E, b: E) => {
16
+ if (!(typeof a === 'number' && typeof b === 'number')) {
17
+ throw new Error('The a, b params of compare function must be number');
18
+ } else {
19
+ return b - a;
20
+ }
19
21
  }
20
22
  }
21
23
  ) {
22
- super(comparator);
24
+ super(options);
23
25
  }
24
26
  }
@@ -11,14 +11,16 @@ import type {Comparator} from '../../types';
11
11
 
12
12
  export class MinHeap<E = any> extends Heap<E> {
13
13
  constructor(
14
- comparator: Comparator<E> = (a: E, b: E) => {
15
- if (!(typeof a === 'number' && typeof b === 'number')) {
16
- throw new Error('The a, b params of compare function must be number');
17
- } else {
18
- return a - b;
14
+ options: { comparator: Comparator<E>; nodes?: E[] } = {
15
+ comparator: (a: E, b: E) => {
16
+ if (!(typeof a === 'number' && typeof b === 'number')) {
17
+ throw new Error('The a, b params of compare function must be number');
18
+ } else {
19
+ return a - b;
20
+ }
19
21
  }
20
22
  }
21
23
  ) {
22
- super(comparator);
24
+ super(options);
23
25
  }
24
26
  }
@@ -10,14 +10,16 @@ import type {Comparator} from '../../types';
10
10
 
11
11
  export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
12
12
  constructor(
13
- compare: Comparator<E> = (a: E, b: E) => {
14
- if (!(typeof a === 'number' && typeof b === 'number')) {
15
- throw new Error('The a, b params of compare function must be number');
16
- } else {
17
- return b - a;
13
+ options: { comparator: Comparator<E>; nodes?: E[] } = {
14
+ comparator: (a: E, b: E) => {
15
+ if (!(typeof a === 'number' && typeof b === 'number')) {
16
+ throw new Error('The a, b params of compare function must be number');
17
+ } else {
18
+ return b - a;
19
+ }
18
20
  }
19
21
  }
20
22
  ) {
21
- super(compare);
23
+ super(options);
22
24
  }
23
25
  }
@@ -10,14 +10,16 @@ import type {Comparator} from '../../types';
10
10
 
11
11
  export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
12
12
  constructor(
13
- compare: Comparator<E> = (a: E, b: E) => {
14
- if (!(typeof a === 'number' && typeof b === 'number')) {
15
- throw new Error('The a, b params of compare function must be number');
16
- } else {
17
- return a - b;
13
+ options: { comparator: Comparator<E>; nodes?: E[] } = {
14
+ comparator: (a: E, b: E) => {
15
+ if (!(typeof a === 'number' && typeof b === 'number')) {
16
+ throw new Error('The a, b params of compare function must be number');
17
+ } else {
18
+ return a - b;
19
+ }
18
20
  }
19
21
  }
20
22
  ) {
21
- super(compare);
23
+ super(options);
22
24
  }
23
25
  }
@@ -9,8 +9,8 @@
9
9
  import {Heap} from '../heap';
10
10
  import {Comparator} from '../../types';
11
11
 
12
- export class PriorityQueue<E> extends Heap<E> {
13
- constructor(comparator: Comparator<E>) {
14
- super(comparator);
12
+ export class PriorityQueue<E = any> extends Heap<E> {
13
+ constructor(options: { comparator: Comparator<E>; nodes?: E[] }) {
14
+ super(options);
15
15
  }
16
16
  }
@@ -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
  }
@@ -283,3 +283,38 @@ function loopLowerBoundTests(bit: BinaryIndexedTree, values: number[]) {
283
283
  }
284
284
  }
285
285
  }
286
+
287
+ describe('', () => {
288
+ class NumArrayDC {
289
+ private _tree: BinaryIndexedTree;
290
+ private readonly _nums: number[];
291
+
292
+ constructor(nums: number[]) {
293
+ this._nums = nums;
294
+ this._tree = new BinaryIndexedTree({max: nums.length + 1});
295
+ for (let i = 0; i < nums.length; i++) {
296
+ this._tree.update(i + 1, nums[i]);
297
+ }
298
+ }
299
+
300
+ update(index: number, val: number): void {
301
+ this._tree.update(index + 1, val - this._nums[index]);
302
+ this._nums[index] = val;
303
+ }
304
+
305
+ sumRange(left: number, right: number): number {
306
+ return this._tree.getPrefixSum(right + 1) - this._tree.getPrefixSum(left);
307
+ }
308
+ }
309
+
310
+ it('', () => {
311
+ const numArray = new NumArrayDC([1, 3, 5, 8, 2, 9, 4, 5, 8, 1, 3, 2]);
312
+ expect(numArray.sumRange(0, 8)).toBe(45);
313
+ expect(numArray.sumRange(0, 2)).toBe(9);
314
+ numArray.update(1, 2);
315
+ expect(numArray.sumRange(0, 2)).toBe(8);
316
+ expect(numArray.sumRange(3, 4)).toBe(10);
317
+ numArray.update(3, 2);
318
+ expect(numArray.sumRange(3, 4)).toBe(4);
319
+ });
320
+ });
@@ -1,5 +1,5 @@
1
1
  import {AVLTree, AVLTreeNode, BinaryTree, BinaryTreeNode, IterationType} from '../../../../src';
2
- import {isDebugTest} from "../../../config";
2
+ import {isDebugTest} from '../../../config';
3
3
 
4
4
  const isDebug = isDebugTest;
5
5
  // const isDebug = true;
@@ -226,11 +226,14 @@ describe('BinaryTree traversals', () => {
226
226
  expect(tree.dfs(node => node.key, 'pre')).toEqual([35, 20, 15, 16, 29, 28, 30, 40, 50, 45, 55]);
227
227
  expect(tree.dfs(node => node.key, 'in')).toEqual([15, 16, 20, 28, 29, 30, 35, 40, 45, 50, 55]);
228
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([35, 20, 40, 15, 29, 50, 16, 28, 30, 45, 55]);
230
- expect(tree.bfs(node => node.key, tree.root, IterationType.ITERATIVE)).toEqual([35, 20, 40, 15, 29, 50, 16, 28, 30, 45, 55]);
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
+ ]);
231
235
 
232
236
  const levels = tree.listLevels(node => node.key);
233
237
  expect(levels).toEqual([[35], [20, 40], [15, 29, 50], [16, 28, 30, 45, 55]]);
234
238
  isDebug && console.log(levels);
235
-
236
- })
239
+ });
@@ -435,7 +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[][] = bst.listLevels((node) => node.key);
438
+ const arr: number[][] = bst.listLevels(node => node.key);
439
439
  isDebug && console.log('---listLevels', arr);
440
440
  isDebug && console.log('---listLevels', performance.now() - startL);
441
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) => (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
  });
@@ -40,7 +40,7 @@ describe('DirectedGraph Operation Test', () => {
40
40
  graph.addVertex(vertex2);
41
41
  graph.addEdge(edge);
42
42
 
43
- expect(graph.removeEdge(edge)).toBe(edge);
43
+ expect(graph.deleteEdge(edge)).toBe(edge);
44
44
  expect(graph.hasEdge('A', 'B')).toBe(false);
45
45
  });
46
46
 
@@ -164,7 +164,7 @@ describe('Inherit from DirectedGraph and perform operations', () => {
164
164
  myGraph.addVertex(2, 'data2');
165
165
  myGraph.addEdge(1, 2, 10, 'edge-data1-2');
166
166
 
167
- const removedEdge = myGraph.removeEdgeSrcToDest(1, 2);
167
+ const removedEdge = myGraph.deleteEdgeSrcToDest(1, 2);
168
168
  const edgeAfterRemoval = myGraph.getEdge(1, 2);
169
169
 
170
170
  expect(removedEdge).toBeInstanceOf(MyEdge);
@@ -233,7 +233,7 @@ describe('Inherit from DirectedGraph and perform operations test2.', () => {
233
233
  expect(myGraph.getEdge(2, 1)).toBeTruthy();
234
234
  expect(myGraph.getEdge(1, '100')).toBeFalsy();
235
235
 
236
- myGraph.removeEdgeSrcToDest(1, 2);
236
+ myGraph.deleteEdgeSrcToDest(1, 2);
237
237
  expect(myGraph.getEdge(1, 2)).toBeFalsy();
238
238
 
239
239
  myGraph.addEdge(3, 1, 3, 'edge-data-3-1');
@@ -20,7 +20,7 @@ describe('Overall Graph Operation Test', () => {
20
20
  expect(graph.hasEdge('A', 'B')).toBe(true); // true
21
21
  expect(graph.hasEdge('B', 'A')).toBe(false); // false
22
22
 
23
- graph.removeEdgeSrcToDest('A', 'B');
23
+ graph.deleteEdgeSrcToDest('A', 'B');
24
24
  graph.hasEdge('A', 'B'); // false
25
25
  expect(graph.hasEdge('A', 'B')).toBe(false); // false
26
26
 
@@ -38,7 +38,7 @@ describe('Overall Graph Operation Test', () => {
38
38
  graph.addVertex('B');
39
39
  graph.addVertex('C');
40
40
  graph.addVertex('D');
41
- graph.removeVertex('C');
41
+ graph.deleteVertex('C');
42
42
  graph.addEdge('A', 'B');
43
43
  graph.addEdge('B', 'D');
44
44
 
@@ -40,7 +40,7 @@ describe('UndirectedGraph Operation Test', () => {
40
40
  graph.addVertex(vertex2);
41
41
  graph.addEdge(edge);
42
42
 
43
- expect(graph.removeEdge(edge)).toBe(edge);
43
+ expect(graph.deleteEdge(edge)).toBe(edge);
44
44
  expect(graph.hasEdge('A', 'B')).toBe(false);
45
45
  });
46
46
 
@@ -49,7 +49,7 @@ describe('UndirectedGraph Operation Test', () => {
49
49
  graph.addVertex('B');
50
50
  graph.addVertex('C');
51
51
  graph.addVertex('D');
52
- graph.removeVertex('C');
52
+ graph.deleteVertex('C');
53
53
  graph.addEdge('A', 'B');
54
54
  graph.addEdge('B', 'D');
55
55
 
@@ -22,7 +22,7 @@ describe('Heap Operation Test', () => {
22
22
  });
23
23
 
24
24
  it('should object heap work well', function () {
25
- const minHeap = new MinHeap<{ a: string; key: number }>((a, b) => a.key - b.key);
25
+ const minHeap = new MinHeap<{ a: string; key: number }>({comparator: (a, b) => a.key - b.key});
26
26
  minHeap.add({key: 1, a: 'a1'});
27
27
  minHeap.add({key: 6, a: 'a6'});
28
28
  minHeap.add({key: 2, a: 'a2'});
@@ -37,7 +37,7 @@ describe('Heap Operation Test', () => {
37
37
  i++;
38
38
  }
39
39
 
40
- const maxHeap = new MaxHeap<{ key: number; a: string }>((a, b) => b.key - a.key);
40
+ const maxHeap = new MaxHeap<{ key: number; a: string }>({comparator: (a, b) => b.key - a.key});
41
41
  maxHeap.add({key: 1, a: 'a1'});
42
42
  maxHeap.add({key: 6, a: 'a6'});
43
43
  maxHeap.add({key: 5, a: 'a5'});
@@ -5,7 +5,7 @@ describe('MaxHeap', () => {
5
5
  let maxHeap: MaxHeap<number>;
6
6
 
7
7
  beforeEach(() => {
8
- maxHeap = new MaxHeap(numberComparator);
8
+ maxHeap = new MaxHeap({comparator: numberComparator});
9
9
  });
10
10
 
11
11
  test('add and poll elements in descending order', () => {
@@ -5,7 +5,7 @@ describe('MinHeap', () => {
5
5
  let minHeap: MinHeap<number>;
6
6
 
7
7
  beforeEach(() => {
8
- minHeap = new MinHeap(numberComparator);
8
+ minHeap = new MinHeap({comparator: numberComparator});
9
9
  });
10
10
 
11
11
  test('add and poll elements in ascending order', () => {