data-structure-typed 1.20.0 → 1.21.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/LICENSE +21 -0
- package/README.md +6 -4
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +11 -18
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +52 -74
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +6 -6
- package/dist/data-structures/binary-tree/tree-multiset.js +20 -28
- package/dist/data-structures/graph/abstract-graph.d.ts +3 -3
- package/dist/data-structures/graph/abstract-graph.js +6 -2
- package/dist/data-structures/graph/index.d.ts +1 -0
- package/dist/data-structures/graph/index.js +1 -0
- package/dist/data-structures/graph/map-graph.d.ts +79 -0
- package/dist/data-structures/graph/map-graph.js +111 -0
- package/dist/data-structures/interfaces/abstract-binary-tree.d.ts +0 -2
- package/dist/data-structures/tree/tree.d.ts +1 -1
- package/dist/data-structures/types/abstract-binary-tree.d.ts +0 -1
- package/dist/data-structures/types/abstract-graph.d.ts +1 -0
- package/dist/data-structures/types/index.d.ts +1 -0
- package/dist/data-structures/types/index.js +1 -0
- package/dist/data-structures/types/map-graph.d.ts +1 -0
- package/dist/data-structures/types/map-graph.js +2 -0
- package/dist/data-structures/types/tree-multiset.d.ts +1 -3
- package/package.json +1 -1
- package/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +0 -1479
- package/src/data-structures/binary-tree/avl-tree.ts +0 -301
- package/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -78
- package/src/data-structures/binary-tree/binary-tree.ts +0 -42
- package/src/data-structures/binary-tree/bst.ts +0 -438
- package/src/data-structures/binary-tree/index.ts +0 -12
- package/src/data-structures/binary-tree/rb-tree.ts +0 -102
- package/src/data-structures/binary-tree/segment-tree.ts +0 -243
- package/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -712
- 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 -1033
- package/src/data-structures/graph/directed-graph.ts +0 -472
- package/src/data-structures/graph/index.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +0 -270
- package/src/data-structures/hash/coordinate-map.ts +0 -67
- package/src/data-structures/hash/coordinate-set.ts +0 -56
- package/src/data-structures/hash/hash-table.ts +0 -3
- package/src/data-structures/hash/index.ts +0 -6
- package/src/data-structures/hash/pair.ts +0 -3
- package/src/data-structures/hash/tree-map.ts +0 -3
- package/src/data-structures/hash/tree-set.ts +0 -3
- package/src/data-structures/heap/heap.ts +0 -200
- 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 -15
- package/src/data-structures/interfaces/abstract-binary-tree.ts +0 -194
- package/src/data-structures/interfaces/abstract-graph.ts +0 -40
- package/src/data-structures/interfaces/avl-tree.ts +0 -28
- package/src/data-structures/interfaces/binary-tree.ts +0 -8
- package/src/data-structures/interfaces/bst.ts +0 -32
- package/src/data-structures/interfaces/directed-graph.ts +0 -20
- package/src/data-structures/interfaces/doubly-linked-list.ts +0 -1
- package/src/data-structures/interfaces/heap.ts +0 -1
- package/src/data-structures/interfaces/index.ts +0 -15
- package/src/data-structures/interfaces/navigator.ts +0 -1
- package/src/data-structures/interfaces/priority-queue.ts +0 -1
- package/src/data-structures/interfaces/rb-tree.ts +0 -11
- package/src/data-structures/interfaces/segment-tree.ts +0 -1
- package/src/data-structures/interfaces/singly-linked-list.ts +0 -1
- package/src/data-structures/interfaces/tree-multiset.ts +0 -12
- package/src/data-structures/interfaces/undirected-graph.ts +0 -6
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -573
- package/src/data-structures/linked-list/index.ts +0 -3
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -490
- package/src/data-structures/linked-list/skip-linked-list.ts +0 -3
- 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 -49
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -50
- package/src/data-structures/priority-queue/priority-queue.ts +0 -354
- 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/tree/index.ts +0 -1
- package/src/data-structures/tree/tree.ts +0 -69
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -227
- package/src/data-structures/types/abstract-binary-tree.ts +0 -41
- package/src/data-structures/types/abstract-graph.ts +0 -5
- package/src/data-structures/types/avl-tree.ts +0 -5
- package/src/data-structures/types/binary-tree.ts +0 -9
- package/src/data-structures/types/bst.ts +0 -12
- package/src/data-structures/types/directed-graph.ts +0 -8
- package/src/data-structures/types/doubly-linked-list.ts +0 -1
- package/src/data-structures/types/heap.ts +0 -5
- package/src/data-structures/types/helpers.ts +0 -1
- package/src/data-structures/types/index.ts +0 -15
- package/src/data-structures/types/navigator.ts +0 -13
- package/src/data-structures/types/priority-queue.ts +0 -9
- package/src/data-structures/types/rb-tree.ts +0 -8
- 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 -8
- package/src/index.ts +0 -2
- package/src/utils/index.ts +0 -3
- package/src/utils/types/index.ts +0 -2
- package/src/utils/types/utils.ts +0 -6
- package/src/utils/types/validate-type.ts +0 -25
- package/src/utils/utils.ts +0 -78
- package/src/utils/validate-type.ts +0 -69
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -356,13 +356,15 @@ import {UndirectedGraph} from 'data-structure-typed';
|
|
|
356
356
|
</table>
|
|
357
357
|
|
|
358
358
|
|
|
359
|
-

