data-structure-typed 1.52.1 → 1.52.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/README.md +13 -13
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +162 -162
- package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -1
- package/dist/cjs/data-structures/base/iterable-entry-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +2 -2
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.js +2 -2
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +18 -0
- package/dist/cjs/data-structures/queue/queue.js +32 -6
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/queue/queue.d.ts +3 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +2 -2
- package/dist/mjs/data-structures/binary-tree/bst.js +2 -2
- package/dist/mjs/data-structures/queue/queue.d.ts +18 -0
- package/dist/mjs/data-structures/queue/queue.js +32 -6
- package/dist/mjs/types/data-structures/queue/queue.d.ts +3 -1
- package/dist/umd/data-structure-typed.js +32 -4
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +7 -7
- package/src/data-structures/base/iterable-element-base.ts +2 -2
- package/src/data-structures/base/iterable-entry-base.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +13 -12
- package/src/data-structures/binary-tree/avl-tree.ts +9 -8
- package/src/data-structures/binary-tree/binary-tree.ts +11 -10
- package/src/data-structures/binary-tree/bst.ts +10 -9
- package/src/data-structures/binary-tree/rb-tree.ts +8 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +8 -7
- package/src/data-structures/graph/abstract-graph.ts +15 -14
- package/src/data-structures/graph/directed-graph.ts +7 -6
- package/src/data-structures/graph/undirected-graph.ts +7 -6
- package/src/data-structures/hash/hash-map.ts +4 -4
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/queue/deque.ts +3 -3
- package/src/data-structures/queue/queue.ts +38 -7
- package/src/data-structures/stack/stack.ts +1 -1
- package/src/data-structures/trie/trie.ts +1 -1
- package/src/types/data-structures/graph/abstract-graph.ts +8 -8
- package/src/types/data-structures/queue/deque.ts +2 -2
- package/src/types/data-structures/queue/queue.ts +3 -1
- package/src/types/utils/utils.ts +4 -4
- package/test/integration/bst.test.ts +4 -1
- package/test/performance/data-structures/comparison/comparison.test.ts +12 -4
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +6 -2
- package/test/performance/reportor.ts +23 -8
- package/test/performance/types/reportor.ts +5 -1
- package/test/types/utils/json2html.ts +5 -1
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +6 -2
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +8 -2
- package/test/unit/data-structures/binary-tree/binary-index-tree.test.ts +36 -11
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +3 -1
- package/test/unit/data-structures/binary-tree/bst.test.ts +18 -5
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +3 -1
- package/test/unit/data-structures/graph/abstract-graph.test.ts +2 -4
- package/test/unit/data-structures/graph/directed-graph.test.ts +4 -5
- package/test/unit/data-structures/hash/hash-map.test.ts +6 -2
- package/test/unit/data-structures/heap/heap.test.ts +45 -13
- package/test/unit/data-structures/heap/max-heap.test.ts +10 -3
- package/test/unit/data-structures/heap/min-heap.test.ts +3 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +3 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +6 -2
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +22 -6
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +12 -4
- package/test/unit/data-structures/queue/deque.test.ts +14 -8
- package/test/unit/data-structures/queue/queue.test.ts +14 -1
- package/test/unit/data-structures/trie/trie.test.ts +3 -1
- package/test/unit/unrestricted-interconversion.test.ts +3 -1
- package/test/utils/big-o.ts +3 -1
- package/test/utils/json2html.ts +2 -6
|
@@ -30,7 +30,9 @@ suite
|
|
|
30
30
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
|
|
31
31
|
})
|
|
32
32
|
.add(`MJS PQ ${TEN_THOUSAND.toLocaleString()} add`, () => {
|
|
33
|
-
const pq = new MJSPriorityQueue<number>([], {
|
|
33
|
+
const pq = new MJSPriorityQueue<number>([], {
|
|
34
|
+
comparator: (a, b) => b - a
|
|
35
|
+
});
|
|
34
36
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
|
|
35
37
|
});
|
|
36
38
|
|
|
@@ -43,19 +45,25 @@ if (isCompetitor) {
|
|
|
43
45
|
|
|
44
46
|
suite
|
|
45
47
|
.add(`SRC PQ ${TEN_THOUSAND.toLocaleString()} add & poll`, () => {
|
|
46
|
-
const pq = new SRCPriorityQueue<number>([], {
|
|
48
|
+
const pq = new SRCPriorityQueue<number>([], {
|
|
49
|
+
comparator: (a, b) => b - a
|
|
50
|
+
});
|
|
47
51
|
|
|
48
52
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
|
|
49
53
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.poll();
|
|
50
54
|
})
|
|
51
55
|
.add(`CJS PQ ${TEN_THOUSAND.toLocaleString()} add & poll`, () => {
|
|
52
|
-
const pq = new CJSPriorityQueue<number>([], {
|
|
56
|
+
const pq = new CJSPriorityQueue<number>([], {
|
|
57
|
+
comparator: (a, b) => b - a
|
|
58
|
+
});
|
|
53
59
|
|
|
54
60
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
|
|
55
61
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.poll();
|
|
56
62
|
})
|
|
57
63
|
.add(`MJS PQ ${TEN_THOUSAND.toLocaleString()} add & poll`, () => {
|
|
58
|
-
const pq = new MJSPriorityQueue<number>([], {
|
|
64
|
+
const pq = new MJSPriorityQueue<number>([], {
|
|
65
|
+
comparator: (a, b) => b - a
|
|
66
|
+
});
|
|
59
67
|
|
|
60
68
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
|
|
61
69
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.poll();
|
|
@@ -9,11 +9,15 @@ const { HUNDRED_THOUSAND } = magnitude;
|
|
|
9
9
|
|
|
10
10
|
suite
|
|
11
11
|
.add(`${HUNDRED_THOUSAND.toLocaleString()} add`, () => {
|
|
12
|
-
const heap = new PriorityQueue<number>([], {
|
|
12
|
+
const heap = new PriorityQueue<number>([], {
|
|
13
|
+
comparator: (a, b) => b - a
|
|
14
|
+
});
|
|
13
15
|
for (let i = 0; i < HUNDRED_THOUSAND; i++) heap.add(i);
|
|
14
16
|
})
|
|
15
17
|
.add(`${HUNDRED_THOUSAND.toLocaleString()} add & poll`, () => {
|
|
16
|
-
const heap = new PriorityQueue<number>([], {
|
|
18
|
+
const heap = new PriorityQueue<number>([], {
|
|
19
|
+
comparator: (a, b) => b - a
|
|
20
|
+
});
|
|
17
21
|
|
|
18
22
|
for (let i = 0; i < HUNDRED_THOUSAND; i++) heap.add(i);
|
|
19
23
|
for (let i = 0; i < HUNDRED_THOUSAND; i++) heap.poll();
|
|
@@ -51,11 +51,19 @@ testFiles.forEach((file: string) => {
|
|
|
51
51
|
const testName = path.basename(file, '.test.ts');
|
|
52
52
|
const testFunction = require(file);
|
|
53
53
|
const { suite } = testFunction;
|
|
54
|
-
if (suite)
|
|
54
|
+
if (suite)
|
|
55
|
+
performanceTests.push({
|
|
56
|
+
testName,
|
|
57
|
+
suite,
|
|
58
|
+
file
|
|
59
|
+
});
|
|
55
60
|
});
|
|
56
61
|
|
|
57
62
|
const composeReport = () => {
|
|
58
|
-
if (!fs.existsSync(reportDistPath))
|
|
63
|
+
if (!fs.existsSync(reportDistPath))
|
|
64
|
+
fs.mkdirSync(reportDistPath, {
|
|
65
|
+
recursive: true
|
|
66
|
+
});
|
|
59
67
|
|
|
60
68
|
const filePath = path.join(reportDistPath, 'report.json');
|
|
61
69
|
const htmlFilePath = path.join(reportDistPath, 'report.html');
|
|
@@ -114,9 +122,18 @@ const composeReport = () => {
|
|
|
114
122
|
{
|
|
115
123
|
'<>': 'tr',
|
|
116
124
|
html: [
|
|
117
|
-
{
|
|
118
|
-
|
|
119
|
-
|
|
125
|
+
{
|
|
126
|
+
'<>': 'td',
|
|
127
|
+
html: '${name}'
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
'<>': 'td',
|
|
131
|
+
html: '${periodMS}'
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
'<>': 'td',
|
|
135
|
+
html: '${mean}'
|
|
136
|
+
}
|
|
120
137
|
]
|
|
121
138
|
}
|
|
122
139
|
]
|
|
@@ -242,9 +259,7 @@ sortedPerformanceTests.forEach(item => {
|
|
|
242
259
|
console.log(
|
|
243
260
|
// `Files: ${GREEN}${testFileCount}${END} `,
|
|
244
261
|
// `Suites: ${GREEN}${performanceTests.length}${END} `,
|
|
245
|
-
`Suites Progress: ${isDone ? GREEN : YELLOW}${completedCount}${END}/${isDone ? GREEN : YELLOW}${
|
|
246
|
-
performanceTests.length
|
|
247
|
-
}${END}`,
|
|
262
|
+
`Suites Progress: ${isDone ? GREEN : YELLOW}${completedCount}${END}/${isDone ? GREEN : YELLOW}${performanceTests.length}${END}`,
|
|
248
263
|
`Time: ${isTimeWarn ? YELLOW : GREEN}${runTime}s${END}`
|
|
249
264
|
);
|
|
250
265
|
if (isDone) {
|
|
@@ -291,7 +291,9 @@ describe('AVLTreeMultiMap operations test1', () => {
|
|
|
291
291
|
|
|
292
292
|
describe('AVLTreeMultiMap operations test recursively1', () => {
|
|
293
293
|
it('should perform various operations on a Binary Search Tree with numeric values1', () => {
|
|
294
|
-
const treeMultimap = new AVLTreeMultiMap<number>([], {
|
|
294
|
+
const treeMultimap = new AVLTreeMultiMap<number>([], {
|
|
295
|
+
iterationType: 'RECURSIVE'
|
|
296
|
+
});
|
|
295
297
|
|
|
296
298
|
expect(treeMultimap instanceof AVLTreeMultiMap);
|
|
297
299
|
treeMultimap.add([11, 11]);
|
|
@@ -741,7 +743,9 @@ describe('AVLTree toEntryFn', () => {
|
|
|
741
743
|
it('should toEntryFn 3', () => {
|
|
742
744
|
const tree = new AVLTreeMultiMap<{ obj: { id: number } }, number>(
|
|
743
745
|
[{ obj: { id: 1 } }, { obj: { id: 2 } }, { obj: { id: 3 } }, { obj: { id: 4 } }, { obj: { id: 5 } }],
|
|
744
|
-
{
|
|
746
|
+
{
|
|
747
|
+
comparator: (a, b) => a.obj.id - b.obj.id
|
|
748
|
+
}
|
|
745
749
|
);
|
|
746
750
|
|
|
747
751
|
const expected = [
|
|
@@ -228,7 +228,10 @@ describe('AVLTree APIs test', () => {
|
|
|
228
228
|
avl.add(1);
|
|
229
229
|
const node2 = new AVLTreeNode(2);
|
|
230
230
|
avl.add(node2);
|
|
231
|
-
const node3 = new AVLTreeNode(3, {
|
|
231
|
+
const node3 = new AVLTreeNode(3, {
|
|
232
|
+
id: 3,
|
|
233
|
+
text: 'text3'
|
|
234
|
+
});
|
|
232
235
|
avl.add(node3);
|
|
233
236
|
avl.add([3, { id: 3, text: 'text33' }]);
|
|
234
237
|
|
|
@@ -314,7 +317,10 @@ describe('AVLTree', () => {
|
|
|
314
317
|
avl.add(1);
|
|
315
318
|
const node2 = new AVLTreeNode(2);
|
|
316
319
|
avl.add(node2);
|
|
317
|
-
const node3 = new AVLTreeNode(3, {
|
|
320
|
+
const node3 = new AVLTreeNode(3, {
|
|
321
|
+
id: 3,
|
|
322
|
+
text: 'text3'
|
|
323
|
+
});
|
|
318
324
|
avl.add(node3);
|
|
319
325
|
avl.add([3, { id: 3, text: 'text33' }]);
|
|
320
326
|
|
|
@@ -8,13 +8,18 @@ describe('BinaryIndexedTree simple', () => {
|
|
|
8
8
|
|
|
9
9
|
beforeEach(() => {
|
|
10
10
|
//Create a new BinaryIndexedTree instance before each test case
|
|
11
|
-
bit = new BinaryIndexedTree({
|
|
11
|
+
bit = new BinaryIndexedTree({
|
|
12
|
+
frequency: 0,
|
|
13
|
+
max: 10
|
|
14
|
+
}); // Modify the value of max as needed
|
|
12
15
|
});
|
|
13
16
|
|
|
14
17
|
it('should initialize correctly', () => {
|
|
15
18
|
expect(bit.freq).toBe(0);
|
|
16
19
|
expect(bit.max).toBe(10);
|
|
17
|
-
expect(bit.freqMap).toEqual({
|
|
20
|
+
expect(bit.freqMap).toEqual({
|
|
21
|
+
0: 0
|
|
22
|
+
}); // Modify the initialized record value according to the actual situation
|
|
18
23
|
// More initialization checks can be added
|
|
19
24
|
});
|
|
20
25
|
|
|
@@ -54,7 +59,10 @@ describe('BinaryIndexedTree', () => {
|
|
|
54
59
|
let bit: BinaryIndexedTree;
|
|
55
60
|
|
|
56
61
|
beforeEach(function () {
|
|
57
|
-
bit = new BinaryIndexedTree({
|
|
62
|
+
bit = new BinaryIndexedTree({
|
|
63
|
+
frequency,
|
|
64
|
+
max
|
|
65
|
+
});
|
|
58
66
|
});
|
|
59
67
|
it('should validate the index', function () {
|
|
60
68
|
expect(() => bit.readSingle(-1)).toThrow('Index out of range');
|
|
@@ -73,7 +81,10 @@ describe('BinaryIndexedTree', () => {
|
|
|
73
81
|
it('should frequency and max', function () {
|
|
74
82
|
const frequency = 200;
|
|
75
83
|
const max = 1000;
|
|
76
|
-
const bit = new BinaryIndexedTree({
|
|
84
|
+
const bit = new BinaryIndexedTree({
|
|
85
|
+
frequency,
|
|
86
|
+
max
|
|
87
|
+
});
|
|
77
88
|
|
|
78
89
|
expect(bit.freq).toBe(frequency);
|
|
79
90
|
expect(bit.max).toBe(max);
|
|
@@ -123,7 +134,9 @@ describe('designated values', function () {
|
|
|
123
134
|
let bit: BinaryIndexedTree;
|
|
124
135
|
|
|
125
136
|
beforeEach(function () {
|
|
126
|
-
bit = new BinaryIndexedTree({
|
|
137
|
+
bit = new BinaryIndexedTree({
|
|
138
|
+
max: array.length
|
|
139
|
+
});
|
|
127
140
|
array.forEach((value, i) => bit.writeSingle(i, value));
|
|
128
141
|
});
|
|
129
142
|
|
|
@@ -182,7 +195,9 @@ describe('descending sequence', function () {
|
|
|
182
195
|
let bit: BinaryIndexedTree;
|
|
183
196
|
|
|
184
197
|
beforeEach(function () {
|
|
185
|
-
bit = new BinaryIndexedTree({
|
|
198
|
+
bit = new BinaryIndexedTree({
|
|
199
|
+
max: array.length
|
|
200
|
+
});
|
|
186
201
|
array.forEach((value, i) => bit.writeSingle(i, value));
|
|
187
202
|
});
|
|
188
203
|
|
|
@@ -219,7 +234,9 @@ describe('descending sequence', function () {
|
|
|
219
234
|
|
|
220
235
|
describe('BinaryIndexedTree additional tests', () => {
|
|
221
236
|
it('should handle read method correctly', () => {
|
|
222
|
-
const bit = new BinaryIndexedTree({
|
|
237
|
+
const bit = new BinaryIndexedTree({
|
|
238
|
+
max: 10
|
|
239
|
+
});
|
|
223
240
|
bit.writeSingle(2, 10);
|
|
224
241
|
bit.writeSingle(5, 20);
|
|
225
242
|
bit.writeSingle(8, 30);
|
|
@@ -227,7 +244,9 @@ describe('BinaryIndexedTree additional tests', () => {
|
|
|
227
244
|
});
|
|
228
245
|
|
|
229
246
|
it('should handle consecutive operations', () => {
|
|
230
|
-
const bit = new BinaryIndexedTree({
|
|
247
|
+
const bit = new BinaryIndexedTree({
|
|
248
|
+
max: 10
|
|
249
|
+
});
|
|
231
250
|
bit.writeSingle(2, 10);
|
|
232
251
|
bit.update(2, 5);
|
|
233
252
|
expect(bit.readSingle(2)).toBe(15);
|
|
@@ -237,7 +256,9 @@ describe('BinaryIndexedTree additional tests', () => {
|
|
|
237
256
|
});
|
|
238
257
|
|
|
239
258
|
it('should handle frequent increment updates', () => {
|
|
240
|
-
const bit = new BinaryIndexedTree({
|
|
259
|
+
const bit = new BinaryIndexedTree({
|
|
260
|
+
max: 10
|
|
261
|
+
});
|
|
241
262
|
for (let i = 0; i < 10; i++) {
|
|
242
263
|
bit.update(2, 5);
|
|
243
264
|
}
|
|
@@ -245,7 +266,9 @@ describe('BinaryIndexedTree additional tests', () => {
|
|
|
245
266
|
});
|
|
246
267
|
|
|
247
268
|
it('should handle edge cases', () => {
|
|
248
|
-
const bit = new BinaryIndexedTree({
|
|
269
|
+
const bit = new BinaryIndexedTree({
|
|
270
|
+
max: 10
|
|
271
|
+
});
|
|
249
272
|
bit.writeSingle(9, 100);
|
|
250
273
|
expect(bit.readSingle(9)).toBe(100);
|
|
251
274
|
expect(bit.lowerBound(200)).toBe(10);
|
|
@@ -291,7 +314,9 @@ describe('', () => {
|
|
|
291
314
|
|
|
292
315
|
constructor(nums: number[]) {
|
|
293
316
|
this._nums = nums;
|
|
294
|
-
this._tree = new BinaryIndexedTree({
|
|
317
|
+
this._tree = new BinaryIndexedTree({
|
|
318
|
+
max: nums.length + 1
|
|
319
|
+
});
|
|
295
320
|
for (let i = 0; i < nums.length; i++) {
|
|
296
321
|
this._tree.update(i + 1, nums[i]);
|
|
297
322
|
}
|
|
@@ -570,7 +570,9 @@ describe('BinaryTree', () => {
|
|
|
570
570
|
let tree: BinaryTree<number, string>;
|
|
571
571
|
|
|
572
572
|
beforeEach(() => {
|
|
573
|
-
tree = new BinaryTree<number, string>([], {
|
|
573
|
+
tree = new BinaryTree<number, string>([], {
|
|
574
|
+
iterationType: 'RECURSIVE'
|
|
575
|
+
});
|
|
574
576
|
});
|
|
575
577
|
|
|
576
578
|
afterEach(() => {
|
|
@@ -252,7 +252,10 @@ describe('BST operations test', () => {
|
|
|
252
252
|
expect(leftMost?.key).toBe(1);
|
|
253
253
|
|
|
254
254
|
const node15 = objBST.getNode(15);
|
|
255
|
-
expect(node15?.value).toEqual({
|
|
255
|
+
expect(node15?.value).toEqual({
|
|
256
|
+
name: 'Alice',
|
|
257
|
+
age: 15
|
|
258
|
+
});
|
|
256
259
|
const minNodeBySpecificNode = node15 && objBST.getLeftMost(node15);
|
|
257
260
|
expect(minNodeBySpecificNode?.key).toBe(12);
|
|
258
261
|
|
|
@@ -411,7 +414,9 @@ describe('BST operations test', () => {
|
|
|
411
414
|
|
|
412
415
|
describe('BST operations test recursively', () => {
|
|
413
416
|
it('should perform various operations on a Binary Search Tree with numeric values', () => {
|
|
414
|
-
const bst = new BST<number>([], {
|
|
417
|
+
const bst = new BST<number>([], {
|
|
418
|
+
iterationType: 'RECURSIVE'
|
|
419
|
+
});
|
|
415
420
|
expect(bst).toBeInstanceOf(BST);
|
|
416
421
|
bst.add([11, 11]);
|
|
417
422
|
bst.add([3, 3]);
|
|
@@ -626,7 +631,10 @@ describe('BST operations test recursively', () => {
|
|
|
626
631
|
expect(objBST.has(6)).toBe(true);
|
|
627
632
|
|
|
628
633
|
const node6 = objBST.getNode(6);
|
|
629
|
-
expect(objBST.get(6)).toEqual({
|
|
634
|
+
expect(objBST.get(6)).toEqual({
|
|
635
|
+
key: 6,
|
|
636
|
+
keyA: 6
|
|
637
|
+
});
|
|
630
638
|
expect(node6 && objBST.getHeight(node6)).toBe(2);
|
|
631
639
|
expect(node6 && objBST.getDepth(node6)).toBe(3);
|
|
632
640
|
|
|
@@ -640,7 +648,10 @@ describe('BST operations test recursively', () => {
|
|
|
640
648
|
expect(leftMost?.key).toBe(1);
|
|
641
649
|
|
|
642
650
|
const node15 = objBST.getNode(15);
|
|
643
|
-
expect(node15?.value).toEqual({
|
|
651
|
+
expect(node15?.value).toEqual({
|
|
652
|
+
key: 15,
|
|
653
|
+
keyA: 15
|
|
654
|
+
});
|
|
644
655
|
const minNodeBySpecificNode = node15 && objBST.getLeftMost(node15);
|
|
645
656
|
expect(minNodeBySpecificNode?.key).toBe(12);
|
|
646
657
|
|
|
@@ -869,7 +880,9 @@ describe('BST isBST', function () {
|
|
|
869
880
|
});
|
|
870
881
|
|
|
871
882
|
test('isBST when variant is Max', () => {
|
|
872
|
-
const bst = new BST<number, number>([1, 2, 3, 9, 8, 5, 6, 7, 4], {
|
|
883
|
+
const bst = new BST<number, number>([1, 2, 3, 9, 8, 5, 6, 7, 4], {
|
|
884
|
+
comparator: (a, b) => b - a
|
|
885
|
+
});
|
|
873
886
|
bst.addMany([1, 2, 3, 9, 8, 5, 6, 7, 4]);
|
|
874
887
|
expect(bst.isBST()).toBe(true);
|
|
875
888
|
});
|
|
@@ -350,7 +350,9 @@ describe('TreeMultiMap operations test1', () => {
|
|
|
350
350
|
|
|
351
351
|
describe('TreeMultiMap operations test recursively1', () => {
|
|
352
352
|
it('should perform various operations on a Binary Search Tree with numeric values1', () => {
|
|
353
|
-
const tmm = new TreeMultiMap<number>([], {
|
|
353
|
+
const tmm = new TreeMultiMap<number>([], {
|
|
354
|
+
iterationType: 'RECURSIVE'
|
|
355
|
+
});
|
|
354
356
|
|
|
355
357
|
expect(tmm instanceof TreeMultiMap);
|
|
356
358
|
tmm.add([11, 11]);
|
|
@@ -81,8 +81,7 @@ class MyGraph<
|
|
|
81
81
|
return true;
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
clear(): void {
|
|
85
|
-
}
|
|
84
|
+
clear(): void {}
|
|
86
85
|
|
|
87
86
|
clone(): any {
|
|
88
87
|
return {};
|
|
@@ -96,8 +95,7 @@ class MyGraph<
|
|
|
96
95
|
describe('AbstractGraph Operation Test', () => {
|
|
97
96
|
const myGraph: MyGraph<number, string> = new MyGraph<number, string>();
|
|
98
97
|
|
|
99
|
-
beforeEach(() => {
|
|
100
|
-
});
|
|
98
|
+
beforeEach(() => {});
|
|
101
99
|
it('should edge cases', function () {
|
|
102
100
|
myGraph.addVertex('A', 1);
|
|
103
101
|
myGraph.addVertex('B', 2);
|
|
@@ -998,7 +998,6 @@ describe('DirectedGraph tarjan', () => {
|
|
|
998
998
|
|
|
999
999
|
describe('delete', () => {
|
|
1000
1000
|
it(`deleteVertex deletes all of it's neighbors from the inEdge Map`, () => {
|
|
1001
|
-
|
|
1002
1001
|
const graph = new DirectedGraph();
|
|
1003
1002
|
graph.addVertex('A');
|
|
1004
1003
|
graph.addVertex('B');
|
|
@@ -1008,18 +1007,18 @@ describe('delete', () => {
|
|
|
1008
1007
|
graph.addEdge('C', 'A');
|
|
1009
1008
|
|
|
1010
1009
|
// 'Incoming to A should contain ['B','C']
|
|
1011
|
-
expect(graph.incomingEdgesOf('A').map(
|
|
1010
|
+
expect(graph.incomingEdgesOf('A').map(e => e.src)).toEqual(['B', 'C']); // ['B','C']
|
|
1012
1011
|
|
|
1013
1012
|
// Now delete B, which has no direct link to C, only that C -> A.
|
|
1014
1013
|
graph.deleteVertex('B');
|
|
1015
1014
|
|
|
1016
1015
|
// Now if we do the same call to incoming edges for we should get only ['C']
|
|
1017
|
-
expect(graph.incomingEdgesOf('A').map(
|
|
1016
|
+
expect(graph.incomingEdgesOf('A').map(e => e.src)).toEqual(['C']); // [];
|
|
1018
1017
|
|
|
1019
1018
|
// but it only shows an empty array, since we deleted all of `A's edges, not just the one to `B`.
|
|
1020
1019
|
|
|
1021
1020
|
// If we check C, it correctly shows A as an outgoing edge,
|
|
1022
1021
|
// even though A no longer has any knowledge of C linking to it.
|
|
1023
|
-
expect(graph.outgoingEdgesOf('C').map(
|
|
1022
|
+
expect(graph.outgoingEdgesOf('C').map(e => e.dest)).toEqual(['A']);
|
|
1024
1023
|
});
|
|
1025
|
-
})
|
|
1024
|
+
});
|
|
@@ -147,7 +147,9 @@ describe('HashMap', () => {
|
|
|
147
147
|
}
|
|
148
148
|
}
|
|
149
149
|
|
|
150
|
-
const eHM = new ExtendedHashMap<string, number>([], {
|
|
150
|
+
const eHM = new ExtendedHashMap<string, number>([], {
|
|
151
|
+
someOtherParam: 'someOtherParam'
|
|
152
|
+
});
|
|
151
153
|
eHM.set('one', 1);
|
|
152
154
|
expect(eHM.get('one')).toBe(1);
|
|
153
155
|
});
|
|
@@ -357,7 +359,9 @@ describe('LinkedHashMap', () => {
|
|
|
357
359
|
|
|
358
360
|
it('should handle number keys correctly', () => {
|
|
359
361
|
hashMap.set(999, { a: '999Value' });
|
|
360
|
-
expect(hashMap.get(999)).toEqual({
|
|
362
|
+
expect(hashMap.get(999)).toEqual({
|
|
363
|
+
a: '999Value'
|
|
364
|
+
});
|
|
361
365
|
});
|
|
362
366
|
|
|
363
367
|
it('should update the value for an existing key', () => {
|
|
@@ -2,7 +2,6 @@ import { FibonacciHeap, Heap, MaxHeap, MinHeap } from '../../../../src';
|
|
|
2
2
|
import { logBigOMetricsWrap } from '../../../utils';
|
|
3
3
|
|
|
4
4
|
describe('Heap Operation Test', () => {
|
|
5
|
-
|
|
6
5
|
it('should heap add and delete work well', function () {
|
|
7
6
|
const hp = new MinHeap<number>();
|
|
8
7
|
|
|
@@ -47,7 +46,9 @@ describe('Heap Operation Test', () => {
|
|
|
47
46
|
});
|
|
48
47
|
|
|
49
48
|
it('should clone', function () {
|
|
50
|
-
const minNumHeap = new Heap<string>([], {
|
|
49
|
+
const minNumHeap = new Heap<string>([], {
|
|
50
|
+
comparator: (a, b) => Number(a) - Number(b)
|
|
51
|
+
});
|
|
51
52
|
minNumHeap.add('1');
|
|
52
53
|
minNumHeap.add('6');
|
|
53
54
|
minNumHeap.add('2');
|
|
@@ -64,13 +65,21 @@ describe('Heap Operation Test', () => {
|
|
|
64
65
|
});
|
|
65
66
|
|
|
66
67
|
it('should object heap work well', function () {
|
|
67
|
-
const minHeap = new MinHeap<{
|
|
68
|
+
const minHeap = new MinHeap<{
|
|
69
|
+
a: string;
|
|
70
|
+
key: number;
|
|
71
|
+
}>([], {
|
|
72
|
+
comparator: (a, b) => a.key - b.key
|
|
73
|
+
});
|
|
68
74
|
minHeap.add({ key: 1, a: 'a1' });
|
|
69
75
|
minHeap.add({ key: 6, a: 'a6' });
|
|
70
76
|
minHeap.add({ key: 2, a: 'a2' });
|
|
71
77
|
minHeap.add({ key: 0, a: 'a0' });
|
|
72
78
|
|
|
73
|
-
expect(minHeap.peek()).toEqual({
|
|
79
|
+
expect(minHeap.peek()).toEqual({
|
|
80
|
+
a: 'a0',
|
|
81
|
+
key: 0
|
|
82
|
+
});
|
|
74
83
|
expect(minHeap.toArray().map(item => ({ a: item.a }))).toEqual([
|
|
75
84
|
{ a: 'a0' },
|
|
76
85
|
{ a: 'a1' },
|
|
@@ -80,18 +89,28 @@ describe('Heap Operation Test', () => {
|
|
|
80
89
|
let i = 0;
|
|
81
90
|
const expectPolled = [{ a: 'a0' }, { a: 'a1' }, { a: 'a2' }, { a: 'a6' }];
|
|
82
91
|
while (minHeap.size > 0) {
|
|
83
|
-
expect({
|
|
92
|
+
expect({
|
|
93
|
+
a: minHeap.poll()?.a
|
|
94
|
+
}).toEqual(expectPolled[i]);
|
|
84
95
|
i++;
|
|
85
96
|
}
|
|
86
97
|
|
|
87
|
-
const maxHeap = new MaxHeap<{
|
|
98
|
+
const maxHeap = new MaxHeap<{
|
|
99
|
+
key: number;
|
|
100
|
+
a: string;
|
|
101
|
+
}>([], {
|
|
102
|
+
comparator: (a, b) => b.key - a.key
|
|
103
|
+
});
|
|
88
104
|
maxHeap.add({ key: 1, a: 'a1' });
|
|
89
105
|
maxHeap.add({ key: 6, a: 'a6' });
|
|
90
106
|
maxHeap.add({ key: 5, a: 'a5' });
|
|
91
107
|
maxHeap.add({ key: 2, a: 'a2' });
|
|
92
108
|
maxHeap.add({ key: 0, a: 'a0' });
|
|
93
109
|
maxHeap.add({ key: 9, a: 'a9' });
|
|
94
|
-
expect(maxHeap.peek()).toEqual({
|
|
110
|
+
expect(maxHeap.peek()).toEqual({
|
|
111
|
+
a: 'a9',
|
|
112
|
+
key: 9
|
|
113
|
+
});
|
|
95
114
|
expect(maxHeap.toArray().map(item => ({ a: item.a }))).toEqual([
|
|
96
115
|
{ a: 'a9' },
|
|
97
116
|
{ a: 'a2' },
|
|
@@ -103,13 +122,18 @@ describe('Heap Operation Test', () => {
|
|
|
103
122
|
const maxExpectPolled = [{ a: 'a9' }, { a: 'a6' }, { a: 'a5' }, { a: 'a2' }, { a: 'a1' }, { a: 'a0' }];
|
|
104
123
|
let maxI = 0;
|
|
105
124
|
while (maxHeap.size > 0) {
|
|
106
|
-
expect({
|
|
125
|
+
expect({
|
|
126
|
+
a: maxHeap.poll()?.a
|
|
127
|
+
}).toEqual(maxExpectPolled[maxI]);
|
|
107
128
|
maxI++;
|
|
108
129
|
}
|
|
109
130
|
});
|
|
110
131
|
|
|
111
132
|
it('should object heap map & filter', function () {
|
|
112
|
-
const minHeap = new MinHeap<{
|
|
133
|
+
const minHeap = new MinHeap<{
|
|
134
|
+
a: string;
|
|
135
|
+
key: number;
|
|
136
|
+
}>(
|
|
113
137
|
[
|
|
114
138
|
{ key: 1, a: 'a1' },
|
|
115
139
|
{ key: 6, a: 'a6' },
|
|
@@ -119,7 +143,9 @@ describe('Heap Operation Test', () => {
|
|
|
119
143
|
{ key: 4, a: 'a4' },
|
|
120
144
|
{ key: 0, a: 'a0' }
|
|
121
145
|
],
|
|
122
|
-
{
|
|
146
|
+
{
|
|
147
|
+
comparator: (a, b) => a.key - b.key
|
|
148
|
+
}
|
|
123
149
|
);
|
|
124
150
|
|
|
125
151
|
const mappedMinHeap = minHeap.map(
|
|
@@ -147,7 +173,9 @@ describe('Heap Operation Test', () => {
|
|
|
147
173
|
});
|
|
148
174
|
|
|
149
175
|
it('should object heap', () => {
|
|
150
|
-
const heap = new Heap<{
|
|
176
|
+
const heap = new Heap<{
|
|
177
|
+
rawItem: { id: number };
|
|
178
|
+
}>(
|
|
151
179
|
[
|
|
152
180
|
{ rawItem: { id: 4 } },
|
|
153
181
|
{ rawItem: { id: 8 } },
|
|
@@ -157,7 +185,9 @@ describe('Heap Operation Test', () => {
|
|
|
157
185
|
{ rawItem: { id: 3 } },
|
|
158
186
|
{ rawItem: { id: 5 } }
|
|
159
187
|
],
|
|
160
|
-
{
|
|
188
|
+
{
|
|
189
|
+
comparator: (a, b) => a.rawItem.id - b.rawItem.id
|
|
190
|
+
}
|
|
161
191
|
);
|
|
162
192
|
|
|
163
193
|
expect([...heap.sort()]).toEqual([
|
|
@@ -182,7 +212,9 @@ describe('Heap Operation Test', () => {
|
|
|
182
212
|
{ rawItem: { id: 3 } },
|
|
183
213
|
{ rawItem: { id: 5 } }
|
|
184
214
|
],
|
|
185
|
-
{
|
|
215
|
+
{
|
|
216
|
+
toElementFn: rawElement => rawElement.rawItem.id
|
|
217
|
+
}
|
|
186
218
|
);
|
|
187
219
|
|
|
188
220
|
expect([...heap.sort()]).toEqual([1, 3, 4, 5, 6, 7, 8]);
|
|
@@ -5,7 +5,9 @@ describe('MaxHeap', () => {
|
|
|
5
5
|
let maxHeap: MaxHeap<number>;
|
|
6
6
|
|
|
7
7
|
beforeEach(() => {
|
|
8
|
-
maxHeap = new MaxHeap<number>([], {
|
|
8
|
+
maxHeap = new MaxHeap<number>([], {
|
|
9
|
+
comparator: numberComparator
|
|
10
|
+
});
|
|
9
11
|
});
|
|
10
12
|
|
|
11
13
|
it('add and poll elements in descending order', () => {
|
|
@@ -51,7 +53,10 @@ describe('MaxHeap', () => {
|
|
|
51
53
|
});
|
|
52
54
|
|
|
53
55
|
it('should object heap map & filter', function () {
|
|
54
|
-
const maxHeap = new MaxHeap<{
|
|
56
|
+
const maxHeap = new MaxHeap<{
|
|
57
|
+
a: string;
|
|
58
|
+
key: number;
|
|
59
|
+
}>(
|
|
55
60
|
[
|
|
56
61
|
{ key: 1, a: 'a1' },
|
|
57
62
|
{ key: 6, a: 'a6' },
|
|
@@ -61,7 +66,9 @@ describe('MaxHeap', () => {
|
|
|
61
66
|
{ key: 4, a: 'a4' },
|
|
62
67
|
{ key: 0, a: 'a0' }
|
|
63
68
|
],
|
|
64
|
-
{
|
|
69
|
+
{
|
|
70
|
+
comparator: (a, b) => b.key - a.key
|
|
71
|
+
}
|
|
65
72
|
);
|
|
66
73
|
|
|
67
74
|
const mappedMaxHeap = maxHeap.map(
|
|
@@ -5,7 +5,9 @@ describe('MinHeap', () => {
|
|
|
5
5
|
let minHeap: MinHeap<number>;
|
|
6
6
|
|
|
7
7
|
beforeEach(() => {
|
|
8
|
-
minHeap = new MinHeap<number>([], {
|
|
8
|
+
minHeap = new MinHeap<number>([], {
|
|
9
|
+
comparator: numberComparator
|
|
10
|
+
});
|
|
9
11
|
});
|
|
10
12
|
|
|
11
13
|
it('add and poll elements in ascending order', () => {
|
|
@@ -79,7 +79,9 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
79
79
|
|
|
80
80
|
describe('DoublyLinkedList Operation Test', () => {
|
|
81
81
|
let list: DoublyLinkedList<number>;
|
|
82
|
-
let objectList: DoublyLinkedList<{
|
|
82
|
+
let objectList: DoublyLinkedList<{
|
|
83
|
+
keyA: number;
|
|
84
|
+
}>;
|
|
83
85
|
|
|
84
86
|
beforeEach(() => {
|
|
85
87
|
list = new DoublyLinkedList();
|
|
@@ -11,10 +11,14 @@ describe('SinglyLinkedListNode', () => {
|
|
|
11
11
|
|
|
12
12
|
describe('SinglyLinkedList Operation Test', () => {
|
|
13
13
|
let list: SinglyLinkedList<number>;
|
|
14
|
-
let objectList: SinglyLinkedList<{
|
|
14
|
+
let objectList: SinglyLinkedList<{
|
|
15
|
+
keyA: number;
|
|
16
|
+
}>;
|
|
15
17
|
beforeEach(() => {
|
|
16
18
|
list = new SinglyLinkedList<number>();
|
|
17
|
-
objectList = new SinglyLinkedList<{
|
|
19
|
+
objectList = new SinglyLinkedList<{
|
|
20
|
+
keyA: number;
|
|
21
|
+
}>();
|
|
18
22
|
});
|
|
19
23
|
|
|
20
24
|
describe('push', () => {
|