@raikuxq/alg-ds 1.0.0

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 (114) hide show
  1. package/.eslintrc.js +14 -0
  2. package/LICENSE +21 -0
  3. package/README.md +221 -0
  4. package/jest.config.js +4 -0
  5. package/nodemon.json +6 -0
  6. package/package.json +43 -0
  7. package/src/algorithms/binary-search.ts +28 -0
  8. package/src/algorithms/factorial.ts +18 -0
  9. package/src/algorithms/fibonacci.ts +18 -0
  10. package/src/algorithms/memoize.ts +21 -0
  11. package/src/algorithms/sorts/bubble-sort.ts +21 -0
  12. package/src/algorithms/sorts/insertion-sort.ts +25 -0
  13. package/src/algorithms/sorts/merge-sort.ts +74 -0
  14. package/src/algorithms/sorts/quick-sort.ts +54 -0
  15. package/src/algorithms/sorts/select-sort.ts +19 -0
  16. package/src/algorithms/transpose-matrix.ts +19 -0
  17. package/src/constants.ts +2 -0
  18. package/src/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryNode.ts +45 -0
  19. package/src/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryTree.ts +80 -0
  20. package/src/data-structures/BinaryTree/BinarySearchTree/BinarySearchNode.ts +38 -0
  21. package/src/data-structures/BinaryTree/BinarySearchTree/BinarySearchTree.ts +286 -0
  22. package/src/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchNode.ts +48 -0
  23. package/src/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchTree.ts +228 -0
  24. package/src/data-structures/Graph/AbstractGraph.ts +189 -0
  25. package/src/data-structures/Graph/DirectedGraph.ts +84 -0
  26. package/src/data-structures/Graph/GraphEdge.ts +33 -0
  27. package/src/data-structures/Graph/UndirectedGraph.ts +108 -0
  28. package/src/data-structures/Graph/demo/generateRandomGraph.ts +93 -0
  29. package/src/data-structures/Graph/iterator/AbstractGraphIterator.ts +99 -0
  30. package/src/data-structures/Graph/iterator/GraphIteratorBFS.ts +60 -0
  31. package/src/data-structures/Graph/iterator/GraphIteratorDFS.ts +60 -0
  32. package/src/data-structures/Graph/iterator/GraphIteratorDijkstra.ts +94 -0
  33. package/src/data-structures/Graph/presenter/presenterAdjacencyLists.ts +29 -0
  34. package/src/data-structures/Graph/presenter/presenterAdjacencyMatrix.ts +51 -0
  35. package/src/data-structures/Graph/searching/hasPath.ts +38 -0
  36. package/src/data-structures/Graph/searching/shortestPath.ts +38 -0
  37. package/src/data-structures/Graph/strategy/BFSIterationStrategy.ts +11 -0
  38. package/src/data-structures/Graph/strategy/DFSIterationStrategy.ts +11 -0
  39. package/src/data-structures/Graph/strategy/DijkstraIterationStrategy.ts +11 -0
  40. package/src/data-structures/Graph/transposing/transposeDirectedGraph.ts +19 -0
  41. package/src/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedList.ts +310 -0
  42. package/src/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedNode.ts +33 -0
  43. package/src/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedList.ts +156 -0
  44. package/src/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedNode.ts +47 -0
  45. package/src/data-structures/LinkedList/SingleLinkedList/SingleLinkedList.ts +147 -0
  46. package/src/data-structures/LinkedList/SingleLinkedList/SingleLinkedNode.ts +10 -0
  47. package/src/data-structures/LoopedArray/LoopedArray.ts +182 -0
  48. package/src/data-structures/Queue/Queue.ts +92 -0
  49. package/src/data-structures/Stack/Stack.ts +92 -0
  50. package/src/demo/demo.bst.ts +67 -0
  51. package/src/demo/demo.graph.ts +246 -0
  52. package/src/demo/demo.linked-list.ts +78 -0
  53. package/src/demo/demo.looped-array.ts +104 -0
  54. package/src/demo/demo.queue.ts +40 -0
  55. package/src/demo/demo.stack.ts +40 -0
  56. package/src/demo/performance/bst-compare.ts +38 -0
  57. package/src/demo/performance/ds-compare.ts +58 -0
  58. package/src/demo/performance/sort-compare.ts +60 -0
  59. package/src/exports/algotirhms.ts +35 -0
  60. package/src/exports/constants.ts +3 -0
  61. package/src/exports/helpers.ts +13 -0
  62. package/src/exports/main.ts +21 -0
  63. package/src/exports/sorts.ts +7 -0
  64. package/src/exports/types.ts +53 -0
  65. package/src/exports/utils.ts +21 -0
  66. package/src/helpers/createBinaryTree.ts +24 -0
  67. package/src/helpers/createGraph.ts +24 -0
  68. package/src/helpers/createGraphFromMatrix.ts +47 -0
  69. package/src/helpers/createLinkedList.ts +24 -0
  70. package/src/index.ts +40 -0
  71. package/src/types/ArrayMatrix.ts +1 -0
  72. package/src/types/EnumBinarySearchTreeType.ts +4 -0
  73. package/src/types/EnumGraphTraversalType.ts +5 -0
  74. package/src/types/EnumGraphType.ts +4 -0
  75. package/src/types/EnumLinkedListType.ts +4 -0
  76. package/src/types/EnumRandomGenerationFormat.ts +4 -0
  77. package/src/types/EnumSortType.ts +7 -0
  78. package/src/types/EnumTreeTraversalType.ts +5 -0
  79. package/src/types/FnCompareTwo.ts +1 -0
  80. package/src/types/FnSort.ts +1 -0
  81. package/src/types/FnToMemoize.ts +1 -0
  82. package/src/types/IArrayFacade.ts +6 -0
  83. package/src/types/IBiDirectIterable.ts +6 -0
  84. package/src/types/IBiDirectIterator.ts +12 -0
  85. package/src/types/IBinaryTree.ts +13 -0
  86. package/src/types/IConvertableToArray.ts +4 -0
  87. package/src/types/IGraph.ts +16 -0
  88. package/src/types/IGraphCreator.ts +5 -0
  89. package/src/types/IGraphIterationStrategy.ts +6 -0
  90. package/src/types/IGraphIterator.ts +13 -0
  91. package/src/types/IIterable.ts +5 -0
  92. package/src/types/IIterator.ts +14 -0
  93. package/src/types/ILinearStorage.ts +11 -0
  94. package/src/types/ILinearStorageRA.ts +14 -0
  95. package/src/types/ILinkedList.ts +6 -0
  96. package/src/utils.ts +65 -0
  97. package/test/unit/algorithms/binary-search.test.ts +25 -0
  98. package/test/unit/algorithms/factorial.test.ts +43 -0
  99. package/test/unit/algorithms/fibonacci.test.ts +41 -0
  100. package/test/unit/algorithms/sorts.test.ts +74 -0
  101. package/test/unit/algorithms/transpose-matrix.test.ts +39 -0
  102. package/test/unit/data-structures/binary-tree/binary-search-tree.test.ts +230 -0
  103. package/test/unit/data-structures/graph/graph.create-from-matrix.test.ts +106 -0
  104. package/test/unit/data-structures/graph/graph.has-path.test.ts +115 -0
  105. package/test/unit/data-structures/graph/graph.presenter.lists.test.ts +76 -0
  106. package/test/unit/data-structures/graph/graph.presenter.matrix.test.ts +73 -0
  107. package/test/unit/data-structures/graph/graph.shortest-path.test.ts +207 -0
  108. package/test/unit/data-structures/graph/graph.test.ts +394 -0
  109. package/test/unit/data-structures/graph/graph.transpose.test.ts +52 -0
  110. package/test/unit/data-structures/linked-list/linked-list.test.ts +477 -0
  111. package/test/unit/data-structures/looped-array/looped-array.test.ts +387 -0
  112. package/test/unit/data-structures/queue/queue.test.ts +147 -0
  113. package/test/unit/data-structures/stack/stack.test.ts +155 -0
  114. package/tsconfig.json +18 -0
