data-structure-typed 1.33.0 → 1.33.5
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/{.eslintrc.json → .eslintrc.js} +2 -1
- package/.github/workflows/ci.yml +15 -3
- package/.github/workflows/release-package.yml +32 -0
- package/{.prettierrc → .prettierrc.js} +1 -1
- package/CHANGELOG.md +5 -1
- package/README.md +196 -257
- package/coverage/coverage-final.json +64 -64
- package/coverage/coverage-summary.json +16 -16
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +1 -16
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/rb-tree.js +3 -4
- package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/data-structures/graph/abstract-graph.js +12 -12
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/data-structures/hash/hash-table.js +107 -2
- package/dist/data-structures/hash/hash-table.js.map +1 -1
- package/dist/data-structures/heap/max-heap.js.map +1 -1
- package/dist/data-structures/heap/min-heap.js.map +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +30 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/data-structures/matrix/matrix2d.js +5 -8
- package/dist/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/priority-queue.js +6 -6
- package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/data-structures/queue/deque.js.map +1 -1
- package/docs/index.html +196 -256
- package/docs/modules.html +2 -0
- package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +8 -18
- package/lib/data-structures/binary-tree/abstract-binary-tree.js +5 -23
- package/lib/data-structures/binary-tree/avl-tree.d.ts +7 -10
- package/lib/data-structures/binary-tree/avl-tree.js +6 -9
- package/lib/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/lib/data-structures/binary-tree/bst.d.ts +2 -2
- package/lib/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/lib/data-structures/binary-tree/rb-tree.js +3 -3
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +3 -3
- package/lib/data-structures/binary-tree/tree-multiset.js +1 -1
- package/lib/data-structures/graph/abstract-graph.d.ts +10 -10
- package/lib/data-structures/graph/abstract-graph.js +2 -2
- package/lib/data-structures/graph/directed-graph.d.ts +6 -6
- package/lib/data-structures/graph/directed-graph.js +2 -2
- package/lib/data-structures/graph/map-graph.d.ts +6 -6
- package/lib/data-structures/graph/map-graph.js +2 -2
- package/lib/data-structures/graph/undirected-graph.d.ts +6 -6
- package/lib/data-structures/graph/undirected-graph.js +2 -2
- package/lib/data-structures/hash/hash-table.d.ts +61 -1
- package/lib/data-structures/hash/hash-table.js +136 -0
- package/lib/data-structures/heap/heap.d.ts +31 -31
- package/lib/data-structures/heap/heap.js +11 -11
- package/lib/data-structures/heap/max-heap.d.ts +4 -4
- package/lib/data-structures/heap/max-heap.js +1 -1
- package/lib/data-structures/heap/min-heap.d.ts +4 -4
- package/lib/data-structures/heap/min-heap.js +1 -1
- package/lib/data-structures/linked-list/doubly-linked-list.d.ts +93 -54
- package/lib/data-structures/linked-list/doubly-linked-list.js +79 -22
- package/lib/data-structures/linked-list/singly-linked-list.d.ts +46 -46
- package/lib/data-structures/linked-list/singly-linked-list.js +20 -20
- package/lib/data-structures/matrix/matrix.d.ts +3 -3
- package/lib/data-structures/matrix/matrix2d.d.ts +1 -1
- package/lib/data-structures/matrix/matrix2d.js +3 -2
- package/lib/data-structures/priority-queue/max-priority-queue.d.ts +4 -4
- package/lib/data-structures/priority-queue/min-priority-queue.d.ts +4 -4
- package/lib/data-structures/priority-queue/priority-queue.d.ts +26 -26
- package/lib/data-structures/priority-queue/priority-queue.js +8 -8
- package/lib/data-structures/queue/deque.d.ts +30 -30
- package/lib/data-structures/queue/deque.js +7 -7
- package/lib/data-structures/queue/queue.d.ts +27 -27
- package/lib/data-structures/queue/queue.js +8 -8
- package/lib/data-structures/stack/stack.d.ts +15 -15
- package/lib/data-structures/stack/stack.js +6 -6
- package/lib/data-structures/tree/tree.d.ts +7 -7
- package/lib/interfaces/abstract-binary-tree.d.ts +0 -1
- package/lib/interfaces/avl-tree.d.ts +1 -1
- package/lib/types/data-structures/navigator.d.ts +1 -1
- package/package.json +68 -62
- package/src/data-structures/binary-tree/aa-tree.ts +1 -0
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +1608 -0
- package/src/data-structures/binary-tree/avl-tree.ts +307 -0
- package/src/data-structures/binary-tree/b-tree.ts +1 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +76 -0
- package/src/data-structures/binary-tree/binary-tree.ts +47 -0
- package/src/data-structures/binary-tree/bst.ts +537 -0
- package/src/data-structures/binary-tree/index.ts +12 -0
- package/src/data-structures/binary-tree/rb-tree.ts +366 -0
- package/src/data-structures/binary-tree/segment-tree.ts +242 -0
- package/src/data-structures/binary-tree/splay-tree.ts +1 -0
- package/src/data-structures/binary-tree/tree-multiset.ts +700 -0
- package/src/data-structures/binary-tree/two-three-tree.ts +1 -0
- package/src/data-structures/graph/abstract-graph.ts +1040 -0
- package/src/data-structures/graph/directed-graph.ts +470 -0
- package/src/data-structures/graph/index.ts +4 -0
- package/src/data-structures/graph/map-graph.ts +129 -0
- package/src/data-structures/graph/undirected-graph.ts +274 -0
- package/src/data-structures/hash/coordinate-map.ts +67 -0
- package/src/data-structures/hash/coordinate-set.ts +56 -0
- package/src/data-structures/hash/hash-table.ts +157 -0
- package/src/data-structures/hash/index.ts +6 -0
- package/src/data-structures/hash/pair.ts +1 -0
- package/src/data-structures/hash/tree-map.ts +1 -0
- package/src/data-structures/hash/tree-set.ts +1 -0
- package/src/data-structures/heap/heap.ts +212 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +31 -0
- package/src/data-structures/heap/min-heap.ts +32 -0
- package/src/data-structures/index.ts +11 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +636 -0
- package/src/data-structures/linked-list/index.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +501 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +1 -0
- package/src/data-structures/matrix/index.ts +4 -0
- package/src/data-structures/matrix/matrix.ts +27 -0
- package/src/data-structures/matrix/matrix2d.ts +213 -0
- package/src/data-structures/matrix/navigator.ts +121 -0
- package/src/data-structures/matrix/vector2d.ts +316 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +56 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/priority-queue.ts +359 -0
- package/src/data-structures/queue/deque.ts +297 -0
- package/src/data-structures/queue/index.ts +2 -0
- package/src/data-structures/queue/queue.ts +191 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +98 -0
- package/src/data-structures/tree/index.ts +1 -0
- package/src/data-structures/tree/tree.ts +69 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +225 -0
- package/src/index.ts +4 -0
- package/src/interfaces/abstract-binary-tree.ts +189 -0
- package/src/interfaces/abstract-graph.ts +31 -0
- package/src/interfaces/avl-tree.ts +25 -0
- package/src/interfaces/binary-tree.ts +6 -0
- package/src/interfaces/bst.ts +31 -0
- package/src/interfaces/directed-graph.ts +20 -0
- package/src/interfaces/doubly-linked-list.ts +1 -0
- package/src/interfaces/heap.ts +1 -0
- package/src/interfaces/index.ts +15 -0
- package/src/interfaces/navigator.ts +1 -0
- package/src/interfaces/priority-queue.ts +1 -0
- package/src/interfaces/rb-tree.ts +9 -0
- package/src/interfaces/segment-tree.ts +1 -0
- package/src/interfaces/singly-linked-list.ts +1 -0
- package/src/interfaces/tree-multiset.ts +7 -0
- package/src/interfaces/undirected-graph.ts +6 -0
- package/src/types/data-structures/abstract-binary-tree.ts +50 -0
- package/src/types/data-structures/abstract-graph.ts +11 -0
- package/src/types/data-structures/avl-tree.ts +5 -0
- package/src/types/data-structures/binary-tree.ts +5 -0
- package/src/types/data-structures/bst.ts +13 -0
- package/src/types/data-structures/directed-graph.ts +8 -0
- package/src/types/data-structures/doubly-linked-list.ts +1 -0
- package/src/types/data-structures/heap.ts +5 -0
- package/src/types/data-structures/index.ts +15 -0
- package/src/types/data-structures/map-graph.ts +1 -0
- package/src/types/data-structures/navigator.ts +13 -0
- package/src/types/data-structures/priority-queue.ts +9 -0
- package/src/types/data-structures/rb-tree.ts +8 -0
- package/src/types/data-structures/segment-tree.ts +1 -0
- package/src/types/data-structures/singly-linked-list.ts +1 -0
- package/src/types/data-structures/tree-multiset.ts +6 -0
- package/src/types/helpers.ts +1 -0
- package/src/types/index.ts +3 -0
- package/src/types/utils/index.ts +2 -0
- package/src/types/utils/utils.ts +6 -0
- package/src/types/utils/validate-type.ts +35 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/utils.ts +79 -0
- package/test/integration/avl-tree.test.ts +14 -17
- package/test/integration/bst.test.ts +50 -41
- package/test/integration/heap.test.js +0 -3
- package/test/integration/index.html +6 -6
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +14 -17
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +142 -0
- package/test/unit/data-structures/binary-tree/bst.test.ts +50 -41
- package/test/unit/data-structures/binary-tree/overall.test.ts +36 -28
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +43 -0
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +23 -12
- package/test/unit/data-structures/graph/directed-graph.test.ts +27 -25
- package/test/unit/data-structures/graph/map-graph.test.ts +4 -5
- package/test/unit/data-structures/graph/overall.test.ts +10 -11
- package/test/unit/data-structures/graph/undirected-graph.test.ts +0 -1
- package/test/unit/data-structures/hash/coordinate-map.test.ts +54 -0
- package/test/unit/data-structures/hash/coordinate-set.test.ts +41 -0
- package/test/unit/data-structures/hash/hash-table.test.ts +97 -0
- package/test/unit/data-structures/heap/heap.test.ts +7 -8
- package/test/unit/data-structures/heap/max-heap.test.ts +7 -5
- package/test/unit/data-structures/heap/min-heap.test.ts +6 -5
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +8 -9
- package/test/unit/data-structures/linked-list/linked-list.test.ts +2 -4
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +57 -7
- package/test/unit/data-structures/linked-list/skip-linked-list.test.ts +3 -3
- package/test/unit/data-structures/matrix/matrix.test.ts +54 -0
- package/test/unit/data-structures/matrix/matrix2d.test.ts +138 -0
- package/test/unit/data-structures/matrix/navigator.test.ts +79 -0
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +5 -7
- package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +13 -13
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +8 -8
- package/test/unit/data-structures/queue/deque.test.ts +130 -0
- package/test/unit/data-structures/queue/queue.test.ts +167 -4
- package/test/unit/data-structures/stack/stack.test.ts +67 -0
- package/test/unit/data-structures/tree/tree.test.ts +39 -0
- package/test/unit/data-structures/trie/trie.test.ts +95 -0
- package/test/utils/magnitude.ts +3 -3
- package/tsconfig.json +3 -12
- package/tsconfig.prod.json +25 -0
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
- package/.auto-changelog +0 -9
- package/.gitattributes +0 -112
- package/.idea/data-structure-typed.iml +0 -19
- package/.idea/inspectionProfiles/Project_Default.xml +0 -6
- package/.idea/misc.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/.prettierignore +0 -6
- package/webpack.config.js +0 -28
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// import {RBTree, RBTreeNode} from '../../../../src';
|
|
2
|
+
|
|
3
|
+
describe('Red-Black Tree Tests', () => {
|
|
4
|
+
// let tree: RBTree<RBTreeNode<number>>;
|
|
5
|
+
//
|
|
6
|
+
// beforeEach(() => {
|
|
7
|
+
// tree = new RBTree<RBTreeNode<number>>();
|
|
8
|
+
// });
|
|
9
|
+
|
|
10
|
+
test('Insertion and In-order Traversal', () => {
|
|
11
|
+
// tree.add(5);
|
|
12
|
+
// tree.add(3);
|
|
13
|
+
// tree.add(7);
|
|
14
|
+
// tree.add(2);
|
|
15
|
+
// tree.add(4);
|
|
16
|
+
// tree.add(6);
|
|
17
|
+
// tree.add(8);
|
|
18
|
+
//
|
|
19
|
+
// const inOrderTraversal: number[] = tree.DFS('in')
|
|
20
|
+
//
|
|
21
|
+
// expect(inOrderTraversal).toEqual([2, 3, 4, 5, 6, 7, 8]);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test('Deletion', () => {
|
|
25
|
+
// tree.add(5);
|
|
26
|
+
// tree.add(3);
|
|
27
|
+
// tree.add(7);
|
|
28
|
+
// tree.add(2);
|
|
29
|
+
// tree.add(4);
|
|
30
|
+
// tree.add(6);
|
|
31
|
+
// tree.add(8);
|
|
32
|
+
//
|
|
33
|
+
// // Delete a node (e.g., 3) and check if it's gone
|
|
34
|
+
// tree.remove(3);
|
|
35
|
+
// expect(tree.has(3)).toBe(false);
|
|
36
|
+
//
|
|
37
|
+
// // Perform in-order traversal to check if the tree is still balanced
|
|
38
|
+
// const inOrderTraversal: number[] = tree.DFS('in');
|
|
39
|
+
//
|
|
40
|
+
//
|
|
41
|
+
// expect(inOrderTraversal).toEqual([2, 4, 5, 6, 7, 8]);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
@@ -15,7 +15,7 @@ describe('TreeMultiset operations test', () => {
|
|
|
15
15
|
|
|
16
16
|
expect(treeMultiset.size).toBe(16);
|
|
17
17
|
expect(treeMultiset.count).toBe(18);
|
|
18
|
-
expect(treeMultiset.BFS('id'))
|
|
18
|
+
expect(treeMultiset.BFS('id'));
|
|
19
19
|
|
|
20
20
|
expect(treeMultiset.has(6));
|
|
21
21
|
|
|
@@ -44,7 +44,6 @@ describe('TreeMultiset operations test', () => {
|
|
|
44
44
|
const lesserSum = treeMultiset.lesserSum(10);
|
|
45
45
|
expect(lesserSum).toBe(45);
|
|
46
46
|
|
|
47
|
-
|
|
48
47
|
expect(node15 instanceof TreeMultisetNode);
|
|
49
48
|
if (node15 instanceof TreeMultisetNode) {
|
|
50
49
|
const subTreeAdd = treeMultiset.subTreeAddCount(15, 1);
|
|
@@ -204,17 +203,31 @@ describe('TreeMultiset operations test', () => {
|
|
|
204
203
|
});
|
|
205
204
|
|
|
206
205
|
it('should perform various operations on a Binary Search Tree with object values', () => {
|
|
207
|
-
const objTreeMultiset = new TreeMultiset<TreeMultisetNode<{
|
|
206
|
+
const objTreeMultiset = new TreeMultiset<TreeMultisetNode<{id: number; keyA: number}>>();
|
|
208
207
|
expect(objTreeMultiset).toBeInstanceOf(TreeMultiset);
|
|
209
208
|
objTreeMultiset.add(11, {id: 11, keyA: 11});
|
|
210
209
|
objTreeMultiset.add(3, {id: 3, keyA: 3});
|
|
211
|
-
const values = [
|
|
212
|
-
{id:
|
|
213
|
-
{id:
|
|
214
|
-
{id:
|
|
215
|
-
{id:
|
|
216
|
-
|
|
217
|
-
|
|
210
|
+
const values = [
|
|
211
|
+
{id: 15, keyA: 15},
|
|
212
|
+
{id: 1, keyA: 1},
|
|
213
|
+
{id: 8, keyA: 8},
|
|
214
|
+
{id: 13, keyA: 13},
|
|
215
|
+
{id: 16, keyA: 16},
|
|
216
|
+
{id: 2, keyA: 2},
|
|
217
|
+
{id: 6, keyA: 6},
|
|
218
|
+
{id: 9, keyA: 9},
|
|
219
|
+
{id: 12, keyA: 12},
|
|
220
|
+
{id: 14, keyA: 14},
|
|
221
|
+
{id: 4, keyA: 4},
|
|
222
|
+
{id: 7, keyA: 7},
|
|
223
|
+
{id: 10, keyA: 10},
|
|
224
|
+
{id: 5, keyA: 5}
|
|
225
|
+
];
|
|
226
|
+
|
|
227
|
+
objTreeMultiset.addMany(
|
|
228
|
+
values.map(item => item.id),
|
|
229
|
+
values
|
|
230
|
+
);
|
|
218
231
|
|
|
219
232
|
expect(objTreeMultiset.root).toBeInstanceOf(TreeMultisetNode);
|
|
220
233
|
|
|
@@ -414,7 +427,6 @@ describe('TreeMultiset Performance test', function () {
|
|
|
414
427
|
// }
|
|
415
428
|
|
|
416
429
|
it(`Observe the time consumption of TreeMultiset.add fitting O(n log n)`, function () {
|
|
417
|
-
|
|
418
430
|
// // Create a benchmark suite
|
|
419
431
|
// const suite = new Benchmark.Suite();
|
|
420
432
|
// // Define a function to generate a random array of a given size
|
|
@@ -446,5 +458,4 @@ describe('TreeMultiset Performance test', function () {
|
|
|
446
458
|
// })
|
|
447
459
|
// .run({async: true});
|
|
448
460
|
});
|
|
449
|
-
|
|
450
461
|
});
|
|
@@ -7,7 +7,6 @@ describe('DirectedGraph Operation Test', () => {
|
|
|
7
7
|
graph = new DirectedGraph();
|
|
8
8
|
});
|
|
9
9
|
|
|
10
|
-
|
|
11
10
|
it('should add vertices', () => {
|
|
12
11
|
const vertex1 = new DirectedVertex('A');
|
|
13
12
|
const vertex2 = new DirectedVertex('B');
|
|
@@ -64,7 +63,6 @@ describe('DirectedGraph Operation Test', () => {
|
|
|
64
63
|
});
|
|
65
64
|
|
|
66
65
|
class MyVertex<V extends string> extends DirectedVertex<V> {
|
|
67
|
-
|
|
68
66
|
constructor(id: VertexId, val?: V) {
|
|
69
67
|
super(id, val);
|
|
70
68
|
this._data = val;
|
|
@@ -82,7 +80,6 @@ class MyVertex<V extends string> extends DirectedVertex<V> {
|
|
|
82
80
|
}
|
|
83
81
|
|
|
84
82
|
class MyEdge<E extends string> extends DirectedEdge<E> {
|
|
85
|
-
|
|
86
83
|
constructor(v1: VertexId, v2: VertexId, weight?: number, val?: E) {
|
|
87
84
|
super(v1, v2, weight, val);
|
|
88
85
|
this._data = val;
|
|
@@ -125,7 +122,6 @@ describe('Inherit from DirectedGraph and perform operations', () => {
|
|
|
125
122
|
myGraph.addVertex(new MyVertex(7, 'data7'));
|
|
126
123
|
myGraph.addVertex(new MyVertex(8, 'data8'));
|
|
127
124
|
myGraph.addVertex(new MyVertex(9, 'data9'));
|
|
128
|
-
|
|
129
125
|
});
|
|
130
126
|
|
|
131
127
|
it('Add edges', () => {
|
|
@@ -157,13 +153,10 @@ describe('Inherit from DirectedGraph and perform operations', () => {
|
|
|
157
153
|
expect(edge1).toEqual(edge2);
|
|
158
154
|
expect(edge3).toBeNull();
|
|
159
155
|
}
|
|
160
|
-
|
|
161
156
|
});
|
|
162
157
|
|
|
163
158
|
it('Edge set and vertex set', () => {
|
|
164
|
-
|
|
165
|
-
const vertices = myGraph.vertices;
|
|
166
|
-
|
|
159
|
+
expect(true).toBeTruthy();
|
|
167
160
|
});
|
|
168
161
|
|
|
169
162
|
it('Remove edge between vertices', () => {
|
|
@@ -177,13 +170,12 @@ describe('Inherit from DirectedGraph and perform operations', () => {
|
|
|
177
170
|
expect(removedEdge).toBeInstanceOf(MyEdge);
|
|
178
171
|
if (removedEdge) {
|
|
179
172
|
removedEdge && expect(removedEdge.val).toBe('edge-data1-2');
|
|
180
|
-
removedEdge && expect(removedEdge.src).toBe(1)
|
|
173
|
+
removedEdge && expect(removedEdge.src).toBe(1);
|
|
181
174
|
}
|
|
182
175
|
expect(edgeAfterRemoval).toBeNull();
|
|
183
176
|
});
|
|
184
177
|
|
|
185
178
|
it('Topological sort', () => {
|
|
186
|
-
|
|
187
179
|
const sorted = myGraph.topologicalSort();
|
|
188
180
|
|
|
189
181
|
expect(sorted).toBeInstanceOf(Array);
|
|
@@ -193,15 +185,12 @@ describe('Inherit from DirectedGraph and perform operations', () => {
|
|
|
193
185
|
sorted[3] instanceof MyVertex && expect(sorted[3].data).toBe('data6');
|
|
194
186
|
sorted[8] instanceof MyVertex && expect(sorted[8].id).toBe(1);
|
|
195
187
|
}
|
|
196
|
-
|
|
197
188
|
});
|
|
198
189
|
|
|
199
190
|
it('Minimum path between vertices', () => {
|
|
200
191
|
myGraph.addVertex(new MyVertex(1, 'data1'));
|
|
201
192
|
myGraph.addVertex(new MyVertex(2, 'data2'));
|
|
202
193
|
myGraph.addEdge(new MyEdge(1, 2, 10, 'edge-data1-2'));
|
|
203
|
-
|
|
204
|
-
const minPath = myGraph.getMinPathBetween(1, 2);
|
|
205
194
|
});
|
|
206
195
|
|
|
207
196
|
it('All paths between vertices', () => {
|
|
@@ -210,8 +199,6 @@ describe('Inherit from DirectedGraph and perform operations', () => {
|
|
|
210
199
|
myGraph.addVertex(new MyVertex(2, 'data2'));
|
|
211
200
|
myGraph.addEdge(new MyEdge(1, 2, 10, 'edge-data1-2'));
|
|
212
201
|
|
|
213
|
-
const allPaths = myGraph.getAllPathsBetween(1, 2);
|
|
214
|
-
|
|
215
202
|
// Add expect statements here to verify the allPaths
|
|
216
203
|
});
|
|
217
204
|
});
|
|
@@ -308,7 +295,6 @@ describe('Inherit from DirectedGraph and perform operations test2.', () => {
|
|
|
308
295
|
expect(min).toBe(Infinity);
|
|
309
296
|
expect(minPath).toBeInstanceOf(Array);
|
|
310
297
|
|
|
311
|
-
|
|
312
298
|
const floydResult = myGraph.floyd();
|
|
313
299
|
expect(floydResult).toBeTruthy();
|
|
314
300
|
if (floydResult) {
|
|
@@ -320,9 +306,29 @@ describe('Inherit from DirectedGraph and perform operations test2.', () => {
|
|
|
320
306
|
expect(costs[2]).toEqual([3, 15, 38, 17, 35, Infinity, 64, Infinity, 22]);
|
|
321
307
|
expect(costs[3]).toEqual([123, 135, 120, 137, 155, Infinity, 47, Infinity, 126]);
|
|
322
308
|
expect(costs[4]).toEqual([133, 145, 130, 147, 165, Infinity, 57, Infinity, 136]);
|
|
323
|
-
expect(costs[5]).toEqual([
|
|
309
|
+
expect(costs[5]).toEqual([
|
|
310
|
+
Infinity,
|
|
311
|
+
Infinity,
|
|
312
|
+
Infinity,
|
|
313
|
+
Infinity,
|
|
314
|
+
Infinity,
|
|
315
|
+
Infinity,
|
|
316
|
+
Infinity,
|
|
317
|
+
Infinity,
|
|
318
|
+
Infinity
|
|
319
|
+
]);
|
|
324
320
|
expect(costs[6]).toEqual([76, 88, 73, 90, 108, Infinity, 137, Infinity, 79]);
|
|
325
|
-
expect(costs[7]).toEqual([
|
|
321
|
+
expect(costs[7]).toEqual([
|
|
322
|
+
Infinity,
|
|
323
|
+
Infinity,
|
|
324
|
+
Infinity,
|
|
325
|
+
Infinity,
|
|
326
|
+
Infinity,
|
|
327
|
+
Infinity,
|
|
328
|
+
Infinity,
|
|
329
|
+
Infinity,
|
|
330
|
+
Infinity
|
|
331
|
+
]);
|
|
326
332
|
expect(costs[8]).toEqual([173, 185, 170, 187, 205, Infinity, 97, Infinity, 176]);
|
|
327
333
|
|
|
328
334
|
expect(predecessor).toBeInstanceOf(Array);
|
|
@@ -338,7 +344,7 @@ describe('Inherit from DirectedGraph and perform operations test2.', () => {
|
|
|
338
344
|
|
|
339
345
|
expect(dijkstraRes12tt).toBeTruthy();
|
|
340
346
|
if (dijkstraRes12tt) {
|
|
341
|
-
const {distMap, minDist, minPath, paths
|
|
347
|
+
const {distMap, minDist, minPath, paths} = dijkstraRes12tt;
|
|
342
348
|
expect(distMap).toBeInstanceOf(Map);
|
|
343
349
|
expect(distMap.size).toBe(9);
|
|
344
350
|
expect(distMap.get(vertex1)).toBe(0);
|
|
@@ -383,14 +389,13 @@ describe('Inherit from DirectedGraph and perform operations test2.', () => {
|
|
|
383
389
|
expect(paths[8]).toBeInstanceOf(Array);
|
|
384
390
|
expect(paths[8][0]).toBe(vertex1);
|
|
385
391
|
expect(paths[8][1]).toBe(vertex9);
|
|
386
|
-
|
|
387
392
|
}
|
|
388
393
|
|
|
389
394
|
const dijkstraRes1ntt = myGraph.dijkstra(1, null, true, true);
|
|
390
395
|
|
|
391
396
|
expect(dijkstraRes1ntt).toBeTruthy();
|
|
392
397
|
if (dijkstraRes1ntt) {
|
|
393
|
-
const {distMap, minDist, minPath, paths
|
|
398
|
+
const {distMap, minDist, minPath, paths} = dijkstraRes1ntt;
|
|
394
399
|
expect(distMap).toBeInstanceOf(Map);
|
|
395
400
|
expect(distMap.size).toBe(9);
|
|
396
401
|
expect(distMap.get(vertex1)).toBe(0);
|
|
@@ -447,13 +452,12 @@ describe('Inherit from DirectedGraph and perform operations test2.', () => {
|
|
|
447
452
|
expect(paths[8]).toBeInstanceOf(Array);
|
|
448
453
|
expect(paths[8][0]).toBe(vertex1);
|
|
449
454
|
expect(paths[8][1]).toBe(vertex9);
|
|
450
|
-
|
|
451
455
|
}
|
|
452
456
|
|
|
453
457
|
const dijkstraWithoutHeapRes1ntt = myGraph.dijkstraWithoutHeap(1, null, true, true);
|
|
454
458
|
expect(dijkstraWithoutHeapRes1ntt).toBeTruthy();
|
|
455
459
|
if (dijkstraWithoutHeapRes1ntt) {
|
|
456
|
-
const {distMap, minDist, minPath, paths
|
|
460
|
+
const {distMap, minDist, minPath, paths} = dijkstraWithoutHeapRes1ntt;
|
|
457
461
|
expect(distMap).toBeInstanceOf(Map);
|
|
458
462
|
expect(distMap.size).toBe(9);
|
|
459
463
|
expect(distMap.get(vertex1)).toBe(0);
|
|
@@ -510,8 +514,6 @@ describe('Inherit from DirectedGraph and perform operations test2.', () => {
|
|
|
510
514
|
expect(paths[8]).toBeInstanceOf(Array);
|
|
511
515
|
expect(paths[8][0]).toBe(vertex1);
|
|
512
516
|
expect(paths[8][1]).toBe(vertex9);
|
|
513
|
-
|
|
514
517
|
}
|
|
515
518
|
});
|
|
516
519
|
});
|
|
517
|
-
|
|
@@ -5,13 +5,13 @@ describe('MapGraph Operation Test', () => {
|
|
|
5
5
|
const mapGraph = new MapGraph([5.500338, 100.173665]);
|
|
6
6
|
|
|
7
7
|
mapGraph.addVertex(new MapVertex('Surin', 5.466724, 100.274805));
|
|
8
|
-
mapGraph.addVertex(new MapVertex('Batu Feringgi Beach', 5.475141, 100.
|
|
8
|
+
mapGraph.addVertex(new MapVertex('Batu Feringgi Beach', 5.475141, 100.27667));
|
|
9
9
|
mapGraph.addVertex(new MapVertex('Lotus', 5.459044, 100.308767));
|
|
10
10
|
mapGraph.addVertex(new MapVertex('The Breeza', 5.454197, 100.307859));
|
|
11
|
-
mapGraph.addVertex(new MapVertex('Hard Rock Hotel', 5.
|
|
12
|
-
mapGraph.addVertex(new MapVertex('Mira', 5.456749, 100.
|
|
11
|
+
mapGraph.addVertex(new MapVertex('Hard Rock Hotel', 5.46785, 100.241876));
|
|
12
|
+
mapGraph.addVertex(new MapVertex('Mira', 5.456749, 100.28665));
|
|
13
13
|
mapGraph.addVertex(new MapVertex('Penang Bible Church', 5.428683, 100.314825));
|
|
14
|
-
mapGraph.addVertex(new MapVertex('Queensbay', 5.
|
|
14
|
+
mapGraph.addVertex(new MapVertex('Queensbay', 5.33276, 100.306651));
|
|
15
15
|
mapGraph.addVertex(new MapVertex('Saanen Goat Farm', 5.405738, 100.207699));
|
|
16
16
|
mapGraph.addVertex(new MapVertex('Trinity Auto', 5.401126, 100.303739));
|
|
17
17
|
mapGraph.addVertex(new MapVertex('Penang Airport', 5.293185, 100.265772));
|
|
@@ -42,5 +42,4 @@ describe('MapGraph Operation Test', () => {
|
|
|
42
42
|
expect(surinToSaanenGoatFarmViaDij?.minPath.map(v => v.id)).toEqual(expected2);
|
|
43
43
|
expect(surinToSaanenGoatFarmViaDij?.minDist).toBe(25.2);
|
|
44
44
|
});
|
|
45
|
-
|
|
46
45
|
});
|
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
import {DirectedGraph, UndirectedGraph} from '../../../../src';
|
|
2
2
|
|
|
3
3
|
describe('Overall Graph Operation Test', () => {
|
|
4
|
-
|
|
5
4
|
it('Overall DirectedGraph Operation Test', () => {
|
|
6
5
|
const graph = new DirectedGraph();
|
|
7
6
|
|
|
8
7
|
graph.addVertex('A');
|
|
9
8
|
graph.addVertex('B');
|
|
10
9
|
|
|
11
|
-
graph.hasVertex('A');
|
|
12
|
-
graph.hasVertex('B');
|
|
13
|
-
graph.hasVertex('C');
|
|
14
|
-
expect(graph.hasVertex('A')).toBe(true);
|
|
15
|
-
expect(graph.hasVertex('B')).toBe(true);
|
|
16
|
-
expect(graph.hasVertex('C')).toBe(false);
|
|
10
|
+
graph.hasVertex('A'); // true
|
|
11
|
+
graph.hasVertex('B'); // true
|
|
12
|
+
graph.hasVertex('C'); // false
|
|
13
|
+
expect(graph.hasVertex('A')).toBe(true); // true
|
|
14
|
+
expect(graph.hasVertex('B')).toBe(true); // true
|
|
15
|
+
expect(graph.hasVertex('C')).toBe(false); // false
|
|
17
16
|
|
|
18
17
|
graph.addEdge('A', 'B');
|
|
19
18
|
graph.hasEdge('A', 'B'); // true
|
|
@@ -22,8 +21,8 @@ describe('Overall Graph Operation Test', () => {
|
|
|
22
21
|
expect(graph.hasEdge('B', 'A')).toBe(false); // false
|
|
23
22
|
|
|
24
23
|
graph.removeEdgeSrcToDest('A', 'B');
|
|
25
|
-
graph.hasEdge('A', 'B');
|
|
26
|
-
expect(graph.hasEdge('A', 'B')).toBe(false);
|
|
24
|
+
graph.hasEdge('A', 'B'); // false
|
|
25
|
+
expect(graph.hasEdge('A', 'B')).toBe(false); // false
|
|
27
26
|
|
|
28
27
|
graph.addVertex('C');
|
|
29
28
|
|
|
@@ -31,7 +30,7 @@ describe('Overall Graph Operation Test', () => {
|
|
|
31
30
|
graph.addEdge('B', 'C');
|
|
32
31
|
|
|
33
32
|
const topologicalOrderIds = graph.topologicalSort();
|
|
34
|
-
expect(topologicalOrderIds).toEqual(['A', 'B', 'C'])
|
|
33
|
+
expect(topologicalOrderIds).toEqual(['A', 'B', 'C']);
|
|
35
34
|
});
|
|
36
35
|
it('Overall UndirectedGraph Operation Test', () => {
|
|
37
36
|
const graph = new UndirectedGraph();
|
|
@@ -44,7 +43,7 @@ describe('Overall Graph Operation Test', () => {
|
|
|
44
43
|
graph.addEdge('B', 'D');
|
|
45
44
|
|
|
46
45
|
const dijkstraResult = graph.dijkstra('A');
|
|
47
|
-
Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.id) // ['A', 'B', 'D']
|
|
46
|
+
Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.id); // ['A', 'B', 'D']
|
|
48
47
|
expect(Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.id)).toEqual(['A', 'B', 'D']);
|
|
49
48
|
});
|
|
50
49
|
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import {CoordinateMap} from '../../../../src';
|
|
2
|
+
|
|
3
|
+
describe('CoordinateMap', () => {
|
|
4
|
+
it('should set and get values correctly', () => {
|
|
5
|
+
const coordinateMap = new CoordinateMap<string>();
|
|
6
|
+
const key = [1, 2, 3];
|
|
7
|
+
const value = 'TestValue';
|
|
8
|
+
|
|
9
|
+
coordinateMap.set(key, value);
|
|
10
|
+
const retrievedValue = coordinateMap.get(key);
|
|
11
|
+
|
|
12
|
+
expect(retrievedValue).toBe(value);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('should return true when key exists', () => {
|
|
16
|
+
const coordinateMap = new CoordinateMap<string>();
|
|
17
|
+
const key = [1, 2, 3];
|
|
18
|
+
const value = 'TestValue';
|
|
19
|
+
|
|
20
|
+
coordinateMap.set(key, value);
|
|
21
|
+
|
|
22
|
+
expect(coordinateMap.has(key)).toBe(true);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should return false when key does not exist', () => {
|
|
26
|
+
const coordinateMap = new CoordinateMap<string>();
|
|
27
|
+
const key = [1, 2, 3];
|
|
28
|
+
|
|
29
|
+
expect(coordinateMap.has(key)).toBe(false);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should delete key-value pair correctly', () => {
|
|
33
|
+
const coordinateMap = new CoordinateMap<string>();
|
|
34
|
+
const key = [1, 2, 3];
|
|
35
|
+
const value = 'TestValue';
|
|
36
|
+
|
|
37
|
+
coordinateMap.set(key, value);
|
|
38
|
+
coordinateMap.delete(key);
|
|
39
|
+
|
|
40
|
+
expect(coordinateMap.has(key)).toBe(false);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('should allow changing the joint character', () => {
|
|
44
|
+
const coordinateMap = new CoordinateMap<string>();
|
|
45
|
+
const key = [1, 2, 3];
|
|
46
|
+
const value = 'TestValue';
|
|
47
|
+
|
|
48
|
+
coordinateMap.set(key, value);
|
|
49
|
+
const newKey = [1, 2, 3];
|
|
50
|
+
const retrievedValue = coordinateMap.get(newKey);
|
|
51
|
+
|
|
52
|
+
expect(retrievedValue).toBe(value);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {CoordinateSet} from '../../../../src';
|
|
2
|
+
|
|
3
|
+
describe('CoordinateSet', () => {
|
|
4
|
+
it('should add and check values correctly', () => {
|
|
5
|
+
const coordinateSet = new CoordinateSet();
|
|
6
|
+
const value = [1, 2, 3];
|
|
7
|
+
|
|
8
|
+
coordinateSet.add(value);
|
|
9
|
+
const hasValue = coordinateSet.has(value);
|
|
10
|
+
|
|
11
|
+
expect(hasValue).toBe(true);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('should return false when value does not exist', () => {
|
|
15
|
+
const coordinateSet = new CoordinateSet();
|
|
16
|
+
const value = [1, 2, 3];
|
|
17
|
+
|
|
18
|
+
expect(coordinateSet.has(value)).toBe(false);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should delete value correctly', () => {
|
|
22
|
+
const coordinateSet = new CoordinateSet();
|
|
23
|
+
const value = [1, 2, 3];
|
|
24
|
+
|
|
25
|
+
coordinateSet.add(value);
|
|
26
|
+
coordinateSet.delete(value);
|
|
27
|
+
|
|
28
|
+
expect(coordinateSet.has(value)).toBe(false);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('should allow changing the joint character', () => {
|
|
32
|
+
const coordinateSet = new CoordinateSet();
|
|
33
|
+
const value = [1, 2, 3];
|
|
34
|
+
|
|
35
|
+
coordinateSet.add(value);
|
|
36
|
+
const newValue = [1, 2, 3];
|
|
37
|
+
const hasValue = coordinateSet.has(newValue);
|
|
38
|
+
|
|
39
|
+
expect(hasValue).toBe(true);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import {HashNode, HashTable} from '../../../../src';
|
|
2
|
+
|
|
3
|
+
describe('HashNode', () => {
|
|
4
|
+
it('should create a HashNode with key and value', () => {
|
|
5
|
+
const key = 'testKey';
|
|
6
|
+
const value = 'testValue';
|
|
7
|
+
const hashNode = new HashNode(key, value);
|
|
8
|
+
|
|
9
|
+
expect(hashNode.key).toBe(key);
|
|
10
|
+
expect(hashNode.val).toBe(value);
|
|
11
|
+
expect(hashNode.next).toBe(null);
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
describe('HashTable', () => {
|
|
16
|
+
it('should initialize with default capacity', () => {
|
|
17
|
+
const hashTable = new HashTable<string, string>();
|
|
18
|
+
|
|
19
|
+
expect(hashTable.capacity).toBe(1000);
|
|
20
|
+
expect(hashTable.size).toBe(0);
|
|
21
|
+
expect(hashTable.buckets.length).toBe(1000);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('should initialize with custom capacity', () => {
|
|
25
|
+
const customCapacity = 500;
|
|
26
|
+
const hashTable = new HashTable<string, string>(customCapacity);
|
|
27
|
+
|
|
28
|
+
expect(hashTable.capacity).toBe(customCapacity);
|
|
29
|
+
expect(hashTable.size).toBe(0);
|
|
30
|
+
expect(hashTable.buckets.length).toBe(customCapacity);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('should put and get values correctly', () => {
|
|
34
|
+
const hashTable = new HashTable<string, string>();
|
|
35
|
+
const key = 'testKey';
|
|
36
|
+
const value = 'testValue';
|
|
37
|
+
|
|
38
|
+
hashTable.put(key, value);
|
|
39
|
+
const retrievedValue = hashTable.get(key);
|
|
40
|
+
|
|
41
|
+
expect(retrievedValue).toBe(value);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should handle collisions by chaining', () => {
|
|
45
|
+
const hashTable = new HashTable<string, string>();
|
|
46
|
+
const key1 = 'testKey1';
|
|
47
|
+
const value1 = 'testValue1';
|
|
48
|
+
const key2 = 'testKey2';
|
|
49
|
+
const value2 = 'testValue2';
|
|
50
|
+
|
|
51
|
+
hashTable.put(key1, value1);
|
|
52
|
+
hashTable.put(key2, value2);
|
|
53
|
+
|
|
54
|
+
const retrievedValue1 = hashTable.get(key1);
|
|
55
|
+
const retrievedValue2 = hashTable.get(key2);
|
|
56
|
+
|
|
57
|
+
expect(retrievedValue1).toBe(value1);
|
|
58
|
+
expect(retrievedValue2).toBe(value2);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('should update value for an existing key', () => {
|
|
62
|
+
const hashTable = new HashTable<string, string>();
|
|
63
|
+
const key = 'testKey';
|
|
64
|
+
const initialValue = 'testValue1';
|
|
65
|
+
const updatedValue = 'testValue2';
|
|
66
|
+
|
|
67
|
+
hashTable.put(key, initialValue);
|
|
68
|
+
hashTable.put(key, updatedValue);
|
|
69
|
+
|
|
70
|
+
const retrievedValue = hashTable.get(key);
|
|
71
|
+
|
|
72
|
+
expect(retrievedValue).toBe(updatedValue);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should return undefined for non-existent key', () => {
|
|
76
|
+
const hashTable = new HashTable<string, string>();
|
|
77
|
+
const key = 'nonExistentKey';
|
|
78
|
+
|
|
79
|
+
const retrievedValue = hashTable.get(key);
|
|
80
|
+
|
|
81
|
+
expect(retrievedValue).toBeUndefined();
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('should remove key-value pair correctly', () => {
|
|
85
|
+
const hashTable = new HashTable<string, string>();
|
|
86
|
+
const key = 'testKey';
|
|
87
|
+
const value = 'testValue';
|
|
88
|
+
|
|
89
|
+
hashTable.put(key, value);
|
|
90
|
+
hashTable.remove(key);
|
|
91
|
+
|
|
92
|
+
const retrievedValue = hashTable.get(key);
|
|
93
|
+
|
|
94
|
+
expect(retrievedValue).toBeUndefined();
|
|
95
|
+
expect(hashTable.size).toBe(0);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
@@ -21,31 +21,31 @@ describe('Heap Operation Test', () => {
|
|
|
21
21
|
});
|
|
22
22
|
|
|
23
23
|
it('should object heap work well', function () {
|
|
24
|
-
const minHeap = new MinHeap<{
|
|
24
|
+
const minHeap = new MinHeap<{a: string}>();
|
|
25
25
|
minHeap.add(1, {a: 'a1'});
|
|
26
26
|
minHeap.add(6, {a: 'a6'});
|
|
27
27
|
minHeap.add(2, {a: 'a2'});
|
|
28
28
|
minHeap.add(0, {a: 'a0'});
|
|
29
29
|
|
|
30
30
|
expect(minHeap.peek()).toEqual({a: 'a0'});
|
|
31
|
-
expect(minHeap.toArray()).toEqual(
|
|
31
|
+
expect(minHeap.toArray()).toEqual([{a: 'a0'}, {a: 'a1'}, {a: 'a2'}, {a: 'a6'}]);
|
|
32
32
|
let i = 0;
|
|
33
|
-
const expectPolled = [{
|
|
33
|
+
const expectPolled = [{a: 'a0'}, {a: 'a1'}, {a: 'a2'}, {a: 'a6'}];
|
|
34
34
|
while (minHeap.size > 0) {
|
|
35
35
|
expect(minHeap.poll()).toEqual(expectPolled[i]);
|
|
36
36
|
i++;
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
const maxHeap = new MaxHeap<{
|
|
39
|
+
const maxHeap = new MaxHeap<{a: string}>();
|
|
40
40
|
maxHeap.add(1, {a: 'a1'});
|
|
41
41
|
maxHeap.add(6, {a: 'a6'});
|
|
42
42
|
maxHeap.add(5, {a: 'a5'});
|
|
43
43
|
maxHeap.add(2, {a: 'a2'});
|
|
44
44
|
maxHeap.add(0, {a: 'a0'});
|
|
45
45
|
maxHeap.add(9, {a: 'a9'});
|
|
46
|
-
expect(maxHeap.peek()).toEqual({
|
|
47
|
-
expect(maxHeap.toArray()).toEqual([{
|
|
48
|
-
const maxExpectPolled = [{
|
|
46
|
+
expect(maxHeap.peek()).toEqual({a: 'a9'});
|
|
47
|
+
expect(maxHeap.toArray()).toEqual([{a: 'a9'}, {a: 'a2'}, {a: 'a6'}, {a: 'a1'}, {a: 'a0'}, {a: 'a5'}]);
|
|
48
|
+
const maxExpectPolled = [{a: 'a9'}, {a: 'a6'}, {a: 'a5'}, {a: 'a2'}, {a: 'a1'}, {a: 'a0'}];
|
|
49
49
|
let maxI = 0;
|
|
50
50
|
while (maxHeap.size > 0) {
|
|
51
51
|
expect(maxHeap.poll()).toEqual(maxExpectPolled[maxI]);
|
|
@@ -53,4 +53,3 @@ describe('Heap Operation Test', () => {
|
|
|
53
53
|
}
|
|
54
54
|
});
|
|
55
55
|
});
|
|
56
|
-
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import {HeapItem, MaxHeap} from '../../../../src';
|
|
2
2
|
|
|
3
3
|
describe('MaxHeap Operation Test', () => {
|
|
4
|
-
|
|
5
4
|
it('should object Max Heap operations be proper', function () {
|
|
6
|
-
const maxHeap = new MaxHeap<{
|
|
7
|
-
const myObj1 = {keyA: 'a1'},
|
|
8
|
-
|
|
5
|
+
const maxHeap = new MaxHeap<{keyA: string}>();
|
|
6
|
+
const myObj1 = {keyA: 'a1'},
|
|
7
|
+
myObj6 = {keyA: 'a6'},
|
|
8
|
+
myObj5 = {keyA: 'a5'},
|
|
9
|
+
myObj2 = {keyA: 'a2'},
|
|
10
|
+
myObj0 = {keyA: 'a0'},
|
|
11
|
+
myObj9 = {keyA: 'a9'};
|
|
9
12
|
maxHeap.add(1, myObj1);
|
|
10
13
|
expect(maxHeap.has(myObj1)).toBe(true);
|
|
11
14
|
expect(maxHeap.has(myObj9)).toBe(false);
|
|
@@ -38,5 +41,4 @@ describe('MaxHeap Operation Test', () => {
|
|
|
38
41
|
i++;
|
|
39
42
|
}
|
|
40
43
|
});
|
|
41
|
-
|
|
42
44
|
});
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import {HeapItem, MinHeap} from '../../../../src';
|
|
2
2
|
|
|
3
3
|
describe('MinHeap Operation Test', () => {
|
|
4
|
-
|
|
5
4
|
it('should numeric Min Heap operations be proper', function () {
|
|
6
5
|
const minNumHeap = new MinHeap<number>();
|
|
7
6
|
expect(minNumHeap).toBeInstanceOf(MinHeap);
|
|
@@ -22,15 +21,15 @@ describe('MinHeap Operation Test', () => {
|
|
|
22
21
|
expect(minNumHeap.size).toBe(6);
|
|
23
22
|
|
|
24
23
|
const poll1 = minNumHeap.poll(true);
|
|
25
|
-
expect(poll1).toBeInstanceOf(HeapItem)
|
|
24
|
+
expect(poll1).toBeInstanceOf(HeapItem);
|
|
26
25
|
poll1 instanceof HeapItem && expect(poll1.val).toBe(0);
|
|
27
26
|
|
|
28
27
|
const poll2 = minNumHeap.poll(true);
|
|
29
|
-
expect(poll2).toBeInstanceOf(HeapItem)
|
|
28
|
+
expect(poll2).toBeInstanceOf(HeapItem);
|
|
30
29
|
poll2 instanceof HeapItem && expect(poll2.val).toBe(1);
|
|
31
30
|
|
|
32
31
|
const peek1 = minNumHeap.peek(true);
|
|
33
|
-
expect(peek1).toBeInstanceOf(HeapItem)
|
|
32
|
+
expect(peek1).toBeInstanceOf(HeapItem);
|
|
34
33
|
peek1 instanceof HeapItem && expect(peek1.val).toBe(2);
|
|
35
34
|
|
|
36
35
|
const heapArray = minNumHeap.toArray(true);
|
|
@@ -50,7 +49,9 @@ describe('MinHeap Operation Test', () => {
|
|
|
50
49
|
|
|
51
50
|
const minObjHeap = new MinHeap<MyObject>();
|
|
52
51
|
|
|
53
|
-
const obj1 = new MyObject('a1'),
|
|
52
|
+
const obj1 = new MyObject('a1'),
|
|
53
|
+
obj6 = new MyObject('a6'),
|
|
54
|
+
obj2 = new MyObject('a2'),
|
|
54
55
|
obj0 = new MyObject('a0');
|
|
55
56
|
minObjHeap.add(1, obj1);
|
|
56
57
|
expect(minObjHeap.has(obj1)).toBe(true);
|