data-structure-typed 1.18.7 → 1.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (269) hide show
  1. package/README.md +208 -419
  2. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +198 -167
  3. package/dist/data-structures/binary-tree/abstract-binary-tree.js +413 -398
  4. package/dist/data-structures/binary-tree/avl-tree.d.ts +24 -16
  5. package/dist/data-structures/binary-tree/avl-tree.js +23 -17
  6. package/dist/data-structures/binary-tree/binary-tree.d.ts +12 -26
  7. package/dist/data-structures/binary-tree/binary-tree.js +12 -27
  8. package/dist/data-structures/binary-tree/bst.d.ts +66 -90
  9. package/dist/data-structures/binary-tree/bst.js +85 -163
  10. package/dist/data-structures/binary-tree/rb-tree.d.ts +6 -17
  11. package/dist/data-structures/binary-tree/rb-tree.js +5 -17
  12. package/dist/data-structures/binary-tree/tree-multiset.d.ts +187 -18
  13. package/dist/data-structures/binary-tree/tree-multiset.js +716 -39
  14. package/dist/data-structures/graph/abstract-graph.d.ts +112 -52
  15. package/dist/data-structures/graph/abstract-graph.js +138 -72
  16. package/dist/data-structures/graph/directed-graph.d.ts +104 -101
  17. package/dist/data-structures/graph/directed-graph.js +134 -129
  18. package/dist/data-structures/graph/undirected-graph.d.ts +66 -65
  19. package/dist/data-structures/graph/undirected-graph.js +107 -106
  20. package/dist/data-structures/interfaces/abstract-binary-tree.d.ts +12 -18
  21. package/dist/data-structures/interfaces/abstract-graph.d.ts +2 -4
  22. package/dist/data-structures/interfaces/avl-tree.d.ts +2 -2
  23. package/dist/data-structures/interfaces/binary-tree.d.ts +1 -1
  24. package/dist/data-structures/interfaces/bst.d.ts +4 -5
  25. package/dist/data-structures/interfaces/rb-tree.d.ts +2 -3
  26. package/dist/data-structures/interfaces/tree-multiset.d.ts +7 -1
  27. package/dist/data-structures/tree/tree.d.ts +1 -4
  28. package/dist/data-structures/tree/tree.js +1 -12
  29. package/dist/data-structures/types/abstract-binary-tree.d.ts +10 -6
  30. package/dist/data-structures/types/abstract-binary-tree.js +9 -5
  31. package/dist/data-structures/types/avl-tree.d.ts +1 -1
  32. package/dist/data-structures/types/binary-tree.d.ts +1 -1
  33. package/dist/data-structures/types/bst.d.ts +1 -1
  34. package/dist/data-structures/types/bst.js +1 -1
  35. package/dist/data-structures/types/directed-graph.js +1 -1
  36. package/dist/data-structures/types/helpers.d.ts +1 -8
  37. package/dist/data-structures/types/rb-tree.d.ts +2 -0
  38. package/dist/data-structures/types/rb-tree.js +1 -1
  39. package/dist/data-structures/types/tree-multiset.d.ts +5 -5
  40. package/dist/utils/index.d.ts +1 -0
  41. package/dist/utils/index.js +1 -0
  42. package/dist/utils/types/index.d.ts +1 -0
  43. package/dist/utils/types/index.js +1 -0
  44. package/dist/utils/types/validate-type.d.ts +19 -0
  45. package/dist/utils/types/validate-type.js +2 -0
  46. package/dist/utils/utils.d.ts +2 -2
  47. package/dist/utils/utils.js +0 -62
  48. package/dist/utils/validate-type.d.ts +45 -0
  49. package/dist/utils/validate-type.js +58 -0
  50. package/package.json +5 -2
  51. package/backup/recursive-type/src/assets/complexities-diff.jpg +0 -0
  52. package/backup/recursive-type/src/assets/data-structure-complexities.jpg +0 -0
  53. package/backup/recursive-type/src/assets/logo.png +0 -0
  54. package/backup/recursive-type/src/assets/overview-diagram-of-data-structures.png +0 -0
  55. package/backup/recursive-type/src/data-structures/binary-tree/aa-tree.ts +0 -3
  56. package/backup/recursive-type/src/data-structures/binary-tree/avl-tree.ts +0 -288
  57. package/backup/recursive-type/src/data-structures/binary-tree/b-tree.ts +0 -3
  58. package/backup/recursive-type/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -78
  59. package/backup/recursive-type/src/data-structures/binary-tree/binary-tree.ts +0 -1502
  60. package/backup/recursive-type/src/data-structures/binary-tree/bst.ts +0 -503
  61. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  62. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  63. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  64. package/backup/recursive-type/src/data-structures/binary-tree/index.ts +0 -11
  65. package/backup/recursive-type/src/data-structures/binary-tree/rb-tree.ts +0 -110
  66. package/backup/recursive-type/src/data-structures/binary-tree/segment-tree.ts +0 -243
  67. package/backup/recursive-type/src/data-structures/binary-tree/splay-tree.ts +0 -3
  68. package/backup/recursive-type/src/data-structures/binary-tree/tree-multiset.ts +0 -55
  69. package/backup/recursive-type/src/data-structures/binary-tree/two-three-tree.ts +0 -3
  70. package/backup/recursive-type/src/data-structures/diagrams/README.md +0 -5
  71. package/backup/recursive-type/src/data-structures/graph/abstract-graph.ts +0 -985
  72. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  73. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  74. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  75. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  76. package/backup/recursive-type/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  77. package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  78. package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  79. package/backup/recursive-type/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  80. package/backup/recursive-type/src/data-structures/graph/diagrams/mst.jpg +0 -0
  81. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  82. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  83. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  84. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  85. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  86. package/backup/recursive-type/src/data-structures/graph/directed-graph.ts +0 -478
  87. package/backup/recursive-type/src/data-structures/graph/index.ts +0 -3
  88. package/backup/recursive-type/src/data-structures/graph/undirected-graph.ts +0 -293
  89. package/backup/recursive-type/src/data-structures/hash/coordinate-map.ts +0 -67
  90. package/backup/recursive-type/src/data-structures/hash/coordinate-set.ts +0 -56
  91. package/backup/recursive-type/src/data-structures/hash/hash-table.ts +0 -3
  92. package/backup/recursive-type/src/data-structures/hash/index.ts +0 -6
  93. package/backup/recursive-type/src/data-structures/hash/pair.ts +0 -3
  94. package/backup/recursive-type/src/data-structures/hash/tree-map.ts +0 -3
  95. package/backup/recursive-type/src/data-structures/hash/tree-set.ts +0 -3
  96. package/backup/recursive-type/src/data-structures/heap/heap.ts +0 -176
  97. package/backup/recursive-type/src/data-structures/heap/index.ts +0 -3
  98. package/backup/recursive-type/src/data-structures/heap/max-heap.ts +0 -31
  99. package/backup/recursive-type/src/data-structures/heap/min-heap.ts +0 -34
  100. package/backup/recursive-type/src/data-structures/index.ts +0 -15
  101. package/backup/recursive-type/src/data-structures/interfaces/abstract-graph.ts +0 -42
  102. package/backup/recursive-type/src/data-structures/interfaces/avl-tree.ts +0 -1
  103. package/backup/recursive-type/src/data-structures/interfaces/binary-tree.ts +0 -56
  104. package/backup/recursive-type/src/data-structures/interfaces/bst.ts +0 -1
  105. package/backup/recursive-type/src/data-structures/interfaces/directed-graph.ts +0 -15
  106. package/backup/recursive-type/src/data-structures/interfaces/doubly-linked-list.ts +0 -1
  107. package/backup/recursive-type/src/data-structures/interfaces/heap.ts +0 -1
  108. package/backup/recursive-type/src/data-structures/interfaces/index.ts +0 -13
  109. package/backup/recursive-type/src/data-structures/interfaces/navigator.ts +0 -1
  110. package/backup/recursive-type/src/data-structures/interfaces/priority-queue.ts +0 -1
  111. package/backup/recursive-type/src/data-structures/interfaces/segment-tree.ts +0 -1
  112. package/backup/recursive-type/src/data-structures/interfaces/singly-linked-list.ts +0 -1
  113. package/backup/recursive-type/src/data-structures/interfaces/tree-multiset.ts +0 -1
  114. package/backup/recursive-type/src/data-structures/interfaces/undirected-graph.ts +0 -3
  115. package/backup/recursive-type/src/data-structures/linked-list/doubly-linked-list.ts +0 -573
  116. package/backup/recursive-type/src/data-structures/linked-list/index.ts +0 -3
  117. package/backup/recursive-type/src/data-structures/linked-list/singly-linked-list.ts +0 -490
  118. package/backup/recursive-type/src/data-structures/linked-list/skip-linked-list.ts +0 -3
  119. package/backup/recursive-type/src/data-structures/matrix/index.ts +0 -4
  120. package/backup/recursive-type/src/data-structures/matrix/matrix.ts +0 -27
  121. package/backup/recursive-type/src/data-structures/matrix/matrix2d.ts +0 -208
  122. package/backup/recursive-type/src/data-structures/matrix/navigator.ts +0 -122
  123. package/backup/recursive-type/src/data-structures/matrix/vector2d.ts +0 -316
  124. package/backup/recursive-type/src/data-structures/priority-queue/index.ts +0 -3
  125. package/backup/recursive-type/src/data-structures/priority-queue/max-priority-queue.ts +0 -49
  126. package/backup/recursive-type/src/data-structures/priority-queue/min-priority-queue.ts +0 -50
  127. package/backup/recursive-type/src/data-structures/priority-queue/priority-queue.ts +0 -354
  128. package/backup/recursive-type/src/data-structures/queue/deque.ts +0 -251
  129. package/backup/recursive-type/src/data-structures/queue/index.ts +0 -2
  130. package/backup/recursive-type/src/data-structures/queue/queue.ts +0 -120
  131. package/backup/recursive-type/src/data-structures/stack/index.ts +0 -1
  132. package/backup/recursive-type/src/data-structures/stack/stack.ts +0 -98
  133. package/backup/recursive-type/src/data-structures/tree/index.ts +0 -1
  134. package/backup/recursive-type/src/data-structures/tree/tree.ts +0 -80
  135. package/backup/recursive-type/src/data-structures/trie/index.ts +0 -1
  136. package/backup/recursive-type/src/data-structures/trie/trie.ts +0 -227
  137. package/backup/recursive-type/src/data-structures/types/abstract-graph.ts +0 -5
  138. package/backup/recursive-type/src/data-structures/types/avl-tree.ts +0 -8
  139. package/backup/recursive-type/src/data-structures/types/binary-tree.ts +0 -10
  140. package/backup/recursive-type/src/data-structures/types/bst.ts +0 -6
  141. package/backup/recursive-type/src/data-structures/types/directed-graph.ts +0 -8
  142. package/backup/recursive-type/src/data-structures/types/doubly-linked-list.ts +0 -1
  143. package/backup/recursive-type/src/data-structures/types/heap.ts +0 -5
  144. package/backup/recursive-type/src/data-structures/types/index.ts +0 -12
  145. package/backup/recursive-type/src/data-structures/types/navigator.ts +0 -13
  146. package/backup/recursive-type/src/data-structures/types/priority-queue.ts +0 -9
  147. package/backup/recursive-type/src/data-structures/types/segment-tree.ts +0 -1
  148. package/backup/recursive-type/src/data-structures/types/singly-linked-list.ts +0 -1
  149. package/backup/recursive-type/src/data-structures/types/tree-multiset.ts +0 -1
  150. package/backup/recursive-type/src/index.ts +0 -1
  151. package/backup/recursive-type/src/utils/index.ts +0 -2
  152. package/backup/recursive-type/src/utils/types/index.ts +0 -1
  153. package/backup/recursive-type/src/utils/types/utils.ts +0 -6
  154. package/backup/recursive-type/src/utils/utils.ts +0 -78
  155. package/docs/.nojekyll +0 -1
  156. package/docs/assets/highlight.css +0 -92
  157. package/docs/assets/main.js +0 -58
  158. package/docs/assets/search.js +0 -1
  159. package/docs/assets/style.css +0 -1367
  160. package/docs/classes/AVLTree.html +0 -2451
  161. package/docs/classes/AVLTreeNode.html +0 -499
  162. package/docs/classes/AaTree.html +0 -172
  163. package/docs/classes/AbstractBinaryTree.html +0 -2118
  164. package/docs/classes/AbstractBinaryTreeNode.html +0 -524
  165. package/docs/classes/AbstractEdge.html +0 -295
  166. package/docs/classes/AbstractGraph.html +0 -1043
  167. package/docs/classes/AbstractVertex.html +0 -258
  168. package/docs/classes/ArrayDeque.html +0 -439
  169. package/docs/classes/BST.html +0 -2297
  170. package/docs/classes/BSTNode.html +0 -503
  171. package/docs/classes/BTree.html +0 -172
  172. package/docs/classes/BinaryIndexedTree.html +0 -341
  173. package/docs/classes/BinaryTree.html +0 -2133
  174. package/docs/classes/BinaryTreeNode.html +0 -501
  175. package/docs/classes/Character.html +0 -220
  176. package/docs/classes/CoordinateMap.html +0 -483
  177. package/docs/classes/CoordinateSet.html +0 -444
  178. package/docs/classes/Deque.html +0 -975
  179. package/docs/classes/DirectedEdge.html +0 -366
  180. package/docs/classes/DirectedGraph.html +0 -1443
  181. package/docs/classes/DirectedVertex.html +0 -254
  182. package/docs/classes/DoublyLinkedList.html +0 -968
  183. package/docs/classes/DoublyLinkedListNode.html +0 -297
  184. package/docs/classes/HashTable.html +0 -172
  185. package/docs/classes/Heap.html +0 -423
  186. package/docs/classes/HeapItem.html +0 -255
  187. package/docs/classes/Matrix2D.html +0 -502
  188. package/docs/classes/MatrixNTI2D.html +0 -240
  189. package/docs/classes/MaxHeap.html +0 -436
  190. package/docs/classes/MaxPriorityQueue.html +0 -836
  191. package/docs/classes/MinHeap.html +0 -437
  192. package/docs/classes/MinPriorityQueue.html +0 -838
  193. package/docs/classes/Navigator.html +0 -313
  194. package/docs/classes/ObjectDeque.html +0 -455
  195. package/docs/classes/Pair.html +0 -172
  196. package/docs/classes/PriorityQueue.html +0 -760
  197. package/docs/classes/Queue.html +0 -392
  198. package/docs/classes/RBTree.html +0 -2388
  199. package/docs/classes/RBTreeNode.html +0 -516
  200. package/docs/classes/SegmentTree.html +0 -434
  201. package/docs/classes/SegmentTreeNode.html +0 -357
  202. package/docs/classes/SinglyLinkedList.html +0 -788
  203. package/docs/classes/SinglyLinkedListNode.html +0 -270
  204. package/docs/classes/SkipLinkedList.html +0 -172
  205. package/docs/classes/SplayTree.html +0 -172
  206. package/docs/classes/Stack.html +0 -368
  207. package/docs/classes/TreeMap.html +0 -172
  208. package/docs/classes/TreeMultiSet.html +0 -2297
  209. package/docs/classes/TreeMultiSetNode.html +0 -499
  210. package/docs/classes/TreeNode.html +0 -343
  211. package/docs/classes/TreeSet.html +0 -172
  212. package/docs/classes/Trie.html +0 -372
  213. package/docs/classes/TrieNode.html +0 -280
  214. package/docs/classes/TwoThreeTree.html +0 -172
  215. package/docs/classes/UndirectedEdge.html +0 -337
  216. package/docs/classes/UndirectedGraph.html +0 -1210
  217. package/docs/classes/UndirectedVertex.html +0 -254
  218. package/docs/classes/Vector2D.html +0 -805
  219. package/docs/enums/CP.html +0 -181
  220. package/docs/enums/FamilyPosition.html +0 -181
  221. package/docs/enums/LoopType.html +0 -182
  222. package/docs/enums/RBColor.html +0 -174
  223. package/docs/enums/TopologicalProperty.html +0 -181
  224. package/docs/index.html +0 -645
  225. package/docs/interfaces/IAVLTree.html +0 -1378
  226. package/docs/interfaces/IAVLTreeNode.html +0 -405
  227. package/docs/interfaces/IAbstractBinaryTree.html +0 -1124
  228. package/docs/interfaces/IAbstractBinaryTreeNode.html +0 -384
  229. package/docs/interfaces/IAbstractGraph.html +0 -433
  230. package/docs/interfaces/IBST.html +0 -1271
  231. package/docs/interfaces/IBSTNode.html +0 -408
  232. package/docs/interfaces/IDirectedGraph.html +0 -572
  233. package/docs/interfaces/IUNDirectedGraph.html +0 -465
  234. package/docs/modules.html +0 -262
  235. package/docs/types/AVLTreeOptions.html +0 -150
  236. package/docs/types/AbstractBinaryTreeNodeProperties.html +0 -152
  237. package/docs/types/AbstractBinaryTreeNodeProperty.html +0 -152
  238. package/docs/types/AbstractBinaryTreeOptions.html +0 -156
  239. package/docs/types/AbstractRecursiveBinaryTreeNode.html +0 -152
  240. package/docs/types/BSTComparator.html +0 -162
  241. package/docs/types/BSTOptions.html +0 -152
  242. package/docs/types/BinaryTreeDeletedResult.html +0 -159
  243. package/docs/types/BinaryTreeNodeId.html +0 -147
  244. package/docs/types/BinaryTreeNodePropertyName.html +0 -147
  245. package/docs/types/BinaryTreeOptions.html +0 -150
  246. package/docs/types/DFSOrderPattern.html +0 -147
  247. package/docs/types/DijkstraResult.html +0 -167
  248. package/docs/types/Direction.html +0 -147
  249. package/docs/types/EdgeId.html +0 -147
  250. package/docs/types/HeapOptions.html +0 -168
  251. package/docs/types/IdObject.html +0 -157
  252. package/docs/types/KeyValObject.html +0 -152
  253. package/docs/types/NavigatorParams.html +0 -181
  254. package/docs/types/NodeOrPropertyName.html +0 -147
  255. package/docs/types/PriorityQueueComparator.html +0 -167
  256. package/docs/types/PriorityQueueDFSOrderPattern.html +0 -147
  257. package/docs/types/PriorityQueueOptions.html +0 -161
  258. package/docs/types/RBTreeOptions.html +0 -150
  259. package/docs/types/RecursiveAVLTreeNode.html +0 -152
  260. package/docs/types/RecursiveBSTNode.html +0 -152
  261. package/docs/types/RecursiveBinaryTreeNode.html +0 -152
  262. package/docs/types/RecursiveTreeMultiSetNode.html +0 -152
  263. package/docs/types/SegmentTreeNodeVal.html +0 -147
  264. package/docs/types/TopologicalStatus.html +0 -147
  265. package/docs/types/TreeMultiSetOptions.html +0 -152
  266. package/docs/types/Turning.html +0 -147
  267. package/docs/types/VertexId.html +0 -147
  268. package/notes/bst.test.ts +0 -181
  269. package/notes/note.md +0 -34
