@raikuxq/alg-ds 1.1.2 → 1.1.5

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 (257) hide show
  1. package/README.md +26 -2
  2. package/lib/algorithms/binary-search.d.ts +5 -0
  3. package/lib/algorithms/binary-search.js +27 -0
  4. package/lib/algorithms/factorial.d.ts +9 -0
  5. package/lib/algorithms/factorial.js +17 -0
  6. package/lib/algorithms/fibonacci.d.ts +9 -0
  7. package/lib/algorithms/fibonacci.js +17 -0
  8. package/lib/algorithms/memoize.d.ts +5 -0
  9. package/lib/algorithms/memoize.js +22 -0
  10. package/lib/algorithms/sorts/bubble-sort.d.ts +9 -0
  11. package/lib/algorithms/sorts/bubble-sort.js +23 -0
  12. package/lib/algorithms/sorts/insertion-sort.d.ts +9 -0
  13. package/lib/algorithms/sorts/insertion-sort.js +25 -0
  14. package/lib/algorithms/sorts/merge-sort.d.ts +9 -0
  15. package/lib/algorithms/sorts/merge-sort.js +61 -0
  16. package/lib/algorithms/sorts/quick-sort.d.ts +9 -0
  17. package/lib/algorithms/sorts/quick-sort.js +45 -0
  18. package/lib/algorithms/sorts/select-sort.d.ts +9 -0
  19. package/lib/algorithms/sorts/select-sort.js +20 -0
  20. package/lib/algorithms/transpose-matrix.d.ts +5 -0
  21. package/lib/algorithms/transpose-matrix.js +20 -0
  22. package/lib/constants.d.ts +2 -0
  23. package/lib/constants.js +6 -0
  24. package/lib/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryNode.d.ts +15 -0
  25. package/lib/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryNode.js +53 -0
  26. package/lib/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryTree.d.ts +60 -0
  27. package/lib/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryTree.js +36 -0
  28. package/lib/data-structures/BinaryTree/BinarySearchTree/BinarySearchNode.d.ts +13 -0
  29. package/lib/data-structures/BinaryTree/BinarySearchTree/BinarySearchNode.js +59 -0
  30. package/lib/data-structures/BinaryTree/BinarySearchTree/BinarySearchTree.d.ts +70 -0
  31. package/lib/data-structures/BinaryTree/BinarySearchTree/BinarySearchTree.js +268 -0
  32. package/lib/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchNode.d.ts +16 -0
  33. package/lib/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchNode.js +70 -0
  34. package/lib/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchTree.d.ts +57 -0
  35. package/lib/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchTree.js +234 -0
  36. package/lib/data-structures/Graph/AbstractGraph.d.ts +84 -0
  37. package/lib/data-structures/Graph/AbstractGraph.js +141 -0
  38. package/lib/data-structures/Graph/DirectedGraph.d.ts +24 -0
  39. package/lib/data-structures/Graph/DirectedGraph.js +85 -0
  40. package/lib/data-structures/Graph/GraphEdge.d.ts +16 -0
  41. package/lib/data-structures/Graph/GraphEdge.js +43 -0
  42. package/lib/data-structures/Graph/UndirectedGraph.d.ts +28 -0
  43. package/lib/data-structures/Graph/UndirectedGraph.js +102 -0
  44. package/lib/data-structures/Graph/demo/generateRandomGraph.d.ts +4 -0
  45. package/lib/data-structures/Graph/demo/generateRandomGraph.js +72 -0
  46. package/lib/data-structures/Graph/iterator/AbstractGraphIterator.d.ts +35 -0
  47. package/lib/data-structures/Graph/iterator/AbstractGraphIterator.js +90 -0
  48. package/lib/data-structures/Graph/iterator/GraphIteratorBFS.d.ts +28 -0
  49. package/lib/data-structures/Graph/iterator/GraphIteratorBFS.js +70 -0
  50. package/lib/data-structures/Graph/iterator/GraphIteratorDFS.d.ts +28 -0
  51. package/lib/data-structures/Graph/iterator/GraphIteratorDFS.js +70 -0
  52. package/lib/data-structures/Graph/iterator/GraphIteratorDijkstra.d.ts +32 -0
  53. package/lib/data-structures/Graph/iterator/GraphIteratorDijkstra.js +99 -0
  54. package/lib/data-structures/Graph/presenter/presenterAdjacencyLists.d.ts +19 -0
  55. package/lib/data-structures/Graph/presenter/presenterAdjacencyLists.js +28 -0
  56. package/{src/data-structures/Graph/presenter/presenterAdjacencyMatrix.ts → lib/data-structures/Graph/presenter/presenterAdjacencyMatrix.d.ts} +32 -51
  57. package/lib/data-structures/Graph/presenter/presenterAdjacencyMatrix.js +48 -0
  58. package/lib/data-structures/Graph/searching/hasPath.d.ts +9 -0
  59. package/lib/data-structures/Graph/searching/hasPath.js +29 -0
  60. package/lib/data-structures/Graph/searching/shortestPath.d.ts +9 -0
  61. package/lib/data-structures/Graph/searching/shortestPath.js +29 -0
  62. package/lib/data-structures/Graph/strategy/BFSIterationStrategy.d.ts +6 -0
  63. package/lib/data-structures/Graph/strategy/BFSIterationStrategy.js +13 -0
  64. package/lib/data-structures/Graph/strategy/DFSIterationStrategy.d.ts +6 -0
  65. package/lib/data-structures/Graph/strategy/DFSIterationStrategy.js +13 -0
  66. package/lib/data-structures/Graph/strategy/DijkstraIterationStrategy.d.ts +6 -0
  67. package/lib/data-structures/Graph/strategy/DijkstraIterationStrategy.js +13 -0
  68. package/lib/data-structures/Graph/transposing/transposeDirectedGraph.d.ts +2 -0
  69. package/lib/data-structures/Graph/transposing/transposeDirectedGraph.js +14 -0
  70. package/lib/data-structures/HashTable/HashTable.d.ts +73 -0
  71. package/lib/data-structures/HashTable/HashTable.js +169 -0
  72. package/lib/data-structures/HashTable/HashTableNode.d.ts +11 -0
  73. package/lib/data-structures/HashTable/HashTableNode.js +39 -0
  74. package/lib/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedList.d.ts +125 -0
  75. package/lib/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedList.js +236 -0
  76. package/lib/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedNode.d.ts +20 -0
  77. package/lib/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedNode.js +41 -0
  78. package/lib/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedList.d.ts +48 -0
  79. package/lib/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedList.js +150 -0
  80. package/lib/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedNode.d.ts +25 -0
  81. package/lib/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedNode.js +65 -0
  82. package/lib/data-structures/LinkedList/SingleLinkedList/SingleLinkedList.d.ts +52 -0
  83. package/lib/data-structures/LinkedList/SingleLinkedList/SingleLinkedList.js +137 -0
  84. package/{src/data-structures/LinkedList/SingleLinkedList/SingleLinkedNode.ts → lib/data-structures/LinkedList/SingleLinkedList/SingleLinkedNode.d.ts} +7 -10
  85. package/lib/data-structures/LinkedList/SingleLinkedList/SingleLinkedNode.js +29 -0
  86. package/lib/data-structures/LoopedArray/LoopedArray.d.ts +86 -0
  87. package/lib/data-structures/LoopedArray/LoopedArray.js +161 -0
  88. package/lib/data-structures/Queue/Queue.d.ts +50 -0
  89. package/lib/data-structures/Queue/Queue.js +83 -0
  90. package/lib/data-structures/Stack/Stack.d.ts +50 -0
  91. package/lib/data-structures/Stack/Stack.js +83 -0
  92. package/lib/exports/algorithms.d.ts +16 -0
  93. package/lib/exports/algorithms.js +36 -0
  94. package/lib/exports/constants.d.ts +2 -0
  95. package/lib/exports/constants.js +7 -0
  96. package/lib/exports/data-structures.d.ts +11 -0
  97. package/lib/exports/data-structures.js +24 -0
  98. package/lib/exports/helpers.d.ts +6 -0
  99. package/lib/exports/helpers.js +14 -0
  100. package/lib/exports/sorts.d.ts +6 -0
  101. package/lib/exports/sorts.js +14 -0
  102. package/lib/exports/utils.d.ts +3 -0
  103. package/lib/exports/utils.js +14 -0
  104. package/lib/exports.d.ts +44 -0
  105. package/lib/exports.js +89 -0
  106. package/lib/helpers/createBinaryTree.d.ts +6 -0
  107. package/lib/helpers/createBinaryTree.js +22 -0
  108. package/lib/helpers/createGraph.d.ts +6 -0
  109. package/lib/helpers/createGraph.js +24 -0
  110. package/lib/helpers/createGraphFromMatrix.d.ts +7 -0
  111. package/lib/helpers/createGraphFromMatrix.js +37 -0
  112. package/lib/helpers/createLinkedList.d.ts +3 -0
  113. package/lib/helpers/createLinkedList.js +21 -0
  114. package/lib/index.d.ts +3 -0
  115. package/lib/index.js +6 -0
  116. package/lib/types/ArrayMatrix.d.ts +1 -0
  117. package/lib/types/ArrayMatrix.js +3 -0
  118. package/lib/types/EnumBinarySearchTreeType.d.ts +4 -0
  119. package/lib/types/EnumBinarySearchTreeType.js +9 -0
  120. package/lib/types/EnumGraphType.d.ts +4 -0
  121. package/lib/types/EnumGraphType.js +9 -0
  122. package/lib/types/EnumLinkedListType.d.ts +4 -0
  123. package/lib/types/EnumLinkedListType.js +9 -0
  124. package/lib/types/EnumRandomGenerationFormat.d.ts +4 -0
  125. package/lib/types/EnumRandomGenerationFormat.js +9 -0
  126. package/lib/types/EnumTreeTraversalType.d.ts +5 -0
  127. package/lib/types/EnumTreeTraversalType.js +10 -0
  128. package/lib/types/FnCompareTwo.d.ts +1 -0
  129. package/lib/types/FnCompareTwo.js +3 -0
  130. package/lib/types/FnToMemoize.d.ts +1 -0
  131. package/lib/types/FnToMemoize.js +3 -0
  132. package/{src/types/ILinkedList.ts → lib/types/IArrayFacade.d.ts} +4 -6
  133. package/lib/types/IArrayFacade.js +3 -0
  134. package/{src/types/IBiDirectIterable.ts → lib/types/IBiDirectIterable.d.ts} +5 -6
  135. package/lib/types/IBiDirectIterable.js +3 -0
  136. package/lib/types/IBiDirectIterator.d.ts +11 -0
  137. package/lib/types/IBiDirectIterator.js +3 -0
  138. package/lib/types/IBinaryTree.d.ts +12 -0
  139. package/lib/types/IBinaryTree.js +3 -0
  140. package/lib/types/IConvertableToArray.d.ts +4 -0
  141. package/lib/types/IConvertableToArray.js +3 -0
  142. package/lib/types/IGraph.d.ts +14 -0
  143. package/lib/types/IGraph.js +3 -0
  144. package/{src/types/IGraphIterationStrategy.ts → lib/types/IGraphIterationStrategy.d.ts} +5 -6
  145. package/lib/types/IGraphIterationStrategy.js +3 -0
  146. package/lib/types/IGraphIterator.d.ts +11 -0
  147. package/lib/types/IGraphIterator.js +3 -0
  148. package/{src/types/IIterable.ts → lib/types/IIterable.d.ts} +4 -5
  149. package/lib/types/IIterable.js +3 -0
  150. package/lib/types/IIterator.d.ts +14 -0
  151. package/lib/types/IIterator.js +3 -0
  152. package/lib/types/IKeyValueStorage.d.ts +8 -0
  153. package/lib/types/IKeyValueStorage.js +3 -0
  154. package/lib/types/ILinearStorage.d.ts +11 -0
  155. package/lib/types/ILinearStorage.js +3 -0
  156. package/{src/types/ILinearStorageRA.ts → lib/types/ILinearStorageRA.d.ts} +13 -14
  157. package/lib/types/ILinearStorageRA.js +3 -0
  158. package/{src/types/IArrayFacade.ts → lib/types/ILinkedList.d.ts} +4 -6
  159. package/lib/types/ILinkedList.js +3 -0
  160. package/lib/utils.d.ts +29 -0
  161. package/lib/utils.js +95 -0
  162. package/package.json +9 -3
  163. package/.idea/algorythmes.iml +0 -15
  164. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  165. package/.idea/deployment.xml +0 -14
  166. package/.idea/inspectionProfiles/Project_Default.xml +0 -7
  167. package/.idea/jsLinters/eslint.xml +0 -6
  168. package/.idea/misc.xml +0 -6
  169. package/.idea/modules.xml +0 -8
  170. package/.idea/vcs.xml +0 -6
  171. package/lib/algotirhms.ts +0 -35
  172. package/lib/constants.ts +0 -3
  173. package/lib/data-structures.ts +0 -23
  174. package/lib/helpers.ts +0 -13
  175. package/lib/sorts.ts +0 -7
  176. package/lib/types.ts +0 -53
  177. package/lib/utils.ts +0 -21
  178. package/src/algorithms/binary-search.ts +0 -28
  179. package/src/algorithms/factorial.ts +0 -18
  180. package/src/algorithms/fibonacci.ts +0 -18
  181. package/src/algorithms/memoize.ts +0 -21
  182. package/src/algorithms/sorts/bubble-sort.ts +0 -21
  183. package/src/algorithms/sorts/insertion-sort.ts +0 -25
  184. package/src/algorithms/sorts/merge-sort.ts +0 -74
  185. package/src/algorithms/sorts/quick-sort.ts +0 -54
  186. package/src/algorithms/sorts/select-sort.ts +0 -19
  187. package/src/algorithms/transpose-matrix.ts +0 -19
  188. package/src/constants.ts +0 -2
  189. package/src/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryNode.ts +0 -45
  190. package/src/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryTree.ts +0 -80
  191. package/src/data-structures/BinaryTree/BinarySearchTree/BinarySearchNode.ts +0 -38
  192. package/src/data-structures/BinaryTree/BinarySearchTree/BinarySearchTree.ts +0 -286
  193. package/src/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchNode.ts +0 -48
  194. package/src/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchTree.ts +0 -228
  195. package/src/data-structures/Graph/AbstractGraph.ts +0 -189
  196. package/src/data-structures/Graph/DirectedGraph.ts +0 -84
  197. package/src/data-structures/Graph/GraphEdge.ts +0 -33
  198. package/src/data-structures/Graph/UndirectedGraph.ts +0 -108
  199. package/src/data-structures/Graph/demo/generateRandomGraph.ts +0 -93
  200. package/src/data-structures/Graph/iterator/AbstractGraphIterator.ts +0 -99
  201. package/src/data-structures/Graph/iterator/GraphIteratorBFS.ts +0 -60
  202. package/src/data-structures/Graph/iterator/GraphIteratorDFS.ts +0 -60
  203. package/src/data-structures/Graph/iterator/GraphIteratorDijkstra.ts +0 -94
  204. package/src/data-structures/Graph/presenter/presenterAdjacencyLists.ts +0 -29
  205. package/src/data-structures/Graph/searching/hasPath.ts +0 -38
  206. package/src/data-structures/Graph/searching/shortestPath.ts +0 -38
  207. package/src/data-structures/Graph/strategy/BFSIterationStrategy.ts +0 -11
  208. package/src/data-structures/Graph/strategy/DFSIterationStrategy.ts +0 -11
  209. package/src/data-structures/Graph/strategy/DijkstraIterationStrategy.ts +0 -11
  210. package/src/data-structures/Graph/transposing/transposeDirectedGraph.ts +0 -19
  211. package/src/data-structures/HashTable/HashTable.ts +0 -202
  212. package/src/data-structures/HashTable/HashTableNode.ts +0 -31
  213. package/src/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedList.ts +0 -310
  214. package/src/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedNode.ts +0 -33
  215. package/src/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedList.ts +0 -156
  216. package/src/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedNode.ts +0 -47
  217. package/src/data-structures/LinkedList/SingleLinkedList/SingleLinkedList.ts +0 -147
  218. package/src/data-structures/LoopedArray/LoopedArray.ts +0 -182
  219. package/src/data-structures/Queue/Queue.ts +0 -92
  220. package/src/data-structures/Stack/Stack.ts +0 -92
  221. package/src/demo/demo.bst.ts +0 -67
  222. package/src/demo/demo.graph.ts +0 -246
  223. package/src/demo/demo.hashtable.ts +0 -28
  224. package/src/demo/demo.linked-list.ts +0 -78
  225. package/src/demo/demo.looped-array.ts +0 -104
  226. package/src/demo/demo.queue.ts +0 -40
  227. package/src/demo/demo.stack.ts +0 -40
  228. package/src/demo/performance/bst-compare.ts +0 -35
  229. package/src/demo/performance/ds-compare.ts +0 -58
  230. package/src/demo/performance/hash-table.compare.ts +0 -40
  231. package/src/demo/performance/sort-compare.ts +0 -60
  232. package/src/helpers/createBinaryTree.ts +0 -24
  233. package/src/helpers/createGraph.ts +0 -24
  234. package/src/helpers/createGraphFromMatrix.ts +0 -47
  235. package/src/helpers/createLinkedList.ts +0 -24
  236. package/src/index.ts +0 -44
  237. package/src/types/ArrayMatrix.ts +0 -1
  238. package/src/types/EnumBinarySearchTreeType.ts +0 -4
  239. package/src/types/EnumGraphTraversalType.ts +0 -5
  240. package/src/types/EnumGraphType.ts +0 -4
  241. package/src/types/EnumLinkedListType.ts +0 -4
  242. package/src/types/EnumRandomGenerationFormat.ts +0 -4
  243. package/src/types/EnumSortType.ts +0 -7
  244. package/src/types/EnumTreeTraversalType.ts +0 -5
  245. package/src/types/FnCompareTwo.ts +0 -1
  246. package/src/types/FnSort.ts +0 -1
  247. package/src/types/FnToMemoize.ts +0 -1
  248. package/src/types/IBiDirectIterator.ts +0 -12
  249. package/src/types/IBinaryTree.ts +0 -13
  250. package/src/types/IConvertableToArray.ts +0 -4
  251. package/src/types/IGraph.ts +0 -16
  252. package/src/types/IGraphCreator.ts +0 -5
  253. package/src/types/IGraphIterator.ts +0 -13
  254. package/src/types/IIterator.ts +0 -14
  255. package/src/types/IKeyValueStorage.ts +0 -8
  256. package/src/types/ILinearStorage.ts +0 -11
  257. package/src/utils.ts +0 -65
