@raikuxq/alg-ds 1.0.2 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/.idea/algorythmes.iml +15 -0
  2. package/.idea/codeStyles/codeStyleConfig.xml +5 -0
  3. package/.idea/deployment.xml +14 -0
  4. package/.idea/inspectionProfiles/Project_Default.xml +7 -0
  5. package/.idea/jsLinters/eslint.xml +6 -0
  6. package/.idea/misc.xml +6 -0
  7. package/.idea/modules.xml +8 -0
  8. package/.idea/vcs.xml +6 -0
  9. package/README.md +12 -0
  10. package/lib/algotirhms.ts +35 -0
  11. package/lib/constants.ts +3 -0
  12. package/lib/data-structures.ts +23 -0
  13. package/lib/helpers.ts +13 -0
  14. package/lib/sorts.ts +7 -0
  15. package/lib/types.ts +53 -0
  16. package/{src/exports → lib}/utils.ts +2 -2
  17. package/package.json +4 -3
  18. package/src/data-structures/HashTable/HashTable.ts +202 -0
  19. package/src/data-structures/HashTable/HashTableNode.ts +31 -0
  20. package/src/demo/demo.hashtable.ts +28 -0
  21. package/src/demo/performance/bst-compare.ts +1 -4
  22. package/src/demo/performance/hash-table.compare.ts +40 -0
  23. package/src/index.ts +44 -40
  24. package/src/types/IKeyValueStorage.ts +8 -0
  25. package/.eslintrc.js +0 -14
  26. package/jest.config.js +0 -4
  27. package/nodemon.json +0 -6
  28. package/src/exports/algotirhms.ts +0 -35
  29. package/src/exports/constants.ts +0 -3
  30. package/src/exports/helpers.ts +0 -13
  31. package/src/exports/main.ts +0 -21
  32. package/src/exports/sorts.ts +0 -7
  33. package/src/exports/types.ts +0 -53
  34. package/test/unit/algorithms/binary-search.test.ts +0 -25
  35. package/test/unit/algorithms/factorial.test.ts +0 -43
  36. package/test/unit/algorithms/fibonacci.test.ts +0 -41
  37. package/test/unit/algorithms/sorts.test.ts +0 -74
  38. package/test/unit/algorithms/transpose-matrix.test.ts +0 -39
  39. package/test/unit/data-structures/binary-tree/binary-search-tree.test.ts +0 -230
  40. package/test/unit/data-structures/graph/graph.create-from-matrix.test.ts +0 -106
  41. package/test/unit/data-structures/graph/graph.has-path.test.ts +0 -115
  42. package/test/unit/data-structures/graph/graph.presenter.lists.test.ts +0 -76
  43. package/test/unit/data-structures/graph/graph.presenter.matrix.test.ts +0 -73
  44. package/test/unit/data-structures/graph/graph.shortest-path.test.ts +0 -207
  45. package/test/unit/data-structures/graph/graph.test.ts +0 -394
  46. package/test/unit/data-structures/graph/graph.transpose.test.ts +0 -52
  47. package/test/unit/data-structures/linked-list/linked-list.test.ts +0 -477
  48. package/test/unit/data-structures/looped-array/looped-array.test.ts +0 -387
  49. package/test/unit/data-structures/queue/queue.test.ts +0 -147
  50. package/test/unit/data-structures/stack/stack.test.ts +0 -155
  51. package/tsconfig.json +0 -18
