directed-graph-typed 1.39.5 → 1.39.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree.d.ts +6 -6
- package/dist/data-structures/binary-tree/avl-tree.js +13 -13
- package/dist/data-structures/binary-tree/binary-tree.d.ts +7 -7
- package/dist/data-structures/binary-tree/binary-tree.js +17 -17
- package/dist/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/data-structures/binary-tree/bst.js +13 -13
- package/dist/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/dist/data-structures/binary-tree/rb-tree.js +4 -4
- package/dist/data-structures/binary-tree/segment-tree.d.ts +7 -7
- package/dist/data-structures/binary-tree/segment-tree.js +16 -16
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +6 -6
- package/dist/data-structures/binary-tree/tree-multiset.js +18 -18
- package/dist/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/data-structures/graph/abstract-graph.js +24 -24
- package/dist/data-structures/graph/directed-graph.d.ts +12 -12
- package/dist/data-structures/graph/directed-graph.js +15 -15
- package/dist/data-structures/graph/map-graph.d.ts +9 -9
- package/dist/data-structures/graph/map-graph.js +13 -13
- package/dist/data-structures/graph/undirected-graph.d.ts +11 -11
- package/dist/data-structures/graph/undirected-graph.js +14 -14
- package/dist/data-structures/hash/hash-table.d.ts +4 -4
- package/dist/data-structures/hash/hash-table.js +8 -8
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +31 -31
- package/dist/data-structures/linked-list/doubly-linked-list.js +54 -54
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +24 -24
- package/dist/data-structures/linked-list/singly-linked-list.js +52 -52
- package/dist/data-structures/queue/queue.js +1 -1
- package/dist/interfaces/binary-tree.d.ts +2 -2
- package/dist/interfaces/graph.d.ts +2 -2
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +13 -13
- package/src/data-structures/binary-tree/binary-tree.ts +18 -18
- package/src/data-structures/binary-tree/bst.ts +16 -16
- package/src/data-structures/binary-tree/rb-tree.ts +6 -6
- package/src/data-structures/binary-tree/segment-tree.ts +15 -15
- package/src/data-structures/binary-tree/tree-multiset.ts +18 -18
- package/src/data-structures/graph/abstract-graph.ts +34 -34
- package/src/data-structures/graph/directed-graph.ts +16 -16
- package/src/data-structures/graph/map-graph.ts +13 -13
- package/src/data-structures/graph/undirected-graph.ts +15 -15
- package/src/data-structures/hash/hash-table.ts +9 -9
- package/src/data-structures/linked-list/doubly-linked-list.ts +61 -61
- package/src/data-structures/linked-list/singly-linked-list.ts +58 -58
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/interfaces/graph.ts +2 -2
|
@@ -3,8 +3,8 @@ import {IBinaryTree} from '../../interfaces';
|
|
|
3
3
|
import {BST, BSTNode} from './bst';
|
|
4
4
|
|
|
5
5
|
export class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
6
|
-
constructor(key: BTNKey,
|
|
7
|
-
super(key,
|
|
6
|
+
constructor(key: BTNKey, value?: V) {
|
|
7
|
+
super(key, value);
|
|
8
8
|
this._color = RBColor.RED;
|
|
9
9
|
}
|
|
10
10
|
|
|
@@ -27,12 +27,12 @@ export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNest
|
|
|
27
27
|
super(options);
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
override createNode(key: BTNKey,
|
|
31
|
-
return new RBTreeNode(key,
|
|
30
|
+
override createNode(key: BTNKey, value?: V): N {
|
|
31
|
+
return new RBTreeNode(key, value) as N;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
// override add(keyOrNode: BTNKey | N | null,
|
|
35
|
-
// const inserted = super.add(keyOrNode,
|
|
34
|
+
// override add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined {
|
|
35
|
+
// const inserted = super.add(keyOrNode, value);
|
|
36
36
|
// if (inserted) this._fixInsertViolation(inserted);
|
|
37
37
|
// return inserted;
|
|
38
38
|
// }
|
|
@@ -9,11 +9,11 @@
|
|
|
9
9
|
import type {SegmentTreeNodeVal} from '../../types';
|
|
10
10
|
|
|
11
11
|
export class SegmentTreeNode {
|
|
12
|
-
constructor(start: number, end: number, sum: number,
|
|
12
|
+
constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | null) {
|
|
13
13
|
this._start = start;
|
|
14
14
|
this._end = end;
|
|
15
15
|
this._sum = sum;
|
|
16
|
-
this.
|
|
16
|
+
this._value = value || null;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
private _start = 0;
|
|
@@ -35,14 +35,14 @@ export class SegmentTreeNode {
|
|
|
35
35
|
this._end = v;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
private
|
|
38
|
+
private _value: SegmentTreeNodeVal | null = null;
|
|
39
39
|
|
|
40
|
-
get
|
|
41
|
-
return this.
|
|
40
|
+
get value(): SegmentTreeNodeVal | null {
|
|
41
|
+
return this._value;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
set
|
|
45
|
-
this.
|
|
44
|
+
set value(v: SegmentTreeNodeVal | null) {
|
|
45
|
+
this._value = v;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
private _sum = 0;
|
|
@@ -154,30 +154,30 @@ export class SegmentTree {
|
|
|
154
154
|
* updated.
|
|
155
155
|
* @param {number} sum - The `sum` parameter represents the new value that should be assigned to the `sum` property of
|
|
156
156
|
* the `SegmentTreeNode` at the specified `index`.
|
|
157
|
-
* @param {SegmentTreeNodeVal} [
|
|
157
|
+
* @param {SegmentTreeNodeVal} [value] - The `value` parameter is an optional value that can be assigned to the `value`
|
|
158
158
|
* property of the `SegmentTreeNode` object. It is not currently used in the code, but you can uncomment the line `//
|
|
159
|
-
* cur.
|
|
159
|
+
* cur.value = value;` and pass a value for `value` in the
|
|
160
160
|
* @returns The function does not return anything.
|
|
161
161
|
*/
|
|
162
|
-
updateNode(index: number, sum: number,
|
|
162
|
+
updateNode(index: number, sum: number, value?: SegmentTreeNodeVal) {
|
|
163
163
|
const root = this.root || null;
|
|
164
164
|
if (!root) {
|
|
165
165
|
return;
|
|
166
166
|
}
|
|
167
|
-
const dfs = (cur: SegmentTreeNode, index: number, sum: number,
|
|
167
|
+
const dfs = (cur: SegmentTreeNode, index: number, sum: number, value?: SegmentTreeNodeVal) => {
|
|
168
168
|
if (cur.start === cur.end && cur.start === index) {
|
|
169
169
|
cur.sum = sum;
|
|
170
|
-
if (
|
|
170
|
+
if (value !== undefined) cur.value = value;
|
|
171
171
|
return;
|
|
172
172
|
}
|
|
173
173
|
const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
|
|
174
174
|
if (index <= mid) {
|
|
175
175
|
if (cur.left) {
|
|
176
|
-
dfs(cur.left, index, sum,
|
|
176
|
+
dfs(cur.left, index, sum, value);
|
|
177
177
|
}
|
|
178
178
|
} else {
|
|
179
179
|
if (cur.right) {
|
|
180
|
-
dfs(cur.right, index, sum,
|
|
180
|
+
dfs(cur.right, index, sum, value);
|
|
181
181
|
}
|
|
182
182
|
}
|
|
183
183
|
if (cur.left && cur.right) {
|
|
@@ -185,7 +185,7 @@ export class SegmentTree {
|
|
|
185
185
|
}
|
|
186
186
|
};
|
|
187
187
|
|
|
188
|
-
dfs(root, index, sum,
|
|
188
|
+
dfs(root, index, sum, value);
|
|
189
189
|
}
|
|
190
190
|
|
|
191
191
|
/**
|
|
@@ -20,14 +20,14 @@ export class TreeMultisetNode<
|
|
|
20
20
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
21
21
|
* @param {BTNKey} key - The `key` parameter is of type `BTNKey` and represents the unique identifier
|
|
22
22
|
* of the binary tree node.
|
|
23
|
-
* @param {V} [
|
|
23
|
+
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the value of the binary
|
|
24
24
|
* tree node. If no value is provided, it will be `undefined`.
|
|
25
25
|
* @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value
|
|
26
26
|
* occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
|
|
27
27
|
* parameter when creating a new instance of the `BinaryTreeNode` class.
|
|
28
28
|
*/
|
|
29
|
-
constructor(key: BTNKey,
|
|
30
|
-
super(key,
|
|
29
|
+
constructor(key: BTNKey, value?: V, count = 1) {
|
|
30
|
+
super(key, value);
|
|
31
31
|
this.count = count;
|
|
32
32
|
}
|
|
33
33
|
}
|
|
@@ -59,13 +59,13 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
59
59
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
60
60
|
* @param {BTNKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
61
61
|
* distinguish one node from another in the tree.
|
|
62
|
-
* @param {N}
|
|
62
|
+
* @param {N} value - The `value` parameter represents the value that will be stored in the binary search tree node.
|
|
63
63
|
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
64
64
|
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
65
65
|
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
66
66
|
*/
|
|
67
|
-
override createNode(key: BTNKey,
|
|
68
|
-
return new TreeMultisetNode(key,
|
|
67
|
+
override createNode(key: BTNKey, value?: V, count?: number): N {
|
|
68
|
+
return new TreeMultisetNode(key, value, count) as N;
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
/**
|
|
@@ -74,22 +74,22 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
74
74
|
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
75
75
|
* `BTNKey` (which represents the key of the node to be added), a `N` (which represents a
|
|
76
76
|
* node to be added), or `null` (which represents a null node).
|
|
77
|
-
* @param [
|
|
77
|
+
* @param [value] - The `value` parameter represents the value associated with the key that is being
|
|
78
78
|
* added to the binary tree.
|
|
79
79
|
* @param [count=1] - The `count` parameter represents the number of occurrences of the key/value
|
|
80
80
|
* pair that will be added to the binary tree. It has a default value of 1, which means that if no
|
|
81
81
|
* count is specified, the default count will be 1.
|
|
82
82
|
* @returns The function `add` returns a value of type `N | null | undefined`.
|
|
83
83
|
*/
|
|
84
|
-
override add(keyOrNode: BTNKey | N | null,
|
|
84
|
+
override add(keyOrNode: BTNKey | N | null, value?: V, count = 1): N | null | undefined {
|
|
85
85
|
let inserted: N | null | undefined = undefined,
|
|
86
86
|
newNode: N | null;
|
|
87
87
|
if (keyOrNode instanceof TreeMultisetNode) {
|
|
88
|
-
newNode = this.createNode(keyOrNode.key, keyOrNode.
|
|
88
|
+
newNode = this.createNode(keyOrNode.key, keyOrNode.value, keyOrNode.count);
|
|
89
89
|
} else if (keyOrNode === null) {
|
|
90
90
|
newNode = null;
|
|
91
91
|
} else {
|
|
92
|
-
newNode = this.createNode(keyOrNode,
|
|
92
|
+
newNode = this.createNode(keyOrNode, value, count);
|
|
93
93
|
}
|
|
94
94
|
if (!this.root) {
|
|
95
95
|
this._setRoot(newNode);
|
|
@@ -103,7 +103,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
103
103
|
if (cur) {
|
|
104
104
|
if (newNode) {
|
|
105
105
|
if (this._compare(cur.key, newNode.key) === CP.eq) {
|
|
106
|
-
cur.
|
|
106
|
+
cur.value = newNode.value;
|
|
107
107
|
cur.count += newNode.count;
|
|
108
108
|
this._setCount(this.count + newNode.count);
|
|
109
109
|
traversing = false;
|
|
@@ -199,7 +199,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
199
199
|
const keyOrNode = keysOrNodes[i];
|
|
200
200
|
|
|
201
201
|
if (keyOrNode instanceof TreeMultisetNode) {
|
|
202
|
-
inserted.push(this.add(keyOrNode.key, keyOrNode.
|
|
202
|
+
inserted.push(this.add(keyOrNode.key, keyOrNode.value, keyOrNode.count));
|
|
203
203
|
continue;
|
|
204
204
|
}
|
|
205
205
|
|
|
@@ -233,7 +233,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
233
233
|
if (l > r) return;
|
|
234
234
|
const m = l + Math.floor((r - l) / 2);
|
|
235
235
|
const midNode = sorted[m];
|
|
236
|
-
this.add(midNode.key, midNode.
|
|
236
|
+
this.add(midNode.key, midNode.value, midNode.count);
|
|
237
237
|
buildBalanceBST(l, m - 1);
|
|
238
238
|
buildBalanceBST(m + 1, r);
|
|
239
239
|
};
|
|
@@ -249,7 +249,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
249
249
|
if (l <= r) {
|
|
250
250
|
const m = l + Math.floor((r - l) / 2);
|
|
251
251
|
const midNode = sorted[m];
|
|
252
|
-
this.add(midNode.key, midNode.
|
|
252
|
+
this.add(midNode.key, midNode.value, midNode.count);
|
|
253
253
|
stack.push([m + 1, r]);
|
|
254
254
|
stack.push([l, m - 1]);
|
|
255
255
|
}
|
|
@@ -351,18 +351,18 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
351
351
|
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
352
352
|
*/
|
|
353
353
|
protected override _swap(srcNode: N, destNode: N): N {
|
|
354
|
-
const {key,
|
|
355
|
-
const tempNode = this.createNode(key,
|
|
354
|
+
const {key, value, count, height} = destNode;
|
|
355
|
+
const tempNode = this.createNode(key, value, count);
|
|
356
356
|
if (tempNode) {
|
|
357
357
|
tempNode.height = height;
|
|
358
358
|
|
|
359
359
|
destNode.key = srcNode.key;
|
|
360
|
-
destNode.
|
|
360
|
+
destNode.value = srcNode.value;
|
|
361
361
|
destNode.count = srcNode.count;
|
|
362
362
|
destNode.height = srcNode.height;
|
|
363
363
|
|
|
364
364
|
srcNode.key = tempNode.key;
|
|
365
|
-
srcNode.
|
|
365
|
+
srcNode.value = tempNode.value;
|
|
366
366
|
srcNode.count = tempNode.count;
|
|
367
367
|
srcNode.height = tempNode.height;
|
|
368
368
|
}
|
|
@@ -16,12 +16,12 @@ export abstract class AbstractVertex<V = any> {
|
|
|
16
16
|
* The function is a protected constructor that takes an key and an optional value as parameters.
|
|
17
17
|
* @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
|
|
18
18
|
* used to uniquely identify the vertex object.
|
|
19
|
-
* @param {V} [
|
|
19
|
+
* @param {V} [value] - The parameter "value" is an optional parameter of type V. It is used to assign a value to the
|
|
20
20
|
* vertex. If no value is provided, it will be set to undefined.
|
|
21
21
|
*/
|
|
22
|
-
protected constructor(key: VertexKey,
|
|
22
|
+
protected constructor(key: VertexKey, value?: V) {
|
|
23
23
|
this._key = key;
|
|
24
|
-
this.
|
|
24
|
+
this._value = value;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
private _key: VertexKey;
|
|
@@ -34,41 +34,41 @@ export abstract class AbstractVertex<V = any> {
|
|
|
34
34
|
this._key = v;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
private
|
|
37
|
+
private _value: V | undefined;
|
|
38
38
|
|
|
39
|
-
get
|
|
40
|
-
return this.
|
|
39
|
+
get value(): V | undefined {
|
|
40
|
+
return this._value;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
set
|
|
44
|
-
this.
|
|
43
|
+
set value(value: V | undefined) {
|
|
44
|
+
this._value = value;
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
export abstract class AbstractEdge<
|
|
48
|
+
export abstract class AbstractEdge<E = any> {
|
|
49
49
|
/**
|
|
50
50
|
* The above function is a protected constructor that initializes the weight, value, and hash code properties of an
|
|
51
51
|
* object.
|
|
52
52
|
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the object. If
|
|
53
53
|
* a value is provided, it will be assigned to the `_weight` property. If no value is provided, the default value of 1
|
|
54
54
|
* will be assigned.
|
|
55
|
-
* @param {VO} [
|
|
55
|
+
* @param {VO} [value] - The `value` parameter is of type `VO`, which means it can be any type. It is an optional parameter,
|
|
56
56
|
* meaning it can be omitted when creating an instance of the class.
|
|
57
57
|
*/
|
|
58
|
-
protected constructor(weight?: number,
|
|
58
|
+
protected constructor(weight?: number, value?: E) {
|
|
59
59
|
this._weight = weight !== undefined ? weight : 1;
|
|
60
|
-
this.
|
|
60
|
+
this._value = value;
|
|
61
61
|
this._hashCode = uuidV4();
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
private
|
|
64
|
+
private _value: E | undefined;
|
|
65
65
|
|
|
66
|
-
get
|
|
67
|
-
return this.
|
|
66
|
+
get value(): E | undefined {
|
|
67
|
+
return this._value;
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
set
|
|
71
|
-
this.
|
|
70
|
+
set value(value: E | undefined) {
|
|
71
|
+
this._value = value;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
private _weight: number;
|
|
@@ -119,9 +119,9 @@ export abstract class AbstractGraph<
|
|
|
119
119
|
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
120
120
|
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
|
121
121
|
* @param key
|
|
122
|
-
* @param
|
|
122
|
+
* @param value
|
|
123
123
|
*/
|
|
124
|
-
abstract createVertex(key: VertexKey,
|
|
124
|
+
abstract createVertex(key: VertexKey, value?: V): VO;
|
|
125
125
|
|
|
126
126
|
/**
|
|
127
127
|
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
@@ -129,9 +129,9 @@ export abstract class AbstractGraph<
|
|
|
129
129
|
* @param srcOrV1
|
|
130
130
|
* @param destOrV2
|
|
131
131
|
* @param weight
|
|
132
|
-
* @param
|
|
132
|
+
* @param value
|
|
133
133
|
*/
|
|
134
|
-
abstract createEdge(srcOrV1: VertexKey
|
|
134
|
+
abstract createEdge(srcOrV1: VertexKey, destOrV2: VertexKey, weight?: number, value?: E): EO;
|
|
135
135
|
|
|
136
136
|
abstract deleteEdge(edge: EO): EO | null;
|
|
137
137
|
|
|
@@ -170,13 +170,13 @@ export abstract class AbstractGraph<
|
|
|
170
170
|
|
|
171
171
|
addVertex(vertex: VO): boolean;
|
|
172
172
|
|
|
173
|
-
addVertex(key: VertexKey,
|
|
173
|
+
addVertex(key: VertexKey, value?: V): boolean;
|
|
174
174
|
|
|
175
|
-
addVertex(keyOrVertex: VertexKey | VO,
|
|
175
|
+
addVertex(keyOrVertex: VertexKey | VO, value?: V): boolean {
|
|
176
176
|
if (keyOrVertex instanceof AbstractVertex) {
|
|
177
177
|
return this._addVertexOnly(keyOrVertex);
|
|
178
178
|
} else {
|
|
179
|
-
const newVertex = this.createVertex(keyOrVertex,
|
|
179
|
+
const newVertex = this.createVertex(keyOrVertex, value);
|
|
180
180
|
return this._addVertexOnly(newVertex);
|
|
181
181
|
}
|
|
182
182
|
}
|
|
@@ -222,9 +222,9 @@ export abstract class AbstractGraph<
|
|
|
222
222
|
|
|
223
223
|
addEdge(edge: EO): boolean;
|
|
224
224
|
|
|
225
|
-
addEdge(src: VO | VertexKey, dest: VO | VertexKey, weight?: number,
|
|
225
|
+
addEdge(src: VO | VertexKey, dest: VO | VertexKey, weight?: number, value?: E): boolean;
|
|
226
226
|
|
|
227
|
-
addEdge(srcOrEdge: VO | VertexKey | EO, dest?: VO | VertexKey, weight?: number,
|
|
227
|
+
addEdge(srcOrEdge: VO | VertexKey | EO, dest?: VO | VertexKey, weight?: number, value?: E): boolean {
|
|
228
228
|
if (srcOrEdge instanceof AbstractEdge) {
|
|
229
229
|
return this._addEdgeOnly(srcOrEdge);
|
|
230
230
|
} else {
|
|
@@ -232,7 +232,7 @@ export abstract class AbstractGraph<
|
|
|
232
232
|
if (!(this.hasVertex(srcOrEdge) && this.hasVertex(dest))) return false;
|
|
233
233
|
if (srcOrEdge instanceof AbstractVertex) srcOrEdge = srcOrEdge.key;
|
|
234
234
|
if (dest instanceof AbstractVertex) dest = dest.key;
|
|
235
|
-
const newEdge = this.createEdge(srcOrEdge, dest, weight,
|
|
235
|
+
const newEdge = this.createEdge(srcOrEdge, dest, weight, value);
|
|
236
236
|
return this._addEdgeOnly(newEdge);
|
|
237
237
|
} else {
|
|
238
238
|
throw new Error('dest must be a Vertex or vertex key while srcOrEdge is an Edge');
|
|
@@ -493,10 +493,10 @@ export abstract class AbstractGraph<
|
|
|
493
493
|
const getMinOfNoSeen = () => {
|
|
494
494
|
let min = Infinity;
|
|
495
495
|
let minV: VO | null = null;
|
|
496
|
-
for (const [key,
|
|
496
|
+
for (const [key, value] of distMap) {
|
|
497
497
|
if (!seen.has(key)) {
|
|
498
|
-
if (
|
|
499
|
-
min =
|
|
498
|
+
if (value < min) {
|
|
499
|
+
min = value;
|
|
500
500
|
minV = key;
|
|
501
501
|
}
|
|
502
502
|
}
|
|
@@ -625,8 +625,8 @@ export abstract class AbstractGraph<
|
|
|
625
625
|
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
|
626
626
|
}
|
|
627
627
|
|
|
628
|
-
const heap = new PriorityQueue<{key: number;
|
|
629
|
-
heap.add({key: 0,
|
|
628
|
+
const heap = new PriorityQueue<{key: number; value: VO}>({comparator: (a, b) => a.key - b.key});
|
|
629
|
+
heap.add({key: 0, value: srcVertex});
|
|
630
630
|
|
|
631
631
|
distMap.set(srcVertex, 0);
|
|
632
632
|
preMap.set(srcVertex, null);
|
|
@@ -656,7 +656,7 @@ export abstract class AbstractGraph<
|
|
|
656
656
|
while (heap.size > 0) {
|
|
657
657
|
const curHeapNode = heap.poll();
|
|
658
658
|
const dist = curHeapNode?.key;
|
|
659
|
-
const cur = curHeapNode?.
|
|
659
|
+
const cur = curHeapNode?.value;
|
|
660
660
|
if (dist !== undefined) {
|
|
661
661
|
if (cur) {
|
|
662
662
|
seen.add(cur);
|
|
@@ -677,7 +677,7 @@ export abstract class AbstractGraph<
|
|
|
677
677
|
const distSrcToNeighbor = distMap.get(neighbor);
|
|
678
678
|
if (distSrcToNeighbor) {
|
|
679
679
|
if (dist + weight < distSrcToNeighbor) {
|
|
680
|
-
heap.add({key: dist + weight,
|
|
680
|
+
heap.add({key: dist + weight, value: neighbor});
|
|
681
681
|
preMap.set(neighbor, cur);
|
|
682
682
|
distMap.set(neighbor, dist + weight);
|
|
683
683
|
}
|
|
@@ -15,11 +15,11 @@ export class DirectedVertex<V = any> extends AbstractVertex<V> {
|
|
|
15
15
|
* The constructor function initializes a vertex with an optional value.
|
|
16
16
|
* @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
|
|
17
17
|
* used to uniquely identify the vertex within a graph or data structure.
|
|
18
|
-
* @param {V} [
|
|
18
|
+
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It is used to initialize the value of the
|
|
19
19
|
* vertex. If no value is provided, the vertex will be initialized with a default value.
|
|
20
20
|
*/
|
|
21
|
-
constructor(key: VertexKey,
|
|
22
|
-
super(key,
|
|
21
|
+
constructor(key: VertexKey, value?: V) {
|
|
22
|
+
super(key, value);
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
|
|
@@ -32,11 +32,11 @@ export class DirectedEdge<E = any> extends AbstractEdge<E> {
|
|
|
32
32
|
* @param {VertexKey} dest - The `dest` parameter represents the destination vertex of an edge. It is of type
|
|
33
33
|
* `VertexKey`, which is likely a unique identifier for a vertex in a graph.
|
|
34
34
|
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
|
|
35
|
-
* @param {E} [
|
|
35
|
+
* @param {E} [value] - The `value` parameter is an optional parameter of type `E`. It represents the value associated with
|
|
36
36
|
* the edge.
|
|
37
37
|
*/
|
|
38
|
-
constructor(src: VertexKey, dest: VertexKey, weight?: number,
|
|
39
|
-
super(weight,
|
|
38
|
+
constructor(src: VertexKey, dest: VertexKey, weight?: number, value?: E) {
|
|
39
|
+
super(weight, value);
|
|
40
40
|
this._src = src;
|
|
41
41
|
this._dest = dest;
|
|
42
42
|
}
|
|
@@ -99,13 +99,13 @@ export class DirectedGraph<
|
|
|
99
99
|
* The function creates a new vertex with an optional value and returns it.
|
|
100
100
|
* @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is of type `VertexKey`, which
|
|
101
101
|
* could be a number or a string depending on how you want to identify your vertices.
|
|
102
|
-
* @param [
|
|
103
|
-
* it will be assigned to the '
|
|
102
|
+
* @param [value] - The 'value' parameter is an optional value that can be assigned to the vertex. If a value is provided,
|
|
103
|
+
* it will be assigned to the 'value' property of the vertex. If no value is provided, the 'value' property will be
|
|
104
104
|
* assigned the same value as the 'key' parameter
|
|
105
105
|
* @returns a new instance of a DirectedVertex object, casted as type VO.
|
|
106
106
|
*/
|
|
107
|
-
createVertex(key: VertexKey,
|
|
108
|
-
return new DirectedVertex(key,
|
|
107
|
+
createVertex(key: VertexKey, value?: V): VO {
|
|
108
|
+
return new DirectedVertex(key, value ?? key) as VO;
|
|
109
109
|
}
|
|
110
110
|
|
|
111
111
|
/**
|
|
@@ -119,23 +119,23 @@ export class DirectedGraph<
|
|
|
119
119
|
* @param {VertexKey} dest - The `dest` parameter is the identifier of the destination vertex for the edge.
|
|
120
120
|
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge. If no
|
|
121
121
|
* weight is provided, it defaults to 1.
|
|
122
|
-
* @param [
|
|
122
|
+
* @param [value] - The 'value' parameter is an optional value that can be assigned to the edge. It can be of any type and
|
|
123
123
|
* is used to store additional information or data associated with the edge.
|
|
124
124
|
* @returns a new instance of a DirectedEdge object, casted as type EO.
|
|
125
125
|
*/
|
|
126
|
-
createEdge(src: VertexKey, dest: VertexKey, weight?: number,
|
|
127
|
-
return new DirectedEdge(src, dest, weight ?? 1,
|
|
126
|
+
createEdge(src: VertexKey, dest: VertexKey, weight?: number, value?: E): EO {
|
|
127
|
+
return new DirectedEdge(src, dest, weight ?? 1, value) as EO;
|
|
128
128
|
}
|
|
129
129
|
|
|
130
130
|
/**
|
|
131
131
|
* The `getEdge` function retrieves an edge between two vertices based on their source and destination IDs.
|
|
132
|
-
* @param {VO |
|
|
133
|
-
* @param {VO |
|
|
132
|
+
* @param {VO | VertexKey | null} srcOrKey - The source vertex or its ID. It can be either a vertex object or a vertex ID.
|
|
133
|
+
* @param {VO | VertexKey | null} destOrKey - The `destOrKey` parameter in the `getEdge` function represents the
|
|
134
134
|
* destination vertex of the edge. It can be either a vertex object (`VO`), a vertex ID (`VertexKey`), or `null` if the
|
|
135
135
|
* destination is not specified.
|
|
136
136
|
* @returns the first edge found between the source and destination vertices, or null if no such edge is found.
|
|
137
137
|
*/
|
|
138
|
-
getEdge(srcOrKey: VO |
|
|
138
|
+
getEdge(srcOrKey: VO | VertexKey | null, destOrKey: VO | VertexKey | null): EO | null {
|
|
139
139
|
let edges: EO[] = [];
|
|
140
140
|
|
|
141
141
|
if (srcOrKey !== null && destOrKey !== null) {
|
|
@@ -11,11 +11,11 @@ export class MapVertex<V = any> extends DirectedVertex<V> {
|
|
|
11
11
|
* @param {number} long - The "long" parameter represents the longitude of a location. Longitude is a geographic
|
|
12
12
|
* coordinate that specifies the east-west position of a point on the Earth's surface. It is measured in degrees, with
|
|
13
13
|
* values ranging from -180 to 180.
|
|
14
|
-
* @param {V} [
|
|
14
|
+
* @param {V} [value] - The "value" parameter is an optional value of type V. It is not required to be provided when
|
|
15
15
|
* creating an instance of the class.
|
|
16
16
|
*/
|
|
17
|
-
constructor(key: VertexKey,
|
|
18
|
-
super(key,
|
|
17
|
+
constructor(key: VertexKey, value: V, lat: number, long: number) {
|
|
18
|
+
super(key, value);
|
|
19
19
|
this._lat = lat;
|
|
20
20
|
this._long = long;
|
|
21
21
|
}
|
|
@@ -49,11 +49,11 @@ export class MapEdge<E = any> extends DirectedEdge<E> {
|
|
|
49
49
|
* a graph.
|
|
50
50
|
* @param {VertexKey} dest - The `dest` parameter is the identifier of the destination vertex for an edge.
|
|
51
51
|
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
|
|
52
|
-
* @param {E} [
|
|
52
|
+
* @param {E} [value] - The "value" parameter is an optional parameter of type E. It is used to store additional
|
|
53
53
|
* information or data associated with the edge.
|
|
54
54
|
*/
|
|
55
|
-
constructor(src: VertexKey, dest: VertexKey, weight?: number,
|
|
56
|
-
super(src, dest, weight,
|
|
55
|
+
constructor(src: VertexKey, dest: VertexKey, weight?: number, value?: E) {
|
|
56
|
+
super(src, dest, weight, value);
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
|
|
@@ -102,15 +102,15 @@ export class MapGraph<
|
|
|
102
102
|
* The function creates a new vertex with the given key, value, latitude, and longitude.
|
|
103
103
|
* @param {VertexKey} key - The key parameter is the unique identifier for the vertex. It is of type VertexKey, which could
|
|
104
104
|
* be a string or a number depending on how you define it in your code.
|
|
105
|
-
* @param [
|
|
106
|
-
* is of type `V`, which means it should be of the same type as the `
|
|
105
|
+
* @param [value] - The `value` parameter is an optional value that can be assigned to the `value` property of the vertex. It
|
|
106
|
+
* is of type `V`, which means it should be of the same type as the `value` property of the vertex class `VO`.
|
|
107
107
|
* @param {number} lat - The `lat` parameter represents the latitude of the vertex. It is a number that specifies the
|
|
108
108
|
* position of the vertex on the Earth's surface in the north-south direction.
|
|
109
109
|
* @param {number} long - The `long` parameter represents the longitude coordinate of the vertex.
|
|
110
110
|
* @returns The method is returning a new instance of the `MapVertex` class, casted as type `VO`.
|
|
111
111
|
*/
|
|
112
|
-
override createVertex(key: VertexKey,
|
|
113
|
-
return new MapVertex(key,
|
|
112
|
+
override createVertex(key: VertexKey, value?: V, lat: number = this.origin[0], long: number = this.origin[1]): VO {
|
|
113
|
+
return new MapVertex(key, value, lat, long) as VO;
|
|
114
114
|
}
|
|
115
115
|
|
|
116
116
|
/**
|
|
@@ -121,11 +121,11 @@ export class MapGraph<
|
|
|
121
121
|
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. It
|
|
122
122
|
* is used to assign a numerical value to the edge, which can be used in algorithms such as shortest path algorithms.
|
|
123
123
|
* If the weight is not provided, it can be set to a default value or left undefined.
|
|
124
|
-
* @param [
|
|
124
|
+
* @param [value] - The `value` parameter is an optional value that can be assigned to the edge. It can be of any type,
|
|
125
125
|
* depending on the specific implementation of the `MapEdge` class.
|
|
126
126
|
* @returns a new instance of the `MapEdge` class, cast as type `EO`.
|
|
127
127
|
*/
|
|
128
|
-
override createEdge(src: VertexKey, dest: VertexKey, weight?: number,
|
|
129
|
-
return new MapEdge(src, dest, weight,
|
|
128
|
+
override createEdge(src: VertexKey, dest: VertexKey, weight?: number, value?: E): EO {
|
|
129
|
+
return new MapEdge(src, dest, weight, value) as EO;
|
|
130
130
|
}
|
|
131
131
|
}
|
|
@@ -15,11 +15,11 @@ export class UndirectedVertex<V = any> extends AbstractVertex<V> {
|
|
|
15
15
|
* The constructor function initializes a vertex with an optional value.
|
|
16
16
|
* @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
|
|
17
17
|
* used to uniquely identify the vertex within a graph or network.
|
|
18
|
-
* @param {V} [
|
|
18
|
+
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It is used to initialize the value of the
|
|
19
19
|
* vertex. If no value is provided, the vertex will be initialized with a default value.
|
|
20
20
|
*/
|
|
21
|
-
constructor(key: VertexKey,
|
|
22
|
-
super(key,
|
|
21
|
+
constructor(key: VertexKey, value?: V) {
|
|
22
|
+
super(key, value);
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
|
|
@@ -31,11 +31,11 @@ export class UndirectedEdge<E = number> extends AbstractEdge<E> {
|
|
|
31
31
|
* @param {VertexKey} v2 - The parameter `v2` is a `VertexKey`, which represents the identifier of the second vertex in a
|
|
32
32
|
* graph edge.
|
|
33
33
|
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
|
|
34
|
-
* @param {E} [
|
|
34
|
+
* @param {E} [value] - The "value" parameter is an optional parameter of type E. It is used to store a value associated
|
|
35
35
|
* with the edge.
|
|
36
36
|
*/
|
|
37
|
-
constructor(v1: VertexKey, v2: VertexKey, weight?: number,
|
|
38
|
-
super(weight,
|
|
37
|
+
constructor(v1: VertexKey, v2: VertexKey, weight?: number, value?: E) {
|
|
38
|
+
super(weight, value);
|
|
39
39
|
this._vertices = [v1, v2];
|
|
40
40
|
}
|
|
41
41
|
|
|
@@ -77,13 +77,13 @@ export class UndirectedGraph<
|
|
|
77
77
|
* The function creates a new vertex with an optional value and returns it.
|
|
78
78
|
* @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is used to distinguish one
|
|
79
79
|
* vertex from another in the graph.
|
|
80
|
-
* @param [
|
|
80
|
+
* @param [value] - The `value` parameter is an optional value that can be assigned to the vertex. If a value is provided,
|
|
81
81
|
* it will be used as the value of the vertex. If no value is provided, the `key` parameter will be used as the value of
|
|
82
82
|
* the vertex.
|
|
83
83
|
* @returns The method is returning a new instance of the `UndirectedVertex` class, casted as type `VO`.
|
|
84
84
|
*/
|
|
85
|
-
override createVertex(key: VertexKey,
|
|
86
|
-
return new UndirectedVertex(key,
|
|
85
|
+
override createVertex(key: VertexKey, value?: VO['value']): VO {
|
|
86
|
+
return new UndirectedVertex(key, value ?? key) as VO;
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
/**
|
|
@@ -92,23 +92,23 @@ export class UndirectedGraph<
|
|
|
92
92
|
* @param {VertexKey} v2 - The parameter `v2` represents the second vertex of the edge.
|
|
93
93
|
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. If
|
|
94
94
|
* no weight is provided, it defaults to 1.
|
|
95
|
-
* @param [
|
|
95
|
+
* @param [value] - The `value` parameter is an optional value that can be assigned to the edge. It can be of any type and
|
|
96
96
|
* is used to store additional information or data associated with the edge.
|
|
97
97
|
* @returns a new instance of the `UndirectedEdge` class, which is casted as type `EO`.
|
|
98
98
|
*/
|
|
99
|
-
override createEdge(v1: VertexKey, v2: VertexKey, weight?: number,
|
|
100
|
-
return new UndirectedEdge(v1, v2, weight ?? 1,
|
|
99
|
+
override createEdge(v1: VertexKey, v2: VertexKey, weight?: number, value?: EO['value']): EO {
|
|
100
|
+
return new UndirectedEdge(v1, v2, weight ?? 1, value) as EO;
|
|
101
101
|
}
|
|
102
102
|
|
|
103
103
|
/**
|
|
104
104
|
* The function `getEdge` returns the first edge that connects two vertices, or null if no such edge exists.
|
|
105
|
-
* @param {VO |
|
|
105
|
+
* @param {VO | VertexKey | null} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
106
106
|
* object), `null`, or `VertexKey` (a string or number representing the ID of a vertex).
|
|
107
|
-
* @param {VO |
|
|
107
|
+
* @param {VO | VertexKey | null} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
108
108
|
* object), `null`, or `VertexKey` (vertex ID).
|
|
109
109
|
* @returns an edge (EO) or null.
|
|
110
110
|
*/
|
|
111
|
-
getEdge(v1: VO |
|
|
111
|
+
getEdge(v1: VO | VertexKey | null, v2: VO | VertexKey | null): EO | null {
|
|
112
112
|
let edges: EO[] | undefined = [];
|
|
113
113
|
|
|
114
114
|
if (v1 !== null && v2 !== null) {
|