data-structure-typed 1.33.9 → 1.33.10
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/package.json +1 -1
- 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 +260 -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-map.ts +203 -0
- package/src/data-structures/hash/hash-table.ts +277 -0
- package/src/data-structures/hash/index.ts +7 -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 +166 -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/hash.ts +1 -0
- package/src/types/data-structures/heap.ts +5 -0
- package/src/types/data-structures/index.ts +16 -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 +108 -0
- package/test/integration/bst.test.ts +380 -0
- package/test/integration/heap.test.js +16 -0
- package/test/integration/index.html +52 -0
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +108 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +142 -0
- package/test/unit/data-structures/binary-tree/bst.test.ts +380 -0
- package/test/unit/data-structures/binary-tree/overall.test.ts +65 -0
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +43 -0
- package/test/unit/data-structures/binary-tree/segment-tree.test.ts +50 -0
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +461 -0
- package/test/unit/data-structures/graph/abstract-graph.test.ts +5 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +519 -0
- package/test/unit/data-structures/graph/index.ts +2 -0
- package/test/unit/data-structures/graph/map-graph.test.ts +45 -0
- package/test/unit/data-structures/graph/overall.test.ts +49 -0
- package/test/unit/data-structures/graph/undirected-graph.test.ts +59 -0
- 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-map.test.ts +104 -0
- package/test/unit/data-structures/hash/hash-table.test.ts +184 -0
- package/test/unit/data-structures/heap/heap.test.ts +55 -0
- package/test/unit/data-structures/heap/max-heap.test.ts +44 -0
- package/test/unit/data-structures/heap/min-heap.test.ts +82 -0
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +364 -0
- package/test/unit/data-structures/linked-list/index.ts +4 -0
- package/test/unit/data-structures/linked-list/linked-list.test.ts +35 -0
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +451 -0
- package/test/unit/data-structures/linked-list/skip-linked-list.test.ts +13 -0
- package/test/unit/data-structures/linked-list/skip-list.test.ts +55 -0
- 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 +106 -0
- package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +105 -0
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +27 -0
- package/test/unit/data-structures/queue/deque.test.ts +130 -0
- package/test/unit/data-structures/queue/queue.test.ts +199 -0
- 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/index.ts +2 -0
- package/test/utils/magnitude.ts +21 -0
- package/test/utils/number.ts +3 -0
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
import {DoublyLinkedList} from '../../../../src';
|
|
2
|
+
import {bigO, magnitude} from '../../../utils';
|
|
3
|
+
|
|
4
|
+
describe('DoublyLinkedList Operation Test', () => {
|
|
5
|
+
let list: DoublyLinkedList<number>;
|
|
6
|
+
let objectList: DoublyLinkedList<{keyA: number}>;
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
list = new DoublyLinkedList();
|
|
10
|
+
objectList = new DoublyLinkedList();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('should initialize an empty list', () => {
|
|
14
|
+
expect(list.length).toBe(0);
|
|
15
|
+
expect(list.head).toBeNull();
|
|
16
|
+
expect(list.tail).toBeNull();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('should push elements to the list', () => {
|
|
20
|
+
list.push(1);
|
|
21
|
+
list.push(2);
|
|
22
|
+
list.push(3);
|
|
23
|
+
expect(list.length).toBe(3);
|
|
24
|
+
expect(list.head!.val).toBe(1);
|
|
25
|
+
expect(list.tail!.val).toBe(3);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('should pop elements from the end of the list', () => {
|
|
29
|
+
list.push(1);
|
|
30
|
+
list.push(2);
|
|
31
|
+
const poppedValue = list.pop();
|
|
32
|
+
expect(poppedValue).toBe(2);
|
|
33
|
+
expect(list.length).toBe(1);
|
|
34
|
+
expect(list.head!.val).toBe(1);
|
|
35
|
+
expect(list.tail!.val).toBe(1);
|
|
36
|
+
});
|
|
37
|
+
it('should insert elements at specific positions', () => {
|
|
38
|
+
list.push(1);
|
|
39
|
+
list.push(2);
|
|
40
|
+
list.push(3);
|
|
41
|
+
|
|
42
|
+
// Inserting at the beginning
|
|
43
|
+
list.insertAt(0, 0);
|
|
44
|
+
expect(list.length).toBe(4);
|
|
45
|
+
expect(list.getAt(0)).toBe(0);
|
|
46
|
+
expect(list.getAt(1)).toBe(1);
|
|
47
|
+
|
|
48
|
+
// Inserting in the middle
|
|
49
|
+
list.insertAt(2, 1.5);
|
|
50
|
+
expect(list.length).toBe(5);
|
|
51
|
+
expect(list.getAt(2)).toBe(1.5);
|
|
52
|
+
expect(list.getAt(3)).toBe(2);
|
|
53
|
+
|
|
54
|
+
// Inserting at the end
|
|
55
|
+
list.insertAt(5, 4);
|
|
56
|
+
expect(list.length).toBe(6);
|
|
57
|
+
expect(list.getAt(5)).toBe(4);
|
|
58
|
+
expect(list.tail!.val).toBe(4);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('should delete elements at specific positions', () => {
|
|
62
|
+
list.push(1);
|
|
63
|
+
list.push(2);
|
|
64
|
+
list.push(3);
|
|
65
|
+
|
|
66
|
+
// Deleting from the beginning
|
|
67
|
+
const deletedValue = list.deleteAt(0);
|
|
68
|
+
expect(deletedValue).toBe(1);
|
|
69
|
+
expect(list.length).toBe(2);
|
|
70
|
+
expect(list.head!.val).toBe(2);
|
|
71
|
+
|
|
72
|
+
// Deleting from the middle
|
|
73
|
+
list.deleteAt(0); // Deleting the second element
|
|
74
|
+
expect(list.length).toBe(1);
|
|
75
|
+
expect(list.head!.val).toBe(3);
|
|
76
|
+
|
|
77
|
+
// Deleting from the end
|
|
78
|
+
list.deleteAt(0);
|
|
79
|
+
expect(list.length).toBe(0);
|
|
80
|
+
expect(list.head).toBeNull();
|
|
81
|
+
expect(list.tail).toBeNull();
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('should delete elements by value', () => {
|
|
85
|
+
list.push(1);
|
|
86
|
+
list.push(2);
|
|
87
|
+
list.push(3);
|
|
88
|
+
|
|
89
|
+
list.delete(2);
|
|
90
|
+
expect(list.length).toBe(2);
|
|
91
|
+
expect(list.head!.val).toBe(1);
|
|
92
|
+
expect(list.tail!.val).toBe(3);
|
|
93
|
+
|
|
94
|
+
list.delete(1);
|
|
95
|
+
expect(list.length).toBe(1);
|
|
96
|
+
expect(list.head!.val).toBe(3);
|
|
97
|
+
|
|
98
|
+
list.delete(3);
|
|
99
|
+
expect(list.length).toBe(0);
|
|
100
|
+
expect(list.head).toBeNull();
|
|
101
|
+
expect(list.tail).toBeNull();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('should reverse the linked list', () => {
|
|
105
|
+
list.push(1);
|
|
106
|
+
list.push(2);
|
|
107
|
+
list.push(3);
|
|
108
|
+
|
|
109
|
+
list.reverse();
|
|
110
|
+
|
|
111
|
+
expect(list.toArray()).toEqual([3, 2, 1]);
|
|
112
|
+
expect(list.toArrayReverse()).toEqual([1, 2, 3]);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('should map elements using a callback function', () => {
|
|
116
|
+
list.push(1);
|
|
117
|
+
list.push(2);
|
|
118
|
+
list.push(3);
|
|
119
|
+
|
|
120
|
+
const mappedList = list.map(val => val * 2);
|
|
121
|
+
|
|
122
|
+
expect(mappedList.toArray()).toEqual([2, 4, 6]);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('should filter elements using a callback function', () => {
|
|
126
|
+
list.push(1);
|
|
127
|
+
list.push(2);
|
|
128
|
+
list.push(3);
|
|
129
|
+
list.push(4);
|
|
130
|
+
|
|
131
|
+
const filteredList = list.filter(val => val % 2 === 0);
|
|
132
|
+
|
|
133
|
+
expect(filteredList.toArray()).toEqual([2, 4]);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('should reduce elements using a callback function and an initial value', () => {
|
|
137
|
+
list.push(1);
|
|
138
|
+
list.push(2);
|
|
139
|
+
list.push(3);
|
|
140
|
+
list.push(4);
|
|
141
|
+
|
|
142
|
+
const sum = list.reduce((acc, val) => acc + val, 0);
|
|
143
|
+
|
|
144
|
+
expect(sum).toBe(10);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('should insert an element after a specific value', () => {
|
|
148
|
+
list.push(1);
|
|
149
|
+
list.push(2);
|
|
150
|
+
list.push(3);
|
|
151
|
+
|
|
152
|
+
list.insertAfter(2, 2.5);
|
|
153
|
+
|
|
154
|
+
expect(list.toArray()).toEqual([1, 2, 2.5, 3]);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('should insert an element before a specific value', () => {
|
|
158
|
+
list.push(1);
|
|
159
|
+
list.push(2);
|
|
160
|
+
list.push(3);
|
|
161
|
+
|
|
162
|
+
list.insertBefore(2, 1.5);
|
|
163
|
+
|
|
164
|
+
expect(list.toArray()).toEqual([1, 1.5, 2, 3]);
|
|
165
|
+
});
|
|
166
|
+
it('should find the first element that satisfies a condition', () => {
|
|
167
|
+
list.push(1);
|
|
168
|
+
list.push(2);
|
|
169
|
+
list.push(3);
|
|
170
|
+
|
|
171
|
+
const found = list.find(val => val % 2 === 0);
|
|
172
|
+
|
|
173
|
+
expect(found).toBe(2);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('should find the index of an element', () => {
|
|
177
|
+
list.push(1);
|
|
178
|
+
list.push(2);
|
|
179
|
+
list.push(3);
|
|
180
|
+
|
|
181
|
+
const index = list.indexOf(2);
|
|
182
|
+
|
|
183
|
+
expect(index).toBe(1);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it('should find the last element that satisfies a condition', () => {
|
|
187
|
+
list.push(1);
|
|
188
|
+
list.push(2);
|
|
189
|
+
list.push(3);
|
|
190
|
+
list.push(4);
|
|
191
|
+
|
|
192
|
+
const lastEven = list.findLast(val => val % 2 === 0);
|
|
193
|
+
|
|
194
|
+
expect(lastEven).toBe(4);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('should clear the linked list', () => {
|
|
198
|
+
list.push(1);
|
|
199
|
+
list.push(2);
|
|
200
|
+
list.push(3);
|
|
201
|
+
|
|
202
|
+
list.clear();
|
|
203
|
+
|
|
204
|
+
expect(list.length).toBe(0);
|
|
205
|
+
expect(list.head).toBe(null);
|
|
206
|
+
expect(list.tail).toBe(null);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('should create a reversed array of values', () => {
|
|
210
|
+
list.push(1);
|
|
211
|
+
list.push(2);
|
|
212
|
+
list.push(3);
|
|
213
|
+
|
|
214
|
+
const reversedArray = list.toArrayReverse();
|
|
215
|
+
|
|
216
|
+
expect(reversedArray).toEqual([3, 2, 1]);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('should reverse the linked list', () => {
|
|
220
|
+
list.push(1);
|
|
221
|
+
list.push(2);
|
|
222
|
+
list.push(3);
|
|
223
|
+
|
|
224
|
+
list.reverse();
|
|
225
|
+
|
|
226
|
+
expect(list.toArray()).toEqual([3, 2, 1]);
|
|
227
|
+
expect(list.head?.val).toBe(3);
|
|
228
|
+
expect(list.tail?.val).toBe(1);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it('should iterate over each element and apply a callback', () => {
|
|
232
|
+
list.push(1);
|
|
233
|
+
list.push(2);
|
|
234
|
+
list.push(3);
|
|
235
|
+
|
|
236
|
+
const result: number[] = [];
|
|
237
|
+
list.forEach(val => {
|
|
238
|
+
result.push(val * 2);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
expect(result).toEqual([2, 4, 6]);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('should create a new linked list by applying a mapping function', () => {
|
|
245
|
+
list.push(1);
|
|
246
|
+
list.push(2);
|
|
247
|
+
list.push(3);
|
|
248
|
+
|
|
249
|
+
const mappedList = list.map(val => val * 2);
|
|
250
|
+
|
|
251
|
+
expect(mappedList.toArray()).toEqual([2, 4, 6]);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
it('should create a new linked list by filtering elements', () => {
|
|
255
|
+
list.push(1);
|
|
256
|
+
list.push(2);
|
|
257
|
+
list.push(3);
|
|
258
|
+
list.push(4);
|
|
259
|
+
|
|
260
|
+
const filteredList = list.filter(val => val % 2 === 0);
|
|
261
|
+
|
|
262
|
+
expect(filteredList.toArray()).toEqual([2, 4]);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it('should reduce the linked list to a single value', () => {
|
|
266
|
+
list.push(1);
|
|
267
|
+
list.push(2);
|
|
268
|
+
list.push(3);
|
|
269
|
+
|
|
270
|
+
const sum = list.reduce((acc, val) => acc + val, 0);
|
|
271
|
+
|
|
272
|
+
expect(sum).toBe(6);
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it('should insert a new value after an existing value', () => {
|
|
276
|
+
list.push(1);
|
|
277
|
+
list.push(2);
|
|
278
|
+
list.push(3);
|
|
279
|
+
|
|
280
|
+
const success = list.insertAfter(2, 4);
|
|
281
|
+
expect(success).toBe(true);
|
|
282
|
+
expect(list.toArray()).toEqual([1, 2, 4, 3]);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it('should insert a new value before an existing value', () => {
|
|
286
|
+
list.push(1);
|
|
287
|
+
list.push(2);
|
|
288
|
+
list.push(3);
|
|
289
|
+
|
|
290
|
+
const success = list.insertBefore(2, 0);
|
|
291
|
+
expect(success).toBe(true);
|
|
292
|
+
expect(list.toArray()).toEqual([1, 0, 2, 3]);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it('should not insert a new value after a non-existing value', () => {
|
|
296
|
+
list.push(1);
|
|
297
|
+
list.push(2);
|
|
298
|
+
list.push(3);
|
|
299
|
+
|
|
300
|
+
const success = list.insertAfter(4, 5);
|
|
301
|
+
expect(success).toBe(false);
|
|
302
|
+
expect(list.toArray()).toEqual([1, 2, 3]);
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it('should not insert a new value before a non-existing value', () => {
|
|
306
|
+
list.push(1);
|
|
307
|
+
list.push(2);
|
|
308
|
+
list.push(3);
|
|
309
|
+
|
|
310
|
+
const success = list.insertBefore(4, 0);
|
|
311
|
+
expect(success).toBe(false);
|
|
312
|
+
expect(list.toArray()).toEqual([1, 2, 3]);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it('should insert and manipulate objects with numeric properties', () => {
|
|
316
|
+
const obj1 = {keyA: 10};
|
|
317
|
+
const obj2 = {keyA: 20};
|
|
318
|
+
const obj3 = {keyA: 30};
|
|
319
|
+
|
|
320
|
+
objectList.push(obj1);
|
|
321
|
+
objectList.push(obj2);
|
|
322
|
+
objectList.push(obj3);
|
|
323
|
+
|
|
324
|
+
expect(objectList.toArray()).toEqual([obj1, obj2, obj3]);
|
|
325
|
+
|
|
326
|
+
const newObj = {keyA: 25}; // Corrected newObj value
|
|
327
|
+
const insertSuccess = objectList.insertBefore(obj2, newObj);
|
|
328
|
+
expect(insertSuccess).toBe(true);
|
|
329
|
+
|
|
330
|
+
const findNode = objectList.findNode(newObj); // Use newObj instead of obj2
|
|
331
|
+
expect(findNode?.val).toEqual(newObj);
|
|
332
|
+
|
|
333
|
+
const deleted = objectList.delete(newObj); // Use newObj instead of obj2
|
|
334
|
+
expect(deleted).toBe(true);
|
|
335
|
+
|
|
336
|
+
const poppedObj = objectList.pop();
|
|
337
|
+
expect(poppedObj).toBe(obj3);
|
|
338
|
+
|
|
339
|
+
const shiftedObj = objectList.shift();
|
|
340
|
+
expect(shiftedObj).toBe(obj1);
|
|
341
|
+
});
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
describe('DoublyLinkedList Performance Test', () => {
|
|
345
|
+
it('should the push and pop methods adhere to a time complexity of O(n) and executed correctly under large scale data', () => {
|
|
346
|
+
const list = new DoublyLinkedList<number>();
|
|
347
|
+
|
|
348
|
+
const startPushTime = performance.now();
|
|
349
|
+
for (let i = 0; i < magnitude.LINEAR; i++) {
|
|
350
|
+
list.unshift(i);
|
|
351
|
+
}
|
|
352
|
+
expect(performance.now() - startPushTime).toBeLessThan(bigO.LINEAR * 10);
|
|
353
|
+
|
|
354
|
+
expect(list.length).toBeGreaterThan(0);
|
|
355
|
+
const startPopTime = performance.now();
|
|
356
|
+
for (let i = 0; i < magnitude.LINEAR; i++) {
|
|
357
|
+
list.shift();
|
|
358
|
+
}
|
|
359
|
+
expect(performance.now() - startPopTime).toBeLessThan(bigO.LINEAR * 100);
|
|
360
|
+
|
|
361
|
+
expect(list.pop()).toBeUndefined();
|
|
362
|
+
expect(list.length).toBe(0);
|
|
363
|
+
});
|
|
364
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {DoublyLinkedList, DoublyLinkedListNode, SinglyLinkedList, SinglyLinkedListNode} from '../../../../src';
|
|
2
|
+
import {bigO, magnitude} from '../../../utils';
|
|
3
|
+
|
|
4
|
+
describe('LinkedList Performance Test', () => {
|
|
5
|
+
it('should DoublyLinkedList insertBefore faster than SinglyLinkedList', () => {
|
|
6
|
+
const doublyList = new DoublyLinkedList<number>();
|
|
7
|
+
|
|
8
|
+
const startPushTime = performance.now();
|
|
9
|
+
let midNode: DoublyLinkedListNode | null = null;
|
|
10
|
+
const midIndex = Math.floor(magnitude.SQUARED / 2);
|
|
11
|
+
for (let i = 0; i < magnitude.SQUARED; i++) {
|
|
12
|
+
doublyList.push(i);
|
|
13
|
+
if (i === midIndex) {
|
|
14
|
+
midNode = doublyList.findNode(i);
|
|
15
|
+
} else if (i > midIndex && midNode) {
|
|
16
|
+
doublyList.insertBefore(midNode, i);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const doublyListPushCost = performance.now() - startPushTime;
|
|
20
|
+
|
|
21
|
+
const singlyList = new SinglyLinkedList<number>();
|
|
22
|
+
let midSinglyNode: SinglyLinkedListNode | null = null;
|
|
23
|
+
|
|
24
|
+
for (let i = 0; i < magnitude.SQUARED; i++) {
|
|
25
|
+
singlyList.push(i);
|
|
26
|
+
if (i === midIndex) {
|
|
27
|
+
midSinglyNode = singlyList.findNode(i);
|
|
28
|
+
} else if (i > midIndex && midSinglyNode) {
|
|
29
|
+
singlyList.insertBefore(midSinglyNode.val, i);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
expect(doublyListPushCost).toBeLessThan(bigO.SQUARED * 5);
|
|
34
|
+
});
|
|
35
|
+
});
|