@@ -1,251 +0,0 @@
1
- /**
2
- * data-structure-typed
3
- *
4
- * @author Tyler Zeng
5
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
- * @license MIT License
7
- */
8
- import {DoublyLinkedList} from '../linked-list';
9
-
10
- // O(n) time complexity of obtaining the value
11
- // O(1) time complexity of adding at the beginning and the end
12
- export class Deque<T> extends DoublyLinkedList<T> {
13
-
14
- }
15
-
16
- // O(1) time complexity of obtaining the value
17
- // O(n) time complexity of adding at the beginning and the end
18
- // todo tested slowest one
19
- export class ObjectDeque<T = number> {
20
- constructor(capacity?: number) {
21
- if (capacity !== undefined) this._capacity = capacity;
22
- }
23
-
24
- private _nodes: { [key: number]: T } = {};
25
-
26
- get nodes(): { [p: number]: T } {
27
- return this._nodes;
28
- }
29
-
30
- private _capacity = Number.MAX_SAFE_INTEGER;
31
-
32
- get capacity(): number {
33
- return this._capacity;
34
- }
35
-
36
- set capacity(value: number) {
37
- this._capacity = value;
38
- }
39
-
40
- private _first: number = -1;
41
-
42
- get first(): number {
43
- return this._first;
44
- }
45
-
46
- set first(value: number) {
47
- this._first = value;
48
- }
49
-
50
- private _last: number = -1;
51
-
52
- get last(): number {
53
- return this._last;
54
- }
55
-
56
- set last(value: number) {
57
- this._last = value;
58
- }
59
-
60
- private _size: number = 0;
61
-
62
- get size(): number {
63
- return this._size;
64
- }
65
-
66
- addFirst(value: T) {
67
- if (this._size === 0) {
68
- const mid = Math.floor(this._capacity / 2);
69
- this._first = mid;
70
- this._last = mid;
71
- } else {
72
- this._first--;
73
- }
74
- this._nodes[this._first] = value;
75
- this._size++;
76
- }
77
-
78
- addLast(value: T) {
79
- if (this._size === 0) {
80
- const mid = Math.floor(this._capacity / 2);
81
- this._first = mid;
82
- this._last = mid;
83
- } else {
84
- this._last++;
85
- }
86
- this._nodes[this._last] = value;
87
- this._size++;
88
- }
89
-
90
- pollFirst() {
91
- if (!this._size) return;
92
- const value = this.peekFirst();
93
- delete this._nodes[this._first];
94
- this._first++;
95
- this._size--;
96
- return value;
97
- }
98
-
99
- peekFirst() {
100
- if (this._size) return this._nodes[this._first];
101
- }
102
-
103
- pollLast() {
104
- if (!this._size) return;
105
- const value = this.peekLast();
106
- delete this._nodes[this._last];
107
- this._last--;
108
- this._size--;
109
-
110
- return value;
111
- }
112
-
113
- peekLast() {
114
- if (this._size) return this._nodes[this._last];
115
- }
116
-
117
- get(index: number) {
118
- return this._nodes[this._first + index] || null;
119
- }
120
-
121
- isEmpty() {
122
- return this._size <= 0;
123
- }
124
-
125
- protected _seNodes(value: { [p: number]: T }) {
126
- this._nodes = value;
127
- }
128
-
129
- protected _setSize(value: number) {
130
- this._size = value;
131
- }
132
- }
133
-
134
- // O(1) time complexity of obtaining the value
135
- // O(n) time complexity of adding at the beginning and the end
136
- export class ArrayDeque<T> {
137
- protected _nodes: T[] = [];
138
-
139
- get size() {
140
- return this._nodes.length;
141
- }
142
-
143
- /**
144
- * The function "addLast" adds a value to the end of an array.
145
- * @param {T} value - The value parameter represents the value that you want to add to the end of the array.
146
- * @returns The return value is the new length of the array after the value has been added.
147
- */
148
- addLast(value: T) {
149
- return this._nodes.push(value);
150
- }
151
-
152
- /**
153
- * The function "pollLast" returns and removes the last element from an array, or returns null if the array is empty.
154
- * @returns The method `pollLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
155
- */
156
- pollLast(): T | null {
157
- return this._nodes.pop() ?? null;
158
- }
159
-
160
- /**
161
- * The `pollFirst` function removes and returns the first element from an array, or returns null if the array is empty.
162
- * @returns The `pollFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
163
- * empty.
164
- */
165
- pollFirst(): T | null {
166
- return this._nodes.shift() ?? null;
167
- }
168
-
169
- /**
170
- * The function "addFirst" adds a value to the beginning of an array.
171
- * @param {T} value - The value parameter represents the value that you want to add to the beginning of the array.
172
- * @returns The return value of the `addFirst` function is the new length of the array `_nodes` after adding the
173
- * `value` at the beginning.
174
- */
175
- addFirst(value: T) {
176
- return this._nodes.unshift(value);
177
- }
178
-
179
- /**
180
- * The `peekFirst` function returns the first element of an array or null if the array is empty.
181
- * @returns The function `peekFirst()` is returning the first element (`T`) of the `_nodes` array. If the array is
182
- * empty, it will return `null`.
183
- */
184
- peekFirst(): T | null {
185
- return this._nodes[0] ?? null;
186
- }
187
-
188
- /**
189
- * The `peekLast` function returns the last element of an array or null if the array is empty.
190
- * @returns The method `peekLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
191
- */
192
- peekLast(): T | null {
193
- return this._nodes[this._nodes.length - 1] ?? null;
194
- }
195
-
196
- /**
197
- * The get function returns the element at the specified index in an array, or null if the index is out of bounds.
198
- * @param {number} index - The index parameter is a number that represents the position of the element you want to
199
- * retrieve from the array.
200
- * @returns The method is returning the element at the specified index in the `_nodes` array. If the element exists, it
201
- * will be returned. If the element does not exist (i.e., the index is out of bounds), `null` will be returned.
202
- */
203
- get(index: number): T | null {
204
- return this._nodes[index] ?? null;
205
- }
206
-
207
- /**
208
- * The set function assigns a value to a specific index in an array.
209
- * @param {number} index - The index parameter is a number that represents the position of the element in the array
210
- * that you want to set a new value for.
211
- * @param {T} value - The value parameter represents the new value that you want to set at the specified index in the
212
- * _nodes array.
213
- * @returns The value that is being set at the specified index in the `_nodes` array.
214
- */
215
- set(index: number, value: T) {
216
- return this._nodes[index] = value;
217
- }
218
-
219
- /**
220
- * The insert function adds a value at a specified index in an array.
221
- * @param {number} index - The index parameter specifies the position at which the value should be inserted in the
222
- * array. It is a number that represents the index of the array where the value should be inserted. The index starts
223
- * from 0, so the first element of the array has an index of 0, the second element has
224
- * @param {T} value - The value parameter represents the value that you want to insert into the array at the specified
225
- * index.
226
- * @returns The splice method returns an array containing the removed elements, if any. In this case, since no elements
227
- * are being removed, an empty array will be returned.
228
- */
229
- insert(index: number, value: T) {
230
- return this._nodes.splice(index, 0, value);
231
- }
232
-
233
- /**
234
- * The remove function removes an element from an array at a specified index.
235
- * @param {number} index - The index parameter specifies the position of the element to be removed from the array. It
236
- * is a number that represents the index of the element to be removed.
237
- * @returns The method is returning an array containing the removed element.
238
- */
239
- remove(index: number) {
240
- return this._nodes.splice(index, 1);
241
- }
242
-
243
- /**
244
- * The function checks if an array called "_nodes" is empty.
245
- * @returns The method `isEmpty()` is returning a boolean value. It returns `true` if the length of the `_nodes` array
246
- * is 0, indicating that the array is empty. Otherwise, it returns `false`.
247
- */
248
- isEmpty() {
249
- return this._nodes.length === 0;
250
- }
251
- }
@@ -1,2 +0,0 @@
1
- export * from './queue';
2
- export * from './deque';
@@ -1,120 +0,0 @@
1
- /**
2
- * @license MIT
3
- * @copyright Tyler Zeng <zrwusa@gmail.com>
4
- * @class
5
- */
6
- export class Queue<T = number> {
7
- protected _nodes: T[];
8
- protected _offset: number;
9
-
10
- /**
11
- * The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
12
- * @param {T[]} [elements] - The `elements` parameter is an optional array of elements of type `T`. If provided, it
13
- * will be used to initialize the `_nodes` property of the class. If not provided, the `_nodes` property will be
14
- * initialized as an empty array.
15
- */
16
- constructor(elements?: T[]) {
17
- this._nodes = elements || [];
18
- this._offset = 0;
19
- }
20
-
21
- /**
22
- * The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
23
- * @public
24
- * @static
25
- * @param {T[]} elements - The "elements" parameter is an array of elements of type T.
26
- * @returns The method is returning a new instance of the Queue class, initialized with the elements from the input
27
- * array.
28
- */
29
- static fromArray<T>(elements: T[]): Queue<T> {
30
- return new Queue(elements);
31
- }
32
-
33
- /**
34
- * The add function adds an element to the end of the queue and returns the updated queue.Adds an element at the back of the queue.
35
- * @param {T} element - The `element` parameter represents the element that you want to add to the queue.
36
- * @returns The `add` method is returning a `Queue<T>` object.
37
- */
38
- add(element: T): Queue<T> {
39
- this._nodes.push(element);
40
- return this;
41
- }
42
-
43
- /**
44
- * The `poll` function removes and returns the first element in the queue, and adjusts the internal data structure if
45
- * necessary to optimize performance.
46
- * @returns The function `poll()` returns either the first element in the queue or `null` if the queue is empty.
47
- */
48
- poll(): T | null {
49
- if (this.size() === 0) return null;
50
-
51
- const first = this.peek();
52
- this._offset += 1;
53
-
54
- if (this._offset * 2 < this._nodes.length) return first;
55
-
56
- // only remove dequeued elements when reaching half size
57
- // to decrease latency of shifting elements.
58
- this._nodes = this._nodes.slice(this._offset);
59
- this._offset = 0;
60
- return first;
61
- }
62
-
63
- /**
64
- * The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
65
- * @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
66
- * the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
67
- */
68
- peek(): T | null {
69
- return this.size() > 0 ? this._nodes[this._offset] : null;
70
- }
71
-
72
- /**
73
- * The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty.
74
- * @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
75
- * array is empty, it returns `null`.
76
- */
77
- peekLast(): T | null {
78
- return this.size() > 0 ? this._nodes[this._nodes.length - 1] : null;
79
- }
80
-
81
- /**
82
- * The size function returns the number of elements in an array.
83
- * @returns {number} The size of the array, which is the difference between the length of the array and the offset.
84
- */
85
- size(): number {
86
- return this._nodes.length - this._offset;
87
- }
88
-
89
- /**
90
- * The function checks if a data structure is empty by comparing its size to zero.
91
- * @returns {boolean} A boolean value indicating whether the size of the object is 0 or not.
92
- */
93
- isEmpty(): boolean {
94
- return this.size() === 0;
95
- }
96
-
97
- /**
98
- * The toArray() function returns an array of elements from the current offset to the end of the _nodes array.
99
- * @returns An array of type T is being returned.
100
- */
101
- toArray(): T[] {
102
- return this._nodes.slice(this._offset);
103
- }
104
-
105
- /**
106
- * The clear function resets the nodes array and offset to their initial values.
107
- */
108
- clear(): void {
109
- this._nodes = [];
110
- this._offset = 0;
111
- }
112
-
113
- /**
114
- * The `clone()` function returns a new Queue object with the same elements as the original Queue.
115
- * @returns The `clone()` method is returning a new instance of the `Queue` class.
116
- */
117
- clone(): Queue<T> {
118
- return new Queue(this._nodes.slice(this._offset));
119
- }
120
- }
@@ -1 +0,0 @@
1
- export * from './stack';
@@ -1,98 +0,0 @@
1
- /**
2
- * @license MIT
3
- * @copyright Tyler Zeng <zrwusa@gmail.com>
4
- * @class
5
- */
6
- export class Stack<T = number> {
7
- protected _elements: T[];
8
-
9
- /**
10
- * The constructor initializes an array of elements, which can be provided as an optional parameter.
11
- * @param {T[]} [elements] - The `elements` parameter is an optional parameter of type `T[]`, which represents an array
12
- * of elements of type `T`. It is used to initialize the `_elements` property of the class. If the `elements` parameter
13
- * is provided and is an array, it is assigned to the `_elements
14
- */
15
- constructor(elements?: T[]) {
16
- this._elements = Array.isArray(elements) ? elements : [];
17
- }
18
-
19
- /**
20
- * The function "fromArray" creates a new Stack object from an array of elements.
21
- * @param {T[]} elements - The `elements` parameter is an array of elements of type `T`.
22
- * @returns {Stack} The method is returning a new instance of the Stack class, initialized with the elements from the input
23
- * array.
24
- */
25
- static fromArray<T>(elements: T[]): Stack<T> {
26
- return new Stack(elements);
27
- }
28
-
29
- /**
30
- * The function checks if an array is empty and returns a boolean value.
31
- * @returns A boolean value indicating whether the `_elements` array is empty or not.
32
- */
33
- isEmpty(): boolean {
34
- return this._elements.length === 0;
35
- }
36
-
37
- /**
38
- * The size() function returns the number of elements in an array.
39
- * @returns The size of the elements array.
40
- */
41
- size(): number {
42
- return this._elements.length;
43
- }
44
-
45
- /**
46
- * The `peek` function returns the last element of an array, or null if the array is empty.
47
- * @returns The `peek()` function returns the last element of the `_elements` array, or `null` if the array is empty.
48
- */
49
- peek(): T | null {
50
- if (this.isEmpty()) return null;
51
-
52
- return this._elements[this._elements.length - 1];
53
- }
54
-
55
- /**
56
- * The push function adds an element to the stack and returns the updated stack.
57
- * @param {T} element - The parameter "element" is of type T, which means it can be any data type.
58
- * @returns The `push` method is returning the updated `Stack<T>` object.
59
- */
60
- push(element: T): Stack<T> {
61
- this._elements.push(element);
62
- return this;
63
- }
64
-
65
- /**
66
- * The `pop` function removes and returns the last element from an array, or returns null if the array is empty.
67
- * @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
68
- * array is empty, it returns `null`.
69
- */
70
- pop(): T | null {
71
- if (this.isEmpty()) return null;
72
-
73
- return this._elements.pop() || null;
74
- }
75
-
76
- /**
77
- * The toArray function returns a copy of the elements in an array.
78
- * @returns An array of type T.
79
- */
80
- toArray(): T[] {
81
- return this._elements.slice();
82
- }
83
-
84
- /**
85
- * The clear function clears the elements array.
86
- */
87
- clear(): void {
88
- this._elements = [];
89
- }
90
-
91
- /**
92
- * The `clone()` function returns a new `Stack` object with the same elements as the original stack.
93
- * @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
94
- */
95
- clone(): Stack<T> {
96
- return new Stack(this._elements.slice());
97
- }
98
- }
@@ -1 +0,0 @@
1
- export * from './tree';
@@ -1,80 +0,0 @@
1
- export class TreeNode<T = number> {
2
- constructor(id: string, name?: string, value?: T, children?: TreeNode<T>[]) {
3
- this._id = id;
4
- this._name = name || '';
5
- this._value = value || undefined;
6
- this._children = children || [];
7
- }
8
-
9
- private _id: string;
10
-
11
- get id(): string {
12
- return this._id;
13
- }
14
-
15
- set id(value: string) {
16
- this._id = value;
17
- }
18
-
19
- private _name?: string | undefined;
20
-
21
- get name(): string | undefined {
22
- return this._name;
23
- }
24
-
25
- set name(value: string | undefined) {
26
- this._name = value;
27
- }
28
-
29
- private _value?: T | undefined;
30
-
31
- get value(): T | undefined {
32
- return this._value;
33
- }
34
-
35
- set value(value: T | undefined) {
36
- this._value = value;
37
- }
38
-
39
- private _children?: TreeNode<T>[] | undefined;
40
-
41
- get children(): TreeNode<T>[] | undefined {
42
- return this._children;
43
- }
44
-
45
- set children(value: TreeNode<T>[] | undefined) {
46
- this._children = value;
47
- }
48
-
49
- addChildren(children: TreeNode<T> | TreeNode<T> []) {
50
- if (!this.children) {
51
- this.children = [];
52
- }
53
- if (children instanceof TreeNode) {
54
- this.children.push(children);
55
- } else {
56
- this.children = this.children.concat(children);
57
- }
58
- }
59
-
60
- getHeight() {
61
- // eslint-disable-next-line @typescript-eslint/no-this-alias
62
- const beginRoot = this;
63
- let maxDepth = 1;
64
- if (beginRoot) {
65
- const bfs = (node: TreeNode<T>, level: number) => {
66
- if (level > maxDepth) {
67
- maxDepth = level;
68
- }
69
- const {children} = node;
70
- if (children) {
71
- for (let i = 0, len = children.length; i < len; i++) {
72
- bfs(children[i], level + 1);
73
- }
74
- }
75
- };
76
- bfs(beginRoot, 1);
77
- }
78
- return maxDepth;
79
- }
80
- }
@@ -1 +0,0 @@
1
- export * from './trie';