data-structure-typed 1.48.0 → 1.48.2
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 +24 -18
- package/benchmark/report.html +16 -16
- package/benchmark/report.json +188 -308
- package/dist/cjs/data-structures/base/index.d.ts +1 -0
- package/dist/cjs/data-structures/base/index.js +18 -0
- package/dist/cjs/data-structures/base/index.js.map +1 -0
- package/dist/cjs/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/cjs/data-structures/base/iterable-base.js +313 -0
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +45 -40
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +91 -88
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +12 -0
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +16 -0
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +44 -6
- package/dist/cjs/data-structures/graph/abstract-graph.js +50 -27
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +160 -44
- package/dist/cjs/data-structures/hash/hash-map.js +314 -82
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +50 -7
- package/dist/cjs/data-structures/heap/heap.js +60 -30
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/index.d.ts +1 -0
- package/dist/cjs/data-structures/index.js +1 -0
- package/dist/cjs/data-structures/index.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +46 -73
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +40 -73
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/cjs/data-structures/matrix/navigator.js.map +1 -1
- package/dist/cjs/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +29 -51
- package/dist/cjs/data-structures/queue/deque.js +36 -71
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +49 -48
- package/dist/cjs/data-structures/queue/queue.js +69 -82
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +43 -10
- package/dist/cjs/data-structures/stack/stack.js +50 -31
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/tree/tree.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +41 -6
- package/dist/cjs/data-structures/trie/trie.js +53 -32
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/base/base.d.ts +5 -0
- package/dist/cjs/types/data-structures/base/base.js +3 -0
- package/dist/cjs/types/data-structures/base/base.js.map +1 -0
- package/dist/cjs/types/data-structures/base/index.d.ts +1 -0
- package/dist/cjs/types/data-structures/base/index.js +18 -0
- package/dist/cjs/types/data-structures/base/index.js.map +1 -0
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -0
- package/dist/cjs/types/data-structures/index.d.ts +1 -0
- package/dist/cjs/types/data-structures/index.js +1 -0
- package/dist/cjs/types/data-structures/index.js.map +1 -1
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/base/index.d.ts +1 -0
- package/dist/mjs/data-structures/base/index.js +1 -0
- package/dist/mjs/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/mjs/data-structures/base/iterable-base.js +307 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +45 -40
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +91 -88
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +12 -0
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +16 -0
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +44 -6
- package/dist/mjs/data-structures/graph/abstract-graph.js +52 -27
- package/dist/mjs/data-structures/hash/hash-map.d.ts +160 -44
- package/dist/mjs/data-structures/hash/hash-map.js +310 -80
- package/dist/mjs/data-structures/heap/heap.d.ts +50 -7
- package/dist/mjs/data-structures/heap/heap.js +60 -30
- package/dist/mjs/data-structures/index.d.ts +1 -0
- package/dist/mjs/data-structures/index.js +1 -0
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +46 -73
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +40 -73
- package/dist/mjs/data-structures/queue/deque.d.ts +29 -51
- package/dist/mjs/data-structures/queue/deque.js +36 -71
- package/dist/mjs/data-structures/queue/queue.d.ts +49 -48
- package/dist/mjs/data-structures/queue/queue.js +66 -79
- package/dist/mjs/data-structures/stack/stack.d.ts +43 -10
- package/dist/mjs/data-structures/stack/stack.js +50 -31
- package/dist/mjs/data-structures/trie/trie.d.ts +41 -6
- package/dist/mjs/data-structures/trie/trie.js +53 -32
- package/dist/mjs/types/data-structures/base/base.d.ts +5 -0
- package/dist/mjs/types/data-structures/base/base.js +1 -0
- package/dist/mjs/types/data-structures/base/index.d.ts +1 -0
- package/dist/mjs/types/data-structures/base/index.js +1 -0
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -0
- package/dist/mjs/types/data-structures/index.d.ts +1 -0
- package/dist/mjs/types/data-structures/index.js +1 -0
- package/dist/umd/data-structure-typed.js +1104 -575
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-base.ts +329 -0
- package/src/data-structures/binary-tree/binary-tree.ts +98 -93
- package/src/data-structures/binary-tree/tree-multimap.ts +18 -0
- package/src/data-structures/graph/abstract-graph.ts +55 -28
- package/src/data-structures/hash/hash-map.ts +334 -83
- package/src/data-structures/heap/heap.ts +63 -36
- package/src/data-structures/index.ts +1 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
- package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
- package/src/data-structures/queue/deque.ts +40 -82
- package/src/data-structures/queue/queue.ts +72 -87
- package/src/data-structures/stack/stack.ts +53 -34
- package/src/data-structures/trie/trie.ts +58 -35
- package/src/types/data-structures/base/base.ts +6 -0
- package/src/types/data-structures/base/index.ts +1 -0
- package/src/types/data-structures/hash/hash-map.ts +2 -0
- package/src/types/data-structures/index.ts +1 -0
- package/test/integration/avl-tree.test.ts +2 -2
- package/test/integration/bst.test.ts +21 -25
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +17 -12
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +24 -8
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +24 -9
- package/test/unit/data-structures/binary-tree/bst.test.ts +24 -8
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +8 -8
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +82 -1
- package/test/unit/data-structures/graph/directed-graph.test.ts +4 -4
- package/test/unit/data-structures/hash/hash-map.test.ts +312 -18
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +28 -0
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +25 -0
- package/test/unit/data-structures/queue/deque.test.ts +25 -0
|
@@ -2,10 +2,10 @@ import { AVLTree, CP } from 'avl-tree-typed';
|
|
|
2
2
|
|
|
3
3
|
describe('AVL Tree Test', () => {
|
|
4
4
|
it('should perform various operations on a AVL Tree', () => {
|
|
5
|
-
const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
|
5
|
+
const arr: [number, number][] = [[11, 11], [3, 3], [15, 15], [1, 1], [8, 8], [13, 13], [16, 16], [2, 2], [6, 6], [9, 9], [12, 12], [14, 14], [4, 4], [7, 7], [10, 10], [5, 5]];
|
|
6
6
|
const tree = new AVLTree();
|
|
7
7
|
|
|
8
|
-
for (const i of arr) tree.add(i
|
|
8
|
+
for (const i of arr) tree.add(i);
|
|
9
9
|
|
|
10
10
|
const node6 = tree.get(6);
|
|
11
11
|
|
|
@@ -4,10 +4,10 @@ describe('Individual package BST operations test', () => {
|
|
|
4
4
|
it('should perform various operations on a Binary Search Tree with numeric values', () => {
|
|
5
5
|
const bst = new BST();
|
|
6
6
|
expect(bst).toBeInstanceOf(BST);
|
|
7
|
-
bst.add(11, 11);
|
|
8
|
-
bst.add(3, 3);
|
|
7
|
+
bst.add([11, 11]);
|
|
8
|
+
bst.add([3, 3]);
|
|
9
9
|
const idsOrValues = [15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
|
10
|
-
bst.addMany(idsOrValues,
|
|
10
|
+
bst.addMany(idsOrValues, false);
|
|
11
11
|
expect(bst.root).toBeInstanceOf(BSTNode);
|
|
12
12
|
|
|
13
13
|
if (bst.root) expect(bst.root.key).toBe(11);
|
|
@@ -185,30 +185,26 @@ describe('Individual package BST operations test', () => {
|
|
|
185
185
|
it('should perform various operations on a Binary Search Tree with object values', () => {
|
|
186
186
|
const objBST = new BST<{ key: number; keyA: number }>();
|
|
187
187
|
expect(objBST).toBeInstanceOf(BST);
|
|
188
|
-
objBST.add(11, { key: 11, keyA: 11 });
|
|
189
|
-
objBST.add(3, { key: 3, keyA: 3 });
|
|
190
|
-
const values = [
|
|
191
|
-
{ key: 15, keyA: 15 },
|
|
192
|
-
{ key: 1, keyA: 1 },
|
|
193
|
-
{ key: 8, keyA: 8 },
|
|
194
|
-
{ key: 13, keyA: 13 },
|
|
195
|
-
{ key: 16, keyA: 16 },
|
|
196
|
-
{ key: 2, keyA: 2 },
|
|
197
|
-
{ key: 6, keyA: 6 },
|
|
198
|
-
{ key: 9, keyA: 9 },
|
|
199
|
-
{ key: 12, keyA: 12 },
|
|
200
|
-
{ key: 14, keyA: 14 },
|
|
201
|
-
{ key: 4, keyA: 4 },
|
|
202
|
-
{ key: 7, keyA: 7 },
|
|
203
|
-
{ key: 10, keyA: 10 },
|
|
204
|
-
{ key: 5, keyA: 5 }
|
|
188
|
+
objBST.add([11, { key: 11, keyA: 11 }]);
|
|
189
|
+
objBST.add([3, { key: 3, keyA: 3 }]);
|
|
190
|
+
const values: [number, { key: number; keyA: number }][] = [
|
|
191
|
+
[15, { key: 15, keyA: 15 }],
|
|
192
|
+
[1, { key: 1, keyA: 1 }],
|
|
193
|
+
[8, { key: 8, keyA: 8 }],
|
|
194
|
+
[13, { key: 13, keyA: 13 }],
|
|
195
|
+
[16, { key: 16, keyA: 16 }],
|
|
196
|
+
[2, { key: 2, keyA: 2 }],
|
|
197
|
+
[6, { key: 6, keyA: 6 }],
|
|
198
|
+
[9, { key: 9, keyA: 9 }],
|
|
199
|
+
[12, { key: 12, keyA: 12 }],
|
|
200
|
+
[14, { key: 14, keyA: 14 }],
|
|
201
|
+
[4, { key: 4, keyA: 4 }],
|
|
202
|
+
[7, { key: 7, keyA: 7 }],
|
|
203
|
+
[10, { key: 10, keyA: 10 }],
|
|
204
|
+
[5, { key: 5, keyA: 5 }]
|
|
205
205
|
];
|
|
206
206
|
|
|
207
|
-
objBST.addMany(
|
|
208
|
-
values.map(item => item.key),
|
|
209
|
-
values,
|
|
210
|
-
false
|
|
211
|
-
);
|
|
207
|
+
objBST.addMany(values, false);
|
|
212
208
|
|
|
213
209
|
expect(objBST.root).toBeInstanceOf(BSTNode);
|
|
214
210
|
|
|
@@ -4,17 +4,17 @@ import { getRandomIntArray, magnitude } from '../../../utils';
|
|
|
4
4
|
|
|
5
5
|
const suite = new Benchmark.Suite();
|
|
6
6
|
const biTree = new BinaryTree<number>();
|
|
7
|
-
const {
|
|
8
|
-
const arr = getRandomIntArray(
|
|
7
|
+
const { THOUSAND } = magnitude;
|
|
8
|
+
const arr = getRandomIntArray(THOUSAND, 0, THOUSAND, true);
|
|
9
9
|
|
|
10
10
|
suite
|
|
11
|
-
.add(`${
|
|
11
|
+
.add(`${THOUSAND.toLocaleString()} add randomly`, () => {
|
|
12
12
|
biTree.clear();
|
|
13
13
|
for (let i = 0; i < arr.length; i++) {
|
|
14
14
|
biTree.add(arr[i]);
|
|
15
15
|
}
|
|
16
16
|
})
|
|
17
|
-
.add(`${
|
|
17
|
+
.add(`${THOUSAND.toLocaleString()} add & delete randomly`, () => {
|
|
18
18
|
biTree.clear();
|
|
19
19
|
for (let i = 0; i < arr.length; i++) {
|
|
20
20
|
biTree.add(arr[i]);
|
|
@@ -23,23 +23,28 @@ suite
|
|
|
23
23
|
biTree.delete(arr[i]);
|
|
24
24
|
}
|
|
25
25
|
})
|
|
26
|
-
.add(`${
|
|
26
|
+
.add(`${THOUSAND.toLocaleString()} addMany`, () => {
|
|
27
27
|
biTree.clear();
|
|
28
28
|
biTree.addMany(arr);
|
|
29
29
|
})
|
|
30
|
-
.add(`${
|
|
30
|
+
.add(`${THOUSAND.toLocaleString()} get`, () => {
|
|
31
31
|
for (let i = 0; i < arr.length; i++) {
|
|
32
32
|
biTree.get(arr[i]);
|
|
33
33
|
}
|
|
34
34
|
})
|
|
35
|
-
.add(`${
|
|
36
|
-
for (let i = 0; i <
|
|
35
|
+
.add(`${THOUSAND.toLocaleString()} has`, () => {
|
|
36
|
+
for (let i = 0; i < arr.length; i++) {
|
|
37
|
+
biTree.get(arr[i]);
|
|
38
|
+
}
|
|
39
|
+
})
|
|
40
|
+
.add(`${THOUSAND.toLocaleString()} dfs`, () => {
|
|
41
|
+
for (let i = 0; i < THOUSAND; i++) biTree.dfs();
|
|
37
42
|
})
|
|
38
|
-
.add(`${
|
|
39
|
-
for (let i = 0; i <
|
|
43
|
+
.add(`${THOUSAND.toLocaleString()} bfs`, () => {
|
|
44
|
+
for (let i = 0; i < THOUSAND; i++) biTree.bfs();
|
|
40
45
|
})
|
|
41
|
-
.add(`${
|
|
42
|
-
for (let i = 0; i <
|
|
46
|
+
.add(`${THOUSAND.toLocaleString()} morris`, () => {
|
|
47
|
+
for (let i = 0; i < THOUSAND; i++) biTree.morris(n => n, 'pre');
|
|
43
48
|
});
|
|
44
49
|
|
|
45
50
|
export { suite };
|
|
@@ -305,30 +305,30 @@ describe('AVLTree iterative methods test', () => {
|
|
|
305
305
|
|
|
306
306
|
test('forEach should iterate over all elements', () => {
|
|
307
307
|
const mockCallback = jest.fn();
|
|
308
|
-
avl.forEach((
|
|
309
|
-
mockCallback(
|
|
308
|
+
avl.forEach((value, key) => {
|
|
309
|
+
mockCallback(value, key);
|
|
310
310
|
});
|
|
311
311
|
|
|
312
312
|
expect(mockCallback.mock.calls.length).toBe(3);
|
|
313
|
-
expect(mockCallback.mock.calls[0]
|
|
314
|
-
expect(mockCallback.mock.calls[1]
|
|
315
|
-
expect(mockCallback.mock.calls[2]
|
|
313
|
+
expect(mockCallback.mock.calls[0]).toEqual(['a', 1]);
|
|
314
|
+
expect(mockCallback.mock.calls[1]).toEqual(['b', 2]);
|
|
315
|
+
expect(mockCallback.mock.calls[2]).toEqual(['c', 3]);
|
|
316
316
|
});
|
|
317
317
|
|
|
318
318
|
test('filter should return a new tree with filtered elements', () => {
|
|
319
|
-
const filteredTree = avl.filter((
|
|
319
|
+
const filteredTree = avl.filter((value, key) => key > 1);
|
|
320
320
|
expect(filteredTree.size).toBe(2);
|
|
321
321
|
expect([...filteredTree]).toEqual([[2, 'b'], [3, 'c']]);
|
|
322
322
|
});
|
|
323
323
|
|
|
324
324
|
test('map should return a new tree with modified elements', () => {
|
|
325
|
-
const mappedTree = avl.map((
|
|
325
|
+
const mappedTree = avl.map((value, key) => (key * 2).toString());
|
|
326
326
|
expect(mappedTree.size).toBe(3);
|
|
327
327
|
expect([...mappedTree]).toEqual([[1, '2'], [2, '4'], [3, '6']]);
|
|
328
328
|
});
|
|
329
329
|
|
|
330
330
|
test('reduce should accumulate values', () => {
|
|
331
|
-
const sum = avl.reduce((acc,
|
|
331
|
+
const sum = avl.reduce((acc, value, key) => acc + key, 0);
|
|
332
332
|
expect(sum).toBe(6);
|
|
333
333
|
});
|
|
334
334
|
|
|
@@ -341,4 +341,20 @@ describe('AVLTree iterative methods test', () => {
|
|
|
341
341
|
expect(entries.length).toBe(3);
|
|
342
342
|
expect(entries).toEqual([[1, 'a'], [2, 'b'], [3, 'c']]);
|
|
343
343
|
});
|
|
344
|
+
|
|
345
|
+
test('should clone work well', () => {
|
|
346
|
+
const cloned = avl.clone();
|
|
347
|
+
expect(cloned.root?.left?.key).toBe(1);
|
|
348
|
+
expect(cloned.root?.right?.value).toBe('c');
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
test('should keys', () => {
|
|
352
|
+
const keys = avl.keys();
|
|
353
|
+
expect([...keys]).toEqual([1, 2, 3]);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
test('should values', () => {
|
|
357
|
+
const values = avl.values();
|
|
358
|
+
expect([...values]).toEqual(['a', 'b', 'c']);
|
|
359
|
+
});
|
|
344
360
|
});
|
|
@@ -564,7 +564,6 @@ describe('BinaryTree', () => {
|
|
|
564
564
|
});
|
|
565
565
|
});
|
|
566
566
|
|
|
567
|
-
|
|
568
567
|
describe('BinaryTree iterative methods test', () => {
|
|
569
568
|
let binaryTree: BinaryTree<string>;
|
|
570
569
|
beforeEach(() => {
|
|
@@ -581,30 +580,30 @@ describe('BinaryTree iterative methods test', () => {
|
|
|
581
580
|
|
|
582
581
|
test('forEach should iterate over all elements', () => {
|
|
583
582
|
const mockCallback = jest.fn();
|
|
584
|
-
binaryTree.forEach((
|
|
585
|
-
mockCallback(
|
|
583
|
+
binaryTree.forEach((value, key) => {
|
|
584
|
+
mockCallback(value, key);
|
|
586
585
|
});
|
|
587
586
|
|
|
588
587
|
expect(mockCallback.mock.calls.length).toBe(3);
|
|
589
|
-
expect(mockCallback.mock.calls[0]
|
|
590
|
-
expect(mockCallback.mock.calls[1]
|
|
591
|
-
expect(mockCallback.mock.calls[2]
|
|
588
|
+
expect(mockCallback.mock.calls[0]).toEqual(['b', 2]);
|
|
589
|
+
expect(mockCallback.mock.calls[1]).toEqual(['a', 1]);
|
|
590
|
+
expect(mockCallback.mock.calls[2]).toEqual(['c', 3]);
|
|
592
591
|
});
|
|
593
592
|
|
|
594
593
|
test('filter should return a new tree with filtered elements', () => {
|
|
595
|
-
const filteredTree = binaryTree.filter((
|
|
594
|
+
const filteredTree = binaryTree.filter((value, key) => key > 1);
|
|
596
595
|
expect(filteredTree.size).toBe(2);
|
|
597
596
|
expect([...filteredTree]).toEqual([[3, 'c'], [2, 'b']]);
|
|
598
597
|
});
|
|
599
598
|
|
|
600
599
|
test('map should return a new tree with modified elements', () => {
|
|
601
|
-
const mappedTree = binaryTree.map((
|
|
600
|
+
const mappedTree = binaryTree.map((value, key) => (key * 2).toString());
|
|
602
601
|
expect(mappedTree.size).toBe(3);
|
|
603
602
|
expect([...mappedTree]).toEqual([[1, '2'], [2, '4'], [3, '6']]);
|
|
604
603
|
});
|
|
605
604
|
|
|
606
605
|
test('reduce should accumulate values', () => {
|
|
607
|
-
const sum = binaryTree.reduce((acc,
|
|
606
|
+
const sum = binaryTree.reduce((acc, currentValue, currentKey) => acc + currentKey, 0);
|
|
608
607
|
expect(sum).toBe(6);
|
|
609
608
|
});
|
|
610
609
|
|
|
@@ -617,4 +616,20 @@ describe('BinaryTree iterative methods test', () => {
|
|
|
617
616
|
expect(entries.length).toBe(3);
|
|
618
617
|
expect(entries).toEqual([[2, 'b'], [1, 'a'], [3, 'c']]);
|
|
619
618
|
});
|
|
619
|
+
|
|
620
|
+
test('should clone work well', () => {
|
|
621
|
+
const cloned = binaryTree.clone();
|
|
622
|
+
expect(cloned.root?.left?.key).toBe(2);
|
|
623
|
+
expect(cloned.root?.right?.value).toBe('c');
|
|
624
|
+
});
|
|
625
|
+
|
|
626
|
+
test('should keys', () => {
|
|
627
|
+
const keys = binaryTree.keys();
|
|
628
|
+
expect([...keys]).toEqual([2, 1, 3]);
|
|
629
|
+
});
|
|
630
|
+
|
|
631
|
+
test('should values', () => {
|
|
632
|
+
const values = binaryTree.values();
|
|
633
|
+
expect([...values]).toEqual(['b', 'a', 'c']);
|
|
634
|
+
});
|
|
620
635
|
});
|
|
@@ -864,30 +864,30 @@ describe('BST iterative methods test', () => {
|
|
|
864
864
|
|
|
865
865
|
test('forEach should iterate over all elements', () => {
|
|
866
866
|
const mockCallback = jest.fn();
|
|
867
|
-
bst.forEach((
|
|
868
|
-
mockCallback(
|
|
867
|
+
bst.forEach((value, key) => {
|
|
868
|
+
mockCallback(value, key);
|
|
869
869
|
});
|
|
870
870
|
|
|
871
871
|
expect(mockCallback.mock.calls.length).toBe(3);
|
|
872
|
-
expect(mockCallback.mock.calls[0]
|
|
873
|
-
expect(mockCallback.mock.calls[1]
|
|
874
|
-
expect(mockCallback.mock.calls[2]
|
|
872
|
+
expect(mockCallback.mock.calls[0]).toEqual(['a', 1]);
|
|
873
|
+
expect(mockCallback.mock.calls[1]).toEqual(['b', 2]);
|
|
874
|
+
expect(mockCallback.mock.calls[2]).toEqual(['c', 3]);
|
|
875
875
|
});
|
|
876
876
|
|
|
877
877
|
test('filter should return a new tree with filtered elements', () => {
|
|
878
|
-
const filteredTree = bst.filter((
|
|
878
|
+
const filteredTree = bst.filter((value, key) => key > 1);
|
|
879
879
|
expect(filteredTree.size).toBe(2);
|
|
880
880
|
expect([...filteredTree]).toEqual([[2, 'b'], [3, 'c']]);
|
|
881
881
|
});
|
|
882
882
|
|
|
883
883
|
test('map should return a new tree with modified elements', () => {
|
|
884
|
-
const mappedTree = bst.map((
|
|
884
|
+
const mappedTree = bst.map((value, key) => (key * 2).toString());
|
|
885
885
|
expect(mappedTree.size).toBe(3);
|
|
886
886
|
expect([...mappedTree]).toEqual([[1, '2'], [2, '4'], [3, '6']]);
|
|
887
887
|
});
|
|
888
888
|
|
|
889
889
|
test('reduce should accumulate values', () => {
|
|
890
|
-
const sum = bst.reduce((acc,
|
|
890
|
+
const sum = bst.reduce((acc, value, key) => acc + key, 0);
|
|
891
891
|
expect(sum).toBe(6);
|
|
892
892
|
});
|
|
893
893
|
|
|
@@ -900,4 +900,20 @@ describe('BST iterative methods test', () => {
|
|
|
900
900
|
expect(entries.length).toBe(3);
|
|
901
901
|
expect(entries).toEqual([[1, 'a'], [2, 'b'], [3, 'c']]);
|
|
902
902
|
});
|
|
903
|
+
|
|
904
|
+
test('should clone work well', () => {
|
|
905
|
+
const cloned = bst.clone();
|
|
906
|
+
expect(cloned.root?.left).toBe(undefined);
|
|
907
|
+
expect(cloned.root?.right?.value).toBe('b');
|
|
908
|
+
});
|
|
909
|
+
|
|
910
|
+
test('should keys', () => {
|
|
911
|
+
const keys = bst.keys();
|
|
912
|
+
expect([...keys]).toEqual([1, 2, 3]);
|
|
913
|
+
});
|
|
914
|
+
|
|
915
|
+
test('should values', () => {
|
|
916
|
+
const values = bst.values();
|
|
917
|
+
expect([...values]).toEqual(['a', 'b', 'c']);
|
|
918
|
+
});
|
|
903
919
|
});
|
|
@@ -524,30 +524,30 @@ describe('RedBlackTree iterative methods test', () => {
|
|
|
524
524
|
|
|
525
525
|
test('forEach should iterate over all elements', () => {
|
|
526
526
|
const mockCallback = jest.fn();
|
|
527
|
-
rbTree.forEach((
|
|
528
|
-
mockCallback(
|
|
527
|
+
rbTree.forEach((value, key) => {
|
|
528
|
+
mockCallback(value, key);
|
|
529
529
|
});
|
|
530
530
|
|
|
531
531
|
expect(mockCallback.mock.calls.length).toBe(3);
|
|
532
|
-
expect(mockCallback.mock.calls[0]
|
|
533
|
-
expect(mockCallback.mock.calls[1]
|
|
534
|
-
expect(mockCallback.mock.calls[2]
|
|
532
|
+
expect(mockCallback.mock.calls[0]).toEqual(['a', 1]);
|
|
533
|
+
expect(mockCallback.mock.calls[1]).toEqual(['b', 2]);
|
|
534
|
+
expect(mockCallback.mock.calls[2]).toEqual(['c', 3]);
|
|
535
535
|
});
|
|
536
536
|
|
|
537
537
|
test('filter should return a new tree with filtered elements', () => {
|
|
538
|
-
const filteredTree = rbTree.filter((
|
|
538
|
+
const filteredTree = rbTree.filter((value, key) => key > 1);
|
|
539
539
|
expect(filteredTree.size).toBe(2);
|
|
540
540
|
expect([...filteredTree]).toEqual([[2, 'b'], [3, 'c']]);
|
|
541
541
|
});
|
|
542
542
|
|
|
543
543
|
test('map should return a new tree with modified elements', () => {
|
|
544
|
-
const mappedTree = rbTree.map((
|
|
544
|
+
const mappedTree = rbTree.map((value, key) => (key * 2).toString());
|
|
545
545
|
expect(mappedTree.size).toBe(3);
|
|
546
546
|
expect([...mappedTree]).toEqual([[1, '2'], [2, '4'], [3, '6']]);
|
|
547
547
|
});
|
|
548
548
|
|
|
549
549
|
test('reduce should accumulate values', () => {
|
|
550
|
-
const sum = rbTree.reduce((acc,
|
|
550
|
+
const sum = rbTree.reduce((acc, value, key) => acc + key, 0);
|
|
551
551
|
expect(sum).toBe(6);
|
|
552
552
|
});
|
|
553
553
|
|
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
AVLTreeNode,
|
|
3
|
+
BinaryTreeNode,
|
|
4
|
+
BSTNode,
|
|
5
|
+
CP,
|
|
6
|
+
IterationType,
|
|
7
|
+
TreeMultimap,
|
|
8
|
+
TreeMultimapNode
|
|
9
|
+
} from '../../../../src';
|
|
2
10
|
import { isDebugTest } from '../../../config';
|
|
3
11
|
|
|
4
12
|
const isDebug = isDebugTest;
|
|
@@ -592,3 +600,76 @@ describe('TreeMultimap Performance test', function () {
|
|
|
592
600
|
isDebug && console.log('---lesserOrGreaterTraverse', performance.now() - startL);
|
|
593
601
|
});
|
|
594
602
|
});
|
|
603
|
+
|
|
604
|
+
describe('TreeMultimap iterative methods test', () => {
|
|
605
|
+
let treeMM: TreeMultimap<string>;
|
|
606
|
+
beforeEach(() => {
|
|
607
|
+
treeMM = new TreeMultimap<string>();
|
|
608
|
+
treeMM.add([1, 'a'], 10);
|
|
609
|
+
treeMM.add([2, 'b'], 10);
|
|
610
|
+
treeMM.add([3, 'c'], 1);
|
|
611
|
+
});
|
|
612
|
+
|
|
613
|
+
test('The node obtained by get Node should match the node type', () => {
|
|
614
|
+
const node3 = treeMM.getNode(3);
|
|
615
|
+
expect(node3).toBeInstanceOf(BinaryTreeNode);
|
|
616
|
+
expect(node3).toBeInstanceOf(BSTNode);
|
|
617
|
+
expect(node3).toBeInstanceOf(AVLTreeNode);
|
|
618
|
+
});
|
|
619
|
+
|
|
620
|
+
test('forEach should iterate over all elements', () => {
|
|
621
|
+
const mockCallback = jest.fn();
|
|
622
|
+
treeMM.forEach((value, key) => {
|
|
623
|
+
mockCallback(value, key);
|
|
624
|
+
});
|
|
625
|
+
|
|
626
|
+
expect(mockCallback.mock.calls.length).toBe(3);
|
|
627
|
+
expect(mockCallback.mock.calls[0]).toEqual(['a', 1]);
|
|
628
|
+
expect(mockCallback.mock.calls[1]).toEqual(['b', 2]);
|
|
629
|
+
expect(mockCallback.mock.calls[2]).toEqual(['c', 3]);
|
|
630
|
+
});
|
|
631
|
+
|
|
632
|
+
test('filter should return a new tree with filtered elements', () => {
|
|
633
|
+
const filteredTree = treeMM.filter((value, key) => key > 1);
|
|
634
|
+
expect(filteredTree.size).toBe(2);
|
|
635
|
+
expect([...filteredTree]).toEqual([[2, 'b'], [3, 'c']]);
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
test('map should return a new tree with modified elements', () => {
|
|
639
|
+
const mappedTree = treeMM.map((value, key) => (key * 2).toString());
|
|
640
|
+
expect(mappedTree.size).toBe(3);
|
|
641
|
+
expect([...mappedTree]).toEqual([[1, '2'], [2, '4'], [3, '6']]);
|
|
642
|
+
});
|
|
643
|
+
|
|
644
|
+
test('reduce should accumulate values', () => {
|
|
645
|
+
const sum = treeMM.reduce((acc, value, key) => acc + key, 0);
|
|
646
|
+
expect(sum).toBe(6);
|
|
647
|
+
});
|
|
648
|
+
|
|
649
|
+
test('[Symbol.iterator] should provide an iterator', () => {
|
|
650
|
+
const entries = [];
|
|
651
|
+
for (const entry of treeMM) {
|
|
652
|
+
entries.push(entry);
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
expect(entries.length).toBe(3);
|
|
656
|
+
expect(entries).toEqual([[1, 'a'], [2, 'b'], [3, 'c']]);
|
|
657
|
+
});
|
|
658
|
+
|
|
659
|
+
test('should clone work well', () => {
|
|
660
|
+
expect(treeMM.count).toBe(21)
|
|
661
|
+
const cloned = treeMM.clone();
|
|
662
|
+
expect(cloned.root?.left?.key).toBe(1);
|
|
663
|
+
expect(cloned.root?.right?.value).toBe('c');
|
|
664
|
+
});
|
|
665
|
+
|
|
666
|
+
test('should keys', () => {
|
|
667
|
+
const keys = treeMM.keys();
|
|
668
|
+
expect([...keys]).toEqual([1, 2, 3]);
|
|
669
|
+
});
|
|
670
|
+
|
|
671
|
+
test('should values', () => {
|
|
672
|
+
const values = treeMM.values();
|
|
673
|
+
expect([...values]).toEqual(['a', 'b', 'c']);
|
|
674
|
+
});
|
|
675
|
+
});
|
|
@@ -616,22 +616,22 @@ describe('DirectedGraph iterative Methods', () => {
|
|
|
616
616
|
|
|
617
617
|
test('forEach should apply a function to each vertex', () => {
|
|
618
618
|
const result: VertexKey[] = [];
|
|
619
|
-
graph.forEach(
|
|
619
|
+
graph.forEach((value, key) => key && result.push(key));
|
|
620
620
|
expect(result).toEqual(vertices);
|
|
621
621
|
});
|
|
622
622
|
|
|
623
623
|
test('filter should return vertices that satisfy the condition', () => {
|
|
624
|
-
const filtered = graph.filter(vertex => vertex
|
|
624
|
+
const filtered = graph.filter((value, vertex) => vertex === 'A' || vertex === 'B');
|
|
625
625
|
expect(filtered).toEqual([["A", undefined], ["B", undefined]]);
|
|
626
626
|
});
|
|
627
627
|
|
|
628
628
|
test('map should apply a function to each vertex and return a new array', () => {
|
|
629
|
-
const mapped = graph.map(vertex => vertex
|
|
629
|
+
const mapped = graph.map((value, vertex) => vertex + '_mapped');
|
|
630
630
|
expect(mapped).toEqual(vertices.map(v => v + '_mapped'));
|
|
631
631
|
});
|
|
632
632
|
|
|
633
633
|
test('reduce should accumulate a value based on each vertex', () => {
|
|
634
|
-
const concatenated = graph.reduce((acc,
|
|
634
|
+
const concatenated = graph.reduce((acc, value, key) => acc + key, '');
|
|
635
635
|
expect(concatenated).toBe(vertices.join(''));
|
|
636
636
|
});
|
|
637
637
|
});
|