data-structure-typed 1.38.9 → 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.
- package/CHANGELOG.md +1 -1
- package/README.md +9 -9
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.d.ts +8 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +17 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +3 -3
- package/dist/cjs/data-structures/graph/abstract-graph.js +4 -4
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +4 -4
- package/dist/cjs/data-structures/graph/directed-graph.js +6 -6
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +3 -3
- package/dist/cjs/data-structures/graph/undirected-graph.js +4 -4
- package/dist/cjs/data-structures/heap/heap.d.ts +10 -5
- package/dist/cjs/data-structures/heap/heap.js +10 -10
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +4 -1
- package/dist/cjs/data-structures/heap/max-heap.js +9 -7
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +4 -1
- package/dist/cjs/data-structures/heap/min-heap.js +9 -7
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +4 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +9 -7
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +4 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +9 -7
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +5 -2
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.d.ts +8 -0
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +17 -0
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +3 -3
- package/dist/mjs/data-structures/graph/abstract-graph.js +4 -4
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +4 -4
- package/dist/mjs/data-structures/graph/directed-graph.js +6 -6
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +3 -3
- package/dist/mjs/data-structures/graph/undirected-graph.js +4 -4
- package/dist/mjs/data-structures/heap/heap.d.ts +10 -5
- package/dist/mjs/data-structures/heap/heap.js +10 -10
- package/dist/mjs/data-structures/heap/max-heap.d.ts +4 -1
- package/dist/mjs/data-structures/heap/max-heap.js +9 -7
- package/dist/mjs/data-structures/heap/min-heap.d.ts +4 -1
- package/dist/mjs/data-structures/heap/min-heap.js +9 -7
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +4 -1
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +9 -7
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +4 -1
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +9 -7
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +5 -2
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +5 -5
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +20 -0
- package/src/data-structures/graph/abstract-graph.ts +5 -5
- package/src/data-structures/graph/directed-graph.ts +6 -6
- package/src/data-structures/graph/undirected-graph.ts +4 -4
- package/src/data-structures/heap/heap.ts +12 -12
- package/src/data-structures/heap/max-heap.ts +8 -6
- package/src/data-structures/heap/min-heap.ts +8 -6
- package/src/data-structures/priority-queue/max-priority-queue.ts +8 -6
- package/src/data-structures/priority-queue/min-priority-queue.ts +8 -6
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/test/integration/bst.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/binary-index-tree.test.ts +35 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/bst.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/overall.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +1 -1
- package/test/unit/data-structures/graph/directed-graph.test.ts +3 -3
- package/test/unit/data-structures/graph/overall.test.ts +2 -2
- package/test/unit/data-structures/graph/undirected-graph.test.ts +2 -2
- package/test/unit/data-structures/heap/heap.test.ts +2 -2
- package/test/unit/data-structures/heap/max-heap.test.ts +1 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +1 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +2 -2
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +3 -3
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +10 -10
- package/test/utils/big-o.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.
|
|
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.
|
|
64
|
+
"avl-tree-typed": "^1.38.9",
|
|
65
65
|
"benchmark": "^2.1.4",
|
|
66
|
-
"binary-tree-typed": "^1.38.
|
|
67
|
-
"bst-typed": "^1.38.
|
|
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.
|
|
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.
|
|
@@ -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
|
|
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 `
|
|
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
|
-
|
|
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.
|
|
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
|
-
|
|
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 `
|
|
180
|
+
* @returns The method `deleteEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
|
|
181
181
|
*/
|
|
182
|
-
|
|
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
|
-
|
|
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.
|
|
214
|
-
const v2ToV1 = this.
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
156
|
-
return this.
|
|
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[]
|
|
40
|
-
|
|
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
|
|
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
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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(
|
|
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
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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(
|
|
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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(
|
|
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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(
|
|
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(
|
|
12
|
+
export class PriorityQueue<E = any> extends Heap<E> {
|
|
13
|
+
constructor(options: { comparator: Comparator<E>; nodes?: E[] }) {
|
|
14
|
+
super(options);
|
|
15
15
|
}
|
|
16
16
|
}
|
|
@@ -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<{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});
|
|
@@ -110,7 +110,7 @@ describe('AVL Tree Test', () => {
|
|
|
110
110
|
});
|
|
111
111
|
|
|
112
112
|
describe('AVLTree APIs test', () => {
|
|
113
|
-
const avl = new AVLTree<{id: number; text: string}>();
|
|
113
|
+
const avl = new AVLTree<{ id: number; text: string }>();
|
|
114
114
|
beforeEach(() => {
|
|
115
115
|
avl.clear();
|
|
116
116
|
});
|
|
@@ -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
|
+
});
|
|
@@ -200,7 +200,7 @@ describe('BinaryTree Morris Traversal', () => {
|
|
|
200
200
|
});
|
|
201
201
|
|
|
202
202
|
describe('BinaryTree APIs test', () => {
|
|
203
|
-
const avl = new AVLTree<{id: number; text: string}>();
|
|
203
|
+
const avl = new AVLTree<{ id: number; text: string }>();
|
|
204
204
|
beforeEach(() => {
|
|
205
205
|
avl.clear();
|
|
206
206
|
});
|
|
@@ -189,7 +189,7 @@ describe('BST operations test', () => {
|
|
|
189
189
|
});
|
|
190
190
|
|
|
191
191
|
it('should perform various operations on a Binary Search Tree with object values', () => {
|
|
192
|
-
const objBST = new BST<{key: number; keyA: number}>();
|
|
192
|
+
const objBST = new BST<{ key: number; keyA: number }>();
|
|
193
193
|
expect(objBST).toBeInstanceOf(BST);
|
|
194
194
|
objBST.add(11, {key: 11, keyA: 11});
|
|
195
195
|
objBST.add(3, {key: 3, keyA: 3});
|
|
@@ -260,7 +260,7 @@ describe('BST operations test', () => {
|
|
|
260
260
|
objBST.perfectlyBalance();
|
|
261
261
|
expect(objBST.isPerfectlyBalanced()).toBe(true);
|
|
262
262
|
|
|
263
|
-
const bfsNodesAfterBalanced: BSTNode<{key: number; keyA: number}>[] = [];
|
|
263
|
+
const bfsNodesAfterBalanced: BSTNode<{ key: number; keyA: number }>[] = [];
|
|
264
264
|
objBST.bfs(node => bfsNodesAfterBalanced.push(node));
|
|
265
265
|
expect(bfsNodesAfterBalanced[0].key).toBe(8);
|
|
266
266
|
expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16);
|
|
@@ -385,7 +385,7 @@ describe('BST operations test', () => {
|
|
|
385
385
|
expect(bfsIDs[1]).toBe(12);
|
|
386
386
|
expect(bfsIDs[2]).toBe(16);
|
|
387
387
|
|
|
388
|
-
const bfsNodes: BSTNode<{key: number; keyA: number}>[] = [];
|
|
388
|
+
const bfsNodes: BSTNode<{ key: number; keyA: number }>[] = [];
|
|
389
389
|
objBST.bfs(node => bfsNodes.push(node));
|
|
390
390
|
expect(bfsNodes[0].key).toBe(2);
|
|
391
391
|
expect(bfsNodes[1].key).toBe(12);
|
|
@@ -29,7 +29,7 @@ describe('Overall BinaryTree Test', () => {
|
|
|
29
29
|
bfsIDs[0] === 11; // true
|
|
30
30
|
expect(bfsIDs[0]).toBe(11);
|
|
31
31
|
|
|
32
|
-
const objBST = new BST<{key: number; keyA: number}>();
|
|
32
|
+
const objBST = new BST<{ key: number; keyA: number }>();
|
|
33
33
|
objBST.add(11, {key: 11, keyA: 11});
|
|
34
34
|
objBST.add(3, {key: 3, keyA: 3});
|
|
35
35
|
|
|
@@ -206,7 +206,7 @@ describe('TreeMultiset operations test', () => {
|
|
|
206
206
|
});
|
|
207
207
|
|
|
208
208
|
it('should perform various operations on a Binary Search Tree with object values', () => {
|
|
209
|
-
const objTreeMultiset = new TreeMultiset<{key: number; keyA: number}>();
|
|
209
|
+
const objTreeMultiset = new TreeMultiset<{ key: number; keyA: number }>();
|
|
210
210
|
expect(objTreeMultiset).toBeInstanceOf(TreeMultiset);
|
|
211
211
|
objTreeMultiset.add(11, {key: 11, keyA: 11});
|
|
212
212
|
objTreeMultiset.add(3, {key: 3, keyA: 3});
|
|
@@ -40,7 +40,7 @@ describe('DirectedGraph Operation Test', () => {
|
|
|
40
40
|
graph.addVertex(vertex2);
|
|
41
41
|
graph.addEdge(edge);
|
|
42
42
|
|
|
43
|
-
expect(graph.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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'});
|
|
@@ -3,7 +3,7 @@ import {bigO, magnitude} from '../../../utils';
|
|
|
3
3
|
|
|
4
4
|
describe('DoublyLinkedList Operation Test', () => {
|
|
5
5
|
let list: DoublyLinkedList<number>;
|
|
6
|
-
let objectList: DoublyLinkedList<{keyA: number}>;
|
|
6
|
+
let objectList: DoublyLinkedList<{ keyA: number }>;
|
|
7
7
|
|
|
8
8
|
beforeEach(() => {
|
|
9
9
|
list = new DoublyLinkedList();
|
|
@@ -3,10 +3,10 @@ import {bigO, magnitude} from '../../../utils';
|
|
|
3
3
|
|
|
4
4
|
describe('SinglyLinkedList Operation Test', () => {
|
|
5
5
|
let list: SinglyLinkedList<number>;
|
|
6
|
-
let objectList: SinglyLinkedList<{keyA: number}>;
|
|
6
|
+
let objectList: SinglyLinkedList<{ keyA: number }>;
|
|
7
7
|
beforeEach(() => {
|
|
8
8
|
list = new SinglyLinkedList<number>();
|
|
9
|
-
objectList = new SinglyLinkedList<{keyA: number}>();
|
|
9
|
+
objectList = new SinglyLinkedList<{ keyA: number }>();
|
|
10
10
|
});
|
|
11
11
|
|
|
12
12
|
describe('push', () => {
|
|
@@ -17,7 +17,7 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|
|
17
17
|
});
|
|
18
18
|
|
|
19
19
|
it('should add elements and maintain heap property in a object MaxPriorityQueue', () => {
|
|
20
|
-
const priorityQueue = new MaxPriorityQueue<{keyA: number}>((a, b) => b.keyA - a.keyA);
|
|
20
|
+
const priorityQueue = new MaxPriorityQueue<{ keyA: number }>({comparator: (a, b) => b.keyA - a.keyA});
|
|
21
21
|
priorityQueue.refill([{keyA: 5}, {keyA: 3}, {keyA: 1}]);
|
|
22
22
|
priorityQueue.add({keyA: 7});
|
|
23
23
|
|
|
@@ -53,7 +53,7 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|
|
53
53
|
|
|
54
54
|
it('should correctly heapify an array', () => {
|
|
55
55
|
const array = [5, 3, 7, 1];
|
|
56
|
-
const heap = MaxPriorityQueue.heapify<number>(array, (a, b) => b - a);
|
|
56
|
+
const heap = MaxPriorityQueue.heapify<number>({nodes: array, comparator: (a, b) => b - a});
|
|
57
57
|
heap.refill(array);
|
|
58
58
|
|
|
59
59
|
expect(heap.poll()).toBe(7);
|
|
@@ -64,7 +64,7 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|
|
64
64
|
|
|
65
65
|
it('should correctly heapify an object array', () => {
|
|
66
66
|
const nodes = [{keyA: 5}, {keyA: 3}, {keyA: 7}, {keyA: 1}];
|
|
67
|
-
const maxPQ = MaxPriorityQueue.heapify<{keyA: number}>(nodes, (a, b) => b.keyA - a.keyA);
|
|
67
|
+
const maxPQ = MaxPriorityQueue.heapify<{ keyA: number }>({nodes: nodes, comparator: (a, b) => b.keyA - a.keyA});
|
|
68
68
|
|
|
69
69
|
expect(maxPQ.poll()?.keyA).toBe(7);
|
|
70
70
|
expect(maxPQ.poll()?.keyA).toBe(5);
|