data-structure-typed 1.33.2 → 1.33.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.
- package/{.eslintrc.json → .eslintrc.js} +2 -1
- package/.github/workflows/ci.yml +15 -3
- package/.github/workflows/release-package.yml +32 -0
- package/{.prettierrc → .prettierrc.js} +1 -1
- package/CHANGELOG.md +5 -1
- package/README.md +2 -3
- package/coverage/coverage-final.json +64 -64
- package/coverage/coverage-summary.json +2 -2
- package/dist/data-structures/graph/abstract-graph.js +12 -12
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/data-structures/priority-queue/priority-queue.js +6 -6
- package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/docs/index.html +2 -3
- package/package.json +68 -60
- package/src/data-structures/binary-tree/aa-tree.ts +1 -0
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +1608 -0
- package/src/data-structures/binary-tree/avl-tree.ts +307 -0
- package/src/data-structures/binary-tree/b-tree.ts +1 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +76 -0
- package/src/data-structures/binary-tree/binary-tree.ts +47 -0
- package/src/data-structures/binary-tree/bst.ts +537 -0
- package/src/data-structures/binary-tree/index.ts +12 -0
- package/src/data-structures/binary-tree/rb-tree.ts +366 -0
- package/src/data-structures/binary-tree/segment-tree.ts +242 -0
- package/src/data-structures/binary-tree/splay-tree.ts +1 -0
- package/src/data-structures/binary-tree/tree-multiset.ts +700 -0
- package/src/data-structures/binary-tree/two-three-tree.ts +1 -0
- package/src/data-structures/graph/abstract-graph.ts +1040 -0
- package/src/data-structures/graph/directed-graph.ts +470 -0
- package/src/data-structures/graph/index.ts +4 -0
- package/src/data-structures/graph/map-graph.ts +129 -0
- package/src/data-structures/graph/undirected-graph.ts +274 -0
- package/src/data-structures/hash/coordinate-map.ts +67 -0
- package/src/data-structures/hash/coordinate-set.ts +56 -0
- package/src/data-structures/hash/hash-table.ts +157 -0
- package/src/data-structures/hash/index.ts +6 -0
- package/src/data-structures/hash/pair.ts +1 -0
- package/src/data-structures/hash/tree-map.ts +1 -0
- package/src/data-structures/hash/tree-set.ts +1 -0
- package/src/data-structures/heap/heap.ts +212 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +31 -0
- package/src/data-structures/heap/min-heap.ts +32 -0
- package/src/data-structures/index.ts +11 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +636 -0
- package/src/data-structures/linked-list/index.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +501 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +1 -0
- package/src/data-structures/matrix/index.ts +4 -0
- package/src/data-structures/matrix/matrix.ts +27 -0
- package/src/data-structures/matrix/matrix2d.ts +213 -0
- package/src/data-structures/matrix/navigator.ts +121 -0
- package/src/data-structures/matrix/vector2d.ts +316 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +56 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/priority-queue.ts +359 -0
- package/src/data-structures/queue/deque.ts +297 -0
- package/src/data-structures/queue/index.ts +2 -0
- package/src/data-structures/queue/queue.ts +191 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +98 -0
- package/src/data-structures/tree/index.ts +1 -0
- package/src/data-structures/tree/tree.ts +69 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +225 -0
- package/src/index.ts +4 -0
- package/src/interfaces/abstract-binary-tree.ts +189 -0
- package/src/interfaces/abstract-graph.ts +31 -0
- package/src/interfaces/avl-tree.ts +25 -0
- package/src/interfaces/binary-tree.ts +6 -0
- package/src/interfaces/bst.ts +31 -0
- package/src/interfaces/directed-graph.ts +20 -0
- package/src/interfaces/doubly-linked-list.ts +1 -0
- package/src/interfaces/heap.ts +1 -0
- package/src/interfaces/index.ts +15 -0
- package/src/interfaces/navigator.ts +1 -0
- package/src/interfaces/priority-queue.ts +1 -0
- package/src/interfaces/rb-tree.ts +9 -0
- package/src/interfaces/segment-tree.ts +1 -0
- package/src/interfaces/singly-linked-list.ts +1 -0
- package/src/interfaces/tree-multiset.ts +7 -0
- package/src/interfaces/undirected-graph.ts +6 -0
- package/src/types/data-structures/abstract-binary-tree.ts +50 -0
- package/src/types/data-structures/abstract-graph.ts +11 -0
- package/src/types/data-structures/avl-tree.ts +5 -0
- package/src/types/data-structures/binary-tree.ts +5 -0
- package/src/types/data-structures/bst.ts +13 -0
- package/src/types/data-structures/directed-graph.ts +8 -0
- package/src/types/data-structures/doubly-linked-list.ts +1 -0
- package/src/types/data-structures/heap.ts +5 -0
- package/src/types/data-structures/index.ts +15 -0
- package/src/types/data-structures/map-graph.ts +1 -0
- package/src/types/data-structures/navigator.ts +13 -0
- package/src/types/data-structures/priority-queue.ts +9 -0
- package/src/types/data-structures/rb-tree.ts +8 -0
- package/src/types/data-structures/segment-tree.ts +1 -0
- package/src/types/data-structures/singly-linked-list.ts +1 -0
- package/src/types/data-structures/tree-multiset.ts +6 -0
- package/src/types/helpers.ts +1 -0
- package/src/types/index.ts +3 -0
- package/src/types/utils/index.ts +2 -0
- package/src/types/utils/utils.ts +6 -0
- package/src/types/utils/validate-type.ts +35 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/utils.ts +79 -0
- package/test/integration/avl-tree.test.ts +14 -17
- package/test/integration/bst.test.ts +50 -41
- package/test/integration/heap.test.js +0 -3
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +14 -17
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +0 -4
- package/test/unit/data-structures/binary-tree/bst.test.ts +50 -41
- package/test/unit/data-structures/binary-tree/overall.test.ts +36 -28
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +0 -1
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +23 -12
- package/test/unit/data-structures/graph/directed-graph.test.ts +27 -25
- package/test/unit/data-structures/graph/map-graph.test.ts +4 -5
- package/test/unit/data-structures/graph/overall.test.ts +10 -11
- package/test/unit/data-structures/graph/undirected-graph.test.ts +0 -1
- package/test/unit/data-structures/heap/heap.test.ts +7 -8
- package/test/unit/data-structures/heap/max-heap.test.ts +7 -5
- package/test/unit/data-structures/heap/min-heap.test.ts +6 -5
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +9 -10
- package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +4 -7
- package/test/unit/data-structures/linked-list/skip-linked-list.test.ts +3 -3
- package/test/unit/data-structures/matrix/matrix.test.ts +4 -4
- package/test/unit/data-structures/matrix/navigator.test.ts +4 -5
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +5 -7
- package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +13 -13
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +8 -8
- package/test/unit/data-structures/queue/deque.test.ts +0 -1
- package/test/unit/data-structures/queue/queue.test.ts +1 -5
- package/test/unit/data-structures/trie/trie.test.ts +2 -2
- package/test/utils/magnitude.ts +3 -3
- package/tsconfig.json +3 -12
- package/tsconfig.prod.json +25 -0
- package/.auto-changelog +0 -9
- package/.gitattributes +0 -112
- package/.idea/codeStyles/Project.xml +0 -61
- package/.idea/codeStyles/codeStyleConfig.xml +0 -5
- package/.idea/data-structure-typed.iml +0 -19
- package/.idea/inspectionProfiles/Project_Default.xml +0 -6
- package/.idea/misc.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/.prettierignore +0 -6
- package/webpack.config.js +0 -28
|
@@ -0,0 +1,274 @@
|
|
|
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 {arrayRemove} from '../../utils';
|
|
9
|
+
import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
|
|
10
|
+
import type {VertexId} from '../../types';
|
|
11
|
+
import {IUNDirectedGraph} from '../../interfaces';
|
|
12
|
+
|
|
13
|
+
export class UndirectedVertex<V = any> extends AbstractVertex<V> {
|
|
14
|
+
/**
|
|
15
|
+
* The constructor function initializes a vertex with an optional value.
|
|
16
|
+
* @param {VertexId} id - The `id` parameter is of type `VertexId` and represents the identifier of the vertex. It is
|
|
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
|
|
19
|
+
* vertex. If no value is provided, the vertex will be initialized with a default value.
|
|
20
|
+
*/
|
|
21
|
+
constructor(id: VertexId, val?: V) {
|
|
22
|
+
super(id, val);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class UndirectedEdge<V = number> extends AbstractEdge<V> {
|
|
27
|
+
/**
|
|
28
|
+
* The constructor function creates an instance of a class with two vertex IDs, an optional weight, and an optional
|
|
29
|
+
* value.
|
|
30
|
+
* @param {VertexId} v1 - The first vertex ID of the edge.
|
|
31
|
+
* @param {VertexId} v2 - The parameter `v2` is a `VertexId`, which represents the identifier of the second vertex in a
|
|
32
|
+
* graph edge.
|
|
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
|
|
35
|
+
* with the edge.
|
|
36
|
+
*/
|
|
37
|
+
constructor(v1: VertexId, v2: VertexId, weight?: number, val?: V) {
|
|
38
|
+
super(weight, val);
|
|
39
|
+
this._vertices = [v1, v2];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
private _vertices: [VertexId, VertexId];
|
|
43
|
+
|
|
44
|
+
get vertices() {
|
|
45
|
+
return this._vertices;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
set vertices(v: [VertexId, VertexId]) {
|
|
49
|
+
this._vertices = v;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export class UndirectedGraph<
|
|
54
|
+
V extends UndirectedVertex<any> = UndirectedVertex,
|
|
55
|
+
E extends UndirectedEdge<any> = UndirectedEdge
|
|
56
|
+
>
|
|
57
|
+
extends AbstractGraph<V, E>
|
|
58
|
+
implements IUNDirectedGraph<V, E>
|
|
59
|
+
{
|
|
60
|
+
/**
|
|
61
|
+
* The constructor initializes a new Map object to store edges.
|
|
62
|
+
*/
|
|
63
|
+
constructor() {
|
|
64
|
+
super();
|
|
65
|
+
this._edges = new Map<V, E[]>();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
protected _edges: Map<V, E[]>;
|
|
69
|
+
|
|
70
|
+
get edges(): Map<V, E[]> {
|
|
71
|
+
return this._edges;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The function creates a new vertex with an optional value and returns it.
|
|
76
|
+
* @param {VertexId} id - The `id` parameter is the unique identifier for the vertex. It is used to distinguish one
|
|
77
|
+
* 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,
|
|
79
|
+
* it will be used as the value of the vertex. If no value is provided, the `id` parameter will be used as the value of
|
|
80
|
+
* the vertex.
|
|
81
|
+
* @returns The method is returning a new instance of the `UndirectedVertex` class, casted as type `V`.
|
|
82
|
+
*/
|
|
83
|
+
override createVertex(id: VertexId, val?: V['val']): V {
|
|
84
|
+
return new UndirectedVertex(id, val ?? id) as V;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The function creates an undirected edge between two vertices with an optional weight and value.
|
|
89
|
+
* @param {VertexId} v1 - The parameter `v1` represents the first vertex of the edge.
|
|
90
|
+
* @param {VertexId} v2 - The parameter `v2` represents the second vertex of the edge.
|
|
91
|
+
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. If
|
|
92
|
+
* 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
|
|
94
|
+
* 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`.
|
|
96
|
+
*/
|
|
97
|
+
override createEdge(v1: VertexId, v2: VertexId, weight?: number, val?: E['val']): E {
|
|
98
|
+
return new UndirectedEdge(v1, v2, weight ?? 1, val) as E;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The function `getEdge` returns the first edge that connects two vertices, or null if no such edge exists.
|
|
103
|
+
* @param {V | null | VertexId} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `V` (vertex
|
|
104
|
+
* object), `null`, or `VertexId` (a string or number representing the ID of a vertex).
|
|
105
|
+
* @param {V | null | VertexId} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `V` (vertex
|
|
106
|
+
* object), `null`, or `VertexId` (vertex ID).
|
|
107
|
+
* @returns an edge (E) or null.
|
|
108
|
+
*/
|
|
109
|
+
getEdge(v1: V | null | VertexId, v2: V | null | VertexId): E | null {
|
|
110
|
+
let edges: E[] | undefined = [];
|
|
111
|
+
|
|
112
|
+
if (v1 !== null && v2 !== null) {
|
|
113
|
+
const vertex1: V | null = this._getVertex(v1);
|
|
114
|
+
const vertex2: V | null = this._getVertex(v2);
|
|
115
|
+
|
|
116
|
+
if (vertex1 && vertex2) {
|
|
117
|
+
edges = this._edges.get(vertex1)?.filter(e => e.vertices.includes(vertex2.id));
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return edges ? edges[0] || null : null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The function removes an edge between two vertices in a graph and returns the removed edge.
|
|
126
|
+
* @param {V | VertexId} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexId`).
|
|
127
|
+
* @param {V | VertexId} v2 - V | VertexId - This parameter can be either a vertex object (V) or a vertex ID
|
|
128
|
+
* (VertexId). 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.
|
|
130
|
+
*/
|
|
131
|
+
removeEdgeBetween(v1: V | VertexId, v2: V | VertexId): E | null {
|
|
132
|
+
const vertex1: V | null = this._getVertex(v1);
|
|
133
|
+
const vertex2: V | null = this._getVertex(v2);
|
|
134
|
+
|
|
135
|
+
if (!vertex1 || !vertex2) {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const v1Edges = this._edges.get(vertex1);
|
|
140
|
+
let removed: E | null = null;
|
|
141
|
+
if (v1Edges) {
|
|
142
|
+
removed = arrayRemove<E>(v1Edges, (e: E) => e.vertices.includes(vertex2.id))[0] || null;
|
|
143
|
+
}
|
|
144
|
+
const v2Edges = this._edges.get(vertex2);
|
|
145
|
+
if (v2Edges) {
|
|
146
|
+
arrayRemove<E>(v2Edges, (e: E) => e.vertices.includes(vertex1.id));
|
|
147
|
+
}
|
|
148
|
+
return removed;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* The removeEdge 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
|
+
*/
|
|
156
|
+
removeEdge(edge: E): E | null {
|
|
157
|
+
return this.removeEdgeBetween(edge.vertices[0], edge.vertices[1]);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* The function `degreeOf` returns the degree of a vertex in a graph, which is the number of edges connected to that
|
|
162
|
+
* vertex.
|
|
163
|
+
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
|
|
164
|
+
* @returns The function `degreeOf` returns the degree of a vertex in a graph. The degree of a vertex is the number of
|
|
165
|
+
* edges connected to that vertex.
|
|
166
|
+
*/
|
|
167
|
+
degreeOf(vertexOrId: VertexId | V): number {
|
|
168
|
+
const vertex = this._getVertex(vertexOrId);
|
|
169
|
+
if (vertex) {
|
|
170
|
+
return this._edges.get(vertex)?.length || 0;
|
|
171
|
+
} else {
|
|
172
|
+
return 0;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* The function returns the edges of a given vertex or vertex ID.
|
|
178
|
+
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`. A `VertexId` is a
|
|
179
|
+
* unique identifier for a vertex in a graph, while `V` represents the type of the vertex.
|
|
180
|
+
* @returns an array of edges.
|
|
181
|
+
*/
|
|
182
|
+
edgesOf(vertexOrId: VertexId | V): E[] {
|
|
183
|
+
const vertex = this._getVertex(vertexOrId);
|
|
184
|
+
if (vertex) {
|
|
185
|
+
return this._edges.get(vertex) || [];
|
|
186
|
+
} else {
|
|
187
|
+
return [];
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* 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[]`.
|
|
194
|
+
*/
|
|
195
|
+
edgeSet(): E[] {
|
|
196
|
+
const edgeSet: Set<E> = new Set();
|
|
197
|
+
this._edges.forEach(edges => {
|
|
198
|
+
edges.forEach(edge => {
|
|
199
|
+
edgeSet.add(edge);
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
return [...edgeSet];
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* The function "getNeighbors" returns an array of neighboring vertices for a given vertex or vertex ID.
|
|
207
|
+
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
208
|
+
* (`VertexId`).
|
|
209
|
+
* @returns an array of vertices (V[]).
|
|
210
|
+
*/
|
|
211
|
+
getNeighbors(vertexOrId: V | VertexId): V[] {
|
|
212
|
+
const neighbors: V[] = [];
|
|
213
|
+
const vertex = this._getVertex(vertexOrId);
|
|
214
|
+
if (vertex) {
|
|
215
|
+
const neighborEdges = this.edgesOf(vertex);
|
|
216
|
+
for (const edge of neighborEdges) {
|
|
217
|
+
const neighbor = this._getVertex(edge.vertices.filter(e => e !== vertex.id)[0]);
|
|
218
|
+
if (neighbor) {
|
|
219
|
+
neighbors.push(neighbor);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return neighbors;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* The function "getEndsOfEdge" returns the vertices at the ends of an edge if the edge exists in the graph, otherwise
|
|
228
|
+
* 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
|
+
* graph. If the edge does not exist, it returns `null`.
|
|
232
|
+
*/
|
|
233
|
+
getEndsOfEdge(edge: E): [V, V] | null {
|
|
234
|
+
if (!this.hasEdge(edge.vertices[0], edge.vertices[1])) {
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
const v1 = this._getVertex(edge.vertices[0]);
|
|
238
|
+
const v2 = this._getVertex(edge.vertices[1]);
|
|
239
|
+
if (v1 && v2) {
|
|
240
|
+
return [v1, v2];
|
|
241
|
+
} else {
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* 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.
|
|
249
|
+
* @returns a boolean value.
|
|
250
|
+
*/
|
|
251
|
+
protected _addEdgeOnly(edge: E): boolean {
|
|
252
|
+
for (const end of edge.vertices) {
|
|
253
|
+
const endVertex = this._getVertex(end);
|
|
254
|
+
if (endVertex === null) return false;
|
|
255
|
+
if (endVertex) {
|
|
256
|
+
const edges = this._edges.get(endVertex);
|
|
257
|
+
if (edges) {
|
|
258
|
+
edges.push(edge);
|
|
259
|
+
} else {
|
|
260
|
+
this._edges.set(endVertex, [edge]);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return true;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* 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.
|
|
270
|
+
*/
|
|
271
|
+
protected _setEdges(v: Map<V, E[]>) {
|
|
272
|
+
this._edges = v;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
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
|
+
export class CoordinateMap<V> extends Map<any, V> {
|
|
9
|
+
constructor(joint?: string) {
|
|
10
|
+
super();
|
|
11
|
+
if (joint !== undefined) this._joint = joint;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
protected _joint = '_';
|
|
15
|
+
|
|
16
|
+
get joint(): string {
|
|
17
|
+
return this._joint;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The "has" function overrides the base class's "has" function and checks if a key exists in the map by joining the
|
|
22
|
+
* key array with a specified delimiter.
|
|
23
|
+
* @param {number[]} key - The parameter "key" is an array of numbers.
|
|
24
|
+
* @returns The `has` method is being overridden to return the result of calling the `has` method of the superclass
|
|
25
|
+
* (`super.has`) with the `key` array joined together using the `_joint` property.
|
|
26
|
+
*/
|
|
27
|
+
override has(key: number[]) {
|
|
28
|
+
return super.has(key.join(this._joint));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The function overrides the set method of a Map object to convert the key from an array to a string using a specified
|
|
33
|
+
* delimiter before calling the original set method.
|
|
34
|
+
* @param {number[]} key - The key parameter is an array of numbers.
|
|
35
|
+
* @param {V} value - The value parameter is the value that you want to associate with the specified key.
|
|
36
|
+
* @returns The `set` method is returning the result of calling the `set` method of the superclass
|
|
37
|
+
* (`super.set(key.join(this._joint), value)`).
|
|
38
|
+
*/
|
|
39
|
+
override set(key: number[], value: V) {
|
|
40
|
+
return super.set(key.join(this._joint), value);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The function overrides the get method to join the key array with a specified joint and then calls the super get
|
|
45
|
+
* method.
|
|
46
|
+
* @param {number[]} key - An array of numbers
|
|
47
|
+
* @returns The code is returning the value associated with the specified key in the map.
|
|
48
|
+
*/
|
|
49
|
+
override get(key: number[]) {
|
|
50
|
+
return super.get(key.join(this._joint));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The function overrides the delete method and joins the key array using a specified joint character before calling
|
|
55
|
+
* the super delete method.
|
|
56
|
+
* @param {number[]} key - An array of numbers that represents the key to be deleted.
|
|
57
|
+
* @returns The `delete` method is returning the result of calling the `delete` method on the superclass, with the
|
|
58
|
+
* `key` array joined together using the `_joint` property.
|
|
59
|
+
*/
|
|
60
|
+
override delete(key: number[]) {
|
|
61
|
+
return super.delete(key.join(this._joint));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
protected _setJoint(v: string) {
|
|
65
|
+
this._joint = v;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
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
|
+
export class CoordinateSet extends Set<any> {
|
|
9
|
+
constructor(joint?: string) {
|
|
10
|
+
super();
|
|
11
|
+
if (joint !== undefined) this._joint = joint;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
protected _joint = '_';
|
|
15
|
+
|
|
16
|
+
get joint(): string {
|
|
17
|
+
return this._joint;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The "has" function overrides the "has" method of the superclass and checks if a value exists in an array after
|
|
22
|
+
* joining its elements with a specified separator.
|
|
23
|
+
* @param {number[]} value - The parameter "value" is an array of numbers.
|
|
24
|
+
* @returns The overridden `has` method is returning the result of calling the `has` method of the superclass, passing
|
|
25
|
+
* in the joined value as an argument.
|
|
26
|
+
*/
|
|
27
|
+
override has(value: number[]) {
|
|
28
|
+
return super.has(value.join(this._joint));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The "add" function overrides the parent class's "add" function by joining the elements of the input array with a
|
|
33
|
+
* specified delimiter before calling the parent class's "add" function.
|
|
34
|
+
* @param {number[]} value - An array of numbers
|
|
35
|
+
* @returns The overridden `add` method is returning the result of calling the `add` method of the superclass
|
|
36
|
+
* (`super.add`) with the joined string representation of the `value` array (`value.join(this._joint)`).
|
|
37
|
+
*/
|
|
38
|
+
override add(value: number[]) {
|
|
39
|
+
return super.add(value.join(this._joint));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The function overrides the delete method and deletes an element from a Set by joining the elements of the input
|
|
44
|
+
* array with a specified joint and then calling the delete method of the parent class.
|
|
45
|
+
* @param {number[]} value - An array of numbers
|
|
46
|
+
* @returns The `delete` method is returning the result of calling the `delete` method of the superclass, with the
|
|
47
|
+
* `value` array joined together using the `_joint` property.
|
|
48
|
+
*/
|
|
49
|
+
override delete(value: number[]) {
|
|
50
|
+
return super.delete(value.join(this._joint));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
protected _setJoint(v: string) {
|
|
54
|
+
this._joint = v;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
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
|
+
export class HashNode<K, V> {
|
|
9
|
+
key: K;
|
|
10
|
+
val: V;
|
|
11
|
+
next: HashNode<K, V> | null;
|
|
12
|
+
|
|
13
|
+
constructor(key: K, val: V) {
|
|
14
|
+
this.key = key;
|
|
15
|
+
this.val = val;
|
|
16
|
+
this.next = null;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class HashTable<K, V> {
|
|
21
|
+
get buckets(): Array<HashNode<K, V> | null> {
|
|
22
|
+
return this._buckets;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
set buckets(value: Array<HashNode<K, V> | null>) {
|
|
26
|
+
this._buckets = value;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
get size(): number {
|
|
30
|
+
return this._size;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
set size(value: number) {
|
|
34
|
+
this._size = value;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
get capacity(): number {
|
|
38
|
+
return this._capacity;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
set capacity(value: number) {
|
|
42
|
+
this._capacity = value;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
private _capacity: number;
|
|
46
|
+
private _size: number;
|
|
47
|
+
private _buckets: Array<HashNode<K, V> | null>;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The constructor initializes the capacity, size, and buckets of an object.
|
|
51
|
+
* @param [capacity=1000] - The `capacity` parameter represents the maximum number of elements that the data structure
|
|
52
|
+
* can hold. It is an optional parameter with a default value of 1000.
|
|
53
|
+
*/
|
|
54
|
+
constructor(capacity = 1000) {
|
|
55
|
+
this._capacity = capacity;
|
|
56
|
+
this._size = 0;
|
|
57
|
+
this._buckets = new Array(this.capacity).fill(null);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The hash function takes a key, converts it to a string, calculates the sum of the ASCII values of its characters, and
|
|
62
|
+
* returns the remainder when divided by the capacity of the data structure.
|
|
63
|
+
* @param {K} key - The `key` parameter represents the key that needs to be hashed. It is of type `K`, which means it can
|
|
64
|
+
* be any data type that can be converted to a string.
|
|
65
|
+
* @returns The hash value of the key modulo the capacity of the data structure.
|
|
66
|
+
*/
|
|
67
|
+
private hash(key: K): number {
|
|
68
|
+
const keyString = String(key);
|
|
69
|
+
let hash = 0;
|
|
70
|
+
for (let i = 0; i < keyString.length; i++) {
|
|
71
|
+
hash += keyString.charCodeAt(i);
|
|
72
|
+
}
|
|
73
|
+
return hash % this.capacity;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The put function adds a key-value pair to a hash table, handling collisions by chaining.
|
|
78
|
+
* @param {K} key - The key parameter represents the key of the key-value pair that you want to insert into the hash
|
|
79
|
+
* table. It is of type K, which can be any data type that can be used as a key, such as a string, number, or object.
|
|
80
|
+
* @param {V} val - The `val` parameter represents the value associated with the key in the hash table.
|
|
81
|
+
* @returns Nothing is being returned. The return type of the function is void, which means it does not return any value.
|
|
82
|
+
*/
|
|
83
|
+
put(key: K, val: V): void {
|
|
84
|
+
const index = this.hash(key);
|
|
85
|
+
const newNode = new HashNode(key, val);
|
|
86
|
+
|
|
87
|
+
if (!this.buckets[index]) {
|
|
88
|
+
this.buckets[index] = newNode;
|
|
89
|
+
} else {
|
|
90
|
+
// Handle collision by chaining
|
|
91
|
+
let currentNode = this.buckets[index]!;
|
|
92
|
+
while (currentNode.next) {
|
|
93
|
+
if (currentNode.key === key) {
|
|
94
|
+
// Update the val if the key already exists
|
|
95
|
+
currentNode.val = val;
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
currentNode = currentNode.next;
|
|
99
|
+
}
|
|
100
|
+
if (currentNode.key === key) {
|
|
101
|
+
// Update the val if the key already exists (last node)
|
|
102
|
+
currentNode.val = val;
|
|
103
|
+
} else {
|
|
104
|
+
// Add the new node to the end of the chain
|
|
105
|
+
currentNode.next = newNode;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
this.size++;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* The `get` function retrieves the value associated with a given key from a hash table.
|
|
113
|
+
* @param {K} key - The parameter "key" represents the key of the element that we want to retrieve from the data
|
|
114
|
+
* structure.
|
|
115
|
+
* @returns The method is returning the value associated with the given key if it exists in the hash table. If the key is
|
|
116
|
+
* not found, it returns `undefined`.
|
|
117
|
+
*/
|
|
118
|
+
get(key: K): V | undefined {
|
|
119
|
+
const index = this.hash(key);
|
|
120
|
+
let currentNode = this.buckets[index];
|
|
121
|
+
|
|
122
|
+
while (currentNode) {
|
|
123
|
+
if (currentNode.key === key) {
|
|
124
|
+
return currentNode.val;
|
|
125
|
+
}
|
|
126
|
+
currentNode = currentNode.next;
|
|
127
|
+
}
|
|
128
|
+
return undefined; // Key not found
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* The `remove` function removes a key-value pair from a hash table.
|
|
133
|
+
* @param {K} key - The `key` parameter represents the key of the key-value pair that needs to be removed from the hash
|
|
134
|
+
* table.
|
|
135
|
+
* @returns Nothing is being returned. The `remove` method has a return type of `void`, which means it does not return
|
|
136
|
+
* any value.
|
|
137
|
+
*/
|
|
138
|
+
remove(key: K): void {
|
|
139
|
+
const index = this.hash(key);
|
|
140
|
+
let currentNode = this.buckets[index];
|
|
141
|
+
let prevNode: HashNode<K, V> | null = null;
|
|
142
|
+
|
|
143
|
+
while (currentNode) {
|
|
144
|
+
if (currentNode.key === key) {
|
|
145
|
+
if (prevNode) {
|
|
146
|
+
prevNode.next = currentNode.next;
|
|
147
|
+
} else {
|
|
148
|
+
this.buckets[index] = currentNode.next;
|
|
149
|
+
}
|
|
150
|
+
this.size--;
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
prevNode = currentNode;
|
|
154
|
+
currentNode = currentNode.next;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export class Pair {}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export class TreeMap {}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export class TreeSet {}
|