@@ -0,0 +1,38 @@
1
+ import RandBinarySearchTree from "../../data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchTree";
2
+ import BinarySearchTree from "../../data-structures/BinaryTree/BinarySearchTree/BinarySearchTree";
3
+ import { EnumTreeTraversalType } from "../../types/EnumTreeTraversalType";
4
+ import IBinaryTree from "../../types/IBinaryTree";
5
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
6
+ const util = require("util");
7
+
8
+ const logTree = <T>(tree: IBinaryTree<T>) => {
9
+ console.log("HEIGHT");
10
+ console.log(tree.height());
11
+ console.log("LENGTH");
12
+ console.log(tree.length());
13
+ console.log("MAX");
14
+ console.log(tree.max());
15
+ console.log("MIN");
16
+ console.log(tree.min());
17
+ };
18
+
19
+ export const bstCompare = (): void => {
20
+ const bstLeaf = new BinarySearchTree<number>();
21
+ const bstRand = new RandBinarySearchTree<number>();
22
+
23
+ const arraySrc = Array.from(Array(1000).keys());
24
+
25
+ arraySrc.forEach((num, index) => {
26
+ bstLeaf.insert(num);
27
+ bstRand.insert(num);
28
+ });
29
+
30
+ console.log(
31
+ "***************************LEAF TREE*****************************"
32
+ );
33
+ logTree<number>(bstLeaf);
34
+ console.log(
35
+ "\n\n\n***************************RAND TREE*****************************"
36
+ );
37
+ logTree<number>(bstRand);
38
+ };
@@ -0,0 +1,58 @@
1
+ import Queue from "../../data-structures/Queue/Queue";
2
+ import Stack from "../../data-structures/Stack/Stack";
3
+ import ILinearStorage from "../../types/ILinearStorage";
4
+ import { perf, roundNumber } from "../../utils";
5
+
6
+ export const pushToLinearDS = (
7
+ linearDS: ILinearStorage<string>,
8
+ elementsCount: number,
9
+ item: string
10
+ ): void => {
11
+ for (let i = 0; i < elementsCount; i++) {
12
+ linearDS.push(`${item}_${i}`);
13
+ }
14
+ };
15
+
16
+ export const perfLinearDS = (linearDS: ILinearStorage<string>): void => {
17
+ let elementsCount = 50;
18
+
19
+ while (elementsCount <= 5000000) {
20
+ pushToLinearDS(linearDS, elementsCount, "qwerty");
21
+
22
+ const perfPush = perf(() => {
23
+ linearDS.push("qwerty");
24
+ });
25
+ const perfPeek = perf(() => {
26
+ linearDS.peek();
27
+ });
28
+
29
+ const perfPop = perf(() => {
30
+ linearDS.pop();
31
+ });
32
+ const perfHas = perf(() => {
33
+ linearDS.has(`qwerty_${elementsCount - 10}`);
34
+ });
35
+
36
+ console.log(`N: ${elementsCount} push: ${roundNumber(perfPush)}ms`);
37
+ console.log(`N: ${elementsCount} peek: ${roundNumber(perfPeek)}ms`);
38
+ console.log(`N: ${elementsCount} pop: ${roundNumber(perfPop)}ms`);
39
+ console.log(`N: ${elementsCount} has: ${roundNumber(perfHas)}ms`);
40
+ console.log("=========================");
41
+
42
+ elementsCount *= 10;
43
+ }
44
+ };
45
+
46
+ export const perfQueue = (): void => {
47
+ console.log(`QUEUE PERFORMANCE TEST:`);
48
+ const queue: ILinearStorage<string> = new Queue<string>();
49
+
50
+ perfLinearDS(queue);
51
+ };
52
+
53
+ export const perfStack = (): void => {
54
+ console.log(`STACK PERFORMANCE TEST:`);
55
+ const stack: ILinearStorage<string> = new Stack<string>();
56
+
57
+ perfLinearDS(stack);
58
+ };
@@ -0,0 +1,60 @@
1
+ import { FnSort } from "../../types/FnSort";
2
+ import { mergeSort } from "../../algorithms/sorts/merge-sort";
3
+ import { insertionSort } from "../../algorithms/sorts/insertion-sort";
4
+ import { bubbleSort } from "../../algorithms/sorts/bubble-sort";
5
+ import { quickSort } from "../../algorithms/sorts/quick-sort";
6
+ import { selectSort } from "../../algorithms/sorts/select-sort";
7
+ import { perf } from "../../utils";
8
+
9
+ export const randomizeArray = (length: number, max: number): Array<number> =>
10
+ new Array(length).fill(0).map(() => Math.round(Math.random() * max));
11
+
12
+ export const sortCompare = (
13
+ sortFn: FnSort,
14
+ n: number,
15
+ callsNumber: number
16
+ ): void => {
17
+ let totalTime = 0;
18
+
19
+ for (let i = 0; i < callsNumber; i++) {
20
+ const generatedArr = randomizeArray(n, 1000);
21
+ const perfResult = perf(() => {
22
+ sortFn(generatedArr);
23
+ });
24
+ totalTime += perfResult;
25
+ }
26
+
27
+ const averageTime = totalTime / callsNumber;
28
+
29
+ console.log(`N: ${n} = ${averageTime}ms`);
30
+ };
31
+
32
+ export const sortCompareRunner = (
33
+ sortFn: FnSort,
34
+ callsNumber: number
35
+ ): void => {
36
+ sortCompare(sortFn, 5, callsNumber);
37
+ sortCompare(sortFn, 50, callsNumber);
38
+ sortCompare(sortFn, 500, callsNumber);
39
+ sortCompare(sortFn, 5000, callsNumber);
40
+ sortCompare(sortFn, 50000, callsNumber);
41
+ };
42
+
43
+ export const compareAllSortTypes = (): void => {
44
+ const callsNumber = 10;
45
+
46
+ console.log(`MERGE SORT:`);
47
+ sortCompareRunner(mergeSort, callsNumber);
48
+
49
+ console.log(`QUICK SORT:`);
50
+ sortCompareRunner(quickSort, callsNumber);
51
+
52
+ console.log(`SELECTION SORT:`);
53
+ sortCompareRunner(selectSort, callsNumber);
54
+
55
+ console.log(`BUBBLE SORT:`);
56
+ sortCompareRunner(bubbleSort, callsNumber);
57
+
58
+ console.log(`INSERTION SORT:`);
59
+ sortCompareRunner(insertionSort, callsNumber);
60
+ };
@@ -0,0 +1,35 @@
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
+ };
@@ -0,0 +1,3 @@
1
+ import { EDGE_EXISTS_STATE, EDGE_NOT_EXISTS_STATE } from "../constants";
2
+
3
+ export { EDGE_NOT_EXISTS_STATE, EDGE_EXISTS_STATE };
@@ -0,0 +1,13 @@
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
+ };
@@ -0,0 +1,21 @@
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
+ };
@@ -0,0 +1,7 @@
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 };
@@ -0,0 +1,53 @@
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
+ };
@@ -0,0 +1,21 @@
1
+ import { memoize } from "../algorithms/memoize";
2
+ import {
3
+ getMinIndex,
4
+ getMinIndexFromIndex,
5
+ perf,
6
+ randomizeNumberInRange,
7
+ roundNumber,
8
+ swapArrayItems,
9
+ perfAsync,
10
+ } from "../utils";
11
+
12
+ export {
13
+ perf,
14
+ getMinIndex,
15
+ getMinIndexFromIndex,
16
+ memoize,
17
+ perfAsync,
18
+ roundNumber,
19
+ randomizeNumberInRange,
20
+ swapArrayItems,
21
+ };
@@ -0,0 +1,24 @@
1
+ import { EnumBinarySearchTreeType } from "../types/EnumBinarySearchTreeType";
2
+ import BinarySearchTree from "../data-structures/BinaryTree/BinarySearchTree/BinarySearchTree";
3
+ import RandBinarySearchTree from "../data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchTree";
4
+ import IBinaryTree from "../types/IBinaryTree";
5
+
6
+ /**
7
+ * Returns binary tree by type
8
+ */
9
+ export const createBinaryTree = <T>(
10
+ type: EnumBinarySearchTreeType
11
+ ): IBinaryTree<T> => {
12
+ let binaryTree: IBinaryTree<T>;
13
+
14
+ switch (type) {
15
+ case EnumBinarySearchTreeType.BST:
16
+ binaryTree = new BinarySearchTree();
17
+ break;
18
+ case EnumBinarySearchTreeType.RANDOMIZED_BST:
19
+ binaryTree = new RandBinarySearchTree();
20
+ break;
21
+ }
22
+
23
+ return binaryTree;
24
+ };
@@ -0,0 +1,24 @@
1
+ import IGraph from "../types/IGraph";
2
+ import DirectedGraph from "../data-structures/Graph/DirectedGraph";
3
+ import UndirectedGraph from "../data-structures/Graph/UndirectedGraph";
4
+ import { EnumGraphType } from "../types/EnumGraphType";
5
+
6
+ /**
7
+ * Returns graph by type
8
+ */
9
+ export const createGraph = <T>(type: EnumGraphType): IGraph<T> => {
10
+ let graph: IGraph<T>;
11
+
12
+ switch (type) {
13
+ case EnumGraphType.Directed:
14
+ graph = new DirectedGraph();
15
+ break;
16
+ case EnumGraphType.Undirected:
17
+ graph = new UndirectedGraph();
18
+ break;
19
+ default:
20
+ throw new Error("Invalid graph type");
21
+ }
22
+
23
+ return graph;
24
+ };
@@ -0,0 +1,47 @@
1
+ import IGraph from "../types/IGraph";
2
+ import { EnumGraphType } from "../types/EnumGraphType";
3
+ import { ArrayMatrix } from "../types/ArrayMatrix";
4
+ import { createGraph } from "./createGraph";
5
+ import { EDGE_EXISTS_STATE } from "../constants";
6
+
7
+ /**
8
+ * Creates a graph from N*N matrix that contains 1 in case of edge exists or 0 in case it does not
9
+ */
10
+ export const createGraphFromMatrix = <T>(
11
+ matrix: ArrayMatrix,
12
+ fieldsList: Array<T>,
13
+ type: EnumGraphType
14
+ ): IGraph<T> => {
15
+ const graph: IGraph<T> = createGraph(type);
16
+
17
+ fieldsList.forEach((fieldName) => {
18
+ graph.addVertex(fieldName);
19
+ });
20
+
21
+ matrix.forEach((row: Array<number>, rowIndex: number) => {
22
+ row.forEach((col: number, colIndex: number) => {
23
+ const rowColState = matrix[rowIndex][colIndex];
24
+ const colRowState = matrix[colIndex][rowIndex];
25
+
26
+ if (type === EnumGraphType.Undirected) {
27
+ if (
28
+ rowColState === EDGE_EXISTS_STATE &&
29
+ colRowState === EDGE_EXISTS_STATE
30
+ ) {
31
+ graph.addEdge(fieldsList[rowIndex], fieldsList[colIndex]);
32
+ }
33
+ }
34
+
35
+ if (type === EnumGraphType.Directed) {
36
+ if (rowColState === EDGE_EXISTS_STATE) {
37
+ graph.addEdge(fieldsList[rowIndex], fieldsList[colIndex]);
38
+ }
39
+ if (colRowState === EDGE_EXISTS_STATE) {
40
+ graph.addEdge(fieldsList[colIndex], fieldsList[rowIndex]);
41
+ }
42
+ }
43
+ });
44
+ });
45
+
46
+ return graph;
47
+ };
@@ -0,0 +1,24 @@
1
+ import { EnumLinkedListType } from "../types/EnumLinkedListType";
2
+ import DoubleLinkedList from "../data-structures/LinkedList/DoubleLinkedList/DoubleLinkedList";
3
+ import SingleLinkedList from "../data-structures/LinkedList/SingleLinkedList/SingleLinkedList";
4
+ import ILinkedList from "../types/ILinkedList";
5
+
6
+ export const createLinkedList = <T>(
7
+ type: EnumLinkedListType,
8
+ capacity?: number
9
+ ): ILinkedList<T> => {
10
+ let linkedList: ILinkedList<T>;
11
+
12
+ switch (type) {
13
+ case EnumLinkedListType.DOUBLE:
14
+ linkedList = new DoubleLinkedList(capacity);
15
+ break;
16
+ case EnumLinkedListType.SINGLE:
17
+ linkedList = new SingleLinkedList(capacity);
18
+ break;
19
+ default:
20
+ throw new Error("Invalid list type");
21
+ }
22
+
23
+ return linkedList;
24
+ };
package/src/index.ts ADDED
@@ -0,0 +1,40 @@
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();
@@ -0,0 +1 @@
1
+ export type ArrayMatrix = Array<Array<number>>;
@@ -0,0 +1,4 @@
1
+ export enum EnumBinarySearchTreeType {
2
+ BST = "Binary Search Tree",
3
+ RANDOMIZED_BST = "Randomized Binary Search Tree",
4
+ }
@@ -0,0 +1,5 @@
1
+ export enum EnumGraphTraversalType {
2
+ BFS = "Breadth first traversal",
3
+ DFS = "Deep first traversal",
4
+ DIJKSTRA = "Dijkstra traversal",
5
+ }
@@ -0,0 +1,4 @@
1
+ export enum EnumGraphType {
2
+ Directed = "Directed",
3
+ Undirected = "Undirected",
4
+ }
@@ -0,0 +1,4 @@
1
+ export enum EnumLinkedListType {
2
+ DOUBLE = "Double linked list",
3
+ SINGLE = "Single linked list",
4
+ }
@@ -0,0 +1,4 @@
1
+ export enum EnumRandomGenerationFormat {
2
+ Numbers = "NUMBERS",
3
+ Hash = "HASH",
4
+ }
@@ -0,0 +1,7 @@
1
+ export enum EnumSortType {
2
+ Quick = "Quick",
3
+ Merge = "Merge",
4
+ Selection = "Selection",
5
+ Bubble = "Bubble",
6
+ Insertion = "Insertion",
7
+ }
@@ -0,0 +1,5 @@
1
+ export enum EnumTreeTraversalType {
2
+ InOrder = "InOrder",
3
+ PreOrder = "PreOrder",
4
+ PostOrder = "PostOrder",
5
+ }
@@ -0,0 +1 @@
1
+ export type FnCompareTwo<T> = (firstItem: T, secondItem: T) => boolean;
@@ -0,0 +1 @@
1
+ export type FnSort = (arg: Array<number>) => Array<number>;
@@ -0,0 +1 @@
1
+ export type FnToMemoize<Key, Value> = (...args: Array<Key>) => Value;
@@ -0,0 +1,6 @@
1
+ import ILinearStorageRA from "./ILinearStorageRA";
2
+ import IConvertableToArray from "./IConvertableToArray";
3
+
4
+ export default interface IArrayFacade<T>
5
+ extends ILinearStorageRA<T>,
6
+ IConvertableToArray<T> {}
@@ -0,0 +1,6 @@
1
+ import IBiDirectIterator from "./IBiDirectIterator";
2
+ import IIterable from "./IIterable";
3
+
4
+ export default interface IBiDirectIterable<T> extends IIterable<T> {
5
+ iterator(fromIndex?: number): IBiDirectIterator<T>;
6
+ }
@@ -0,0 +1,12 @@
1
+ import IIterator from "./IIterator";
2
+
3
+ export default interface IBiDirectIterator<T> extends IIterator<T> {
4
+ /**
5
+ * Will do one iteration back and returns prev item value
6
+ */
7
+ prev(): T;
8
+ /**
9
+ * Check if next element exists
10
+ */
11
+ hasPrev(): boolean;
12
+ }
@@ -0,0 +1,13 @@
1
+ import { EnumTreeTraversalType } from "./EnumTreeTraversalType";
2
+
3
+ export default interface IBinaryTree<T> {
4
+ has(value: T): boolean;
5
+ insert(value: T): void;
6
+ delete(value: T): void;
7
+ subtree(value: T): IBinaryTree<T>;
8
+ max(): T;
9
+ min(): T;
10
+ length(): number;
11
+ height(): number;
12
+ traverse(type: EnumTreeTraversalType): Array<T>;
13
+ }
@@ -0,0 +1,4 @@
1
+ export default interface IConvertableToArray<T> {
2
+ pushFromArray(elements: Array<T>): void;
3
+ getAsArray(): Array<T>;
4
+ }
@@ -0,0 +1,16 @@
1
+ export default interface IGraph<T> {
2
+ weight(): number;
3
+ vertices(): Array<T>;
4
+ verticesCount(): number;
5
+ edgesCount(): number;
6
+
7
+ addVertex(data: T): this;
8
+ removeVertex(data: T): this;
9
+ hasVertex(data: T): boolean;
10
+ getVertexNeighbors(data: T): Array<T>;
11
+
12
+ addEdge(from: T, to: T, weight?: number): this;
13
+ removeEdge(from: T, to: T): this;
14
+ hasEdge(from: T, to: T): boolean;
15
+ getEdgeWeight(from: T, to: T): number;
16
+ }
@@ -0,0 +1,5 @@
1
+ import IGraph from "./IGraph";
2
+
3
+ export default interface IGraphCreator<T> {
4
+ createGraph(): IGraph<T>;
5
+ }
@@ -0,0 +1,6 @@
1
+ import IGraph from "./IGraph";
2
+ import IGraphIterator from "./IGraphIterator";
3
+
4
+ export default interface IGraphIterationStrategy<T> {
5
+ createIterator(graph: IGraph<T>): IGraphIterator<T>;
6
+ }
@@ -0,0 +1,13 @@
1
+ import IIterator from "./IIterator";
2
+
3
+ export default interface IGraphIterator<T> extends IIterator<T> {
4
+ /**
5
+ * Get path which passed by iterator between two vertices
6
+ */
7
+ getPath(from: T, to: T): Array<T>;
8
+
9
+ /**
10
+ * Initialize iterator by passing start vertex
11
+ */
12
+ initIterator(from: T): void;
13
+ }
@@ -0,0 +1,5 @@
1
+ import IIterator from "./IIterator";
2
+
3
+ export default interface IIterable<T> {
4
+ iterator(fromIndex?: number): IIterator<T>;
5
+ }