data-structure-typed 1.34.6 → 1.34.8
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/CHANGELOG.md +1 -1
- package/README.md +74 -35
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +70 -70
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/avl-tree.js +8 -8
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +4 -4
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.js +59 -59
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/rb-tree.js +4 -4
- package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.js +39 -39
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/graph/abstract-graph.js +49 -49
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/data-structures/graph/directed-graph.js +33 -33
- package/dist/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/data-structures/graph/map-graph.js +4 -4
- package/dist/data-structures/graph/map-graph.js.map +1 -1
- package/dist/data-structures/graph/undirected-graph.js +14 -14
- package/dist/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/data-structures/tree/tree.js +5 -5
- package/dist/data-structures/tree/tree.js.map +1 -1
- package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +71 -71
- package/lib/data-structures/binary-tree/abstract-binary-tree.js +110 -110
- package/lib/data-structures/binary-tree/avl-tree.d.ts +10 -10
- package/lib/data-structures/binary-tree/avl-tree.js +13 -13
- package/lib/data-structures/binary-tree/binary-tree.d.ts +6 -6
- package/lib/data-structures/binary-tree/binary-tree.js +7 -7
- package/lib/data-structures/binary-tree/bst.d.ts +34 -34
- package/lib/data-structures/binary-tree/bst.js +80 -80
- package/lib/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/lib/data-structures/binary-tree/rb-tree.js +4 -4
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +27 -27
- package/lib/data-structures/binary-tree/tree-multiset.js +55 -55
- package/lib/data-structures/graph/abstract-graph.d.ts +60 -60
- package/lib/data-structures/graph/abstract-graph.js +81 -81
- package/lib/data-structures/graph/directed-graph.d.ts +51 -51
- package/lib/data-structures/graph/directed-graph.js +63 -63
- package/lib/data-structures/graph/map-graph.d.ts +13 -13
- package/lib/data-structures/graph/map-graph.js +12 -12
- package/lib/data-structures/graph/undirected-graph.d.ts +30 -30
- package/lib/data-structures/graph/undirected-graph.js +32 -32
- package/lib/data-structures/heap/heap.d.ts +1 -1
- package/lib/data-structures/tree/tree.d.ts +4 -4
- package/lib/data-structures/tree/tree.js +6 -6
- package/lib/interfaces/abstract-binary-tree.d.ts +24 -24
- package/lib/interfaces/abstract-graph.d.ts +13 -13
- package/lib/interfaces/avl-tree.d.ts +3 -3
- package/lib/interfaces/bst.d.ts +8 -8
- package/lib/interfaces/directed-graph.d.ts +5 -5
- package/lib/interfaces/rb-tree.d.ts +2 -2
- package/lib/interfaces/undirected-graph.d.ts +2 -2
- package/lib/types/data-structures/abstract-binary-tree.d.ts +3 -3
- package/lib/types/data-structures/abstract-graph.d.ts +2 -2
- package/lib/types/data-structures/bst.d.ts +2 -2
- package/lib/types/data-structures/tree-multiset.d.ts +1 -1
- package/lib/types/utils/validate-type.d.ts +8 -8
- package/package.json +1 -1
- package/scripts/rename_clear_files.sh +29 -0
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +147 -147
- package/src/data-structures/binary-tree/avl-tree.ts +14 -14
- package/src/data-structures/binary-tree/binary-tree.ts +8 -8
- package/src/data-structures/binary-tree/bst.ts +98 -90
- package/src/data-structures/binary-tree/rb-tree.ts +9 -9
- package/src/data-structures/binary-tree/tree-multiset.ts +62 -62
- package/src/data-structures/graph/abstract-graph.ts +109 -104
- package/src/data-structures/graph/directed-graph.ts +77 -77
- package/src/data-structures/graph/map-graph.ts +20 -15
- package/src/data-structures/graph/undirected-graph.ts +39 -39
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/tree/tree.ts +7 -7
- package/src/interfaces/abstract-binary-tree.ts +24 -24
- package/src/interfaces/abstract-graph.ts +13 -13
- package/src/interfaces/avl-tree.ts +3 -3
- package/src/interfaces/bst.ts +8 -8
- package/src/interfaces/directed-graph.ts +5 -5
- package/src/interfaces/rb-tree.ts +2 -2
- package/src/interfaces/undirected-graph.ts +2 -2
- package/src/types/data-structures/abstract-binary-tree.ts +3 -3
- package/src/types/data-structures/abstract-graph.ts +2 -2
- package/src/types/data-structures/bst.ts +2 -2
- package/src/types/data-structures/tree-multiset.ts +1 -1
- package/src/types/utils/validate-type.ts +10 -10
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +24 -24
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/bst.test.ts +71 -71
- package/test/unit/data-structures/binary-tree/overall.test.ts +19 -19
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +72 -72
- package/test/unit/data-structures/graph/directed-graph.test.ts +8 -8
- package/test/unit/data-structures/graph/map-graph.test.ts +4 -4
- package/test/unit/data-structures/graph/overall.test.ts +2 -2
- package/test/unit/data-structures/graph/undirected-graph.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +0 -1
- package/test/unit/data-structures/tree/tree.test.ts +2 -2
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -2,8 +2,8 @@ import { CP, FamilyPosition, LoopType } from '../../types';
|
|
|
2
2
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
3
3
|
export class TreeMultisetNode extends AVLTreeNode {
|
|
4
4
|
/**
|
|
5
|
-
* The constructor function initializes a BinaryTreeNode object with an
|
|
6
|
-
* @param {
|
|
5
|
+
* The constructor function initializes a BinaryTreeNode object with an key, value, and count.
|
|
6
|
+
* @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
|
|
7
7
|
* of the binary tree node.
|
|
8
8
|
* @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value of the binary
|
|
9
9
|
* tree node. If no value is provided, it will be `undefined`.
|
|
@@ -11,8 +11,8 @@ export class TreeMultisetNode extends AVLTreeNode {
|
|
|
11
11
|
* occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
|
|
12
12
|
* parameter when creating a new instance of the `BinaryTreeNode` class,
|
|
13
13
|
*/
|
|
14
|
-
constructor(
|
|
15
|
-
super(
|
|
14
|
+
constructor(key, val, count = 1) {
|
|
15
|
+
super(key, val);
|
|
16
16
|
this._count = count;
|
|
17
17
|
}
|
|
18
18
|
get count() {
|
|
@@ -40,16 +40,16 @@ export class TreeMultiset extends AVLTree {
|
|
|
40
40
|
return this._count;
|
|
41
41
|
}
|
|
42
42
|
/**
|
|
43
|
-
* The function creates a new BSTNode with the given
|
|
44
|
-
* @param {
|
|
43
|
+
* The function creates a new BSTNode with the given key, value, and count.
|
|
44
|
+
* @param {BinaryTreeNodeKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
45
45
|
* distinguish one node from another in the tree.
|
|
46
46
|
* @param {N} val - The `val` parameter represents the value that will be stored in the binary search tree node.
|
|
47
47
|
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
48
48
|
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
49
|
-
* @returns A new instance of the BSTNode class with the specified
|
|
49
|
+
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
50
50
|
*/
|
|
51
|
-
createNode(
|
|
52
|
-
return new TreeMultisetNode(
|
|
51
|
+
createNode(key, val, count) {
|
|
52
|
+
return new TreeMultisetNode(key, val, count);
|
|
53
53
|
}
|
|
54
54
|
/**
|
|
55
55
|
* The function swaps the location of two nodes in a tree data structure.
|
|
@@ -59,15 +59,15 @@ export class TreeMultiset extends AVLTree {
|
|
|
59
59
|
* @returns the `destNode` after swapping its values with the `srcNode`.
|
|
60
60
|
*/
|
|
61
61
|
swapLocation(srcNode, destNode) {
|
|
62
|
-
const {
|
|
63
|
-
const tempNode = this.createNode(
|
|
62
|
+
const { key, val, count, height } = destNode;
|
|
63
|
+
const tempNode = this.createNode(key, val, count);
|
|
64
64
|
if (tempNode) {
|
|
65
65
|
tempNode.height = height;
|
|
66
|
-
destNode.
|
|
66
|
+
destNode.key = srcNode.key;
|
|
67
67
|
destNode.val = srcNode.val;
|
|
68
68
|
destNode.count = srcNode.count;
|
|
69
69
|
destNode.height = srcNode.height;
|
|
70
|
-
srcNode.
|
|
70
|
+
srcNode.key = tempNode.key;
|
|
71
71
|
srcNode.val = tempNode.val;
|
|
72
72
|
srcNode.count = tempNode.count;
|
|
73
73
|
srcNode.height = tempNode.height;
|
|
@@ -77,24 +77,24 @@ export class TreeMultiset extends AVLTree {
|
|
|
77
77
|
/**
|
|
78
78
|
* The `add` function adds a new node to a binary search tree, maintaining the tree's properties and balancing if
|
|
79
79
|
* necessary.
|
|
80
|
-
* @param {
|
|
80
|
+
* @param {BinaryTreeNodeKey | N} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey` or a `N` (which
|
|
81
81
|
* represents a `BinaryTreeNode`).
|
|
82
82
|
* @param [val] - The `val` parameter represents the value to be added to the binary tree node.
|
|
83
83
|
* @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the
|
|
84
84
|
* value should be added to the binary tree. If the `count` parameter is not provided, it defaults to 1.
|
|
85
85
|
* @returns The method `add` returns either the inserted node (`N`), `null`, or `undefined`.
|
|
86
86
|
*/
|
|
87
|
-
add(
|
|
87
|
+
add(keyOrNode, val, count) {
|
|
88
88
|
count = count !== null && count !== void 0 ? count : 1;
|
|
89
89
|
let inserted = undefined, newNode;
|
|
90
|
-
if (
|
|
91
|
-
newNode = this.createNode(
|
|
90
|
+
if (keyOrNode instanceof TreeMultisetNode) {
|
|
91
|
+
newNode = this.createNode(keyOrNode.key, keyOrNode.val, keyOrNode.count);
|
|
92
92
|
}
|
|
93
|
-
else if (
|
|
93
|
+
else if (keyOrNode === null) {
|
|
94
94
|
newNode = null;
|
|
95
95
|
}
|
|
96
96
|
else {
|
|
97
|
-
newNode = this.createNode(
|
|
97
|
+
newNode = this.createNode(keyOrNode, val, count);
|
|
98
98
|
}
|
|
99
99
|
if (!this.root) {
|
|
100
100
|
this._setRoot(newNode);
|
|
@@ -108,14 +108,14 @@ export class TreeMultiset extends AVLTree {
|
|
|
108
108
|
while (traversing) {
|
|
109
109
|
if (cur) {
|
|
110
110
|
if (newNode) {
|
|
111
|
-
if (this._compare(cur.
|
|
111
|
+
if (this._compare(cur.key, newNode.key) === CP.eq) {
|
|
112
112
|
cur.val = newNode.val;
|
|
113
113
|
cur.count += newNode.count;
|
|
114
114
|
this._setCount(this.count + newNode.count);
|
|
115
115
|
traversing = false;
|
|
116
116
|
inserted = cur;
|
|
117
117
|
}
|
|
118
|
-
else if (this._compare(cur.
|
|
118
|
+
else if (this._compare(cur.key, newNode.key) === CP.gt) {
|
|
119
119
|
// Traverse left of the node
|
|
120
120
|
if (cur.left === undefined) {
|
|
121
121
|
//Add to the left of the current node
|
|
@@ -131,7 +131,7 @@ export class TreeMultiset extends AVLTree {
|
|
|
131
131
|
cur = cur.left;
|
|
132
132
|
}
|
|
133
133
|
}
|
|
134
|
-
else if (this._compare(cur.
|
|
134
|
+
else if (this._compare(cur.key, newNode.key) === CP.lt) {
|
|
135
135
|
// Traverse right of the node
|
|
136
136
|
if (cur.right === undefined) {
|
|
137
137
|
//Add to the right of the current node
|
|
@@ -199,26 +199,26 @@ export class TreeMultiset extends AVLTree {
|
|
|
199
199
|
/**
|
|
200
200
|
* The `addMany` function takes an array of node IDs or nodes and adds them to the tree multiset, returning an array of
|
|
201
201
|
* the inserted nodes.
|
|
202
|
-
* @param {(
|
|
202
|
+
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} idsOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
|
|
203
203
|
* objects, or null values.
|
|
204
204
|
* @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
|
|
205
|
-
* the nodes being added. It is used when adding nodes using the `
|
|
205
|
+
* the nodes being added. It is used when adding nodes using the `keyOrNode` and `data` arguments in the `this.add()`
|
|
206
206
|
* method. If provided, the `data` array should
|
|
207
207
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
208
208
|
*/
|
|
209
209
|
addMany(idsOrNodes, data) {
|
|
210
210
|
const inserted = [];
|
|
211
211
|
for (let i = 0; i < idsOrNodes.length; i++) {
|
|
212
|
-
const
|
|
213
|
-
if (
|
|
214
|
-
inserted.push(this.add(
|
|
212
|
+
const keyOrNode = idsOrNodes[i];
|
|
213
|
+
if (keyOrNode instanceof TreeMultisetNode) {
|
|
214
|
+
inserted.push(this.add(keyOrNode.key, keyOrNode.val, keyOrNode.count));
|
|
215
215
|
continue;
|
|
216
216
|
}
|
|
217
|
-
if (
|
|
217
|
+
if (keyOrNode === null) {
|
|
218
218
|
inserted.push(this.add(NaN, null, 0));
|
|
219
219
|
continue;
|
|
220
220
|
}
|
|
221
|
-
inserted.push(this.add(
|
|
221
|
+
inserted.push(this.add(keyOrNode, data === null || data === void 0 ? void 0 : data[i], 1));
|
|
222
222
|
}
|
|
223
223
|
return inserted;
|
|
224
224
|
}
|
|
@@ -238,7 +238,7 @@ export class TreeMultiset extends AVLTree {
|
|
|
238
238
|
return;
|
|
239
239
|
const m = l + Math.floor((r - l) / 2);
|
|
240
240
|
const midNode = sorted[m];
|
|
241
|
-
this.add(midNode.
|
|
241
|
+
this.add(midNode.key, midNode.val, midNode.count);
|
|
242
242
|
buildBalanceBST(l, m - 1);
|
|
243
243
|
buildBalanceBST(m + 1, r);
|
|
244
244
|
};
|
|
@@ -254,7 +254,7 @@ export class TreeMultiset extends AVLTree {
|
|
|
254
254
|
if (l <= r) {
|
|
255
255
|
const m = l + Math.floor((r - l) / 2);
|
|
256
256
|
const midNode = sorted[m];
|
|
257
|
-
this.add(midNode.
|
|
257
|
+
this.add(midNode.key, midNode.val, midNode.count);
|
|
258
258
|
stack.push([m + 1, r]);
|
|
259
259
|
stack.push([l, m - 1]);
|
|
260
260
|
}
|
|
@@ -266,17 +266,17 @@ export class TreeMultiset extends AVLTree {
|
|
|
266
266
|
/**
|
|
267
267
|
* The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent
|
|
268
268
|
* node that needs to be balanced.
|
|
269
|
-
* @param {N |
|
|
269
|
+
* @param {N | BinaryTreeNodeKey | null} nodeOrKey - The `nodeOrKey` parameter can be one of the following:
|
|
270
270
|
* @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
|
|
271
271
|
* whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
|
|
272
272
|
* not be taken into account when removing it. If `ignoreCount` is set to `false
|
|
273
273
|
* @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
274
274
|
*/
|
|
275
|
-
remove(
|
|
275
|
+
remove(nodeOrKey, ignoreCount) {
|
|
276
276
|
const bstDeletedResult = [];
|
|
277
277
|
if (!this.root)
|
|
278
278
|
return bstDeletedResult;
|
|
279
|
-
const curr = this.get(
|
|
279
|
+
const curr = this.get(nodeOrKey);
|
|
280
280
|
if (!curr)
|
|
281
281
|
return bstDeletedResult;
|
|
282
282
|
const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
|
|
@@ -364,14 +364,14 @@ export class TreeMultiset extends AVLTree {
|
|
|
364
364
|
/**
|
|
365
365
|
* The function `subTreeSumCount` calculates the sum of the `count` property of each node in a subtree, either
|
|
366
366
|
* recursively or iteratively.
|
|
367
|
-
* @param {N |
|
|
368
|
-
* in a binary tree. It can be either a `
|
|
367
|
+
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
|
|
368
|
+
* in a binary tree. It can be either a `BinaryTreeNodeKey` (a unique identifier for a node in the binary tree) or
|
|
369
369
|
* `null` if the subtree is empty.
|
|
370
370
|
* @returns the sum of the count values of all nodes in the subtree rooted at `subTreeRoot`.
|
|
371
371
|
*/
|
|
372
372
|
subTreeSumCount(subTreeRoot) {
|
|
373
373
|
if (typeof subTreeRoot === 'number')
|
|
374
|
-
subTreeRoot = this.get(subTreeRoot, '
|
|
374
|
+
subTreeRoot = this.get(subTreeRoot, 'key');
|
|
375
375
|
if (!subTreeRoot)
|
|
376
376
|
return 0;
|
|
377
377
|
let sum = 0;
|
|
@@ -397,8 +397,8 @@ export class TreeMultiset extends AVLTree {
|
|
|
397
397
|
/**
|
|
398
398
|
* The function `subTreeAddCount` recursively or iteratively traverses a binary tree and adds a given delta value to
|
|
399
399
|
* the `count` property of each node.
|
|
400
|
-
* @param {N |
|
|
401
|
-
* in a binary tree. It can be either a `
|
|
400
|
+
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
|
|
401
|
+
* in a binary tree. It can be either a `BinaryTreeNodeKey` (a unique identifier for a node in the binary tree), a
|
|
402
402
|
* `BinaryTreeNode` object, or `null` if the subtree is empty.
|
|
403
403
|
* @param {number} delta - The delta parameter is a number that represents the amount by which the count of each node
|
|
404
404
|
* in the subtree should be increased or decreased.
|
|
@@ -406,7 +406,7 @@ export class TreeMultiset extends AVLTree {
|
|
|
406
406
|
*/
|
|
407
407
|
subTreeAddCount(subTreeRoot, delta) {
|
|
408
408
|
if (typeof subTreeRoot === 'number')
|
|
409
|
-
subTreeRoot = this.get(subTreeRoot, '
|
|
409
|
+
subTreeRoot = this.get(subTreeRoot, 'key');
|
|
410
410
|
if (!subTreeRoot)
|
|
411
411
|
return false;
|
|
412
412
|
const _addByProperty = (cur) => {
|
|
@@ -435,7 +435,7 @@ export class TreeMultiset extends AVLTree {
|
|
|
435
435
|
/**
|
|
436
436
|
* The function `getNodesByCount` returns an array of nodes that have a specific count property, either recursively or
|
|
437
437
|
* using a queue.
|
|
438
|
-
* @param {
|
|
438
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
|
|
439
439
|
* `N`. It represents the property of the nodes that you want to search for.
|
|
440
440
|
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
441
441
|
* return only one node that matches the `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne` is set
|
|
@@ -538,21 +538,21 @@ export class TreeMultiset extends AVLTree {
|
|
|
538
538
|
/**
|
|
539
539
|
* The `lesserSumCount` function calculates the sum of the counts of all nodes in a binary tree that have a lesser
|
|
540
540
|
* value than a given node.
|
|
541
|
-
* @param {N |
|
|
541
|
+
* @param {N | BinaryTreeNodeKey | null} beginNode - The `beginNode` parameter can be one of the following:
|
|
542
542
|
* @returns the sum of the counts of nodes in the binary tree that have a lesser value than the given beginNode.
|
|
543
543
|
*/
|
|
544
544
|
lesserSumCount(beginNode) {
|
|
545
545
|
if (typeof beginNode === 'number')
|
|
546
|
-
beginNode = this.get(beginNode, '
|
|
546
|
+
beginNode = this.get(beginNode, 'key');
|
|
547
547
|
if (!beginNode)
|
|
548
548
|
return 0;
|
|
549
549
|
if (!this.root)
|
|
550
550
|
return 0;
|
|
551
|
-
const
|
|
551
|
+
const key = beginNode.key;
|
|
552
552
|
let sum = 0;
|
|
553
553
|
if (this.loopType === LoopType.RECURSIVE) {
|
|
554
554
|
const _traverse = (cur) => {
|
|
555
|
-
const compared = this._compare(cur.
|
|
555
|
+
const compared = this._compare(cur.key, key);
|
|
556
556
|
if (compared === CP.eq) {
|
|
557
557
|
if (cur.right)
|
|
558
558
|
sum += this.subTreeSumCount(cur.right);
|
|
@@ -581,7 +581,7 @@ export class TreeMultiset extends AVLTree {
|
|
|
581
581
|
while (queue.length > 0) {
|
|
582
582
|
const cur = queue.shift();
|
|
583
583
|
if (cur) {
|
|
584
|
-
const compared = this._compare(cur.
|
|
584
|
+
const compared = this._compare(cur.key, key);
|
|
585
585
|
if (compared === CP.eq) {
|
|
586
586
|
if (cur.right)
|
|
587
587
|
sum += this.subTreeSumCount(cur.right);
|
|
@@ -611,29 +611,29 @@ export class TreeMultiset extends AVLTree {
|
|
|
611
611
|
/**
|
|
612
612
|
* The function `allGreaterNodesAddCount` updates the count property of all nodes in a binary tree that have an ID
|
|
613
613
|
* greater than a given ID by a specified delta value.
|
|
614
|
-
* @param {N |
|
|
614
|
+
* @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be one of the following:
|
|
615
615
|
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the `count` property
|
|
616
616
|
* of each node should be increased.
|
|
617
617
|
* @returns a boolean value.
|
|
618
618
|
*/
|
|
619
619
|
allGreaterNodesAddCount(node, delta) {
|
|
620
620
|
if (typeof node === 'number')
|
|
621
|
-
node = this.get(node, '
|
|
621
|
+
node = this.get(node, 'key');
|
|
622
622
|
if (!node)
|
|
623
623
|
return false;
|
|
624
|
-
const
|
|
624
|
+
const key = node.key;
|
|
625
625
|
if (!this.root)
|
|
626
626
|
return false;
|
|
627
627
|
if (this.loopType === LoopType.RECURSIVE) {
|
|
628
628
|
const _traverse = (cur) => {
|
|
629
|
-
const compared = this._compare(cur.
|
|
629
|
+
const compared = this._compare(cur.key, key);
|
|
630
630
|
if (compared === CP.gt)
|
|
631
631
|
cur.count += delta;
|
|
632
632
|
if (!cur.left && !cur.right)
|
|
633
633
|
return;
|
|
634
|
-
if (cur.left && this._compare(cur.left.
|
|
634
|
+
if (cur.left && this._compare(cur.left.key, key) === CP.gt)
|
|
635
635
|
_traverse(cur.left);
|
|
636
|
-
if (cur.right && this._compare(cur.right.
|
|
636
|
+
if (cur.right && this._compare(cur.right.key, key) === CP.gt)
|
|
637
637
|
_traverse(cur.right);
|
|
638
638
|
};
|
|
639
639
|
_traverse(this.root);
|
|
@@ -644,12 +644,12 @@ export class TreeMultiset extends AVLTree {
|
|
|
644
644
|
while (queue.length > 0) {
|
|
645
645
|
const cur = queue.shift();
|
|
646
646
|
if (cur) {
|
|
647
|
-
const compared = this._compare(cur.
|
|
647
|
+
const compared = this._compare(cur.key, key);
|
|
648
648
|
if (compared === CP.gt)
|
|
649
649
|
cur.count += delta;
|
|
650
|
-
if (cur.left && this._compare(cur.left.
|
|
650
|
+
if (cur.left && this._compare(cur.left.key, key) === CP.gt)
|
|
651
651
|
queue.push(cur.left);
|
|
652
|
-
if (cur.right && this._compare(cur.right.
|
|
652
|
+
if (cur.right && this._compare(cur.right.key, key) === CP.gt)
|
|
653
653
|
queue.push(cur.right);
|
|
654
654
|
}
|
|
655
655
|
}
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import type { DijkstraResult,
|
|
1
|
+
import type { DijkstraResult, VertexKey } from '../../types';
|
|
2
2
|
import { IAbstractGraph } from '../../interfaces';
|
|
3
3
|
export declare abstract class AbstractVertex<V = any> {
|
|
4
4
|
/**
|
|
5
|
-
* The function is a protected constructor that takes an
|
|
6
|
-
* @param {
|
|
5
|
+
* The function is a protected constructor that takes an key and an optional value as parameters.
|
|
6
|
+
* @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
|
|
7
7
|
* used to uniquely identify the vertex object.
|
|
8
8
|
* @param {V} [val] - The parameter "val" is an optional parameter of type V. It is used to assign a value to the
|
|
9
9
|
* vertex. If no value is provided, it will be set to undefined.
|
|
10
10
|
*/
|
|
11
|
-
protected constructor(
|
|
12
|
-
private
|
|
13
|
-
get
|
|
14
|
-
set
|
|
11
|
+
protected constructor(key: VertexKey, val?: V);
|
|
12
|
+
private _key;
|
|
13
|
+
get key(): VertexKey;
|
|
14
|
+
set key(v: VertexKey);
|
|
15
15
|
private _val;
|
|
16
16
|
get val(): V | undefined;
|
|
17
17
|
set val(value: V | undefined);
|
|
@@ -48,14 +48,14 @@ export declare abstract class AbstractEdge<V = any> {
|
|
|
48
48
|
}
|
|
49
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
|
-
get vertices(): Map<
|
|
51
|
+
get vertices(): Map<VertexKey, V>;
|
|
52
52
|
/**
|
|
53
53
|
* 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.
|
|
54
54
|
* 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.
|
|
55
|
-
* @param
|
|
55
|
+
* @param key
|
|
56
56
|
* @param val
|
|
57
57
|
*/
|
|
58
|
-
abstract createVertex(
|
|
58
|
+
abstract createVertex(key: VertexKey, val?: V): V;
|
|
59
59
|
/**
|
|
60
60
|
* 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.
|
|
61
61
|
* 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.
|
|
@@ -64,77 +64,77 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any> = Abst
|
|
|
64
64
|
* @param weight
|
|
65
65
|
* @param val
|
|
66
66
|
*/
|
|
67
|
-
abstract createEdge(srcOrV1:
|
|
67
|
+
abstract createEdge(srcOrV1: VertexKey | string, destOrV2: VertexKey | string, weight?: number, val?: E): E;
|
|
68
68
|
abstract removeEdge(edge: E): E | null;
|
|
69
|
-
abstract getEdge(
|
|
70
|
-
abstract degreeOf(
|
|
69
|
+
abstract getEdge(srcOrKey: V | VertexKey, destOrKey: V | VertexKey): E | null;
|
|
70
|
+
abstract degreeOf(vertexOrKey: V | VertexKey): number;
|
|
71
71
|
abstract edgeSet(): E[];
|
|
72
|
-
abstract edgesOf(
|
|
73
|
-
abstract getNeighbors(
|
|
72
|
+
abstract edgesOf(vertexOrKey: V | VertexKey): E[];
|
|
73
|
+
abstract getNeighbors(vertexOrKey: V | VertexKey): V[];
|
|
74
74
|
abstract getEndsOfEdge(edge: E): [V, V] | null;
|
|
75
75
|
/**
|
|
76
76
|
* The function "getVertex" returns the vertex with the specified ID or null if it doesn't exist.
|
|
77
|
-
* @param {
|
|
77
|
+
* @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
|
|
78
78
|
* the `_vertices` map.
|
|
79
|
-
* @returns The method `getVertex` returns the vertex with the specified `
|
|
79
|
+
* @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `_vertices`
|
|
80
80
|
* map. If the vertex does not exist, it returns `null`.
|
|
81
81
|
*/
|
|
82
|
-
getVertex(
|
|
82
|
+
getVertex(vertexKey: VertexKey): V | null;
|
|
83
83
|
/**
|
|
84
84
|
* The function checks if a vertex exists in a graph.
|
|
85
|
-
* @param {V |
|
|
86
|
-
* (`
|
|
85
|
+
* @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`V`) or a vertex ID
|
|
86
|
+
* (`VertexKey`).
|
|
87
87
|
* @returns a boolean value.
|
|
88
88
|
*/
|
|
89
|
-
hasVertex(
|
|
89
|
+
hasVertex(vertexOrKey: V | VertexKey): boolean;
|
|
90
90
|
addVertex(vertex: V): boolean;
|
|
91
|
-
addVertex(
|
|
91
|
+
addVertex(key: VertexKey, val?: V['val']): boolean;
|
|
92
92
|
/**
|
|
93
93
|
* The `removeVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
94
|
-
* @param {V |
|
|
95
|
-
* (`
|
|
94
|
+
* @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`V`) or a vertex ID
|
|
95
|
+
* (`VertexKey`).
|
|
96
96
|
* @returns The method is returning a boolean value.
|
|
97
97
|
*/
|
|
98
|
-
removeVertex(
|
|
98
|
+
removeVertex(vertexOrKey: V | VertexKey): boolean;
|
|
99
99
|
/**
|
|
100
100
|
* The function removes all vertices from a graph and returns a boolean indicating if any vertices were removed.
|
|
101
|
-
* @param {V[] |
|
|
102
|
-
* of vertex IDs (`
|
|
101
|
+
* @param {V[] | VertexKey[]} vertices - The `vertices` parameter can be either an array of vertices (`V[]`) or an array
|
|
102
|
+
* of vertex IDs (`VertexKey[]`).
|
|
103
103
|
* @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertices
|
|
104
104
|
* were removed.
|
|
105
105
|
*/
|
|
106
|
-
removeAllVertices(vertices: V[] |
|
|
106
|
+
removeAllVertices(vertices: V[] | VertexKey[]): boolean;
|
|
107
107
|
/**
|
|
108
108
|
* The function checks if there is an edge between two vertices and returns a boolean value indicating the result.
|
|
109
|
-
* @param {
|
|
109
|
+
* @param {VertexKey | V} v1 - The parameter v1 can be either a VertexKey or a V. A VertexKey represents the unique
|
|
110
110
|
* identifier of a vertex in a graph, while V represents the type of the vertex object itself.
|
|
111
|
-
* @param {
|
|
112
|
-
* `
|
|
111
|
+
* @param {VertexKey | V} v2 - The parameter `v2` represents the second vertex in the edge. It can be either a
|
|
112
|
+
* `VertexKey` or a `V` type, which represents the type of the vertex.
|
|
113
113
|
* @returns A boolean value is being returned.
|
|
114
114
|
*/
|
|
115
|
-
hasEdge(v1:
|
|
115
|
+
hasEdge(v1: VertexKey | V, v2: VertexKey | V): boolean;
|
|
116
116
|
addEdge(edge: E): boolean;
|
|
117
|
-
addEdge(src: V |
|
|
117
|
+
addEdge(src: V | VertexKey, dest: V | VertexKey, weight?: number, val?: E['val']): boolean;
|
|
118
118
|
/**
|
|
119
119
|
* The function sets the weight of an edge between two vertices in a graph.
|
|
120
|
-
* @param {
|
|
120
|
+
* @param {VertexKey | V} srcOrKey - The `srcOrKey` parameter can be either a `VertexKey` or a `V` object. It represents
|
|
121
121
|
* the source vertex of the edge.
|
|
122
|
-
* @param {
|
|
123
|
-
* either a `
|
|
124
|
-
* @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (
|
|
125
|
-
* and the destination vertex (
|
|
122
|
+
* @param {VertexKey | V} destOrKey - The `destOrKey` parameter represents the destination vertex of the edge. It can be
|
|
123
|
+
* either a `VertexKey` or a vertex object `V`.
|
|
124
|
+
* @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (srcOrKey)
|
|
125
|
+
* and the destination vertex (destOrKey).
|
|
126
126
|
* @returns a boolean value. If the edge exists between the source and destination vertices, the function will update
|
|
127
127
|
* the weight of the edge and return true. If the edge does not exist, the function will return false.
|
|
128
128
|
*/
|
|
129
|
-
setEdgeWeight(
|
|
129
|
+
setEdgeWeight(srcOrKey: VertexKey | V, destOrKey: VertexKey | V, weight: number): boolean;
|
|
130
130
|
/**
|
|
131
131
|
* The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
|
|
132
|
-
* @param {V |
|
|
132
|
+
* @param {V | VertexKey} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexKey`).
|
|
133
133
|
* It is the starting vertex for finding paths.
|
|
134
|
-
* @param {V |
|
|
134
|
+
* @param {V | VertexKey} v2 - The parameter `v2` represents either a vertex object (`V`) or a vertex ID (`VertexKey`).
|
|
135
135
|
* @returns The function `getAllPathsBetween` returns an array of arrays of vertices (`V[][]`).
|
|
136
136
|
*/
|
|
137
|
-
getAllPathsBetween(v1: V |
|
|
137
|
+
getAllPathsBetween(v1: V | VertexKey, v2: V | VertexKey): V[][];
|
|
138
138
|
/**
|
|
139
139
|
* The function calculates the sum of weights along a given path.
|
|
140
140
|
* @param {V[]} path - An array of vertices (V) representing a path in a graph.
|
|
@@ -144,8 +144,8 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any> = Abst
|
|
|
144
144
|
/**
|
|
145
145
|
* The function `getMinCostBetween` calculates the minimum cost between two vertices in a graph, either based on edge
|
|
146
146
|
* weights or using a breadth-first search algorithm.
|
|
147
|
-
* @param {V |
|
|
148
|
-
* @param {V |
|
|
147
|
+
* @param {V | VertexKey} v1 - The parameter `v1` represents the starting vertex or its ID.
|
|
148
|
+
* @param {V | VertexKey} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex to which
|
|
149
149
|
* you want to find the minimum cost or weight from the source vertex `v1`.
|
|
150
150
|
* @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edges have weights.
|
|
151
151
|
* If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
|
|
@@ -155,13 +155,13 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any> = Abst
|
|
|
155
155
|
* vertices. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
|
|
156
156
|
* minimum number of
|
|
157
157
|
*/
|
|
158
|
-
getMinCostBetween(v1: V |
|
|
158
|
+
getMinCostBetween(v1: V | VertexKey, v2: V | VertexKey, isWeight?: boolean): number | null;
|
|
159
159
|
/**
|
|
160
160
|
* The function `getMinPathBetween` returns the minimum path between two vertices in a graph, either based on weight or
|
|
161
161
|
* using a breadth-first search algorithm.
|
|
162
|
-
* @param {V |
|
|
163
|
-
* object (`V`) or a vertex ID (`
|
|
164
|
-
* @param {V |
|
|
162
|
+
* @param {V | VertexKey} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
|
|
163
|
+
* object (`V`) or a vertex ID (`VertexKey`).
|
|
164
|
+
* @param {V | VertexKey} v2 - V | VertexKey - The second vertex or vertex ID between which we want to find the minimum
|
|
165
165
|
* path.
|
|
166
166
|
* @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edges in finding the
|
|
167
167
|
* minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
|
|
@@ -169,7 +169,7 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any> = Abst
|
|
|
169
169
|
* @returns The function `getMinPathBetween` returns an array of vertices (`V[]`) representing the minimum path between
|
|
170
170
|
* two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `null`.
|
|
171
171
|
*/
|
|
172
|
-
getMinPathBetween(v1: V |
|
|
172
|
+
getMinPathBetween(v1: V | VertexKey, v2: V | VertexKey, isWeight?: boolean): V[] | null;
|
|
173
173
|
/**
|
|
174
174
|
* Dijkstra algorithm time: O(VE) space: O(V + E)
|
|
175
175
|
* /
|
|
@@ -178,9 +178,9 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any> = Abst
|
|
|
178
178
|
* Dijkstra algorithm time: O(VE) space: O(V + E)
|
|
179
179
|
* The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertices in
|
|
180
180
|
* a graph without using a heap data structure.
|
|
181
|
-
* @param {V |
|
|
181
|
+
* @param {V | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
182
182
|
* vertex object or a vertex ID.
|
|
183
|
-
* @param {V |
|
|
183
|
+
* @param {V | VertexKey | null} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
|
|
184
184
|
* parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
|
|
185
185
|
* identifier. If no destination is provided, the value is set to `null`.
|
|
186
186
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
@@ -191,7 +191,7 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any> = Abst
|
|
|
191
191
|
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
192
192
|
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<V>`.
|
|
193
193
|
*/
|
|
194
|
-
dijkstraWithoutHeap(src: V |
|
|
194
|
+
dijkstraWithoutHeap(src: V | VertexKey, dest?: V | VertexKey | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<V>;
|
|
195
195
|
/**
|
|
196
196
|
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
197
197
|
*
|
|
@@ -205,9 +205,9 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any> = Abst
|
|
|
205
205
|
* Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
|
|
206
206
|
* The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
|
|
207
207
|
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
208
|
-
* @param {V |
|
|
208
|
+
* @param {V | VertexKey} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
|
|
209
209
|
* start. It can be either a vertex object or a vertex ID.
|
|
210
|
-
* @param {V |
|
|
210
|
+
* @param {V | VertexKey | null} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
211
211
|
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
212
212
|
* will calculate the shortest paths to all other vertices from the source vertex.
|
|
213
213
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
@@ -218,7 +218,7 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any> = Abst
|
|
|
218
218
|
* shortest paths from the source vertex to all other vertices in the graph. If `genPaths
|
|
219
219
|
* @returns The function `dijkstra` returns an object of type `DijkstraResult<V>`.
|
|
220
220
|
*/
|
|
221
|
-
dijkstra(src: V |
|
|
221
|
+
dijkstra(src: V | VertexKey, dest?: V | VertexKey | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<V>;
|
|
222
222
|
/**
|
|
223
223
|
* BellmanFord time:O(VE) space:O(V)
|
|
224
224
|
* one to rest pairs
|
|
@@ -230,7 +230,7 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any> = Abst
|
|
|
230
230
|
* The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
|
|
231
231
|
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
232
232
|
* all other vertices in a graph, and optionally detects negative cycles and generates the minimum path.
|
|
233
|
-
* @param {V |
|
|
233
|
+
* @param {V | VertexKey} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
|
|
234
234
|
* start calculating the shortest paths. It can be either a vertex object or a vertex ID.
|
|
235
235
|
* @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
|
|
236
236
|
* @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
|
|
@@ -240,7 +240,7 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any> = Abst
|
|
|
240
240
|
* vertex.
|
|
241
241
|
* @returns The function `bellmanFord` returns an object with the following properties:
|
|
242
242
|
*/
|
|
243
|
-
bellmanFord(src: V |
|
|
243
|
+
bellmanFord(src: V | VertexKey, scanNegativeCycle?: boolean, getMin?: boolean, genPath?: boolean): {
|
|
244
244
|
hasNegativeCycle: boolean | undefined;
|
|
245
245
|
distMap: Map<V, number>;
|
|
246
246
|
preMap: Map<V, V>;
|
|
@@ -326,7 +326,7 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any> = Abst
|
|
|
326
326
|
};
|
|
327
327
|
protected abstract _addEdgeOnly(edge: E): boolean;
|
|
328
328
|
protected _addVertexOnly(newVertex: V): boolean;
|
|
329
|
-
protected _getVertex(
|
|
330
|
-
protected
|
|
331
|
-
protected _setVertices(value: Map<
|
|
329
|
+
protected _getVertex(vertexOrKey: VertexKey | V): V | null;
|
|
330
|
+
protected _getVertexKey(vertexOrKey: V | VertexKey): VertexKey;
|
|
331
|
+
protected _setVertices(value: Map<VertexKey, V>): void;
|
|
332
332
|
}
|