data-structure-typed 0.9.16 → 1.12.10

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 (199) hide show
  1. package/.dependency-cruiser.js +449 -0
  2. package/.idea/data-structure-typed.iml +2 -0
  3. package/README.md +102 -1
  4. package/dist/data-structures/binary-tree/avl-tree.d.ts +57 -0
  5. package/dist/data-structures/binary-tree/avl-tree.js +57 -0
  6. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +27 -0
  7. package/dist/data-structures/binary-tree/binary-indexed-tree.js +27 -0
  8. package/dist/data-structures/binary-tree/binary-tree.d.ts +215 -9
  9. package/dist/data-structures/binary-tree/binary-tree.js +283 -15
  10. package/dist/data-structures/binary-tree/bst.d.ts +16 -0
  11. package/dist/data-structures/binary-tree/bst.js +12 -0
  12. package/dist/data-structures/binary-tree/segment-tree.d.ts +4 -0
  13. package/dist/data-structures/binary-tree/segment-tree.js +4 -0
  14. package/dist/data-structures/binary-tree/tree-multiset.d.ts +4 -0
  15. package/dist/data-structures/binary-tree/tree-multiset.js +4 -0
  16. package/dist/data-structures/graph/abstract-graph.d.ts +163 -13
  17. package/dist/data-structures/graph/abstract-graph.js +166 -12
  18. package/dist/data-structures/graph/directed-graph.d.ts +111 -1
  19. package/dist/data-structures/graph/directed-graph.js +122 -0
  20. package/dist/data-structures/graph/undirected-graph.d.ts +63 -0
  21. package/dist/data-structures/graph/undirected-graph.js +67 -0
  22. package/dist/data-structures/hash/coordinate-map.d.ts +32 -0
  23. package/dist/data-structures/hash/coordinate-map.js +32 -0
  24. package/dist/data-structures/hash/coordinate-set.d.ts +25 -0
  25. package/dist/data-structures/hash/coordinate-set.js +25 -0
  26. package/dist/data-structures/heap/heap.d.ts +26 -31
  27. package/dist/data-structures/heap/heap.js +23 -32
  28. package/dist/data-structures/heap/max-heap.d.ts +6 -1
  29. package/dist/data-structures/heap/max-heap.js +6 -1
  30. package/dist/data-structures/heap/min-heap.d.ts +7 -1
  31. package/dist/data-structures/heap/min-heap.js +7 -1
  32. package/dist/data-structures/index.d.ts +0 -1
  33. package/dist/data-structures/index.js +0 -1
  34. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +14 -6
  35. package/dist/data-structures/linked-list/doubly-linked-list.js +45 -8
  36. package/dist/data-structures/linked-list/singly-linked-list.d.ts +11 -8
  37. package/dist/data-structures/linked-list/singly-linked-list.js +4 -0
  38. package/dist/data-structures/matrix/matrix.d.ts +9 -0
  39. package/dist/data-structures/matrix/matrix.js +11 -0
  40. package/dist/data-structures/matrix/matrix2d.d.ts +82 -2
  41. package/dist/data-structures/matrix/matrix2d.js +82 -2
  42. package/dist/data-structures/matrix/navigator.d.ts +32 -0
  43. package/dist/data-structures/matrix/navigator.js +28 -0
  44. package/dist/data-structures/matrix/vector2d.d.ts +139 -15
  45. package/dist/data-structures/matrix/vector2d.js +139 -15
  46. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +8 -0
  47. package/dist/data-structures/priority-queue/max-priority-queue.js +8 -0
  48. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +8 -0
  49. package/dist/data-structures/priority-queue/min-priority-queue.js +8 -0
  50. package/dist/data-structures/priority-queue/priority-queue.d.ts +130 -0
  51. package/dist/data-structures/priority-queue/priority-queue.js +126 -0
  52. package/dist/data-structures/queue/deque.d.ts +69 -0
  53. package/dist/data-structures/queue/deque.js +69 -0
  54. package/dist/data-structures/queue/queue.d.ts +30 -32
  55. package/dist/data-structures/queue/queue.js +30 -32
  56. package/dist/data-structures/stack/stack.d.ts +26 -31
  57. package/dist/data-structures/stack/stack.js +26 -31
  58. package/dist/data-structures/trie/trie.d.ts +26 -6
  59. package/dist/data-structures/trie/trie.js +26 -7
  60. package/dist/data-structures/types/avl-tree.d.ts +1 -1
  61. package/dist/data-structures/types/binary-tree.d.ts +1 -1
  62. package/dist/data-structures/types/index.d.ts +1 -1
  63. package/dist/data-structures/types/index.js +1 -1
  64. package/dist/data-structures/types/singly-linked-list.d.ts +1 -5
  65. package/dist/data-structures/types/tree-multiset.d.ts +1 -1
  66. package/dist/{data-structures → utils}/trampoline.d.ts +5 -7
  67. package/dist/utils/types/index.d.ts +1 -0
  68. package/dist/utils/types/index.js +17 -0
  69. package/dist/{data-structures → utils}/types/utils.d.ts +8 -0
  70. package/dist/{data-structures → utils}/types/utils.js +1 -0
  71. package/dist/utils/utils.d.ts +1 -1
  72. package/docs/.nojekyll +1 -0
  73. package/docs/assets/highlight.css +85 -0
  74. package/docs/assets/main.js +58 -0
  75. package/docs/assets/search.js +1 -0
  76. package/docs/assets/style.css +1367 -0
  77. package/docs/classes/AVLTree.html +2046 -0
  78. package/docs/classes/AVLTreeNode.html +423 -0
  79. package/docs/classes/AaTree.html +117 -0
  80. package/docs/classes/AbstractEdge.html +198 -0
  81. package/docs/classes/AbstractGraph.html +891 -0
  82. package/docs/classes/AbstractVertex.html +164 -0
  83. package/docs/classes/ArrayDeque.html +384 -0
  84. package/docs/classes/BST.html +1893 -0
  85. package/docs/classes/BSTNode.html +425 -0
  86. package/docs/classes/BTree.html +117 -0
  87. package/docs/classes/BinaryIndexedTree.html +244 -0
  88. package/docs/classes/BinaryTree.html +1754 -0
  89. package/docs/classes/BinaryTreeNode.html +396 -0
  90. package/docs/classes/Character.html +165 -0
  91. package/docs/classes/CoordinateMap.html +394 -0
  92. package/docs/classes/CoordinateSet.html +355 -0
  93. package/docs/classes/Deque.html +617 -0
  94. package/docs/classes/DirectedEdge.html +247 -0
  95. package/docs/classes/DirectedGraph.html +1207 -0
  96. package/docs/classes/DirectedVertex.html +154 -0
  97. package/docs/classes/DoublyLinkedList.html +619 -0
  98. package/docs/classes/DoublyLinkedListNode.html +160 -0
  99. package/docs/classes/Heap.html +315 -0
  100. package/docs/classes/Matrix2D.html +447 -0
  101. package/docs/classes/MatrixNTI2D.html +181 -0
  102. package/docs/classes/MaxHeap.html +325 -0
  103. package/docs/classes/MaxPriorityQueue.html +668 -0
  104. package/docs/classes/MinHeap.html +326 -0
  105. package/docs/classes/MinPriorityQueue.html +668 -0
  106. package/docs/classes/Navigator.html +285 -0
  107. package/docs/classes/ObjectDeque.html +289 -0
  108. package/docs/classes/PriorityQueue.html +643 -0
  109. package/docs/classes/Queue.html +337 -0
  110. package/docs/classes/RBTree.html +117 -0
  111. package/docs/classes/SegmentTree.html +234 -0
  112. package/docs/classes/SegmentTreeNode.html +302 -0
  113. package/docs/classes/SinglyLinkedList.html +1035 -0
  114. package/docs/classes/SinglyLinkedListNode.html +304 -0
  115. package/docs/classes/SplayTree.html +117 -0
  116. package/docs/classes/Stack.html +313 -0
  117. package/docs/classes/TreeMultiSet.html +1897 -0
  118. package/docs/classes/Trie.html +317 -0
  119. package/docs/classes/TrieNode.html +221 -0
  120. package/docs/classes/TwoThreeTree.html +117 -0
  121. package/docs/classes/UndirectedEdge.html +220 -0
  122. package/docs/classes/UndirectedGraph.html +1006 -0
  123. package/docs/classes/UndirectedVertex.html +154 -0
  124. package/docs/classes/Vector2D.html +746 -0
  125. package/docs/enums/CP.html +126 -0
  126. package/docs/enums/FamilyPosition.html +126 -0
  127. package/docs/enums/LoopType.html +119 -0
  128. package/docs/index.html +288 -0
  129. package/docs/modules.html +146 -0
  130. package/jest.config.js +5 -0
  131. package/package.json +19 -11
  132. package/rename_clear_files.sh +29 -0
  133. package/src/assets/complexities-diff.jpg +0 -0
  134. package/src/assets/data-structure-complexities.jpg +0 -0
  135. package/src/data-structures/binary-tree/avl-tree.ts +57 -0
  136. package/src/data-structures/binary-tree/binary-indexed-tree.ts +27 -0
  137. package/src/data-structures/binary-tree/binary-tree.ts +288 -18
  138. package/src/data-structures/binary-tree/bst.ts +16 -1
  139. package/src/data-structures/binary-tree/segment-tree.ts +5 -0
  140. package/src/data-structures/binary-tree/tree-multiset.ts +4 -0
  141. package/src/data-structures/diagrams/README.md +5 -7
  142. package/src/data-structures/graph/abstract-graph.ts +167 -15
  143. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  144. package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  145. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  146. package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  147. package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  148. package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  149. package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  150. package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  151. package/src/data-structures/graph/diagrams/mst.jpg +0 -0
  152. package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  153. package/src/data-structures/graph/directed-graph.ts +127 -5
  154. package/src/data-structures/graph/undirected-graph.ts +67 -1
  155. package/src/data-structures/hash/coordinate-map.ts +32 -0
  156. package/src/data-structures/hash/coordinate-set.ts +25 -0
  157. package/src/data-structures/heap/heap.ts +27 -32
  158. package/src/data-structures/heap/max-heap.ts +6 -1
  159. package/src/data-structures/heap/min-heap.ts +7 -1
  160. package/src/data-structures/index.ts +0 -1
  161. package/src/data-structures/linked-list/doubly-linked-list.ts +49 -8
  162. package/src/data-structures/linked-list/singly-linked-list.ts +39 -8
  163. package/src/data-structures/matrix/matrix.ts +11 -0
  164. package/src/data-structures/matrix/matrix2d.ts +82 -2
  165. package/src/data-structures/matrix/navigator.ts +32 -0
  166. package/src/data-structures/matrix/vector2d.ts +139 -15
  167. package/src/data-structures/priority-queue/max-priority-queue.ts +8 -0
  168. package/src/data-structures/priority-queue/min-priority-queue.ts +8 -0
  169. package/src/data-structures/priority-queue/priority-queue.ts +130 -0
  170. package/src/data-structures/queue/deque.ts +69 -0
  171. package/src/data-structures/queue/queue.ts +30 -32
  172. package/src/data-structures/stack/stack.ts +26 -31
  173. package/src/data-structures/trie/trie.ts +26 -7
  174. package/src/data-structures/types/avl-tree.ts +1 -1
  175. package/src/data-structures/types/binary-tree.ts +1 -1
  176. package/src/data-structures/types/bst.ts +1 -1
  177. package/src/data-structures/types/index.ts +1 -1
  178. package/src/data-structures/types/navigator.ts +1 -0
  179. package/src/data-structures/types/singly-linked-list.ts +1 -15
  180. package/src/data-structures/types/tree-multiset.ts +1 -1
  181. package/src/{data-structures → utils}/trampoline.ts +6 -6
  182. package/src/utils/types/index.ts +1 -0
  183. package/src/{data-structures → utils}/types/utils.ts +7 -0
  184. package/src/utils/utils.ts +12 -2
  185. package/tests/unit/data-structures/binary-tree/bst.test.ts +185 -0
  186. package/tests/unit/data-structures/graph/abstract-graph.ts +0 -0
  187. package/tests/unit/data-structures/graph/directed-graph.test.ts +71 -0
  188. package/tests/unit/data-structures/graph/index.ts +3 -0
  189. package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
  190. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
  191. package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
  192. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
  193. package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
  194. package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
  195. package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
  196. package/src/data-structures/graph/diagrams/edge-list.png +0 -0
  197. package/src/data-structures/graph/diagrams/max-flow.png +0 -0
  198. package/src/data-structures/graph/diagrams/mst.png +0 -0
  199. /package/dist/{data-structures → utils}/trampoline.js +0 -0
