data-structure-typed 1.15.2 → 1.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -24
- package/dist/data-structures/heap/heap.d.ts +30 -15
- package/dist/data-structures/heap/heap.js +60 -15
- package/dist/data-structures/heap/max-heap.d.ts +3 -3
- package/dist/data-structures/heap/min-heap.d.ts +3 -3
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +195 -68
- package/dist/data-structures/linked-list/doubly-linked-list.js +456 -244
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +126 -241
- package/dist/data-structures/linked-list/singly-linked-list.js +331 -628
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +4 -5
- package/dist/data-structures/priority-queue/max-priority-queue.js +30 -6
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +4 -5
- package/dist/data-structures/priority-queue/min-priority-queue.js +31 -6
- package/dist/data-structures/priority-queue/priority-queue.d.ts +13 -2
- package/dist/data-structures/priority-queue/priority-queue.js +28 -22
- package/dist/data-structures/tree/index.d.ts +1 -0
- package/dist/data-structures/tree/index.js +17 -0
- package/dist/data-structures/tree/tree.d.ts +9 -0
- package/dist/data-structures/tree/tree.js +52 -0
- package/dist/data-structures/types/binary-tree.d.ts +0 -5
- package/dist/data-structures/types/heap.d.ts +0 -4
- package/dist/utils/types/utils.d.ts +1 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +92 -0
- package/docs/assets/main.js +58 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1367 -0
- package/docs/classes/AVLTree.html +2191 -0
- package/docs/classes/AVLTreeNode.html +573 -0
- package/docs/classes/AaTree.html +149 -0
- package/docs/classes/AbstractEdge.html +268 -0
- package/docs/classes/AbstractGraph.html +926 -0
- package/docs/classes/AbstractVertex.html +214 -0
- package/docs/classes/ArrayDeque.html +416 -0
- package/docs/classes/BST.html +2037 -0
- package/docs/classes/BSTNode.html +569 -0
- package/docs/classes/BTree.html +149 -0
- package/docs/classes/BinaryIndexedTree.html +288 -0
- package/docs/classes/BinaryTree.html +1826 -0
- package/docs/classes/BinaryTreeNode.html +532 -0
- package/docs/classes/Character.html +197 -0
- package/docs/classes/CoordinateMap.html +468 -0
- package/docs/classes/CoordinateSet.html +429 -0
- package/docs/classes/Deque.html +897 -0
- package/docs/classes/DirectedEdge.html +353 -0
- package/docs/classes/DirectedGraph.html +1242 -0
- package/docs/classes/DirectedVertex.html +225 -0
- package/docs/classes/DoublyLinkedList.html +891 -0
- package/docs/classes/DoublyLinkedListNode.html +274 -0
- package/docs/classes/Heap.html +481 -0
- package/docs/classes/HeapItem.html +232 -0
- package/docs/classes/Matrix2D.html +479 -0
- package/docs/classes/MatrixNTI2D.html +217 -0
- package/docs/classes/MaxHeap.html +499 -0
- package/docs/classes/MaxPriorityQueue.html +808 -0
- package/docs/classes/MinHeap.html +500 -0
- package/docs/classes/MinPriorityQueue.html +810 -0
- package/docs/classes/Navigator.html +290 -0
- package/docs/classes/ObjectDeque.html +422 -0
- package/docs/classes/PriorityQueue.html +732 -0
- package/docs/classes/Queue.html +369 -0
- package/docs/classes/RBTree.html +149 -0
- package/docs/classes/SegmentTree.html +344 -0
- package/docs/classes/SegmentTreeNode.html +417 -0
- package/docs/classes/SinglyLinkedList.html +718 -0
- package/docs/classes/SinglyLinkedListNode.html +247 -0
- package/docs/classes/SplayTree.html +149 -0
- package/docs/classes/Stack.html +345 -0
- package/docs/classes/TreeMultiSet.html +2035 -0
- package/docs/classes/TreeNode.html +235 -0
- package/docs/classes/Trie.html +349 -0
- package/docs/classes/TrieNode.html +257 -0
- package/docs/classes/TwoThreeTree.html +149 -0
- package/docs/classes/UndirectedEdge.html +312 -0
- package/docs/classes/UndirectedGraph.html +1079 -0
- package/docs/classes/UndirectedVertex.html +225 -0
- package/docs/classes/Vector2D.html +782 -0
- package/docs/enums/CP.html +158 -0
- package/docs/enums/FamilyPosition.html +158 -0
- package/docs/enums/LoopType.html +159 -0
- package/docs/index.html +493 -0
- package/docs/interfaces/AVLTreeDeleted.html +160 -0
- package/docs/interfaces/HeapOptions.html +166 -0
- package/docs/interfaces/IDirectedGraph.html +242 -0
- package/docs/interfaces/IGraph.html +426 -0
- package/docs/interfaces/NavigatorParams.html +196 -0
- package/docs/interfaces/PriorityQueueOptions.html +167 -0
- package/docs/modules.html +216 -0
- package/docs/types/BSTComparator.html +139 -0
- package/docs/types/BSTDeletedResult.html +136 -0
- package/docs/types/BinaryTreeDeleted.html +136 -0
- package/docs/types/BinaryTreeNodeId.html +124 -0
- package/docs/types/BinaryTreeNodePropertyName.html +124 -0
- package/docs/types/DFSOrderPattern.html +124 -0
- package/docs/types/DijkstraResult.html +144 -0
- package/docs/types/Direction.html +124 -0
- package/docs/types/DoublyLinkedListGetBy.html +124 -0
- package/docs/types/NodeOrPropertyName.html +124 -0
- package/docs/types/PriorityQueueComparator.html +144 -0
- package/docs/types/PriorityQueueDFSOrderPattern.html +124 -0
- package/docs/types/ResultByProperty.html +129 -0
- package/docs/types/ResultsByProperty.html +129 -0
- package/docs/types/SegmentTreeNodeVal.html +124 -0
- package/docs/types/SpecifyOptional.html +131 -0
- package/docs/types/Thunk.html +132 -0
- package/docs/types/ToThunkFn.html +132 -0
- package/docs/types/TopologicalStatus.html +124 -0
- package/docs/types/TreeMultiSetDeletedResult.html +136 -0
- package/docs/types/TrlAsyncFn.html +137 -0
- package/docs/types/TrlFn.html +137 -0
- package/docs/types/Turning.html +124 -0
- package/docs/types/VertexId.html +124 -0
- package/{tests/unit/data-structures/binary-tree → notes}/bst.test.ts +37 -50
- package/notes/note.md +23 -0
- package/package.json +1 -3
- package/.idea/data-structure-typed.iml +0 -14
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/src/assets/complexities-diff.jpg +0 -0
- package/src/assets/data-structure-complexities.jpg +0 -0
- package/src/assets/logo.png +0 -0
- package/src/assets/overview-diagram-of-data-structures.png +0 -0
- package/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/src/data-structures/binary-tree/avl-tree.ts +0 -293
- package/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -69
- package/src/data-structures/binary-tree/binary-tree.ts +0 -1492
- package/src/data-structures/binary-tree/bst.ts +0 -497
- package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
- package/src/data-structures/binary-tree/index.ts +0 -11
- package/src/data-structures/binary-tree/rb-tree.ts +0 -3
- package/src/data-structures/binary-tree/segment-tree.ts +0 -267
- package/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -53
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/src/data-structures/diagrams/README.md +0 -5
- package/src/data-structures/graph/abstract-graph.ts +0 -958
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
- package/src/data-structures/graph/diagrams/mst.jpg +0 -0
- package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
- package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
- package/src/data-structures/graph/directed-graph.ts +0 -429
- package/src/data-structures/graph/index.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +0 -259
- package/src/data-structures/hash/coordinate-map.ts +0 -74
- package/src/data-structures/hash/coordinate-set.ts +0 -63
- package/src/data-structures/hash/hash-table.ts +0 -1
- package/src/data-structures/hash/index.ts +0 -6
- package/src/data-structures/hash/pair.ts +0 -1
- package/src/data-structures/hash/tree-map.ts +0 -1
- package/src/data-structures/hash/tree-set.ts +0 -1
- package/src/data-structures/heap/heap.ts +0 -162
- package/src/data-structures/heap/index.ts +0 -3
- package/src/data-structures/heap/max-heap.ts +0 -31
- package/src/data-structures/heap/min-heap.ts +0 -34
- package/src/data-structures/index.ts +0 -13
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -365
- package/src/data-structures/linked-list/index.ts +0 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -757
- package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
- package/src/data-structures/matrix/index.ts +0 -4
- package/src/data-structures/matrix/matrix.ts +0 -27
- package/src/data-structures/matrix/matrix2d.ts +0 -208
- package/src/data-structures/matrix/navigator.ts +0 -122
- package/src/data-structures/matrix/vector2d.ts +0 -316
- package/src/data-structures/priority-queue/index.ts +0 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +0 -24
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -24
- package/src/data-structures/priority-queue/priority-queue.ts +0 -349
- package/src/data-structures/queue/deque.ts +0 -251
- package/src/data-structures/queue/index.ts +0 -2
- package/src/data-structures/queue/queue.ts +0 -120
- package/src/data-structures/stack/index.ts +0 -1
- package/src/data-structures/stack/stack.ts +0 -98
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -225
- package/src/data-structures/types/abstract-graph.ts +0 -51
- package/src/data-structures/types/avl-tree.ts +0 -6
- package/src/data-structures/types/binary-tree.ts +0 -15
- package/src/data-structures/types/bst.ts +0 -5
- package/src/data-structures/types/directed-graph.ts +0 -18
- package/src/data-structures/types/doubly-linked-list.ts +0 -1
- package/src/data-structures/types/heap.ts +0 -8
- package/src/data-structures/types/index.ts +0 -13
- package/src/data-structures/types/navigator.ts +0 -13
- package/src/data-structures/types/priority-queue.ts +0 -9
- package/src/data-structures/types/segment-tree.ts +0 -1
- package/src/data-structures/types/singly-linked-list.ts +0 -1
- package/src/data-structures/types/tree-multiset.ts +0 -3
- package/src/index.ts +0 -1
- package/src/utils/index.ts +0 -2
- package/src/utils/types/index.ts +0 -1
- package/src/utils/types/utils.ts +0 -4
- package/src/utils/utils.ts +0 -78
- package/tests/unit/data-structures/graph/abstract-graph.ts +0 -0
- package/tests/unit/data-structures/graph/directed-graph.test.ts +0 -492
- package/tests/unit/data-structures/graph/index.ts +0 -3
- package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
|
@@ -1,259 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @author Tyler Zeng
|
|
5
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
-
* @license MIT License
|
|
7
|
-
*/
|
|
8
|
-
import {arrayRemove} from '../../utils';
|
|
9
|
-
import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
|
|
10
|
-
import type {VertexId} from '../types';
|
|
11
|
-
|
|
12
|
-
export class UndirectedVertex extends AbstractVertex {
|
|
13
|
-
/**
|
|
14
|
-
* The constructor function initializes an object with a given id.
|
|
15
|
-
* @param {VertexId} id - The `id` parameter is the identifier for the vertex. It is used to uniquely identify the
|
|
16
|
-
* vertex within a graph or network.
|
|
17
|
-
*/
|
|
18
|
-
constructor(id: VertexId) {
|
|
19
|
-
super(id);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export class UndirectedEdge extends AbstractEdge {
|
|
24
|
-
/**
|
|
25
|
-
* The constructor function initializes an instance of a class with two vertex IDs and an optional weight.
|
|
26
|
-
* @param {VertexId} v1 - The parameter `v1` is of type `VertexId` and represents the first vertex in the edge.
|
|
27
|
-
* @param {VertexId} v2 - The parameter `v2` is a `VertexId`, which represents the identifier of the second vertex in a
|
|
28
|
-
* graph.
|
|
29
|
-
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge
|
|
30
|
-
* between two vertices.
|
|
31
|
-
*/
|
|
32
|
-
constructor(v1: VertexId, v2: VertexId, weight?: number) {
|
|
33
|
-
super(weight);
|
|
34
|
-
this._vertices = [v1, v2];
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
private _vertices: [VertexId, VertexId];
|
|
38
|
-
get vertices() {
|
|
39
|
-
return this._vertices;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
set vertices(v: [VertexId, VertexId]) {
|
|
43
|
-
this._vertices = v;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
48
|
-
*/
|
|
49
|
-
getVertices() {
|
|
50
|
-
return this._vertices;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdge> extends AbstractGraph<V, E> {
|
|
55
|
-
constructor() {
|
|
56
|
-
super();
|
|
57
|
-
this._edges = new Map<V, E[]>();
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
protected _edges: Map<V, E[]>;
|
|
61
|
-
|
|
62
|
-
get edges(): Map<V, E[]> {
|
|
63
|
-
return this._edges;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
protected set edges(v: Map<V, E[]>) {
|
|
67
|
-
this._edges = v;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
72
|
-
*/
|
|
73
|
-
getEdges(): Map<V, E[]> {
|
|
74
|
-
return this._edges;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* The function `getEdge` returns the first edge that connects two vertices, or null if no such edge exists.
|
|
79
|
-
* @param {V | null | VertexId} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID
|
|
80
|
-
* (`VertexId`). It can also be `null`.
|
|
81
|
-
* @param {V | null | VertexId} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `V` (vertex
|
|
82
|
-
* object), `null`, or `VertexId` (vertex ID).
|
|
83
|
-
* @returns an edge (E) or null.
|
|
84
|
-
*/
|
|
85
|
-
getEdge(v1: V | null | VertexId, v2: V | null | VertexId): E | null {
|
|
86
|
-
let edges: E[] | undefined = [];
|
|
87
|
-
|
|
88
|
-
if (v1 !== null && v2 !== null) {
|
|
89
|
-
const vertex1: V | null = this.getVertex(v1);
|
|
90
|
-
const vertex2: V | null = this.getVertex(v2);
|
|
91
|
-
|
|
92
|
-
if (vertex1 && vertex2) {
|
|
93
|
-
edges = this._edges.get(vertex1)?.filter(e => e.vertices.includes(vertex2.id));
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return edges ? edges[0] || null : null;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* The function adds an edge to a graph by connecting two vertices.
|
|
102
|
-
* @param {E} edge - The `edge` parameter is an object of type `E`, which represents an edge in a graph.
|
|
103
|
-
* @returns a boolean value.
|
|
104
|
-
*/
|
|
105
|
-
addEdge(edge: E): boolean {
|
|
106
|
-
for (const end of edge.vertices) {
|
|
107
|
-
const endVertex = this.getVertex(end);
|
|
108
|
-
if (endVertex === null) return false;
|
|
109
|
-
if (endVertex) {
|
|
110
|
-
const edges = this._edges.get(endVertex);
|
|
111
|
-
if (edges) {
|
|
112
|
-
edges.push(edge);
|
|
113
|
-
} else {
|
|
114
|
-
this._edges.set(endVertex, [edge]);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
return true;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* The function removes an edge between two vertices in a graph and returns the removed edge, or null if either of the
|
|
123
|
-
* vertices does not exist.
|
|
124
|
-
* @param {V | VertexId} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexId`).
|
|
125
|
-
* @param {V | VertexId} v2 - V | VertexId: The second vertex or vertex ID of the edge to be removed.
|
|
126
|
-
* @returns the removed edge (E) if it exists, or null if either of the vertices (v1 or v2) does not exist.
|
|
127
|
-
*/
|
|
128
|
-
removeEdgeBetween(v1: V | VertexId, v2: V | VertexId): E | null {
|
|
129
|
-
|
|
130
|
-
const vertex1: V | null = this.getVertex(v1);
|
|
131
|
-
const vertex2: V | null = this.getVertex(v2);
|
|
132
|
-
|
|
133
|
-
if (!vertex1 || !vertex2) {
|
|
134
|
-
return null;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const v1Edges = this._edges.get(vertex1);
|
|
138
|
-
let removed: E | null = null;
|
|
139
|
-
if (v1Edges) {
|
|
140
|
-
removed = arrayRemove<E>(v1Edges, (e: UndirectedEdge) => e.vertices.includes(vertex2.id))[0] || null;
|
|
141
|
-
}
|
|
142
|
-
const v2Edges = this._edges.get(vertex2);
|
|
143
|
-
if (v2Edges) {
|
|
144
|
-
arrayRemove<E>(v2Edges, (e: UndirectedEdge) => e.vertices.includes(vertex1.id));
|
|
145
|
-
}
|
|
146
|
-
return removed;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* The removeEdge function removes an edge between two vertices in a graph.
|
|
151
|
-
* @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
|
|
152
|
-
* @returns The method is returning either the removed edge (of type E) or null if the edge was not found.
|
|
153
|
-
*/
|
|
154
|
-
removeEdge(edge: E): E | null {
|
|
155
|
-
return this.removeEdgeBetween(edge.vertices[0], edge.vertices[1]);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* The function `degreeOf` returns the degree of a vertex in a graph, which is the number of edges connected to that
|
|
160
|
-
* vertex.
|
|
161
|
-
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
|
|
162
|
-
* @returns The function `degreeOf` returns the degree of a vertex in a graph. The degree of a vertex is the number of
|
|
163
|
-
* edges that are incident to that vertex.
|
|
164
|
-
*/
|
|
165
|
-
degreeOf(vertexOrId: VertexId | V): number {
|
|
166
|
-
const vertex = this.getVertex(vertexOrId);
|
|
167
|
-
if (vertex) {
|
|
168
|
-
return this._edges.get(vertex)?.length || 0;
|
|
169
|
-
} else {
|
|
170
|
-
return 0;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* The function "edgesOf" returns an array of edges connected to a given vertex.
|
|
176
|
-
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
|
|
177
|
-
* @returns an array of edges connected to the specified vertex. If the vertex exists in the graph, the function
|
|
178
|
-
* returns the array of edges connected to that vertex. If the vertex does not exist in the graph, the function returns
|
|
179
|
-
* an empty array.
|
|
180
|
-
*/
|
|
181
|
-
edgesOf(vertexOrId: VertexId | V): E[] {
|
|
182
|
-
const vertex = this.getVertex(vertexOrId);
|
|
183
|
-
if (vertex) {
|
|
184
|
-
return this._edges.get(vertex) || [];
|
|
185
|
-
} else {
|
|
186
|
-
return [];
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* The function "edgeSet" returns an array of unique edges from a set of edges.
|
|
192
|
-
* @returns The method `edgeSet()` returns an array of type `E[]`.
|
|
193
|
-
*/
|
|
194
|
-
edgeSet(): E[] {
|
|
195
|
-
const edgeSet: Set<E> = new Set();
|
|
196
|
-
this._edges.forEach(edges => {
|
|
197
|
-
edges.forEach(edge => {
|
|
198
|
-
edgeSet.add(edge);
|
|
199
|
-
});
|
|
200
|
-
});
|
|
201
|
-
return [...edgeSet];
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* The function "getEdgesOf" returns an array of edges connected to a given vertex or vertex ID.
|
|
206
|
-
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID
|
|
207
|
-
* (`VertexId`).
|
|
208
|
-
* @returns an array of edges (E[]) that are connected to the specified vertex or vertex ID.
|
|
209
|
-
*/
|
|
210
|
-
getEdgesOf(vertexOrId: V | VertexId): E[] {
|
|
211
|
-
const vertex = this.getVertex(vertexOrId);
|
|
212
|
-
if (!vertex) {
|
|
213
|
-
return [];
|
|
214
|
-
}
|
|
215
|
-
return this._edges.get(vertex) || [];
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* The function "getNeighbors" returns an array of neighboring vertices of a given vertex.
|
|
220
|
-
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
221
|
-
* (`VertexId`).
|
|
222
|
-
* @returns an array of vertices (V[]).
|
|
223
|
-
*/
|
|
224
|
-
getNeighbors(vertexOrId: V | VertexId): V[] {
|
|
225
|
-
const neighbors: V[] = [];
|
|
226
|
-
const vertex = this.getVertex(vertexOrId);
|
|
227
|
-
if (vertex) {
|
|
228
|
-
const neighborEdges = this.getEdgesOf(vertex);
|
|
229
|
-
for (const edge of neighborEdges) {
|
|
230
|
-
const neighbor = this.getVertex(edge.vertices.filter(e => e !== vertex.id)[0]);
|
|
231
|
-
if (neighbor) {
|
|
232
|
-
neighbors.push(neighbor);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
return neighbors;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* The function "getEndsOfEdge" returns the vertices at the ends of a given edge, or null if the edge does not exist in
|
|
241
|
-
* the graph.
|
|
242
|
-
* @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
|
|
243
|
-
* @returns The function `getEndsOfEdge` returns an array containing two vertices `[V, V]` if the edge exists in the
|
|
244
|
-
* graph and both vertices are found. If the edge does not exist or one or both vertices are not found, it returns
|
|
245
|
-
* `null`.
|
|
246
|
-
*/
|
|
247
|
-
getEndsOfEdge(edge: E): [V, V] | null {
|
|
248
|
-
if (!this.hasEdge(edge.vertices[0], edge.vertices[1])) {
|
|
249
|
-
return null;
|
|
250
|
-
}
|
|
251
|
-
const v1 = this.getVertex(edge.vertices[0]);
|
|
252
|
-
const v2 = this.getVertex(edge.vertices[1]);
|
|
253
|
-
if (v1 && v2) {
|
|
254
|
-
return [v1, v2];
|
|
255
|
-
} else {
|
|
256
|
-
return null;
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @author Tyler Zeng
|
|
5
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
-
* @license MIT License
|
|
7
|
-
*/
|
|
8
|
-
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: string = '_';
|
|
15
|
-
|
|
16
|
-
get joint(): string {
|
|
17
|
-
return this._joint;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
protected set joint(v: string) {
|
|
21
|
-
this._joint = v;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
26
|
-
*/
|
|
27
|
-
getJoint(): string {
|
|
28
|
-
return this._joint;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* The "has" function overrides the base class's "has" function and checks if a key exists in the map by joining the
|
|
33
|
-
* key array with a specified delimiter.
|
|
34
|
-
* @param {number[]} key - The parameter "key" is an array of numbers.
|
|
35
|
-
* @returns The `has` method is being overridden to return the result of calling the `has` method of the superclass
|
|
36
|
-
* (`super.has`) with the `key` array joined together using the `_joint` property.
|
|
37
|
-
*/
|
|
38
|
-
override has(key: number[]) {
|
|
39
|
-
return super.has(key.join(this._joint));
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* The function overrides the set method of a Map object to convert the key from an array to a string using a specified
|
|
44
|
-
* delimiter before calling the original set method.
|
|
45
|
-
* @param {number[]} key - The key parameter is an array of numbers.
|
|
46
|
-
* @param {V} value - The value parameter is the value that you want to associate with the specified key.
|
|
47
|
-
* @returns The `set` method is returning the result of calling the `set` method of the superclass
|
|
48
|
-
* (`super.set(key.join(this._joint), value)`).
|
|
49
|
-
*/
|
|
50
|
-
override set(key: number[], value: V) {
|
|
51
|
-
return super.set(key.join(this._joint), value);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* The function overrides the get method to join the key array with a specified joint and then calls the super get
|
|
56
|
-
* method.
|
|
57
|
-
* @param {number[]} key - An array of numbers
|
|
58
|
-
* @returns The code is returning the value associated with the specified key in the map.
|
|
59
|
-
*/
|
|
60
|
-
override get(key: number[]) {
|
|
61
|
-
return super.get(key.join(this._joint));
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* The function overrides the delete method and joins the key array using a specified joint character before calling
|
|
66
|
-
* the super delete method.
|
|
67
|
-
* @param {number[]} key - An array of numbers that represents the key to be deleted.
|
|
68
|
-
* @returns The `delete` method is returning the result of calling the `delete` method on the superclass, with the
|
|
69
|
-
* `key` array joined together using the `_joint` property.
|
|
70
|
-
*/
|
|
71
|
-
override delete(key: number[]) {
|
|
72
|
-
return super.delete(key.join(this._joint));
|
|
73
|
-
}
|
|
74
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @author Tyler Zeng
|
|
5
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
-
* @license MIT License
|
|
7
|
-
*/
|
|
8
|
-
export class CoordinateSet extends Set {
|
|
9
|
-
constructor(joint?: string) {
|
|
10
|
-
super();
|
|
11
|
-
if (joint !== undefined) this._joint = joint;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
protected _joint: string = '_';
|
|
15
|
-
|
|
16
|
-
get joint(): string {
|
|
17
|
-
return this._joint;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
protected set joint(v: string) {
|
|
21
|
-
this._joint = v;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
26
|
-
*/
|
|
27
|
-
getJoint(): string {
|
|
28
|
-
return this._joint;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* The "has" function overrides the "has" method of the superclass and checks if a value exists in an array after
|
|
33
|
-
* joining its elements with a specified separator.
|
|
34
|
-
* @param {number[]} value - The parameter "value" is an array of numbers.
|
|
35
|
-
* @returns The overridden `has` method is returning the result of calling the `has` method of the superclass, passing
|
|
36
|
-
* in the joined value as an argument.
|
|
37
|
-
*/
|
|
38
|
-
override has(value: number[]) {
|
|
39
|
-
return super.has(value.join(this._joint));
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* The "add" function overrides the parent class's "add" function by joining the elements of the input array with a
|
|
44
|
-
* specified delimiter before calling the parent class's "add" function.
|
|
45
|
-
* @param {number[]} value - An array of numbers
|
|
46
|
-
* @returns The overridden `add` method is returning the result of calling the `add` method of the superclass
|
|
47
|
-
* (`super.add`) with the joined string representation of the `value` array (`value.join(this._joint)`).
|
|
48
|
-
*/
|
|
49
|
-
override add(value: number[]) {
|
|
50
|
-
return super.add(value.join(this._joint));
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* The function overrides the delete method and deletes an element from a Set by joining the elements of the input
|
|
55
|
-
* array with a specified joint and then calling the delete method of the parent class.
|
|
56
|
-
* @param {number[]} value - An array of numbers
|
|
57
|
-
* @returns The `delete` method is returning the result of calling the `delete` method of the superclass, with the
|
|
58
|
-
* `value` array joined together using the `_joint` property.
|
|
59
|
-
*/
|
|
60
|
-
override delete(value: number[]) {
|
|
61
|
-
return super.delete(value.join(this._joint));
|
|
62
|
-
}
|
|
63
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @author Tyler Zeng
|
|
5
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
-
* @license MIT License
|
|
7
|
-
*/
|
|
8
|
-
import {PriorityQueue} from '../priority-queue';
|
|
9
|
-
import type {HeapItem, HeapOptions} from '../types';
|
|
10
|
-
|
|
11
|
-
export abstract class Heap<T> {
|
|
12
|
-
/**
|
|
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.
|
|
16
|
-
*/
|
|
17
|
-
protected constructor(options?: HeapOptions<T>) {
|
|
18
|
-
if (options) {
|
|
19
|
-
const {priority} = options;
|
|
20
|
-
if (priority !== undefined && typeof priority !== 'function') {
|
|
21
|
-
throw new Error('.constructor expects a valid priority function');
|
|
22
|
-
}
|
|
23
|
-
this._priorityCb = priority || ((el) => +el);
|
|
24
|
-
} else {
|
|
25
|
-
this._priorityCb = (el) => +el;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
protected abstract _pq: PriorityQueue<HeapItem<T>>;
|
|
30
|
-
|
|
31
|
-
get pq() {
|
|
32
|
-
return this._pq;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
protected set pq(v: PriorityQueue<HeapItem<T>>) {
|
|
36
|
-
this._pq = v;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
protected _priorityCb: (element: T) => number;
|
|
40
|
-
get priorityCb() {
|
|
41
|
-
return this._priorityCb;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
protected set priorityCb(v: (element: T) => number) {
|
|
45
|
-
this._priorityCb = v;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* The function returns the size of a priority queue.
|
|
50
|
-
* @returns The size of the priority queue.
|
|
51
|
-
*/
|
|
52
|
-
get size(): number {
|
|
53
|
-
return this._pq.size;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
58
|
-
*/
|
|
59
|
-
getPq() {
|
|
60
|
-
return this._pq;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
65
|
-
*/
|
|
66
|
-
getPriorityCb() {
|
|
67
|
-
return this._priorityCb;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
72
|
-
*/
|
|
73
|
-
getSize(): number {
|
|
74
|
-
return this._pq.size;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* The function checks if a priority queue is empty.
|
|
79
|
-
* @returns {boolean} A boolean value indicating whether the size of the priority queue is less than 1.
|
|
80
|
-
*/
|
|
81
|
-
isEmpty(): boolean {
|
|
82
|
-
return this._pq.size < 1;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* The `peek` function returns the top item in the priority queue without removing it.
|
|
87
|
-
* @returns The `peek()` method is returning either a `HeapItem<T>` object or `null`.Returns an element with the highest priority in the queue
|
|
88
|
-
*/
|
|
89
|
-
peek(): HeapItem<T> | null {
|
|
90
|
-
return this._pq.peek();
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* The `peekLast` function returns the last item in the heap.
|
|
95
|
-
* @returns The method `peekLast()` returns either a `HeapItem<T>` object or `null`.Returns an element with the lowest priority in the queue
|
|
96
|
-
*/
|
|
97
|
-
peekLast(): HeapItem<T> | null {
|
|
98
|
-
return this._pq.leaf();
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* The `add` function adds an element to a priority queue with an optional priority value.
|
|
103
|
-
* @param {T} element - The `element` parameter represents the value that you want to add to the heap. It can be of any
|
|
104
|
-
* type.
|
|
105
|
-
* @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
|
|
106
|
-
* element being added to the heap. If the `element` parameter is a number, then the `priority` parameter is set to
|
|
107
|
-
* the value of `element`. If the `element` parameter is not a number, then the
|
|
108
|
-
* @returns The `add` method returns the instance of the `Heap` class.
|
|
109
|
-
* @throws {Error} if priority is not a valid number
|
|
110
|
-
*/
|
|
111
|
-
add(element: T, priority?: number): Heap<T> {
|
|
112
|
-
if (typeof element === 'number') {
|
|
113
|
-
priority = element;
|
|
114
|
-
} else {
|
|
115
|
-
if (priority === undefined) {
|
|
116
|
-
throw new Error('.add expects a numeric priority');
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
if (priority && Number.isNaN(+priority)) {
|
|
121
|
-
throw new Error('.add expects a numeric priority');
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if (Number.isNaN(+priority) && Number.isNaN(this._priorityCb(element))) {
|
|
125
|
-
throw new Error(
|
|
126
|
-
'.add expects a numeric priority '
|
|
127
|
-
+ 'or a constructor callback that returns a number'
|
|
128
|
-
);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const _priority = !Number.isNaN(+priority) ? priority : this._priorityCb(element);
|
|
132
|
-
this._pq.add({priority: _priority, element});
|
|
133
|
-
return this;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* 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
|
|
138
|
-
* @returns either a HeapItem<T> object or null.
|
|
139
|
-
*/
|
|
140
|
-
poll(): HeapItem<T> | null {
|
|
141
|
-
const top = this._pq.poll();
|
|
142
|
-
if (!top) {
|
|
143
|
-
return null;
|
|
144
|
-
}
|
|
145
|
-
return top;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* The `toArray` function returns an array of `HeapItem<T>` objects.
|
|
150
|
-
* @returns An array of HeapItem<T> objects.Returns a sorted list of elements
|
|
151
|
-
*/
|
|
152
|
-
toArray(): HeapItem<T>[] {
|
|
153
|
-
return this._pq.toArray();
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* The clear function clears the priority queue.
|
|
158
|
-
*/
|
|
159
|
-
clear(): void {
|
|
160
|
-
this._pq.clear();
|
|
161
|
-
}
|
|
162
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @author Tyler Zeng
|
|
5
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
-
* @license MIT License
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import {Heap} from './heap';
|
|
10
|
-
import {PriorityQueue} from '../priority-queue';
|
|
11
|
-
import type {HeapItem, HeapOptions} from '../types';
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* @class MaxHeap
|
|
15
|
-
* @extends Heap
|
|
16
|
-
*/
|
|
17
|
-
export class MaxHeap<T> extends Heap<T> {
|
|
18
|
-
protected _pq: PriorityQueue<HeapItem<T>>;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* The constructor initializes a PriorityQueue with a custom comparator function.
|
|
22
|
-
* @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
|
|
23
|
-
* type `HeapOptions<T>`, which is a generic type that represents the options for the heap.
|
|
24
|
-
*/
|
|
25
|
-
constructor(options?: HeapOptions<T>) {
|
|
26
|
-
super(options);
|
|
27
|
-
this._pq = new PriorityQueue<HeapItem<T>>({
|
|
28
|
-
comparator: (a, b) => b.priority - a.priority
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @author Tyler Zeng
|
|
5
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
-
* @license MIT License
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import {Heap} from './heap';
|
|
10
|
-
import {PriorityQueue} from '../priority-queue';
|
|
11
|
-
import type {HeapItem, HeapOptions} from '../types';
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* @class MinHeap
|
|
15
|
-
* @extends Heap
|
|
16
|
-
*/
|
|
17
|
-
export class MinHeap<T> extends Heap<T> {
|
|
18
|
-
protected _pq: PriorityQueue<HeapItem<T>>;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* The constructor initializes a PriorityQueue with a comparator function that compares the priority of two HeapItem
|
|
22
|
-
* objects.
|
|
23
|
-
* @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
|
|
24
|
-
* type `HeapOptions<T>`, which is a generic type that represents the options for the heap.
|
|
25
|
-
*/
|
|
26
|
-
constructor(options?: HeapOptions<T>) {
|
|
27
|
-
super(options);
|
|
28
|
-
this._pq = new PriorityQueue<HeapItem<T>>({
|
|
29
|
-
comparator: (a, b) => a.priority - b.priority
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export * from './hash';
|
|
2
|
-
export * from './linked-list';
|
|
3
|
-
export * from './stack';
|
|
4
|
-
export * from './queue';
|
|
5
|
-
export * from './graph';
|
|
6
|
-
export * from './binary-tree';
|
|
7
|
-
export * from './heap';
|
|
8
|
-
export * from './priority-queue';
|
|
9
|
-
export * from './matrix';
|
|
10
|
-
export * from './trie';
|
|
11
|
-
export * from './types';
|
|
12
|
-
|
|
13
|
-
|