data-structure-typed 1.39.4 → 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 +3 -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 +96 -96
- package/dist/cjs/data-structures/graph/abstract-graph.js +64 -64
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +68 -68
- package/dist/cjs/data-structures/graph/directed-graph.js +48 -48
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +13 -13
- package/dist/cjs/data-structures/graph/map-graph.js +15 -15
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +42 -42
- package/dist/cjs/data-structures/graph/undirected-graph.js +32 -32
- 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.d.ts +1 -1
- package/dist/cjs/data-structures/queue/queue.js +4 -4
- 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 +3 -3
- 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 +96 -96
- package/dist/mjs/data-structures/graph/abstract-graph.js +66 -66
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +68 -68
- package/dist/mjs/data-structures/graph/directed-graph.js +48 -48
- package/dist/mjs/data-structures/graph/map-graph.d.ts +13 -13
- package/dist/mjs/data-structures/graph/map-graph.js +15 -15
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +42 -42
- package/dist/mjs/data-structures/graph/undirected-graph.js +32 -32
- 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.d.ts +1 -1
- package/dist/mjs/data-structures/queue/queue.js +4 -4
- package/dist/mjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/mjs/interfaces/graph.d.ts +3 -3
- 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 +156 -154
- package/src/data-structures/graph/directed-graph.ts +99 -94
- package/src/data-structures/graph/map-graph.ts +22 -25
- package/src/data-structures/graph/undirected-graph.ts +62 -60
- 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 +2 -2
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/interfaces/graph.ts +3 -3
- 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 +38 -23
- package/test/unit/data-structures/graph/map-graph.test.ts +23 -23
- package/test/unit/data-structures/graph/undirected-graph.test.ts +10 -2
- 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/unit/data-structures/queue/queue.test.ts +8 -8
- package/test/utils/big-o.ts +7 -7
|
@@ -92,52 +92,65 @@ describe('DirectedGraph Operation Test', () => {
|
|
|
92
92
|
});
|
|
93
93
|
});
|
|
94
94
|
|
|
95
|
-
class MyVertex<V
|
|
96
|
-
constructor(key: VertexKey,
|
|
97
|
-
super(key,
|
|
98
|
-
this._data =
|
|
95
|
+
class MyVertex<V = any> extends DirectedVertex<V> {
|
|
96
|
+
constructor(key: VertexKey, value?: V) {
|
|
97
|
+
super(key, value);
|
|
98
|
+
this._data = value;
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
-
private _data:
|
|
101
|
+
private _data: V | undefined;
|
|
102
102
|
|
|
103
|
-
get data():
|
|
103
|
+
get data(): V | undefined {
|
|
104
104
|
return this._data;
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
set data(value:
|
|
107
|
+
set data(value: V | undefined) {
|
|
108
108
|
this._data = value;
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
-
class MyEdge<E
|
|
113
|
-
constructor(v1: VertexKey, v2: VertexKey, weight?: number,
|
|
114
|
-
super(v1, v2, weight,
|
|
115
|
-
this._data =
|
|
112
|
+
class MyEdge<E = any> extends DirectedEdge<E> {
|
|
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
|
-
private _data:
|
|
118
|
+
private _data: E | undefined;
|
|
119
119
|
|
|
120
|
-
get data():
|
|
120
|
+
get data(): E | undefined {
|
|
121
121
|
return this._data;
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
-
set data(value:
|
|
124
|
+
set data(value: E | undefined) {
|
|
125
125
|
this._data = value;
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
128
|
|
|
129
|
-
class MyDirectedGraph<
|
|
130
|
-
|
|
131
|
-
|
|
129
|
+
class MyDirectedGraph<
|
|
130
|
+
V = any,
|
|
131
|
+
E = any,
|
|
132
|
+
VO extends MyVertex<V> = MyVertex<V>,
|
|
133
|
+
EO extends MyEdge<E> = MyEdge<E>
|
|
134
|
+
> extends DirectedGraph<V, E, VO, EO> {
|
|
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);
|
|
132
145
|
}
|
|
133
146
|
|
|
134
|
-
|
|
135
|
-
|
|
147
|
+
setOutEdgeMap(value: Map<VO, EO[]>) {
|
|
148
|
+
super._setOutEdgeMap(value);
|
|
136
149
|
}
|
|
137
150
|
}
|
|
138
151
|
|
|
139
152
|
describe('Inherit from DirectedGraph and perform operations', () => {
|
|
140
|
-
let myGraph = new MyDirectedGraph<
|
|
153
|
+
let myGraph = new MyDirectedGraph<string, string>();
|
|
141
154
|
beforeEach(() => {
|
|
142
155
|
myGraph = new MyDirectedGraph();
|
|
143
156
|
});
|
|
@@ -159,6 +172,8 @@ describe('Inherit from DirectedGraph and perform operations', () => {
|
|
|
159
172
|
myGraph.addVertex(2, 'data2');
|
|
160
173
|
myGraph.addEdge(1, 2, 10, 'edge-data1-2');
|
|
161
174
|
myGraph.addEdge(new MyEdge(2, 1, 20, 'edge-data2-1'));
|
|
175
|
+
myGraph.setInEdgeMap(myGraph.inEdgeMap);
|
|
176
|
+
myGraph.setOutEdgeMap(myGraph.outEdgeMap);
|
|
162
177
|
|
|
163
178
|
expect(myGraph.edgeSet().length).toBe(2);
|
|
164
179
|
// TODO
|
|
@@ -177,7 +192,7 @@ describe('Inherit from DirectedGraph and perform operations', () => {
|
|
|
177
192
|
expect(edge1).toBeInstanceOf(MyEdge);
|
|
178
193
|
if (edge1) {
|
|
179
194
|
expect(edge1.data).toBe('val1');
|
|
180
|
-
expect(edge1?.
|
|
195
|
+
expect(edge1?.value).toBe('val1');
|
|
181
196
|
expect(edge1).toBeInstanceOf(MyEdge);
|
|
182
197
|
expect(edge1.src).toBe(1);
|
|
183
198
|
expect(edge1).toEqual(edge2);
|
|
@@ -199,7 +214,7 @@ describe('Inherit from DirectedGraph and perform operations', () => {
|
|
|
199
214
|
|
|
200
215
|
expect(removedEdge).toBeInstanceOf(MyEdge);
|
|
201
216
|
if (removedEdge) {
|
|
202
|
-
removedEdge && expect(removedEdge.
|
|
217
|
+
removedEdge && expect(removedEdge.value).toBe('edge-data1-2');
|
|
203
218
|
removedEdge && expect(removedEdge.src).toBe(1);
|
|
204
219
|
}
|
|
205
220
|
expect(edgeAfterRemoval).toBeNull();
|
|
@@ -234,7 +249,7 @@ describe('Inherit from DirectedGraph and perform operations', () => {
|
|
|
234
249
|
});
|
|
235
250
|
|
|
236
251
|
describe('Inherit from DirectedGraph and perform operations test2.', () => {
|
|
237
|
-
const myGraph = new MyDirectedGraph<
|
|
252
|
+
const myGraph = new MyDirectedGraph<string, string>();
|
|
238
253
|
|
|
239
254
|
it('should test graph operations', () => {
|
|
240
255
|
const vertex1 = new MyVertex(1, 'data1');
|
|
@@ -4,17 +4,17 @@ describe('MapGraph Operation Test', () => {
|
|
|
4
4
|
it('dijkstra shortest path', () => {
|
|
5
5
|
const mapGraph = new MapGraph([5.500338, 100.173665]);
|
|
6
6
|
|
|
7
|
-
mapGraph.addVertex(new MapVertex('Surin', 5.466724, 100.274805));
|
|
8
|
-
mapGraph.addVertex(new MapVertex('Batu Feringgi Beach', 5.475141, 100.27667));
|
|
9
|
-
mapGraph.addVertex(new MapVertex('Lotus', 5.459044, 100.308767));
|
|
10
|
-
mapGraph.addVertex(new MapVertex('The Breeza', 5.454197, 100.307859));
|
|
11
|
-
mapGraph.addVertex(new MapVertex('Hard Rock Hotel', 5.46785, 100.241876));
|
|
12
|
-
mapGraph.addVertex(new MapVertex('Mira', 5.456749, 100.28665));
|
|
13
|
-
mapGraph.addVertex(new MapVertex('Penang Bible Church', 5.428683, 100.314825));
|
|
14
|
-
mapGraph.addVertex(new MapVertex('Queensbay', 5.33276, 100.306651));
|
|
15
|
-
mapGraph.addVertex(new MapVertex('Saanen Goat Farm', 5.405738, 100.207699));
|
|
16
|
-
mapGraph.addVertex(new MapVertex('Trinity Auto', 5.401126, 100.303739));
|
|
17
|
-
mapGraph.addVertex(new MapVertex('Penang Airport', 5.293185, 100.265772));
|
|
7
|
+
mapGraph.addVertex(new MapVertex('Surin', '', 5.466724, 100.274805));
|
|
8
|
+
mapGraph.addVertex(new MapVertex('Batu Feringgi Beach', '', 5.475141, 100.27667));
|
|
9
|
+
mapGraph.addVertex(new MapVertex('Lotus', '', 5.459044, 100.308767));
|
|
10
|
+
mapGraph.addVertex(new MapVertex('The Breeza', '', 5.454197, 100.307859));
|
|
11
|
+
mapGraph.addVertex(new MapVertex('Hard Rock Hotel', '', 5.46785, 100.241876));
|
|
12
|
+
mapGraph.addVertex(new MapVertex('Mira', '', 5.456749, 100.28665));
|
|
13
|
+
mapGraph.addVertex(new MapVertex('Penang Bible Church', '', 5.428683, 100.314825));
|
|
14
|
+
mapGraph.addVertex(new MapVertex('Queensbay', '', 5.33276, 100.306651));
|
|
15
|
+
mapGraph.addVertex(new MapVertex('Saanen Goat Farm', '', 5.405738, 100.207699));
|
|
16
|
+
mapGraph.addVertex(new MapVertex('Trinity Auto', '', 5.401126, 100.303739));
|
|
17
|
+
mapGraph.addVertex(new MapVertex('Penang Airport', '', 5.293185, 100.265772));
|
|
18
18
|
mapGraph.addEdge('Surin', 'Lotus', 4.7);
|
|
19
19
|
mapGraph.addEdge('Lotus', 'The Breeza', 1);
|
|
20
20
|
mapGraph.addEdge('Batu Feringgi Beach', 'Hard Rock Hotel', 5.2);
|
|
@@ -45,17 +45,17 @@ describe('MapGraph Operation Test', () => {
|
|
|
45
45
|
});
|
|
46
46
|
|
|
47
47
|
describe('MapGraph', () => {
|
|
48
|
-
let mapGraph: MapGraph
|
|
48
|
+
let mapGraph: MapGraph<string, string>;
|
|
49
49
|
|
|
50
50
|
beforeEach(() => {
|
|
51
51
|
// Create a new MapGraph instance before each test
|
|
52
|
-
mapGraph = new MapGraph([0, 0], [100, 100]);
|
|
52
|
+
mapGraph = new MapGraph<string, string>([0, 0], [100, 100]);
|
|
53
53
|
});
|
|
54
54
|
|
|
55
55
|
// Test adding vertices to the graph
|
|
56
56
|
it('should add vertices to the graph', () => {
|
|
57
|
-
const locationA = new MapVertex('A',
|
|
58
|
-
const locationB = new MapVertex('B',
|
|
57
|
+
const locationA = new MapVertex('A', 'Location A', 10, 20);
|
|
58
|
+
const locationB = new MapVertex('B', 'Location B', 30, 40);
|
|
59
59
|
|
|
60
60
|
mapGraph.addVertex(locationA);
|
|
61
61
|
mapGraph.addVertex(locationB);
|
|
@@ -66,8 +66,8 @@ describe('MapGraph', () => {
|
|
|
66
66
|
|
|
67
67
|
// Test adding edges to the graph
|
|
68
68
|
it('should add edges to the graph', () => {
|
|
69
|
-
const locationA = new MapVertex('A',
|
|
70
|
-
const locationB = new MapVertex('B',
|
|
69
|
+
const locationA = new MapVertex('A', 'Location A', 10, 20);
|
|
70
|
+
const locationB = new MapVertex('B', 'Location B', 30, 40);
|
|
71
71
|
const edgeAB = new MapEdge('A', 'B', 50, 'Edge from A to B');
|
|
72
72
|
|
|
73
73
|
mapGraph.addVertex(locationA);
|
|
@@ -79,12 +79,12 @@ describe('MapGraph', () => {
|
|
|
79
79
|
|
|
80
80
|
// Test getting neighbors of a vertex
|
|
81
81
|
it('should return the neighbors of a vertex', () => {
|
|
82
|
-
const locationA = new MapVertex('A',
|
|
82
|
+
const locationA = new MapVertex('A', 'Location A', 10, 20);
|
|
83
83
|
locationA.lat = locationA.lat;
|
|
84
84
|
locationA.long = locationA.long;
|
|
85
|
-
const locationB = mapGraph.createVertex('B',
|
|
85
|
+
const locationB = mapGraph.createVertex('B', 'Location B', 30, 40);
|
|
86
86
|
|
|
87
|
-
const locationC = new MapVertex('C',
|
|
87
|
+
const locationC = new MapVertex('C', 'Location C', 50, 60);
|
|
88
88
|
const edgeAB = new MapEdge('A', 'B', 50, 'Edge from A to B');
|
|
89
89
|
const edgeBC = new MapEdge('B', 'C', 60, 'Edge from B to C');
|
|
90
90
|
|
|
@@ -106,9 +106,9 @@ describe('MapGraph', () => {
|
|
|
106
106
|
|
|
107
107
|
// Test finding the shortest path between locations
|
|
108
108
|
it('should find the shortest path between two locations', () => {
|
|
109
|
-
const locationA = new MapVertex('A',
|
|
110
|
-
const locationB = new MapVertex('B',
|
|
111
|
-
const locationC = new MapVertex('C',
|
|
109
|
+
const locationA = new MapVertex('A', 'Location A', 10, 20);
|
|
110
|
+
const locationB = new MapVertex('B', 'Location B', 30, 40);
|
|
111
|
+
const locationC = new MapVertex('C', 'Location C', 50, 60);
|
|
112
112
|
const edgeAB = new MapEdge('A', 'B', 50, 'Edge from A to B');
|
|
113
113
|
const edgeBC = new MapEdge('B', 'C', 60, 'Edge from B to C');
|
|
114
114
|
|
|
@@ -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');
|
|
@@ -59,11 +67,11 @@ describe('UndirectedGraph Operation Test', () => {
|
|
|
59
67
|
});
|
|
60
68
|
|
|
61
69
|
describe('UndirectedGraph', () => {
|
|
62
|
-
let undirectedGraph: UndirectedGraph<
|
|
70
|
+
let undirectedGraph: UndirectedGraph<string, string>;
|
|
63
71
|
|
|
64
72
|
beforeEach(() => {
|
|
65
73
|
// Create a new UndirectedGraph instance before each test
|
|
66
|
-
undirectedGraph = new UndirectedGraph<
|
|
74
|
+
undirectedGraph = new UndirectedGraph<string, string>();
|
|
67
75
|
});
|
|
68
76
|
|
|
69
77
|
// Test adding vertices to the graph
|
|
@@ -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
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {SkipQueue, Queue} from '../../../../src';
|
|
2
2
|
import {bigO, magnitude} from '../../../utils';
|
|
3
3
|
import {isDebugTest} from '../../../config';
|
|
4
4
|
|
|
@@ -168,11 +168,11 @@ describe('Queue', () => {
|
|
|
168
168
|
expect(values).toEqual([1, 2, 3]);
|
|
169
169
|
});
|
|
170
170
|
});
|
|
171
|
-
describe('
|
|
172
|
-
let queue:
|
|
171
|
+
describe('SkipQueue', () => {
|
|
172
|
+
let queue: SkipQueue<string>;
|
|
173
173
|
|
|
174
174
|
beforeEach(() => {
|
|
175
|
-
queue = new
|
|
175
|
+
queue = new SkipQueue<string>();
|
|
176
176
|
});
|
|
177
177
|
|
|
178
178
|
it('should enqueue elements to the end of the queue', () => {
|
|
@@ -197,7 +197,7 @@ describe('LinkedListQueue', () => {
|
|
|
197
197
|
expect(queue.peek()).toBe('A');
|
|
198
198
|
});
|
|
199
199
|
|
|
200
|
-
// Add more test cases for other methods of
|
|
200
|
+
// Add more test cases for other methods of SkipQueue.
|
|
201
201
|
});
|
|
202
202
|
|
|
203
203
|
describe('Queue Performance Test', () => {
|
|
@@ -228,16 +228,16 @@ describe('Queue Performance Test', () => {
|
|
|
228
228
|
expect(performance.now() - startTime2).toBeLessThan(bigO.CUBED * 100);
|
|
229
229
|
});
|
|
230
230
|
|
|
231
|
-
it('should numeric
|
|
231
|
+
it('should numeric SkipQueue be efficient', function () {
|
|
232
232
|
const startTime = performance.now();
|
|
233
|
-
const queue = new
|
|
233
|
+
const queue = new SkipQueue<number>();
|
|
234
234
|
for (let i = 0; i < dataSize; i++) {
|
|
235
235
|
queue.enqueue(i);
|
|
236
236
|
}
|
|
237
237
|
for (let i = 0; i < dataSize; i++) {
|
|
238
238
|
queue.dequeue();
|
|
239
239
|
}
|
|
240
|
-
console.log(`
|
|
240
|
+
console.log(`SkipQueue Performance Test: ${performance.now() - startTime} ms`);
|
|
241
241
|
expect(performance.now() - startTime).toBeLessThan(bigO.LINEAR * 100);
|
|
242
242
|
});
|
|
243
243
|
});
|
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
|
|