data-structure-typed 1.49.1 → 1.49.3
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 +16 -16
- package/README_zh-CN.md +2 -2
- package/benchmark/report.html +46 -1
- package/benchmark/report.json +457 -22
- package/dist/cjs/data-structures/base/iterable-base.d.ts +11 -0
- package/dist/cjs/data-structures/base/iterable-base.js +21 -0
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +7 -7
- package/dist/cjs/data-structures/graph/abstract-graph.js +43 -12
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/cjs/data-structures/graph/directed-graph.js +2 -2
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +9 -9
- package/dist/cjs/data-structures/hash/hash-map.js +16 -15
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +6 -35
- package/dist/cjs/data-structures/heap/heap.js +10 -42
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +99 -105
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +143 -146
- 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 +16 -21
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +42 -42
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +25 -25
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +36 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +70 -75
- package/dist/cjs/data-structures/queue/deque.js +100 -110
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +37 -38
- package/dist/cjs/data-structures/queue/queue.js +46 -49
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +2 -3
- package/dist/cjs/data-structures/stack/stack.js +2 -5
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +1 -2
- package/dist/cjs/data-structures/trie/trie.js +2 -5
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/mjs/data-structures/base/iterable-base.d.ts +11 -0
- package/dist/mjs/data-structures/base/iterable-base.js +21 -0
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +7 -7
- package/dist/mjs/data-structures/graph/abstract-graph.js +43 -12
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/mjs/data-structures/graph/directed-graph.js +2 -2
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +1 -1
- package/dist/mjs/data-structures/graph/undirected-graph.js +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +9 -9
- package/dist/mjs/data-structures/hash/hash-map.js +16 -15
- package/dist/mjs/data-structures/heap/heap.d.ts +6 -35
- package/dist/mjs/data-structures/heap/heap.js +10 -42
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +99 -105
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +142 -145
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +16 -21
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +43 -43
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +25 -25
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +36 -36
- package/dist/mjs/data-structures/queue/deque.d.ts +70 -75
- package/dist/mjs/data-structures/queue/deque.js +100 -110
- package/dist/mjs/data-structures/queue/queue.d.ts +37 -38
- package/dist/mjs/data-structures/queue/queue.js +45 -48
- package/dist/mjs/data-structures/stack/stack.d.ts +2 -3
- package/dist/mjs/data-structures/stack/stack.js +2 -5
- package/dist/mjs/data-structures/trie/trie.d.ts +1 -2
- package/dist/mjs/data-structures/trie/trie.js +2 -5
- package/dist/umd/data-structure-typed.js +531 -532
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/base/iterable-base.ts +24 -0
- package/src/data-structures/graph/abstract-graph.ts +55 -14
- package/src/data-structures/graph/directed-graph.ts +3 -2
- package/src/data-structures/graph/undirected-graph.ts +1 -1
- package/src/data-structures/hash/hash-map.ts +27 -28
- package/src/data-structures/heap/heap.ts +19 -57
- package/src/data-structures/linked-list/doubly-linked-list.ts +157 -161
- package/src/data-structures/linked-list/singly-linked-list.ts +49 -49
- package/src/data-structures/linked-list/skip-linked-list.ts +40 -40
- package/src/data-structures/queue/deque.ts +122 -135
- package/src/data-structures/queue/queue.ts +54 -58
- package/src/data-structures/stack/stack.ts +4 -8
- package/src/data-structures/trie/trie.ts +5 -9
- package/test/performance/data-structures/comparison/comparison.test.ts +6 -6
- package/test/performance/data-structures/heap/heap.test.ts +2 -2
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +2 -2
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +2 -2
- package/test/unit/data-structures/graph/abstract-graph.test.ts +1 -1
- package/test/unit/data-structures/graph/directed-graph.test.ts +48 -3
- package/test/unit/data-structures/graph/undirected-graph.test.ts +48 -4
- package/test/unit/data-structures/hash/hash-map.test.ts +2 -2
- package/test/unit/data-structures/heap/heap.test.ts +6 -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 +30 -30
- package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +21 -21
- package/test/unit/data-structures/linked-list/skip-list.test.ts +2 -2
- package/test/unit/data-structures/queue/deque.test.ts +5 -5
- package/test/unit/data-structures/queue/queue.test.ts +4 -4
- package/test/unit/data-structures/trie/trie.test.ts +1 -1
|
@@ -138,7 +138,7 @@ export class Trie extends IterableElementBase<string> {
|
|
|
138
138
|
* @param{string} word - The word to delete.
|
|
139
139
|
* @returns {boolean} True if the word was successfully removed.
|
|
140
140
|
*/
|
|
141
|
-
delete(word: string) {
|
|
141
|
+
delete(word: string): boolean {
|
|
142
142
|
word = this._caseProcess(word);
|
|
143
143
|
let isDeleted = false;
|
|
144
144
|
const dfs = (cur: TrieNode, i: number): boolean => {
|
|
@@ -184,7 +184,7 @@ export class Trie extends IterableElementBase<string> {
|
|
|
184
184
|
* Space Complexity: O(1) - Constant space.
|
|
185
185
|
*
|
|
186
186
|
*/
|
|
187
|
-
getHeight() {
|
|
187
|
+
getHeight(): number {
|
|
188
188
|
const beginRoot = this.root;
|
|
189
189
|
let maxDepth = 0;
|
|
190
190
|
if (beginRoot) {
|
|
@@ -371,12 +371,12 @@ export class Trie extends IterableElementBase<string> {
|
|
|
371
371
|
* specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
|
|
372
372
|
* @returns The `filter` method is returning an array of strings (`string[]`).
|
|
373
373
|
*/
|
|
374
|
-
filter(predicate: ElementCallback<string, boolean>, thisArg?: any):
|
|
375
|
-
const results:
|
|
374
|
+
filter(predicate: ElementCallback<string, boolean>, thisArg?: any): Trie {
|
|
375
|
+
const results: Trie = new Trie();
|
|
376
376
|
let index = 0;
|
|
377
377
|
for (const word of this) {
|
|
378
378
|
if (predicate.call(thisArg, word, index, this)) {
|
|
379
|
-
results.
|
|
379
|
+
results.add(word);
|
|
380
380
|
}
|
|
381
381
|
index++;
|
|
382
382
|
}
|
|
@@ -411,10 +411,6 @@ export class Trie extends IterableElementBase<string> {
|
|
|
411
411
|
return newTrie;
|
|
412
412
|
}
|
|
413
413
|
|
|
414
|
-
print() {
|
|
415
|
-
console.log([...this]);
|
|
416
|
-
}
|
|
417
|
-
|
|
418
414
|
protected* _getIterator(): IterableIterator<string> {
|
|
419
415
|
function* _dfs(node: TrieNode, path: string): IterableIterator<string> {
|
|
420
416
|
if (node.isEnd) {
|
|
@@ -47,23 +47,23 @@ if (isCompetitor) {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
suite
|
|
50
|
-
.add(`SRC PQ ${TEN_THOUSAND.toLocaleString()} add &
|
|
50
|
+
.add(`SRC PQ ${TEN_THOUSAND.toLocaleString()} add & poll`, () => {
|
|
51
51
|
const pq = new SRCPriorityQueue<number>([], { comparator: (a, b) => b - a });
|
|
52
52
|
|
|
53
53
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
|
|
54
|
-
for (let i = 0; i < TEN_THOUSAND; i++) pq.
|
|
54
|
+
for (let i = 0; i < TEN_THOUSAND; i++) pq.poll();
|
|
55
55
|
})
|
|
56
|
-
.add(`CJS PQ ${TEN_THOUSAND.toLocaleString()} add &
|
|
56
|
+
.add(`CJS PQ ${TEN_THOUSAND.toLocaleString()} add & poll`, () => {
|
|
57
57
|
const pq = new CJSPriorityQueue<number>([], { comparator: (a, b) => b - a });
|
|
58
58
|
|
|
59
59
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
|
|
60
|
-
for (let i = 0; i < TEN_THOUSAND; i++) pq.
|
|
60
|
+
for (let i = 0; i < TEN_THOUSAND; i++) pq.poll();
|
|
61
61
|
})
|
|
62
|
-
.add(`MJS PQ ${TEN_THOUSAND.toLocaleString()} add &
|
|
62
|
+
.add(`MJS PQ ${TEN_THOUSAND.toLocaleString()} add & poll`, () => {
|
|
63
63
|
const pq = new MJSPriorityQueue<number>([], { comparator: (a, b) => b - a });
|
|
64
64
|
|
|
65
65
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
|
|
66
|
-
for (let i = 0; i < TEN_THOUSAND; i++) pq.
|
|
66
|
+
for (let i = 0; i < TEN_THOUSAND; i++) pq.poll();
|
|
67
67
|
});
|
|
68
68
|
|
|
69
69
|
|
|
@@ -6,7 +6,7 @@ const suite = new Benchmark.Suite();
|
|
|
6
6
|
const { HUNDRED_THOUSAND, TEN_THOUSAND } = magnitude;
|
|
7
7
|
|
|
8
8
|
suite
|
|
9
|
-
.add(`${HUNDRED_THOUSAND.toLocaleString()} add &
|
|
9
|
+
.add(`${HUNDRED_THOUSAND.toLocaleString()} add & poll`, () => {
|
|
10
10
|
const heap = new Heap<number>([], { comparator: (a, b) => b - a });
|
|
11
11
|
|
|
12
12
|
for (let i = 0; i < HUNDRED_THOUSAND; i++) {
|
|
@@ -14,7 +14,7 @@ suite
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
for (let i = 0; i < HUNDRED_THOUSAND; i++) {
|
|
17
|
-
heap.
|
|
17
|
+
heap.poll();
|
|
18
18
|
}
|
|
19
19
|
})
|
|
20
20
|
.add(`${HUNDRED_THOUSAND.toLocaleString()} add & dfs`, () => {
|
|
@@ -55,7 +55,7 @@ suite
|
|
|
55
55
|
list.shift();
|
|
56
56
|
}
|
|
57
57
|
})
|
|
58
|
-
.add(`${LINEAR.toLocaleString()}
|
|
58
|
+
.add(`${LINEAR.toLocaleString()} addBefore`, () => {
|
|
59
59
|
const doublyList = new DoublyLinkedList<number>();
|
|
60
60
|
let midNode: DoublyLinkedListNode | undefined;
|
|
61
61
|
const midIndex = Math.floor(LINEAR / 2);
|
|
@@ -64,7 +64,7 @@ suite
|
|
|
64
64
|
if (i === midIndex) {
|
|
65
65
|
midNode = doublyList.getNode(i);
|
|
66
66
|
} else if (i > midIndex && midNode) {
|
|
67
|
-
doublyList.
|
|
67
|
+
doublyList.addBefore(midNode, i);
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
70
|
});
|
|
@@ -28,7 +28,7 @@ suite
|
|
|
28
28
|
list.pop();
|
|
29
29
|
}
|
|
30
30
|
})
|
|
31
|
-
.add(`${TEN_THOUSAND.toLocaleString()}
|
|
31
|
+
.add(`${TEN_THOUSAND.toLocaleString()} addBefore`, () => {
|
|
32
32
|
const singlyList = new SinglyLinkedList<number>();
|
|
33
33
|
let midSinglyNode: SinglyLinkedListNode | undefined;
|
|
34
34
|
const midIndex = Math.floor(TEN_THOUSAND / 2);
|
|
@@ -37,7 +37,7 @@ suite
|
|
|
37
37
|
if (i === midIndex) {
|
|
38
38
|
midSinglyNode = singlyList.getNode(i);
|
|
39
39
|
} else if (i > midIndex && midSinglyNode) {
|
|
40
|
-
singlyList.
|
|
40
|
+
singlyList.addBefore(midSinglyNode.value, i);
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
});
|
|
@@ -7,7 +7,7 @@ import { isCompetitor } from '../../../config';
|
|
|
7
7
|
const suite = new Benchmark.Suite();
|
|
8
8
|
const { HUNDRED_THOUSAND } = magnitude;
|
|
9
9
|
|
|
10
|
-
suite.add(`${HUNDRED_THOUSAND.toLocaleString()} add &
|
|
10
|
+
suite.add(`${HUNDRED_THOUSAND.toLocaleString()} add & poll`, () => {
|
|
11
11
|
const pq = new PriorityQueue<number>([], { comparator: (a, b) => b - a });
|
|
12
12
|
|
|
13
13
|
for (let i = 0; i < HUNDRED_THOUSAND; i++) {
|
|
@@ -15,7 +15,7 @@ suite.add(`${HUNDRED_THOUSAND.toLocaleString()} add & pop`, () => {
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
for (let i = 0; i < HUNDRED_THOUSAND; i++) {
|
|
18
|
-
pq.
|
|
18
|
+
pq.poll();
|
|
19
19
|
}
|
|
20
20
|
});
|
|
21
21
|
if (isCompetitor) {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { BinaryTree, BinaryTreeNode, FamilyPosition, IterationType } from '../../../../src';
|
|
2
2
|
import { getRandomIntArray } from '../../../utils';
|
|
3
|
-
import { isDebugTest } from '../../../config';
|
|
3
|
+
// import { isDebugTest } from '../../../config';
|
|
4
4
|
|
|
5
|
-
const isDebug = isDebugTest;
|
|
5
|
+
// const isDebug = isDebugTest;
|
|
6
6
|
|
|
7
7
|
describe('BinaryTreeNode', () => {
|
|
8
8
|
it('should create an instance of BinaryTreeNode', () => {
|
|
@@ -588,7 +588,7 @@ describe('cycles, strongly connected components, bridges, articular points in Di
|
|
|
588
588
|
const cutVertexes = graph.getCutVertexes();
|
|
589
589
|
const dfnMap = graph.getDFNMap();
|
|
590
590
|
const lowMap = graph.getLowMap();
|
|
591
|
-
expect(cycles.
|
|
591
|
+
expect(cycles.length).toBe(2);
|
|
592
592
|
expect(scCs.size).toBe(5);
|
|
593
593
|
expect(bridges.length).toBe(4);
|
|
594
594
|
expect(cutVertexes.length).toBe(4);
|
|
@@ -688,8 +688,53 @@ describe('DirectedGraph getCycles', () => {
|
|
|
688
688
|
graph.addEdge('D', 'E');
|
|
689
689
|
graph.addEdge('E', 'B');
|
|
690
690
|
const cycles = graph.getCycles();
|
|
691
|
-
expect(cycles.
|
|
692
|
-
expect(cycles
|
|
691
|
+
expect(cycles.length).toBe(1);
|
|
692
|
+
expect(cycles[0]).toEqual(["B", "D", "E"]);
|
|
693
693
|
})
|
|
694
|
+
|
|
695
|
+
test('should simple cycles graph getCycles return correct result', () => {
|
|
696
|
+
const graph = new DirectedGraph();
|
|
697
|
+
|
|
698
|
+
graph.addVertex('A');
|
|
699
|
+
graph.addVertex('B');
|
|
700
|
+
graph.addVertex('C');
|
|
701
|
+
graph.addVertex('D');
|
|
702
|
+
|
|
703
|
+
graph.addEdge('A', 'B');
|
|
704
|
+
graph.addEdge('B', 'C');
|
|
705
|
+
graph.addEdge('C', 'A');
|
|
706
|
+
graph.addEdge('A', 'D');
|
|
707
|
+
graph.addEdge('D', 'C');
|
|
708
|
+
const cycles = graph.getCycles();
|
|
709
|
+
expect(cycles.length).toBe(2)
|
|
710
|
+
expect(cycles).toEqual([["A", "B", "C"], ["A", "D", "C"]])
|
|
711
|
+
});
|
|
712
|
+
|
|
713
|
+
test('should 3 cycles graph getCycles return correct result', () => {
|
|
714
|
+
const graph = new DirectedGraph();
|
|
715
|
+
|
|
716
|
+
graph.addVertex('A');
|
|
717
|
+
graph.addVertex('B');
|
|
718
|
+
graph.addVertex('C');
|
|
719
|
+
graph.addVertex('D');
|
|
720
|
+
graph.addVertex('E');
|
|
721
|
+
graph.addVertex('F');
|
|
722
|
+
graph.addVertex('G');
|
|
723
|
+
|
|
724
|
+
graph.addEdge('A', 'B');
|
|
725
|
+
graph.addEdge('A', 'C');
|
|
726
|
+
graph.addEdge('B', 'D');
|
|
727
|
+
graph.addEdge('C', 'D');
|
|
728
|
+
graph.addEdge('D', 'E');
|
|
729
|
+
graph.addEdge('E', 'B');
|
|
730
|
+
graph.addEdge('B', 'F');
|
|
731
|
+
graph.addEdge('F', 'E');
|
|
732
|
+
graph.addEdge('C', 'G');
|
|
733
|
+
graph.addEdge('G', 'A');
|
|
734
|
+
|
|
735
|
+
const cycles = graph.getCycles();
|
|
736
|
+
expect(cycles.length).toBe(3)
|
|
737
|
+
expect(cycles).toEqual([["A", "C", "G"], ["B", "D", "E"], ["B", "F", "E"]]);
|
|
738
|
+
});
|
|
694
739
|
})
|
|
695
740
|
|
|
@@ -237,7 +237,7 @@ describe('cycles, strongly connected components, bridges, articular points in Un
|
|
|
237
237
|
const cutVertexes = graph.getCutVertexes();
|
|
238
238
|
const dfnMap = graph.getDFNMap();
|
|
239
239
|
const lowMap = graph.getLowMap();
|
|
240
|
-
expect(cycles.
|
|
240
|
+
expect(cycles.length).toBe(3);
|
|
241
241
|
expect(scCs.size).toBe(5);
|
|
242
242
|
expect(bridges.length).toBe(4);
|
|
243
243
|
expect(cutVertexes.length).toBe(4);
|
|
@@ -277,8 +277,52 @@ describe('UndirectedGraph getCycles', () => {
|
|
|
277
277
|
graph.addEdge('D', 'E');
|
|
278
278
|
graph.addEdge('E', 'B');
|
|
279
279
|
const cycles = graph.getCycles();
|
|
280
|
-
expect(cycles.
|
|
281
|
-
expect(cycles
|
|
282
|
-
expect(cycles.get(2)).toEqual([{ "key": "B", "value": "B" }, { "key": "D", "value": "D" }, { "key": "E", "value": "E" }]);
|
|
280
|
+
expect(cycles.length).toBe(3);
|
|
281
|
+
expect(cycles).toEqual([["A", "B", "D", "C"], ["A", "B", "E", "D", "C"], ["B", "D", "E"]]);
|
|
283
282
|
})
|
|
283
|
+
|
|
284
|
+
test('should simple cycles graph getCycles return correct result', () => {
|
|
285
|
+
const graph = new UndirectedGraph();
|
|
286
|
+
|
|
287
|
+
graph.addVertex('A');
|
|
288
|
+
graph.addVertex('B');
|
|
289
|
+
graph.addVertex('C');
|
|
290
|
+
graph.addVertex('D');
|
|
291
|
+
|
|
292
|
+
graph.addEdge('A', 'B');
|
|
293
|
+
graph.addEdge('B', 'C');
|
|
294
|
+
graph.addEdge('C', 'A');
|
|
295
|
+
graph.addEdge('A', 'D');
|
|
296
|
+
graph.addEdge('D', 'C');
|
|
297
|
+
const cycles = graph.getCycles();
|
|
298
|
+
expect(cycles.length).toBe(3)
|
|
299
|
+
expect(cycles).toEqual([["A", "B", "C"], ["A", "B", "C", "D"], ["A", "C", "D"]])
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
test('should 3 cycles graph getCycles return correct result', () => {
|
|
303
|
+
const graph = new UndirectedGraph();
|
|
304
|
+
|
|
305
|
+
graph.addVertex('A');
|
|
306
|
+
graph.addVertex('B');
|
|
307
|
+
graph.addVertex('C');
|
|
308
|
+
graph.addVertex('D');
|
|
309
|
+
graph.addVertex('E');
|
|
310
|
+
graph.addVertex('F');
|
|
311
|
+
graph.addVertex('G');
|
|
312
|
+
|
|
313
|
+
graph.addEdge('A', 'B');
|
|
314
|
+
graph.addEdge('A', 'C');
|
|
315
|
+
graph.addEdge('B', 'D');
|
|
316
|
+
graph.addEdge('C', 'D');
|
|
317
|
+
graph.addEdge('D', 'E');
|
|
318
|
+
graph.addEdge('E', 'B');
|
|
319
|
+
graph.addEdge('B', 'F');
|
|
320
|
+
graph.addEdge('F', 'E');
|
|
321
|
+
graph.addEdge('C', 'G');
|
|
322
|
+
graph.addEdge('G', 'A');
|
|
323
|
+
|
|
324
|
+
const cycles = graph.getCycles();
|
|
325
|
+
expect(cycles.length).toBe(10)
|
|
326
|
+
expect(cycles).toEqual([["A", "B", "D", "C"], ["A", "B", "D", "C", "G"], ["A", "B", "E", "D", "C"], ["A", "B", "E", "D", "C", "G"], ["A", "B", "F", "E", "D", "C"], ["A", "B", "F", "E", "D", "C", "G"], ["A", "C", "G"], ["B", "D", "E"], ["B", "D", "E", "F"], ["B", "E", "F"]]);
|
|
327
|
+
});
|
|
284
328
|
})
|
|
@@ -453,7 +453,7 @@ describe('LinkedHashMap Test2', () => {
|
|
|
453
453
|
expect(hashMap.last).toEqual([key, value]);
|
|
454
454
|
expect(hashMap.reverseBegin().next().value).toEqual([key, value]);
|
|
455
455
|
} else if (index <= 1000) {
|
|
456
|
-
expect(hashMap.getAt(index)).
|
|
456
|
+
expect(hashMap.getAt(index)).toBe(value);
|
|
457
457
|
}
|
|
458
458
|
expect(hashMap.get(key)).toEqual(value);
|
|
459
459
|
index++;
|
|
@@ -502,7 +502,7 @@ describe('LinkedHashMap Test2', () => {
|
|
|
502
502
|
test('should get element at specific index', () => {
|
|
503
503
|
hashMap.set('key1', 'value1');
|
|
504
504
|
hashMap.set('key2', 'value2');
|
|
505
|
-
expect(hashMap.getAt(1)).
|
|
505
|
+
expect(hashMap.getAt(1)).toBe('value2');
|
|
506
506
|
});
|
|
507
507
|
});
|
|
508
508
|
|
|
@@ -5,7 +5,12 @@ import { logBigOMetricsWrap } from '../../../utils';
|
|
|
5
5
|
describe('Heap Operation Test', () => {
|
|
6
6
|
it('should numeric heap work well', function () {
|
|
7
7
|
const minNumHeap = new MinHeap<number>();
|
|
8
|
-
minNumHeap.add(1)
|
|
8
|
+
minNumHeap.add(1);
|
|
9
|
+
minNumHeap.add(6);
|
|
10
|
+
minNumHeap.add(2);
|
|
11
|
+
minNumHeap.add(0);
|
|
12
|
+
minNumHeap.add(5);
|
|
13
|
+
minNumHeap.add(9);
|
|
9
14
|
expect(minNumHeap.has(1)).toBe(true);
|
|
10
15
|
expect(minNumHeap.has(2)).toBe(true);
|
|
11
16
|
expect(minNumHeap.poll()).toBe(0);
|
|
@@ -19,11 +19,11 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
19
19
|
it('should out of bound index', () => {
|
|
20
20
|
expect(list.getNodeAt(-1)).toBe(undefined);
|
|
21
21
|
expect(list.getNodeAt(5)).toBe(undefined);
|
|
22
|
-
expect(list.
|
|
22
|
+
expect(list.addAt(5, 6)).toBe(true);
|
|
23
23
|
});
|
|
24
24
|
|
|
25
|
-
it('should
|
|
26
|
-
expect(list.
|
|
25
|
+
it('should addBefore', () => {
|
|
26
|
+
expect(list.addBefore(1, 0)).toBe(true);
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
it('should deleteAt', () => {
|
|
@@ -49,11 +49,11 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
49
49
|
expect(list.findBackward(value => value === 0)).toBe(undefined);
|
|
50
50
|
});
|
|
51
51
|
|
|
52
|
-
it('should
|
|
53
|
-
expect(list.
|
|
52
|
+
it('should addAfter tail', () => {
|
|
53
|
+
expect(list.addAfter(list.tail!, 6)).toBe(true);
|
|
54
54
|
});
|
|
55
55
|
|
|
56
|
-
it('should
|
|
56
|
+
it('should addAfter tail', () => {
|
|
57
57
|
expect([...list]).toEqual([1, 2, 3, 4, 5]);
|
|
58
58
|
});
|
|
59
59
|
});
|
|
@@ -68,7 +68,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
68
68
|
});
|
|
69
69
|
|
|
70
70
|
it('should initialize an empty list', () => {
|
|
71
|
-
expect(list.
|
|
71
|
+
expect(list.size).toBe(0);
|
|
72
72
|
expect(list.head).toBe(undefined);
|
|
73
73
|
expect(list.tail).toBe(undefined);
|
|
74
74
|
});
|
|
@@ -77,7 +77,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
77
77
|
list.push(1);
|
|
78
78
|
list.push(2);
|
|
79
79
|
list.push(3);
|
|
80
|
-
expect(list.
|
|
80
|
+
expect(list.size).toBe(3);
|
|
81
81
|
expect(list.head!.value).toBe(1);
|
|
82
82
|
expect(list.tail!.value).toBe(3);
|
|
83
83
|
});
|
|
@@ -87,7 +87,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
87
87
|
list.push(2);
|
|
88
88
|
const poppedValue = list.pop();
|
|
89
89
|
expect(poppedValue).toBe(2);
|
|
90
|
-
expect(list.
|
|
90
|
+
expect(list.size).toBe(1);
|
|
91
91
|
expect(list.head!.value).toBe(1);
|
|
92
92
|
expect(list.tail!.value).toBe(1);
|
|
93
93
|
});
|
|
@@ -97,20 +97,20 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
97
97
|
list.push(3);
|
|
98
98
|
|
|
99
99
|
// Inserting at the beginning
|
|
100
|
-
list.
|
|
101
|
-
expect(list.
|
|
100
|
+
list.addAt(0, 0);
|
|
101
|
+
expect(list.size).toBe(4);
|
|
102
102
|
expect(list.getAt(0)).toBe(0);
|
|
103
103
|
expect(list.getAt(1)).toBe(1);
|
|
104
104
|
|
|
105
105
|
// Inserting in the middle
|
|
106
|
-
list.
|
|
107
|
-
expect(list.
|
|
106
|
+
list.addAt(2, 1.5);
|
|
107
|
+
expect(list.size).toBe(5);
|
|
108
108
|
expect(list.getAt(2)).toBe(1.5);
|
|
109
109
|
expect(list.getAt(3)).toBe(2);
|
|
110
110
|
|
|
111
111
|
// Inserting at the end
|
|
112
|
-
list.
|
|
113
|
-
expect(list.
|
|
112
|
+
list.addAt(5, 4);
|
|
113
|
+
expect(list.size).toBe(6);
|
|
114
114
|
expect(list.getAt(5)).toBe(4);
|
|
115
115
|
expect(list.tail!.value).toBe(4);
|
|
116
116
|
});
|
|
@@ -122,18 +122,18 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
122
122
|
|
|
123
123
|
// Deleting from the beginning
|
|
124
124
|
const deletedValue = list.deleteAt(0);
|
|
125
|
-
expect(deletedValue).toBe(
|
|
126
|
-
expect(list.
|
|
125
|
+
expect(deletedValue).toBe(true);
|
|
126
|
+
expect(list.size).toBe(2);
|
|
127
127
|
expect(list.head!.value).toBe(2);
|
|
128
128
|
|
|
129
129
|
// Deleting from the middle
|
|
130
130
|
list.deleteAt(0); // Deleting the second element
|
|
131
|
-
expect(list.
|
|
131
|
+
expect(list.size).toBe(1);
|
|
132
132
|
expect(list.head!.value).toBe(3);
|
|
133
133
|
|
|
134
134
|
// Deleting from the end
|
|
135
135
|
list.deleteAt(0);
|
|
136
|
-
expect(list.
|
|
136
|
+
expect(list.size).toBe(0);
|
|
137
137
|
expect(list.head).toBe(undefined);
|
|
138
138
|
expect(list.tail).toBe(undefined);
|
|
139
139
|
});
|
|
@@ -144,16 +144,16 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
144
144
|
list.push(3);
|
|
145
145
|
|
|
146
146
|
list.delete(2);
|
|
147
|
-
expect(list.
|
|
147
|
+
expect(list.size).toBe(2);
|
|
148
148
|
expect(list.head!.value).toBe(1);
|
|
149
149
|
expect(list.tail!.value).toBe(3);
|
|
150
150
|
|
|
151
151
|
list.delete(1);
|
|
152
|
-
expect(list.
|
|
152
|
+
expect(list.size).toBe(1);
|
|
153
153
|
expect(list.head!.value).toBe(3);
|
|
154
154
|
|
|
155
155
|
list.delete(3);
|
|
156
|
-
expect(list.
|
|
156
|
+
expect(list.size).toBe(0);
|
|
157
157
|
expect(list.head).toBe(undefined);
|
|
158
158
|
expect(list.tail).toBe(undefined);
|
|
159
159
|
});
|
|
@@ -206,7 +206,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
206
206
|
list.push(2);
|
|
207
207
|
list.push(3);
|
|
208
208
|
|
|
209
|
-
list.
|
|
209
|
+
list.addAfter(2, 2.5);
|
|
210
210
|
|
|
211
211
|
expect(list.toArray()).toEqual([1, 2, 2.5, 3]);
|
|
212
212
|
});
|
|
@@ -216,7 +216,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
216
216
|
list.push(2);
|
|
217
217
|
list.push(3);
|
|
218
218
|
|
|
219
|
-
list.
|
|
219
|
+
list.addBefore(2, 1.5);
|
|
220
220
|
|
|
221
221
|
expect(list.toArray()).toEqual([1, 1.5, 2, 3]);
|
|
222
222
|
});
|
|
@@ -258,7 +258,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
258
258
|
|
|
259
259
|
list.clear();
|
|
260
260
|
|
|
261
|
-
expect(list.
|
|
261
|
+
expect(list.size).toBe(0);
|
|
262
262
|
expect(list.head).toBe(undefined);
|
|
263
263
|
expect(list.tail).toBe(undefined);
|
|
264
264
|
});
|
|
@@ -334,7 +334,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
334
334
|
list.push(2);
|
|
335
335
|
list.push(3);
|
|
336
336
|
|
|
337
|
-
const success = list.
|
|
337
|
+
const success = list.addAfter(2, 4);
|
|
338
338
|
expect(success).toBe(true);
|
|
339
339
|
expect(list.toArray()).toEqual([1, 2, 4, 3]);
|
|
340
340
|
});
|
|
@@ -344,7 +344,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
344
344
|
list.push(2);
|
|
345
345
|
list.push(3);
|
|
346
346
|
|
|
347
|
-
const success = list.
|
|
347
|
+
const success = list.addBefore(2, 0);
|
|
348
348
|
expect(success).toBe(true);
|
|
349
349
|
expect(list.toArray()).toEqual([1, 0, 2, 3]);
|
|
350
350
|
});
|
|
@@ -354,7 +354,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
354
354
|
list.push(2);
|
|
355
355
|
list.push(3);
|
|
356
356
|
|
|
357
|
-
const success = list.
|
|
357
|
+
const success = list.addAfter(4, 5);
|
|
358
358
|
expect(success).toBe(false);
|
|
359
359
|
expect(list.toArray()).toEqual([1, 2, 3]);
|
|
360
360
|
});
|
|
@@ -364,7 +364,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
364
364
|
list.push(2);
|
|
365
365
|
list.push(3);
|
|
366
366
|
|
|
367
|
-
const success = list.
|
|
367
|
+
const success = list.addBefore(4, 0);
|
|
368
368
|
expect(success).toBe(false);
|
|
369
369
|
expect(list.toArray()).toEqual([1, 2, 3]);
|
|
370
370
|
});
|
|
@@ -381,7 +381,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
381
381
|
expect(objectList.toArray()).toEqual([obj1, obj2, obj3]);
|
|
382
382
|
|
|
383
383
|
const newObj = { keyA: 25 }; // Corrected newObj value
|
|
384
|
-
const insertSuccess = objectList.
|
|
384
|
+
const insertSuccess = objectList.addBefore(obj2, newObj);
|
|
385
385
|
expect(insertSuccess).toBe(true);
|
|
386
386
|
|
|
387
387
|
const getNode = objectList.getNode(newObj); // Use newObj instead of obj2
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// import {bigO, magnitude} from '../../../utils';
|
|
3
3
|
|
|
4
4
|
describe('LinkedList Performance Test', () => {
|
|
5
|
-
it('should DoublyLinkedList
|
|
5
|
+
it('should DoublyLinkedList addBefore faster than SinglyLinkedList', () => {
|
|
6
6
|
expect(1).toBe(1);
|
|
7
7
|
});
|
|
8
8
|
});
|