@@ -1,182 +0,0 @@
1
- import IArrayFacade from "../../types/IArrayFacade";
2
-
3
- /**
4
- * Linear data structure
5
- * Facade above array
6
- * After reaching full array new pushed elements will be overwritten over old elements
7
- */
8
- export default class LoopedArray<T> implements IArrayFacade<T> {
9
- private readonly _capacity: number;
10
- private _realLength = 0;
11
- private _array: Array<T>;
12
-
13
- /**
14
- * Create empty instance
15
- */
16
- constructor(capacity: number) {
17
- if (capacity <= 0) {
18
- throw new Error("Capacity must be larger than 0");
19
- }
20
- this._capacity = capacity;
21
- this._array = new Array<T>(0);
22
- }
23
-
24
- /**
25
- * Push into end
26
- */
27
- public push(value: T): void {
28
- if (this._realLength % this._capacity === 0) {
29
- this._array = new Array<T>(0);
30
- }
31
-
32
- this._realLength++;
33
-
34
- if (!this.isFull()) {
35
- this._array.push(value);
36
- } else {
37
- const indexToPush = (this._realLength % this._capacity) - 1;
38
- this._array.splice(indexToPush, 1, value);
39
- }
40
- }
41
-
42
- /**
43
- * Push into start
44
- */
45
- public unshift(value: T): void {
46
- if (this._realLength % this._capacity === 0) {
47
- this._array = new Array<T>(0);
48
- }
49
- this._realLength++;
50
-
51
- if (!this.isFull()) {
52
- this._array.unshift(value);
53
- } else {
54
- this._array.splice(this._capacity - 1);
55
- this._array.unshift(value);
56
- }
57
- }
58
-
59
- /**
60
- * Delete node from array's end
61
- */
62
- public pop(): T {
63
- this._realLength--;
64
- const deletedItem = this._array.pop();
65
- if (deletedItem === undefined)
66
- throw new Error(
67
- "cannot delete last element because of it does not exists"
68
- );
69
- return deletedItem;
70
- }
71
-
72
- /**
73
- * Delete node from array's start
74
- */
75
- public shift(): T {
76
- this._realLength--;
77
- const deletedItem = this._array.shift();
78
- if (deletedItem === undefined)
79
- throw new Error(
80
- "cannot delete first element because of it does not exists"
81
- );
82
- return deletedItem;
83
- }
84
-
85
- /**
86
- * Get head element data
87
- * @throws Error when head does not exists
88
- */
89
- public peek(): T {
90
- return this._array[this._array.length - 1];
91
- }
92
-
93
- /**
94
- * Get tail element data
95
- * @throws Error when head does not exists
96
- */
97
- public peekFromStart(): T {
98
- return this._array[0];
99
- }
100
-
101
- /**
102
- * Get array element by index from start
103
- * @throws when element does not exists
104
- */
105
- peekByIndex(index: number): T {
106
- return this._array[index];
107
- }
108
-
109
- /**
110
- * Push from index
111
- */
112
- pushFromIndex(value: T, fromIndex: number): void {
113
- this._array[fromIndex] = value;
114
- }
115
-
116
- /**
117
- * Get elements as array
118
- */
119
- public getAsArray(): Array<T> {
120
- return this._array;
121
- }
122
-
123
- /**
124
- * Check if element exists in array
125
- */
126
- public has(item: T): boolean {
127
- return this._array.includes(item);
128
- }
129
-
130
- /**
131
- * Is array empty
132
- */
133
- public isEmpty(): boolean {
134
- return this._array.length === 0;
135
- }
136
-
137
- /**
138
- * Is array full
139
- */
140
- public isFull(): boolean {
141
- return this._array.length >= this._capacity;
142
- }
143
-
144
- /**
145
- * List length
146
- */
147
- public length(): number {
148
- return this._array.length;
149
- }
150
-
151
- /**
152
- * Remove all elements from array
153
- */
154
- public clear(): void {
155
- this._array = new Array<T>(0);
156
- }
157
-
158
- /**
159
- * Delete node from array by index from start
160
- */
161
- deleteFromIndex(fromIndex: number): T {
162
- const deletedElement = this._array[fromIndex];
163
- delete this._array[fromIndex];
164
- return deletedElement;
165
- }
166
-
167
- /**
168
- * Add elements to array from array
169
- * */
170
- pushFromArray(elements: Array<T>): void {
171
- elements.forEach((element: T) => {
172
- this.push(element);
173
- });
174
- }
175
-
176
- /**
177
- * Reverse array nodes links and swap head with tail
178
- */
179
- reverse(): void {
180
- this._array = this._array.reverse();
181
- }
182
- }
@@ -1,92 +0,0 @@
1
- import DoubleLinkedList from "../LinkedList/DoubleLinkedList/DoubleLinkedList";
2
- import ILinearStorage from "../../types/ILinearStorage";
3
- import ILinearStorageRA from "../../types/ILinearStorageRA";
4
-
5
- /**
6
- * FIFO data structure
7
- */
8
- export default class Queue<T> implements ILinearStorage<T> {
9
- private readonly _list: ILinearStorageRA<T>;
10
-
11
- /**
12
- * Create a queue instance
13
- */
14
- public constructor(capacity?: number) {
15
- this._list = new DoubleLinkedList<T>(capacity);
16
- }
17
-
18
- /**
19
- * Get first element in queue (without deleting)
20
- * @throws when list is empty
21
- */
22
- public peek(): T {
23
- if (this.isEmpty()) {
24
- throw new Error("Cannot peek when list is empty");
25
- }
26
- return this._list.peek();
27
- }
28
-
29
- /**
30
- * Add element to queue
31
- * @throws when list is full
32
- */
33
- public push(item: T): void {
34
- if (this._list.isFull()) {
35
- throw new Error("Cannot push when queue is full");
36
- }
37
- this._list.unshift(item);
38
- }
39
-
40
- /**
41
- * Delete first element in queue
42
- * @throws when list is empty
43
- */
44
- public pop(): T {
45
- if (this.isEmpty()) {
46
- throw new Error("Cannot pop when list is empty");
47
- }
48
- return this._list.pop();
49
- }
50
-
51
- /**
52
- * Check if element exists in list
53
- */
54
- public has(item: T): boolean {
55
- return this._list.has(item);
56
- }
57
-
58
- /**
59
- * Is queue empty
60
- */
61
- public isEmpty(): boolean {
62
- return this._list.isEmpty();
63
- }
64
-
65
- /**
66
- * Is stack full
67
- */
68
- public isFull(): boolean {
69
- return this._list.isFull();
70
- }
71
-
72
- /**
73
- * Remove all elements in queue
74
- */
75
- public clear(): void {
76
- this._list.clear();
77
- }
78
-
79
- /**
80
- * Queue length
81
- */
82
- public length(): number {
83
- return this._list.length();
84
- }
85
-
86
- /**
87
- * Reverse queue
88
- */
89
- public reverse(): void {
90
- this._list.reverse();
91
- }
92
- }
@@ -1,92 +0,0 @@
1
- import DoubleLinkedList from "../LinkedList/DoubleLinkedList/DoubleLinkedList";
2
- import ILinearStorage from "../../types/ILinearStorage";
3
- import ILinearStorageRA from "../../types/ILinearStorageRA";
4
-
5
- /**
6
- * LIFO data structure
7
- */
8
- export default class Stack<T> implements ILinearStorage<T> {
9
- private readonly _list: ILinearStorageRA<T>;
10
-
11
- /**
12
- * Create a stack instance
13
- */
14
- public constructor(capacity?: number) {
15
- this._list = new DoubleLinkedList(capacity);
16
- }
17
-
18
- /**
19
- * Get stack top element
20
- * @throws when list is empty
21
- */
22
- public peek(): T {
23
- if (this.isEmpty()) {
24
- throw new Error("Cannot peek when list is empty");
25
- }
26
- return this._list.peek();
27
- }
28
-
29
- /**
30
- * Add element to stack head
31
- * @throws when list is full
32
- */
33
- public push(item: T): void {
34
- if (this.isFull()) {
35
- throw new Error("Stack is full");
36
- }
37
- this._list.push(item);
38
- }
39
-
40
- /**
41
- * Remove element from stack head
42
- * @throws when list is empty
43
- */
44
- public pop(): T {
45
- if (this.isEmpty()) {
46
- throw new Error("Cannot pop when stack is empty");
47
- }
48
- return this._list.pop();
49
- }
50
-
51
- /**
52
- * Check if element exists in list
53
- */
54
- public has(item: T): boolean {
55
- return this._list.has(item);
56
- }
57
-
58
- /**
59
- * Is stack empty
60
- */
61
- public isEmpty(): boolean {
62
- return this._list.isEmpty();
63
- }
64
-
65
- /**
66
- * Is stack full
67
- */
68
- public isFull(): boolean {
69
- return this._list.isFull();
70
- }
71
-
72
- /**
73
- * Remove all elements in stack
74
- */
75
- public clear(): void {
76
- this._list.clear();
77
- }
78
-
79
- /**
80
- * Queue length
81
- */
82
- public length(): number {
83
- return this._list.length();
84
- }
85
-
86
- /**
87
- * Reverse stack
88
- */
89
- public reverse(): void {
90
- this._list.reverse();
91
- }
92
- }
@@ -1,67 +0,0 @@
1
- import BinarySearchTree from "../data-structures/BinaryTree/BinarySearchTree/BinarySearchTree";
2
- import { EnumTreeTraversalType } from "../types/EnumTreeTraversalType";
3
- // eslint-disable-next-line @typescript-eslint/no-var-requires
4
- const util = require("util");
5
-
6
- const logTree = <T>(tree: BinarySearchTree<T>) => {
7
- console.log(
8
- util.inspect(tree, { showHidden: false, depth: null, colors: true })
9
- );
10
-
11
- console.log("IN ORDER TRAVERSE");
12
- console.log(tree.traverse(EnumTreeTraversalType.InOrder));
13
- console.log("PRE ORDER TRAVERSE");
14
- console.log(tree.traverse(EnumTreeTraversalType.PreOrder));
15
- console.log("POST ORDER TRAVERSE");
16
- console.log(tree.traverse(EnumTreeTraversalType.PostOrder));
17
- console.log("HEIGHT");
18
- console.log(tree.height());
19
- console.log("LENGTH");
20
- console.log(tree.length());
21
- console.log("MAX");
22
- console.log(tree.max());
23
- console.log("MIN");
24
- console.log(tree.min());
25
- };
26
-
27
- export const demoBst = (): void => {
28
- const bst = new BinarySearchTree<number>();
29
-
30
- bst.insert(22);
31
- bst.insert(4);
32
- bst.insert(16);
33
- bst.insert(19);
34
- bst.insert(15);
35
- bst.insert(8);
36
- bst.insert(23);
37
- logTree(bst);
38
-
39
- bst.delete(15);
40
- logTree(bst);
41
- };
42
-
43
- class Human {
44
- public readonly age: number;
45
- public readonly name: string;
46
-
47
- public constructor(name: string, age: number) {
48
- this.age = age;
49
- this.name = name;
50
- }
51
- }
52
-
53
- export const demoBstObjects = (): void => {
54
- const bst = new BinarySearchTree<Human>((human1, human2) => {
55
- return human1.age > human2.age;
56
- });
57
-
58
- bst.insert(new Human("Bob", 22));
59
- bst.insert(new Human("Alice", 14));
60
- bst.insert(new Human("John", 16));
61
- bst.insert(new Human("Boris", 19));
62
- bst.insert(new Human("Anna", 15));
63
- bst.insert(new Human("Dmitry", 8));
64
- bst.insert(new Human("Alex", 23));
65
-
66
- logTree(bst);
67
- };
@@ -1,246 +0,0 @@
1
- import IGraph from "../types/IGraph";
2
- import { createGraph } from "../helpers/createGraph";
3
- import { EnumGraphType } from "../types/EnumGraphType";
4
- import { generateRandomGraph } from "../data-structures/Graph/demo/generateRandomGraph";
5
- import { shortestPath } from "../data-structures/Graph/searching/shortestPath";
6
- import BFSIterationStrategy from "../data-structures/Graph/strategy/BFSIterationStrategy";
7
-
8
- export const demoUndirectedGraph = (): void => {
9
- console.log("\nEmpty undirected graph created");
10
- const graph: IGraph<string> = createGraph(EnumGraphType.Undirected);
11
-
12
- graph.addVertex("John");
13
- graph.addVertex("Mary");
14
- graph.addVertex("Kate");
15
- console.log("\nJohn, Mary and Kate was added to graph");
16
- console.log("Vertices count:");
17
- console.log(graph.verticesCount());
18
- console.log("Vertices list:");
19
- console.log(graph.vertices());
20
-
21
- console.log("\nChecking is graph hasVertex John: ");
22
- console.log(graph.hasVertex("John"));
23
-
24
- graph.removeVertex("John");
25
- console.log("\nDeleting vertex John... ");
26
- console.log("\nChecking is graph hasVertex John: ");
27
- console.log(graph.hasVertex("John"));
28
- console.log(graph);
29
-
30
- graph.addVertex("Bob");
31
- graph.addVertex("Elon");
32
- console.log("\nAdding Bob и Elon: ");
33
- console.log(graph);
34
-
35
- graph.addEdge("Bob", "Elon", 5);
36
- console.log("\nAdding edge between Bob and Elon: ");
37
- console.log("\nHas edge between Bob и Elon: ");
38
- console.log(graph.hasEdge("Bob", "Elon"));
39
- console.log("\nHas edge between Elon и Bob: ");
40
- console.log(graph.hasEdge("Elon", "Bob"));
41
- console.log("\nEdge weight between Bob и Elon: ");
42
- console.log(graph.getEdgeWeight("Bob", "Elon"));
43
- console.log("\nEdge weight between Elon and Kate: ");
44
- console.log(graph.getEdgeWeight("Elon", "Bob"));
45
- console.log("==========================================");
46
-
47
- graph.addEdge("Kate", "Elon", 12);
48
- console.log("\nAdding edge between Kate and Elon: ");
49
- console.log("\nHas edge between Kate и Elon: ");
50
- console.log(graph.hasEdge("Kate", "Elon"));
51
- console.log("\nHas edge between Elon и Kate: ");
52
- console.log(graph.hasEdge("Elon", "Kate"));
53
- console.log("\nEdge weight between Kate и Elon: ");
54
- console.log(graph.getEdgeWeight("Kate", "Elon"));
55
- console.log("\nEdge weight between Elon and Kate: ");
56
- console.log(graph.getEdgeWeight("Elon", "Kate"));
57
- console.log("==========================================");
58
-
59
- graph.addEdge("Kate", "Mary", 8);
60
- console.log("\nAdding edge between Kate and Mary: ");
61
- console.log("\nHas edge between Kate и Mary: ");
62
- console.log(graph.hasEdge("Kate", "Mary"));
63
- console.log("\nHas edge between Mary и Kate: ");
64
- console.log(graph.hasEdge("Mary", "Kate"));
65
- console.log("\nEdge weight between Kate и Mary: ");
66
- console.log(graph.getEdgeWeight("Kate", "Mary"));
67
- console.log("\nEdge weight between Mary and Kate: ");
68
- console.log(graph.getEdgeWeight("Mary", "Kate"));
69
- console.log("==========================================");
70
-
71
- console.log("Edges count:");
72
- console.log(graph.edgesCount());
73
-
74
- console.log("Elon neighbors:");
75
- console.log(graph.getVertexNeighbors("Elon"));
76
- console.log("Bob neighbors:");
77
- console.log(graph.getVertexNeighbors("Bob"));
78
- console.log("Kate neighbors:");
79
- console.log(graph.getVertexNeighbors("Kate"));
80
- console.log("Mary neighbors:");
81
- console.log(graph.getVertexNeighbors("Mary"));
82
-
83
- console.log("\nGraph weight: ");
84
- console.log(graph.weight());
85
-
86
- console.log("\nUpdating edge between Elon and Bob: ");
87
- graph.addEdge("Elon", "Bob", 10);
88
- console.log("\nEdge weight between Bob и Elon: ");
89
- console.log(graph.getEdgeWeight("Bob", "Elon"));
90
- console.log("\nEdge weight between Elon and Kate: ");
91
- console.log(graph.getEdgeWeight("Elon", "Bob"));
92
- console.log("==========================================");
93
-
94
- console.log("\nGraph weight after one edge weight was updated: ");
95
- console.log(graph.weight());
96
-
97
- graph.removeEdge("Bob", "Elon");
98
- console.log("\nRemove edge between Bob and Elon: ");
99
- console.log("\nGraph after one edge weight was deleted: ");
100
- console.log(graph);
101
-
102
- graph.removeVertex("Kate");
103
- console.log("\nRemove vertex Kate and cascade all related edges: ");
104
- console.log(graph);
105
- };
106
-
107
- export const demoDirectedGraph = (): void => {
108
- console.log("\nEmpty directed graph created");
109
- const graph: IGraph<string> = createGraph(EnumGraphType.Directed);
110
-
111
- graph.addVertex("John");
112
- graph.addVertex("Mary");
113
- graph.addVertex("Kate");
114
- console.log("\nJohn, Mary and Kate was added to graph");
115
- console.log("Vertices count:");
116
- console.log(graph.verticesCount());
117
- console.log("Vertices list:");
118
- console.log(graph.vertices());
119
-
120
- console.log("\nChecking is graph hasVertex John: ");
121
- console.log(graph.hasVertex("John"));
122
-
123
- graph.removeVertex("John");
124
- console.log("\nDeleting vertex John... ");
125
- console.log("\nChecking is graph hasVertex John: ");
126
- console.log(graph.hasVertex("John"));
127
- console.log(graph);
128
-
129
- graph.addVertex("Bob");
130
- graph.addVertex("Elon");
131
- console.log("\nAdding Bob и Elon: ");
132
- console.log(graph);
133
-
134
- graph.addEdge("Bob", "Elon", 5);
135
- console.log("\nAdding edge between Bob and Elon: ");
136
- console.log("\nHas edge between Bob и Elon: ");
137
- console.log(graph.hasEdge("Bob", "Elon"));
138
- console.log("\nHas edge between Elon и Bob: ");
139
- console.log(graph.hasEdge("Elon", "Bob"));
140
- console.log("\nEdge weight between Bob и Elon: ");
141
- console.log(graph.getEdgeWeight("Bob", "Elon"));
142
- console.log("==========================================");
143
-
144
- graph.addEdge("Kate", "Elon", 12);
145
- console.log("\nAdding edge between Kate and Elon: ");
146
- console.log("\nHas edge between Kate и Elon: ");
147
- console.log(graph.hasEdge("Kate", "Elon"));
148
- console.log("\nHas edge between Elon и Kate: ");
149
- console.log(graph.hasEdge("Elon", "Kate"));
150
- console.log("\nEdge weight between Kate и Elon: ");
151
- console.log(graph.getEdgeWeight("Kate", "Elon"));
152
- console.log("==========================================");
153
-
154
- graph.addEdge("Kate", "Mary", 8);
155
- console.log("\nAdding edge between Kate and Mary: ");
156
- console.log("\nHas edge between Kate и Mary: ");
157
- console.log(graph.hasEdge("Kate", "Mary"));
158
- console.log("\nHas edge between Mary и Kate: ");
159
- console.log(graph.hasEdge("Mary", "Kate"));
160
- console.log("\nEdge weight between Kate и Mary: ");
161
- console.log(graph.getEdgeWeight("Kate", "Mary"));
162
- console.log("==========================================");
163
-
164
- console.log("Edges count:");
165
- console.log(graph.edgesCount());
166
-
167
- console.log("Elon neighbors:");
168
- console.log(graph.getVertexNeighbors("Elon"));
169
- console.log("Bob neighbors:");
170
- console.log(graph.getVertexNeighbors("Bob"));
171
- console.log("Kate neighbors:");
172
- console.log(graph.getVertexNeighbors("Kate"));
173
- console.log("Mary neighbors:");
174
- console.log(graph.getVertexNeighbors("Mary"));
175
-
176
- console.log("\nGraph weight: ");
177
- console.log(graph.weight());
178
-
179
- console.log("\nAdding edge between Elon and Bob: ");
180
- graph.addEdge("Elon", "Bob", 10);
181
- console.log("\nEdge weight between Elon и Bob: ");
182
- console.log(graph.getEdgeWeight("Elon", "Bob"));
183
- console.log("\nEdge weight between Bob and Elon: ");
184
- console.log(graph.getEdgeWeight("Bob", "Elon"));
185
- console.log("==========================================");
186
- console.log(graph);
187
-
188
- console.log("\nGraph weight after one edge weight was updated: ");
189
- console.log(graph.weight());
190
-
191
- graph.removeEdge("Bob", "Elon");
192
- console.log("\nRemove edge between Bob and Elon: ");
193
- console.log("\nGraph after one edge weight was deleted: ");
194
- console.log(graph);
195
-
196
- graph.removeVertex("Kate");
197
- console.log("\nRemove vertex Kate and cascade all related edges: ");
198
- console.log(graph);
199
- };
200
-
201
- export const demoGraphGeneratedByType = (
202
- type: EnumGraphType,
203
- verticesCount: number,
204
- edgesCount: number
205
- ): void => {
206
- const bfsIteration = new BFSIterationStrategy<string>();
207
- const graph: IGraph<string> = generateRandomGraph(
208
- verticesCount,
209
- edgesCount,
210
- type
211
- );
212
- console.log(
213
- `\nGenerated ${type.toLocaleLowerCase()} graph (N = ${verticesCount}, K = ${edgesCount}): \n`
214
- );
215
- console.log(graph);
216
-
217
- console.log(`\nFinding shortest path via BFS: \n`);
218
- graph.vertices().forEach((vertexFrom: string) => {
219
- graph.vertices().forEach((vertexTo: string) => {
220
- const getPath = () => {
221
- try {
222
- return shortestPath<string>(
223
- graph,
224
- vertexFrom,
225
- vertexTo,
226
- bfsIteration
227
- );
228
- } catch (e) {
229
- return `[-- ${e.message} --]`;
230
- }
231
- };
232
-
233
- if (vertexFrom !== vertexTo) {
234
- console.log(`\nShortest path from ${vertexFrom} to ${vertexTo} is:`);
235
- console.log(getPath());
236
- }
237
- });
238
- });
239
- };
240
-
241
- export const demoGraphGenerated = (): void => {
242
- const verticesCount = 6;
243
- const edgesCount = 6;
244
- demoGraphGeneratedByType(EnumGraphType.Directed, verticesCount, edgesCount);
245
- demoGraphGeneratedByType(EnumGraphType.Undirected, verticesCount, edgesCount);
246
- };
@@ -1,28 +0,0 @@
1
- import HashTable from "../data-structures/HashTable/HashTable";
2
-
3
- export const demoHashtable = (): void => {
4
- const hashTable = new HashTable<number>(15);
5
-
6
- // emulate 66% load
7
- for (let i = 0; i < 10; i++) {
8
- hashTable.set(`key${i * 2}`, i * 2);
9
- }
10
- console.log(hashTable);
11
-
12
- for (let i = 10; i < 15; i++) {
13
- hashTable.set(`key${i * 2}`, i * 2);
14
- }
15
- console.log(hashTable);
16
- console.log("\n length:", hashTable.length());
17
-
18
- hashTable.set("key4", 10000);
19
- console.log("\n", hashTable.get("key4"));
20
- hashTable.delete("key4");
21
- console.log("\n has key4:", hashTable.has("key4"));
22
- hashTable.set("key4", 20000);
23
- console.log("\n has key4:", hashTable.has("key4"));
24
- console.log("\n key4 value:", hashTable.get("key4"));
25
- console.log("\n", hashTable);
26
- hashTable.clear();
27
- console.log("\n", hashTable);
28
- };