@@ -1,477 +0,0 @@
1
- import SingleLinkedList from "../../../../src/data-structures/LinkedList/SingleLinkedList/SingleLinkedList";
2
- import DoubleLinkedList from "../../../../src/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedList";
3
- import { EnumLinkedListType } from "../../../../src/types/EnumLinkedListType";
4
- import { createLinkedList } from "../../../../src/helpers/createLinkedList";
5
- import ILinearStorage from "../../../../src/types/ILinearStorage";
6
-
7
- describe.each([EnumLinkedListType.SINGLE, EnumLinkedListType.DOUBLE])(
8
- "%s linked list",
9
- (listType: EnumLinkedListType) => {
10
- describe("constructor", () => {
11
- it("should throw when capacity is less than 1", () => {
12
- expect(() => {
13
- createLinkedList<number>(listType, -5);
14
- }).toThrowError();
15
- });
16
- });
17
-
18
- describe("method push", () => {
19
- it("should add elements to list's end", () => {
20
- const list = createLinkedList<number>(listType);
21
- list.push(1);
22
-
23
- expect(list.peek()).toBe(1);
24
- });
25
- it("should throw when list is full", () => {
26
- const list = createLinkedList<number>(listType, 2);
27
- list.push(1);
28
- list.push(2);
29
-
30
- expect(() => {
31
- list.push(3);
32
- }).toThrowError();
33
- });
34
- });
35
-
36
- describe("method pushFromIndex", () => {
37
- it("should add elements to list from index", () => {
38
- const list = createLinkedList<number>(listType);
39
- list.pushFromArray([10, 30, 40, 50]);
40
- list.pushFromIndex(20, 1);
41
-
42
- expect(list.getAsArray()).toEqual([10, 20, 30, 40, 50]);
43
- });
44
-
45
- it("should add elements to list from start", () => {
46
- const list = createLinkedList<number>(listType);
47
- list.pushFromArray([0]);
48
- list.pushFromIndex(10, 1);
49
-
50
- expect(list.getAsArray()).toEqual([0, 10]);
51
- });
52
-
53
- it("should add elements to empty list", () => {
54
- const list = createLinkedList<number>(listType);
55
- list.pushFromIndex(10, 0);
56
-
57
- expect(list.getAsArray()).toEqual([10]);
58
- });
59
-
60
- it("should add elements to list from end", () => {
61
- const list = createLinkedList<number>(listType);
62
- list.pushFromArray([0, 10, 30]);
63
- list.pushFromIndex(20, 2);
64
-
65
- expect(list.getAsArray()).toEqual([0, 10, 20, 30]);
66
- });
67
-
68
- it("should throw when index exceeds list length ", () => {
69
- const list = createLinkedList<number>(listType);
70
- list.pushFromArray([0, 10, 30]);
71
-
72
- expect(() => {
73
- list.pushFromIndex(10, 1000);
74
- }).toThrowError();
75
- });
76
-
77
- it("should throw when index less than 0 ", () => {
78
- const list = createLinkedList<number>(listType);
79
- list.pushFromArray([0, 10, 30]);
80
-
81
- expect(() => {
82
- list.pushFromIndex(10, -20);
83
- }).toThrowError();
84
- });
85
- });
86
-
87
- describe("method unshift", () => {
88
- it("should add elements to list's start", () => {
89
- const list = createLinkedList<number>(listType);
90
- list.unshift(1);
91
- list.unshift(0);
92
-
93
- expect(list.peekFromStart()).toBe(0);
94
- });
95
-
96
- it("should throw when list is full", () => {
97
- const list = createLinkedList<number>(listType, 2);
98
- list.unshift(1);
99
- list.unshift(2);
100
-
101
- expect(() => {
102
- list.unshift(3);
103
- }).toThrowError();
104
- });
105
- });
106
-
107
- describe("method pushFromArray", () => {
108
- it("should add elements to list's end", () => {
109
- const list = createLinkedList<number>(listType);
110
- const arr = [1, 2, 3, 4, 5];
111
- list.pushFromArray(arr);
112
-
113
- expect(list.getAsArray()).toEqual(arr);
114
- });
115
-
116
- it("should throw when list is full", () => {
117
- const list = createLinkedList<number>(listType, 2);
118
- list.pushFromArray([1]);
119
-
120
- expect(() => {
121
- list.pushFromArray([2, 3]);
122
- }).toThrowError();
123
- });
124
- });
125
-
126
- describe("method peekFromStart", () => {
127
- it("should throw when list is empty", () => {
128
- const emptyList = createLinkedList<number>(listType);
129
-
130
- expect(() => {
131
- emptyList.peekFromStart();
132
- }).toThrowError();
133
- });
134
-
135
- it("should return first element from list", () => {
136
- const list = new DoubleLinkedList();
137
- list.pushFromArray([10, 20, 30, 40, 50]);
138
-
139
- expect(list.peekFromStart()).toBe(10);
140
- });
141
- });
142
-
143
- describe("method peek", () => {
144
- it("should throw when list is empty", () => {
145
- const emptyList = createLinkedList<number>(listType);
146
-
147
- expect(() => {
148
- emptyList.peek();
149
- }).toThrowError();
150
- });
151
-
152
- it("should return first element from list", () => {
153
- const list = createLinkedList<number>(listType);
154
- list.pushFromArray([10, 20, 30, 40, 50]);
155
-
156
- expect(list.peek()).toBe(50);
157
- });
158
- });
159
-
160
- describe("method peekByIndex", () => {
161
- it("should throw when list is empty", () => {
162
- const emptyList = createLinkedList<number>(listType);
163
-
164
- expect(() => {
165
- emptyList.peekByIndex(0);
166
- }).toThrowError();
167
- });
168
-
169
- it("should return element by its index from list", () => {
170
- const list = createLinkedList<number>(listType);
171
- list.pushFromArray([10, 20, 30, 40, 50]);
172
-
173
- expect(list.peekByIndex(2)).toBe(30);
174
- });
175
-
176
- it("should throw when index exceed list length", () => {
177
- const list = createLinkedList<number>(listType);
178
-
179
- expect(() => {
180
- list.peekByIndex(1000);
181
- }).toThrowError();
182
- });
183
- });
184
-
185
- describe("method shift", () => {
186
- describe("should delete first element and return its value", () => {
187
- const list = createLinkedList<number>(listType);
188
- list.pushFromArray([10, 20]);
189
- const shifted = list.shift();
190
-
191
- it("should delete correctly", () => {
192
- expect(list.getAsArray()).toEqual([20]);
193
- });
194
-
195
- it("should return correct value", () => {
196
- expect(shifted).toBe(10);
197
- });
198
- });
199
-
200
- it("should throw when list is empty", () => {
201
- const emptyList = createLinkedList<number>(listType);
202
-
203
- expect(() => {
204
- emptyList.shift();
205
- }).toThrowError();
206
- });
207
- });
208
-
209
- describe("method pop", () => {
210
- describe("should delete last element and return its value", () => {
211
- const list = createLinkedList<number>(listType);
212
- list.pushFromArray([10, 40, 50]);
213
- const shifted = list.pop();
214
-
215
- it("should delete correctly", () => {
216
- expect(list.getAsArray()).toEqual([10, 40]);
217
- });
218
-
219
- it("should return correct value", () => {
220
- expect(shifted).toBe(50);
221
- });
222
- });
223
-
224
- it("should throw when list is empty", () => {
225
- const emptyList = createLinkedList<number>(listType);
226
-
227
- expect(() => {
228
- emptyList.pop();
229
- }).toThrowError();
230
- });
231
- });
232
-
233
- describe("method deleteFromIndex", () => {
234
- describe("should delete element by index and return its value", () => {
235
- const list = createLinkedList<number>(listType);
236
- list.pushFromArray([10, 20, 30]);
237
- const shifted = list.deleteFromIndex(1);
238
-
239
- it("should delete correctly", () => {
240
- expect(list.getAsArray()).toEqual([10, 30]);
241
- });
242
-
243
- it("should return correct value", () => {
244
- expect(shifted).toBe(20);
245
- });
246
- });
247
-
248
- it("should throw when list is empty", () => {
249
- const emptyList = createLinkedList<number>(listType);
250
-
251
- expect(() => {
252
- emptyList.shift();
253
- }).toThrowError();
254
- });
255
- });
256
-
257
- describe("method reverse", () => {
258
- it("should correctly reverse list", () => {
259
- const list = createLinkedList<number>(listType);
260
- const array = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110];
261
- const reversedArray = [...array].reverse();
262
- list.pushFromArray(array);
263
- list.reverse();
264
-
265
- expect(list.getAsArray()).toEqual(reversedArray);
266
- });
267
- });
268
-
269
- describe("method clear", () => {
270
- it("should correctly clear list", () => {
271
- const list = createLinkedList<number>(listType);
272
- const testArray: Array<number> = [10, 20, 30, 40, 50, 60, 70, 80, 90];
273
- list.pushFromArray(testArray);
274
- list.clear();
275
-
276
- expect(list.isEmpty()).toBe(true);
277
- });
278
- });
279
-
280
- describe("method has", function () {
281
- const list = createLinkedList<number>(listType);
282
- list.push(5);
283
-
284
- it("should return true when value exists", () => {
285
- expect(list.has(5)).toBe(true);
286
- });
287
-
288
- it("should return false when value does not exist", () => {
289
- expect(list.has(10)).toBe(false);
290
- });
291
- });
292
-
293
- describe("method length", function () {
294
- describe("when list is non empty", () => {
295
- describe("after adding", () => {
296
- it("should return updated length value", () => {
297
- const list = createLinkedList<number>(listType);
298
- list.push(5);
299
- list.push(15);
300
- list.push(10);
301
-
302
- expect(list.length()).toBe(3);
303
- });
304
- });
305
-
306
- describe("after deleting", () => {
307
- it("should return updated length value", () => {
308
- const list = createLinkedList<number>(listType);
309
- list.push(5);
310
- list.push(15);
311
- list.push(10);
312
- list.pop();
313
-
314
- expect(list.length()).toBe(2);
315
- });
316
- });
317
- });
318
-
319
- describe("when list is empty", () => {
320
- it("should return zero value", () => {
321
- const list = createLinkedList<number>(listType);
322
-
323
- expect(list.length()).toBe(0);
324
- });
325
- });
326
- });
327
-
328
- describe("method isEmpty", () => {
329
- it("should return true when list is empty", () => {
330
- const list: ILinearStorage<number> = createLinkedList<number>(listType);
331
- expect(list.isEmpty()).toBe(true);
332
- });
333
- });
334
-
335
- describe("method isFull", () => {
336
- it("should return false when list elements length lower than its capacity", () => {
337
- const list: ILinearStorage<number> = createLinkedList<number>(
338
- listType,
339
- 100
340
- );
341
- list.push(10);
342
-
343
- expect(list.isFull()).toBe(false);
344
- });
345
- it("should return true when list elements length same as its capacity", () => {
346
- const list: ILinearStorage<number> = createLinkedList<number>(
347
- listType,
348
- 1
349
- );
350
- list.push(10);
351
-
352
- expect(list.isFull()).toBe(true);
353
- });
354
- });
355
-
356
- describe("method iterator", () => {
357
- describe("constructor", () => {
358
- it("should throw when list is empty", () => {
359
- const linkedList = createLinkedList<number>(listType) as
360
- | SingleLinkedList<number>
361
- | DoubleLinkedList<number>;
362
-
363
- expect(() => {
364
- linkedList.iterator(0);
365
- }).toThrowError();
366
- });
367
- });
368
-
369
- describe("method hasNext", () => {
370
- it("should return false when there is no next element available", () => {
371
- const linkedList = createLinkedList<number>(listType) as
372
- | SingleLinkedList<number>
373
- | DoubleLinkedList<number>;
374
- linkedList.pushFromArray([10, 20, 30]);
375
- const iterator = linkedList.iterator(0);
376
- iterator.next();
377
- iterator.next();
378
-
379
- expect(iterator.hasNext()).toBe(false);
380
- });
381
-
382
- it("should return true when next element is available", () => {
383
- const linkedList = createLinkedList<number>(listType) as
384
- | SingleLinkedList<number>
385
- | DoubleLinkedList<number>;
386
- linkedList.pushFromArray([10, 20, 30]);
387
- const iterator = linkedList.iterator(0);
388
-
389
- expect(iterator.hasNext()).toBe(true);
390
- });
391
- });
392
-
393
- describe("method next", () => {
394
- it("should iterate to next", () => {
395
- const linkedList = createLinkedList<number>(listType) as
396
- | SingleLinkedList<number>
397
- | DoubleLinkedList<number>;
398
- linkedList.pushFromArray([10, 20, 30]);
399
- const iterator = linkedList.iterator(0);
400
-
401
- expect(iterator.next()).toBe(20);
402
- });
403
-
404
- it("should throw when next element is not available", () => {
405
- const linkedList = createLinkedList<number>(listType) as
406
- | SingleLinkedList<number>
407
- | DoubleLinkedList<number>;
408
- linkedList.pushFromArray([10, 20, 30]);
409
- const iterator = linkedList.iterator(0);
410
- iterator.next();
411
- iterator.next();
412
-
413
- expect(() => {
414
- iterator.next();
415
- }).toThrowError();
416
- });
417
- });
418
-
419
- describe("method current", () => {
420
- it("should return current value", () => {
421
- const linkedList = createLinkedList<number>(listType) as
422
- | SingleLinkedList<number>
423
- | DoubleLinkedList<number>;
424
- linkedList.pushFromArray([10, 20, 30]);
425
- const iterator = linkedList.iterator(0);
426
-
427
- expect(iterator.current()).toBe(10);
428
- });
429
- });
430
-
431
- if (listType === EnumLinkedListType.DOUBLE) {
432
- describe("method hasPrev", () => {
433
- it("should return false when there is no prev element available", () => {
434
- const list = new DoubleLinkedList<number>();
435
- list.pushFromArray([10, 20, 30]);
436
- const iterator = list.iterator(0);
437
-
438
- expect(iterator.hasPrev()).toBe(false);
439
- });
440
-
441
- it("should return true when prev element is available", () => {
442
- const list = new DoubleLinkedList<number>();
443
- list.pushFromArray([10, 20, 30]);
444
- const iterator = list.iterator(0);
445
- iterator.next();
446
- iterator.next();
447
-
448
- expect(iterator.hasPrev()).toBe(true);
449
- });
450
- });
451
-
452
- describe("method prev", () => {
453
- const list = new DoubleLinkedList<number>();
454
- list.pushFromArray([10, 20, 30]);
455
- const iterator = list.iterator(0);
456
- iterator.next();
457
-
458
- it("should iterate to prev", () => {
459
- expect(iterator.prev()).toBe(10);
460
- });
461
-
462
- it("should throw when prev element is not available", () => {
463
- const list = new DoubleLinkedList<number>();
464
- list.pushFromArray([10, 20]);
465
- const iterator = list.iterator(0);
466
- iterator.next();
467
- iterator.prev();
468
-
469
- expect(() => {
470
- iterator.prev();
471
- }).toThrowError();
472
- });
473
- });
474
- }
475
- });
476
- }
477
- );