data-structure-typed 1.12.11 → 1.12.21
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 +14 -5
- package/dist/data-structures/binary-tree/avl-tree.js +15 -6
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +11 -2
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +11 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +26 -17
- package/dist/data-structures/binary-tree/binary-tree.js +72 -62
- package/dist/data-structures/binary-tree/bst.d.ts +92 -5
- package/dist/data-structures/binary-tree/bst.js +89 -5
- package/dist/data-structures/binary-tree/segment-tree.d.ts +41 -2
- package/dist/data-structures/binary-tree/segment-tree.js +41 -2
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +34 -3
- package/dist/data-structures/binary-tree/tree-multiset.js +35 -4
- package/dist/data-structures/graph/abstract-graph.d.ts +5 -0
- package/dist/data-structures/graph/abstract-graph.js +12 -4
- package/dist/data-structures/graph/directed-graph.d.ts +18 -4
- package/dist/data-structures/graph/directed-graph.js +24 -37
- package/dist/data-structures/graph/undirected-graph.d.ts +13 -0
- package/dist/data-structures/graph/undirected-graph.js +18 -2
- package/dist/data-structures/hash/coordinate-map.d.ts +5 -2
- package/dist/data-structures/hash/coordinate-map.js +5 -2
- package/dist/data-structures/hash/coordinate-set.d.ts +5 -2
- package/dist/data-structures/hash/coordinate-set.js +5 -2
- package/dist/data-structures/heap/heap.d.ts +9 -6
- package/dist/data-structures/heap/heap.js +8 -8
- package/dist/data-structures/heap/max-heap.d.ts +5 -2
- package/dist/data-structures/heap/max-heap.js +5 -2
- package/dist/data-structures/heap/min-heap.d.ts +5 -2
- package/dist/data-structures/heap/min-heap.js +5 -2
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.js +4 -4
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +5 -2
- package/dist/data-structures/linked-list/singly-linked-list.js +5 -2
- package/dist/data-structures/matrix/matrix.d.ts +5 -2
- package/dist/data-structures/matrix/matrix.js +5 -2
- package/dist/data-structures/matrix/matrix2d.d.ts +5 -2
- package/dist/data-structures/matrix/matrix2d.js +5 -2
- package/dist/data-structures/matrix/navigator.d.ts +5 -2
- package/dist/data-structures/matrix/vector2d.d.ts +5 -2
- package/dist/data-structures/matrix/vector2d.js +5 -2
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +5 -2
- package/dist/data-structures/priority-queue/max-priority-queue.js +5 -2
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +5 -2
- package/dist/data-structures/priority-queue/min-priority-queue.js +5 -2
- package/dist/data-structures/priority-queue/priority-queue.d.ts +7 -4
- package/dist/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/data-structures/queue/deque.d.ts +12 -9
- package/dist/data-structures/queue/deque.js +12 -9
- package/dist/data-structures/queue/queue.d.ts +4 -4
- package/dist/data-structures/queue/queue.js +4 -4
- package/dist/data-structures/stack/stack.d.ts +1 -1
- package/dist/data-structures/stack/stack.js +1 -1
- package/dist/data-structures/trie/trie.d.ts +6 -3
- package/dist/data-structures/trie/trie.js +7 -4
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/types/utils.d.ts +8 -10
- package/dist/utils/types/utils.js +0 -1
- package/dist/utils/utils.d.ts +18 -8
- package/dist/utils/utils.js +93 -47
- package/package.json +1 -1
- package/dist/utils/trampoline.d.ts +0 -14
- package/dist/utils/trampoline.js +0 -130
|
@@ -52,6 +52,10 @@ var BSTNode = /** @class */ (function (_super) {
|
|
|
52
52
|
exports.BSTNode = BSTNode;
|
|
53
53
|
var BST = /** @class */ (function (_super) {
|
|
54
54
|
__extends(BST, _super);
|
|
55
|
+
/**
|
|
56
|
+
* The constructor function accepts an optional options object and sets the comparator property if provided.
|
|
57
|
+
* @param [options] - An optional object that can contain the following properties:
|
|
58
|
+
*/
|
|
55
59
|
function BST(options) {
|
|
56
60
|
var _this = _super.call(this, options) || this;
|
|
57
61
|
_this._comparator = function (a, b) { return a - b; };
|
|
@@ -67,7 +71,7 @@ var BST = /** @class */ (function (_super) {
|
|
|
67
71
|
return val !== null ? new BSTNode(id, val, count) : null;
|
|
68
72
|
};
|
|
69
73
|
/**
|
|
70
|
-
* The `
|
|
74
|
+
* The `add` function inserts a new node into a binary search tree, updating the count and value of an existing node if
|
|
71
75
|
* the ID matches, and returns the inserted node.
|
|
72
76
|
* @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node. It is used to
|
|
73
77
|
* determine the position of the node in the binary search tree.
|
|
@@ -76,9 +80,9 @@ var BST = /** @class */ (function (_super) {
|
|
|
76
80
|
* @param {number} [count=1] - The `count` parameter represents the number of times the value should be inserted into
|
|
77
81
|
* the binary search tree. By default, it is set to 1, meaning that if no count is specified, the value will be
|
|
78
82
|
* inserted once.
|
|
79
|
-
* @returns The method `
|
|
83
|
+
* @returns The method `add` returns a `BSTNode<T>` object or `null`.
|
|
80
84
|
*/
|
|
81
|
-
BST.prototype.
|
|
85
|
+
BST.prototype.add = function (id, val, count) {
|
|
82
86
|
var _a;
|
|
83
87
|
if (count === void 0) { count = 1; }
|
|
84
88
|
var inserted = null;
|
|
@@ -152,11 +156,28 @@ var BST = /** @class */ (function (_super) {
|
|
|
152
156
|
}
|
|
153
157
|
return inserted;
|
|
154
158
|
};
|
|
159
|
+
/**
|
|
160
|
+
* The `get` function returns the first node in a binary search tree that matches the given property value or name.
|
|
161
|
+
* @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
|
|
162
|
+
* generic type `T`. It represents the value of the property that you want to search for in the binary search tree.
|
|
163
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
164
|
+
* specifies the property name to use for searching the binary search tree nodes. If not provided, it defaults to
|
|
165
|
+
* `'id'`.
|
|
166
|
+
* @returns The method is returning a BSTNode<T> object or null.
|
|
167
|
+
*/
|
|
155
168
|
BST.prototype.get = function (nodeProperty, propertyName) {
|
|
156
169
|
var _a;
|
|
157
170
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
158
171
|
return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
|
|
159
172
|
};
|
|
173
|
+
/**
|
|
174
|
+
* The function returns the id of the rightmost node if the comparison between two values is less than, the id of the
|
|
175
|
+
* leftmost node if the comparison is greater than, and the id of the rightmost node otherwise.
|
|
176
|
+
* @returns The function `lastKey()` returns the ID of the rightmost node in a binary tree. If the comparison between
|
|
177
|
+
* the first two elements in the tree is less than, it returns the ID of the rightmost node. If the comparison is
|
|
178
|
+
* greater than, it returns the ID of the leftmost node. Otherwise, it also returns the ID of the rightmost node. If
|
|
179
|
+
* there are no nodes in
|
|
180
|
+
*/
|
|
160
181
|
BST.prototype.lastKey = function () {
|
|
161
182
|
var _a, _b, _c, _d, _e, _f;
|
|
162
183
|
if (this._compare(0, 1) === CP.lt)
|
|
@@ -166,6 +187,16 @@ var BST = /** @class */ (function (_super) {
|
|
|
166
187
|
else
|
|
167
188
|
return (_f = (_e = this.getRightMost()) === null || _e === void 0 ? void 0 : _e.id) !== null && _f !== void 0 ? _f : 0;
|
|
168
189
|
};
|
|
190
|
+
/**
|
|
191
|
+
* The `remove` function in this TypeScript code removes a node from a binary search tree and returns information about
|
|
192
|
+
* the deleted node and any nodes that need to be balanced.
|
|
193
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that needs to be removed
|
|
194
|
+
* from the binary search tree.
|
|
195
|
+
* @param {boolean} [ignoreCount] - A boolean flag indicating whether to ignore the count of the node being removed. If
|
|
196
|
+
* set to true, the count of the node will not be considered and the node will be removed regardless of its count. If
|
|
197
|
+
* set to false or not provided, the count of the node will be taken into account and the
|
|
198
|
+
* @returns an array of `BSTDeletedResult<T>` objects.
|
|
199
|
+
*/
|
|
169
200
|
BST.prototype.remove = function (id, ignoreCount) {
|
|
170
201
|
var bstDeletedResult = [];
|
|
171
202
|
if (!this.root)
|
|
@@ -217,6 +248,19 @@ var BST = /** @class */ (function (_super) {
|
|
|
217
248
|
bstDeletedResult.push({ deleted: orgCurrent, needBalanced: needBalanced });
|
|
218
249
|
return bstDeletedResult;
|
|
219
250
|
};
|
|
251
|
+
/**
|
|
252
|
+
* The function `getNodes` returns an array of binary search tree nodes that match a given property value, with the
|
|
253
|
+
* option to specify the property name and whether to return only one node.
|
|
254
|
+
* @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
|
|
255
|
+
* generic type `T`. It represents the property value that you want to search for in the binary search tree.
|
|
256
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
257
|
+
* specifies the property of the nodes to compare with the `nodeProperty` parameter. If not provided, it defaults to
|
|
258
|
+
* `'id'`.
|
|
259
|
+
* @param {boolean} [onlyOne] - A boolean value indicating whether to return only one node that matches the given
|
|
260
|
+
* nodeProperty. If set to true, the function will stop traversing the tree and return the first matching node. If set
|
|
261
|
+
* to false or not provided, the function will return all nodes that match the given nodeProperty.
|
|
262
|
+
* @returns an array of BSTNode<T> objects.
|
|
263
|
+
*/
|
|
220
264
|
BST.prototype.getNodes = function (nodeProperty, propertyName, onlyOne) {
|
|
221
265
|
var _this = this;
|
|
222
266
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
@@ -265,6 +309,16 @@ var BST = /** @class */ (function (_super) {
|
|
|
265
309
|
return result;
|
|
266
310
|
};
|
|
267
311
|
// --- start additional functions
|
|
312
|
+
/**
|
|
313
|
+
* The `lesserSum` function calculates the sum of a specified property in all nodes with an ID less than a given ID in
|
|
314
|
+
* a binary search tree.
|
|
315
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node for which you want to
|
|
316
|
+
* calculate the lesser sum.
|
|
317
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
318
|
+
* specifies the property of the binary tree node to use for calculating the sum. If not provided, it defaults to 'id'.
|
|
319
|
+
* @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in the
|
|
320
|
+
* binary search tree that have a property value lesser than the given `id`.
|
|
321
|
+
*/
|
|
268
322
|
BST.prototype.lesserSum = function (id, propertyName) {
|
|
269
323
|
var _this = this;
|
|
270
324
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
@@ -343,6 +397,18 @@ var BST = /** @class */ (function (_super) {
|
|
|
343
397
|
}
|
|
344
398
|
return sum;
|
|
345
399
|
};
|
|
400
|
+
/**
|
|
401
|
+
* The function `allGreaterNodesAdd` updates the value of a specified property for all nodes in a binary search tree
|
|
402
|
+
* that have a greater value than a given node.
|
|
403
|
+
* @param node - The `node` parameter is of type `BSTNode<T>`, which represents a node in a binary search tree. It
|
|
404
|
+
* contains properties such as `id` and `count`.
|
|
405
|
+
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
|
|
406
|
+
* each node should be increased.
|
|
407
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - propertyName is an optional parameter that specifies the
|
|
408
|
+
* property of the BSTNode to be modified. It can be either 'id' or 'count'. If propertyName is not provided, it
|
|
409
|
+
* defaults to 'id'.
|
|
410
|
+
* @returns a boolean value.
|
|
411
|
+
*/
|
|
346
412
|
BST.prototype.allGreaterNodesAdd = function (node, delta, propertyName) {
|
|
347
413
|
var _this = this;
|
|
348
414
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
@@ -391,6 +457,11 @@ var BST = /** @class */ (function (_super) {
|
|
|
391
457
|
return true;
|
|
392
458
|
}
|
|
393
459
|
};
|
|
460
|
+
/**
|
|
461
|
+
* The `balance` function takes a sorted array of nodes and builds a balanced binary search tree using either a
|
|
462
|
+
* recursive or iterative approach.
|
|
463
|
+
* @returns The `balance()` function returns a boolean value.
|
|
464
|
+
*/
|
|
394
465
|
BST.prototype.balance = function () {
|
|
395
466
|
var _this = this;
|
|
396
467
|
var sorted = this.DFS('in', 'node'), n = sorted.length;
|
|
@@ -403,7 +474,7 @@ var BST = /** @class */ (function (_super) {
|
|
|
403
474
|
return;
|
|
404
475
|
var m = l + Math.floor((r - l) / 2);
|
|
405
476
|
var midNode = sorted[m];
|
|
406
|
-
_this.
|
|
477
|
+
_this.add(midNode.id, midNode.val, midNode.count);
|
|
407
478
|
buildBalanceBST_1(l, m - 1);
|
|
408
479
|
buildBalanceBST_1(m + 1, r);
|
|
409
480
|
};
|
|
@@ -419,7 +490,7 @@ var BST = /** @class */ (function (_super) {
|
|
|
419
490
|
if (l <= r) {
|
|
420
491
|
var m = l + Math.floor((r - l) / 2);
|
|
421
492
|
var midNode = sorted[m];
|
|
422
|
-
this.
|
|
493
|
+
this.add(midNode.id, midNode.val, midNode.count);
|
|
423
494
|
stack.push([m + 1, r]);
|
|
424
495
|
stack.push([l, m - 1]);
|
|
425
496
|
}
|
|
@@ -428,6 +499,11 @@ var BST = /** @class */ (function (_super) {
|
|
|
428
499
|
return true;
|
|
429
500
|
}
|
|
430
501
|
};
|
|
502
|
+
/**
|
|
503
|
+
* The function `isAVLBalanced` checks if a binary search tree is balanced according to the AVL tree property.
|
|
504
|
+
* @returns The function `isAVLBalanced()` returns a boolean value. It returns `true` if the binary search tree (BST)
|
|
505
|
+
* is balanced according to the AVL tree property, and `false` otherwise.
|
|
506
|
+
*/
|
|
431
507
|
BST.prototype.isAVLBalanced = function () {
|
|
432
508
|
var _a, _b;
|
|
433
509
|
if (!this.root)
|
|
@@ -474,6 +550,14 @@ var BST = /** @class */ (function (_super) {
|
|
|
474
550
|
}
|
|
475
551
|
return balanced;
|
|
476
552
|
};
|
|
553
|
+
/**
|
|
554
|
+
* The function compares two binary tree node IDs using a comparator function and returns whether the first ID is
|
|
555
|
+
* greater than, less than, or equal to the second ID.
|
|
556
|
+
* @param {BinaryTreeNodeId} a - a is a BinaryTreeNodeId, which represents the identifier of a binary tree node.
|
|
557
|
+
* @param {BinaryTreeNodeId} b - The parameter "b" in the above code refers to a BinaryTreeNodeId.
|
|
558
|
+
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater than), CP.lt (less
|
|
559
|
+
* than), or CP.eq (equal).
|
|
560
|
+
*/
|
|
477
561
|
BST.prototype._compare = function (a, b) {
|
|
478
562
|
var compared = this._comparator(a, b);
|
|
479
563
|
if (compared > 0)
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
4
7
|
*/
|
|
5
8
|
import type { SegmentTreeNodeVal } from '../types';
|
|
6
9
|
export declare class SegmentTreeNode {
|
|
@@ -28,10 +31,46 @@ export declare class SegmentTree {
|
|
|
28
31
|
protected _values: number[];
|
|
29
32
|
protected _start: number;
|
|
30
33
|
protected _end: number;
|
|
34
|
+
/**
|
|
35
|
+
* The constructor initializes the values, start, end, and root properties of an object.
|
|
36
|
+
* @param {number[]} values - An array of numbers that will be used to build a binary search tree.
|
|
37
|
+
* @param {number} [start] - The `start` parameter is the index of the first element in the `values` array that should
|
|
38
|
+
* be included in the range. If no value is provided for `start`, it defaults to 0, which means the range starts from
|
|
39
|
+
* the beginning of the array.
|
|
40
|
+
* @param {number} [end] - The "end" parameter is the index of the last element in the "values" array that should be
|
|
41
|
+
* included in the range. If not provided, it defaults to the index of the last element in the "values" array.
|
|
42
|
+
*/
|
|
31
43
|
constructor(values: number[], start?: number, end?: number);
|
|
32
44
|
protected _root: SegmentTreeNode | null;
|
|
33
45
|
get root(): SegmentTreeNode | null;
|
|
46
|
+
/**
|
|
47
|
+
* The function builds a segment tree by recursively dividing the given range into smaller segments and creating nodes
|
|
48
|
+
* for each segment.
|
|
49
|
+
* @param {number} start - The `start` parameter represents the starting index of the segment or range for which we are
|
|
50
|
+
* building the segment tree.
|
|
51
|
+
* @param {number} end - The `end` parameter represents the ending index of the segment or range for which we are
|
|
52
|
+
* building the segment tree.
|
|
53
|
+
* @returns a SegmentTreeNode object.
|
|
54
|
+
*/
|
|
34
55
|
build(start: number, end: number): SegmentTreeNode;
|
|
56
|
+
/**
|
|
57
|
+
* The function updates the value of a node in a segment tree and recalculates the sum of its children if they exist.
|
|
58
|
+
* @param {number} index - The index parameter represents the index of the node in the segment tree that needs to be
|
|
59
|
+
* updated.
|
|
60
|
+
* @param {number} sum - The `sum` parameter represents the new value that should be assigned to the `sum` property of
|
|
61
|
+
* the `SegmentTreeNode` at the specified `index`.
|
|
62
|
+
* @param {SegmentTreeNodeVal} [val] - The `val` parameter is an optional value that can be assigned to the `val`
|
|
63
|
+
* property of the `SegmentTreeNode` object. It is not currently used in the code, but you can uncomment the line `//
|
|
64
|
+
* cur.val = val;` and pass a value for `val` in the
|
|
65
|
+
* @returns The function does not return anything.
|
|
66
|
+
*/
|
|
35
67
|
updateNode(index: number, sum: number, val?: SegmentTreeNodeVal): void;
|
|
68
|
+
/**
|
|
69
|
+
* The function `querySumByRange` calculates the sum of values within a given range in a segment tree.
|
|
70
|
+
* @param {number} indexA - The starting index of the range for which you want to calculate the sum.
|
|
71
|
+
* @param {number} indexB - The parameter `indexB` represents the ending index of the range for which you want to
|
|
72
|
+
* calculate the sum.
|
|
73
|
+
* @returns The function `querySumByRange` returns a number.
|
|
74
|
+
*/
|
|
36
75
|
querySumByRange(indexA: number, indexB: number): number;
|
|
37
76
|
}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
3
|
+
* data-structure-typed
|
|
4
|
+
*
|
|
5
|
+
* @author Tyler Zeng
|
|
6
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
7
|
+
* @license MIT License
|
|
5
8
|
*/
|
|
6
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
10
|
exports.SegmentTree = exports.SegmentTreeNode = void 0;
|
|
@@ -82,6 +85,15 @@ var SegmentTreeNode = /** @class */ (function () {
|
|
|
82
85
|
}());
|
|
83
86
|
exports.SegmentTreeNode = SegmentTreeNode;
|
|
84
87
|
var SegmentTree = /** @class */ (function () {
|
|
88
|
+
/**
|
|
89
|
+
* The constructor initializes the values, start, end, and root properties of an object.
|
|
90
|
+
* @param {number[]} values - An array of numbers that will be used to build a binary search tree.
|
|
91
|
+
* @param {number} [start] - The `start` parameter is the index of the first element in the `values` array that should
|
|
92
|
+
* be included in the range. If no value is provided for `start`, it defaults to 0, which means the range starts from
|
|
93
|
+
* the beginning of the array.
|
|
94
|
+
* @param {number} [end] - The "end" parameter is the index of the last element in the "values" array that should be
|
|
95
|
+
* included in the range. If not provided, it defaults to the index of the last element in the "values" array.
|
|
96
|
+
*/
|
|
85
97
|
function SegmentTree(values, start, end) {
|
|
86
98
|
this._values = [];
|
|
87
99
|
this._start = 0;
|
|
@@ -99,6 +111,15 @@ var SegmentTree = /** @class */ (function () {
|
|
|
99
111
|
enumerable: false,
|
|
100
112
|
configurable: true
|
|
101
113
|
});
|
|
114
|
+
/**
|
|
115
|
+
* The function builds a segment tree by recursively dividing the given range into smaller segments and creating nodes
|
|
116
|
+
* for each segment.
|
|
117
|
+
* @param {number} start - The `start` parameter represents the starting index of the segment or range for which we are
|
|
118
|
+
* building the segment tree.
|
|
119
|
+
* @param {number} end - The `end` parameter represents the ending index of the segment or range for which we are
|
|
120
|
+
* building the segment tree.
|
|
121
|
+
* @returns a SegmentTreeNode object.
|
|
122
|
+
*/
|
|
102
123
|
SegmentTree.prototype.build = function (start, end) {
|
|
103
124
|
if (start === end) {
|
|
104
125
|
return new SegmentTreeNode(start, end, this._values[start]);
|
|
@@ -111,6 +132,17 @@ var SegmentTree = /** @class */ (function () {
|
|
|
111
132
|
cur.right = right;
|
|
112
133
|
return cur;
|
|
113
134
|
};
|
|
135
|
+
/**
|
|
136
|
+
* The function updates the value of a node in a segment tree and recalculates the sum of its children if they exist.
|
|
137
|
+
* @param {number} index - The index parameter represents the index of the node in the segment tree that needs to be
|
|
138
|
+
* updated.
|
|
139
|
+
* @param {number} sum - The `sum` parameter represents the new value that should be assigned to the `sum` property of
|
|
140
|
+
* the `SegmentTreeNode` at the specified `index`.
|
|
141
|
+
* @param {SegmentTreeNodeVal} [val] - The `val` parameter is an optional value that can be assigned to the `val`
|
|
142
|
+
* property of the `SegmentTreeNode` object. It is not currently used in the code, but you can uncomment the line `//
|
|
143
|
+
* cur.val = val;` and pass a value for `val` in the
|
|
144
|
+
* @returns The function does not return anything.
|
|
145
|
+
*/
|
|
114
146
|
SegmentTree.prototype.updateNode = function (index, sum, val) {
|
|
115
147
|
var root = this.root || null;
|
|
116
148
|
if (!root) {
|
|
@@ -139,6 +171,13 @@ var SegmentTree = /** @class */ (function () {
|
|
|
139
171
|
};
|
|
140
172
|
dfs(root, index, sum);
|
|
141
173
|
};
|
|
174
|
+
/**
|
|
175
|
+
* The function `querySumByRange` calculates the sum of values within a given range in a segment tree.
|
|
176
|
+
* @param {number} indexA - The starting index of the range for which you want to calculate the sum.
|
|
177
|
+
* @param {number} indexB - The parameter `indexB` represents the ending index of the range for which you want to
|
|
178
|
+
* calculate the sum.
|
|
179
|
+
* @returns The function `querySumByRange` returns a number.
|
|
180
|
+
*/
|
|
142
181
|
SegmentTree.prototype.querySumByRange = function (indexA, indexB) {
|
|
143
182
|
var root = this.root || null;
|
|
144
183
|
if (!root) {
|
|
@@ -1,11 +1,42 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
4
7
|
*/
|
|
5
8
|
import { BST, BSTNode } from './bst';
|
|
6
9
|
import type { BinaryTreeNodeId, TreeMultiSetDeletedResult } from '../types';
|
|
7
10
|
export declare class TreeMultiSet<T> extends BST<T> {
|
|
11
|
+
/**
|
|
12
|
+
* The function creates a new BSTNode with the given id, value, and count.
|
|
13
|
+
* @param {BinaryTreeNodeId} id - The id parameter is the unique identifier for the binary tree node. It is used to
|
|
14
|
+
* distinguish one node from another in the tree.
|
|
15
|
+
* @param {T} val - The `val` parameter represents the value that will be stored in the binary search tree node.
|
|
16
|
+
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
17
|
+
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
18
|
+
* @returns A new instance of the BSTNode class with the specified id, value, and count (if provided).
|
|
19
|
+
*/
|
|
8
20
|
createNode(id: BinaryTreeNodeId, val: T, count?: number): BSTNode<T>;
|
|
9
|
-
|
|
21
|
+
/**
|
|
22
|
+
* The function overrides the add method of the BinarySearchTree class in TypeScript.
|
|
23
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that you want to add.
|
|
24
|
+
* @param {T | null} val - The `val` parameter represents the value that you want to add to the binary search tree. It
|
|
25
|
+
* can be of type `T` (the generic type) or `null`.
|
|
26
|
+
* @param {number} [count] - The `count` parameter is an optional parameter of type `number`. It represents the number
|
|
27
|
+
* of times the value should be added to the binary search tree. If not provided, the default value is `undefined`.
|
|
28
|
+
* @returns The `add` method is returning a `BSTNode<T>` object or `null`.
|
|
29
|
+
*/
|
|
30
|
+
add(id: BinaryTreeNodeId, val: T | null, count?: number): BSTNode<T> | null;
|
|
31
|
+
/**
|
|
32
|
+
* The function overrides the remove method of the superclass and returns the result of calling the superclass's remove
|
|
33
|
+
* method.
|
|
34
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node that needs to be
|
|
35
|
+
* removed from the tree.
|
|
36
|
+
* @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean value that
|
|
37
|
+
* determines whether to update the left sum of all nodes in the tree after removing a node. If `isUpdateAllLeftSum` is
|
|
38
|
+
* set to `true`, the left sum of all nodes will be recalculated. If it
|
|
39
|
+
* @returns The method is returning an array of TreeMultiSetDeletedResult objects.
|
|
40
|
+
*/
|
|
10
41
|
remove(id: BinaryTreeNodeId, isUpdateAllLeftSum?: boolean): TreeMultiSetDeletedResult<T>[];
|
|
11
42
|
}
|
|
@@ -17,8 +17,11 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.TreeMultiSet = void 0;
|
|
19
19
|
/**
|
|
20
|
-
*
|
|
21
|
-
*
|
|
20
|
+
* data-structure-typed
|
|
21
|
+
*
|
|
22
|
+
* @author Tyler Zeng
|
|
23
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
24
|
+
* @license MIT License
|
|
22
25
|
*/
|
|
23
26
|
var bst_1 = require("./bst");
|
|
24
27
|
var TreeMultiSet = /** @class */ (function (_super) {
|
|
@@ -26,12 +29,40 @@ var TreeMultiSet = /** @class */ (function (_super) {
|
|
|
26
29
|
function TreeMultiSet() {
|
|
27
30
|
return _super !== null && _super.apply(this, arguments) || this;
|
|
28
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* The function creates a new BSTNode with the given id, value, and count.
|
|
34
|
+
* @param {BinaryTreeNodeId} id - The id parameter is the unique identifier for the binary tree node. It is used to
|
|
35
|
+
* distinguish one node from another in the tree.
|
|
36
|
+
* @param {T} val - The `val` parameter represents the value that will be stored in the binary search tree node.
|
|
37
|
+
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
38
|
+
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
39
|
+
* @returns A new instance of the BSTNode class with the specified id, value, and count (if provided).
|
|
40
|
+
*/
|
|
29
41
|
TreeMultiSet.prototype.createNode = function (id, val, count) {
|
|
30
42
|
return new bst_1.BSTNode(id, val, count);
|
|
31
43
|
};
|
|
32
|
-
|
|
33
|
-
|
|
44
|
+
/**
|
|
45
|
+
* The function overrides the add method of the BinarySearchTree class in TypeScript.
|
|
46
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that you want to add.
|
|
47
|
+
* @param {T | null} val - The `val` parameter represents the value that you want to add to the binary search tree. It
|
|
48
|
+
* can be of type `T` (the generic type) or `null`.
|
|
49
|
+
* @param {number} [count] - The `count` parameter is an optional parameter of type `number`. It represents the number
|
|
50
|
+
* of times the value should be added to the binary search tree. If not provided, the default value is `undefined`.
|
|
51
|
+
* @returns The `add` method is returning a `BSTNode<T>` object or `null`.
|
|
52
|
+
*/
|
|
53
|
+
TreeMultiSet.prototype.add = function (id, val, count) {
|
|
54
|
+
return _super.prototype.add.call(this, id, val, count);
|
|
34
55
|
};
|
|
56
|
+
/**
|
|
57
|
+
* The function overrides the remove method of the superclass and returns the result of calling the superclass's remove
|
|
58
|
+
* method.
|
|
59
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node that needs to be
|
|
60
|
+
* removed from the tree.
|
|
61
|
+
* @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean value that
|
|
62
|
+
* determines whether to update the left sum of all nodes in the tree after removing a node. If `isUpdateAllLeftSum` is
|
|
63
|
+
* set to `true`, the left sum of all nodes will be recalculated. If it
|
|
64
|
+
* @returns The method is returning an array of TreeMultiSetDeletedResult objects.
|
|
65
|
+
*/
|
|
35
66
|
TreeMultiSet.prototype.remove = function (id, isUpdateAllLeftSum) {
|
|
36
67
|
return _super.prototype.remove.call(this, id, isUpdateAllLeftSum);
|
|
37
68
|
};
|
|
@@ -7,6 +7,11 @@ export declare class AbstractVertex {
|
|
|
7
7
|
}
|
|
8
8
|
export declare abstract class AbstractEdge {
|
|
9
9
|
static DEFAULT_EDGE_WEIGHT: number;
|
|
10
|
+
/**
|
|
11
|
+
* The function is a protected constructor that initializes the weight and generates a unique hash code for an edge.
|
|
12
|
+
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. If
|
|
13
|
+
* no weight is provided, it will default to the value of `AbstractEdge.DEFAULT_EDGE_WEIGHT`.
|
|
14
|
+
*/
|
|
10
15
|
protected constructor(weight?: number);
|
|
11
16
|
private _weight;
|
|
12
17
|
get weight(): number;
|
|
@@ -38,8 +38,11 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
39
|
exports.AbstractGraph = exports.AbstractEdge = exports.AbstractVertex = void 0;
|
|
40
40
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
41
|
+
* data-structure-typed
|
|
42
|
+
*
|
|
43
|
+
* @author Tyler Zeng
|
|
44
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
45
|
+
* @license MIT License
|
|
43
46
|
*/
|
|
44
47
|
var utils_1 = require("../../utils");
|
|
45
48
|
var priority_queue_1 = require("../priority-queue");
|
|
@@ -61,6 +64,11 @@ var AbstractVertex = /** @class */ (function () {
|
|
|
61
64
|
}());
|
|
62
65
|
exports.AbstractVertex = AbstractVertex;
|
|
63
66
|
var AbstractEdge = /** @class */ (function () {
|
|
67
|
+
/**
|
|
68
|
+
* The function is a protected constructor that initializes the weight and generates a unique hash code for an edge.
|
|
69
|
+
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. If
|
|
70
|
+
* no weight is provided, it will default to the value of `AbstractEdge.DEFAULT_EDGE_WEIGHT`.
|
|
71
|
+
*/
|
|
64
72
|
function AbstractEdge(weight) {
|
|
65
73
|
if (weight === undefined)
|
|
66
74
|
weight = AbstractEdge.DEFAULT_EDGE_WEIGHT;
|
|
@@ -660,7 +668,7 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
660
668
|
finally { if (e_11) throw e_11.error; }
|
|
661
669
|
}
|
|
662
670
|
var heap = new priority_queue_1.PriorityQueue({ comparator: function (a, b) { return a.id - b.id; } });
|
|
663
|
-
heap.
|
|
671
|
+
heap.add({ id: 0, val: srcVertex });
|
|
664
672
|
distMap.set(srcVertex, 0);
|
|
665
673
|
preMap.set(srcVertex, null);
|
|
666
674
|
var getPaths = function (minV) {
|
|
@@ -717,7 +725,7 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
717
725
|
var distSrcToNeighbor = distMap.get(neighbor);
|
|
718
726
|
if (distSrcToNeighbor) {
|
|
719
727
|
if (dist + weight < distSrcToNeighbor) {
|
|
720
|
-
heap.
|
|
728
|
+
heap.add({ id: dist + weight, val: neighbor });
|
|
721
729
|
preMap.set(neighbor, cur);
|
|
722
730
|
distMap.set(neighbor, dist + weight);
|
|
723
731
|
}
|
|
@@ -1,9 +1,23 @@
|
|
|
1
1
|
import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
|
|
2
2
|
import type { IDirectedGraph, VertexId } from '../types';
|
|
3
3
|
export declare class DirectedVertex extends AbstractVertex {
|
|
4
|
+
/**
|
|
5
|
+
* The constructor function initializes an object with a given id.
|
|
6
|
+
* @param {VertexId} id - The `id` parameter is the identifier for the vertex. It is used to uniquely identify the
|
|
7
|
+
* vertex within a graph or network.
|
|
8
|
+
*/
|
|
4
9
|
constructor(id: VertexId);
|
|
5
10
|
}
|
|
6
11
|
export declare class DirectedEdge extends AbstractEdge {
|
|
12
|
+
/**
|
|
13
|
+
* The constructor function initializes the source and destination vertices of an edge, with an optional weight.
|
|
14
|
+
* @param {VertexId} src - The `src` parameter is the source vertex ID. It represents the starting point of an edge in
|
|
15
|
+
* a graph.
|
|
16
|
+
* @param {VertexId} dest - The `dest` parameter is the identifier of the destination vertex. It represents the vertex
|
|
17
|
+
* to which an edge is directed.
|
|
18
|
+
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge
|
|
19
|
+
* between two vertices.
|
|
20
|
+
*/
|
|
7
21
|
constructor(src: VertexId, dest: VertexId, weight?: number);
|
|
8
22
|
private _src;
|
|
9
23
|
get src(): VertexId;
|
|
@@ -123,10 +137,10 @@ export declare class DirectedGraph<V extends DirectedVertex, E extends DirectedE
|
|
|
123
137
|
/**
|
|
124
138
|
* when stored with adjacency list time: O(V+E)
|
|
125
139
|
* when stored with adjacency matrix time: O(V^2)
|
|
126
|
-
* The `topologicalSort` function performs a topological sort on a
|
|
127
|
-
*
|
|
128
|
-
* @returns The
|
|
129
|
-
* the graph. If there is a cycle
|
|
140
|
+
* The `topologicalSort` function performs a topological sort on a graph and returns the sorted vertices in reverse
|
|
141
|
+
* order, or null if the graph contains a cycle.
|
|
142
|
+
* @returns The `topologicalSort()` function returns an array of vertices (`V[]`) in topological order if there is no
|
|
143
|
+
* cycle in the graph. If there is a cycle, it returns `null`.
|
|
130
144
|
*/
|
|
131
145
|
topologicalSort(): V[] | null;
|
|
132
146
|
/**--- end find cycles --- */
|
|
@@ -53,13 +53,21 @@ var __values = (this && this.__values) || function(o) {
|
|
|
53
53
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
54
54
|
exports.DirectedGraph = exports.DirectedEdge = exports.DirectedVertex = void 0;
|
|
55
55
|
/**
|
|
56
|
-
*
|
|
57
|
-
*
|
|
56
|
+
* data-structure-typed
|
|
57
|
+
*
|
|
58
|
+
* @author Tyler Zeng
|
|
59
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
60
|
+
* @license MIT License
|
|
58
61
|
*/
|
|
59
62
|
var utils_1 = require("../../utils");
|
|
60
63
|
var abstract_graph_1 = require("./abstract-graph");
|
|
61
64
|
var DirectedVertex = /** @class */ (function (_super) {
|
|
62
65
|
__extends(DirectedVertex, _super);
|
|
66
|
+
/**
|
|
67
|
+
* The constructor function initializes an object with a given id.
|
|
68
|
+
* @param {VertexId} id - The `id` parameter is the identifier for the vertex. It is used to uniquely identify the
|
|
69
|
+
* vertex within a graph or network.
|
|
70
|
+
*/
|
|
63
71
|
function DirectedVertex(id) {
|
|
64
72
|
return _super.call(this, id) || this;
|
|
65
73
|
}
|
|
@@ -68,6 +76,15 @@ var DirectedVertex = /** @class */ (function (_super) {
|
|
|
68
76
|
exports.DirectedVertex = DirectedVertex;
|
|
69
77
|
var DirectedEdge = /** @class */ (function (_super) {
|
|
70
78
|
__extends(DirectedEdge, _super);
|
|
79
|
+
/**
|
|
80
|
+
* The constructor function initializes the source and destination vertices of an edge, with an optional weight.
|
|
81
|
+
* @param {VertexId} src - The `src` parameter is the source vertex ID. It represents the starting point of an edge in
|
|
82
|
+
* a graph.
|
|
83
|
+
* @param {VertexId} dest - The `dest` parameter is the identifier of the destination vertex. It represents the vertex
|
|
84
|
+
* to which an edge is directed.
|
|
85
|
+
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge
|
|
86
|
+
* between two vertices.
|
|
87
|
+
*/
|
|
71
88
|
function DirectedEdge(src, dest, weight) {
|
|
72
89
|
var _this = _super.call(this, weight) || this;
|
|
73
90
|
_this._src = src;
|
|
@@ -342,43 +359,14 @@ var DirectedGraph = /** @class */ (function (_super) {
|
|
|
342
359
|
/**
|
|
343
360
|
* when stored with adjacency list time: O(V+E)
|
|
344
361
|
* when stored with adjacency matrix time: O(V^2)
|
|
345
|
-
* The `topologicalSort` function performs a topological sort on a
|
|
346
|
-
*
|
|
347
|
-
* @returns The
|
|
348
|
-
* the graph. If there is a cycle
|
|
362
|
+
* The `topologicalSort` function performs a topological sort on a graph and returns the sorted vertices in reverse
|
|
363
|
+
* order, or null if the graph contains a cycle.
|
|
364
|
+
* @returns The `topologicalSort()` function returns an array of vertices (`V[]`) in topological order if there is no
|
|
365
|
+
* cycle in the graph. If there is a cycle, it returns `null`.
|
|
349
366
|
*/
|
|
350
367
|
DirectedGraph.prototype.topologicalSort = function () {
|
|
351
368
|
var e_2, _a, e_3, _b;
|
|
352
369
|
var _this = this;
|
|
353
|
-
// vector<vector<int>> g;
|
|
354
|
-
// vector<int> color;
|
|
355
|
-
// int last;
|
|
356
|
-
// bool hasCycle;
|
|
357
|
-
//
|
|
358
|
-
// bool topo_sort() {
|
|
359
|
-
// int n = g.size();
|
|
360
|
-
// vector<int> degree(n, 0);
|
|
361
|
-
// queue<int> q;
|
|
362
|
-
// for (int i = 0; i < n; i++) {
|
|
363
|
-
// degree[i] = g[i].size();
|
|
364
|
-
// if (degree[i] <= 1) {
|
|
365
|
-
// q.push(i);
|
|
366
|
-
// }
|
|
367
|
-
// }
|
|
368
|
-
// int cnt = 0;
|
|
369
|
-
// while (!q.empty()) {
|
|
370
|
-
// cnt++;
|
|
371
|
-
// int root = q.front();
|
|
372
|
-
// q.pop();
|
|
373
|
-
// for (auto child : g[root]) {
|
|
374
|
-
// degree[child]--;
|
|
375
|
-
// if (degree[child] == 1) {
|
|
376
|
-
// q.push(child);
|
|
377
|
-
// }
|
|
378
|
-
// }
|
|
379
|
-
// }
|
|
380
|
-
// return (cnt != n);
|
|
381
|
-
// }
|
|
382
370
|
// When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
|
|
383
371
|
// When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
|
|
384
372
|
var statusMap = new Map();
|
|
@@ -438,9 +426,8 @@ var DirectedGraph = /** @class */ (function (_super) {
|
|
|
438
426
|
}
|
|
439
427
|
finally { if (e_3) throw e_3.error; }
|
|
440
428
|
}
|
|
441
|
-
if (hasCycle)
|
|
429
|
+
if (hasCycle)
|
|
442
430
|
return null;
|
|
443
|
-
}
|
|
444
431
|
return sorted.reverse();
|
|
445
432
|
};
|
|
446
433
|
/**--- end find cycles --- */
|
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
|
|
2
2
|
import type { VertexId } from '../types';
|
|
3
3
|
export declare class UndirectedVertex extends AbstractVertex {
|
|
4
|
+
/**
|
|
5
|
+
* The constructor function initializes an object with a given id.
|
|
6
|
+
* @param {VertexId} id - The `id` parameter is the identifier for the vertex. It is used to uniquely identify the
|
|
7
|
+
* vertex within a graph or network.
|
|
8
|
+
*/
|
|
4
9
|
constructor(id: VertexId);
|
|
5
10
|
}
|
|
6
11
|
export declare class UndirectedEdge extends AbstractEdge {
|
|
12
|
+
/**
|
|
13
|
+
* The constructor function initializes an instance of a class with two vertex IDs and an optional weight.
|
|
14
|
+
* @param {VertexId} v1 - The parameter `v1` is of type `VertexId` and represents the first vertex in the edge.
|
|
15
|
+
* @param {VertexId} v2 - The parameter `v2` is a `VertexId`, which represents the identifier of the second vertex in a
|
|
16
|
+
* graph.
|
|
17
|
+
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge
|
|
18
|
+
* between two vertices.
|
|
19
|
+
*/
|
|
7
20
|
constructor(v1: VertexId, v2: VertexId, weight?: number);
|
|
8
21
|
private _vertices;
|
|
9
22
|
get vertices(): [VertexId, VertexId];
|