data-structure-typed 1.39.5 → 1.39.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 +2 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +6 -6
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +13 -13
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +7 -7
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +17 -17
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/cjs/data-structures/binary-tree/bst.js +13 -13
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +4 -4
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +7 -7
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +16 -16
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multiset.d.ts +6 -6
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js +18 -18
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/cjs/data-structures/graph/abstract-graph.js +24 -24
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +12 -12
- package/dist/cjs/data-structures/graph/directed-graph.js +15 -15
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +9 -9
- package/dist/cjs/data-structures/graph/map-graph.js +13 -13
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +11 -11
- package/dist/cjs/data-structures/graph/undirected-graph.js +14 -14
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-table.d.ts +4 -4
- package/dist/cjs/data-structures/hash/hash-table.js +8 -8
- package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +31 -31
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +54 -54
- 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 +24 -24
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +52 -52
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.js +1 -1
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/cjs/interfaces/graph.d.ts +2 -2
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +6 -6
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +13 -13
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +7 -7
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +18 -18
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/mjs/data-structures/binary-tree/bst.js +13 -13
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +4 -4
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +7 -7
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +16 -16
- package/dist/mjs/data-structures/binary-tree/tree-multiset.d.ts +6 -6
- package/dist/mjs/data-structures/binary-tree/tree-multiset.js +18 -18
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/mjs/data-structures/graph/abstract-graph.js +26 -26
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +12 -12
- package/dist/mjs/data-structures/graph/directed-graph.js +15 -15
- package/dist/mjs/data-structures/graph/map-graph.d.ts +9 -9
- package/dist/mjs/data-structures/graph/map-graph.js +13 -13
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +11 -11
- package/dist/mjs/data-structures/graph/undirected-graph.js +14 -14
- package/dist/mjs/data-structures/hash/hash-table.d.ts +4 -4
- package/dist/mjs/data-structures/hash/hash-table.js +9 -9
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +31 -31
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +55 -55
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +24 -24
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +53 -53
- package/dist/mjs/data-structures/queue/queue.js +1 -1
- package/dist/mjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/mjs/interfaces/graph.d.ts +2 -2
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +5 -5
- package/src/data-structures/binary-tree/avl-tree.ts +13 -13
- package/src/data-structures/binary-tree/binary-tree.ts +18 -18
- package/src/data-structures/binary-tree/bst.ts +16 -16
- package/src/data-structures/binary-tree/rb-tree.ts +6 -6
- package/src/data-structures/binary-tree/segment-tree.ts +15 -15
- package/src/data-structures/binary-tree/tree-multiset.ts +18 -18
- package/src/data-structures/graph/abstract-graph.ts +34 -34
- package/src/data-structures/graph/directed-graph.ts +16 -16
- package/src/data-structures/graph/map-graph.ts +13 -13
- package/src/data-structures/graph/undirected-graph.ts +15 -15
- package/src/data-structures/hash/hash-table.ts +9 -9
- package/src/data-structures/linked-list/doubly-linked-list.ts +61 -61
- package/src/data-structures/linked-list/singly-linked-list.ts +58 -58
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/interfaces/graph.ts +2 -2
- package/test/integration/bst.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-index-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +8 -8
- package/test/unit/data-structures/binary-tree/bst.test.ts +4 -4
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +2 -2
- package/test/unit/data-structures/graph/abstract-graph.test.ts +99 -2
- package/test/unit/data-structures/graph/directed-graph.test.ts +22 -12
- package/test/unit/data-structures/graph/undirected-graph.test.ts +8 -0
- package/test/unit/data-structures/hash/hash-table.test.ts +1 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +30 -30
- package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +15 -15
- package/test/utils/big-o.ts +7 -7
|
@@ -93,9 +93,9 @@ describe('DirectedGraph Operation Test', () => {
|
|
|
93
93
|
});
|
|
94
94
|
|
|
95
95
|
class MyVertex<V = any> extends DirectedVertex<V> {
|
|
96
|
-
constructor(key: VertexKey,
|
|
97
|
-
super(key,
|
|
98
|
-
this._data =
|
|
96
|
+
constructor(key: VertexKey, value?: V) {
|
|
97
|
+
super(key, value);
|
|
98
|
+
this._data = value;
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
private _data: V | undefined;
|
|
@@ -110,9 +110,9 @@ class MyVertex<V = any> extends DirectedVertex<V> {
|
|
|
110
110
|
}
|
|
111
111
|
|
|
112
112
|
class MyEdge<E = any> extends DirectedEdge<E> {
|
|
113
|
-
constructor(v1: VertexKey, v2: VertexKey, weight?: number,
|
|
114
|
-
super(v1, v2, weight,
|
|
115
|
-
this._data =
|
|
113
|
+
constructor(v1: VertexKey, v2: VertexKey, weight?: number, value?: E) {
|
|
114
|
+
super(v1, v2, weight, value);
|
|
115
|
+
this._data = value;
|
|
116
116
|
}
|
|
117
117
|
|
|
118
118
|
private _data: E | undefined;
|
|
@@ -132,12 +132,20 @@ class MyDirectedGraph<
|
|
|
132
132
|
VO extends MyVertex<V> = MyVertex<V>,
|
|
133
133
|
EO extends MyEdge<E> = MyEdge<E>
|
|
134
134
|
> extends DirectedGraph<V, E, VO, EO> {
|
|
135
|
-
createVertex(key: VertexKey,
|
|
136
|
-
return new MyVertex(key,
|
|
135
|
+
createVertex(key: VertexKey, value: V): VO {
|
|
136
|
+
return new MyVertex(key, value) as VO;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
createEdge(src: VertexKey, dest: VertexKey, weight?: number, value?: E): EO {
|
|
140
|
+
return new MyEdge(src, dest, weight ?? 1, value) as EO;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
setInEdgeMap(value: Map<VO, EO[]>) {
|
|
144
|
+
super._setInEdgeMap(value);
|
|
137
145
|
}
|
|
138
146
|
|
|
139
|
-
|
|
140
|
-
|
|
147
|
+
setOutEdgeMap(value: Map<VO, EO[]>) {
|
|
148
|
+
super._setOutEdgeMap(value);
|
|
141
149
|
}
|
|
142
150
|
}
|
|
143
151
|
|
|
@@ -164,6 +172,8 @@ describe('Inherit from DirectedGraph and perform operations', () => {
|
|
|
164
172
|
myGraph.addVertex(2, 'data2');
|
|
165
173
|
myGraph.addEdge(1, 2, 10, 'edge-data1-2');
|
|
166
174
|
myGraph.addEdge(new MyEdge(2, 1, 20, 'edge-data2-1'));
|
|
175
|
+
myGraph.setInEdgeMap(myGraph.inEdgeMap);
|
|
176
|
+
myGraph.setOutEdgeMap(myGraph.outEdgeMap);
|
|
167
177
|
|
|
168
178
|
expect(myGraph.edgeSet().length).toBe(2);
|
|
169
179
|
// TODO
|
|
@@ -182,7 +192,7 @@ describe('Inherit from DirectedGraph and perform operations', () => {
|
|
|
182
192
|
expect(edge1).toBeInstanceOf(MyEdge);
|
|
183
193
|
if (edge1) {
|
|
184
194
|
expect(edge1.data).toBe('val1');
|
|
185
|
-
expect(edge1?.
|
|
195
|
+
expect(edge1?.value).toBe('val1');
|
|
186
196
|
expect(edge1).toBeInstanceOf(MyEdge);
|
|
187
197
|
expect(edge1.src).toBe(1);
|
|
188
198
|
expect(edge1).toEqual(edge2);
|
|
@@ -204,7 +214,7 @@ describe('Inherit from DirectedGraph and perform operations', () => {
|
|
|
204
214
|
|
|
205
215
|
expect(removedEdge).toBeInstanceOf(MyEdge);
|
|
206
216
|
if (removedEdge) {
|
|
207
|
-
removedEdge && expect(removedEdge.
|
|
217
|
+
removedEdge && expect(removedEdge.value).toBe('edge-data1-2');
|
|
208
218
|
removedEdge && expect(removedEdge.src).toBe(1);
|
|
209
219
|
}
|
|
210
220
|
expect(edgeAfterRemoval).toBeNull();
|
|
@@ -7,6 +7,14 @@ describe('UndirectedGraph Operation Test', () => {
|
|
|
7
7
|
graph = new UndirectedGraph();
|
|
8
8
|
});
|
|
9
9
|
|
|
10
|
+
it('should edge cases', () => {
|
|
11
|
+
expect(graph.deleteEdge(new UndirectedEdge('c', 'd'))).toBe(null);
|
|
12
|
+
expect(graph.deleteEdgeBetween('c', 'd')).toBe(null);
|
|
13
|
+
expect(graph.degreeOf('c')).toBe(0);
|
|
14
|
+
expect(graph.edgesOf('c').length).toBe(0);
|
|
15
|
+
expect(graph.getEndsOfEdge(new UndirectedEdge('c', 'd'))).toBe(null);
|
|
16
|
+
});
|
|
17
|
+
|
|
10
18
|
it('should add vertices', () => {
|
|
11
19
|
const vertex1 = new UndirectedVertex('A');
|
|
12
20
|
const vertex2 = new UndirectedVertex('B');
|
|
@@ -4,9 +4,9 @@ import {bigO, magnitude} from '../../../utils';
|
|
|
4
4
|
describe('DoublyLinkedListNode', () => {
|
|
5
5
|
it('should DoublyLinkedListNode', () => {
|
|
6
6
|
const node1 = new DoublyLinkedListNode<number>(2);
|
|
7
|
-
expect(node1.
|
|
8
|
-
node1.
|
|
9
|
-
expect(node1.
|
|
7
|
+
expect(node1.value).toBe(2);
|
|
8
|
+
node1.value = 1;
|
|
9
|
+
expect(node1.value).toBe(1);
|
|
10
10
|
});
|
|
11
11
|
});
|
|
12
12
|
|
|
@@ -33,13 +33,13 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
33
33
|
|
|
34
34
|
it('should delete tail', () => {
|
|
35
35
|
expect(list.delete(list.tail)).toBe(true);
|
|
36
|
-
expect(list.tail?.
|
|
36
|
+
expect(list.tail?.value).toBe(4);
|
|
37
37
|
expect(list.delete(6)).toBe(false);
|
|
38
|
-
expect(list.tail?.
|
|
38
|
+
expect(list.tail?.value).toBe(4);
|
|
39
39
|
});
|
|
40
40
|
|
|
41
41
|
it('should find null', () => {
|
|
42
|
-
expect(list.find(
|
|
42
|
+
expect(list.find(value => value === 6)).toBe(null);
|
|
43
43
|
});
|
|
44
44
|
|
|
45
45
|
it('should indexOf -1', () => {
|
|
@@ -47,7 +47,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
47
47
|
});
|
|
48
48
|
|
|
49
49
|
it('should findBackward null', () => {
|
|
50
|
-
expect(list.findBackward(
|
|
50
|
+
expect(list.findBackward(value => value === 0)).toBe(null);
|
|
51
51
|
});
|
|
52
52
|
|
|
53
53
|
it('should insertAfter tail', () => {
|
|
@@ -80,8 +80,8 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
80
80
|
list.push(2);
|
|
81
81
|
list.push(3);
|
|
82
82
|
expect(list.length).toBe(3);
|
|
83
|
-
expect(list.head!.
|
|
84
|
-
expect(list.tail!.
|
|
83
|
+
expect(list.head!.value).toBe(1);
|
|
84
|
+
expect(list.tail!.value).toBe(3);
|
|
85
85
|
});
|
|
86
86
|
|
|
87
87
|
it('should pop elements from the end of the list', () => {
|
|
@@ -90,8 +90,8 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
90
90
|
const poppedValue = list.pop();
|
|
91
91
|
expect(poppedValue).toBe(2);
|
|
92
92
|
expect(list.length).toBe(1);
|
|
93
|
-
expect(list.head!.
|
|
94
|
-
expect(list.tail!.
|
|
93
|
+
expect(list.head!.value).toBe(1);
|
|
94
|
+
expect(list.tail!.value).toBe(1);
|
|
95
95
|
});
|
|
96
96
|
it('should insert elements at specific positions', () => {
|
|
97
97
|
list.push(1);
|
|
@@ -114,7 +114,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
114
114
|
list.insertAt(5, 4);
|
|
115
115
|
expect(list.length).toBe(6);
|
|
116
116
|
expect(list.getAt(5)).toBe(4);
|
|
117
|
-
expect(list.tail!.
|
|
117
|
+
expect(list.tail!.value).toBe(4);
|
|
118
118
|
});
|
|
119
119
|
|
|
120
120
|
it('should delete elements at specific positions', () => {
|
|
@@ -126,12 +126,12 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
126
126
|
const deletedValue = list.deleteAt(0);
|
|
127
127
|
expect(deletedValue).toBe(1);
|
|
128
128
|
expect(list.length).toBe(2);
|
|
129
|
-
expect(list.head!.
|
|
129
|
+
expect(list.head!.value).toBe(2);
|
|
130
130
|
|
|
131
131
|
// Deleting from the middle
|
|
132
132
|
list.deleteAt(0); // Deleting the second element
|
|
133
133
|
expect(list.length).toBe(1);
|
|
134
|
-
expect(list.head!.
|
|
134
|
+
expect(list.head!.value).toBe(3);
|
|
135
135
|
|
|
136
136
|
// Deleting from the end
|
|
137
137
|
list.deleteAt(0);
|
|
@@ -147,12 +147,12 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
147
147
|
|
|
148
148
|
list.delete(2);
|
|
149
149
|
expect(list.length).toBe(2);
|
|
150
|
-
expect(list.head!.
|
|
151
|
-
expect(list.tail!.
|
|
150
|
+
expect(list.head!.value).toBe(1);
|
|
151
|
+
expect(list.tail!.value).toBe(3);
|
|
152
152
|
|
|
153
153
|
list.delete(1);
|
|
154
154
|
expect(list.length).toBe(1);
|
|
155
|
-
expect(list.head!.
|
|
155
|
+
expect(list.head!.value).toBe(3);
|
|
156
156
|
|
|
157
157
|
list.delete(3);
|
|
158
158
|
expect(list.length).toBe(0);
|
|
@@ -176,7 +176,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
176
176
|
list.push(2);
|
|
177
177
|
list.push(3);
|
|
178
178
|
|
|
179
|
-
const mappedList = list.map(
|
|
179
|
+
const mappedList = list.map(value => value * 2);
|
|
180
180
|
|
|
181
181
|
expect(mappedList.toArray()).toEqual([2, 4, 6]);
|
|
182
182
|
});
|
|
@@ -187,7 +187,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
187
187
|
list.push(3);
|
|
188
188
|
list.push(4);
|
|
189
189
|
|
|
190
|
-
const filteredList = list.filter(
|
|
190
|
+
const filteredList = list.filter(value => value % 2 === 0);
|
|
191
191
|
|
|
192
192
|
expect(filteredList.toArray()).toEqual([2, 4]);
|
|
193
193
|
});
|
|
@@ -198,7 +198,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
198
198
|
list.push(3);
|
|
199
199
|
list.push(4);
|
|
200
200
|
|
|
201
|
-
const sum = list.reduce((acc,
|
|
201
|
+
const sum = list.reduce((acc, value) => acc + value, 0);
|
|
202
202
|
|
|
203
203
|
expect(sum).toBe(10);
|
|
204
204
|
});
|
|
@@ -227,7 +227,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
227
227
|
list.push(2);
|
|
228
228
|
list.push(3);
|
|
229
229
|
|
|
230
|
-
const found = list.find(
|
|
230
|
+
const found = list.find(value => value % 2 === 0);
|
|
231
231
|
|
|
232
232
|
expect(found).toBe(2);
|
|
233
233
|
});
|
|
@@ -248,7 +248,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
248
248
|
list.push(3);
|
|
249
249
|
list.push(4);
|
|
250
250
|
|
|
251
|
-
const lastEven = list.findBackward(
|
|
251
|
+
const lastEven = list.findBackward(value => value % 2 === 0);
|
|
252
252
|
|
|
253
253
|
expect(lastEven).toBe(4);
|
|
254
254
|
});
|
|
@@ -283,8 +283,8 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
283
283
|
list.reverse();
|
|
284
284
|
|
|
285
285
|
expect(list.toArray()).toEqual([3, 2, 1]);
|
|
286
|
-
expect(list.head?.
|
|
287
|
-
expect(list.tail?.
|
|
286
|
+
expect(list.head?.value).toBe(3);
|
|
287
|
+
expect(list.tail?.value).toBe(1);
|
|
288
288
|
});
|
|
289
289
|
|
|
290
290
|
it('should iterate over each element and apply a callback', () => {
|
|
@@ -293,8 +293,8 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
293
293
|
list.push(3);
|
|
294
294
|
|
|
295
295
|
const result: number[] = [];
|
|
296
|
-
list.forEach(
|
|
297
|
-
result.push(
|
|
296
|
+
list.forEach(value => {
|
|
297
|
+
result.push(value * 2);
|
|
298
298
|
});
|
|
299
299
|
|
|
300
300
|
expect(result).toEqual([2, 4, 6]);
|
|
@@ -305,7 +305,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
305
305
|
list.push(2);
|
|
306
306
|
list.push(3);
|
|
307
307
|
|
|
308
|
-
const mappedList = list.map(
|
|
308
|
+
const mappedList = list.map(value => value * 2);
|
|
309
309
|
|
|
310
310
|
expect(mappedList.toArray()).toEqual([2, 4, 6]);
|
|
311
311
|
});
|
|
@@ -316,7 +316,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
316
316
|
list.push(3);
|
|
317
317
|
list.push(4);
|
|
318
318
|
|
|
319
|
-
const filteredList = list.filter(
|
|
319
|
+
const filteredList = list.filter(value => value % 2 === 0);
|
|
320
320
|
|
|
321
321
|
expect(filteredList.toArray()).toEqual([2, 4]);
|
|
322
322
|
});
|
|
@@ -326,7 +326,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
326
326
|
list.push(2);
|
|
327
327
|
list.push(3);
|
|
328
328
|
|
|
329
|
-
const sum = list.reduce((acc,
|
|
329
|
+
const sum = list.reduce((acc, value) => acc + value, 0);
|
|
330
330
|
|
|
331
331
|
expect(sum).toBe(6);
|
|
332
332
|
});
|
|
@@ -387,7 +387,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
387
387
|
expect(insertSuccess).toBe(true);
|
|
388
388
|
|
|
389
389
|
const getNode = objectList.getNode(newObj); // Use newObj instead of obj2
|
|
390
|
-
expect(getNode?.
|
|
390
|
+
expect(getNode?.value).toEqual(newObj);
|
|
391
391
|
|
|
392
392
|
const deleted = objectList.delete(newObj); // Use newObj instead of obj2
|
|
393
393
|
expect(deleted).toBe(true);
|
|
@@ -26,7 +26,7 @@ describe('LinkedList Performance Test', () => {
|
|
|
26
26
|
if (i === midIndex) {
|
|
27
27
|
midSinglyNode = singlyList.getNode(i);
|
|
28
28
|
} else if (i > midIndex && midSinglyNode) {
|
|
29
|
-
singlyList.insertBefore(midSinglyNode.
|
|
29
|
+
singlyList.insertBefore(midSinglyNode.value, i);
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
|
|
@@ -4,9 +4,9 @@ import {bigO, magnitude} from '../../../utils';
|
|
|
4
4
|
describe('SinglyLinkedListNode', () => {
|
|
5
5
|
it('should SinglyLinkedList', () => {
|
|
6
6
|
const node1 = new SinglyLinkedListNode<number>(2);
|
|
7
|
-
expect(node1.
|
|
8
|
-
node1.
|
|
9
|
-
expect(node1.
|
|
7
|
+
expect(node1.value).toBe(2);
|
|
8
|
+
node1.value = 1;
|
|
9
|
+
expect(node1.value).toBe(1);
|
|
10
10
|
});
|
|
11
11
|
});
|
|
12
12
|
|
|
@@ -75,7 +75,7 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
75
75
|
list.push(3);
|
|
76
76
|
const element = list.getAt(1);
|
|
77
77
|
expect(element).toBe(2);
|
|
78
|
-
expect(list.getNodeAt(2)?.
|
|
78
|
+
expect(list.getNodeAt(2)?.value).toBe(3);
|
|
79
79
|
});
|
|
80
80
|
|
|
81
81
|
it('should return undefined for an out-of-bounds index', () => {
|
|
@@ -382,7 +382,7 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
382
382
|
expect(insertSuccess).toBe(true);
|
|
383
383
|
|
|
384
384
|
const getNode = objectList.getNode(newObj); // Use newObj instead of obj2
|
|
385
|
-
expect(getNode?.
|
|
385
|
+
expect(getNode?.value).toEqual(newObj);
|
|
386
386
|
|
|
387
387
|
const deleted = objectList.delete(newObj); // Use newObj instead of obj2
|
|
388
388
|
expect(deleted).toBe(true);
|
|
@@ -431,8 +431,8 @@ describe('SinglyLinkedList', () => {
|
|
|
431
431
|
it('should push elements to the end of the list', () => {
|
|
432
432
|
list.push(1);
|
|
433
433
|
list.push(2);
|
|
434
|
-
expect(list.head!.
|
|
435
|
-
expect(list.tail!.
|
|
434
|
+
expect(list.head!.value).toBe(1);
|
|
435
|
+
expect(list.tail!.value).toBe(2);
|
|
436
436
|
expect(list.length).toBe(2);
|
|
437
437
|
});
|
|
438
438
|
|
|
@@ -441,8 +441,8 @@ describe('SinglyLinkedList', () => {
|
|
|
441
441
|
list.push(2);
|
|
442
442
|
const popped = list.pop();
|
|
443
443
|
expect(popped).toBe(2);
|
|
444
|
-
expect(list.head!.
|
|
445
|
-
expect(list.tail!.
|
|
444
|
+
expect(list.head!.value).toBe(1);
|
|
445
|
+
expect(list.tail!.value).toBe(1);
|
|
446
446
|
expect(list.length).toBe(1);
|
|
447
447
|
});
|
|
448
448
|
|
|
@@ -451,8 +451,8 @@ describe('SinglyLinkedList', () => {
|
|
|
451
451
|
list.push(2);
|
|
452
452
|
list.push(3);
|
|
453
453
|
list.reverse();
|
|
454
|
-
expect(list.head!.
|
|
455
|
-
expect(list.tail!.
|
|
454
|
+
expect(list.head!.value).toBe(3);
|
|
455
|
+
expect(list.tail!.value).toBe(1);
|
|
456
456
|
// Add more assertions for reversed order.
|
|
457
457
|
});
|
|
458
458
|
|
|
@@ -470,14 +470,14 @@ describe('SinglyLinkedList', () => {
|
|
|
470
470
|
list.push(1);
|
|
471
471
|
list.push(2);
|
|
472
472
|
list.push(3);
|
|
473
|
-
expect(list.filter(
|
|
473
|
+
expect(list.filter(value => value !== 2).toArray()).toEqual([1, 3]);
|
|
474
474
|
});
|
|
475
475
|
|
|
476
476
|
it('should forEach the list', () => {
|
|
477
477
|
list.push(1);
|
|
478
478
|
list.push(2);
|
|
479
479
|
list.push(3);
|
|
480
|
-
list.forEach(
|
|
480
|
+
list.forEach(value => value++);
|
|
481
481
|
expect(list.toArray()).toEqual([1, 2, 3]);
|
|
482
482
|
});
|
|
483
483
|
|
|
@@ -485,11 +485,11 @@ describe('SinglyLinkedList', () => {
|
|
|
485
485
|
list.addLast(1);
|
|
486
486
|
list.push(2);
|
|
487
487
|
list.push(3);
|
|
488
|
-
expect(list.map(
|
|
488
|
+
expect(list.map(value => value * 2).toArray()).toEqual([2, 4, 6]);
|
|
489
489
|
});
|
|
490
490
|
|
|
491
491
|
it('should reduce the list', () => {
|
|
492
492
|
const list1 = SinglyLinkedList.fromArray([1, 2, 3]);
|
|
493
|
-
expect(list1.reduce((acc,
|
|
493
|
+
expect(list1.reduce((acc, value) => acc + value, 0)).toEqual(6);
|
|
494
494
|
});
|
|
495
495
|
});
|
package/test/utils/big-o.ts
CHANGED
|
@@ -59,19 +59,19 @@ function findPotentialN(input: any): number {
|
|
|
59
59
|
function linearRegression(x: number[], y: number[]) {
|
|
60
60
|
const n = x.length;
|
|
61
61
|
|
|
62
|
-
const sumX = x.reduce((acc,
|
|
63
|
-
const sumY = y.reduce((acc,
|
|
62
|
+
const sumX = x.reduce((acc, value) => acc + value, 0);
|
|
63
|
+
const sumY = y.reduce((acc, value) => acc + value, 0);
|
|
64
64
|
|
|
65
|
-
const sumXSquared = x.reduce((acc,
|
|
66
|
-
const sumXY = x.reduce((acc,
|
|
65
|
+
const sumXSquared = x.reduce((acc, value) => acc + value ** 2, 0);
|
|
66
|
+
const sumXY = x.reduce((acc, value, i) => acc + value * y[i], 0);
|
|
67
67
|
|
|
68
68
|
const slope = (n * sumXY - sumX * sumY) / (n * sumXSquared - sumX ** 2);
|
|
69
69
|
const intercept = (sumY - slope * sumX) / n;
|
|
70
70
|
|
|
71
|
-
const yHat = x.map(
|
|
71
|
+
const yHat = x.map(value => slope * value + intercept);
|
|
72
72
|
|
|
73
|
-
const totalVariation = y.map((
|
|
74
|
-
const explainedVariation = y.map(
|
|
73
|
+
const totalVariation = y.map((value, i) => (value - yHat[i]) ** 2).reduce((acc, value) => acc + value, 0);
|
|
74
|
+
const explainedVariation = y.map(value => (value - sumY / n) ** 2).reduce((acc, value) => acc + value, 0);
|
|
75
75
|
|
|
76
76
|
const rSquared = 1 - totalVariation / explainedVariation;
|
|
77
77
|
|