|
|
359
|
+

|
|
360
360
|
|
|
361
|
-

|
|
362
362
|
|
|
363
|
-

|
|
364
364
|
|
|
365
|
-

|
|
366
|
+
|
|
367
|
+

|
|
366
368
|
|
|
367
369
|
|
|
368
370
|
## API docs & Examples
|
|
@@ -54,8 +54,6 @@ export declare abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNod
|
|
|
54
54
|
get size(): number;
|
|
55
55
|
private _loopType;
|
|
56
56
|
get loopType(): LoopType;
|
|
57
|
-
private _isMergeDuplicatedNodeById;
|
|
58
|
-
get isMergeDuplicatedNodeById(): boolean;
|
|
59
57
|
private _visitedId;
|
|
60
58
|
get visitedId(): BinaryTreeNodeId[];
|
|
61
59
|
private _visitedVal;
|
|
@@ -96,16 +94,6 @@ export declare abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNod
|
|
|
96
94
|
* @returns The function `add` returns either the inserted node (`N`), `null`, or `undefined`.
|
|
97
95
|
*/
|
|
98
96
|
add(idOrNode: BinaryTreeNodeId | N | null, val?: N['val']): N | null | undefined;
|
|
99
|
-
/**
|
|
100
|
-
* The function adds a new node to the left or right child of a parent node, updating the size of the tree if
|
|
101
|
-
* necessary.
|
|
102
|
-
* @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to the tree. It can be
|
|
103
|
-
* either a node object (`N`) or `null`.
|
|
104
|
-
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will be added as a
|
|
105
|
-
* child.
|
|
106
|
-
* @returns either the left child node, the right child node, or undefined.
|
|
107
|
-
*/
|
|
108
|
-
addTo(newNode: N | null, parent: N): N | null | undefined;
|
|
109
97
|
/**
|
|
110
98
|
* The `addMany` function adds multiple nodes to a tree data structure and returns an array of the inserted nodes or
|
|
111
99
|
* null/undefined values.
|
|
@@ -285,6 +273,17 @@ export declare abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNod
|
|
|
285
273
|
* Space complexity of Iterative DFS equals to recursive DFS which is O(n) because of the stack
|
|
286
274
|
*/
|
|
287
275
|
morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): N[];
|
|
276
|
+
/**
|
|
277
|
+
* The function adds a new node to a binary tree if there is an available position.
|
|
278
|
+
* @param {N | null} newNode - The `newNode` parameter is of type `N | null`, which means it can either be a node of
|
|
279
|
+
* type `N` or `null`. It represents the node that you want to add to the binary tree.
|
|
280
|
+
* @param {N} parent - The parent parameter is of type N, which represents a node in a binary tree.
|
|
281
|
+
* @returns either the left or right child node of the parent node, depending on which child is available for adding
|
|
282
|
+
* the new node. If a new node is added, the function also updates the size of the binary tree. If neither the left nor
|
|
283
|
+
* right child is available, the function returns undefined. If the parent node is null, the function also returns
|
|
284
|
+
* undefined.
|
|
285
|
+
*/
|
|
286
|
+
protected _addTo(newNode: N | null, parent: N): N | null | undefined;
|
|
288
287
|
/**
|
|
289
288
|
* The function sets the loop type for a protected variable.
|
|
290
289
|
* @param {LoopType} value - The value parameter is of type LoopType.
|
|
@@ -310,12 +309,6 @@ export declare abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNod
|
|
|
310
309
|
* @param {number[]} value - An array of numbers that represents the visited left sum.
|
|
311
310
|
*/
|
|
312
311
|
protected _setVisitedLeftSum(value: number[]): void;
|
|
313
|
-
/**
|
|
314
|
-
* The function sets the value of a protected property called "_isMergeDuplicatedNodeById".
|
|
315
|
-
* @param {boolean} value - The value parameter is a boolean value that determines whether the isMergeDuplicatedNodeById
|
|
316
|
-
* property should be set to true or false.
|
|
317
|
-
*/
|
|
318
|
-
protected _setIsDuplicatedVal(value: boolean): void;
|
|
319
312
|
/**
|
|
320
313
|
* The function sets the root property of an object to a given value, and if the value is not null, it also sets the
|
|
321
314
|
* parent property of the value to undefined.
|
|
@@ -113,21 +113,14 @@ class AbstractBinaryTree {
|
|
|
113
113
|
this._root = null;
|
|
114
114
|
this._size = 0;
|
|
115
115
|
this._loopType = types_1.LoopType.ITERATIVE;
|
|
116
|
-
// TODO this variable may be moved to TreeMultiset
|
|
117
|
-
this._isMergeDuplicatedNodeById = true;
|
|
118
116
|
this._visitedId = [];
|
|
119
117
|
this._visitedVal = [];
|
|
120
118
|
this._visitedNode = [];
|
|
121
119
|
this._visitedLeftSum = [];
|
|
122
120
|
if (options !== undefined) {
|
|
123
|
-
const { loopType = types_1.LoopType.ITERATIVE
|
|
124
|
-
this._isMergeDuplicatedNodeById = isMergeDuplicatedNodeById;
|
|
121
|
+
const { loopType = types_1.LoopType.ITERATIVE } = options;
|
|
125
122
|
this._loopType = loopType;
|
|
126
123
|
}
|
|
127
|
-
else {
|
|
128
|
-
this._isMergeDuplicatedNodeById = true;
|
|
129
|
-
this._loopType = types_1.LoopType.ITERATIVE;
|
|
130
|
-
}
|
|
131
124
|
this.clear();
|
|
132
125
|
}
|
|
133
126
|
get root() {
|
|
@@ -139,9 +132,6 @@ class AbstractBinaryTree {
|
|
|
139
132
|
get loopType() {
|
|
140
133
|
return this._loopType;
|
|
141
134
|
}
|
|
142
|
-
get isMergeDuplicatedNodeById() {
|
|
143
|
-
return this._isMergeDuplicatedNodeById;
|
|
144
|
-
}
|
|
145
135
|
get visitedId() {
|
|
146
136
|
return this._visitedId;
|
|
147
137
|
}
|
|
@@ -209,7 +199,9 @@ class AbstractBinaryTree {
|
|
|
209
199
|
while (queue.length > 0) {
|
|
210
200
|
const cur = queue.shift();
|
|
211
201
|
if (cur) {
|
|
212
|
-
|
|
202
|
+
if (newNode && cur.id === newNode.id)
|
|
203
|
+
return;
|
|
204
|
+
const inserted = this._addTo(newNode, cur);
|
|
213
205
|
if (inserted !== undefined)
|
|
214
206
|
return inserted;
|
|
215
207
|
if (cur.left)
|
|
@@ -257,41 +249,6 @@ class AbstractBinaryTree {
|
|
|
257
249
|
}
|
|
258
250
|
return inserted;
|
|
259
251
|
}
|
|
260
|
-
/**
|
|
261
|
-
* The function adds a new node to the left or right child of a parent node, updating the size of the tree if
|
|
262
|
-
* necessary.
|
|
263
|
-
* @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to the tree. It can be
|
|
264
|
-
* either a node object (`N`) or `null`.
|
|
265
|
-
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will be added as a
|
|
266
|
-
* child.
|
|
267
|
-
* @returns either the left child node, the right child node, or undefined.
|
|
268
|
-
*/
|
|
269
|
-
addTo(newNode, parent) {
|
|
270
|
-
if (parent) {
|
|
271
|
-
// When all leaf nodes are null, it will no longer be possible to add new entity nodes to this binary tree.
|
|
272
|
-
// In this scenario, null nodes serve as "sentinel nodes," "virtual nodes," or "placeholder nodes."
|
|
273
|
-
if (parent.left === undefined) {
|
|
274
|
-
parent.left = newNode;
|
|
275
|
-
if (newNode) {
|
|
276
|
-
this._setSize(this.size + 1);
|
|
277
|
-
}
|
|
278
|
-
return parent.left;
|
|
279
|
-
}
|
|
280
|
-
else if (parent.right === undefined) {
|
|
281
|
-
parent.right = newNode;
|
|
282
|
-
if (newNode) {
|
|
283
|
-
this._setSize(this.size + 1);
|
|
284
|
-
}
|
|
285
|
-
return parent.right;
|
|
286
|
-
}
|
|
287
|
-
else {
|
|
288
|
-
return;
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
else {
|
|
292
|
-
return;
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
252
|
/**
|
|
296
253
|
* The `addMany` function adds multiple nodes to a tree data structure and returns an array of the inserted nodes or
|
|
297
254
|
* null/undefined values.
|
|
@@ -306,29 +263,22 @@ class AbstractBinaryTree {
|
|
|
306
263
|
// TODO not sure addMany not be run multi times
|
|
307
264
|
const inserted = [];
|
|
308
265
|
const map = new Map();
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
map.set(idOrNode, ((_a = map.get(idOrNode)) !== null && _a !== void 0 ? _a : 0) + 1);
|
|
312
|
-
}
|
|
266
|
+
for (const idOrNode of idsOrNodes)
|
|
267
|
+
map.set(idOrNode, ((_a = map.get(idOrNode)) !== null && _a !== void 0 ? _a : 0) + 1);
|
|
313
268
|
for (let i = 0; i < idsOrNodes.length; i++) {
|
|
314
269
|
const idOrNode = idsOrNodes[i];
|
|
315
|
-
if (idOrNode
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
if (idOrNode === null) {
|
|
320
|
-
inserted.push(this.add(null));
|
|
321
|
-
continue;
|
|
322
|
-
}
|
|
323
|
-
const val = data === null || data === void 0 ? void 0 : data[i];
|
|
324
|
-
if (this.isMergeDuplicatedNodeById) {
|
|
325
|
-
if (map.has(idOrNode)) {
|
|
326
|
-
inserted.push(this.add(idOrNode, val));
|
|
327
|
-
map.delete(idOrNode);
|
|
270
|
+
if (map.has(idOrNode)) {
|
|
271
|
+
if (idOrNode instanceof AbstractBinaryTreeNode) {
|
|
272
|
+
inserted.push(this.add(idOrNode.id, idOrNode.val));
|
|
273
|
+
continue;
|
|
328
274
|
}
|
|
329
|
-
|
|
330
|
-
|
|
275
|
+
if (idOrNode === null) {
|
|
276
|
+
inserted.push(this.add(null));
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
const val = data === null || data === void 0 ? void 0 : data[i];
|
|
331
280
|
inserted.push(this.add(idOrNode, val));
|
|
281
|
+
map.delete(idOrNode);
|
|
332
282
|
}
|
|
333
283
|
}
|
|
334
284
|
return inserted;
|
|
@@ -1170,6 +1120,42 @@ class AbstractBinaryTree {
|
|
|
1170
1120
|
}
|
|
1171
1121
|
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
1172
1122
|
}
|
|
1123
|
+
/**
|
|
1124
|
+
* The function adds a new node to a binary tree if there is an available position.
|
|
1125
|
+
* @param {N | null} newNode - The `newNode` parameter is of type `N | null`, which means it can either be a node of
|
|
1126
|
+
* type `N` or `null`. It represents the node that you want to add to the binary tree.
|
|
1127
|
+
* @param {N} parent - The parent parameter is of type N, which represents a node in a binary tree.
|
|
1128
|
+
* @returns either the left or right child node of the parent node, depending on which child is available for adding
|
|
1129
|
+
* the new node. If a new node is added, the function also updates the size of the binary tree. If neither the left nor
|
|
1130
|
+
* right child is available, the function returns undefined. If the parent node is null, the function also returns
|
|
1131
|
+
* undefined.
|
|
1132
|
+
*/
|
|
1133
|
+
_addTo(newNode, parent) {
|
|
1134
|
+
if (parent) {
|
|
1135
|
+
// When all leaf nodes are null, it will no longer be possible to add new entity nodes to this binary tree.
|
|
1136
|
+
// In this scenario, null nodes serve as "sentinel nodes," "virtual nodes," or "placeholder nodes."
|
|
1137
|
+
if (parent.left === undefined) {
|
|
1138
|
+
parent.left = newNode;
|
|
1139
|
+
if (newNode) {
|
|
1140
|
+
this._setSize(this.size + 1);
|
|
1141
|
+
}
|
|
1142
|
+
return parent.left;
|
|
1143
|
+
}
|
|
1144
|
+
else if (parent.right === undefined) {
|
|
1145
|
+
parent.right = newNode;
|
|
1146
|
+
if (newNode) {
|
|
1147
|
+
this._setSize(this.size + 1);
|
|
1148
|
+
}
|
|
1149
|
+
return parent.right;
|
|
1150
|
+
}
|
|
1151
|
+
else {
|
|
1152
|
+
return;
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
else {
|
|
1156
|
+
return;
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1173
1159
|
/**
|
|
1174
1160
|
* The function sets the loop type for a protected variable.
|
|
1175
1161
|
* @param {LoopType} value - The value parameter is of type LoopType.
|
|
@@ -1205,14 +1191,6 @@ class AbstractBinaryTree {
|
|
|
1205
1191
|
_setVisitedLeftSum(value) {
|
|
1206
1192
|
this._visitedLeftSum = value;
|
|
1207
1193
|
}
|
|
1208
|
-
/**
|
|
1209
|
-
* The function sets the value of a protected property called "_isMergeDuplicatedNodeById".
|
|
1210
|
-
* @param {boolean} value - The value parameter is a boolean value that determines whether the isMergeDuplicatedNodeById
|
|
1211
|
-
* property should be set to true or false.
|
|
1212
|
-
*/
|
|
1213
|
-
_setIsDuplicatedVal(value) {
|
|
1214
|
-
this._isMergeDuplicatedNodeById = value;
|
|
1215
|
-
}
|
|
1216
1194
|
/**
|
|
1217
1195
|
* The function sets the root property of an object to a given value, and if the value is not null, it also sets the
|
|
1218
1196
|
* parent property of the value to undefined.
|
|
@@ -68,15 +68,15 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
68
68
|
*/
|
|
69
69
|
add(idOrNode: BinaryTreeNodeId | N | null, val?: N['val'], count?: number): N | null | undefined;
|
|
70
70
|
/**
|
|
71
|
-
* The function adds a new node to a binary tree
|
|
72
|
-
*
|
|
73
|
-
*
|
|
71
|
+
* The function adds a new node to a binary tree if there is an available slot on the left or right side of the parent
|
|
72
|
+
* node.
|
|
73
|
+
* @param {N | null} newNode - The `newNode` parameter represents the node that needs to be added to the tree. It can
|
|
74
|
+
* be either a node object (`N`) or `null`.
|
|
74
75
|
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will be added as a
|
|
75
76
|
* child.
|
|
76
|
-
* @returns
|
|
77
|
-
* `undefined` in certain cases.
|
|
77
|
+
* @returns The method returns either the `parent.left`, `parent.right`, or `undefined`.
|
|
78
78
|
*/
|
|
79
|
-
|
|
79
|
+
_addTo(newNode: N | null, parent: N): N | null | undefined;
|
|
80
80
|
/**
|
|
81
81
|
* The `addMany` function adds multiple nodes to a binary tree and returns an array of the inserted nodes.
|
|
82
82
|
* @param {BinaryTreeNodeId[] | N[]} idsOrNodes - An array of BinaryTreeNodeId objects or N objects. These objects
|
|
@@ -37,7 +37,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
37
37
|
* TreeMultiset.
|
|
38
38
|
*/
|
|
39
39
|
constructor(options) {
|
|
40
|
-
super(Object.assign(
|
|
40
|
+
super(Object.assign({}, options));
|
|
41
41
|
this._count = 0;
|
|
42
42
|
}
|
|
43
43
|
get count() {
|
|
@@ -166,15 +166,15 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
166
166
|
return inserted;
|
|
167
167
|
}
|
|
168
168
|
/**
|
|
169
|
-
* The function adds a new node to a binary tree
|
|
170
|
-
*
|
|
171
|
-
*
|
|
169
|
+
* The function adds a new node to a binary tree if there is an available slot on the left or right side of the parent
|
|
170
|
+
* node.
|
|
171
|
+
* @param {N | null} newNode - The `newNode` parameter represents the node that needs to be added to the tree. It can
|
|
172
|
+
* be either a node object (`N`) or `null`.
|
|
172
173
|
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will be added as a
|
|
173
174
|
* child.
|
|
174
|
-
* @returns
|
|
175
|
-
* `undefined` in certain cases.
|
|
175
|
+
* @returns The method returns either the `parent.left`, `parent.right`, or `undefined`.
|
|
176
176
|
*/
|
|
177
|
-
|
|
177
|
+
_addTo(newNode, parent) {
|
|
178
178
|
if (parent) {
|
|
179
179
|
if (parent.left === undefined) {
|
|
180
180
|
parent.left = newNode;
|
|
@@ -213,30 +213,22 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
213
213
|
// TODO not sure addMany not be run multi times
|
|
214
214
|
const inserted = [];
|
|
215
215
|
const map = new Map();
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
map.set(idOrNode, ((_a = map.get(idOrNode)) !== null && _a !== void 0 ? _a : 0) + 1);
|
|
219
|
-
}
|
|
216
|
+
for (const idOrNode of idsOrNodes)
|
|
217
|
+
map.set(idOrNode, ((_a = map.get(idOrNode)) !== null && _a !== void 0 ? _a : 0) + 1);
|
|
220
218
|
for (let i = 0; i < idsOrNodes.length; i++) {
|
|
221
219
|
const idOrNode = idsOrNodes[i];
|
|
222
|
-
if (idOrNode
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
if (idOrNode === null) {
|
|
227
|
-
inserted.push(this.add(NaN, null, 0));
|
|
228
|
-
continue;
|
|
229
|
-
}
|
|
230
|
-
const count = this.isMergeDuplicatedNodeById ? map.get(idOrNode) : 1;
|
|
231
|
-
const val = data === null || data === void 0 ? void 0 : data[i];
|
|
232
|
-
if (this.isMergeDuplicatedNodeById) {
|
|
233
|
-
if (map.has(idOrNode)) {
|
|
234
|
-
inserted.push(this.add(idOrNode, val, count));
|
|
235
|
-
map.delete(idOrNode);
|
|
220
|
+
if (map.has(idOrNode)) {
|
|
221
|
+
if (idOrNode instanceof TreeMultisetNode) {
|
|
222
|
+
inserted.push(this.add(idOrNode.id, idOrNode.val, idOrNode.count));
|
|
223
|
+
continue;
|
|
236
224
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
225
|
+
if (idOrNode === null) {
|
|
226
|
+
inserted.push(this.add(NaN, null, 0));
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
const val = data === null || data === void 0 ? void 0 : data[i], count = map.get(idOrNode);
|
|
230
|
+
inserted.push(this.add(idOrNode, val, count));
|
|
231
|
+
map.delete(idOrNode);
|
|
240
232
|
}
|
|
241
233
|
}
|
|
242
234
|
return inserted;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { DijkstraResult, VertexId } from '../types';
|
|
2
2
|
import { IAbstractGraph } from '../interfaces';
|
|
3
|
-
export declare abstract class AbstractVertex<T =
|
|
3
|
+
export declare abstract class AbstractVertex<T = any> {
|
|
4
4
|
/**
|
|
5
5
|
* The function is a protected constructor that takes an id and an optional value as parameters.
|
|
6
6
|
* @param {VertexId} id - The `id` parameter is of type `VertexId` and represents the identifier of the vertex. It is
|
|
@@ -16,7 +16,7 @@ export declare abstract class AbstractVertex<T = number> {
|
|
|
16
16
|
get val(): T | undefined;
|
|
17
17
|
set val(value: T | undefined);
|
|
18
18
|
}
|
|
19
|
-
export declare abstract class AbstractEdge<T =
|
|
19
|
+
export declare abstract class AbstractEdge<T = any> {
|
|
20
20
|
/**
|
|
21
21
|
* The above function is a protected constructor that initializes the weight, value, and hash code properties of an
|
|
22
22
|
* object.
|
|
@@ -46,7 +46,7 @@ export declare abstract class AbstractEdge<T = number> {
|
|
|
46
46
|
*/
|
|
47
47
|
protected _setHashCode(v: string): void;
|
|
48
48
|
}
|
|
49
|
-
export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E extends AbstractEdge<any>> implements IAbstractGraph<V, E> {
|
|
49
|
+
export declare abstract class AbstractGraph<V extends AbstractVertex<any> = AbstractVertex<any>, E extends AbstractEdge<any> = AbstractEdge<any>> implements IAbstractGraph<V, E> {
|
|
50
50
|
private _vertices;
|
|
51
51
|
get vertices(): Map<VertexId, V>;
|
|
52
52
|
/**
|
|
@@ -520,9 +520,8 @@ class AbstractGraph {
|
|
|
520
520
|
const preMap = new Map(); // predecessor
|
|
521
521
|
const srcVertex = this._getVertex(src);
|
|
522
522
|
const destVertex = dest ? this._getVertex(dest) : null;
|
|
523
|
-
if (!srcVertex)
|
|
523
|
+
if (!srcVertex)
|
|
524
524
|
return null;
|
|
525
|
-
}
|
|
526
525
|
for (const vertex of vertices) {
|
|
527
526
|
const vertexOrId = vertex[1];
|
|
528
527
|
if (vertexOrId instanceof AbstractVertex)
|
|
@@ -532,6 +531,11 @@ class AbstractGraph {
|
|
|
532
531
|
heap.add({ id: 0, val: srcVertex });
|
|
533
532
|
distMap.set(srcVertex, 0);
|
|
534
533
|
preMap.set(srcVertex, null);
|
|
534
|
+
/**
|
|
535
|
+
* The function `getPaths` retrieves all paths from vertices to a specified minimum vertex.
|
|
536
|
+
* @param {V | null} minV - The parameter `minV` is of type `V | null`. It represents the minimum vertex value or
|
|
537
|
+
* null.
|
|
538
|
+
*/
|
|
535
539
|
const getPaths = (minV) => {
|
|
536
540
|
for (const vertex of vertices) {
|
|
537
541
|
const vertexOrId = vertex[1];
|
|
@@ -17,3 +17,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./abstract-graph"), exports);
|
|
18
18
|
__exportStar(require("./directed-graph"), exports);
|
|
19
19
|
__exportStar(require("./undirected-graph"), exports);
|
|
20
|
+
__exportStar(require("./map-graph"), exports);
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { MapGraphCoordinate, VertexId } from '../types';
|
|
2
|
+
import { DirectedEdge, DirectedGraph, DirectedVertex } from './directed-graph';
|
|
3
|
+
export declare class MapVertex<T = any> extends DirectedVertex<T> {
|
|
4
|
+
/**
|
|
5
|
+
* The constructor function initializes an object with an id, latitude, longitude, and an optional value.
|
|
6
|
+
* @param {VertexId} id - The `id` parameter is of type `VertexId` and represents the identifier of the vertex.
|
|
7
|
+
* @param {number} lat - The "lat" parameter represents the latitude of a vertex. Latitude is a geographic coordinate
|
|
8
|
+
* that specifies the north-south position of a point on the Earth's surface. It is measured in degrees, with positive
|
|
9
|
+
* values representing points north of the equator and negative values representing points south of the equator.
|
|
10
|
+
* @param {number} long - The "long" parameter represents the longitude of a location. Longitude is a geographic
|
|
11
|
+
* coordinate that specifies the east-west position of a point on the Earth's surface. It is measured in degrees, with
|
|
12
|
+
* values ranging from -180 to 180.
|
|
13
|
+
* @param {T} [val] - The "val" parameter is an optional value of type T. It is not required to be provided when
|
|
14
|
+
* creating an instance of the class.
|
|
15
|
+
*/
|
|
16
|
+
constructor(id: VertexId, lat: number, long: number, val?: T);
|
|
17
|
+
private _lat;
|
|
18
|
+
get lat(): number;
|
|
19
|
+
set lat(value: number);
|
|
20
|
+
private _long;
|
|
21
|
+
get long(): number;
|
|
22
|
+
set long(value: number);
|
|
23
|
+
}
|
|
24
|
+
export declare class MapEdge<T = any> extends DirectedEdge<T> {
|
|
25
|
+
/**
|
|
26
|
+
* The constructor function initializes a new instance of a class with the given source, destination, weight, and
|
|
27
|
+
* value.
|
|
28
|
+
* @param {VertexId} src - The `src` parameter is the source vertex ID. It represents the starting point of an edge in
|
|
29
|
+
* a graph.
|
|
30
|
+
* @param {VertexId} dest - The `dest` parameter is the identifier of the destination vertex for an edge.
|
|
31
|
+
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
|
|
32
|
+
* @param {T} [val] - The "val" parameter is an optional parameter of type T. It is used to store additional
|
|
33
|
+
* information or data associated with the edge.
|
|
34
|
+
*/
|
|
35
|
+
constructor(src: VertexId, dest: VertexId, weight?: number, val?: T);
|
|
36
|
+
}
|
|
37
|
+
export declare class MapGraph<V extends MapVertex<V['val']> = MapVertex, E extends MapEdge = MapEdge> extends DirectedGraph<V, E> {
|
|
38
|
+
/**
|
|
39
|
+
* The constructor function initializes the origin and bottomRight properties of a MapGraphCoordinate object.
|
|
40
|
+
* @param {MapGraphCoordinate} origin - The `origin` parameter is a `MapGraphCoordinate` object that represents the
|
|
41
|
+
* starting point or reference point of the map graph. It defines the coordinates of the top-left corner of the map
|
|
42
|
+
* graph.
|
|
43
|
+
* @param {MapGraphCoordinate} [bottomRight] - The `bottomRight` parameter is an optional parameter of type
|
|
44
|
+
* `MapGraphCoordinate`. It represents the bottom right coordinate of a map graph. If this parameter is not provided,
|
|
45
|
+
* it will default to `undefined`.
|
|
46
|
+
*/
|
|
47
|
+
constructor(origin: MapGraphCoordinate, bottomRight?: MapGraphCoordinate);
|
|
48
|
+
private _origin;
|
|
49
|
+
get origin(): MapGraphCoordinate;
|
|
50
|
+
set origin(value: MapGraphCoordinate);
|
|
51
|
+
private _bottomRight;
|
|
52
|
+
get bottomRight(): MapGraphCoordinate | undefined;
|
|
53
|
+
set bottomRight(value: MapGraphCoordinate | undefined);
|
|
54
|
+
/**
|
|
55
|
+
* The function creates a new vertex with the given id, value, latitude, and longitude.
|
|
56
|
+
* @param {VertexId} id - The id parameter is the unique identifier for the vertex. It is of type VertexId, which could
|
|
57
|
+
* be a string or a number depending on how you define it in your code.
|
|
58
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the vertex. It
|
|
59
|
+
* is of type `V['val']`, which means it should be of the same type as the `val` property of the vertex class `V`.
|
|
60
|
+
* @param {number} lat - The `lat` parameter represents the latitude of the vertex. It is a number that specifies the
|
|
61
|
+
* position of the vertex on the Earth's surface in the north-south direction.
|
|
62
|
+
* @param {number} long - The `long` parameter represents the longitude coordinate of the vertex.
|
|
63
|
+
* @returns The method is returning a new instance of the `MapVertex` class, casted as type `V`.
|
|
64
|
+
*/
|
|
65
|
+
createVertex(id: VertexId, val?: V['val'], lat?: number, long?: number): V;
|
|
66
|
+
/**
|
|
67
|
+
* The function creates a new instance of a MapEdge with the given source, destination, weight, and value.
|
|
68
|
+
* @param {VertexId} src - The source vertex ID of the edge. It represents the starting point of the edge.
|
|
69
|
+
* @param {VertexId} dest - The `dest` parameter is the identifier of the destination vertex for the edge being
|
|
70
|
+
* created.
|
|
71
|
+
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. It
|
|
72
|
+
* is used to assign a numerical value to the edge, which can be used in algorithms such as shortest path algorithms.
|
|
73
|
+
* If the weight is not provided, it can be set to a default value or left undefined.
|
|
74
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the edge. It can be of any type,
|
|
75
|
+
* depending on the specific implementation of the `MapEdge` class.
|
|
76
|
+
* @returns a new instance of the `MapEdge` class, casted as type `E`.
|
|
77
|
+
*/
|
|
78
|
+
createEdge(src: VertexId, dest: VertexId, weight?: number, val?: E['val']): E;
|
|
79
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MapGraph = exports.MapEdge = exports.MapVertex = void 0;
|
|
4
|
+
const directed_graph_1 = require("./directed-graph");
|
|
5
|
+
class MapVertex extends directed_graph_1.DirectedVertex {
|
|
6
|
+
/**
|
|
7
|
+
* The constructor function initializes an object with an id, latitude, longitude, and an optional value.
|
|
8
|
+
* @param {VertexId} id - The `id` parameter is of type `VertexId` and represents the identifier of the vertex.
|
|
9
|
+
* @param {number} lat - The "lat" parameter represents the latitude of a vertex. Latitude is a geographic coordinate
|
|
10
|
+
* that specifies the north-south position of a point on the Earth's surface. It is measured in degrees, with positive
|
|
11
|
+
* values representing points north of the equator and negative values representing points south of the equator.
|
|
12
|
+
* @param {number} long - The "long" parameter represents the longitude of a location. Longitude is a geographic
|
|
13
|
+
* coordinate that specifies the east-west position of a point on the Earth's surface. It is measured in degrees, with
|
|
14
|
+
* values ranging from -180 to 180.
|
|
15
|
+
* @param {T} [val] - The "val" parameter is an optional value of type T. It is not required to be provided when
|
|
16
|
+
* creating an instance of the class.
|
|
17
|
+
*/
|
|
18
|
+
constructor(id, lat, long, val) {
|
|
19
|
+
super(id, val);
|
|
20
|
+
this._lat = lat;
|
|
21
|
+
this._long = long;
|
|
22
|
+
}
|
|
23
|
+
get lat() {
|
|
24
|
+
return this._lat;
|
|
25
|
+
}
|
|
26
|
+
set lat(value) {
|
|
27
|
+
this._lat = value;
|
|
28
|
+
}
|
|
29
|
+
get long() {
|
|
30
|
+
return this._long;
|
|
31
|
+
}
|
|
32
|
+
set long(value) {
|
|
33
|
+
this._long = value;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.MapVertex = MapVertex;
|
|
37
|
+
class MapEdge extends directed_graph_1.DirectedEdge {
|
|
38
|
+
/**
|
|
39
|
+
* The constructor function initializes a new instance of a class with the given source, destination, weight, and
|
|
40
|
+
* value.
|
|
41
|
+
* @param {VertexId} src - The `src` parameter is the source vertex ID. It represents the starting point of an edge in
|
|
42
|
+
* a graph.
|
|
43
|
+
* @param {VertexId} dest - The `dest` parameter is the identifier of the destination vertex for an edge.
|
|
44
|
+
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
|
|
45
|
+
* @param {T} [val] - The "val" parameter is an optional parameter of type T. It is used to store additional
|
|
46
|
+
* information or data associated with the edge.
|
|
47
|
+
*/
|
|
48
|
+
constructor(src, dest, weight, val) {
|
|
49
|
+
super(src, dest, weight, val);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.MapEdge = MapEdge;
|
|
53
|
+
class MapGraph extends directed_graph_1.DirectedGraph {
|
|
54
|
+
/**
|
|
55
|
+
* The constructor function initializes the origin and bottomRight properties of a MapGraphCoordinate object.
|
|
56
|
+
* @param {MapGraphCoordinate} origin - The `origin` parameter is a `MapGraphCoordinate` object that represents the
|
|
57
|
+
* starting point or reference point of the map graph. It defines the coordinates of the top-left corner of the map
|
|
58
|
+
* graph.
|
|
59
|
+
* @param {MapGraphCoordinate} [bottomRight] - The `bottomRight` parameter is an optional parameter of type
|
|
60
|
+
* `MapGraphCoordinate`. It represents the bottom right coordinate of a map graph. If this parameter is not provided,
|
|
61
|
+
* it will default to `undefined`.
|
|
62
|
+
*/
|
|
63
|
+
constructor(origin, bottomRight) {
|
|
64
|
+
super();
|
|
65
|
+
this._origin = [0, 0];
|
|
66
|
+
this._origin = origin;
|
|
67
|
+
this._bottomRight = bottomRight;
|
|
68
|
+
}
|
|
69
|
+
get origin() {
|
|
70
|
+
return this._origin;
|
|
71
|
+
}
|
|
72
|
+
set origin(value) {
|
|
73
|
+
this._origin = value;
|
|
74
|
+
}
|
|
75
|
+
get bottomRight() {
|
|
76
|
+
return this._bottomRight;
|
|
77
|
+
}
|
|
78
|
+
set bottomRight(value) {
|
|
79
|
+
this._bottomRight = value;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* The function creates a new vertex with the given id, value, latitude, and longitude.
|
|
83
|
+
* @param {VertexId} id - The id parameter is the unique identifier for the vertex. It is of type VertexId, which could
|
|
84
|
+
* be a string or a number depending on how you define it in your code.
|
|
85
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the vertex. It
|
|
86
|
+
* is of type `V['val']`, which means it should be of the same type as the `val` property of the vertex class `V`.
|
|
87
|
+
* @param {number} lat - The `lat` parameter represents the latitude of the vertex. It is a number that specifies the
|
|
88
|
+
* position of the vertex on the Earth's surface in the north-south direction.
|
|
89
|
+
* @param {number} long - The `long` parameter represents the longitude coordinate of the vertex.
|
|
90
|
+
* @returns The method is returning a new instance of the `MapVertex` class, casted as type `V`.
|
|
91
|
+
*/
|
|
92
|
+
createVertex(id, val, lat = this.origin[0], long = this.origin[1]) {
|
|
93
|
+
return new MapVertex(id, lat, long, val);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* The function creates a new instance of a MapEdge with the given source, destination, weight, and value.
|
|
97
|
+
* @param {VertexId} src - The source vertex ID of the edge. It represents the starting point of the edge.
|
|
98
|
+
* @param {VertexId} dest - The `dest` parameter is the identifier of the destination vertex for the edge being
|
|
99
|
+
* created.
|
|
100
|
+
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. It
|
|
101
|
+
* is used to assign a numerical value to the edge, which can be used in algorithms such as shortest path algorithms.
|
|
102
|
+
* If the weight is not provided, it can be set to a default value or left undefined.
|
|
103
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the edge. It can be of any type,
|
|
104
|
+
* depending on the specific implementation of the `MapEdge` class.
|
|
105
|
+
* @returns a new instance of the `MapEdge` class, casted as type `E`.
|
|
106
|
+
*/
|
|
107
|
+
createEdge(src, dest, weight, val) {
|
|
108
|
+
return new MapEdge(src, dest, weight, val);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
exports.MapGraph = MapGraph;
|
|
@@ -22,14 +22,12 @@ export interface IAbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val'],
|
|
|
22
22
|
get visitedVal(): Array<N['val']>;
|
|
23
23
|
get visitedNode(): N[];
|
|
24
24
|
get visitedLeftSum(): number[];
|
|
25
|
-
get isMergeDuplicatedNodeById(): boolean;
|
|
26
25
|
get root(): N | null;
|
|
27
26
|
get size(): number;
|
|
28
27
|
swapLocation(srcNode: N, destNode: N): N;
|
|
29
28
|
clear(): void;
|
|
30
29
|
isEmpty(): boolean;
|
|
31
30
|
add(id: BinaryTreeNodeId | N, val?: N['val']): N | null | undefined;
|
|
32
|
-
addTo(newNode: N | null, parent: N): N | null | undefined;
|
|
33
31
|
addMany(idsOrNodes: (BinaryTreeNodeId | N | null)[], data?: N['val'][]): (N | null | undefined)[];
|
|
34
32
|
fill(idsOrNodes: (BinaryTreeNodeId | N | null)[], data?: N[] | Array<N['val']>): boolean;
|
|
35
33
|
remove(id: BinaryTreeNodeId, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
|