@gabrielrufino/cube 1.0.1

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 (240) hide show
  1. package/.github/workflows/cd.yml +26 -0
  2. package/.github/workflows/ci.yml +23 -0
  3. package/LICENSE +24 -0
  4. package/README.md +1695 -0
  5. package/build/Algorithms/index.d.ts +3 -0
  6. package/build/Algorithms/index.js +24 -0
  7. package/build/Array/IArray.d.ts +9 -0
  8. package/build/Array/IArray.js +2 -0
  9. package/build/Array/index.d.ts +12 -0
  10. package/build/Array/index.js +79 -0
  11. package/build/BinarySearchTree/BinarySearchTreeNode.d.ts +7 -0
  12. package/build/BinarySearchTree/BinarySearchTreeNode.js +22 -0
  13. package/build/BinarySearchTree/IBinarySearchNodeOptions.d.ts +5 -0
  14. package/build/BinarySearchTree/IBinarySearchNodeOptions.js +2 -0
  15. package/build/BinarySearchTree/IBinarySearchTree.d.ts +14 -0
  16. package/build/BinarySearchTree/IBinarySearchTree.js +2 -0
  17. package/build/BinarySearchTree/IBinarySearchTreeData.d.ts +7 -0
  18. package/build/BinarySearchTree/IBinarySearchTreeData.js +2 -0
  19. package/build/BinarySearchTree/index.d.ts +24 -0
  20. package/build/BinarySearchTree/index.js +199 -0
  21. package/build/DataStructure/index.d.ts +6 -0
  22. package/build/DataStructure/index.js +32 -0
  23. package/build/Deck/IDeck.d.ts +9 -0
  24. package/build/Deck/IDeck.js +2 -0
  25. package/build/Deck/index.d.ts +12 -0
  26. package/build/Deck/index.js +76 -0
  27. package/build/Dictionary/IDictionary.d.ts +16 -0
  28. package/build/Dictionary/IDictionary.js +2 -0
  29. package/build/Dictionary/IDictionaryData.d.ts +4 -0
  30. package/build/Dictionary/IDictionaryData.js +2 -0
  31. package/build/Dictionary/index.d.ts +21 -0
  32. package/build/Dictionary/index.js +110 -0
  33. package/build/DoublyLinkedList/IDoublyLinkedList.d.ts +16 -0
  34. package/build/DoublyLinkedList/IDoublyLinkedList.js +2 -0
  35. package/build/DoublyLinkedList/Node.d.ts +6 -0
  36. package/build/DoublyLinkedList/Node.js +11 -0
  37. package/build/DoublyLinkedList/index.d.ts +32 -0
  38. package/build/DoublyLinkedList/index.js +221 -0
  39. package/build/Graph/GraphNodeNotFoundError.d.ts +3 -0
  40. package/build/Graph/GraphNodeNotFoundError.js +27 -0
  41. package/build/Graph/GraphSearchNodeStates.d.ts +6 -0
  42. package/build/Graph/GraphSearchNodeStates.js +10 -0
  43. package/build/Graph/IGraph.d.ts +16 -0
  44. package/build/Graph/IGraph.js +2 -0
  45. package/build/Graph/IGraphOptions.d.ts +7 -0
  46. package/build/Graph/IGraphOptions.js +2 -0
  47. package/build/Graph/index.d.ts +20 -0
  48. package/build/Graph/index.js +187 -0
  49. package/build/HashTable/IHashTable.d.ts +9 -0
  50. package/build/HashTable/IHashTable.js +2 -0
  51. package/build/HashTable/IHashTableData.d.ts +4 -0
  52. package/build/HashTable/IHashTableData.js +2 -0
  53. package/build/HashTable/IHashTableInputs.d.ts +4 -0
  54. package/build/HashTable/IHashTableInputs.js +2 -0
  55. package/build/HashTable/IHashTableOptions.d.ts +4 -0
  56. package/build/HashTable/IHashTableOptions.js +2 -0
  57. package/build/HashTable/index.d.ts +16 -0
  58. package/build/HashTable/index.js +83 -0
  59. package/build/HashTableLinearProbing/HashTableLinearProbingElement.d.ts +6 -0
  60. package/build/HashTableLinearProbing/HashTableLinearProbingElement.js +19 -0
  61. package/build/HashTableLinearProbing/IHashTableLinearProbing.d.ts +9 -0
  62. package/build/HashTableLinearProbing/IHashTableLinearProbing.js +2 -0
  63. package/build/HashTableLinearProbing/IHashTableLinearProbingData.d.ts +5 -0
  64. package/build/HashTableLinearProbing/IHashTableLinearProbingData.js +2 -0
  65. package/build/HashTableLinearProbing/IHashTableLinearProbingInputs.d.ts +4 -0
  66. package/build/HashTableLinearProbing/IHashTableLinearProbingInputs.js +2 -0
  67. package/build/HashTableLinearProbing/IHashTableLinearProbingOptions.d.ts +4 -0
  68. package/build/HashTableLinearProbing/IHashTableLinearProbingOptions.js +2 -0
  69. package/build/HashTableLinearProbing/index.d.ts +20 -0
  70. package/build/HashTableLinearProbing/index.js +120 -0
  71. package/build/HashTableSeparateChaining/HashTableSeparateChainingElement.d.ts +6 -0
  72. package/build/HashTableSeparateChaining/HashTableSeparateChainingElement.js +21 -0
  73. package/build/HashTableSeparateChaining/IHashTableSeparateChaining.d.ts +10 -0
  74. package/build/HashTableSeparateChaining/IHashTableSeparateChaining.js +2 -0
  75. package/build/HashTableSeparateChaining/IHashTableSeparateChainingData.d.ts +5 -0
  76. package/build/HashTableSeparateChaining/IHashTableSeparateChainingData.js +2 -0
  77. package/build/HashTableSeparateChaining/IHashTableSeparateChainingInputs.d.ts +4 -0
  78. package/build/HashTableSeparateChaining/IHashTableSeparateChainingInputs.js +2 -0
  79. package/build/HashTableSeparateChaining/IHashTableSeparateChainingOptions.d.ts +4 -0
  80. package/build/HashTableSeparateChaining/IHashTableSeparateChainingOptions.js +2 -0
  81. package/build/HashTableSeparateChaining/index.d.ts +19 -0
  82. package/build/HashTableSeparateChaining/index.js +104 -0
  83. package/build/LinkedList/ILinkedList.d.ts +13 -0
  84. package/build/LinkedList/ILinkedList.js +2 -0
  85. package/build/LinkedList/ILinkedListItem.d.ts +5 -0
  86. package/build/LinkedList/ILinkedListItem.js +2 -0
  87. package/build/LinkedList/Node.d.ts +5 -0
  88. package/build/LinkedList/Node.js +10 -0
  89. package/build/LinkedList/index.d.ts +24 -0
  90. package/build/LinkedList/index.js +169 -0
  91. package/build/List/index.d.ts +1 -0
  92. package/build/List/index.js +8 -0
  93. package/build/MaxHeap/IMaxHeap.d.ts +9 -0
  94. package/build/MaxHeap/IMaxHeap.js +2 -0
  95. package/build/MaxHeap/IMaxHeapOptions.d.ts +5 -0
  96. package/build/MaxHeap/IMaxHeapOptions.js +2 -0
  97. package/build/MaxHeap/index.d.ts +19 -0
  98. package/build/MaxHeap/index.js +102 -0
  99. package/build/MinHeap/IMinHeap.d.ts +9 -0
  100. package/build/MinHeap/IMinHeap.js +2 -0
  101. package/build/MinHeap/IMinHeapOptions.d.ts +5 -0
  102. package/build/MinHeap/IMinHeapOptions.js +2 -0
  103. package/build/MinHeap/index.d.ts +19 -0
  104. package/build/MinHeap/index.js +102 -0
  105. package/build/Queue/IQueue.d.ts +8 -0
  106. package/build/Queue/IQueue.js +2 -0
  107. package/build/Queue/index.d.ts +11 -0
  108. package/build/Queue/index.js +74 -0
  109. package/build/Set/ISet.d.ts +15 -0
  110. package/build/Set/ISet.js +2 -0
  111. package/build/Set/index.d.ts +21 -0
  112. package/build/Set/index.js +132 -0
  113. package/build/Stack/IStack.d.ts +8 -0
  114. package/build/Stack/IStack.js +2 -0
  115. package/build/Stack/index.d.ts +11 -0
  116. package/build/Stack/index.js +64 -0
  117. package/build/index.d.ts +17 -0
  118. package/build/index.js +40 -0
  119. package/coverage/clover.xml +706 -0
  120. package/coverage/coverage-final.json +19 -0
  121. package/coverage/lcov-report/base.css +224 -0
  122. package/coverage/lcov-report/block-navigation.js +87 -0
  123. package/coverage/lcov-report/favicon.png +0 -0
  124. package/coverage/lcov-report/index.html +327 -0
  125. package/coverage/lcov-report/prettify.css +1 -0
  126. package/coverage/lcov-report/prettify.js +2 -0
  127. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  128. package/coverage/lcov-report/sorter.js +196 -0
  129. package/coverage/lcov-report/src/Algorithms/index.html +117 -0
  130. package/coverage/lcov-report/src/Algorithms/index.ts.html +149 -0
  131. package/coverage/lcov-report/src/Array/index.html +117 -0
  132. package/coverage/lcov-report/src/Array/index.ts.html +290 -0
  133. package/coverage/lcov-report/src/DataStructure/index.html +117 -0
  134. package/coverage/lcov-report/src/DataStructure/index.ts.html +131 -0
  135. package/coverage/lcov-report/src/Deck/index.html +117 -0
  136. package/coverage/lcov-report/src/Deck/index.ts.html +239 -0
  137. package/coverage/lcov-report/src/Dictionary/index.html +117 -0
  138. package/coverage/lcov-report/src/Dictionary/index.ts.html +350 -0
  139. package/coverage/lcov-report/src/DoublyLinkedList/Node.ts.html +113 -0
  140. package/coverage/lcov-report/src/DoublyLinkedList/index.html +132 -0
  141. package/coverage/lcov-report/src/DoublyLinkedList/index.ts.html +836 -0
  142. package/coverage/lcov-report/src/HashTable/index.html +117 -0
  143. package/coverage/lcov-report/src/HashTable/index.ts.html +320 -0
  144. package/coverage/lcov-report/src/HashTableLinearProbing/index.html +117 -0
  145. package/coverage/lcov-report/src/HashTableLinearProbing/index.ts.html +95 -0
  146. package/coverage/lcov-report/src/HashTableSeparateChaining/HashTableSeparateChainingElement.ts.html +149 -0
  147. package/coverage/lcov-report/src/HashTableSeparateChaining/index.html +132 -0
  148. package/coverage/lcov-report/src/HashTableSeparateChaining/index.ts.html +398 -0
  149. package/coverage/lcov-report/src/LinkedList/Node.ts.html +110 -0
  150. package/coverage/lcov-report/src/LinkedList/index.html +132 -0
  151. package/coverage/lcov-report/src/LinkedList/index.ts.html +641 -0
  152. package/coverage/lcov-report/src/List/index.html +117 -0
  153. package/coverage/lcov-report/src/List/index.ts.html +89 -0
  154. package/coverage/lcov-report/src/Queue/index.html +117 -0
  155. package/coverage/lcov-report/src/Queue/index.ts.html +218 -0
  156. package/coverage/lcov-report/src/Set/index.html +117 -0
  157. package/coverage/lcov-report/src/Set/index.ts.html +488 -0
  158. package/coverage/lcov-report/src/Stack/index.html +117 -0
  159. package/coverage/lcov-report/src/Stack/index.ts.html +215 -0
  160. package/coverage/lcov-report/src/index.html +117 -0
  161. package/coverage/lcov-report/src/index.ts.html +125 -0
  162. package/coverage/lcov.info +1346 -0
  163. package/package.json +42 -0
  164. package/src/Algorithms/index.spec.ts +15 -0
  165. package/src/Algorithms/index.ts +21 -0
  166. package/src/Array/IArray.ts +10 -0
  167. package/src/Array/index.spec.ts +151 -0
  168. package/src/Array/index.ts +68 -0
  169. package/src/BinarySearchTree/BinarySearchTreeNode.spec.ts +29 -0
  170. package/src/BinarySearchTree/BinarySearchTreeNode.ts +23 -0
  171. package/src/BinarySearchTree/IBinarySearchNodeOptions.ts +6 -0
  172. package/src/BinarySearchTree/IBinarySearchTree.ts +16 -0
  173. package/src/BinarySearchTree/IBinarySearchTreeData.ts +9 -0
  174. package/src/BinarySearchTree/index.spec.ts +486 -0
  175. package/src/BinarySearchTree/index.ts +198 -0
  176. package/src/DataStructure/index.spec.ts +25 -0
  177. package/src/DataStructure/index.ts +15 -0
  178. package/src/Deck/IDeck.ts +10 -0
  179. package/src/Deck/index.spec.ts +131 -0
  180. package/src/Deck/index.ts +51 -0
  181. package/src/Dictionary/IDictionary.ts +18 -0
  182. package/src/Dictionary/IDictionaryData.ts +5 -0
  183. package/src/Dictionary/index.spec.ts +310 -0
  184. package/src/Dictionary/index.ts +88 -0
  185. package/src/DoublyLinkedList/IDoublyLinkedList.ts +18 -0
  186. package/src/DoublyLinkedList/Node.ts +9 -0
  187. package/src/DoublyLinkedList/index.spec.ts +478 -0
  188. package/src/DoublyLinkedList/index.ts +250 -0
  189. package/src/Graph/GraphNodeNotFoundError.ts +7 -0
  190. package/src/Graph/GraphSearchNodeStates.ts +8 -0
  191. package/src/Graph/IGraph.ts +15 -0
  192. package/src/Graph/IGraphOptions.ts +6 -0
  193. package/src/Graph/index.spec.ts +318 -0
  194. package/src/Graph/index.ts +170 -0
  195. package/src/HashTable/IHashTable.ts +11 -0
  196. package/src/HashTable/IHashTableData.ts +5 -0
  197. package/src/HashTable/IHashTableInputs.ts +5 -0
  198. package/src/HashTable/IHashTableOptions.ts +5 -0
  199. package/src/HashTable/index.spec.ts +136 -0
  200. package/src/HashTable/index.ts +78 -0
  201. package/src/HashTableLinearProbing/HashTableLinearProbingElement.spec.ts +28 -0
  202. package/src/HashTableLinearProbing/HashTableLinearProbingElement.ts +18 -0
  203. package/src/HashTableLinearProbing/IHashTableLinearProbing.ts +11 -0
  204. package/src/HashTableLinearProbing/IHashTableLinearProbingData.ts +7 -0
  205. package/src/HashTableLinearProbing/IHashTableLinearProbingInputs.ts +5 -0
  206. package/src/HashTableLinearProbing/IHashTableLinearProbingOptions.ts +5 -0
  207. package/src/HashTableLinearProbing/index.spec.ts +266 -0
  208. package/src/HashTableLinearProbing/index.ts +128 -0
  209. package/src/HashTableSeparateChaining/HashTableSeparateChainingElement.ts +21 -0
  210. package/src/HashTableSeparateChaining/IHashTableSeparateChaining.ts +12 -0
  211. package/src/HashTableSeparateChaining/IHashTableSeparateChainingData.ts +7 -0
  212. package/src/HashTableSeparateChaining/IHashTableSeparateChainingInputs.ts +5 -0
  213. package/src/HashTableSeparateChaining/IHashTableSeparateChainingOptions.ts +5 -0
  214. package/src/HashTableSeparateChaining/index.spec.ts +173 -0
  215. package/src/HashTableSeparateChaining/index.ts +104 -0
  216. package/src/LinkedList/ILinkedList.ts +15 -0
  217. package/src/LinkedList/ILinkedListItem.ts +6 -0
  218. package/src/LinkedList/Node.ts +8 -0
  219. package/src/LinkedList/index.spec.ts +355 -0
  220. package/src/LinkedList/index.ts +185 -0
  221. package/src/List/index.ts +1 -0
  222. package/src/MaxHeap/IMaxHeap.ts +10 -0
  223. package/src/MaxHeap/IMaxHeapOptions.ts +6 -0
  224. package/src/MaxHeap/index.spec.ts +161 -0
  225. package/src/MaxHeap/index.ts +91 -0
  226. package/src/MinHeap/IMinHeap.ts +10 -0
  227. package/src/MinHeap/IMinHeapOptions.ts +6 -0
  228. package/src/MinHeap/index.spec.ts +161 -0
  229. package/src/MinHeap/index.ts +91 -0
  230. package/src/Queue/IQueue.ts +9 -0
  231. package/src/Queue/index.spec.ts +92 -0
  232. package/src/Queue/index.ts +44 -0
  233. package/src/Set/ISet.ts +17 -0
  234. package/src/Set/index.spec.ts +246 -0
  235. package/src/Set/index.ts +134 -0
  236. package/src/Stack/IStack.ts +9 -0
  237. package/src/Stack/index.spec.ts +108 -0
  238. package/src/Stack/index.ts +43 -0
  239. package/src/index.ts +17 -0
  240. package/tsconfig.json +104 -0
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@gabrielrufino/cube",
3
+ "version": "1.0.1",
4
+ "description": "Data Structures and Algorithms made in Typescript",
5
+ "main": "build/index.js",
6
+ "scripts": {
7
+ "lint": "eslint src/**/*.ts",
8
+ "test": "jest",
9
+ "test:cov": "npm t -- --coverage",
10
+ "test:watch": "npm t -- --watch",
11
+ "build": "rimraf ./build && tsc"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/gabrielrufino/cube.git"
16
+ },
17
+ "keywords": [
18
+ "Data",
19
+ "Structures",
20
+ "Algorithms"
21
+ ],
22
+ "author": "Gabriel Rufino <contato@gabrielrufino.com>",
23
+ "license": "UNLICENSED",
24
+ "bugs": {
25
+ "url": "https://github.com/gabrielrufino/cube/issues"
26
+ },
27
+ "homepage": "https://github.com/gabrielrufino/cube#readme",
28
+ "devDependencies": {
29
+ "@types/faker": "^5.5.9",
30
+ "@types/jest": "^27.4.0",
31
+ "@typescript-eslint/eslint-plugin": "^5.10.1",
32
+ "@typescript-eslint/parser": "^5.10.1",
33
+ "eslint": "^8.8.0",
34
+ "eslint-config-xo": "^0.39.0",
35
+ "faker": "^5.5.3",
36
+ "jest": "^27.4.7",
37
+ "rimraf": "^3.0.2",
38
+ "ts-jest": "^27.1.3",
39
+ "ts-node": "^10.4.0",
40
+ "typescript": "^4.5.5"
41
+ }
42
+ }
@@ -0,0 +1,15 @@
1
+ import {describe, it, expect} from '@jest/globals';
2
+
3
+ import Algorithms from './';
4
+
5
+ describe('Algorithms', () => {
6
+ describe('.decimalToBinary()', () => {
7
+ it('Should return the correct result', () => {
8
+ const n = 10;
9
+ const expected = '1010';
10
+
11
+ const result = Algorithms.decimalToBinary(n);
12
+ expect(result).toBe(expected);
13
+ });
14
+ });
15
+ });
@@ -0,0 +1,21 @@
1
+ import {Stack} from '..';
2
+
3
+ export default class Algorithms {
4
+ public static decimalToBinary(n: number) {
5
+ const stack = new Stack();
6
+
7
+ while (n > 0) {
8
+ const rem = Math.floor(n % 2);
9
+ stack.push(rem);
10
+ n = Math.floor(n / 2);
11
+ }
12
+
13
+ let binary = '';
14
+ while (!stack.isEmpty) {
15
+ // eslint-disable-next-line no-unsafe-optional-chaining
16
+ binary += stack.pop()?.toString();
17
+ }
18
+
19
+ return binary;
20
+ }
21
+ }
@@ -0,0 +1,10 @@
1
+ interface IArray<T> {
2
+ insertInLastPosition(_element: T): T;
3
+ insertInFirstPosition(_element: T): T;
4
+ insertInPosition(_position: number, _element: T): T;
5
+ removeFromLastPosition(): T | undefined;
6
+ removeFromFirstPosition(): T | undefined;
7
+ removeFromPosition(_position: number): T | undefined;
8
+ }
9
+
10
+ export default IArray;
@@ -0,0 +1,151 @@
1
+ import {describe, expect, it} from '@jest/globals';
2
+ import faker from 'faker';
3
+
4
+ import Array from './';
5
+
6
+ describe('Array', () => {
7
+ it('Should create an empty array with no problems', () => {
8
+ const array = new Array();
9
+
10
+ expect(array.data).toEqual([]);
11
+ expect(array.size).toBe(0);
12
+ });
13
+
14
+ it('Should create a filled array with no problems', () => {
15
+ const array = new Array(1, 2, 3, 4, 5);
16
+
17
+ expect(array.data).toEqual([1, 2, 3, 4, 5]);
18
+ expect(array.size).toBe(5);
19
+ });
20
+
21
+ it('Should return the correct size of the array', () => {
22
+ const size = faker.datatype.number(200);
23
+ const params = [];
24
+
25
+ for (let i = 0; i < size; i++) {
26
+ params.push(faker.datatype.number());
27
+ }
28
+
29
+ const array = new Array(...params);
30
+
31
+ expect(array.size).toBe(size);
32
+ });
33
+
34
+ describe('.insertInLastPosition()', () => {
35
+ it('Should insert a new element in the last position', () => {
36
+ const array = new Array(1, 2, 3);
37
+ array.insertInLastPosition(4);
38
+
39
+ expect(array.data).toEqual([1, 2, 3, 4]);
40
+ });
41
+ });
42
+
43
+ describe('.insertInFirstPosition()', () => {
44
+ it('Should insert a new element in the first position', () => {
45
+ const array = new Array(2, 3, 4);
46
+ array.insertInFirstPosition(1);
47
+
48
+ expect(array.data).toEqual([1, 2, 3, 4]);
49
+ });
50
+
51
+ it('Should insert a new element in a arbitrary position of an array', () => {
52
+ const array = new Array(1, 2, 4);
53
+ array.insertInPosition(2, 3);
54
+
55
+ expect(array.data).toEqual([1, 2, 3, 4]);
56
+ });
57
+ });
58
+
59
+ describe('.removeFromLastPosition()', () => {
60
+ it('Should remove and return the last element of the array', () => {
61
+ const array = new Array(1, 2, 3, 4);
62
+ const element = array.removeFromLastPosition();
63
+
64
+ expect(array.data).toEqual([1, 2, 3]);
65
+ expect(element).toBe(4);
66
+ });
67
+
68
+ it('Should ruturn undefined when trying to remove the last element of an empty array', () => {
69
+ const array = new Array();
70
+ const element = array.removeFromLastPosition();
71
+
72
+ expect(array.data).toEqual([]);
73
+ expect(element).toBe(undefined);
74
+ });
75
+ });
76
+
77
+ describe('.removeFromFirstPosition()', () => {
78
+ it('Should remove and return the first element of the array', () => {
79
+ const array = new Array(1, 2, 3, 4);
80
+ const element = array.removeFromFirstPosition();
81
+
82
+ expect(array.data).toEqual([2, 3, 4]);
83
+ expect(element).toBe(1);
84
+ });
85
+
86
+ it('Should ruturn undefined when trying to remove the first element of an empty array', () => {
87
+ const array = new Array();
88
+ const element = array.removeFromFirstPosition();
89
+
90
+ expect(array.data).toEqual([]);
91
+ expect(element).toBe(undefined);
92
+ });
93
+ });
94
+
95
+ describe('.removeFromPosition()', () => {
96
+ it('Should remove and return the element of an arbitrary position from array', () => {
97
+ const array = new Array(1, 2, 3, 4);
98
+ const element = array.removeFromPosition(2);
99
+
100
+ expect(array.data).toEqual([1, 2, 4]);
101
+ expect(element).toBe(3);
102
+ });
103
+
104
+ it('Should return return when trying to remove the element of an arbitrary position from an empty array', () => {
105
+ const array = new Array();
106
+ const element = array.removeFromPosition(faker.datatype.number());
107
+
108
+ expect(array.data).toEqual([]);
109
+ expect(element).toBe(undefined);
110
+ });
111
+ });
112
+
113
+ describe('Bidimensional array', () => {
114
+ it('Should create an bidimensional array with no problems', () => {
115
+ const array = new Array<Array>(
116
+ new Array(1, 2, 3),
117
+ new Array(4, 5, 6),
118
+ new Array(7, 8, 9),
119
+ );
120
+
121
+ expect(array.data).toEqual([
122
+ new Array(1, 2, 3),
123
+ new Array(4, 5, 6),
124
+ new Array(7, 8, 9),
125
+ ]);
126
+ expect(array.data[0].data).toEqual([1, 2, 3]);
127
+ expect(array.data[1].data).toEqual([4, 5, 6]);
128
+ expect(array.data[2].data).toEqual([7, 8, 9]);
129
+ expect(array.size).toBe(3);
130
+ expect(array.data[0].size).toEqual(3);
131
+ expect(array.data[1].size).toEqual(3);
132
+ expect(array.data[2].size).toEqual(3);
133
+ });
134
+ });
135
+
136
+ describe('Conversion to primitive', () => {
137
+ it('Should return the elements separated by comma in string conversion', () => {
138
+ const array = new Array(1, 2, 3, 4);
139
+ const string = String(array);
140
+
141
+ expect(string).toBe('1, 2, 3, 4');
142
+ });
143
+
144
+ it('Should return the array size in number conversion', () => {
145
+ const array = new Array(1, 2, 3, 4);
146
+ const number = Number(array);
147
+
148
+ expect(number).toBe(4);
149
+ });
150
+ });
151
+ });
@@ -0,0 +1,68 @@
1
+ import DataStructure from '../DataStructure';
2
+ import IArray from './IArray';
3
+
4
+ export default class Array<T = number> extends DataStructure<T> implements IArray<T> {
5
+ constructor(...inputs: T[]) {
6
+ super(inputs);
7
+ }
8
+
9
+ public insertInLastPosition(element: T): T {
10
+ this._data[this.size] = element;
11
+ return element;
12
+ }
13
+
14
+ public insertInFirstPosition(element: T): T {
15
+ for (let i = this.size; i > 0; i--) {
16
+ this._data[i] = this._data[i - 1];
17
+ }
18
+
19
+ this._data[0] = element;
20
+ return element;
21
+ }
22
+
23
+ public insertInPosition(position: number, element: T): T {
24
+ this._data.splice(position, 0, element);
25
+ return element;
26
+ }
27
+
28
+ public removeFromLastPosition(): T | undefined {
29
+ const element = this._data[this.size - 1];
30
+ const array = new Array<T>();
31
+
32
+ for (let i = 0; i < this.size - 1; i++) {
33
+ array.insertInLastPosition(this._data[i]);
34
+ }
35
+
36
+ this._data = array.data;
37
+ return element;
38
+ }
39
+
40
+ public removeFromFirstPosition(): T | undefined {
41
+ const element = this._data[0];
42
+ const array = new Array<T>();
43
+
44
+ for (let i = 0; i < this.size - 1; i++) {
45
+ array.insertInLastPosition(this._data[i + 1]);
46
+ }
47
+
48
+ this._data = array.data;
49
+ return element;
50
+ }
51
+
52
+ public removeFromPosition(position: number): T | undefined {
53
+ const [element] = this._data.splice(position, 1);
54
+ return element;
55
+ }
56
+
57
+ private [Symbol.toPrimitive](type: string): string | number | null {
58
+ if (type === 'string') {
59
+ return this.data.join(', ');
60
+ }
61
+
62
+ if (type === 'number') {
63
+ return this.size;
64
+ }
65
+
66
+ return null;
67
+ }
68
+ }
@@ -0,0 +1,29 @@
1
+ import {describe, it, expect} from '@jest/globals';
2
+ import faker from 'faker';
3
+
4
+ import BinarySearchTreeNode from './BinarySearchTreeNode';
5
+
6
+ describe('BinarySearchTreeNode', () => {
7
+ it('Should create a node without problems', () => {
8
+ const number = faker.datatype.number();
9
+ const binarySearchTreeNode = new BinarySearchTreeNode<number>(number);
10
+
11
+ expect(binarySearchTreeNode.value).toBe(number);
12
+ expect(binarySearchTreeNode.left).toBeNull();
13
+ expect(binarySearchTreeNode.right).toBeNull();
14
+ });
15
+
16
+ describe('Conversion to primitive', () => {
17
+ it('Should return the value poiting to left and right nodes in string conversion', () => {
18
+ const rootNumber = faker.datatype.number();
19
+ const leftNumber = faker.datatype.number();
20
+ const rightNumber = faker.datatype.number();
21
+ const node = new BinarySearchTreeNode(rootNumber);
22
+ node.left = new BinarySearchTreeNode(leftNumber);
23
+ node.right = new BinarySearchTreeNode(rightNumber);
24
+ const string = String(node);
25
+
26
+ expect(string).toBe(`[[null] <= (${leftNumber}) => [null]] <= (${rootNumber}) => [[null] <= (${rightNumber}) => [null]]`);
27
+ });
28
+ });
29
+ });
@@ -0,0 +1,23 @@
1
+ export default class BinarySearchTreeNode<T> {
2
+ public value: T;
3
+ public left: BinarySearchTreeNode<T> | null = null;
4
+ public right: BinarySearchTreeNode<T> | null = null;
5
+
6
+ constructor(value: T) {
7
+ this.value = value;
8
+ this.left = null;
9
+ this.right = null;
10
+ }
11
+
12
+ [Symbol.toPrimitive](type: string): string | number | null {
13
+ if (type === 'string') {
14
+ return `[${this.left}] <= (${this.value}) => [${this.right}]`;
15
+ }
16
+
17
+ if (type === 'number') {
18
+ return 1 + (this.left ? 1 : 0) + (this.right ? 1 : 0);
19
+ }
20
+
21
+ return null;
22
+ }
23
+ }
@@ -0,0 +1,6 @@
1
+ interface IBinarySearchNodeOptions<T> {
2
+ inputs?: T[];
3
+ lessThanOrEqualTo?: (_value1: T, _value2: T) => boolean;
4
+ }
5
+
6
+ export default IBinarySearchNodeOptions;
@@ -0,0 +1,16 @@
1
+ import IBinarySearchTreeData from './IBinarySearchTreeData';
2
+
3
+ interface IBinarySearchTree<T> {
4
+ get data(): IBinarySearchTreeData<T>;
5
+ get size(): number;
6
+ get min(): T | null;
7
+ get max(): T | null;
8
+ insert(_value: T): T;
9
+ walkInOrder(_callback: (_value: T) => any): void;
10
+ walkPreOrder(_callback: (_value: T) => any): void;
11
+ walkPostOrder(_callback: (_value: T) => any): void;
12
+ search(_value: T): boolean;
13
+ remove(_value: T): T | null;
14
+ }
15
+
16
+ export default IBinarySearchTree;
@@ -0,0 +1,9 @@
1
+ import BinarySearchTreeNode from './BinarySearchTreeNode';
2
+
3
+ interface IBinarySearchTreeData<T> {
4
+ left: BinarySearchTreeNode<T> | null;
5
+ value: T | null;
6
+ right: BinarySearchTreeNode<T> | null;
7
+ }
8
+
9
+ export default IBinarySearchTreeData;