data-structure-typed 1.33.0 → 1.33.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{.eslintrc.json → .eslintrc.js} +2 -1
- package/.github/workflows/ci.yml +15 -3
- package/.github/workflows/release-package.yml +32 -0
- package/{.prettierrc → .prettierrc.js} +1 -1
- package/CHANGELOG.md +5 -1
- package/README.md +196 -257
- package/coverage/coverage-final.json +64 -64
- package/coverage/coverage-summary.json +16 -16
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +1 -16
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/rb-tree.js +3 -4
- package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/data-structures/graph/abstract-graph.js +12 -12
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/data-structures/hash/hash-table.js +107 -2
- package/dist/data-structures/hash/hash-table.js.map +1 -1
- package/dist/data-structures/heap/max-heap.js.map +1 -1
- package/dist/data-structures/heap/min-heap.js.map +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +30 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/data-structures/matrix/matrix2d.js +5 -8
- package/dist/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/priority-queue.js +6 -6
- package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/data-structures/queue/deque.js.map +1 -1
- package/docs/index.html +196 -256
- package/docs/modules.html +2 -0
- package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +8 -18
- package/lib/data-structures/binary-tree/abstract-binary-tree.js +5 -23
- package/lib/data-structures/binary-tree/avl-tree.d.ts +7 -10
- package/lib/data-structures/binary-tree/avl-tree.js +6 -9
- package/lib/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/lib/data-structures/binary-tree/bst.d.ts +2 -2
- package/lib/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/lib/data-structures/binary-tree/rb-tree.js +3 -3
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +3 -3
- package/lib/data-structures/binary-tree/tree-multiset.js +1 -1
- package/lib/data-structures/graph/abstract-graph.d.ts +10 -10
- package/lib/data-structures/graph/abstract-graph.js +2 -2
- package/lib/data-structures/graph/directed-graph.d.ts +6 -6
- package/lib/data-structures/graph/directed-graph.js +2 -2
- package/lib/data-structures/graph/map-graph.d.ts +6 -6
- package/lib/data-structures/graph/map-graph.js +2 -2
- package/lib/data-structures/graph/undirected-graph.d.ts +6 -6
- package/lib/data-structures/graph/undirected-graph.js +2 -2
- package/lib/data-structures/hash/hash-table.d.ts +61 -1
- package/lib/data-structures/hash/hash-table.js +136 -0
- package/lib/data-structures/heap/heap.d.ts +31 -31
- package/lib/data-structures/heap/heap.js +11 -11
- package/lib/data-structures/heap/max-heap.d.ts +4 -4
- package/lib/data-structures/heap/max-heap.js +1 -1
- package/lib/data-structures/heap/min-heap.d.ts +4 -4
- package/lib/data-structures/heap/min-heap.js +1 -1
- package/lib/data-structures/linked-list/doubly-linked-list.d.ts +93 -54
- package/lib/data-structures/linked-list/doubly-linked-list.js +79 -22
- package/lib/data-structures/linked-list/singly-linked-list.d.ts +46 -46
- package/lib/data-structures/linked-list/singly-linked-list.js +20 -20
- package/lib/data-structures/matrix/matrix.d.ts +3 -3
- package/lib/data-structures/matrix/matrix2d.d.ts +1 -1
- package/lib/data-structures/matrix/matrix2d.js +3 -2
- package/lib/data-structures/priority-queue/max-priority-queue.d.ts +4 -4
- package/lib/data-structures/priority-queue/min-priority-queue.d.ts +4 -4
- package/lib/data-structures/priority-queue/priority-queue.d.ts +26 -26
- package/lib/data-structures/priority-queue/priority-queue.js +8 -8
- package/lib/data-structures/queue/deque.d.ts +30 -30
- package/lib/data-structures/queue/deque.js +7 -7
- package/lib/data-structures/queue/queue.d.ts +27 -27
- package/lib/data-structures/queue/queue.js +8 -8
- package/lib/data-structures/stack/stack.d.ts +15 -15
- package/lib/data-structures/stack/stack.js +6 -6
- package/lib/data-structures/tree/tree.d.ts +7 -7
- package/lib/interfaces/abstract-binary-tree.d.ts +0 -1
- package/lib/interfaces/avl-tree.d.ts +1 -1
- package/lib/types/data-structures/navigator.d.ts +1 -1
- package/package.json +68 -62
- package/src/data-structures/binary-tree/aa-tree.ts +1 -0
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +1608 -0
- package/src/data-structures/binary-tree/avl-tree.ts +307 -0
- package/src/data-structures/binary-tree/b-tree.ts +1 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +76 -0
- package/src/data-structures/binary-tree/binary-tree.ts +47 -0
- package/src/data-structures/binary-tree/bst.ts +537 -0
- package/src/data-structures/binary-tree/index.ts +12 -0
- package/src/data-structures/binary-tree/rb-tree.ts +366 -0
- package/src/data-structures/binary-tree/segment-tree.ts +242 -0
- package/src/data-structures/binary-tree/splay-tree.ts +1 -0
- package/src/data-structures/binary-tree/tree-multiset.ts +700 -0
- package/src/data-structures/binary-tree/two-three-tree.ts +1 -0
- package/src/data-structures/graph/abstract-graph.ts +1040 -0
- package/src/data-structures/graph/directed-graph.ts +470 -0
- package/src/data-structures/graph/index.ts +4 -0
- package/src/data-structures/graph/map-graph.ts +129 -0
- package/src/data-structures/graph/undirected-graph.ts +274 -0
- package/src/data-structures/hash/coordinate-map.ts +67 -0
- package/src/data-structures/hash/coordinate-set.ts +56 -0
- package/src/data-structures/hash/hash-table.ts +157 -0
- package/src/data-structures/hash/index.ts +6 -0
- package/src/data-structures/hash/pair.ts +1 -0
- package/src/data-structures/hash/tree-map.ts +1 -0
- package/src/data-structures/hash/tree-set.ts +1 -0
- package/src/data-structures/heap/heap.ts +212 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +31 -0
- package/src/data-structures/heap/min-heap.ts +32 -0
- package/src/data-structures/index.ts +11 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +636 -0
- package/src/data-structures/linked-list/index.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +501 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +1 -0
- package/src/data-structures/matrix/index.ts +4 -0
- package/src/data-structures/matrix/matrix.ts +27 -0
- package/src/data-structures/matrix/matrix2d.ts +213 -0
- package/src/data-structures/matrix/navigator.ts +121 -0
- package/src/data-structures/matrix/vector2d.ts +316 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +56 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/priority-queue.ts +359 -0
- package/src/data-structures/queue/deque.ts +297 -0
- package/src/data-structures/queue/index.ts +2 -0
- package/src/data-structures/queue/queue.ts +191 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +98 -0
- package/src/data-structures/tree/index.ts +1 -0
- package/src/data-structures/tree/tree.ts +69 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +225 -0
- package/src/index.ts +4 -0
- package/src/interfaces/abstract-binary-tree.ts +189 -0
- package/src/interfaces/abstract-graph.ts +31 -0
- package/src/interfaces/avl-tree.ts +25 -0
- package/src/interfaces/binary-tree.ts +6 -0
- package/src/interfaces/bst.ts +31 -0
- package/src/interfaces/directed-graph.ts +20 -0
- package/src/interfaces/doubly-linked-list.ts +1 -0
- package/src/interfaces/heap.ts +1 -0
- package/src/interfaces/index.ts +15 -0
- package/src/interfaces/navigator.ts +1 -0
- package/src/interfaces/priority-queue.ts +1 -0
- package/src/interfaces/rb-tree.ts +9 -0
- package/src/interfaces/segment-tree.ts +1 -0
- package/src/interfaces/singly-linked-list.ts +1 -0
- package/src/interfaces/tree-multiset.ts +7 -0
- package/src/interfaces/undirected-graph.ts +6 -0
- package/src/types/data-structures/abstract-binary-tree.ts +50 -0
- package/src/types/data-structures/abstract-graph.ts +11 -0
- package/src/types/data-structures/avl-tree.ts +5 -0
- package/src/types/data-structures/binary-tree.ts +5 -0
- package/src/types/data-structures/bst.ts +13 -0
- package/src/types/data-structures/directed-graph.ts +8 -0
- package/src/types/data-structures/doubly-linked-list.ts +1 -0
- package/src/types/data-structures/heap.ts +5 -0
- package/src/types/data-structures/index.ts +15 -0
- package/src/types/data-structures/map-graph.ts +1 -0
- package/src/types/data-structures/navigator.ts +13 -0
- package/src/types/data-structures/priority-queue.ts +9 -0
- package/src/types/data-structures/rb-tree.ts +8 -0
- package/src/types/data-structures/segment-tree.ts +1 -0
- package/src/types/data-structures/singly-linked-list.ts +1 -0
- package/src/types/data-structures/tree-multiset.ts +6 -0
- package/src/types/helpers.ts +1 -0
- package/src/types/index.ts +3 -0
- package/src/types/utils/index.ts +2 -0
- package/src/types/utils/utils.ts +6 -0
- package/src/types/utils/validate-type.ts +35 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/utils.ts +79 -0
- package/test/integration/avl-tree.test.ts +14 -17
- package/test/integration/bst.test.ts +50 -41
- package/test/integration/heap.test.js +0 -3
- package/test/integration/index.html +6 -6
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +14 -17
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +142 -0
- package/test/unit/data-structures/binary-tree/bst.test.ts +50 -41
- package/test/unit/data-structures/binary-tree/overall.test.ts +36 -28
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +43 -0
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +23 -12
- package/test/unit/data-structures/graph/directed-graph.test.ts +27 -25
- package/test/unit/data-structures/graph/map-graph.test.ts +4 -5
- package/test/unit/data-structures/graph/overall.test.ts +10 -11
- package/test/unit/data-structures/graph/undirected-graph.test.ts +0 -1
- package/test/unit/data-structures/hash/coordinate-map.test.ts +54 -0
- package/test/unit/data-structures/hash/coordinate-set.test.ts +41 -0
- package/test/unit/data-structures/hash/hash-table.test.ts +97 -0
- package/test/unit/data-structures/heap/heap.test.ts +7 -8
- package/test/unit/data-structures/heap/max-heap.test.ts +7 -5
- package/test/unit/data-structures/heap/min-heap.test.ts +6 -5
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +8 -9
- package/test/unit/data-structures/linked-list/linked-list.test.ts +2 -4
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +57 -7
- package/test/unit/data-structures/linked-list/skip-linked-list.test.ts +3 -3
- package/test/unit/data-structures/matrix/matrix.test.ts +54 -0
- package/test/unit/data-structures/matrix/matrix2d.test.ts +138 -0
- package/test/unit/data-structures/matrix/navigator.test.ts +79 -0
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +5 -7
- package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +13 -13
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +8 -8
- package/test/unit/data-structures/queue/deque.test.ts +130 -0
- package/test/unit/data-structures/queue/queue.test.ts +167 -4
- package/test/unit/data-structures/stack/stack.test.ts +67 -0
- package/test/unit/data-structures/tree/tree.test.ts +39 -0
- package/test/unit/data-structures/trie/trie.test.ts +95 -0
- package/test/utils/magnitude.ts +3 -3
- package/tsconfig.json +3 -12
- package/tsconfig.prod.json +25 -0
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
- package/.auto-changelog +0 -9
- package/.gitattributes +0 -112
- package/.idea/data-structure-typed.iml +0 -19
- package/.idea/inspectionProfiles/Project_Default.xml +0 -6
- package/.idea/misc.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/.prettierignore +0 -6
- package/webpack.config.js +0 -28
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import {Deque, ArrayDeque, ObjectDeque} from '../../../../src';
|
|
2
|
+
|
|
3
|
+
describe('Deque Tests', () => {
|
|
4
|
+
// Test cases for the Deque class (DoublyLinkedList-based)
|
|
5
|
+
describe('Deque (DoublyLinkedList-based)', () => {
|
|
6
|
+
let deque: Deque<number>;
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
deque = new Deque<number>();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('should add elements at the beginning and end', () => {
|
|
13
|
+
deque.addFirst(1);
|
|
14
|
+
deque.addLast(2);
|
|
15
|
+
expect(deque.peekFirst()).toBe(1);
|
|
16
|
+
expect(deque.peekLast()).toBe(2);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('should remove elements from the beginning and end', () => {
|
|
20
|
+
deque.addFirst(1);
|
|
21
|
+
deque.addLast(2);
|
|
22
|
+
deque.pollFirst();
|
|
23
|
+
deque.pollLast();
|
|
24
|
+
expect(deque.isEmpty()).toBe(true);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('should handle edge case when removing from an empty deque', () => {
|
|
28
|
+
const result = deque.pollFirst();
|
|
29
|
+
expect(result).toBeUndefined();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should correctly report its size', () => {
|
|
33
|
+
deque.addFirst(1);
|
|
34
|
+
deque.addLast(2);
|
|
35
|
+
expect(deque.size).toBe(2);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('should handle adding and removing elements alternately', () => {
|
|
39
|
+
deque.addFirst(1);
|
|
40
|
+
expect(deque.pollFirst()).toBe(1);
|
|
41
|
+
deque.addLast(2);
|
|
42
|
+
expect(deque.pollLast()).toBe(2);
|
|
43
|
+
expect(deque.isEmpty()).toBe(true);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('should handle adding and removing elements in a cyclic manner', () => {
|
|
47
|
+
deque.addFirst(1);
|
|
48
|
+
deque.addLast(2);
|
|
49
|
+
expect(deque.pollFirst()).toBe(1);
|
|
50
|
+
deque.addFirst(3);
|
|
51
|
+
expect(deque.pollLast()).toBe(2);
|
|
52
|
+
expect(deque.size).toBe(1);
|
|
53
|
+
});
|
|
54
|
+
// Add more test cases as needed
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Test cases for the ObjectDeque class
|
|
58
|
+
describe('ObjectDeque', () => {
|
|
59
|
+
let objectDeque: ObjectDeque<string>;
|
|
60
|
+
|
|
61
|
+
beforeEach(() => {
|
|
62
|
+
objectDeque = new ObjectDeque<string>();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('should add elements at the beginning and end', () => {
|
|
66
|
+
objectDeque.addFirst('one');
|
|
67
|
+
objectDeque.addLast('two');
|
|
68
|
+
expect(objectDeque.peekFirst()).toBe('one');
|
|
69
|
+
expect(objectDeque.peekLast()).toBe('two');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('should remove elements from the beginning and end', () => {
|
|
73
|
+
objectDeque.addFirst('one');
|
|
74
|
+
objectDeque.addLast('two');
|
|
75
|
+
objectDeque.pollFirst();
|
|
76
|
+
objectDeque.pollLast();
|
|
77
|
+
expect(objectDeque.isEmpty()).toBe(true);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('should handle edge case when removing from an empty deque', () => {
|
|
81
|
+
const result = objectDeque.pollFirst();
|
|
82
|
+
expect(result).toBeUndefined();
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('should correctly report its size', () => {
|
|
86
|
+
objectDeque.addFirst('one');
|
|
87
|
+
objectDeque.addLast('two');
|
|
88
|
+
expect(objectDeque.size).toBe(2);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Add more test cases as needed
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// Test cases for the ArrayDeque class
|
|
95
|
+
describe('ArrayDeque', () => {
|
|
96
|
+
let arrayDeque: ArrayDeque<number>;
|
|
97
|
+
|
|
98
|
+
beforeEach(() => {
|
|
99
|
+
arrayDeque = new ArrayDeque<number>();
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('should add elements at the beginning and end', () => {
|
|
103
|
+
arrayDeque.addFirst(1);
|
|
104
|
+
arrayDeque.addLast(2);
|
|
105
|
+
expect(arrayDeque.peekFirst()).toBe(1);
|
|
106
|
+
expect(arrayDeque.peekLast()).toBe(2);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('should remove elements from the beginning and end', () => {
|
|
110
|
+
arrayDeque.addFirst(1);
|
|
111
|
+
arrayDeque.addLast(2);
|
|
112
|
+
arrayDeque.pollFirst();
|
|
113
|
+
arrayDeque.pollLast();
|
|
114
|
+
expect(arrayDeque.isEmpty()).toBe(true);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('should handle edge case when removing from an empty deque', () => {
|
|
118
|
+
const result = arrayDeque.pollFirst();
|
|
119
|
+
expect(result).toBeNull();
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('should correctly report its size', () => {
|
|
123
|
+
arrayDeque.addFirst(1);
|
|
124
|
+
arrayDeque.addLast(2);
|
|
125
|
+
expect(arrayDeque.size).toBe(2);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// Add more test cases as needed
|
|
129
|
+
});
|
|
130
|
+
});
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import {Queue} from '../../../../src';
|
|
1
|
+
import {Queue, LinkedListQueue} from '../../../../src';
|
|
2
2
|
import {bigO, magnitude} from '../../../utils';
|
|
3
3
|
|
|
4
4
|
describe('Queue Operation Test', () => {
|
|
5
|
-
|
|
6
5
|
it('should validate a queue', () => {
|
|
7
6
|
const queue = new Queue<number>();
|
|
8
7
|
for (let i = 0; i < 1000; i++) {
|
|
@@ -14,7 +13,6 @@ describe('Queue Operation Test', () => {
|
|
|
14
13
|
}
|
|
15
14
|
expect(last).toBe(999);
|
|
16
15
|
});
|
|
17
|
-
|
|
18
16
|
});
|
|
19
17
|
|
|
20
18
|
describe('Queue Performance Test', () => {
|
|
@@ -31,6 +29,171 @@ describe('Queue Performance Test', () => {
|
|
|
31
29
|
}
|
|
32
30
|
expect(last).toBe(magnitude.LINEAR - 1);
|
|
33
31
|
expect(performance.now() - startTime).toBeLessThan(bigO.LINEAR * 100);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe('Queue', () => {
|
|
36
|
+
let queue: Queue<number>;
|
|
37
|
+
|
|
38
|
+
beforeEach(() => {
|
|
39
|
+
queue = new Queue<number>();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('should initialize an empty queue', () => {
|
|
43
|
+
expect(queue.size).toBe(0);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('should push elements to the end of the queue', () => {
|
|
47
|
+
queue.push(1);
|
|
48
|
+
queue.push(2);
|
|
49
|
+
expect(queue.peek()).toBe(1);
|
|
50
|
+
expect(queue.size).toBe(2);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// it('should shift elements from the front of the queue', () => {
|
|
54
|
+
// queue.push(1);
|
|
55
|
+
// queue.push(2);
|
|
56
|
+
// const shifted = queue.shift();
|
|
57
|
+
// expect(shifted).toBe(1);
|
|
58
|
+
// expect(queue.peek()).toBe(2);
|
|
59
|
+
// expect(queue.size).toBe(1);
|
|
60
|
+
// });
|
|
61
|
+
//
|
|
62
|
+
// it('should peek at the front of the queue', () => {
|
|
63
|
+
// queue.push(1);
|
|
64
|
+
// queue.push(2);
|
|
65
|
+
// expect(queue.peek()).toBe(1);
|
|
66
|
+
// });
|
|
67
|
+
|
|
68
|
+
// Add more test cases for other methods of Queue.
|
|
69
|
+
});
|
|
70
|
+
describe('Queue', () => {
|
|
71
|
+
let queue: Queue<number>;
|
|
72
|
+
|
|
73
|
+
beforeEach(() => {
|
|
74
|
+
queue = new Queue<number>();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('should initialize an empty queue', () => {
|
|
78
|
+
expect(queue.size).toBe(0);
|
|
79
|
+
expect(queue.isEmpty()).toBe(true);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('should push elements to the end of the queue', () => {
|
|
83
|
+
queue.push(1);
|
|
84
|
+
queue.push(2);
|
|
85
|
+
expect(queue.size).toBe(2);
|
|
86
|
+
expect(queue.peek()).toBe(1);
|
|
87
|
+
expect(queue.peekLast()).toBe(2);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('should shift elements from the front of the queue', () => {
|
|
91
|
+
queue.push(1);
|
|
92
|
+
queue.push(2);
|
|
93
|
+
const shifted = queue.shift();
|
|
94
|
+
expect(shifted).toBe(1);
|
|
95
|
+
expect(queue.size).toBe(1);
|
|
96
|
+
expect(queue.peek()).toBe(2);
|
|
97
|
+
expect(queue.peekLast()).toBe(2);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('should handle shifting when queue reaches half size', () => {
|
|
101
|
+
for (let i = 1; i <= 5; i++) {
|
|
102
|
+
queue.push(i);
|
|
103
|
+
}
|
|
104
|
+
for (let i = 1; i <= 3; i++) {
|
|
105
|
+
queue.shift();
|
|
106
|
+
}
|
|
107
|
+
// Queue size should be 2, but internal array size is still 5.
|
|
108
|
+
// Test that shifting optimizes the internal array.
|
|
109
|
+
expect(queue.size).toBe(2);
|
|
110
|
+
expect(queue.nodes.length).toBe(2);
|
|
111
|
+
expect(queue.peek()).toBe(4);
|
|
112
|
+
});
|
|
34
113
|
|
|
114
|
+
it('should peek at the front and end of the queue', () => {
|
|
115
|
+
queue.push(1);
|
|
116
|
+
queue.push(2);
|
|
117
|
+
expect(queue.peek()).toBe(1);
|
|
118
|
+
expect(queue.peekLast()).toBe(2);
|
|
35
119
|
});
|
|
36
|
-
|
|
120
|
+
|
|
121
|
+
it('should handle shifting when the queue is empty', () => {
|
|
122
|
+
const shifted = queue.shift();
|
|
123
|
+
expect(shifted).toBeUndefined();
|
|
124
|
+
expect(queue.size).toBe(0);
|
|
125
|
+
expect(queue.peek()).toBeUndefined();
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('should handle peeking when the queue is empty', () => {
|
|
129
|
+
expect(queue.peek()).toBeUndefined();
|
|
130
|
+
expect(queue.peekLast()).toBeUndefined();
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('should handle clearing the queue', () => {
|
|
134
|
+
for (let i = 1; i <= 3; i++) {
|
|
135
|
+
queue.push(i);
|
|
136
|
+
}
|
|
137
|
+
queue.clear();
|
|
138
|
+
expect(queue.size).toBe(0);
|
|
139
|
+
expect(queue.peek()).toBeUndefined();
|
|
140
|
+
expect(queue.peekLast()).toBeUndefined();
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('should clone the queue', () => {
|
|
144
|
+
for (let i = 1; i <= 3; i++) {
|
|
145
|
+
queue.push(i);
|
|
146
|
+
}
|
|
147
|
+
const clonedQueue = queue.clone();
|
|
148
|
+
expect(clonedQueue.size).toBe(3);
|
|
149
|
+
expect(clonedQueue.peek()).toBe(1);
|
|
150
|
+
expect(clonedQueue.peekLast()).toBe(3);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('should handle creating a queue from an array', () => {
|
|
154
|
+
const elements = [1, 2, 3, 4, 5];
|
|
155
|
+
const newQueue = Queue.fromArray(elements);
|
|
156
|
+
expect(newQueue.size).toBe(5);
|
|
157
|
+
expect(newQueue.peek()).toBe(1);
|
|
158
|
+
expect(newQueue.peekLast()).toBe(5);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('should iterate through the queue', () => {
|
|
162
|
+
for (let i = 1; i <= 3; i++) {
|
|
163
|
+
queue.push(i);
|
|
164
|
+
}
|
|
165
|
+
const values = Array.from(queue);
|
|
166
|
+
expect(values).toEqual([1, 2, 3]);
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
describe('LinkedListQueue', () => {
|
|
170
|
+
let queue: LinkedListQueue<string>;
|
|
171
|
+
|
|
172
|
+
beforeEach(() => {
|
|
173
|
+
queue = new LinkedListQueue<string>();
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('should enqueue elements to the end of the queue', () => {
|
|
177
|
+
queue.enqueue('A');
|
|
178
|
+
queue.enqueue('B');
|
|
179
|
+
expect(queue.peek()).toBe('A');
|
|
180
|
+
expect(queue.length).toBe(2);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('should dequeue elements from the front of the queue', () => {
|
|
184
|
+
queue.enqueue('A');
|
|
185
|
+
queue.enqueue('B');
|
|
186
|
+
const dequeued = queue.dequeue();
|
|
187
|
+
expect(dequeued).toBe('A');
|
|
188
|
+
expect(queue.peek()).toBe('B');
|
|
189
|
+
expect(queue.length).toBe(1);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it('should peek at the front of the queue', () => {
|
|
193
|
+
queue.enqueue('A');
|
|
194
|
+
queue.enqueue('B');
|
|
195
|
+
expect(queue.peek()).toBe('A');
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// Add more test cases for other methods of LinkedListQueue.
|
|
199
|
+
});
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import {Stack} from '../../../../src';
|
|
2
|
+
|
|
3
|
+
describe('Stack', () => {
|
|
4
|
+
let stack: Stack<number>;
|
|
5
|
+
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
stack = new Stack<number>();
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('should be empty when initialized', () => {
|
|
11
|
+
expect(stack.isEmpty()).toBe(true);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('should push elements onto the stack', () => {
|
|
15
|
+
stack.push(1);
|
|
16
|
+
stack.push(2);
|
|
17
|
+
stack.push(3);
|
|
18
|
+
expect(stack.size()).toBe(3);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should peek at the top element without removing it', () => {
|
|
22
|
+
stack.push(1);
|
|
23
|
+
stack.push(2);
|
|
24
|
+
stack.push(3);
|
|
25
|
+
expect(stack.peek()).toBe(3);
|
|
26
|
+
expect(stack.size()).toBe(3);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should pop elements from the stack', () => {
|
|
30
|
+
stack.push(1);
|
|
31
|
+
stack.push(2);
|
|
32
|
+
stack.push(3);
|
|
33
|
+
const poppedElement = stack.pop();
|
|
34
|
+
expect(poppedElement).toBe(3);
|
|
35
|
+
expect(stack.size()).toBe(2);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('should return null when popping from an empty stack', () => {
|
|
39
|
+
const poppedElement = stack.pop();
|
|
40
|
+
expect(poppedElement).toBeNull();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('should convert the stack to an array', () => {
|
|
44
|
+
stack.push(1);
|
|
45
|
+
stack.push(2);
|
|
46
|
+
stack.push(3);
|
|
47
|
+
const stackArray = stack.toArray();
|
|
48
|
+
expect(stackArray).toEqual([1, 2, 3]);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should clear all elements from the stack', () => {
|
|
52
|
+
stack.push(1);
|
|
53
|
+
stack.push(2);
|
|
54
|
+
stack.push(3);
|
|
55
|
+
stack.clear();
|
|
56
|
+
expect(stack.size()).toBe(0);
|
|
57
|
+
expect(stack.isEmpty()).toBe(true);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('should clone the stack', () => {
|
|
61
|
+
stack.push(1);
|
|
62
|
+
stack.push(2);
|
|
63
|
+
const clonedStack = stack.clone();
|
|
64
|
+
expect(clonedStack.size()).toBe(2);
|
|
65
|
+
expect(clonedStack.pop()).toBe(2);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {TreeNode} from '../../../../src';
|
|
2
|
+
|
|
3
|
+
describe('TreeNode', () => {
|
|
4
|
+
it('should create a TreeNode with the given id and value', () => {
|
|
5
|
+
const node = new TreeNode<string>('1', 'Node 1');
|
|
6
|
+
expect(node.id).toBe('1');
|
|
7
|
+
expect(node.value).toBe('Node 1');
|
|
8
|
+
expect(node.children).toEqual([]);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('should add children to the TreeNode', () => {
|
|
12
|
+
const parentNode = new TreeNode<string>('1', 'Parent Node');
|
|
13
|
+
const child1 = new TreeNode<string>('2', 'Child 1');
|
|
14
|
+
const child2 = new TreeNode<string>('3', 'Child 2');
|
|
15
|
+
|
|
16
|
+
parentNode.addChildren([child1, child2]);
|
|
17
|
+
|
|
18
|
+
expect(parentNode.children).toEqual([child1, child2]);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should calculate the height of the tree correctly', () => {
|
|
22
|
+
const rootNode = new TreeNode<string>('1', 'Root Node');
|
|
23
|
+
const child1 = new TreeNode<string>('2', 'Child 1');
|
|
24
|
+
const child2 = new TreeNode<string>('3', 'Child 2');
|
|
25
|
+
const grandchild1 = new TreeNode<string>('4', 'Grandchild 1');
|
|
26
|
+
const grandchild2 = new TreeNode<string>('5', 'Grandchild 2');
|
|
27
|
+
|
|
28
|
+
rootNode.addChildren([child1, child2]);
|
|
29
|
+
child1.addChildren([grandchild1]);
|
|
30
|
+
child2.addChildren([grandchild2]);
|
|
31
|
+
|
|
32
|
+
expect(rootNode.getHeight()).toBe(3); // Height of the tree should be 3
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should handle nodes without children when calculating height', () => {
|
|
36
|
+
const rootNode = new TreeNode<string>('1', 'Root Node');
|
|
37
|
+
expect(rootNode.getHeight()).toBe(1); // Height of a single node should be 1
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import {Trie, TrieNode} from '../../../../src';
|
|
2
|
+
|
|
3
|
+
describe('TrieNode', () => {
|
|
4
|
+
it('should create a TrieNode with the given value', () => {
|
|
5
|
+
const node = new TrieNode('a');
|
|
6
|
+
expect(node.val).toBe('a');
|
|
7
|
+
expect(node.isEnd).toBe(false);
|
|
8
|
+
expect(node.children.size).toBe(0);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('should add a child to TrieNode', () => {
|
|
12
|
+
const parentNode = new TrieNode('a');
|
|
13
|
+
const childNode = new TrieNode('b');
|
|
14
|
+
parentNode.children.set('b', childNode);
|
|
15
|
+
|
|
16
|
+
expect(parentNode.children.size).toBe(1);
|
|
17
|
+
expect(parentNode.children.get('b')).toBe(childNode);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should set isEnd property correctly', () => {
|
|
21
|
+
const node = new TrieNode('a');
|
|
22
|
+
node.isEnd = true;
|
|
23
|
+
expect(node.isEnd).toBe(true);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe('Trie', () => {
|
|
28
|
+
it('should create an empty Trie', () => {
|
|
29
|
+
const trie = new Trie();
|
|
30
|
+
expect(trie.root.val).toBe('');
|
|
31
|
+
expect(trie.root.children.size).toBe(0);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('should add words to Trie', () => {
|
|
35
|
+
const trie = new Trie();
|
|
36
|
+
trie.add('apple');
|
|
37
|
+
trie.add('app');
|
|
38
|
+
expect(trie.has('apple')).toBe(true);
|
|
39
|
+
expect(trie.has('app')).toBe(true);
|
|
40
|
+
expect(trie.has('banana')).toBe(false);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('should check if a string is an absolute prefix', () => {
|
|
44
|
+
const trie = new Trie();
|
|
45
|
+
trie.add('apple');
|
|
46
|
+
trie.add('app');
|
|
47
|
+
expect(trie.isAbsPrefix('appl')).toBe(true);
|
|
48
|
+
expect(trie.isAbsPrefix('apples')).toBe(false);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should check if a string is a prefix', () => {
|
|
52
|
+
const trie = new Trie();
|
|
53
|
+
trie.add('apple');
|
|
54
|
+
trie.add('app');
|
|
55
|
+
expect(trie.isPrefix('app')).toBe(true);
|
|
56
|
+
expect(trie.isPrefix('banana')).toBe(false);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('should check if a string is a common prefix', () => {
|
|
60
|
+
const trie = new Trie();
|
|
61
|
+
trie.add('apple');
|
|
62
|
+
trie.add('app');
|
|
63
|
+
expect(trie.isCommonPrefix('ap')).toBe(true);
|
|
64
|
+
expect(trie.isCommonPrefix('app')).toBe(true);
|
|
65
|
+
expect(trie.isCommonPrefix('b')).toBe(false);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('should get the longest common prefix', () => {
|
|
69
|
+
const trie = new Trie();
|
|
70
|
+
trie.add('apple');
|
|
71
|
+
trie.add('app');
|
|
72
|
+
expect(trie.getLongestCommonPrefix()).toBe('app');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should get all words with a given prefix', () => {
|
|
76
|
+
const trie = new Trie();
|
|
77
|
+
trie.add('apple');
|
|
78
|
+
trie.add('app');
|
|
79
|
+
trie.add('application');
|
|
80
|
+
const words = trie.getAll('app');
|
|
81
|
+
expect(words).toEqual(['apple', 'application', 'app']);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('should remove words from Trie', () => {
|
|
85
|
+
const trie = new Trie();
|
|
86
|
+
trie.add('apple');
|
|
87
|
+
trie.add('app');
|
|
88
|
+
expect(trie.has('apple')).toBe(true);
|
|
89
|
+
trie.remove('apple');
|
|
90
|
+
expect(trie.has('apple')).toBe(false);
|
|
91
|
+
expect(trie.has('app')).toBe(true);
|
|
92
|
+
trie.remove('app');
|
|
93
|
+
expect(trie.has('app')).toBe(false);
|
|
94
|
+
});
|
|
95
|
+
});
|
package/test/utils/magnitude.ts
CHANGED
|
@@ -8,14 +8,14 @@ export const magnitude = {
|
|
|
8
8
|
SQUARED: Math.pow(10, 4 - orderReducedBy),
|
|
9
9
|
CUBED: Math.pow(10, 3 - orderReducedBy),
|
|
10
10
|
FACTORIAL: 20 - orderReducedBy
|
|
11
|
-
}
|
|
11
|
+
};
|
|
12
12
|
|
|
13
13
|
export const bigO = {
|
|
14
14
|
CONSTANT: magnitude.CONSTANT / 100000,
|
|
15
15
|
LOG_N: Math.log2(magnitude.LOG_N) / 1000,
|
|
16
16
|
LINEAR: magnitude.LINEAR / 1000,
|
|
17
|
-
N_LOG_N: magnitude.N_LOG_N * Math.log2(magnitude.LOG_N) / 1000,
|
|
17
|
+
N_LOG_N: (magnitude.N_LOG_N * Math.log2(magnitude.LOG_N)) / 1000,
|
|
18
18
|
SQUARED: Math.pow(magnitude.SQUARED, 2) / 1000,
|
|
19
19
|
CUBED: Math.pow(magnitude.SQUARED, 3) / 1000,
|
|
20
20
|
FACTORIAL: 10000
|
|
21
|
-
}
|
|
21
|
+
};
|
package/tsconfig.json
CHANGED
|
@@ -2,12 +2,10 @@
|
|
|
2
2
|
"compilerOptions": {
|
|
3
3
|
"outDir": "./lib",
|
|
4
4
|
"declaration": true,
|
|
5
|
-
// "declarationDir": "./lib",
|
|
6
5
|
"module": "ES6",
|
|
7
6
|
"target": "ES6",
|
|
8
7
|
"lib": [
|
|
9
|
-
"
|
|
10
|
-
"dom"
|
|
8
|
+
"ESNext"
|
|
11
9
|
],
|
|
12
10
|
"strict": true,
|
|
13
11
|
"esModuleInterop": true,
|
|
@@ -15,14 +13,6 @@
|
|
|
15
13
|
"skipLibCheck": true,
|
|
16
14
|
"downlevelIteration": true,
|
|
17
15
|
"experimentalDecorators": true,
|
|
18
|
-
// "removeComments": true,
|
|
19
|
-
// "allowJs": true,
|
|
20
|
-
// "allowSyntheticDefaultImports": true,
|
|
21
|
-
// "forceConsistentCasingInFileNames": true,
|
|
22
|
-
// "noFallthroughCasesInSwitch": true,
|
|
23
|
-
// "resolveJsonModule": true,
|
|
24
|
-
// "isolatedModules": true,
|
|
25
|
-
// "noEmit": true,
|
|
26
16
|
"typeRoots": [
|
|
27
17
|
"node_modules/@types"
|
|
28
18
|
]
|
|
@@ -32,7 +22,8 @@
|
|
|
32
22
|
"exclude": [
|
|
33
23
|
"node_modules",
|
|
34
24
|
"lib",
|
|
35
|
-
"dist"
|
|
25
|
+
"dist",
|
|
26
|
+
"umd"
|
|
36
27
|
]
|
|
37
28
|
}
|
|
38
29
|
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES5",
|
|
4
|
+
"module": "CommonJS",
|
|
5
|
+
"outDir": "./dist",
|
|
6
|
+
"sourceMap": true,
|
|
7
|
+
"lib": [
|
|
8
|
+
"ESNext"
|
|
9
|
+
],
|
|
10
|
+
"strict": true,
|
|
11
|
+
"downlevelIteration": true,
|
|
12
|
+
"removeComments": true,
|
|
13
|
+
"typeRoots": [
|
|
14
|
+
"node_modules/@types"
|
|
15
|
+
]
|
|
16
|
+
},
|
|
17
|
+
"include": ["src/**/*.ts", "src/**/*.js"],
|
|
18
|
+
"exclude": [
|
|
19
|
+
"node_modules",
|
|
20
|
+
"lib",
|
|
21
|
+
"dist",
|
|
22
|
+
"umd"
|
|
23
|
+
]
|
|
24
|
+
}
|
|
25
|
+
|