@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
package/src/index.ts CHANGED
@@ -1,40 +1,44 @@
1
- /**
2
- * Write your code here
3
- */
4
- // import { demoBst, demoBstObjects } from "./demo/demo.bst";
5
- // import { bstCompare } from "./demo/performance/bst-compare";
6
- // import { demoStack } from "./demo/demo.stack";
7
- // import { perfQueue, perfStack } from "./demo/performance/ds-compare";
8
- // import { demoLinkedList } from "./demo/demo.linked-list";
9
- // import { demoQueue } from "./demo/demo.queue";
10
- // import {
11
- // demoDirectedGraph,
12
- // demoGraphGenerated,
13
- // demoUndirectedGraph,
14
- // } from "./demo/demo.graph";
15
- // import { demoLoopedArray } from "./demo/demo.looped-array";
16
- // import { compareAllSortTypes } from "./demo/performance/sort-compare";
17
- //
18
- // demoLinkedList();
19
- // // console.log("================================================================");
20
- // demoLoopedArray();
21
- // // console.log("================================================================");
22
- // demoQueue();
23
- // // console.log("================================================================");
24
- // demoStack();
25
- // // console.log("================================================================");
26
- // demoUndirectedGraph();
27
- // // console.log("================================================================");
28
- // demoDirectedGraph();
29
- // // console.log("================================================================");
30
- // demoGraphGenerated();
31
- // // console.log("================================================================");
32
- // compareAllSortTypes();
33
- // // console.log("===========================================================");
34
- // perfQueue();
35
- // // console.log("===========================================================");
36
- // perfStack();
37
- // // console.log("===========================================================");
38
- // demoBst();
39
- // // console.log("===========================================================");
40
- // bstCompare();
1
+ /**
2
+ * Write your code here
3
+ */
4
+
5
+ // import { demoHashtable } from "./demo/demo.hashtable";
6
+ // import { demoBst, demoBstObjects } from "./demo/demo.bst";
7
+ // import { bstCompare } from "./demo/performance/bst-compare";
8
+ // import { demoStack } from "./demo/demo.stack";
9
+ // import { perfQueue, perfStack } from "./demo/performance/ds-compare";
10
+ // import { demoLinkedList } from "./demo/demo.linked-list";
11
+ // import { demoQueue } from "./demo/demo.queue";
12
+ // import {
13
+ // demoDirectedGraph,
14
+ // demoGraphGenerated,
15
+ // demoUndirectedGraph,
16
+ // } from "./demo/demo.graph";
17
+ // import { demoLoopedArray } from "./demo/demo.looped-array";
18
+ // import { compareAllSortTypes } from "./demo/performance/sort-compare";
19
+ //
20
+ // demoLinkedList();
21
+ // // console.log("================================================================");
22
+ // demoLoopedArray();
23
+ // // console.log("================================================================");
24
+ // demoQueue();
25
+ // // console.log("================================================================");
26
+ // demoStack();
27
+ // // console.log("================================================================");
28
+ // demoUndirectedGraph();
29
+ // // console.log("================================================================");
30
+ // demoDirectedGraph();
31
+ // // console.log("================================================================");
32
+ // demoGraphGenerated();
33
+ // // console.log("================================================================");
34
+ // compareAllSortTypes();
35
+ // // console.log("===========================================================");
36
+ // perfQueue();
37
+ // // console.log("===========================================================");
38
+ // perfStack();
39
+ // // console.log("===========================================================");
40
+ // demoBst();
41
+ // // console.log("===========================================================");
42
+ // bstCompare();
43
+ // // console.log("===========================================================");
44
+ // demoHashtable();
@@ -0,0 +1,8 @@
1
+ export default interface IKeyValueStorage<T> {
2
+ set(key: string, value: T): void;
3
+ has(key: string): boolean;
4
+ get(key: string): T;
5
+ delete(key: string): void;
6
+ length(): number;
7
+ clear(): void;
8
+ }
package/.eslintrc.js DELETED
@@ -1,14 +0,0 @@
1
- module.exports = {
2
- root: true,
3
- parser: "@typescript-eslint/parser",
4
- plugins: ["@typescript-eslint", "prettier"],
5
- extends: [
6
- "eslint:recommended",
7
- "plugin:@typescript-eslint/recommended",
8
- "plugin:prettier/recommended",
9
- "prettier/@typescript-eslint",
10
- ],
11
- env: {
12
- node: true,
13
- },
14
- };
package/jest.config.js DELETED
@@ -1,4 +0,0 @@
1
- module.exports = {
2
- preset: "ts-jest",
3
- testEnvironment: "node",
4
- };
package/nodemon.json DELETED
@@ -1,6 +0,0 @@
1
- {
2
- "watch": ["src"],
3
- "ext": ".ts,.js",
4
- "ignore": [],
5
- "exec": "ts-node ./src/index.ts"
6
- }
@@ -1,35 +0,0 @@
1
- import { factorial, memoizedFactorial } from "../algorithms/factorial";
2
- import { fibonacci, memoizedFibonacci } from "../algorithms/fibonacci";
3
- import { binarySearch } from "../algorithms/binary-search";
4
- import { transposeMatrix } from "../algorithms/transpose-matrix";
5
- import { transposeDirectedGraph } from "../data-structures/Graph/transposing/transposeDirectedGraph";
6
- import BFSIterationStrategy from "../data-structures/Graph/strategy/BFSIterationStrategy";
7
- import DFSIterationStrategy from "../data-structures/Graph/strategy/DFSIterationStrategy";
8
- import DijkstraIterationStrategy from "../data-structures/Graph/strategy/DijkstraIterationStrategy";
9
- import GraphIteratorBFS from "../data-structures/Graph/iterator/GraphIteratorBFS";
10
- import GraphIteratorDFS from "../data-structures/Graph/iterator/GraphIteratorDFS";
11
- import GraphIteratorDijkstra from "../data-structures/Graph/iterator/GraphIteratorDijkstra";
12
- import { hasPath } from "../data-structures/Graph/searching/hasPath";
13
- import { shortestPath } from "../data-structures/Graph/searching/shortestPath";
14
- import { presenterAdjacencyMatrix } from "../data-structures/Graph/presenter/presenterAdjacencyMatrix";
15
- import { presenterAdjacencyLists } from "../data-structures/Graph/presenter/presenterAdjacencyLists";
16
-
17
- export {
18
- binarySearch,
19
- factorial,
20
- memoizedFactorial,
21
- memoizedFibonacci,
22
- fibonacci,
23
- transposeMatrix,
24
- GraphIteratorDFS,
25
- presenterAdjacencyLists,
26
- presenterAdjacencyMatrix,
27
- hasPath,
28
- shortestPath,
29
- DijkstraIterationStrategy,
30
- DFSIterationStrategy,
31
- BFSIterationStrategy,
32
- GraphIteratorBFS,
33
- GraphIteratorDijkstra,
34
- transposeDirectedGraph,
35
- };
@@ -1,3 +0,0 @@
1
- import { EDGE_EXISTS_STATE, EDGE_NOT_EXISTS_STATE } from "../constants";
2
-
3
- export { EDGE_NOT_EXISTS_STATE, EDGE_EXISTS_STATE };
@@ -1,13 +0,0 @@
1
- import { generateRandomGraph } from "../data-structures/Graph/demo/generateRandomGraph";
2
- import { createLinkedList } from "../helpers/createLinkedList";
3
- import { createBinaryTree } from "../helpers/createBinaryTree";
4
- import { createGraph } from "../helpers/createGraph";
5
- import { createGraphFromMatrix } from "../helpers/createGraphFromMatrix";
6
-
7
- export {
8
- createGraph,
9
- createGraphFromMatrix,
10
- createBinaryTree,
11
- createLinkedList,
12
- generateRandomGraph,
13
- };
@@ -1,21 +0,0 @@
1
- import Queue from "../data-structures/Queue/Queue";
2
- import Stack from "../data-structures/Stack/Stack";
3
- import UndirectedGraph from "../data-structures/Graph/UndirectedGraph";
4
- import DirectedGraph from "../data-structures/Graph/DirectedGraph";
5
- import BinarySearchTree from "../data-structures/BinaryTree/BinarySearchTree/BinarySearchTree";
6
- import RandBinarySearchTree from "../data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchTree";
7
- import DoubleLinkedList from "../data-structures/LinkedList/DoubleLinkedList/DoubleLinkedList";
8
- import SingleLinkedList from "../data-structures/LinkedList/SingleLinkedList/SingleLinkedList";
9
- import LoopedArray from "../data-structures/LoopedArray/LoopedArray";
10
-
11
- export {
12
- Stack,
13
- Queue,
14
- SingleLinkedList,
15
- DoubleLinkedList,
16
- RandBinarySearchTree,
17
- BinarySearchTree,
18
- DirectedGraph,
19
- UndirectedGraph,
20
- LoopedArray,
21
- };
@@ -1,7 +0,0 @@
1
- import { bubbleSort } from "../algorithms/sorts/bubble-sort";
2
- import { selectSort } from "../algorithms/sorts/select-sort";
3
- import { mergeSort } from "../algorithms/sorts/merge-sort";
4
- import { insertionSort } from "../algorithms/sorts/insertion-sort";
5
- import { quickSort } from "../algorithms/sorts/quick-sort";
6
-
7
- export { bubbleSort, insertionSort, mergeSort, selectSort, quickSort };
@@ -1,53 +0,0 @@
1
- import { ArrayMatrix } from "../types/ArrayMatrix";
2
- import { EnumTreeTraversalType } from "../types/EnumTreeTraversalType";
3
- import { EnumBinarySearchTreeType } from "../types/EnumBinarySearchTreeType";
4
- import { EnumGraphTraversalType } from "../types/EnumGraphTraversalType";
5
- import { EnumGraphType } from "../types/EnumGraphType";
6
- import { EnumLinkedListType } from "../types/EnumLinkedListType";
7
- import { EnumRandomGenerationFormat } from "../types/EnumRandomGenerationFormat";
8
- import { EnumSortType } from "../types/EnumSortType";
9
- import { FnSort } from "../types/FnSort";
10
- import { FnCompareTwo } from "../types/FnCompareTwo";
11
- import { FnToMemoize } from "../types/FnToMemoize";
12
- import IArrayFacade from "../types/IArrayFacade";
13
- import IBiDirectIterator from "../types/IBiDirectIterator";
14
- import IBiDirectIterable from "../types/IBiDirectIterable";
15
- import IIterable from "../types/IIterable";
16
- import IIterator from "../types/IIterator";
17
- import IBinaryTree from "../types/IBinaryTree";
18
- import IConvertableToArray from "../types/IConvertableToArray";
19
- import IGraph from "../types/IGraph";
20
- import IGraphCreator from "../types/IGraphCreator";
21
- import IGraphIterationStrategy from "../types/IGraphIterationStrategy";
22
- import IGraphIterator from "../types/IGraphIterator";
23
- import ILinearStorage from "../types/ILinearStorage";
24
- import ILinearStorageRA from "../types/ILinearStorageRA";
25
- import ILinkedList from "../types/ILinkedList";
26
-
27
- export {
28
- IGraph,
29
- IGraphIterator,
30
- IGraphIterationStrategy,
31
- IIterator,
32
- IIterable,
33
- IGraphCreator,
34
- IBiDirectIterable,
35
- IBiDirectIterator,
36
- EnumGraphType,
37
- EnumSortType,
38
- FnSort,
39
- EnumGraphTraversalType,
40
- EnumTreeTraversalType,
41
- EnumBinarySearchTreeType,
42
- IBinaryTree,
43
- EnumLinkedListType,
44
- ILinkedList,
45
- ILinearStorage,
46
- ILinearStorageRA,
47
- IArrayFacade,
48
- IConvertableToArray,
49
- ArrayMatrix,
50
- FnCompareTwo,
51
- EnumRandomGenerationFormat,
52
- FnToMemoize,
53
- };
@@ -1,25 +0,0 @@
1
- import { binarySearch } from "../../../src/algorithms/binary-search";
2
-
3
- describe("Binary search", () => {
4
- it("should find in positive 100 elements", () => {
5
- const arr: Array<number> = [];
6
- for (let i = 0; i < 100; i++) arr.push(i + 1);
7
- const foundElement = binarySearch(arr, 97);
8
-
9
- expect(foundElement).toBe(96);
10
- });
11
-
12
- it("should find in negative 100 elements", () => {
13
- const arr: Array<number> = [];
14
- for (let i = 0; i < 100; i++) arr.push(-100 + i - 1);
15
- const foundElement = binarySearch(arr, -97);
16
-
17
- expect(foundElement).toBe(4);
18
- });
19
-
20
- it("should return null in empty list", () => {
21
- const arr: Array<number> = [];
22
-
23
- expect(binarySearch(arr, 10)).toBeNull();
24
- });
25
- });
@@ -1,43 +0,0 @@
1
- import {
2
- factorial,
3
- memoizedFactorial,
4
- } from "../../../src/algorithms/factorial";
5
-
6
- describe("Factorial", () => {
7
- const results = new Map<number, number>();
8
- const resultsBigValues = new Map<number, number>();
9
-
10
- results
11
- .set(5, 120)
12
- .set(6, 720)
13
- .set(10, 3628800)
14
- .set(15, 1307674368000)
15
- .set(20, 2432902008176640000)
16
- .set(25, 15511210043330985984000000);
17
-
18
- resultsBigValues
19
- .set(35, 1.0333147966386144e40)
20
- .set(45, 1.1962222086548019e56)
21
- .set(77, 1.4518309202828584e113)
22
- .set(90, 1.4857159644817607e138)
23
- .set(99, 9.33262154439441e155)
24
- .set(100, 9.33262154439441e157);
25
-
26
- const resultsKeys = Array.from(results.keys());
27
- const resultsBigValuesKeys = Array.from(resultsBigValues.keys());
28
-
29
- describe("without memoize", () => {
30
- test.each(resultsKeys)("with n = %i", (n) => {
31
- expect(factorial(n)).toBe(results.get(n));
32
- });
33
- });
34
-
35
- describe("with memoize", () => {
36
- test.each(resultsKeys)("with n = %i", (n) => {
37
- expect(memoizedFactorial(n)).toBe(results.get(n));
38
- });
39
- test.each(resultsBigValuesKeys)("with n = %i", (n) => {
40
- expect(memoizedFactorial(n)).toBe(resultsBigValues.get(n));
41
- });
42
- });
43
- });
@@ -1,41 +0,0 @@
1
- import {
2
- fibonacci,
3
- memoizedFibonacci,
4
- } from "../../../src/algorithms/fibonacci";
5
-
6
- describe("Fibonacci", () => {
7
- const results = new Map<number, number>();
8
- const resultsBigValues = new Map<number, number>();
9
-
10
- results
11
- .set(5, 5)
12
- .set(6, 8)
13
- .set(10, 55)
14
- .set(15, 610)
15
- .set(20, 6765)
16
- .set(25, 75025);
17
-
18
- resultsBigValues
19
- .set(40, 102334155)
20
- .set(45, 1134903170)
21
- .set(77, 5527939700884757)
22
- .set(90, 2880067194370816120);
23
-
24
- const resultsKeys = Array.from(results.keys());
25
- const resultsBigValuesKeys = Array.from(resultsBigValues.keys());
26
-
27
- describe("without memoize", () => {
28
- test.each(resultsKeys)("with n = %i", (n) => {
29
- expect(fibonacci(n)).toBe(results.get(n));
30
- });
31
- });
32
-
33
- describe("with memoize", () => {
34
- test.each(resultsKeys)("with n = %i", (n) => {
35
- expect(memoizedFibonacci(n)).toBe(results.get(n));
36
- });
37
- test.each(resultsBigValuesKeys)("with n = %i", (n) => {
38
- expect(memoizedFibonacci(n)).toBe(resultsBigValues.get(n));
39
- });
40
- });
41
- });
@@ -1,74 +0,0 @@
1
- import { quickSort } from "../../../src/algorithms/sorts/quick-sort";
2
- import { mergeSort } from "../../../src/algorithms/sorts/merge-sort";
3
- import { selectSort } from "../../../src/algorithms/sorts/select-sort";
4
- import { bubbleSort } from "../../../src/algorithms/sorts/bubble-sort";
5
- import { insertionSort } from "../../../src/algorithms/sorts/insertion-sort";
6
- import { randomizeArray } from "../../../src/demo/performance/sort-compare";
7
- import { EnumSortType } from "../../../src/types/EnumSortType";
8
-
9
- const createSortFunction = (
10
- sortType: string
11
- ): ((arr: Array<number>) => Array<number>) => {
12
- switch (sortType) {
13
- case EnumSortType.Quick:
14
- return quickSort;
15
- case EnumSortType.Merge:
16
- return mergeSort;
17
- case EnumSortType.Selection:
18
- return selectSort;
19
- case EnumSortType.Bubble:
20
- return bubbleSort;
21
- case EnumSortType.Insertion:
22
- return insertionSort;
23
- default:
24
- throw new Error("Invalid sort type");
25
- }
26
- };
27
-
28
- describe.each([
29
- EnumSortType.Merge,
30
- EnumSortType.Quick,
31
- EnumSortType.Bubble,
32
- EnumSortType.Insertion,
33
- EnumSortType.Selection,
34
- ])("%s sort", (sortStrategyType: EnumSortType) => {
35
- const sort = createSortFunction(sortStrategyType);
36
-
37
- it("should correctly sort with random numbers", () => {
38
- const notSortedArr: Array<number> = randomizeArray(100, 500);
39
-
40
- const sortedArr = [...notSortedArr].sort((a: number, b: number) => {
41
- if (a > b) return 1;
42
- if (a < b) return -1;
43
- return 0;
44
- });
45
-
46
- expect(sort(notSortedArr)).toEqual(sortedArr);
47
- });
48
-
49
- it("should correctly sort an empty array", () => {
50
- const emptyArr: Array<number> = [];
51
-
52
- expect(sort(emptyArr)).toEqual(emptyArr);
53
- });
54
-
55
- it("should correctly sort already sorted array", () => {
56
- const sortedArr = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5];
57
-
58
- expect(sort(sortedArr)).toEqual(sortedArr);
59
- });
60
-
61
- it("should correctly sort an array with repeated numbers", () => {
62
- const notSortedArr = [7, 2, 1, 2, 7];
63
- const sortedArr = [1, 2, 2, 7, 7];
64
-
65
- expect(sort(notSortedArr)).toEqual(sortedArr);
66
- });
67
-
68
- it("should correctly sort an array with float numbers", () => {
69
- const notSortedArr = [2.5, -2.5, 0, 5.5, -5.5];
70
- const sortedArr = [-5.5, -2.5, 0, 2.5, 5.5];
71
-
72
- expect(sort(notSortedArr)).toEqual(sortedArr);
73
- });
74
- });
@@ -1,39 +0,0 @@
1
- import { transposeMatrix } from "../../../src/algorithms/transpose-matrix";
2
-
3
- describe("transpose matrix", () => {
4
- it("should throw when array is not a matrix", () => {
5
- const srcMatrix = [
6
- [0, 1],
7
- [1, 0],
8
- [0, 1],
9
- ];
10
-
11
- expect(() => {
12
- transposeMatrix(srcMatrix);
13
- }).toThrowError();
14
- });
15
-
16
- it("should transpose matrix correctly", () => {
17
- const srcMatrix = [
18
- [0, 1, 1],
19
- [0, 0, 1],
20
- [1, 0, 0],
21
- ];
22
-
23
- expect(transposeMatrix(srcMatrix)).toEqual([
24
- [0, 0, 1],
25
- [1, 0, 0],
26
- [1, 1, 0],
27
- ]);
28
- });
29
-
30
- it("should return same matrix in case of symmetric matrix", () => {
31
- const srcMatrix = [
32
- [0, 1, 0],
33
- [1, 0, 1],
34
- [0, 1, 0],
35
- ];
36
-
37
- expect(transposeMatrix(srcMatrix)).toEqual(srcMatrix);
38
- });
39
- });