data-structure-typed 1.49.2 → 1.49.4

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 (67) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +69 -66
  3. package/README_zh-CN.md +43 -48
  4. package/benchmark/report.html +16 -16
  5. package/benchmark/report.json +187 -187
  6. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +1 -1
  7. package/dist/cjs/data-structures/binary-tree/binary-tree.js +1 -1
  8. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  9. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +8 -17
  10. package/dist/cjs/data-structures/graph/abstract-graph.js +43 -29
  11. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  12. package/dist/cjs/data-structures/graph/directed-graph.d.ts +2 -2
  13. package/dist/cjs/data-structures/graph/directed-graph.js +6 -2
  14. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  15. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +1 -1
  16. package/dist/cjs/data-structures/graph/undirected-graph.js +1 -1
  17. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  18. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +43 -43
  19. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +49 -49
  20. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  21. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  22. package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +25 -25
  23. package/dist/cjs/data-structures/linked-list/skip-linked-list.js +36 -36
  24. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
  25. package/dist/cjs/data-structures/queue/queue.d.ts +33 -33
  26. package/dist/cjs/data-structures/queue/queue.js +40 -40
  27. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  28. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +1 -1
  29. package/dist/mjs/data-structures/binary-tree/binary-tree.js +1 -1
  30. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +8 -17
  31. package/dist/mjs/data-structures/graph/abstract-graph.js +43 -29
  32. package/dist/mjs/data-structures/graph/directed-graph.d.ts +2 -2
  33. package/dist/mjs/data-structures/graph/directed-graph.js +6 -2
  34. package/dist/mjs/data-structures/graph/undirected-graph.d.ts +1 -1
  35. package/dist/mjs/data-structures/graph/undirected-graph.js +1 -1
  36. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +43 -43
  37. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +47 -47
  38. package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +25 -25
  39. package/dist/mjs/data-structures/linked-list/skip-linked-list.js +36 -36
  40. package/dist/mjs/data-structures/queue/queue.d.ts +33 -33
  41. package/dist/mjs/data-structures/queue/queue.js +39 -39
  42. package/dist/umd/data-structure-typed.js +176 -158
  43. package/dist/umd/data-structure-typed.min.js +2 -2
  44. package/dist/umd/data-structure-typed.min.js.map +1 -1
  45. package/package.json +1 -1
  46. package/src/data-structures/binary-tree/binary-tree.ts +1 -1
  47. package/src/data-structures/graph/abstract-graph.ts +56 -27
  48. package/src/data-structures/graph/directed-graph.ts +10 -5
  49. package/src/data-structures/graph/undirected-graph.ts +2 -2
  50. package/src/data-structures/linked-list/doubly-linked-list.ts +53 -53
  51. package/src/data-structures/linked-list/singly-linked-list.ts +2 -3
  52. package/src/data-structures/linked-list/skip-linked-list.ts +40 -40
  53. package/src/data-structures/queue/queue.ts +45 -45
  54. package/test/performance/data-structures/comparison/comparison.test.ts +12 -12
  55. package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +16 -27
  56. package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +4 -12
  57. package/test/performance/data-structures/queue/deque.test.ts +8 -8
  58. package/test/performance/data-structures/queue/queue.test.ts +5 -5
  59. package/test/performance/data-structures/stack/stack.test.ts +11 -11
  60. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +15 -0
  61. package/test/unit/data-structures/graph/abstract-graph.test.ts +12 -1
  62. package/test/unit/data-structures/graph/directed-graph.test.ts +63 -5
  63. package/test/unit/data-structures/graph/undirected-graph.test.ts +61 -4
  64. package/test/unit/data-structures/hash/hash-map.test.ts +21 -0
  65. package/test/unit/data-structures/heap/heap.test.ts +6 -1
  66. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +27 -0
  67. package/test/utils/big-o.ts +14 -14
@@ -16,7 +16,7 @@ import { getRandomIntArray, magnitude } from '../../../utils';
16
16
  import { isCompetitor } from '../../../config';
17
17
 
18
18
  const suite = new Benchmark.Suite();
