data-structure-typed 1.50.0 → 1.50.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 +2 -1
- package/README.md +204 -212
- package/benchmark/report.html +37 -1
- package/benchmark/report.json +370 -22
- package/dist/cjs/data-structures/base/iterable-base.d.ts +114 -9
- package/dist/cjs/data-structures/base/iterable-base.js +143 -7
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +43 -46
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +68 -71
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +54 -74
- package/dist/cjs/data-structures/binary-tree/bst.js +30 -71
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +78 -60
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +84 -89
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +37 -56
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +64 -85
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -0
- package/dist/cjs/data-structures/graph/abstract-graph.js +3 -0
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +14 -0
- package/dist/cjs/data-structures/graph/directed-graph.js +26 -0
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/cjs/data-structures/graph/map-graph.js +14 -0
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +16 -0
- package/dist/cjs/data-structures/graph/undirected-graph.js +25 -0
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +121 -15
- package/dist/cjs/data-structures/hash/hash-map.js +160 -25
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +66 -6
- package/dist/cjs/data-structures/heap/heap.js +66 -6
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +67 -50
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +70 -64
- 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 +128 -103
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +130 -112
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/cjs/data-structures/matrix/matrix.js +50 -11
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +49 -19
- package/dist/cjs/data-structures/queue/deque.js +101 -47
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +39 -5
- package/dist/cjs/data-structures/queue/queue.js +47 -5
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +16 -0
- package/dist/cjs/data-structures/stack/stack.js +22 -0
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +38 -1
- package/dist/cjs/data-structures/trie/trie.js +41 -0
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/cjs/types/utils/utils.d.ts +1 -0
- package/dist/mjs/data-structures/base/iterable-base.d.ts +114 -9
- package/dist/mjs/data-structures/base/iterable-base.js +143 -7
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +43 -46
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +68 -71
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +483 -375
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +54 -74
- package/dist/mjs/data-structures/binary-tree/bst.js +30 -71
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +78 -60
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +84 -89
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +37 -56
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +64 -85
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -0
- package/dist/mjs/data-structures/graph/abstract-graph.js +3 -0
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +14 -0
- package/dist/mjs/data-structures/graph/directed-graph.js +26 -0
- package/dist/mjs/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/mjs/data-structures/graph/map-graph.js +14 -0
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +16 -0
- package/dist/mjs/data-structures/graph/undirected-graph.js +25 -0
- package/dist/mjs/data-structures/hash/hash-map.d.ts +121 -15
- package/dist/mjs/data-structures/hash/hash-map.js +160 -25
- package/dist/mjs/data-structures/heap/heap.d.ts +66 -6
- package/dist/mjs/data-structures/heap/heap.js +66 -6
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +67 -50
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +70 -64
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +128 -103
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +130 -112
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/mjs/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/mjs/data-structures/matrix/matrix.js +50 -11
- package/dist/mjs/data-structures/queue/deque.d.ts +49 -19
- package/dist/mjs/data-structures/queue/deque.js +101 -47
- package/dist/mjs/data-structures/queue/queue.d.ts +39 -5
- package/dist/mjs/data-structures/queue/queue.js +47 -5
- package/dist/mjs/data-structures/stack/stack.d.ts +16 -0
- package/dist/mjs/data-structures/stack/stack.js +22 -0
- package/dist/mjs/data-structures/trie/trie.d.ts +38 -1
- package/dist/mjs/data-structures/trie/trie.js +41 -0
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/mjs/types/utils/utils.d.ts +1 -0
- package/dist/umd/data-structure-typed.js +1730 -1042
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +16 -40
- package/src/data-structures/base/iterable-base.ts +172 -19
- package/src/data-structures/binary-tree/avl-tree.ts +97 -97
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +89 -131
- package/src/data-structures/binary-tree/rb-tree.ts +127 -155
- package/src/data-structures/binary-tree/tree-multimap.ts +96 -112
- package/src/data-structures/graph/abstract-graph.ts +4 -0
- package/src/data-structures/graph/directed-graph.ts +30 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +28 -0
- package/src/data-structures/hash/hash-map.ts +175 -34
- package/src/data-structures/heap/heap.ts +66 -6
- package/src/data-structures/linked-list/doubly-linked-list.ts +72 -66
- package/src/data-structures/linked-list/singly-linked-list.ts +132 -114
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/queue/deque.ts +108 -49
- package/src/data-structures/queue/queue.ts +51 -5
- package/src/data-structures/stack/stack.ts +24 -0
- package/src/data-structures/trie/trie.ts +45 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
- package/test/performance/data-structures/graph/directed-graph.test.ts +3 -3
- package/test/performance/data-structures/queue/deque.test.ts +26 -25
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +37 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +46 -17
- package/test/unit/data-structures/binary-tree/bst.test.ts +65 -1
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +38 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -32
- package/test/unit/data-structures/graph/abstract-graph.test.ts +8 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +249 -0
- package/test/unit/data-structures/hash/hash-map.test.ts +376 -353
- package/test/unit/data-structures/heap/heap.test.ts +18 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +24 -5
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +20 -2
- package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +71 -10
- package/test/unit/data-structures/queue/queue.test.ts +23 -6
- package/test/unit/data-structures/stack/stack.test.ts +17 -0
- package/test/unit/data-structures/trie/trie.test.ts +17 -0
- package/test/unit/unrestricted-interconversion.test.ts +143 -10
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FibonacciHeap, MaxHeap, MinHeap } from '../../../../src';
|
|
1
|
+
import { FibonacciHeap, Heap, MaxHeap, MinHeap } from '../../../../src';
|
|
2
2
|
import { logBigOMetricsWrap } from '../../../utils';
|
|
3
3
|
|
|
4
4
|
describe('Heap Operation Test', () => {
|
|
@@ -26,6 +26,23 @@ describe('Heap Operation Test', () => {
|
|
|
26
26
|
expect(minNumHeap.sort()).toEqual([2, 5, 6, 9]);
|
|
27
27
|
});
|
|
28
28
|
|
|
29
|
+
it('should clone', function () {
|
|
30
|
+
const minNumHeap = new Heap<string>([], { comparator: (a, b) => Number(a) - Number(b) });
|
|
31
|
+
minNumHeap.add('1');
|
|
32
|
+
minNumHeap.add('6');
|
|
33
|
+
minNumHeap.add('2');
|
|
34
|
+
minNumHeap.add('0');
|
|
35
|
+
minNumHeap.add('5');
|
|
36
|
+
minNumHeap.add('9');
|
|
37
|
+
minNumHeap.delete('2');
|
|
38
|
+
expect([...minNumHeap]).toEqual(['0', '1', '9', '6', '5']);
|
|
39
|
+
const cloned = minNumHeap.clone();
|
|
40
|
+
expect([...cloned]).toEqual(['0', '1', '9', '6', '5']);
|
|
41
|
+
minNumHeap.delete('5');
|
|
42
|
+
expect([...minNumHeap]).toEqual(['0', '1', '9', '6']);
|
|
43
|
+
expect([...cloned]).toEqual(['0', '1', '9', '6', '5']);
|
|
44
|
+
});
|
|
45
|
+
|
|
29
46
|
it('should object heap work well', function () {
|
|
30
47
|
const minHeap = new MinHeap<{ a: string; key: number }>([], { comparator: (a, b) => a.key - b.key });
|
|
31
48
|
minHeap.add({ key: 1, a: 'a1' });
|
|
@@ -37,8 +37,27 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
37
37
|
expect(list.tail?.value).toBe(4);
|
|
38
38
|
});
|
|
39
39
|
|
|
40
|
+
it('should clone', function () {
|
|
41
|
+
const dList = new DoublyLinkedList<string>();
|
|
42
|
+
dList.addLast('1');
|
|
43
|
+
dList.addLast('6');
|
|
44
|
+
dList.addLast('2');
|
|
45
|
+
dList.addLast('0');
|
|
46
|
+
dList.addLast('5');
|
|
47
|
+
dList.addLast('9');
|
|
48
|
+
dList.delete('2');
|
|
49
|
+
expect([...dList]).toEqual(['1', '6', '0', '5', '9']);
|
|
50
|
+
const cloned = dList.clone();
|
|
51
|
+
expect([...cloned]).toEqual(['1', '6', '0', '5', '9']);
|
|
52
|
+
dList.delete('5');
|
|
53
|
+
expect([...dList]).toEqual(['1', '6', '0', '9']);
|
|
54
|
+
expect([...cloned]).toEqual(['1', '6', '0', '5', '9']);
|
|
55
|
+
});
|
|
56
|
+
|
|
40
57
|
it('should find undefined', () => {
|
|
41
58
|
expect(list.find(value => value === 6)).toBe(undefined);
|
|
59
|
+
expect(list.find(value => value === 4)).toBe(4);
|
|
60
|
+
expect(list.find(value => value === 3)).toBe(3);
|
|
42
61
|
});
|
|
43
62
|
|
|
44
63
|
it('should indexOf -1', () => {
|
|
@@ -99,19 +118,19 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
99
118
|
// Inserting at the beginning
|
|
100
119
|
list.addAt(0, 0);
|
|
101
120
|
expect(list.size).toBe(4);
|
|
102
|
-
expect(list.
|
|
103
|
-
expect(list.
|
|
121
|
+
expect(list.at(0)).toBe(0);
|
|
122
|
+
expect(list.at(1)).toBe(1);
|
|
104
123
|
|
|
105
124
|
// Inserting in the middle
|
|
106
125
|
list.addAt(2, 1.5);
|
|
107
126
|
expect(list.size).toBe(5);
|
|
108
|
-
expect(list.
|
|
109
|
-
expect(list.
|
|
127
|
+
expect(list.at(2)).toBe(1.5);
|
|
128
|
+
expect(list.at(3)).toBe(2);
|
|
110
129
|
|
|
111
130
|
// Inserting at the end
|
|
112
131
|
list.addAt(5, 4);
|
|
113
132
|
expect(list.size).toBe(6);
|
|
114
|
-
expect(list.
|
|
133
|
+
expect(list.at(5)).toBe(4);
|
|
115
134
|
expect(list.tail!.value).toBe(4);
|
|
116
135
|
});
|
|
117
136
|
|
|
@@ -72,14 +72,14 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
72
72
|
list.push(1);
|
|
73
73
|
list.push(2);
|
|
74
74
|
list.push(3);
|
|
75
|
-
const element = list.
|
|
75
|
+
const element = list.at(1);
|
|
76
76
|
expect(element).toBe(2);
|
|
77
77
|
expect(list.getNodeAt(2)?.value).toBe(3);
|
|
78
78
|
});
|
|
79
79
|
|
|
80
80
|
it('should return undefined for an out-of-bounds index', () => {
|
|
81
81
|
list.push(1);
|
|
82
|
-
const element = list.
|
|
82
|
+
const element = list.at(1);
|
|
83
83
|
expect(element).toBeUndefined();
|
|
84
84
|
});
|
|
85
85
|
});
|
|
@@ -327,6 +327,7 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
327
327
|
list.push(3);
|
|
328
328
|
const result = list.find(data => data % 2 === 0);
|
|
329
329
|
expect(result).toBe(2);
|
|
330
|
+
expect(list.find(value => value === 3)).toBe(3);
|
|
330
331
|
});
|
|
331
332
|
|
|
332
333
|
it('should return undefined if element is not found', () => {
|
|
@@ -347,6 +348,23 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
347
348
|
});
|
|
348
349
|
});
|
|
349
350
|
|
|
351
|
+
it('should clone', function () {
|
|
352
|
+
const sList = new SinglyLinkedList<string>();
|
|
353
|
+
sList.addLast('1');
|
|
354
|
+
sList.addLast('6');
|
|
355
|
+
sList.addLast('2');
|
|
356
|
+
sList.addLast('0');
|
|
357
|
+
sList.addLast('5');
|
|
358
|
+
sList.addLast('9');
|
|
359
|
+
sList.delete('2');
|
|
360
|
+
expect([...sList]).toEqual(['1', '6', '0', '5', '9']);
|
|
361
|
+
const cloned = sList.clone();
|
|
362
|
+
expect([...cloned]).toEqual(['1', '6', '0', '5', '9']);
|
|
363
|
+
sList.delete('5');
|
|
364
|
+
expect([...sList]).toEqual(['1', '6', '0', '9']);
|
|
365
|
+
expect([...cloned]).toEqual(['1', '6', '0', '5', '9']);
|
|
366
|
+
});
|
|
367
|
+
|
|
350
368
|
describe('countOccurrences', () => {
|
|
351
369
|
it('should count occurrences of a value', () => {
|
|
352
370
|
list.push(1);
|
|
@@ -40,14 +40,42 @@ describe('Deque - Basic Operations', () => {
|
|
|
40
40
|
expect(deque.isEmpty()).toBeTruthy();
|
|
41
41
|
});
|
|
42
42
|
|
|
43
|
-
test('
|
|
44
|
-
expect(deque.
|
|
45
|
-
expect(deque.
|
|
43
|
+
test('at should retrieve the correct element', () => {
|
|
44
|
+
expect(deque.at(0)).toBe(1);
|
|
45
|
+
expect(deque.at(1)).toBe(2);
|
|
46
46
|
});
|
|
47
47
|
|
|
48
48
|
test('setAt should set the correct element', () => {
|
|
49
49
|
deque.setAt(0, 3);
|
|
50
|
-
expect(deque.
|
|
50
|
+
expect(deque.at(0)).toBe(3);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('should clone', function () {
|
|
54
|
+
const deque = new Deque<string>();
|
|
55
|
+
deque.addLast('1');
|
|
56
|
+
deque.addLast('6');
|
|
57
|
+
deque.addLast('2');
|
|
58
|
+
deque.addLast('0');
|
|
59
|
+
deque.addLast('5');
|
|
60
|
+
deque.addLast('9');
|
|
61
|
+
expect(deque.size).toBe(6);
|
|
62
|
+
deque.delete('2');
|
|
63
|
+
expect(deque.size).toBe(5);
|
|
64
|
+
expect([...deque]).toEqual(['1', '6', '0', '5', '9']);
|
|
65
|
+
const cloned = deque.clone();
|
|
66
|
+
expect([...cloned]).toEqual(['1', '6', '0', '5', '9']);
|
|
67
|
+
expect(deque.size).toBe(5);
|
|
68
|
+
deque.delete('5');
|
|
69
|
+
expect(deque.size).toBe(4);
|
|
70
|
+
expect([...deque]).toEqual(['1', '6', '0', '9']);
|
|
71
|
+
expect([...cloned]).toEqual(['1', '6', '0', '5', '9']);
|
|
72
|
+
expect(cloned.size).toBe(5);
|
|
73
|
+
cloned.addLast('8');
|
|
74
|
+
expect(cloned.size).toBe(6);
|
|
75
|
+
cloned.delete('6');
|
|
76
|
+
expect(cloned.size).toBe(5);
|
|
77
|
+
cloned.delete('6');
|
|
78
|
+
expect(cloned.size).toBe(5);
|
|
51
79
|
});
|
|
52
80
|
});
|
|
53
81
|
describe('Deque - Complex Operations', () => {
|
|
@@ -68,8 +96,40 @@ describe('Deque - Complex Operations', () => {
|
|
|
68
96
|
deque.push(1);
|
|
69
97
|
deque.push(2);
|
|
70
98
|
deque.push(3);
|
|
71
|
-
deque.
|
|
99
|
+
expect(deque.size).toBe(3);
|
|
100
|
+
deque.cut(1, true);
|
|
101
|
+
expect(deque.size).toBe(2);
|
|
72
102
|
expect(deque.toArray()).toEqual([1, 2]);
|
|
103
|
+
|
|
104
|
+
const dq1 = new Deque([1, 2, 3, 4, 5, 6, 7]);
|
|
105
|
+
expect(dq1.size).toBe(7);
|
|
106
|
+
expect([...dq1.cut(3, true)]).toEqual([1, 2, 3, 4]);
|
|
107
|
+
expect(dq1.size).toBe(4);
|
|
108
|
+
expect([...dq1]).toEqual([1, 2, 3, 4]);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test('cutRest should remove elements after the specified position', () => {
|
|
112
|
+
deque.push(1);
|
|
113
|
+
deque.push(2);
|
|
114
|
+
deque.push(3);
|
|
115
|
+
deque.cutRest(1, true);
|
|
116
|
+
expect(deque.toArray()).toEqual([2, 3]);
|
|
117
|
+
|
|
118
|
+
const dq = new Deque([1, 2, 3, 4, 5, 6, 7]);
|
|
119
|
+
expect([...dq.cutRest(3, true)]).toEqual([4, 5, 6, 7]);
|
|
120
|
+
expect([...dq]).toEqual([4, 5, 6, 7]);
|
|
121
|
+
|
|
122
|
+
const deque1 = new Deque<number>();
|
|
123
|
+
|
|
124
|
+
deque1.push(1);
|
|
125
|
+
deque1.push(2);
|
|
126
|
+
deque1.push(3);
|
|
127
|
+
expect(deque1.toArray()).toEqual([1, 2, 3]);
|
|
128
|
+
expect(deque1.cutRest(1).toArray()).toEqual([2, 3]);
|
|
129
|
+
|
|
130
|
+
const dq1 = new Deque([1, 2, 3, 4, 5, 6, 7]);
|
|
131
|
+
expect([...dq1.cutRest(3)]).toEqual([4, 5, 6, 7]);
|
|
132
|
+
expect([...dq1]).toEqual([1, 2, 3, 4, 5, 6, 7]);
|
|
73
133
|
});
|
|
74
134
|
|
|
75
135
|
test('deleteAt should remove the element at the specified position', () => {
|
|
@@ -164,13 +224,14 @@ describe('Deque - Utility Operations', () => {
|
|
|
164
224
|
});
|
|
165
225
|
|
|
166
226
|
test('print should print the deque elements', () => {
|
|
167
|
-
const consoleSpy = jest.spyOn(console, 'log');
|
|
168
|
-
deque.push(1);
|
|
169
|
-
deque.push(2);
|
|
170
|
-
deque.print();
|
|
171
|
-
expect(consoleSpy).toHaveBeenCalledWith([1, 2]);
|
|
227
|
+
// const consoleSpy = jest.spyOn(console, 'log');
|
|
228
|
+
// deque.push(1);
|
|
229
|
+
// deque.push(2);
|
|
230
|
+
// deque.print();
|
|
231
|
+
// expect(consoleSpy).toHaveBeenCalledWith([1, 2]);
|
|
172
232
|
});
|
|
173
233
|
});
|
|
234
|
+
|
|
174
235
|
describe('Deque - Additional Operations', () => {
|
|
175
236
|
let deque: Deque<number>;
|
|
176
237
|
|
|
@@ -89,6 +89,23 @@ describe('Queue', () => {
|
|
|
89
89
|
}
|
|
90
90
|
expect(queue.isEmpty()).toBeTruthy();
|
|
91
91
|
});
|
|
92
|
+
|
|
93
|
+
it('should clone', function () {
|
|
94
|
+
const queue = new Queue<string>();
|
|
95
|
+
queue.push('1');
|
|
96
|
+
queue.push('6');
|
|
97
|
+
queue.push('2');
|
|
98
|
+
queue.push('0');
|
|
99
|
+
queue.push('5');
|
|
100
|
+
queue.push('9');
|
|
101
|
+
queue.delete('2');
|
|
102
|
+
expect([...queue]).toEqual(['1', '6', '0', '5', '9']);
|
|
103
|
+
const cloned = queue.clone();
|
|
104
|
+
expect([...cloned]).toEqual(['1', '6', '0', '5', '9']);
|
|
105
|
+
queue.delete('5');
|
|
106
|
+
expect([...queue]).toEqual(['1', '6', '0', '9']);
|
|
107
|
+
expect([...cloned]).toEqual(['1', '6', '0', '5', '9']);
|
|
108
|
+
});
|
|
92
109
|
});
|
|
93
110
|
|
|
94
111
|
describe('Queue - Advanced Methods', () => {
|
|
@@ -156,23 +173,23 @@ describe('Queue - Additional Methods', () => {
|
|
|
156
173
|
expect(queue.peekLast()).toBeUndefined();
|
|
157
174
|
});
|
|
158
175
|
|
|
159
|
-
test('
|
|
176
|
+
test('at should return the element at the specified index', () => {
|
|
160
177
|
queue.push(1);
|
|
161
178
|
queue.push(2);
|
|
162
179
|
queue.push(3);
|
|
163
|
-
expect(queue.
|
|
180
|
+
expect(queue.at(1)).toBe(2);
|
|
164
181
|
});
|
|
165
182
|
|
|
166
|
-
test('
|
|
183
|
+
test('at should return undefined for an invalid index', () => {
|
|
167
184
|
queue.push(1);
|
|
168
|
-
expect(queue.
|
|
169
|
-
expect(queue.
|
|
185
|
+
expect(queue.at(3)).toBeUndefined();
|
|
186
|
+
expect(queue.at(-1)).toBeUndefined();
|
|
170
187
|
});
|
|
171
188
|
|
|
172
189
|
test('print should not throw any errors', () => {
|
|
173
190
|
expect(() => {
|
|
174
191
|
queue.push(1);
|
|
175
|
-
queue.print();
|
|
192
|
+
// queue.print();
|
|
176
193
|
}).not.toThrow();
|
|
177
194
|
});
|
|
178
195
|
});
|
|
@@ -35,6 +35,23 @@ describe('Stack', () => {
|
|
|
35
35
|
expect(stack.size).toBe(2);
|
|
36
36
|
});
|
|
37
37
|
|
|
38
|
+
it('should clone', function () {
|
|
39
|
+
const stack = new Stack<string>();
|
|
40
|
+
stack.push('1');
|
|
41
|
+
stack.push('6');
|
|
42
|
+
stack.push('2');
|
|
43
|
+
stack.push('0');
|
|
44
|
+
stack.push('5');
|
|
45
|
+
stack.push('9');
|
|
46
|
+
stack.delete('2');
|
|
47
|
+
expect([...stack]).toEqual(['1', '6', '0', '5', '9']);
|
|
48
|
+
const cloned = stack.clone();
|
|
49
|
+
expect([...cloned]).toEqual(['1', '6', '0', '5', '9']);
|
|
50
|
+
stack.delete('5');
|
|
51
|
+
expect([...stack]).toEqual(['1', '6', '0', '9']);
|
|
52
|
+
expect([...cloned]).toEqual(['1', '6', '0', '5', '9']);
|
|
53
|
+
});
|
|
54
|
+
|
|
38
55
|
it('should return undefined when popping from an empty stack', () => {
|
|
39
56
|
const poppedElement = stack.pop();
|
|
40
57
|
expect(poppedElement).toBe(undefined);
|
|
@@ -56,6 +56,23 @@ describe('Trie', () => {
|
|
|
56
56
|
expect(trie.hasPrefix('banana')).toBe(false);
|
|
57
57
|
});
|
|
58
58
|
|
|
59
|
+
it('should clone', function () {
|
|
60
|
+
const trie = new Trie();
|
|
61
|
+
trie.add('1');
|
|
62
|
+
trie.add('6');
|
|
63
|
+
trie.add('2');
|
|
64
|
+
trie.add('0');
|
|
65
|
+
trie.add('5');
|
|
66
|
+
trie.add('9');
|
|
67
|
+
trie.delete('2');
|
|
68
|
+
expect([...trie]).toEqual(['1', '6', '0', '5', '9']);
|
|
69
|
+
const cloned = trie.clone();
|
|
70
|
+
expect([...cloned]).toEqual(['1', '6', '0', '5', '9']);
|
|
71
|
+
trie.delete('5');
|
|
72
|
+
expect([...trie]).toEqual(['1', '6', '0', '9']);
|
|
73
|
+
expect([...cloned]).toEqual(['1', '6', '0', '5', '9']);
|
|
74
|
+
});
|
|
75
|
+
|
|
59
76
|
it('should check if a string is a common prefix', () => {
|
|
60
77
|
const trie = new Trie();
|
|
61
78
|
trie.add('apple');
|
|
@@ -33,91 +33,155 @@ const orgStrArr: string[] = [
|
|
|
33
33
|
'trace',
|
|
34
34
|
'transmit'
|
|
35
35
|
];
|
|
36
|
-
const entries: [number,
|
|
37
|
-
[6, 6],
|
|
38
|
-
[1, 1],
|
|
39
|
-
[2, 2],
|
|
40
|
-
[7, 7],
|
|
41
|
-
[5, 5],
|
|
42
|
-
[3, 3],
|
|
43
|
-
[4, 4],
|
|
44
|
-
[9, 9],
|
|
45
|
-
[8, 8]
|
|
36
|
+
const entries: [number, string][] = [
|
|
37
|
+
[6, '6'],
|
|
38
|
+
[1, '1'],
|
|
39
|
+
[2, '2'],
|
|
40
|
+
[7, '7'],
|
|
41
|
+
[5, '5'],
|
|
42
|
+
[3, '3'],
|
|
43
|
+
[4, '4'],
|
|
44
|
+
[9, '9'],
|
|
45
|
+
[8, '8']
|
|
46
46
|
];
|
|
47
47
|
|
|
48
48
|
describe('conversions', () => {
|
|
49
49
|
it('Array to Queue', () => {
|
|
50
50
|
const q = new Queue<number>(orgArr);
|
|
51
51
|
isDebug && q.print();
|
|
52
|
+
expect([...q]).toEqual([6, 1, 2, 7, 5, 3, 4, 9, 8]);
|
|
52
53
|
});
|
|
53
54
|
|
|
54
55
|
it('Array to Deque', () => {
|
|
55
56
|
const dq = new Deque<number>(orgArr);
|
|
56
57
|
isDebug && dq.print();
|
|
58
|
+
expect([...dq]).toEqual([6, 1, 2, 7, 5, 3, 4, 9, 8]);
|
|
57
59
|
});
|
|
58
60
|
|
|
59
61
|
it('Array to SinglyLinkedList', () => {
|
|
60
62
|
const sl = new SinglyLinkedList<number>(orgArr);
|
|
61
63
|
isDebug && sl.print();
|
|
64
|
+
expect([...sl]).toEqual([6, 1, 2, 7, 5, 3, 4, 9, 8]);
|
|
62
65
|
});
|
|
63
66
|
|
|
64
67
|
it('Array to DoublyLinkedList', () => {
|
|
65
68
|
const dl = new DoublyLinkedList<number>(orgArr);
|
|
66
69
|
isDebug && dl.print();
|
|
70
|
+
expect([...dl]).toEqual([6, 1, 2, 7, 5, 3, 4, 9, 8]);
|
|
67
71
|
});
|
|
68
72
|
|
|
69
73
|
it('Array to Stack', () => {
|
|
70
74
|
const stack = new Stack<number>(orgArr);
|
|
71
75
|
isDebug && stack.print();
|
|
76
|
+
expect([...stack]).toEqual([6, 1, 2, 7, 5, 3, 4, 9, 8]);
|
|
72
77
|
});
|
|
73
78
|
|
|
74
79
|
it('Array to MinHeap', () => {
|
|
75
80
|
const minHeap = new MinHeap<number>(orgArr);
|
|
76
81
|
isDebug && minHeap.print();
|
|
82
|
+
expect([...minHeap]).toEqual([1, 5, 2, 7, 6, 3, 4, 9, 8]);
|
|
77
83
|
});
|
|
78
84
|
|
|
79
85
|
it('Array to MaxHeap', () => {
|
|
80
86
|
const maxHeap = new MaxHeap<number>(orgArr);
|
|
81
87
|
isDebug && maxHeap.print();
|
|
88
|
+
expect([...maxHeap]).toEqual([9, 8, 4, 7, 5, 2, 3, 1, 6]);
|
|
82
89
|
});
|
|
83
90
|
|
|
84
91
|
it('Array to MinPriorityQueue', () => {
|
|
85
92
|
const minPQ = new MinPriorityQueue<number>(orgArr);
|
|
86
93
|
isDebug && minPQ.print();
|
|
94
|
+
expect([...minPQ]).toEqual([1, 5, 2, 7, 6, 3, 4, 9, 8]);
|
|
87
95
|
});
|
|
88
96
|
|
|
89
97
|
it('Array to MaxPriorityQueue', () => {
|
|
90
98
|
const maxPQ = new MaxPriorityQueue<number>(orgArr);
|
|
91
99
|
isDebug && maxPQ.print();
|
|
100
|
+
expect([...maxPQ]).toEqual([9, 8, 4, 7, 5, 2, 3, 1, 6]);
|
|
92
101
|
});
|
|
93
102
|
|
|
94
103
|
it('Entry Array to BinaryTree', () => {
|
|
95
104
|
const biTree = new BinaryTree<number>(entries);
|
|
96
105
|
isDebug && biTree.print();
|
|
106
|
+
expect([...biTree]).toEqual([
|
|
107
|
+
[9, '9'],
|
|
108
|
+
[7, '7'],
|
|
109
|
+
[8, '8'],
|
|
110
|
+
[1, '1'],
|
|
111
|
+
[5, '5'],
|
|
112
|
+
[6, '6'],
|
|
113
|
+
[3, '3'],
|
|
114
|
+
[2, '2'],
|
|
115
|
+
[4, '4']
|
|
116
|
+
]);
|
|
97
117
|
});
|
|
98
118
|
|
|
99
119
|
it('Entry Array to BST', () => {
|
|
100
120
|
const bst = new BST<number>(entries);
|
|
101
121
|
expect(bst.size).toBe(9);
|
|
102
122
|
isDebug && bst.print();
|
|
123
|
+
expect([...bst]).toEqual([
|
|
124
|
+
[1, '1'],
|
|
125
|
+
[2, '2'],
|
|
126
|
+
[3, '3'],
|
|
127
|
+
[4, '4'],
|
|
128
|
+
[5, '5'],
|
|
129
|
+
[6, '6'],
|
|
130
|
+
[7, '7'],
|
|
131
|
+
[8, '8'],
|
|
132
|
+
[9, '9']
|
|
133
|
+
]);
|
|
103
134
|
});
|
|
104
135
|
|
|
105
136
|
it('Entry Array to RedBlackTree', () => {
|
|
106
137
|
const rbTree = new RedBlackTree<number>(entries);
|
|
107
138
|
expect(rbTree.size).toBe(9);
|
|
108
139
|
isDebug && rbTree.print();
|
|
140
|
+
expect([...rbTree]).toEqual([
|
|
141
|
+
[1, '1'],
|
|
142
|
+
[2, '2'],
|
|
143
|
+
[3, '3'],
|
|
144
|
+
[4, '4'],
|
|
145
|
+
[5, '5'],
|
|
146
|
+
[6, '6'],
|
|
147
|
+
[7, '7'],
|
|
148
|
+
[8, '8'],
|
|
149
|
+
[9, '9']
|
|
150
|
+
]);
|
|
109
151
|
});
|
|
110
152
|
|
|
111
153
|
it('Entry Array to AVLTree', () => {
|
|
112
154
|
const avl = new AVLTree<number>(entries);
|
|
113
155
|
expect(avl.size).toBe(9);
|
|
114
156
|
isDebug && avl.print();
|
|
157
|
+
expect([...avl]).toEqual([
|
|
158
|
+
[1, '1'],
|
|
159
|
+
[2, '2'],
|
|
160
|
+
[3, '3'],
|
|
161
|
+
[4, '4'],
|
|
162
|
+
[5, '5'],
|
|
163
|
+
[6, '6'],
|
|
164
|
+
[7, '7'],
|
|
165
|
+
[8, '8'],
|
|
166
|
+
[9, '9']
|
|
167
|
+
]);
|
|
115
168
|
});
|
|
116
169
|
|
|
117
170
|
it('Entry Array to TreeMultimap', () => {
|
|
118
171
|
const treeMulti = new TreeMultimap<number>(entries);
|
|
119
172
|
expect(treeMulti.size).toBe(9);
|
|
120
173
|
isDebug && treeMulti.print();
|
|
174
|
+
expect([...treeMulti]).toEqual([
|
|
175
|
+
[1, '1'],
|
|
176
|
+
[2, '2'],
|
|
177
|
+
[3, '3'],
|
|
178
|
+
[4, '4'],
|
|
179
|
+
[5, '5'],
|
|
180
|
+
[6, '6'],
|
|
181
|
+
[7, '7'],
|
|
182
|
+
[8, '8'],
|
|
183
|
+
[9, '9']
|
|
184
|
+
]);
|
|
121
185
|
});
|
|
122
186
|
|
|
123
187
|
it('HashMap to RedBlackTree', () => {
|
|
@@ -126,6 +190,17 @@ describe('conversions', () => {
|
|
|
126
190
|
const rbTree = new RedBlackTree<number>(hm);
|
|
127
191
|
expect(rbTree.size).toBe(9);
|
|
128
192
|
isDebug && rbTree.print();
|
|
193
|
+
expect([...rbTree]).toEqual([
|
|
194
|
+
[1, '1'],
|
|
195
|
+
[2, '2'],
|
|
196
|
+
[3, '3'],
|
|
197
|
+
[4, '4'],
|
|
198
|
+
[5, '5'],
|
|
199
|
+
[6, '6'],
|
|
200
|
+
[7, '7'],
|
|
201
|
+
[8, '8'],
|
|
202
|
+
[9, '9']
|
|
203
|
+
]);
|
|
129
204
|
});
|
|
130
205
|
|
|
131
206
|
it('PriorityQueue to BST', () => {
|
|
@@ -134,6 +209,17 @@ describe('conversions', () => {
|
|
|
134
209
|
const bst = new BST<number>(pq);
|
|
135
210
|
expect(bst.size).toBe(9);
|
|
136
211
|
isDebug && bst.print();
|
|
212
|
+
expect([...bst]).toEqual([
|
|
213
|
+
[1, undefined],
|
|
214
|
+
[2, undefined],
|
|
215
|
+
[3, undefined],
|
|
216
|
+
[4, undefined],
|
|
217
|
+
[5, undefined],
|
|
218
|
+
[6, undefined],
|
|
219
|
+
[7, undefined],
|
|
220
|
+
[8, undefined],
|
|
221
|
+
[9, undefined]
|
|
222
|
+
]);
|
|
137
223
|
});
|
|
138
224
|
|
|
139
225
|
it('Deque to RedBlackTree', () => {
|
|
@@ -142,6 +228,17 @@ describe('conversions', () => {
|
|
|
142
228
|
const rbTree = new RedBlackTree<number>(dq);
|
|
143
229
|
expect(rbTree.size).toBe(9);
|
|
144
230
|
isDebug && rbTree.print();
|
|
231
|
+
expect([...rbTree]).toEqual([
|
|
232
|
+
[1, undefined],
|
|
233
|
+
[2, undefined],
|
|
234
|
+
[3, undefined],
|
|
235
|
+
[4, undefined],
|
|
236
|
+
[5, undefined],
|
|
237
|
+
[6, undefined],
|
|
238
|
+
[7, undefined],
|
|
239
|
+
[8, undefined],
|
|
240
|
+
[9, undefined]
|
|
241
|
+
]);
|
|
145
242
|
});
|
|
146
243
|
|
|
147
244
|
it('Trie to Heap to Deque', () => {
|
|
@@ -151,12 +248,48 @@ describe('conversions', () => {
|
|
|
151
248
|
const heap = new Heap<string>(trie, { comparator: (a, b) => Number(a) - Number(b) });
|
|
152
249
|
expect(heap.size).toBe(10);
|
|
153
250
|
isDebug && heap.print();
|
|
251
|
+
expect([...heap]).toEqual([
|
|
252
|
+
'transmit',
|
|
253
|
+
'trace',
|
|
254
|
+
'tree',
|
|
255
|
+
'trend',
|
|
256
|
+
'track',
|
|
257
|
+
'trial',
|
|
258
|
+
'trip',
|
|
259
|
+
'trie',
|
|
260
|
+
'trick',
|
|
261
|
+
'triangle'
|
|
262
|
+
]);
|
|
154
263
|
const dq = new Deque<string>(heap);
|
|
155
264
|
expect(dq.size).toBe(10);
|
|
156
265
|
isDebug && dq.print();
|
|
266
|
+
expect([...dq]).toEqual([
|
|
267
|
+
'transmit',
|
|
268
|
+
'trace',
|
|
269
|
+
'tree',
|
|
270
|
+
'trend',
|
|
271
|
+
'track',
|
|
272
|
+
'trial',
|
|
273
|
+
'trip',
|
|
274
|
+
'trie',
|
|
275
|
+
'trick',
|
|
276
|
+
'triangle'
|
|
277
|
+
]);
|
|
157
278
|
const entries = dq.map((el, i) => <[number, string]>[i, el]);
|
|
158
279
|
const avl = new AVLTree<number, string>(entries);
|
|
159
280
|
expect(avl.size).toBe(10);
|
|
160
281
|
isDebug && avl.print();
|
|
282
|
+
expect([...avl]).toEqual([
|
|
283
|
+
[0, 'transmit'],
|
|
284
|
+
[1, 'trace'],
|
|
285
|
+
[2, 'tree'],
|
|
286
|
+
[3, 'trend'],
|
|
287
|
+
[4, 'track'],
|
|
288
|
+
[5, 'trial'],
|
|
289
|
+
[6, 'trip'],
|
|
290
|
+
[7, 'trie'],
|
|
291
|
+
[8, 'trick'],
|
|
292
|
+
[9, 'triangle']
|
|
293
|
+
]);
|
|
161
294
|
});
|
|
162
295
|
});
|