data-structure-typed 1.47.4 → 1.47.6
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/benchmark/report.html +2 -2
- package/benchmark/report.json +12 -18
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +8 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +23 -15
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +66 -82
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +38 -37
- package/dist/cjs/data-structures/binary-tree/bst.js +56 -40
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +26 -17
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +31 -22
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +3 -3
- package/dist/cjs/data-structures/hash/hash-map.js +10 -4
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-table.d.ts +9 -4
- package/dist/cjs/data-structures/hash/hash-table.js +50 -5
- package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +25 -22
- package/dist/cjs/data-structures/heap/heap.js +101 -41
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/cjs/data-structures/heap/max-heap.js +2 -2
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/cjs/data-structures/heap/min-heap.js +2 -2
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +58 -57
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +125 -119
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +38 -37
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +65 -60
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +50 -49
- package/dist/cjs/data-structures/queue/deque.js +81 -71
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +46 -0
- package/dist/cjs/data-structures/queue/queue.js +80 -0
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +20 -6
- package/dist/cjs/data-structures/stack/stack.js +65 -8
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +5 -0
- package/dist/cjs/data-structures/trie/trie.js +47 -0
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -1
- package/dist/cjs/types/common.d.ts +2 -0
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/cjs/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +8 -8
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +26 -16
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +66 -83
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +38 -37
- package/dist/mjs/data-structures/binary-tree/bst.js +59 -41
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +30 -19
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +34 -23
- package/dist/mjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +3 -3
- package/dist/mjs/data-structures/hash/hash-map.js +10 -4
- package/dist/mjs/data-structures/hash/hash-table.d.ts +9 -4
- package/dist/mjs/data-structures/hash/hash-table.js +50 -5
- package/dist/mjs/data-structures/heap/heap.d.ts +25 -22
- package/dist/mjs/data-structures/heap/heap.js +102 -42
- package/dist/mjs/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/mjs/data-structures/heap/max-heap.js +2 -2
- package/dist/mjs/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/mjs/data-structures/heap/min-heap.js +2 -2
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +58 -57
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +125 -119
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +38 -37
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +65 -60
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/mjs/data-structures/queue/deque.d.ts +50 -49
- package/dist/mjs/data-structures/queue/deque.js +81 -71
- package/dist/mjs/data-structures/queue/queue.d.ts +46 -0
- package/dist/mjs/data-structures/queue/queue.js +80 -0
- package/dist/mjs/data-structures/stack/stack.d.ts +20 -6
- package/dist/mjs/data-structures/stack/stack.js +65 -8
- package/dist/mjs/data-structures/trie/trie.d.ts +5 -0
- package/dist/mjs/data-structures/trie/trie.js +47 -0
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -1
- package/dist/mjs/types/common.d.ts +2 -0
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/mjs/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/dist/umd/data-structure-typed.js +856 -509
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +27 -17
- package/src/data-structures/binary-tree/binary-tree.ts +114 -97
- package/src/data-structures/binary-tree/bst.ts +67 -47
- package/src/data-structures/binary-tree/rb-tree.ts +34 -20
- package/src/data-structures/binary-tree/tree-multimap.ts +43 -25
- package/src/data-structures/graph/abstract-graph.ts +1 -1
- package/src/data-structures/hash/hash-map.ts +13 -7
- package/src/data-structures/hash/hash-table.ts +59 -9
- package/src/data-structures/heap/heap.ts +115 -46
- package/src/data-structures/heap/max-heap.ts +5 -5
- package/src/data-structures/heap/min-heap.ts +5 -5
- package/src/data-structures/linked-list/doubly-linked-list.ts +137 -128
- package/src/data-structures/linked-list/singly-linked-list.ts +72 -64
- package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/queue/deque.ts +86 -75
- package/src/data-structures/queue/queue.ts +88 -0
- package/src/data-structures/stack/stack.ts +75 -10
- package/src/data-structures/trie/trie.ts +53 -0
- package/src/interfaces/binary-tree.ts +13 -1
- package/src/types/common.ts +5 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -3
- package/src/types/data-structures/heap/heap.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
- package/test/performance/data-structures/comparison/comparison.test.ts +7 -6
- package/test/performance/data-structures/heap/heap.test.ts +14 -5
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +6 -6
- package/test/unit/data-structures/binary-tree/bst.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +1 -1
- package/test/unit/data-structures/hash/hash-table.test.ts +58 -2
- package/test/unit/data-structures/heap/heap.test.ts +2 -2
- package/test/unit/data-structures/heap/max-heap.test.ts +1 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +49 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +4 -3
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +9 -10
- package/test/unit/data-structures/queue/queue.test.ts +37 -0
- package/test/unit/data-structures/stack/stack.test.ts +55 -5
- package/test/unit/data-structures/trie/trie.test.ts +33 -0
- package/test/unit/unrestricted-interconversion.test.ts +100 -0
|
@@ -112,7 +112,7 @@ describe('AVL Tree Test', () => {
|
|
|
112
112
|
describe('AVL Tree Test recursively', () => {
|
|
113
113
|
it('should perform various operations on a AVL Tree', () => {
|
|
114
114
|
const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
|
115
|
-
const tree = new AVLTree<number>({ iterationType: IterationType.RECURSIVE });
|
|
115
|
+
const tree = new AVLTree<number>([], { iterationType: IterationType.RECURSIVE });
|
|
116
116
|
|
|
117
117
|
for (const i of arr) tree.add(i, i);
|
|
118
118
|
|
|
@@ -189,9 +189,9 @@ describe('BinaryTree', () => {
|
|
|
189
189
|
tree.add(4);
|
|
190
190
|
tree.add(2);
|
|
191
191
|
expect(tree.getHeight()).toBe(1);
|
|
192
|
-
tree.
|
|
192
|
+
tree.iterationType = IterationType.RECURSIVE;
|
|
193
193
|
expect(tree.getHeight()).toBe(1);
|
|
194
|
-
tree.
|
|
194
|
+
tree.iterationType = IterationType.ITERATIVE;
|
|
195
195
|
|
|
196
196
|
tree.add(6);
|
|
197
197
|
tree.add(1);
|
|
@@ -381,7 +381,7 @@ describe('BinaryTree', () => {
|
|
|
381
381
|
let tree: BinaryTree<string>;
|
|
382
382
|
|
|
383
383
|
beforeEach(() => {
|
|
384
|
-
tree = new BinaryTree<string>({ iterationType: IterationType.RECURSIVE });
|
|
384
|
+
tree = new BinaryTree<string>([], { iterationType: IterationType.RECURSIVE });
|
|
385
385
|
});
|
|
386
386
|
|
|
387
387
|
afterEach(() => {
|
|
@@ -541,11 +541,11 @@ describe('BinaryTree', () => {
|
|
|
541
541
|
tree.add(3, 'B');
|
|
542
542
|
tree.add(7, 'C');
|
|
543
543
|
|
|
544
|
-
tree.
|
|
544
|
+
tree.iterationType = IterationType.ITERATIVE;
|
|
545
545
|
expect([...tree]).toEqual([[3, "B"], [5, "A"], [7, "C"]]);
|
|
546
|
-
tree.
|
|
546
|
+
tree.iterationType = IterationType.RECURSIVE;
|
|
547
547
|
expect([...tree]).toEqual([[3, "B"], [5, "A"], [7, "C"]]);
|
|
548
|
-
tree.
|
|
548
|
+
tree.iterationType = IterationType.ITERATIVE;
|
|
549
549
|
|
|
550
550
|
const result = tree.morris();
|
|
551
551
|
expect(result).toEqual([3, 5, 7]);
|
|
@@ -395,7 +395,7 @@ describe('BST operations test', () => {
|
|
|
395
395
|
|
|
396
396
|
describe('BST operations test recursively', () => {
|
|
397
397
|
it('should perform various operations on a Binary Search Tree with numeric values', () => {
|
|
398
|
-
const bst = new BST({ iterationType: IterationType.RECURSIVE });
|
|
398
|
+
const bst = new BST([], { iterationType: IterationType.RECURSIVE });
|
|
399
399
|
expect(bst).toBeInstanceOf(BST);
|
|
400
400
|
bst.add(11, 11);
|
|
401
401
|
bst.add(3, 3);
|
|
@@ -245,7 +245,7 @@ describe('TreeMultimap operations test', () => {
|
|
|
245
245
|
|
|
246
246
|
describe('TreeMultimap operations test recursively', () => {
|
|
247
247
|
it('should perform various operations on a Binary Search Tree with numeric values', () => {
|
|
248
|
-
const treeMultimap = new TreeMultimap({ iterationType: IterationType.RECURSIVE });
|
|
248
|
+
const treeMultimap = new TreeMultimap([], { iterationType: IterationType.RECURSIVE });
|
|
249
249
|
|
|
250
250
|
expect(treeMultimap instanceof TreeMultimap);
|
|
251
251
|
treeMultimap.add(11, 11);
|
|
@@ -8,7 +8,7 @@ describe('HashNode', () => {
|
|
|
8
8
|
|
|
9
9
|
expect(hashNode.key).toBe(key);
|
|
10
10
|
expect(hashNode.value).toBe(value);
|
|
11
|
-
expect(hashNode.next).toBe(
|
|
11
|
+
expect(hashNode.next).toBe(undefined);
|
|
12
12
|
});
|
|
13
13
|
});
|
|
14
14
|
|
|
@@ -16,7 +16,7 @@ describe('HashTable', () => {
|
|
|
16
16
|
it('should initialize with default capacity', () => {
|
|
17
17
|
const hashTable = new HashTable<string, string>();
|
|
18
18
|
expect(hashTable.capacity).toBe(16);
|
|
19
|
-
expect(hashTable.buckets).toEqual(new Array(16).fill(
|
|
19
|
+
expect(hashTable.buckets).toEqual(new Array(16).fill(undefined));
|
|
20
20
|
expect(hashTable.hashFn('a')).toBe(6);
|
|
21
21
|
expect(hashTable.capacity).toBe(16);
|
|
22
22
|
expect(hashTable.size).toBe(0);
|
|
@@ -184,3 +184,59 @@ describe('HashTable performance', function () {
|
|
|
184
184
|
}
|
|
185
185
|
});
|
|
186
186
|
});
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
describe('HashTable methods', () => {
|
|
190
|
+
let hashTable: HashTable<string, string>;
|
|
191
|
+
|
|
192
|
+
beforeEach(() => {
|
|
193
|
+
hashTable = new HashTable();
|
|
194
|
+
for (let i = 0; i < 10; i++) {
|
|
195
|
+
hashTable.set(`key${i}`, `value${i}`);
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
test('should retrieve correct values with get method', () => {
|
|
200
|
+
for (let i = 0; i < 10; i++) {
|
|
201
|
+
expect(hashTable.get(`key${i}`)).toBe(`value${i}`);
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
// test('forEach should apply a function to each key-value pair', () => {
|
|
206
|
+
// const mockCallback = jest.fn();
|
|
207
|
+
// hashTable.forEach(mockCallback);
|
|
208
|
+
//
|
|
209
|
+
// expect(mockCallback.mock.calls.length).toBe(10);
|
|
210
|
+
// for (let i = 0; i < 10; i++) {
|
|
211
|
+
// // Check whether each key-value pair has been called before, regardless of the order
|
|
212
|
+
// const call = mockCallback.mock.calls.find(call => call[1] === `value${i}`);
|
|
213
|
+
// expect(call).toBeTruthy();
|
|
214
|
+
// expect(call[0]).toBe(`key${i}`);
|
|
215
|
+
// }
|
|
216
|
+
// });
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
test('filter should return a new HashTable with elements that satisfy the condition', () => {
|
|
220
|
+
const filtered = hashTable.filter(([key]) => key.endsWith('1') || key.endsWith('3'));
|
|
221
|
+
|
|
222
|
+
expect(filtered.size).toBe(2);
|
|
223
|
+
expect(filtered.get('key1')).toBe('value1');
|
|
224
|
+
expect(filtered.get('key3')).toBe('value3');
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
test('map should return a new HashTable with mapped values', () => {
|
|
228
|
+
const mapped = hashTable.map(([, value]) => value.toUpperCase());
|
|
229
|
+
|
|
230
|
+
for (let i = 0; i < 10; i++) {
|
|
231
|
+
expect(mapped.get(`key${i}`)).toBe(`value${i}`.toUpperCase());
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
test('reduce should accumulate values based on the reducer function', () => {
|
|
236
|
+
const result = hashTable.reduce((acc, [, value]) => `${acc}-${value}`, '');
|
|
237
|
+
|
|
238
|
+
expect(result).toBe('-value5-value7-value3-value4-value6-value0-value2-value8-value1-value9');
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
});
|
|
242
|
+
|
|
@@ -23,7 +23,7 @@ describe('Heap Operation Test', () => {
|
|
|
23
23
|
});
|
|
24
24
|
|
|
25
25
|
it('should object heap work well', function () {
|
|
26
|
-
const minHeap = new MinHeap<{ a: string; key: number }>({ comparator: (a, b) => a.key - b.key });
|
|
26
|
+
const minHeap = new MinHeap<{ a: string; key: number }>([], { comparator: (a, b) => a.key - b.key });
|
|
27
27
|
minHeap.add({ key: 1, a: 'a1' });
|
|
28
28
|
minHeap.add({ key: 6, a: 'a6' });
|
|
29
29
|
minHeap.add({ key: 2, a: 'a2' });
|
|
@@ -43,7 +43,7 @@ describe('Heap Operation Test', () => {
|
|
|
43
43
|
i++;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
const maxHeap = new MaxHeap<{ key: number; a: string }>({ comparator: (a, b) => b.key - a.key });
|
|
46
|
+
const maxHeap = new MaxHeap<{ key: number; a: string }>([], { comparator: (a, b) => b.key - a.key });
|
|
47
47
|
maxHeap.add({ key: 1, a: 'a1' });
|
|
48
48
|
maxHeap.add({ key: 6, a: 'a6' });
|
|
49
49
|
maxHeap.add({ key: 5, a: 'a5' });
|
|
@@ -5,7 +5,7 @@ describe('MaxHeap', () => {
|
|
|
5
5
|
let maxHeap: MaxHeap<number>;
|
|
6
6
|
|
|
7
7
|
beforeEach(() => {
|
|
8
|
-
maxHeap = new MaxHeap({ comparator: numberComparator });
|
|
8
|
+
maxHeap = new MaxHeap([], { comparator: numberComparator });
|
|
9
9
|
});
|
|
10
10
|
|
|
11
11
|
it('add and poll elements in descending order', () => {
|
|
@@ -5,7 +5,7 @@ describe('MinHeap', () => {
|
|
|
5
5
|
let minHeap: MinHeap<number>;
|
|
6
6
|
|
|
7
7
|
beforeEach(() => {
|
|
8
|
-
minHeap = new MinHeap({ comparator: numberComparator });
|
|
8
|
+
minHeap = new MinHeap([], { comparator: numberComparator });
|
|
9
9
|
});
|
|
10
10
|
|
|
11
11
|
it('add and poll elements in ascending order', () => {
|
|
@@ -49,4 +49,52 @@ describe('MinHeap', () => {
|
|
|
49
49
|
minHeap.poll();
|
|
50
50
|
expect(minHeap.isEmpty()).toBe(true);
|
|
51
51
|
});
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
const n = 100000;
|
|
55
|
+
|
|
56
|
+
it('should push & dfs', () => {
|
|
57
|
+
for (let i = 0; i < n; i++) {
|
|
58
|
+
minHeap.push(i);
|
|
59
|
+
}
|
|
60
|
+
expect(minHeap.dfs()[0]).toBe(0)
|
|
61
|
+
expect(minHeap.dfs()[999]).toBe(4126)
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe('Heap iterative methods', () => {
|
|
67
|
+
let heap: MinHeap<number>;
|
|
68
|
+
|
|
69
|
+
beforeEach(() => {
|
|
70
|
+
heap = new MinHeap<number>();
|
|
71
|
+
for (let i = 1; i <= 10; i++) {
|
|
72
|
+
heap.add(i * 10); // Add 10, 20, ..., 100
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('Heap is iterable', () => {
|
|
77
|
+
expect([...heap]).toEqual([10, 20, 30, 40, 50, 60, 70, 80, 90, 100]);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('forEach method calls a function for each element', () => {
|
|
81
|
+
const mockCallback = jest.fn();
|
|
82
|
+
heap.forEach(mockCallback);
|
|
83
|
+
expect(mockCallback.mock.calls.length).toBe(10);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test('filter method returns filtered elements', () => {
|
|
87
|
+
const result = heap.filter(x => x > 50);
|
|
88
|
+
expect([...result]).toEqual([60, 70, 80, 90, 100]);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test('map method correctly maps elements', () => {
|
|
92
|
+
const result = heap.map(x => x / 10, (a: number, b: number) => a - b);
|
|
93
|
+
expect([...result]).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test('reduce method correctly reduces elements', () => {
|
|
97
|
+
const result = heap.reduce((acc, curr) => acc + curr, 0);
|
|
98
|
+
expect(result).toBe(550); // 10+20+...+100 = 550
|
|
99
|
+
});
|
|
52
100
|
});
|
|
@@ -166,7 +166,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
166
166
|
list.reverse();
|
|
167
167
|
|
|
168
168
|
expect(list.toArray()).toEqual([3, 2, 1]);
|
|
169
|
-
expect(list.
|
|
169
|
+
expect(list.toReversedArray()).toEqual([1, 2, 3]);
|
|
170
170
|
});
|
|
171
171
|
|
|
172
172
|
it('should map elements using a callback function', () => {
|
|
@@ -268,7 +268,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
268
268
|
list.push(2);
|
|
269
269
|
list.push(3);
|
|
270
270
|
|
|
271
|
-
const reversedArray = list.
|
|
271
|
+
const reversedArray = list.toReversedArray();
|
|
272
272
|
|
|
273
273
|
expect(reversedArray).toEqual([3, 2, 1]);
|
|
274
274
|
});
|
|
@@ -16,7 +16,7 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|
|
16
16
|
});
|
|
17
17
|
|
|
18
18
|
it('should add elements and maintain heap property in a object MaxPriorityQueue', () => {
|
|
19
|
-
const priorityQueue = new MaxPriorityQueue<{ keyA: number }>({ comparator: (a, b) => b.keyA - a.keyA });
|
|
19
|
+
const priorityQueue = new MaxPriorityQueue<{ keyA: number }>([], { comparator: (a, b) => b.keyA - a.keyA });
|
|
20
20
|
priorityQueue.refill([{ keyA: 5 }, { keyA: 3 }, { keyA: 1 }]);
|
|
21
21
|
priorityQueue.add({ keyA: 7 });
|
|
22
22
|
|
|
@@ -52,7 +52,7 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|
|
52
52
|
|
|
53
53
|
it('should correctly heapify an array', () => {
|
|
54
54
|
const array = [5, 3, 7, 1];
|
|
55
|
-
const heap = MaxPriorityQueue.heapify<number>(
|
|
55
|
+
const heap = MaxPriorityQueue.heapify<number>(array, { comparator: (a, b) => b - a });
|
|
56
56
|
heap.refill(array);
|
|
57
57
|
|
|
58
58
|
expect(heap.poll()).toBe(7);
|
|
@@ -63,7 +63,8 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|
|
63
63
|
|
|
64
64
|
it('should correctly heapify an object array', () => {
|
|
65
65
|
const elements = [{ keyA: 5 }, { keyA: 3 }, { keyA: 7 }, { keyA: 1 }];
|
|
66
|
-
|
|
66
|
+
debugger
|
|
67
|
+
const maxPQ = MaxPriorityQueue.heapify<{ keyA: number }>(elements, { comparator: (a, b) => b.keyA - a.keyA });
|
|
67
68
|
|
|
68
69
|
expect(maxPQ.poll()?.keyA).toBe(7);
|
|
69
70
|
expect(maxPQ.poll()?.keyA).toBe(5);
|
|
@@ -5,7 +5,7 @@ import { isDebugTest } from '../../../config';
|
|
|
5
5
|
const isDebug = isDebugTest;
|
|
6
6
|
describe('PriorityQueue Operation Test', () => {
|
|
7
7
|
it('should PriorityQueue poll, pee, heapify, toArray work well', function () {
|
|
8
|
-
const minPQ = new PriorityQueue<number>({ comparator: (a, b) => a - b });
|
|
8
|
+
const minPQ = new PriorityQueue<number>([], { comparator: (a, b) => a - b });
|
|
9
9
|
minPQ.refill([5, 2, 3, 4, 6, 1]);
|
|
10
10
|
expect(minPQ.toArray()).toEqual([1, 2, 3, 4, 6, 5]);
|
|
11
11
|
minPQ.poll();
|
|
@@ -14,15 +14,14 @@ describe('PriorityQueue Operation Test', () => {
|
|
|
14
14
|
expect(minPQ.toArray()).toEqual([4, 5, 6]);
|
|
15
15
|
expect(minPQ.peek()).toBe(4);
|
|
16
16
|
expect(
|
|
17
|
-
PriorityQueue.heapify({
|
|
18
|
-
elements: [3, 2, 1, 5, 6, 7, 8, 9, 10],
|
|
17
|
+
PriorityQueue.heapify([3, 2, 1, 5, 6, 7, 8, 9, 10], {
|
|
19
18
|
comparator: (a, b) => a - b
|
|
20
19
|
}).toArray()
|
|
21
|
-
).toEqual([1,
|
|
20
|
+
).toEqual([1, 3, 2, 5, 6, 7, 8, 9, 10]);
|
|
22
21
|
});
|
|
23
22
|
|
|
24
23
|
it('should Max PriorityQueue poll, peek, heapify, toArray work well', function () {
|
|
25
|
-
const maxPriorityQueue = new PriorityQueue<number>({ comparator: (a, b) => b - a });
|
|
24
|
+
const maxPriorityQueue = new PriorityQueue<number>([], { comparator: (a, b) => b - a });
|
|
26
25
|
maxPriorityQueue.refill([5, 2, 3, 4, 6, 1]);
|
|
27
26
|
expect(maxPriorityQueue.toArray()).toEqual([6, 5, 3, 4, 2, 1]);
|
|
28
27
|
maxPriorityQueue.poll();
|
|
@@ -31,15 +30,15 @@ describe('PriorityQueue Operation Test', () => {
|
|
|
31
30
|
expect(maxPriorityQueue.toArray()).toEqual([3, 2, 1]);
|
|
32
31
|
expect(maxPriorityQueue.peek()).toBe(3);
|
|
33
32
|
expect(
|
|
34
|
-
PriorityQueue.heapify({
|
|
35
|
-
|
|
33
|
+
PriorityQueue.heapify([3, 2, 1, 5, 6, 7, 8, 9, 10], {
|
|
34
|
+
|
|
36
35
|
comparator: (a, b) => a - b
|
|
37
36
|
}).toArray()
|
|
38
|
-
).toEqual([1,
|
|
37
|
+
).toEqual([1, 3, 2, 5, 6, 7, 8, 9, 10]);
|
|
39
38
|
});
|
|
40
39
|
|
|
41
40
|
it('should PriorityQueue clone, sort, getNodes, dfs work well', function () {
|
|
42
|
-
const minPQ1 = new PriorityQueue<number>({ comparator: (a, b) => a - b });
|
|
41
|
+
const minPQ1 = new PriorityQueue<number>([], { comparator: (a, b) => a - b });
|
|
43
42
|
minPQ1.refill([2, 5, 8, 3, 1, 6, 7, 4]);
|
|
44
43
|
const clonedPriorityQueue = minPQ1.clone();
|
|
45
44
|
expect(clonedPriorityQueue.elements).toEqual(minPQ1.elements);
|
|
@@ -52,7 +51,7 @@ describe('PriorityQueue Operation Test', () => {
|
|
|
52
51
|
|
|
53
52
|
describe('Priority Queue Performance Test', () => {
|
|
54
53
|
it('should numeric heap work well', function () {
|
|
55
|
-
const pq = new PriorityQueue({ comparator: (a, b) => b - a });
|
|
54
|
+
const pq = new PriorityQueue<number>([], { comparator: (a, b) => b - a });
|
|
56
55
|
|
|
57
56
|
const tS = performance.now();
|
|
58
57
|
|
|
@@ -205,3 +205,40 @@ describe('Queue Performance Test', () => {
|
|
|
205
205
|
expect(performance.now() - startTime).toBeLessThan(bigO.LINEAR * 100);
|
|
206
206
|
});
|
|
207
207
|
});
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
describe('Queue iterative methods', () => {
|
|
211
|
+
let queue: Queue<number>;
|
|
212
|
+
|
|
213
|
+
beforeEach(() => {
|
|
214
|
+
queue = new Queue();
|
|
215
|
+
for (let i = 0; i < 10; i++) {
|
|
216
|
+
queue.enqueue(i);
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
test('iterator should provide access to all elements', () => {
|
|
221
|
+
const elements = [];
|
|
222
|
+
for (const item of queue) {
|
|
223
|
+
elements.push(item);
|
|
224
|
+
}
|
|
225
|
+
expect(elements).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
test('forEach should apply the callback to each element', () => {
|
|
229
|
+
const elements: number[] = [];
|
|
230
|
+
queue.forEach((element) => elements.push(element * 2));
|
|
231
|
+
expect(elements).toEqual([0, 2, 4, 6, 8, 10, 12, 14, 16, 18]);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
test('filter should return a new queue with only the elements that satisfy the predicate', () => {
|
|
235
|
+
const filteredQueue = queue.filter(element => element % 2 === 0);
|
|
236
|
+
expect([...filteredQueue]).toEqual([0, 2, 4, 6, 8]);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
test('map should return a new queue with the transformed elements', () => {
|
|
240
|
+
const mappedQueue = queue.map(element => element * 2);
|
|
241
|
+
expect([...mappedQueue]).toEqual([0, 2, 4, 6, 8, 10, 12, 14, 16, 18]);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
});
|
|
@@ -15,7 +15,7 @@ describe('Stack', () => {
|
|
|
15
15
|
stack.push(1);
|
|
16
16
|
stack.push(2);
|
|
17
17
|
stack.push(3);
|
|
18
|
-
expect(stack.size
|
|
18
|
+
expect(stack.size).toBe(3);
|
|
19
19
|
});
|
|
20
20
|
|
|
21
21
|
it('should peek at the top element without removing it', () => {
|
|
@@ -23,7 +23,7 @@ describe('Stack', () => {
|
|
|
23
23
|
stack.push(2);
|
|
24
24
|
stack.push(3);
|
|
25
25
|
expect(stack.peek()).toBe(3);
|
|
26
|
-
expect(stack.size
|
|
26
|
+
expect(stack.size).toBe(3);
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
it('should pop elements from the stack', () => {
|
|
@@ -32,7 +32,7 @@ describe('Stack', () => {
|
|
|
32
32
|
stack.push(3);
|
|
33
33
|
const poppedElement = stack.pop();
|
|
34
34
|
expect(poppedElement).toBe(3);
|
|
35
|
-
expect(stack.size
|
|
35
|
+
expect(stack.size).toBe(2);
|
|
36
36
|
});
|
|
37
37
|
|
|
38
38
|
it('should return null when popping from an empty stack', () => {
|
|
@@ -53,7 +53,7 @@ describe('Stack', () => {
|
|
|
53
53
|
stack.push(2);
|
|
54
54
|
stack.push(3);
|
|
55
55
|
stack.clear();
|
|
56
|
-
expect(stack.size
|
|
56
|
+
expect(stack.size).toBe(0);
|
|
57
57
|
expect(stack.isEmpty()).toBe(true);
|
|
58
58
|
});
|
|
59
59
|
|
|
@@ -61,7 +61,57 @@ describe('Stack', () => {
|
|
|
61
61
|
stack.push(1);
|
|
62
62
|
stack.push(2);
|
|
63
63
|
const clonedStack = stack.clone();
|
|
64
|
-
expect(clonedStack.size
|
|
64
|
+
expect(clonedStack.size).toBe(2);
|
|
65
65
|
expect(clonedStack.pop()).toBe(2);
|
|
66
66
|
});
|
|
67
67
|
});
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
describe('Stack iterative methods', () => {
|
|
71
|
+
let stack: Stack<number>; // Declare a Stack instance
|
|
72
|
+
|
|
73
|
+
beforeEach(() => {
|
|
74
|
+
stack = new Stack<number>(); // Create a new Stack instance before each test
|
|
75
|
+
stack.push(1);
|
|
76
|
+
stack.push(2);
|
|
77
|
+
stack.push(3);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('should iterate through the stack', () => {
|
|
81
|
+
const result: number[] = [];
|
|
82
|
+
for (const element of stack) {
|
|
83
|
+
result.push(element);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
expect(result).toEqual([1, 2, 3]); // iteration should start from the top of the stack
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test('should apply forEach to the stack', () => {
|
|
90
|
+
const result: number[] = [];
|
|
91
|
+
stack.forEach((element) => {
|
|
92
|
+
result.push(element);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
expect(result).toEqual([1, 2, 3]);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test('should filter elements in the stack', () => {
|
|
99
|
+
const filteredStack = stack.filter((element) => element > 1);
|
|
100
|
+
|
|
101
|
+
expect(filteredStack.size).toBe(2);
|
|
102
|
+
expect([...filteredStack]).toEqual([2, 3]);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test('should map elements in the stack', () => {
|
|
106
|
+
const mappedStack = stack.map((element) => element * 2);
|
|
107
|
+
|
|
108
|
+
expect(mappedStack.size).toBe(3);
|
|
109
|
+
expect([...mappedStack]).toEqual([2, 4, 6]);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test('should reduce elements in the stack', () => {
|
|
113
|
+
const sum = stack.reduce((accumulator, element) => accumulator + element, 0);
|
|
114
|
+
|
|
115
|
+
expect(sum).toBe(6);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
@@ -823,3 +823,36 @@ describe('Trie operations', () => {
|
|
|
823
823
|
expect(trie.getHeight()).toBe(6); // Assuming 'apple' and 'banana' are the longest words.
|
|
824
824
|
});
|
|
825
825
|
});
|
|
826
|
+
|
|
827
|
+
describe('Trie class', () => {
|
|
828
|
+
let trie: Trie;
|
|
829
|
+
beforeEach(() => {
|
|
830
|
+
trie = new Trie(['apple', 'app', 'banana', 'band', 'bandana']);
|
|
831
|
+
});
|
|
832
|
+
|
|
833
|
+
test('[Symbol.iterator] should iterate over all words', () => {
|
|
834
|
+
const words = [...trie];
|
|
835
|
+
expect(words).toEqual(['app', 'apple', 'banana', 'band', 'bandana']);
|
|
836
|
+
});
|
|
837
|
+
|
|
838
|
+
test('forEach should execute a callback for each word', () => {
|
|
839
|
+
const mockCallback = jest.fn();
|
|
840
|
+
trie.forEach(mockCallback);
|
|
841
|
+
expect(mockCallback).toHaveBeenCalledTimes(5);
|
|
842
|
+
});
|
|
843
|
+
|
|
844
|
+
test('filter should return words that satisfy the predicate', () => {
|
|
845
|
+
const filteredWords = trie.filter(word => word.startsWith('ba'));
|
|
846
|
+
expect(filteredWords).toEqual(['banana', 'band', 'bandana']);
|
|
847
|
+
});
|
|
848
|
+
|
|
849
|
+
test('map should apply a function to each word', () => {
|
|
850
|
+
const mappedWords = trie.map(word => word.length.toString());
|
|
851
|
+
expect([...mappedWords]).toEqual(['3', '5', '6', '4', '7']);
|
|
852
|
+
});
|
|
853
|
+
|
|
854
|
+
test('reduce should reduce the words to a single value', () => {
|
|
855
|
+
const concatenatedWords = trie.reduce((acc, word) => acc + word, '');
|
|
856
|
+
expect(concatenatedWords).toEqual('appapplebananabandbandana');
|
|
857
|
+
});
|
|
858
|
+
});
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AVLTree,
|
|
3
|
+
BinaryTree,
|
|
4
|
+
BST,
|
|
5
|
+
Deque,
|
|
6
|
+
DoublyLinkedList,
|
|
7
|
+
MaxHeap,
|
|
8
|
+
MaxPriorityQueue,
|
|
9
|
+
MinHeap,
|
|
10
|
+
MinPriorityQueue,
|
|
11
|
+
Queue,
|
|
12
|
+
RedBlackTree,
|
|
13
|
+
SinglyLinkedList,
|
|
14
|
+
Stack,
|
|
15
|
+
TreeMultimap
|
|
16
|
+
} from '../../src';
|
|
17
|
+
import { isDebugTest } from "../config";
|
|
18
|
+
|
|
19
|
+
const isDebug = isDebugTest;
|
|
20
|
+
const orgArr: number[] = [6, 1, 2, 7, 5, 3, 4, 9, 8];
|
|
21
|
+
const entries: [number, number][] = [[6, 6], [1, 1], [2, 2], [7, 7], [5, 5], [3, 3], [4, 4], [9, 9], [8, 8]];
|
|
22
|
+
|
|
23
|
+
describe('conversions', () => {
|
|
24
|
+
it('Array to Queue', () => {
|
|
25
|
+
const q = new Queue<number>(orgArr);
|
|
26
|
+
isDebug && q.print();
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('Array to Deque', () => {
|
|
30
|
+
const dq = new Deque<number>(orgArr);
|
|
31
|
+
isDebug && dq.print();
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('Array to SinglyLinkedList', () => {
|
|
35
|
+
const sl = new SinglyLinkedList<number>(orgArr);
|
|
36
|
+
isDebug && sl.print();
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
it('Array to DoublyLinkedList', () => {
|
|
40
|
+
const dl = new DoublyLinkedList<number>(orgArr);
|
|
41
|
+
isDebug && dl.print();
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it('Array to Stack', () => {
|
|
45
|
+
const stack = new Stack<number>(orgArr);
|
|
46
|
+
isDebug && stack.print();
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it('Array to MinHeap', () => {
|
|
50
|
+
const minHeap = new MinHeap<number>(orgArr);
|
|
51
|
+
isDebug && minHeap.print();
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('Array to MaxHeap', () => {
|
|
55
|
+
const maxHeap = new MaxHeap<number>(orgArr);
|
|
56
|
+
isDebug && maxHeap.print();
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it('Array to MinPriorityQueue', () => {
|
|
60
|
+
const minPQ = new MinPriorityQueue<number>(orgArr);
|
|
61
|
+
isDebug && minPQ.print();
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('Array to MaxPriorityQueue', () => {
|
|
65
|
+
const maxPQ = new MaxPriorityQueue<number>(orgArr);
|
|
66
|
+
isDebug && maxPQ.print();
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it('Entry Array to BinaryTree', () => {
|
|
70
|
+
const biTree = new BinaryTree<number>(entries);
|
|
71
|
+
isDebug && biTree.print();
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it('Entry Array to BST', () => {
|
|
75
|
+
const bst = new BST<number>(orgArr);
|
|
76
|
+
expect(bst.size).toBe(9)
|
|
77
|
+
isDebug && bst.print();
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('Entry Array to RedBlackTree', () => {
|
|
81
|
+
const rbTree = new RedBlackTree<number>(orgArr);
|
|
82
|
+
expect(rbTree.size).toBe(9)
|
|
83
|
+
isDebug && rbTree.print();
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it('Entry Array to AVLTree', () => {
|
|
87
|
+
const avl = new AVLTree<number>(orgArr);
|
|
88
|
+
expect(avl.size).toBe(9)
|
|
89
|
+
isDebug && avl.print();
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it('Entry Array to TreeMultimap', () => {
|
|
93
|
+
const treeMulti = new TreeMultimap<number>(orgArr);
|
|
94
|
+
expect(treeMulti.size).toBe(9)
|
|
95
|
+
isDebug && treeMulti.print();
|
|
96
|
+
})
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
|