@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.
- package/.eslintrc.js +14 -0
- package/LICENSE +21 -0
- package/README.md +221 -0
- package/jest.config.js +4 -0
- package/nodemon.json +6 -0
- package/package.json +43 -0
- package/src/algorithms/binary-search.ts +28 -0
- package/src/algorithms/factorial.ts +18 -0
- package/src/algorithms/fibonacci.ts +18 -0
- package/src/algorithms/memoize.ts +21 -0
- package/src/algorithms/sorts/bubble-sort.ts +21 -0
- package/src/algorithms/sorts/insertion-sort.ts +25 -0
- package/src/algorithms/sorts/merge-sort.ts +74 -0
- package/src/algorithms/sorts/quick-sort.ts +54 -0
- package/src/algorithms/sorts/select-sort.ts +19 -0
- package/src/algorithms/transpose-matrix.ts +19 -0
- package/src/constants.ts +2 -0
- package/src/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryNode.ts +45 -0
- package/src/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryTree.ts +80 -0
- package/src/data-structures/BinaryTree/BinarySearchTree/BinarySearchNode.ts +38 -0
- package/src/data-structures/BinaryTree/BinarySearchTree/BinarySearchTree.ts +286 -0
- package/src/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchNode.ts +48 -0
- package/src/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchTree.ts +228 -0
- package/src/data-structures/Graph/AbstractGraph.ts +189 -0
- package/src/data-structures/Graph/DirectedGraph.ts +84 -0
- package/src/data-structures/Graph/GraphEdge.ts +33 -0
- package/src/data-structures/Graph/UndirectedGraph.ts +108 -0
- package/src/data-structures/Graph/demo/generateRandomGraph.ts +93 -0
- package/src/data-structures/Graph/iterator/AbstractGraphIterator.ts +99 -0
- package/src/data-structures/Graph/iterator/GraphIteratorBFS.ts +60 -0
- package/src/data-structures/Graph/iterator/GraphIteratorDFS.ts +60 -0
- package/src/data-structures/Graph/iterator/GraphIteratorDijkstra.ts +94 -0
- package/src/data-structures/Graph/presenter/presenterAdjacencyLists.ts +29 -0
- package/src/data-structures/Graph/presenter/presenterAdjacencyMatrix.ts +51 -0
- package/src/data-structures/Graph/searching/hasPath.ts +38 -0
- package/src/data-structures/Graph/searching/shortestPath.ts +38 -0
- package/src/data-structures/Graph/strategy/BFSIterationStrategy.ts +11 -0
- package/src/data-structures/Graph/strategy/DFSIterationStrategy.ts +11 -0
- package/src/data-structures/Graph/strategy/DijkstraIterationStrategy.ts +11 -0
- package/src/data-structures/Graph/transposing/transposeDirectedGraph.ts +19 -0
- package/src/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedList.ts +310 -0
- package/src/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedNode.ts +33 -0
- package/src/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedList.ts +156 -0
- package/src/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedNode.ts +47 -0
- package/src/data-structures/LinkedList/SingleLinkedList/SingleLinkedList.ts +147 -0
- package/src/data-structures/LinkedList/SingleLinkedList/SingleLinkedNode.ts +10 -0
- package/src/data-structures/LoopedArray/LoopedArray.ts +182 -0
- package/src/data-structures/Queue/Queue.ts +92 -0
- package/src/data-structures/Stack/Stack.ts +92 -0
- package/src/demo/demo.bst.ts +67 -0
- package/src/demo/demo.graph.ts +246 -0
- package/src/demo/demo.linked-list.ts +78 -0
- package/src/demo/demo.looped-array.ts +104 -0
- package/src/demo/demo.queue.ts +40 -0
- package/src/demo/demo.stack.ts +40 -0
- package/src/demo/performance/bst-compare.ts +38 -0
- package/src/demo/performance/ds-compare.ts +58 -0
- package/src/demo/performance/sort-compare.ts +60 -0
- package/src/exports/algotirhms.ts +35 -0
- package/src/exports/constants.ts +3 -0
- package/src/exports/helpers.ts +13 -0
- package/src/exports/main.ts +21 -0
- package/src/exports/sorts.ts +7 -0
- package/src/exports/types.ts +53 -0
- package/src/exports/utils.ts +21 -0
- package/src/helpers/createBinaryTree.ts +24 -0
- package/src/helpers/createGraph.ts +24 -0
- package/src/helpers/createGraphFromMatrix.ts +47 -0
- package/src/helpers/createLinkedList.ts +24 -0
- package/src/index.ts +40 -0
- package/src/types/ArrayMatrix.ts +1 -0
- package/src/types/EnumBinarySearchTreeType.ts +4 -0
- package/src/types/EnumGraphTraversalType.ts +5 -0
- package/src/types/EnumGraphType.ts +4 -0
- package/src/types/EnumLinkedListType.ts +4 -0
- package/src/types/EnumRandomGenerationFormat.ts +4 -0
- package/src/types/EnumSortType.ts +7 -0
- package/src/types/EnumTreeTraversalType.ts +5 -0
- package/src/types/FnCompareTwo.ts +1 -0
- package/src/types/FnSort.ts +1 -0
- package/src/types/FnToMemoize.ts +1 -0
- package/src/types/IArrayFacade.ts +6 -0
- package/src/types/IBiDirectIterable.ts +6 -0
- package/src/types/IBiDirectIterator.ts +12 -0
- package/src/types/IBinaryTree.ts +13 -0
- package/src/types/IConvertableToArray.ts +4 -0
- package/src/types/IGraph.ts +16 -0
- package/src/types/IGraphCreator.ts +5 -0
- package/src/types/IGraphIterationStrategy.ts +6 -0
- package/src/types/IGraphIterator.ts +13 -0
- package/src/types/IIterable.ts +5 -0
- package/src/types/IIterator.ts +14 -0
- package/src/types/ILinearStorage.ts +11 -0
- package/src/types/ILinearStorageRA.ts +14 -0
- package/src/types/ILinkedList.ts +6 -0
- package/src/utils.ts +65 -0
- package/test/unit/algorithms/binary-search.test.ts +25 -0
- package/test/unit/algorithms/factorial.test.ts +43 -0
- package/test/unit/algorithms/fibonacci.test.ts +41 -0
- package/test/unit/algorithms/sorts.test.ts +74 -0
- package/test/unit/algorithms/transpose-matrix.test.ts +39 -0
- package/test/unit/data-structures/binary-tree/binary-search-tree.test.ts +230 -0
- package/test/unit/data-structures/graph/graph.create-from-matrix.test.ts +106 -0
- package/test/unit/data-structures/graph/graph.has-path.test.ts +115 -0
- package/test/unit/data-structures/graph/graph.presenter.lists.test.ts +76 -0
- package/test/unit/data-structures/graph/graph.presenter.matrix.test.ts +73 -0
- package/test/unit/data-structures/graph/graph.shortest-path.test.ts +207 -0
- package/test/unit/data-structures/graph/graph.test.ts +394 -0
- package/test/unit/data-structures/graph/graph.transpose.test.ts +52 -0
- package/test/unit/data-structures/linked-list/linked-list.test.ts +477 -0
- package/test/unit/data-structures/looped-array/looped-array.test.ts +387 -0
- package/test/unit/data-structures/queue/queue.test.ts +147 -0
- package/test/unit/data-structures/stack/stack.test.ts +155 -0
- package/tsconfig.json +18 -0
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
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/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Alexey Stepanov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
Common algorithms and data structures.
|
|
2
|
+
|
|
3
|
+
Written in TypeScript, tested with Jest.
|
|
4
|
+
|
|
5
|
+
# Getting started
|
|
6
|
+
|
|
7
|
+
Clone this repository and install dependencies by using `yarn` command.
|
|
8
|
+
|
|
9
|
+
+ `yarn test` - run all tests via jest
|
|
10
|
+
|
|
11
|
+
+ `yarn dev` - run in dev mode via nodemon (src/index.ts is an entrypoint)
|
|
12
|
+
|
|
13
|
+
+ `yarn build` - compile ts sources into js files
|
|
14
|
+
|
|
15
|
+
+ `yarn start` - build and run in production mode
|
|
16
|
+
|
|
17
|
+
+ `yarn lint` - lint check via eslint
|
|
18
|
+
|
|
19
|
+
+ `yarn lint:fix` - fix source files via eslint
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
# Navigation
|
|
24
|
+
+ [Algorithms](#algorithms)
|
|
25
|
+
+ [Sorting algorithms](#sorting-algorithms)
|
|
26
|
+
+ [Iterators](#iterators)
|
|
27
|
+
+ [Linear data structures](#linear-data-structures)
|
|
28
|
+
+ [Linked list](#linked-list)
|
|
29
|
+
+ [Looped array](#looped-array)
|
|
30
|
+
+ [Stack](#stack)
|
|
31
|
+
+ [Queue](#queue)
|
|
32
|
+
+ [Non-linear data structures](#non-linear-data-structures)
|
|
33
|
+
+ [Graph](#graph)
|
|
34
|
+
+ [Binary tree](#binary-trees)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
# Algorithms
|
|
38
|
+
|
|
39
|
+
## Uncategorized algorithms
|
|
40
|
+
[memoize](src/algorithms/memoize.ts) — Memoization util function.
|
|
41
|
+
|
|
42
|
+
[binary-search](src/algorithms/binary-search.ts) [[ tests ](test/unit/algorithms/binary-search.test.ts)] — Binary searching algorithm. Time: O(log(n)).
|
|
43
|
+
|
|
44
|
+
[factorial](src/algorithms/factorial.ts) [[ tests ](test/unit/algorithms/factorial.test.ts)] — Recursive O(n!) and memoized O(n) factorial implementation.
|
|
45
|
+
|
|
46
|
+
[fibonacci](src/algorithms/fibonacci.ts) [[ tests ](test/unit/algorithms/fibonacci.test.ts)] — Recursive O(n!) and memoized O(n) factorial implementation.
|
|
47
|
+
|
|
48
|
+
[transpose-matrix](src/algorithms/transpose-matrix.ts) [[ tests ](test/unit/algorithms/transpose-matrix.test.ts)] — Transpose N*N matrix util function.
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
## Sorting algorithms
|
|
52
|
+
[bubble-sort](src/algorithms/sorts/bubble-sort.ts) [[ tests ](test/unit/algorithms/sorts.test.ts)] — Time: O(n^2) worst, O(n^2) avg, O(n) best. Memory: O(1) worst.
|
|
53
|
+
|
|
54
|
+
[selection-sort](src/algorithms/sorts/select-sort.ts) [[ tests ](test/unit/algorithms/sorts.test.ts)] — Time: O(n^2) worst, O(n^2) avg, O(n^2) best. Memory: O(1) worst.
|
|
55
|
+
|
|
56
|
+
[insertion-sort](src/algorithms/sorts/insertion-sort.ts) [[ tests ](test/unit/algorithms/sorts.test.ts)] — Time: O(n^2) worst, O(n^2) avg, O(n) best. Memory: O(1) worst.
|
|
57
|
+
|
|
58
|
+
[merge-sort](src/algorithms/sorts/merge-sort.ts) [[ tests ](test/unit/algorithms/sorts.test.ts)] — Time: O(n*log(n)) worst, O(n*log(n)) avg, O(n*log(n)) best. Memory: O(n) worst.
|
|
59
|
+
|
|
60
|
+
[quick-sort](src/algorithms/sorts/quick-sort.ts) [[ tests ](test/unit/algorithms/sorts.test.ts)] — Time: O(n^2) worst, O(n*log(n)) avg, O(n*log(n)) best. Memory: O(1) worst.
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
# Iterators
|
|
66
|
+
|
|
67
|
+
### Interfaces
|
|
68
|
+
[IIterable](src/types/IIterable.ts) — Allows to create an iterator instance.
|
|
69
|
+
|
|
70
|
+
[IBiDirectIterable](src/types/IBiDirectIterable.ts) — Allows to create a bi-direct iterator instance.
|
|
71
|
+
Extends [IIterable](src/types/IIterable.ts) interface.
|
|
72
|
+
|
|
73
|
+
[IIterator](src/types/IIterator.ts) — Allows only next navigation.
|
|
74
|
+
|
|
75
|
+
[IBiDirectIterator](src/types/IBiDirectIterator.ts) — Allows both next and prev navigation.
|
|
76
|
+
Extends [IIterator](src/types/IIterator.ts) interface.
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
# Linear data structures
|
|
80
|
+
|
|
81
|
+
### Interfaces
|
|
82
|
+
[ILinearStorage](src/types/ILinearStorage.ts) — Contains common methods of linear data structures.
|
|
83
|
+
|
|
84
|
+
[ILinearStorageRA](src/types/ILinearStorageRA.ts) — Allows random access (from end, from start, by index).
|
|
85
|
+
Extends [ILinearStorage](src/types/ILinearStorage.ts) interface.
|
|
86
|
+
|
|
87
|
+
[IConvertableToArray](src/types/IConvertableToArray.ts) — Contain methods for converting from/into array.
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
## Linked List
|
|
91
|
+
|
|
92
|
+
### Interfaces
|
|
93
|
+
[ILinkedList](src/types/ILinkedList.ts) — Contains basic linked lists operations.
|
|
94
|
+
Extends [ILinearStorageRA](src/types/ILinearStorageRA.ts) and [IConvertableToArray](src/types/IConvertableToArray.ts) interface.
|
|
95
|
+
|
|
96
|
+
### Implementation
|
|
97
|
+
[AbstractLinkedList](src/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedList.ts) — Common logic for both single and double linked lists.
|
|
98
|
+
Implements [ILinearStorageRA](src/types/ILinearStorageRA.ts) interface.
|
|
99
|
+
|
|
100
|
+
[SingleLinkedList](src/data-structures/LinkedList/SingleLinkedList/SingleLinkedList.ts)
|
|
101
|
+
[ [ tests ] ](test/unit/data-structures/linked-list/linked-list.test.ts)
|
|
102
|
+
— Extends abstract linked list with implementation of one-way linking.
|
|
103
|
+
Implements [IIterable](src/types/IIterable.ts) interface.
|
|
104
|
+
|
|
105
|
+
[DoubleLinkedList](src/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedList.ts)
|
|
106
|
+
[ [ tests ] ](test/unit/data-structures/linked-list/linked-list.test.ts)
|
|
107
|
+
— Extends abstract linked list with implementation of two-way linking.
|
|
108
|
+
Implements [IBiDirectIterable](src/types/IBiDirectIterable.ts) interface.
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
## Looped Array
|
|
112
|
+
|
|
113
|
+
### Interfaces
|
|
114
|
+
[IArrayFacade](src/types/IArrayFacade.ts) — Contains basic array operations.
|
|
115
|
+
Extends [ILinearStorageRA](src/types/ILinearStorageRA.ts) interface.
|
|
116
|
+
Extends [IConvertableToArray](src/types/IConvertableToArray.ts) interface.
|
|
117
|
+
|
|
118
|
+
### Implementation
|
|
119
|
+
[LoopedArray](src/data-structures/LoopedArray/LoopedArray.ts)[ [ tests ]](test/unit/data-structures/looped-array/looped-array.test.ts)
|
|
120
|
+
— Overwrites data on capacity overflow.
|
|
121
|
+
|
|
122
|
+
## Stack
|
|
123
|
+
|
|
124
|
+
### Implementation
|
|
125
|
+
[Stack](src/data-structures/Stack/Stack.ts) [[ tests ](test/unit/data-structures/stack/stack.test.ts)]
|
|
126
|
+
— LIFO data structure. Implements [ILinearStorage](src/types/ILinearStorage.ts) interface.
|
|
127
|
+
|
|
128
|
+
## Queue
|
|
129
|
+
|
|
130
|
+
### Implementation
|
|
131
|
+
[Queue](src/data-structures/Queue/Queue.ts) [[ tests ](test/unit/data-structures/queue/queue.test.ts)]
|
|
132
|
+
— FIFO data structure. Implements [ILinearStorage](src/types/ILinearStorage.ts) interface.
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
# Non linear data structures
|
|
137
|
+
|
|
138
|
+
## Graph
|
|
139
|
+
### Interfaces
|
|
140
|
+
[IGraph](src/types/IGraph.ts) — Contains basic graph operations.
|
|
141
|
+
|
|
142
|
+
[IGraphIterator](src/types/IGraphIterator.ts) — Extends default iterator with init and getPath methods.
|
|
143
|
+
|
|
144
|
+
[IGraphIterationStrategy](src/types/IGraphIterationStrategy.ts) — Iteration strategies which are used in shortest-path, has-path.
|
|
145
|
+
|
|
146
|
+
### Implementation
|
|
147
|
+
[GraphEdge](src/data-structures/Graph/GraphEdge.ts) — Contains from/to links and edge weight.
|
|
148
|
+
|
|
149
|
+
[AbstractGraph](src/data-structures/Graph/AbstractGraph.ts) — Common logic for both directed and undirected graphs.
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
[DirectedGraph](src/data-structures/Graph/DirectedGraph.ts) [ [ tests ] ](test/unit/data-structures/graph/graph.test.ts)
|
|
153
|
+
— In case of directed graph A->B and B->A edges are not the same.
|
|
154
|
+
|
|
155
|
+
[UndirectedGraph](src/data-structures/Graph/UndirectedGraph.ts) [ [ tests ] ](test/unit/data-structures/graph/graph.test.ts)
|
|
156
|
+
— In case of undirected graph A->B and B->A are equal.
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
### Graph Iterators
|
|
160
|
+
|
|
161
|
+
[BreadthFirstSearchIterator](src/data-structures/Graph/iterator/GraphIteratorBFS.ts)
|
|
162
|
+
— Traversal method for unweighted graphs, built on queue.
|
|
163
|
+
|
|
164
|
+
[DepthFirstSearchIterator](src/data-structures/Graph/iterator/GraphIteratorDFS.ts)
|
|
165
|
+
— Traversal method for unweighted graphs, built on stack.
|
|
166
|
+
|
|
167
|
+
[DijkstraMethodIterator](src/data-structures/Graph/iterator/GraphIteratorDijkstra.ts)
|
|
168
|
+
— Traversal method for weighted graphs, built on finding the minimal cost.
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
### Graph Presenter
|
|
172
|
+
[presenter-adjacency-lists](src/data-structures/Graph/presenter/presenterAdjacencyLists.ts) [[ tests ](test/unit/data-structures/graph/graph.presenter.lists.test.ts)]
|
|
173
|
+
— Representation of graph as an adjacency list (using Map).
|
|
174
|
+
|
|
175
|
+
[presenter-adjacency-matrix](src/data-structures/Graph/presenter/presenterAdjacencyMatrix.ts) [[ tests ](test/unit/data-structures/graph/graph.presenter.matrix.test.ts)]
|
|
176
|
+
— Representation of graph as an adjacency matrix (using Array N*N).
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
### Graph Searching
|
|
180
|
+
[has-path (BFS/DFS)](src/data-structures/Graph/searching/hasPath.ts) [[ tests ](test/unit/data-structures/graph/graph.has-path.test.ts)]
|
|
181
|
+
— Search for the existence of a path between two vertices.
|
|
182
|
+
|
|
183
|
+
[shortest-path (BFS/Dijkstra)](src/data-structures/Graph/searching/shortestPath.ts) [[ tests ](test/unit/data-structures/graph/graph.shortest-path.test.ts)]
|
|
184
|
+
— Search for one of several shortest paths between two vertices.
|
|
185
|
+
|
|
186
|
+
### Graph Creators
|
|
187
|
+
[create-graph-from-matrix](src/helpers/createGraphFromMatrix.ts) [[ tests ](test/unit/data-structures/graph/graph.create-from-matrix.test.ts)]
|
|
188
|
+
— Convert a matrix N*N into a graph instance.
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
### Graph Transposing
|
|
192
|
+
[transpose-directed-graph](src/data-structures/Graph/transposing/transposeDirectedGraph.ts) [ [ tests ](test/unit/data-structures/graph/graph.transpose.test.ts)]
|
|
193
|
+
— Transpose a directed graph (undirected graphs are symmetrical already).
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
## Binary trees
|
|
198
|
+
[IBinaryTree](src/types/IBinaryTree.ts) — Contains basic binary tree operations.
|
|
199
|
+
|
|
200
|
+
### Implementation
|
|
201
|
+
|
|
202
|
+
[AbstractBinaryNode](src/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryNode.ts) — Contains left/right/parent links and node data.
|
|
203
|
+
|
|
204
|
+
[AbstractBinaryTree](src/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryTree.ts) — Common logic for all types of binary trees.
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
[BinarySearchNode](src/data-structures/BinaryTree/BinarySearchTree/BinarySearchNode.ts) — Same as abstract binary node.
|
|
208
|
+
|
|
209
|
+
[BinarySearchTree](src/data-structures/BinaryTree/BinarySearchTree/BinarySearchTree.ts) — Implementation of unbalanced binary search tree.
|
|
210
|
+
Each node in left subtree is smaller and each node in right subtree is larger than the node data.
|
|
211
|
+
Extends [AbstractSearchTree](src/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryTree.ts).
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
[RandBinarySearchNode](src/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchNode.ts) — Have a rank attribute.
|
|
216
|
+
Extends [BinarySearchNode](src/data-structures/BinaryTree/BinarySearchTree/BinarySearchNode.ts).
|
|
217
|
+
|
|
218
|
+
[RandBinarySearchTree](src/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchTree.ts)
|
|
219
|
+
— Implementation of randomized binary search tree, which gives expected log(N) height.
|
|
220
|
+
Insertion have a 1/N+1 probability of inserting into root.
|
|
221
|
+
Extends [BinarySearchTree](src/data-structures/BinaryTree/BinarySearchTree/BinarySearchTree.ts).
|
package/jest.config.js
ADDED
package/nodemon.json
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@raikuxq/alg-ds",
|
|
3
|
+
"scripts": {
|
|
4
|
+
"test": "jest",
|
|
5
|
+
"dev": "nodemon",
|
|
6
|
+
"build": "rimraf ./build && tsc",
|
|
7
|
+
"start": "npm run build && node build/main.js",
|
|
8
|
+
"lint": "yarn eslint . --ext .ts",
|
|
9
|
+
"lint:fix": "yarn eslint --fix . --ext .ts"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": "./src/exports/main.ts",
|
|
13
|
+
"algorithms": "./src/exports/algorithms.ts",
|
|
14
|
+
"sorts": "./src/exports/algorithms/sorts.ts",
|
|
15
|
+
"types": "./src/exports/types.ts",
|
|
16
|
+
"utils": "./src/exports/utils.ts",
|
|
17
|
+
"constants": "./src/exports/constants.ts",
|
|
18
|
+
"helpers": "./src/exports/helpers.ts"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/raikuxq/lab-ts-algorithms"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/jest": "^26.0.14",
|
|
26
|
+
"@types/node": "^14.0.24",
|
|
27
|
+
"@typescript-eslint/eslint-plugin": "^4.4.0",
|
|
28
|
+
"@typescript-eslint/parser": "^4.4.0",
|
|
29
|
+
"eslint": "^7.11.0",
|
|
30
|
+
"eslint-config-prettier": "^6.12.0",
|
|
31
|
+
"eslint-config-recommended": "^4.1.0",
|
|
32
|
+
"eslint-plugin-prettier": "^3.1.4",
|
|
33
|
+
"jest": "^26.1.0",
|
|
34
|
+
"nodemon": "^2.0.4",
|
|
35
|
+
"prettier": "^2.1.2",
|
|
36
|
+
"rimraf": "^3.0.2",
|
|
37
|
+
"ts-jest": "^26.4.1",
|
|
38
|
+
"ts-node": "^8.10.2",
|
|
39
|
+
"typescript": "^4.0.3"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {},
|
|
42
|
+
"version": "1.0.0"
|
|
43
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Find element's index in sorted array
|
|
3
|
+
* Time complexity: O(log(n))
|
|
4
|
+
*/
|
|
5
|
+
export const binarySearch = (
|
|
6
|
+
elements: Array<number>,
|
|
7
|
+
searchElement: number
|
|
8
|
+
): number | null => {
|
|
9
|
+
const length: number = elements.length;
|
|
10
|
+
|
|
11
|
+
let leftIndex = 0;
|
|
12
|
+
let rightIndex: number = length - 1;
|
|
13
|
+
|
|
14
|
+
while (leftIndex <= rightIndex) {
|
|
15
|
+
const midIndex: number = Math.ceil((leftIndex + rightIndex) / 2);
|
|
16
|
+
const midEl: number = elements[midIndex];
|
|
17
|
+
|
|
18
|
+
if (searchElement == midEl) {
|
|
19
|
+
return midIndex;
|
|
20
|
+
} else if (midEl > searchElement) {
|
|
21
|
+
rightIndex = midIndex - 1;
|
|
22
|
+
} else if (midEl < searchElement) {
|
|
23
|
+
leftIndex = midIndex + 1;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return null;
|
|
28
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { memoize } from "./memoize";
|
|
2
|
+
import { FnToMemoize } from "../types/FnToMemoize";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Time complexity: O(n!)
|
|
6
|
+
*/
|
|
7
|
+
export const factorial = (x: number): number => {
|
|
8
|
+
return x > 1 ? x * factorial(x - 1) : x;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Time complexity: O(n) - due to caching
|
|
13
|
+
*/
|
|
14
|
+
export const memoizedFactorial: FnToMemoize<number, number> = memoize(
|
|
15
|
+
(n: number) => {
|
|
16
|
+
return n > 1 ? n * memoizedFactorial(n - 1) : n;
|
|
17
|
+
}
|
|
18
|
+
);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { FnToMemoize } from "../types/FnToMemoize";
|
|
2
|
+
import { memoize } from "./memoize";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Time complexity: O(n!)
|
|
6
|
+
*/
|
|
7
|
+
export const fibonacci = (n: number): number => {
|
|
8
|
+
return n > 1 ? fibonacci(n - 1) + fibonacci(n - 2) : n;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Time complexity: O(n) - due to caching
|
|
13
|
+
*/
|
|
14
|
+
export const memoizedFibonacci: FnToMemoize<number, number> = memoize(
|
|
15
|
+
(n: number) => {
|
|
16
|
+
return n > 1 ? memoizedFibonacci(n - 1) + memoizedFibonacci(n - 2) : n;
|
|
17
|
+
}
|
|
18
|
+
);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { FnToMemoize } from "../types/FnToMemoize";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Wrapper function that storing the results of calls and returning the cached result when the same inputs occur again
|
|
5
|
+
*/
|
|
6
|
+
export const memoize = <Key, Value>(
|
|
7
|
+
fn: FnToMemoize<Key, Value>
|
|
8
|
+
): FnToMemoize<Key, Value> => {
|
|
9
|
+
const cache = new Map<string, Value>();
|
|
10
|
+
|
|
11
|
+
return (...args: Array<Key>): Value => {
|
|
12
|
+
const jsonArgs = JSON.stringify(args);
|
|
13
|
+
|
|
14
|
+
if (!cache.has(jsonArgs)) {
|
|
15
|
+
const result = fn(...args);
|
|
16
|
+
cache.set(jsonArgs, result);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return <Value>cache.get(jsonArgs);
|
|
20
|
+
};
|
|
21
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { swapArrayItems } from "../../utils";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Bubble sorting algorithm
|
|
5
|
+
*
|
|
6
|
+
* @description
|
|
7
|
+
* Time complexity: Best O(n); Avg O(n \^ 2); Worst O(n \^ 2)
|
|
8
|
+
* @description
|
|
9
|
+
* Memory complexity: Worst case: O(1)
|
|
10
|
+
*/
|
|
11
|
+
export const bubbleSort = (arr: Array<number>): Array<number> => {
|
|
12
|
+
for (let i = 0; i < arr.length - 1; i++) {
|
|
13
|
+
for (let j = 0; j < arr.length - 1; j++) {
|
|
14
|
+
if (arr[j] > arr[j + 1]) {
|
|
15
|
+
swapArrayItems(arr, j, j + 1);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return arr;
|
|
21
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { swapArrayItems } from "../../utils";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Insertion sorting algorithm
|
|
5
|
+
*
|
|
6
|
+
* @description
|
|
7
|
+
* Time complexity: Best O(n); Avg O(n \^ 2); Worst O(n \^ 2);
|
|
8
|
+
* @description
|
|
9
|
+
* Memory complexity: Worst case: O(1)
|
|
10
|
+
*/
|
|
11
|
+
export const insertionSort = (arr: Array<number>): Array<number> => {
|
|
12
|
+
for (let i = 1; i < arr.length; i++) {
|
|
13
|
+
const current = arr[i];
|
|
14
|
+
let j = i - 1;
|
|
15
|
+
|
|
16
|
+
while (j >= 0 && arr[j] > current) {
|
|
17
|
+
swapArrayItems(arr, j + 1, j);
|
|
18
|
+
j--;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
arr[j + 1] = current;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return arr;
|
|
25
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Merge two sorted arrays into one array
|
|
3
|
+
*/
|
|
4
|
+
const merge = (
|
|
5
|
+
arr: Array<number>,
|
|
6
|
+
leftIndex: number,
|
|
7
|
+
midIndex: number,
|
|
8
|
+
rightIndex: number
|
|
9
|
+
) => {
|
|
10
|
+
const container = arr.slice(leftIndex, rightIndex + 1);
|
|
11
|
+
|
|
12
|
+
let resultArrIndex = leftIndex;
|
|
13
|
+
let leftHalfIndex = leftIndex;
|
|
14
|
+
let rightHalfIndex = midIndex + 1;
|
|
15
|
+
|
|
16
|
+
/** While both halves of array are not ended */
|
|
17
|
+
while (leftHalfIndex <= midIndex && rightHalfIndex <= rightIndex) {
|
|
18
|
+
const leftHalfElement = container[leftHalfIndex - leftIndex];
|
|
19
|
+
const rightHalfElement = container[rightHalfIndex - leftIndex];
|
|
20
|
+
|
|
21
|
+
if (leftHalfElement < rightHalfElement) {
|
|
22
|
+
arr[resultArrIndex] = leftHalfElement;
|
|
23
|
+
leftHalfIndex++;
|
|
24
|
+
} else {
|
|
25
|
+
arr[resultArrIndex] = rightHalfElement;
|
|
26
|
+
rightHalfIndex++;
|
|
27
|
+
}
|
|
28
|
+
resultArrIndex++;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** If one of halves is ended, the remaining one will just be pushed to result */
|
|
32
|
+
while (leftHalfIndex <= midIndex) {
|
|
33
|
+
arr[resultArrIndex] = container[leftHalfIndex - leftIndex];
|
|
34
|
+
resultArrIndex++;
|
|
35
|
+
leftHalfIndex++;
|
|
36
|
+
}
|
|
37
|
+
while (rightHalfIndex <= rightIndex) {
|
|
38
|
+
arr[resultArrIndex] = container[rightHalfIndex - leftIndex];
|
|
39
|
+
resultArrIndex++;
|
|
40
|
+
rightHalfIndex++;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Divide array into 2 halves and merge them
|
|
46
|
+
*/
|
|
47
|
+
const sortRange = (
|
|
48
|
+
arr: Array<number>,
|
|
49
|
+
leftIndex: number,
|
|
50
|
+
rightIndex: number
|
|
51
|
+
) => {
|
|
52
|
+
if (rightIndex > leftIndex) {
|
|
53
|
+
const midIndex = Math.floor(leftIndex + (rightIndex - leftIndex) / 2);
|
|
54
|
+
|
|
55
|
+
sortRange(arr, leftIndex, midIndex);
|
|
56
|
+
sortRange(arr, midIndex + 1, rightIndex);
|
|
57
|
+
|
|
58
|
+
merge(arr, leftIndex, midIndex, rightIndex);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Merge sorting algorithm
|
|
64
|
+
*
|
|
65
|
+
* @description
|
|
66
|
+
* Time complexity: Best O(n * log(n)); Avg O(n * log(n)); Worst O(n * log(n))
|
|
67
|
+
* @description
|
|
68
|
+
* Memory complexity: Worst case: O(n)
|
|
69
|
+
*/
|
|
70
|
+
export const mergeSort = (arr: Array<number>): Array<number> => {
|
|
71
|
+
sortRange(arr, 0, arr.length - 1);
|
|
72
|
+
|
|
73
|
+
return arr;
|
|
74
|
+
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { swapArrayItems } from "../../utils";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Quick sort algorithm
|
|
5
|
+
*
|
|
6
|
+
* @description
|
|
7
|
+
* Time complexity: Best O(n * log(n)); Avg O(n * log(n)); Worst O(n ^2)
|
|
8
|
+
* @description
|
|
9
|
+
* Memory complexity: Worst case: O(1)
|
|
10
|
+
*/
|
|
11
|
+
export const quickSort = (arr: Array<number>): Array<number> => {
|
|
12
|
+
const partition = (
|
|
13
|
+
arr: Array<number>,
|
|
14
|
+
leftIndex: number,
|
|
15
|
+
rightIndex: number
|
|
16
|
+
): number => {
|
|
17
|
+
const pivot = arr[leftIndex];
|
|
18
|
+
|
|
19
|
+
let leftWall = leftIndex;
|
|
20
|
+
let rightWall = rightIndex;
|
|
21
|
+
|
|
22
|
+
while (leftWall <= rightWall) {
|
|
23
|
+
while (arr[rightWall] > pivot) {
|
|
24
|
+
rightWall--;
|
|
25
|
+
}
|
|
26
|
+
while (arr[leftWall] < pivot) {
|
|
27
|
+
leftWall++;
|
|
28
|
+
}
|
|
29
|
+
if (leftWall <= rightWall) {
|
|
30
|
+
swapArrayItems(arr, leftWall, rightWall);
|
|
31
|
+
rightWall--;
|
|
32
|
+
leftWall++;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return leftWall;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const sort = (
|
|
40
|
+
arr: Array<number>,
|
|
41
|
+
leftIndex = 0,
|
|
42
|
+
rightIndex: number = arr.length - 1
|
|
43
|
+
): Array<number> => {
|
|
44
|
+
if (leftIndex < rightIndex) {
|
|
45
|
+
const pivot = partition(arr, leftIndex, rightIndex);
|
|
46
|
+
|
|
47
|
+
sort(arr, leftIndex, pivot - 1);
|
|
48
|
+
sort(arr, pivot, rightIndex);
|
|
49
|
+
}
|
|
50
|
+
return arr;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
return sort(arr);
|
|
54
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { swapArrayItems, getMinIndexFromIndex } from "../../utils";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Selection sorting algorithm
|
|
5
|
+
*
|
|
6
|
+
* @description
|
|
7
|
+
* Time complexity: Best O(n \^ 2); Avg O(n \^ 2); Worst O(n \^ 2)
|
|
8
|
+
* @description
|
|
9
|
+
* Memory complexity: Worst case: O(1)
|
|
10
|
+
*/
|
|
11
|
+
export const selectSort = (arr: Array<number>): Array<number> => {
|
|
12
|
+
for (let index = 0; index < arr.length; index++) {
|
|
13
|
+
const minIndex: number = getMinIndexFromIndex(arr, index);
|
|
14
|
+
|
|
15
|
+
swapArrayItems<number>(arr, minIndex, index);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return arr;
|
|
19
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ArrayMatrix } from "../types/ArrayMatrix";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Will flips a matrix over its diagonal
|
|
5
|
+
*/
|
|
6
|
+
export const transposeMatrix = (matrix: ArrayMatrix): ArrayMatrix => {
|
|
7
|
+
/** Validation */
|
|
8
|
+
matrix.forEach((subArray) => {
|
|
9
|
+
const checkIsSameLength = subArray.length === matrix.length;
|
|
10
|
+
if (!checkIsSameLength) {
|
|
11
|
+
throw new Error("Given array is not a matrix");
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
return matrix.reduce((acc, current, currentIndex) => {
|
|
16
|
+
acc[currentIndex] = matrix.map((rowArr) => rowArr[currentIndex]);
|
|
17
|
+
return acc;
|
|
18
|
+
}, new Array(matrix.length));
|
|
19
|
+
};
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export default abstract class AbstractBinaryNode<T> {
|
|
2
|
+
protected _data: T;
|
|
3
|
+
protected _left: AbstractBinaryNode<T> | null;
|
|
4
|
+
protected _right: AbstractBinaryNode<T> | null;
|
|
5
|
+
protected _parent: AbstractBinaryNode<T> | null;
|
|
6
|
+
|
|
7
|
+
protected constructor(initialData: T) {
|
|
8
|
+
this._data = initialData;
|
|
9
|
+
this._left = null;
|
|
10
|
+
this._right = null;
|
|
11
|
+
this._parent = null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public get data(): T {
|
|
15
|
+
return this._data;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public set data(value: T) {
|
|
19
|
+
this._data = value;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public get left(): AbstractBinaryNode<T> | null {
|
|
23
|
+
return this._left;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public set left(value: AbstractBinaryNode<T> | null) {
|
|
27
|
+
this._left = value;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public get right(): AbstractBinaryNode<T> | null {
|
|
31
|
+
return this._right;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public set right(value: AbstractBinaryNode<T> | null) {
|
|
35
|
+
this._right = value;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public get parent(): AbstractBinaryNode<T> | null {
|
|
39
|
+
return this._parent;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public set parent(value: AbstractBinaryNode<T> | null) {
|
|
43
|
+
this._parent = value;
|
|
44
|
+
}
|
|
45
|
+
}
|