@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,80 +0,0 @@
1
- import IBinaryTree from "../../../types/IBinaryTree";
2
- import { FnCompareTwo } from "../../../types/FnCompareTwo";
3
- import AbstractBinaryNode from "./AbstractBinaryNode";
4
- import { EnumTreeTraversalType } from "../../../types/EnumTreeTraversalType";
5
-
6
- /**
7
- *
8
- */
9
- export default abstract class AbstractBinaryTree<T> implements IBinaryTree<T> {
10
- /**
11
- * Function that checks is node A better
12
- * @default a > b
13
- * @example 5 > 4
14
- * @example 'abc' > 'aba'
15
- */
16
- protected compare: FnCompareTwo<T> = (a: T, b: T) => a > b;
17
- protected _head: AbstractBinaryNode<T> | null;
18
- protected _length: number;
19
-
20
- /**
21
- *
22
- * @param fnCompare
23
- * @protected
24
- */
25
- protected constructor(fnCompare?: FnCompareTwo<T>) {
26
- if (fnCompare) {
27
- this.compare = fnCompare;
28
- console.log(this.compare);
29
- }
30
- this._head = null;
31
- this._length = 0;
32
- }
33
-
34
- /**
35
- * Returns nodes count in the entire tree
36
- */
37
- public length(): number {
38
- return this._length;
39
- }
40
-
41
- /**
42
- * Will create new node and place it according to the type of tree rules
43
- */
44
- public abstract insert(value: T): void;
45
-
46
- /**
47
- * Will check if tree has a node with given data
48
- */
49
- public abstract has(value: T): boolean;
50
-
51
- /**
52
- * Will delete node from the tree and restructure it
53
- */
54
- public abstract delete(value: T): void;
55
-
56
- /**
57
- * Max value of the tree
58
- */
59
- public abstract max(): T;
60
-
61
- /**
62
- * Min value of the tree
63
- */
64
- public abstract min(): T;
65
-
66
- /**
67
- * Returns same class instance with new head node
68
- */
69
- public abstract subtree(value: T): IBinaryTree<T>;
70
-
71
- /**
72
- * Returns the highest branch length in the tree
73
- */
74
- public abstract height(): number;
75
-
76
- /**
77
- * In-order/Pre-order/Post-order traversing of the tree
78
- */
79
- public abstract traverse(type: EnumTreeTraversalType): Array<T>;
80
- }
@@ -1,38 +0,0 @@
1
- import AbstractBinaryNode from "../AbstractBinaryTree/AbstractBinaryNode";
2
-
3
- export default class BinarySearchNode<T> extends AbstractBinaryNode<T> {
4
- protected _left: BinarySearchNode<T> | null;
5
- protected _right: BinarySearchNode<T> | null;
6
- protected _parent: BinarySearchNode<T> | null;
7
-
8
- public constructor(initialData: T) {
9
- super(initialData);
10
- this._left = null;
11
- this._right = null;
12
- this._parent = null;
13
- }
14
-
15
- public get left(): BinarySearchNode<T> | null {
16
- return this._left;
17
- }
18
-
19
- public set left(value: BinarySearchNode<T> | null) {
20
- this._left = value;
21
- }
22
-
23
- public get right(): BinarySearchNode<T> | null {
24
- return this._right;
25
- }
26
-
27
- public set right(value: BinarySearchNode<T> | null) {
28
- this._right = value;
29
- }
30
-
31
- public get parent(): BinarySearchNode<T> | null {
32
- return this._parent;
33
- }
34
-
35
- public set parent(value: BinarySearchNode<T> | null) {
36
- this._parent = value;
37
- }
38
- }
@@ -1,286 +0,0 @@
1
- import { FnCompareTwo } from "../../../types/FnCompareTwo";
2
- import { EnumTreeTraversalType } from "../../../types/EnumTreeTraversalType";
3
- import IBinaryTree from "../../../types/IBinaryTree";
4
- import AbstractBinaryTree from "../AbstractBinaryTree/AbstractBinaryTree";
5
- import BinarySearchNode from "./BinarySearchNode";
6
- import Queue from "../../Queue/Queue";
7
-
8
- /**
9
- * Unbalanced binary search tree implementation
10
- */
11
- export default class BinarySearchTree<T> extends AbstractBinaryTree<T> {
12
- /**
13
- * Override types
14
- */
15
- protected _head: BinarySearchNode<T> | null;
16
-
17
- /**
18
- * @inheritDoc
19
- */
20
- public constructor(fnCompare?: FnCompareTwo<T>) {
21
- super(fnCompare);
22
- this._head = null;
23
- }
24
-
25
- /**
26
- *
27
- * @throws when head is empty
28
- */
29
- protected checkIsEmpty(): void {
30
- if (this._head === null) {
31
- throw new Error("Tree is empty");
32
- }
33
- }
34
-
35
- /**
36
- * Will update left and right links parent with current node
37
- */
38
- protected updateLeftRightParents(node: BinarySearchNode<T>): void {
39
- if (node.left && node.left.parent !== node) {
40
- node.left.parent = node;
41
- }
42
- if (node.right && node.right.parent !== node) {
43
- node.right.parent = node;
44
- }
45
- }
46
-
47
- /**
48
- * Will return node instance by its data
49
- */
50
- protected findNode(value: T): BinarySearchNode<T> | null {
51
- let current = this._head;
52
-
53
- while (current && current.data !== value) {
54
- current = this.compare(current.data, value)
55
- ? current.left
56
- : current.right;
57
- }
58
-
59
- return current;
60
- }
61
-
62
- /**
63
- * @inheritDoc
64
- */
65
- protected insertToLeaf(createdNode: BinarySearchNode<T>): void {
66
- let parent = null;
67
- let current = this._head;
68
-
69
- while (current) {
70
- parent = current;
71
-
72
- current = this.compare(current.data, createdNode.data)
73
- ? current.left
74
- : current.right;
75
- }
76
-
77
- createdNode.parent = parent;
78
-
79
- if (parent === null) {
80
- this._head = createdNode;
81
- } else {
82
- if (this.compare(parent.data, createdNode.data)) {
83
- parent.left = createdNode;
84
- } else {
85
- parent.right = createdNode;
86
- }
87
- }
88
- this._length++;
89
- }
90
-
91
- /**
92
- * Will join two trees into one */
93
- protected join(
94
- treeLeft: BinarySearchNode<T> | null,
95
- treeRight: BinarySearchNode<T> | null
96
- ): BinarySearchNode<T> | null {
97
- if (treeLeft === null) {
98
- return treeRight;
99
- }
100
- if (treeRight === null) {
101
- return treeLeft;
102
- }
103
-
104
- treeRight.left = this.join(treeLeft, treeRight.left);
105
- if (treeRight.left) {
106
- this.updateLeftRightParents(treeRight);
107
- }
108
- return treeRight;
109
- }
110
-
111
- /**
112
- * @inheritDoc
113
- */
114
- public max(): T {
115
- this.checkIsEmpty();
116
- let currentNode = this._head;
117
- while (currentNode?.right) {
118
- currentNode = currentNode.right;
119
- }
120
- return currentNode!.data;
121
- }
122
-
123
- /**
124
- * @inheritDoc
125
- */
126
- public min(): T {
127
- this.checkIsEmpty();
128
- let currentNode = this._head;
129
- while (currentNode?.left) {
130
- currentNode = currentNode.left;
131
- }
132
- return currentNode!.data;
133
- }
134
-
135
- /**
136
- * @inheritDoc
137
- */
138
- public insert(value: T): void {
139
- if (this.has(value)) {
140
- throw new Error("Node already exists");
141
- }
142
- const createdNode = new BinarySearchNode(value);
143
- this.insertToLeaf(createdNode);
144
- }
145
-
146
- /**
147
- * @inheritDoc
148
- */
149
- public has(value: T): boolean {
150
- const current = this.findNode(value);
151
- return current?.data === value;
152
- }
153
-
154
- /**
155
- * @inheritDoc
156
- */
157
- public delete(value: T): void {
158
- if (!this.has(value)) {
159
- throw new Error("Value does not exist in the tree");
160
- }
161
-
162
- const recursiveDelete = (node: BinarySearchNode<T> | null, value: T) => {
163
- if (node === null) {
164
- return node;
165
- }
166
-
167
- if (node.data === value) {
168
- const updatedNode = this.join(node.left, node.right);
169
- if (updatedNode) {
170
- updatedNode.parent = node.parent;
171
- }
172
- return updatedNode;
173
- } else if (this.compare(node.data, value)) {
174
- node.left = recursiveDelete(node.left, value);
175
- } else {
176
- node.right = recursiveDelete(node.right, value);
177
- }
178
- this.updateLeftRightParents(node);
179
- return node;
180
- };
181
-
182
- this._head = recursiveDelete(this._head, value);
183
- this._length--;
184
- }
185
-
186
- /**
187
- * @inheritDoc
188
- */
189
- public subtree(value: T): IBinaryTree<T> {
190
- const tree = new BinarySearchTree<T>();
191
- const node = this.findNode(value);
192
- const queue = new Queue<BinarySearchNode<T> | null>();
193
-
194
- const traverse = [];
195
- queue.push(node);
196
-
197
- while (!queue.isEmpty()) {
198
- const currentNode = queue.pop();
199
- traverse.push(currentNode);
200
- if (currentNode?.left) {
201
- queue.push(currentNode.left);
202
- }
203
- if (currentNode?.right) {
204
- queue.push(currentNode.right);
205
- }
206
- }
207
-
208
- traverse.forEach((elem) => {
209
- if (elem !== null) {
210
- tree.insert(elem.data);
211
- }
212
- });
213
-
214
- return tree;
215
- }
216
-
217
- /**
218
- * @inheritDoc
219
- */
220
- public traverse(type: EnumTreeTraversalType, from?: T): Array<T> {
221
- this.checkIsEmpty();
222
-
223
- const array = new Array(this.length());
224
- const root = from !== undefined ? this.findNode(from) : this._head;
225
-
226
- const storeInOrder = (node: BinarySearchNode<T> | null) => {
227
- if (node === null) {
228
- return;
229
- }
230
- storeInOrder(node.left);
231
- array.push(node.data);
232
- storeInOrder(node.right);
233
- };
234
-
235
- const storePostOrder = (node: BinarySearchNode<T> | null) => {
236
- if (node === null) {
237
- return;
238
- }
239
- storeInOrder(node.left);
240
- storeInOrder(node.right);
241
- array.push(node.data);
242
- };
243
-
244
- const storePreOrder = (node: BinarySearchNode<T> | null) => {
245
- if (node === null) {
246
- return;
247
- }
248
- array.push(node.data);
249
- storeInOrder(node.left);
250
- storeInOrder(node.right);
251
- };
252
-
253
- switch (type) {
254
- case EnumTreeTraversalType.InOrder:
255
- storeInOrder(root);
256
- break;
257
- case EnumTreeTraversalType.PostOrder:
258
- storePostOrder(root);
259
- break;
260
- case EnumTreeTraversalType.PreOrder:
261
- storePreOrder(root);
262
- break;
263
- }
264
-
265
- return array.filter((item) => item !== undefined);
266
- }
267
-
268
- /**
269
- * Calc max height of the largest branch of the tree
270
- */
271
- public height(): number {
272
- const calcHeight = (node: BinarySearchNode<T>) => {
273
- if (node === null) return 0;
274
- const left: number = node.left === null ? -1 : calcHeight(node.left);
275
- const right: number = node.right === null ? -1 : calcHeight(node.right);
276
- const max = left > right ? left : right;
277
- return max + 1;
278
- };
279
-
280
- if (this._head === null) {
281
- return 0;
282
- } else {
283
- return calcHeight(this._head) + 1;
284
- }
285
- }
286
- }
@@ -1,48 +0,0 @@
1
- import BinarySearchNode from "../BinarySearchTree/BinarySearchNode";
2
-
3
- export default class RandBinarySearchNode<T> extends BinarySearchNode<T> {
4
- private _rank: number;
5
- protected _left: RandBinarySearchNode<T> | null;
6
- protected _right: RandBinarySearchNode<T> | null;
7
- protected _parent: RandBinarySearchNode<T> | null;
8
-
9
- public constructor(initialData: T) {
10
- super(initialData);
11
- this._rank = 0;
12
- this._left = null;
13
- this._right = null;
14
- this._parent = null;
15
- }
16
-
17
- public get rank(): number {
18
- return this._rank;
19
- }
20
-
21
- public set rank(value: number) {
22
- this._rank = value;
23
- }
24
-
25
- public get left(): RandBinarySearchNode<T> | null {
26
- return this._left;
27
- }
28
-
29
- public set left(value: RandBinarySearchNode<T> | null) {
30
- this._left = value;
31
- }
32
-
33
- public get right(): RandBinarySearchNode<T> | null {
34
- return this._right;
35
- }
36
-
37
- public set right(value: RandBinarySearchNode<T> | null) {
38
- this._right = value;
39
- }
40
-
41
- public get parent(): RandBinarySearchNode<T> | null {
42
- return this._parent;
43
- }
44
-
45
- public set parent(value: RandBinarySearchNode<T> | null) {
46
- this._parent = value;
47
- }
48
- }
@@ -1,228 +0,0 @@
1
- import { FnCompareTwo } from "../../../types/FnCompareTwo";
2
- import RandBinarySearchNode from "./RandBinarySearchNode";
3
- import BinarySearchTree from "../BinarySearchTree/BinarySearchTree";
4
-
5
- /**
6
- * Randomized binary search tree implementation
7
- */
8
- export default class RandBinarySearchTree<T> extends BinarySearchTree<T> {
9
- /**
10
- * Override types
11
- */
12
- protected _head: RandBinarySearchNode<T> | null;
13
-
14
- /**
15
- * @inheritDoc
16
- */
17
- public constructor(fnCompare?: FnCompareTwo<T>) {
18
- super(fnCompare);
19
- this._head = null;
20
- }
21
-
22
- /**
23
- * Will update node rank by summing left and right subtrees tanks and itself rank (1)
24
- */
25
- private updateRank(node: RandBinarySearchNode<T>): void {
26
- node.rank = (node.right?.rank || 0) + (node.left?.rank || 0) + 1;
27
- }
28
-
29
- /**
30
- * Will set rank and parent attributes and update tree length
31
- */
32
- private addCreatedNode(
33
- node: RandBinarySearchNode<T>,
34
- parentNode: RandBinarySearchNode<T> | null = null
35
- ): RandBinarySearchNode<T> {
36
- node.rank = 1;
37
- if (parentNode !== null) {
38
- node.parent = parentNode;
39
- }
40
- return node;
41
- }
42
-
43
- /**
44
- * Will rotate node to the right side
45
- */
46
- private rotateNodeRight(node: RandBinarySearchNode<T>): void {
47
- const pivot = node.left;
48
- if (pivot === null) {
49
- return;
50
- }
51
- node.left = pivot.right;
52
- if (pivot.right !== null) {
53
- pivot.right.parent = node;
54
- }
55
- pivot.parent = node.parent;
56
- if (node.parent === null) {
57
- this._head = pivot;
58
- } else {
59
- if (node === node.parent.right) {
60
- node.parent.right = pivot;
61
- } else {
62
- node.parent.left = pivot;
63
- }
64
- }
65
- pivot.right = node;
66
- node.parent = pivot;
67
- this.updateRank(node);
68
- this.updateRank(pivot);
69
- }
70
-
71
- /**
72
- * Will rotate node to the left side
73
- */
74
- private rotateNodeLeft(node: RandBinarySearchNode<T>): void {
75
- const pivot = node.right;
76
- if (pivot === null) {
77
- return;
78
- }
79
- node.right = pivot.left;
80
- if (pivot.left !== null) {
81
- pivot.left.parent = node;
82
- }
83
- pivot.parent = node.parent;
84
- if (node.parent === null) {
85
- this._head = pivot;
86
- } else {
87
- if (node === node.parent.left) {
88
- node.parent.left = pivot;
89
- } else {
90
- node.parent.right = pivot;
91
- }
92
- }
93
- pivot.left = node;
94
- node.parent = pivot;
95
- this.updateRank(node);
96
- this.updateRank(pivot);
97
- }
98
-
99
- /**
100
- * @inheritDoc
101
- */
102
- protected join(
103
- treeLeft: RandBinarySearchNode<T> | null,
104
- treeRight: RandBinarySearchNode<T> | null
105
- ): RandBinarySearchNode<T> | null {
106
- if (treeLeft === null) {
107
- return treeRight;
108
- }
109
- if (treeRight === null) {
110
- return treeLeft;
111
- }
112
-
113
- if (Math.random() < treeLeft.rank / (treeLeft.rank + treeRight.rank)) {
114
- treeLeft.right = this.join(treeLeft.right, treeRight);
115
- if (treeLeft.right) {
116
- this.updateLeftRightParents(treeLeft);
117
- }
118
- this.updateRank(treeLeft);
119
- return treeLeft;
120
- } else {
121
- treeRight.left = this.join(treeLeft, treeRight.left);
122
- if (treeRight.left) {
123
- this.updateLeftRightParents(treeRight);
124
- }
125
- this.updateRank(treeRight);
126
- return treeRight;
127
- }
128
- }
129
-
130
- /**
131
- * @inheritDoc
132
- */
133
- protected updateLeftRightParents(node: RandBinarySearchNode<T>): void {
134
- super.updateLeftRightParents(node);
135
- this.updateRank(node);
136
- }
137
-
138
- /**
139
- * @inheritDoc
140
- */
141
- protected insertToRoot(
142
- createdNode: RandBinarySearchNode<T>,
143
- fromNode?: RandBinarySearchNode<T>
144
- ): void {
145
- const recursiveInsert = (node: RandBinarySearchNode<T>) => {
146
- if (this.compare(node.data, createdNode.data)) {
147
- if (node.left === null) {
148
- node.left = this.addCreatedNode(createdNode, node);
149
- } else {
150
- recursiveInsert(node.left);
151
- }
152
- this.rotateNodeRight(node);
153
- } else {
154
- if (node.right === null) {
155
- node.right = this.addCreatedNode(createdNode, node);
156
- } else {
157
- recursiveInsert(node.right);
158
- }
159
-
160
- this.rotateNodeLeft(node);
161
- }
162
- };
163
-
164
- if (this._head === null) {
165
- this._head = this.addCreatedNode(createdNode);
166
- } else {
167
- fromNode ? recursiveInsert(fromNode) : recursiveInsert(this._head);
168
- }
169
- }
170
-
171
- /**
172
- * @inheritDoc
173
- */
174
- protected insertRandomly(createdNode: RandBinarySearchNode<T>): void {
175
- const recursiveInsertRandomly = (node: RandBinarySearchNode<T>) => {
176
- const shouldInsertToRoot = Math.random() < 1 / (node.rank + 1);
177
-
178
- if (shouldInsertToRoot) {
179
- this.insertToRoot(createdNode, node);
180
- } else {
181
- node.rank = node.rank + 1;
182
-
183
- if (this.compare(node.data, createdNode.data)) {
184
- if (node.left === null) {
185
- node.left = this.addCreatedNode(createdNode, node);
186
- } else {
187
- recursiveInsertRandomly(node.left);
188
- }
189
- } else {
190
- if (node.right === null) {
191
- node.right = this.addCreatedNode(createdNode, node);
192
- } else {
193
- recursiveInsertRandomly(node.right);
194
- }
195
- }
196
- }
197
- };
198
-
199
- if (this._head === null) {
200
- this._head = this.addCreatedNode(createdNode);
201
- } else {
202
- recursiveInsertRandomly(this._head);
203
- }
204
- }
205
-
206
- /**
207
- * @inheritDoc
208
- */
209
- public insert(value: T): void {
210
- if (this.has(value)) {
211
- throw new Error("Node already exists");
212
- }
213
- const createdNode = new RandBinarySearchNode(value);
214
- this.insertRandomly(createdNode);
215
- }
216
-
217
- /**
218
- * @inheritDoc
219
- */
220
- public delete(value: T): void {
221
- super.delete(value);
222
- this._length = this.length();
223
- }
224
-
225
- public length(): number {
226
- return this._head?.rank || 0;
227
- }
228
- }