min-heap-typed 1.39.4 → 1.39.6

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 (47) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +6 -6
  2. package/dist/data-structures/binary-tree/avl-tree.js +13 -13
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +7 -7
  4. package/dist/data-structures/binary-tree/binary-tree.js +17 -17
  5. package/dist/data-structures/binary-tree/bst.d.ts +6 -6
  6. package/dist/data-structures/binary-tree/bst.js +13 -13
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +2 -2
  8. package/dist/data-structures/binary-tree/rb-tree.js +4 -4
  9. package/dist/data-structures/binary-tree/segment-tree.d.ts +7 -7
  10. package/dist/data-structures/binary-tree/segment-tree.js +16 -16
  11. package/dist/data-structures/binary-tree/tree-multiset.d.ts +6 -6
  12. package/dist/data-structures/binary-tree/tree-multiset.js +18 -18
  13. package/dist/data-structures/graph/abstract-graph.d.ts +96 -96
  14. package/dist/data-structures/graph/abstract-graph.js +64 -64
  15. package/dist/data-structures/graph/directed-graph.d.ts +68 -68
  16. package/dist/data-structures/graph/directed-graph.js +48 -48
  17. package/dist/data-structures/graph/map-graph.d.ts +13 -13
  18. package/dist/data-structures/graph/map-graph.js +15 -15
  19. package/dist/data-structures/graph/undirected-graph.d.ts +42 -42
  20. package/dist/data-structures/graph/undirected-graph.js +32 -32
  21. package/dist/data-structures/hash/hash-table.d.ts +4 -4
  22. package/dist/data-structures/hash/hash-table.js +8 -8
  23. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +31 -31
  24. package/dist/data-structures/linked-list/doubly-linked-list.js +54 -54
  25. package/dist/data-structures/linked-list/singly-linked-list.d.ts +24 -24
  26. package/dist/data-structures/linked-list/singly-linked-list.js +52 -52
  27. package/dist/data-structures/queue/queue.d.ts +1 -1
  28. package/dist/data-structures/queue/queue.js +4 -4
  29. package/dist/interfaces/binary-tree.d.ts +2 -2
  30. package/dist/interfaces/graph.d.ts +3 -3
  31. package/package.json +2 -2
  32. package/src/data-structures/binary-tree/avl-tree.ts +13 -13
  33. package/src/data-structures/binary-tree/binary-tree.ts +18 -18
  34. package/src/data-structures/binary-tree/bst.ts +16 -16
  35. package/src/data-structures/binary-tree/rb-tree.ts +6 -6
  36. package/src/data-structures/binary-tree/segment-tree.ts +15 -15
  37. package/src/data-structures/binary-tree/tree-multiset.ts +18 -18
  38. package/src/data-structures/graph/abstract-graph.ts +156 -154
  39. package/src/data-structures/graph/directed-graph.ts +99 -94
  40. package/src/data-structures/graph/map-graph.ts +22 -25
  41. package/src/data-structures/graph/undirected-graph.ts +62 -60
  42. package/src/data-structures/hash/hash-table.ts +9 -9
  43. package/src/data-structures/linked-list/doubly-linked-list.ts +61 -61
  44. package/src/data-structures/linked-list/singly-linked-list.ts +58 -58
  45. package/src/data-structures/queue/queue.ts +2 -2
  46. package/src/interfaces/binary-tree.ts +2 -2
  47. package/src/interfaces/graph.ts +3 -3