@@ -1,3 +1,7 @@
1
+ /**
2
+ * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
+ * @license MIT
4
+ */
1
5
  import {arrayRemove} from '../../utils';
2
6
  import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
3
7
  import type {VertexId} from '../types';
@@ -32,6 +36,14 @@ export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdg
32
36
  super();
33
37
  }
34
38
 
39
+ /**
40
+ * The function `getEdge` returns the first edge that connects two vertices, or null if no such edge exists.
41
+ * @param {V | null | VertexId} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID
42
+ * (`VertexId`). It can also be `null`.
43
+ * @param {V | null | VertexId} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `V` (vertex
44
+ * object), `null`, or `VertexId` (vertex ID).
45
+ * @returns an edge (E) or null.
46
+ */
35
47
  getEdge(v1: V | null | VertexId, v2: V | null | VertexId): E | null {
36
48
  let edges: E[] | undefined = [];
37
49
 
@@ -47,6 +59,11 @@ export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdg
47
59
  return edges ? edges[0] || null : null;
48
60
  }
49
61
 
62
+ /**
63
+ * The function adds an edge to a graph by connecting two vertices.
64
+ * @param {E} edge - The `edge` parameter is an object of type `E`, which represents an edge in a graph.
65
+ * @returns a boolean value.
66
+ */
50
67
  addEdge(edge: E): boolean {
51
68
  for (const end of edge.vertices) {
52
69
  const endVertex = this.getVertex(end);
@@ -63,6 +80,13 @@ export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdg
63
80
  return true;
64
81
  }
65
82
 
83
+ /**
84
+ * The function removes an edge between two vertices in a graph and returns the removed edge, or null if either of the
85
+ * vertices does not exist.
86
+ * @param {V | VertexId} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexId`).
87
+ * @param {V | VertexId} v2 - V | VertexId: The second vertex or vertex ID of the edge to be removed.
88
+ * @returns the removed edge (E) if it exists, or null if either of the vertices (v1 or v2) does not exist.
89
+ */
66
90
  removeEdgeBetween(v1: V | VertexId, v2: V | VertexId): E | null {
67
91
 
68
92
  const vertex1: V | null = this.getVertex(v1);
@@ -84,11 +108,22 @@ export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdg
84
108
  return removed;
85
109
  }
86
110
 
87
-
111
+ /**
112
+ * The removeEdge function removes an edge between two vertices in a graph.
113
+ * @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
114
+ * @returns The method is returning either the removed edge (of type E) or null if the edge was not found.
115
+ */
88
116
  removeEdge(edge: E): E | null {
89
117
  return this.removeEdgeBetween(edge.vertices[0], edge.vertices[1]);
90
118
  }
91
119
 
120
+ /**
121
+ * The function `degreeOf` returns the degree of a vertex in a graph, which is the number of edges connected to that
122
+ * vertex.
123
+ * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
124
+ * @returns The function `degreeOf` returns the degree of a vertex in a graph. The degree of a vertex is the number of
125
+ * edges that are incident to that vertex.
126
+ */
92
127
  degreeOf(vertexOrId: VertexId | V): number {
93
128
  const vertex = this.getVertex(vertexOrId);
94
129
  if (vertex) {
@@ -98,6 +133,13 @@ export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdg
98
133
  }
99
134
  }
100
135
 
136
+ /**
137
+ * The function "edgesOf" returns an array of edges connected to a given vertex.
138
+ * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
139
+ * @returns an array of edges connected to the specified vertex. If the vertex exists in the graph, the function
140
+ * returns the array of edges connected to that vertex. If the vertex does not exist in the graph, the function returns
141
+ * an empty array.
142
+ */
101
143
  edgesOf(vertexOrId: VertexId | V): E[] {
102
144
  const vertex = this.getVertex(vertexOrId);
103
145
  if (vertex) {
@@ -107,6 +149,10 @@ export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdg
107
149
  }
108
150
  }
109
151
 
152
+ /**
153
+ * The function "edgeSet" returns an array of unique edges from a set of edges.
154
+ * @returns The method `edgeSet()` returns an array of type `E[]`.
155
+ */
110
156
  edgeSet(): E[] {
111
157
  const edgeSet: Set<E> = new Set();
112
158
  this._edges.forEach(edges => {
@@ -117,6 +163,12 @@ export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdg
117
163
  return [...edgeSet];
118
164
  }
119
165
 
166
+ /**
167
+ * The function "getEdgesOf" returns an array of edges connected to a given vertex or vertex ID.
168
+ * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID
169
+ * (`VertexId`).
170
+ * @returns an array of edges (E[]) that are connected to the specified vertex or vertex ID.
171
+ */
120
172
  getEdgesOf(vertexOrId: V | VertexId): E[] {
121
173
  const vertex = this.getVertex(vertexOrId);
122
174
  if (!vertex) {
@@ -125,6 +177,12 @@ export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdg
125
177
  return this._edges.get(vertex) || [];
126
178
  }
127
179
 
180
+ /**
181
+ * The function "getNeighbors" returns an array of neighboring vertices of a given vertex.
182
+ * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
183
+ * (`VertexId`).
184
+ * @returns an array of vertices (V[]).
185
+ */
128
186
  getNeighbors(vertexOrId: V | VertexId): V[] {
129
187
  const neighbors: V[] = [];
130
188
  const vertex = this.getVertex(vertexOrId);
@@ -140,6 +198,14 @@ export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdg
140
198
  return neighbors;
141
199
  }
142
200
 
201
+ /**
202
+ * The function "getEndsOfEdge" returns the vertices at the ends of a given edge, or null if the edge does not exist in
203
+ * the graph.
204
+ * @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
205
+ * @returns The function `getEndsOfEdge` returns an array containing two vertices `[V, V]` if the edge exists in the
206
+ * graph and both vertices are found. If the edge does not exist or one or both vertices are not found, it returns
207
+ * `null`.
208
+ */
143
209
  getEndsOfEdge(edge: E): [V, V] | null {
144
210
  if (!this.containsEdge(edge.vertices[0], edge.vertices[1])) {
145
211
  return null;
@@ -1,3 +1,7 @@
1
+ /**
2
+ * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
+ * @license MIT
4
+ */
1
5
  export class CoordinateMap<V> extends Map<any, V> {
2
6
  private readonly _joint: string = '_';
3
7
 
@@ -6,18 +10,46 @@ export class CoordinateMap<V> extends Map<any, V> {
6
10
  if (joint !== undefined) this._joint = joint;
7
11
  }
8
12
 
13
+ /**
14
+ * The "has" function overrides the base class's "has" function and checks if a key exists in the map by joining the
15
+ * key array with a specified delimiter.
16
+ * @param {number[]} key - The parameter "key" is an array of numbers.
17
+ * @returns The `has` method is being overridden to return the result of calling the `has` method of the superclass
18
+ * (`super.has`) with the `key` array joined together using the `_joint` property.
19
+ */
9
20
  override has(key: number[]) {
10
21
  return super.has(key.join(this._joint));
11
22
  }
12
23
 
24
+ /**
25
+ * The function overrides the set method of a Map object to convert the key from an array to a string using a specified
26
+ * delimiter before calling the original set method.
27
+ * @param {number[]} key - The key parameter is an array of numbers.
28
+ * @param {V} value - The value parameter is the value that you want to associate with the specified key.
29
+ * @returns The `set` method is returning the result of calling the `set` method of the superclass
30
+ * (`super.set(key.join(this._joint), value)`).
31
+ */
13
32
  override set(key: number[], value: V) {
14
33
  return super.set(key.join(this._joint), value);
15
34
  }
16
35
 
36
+ /**
37
+ * The function overrides the get method to join the key array with a specified joint and then calls the super get
38
+ * method.
39
+ * @param {number[]} key - An array of numbers
40
+ * @returns The code is returning the value associated with the specified key in the map.
41
+ */
17
42
  override get(key: number[]) {
18
43
  return super.get(key.join(this._joint));
19
44
  }
20
45
 
46
+ /**
47
+ * The function overrides the delete method and joins the key array using a specified joint character before calling
48
+ * the super delete method.
49
+ * @param {number[]} key - An array of numbers that represents the key to be deleted.
50
+ * @returns The `delete` method is returning the result of calling the `delete` method on the superclass, with the
51
+ * `key` array joined together using the `_joint` property.
52
+ */
21
53
  override delete(key: number[]) {
22
54
  return super.delete(key.join(this._joint));
23
55
  }
@@ -1,3 +1,7 @@
1
+ /**
2
+ * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
+ * @license MIT
4
+ */
1
5
  export class CoordinateSet extends Set {
2
6
  private readonly _joint: string = '_';
3
7
 
@@ -6,14 +10,35 @@ export class CoordinateSet extends Set {
6
10
  if (joint !== undefined) this._joint = joint;
7
11
  }
8
12
 
13
+ /**
14
+ * The "has" function overrides the "has" method of the superclass and checks if a value exists in an array after
15
+ * joining its elements with a specified separator.
16
+ * @param {number[]} value - The parameter "value" is an array of numbers.
17
+ * @returns The overridden `has` method is returning the result of calling the `has` method of the superclass, passing
18
+ * in the joined value as an argument.
19
+ */
9
20
  override has(value: number[]) {
10
21
  return super.has(value.join(this._joint));
11
22
  }
12
23
 
24
+ /**
25
+ * The "add" function overrides the parent class's "add" function by joining the elements of the input array with a
26
+ * specified delimiter before calling the parent class's "add" function.
27
+ * @param {number[]} value - An array of numbers
28
+ * @returns The overridden `add` method is returning the result of calling the `add` method of the superclass
29
+ * (`super.add`) with the joined string representation of the `value` array (`value.join(this._joint)`).
30
+ */
13
31
  override add(value: number[]) {
14
32
  return super.add(value.join(this._joint));
15
33
  }
16
34
 
35
+ /**
36
+ * The function overrides the delete method and deletes an element from a Set by joining the elements of the input
37
+ * array with a specified joint and then calling the delete method of the parent class.
38
+ * @param {number[]} value - An array of numbers
39
+ * @returns The `delete` method is returning the result of calling the `delete` method of the superclass, with the
40
+ * `value` array joined together using the `_joint` property.
41
+ */
17
42
  override delete(value: number[]) {
18
43
  return super.delete(value.join(this._joint));
19
44
  }
@@ -1,21 +1,18 @@
1
- import {PriorityQueue} from '../priority-queue';
2
- import type {HeapItem, HeapOptions} from '../types';
3
-
4
1
  /**
5
- * @copyright 2021 Tyler Zeng <zrwusa@gmail.com>
2
+ * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
6
3
  * @license MIT
7
- *
8
- * @abstract
9
- * @class Heap
10
4
  */
5
+ import {PriorityQueue} from '../priority-queue';
6
+ import type {HeapItem, HeapOptions} from '../types';
7
+
11
8
  export abstract class Heap<T> {
12
9
  protected abstract _pq: PriorityQueue<HeapItem<T>>;
13
10
  protected _priorityCb: (element: T) => number;
14
11
 
15
12
  /**
16
- * Creates a priority queue
17
- * @public
18
- * @params {object} [options]
13
+ * The function is a constructor for a class that initializes a priority callback function based on the
14
+ * options provided.
15
+ * @param [options] - An optional object that contains configuration options for the Heap.
19
16
  */
20
17
  protected constructor(options?: HeapOptions<T>) {
21
18
  if (options) {
@@ -30,44 +27,45 @@ export abstract class Heap<T> {
30
27
  }
31
28
 
32
29
  /**
33
- * @public
34
- * @returns {number}
30
+ * The function returns the size of a priority queue.
31
+ * @returns The size of the priority queue.
35
32
  */
36
33
  get size(): number {
37
34
  return this._pq.size;
38
35
  }
39
36
 
40
37
  /**
41
- * @public
42
- * @returns {boolean}
38
+ * The function checks if a priority queue is empty.
39
+ * @returns {boolean} A boolean value indicating whether the size of the priority queue is less than 1.
43
40
  */
44
41
  isEmpty(): boolean {
45
42
  return this._pq.size < 1;
46
43
  }
47
44
 
48
45
  /**
49
- * Returns an element with highest priority in the queue
50
- * @public
51
- * @returns {object}
46
+ * The `peek` function returns the top item in the priority queue without removing it.
47
+ * @returns The `peek()` method is returning either a `HeapItem<T>` object or `null`.Returns an element with the highest priority in the queue
52
48
  */
53
49
  peek(): HeapItem<T> | null {
54
50
  return this._pq.peek();
55
51
  }
56
52
 
57
53
  /**
58
- * Returns an element with lowest priority in the queue
59
- * @public
60
- * @returns {object}
54
+ * The `peekLast` function returns the last item in the heap.
55
+ * @returns The method `peekLast()` returns either a `HeapItem<T>` object or `null`.Returns an element with the lowest priority in the queue
61
56
  */
62
57
  peekLast(): HeapItem<T> | null {
63
58
  return this._pq.leaf();
64
59
  }
65
60
 
66
61
  /**
67
- * Adds an element to the queue
68
- * @public
69
- * @param {any} element
70
- * @param priority
62
+ * The `offer` function adds an element to a priority queue with an optional priority value.
63
+ * @param {T} element - The `element` parameter represents the value that you want to add to the heap. It can be of any
64
+ * type.
65
+ * @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
66
+ * element being offered to the heap. If the `element` parameter is a number, then the `priority` parameter is set to
67
+ * the value of `element`. If the `element` parameter is not a number, then the
68
+ * @returns The `offer` method returns the instance of the `Heap` class.
71
69
  * @throws {Error} if priority is not a valid number
72
70
  */
73
71
  offer(element: T, priority?: number): Heap<T> {
@@ -96,9 +94,8 @@ export abstract class Heap<T> {
96
94
  }
97
95
 
98
96
  /**
99
- * Removes and returns an element with highest priority in the queue
100
- * @public
101
- * @returns {object}
97
+ * The `poll` function returns the top item from a priority queue or null if the queue is empty.Removes and returns an element with the highest priority in the queue
98
+ * @returns either a HeapItem<T> object or null.
102
99
  */
103
100
  poll(): HeapItem<T> | null {
104
101
  const top = this._pq.poll();
@@ -109,17 +106,15 @@ export abstract class Heap<T> {
109
106
  }
110
107
 
111
108
  /**
112
- * Returns a sorted list of elements
113
- * @public
114
- * @returns {array}
109
+ * The `toArray` function returns an array of `HeapItem<T>` objects.
110
+ * @returns An array of HeapItem<T> objects.Returns a sorted list of elements
115
111
  */
116
112
  toArray(): HeapItem<T>[] {
117
113
  return this._pq.toArray();
118
114
  }
119
115
 
120
116
  /**
121
- * Clears the queue
122
- * @public
117
+ * The clear function clears the priority queue.
123
118
  */
124
119
  clear(): void {
125
120
  this._pq.clear();
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @copyright 2020 Tyler Zeng <zrwusa@gmail.com>
2
+ * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
3
  * @license MIT
4
4
  */
5
5
 
@@ -14,6 +14,11 @@ import type {HeapItem, HeapOptions} from '../types';
14
14
  export class MaxHeap<T> extends Heap<T> {
15
15
  protected _pq: PriorityQueue<HeapItem<T>>;
16
16
 
17
+ /**
18
+ * The constructor initializes a PriorityQueue with a custom comparator function.
19
+ * @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
20
+ * type `HeapOptions<T>`, which is a generic type that represents the options for the heap.
21
+ */
17
22
  constructor(options?: HeapOptions<T>) {
18
23
  super(options);
19
24
  this._pq = new PriorityQueue<HeapItem<T>>({
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @copyright 2020 Tyler Zeng <zrwusa@gmail.com>
2
+ * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
3
  * @license MIT
4
4
  */
5
5
 
@@ -14,6 +14,12 @@ import type {HeapItem, HeapOptions} from '../types';
14
14
  export class MinHeap<T> extends Heap<T> {
15
15
  protected _pq: PriorityQueue<HeapItem<T>>;
16
16
 
17
+ /**
18
+ * The constructor initializes a PriorityQueue with a comparator function that compares the priority of two HeapItem
19
+ * objects.
20
+ * @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
21
+ * type `HeapOptions<T>`, which is a generic type that represents the options for the heap.
22
+ */
17
23
  constructor(options?: HeapOptions<T>) {
18
24
  super(options);
19
25
  this._pq = new PriorityQueue<HeapItem<T>>({
@@ -8,5 +8,4 @@ export * from './heap';
8
8
  export * from './priority-queue';
9
9
  export * from './matrix';
10
10
  export * from './trie';
11
- export * from './types';
12
11
 
@@ -1,3 +1,7 @@
1
+ /**
2
+ * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
+ * @license MIT
4
+ */
1
5
  import type {DoublyLinkedListGetBy} from '../types';
2
6
 
3
7
  export class DoublyLinkedListNode<T> {
@@ -25,8 +29,10 @@ export class DoublyLinkedList<T> {
25
29
  }
26
30
 
27
31
  /**
28
- * Adds a node at the beginning of the linked list
29
- * @param val Value to be stored at the beginning of the linked list
32
+ * The function adds a new node with a given value to the beginning of a doubly linked list.
33
+ * @param {T} val - The `val` parameter represents the value of the element that you want to add to the beginning of
34
+ * the doubly linked list.
35
+ * @returns A boolean value is being returned.
30
36
  */
31
37
  offerFirst(val: T): boolean {
32
38
  const newNode = new DoublyLinkedListNode(val);
@@ -43,8 +49,10 @@ export class DoublyLinkedList<T> {
43
49
  }
44
50
 
45
51
  /**
46
- * Adds a node to the end of the linked list
47
- * @param val Value to be stored in the Doubly linked list node
52
+ * The function adds a new node with a given value to the end of a doubly linked list.
53
+ * @param {T} val - The `val` parameter represents the value of the element that you want to add to the end of the
54
+ * doubly linked list.
55
+ * @returns a boolean value, which is always true.
48
56
  */
49
57
  offerLast(val: T): boolean {
50
58
  const newNode = new DoublyLinkedListNode(val);
@@ -63,6 +71,14 @@ export class DoublyLinkedList<T> {
63
71
  peekFirst(): T | null;
64
72
  peekFirst(by: 'val'): T | null;
65
73
  peekFirst(by: 'node'): DoublyLinkedListNode<T> | null;
74
+ /**
75
+ * The `peekFirst` function returns the first node or value in a doubly linked list, depending on the specified
76
+ * parameter.
77
+ * @param {DoublyLinkedListGetBy} [by] - The "by" parameter is an optional parameter of type DoublyLinkedListGetBy. It
78
+ * is used to specify whether to return the first node, the value of the first node, or the first node itself.
79
+ * @returns The method `peekFirst` returns either the first node of the doubly linked list (`DoublyLinkedListNode<T>`),
80
+ * the value of the first node (`T`), or `null` depending on the value of the `by` parameter.
81
+ */
66
82
  peekFirst(by?: DoublyLinkedListGetBy): T | DoublyLinkedListNode<T> | null {
67
83
  switch (by) {
68
84
  case 'node':
@@ -77,6 +93,13 @@ export class DoublyLinkedList<T> {
77
93
  peekLast(): T | null;
78
94
  peekLast(by: 'val'): T | null;
79
95
  peekLast(by: 'node'): DoublyLinkedListNode<T> | null;
96
+ /**
97
+ * The `peekLast` function returns the last node or value in a doubly linked list.
98
+ * @param {DoublyLinkedListGetBy} [by=val] - The "by" parameter is an optional parameter of type DoublyLinkedListGetBy.
99
+ * It specifies whether to return the last node, the value of the last node, or both. The default value is 'val', which
100
+ * means that if no value is provided for the "by" parameter, the method
101
+ * @returns The method `peekLast` returns the last node, value, or null based on the specified `by` parameter.
102
+ */
80
103
  peekLast(by: DoublyLinkedListGetBy = 'val'): T | DoublyLinkedListNode<T> | null {
81
104
  switch (by) {
82
105
  case 'node':
@@ -92,7 +115,14 @@ export class DoublyLinkedList<T> {
92
115
  pollFirst(by: 'val'): T | null;
93
116
  pollFirst(by: 'node'): DoublyLinkedListNode<T> | null;
94
117
  /**
95
- * Removes a node form the beginning of the linked list and will return the node val
118
+ * The function `pollFirst` removes and returns the first element of a doubly linked list, either as a node or its
119
+ * value, depending on the specified parameter.
120
+ * @param {DoublyLinkedListGetBy} [by=val] - The "by" parameter is an optional parameter of type DoublyLinkedListGetBy.
121
+ * It specifies the criteria by which the first element should be retrieved from the doubly linked list. The default
122
+ * value is 'val', which means the first element will be retrieved by its value. Other possible values for "by
123
+ * @returns The method `pollFirst` returns either the value of the first node in the doubly linked list, the first node
124
+ * itself, or null if the list is empty. The specific return type depends on the value of the `by` parameter. If `by`
125
+ * is set to 'node', the method returns the first node. If `by` is set to 'val', the method returns the value
96
126
  */
97
127
  pollFirst(by: DoublyLinkedListGetBy = 'val'): T | DoublyLinkedListNode<T> | null {
98
128
  if (this._size === 0) return null;
@@ -120,7 +150,14 @@ export class DoublyLinkedList<T> {
120
150
  pollLast(by: 'val'): T | null;
121
151
  pollLast(by: 'node'): DoublyLinkedListNode<T> | null;
122
152
  /**
123
- * Removes a node at the end of the linked list and will return the node value
153
+ * The function `pollLast` removes and returns the last element in a doubly linked list, either as a node or its value,
154
+ * depending on the specified parameter.
155
+ * @param {DoublyLinkedListGetBy} [by=val] - The parameter "by" is of type DoublyLinkedListGetBy, which is an enum that
156
+ * can have two possible values: 'node' or 'val'. It determines the type of value that will be returned by the pollLast
157
+ * method. If 'node' is specified, the method will return the
158
+ * @returns The method `pollLast` returns either a `DoublyLinkedListNode<T>`, the value of the node (`T`), or `null`.
159
+ * The specific type that is returned depends on the value of the `by` parameter. If `by` is set to `'node'`, then a
160
+ * `DoublyLinkedListNode<T>` is returned. If `by` is set to `'
124
161
  */
125
162
  pollLast(by: DoublyLinkedListGetBy = 'val'): DoublyLinkedListNode<T> | T | null {
126
163
  if (this._size === 0) return null;
@@ -226,8 +263,12 @@ export class DoublyLinkedList<T> {
226
263
  }
227
264
 
228
265
  /**
229
- * Removes a node at the specified index and returns its value.
230
- * @param index Index at which the node has to be removed.
266
+ * The `remove` function removes an element at a specified index from a data structure, updating the links between
267
+ * nodes accordingly.
268
+ * @param {number} index - The index parameter represents the position of the element to be removed in the data
269
+ * structure. It is of type number.
270
+ * @returns The `remove` method returns the value of the removed element (`T`) if the removal is successful, or `null`
271
+ * if the index is out of bounds.
231
272
  */
232
273
  remove(index: number): T | null {
233
274
  if (index < 0 || index > this._size - 1) return null;
@@ -1,4 +1,7 @@
1
- import type {TMapFunction, TTestFunction} from '../types';
1
+ /**
2
+ * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
+ * @license MIT
4
+ */
2
5
 
3
6
  /**
4
7
  * The class which represents one link or node in a linked list
@@ -173,7 +176,11 @@ export class SinglyLinkedList<NodeData = any> {
173
176
  * ```
174
177
  * @param f A function to be applied to the val of each node
175
178
  */
176
- public findNodeIndex(f: TTestFunction<NodeData>): ({
179
+ public findNodeIndex(f: (
180
+ data: NodeData,
181
+ index: number,
182
+ list: SinglyLinkedList<NodeData>,
183
+ ) => boolean): ({
177
184
  node: SinglyLinkedListNode<NodeData>,
178
185
  index: number,
179
186
  }) | undefined {
@@ -201,7 +208,11 @@ export class SinglyLinkedList<NodeData = any> {
201
208
  * ```
202
209
  * @param f Function to test val against
203
210
  */
204
- public findNode(f: TTestFunction<NodeData>): SinglyLinkedListNode<NodeData> | undefined {
211
+ public findNode(f: (
212
+ data: NodeData,
213
+ index: number,
214
+ list: SinglyLinkedList<NodeData>,
215
+ ) => boolean): SinglyLinkedListNode<NodeData> | undefined {
205
216
  const nodeIndex = this.findNodeIndex(f);
206
217
  return nodeIndex !== undefined ? nodeIndex.node : undefined;
207
218
  }
@@ -214,7 +225,11 @@ export class SinglyLinkedList<NodeData = any> {
214
225
  * ```
215
226
  * @param f Function to test val against
216
227
  */
217
- public find(f: TTestFunction<NodeData>): NodeData | undefined {
228
+ public find(f: (
229
+ data: NodeData,
230
+ index: number,
231
+ list: SinglyLinkedList<NodeData>,
232
+ ) => boolean): NodeData | undefined {
218
233
  const nodeIndex = this.findNodeIndex(f);
219
234
  return nodeIndex !== undefined ? nodeIndex.node.val : undefined;
220
235
  }
@@ -227,7 +242,11 @@ export class SinglyLinkedList<NodeData = any> {
227
242
  * ```
228
243
  * @param f Function to test val against
229
244
  */
230
- public findIndex(f: TTestFunction<NodeData>): number {
245
+ public findIndex(f: (
246
+ data: NodeData,
247
+ index: number,
248
+ list: SinglyLinkedList<NodeData>,
249
+ ) => boolean): number {
231
250
  const nodeIndex = this.findNodeIndex(f);
232
251
  return nodeIndex !== undefined ? nodeIndex.index : -1;
233
252
  }
@@ -601,7 +620,11 @@ export class SinglyLinkedList<NodeData = any> {
601
620
  * @param f Function to execute for each element, taking up to three arguments.
602
621
  * @param reverse Indicates if the list should be walked in reverse order, default is false
603
622
  */
604
- public forEach(f: TMapFunction<NodeData>, reverse = false): void {
623
+ public forEach(f: (
624
+ data: any,
625
+ index: number,
626
+ list: SinglyLinkedList<NodeData>,
627
+ ) => any, reverse = false): void {
605
628
  let currentIndex = reverse ? this.length - 1 : 0;
606
629
  let currentNode = reverse ? this.tail : this.head;
607
630
  const modifier = reverse ? -1 : 1;
@@ -623,7 +646,11 @@ export class SinglyLinkedList<NodeData = any> {
623
646
  * @param reverse Indicates if the list should be mapped in reverse order, default is false
624
647
  */
625
648
  // eslint-disable-next-line @typescript-eslint/ban-types
626
- public map(f: TMapFunction<NodeData>, reverse = false): SinglyLinkedList<NodeData | {}> {
649
+ public map(f: (
650
+ data: any,
651
+ index: number,
652
+ list: SinglyLinkedList<NodeData>,
653
+ ) => any, reverse = false): SinglyLinkedList<NodeData | {}> {
627
654
  const list = new SinglyLinkedList();
628
655
  this.forEach((val, index) => list.append(f(val, index, this)), reverse);
629
656
  return list;
@@ -639,7 +666,11 @@ export class SinglyLinkedList<NodeData = any> {
639
666
  * @param reverse Indicates if the list should be filtered in reverse order, default is false
640
667
  */
641
668
  // eslint-disable-next-line @typescript-eslint/ban-types
642
- public filter(f: TTestFunction<NodeData>, reverse = false): SinglyLinkedList<NodeData | {}> {
669
+ public filter(f: (
670
+ data: NodeData,
671
+ index: number,
672
+ list: SinglyLinkedList<NodeData>,
673
+ ) => boolean, reverse = false): SinglyLinkedList<NodeData | {}> {
643
674
  const list = new SinglyLinkedList();
644
675
  this.forEach((val, index) => {
645
676
  if (f(val, index, this)) {
@@ -1,12 +1,23 @@
1
+ /**
2
+ * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
+ * @license MIT
4
+ */
1
5
  // todo need to be improved
2
6
  export class MatrixNTI2D<T = number> {
3
7
  private readonly _matrix: Array<Array<T>>;
4
8
 
9
+ /**
10
+ * The constructor creates a matrix with the specified number of rows and columns, and initializes all elements to a
11
+ * given initial value or 0 if not provided.
12
+ * @param options - An object containing the following properties:
13
+ */
5
14
  constructor(options: { row: number, col: number, initialVal?: T }) {
6
15
  const {row, col, initialVal} = options;
7
16
  this._matrix = new Array(row).fill(undefined).map(() => new Array(col).fill(initialVal || 0));
8
17
  }
9
18
 
19
+ /* The `toArray` method returns the matrix as a two-dimensional array. It converts the internal representation of the
20
+ matrix, which is an array of arrays, into a format that is more commonly used in JavaScript. */
10
21
  toArray(): Array<Array<T>> {
11
22
  return this._matrix;
12
23
  }