19
- const { TEN_THOUSAND, HUNDRED_THOUSAND, LINEAR } = magnitude;
19
+ const { TEN_THOUSAND, HUNDRED_THOUSAND, MILLION } = magnitude;
20
20
  const cOrderedMap = new OrderedMap<number, number>();
21
21
  const arrHundredThousand = getRandomIntArray(HUNDRED_THOUSAND, 0, HUNDRED_THOUSAND, true);
22
22
 
@@ -96,10 +96,10 @@ if (isCompetitor) {
96
96
  hm.getElementByKey(i);
97
97
  }
98
98
  })
99
- .add(`CPT LL ${LINEAR.toLocaleString()} unshift`, () => {
99
+ .add(`CPT LL ${MILLION.toLocaleString()} unshift`, () => {
100
100
  const list = new CLinkedList<number>();
101
101
 
102
- for (let i = 0; i < LINEAR; i++) {
102
+ for (let i = 0; i < MILLION; i++) {
103
103
  list.pushFront(i);
104
104
  }
105
105
  })
@@ -114,33 +114,33 @@ if (isCompetitor) {
114
114
  pq.pop();
115
115
  }
116
116
  })
117
- .add(`CPT DQ ${LINEAR.toLocaleString()} push`, () => {
117
+ .add(`CPT DQ ${MILLION.toLocaleString()} push`, () => {
118
118
  const deque = new CDeque<number>();
119
- for (let i = 0; i < LINEAR; i++) {
119
+ for (let i = 0; i < MILLION; i++) {
120
120
  deque.pushBack(i);
121
121
  }
122
122
  })
123
- .add(`CPT Q ${LINEAR.toLocaleString()} push`, () => {
123
+ .add(`CPT Q ${MILLION.toLocaleString()} push`, () => {
124
124
  const queue = new CQueue<number>();
125
125
 
126
- for (let i = 0; i < LINEAR; i++) {
126
+ for (let i = 0; i < MILLION; i++) {
127
127
  queue.push(i);
128
128
  }
129
129
  })
130
- .add(`CPT ST ${LINEAR.toLocaleString()} push`, () => {
130
+ .add(`CPT ST ${MILLION.toLocaleString()} push`, () => {
131
131
  const queue = new CStack<number>();
132
132
 
133
- for (let i = 0; i < LINEAR; i++) {
133
+ for (let i = 0; i < MILLION; i++) {
134
134
  queue.push(i);
135
135
  }
136
136
  })
137
- .add(`CPT ST ${LINEAR.toLocaleString()} push & pop`, () => {
137
+ .add(`CPT ST ${MILLION.toLocaleString()} push & pop`, () => {
138
138
  const queue = new CStack<number>();
139
139
 
140
- for (let i = 0; i < LINEAR; i++) {
140
+ for (let i = 0; i < MILLION; i++) {
141
141
  queue.push(i);
142
142
  }
143
- for (let i = 0; i < LINEAR; i++) {
143
+ for (let i = 0; i < MILLION; i++) {
144
144
  queue.pop();
145
145
  }
146
146
  });
@@ -5,61 +5,50 @@ import { magnitude } from '../../../utils';
5
5
  import { isCompetitor } from '../../../config';
6
6
 
7
7
  const suite = new Benchmark.Suite();
8
- const { LINEAR } = magnitude;
8
+ const { MILLION } = magnitude;
9
9
 
10
- suite.add(`${LINEAR.toLocaleString()} push`, () => {
10
+ suite.add(`${MILLION.toLocaleString()} push`, () => {
11
11
  const list = new DoublyLinkedList<number>();
12
12
 
13
- for (let i = 0; i < LINEAR; i++) {
14
- list.push(i);
15
- }
13
+ for (let i = 0; i < MILLION; i++) list.push(i);
16
14
  });
17
15
 
18
16
 
19
17
  if (isCompetitor) {
20
- suite.add(`CPT ${LINEAR.toLocaleString()} push`, () => {
18
+ suite.add(`CPT ${MILLION.toLocaleString()} push`, () => {
21
19
  const list = new CLinkedList<number>();
22
20
 
23
- for (let i = 0; i < LINEAR; i++) {
24
- list.pushBack(i);
25
- }
21
+ for (let i = 0; i < MILLION; i++) list.pushBack(i);
26
22
  });
27
23
  }
28
24
 
29
- suite.add(`${LINEAR.toLocaleString()} unshift`, () => {
25
+ suite.add(`${MILLION.toLocaleString()} unshift`, () => {
30
26
  const list = new DoublyLinkedList<number>();
31
27
 
32
- for (let i = 0; i < LINEAR; i++) {
33
- list.unshift(i);
34
- }
28
+ for (let i = 0; i < MILLION; i++) list.unshift(i);
35
29
  });
36
30
 
37
31
  if (isCompetitor) {
38
- suite.add(`CPT ${LINEAR.toLocaleString()} unshift`, () => {
32
+ suite.add(`CPT ${MILLION.toLocaleString()} unshift`, () => {
39
33
  const list = new CLinkedList<number>();
40
34
 
41
- for (let i = 0; i < LINEAR; i++) {
42
- list.pushFront(i);
43
- }
35
+ for (let i = 0; i < MILLION; i++) list.pushFront(i);
44
36
  });
45
37
  }
46
38
 
47
39
  suite
48
- .add(`${LINEAR.toLocaleString()} unshift & shift`, () => {
40
+ .add(`${MILLION.toLocaleString()} unshift & shift`, () => {
49
41
  const list = new DoublyLinkedList<number>();
50
42
 
51
- for (let i = 0; i < LINEAR; i++) {
52
- list.unshift(i);
53
- }
54
- for (let i = 0; i < LINEAR; i++) {
55
- list.shift();
56
- }
43
+ for (let i = 0; i < MILLION; i++) list.unshift(i);
44
+
45
+ for (let i = 0; i < MILLION; i++) list.shift();
57
46
  })
58
- .add(`${LINEAR.toLocaleString()} addBefore`, () => {
47
+ .add(`${MILLION.toLocaleString()} addBefore`, () => {
59
48
  const doublyList = new DoublyLinkedList<number>();
60
49
  let midNode: DoublyLinkedListNode | undefined;
61
- const midIndex = Math.floor(LINEAR / 2);
62
- for (let i = 0; i < LINEAR; i++) {
50
+ const midIndex = Math.floor(MILLION / 2);
51
+ for (let i = 0; i < MILLION; i++) {
63
52
  doublyList.push(i);
64
53
  if (i === midIndex) {
65
54
  midNode = doublyList.getNode(i);
@@ -9,24 +9,16 @@ suite
9
9
  .add(`${MILLION.toLocaleString()} push & shift`, () => {
10
10
  const list = new SinglyLinkedList<number>();
11
11
 
12
- for (let i = 0; i < MILLION; i++) {
13
- list.push(i);
14
- }
12
+ for (let i = 0; i < MILLION; i++) list.push(i);
15
13
 
16
- for (let i = 0; i < MILLION; i++) {
17
- list.shift();
18
- }
14
+ for (let i = 0; i < MILLION; i++) list.shift();
19
15
  })
20
16
  .add(`${TEN_THOUSAND.toLocaleString()} push & pop`, () => {
21
17
  const list = new SinglyLinkedList<number>();
22
18
 
23
- for (let i = 0; i < TEN_THOUSAND; i++) {
24
- list.push(i);
25
- }
19
+ for (let i = 0; i < TEN_THOUSAND; i++) list.push(i);
26
20
 
27
- for (let i = 0; i < TEN_THOUSAND; i++) {
28
- list.pop();
29
- }
21
+ for (let i = 0; i < TEN_THOUSAND; i++) list.pop();
30
22
  })
31
23
  .add(`${TEN_THOUSAND.toLocaleString()} addBefore`, () => {
32
24
  const singlyList = new SinglyLinkedList<number>();
@@ -5,26 +5,26 @@ import { magnitude } from '../../../utils';
5
5
  import { isCompetitor } from '../../../config';
6
6
 
7
7
  export const suite = new Benchmark.Suite();
8
- const { LINEAR, HUNDRED_THOUSAND } = magnitude;
8
+ const { MILLION, HUNDRED_THOUSAND } = magnitude;
9
9
 
10
- suite.add(`${LINEAR.toLocaleString()} push`, () => {
10
+ suite.add(`${MILLION.toLocaleString()} push`, () => {
11
11
  const deque = new Deque<number>();
12
- for (let i = 0; i < LINEAR; i++) deque.push(i);
12
+ for (let i = 0; i < MILLION; i++) deque.push(i);
13
13
  });
14
14
 
15
15
  if (isCompetitor) {
16
- suite.add(`CPT ${LINEAR.toLocaleString()} push`, () => {
16
+ suite.add(`CPT ${MILLION.toLocaleString()} push`, () => {
17
17
  const _deque = new CDeque<number>();
18
- for (let i = 0; i < LINEAR; i++) _deque.pushBack(i);
18
+ for (let i = 0; i < MILLION; i++) _deque.pushBack(i);
19
19
  });
20
20
  }
21
21
 
22
22
 
23
- suite.add(`${LINEAR.toLocaleString()} push & pop`, () => {
23
+ suite.add(`${MILLION.toLocaleString()} push & pop`, () => {
24
24
  const _deque = new Deque<number>();
25
25
 
26
- for (let i = 0; i < LINEAR; i++) _deque.push(i);
27
- for (let i = 0; i < LINEAR; i++) _deque.pop();
26
+ for (let i = 0; i < MILLION; i++) _deque.push(i);
27
+ for (let i = 0; i < MILLION; i++) _deque.pop();
28
28
 
29
29
  })
30
30
  .add(`${HUNDRED_THOUSAND.toLocaleString()} push & shift`, () => {
@@ -5,20 +5,20 @@ import { magnitude } from '../../../utils';
5
5
  import { isCompetitor } from '../../../config';
6
6
 
7
7
  const suite = new Benchmark.Suite();
8
- const { LINEAR, HUNDRED_THOUSAND } = magnitude;
8
+ const { MILLION, HUNDRED_THOUSAND } = magnitude;
9
9
 
10
- suite.add(`${LINEAR.toLocaleString()} push`, () => {
10
+ suite.add(`${MILLION.toLocaleString()} push`, () => {
11
11
  const queue = new Queue<number>();
12
12
 
13
- for (let i = 0; i < LINEAR; i++) {
13
+ for (let i = 0; i < MILLION; i++) {
14
14
  queue.push(i);
15
15
  }
16
16
  });
17
17
  if (isCompetitor) {
18
- suite.add(`CPT ${LINEAR.toLocaleString()} push`, () => {
18
+ suite.add(`CPT ${MILLION.toLocaleString()} push`, () => {
19
19
  const queue = new CQueue<number>();
20
20
 
21
- for (let i = 0; i < LINEAR; i++) {
21
+ for (let i = 0; i < MILLION; i++) {
22
22
  queue.push(i);
23
23
  }
24
24
  });
@@ -5,42 +5,42 @@ import { magnitude } from '../../../utils';
5
5
  import { isCompetitor } from '../../../config';
6
6
 
7
7
  const suite = new Benchmark.Suite();
8
- const { LINEAR } = magnitude;
8
+ const { MILLION } = magnitude;
9
9
 
10
- suite.add(`${LINEAR.toLocaleString()} push`, () => {
10
+ suite.add(`${MILLION.toLocaleString()} push`, () => {
11
11
  const stack = new Stack<number>();
12
12
 
13
- for (let i = 0; i < LINEAR; i++) {
13
+ for (let i = 0; i < MILLION; i++) {
14
14
  stack.push(i);
15
15
  }
16
16
  });
17
17
  if (isCompetitor) {
18
- suite.add(`CPT ${LINEAR.toLocaleString()} push`, () => {
18
+ suite.add(`CPT ${MILLION.toLocaleString()} push`, () => {
19
19
  const queue = new CStack<number>();
20
20
 
21
- for (let i = 0; i < LINEAR; i++) {
21
+ for (let i = 0; i < MILLION; i++) {
22
22
  queue.push(i);
23
23
  }
24
24
  });
25
25
  }
26
- suite.add(`${LINEAR.toLocaleString()} push & pop`, () => {
26
+ suite.add(`${MILLION.toLocaleString()} push & pop`, () => {
27
27
  const queue = new Stack<number>();
28
28
 
29
- for (let i = 0; i < LINEAR; i++) {
29
+ for (let i = 0; i < MILLION; i++) {
30
30
  queue.push(i);
31
31
  }
32
- for (let i = 0; i < LINEAR; i++) {
32
+ for (let i = 0; i < MILLION; i++) {
33
33
  queue.pop();
34
34
  }
35
35
  });
36
36
  if (isCompetitor) {
37
- suite.add(`CPT ${LINEAR.toLocaleString()} push & pop`, () => {
37
+ suite.add(`CPT ${MILLION.toLocaleString()} push & pop`, () => {
38
38
  const queue = new CStack<number>();
39
39
 
40
- for (let i = 0; i < LINEAR; i++) {
40
+ for (let i = 0; i < MILLION; i++) {
41
41
  queue.push(i);
42
42
  }
43
- for (let i = 0; i < LINEAR; i++) {
43
+ for (let i = 0; i < MILLION; i++) {
44
44
  queue.pop();
45
45
  }
46
46
  });
@@ -639,4 +639,19 @@ describe('BinaryTree iterative methods test', () => {
639
639
  const values = binaryTree.values();
640
640
  expect([...values]).toEqual(['b', 'a', 'c']);
641
641
  });
642
+
643
+ test('should iterative method return undefined when the node is null', () => {
644
+ const tree = new BinaryTree()
645
+ tree.addMany([-10, -10, -10, 9, 9, 20, null, null, 15, 7, 8, null, 2, null, 6, null, null, 8, 8, 8]);
646
+ const bfsResult = tree.bfs(undefined, undefined, undefined, true);
647
+ expect(bfsResult).toEqual([
648
+ -10, 9,
649
+ 20, undefined,
650
+ undefined, 15,
651
+ 7, 8,
652
+ undefined, 2,
653
+ undefined, 6,
654
+ undefined, undefined
655
+ ]);
656
+ })
642
657
  });
@@ -66,7 +66,18 @@ class MyGraph<
66
66
  return edge ? undefined : undefined;
67
67
  }
68
68
 
69
- protected _addEdgeOnly(edge: EO): boolean {
69
+ deleteVertex(vertexOrKey: VertexKey | VO): boolean {
70
+ let vertexKey: VertexKey;
71
+ if (this.isVertexKey(vertexOrKey)) {
72
+ vertexKey = vertexOrKey;
73
+ } else {
74
+ vertexKey = vertexOrKey.key;
75
+ }
76
+ this._vertexMap.delete(vertexKey);
77
+ return true;
78
+ }
79
+
80
+ protected _addEdge(edge: EO): boolean {
70
81
  return edge ? true : true;
71
82
  }
72
83
  }
@@ -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.size).toBe(2);
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);
@@ -635,7 +635,7 @@ describe('DirectedGraph iterative Methods', () => {
635
635
  expect(concatenated).toBe(vertexMap.join(''));
636
636
  });
637
637
 
638
- test('Removing an edge of a DirectedGraph should not delete additional edges', () => {
638
+ test('Removing an edge of a DirectedGraph should delete additional edges', () => {
639
639
  const dg = new DirectedGraph();
640
640
  dg.addVertex('hello')
641
641
  dg.addVertex('hi')
@@ -649,9 +649,22 @@ describe('DirectedGraph iterative Methods', () => {
649
649
  dg.deleteEdge('hello', 'hi')
650
650
  expect(dg.getEdge('hello', 'hi')).toBe(undefined)
651
651
  expect(dg.getEdge('hello', 'hey')).toBeInstanceOf(DirectedEdge)
652
+ expect(dg.incomingEdgesOf("Hi")).toEqual([])
652
653
  });
653
654
 
654
- test('Removing a vertex from a UndirectedGraph should remove its edges', () => {
655
+ test('Removing a vertex of a DirectedGraph should delete additional edges', () => {
656
+ const graph = new DirectedGraph();
657
+
658
+ graph.addVertex("Hello");
659
+ graph.addVertex("Hi");
660
+
661
+ graph.addEdge("Hello", "Hi");
662
+ graph.deleteVertex("Hello");
663
+
664
+ expect(graph.incomingEdgesOf("Hi")).toEqual([]);
665
+ })
666
+
667
+ test('Removing a vertex from a DirectedGraph should remove its edges', () => {
655
668
  const dg = new DirectedGraph();
656
669
  dg.addVertex('hello')
657
670
  dg.addVertex('world')
@@ -688,8 +701,53 @@ describe('DirectedGraph getCycles', () => {
688
701
  graph.addEdge('D', 'E');
689
702
  graph.addEdge('E', 'B');
690
703
  const cycles = graph.getCycles();
691
- expect(cycles.size).toBe(1);
692
- expect(cycles.get(2)).toEqual( [{ "key": "B", "value": undefined }, { "key": "D", "value": undefined }, { "key": "E", "value": undefined }]);
704
+ expect(cycles.length).toBe(1);
705
+ expect(cycles[0]).toEqual(["B", "D", "E"]);
693
706
  })
707
+
708
+ test('should simple cycles graph getCycles return correct result', () => {
709
+ const graph = new DirectedGraph();
710
+
711
+ graph.addVertex('A');
712
+ graph.addVertex('B');
713
+ graph.addVertex('C');
714
+ graph.addVertex('D');
715
+
716
+ graph.addEdge('A', 'B');
717
+ graph.addEdge('B', 'C');
718
+ graph.addEdge('C', 'A');
719
+ graph.addEdge('A', 'D');
720
+ graph.addEdge('D', 'C');
721
+ const cycles = graph.getCycles();
722
+ expect(cycles.length).toBe(2)
723
+ expect(cycles).toEqual([["A", "B", "C"], ["A", "D", "C"]])
724
+ });
725
+
726
+ test('should 3 cycles graph getCycles return correct result', () => {
727
+ const graph = new DirectedGraph();
728
+
729
+ graph.addVertex('A');
730
+ graph.addVertex('B');
731
+ graph.addVertex('C');
732
+ graph.addVertex('D');
733
+ graph.addVertex('E');
734
+ graph.addVertex('F');
735
+ graph.addVertex('G');
736
+
737
+ graph.addEdge('A', 'B');
738
+ graph.addEdge('A', 'C');
739
+ graph.addEdge('B', 'D');
740
+ graph.addEdge('C', 'D');
741
+ graph.addEdge('D', 'E');
742
+ graph.addEdge('E', 'B');
743
+ graph.addEdge('B', 'F');
744
+ graph.addEdge('F', 'E');
745
+ graph.addEdge('C', 'G');
746
+ graph.addEdge('G', 'A');
747
+
748
+ const cycles = graph.getCycles();
749
+ expect(cycles.length).toBe(3)
750
+ expect(cycles).toEqual([["A", "C", "G"], ["B", "D", "E"], ["B", "F", "E"]]);
751
+ });
694
752
  })
695
753
 
@@ -190,6 +190,19 @@ describe('UndirectedGraph', () => {
190
190
  expect(dg.getEdge('hello', 'hey')).toBeInstanceOf(UndirectedEdge)
191
191
  });
192
192
 
193
+
194
+ test('Removing a vertex of a DirectedGraph should delete additional edges', () => {
195
+ const graph = new UndirectedGraph();
196
+
197
+ graph.addVertex("Hello");
198
+ graph.addVertex("Hi");
199
+
200
+ graph.addEdge("Hello", "Hi");
201
+ graph.deleteVertex("Hello");
202
+
203
+ expect(graph.edgesOf("Hi")).toEqual([]);
204
+ })
205
+
193
206
  test('Removing a vertex from a UndirectedGraph should remove its edges', () => {
194
207
  const dg = new UndirectedGraph();
195
208
  dg.addVertex('hello')
@@ -237,7 +250,7 @@ describe('cycles, strongly connected components, bridges, articular points in Un
237
250
  const cutVertexes = graph.getCutVertexes();
238
251
  const dfnMap = graph.getDFNMap();
239
252
  const lowMap = graph.getLowMap();
240
- expect(cycles.size).toBe(2);
253
+ expect(cycles.length).toBe(3);
241
254
  expect(scCs.size).toBe(5);
242
255
  expect(bridges.length).toBe(4);
243
256
  expect(cutVertexes.length).toBe(4);
@@ -277,8 +290,52 @@ describe('UndirectedGraph getCycles', () => {
277
290
  graph.addEdge('D', 'E');
278
291
  graph.addEdge('E', 'B');
279
292
  const cycles = graph.getCycles();
280
- expect(cycles.size).toBe(2);
281
- expect(cycles.get(1)).toEqual([{ "key": "A", "value": "A" }, { "key": "B", "value": "B" }, { "key": "D", "value": "D" }, { "key": "C", "value": "C" }]);
282
- expect(cycles.get(2)).toEqual([{ "key": "B", "value": "B" }, { "key": "D", "value": "D" }, { "key": "E", "value": "E" }]);
293
+ expect(cycles.length).toBe(3);
294
+ expect(cycles).toEqual([["A", "B", "D", "C"], ["A", "B", "E", "D", "C"], ["B", "D", "E"]]);
283
295
  })
296
+
297
+ test('should simple cycles graph getCycles return correct result', () => {
298
+ const graph = new UndirectedGraph();
299
+
300
+ graph.addVertex('A');
301
+ graph.addVertex('B');
302
+ graph.addVertex('C');
303
+ graph.addVertex('D');
304
+
305
+ graph.addEdge('A', 'B');
306
+ graph.addEdge('B', 'C');
307
+ graph.addEdge('C', 'A');
308
+ graph.addEdge('A', 'D');
309
+ graph.addEdge('D', 'C');
310
+ const cycles = graph.getCycles();
311
+ expect(cycles.length).toBe(3)
312
+ expect(cycles).toEqual([["A", "B", "C"], ["A", "B", "C", "D"], ["A", "C", "D"]])
313
+ });
314
+
315
+ test('should 3 cycles graph getCycles return correct result', () => {
316
+ const graph = new UndirectedGraph();
317
+
318
+ graph.addVertex('A');
319
+ graph.addVertex('B');
320
+ graph.addVertex('C');
321
+ graph.addVertex('D');
322
+ graph.addVertex('E');
323
+ graph.addVertex('F');
324
+ graph.addVertex('G');
325
+
326
+ graph.addEdge('A', 'B');
327
+ graph.addEdge('A', 'C');
328
+ graph.addEdge('B', 'D');
329
+ graph.addEdge('C', 'D');
330
+ graph.addEdge('D', 'E');
331
+ graph.addEdge('E', 'B');
332
+ graph.addEdge('B', 'F');
333
+ graph.addEdge('F', 'E');
334
+ graph.addEdge('C', 'G');
335
+ graph.addEdge('G', 'A');
336
+
337
+ const cycles = graph.getCycles();
338
+ expect(cycles.length).toBe(10)
339
+ 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"]]);
340
+ });
284
341
  })
@@ -554,4 +554,25 @@ describe('LinkedHashMap setMany, keys, values', () => {
554
554
  test('values', () => {
555
555
  expect([...hm.values()]).toEqual([2, 3, 4, 5, 6])
556
556
  });
557
+
558
+ test('entries', () => {
559
+ expect([...hm.entries()]).toEqual([[2, 2], [3, 3], [4, 4], [5, 5], [6, 6]])
560
+ });
561
+
562
+ test('every', () => {
563
+ expect(hm.every(value => value > 4)).toBe(false)
564
+ });
565
+
566
+ test('some', () => {
567
+ expect(hm.some(value => value > 6)).toBe(false)
568
+ });
569
+
570
+ test('hasValue', () => {
571
+ expect(hm.hasValue(3)).toBe(true)
572
+ expect(hm.hasValue(7)).toBe(false)
573
+ });
574
+
575
+ test('print', () => {
576
+ hm.print();
577
+ });
557
578
  });
@@ -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);minNumHeap.add(6);minNumHeap.add(2);minNumHeap.add(0);minNumHeap.add(5);minNumHeap.add(9);
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);
@@ -424,4 +424,31 @@ describe('iterable methods', () => {
424
424
  expect([...dl.map(element => element * 2)]).toEqual([2, 4, 6]);
425
425
  expect(dl.reduce((accumulator, element) => accumulator + element, 0)).toEqual(6);
426
426
  });
427
+
428
+ test('values', () => {
429
+ const dl = new DoublyLinkedList<number>()
430
+ dl.push(1);
431
+ dl.push(2);
432
+ dl.push(3);
433
+ dl.delete(2);
434
+ dl.unshift(0);
435
+ dl.shift();
436
+ dl.pop();
437
+ dl.unshift(3);
438
+ expect([...dl.values()]).toEqual([3, 1])
439
+ })
440
+
441
+ test('some', () => {
442
+ const dl = new DoublyLinkedList<number>()
443
+ dl.push(1);
444
+ dl.push(2);
445
+ dl.push(3);
446
+ dl.delete(2);
447
+ dl.unshift(0);
448
+ dl.shift();
449
+ dl.pop();
450
+ dl.unshift(3);
451
+ expect(dl.some(value => value > 1)).toBe(true)
452
+ expect(dl.some(value => value > 100)).toBe(false)
453
+ })
427
454
  });
@@ -2,16 +2,16 @@ import { AnyFunction } from '../types';
2
2
  import { isDebugTest } from '../config';
3
3
 
4
4
  const isDebug = isDebugTest;
5
- const orderReducedBy = 1; // reduction of bigO's order compared to the baseline bigO
5
+ // const orderReducedBy = 1; // reduction of bigO's order compared to the baseline bigO
6
6
 
7
7
  export const magnitude = {
8
- CONSTANT: Math.pow(10, 9),
9
- LOG_N: Math.pow(10, 8 - orderReducedBy),
10
- LINEAR: Math.pow(10, 7 - orderReducedBy),
11
- N_LOG_N: Math.pow(10, 4 - orderReducedBy),
12
- SQUARED: Math.pow(10, 3 - orderReducedBy),
13
- CUBED: Math.pow(10, 2 - orderReducedBy),
14
- FACTORIAL: 20 - orderReducedBy,
8
+ // CONSTANT: Math.pow(10, 9),
9
+ // LOG_N: Math.pow(10, 8 - orderReducedBy),
10
+ // LINEAR: Math.pow(10, 7 - orderReducedBy),
11
+ // N_LOG_N: Math.pow(10, 4 - orderReducedBy),
12
+ // SQUARED: Math.pow(10, 3 - orderReducedBy),
13
+ // CUBED: Math.pow(10, 2 - orderReducedBy),
14
+ // FACTORIAL: 20 - orderReducedBy,
15
15
  THOUSAND: 1000,
16
16
  TEN_THOUSAND: 10000,
17
17
  HUNDRED_THOUSAND: 100000,
@@ -21,12 +21,12 @@ export const magnitude = {
21
21
  };
22
22
 
23
23
  export const bigO = {
24
- CONSTANT: magnitude.CONSTANT / 100000,
25
- LOG_N: Math.log2(magnitude.LOG_N) / 1000,
26
- LINEAR: magnitude.LINEAR / 1000,
27
- N_LOG_N: (magnitude.N_LOG_N * Math.log2(magnitude.LOG_N)) / 1000,
28
- SQUARED: Math.pow(magnitude.SQUARED, 2) / 1000,
29
- CUBED: Math.pow(magnitude.SQUARED, 3) / 1000,
24
+ // CONSTANT: magnitude.CONSTANT / 100000,
25
+ // LOG_N: Math.log2(magnitude.LOG_N) / 1000,
26
+ // LINEAR: magnitude.LINEAR / 1000,
27
+ // N_LOG_N: (magnitude.N_LOG_N * Math.log2(magnitude.LOG_N)) / 1000,
28
+ // SQUARED: Math.pow(magnitude.SQUARED, 2) / 1000,
29
+ // CUBED: Math.pow(magnitude.SQUARED, 3) / 1000,
30
30
  FACTORIAL: 10000
31
31
  };
32
32