@@ -15,15 +15,15 @@ export class UndirectedVertex<V = any> extends AbstractVertex<V> {
15
15
  * The constructor function initializes a vertex with an optional value.
16
16
  * @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
17
17
  * used to uniquely identify the vertex within a graph or network.
18
- * @param {V} [val] - The "val" parameter is an optional parameter of type V. It is used to initialize the value of the
18
+ * @param {V} [value] - The "value" parameter is an optional parameter of type V. It is used to initialize the value of the
19
19
  * vertex. If no value is provided, the vertex will be initialized with a default value.
20
20
  */
21
- constructor(key: VertexKey, val?: V) {
22
- super(key, val);
21
+ constructor(key: VertexKey, value?: V) {
22
+ super(key, value);
23
23
  }
24
24
  }
25
25
 
26
- export class UndirectedEdge<V = number> extends AbstractEdge<V> {
26
+ export class UndirectedEdge<E = number> extends AbstractEdge<E> {
27
27
  /**
28
28
  * The constructor function creates an instance of a class with two vertex IDs, an optional weight, and an optional
29
29
  * value.
@@ -31,11 +31,11 @@ export class UndirectedEdge<V = number> extends AbstractEdge<V> {
31
31
  * @param {VertexKey} v2 - The parameter `v2` is a `VertexKey`, which represents the identifier of the second vertex in a
32
32
  * graph edge.
33
33
  * @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
34
- * @param {V} [val] - The "val" parameter is an optional parameter of type V. It is used to store a value associated
34
+ * @param {E} [value] - The "value" parameter is an optional parameter of type E. It is used to store a value associated
35
35
  * with the edge.
36
36
  */
37
- constructor(v1: VertexKey, v2: VertexKey, weight?: number, val?: V) {
38
- super(weight, val);
37
+ constructor(v1: VertexKey, v2: VertexKey, weight?: number, value?: E) {
38
+ super(weight, value);
39
39
  this._vertices = [v1, v2];
40
40
  }
41
41
 
@@ -51,23 +51,25 @@ export class UndirectedEdge<V = number> extends AbstractEdge<V> {
51
51
  }
52
52
 
53
53
  export class UndirectedGraph<
54
- V extends UndirectedVertex<any> = UndirectedVertex,
55
- E extends UndirectedEdge<any> = UndirectedEdge
54
+ V = any,
55
+ E = any,
56
+ VO extends UndirectedVertex<V> = UndirectedVertex<V>,
57
+ EO extends UndirectedEdge<E> = UndirectedEdge<E>
56
58
  >
57
- extends AbstractGraph<V, E>
58
- implements IGraph<V, E>
59
+ extends AbstractGraph<V, E, VO, EO>
60
+ implements IGraph<V, E, VO, EO>
59
61
  {
60
62
  /**
61
63
  * The constructor initializes a new Map object to store edges.
62
64
  */
63
65
  constructor() {
64
66
  super();
65
- this._edges = new Map<V, E[]>();
67
+ this._edges = new Map<VO, EO[]>();
66
68
  }
67
69
 
68
- protected _edges: Map<V, E[]>;
70
+ protected _edges: Map<VO, EO[]>;
69
71
 
70
- get edges(): Map<V, E[]> {
72
+ get edges(): Map<VO, EO[]> {
71
73
  return this._edges;
72
74
  }
73
75
 
@@ -75,13 +77,13 @@ export class UndirectedGraph<
75
77
  * The function creates a new vertex with an optional value and returns it.
76
78
  * @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is used to distinguish one
77
79
  * vertex from another in the graph.
78
- * @param [val] - The `val` parameter is an optional value that can be assigned to the vertex. If a value is provided,
80
+ * @param [value] - The `value` parameter is an optional value that can be assigned to the vertex. If a value is provided,
79
81
  * it will be used as the value of the vertex. If no value is provided, the `key` parameter will be used as the value of
80
82
  * the vertex.
81
- * @returns The method is returning a new instance of the `UndirectedVertex` class, casted as type `V`.
83
+ * @returns The method is returning a new instance of the `UndirectedVertex` class, casted as type `VO`.
82
84
  */
83
- override createVertex(key: VertexKey, val?: V['val']): V {
84
- return new UndirectedVertex(key, val ?? key) as V;
85
+ override createVertex(key: VertexKey, value?: VO['value']): VO {
86
+ return new UndirectedVertex(key, value ?? key) as VO;
85
87
  }
86
88
 
87
89
  /**
@@ -90,28 +92,28 @@ export class UndirectedGraph<
90
92
  * @param {VertexKey} v2 - The parameter `v2` represents the second vertex of the edge.
91
93
  * @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. If
92
94
  * no weight is provided, it defaults to 1.
93
- * @param [val] - The `val` parameter is an optional value that can be assigned to the edge. It can be of any type and
95
+ * @param [value] - The `value` parameter is an optional value that can be assigned to the edge. It can be of any type and
94
96
  * is used to store additional information or data associated with the edge.
95
- * @returns a new instance of the `UndirectedEdge` class, which is casted as type `E`.
97
+ * @returns a new instance of the `UndirectedEdge` class, which is casted as type `EO`.
96
98
  */
97
- override createEdge(v1: VertexKey, v2: VertexKey, weight?: number, val?: E['val']): E {
98
- return new UndirectedEdge(v1, v2, weight ?? 1, val) as E;
99
+ override createEdge(v1: VertexKey, v2: VertexKey, weight?: number, value?: EO['value']): EO {
100
+ return new UndirectedEdge(v1, v2, weight ?? 1, value) as EO;
99
101
  }
100
102
 
101
103
  /**
102
104
  * The function `getEdge` returns the first edge that connects two vertices, or null if no such edge exists.
103
- * @param {V | null | VertexKey} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `V` (vertex
105
+ * @param {VO | VertexKey | null} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
104
106
  * object), `null`, or `VertexKey` (a string or number representing the ID of a vertex).
105
- * @param {V | null | VertexKey} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `V` (vertex
107
+ * @param {VO | VertexKey | null} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
106
108
  * object), `null`, or `VertexKey` (vertex ID).
107
- * @returns an edge (E) or null.
109
+ * @returns an edge (EO) or null.
108
110
  */
109
- getEdge(v1: V | null | VertexKey, v2: V | null | VertexKey): E | null {
110
- let edges: E[] | undefined = [];
111
+ getEdge(v1: VO | VertexKey | null, v2: VO | VertexKey | null): EO | null {
112
+ let edges: EO[] | undefined = [];
111
113
 
112
114
  if (v1 !== null && v2 !== null) {
113
- const vertex1: V | null = this._getVertex(v1);
114
- const vertex2: V | null = this._getVertex(v2);
115
+ const vertex1: VO | null = this._getVertex(v1);
116
+ const vertex2: VO | null = this._getVertex(v2);
115
117
 
116
118
  if (vertex1 && vertex2) {
117
119
  edges = this._edges.get(vertex1)?.filter(e => e.vertices.includes(vertex2.key));
@@ -123,48 +125,48 @@ export class UndirectedGraph<
123
125
 
124
126
  /**
125
127
  * The function removes an edge between two vertices in a graph and returns the removed edge.
126
- * @param {V | VertexKey} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexKey`).
127
- * @param {V | VertexKey} v2 - V | VertexKey - This parameter can be either a vertex object (V) or a vertex ID
128
+ * @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
129
+ * @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
128
130
  * (VertexKey). It represents the second vertex of the edge that needs to be removed.
129
- * @returns the removed edge (E) if it exists, or null if either of the vertices (V) does not exist.
131
+ * @returns the removed edge (EO) if it exists, or null if either of the vertices (VO) does not exist.
130
132
  */
131
- deleteEdgeBetween(v1: V | VertexKey, v2: V | VertexKey): E | null {
132
- const vertex1: V | null = this._getVertex(v1);
133
- const vertex2: V | null = this._getVertex(v2);
133
+ deleteEdgeBetween(v1: VO | VertexKey, v2: VO | VertexKey): EO | null {
134
+ const vertex1: VO | null = this._getVertex(v1);
135
+ const vertex2: VO | null = this._getVertex(v2);
134
136
 
135
137
  if (!vertex1 || !vertex2) {
136
138
  return null;
137
139
  }
138
140
 
139
141
  const v1Edges = this._edges.get(vertex1);
140
- let removed: E | null = null;
142
+ let removed: EO | null = null;
141
143
  if (v1Edges) {
142
- removed = arrayRemove<E>(v1Edges, (e: E) => e.vertices.includes(vertex2.key))[0] || null;
144
+ removed = arrayRemove<EO>(v1Edges, (e: EO) => e.vertices.includes(vertex2.key))[0] || null;
143
145
  }
144
146
  const v2Edges = this._edges.get(vertex2);
145
147
  if (v2Edges) {
146
- arrayRemove<E>(v2Edges, (e: E) => e.vertices.includes(vertex1.key));
148
+ arrayRemove<EO>(v2Edges, (e: EO) => e.vertices.includes(vertex1.key));
147
149
  }
148
150
  return removed;
149
151
  }
150
152
 
151
153
  /**
152
154
  * The deleteEdge function removes an edge between two vertices in a graph.
153
- * @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
154
- * @returns The method is returning either the removed edge (of type E) or null if the edge was not found.
155
+ * @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
156
+ * @returns The method is returning either the removed edge (of type EO) or null if the edge was not found.
155
157
  */
156
- deleteEdge(edge: E): E | null {
158
+ deleteEdge(edge: EO): EO | null {
157
159
  return this.deleteEdgeBetween(edge.vertices[0], edge.vertices[1]);
158
160
  }
159
161
 
160
162
  /**
161
163
  * The function `degreeOf` returns the degree of a vertex in a graph, which is the number of edges connected to that
162
164
  * vertex.
163
- * @param {VertexKey | V} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `V`.
165
+ * @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
164
166
  * @returns The function `degreeOf` returns the degree of a vertex in a graph. The degree of a vertex is the number of
165
167
  * edges connected to that vertex.
166
168
  */
167
- degreeOf(vertexOrKey: VertexKey | V): number {
169
+ degreeOf(vertexOrKey: VertexKey | VO): number {
168
170
  const vertex = this._getVertex(vertexOrKey);
169
171
  if (vertex) {
170
172
  return this._edges.get(vertex)?.length || 0;
@@ -175,11 +177,11 @@ export class UndirectedGraph<
175
177
 
176
178
  /**
177
179
  * The function returns the edges of a given vertex or vertex ID.
178
- * @param {VertexKey | V} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `V`. A `VertexKey` is a
179
- * unique identifier for a vertex in a graph, while `V` represents the type of the vertex.
180
+ * @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`. A `VertexKey` is a
181
+ * unique identifier for a vertex in a graph, while `VO` represents the type of the vertex.
180
182
  * @returns an array of edges.
181
183
  */
182
- edgesOf(vertexOrKey: VertexKey | V): E[] {
184
+ edgesOf(vertexOrKey: VertexKey | VO): EO[] {
183
185
  const vertex = this._getVertex(vertexOrKey);
184
186
  if (vertex) {
185
187
  return this._edges.get(vertex) || [];
@@ -190,10 +192,10 @@ export class UndirectedGraph<
190
192
 
191
193
  /**
192
194
  * The function "edgeSet" returns an array of unique edges from a set of edges.
193
- * @returns The method `edgeSet()` returns an array of type `E[]`.
195
+ * @returns The method `edgeSet()` returns an array of type `EO[]`.
194
196
  */
195
- edgeSet(): E[] {
196
- const edgeSet: Set<E> = new Set();
197
+ edgeSet(): EO[] {
198
+ const edgeSet: Set<EO> = new Set();
197
199
  this._edges.forEach(edges => {
198
200
  edges.forEach(edge => {
199
201
  edgeSet.add(edge);
@@ -204,12 +206,12 @@ export class UndirectedGraph<
204
206
 
205
207
  /**
206
208
  * The function "getNeighbors" returns an array of neighboring vertices for a given vertex or vertex ID.
207
- * @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`V`) or a vertex ID
209
+ * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
208
210
  * (`VertexKey`).
209
- * @returns an array of vertices (V[]).
211
+ * @returns an array of vertices (VO[]).
210
212
  */
211
- getNeighbors(vertexOrKey: V | VertexKey): V[] {
212
- const neighbors: V[] = [];
213
+ getNeighbors(vertexOrKey: VO | VertexKey): VO[] {
214
+ const neighbors: VO[] = [];
213
215
  const vertex = this._getVertex(vertexOrKey);
214
216
  if (vertex) {
215
217
  const neighborEdges = this.edgesOf(vertex);
@@ -226,11 +228,11 @@ export class UndirectedGraph<
226
228
  /**
227
229
  * The function "getEndsOfEdge" returns the vertices at the ends of an edge if the edge exists in the graph, otherwise
228
230
  * it returns null.
229
- * @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
230
- * @returns The function `getEndsOfEdge` returns an array containing two vertices `[V, V]` if the edge exists in the
231
+ * @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
232
+ * @returns The function `getEndsOfEdge` returns an array containing two vertices `[VO, VO]` if the edge exists in the
231
233
  * graph. If the edge does not exist, it returns `null`.
232
234
  */
233
- getEndsOfEdge(edge: E): [V, V] | null {
235
+ getEndsOfEdge(edge: EO): [VO, VO] | null {
234
236
  if (!this.hasEdge(edge.vertices[0], edge.vertices[1])) {
235
237
  return null;
236
238
  }
@@ -245,10 +247,10 @@ export class UndirectedGraph<
245
247
 
246
248
  /**
247
249
  * The function adds an edge to the graph by updating the adjacency list with the vertices of the edge.
248
- * @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
250
+ * @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
249
251
  * @returns a boolean value.
250
252
  */
251
- protected _addEdgeOnly(edge: E): boolean {
253
+ protected _addEdgeOnly(edge: EO): boolean {
252
254
  for (const end of edge.vertices) {
253
255
  const endVertex = this._getVertex(end);
254
256
  if (endVertex === null) return false;
@@ -266,9 +268,9 @@ export class UndirectedGraph<
266
268
 
267
269
  /**
268
270
  * The function sets the edges of a graph.
269
- * @param v - A map where the keys are of type V and the values are arrays of type E.
271
+ * @param v - A map where the keys are of type VO and the values are arrays of type EO.
270
272
  */
271
- protected _setEdges(v: Map<V, E[]>) {
273
+ protected _setEdges(v: Map<VO, EO[]>) {
272
274
  this._edges = v;
273
275
  }
274
276
  }
@@ -8,12 +8,12 @@
8
8
 
9
9
  export class HashTableNode<K, V> {
10
10
  key: K;
11
- val: V;
11
+ value: V;
12
12
  next: HashTableNode<K, V> | null;
13
13
 
14
- constructor(key: K, val: V) {
14
+ constructor(key: K, value: V) {
15
15
  this.key = key;
16
- this.val = val;
16
+ this.value = value;
17
17
  this.next = null;
18
18
  }
19
19
  }
@@ -71,14 +71,14 @@ export class HashTable<K, V> {
71
71
  * The set function adds a key-value pair to the hash table, handling collisions and resizing if necessary.
72
72
  * @param {K} key - The key parameter represents the key of the key-value pair that you want to insert into the hash
73
73
  * table. It is of type K, which is a generic type representing the key's data type.
74
- * @param {V} val - The parameter `val` represents the value that you want to associate with the given key in the hash
74
+ * @param {V} value - The parameter `value` represents the value that you want to associate with the given key in the hash
75
75
  * table.
76
76
  * @returns Nothing is being returned. The return type of the `put` method is `void`, which means it does not return any
77
77
  * value.
78
78
  */
79
- set(key: K, val: V): void {
79
+ set(key: K, value: V): void {
80
80
  const index = this._hash(key);
81
- const newNode = new HashTableNode<K, V>(key, val);
81
+ const newNode = new HashTableNode<K, V>(key, value);
82
82
 
83
83
  if (!this._buckets[index]) {
84
84
  this._buckets[index] = newNode;
@@ -88,7 +88,7 @@ export class HashTable<K, V> {
88
88
  while (currentNode) {
89
89
  if (currentNode.key === key) {
90
90
  // If the key already exists, update the value
91
- currentNode.val = val;
91
+ currentNode.value = value;
92
92
  return;
93
93
  }
94
94
  if (!currentNode.next) {
@@ -120,7 +120,7 @@ export class HashTable<K, V> {
120
120
 
121
121
  while (currentNode) {
122
122
  if (currentNode.key === key) {
123
- return currentNode.val;
123
+ return currentNode.value;
124
124
  }
125
125
  currentNode = currentNode.next;
126
126
  }
@@ -259,7 +259,7 @@ export class HashTable<K, V> {
259
259
  let currentNode = bucket;
260
260
  while (currentNode) {
261
261
  const newIndex = this._hash(currentNode.key);
262
- const newNode = new HashTableNode<K, V>(currentNode.key, currentNode.val);
262
+ const newNode = new HashTableNode<K, V>(currentNode.key, currentNode.value);
263
263
 
264
264
  if (!newBuckets[newIndex]) {
265
265
  newBuckets[newIndex] = newNode;