data-structure-typed 1.49.4 → 1.49.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/CHANGELOG.md +1 -1
- package/README.md +15 -26
- package/README_zh-CN.md +1 -1
- package/benchmark/report.html +14 -23
- package/benchmark/report.json +163 -256
- package/dist/cjs/data-structures/base/iterable-base.d.ts +1 -1
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +154 -143
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +211 -198
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/cjs/data-structures/binary-tree/bst.js +113 -89
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +46 -55
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +59 -94
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +3 -2
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/cjs/data-structures/hash/hash-map.js +2 -2
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.js +2 -3
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/index.d.ts +0 -2
- package/dist/cjs/data-structures/matrix/index.js +0 -2
- package/dist/cjs/data-structures/matrix/index.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.d.ts +128 -10
- package/dist/cjs/data-structures/matrix/matrix.js +400 -15
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +2 -2
- package/dist/cjs/data-structures/queue/deque.js +5 -7
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/cjs/types/common.d.ts +3 -3
- package/dist/cjs/types/common.js +2 -2
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/cjs/types/data-structures/base/base.d.ts +1 -1
- package/dist/cjs/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/cjs/utils/utils.d.ts +1 -0
- package/dist/cjs/utils/utils.js +6 -1
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/base/iterable-base.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +57 -50
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +154 -143
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +212 -199
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/mjs/data-structures/binary-tree/bst.js +115 -90
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +64 -60
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +46 -55
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +61 -95
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -1
- package/dist/mjs/data-structures/graph/abstract-graph.js +4 -3
- package/dist/mjs/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/mjs/data-structures/hash/hash-map.js +2 -2
- package/dist/mjs/data-structures/heap/heap.js +2 -3
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +1 -1
- package/dist/mjs/data-structures/matrix/index.d.ts +0 -2
- package/dist/mjs/data-structures/matrix/index.js +0 -2
- package/dist/mjs/data-structures/matrix/matrix.d.ts +128 -10
- package/dist/mjs/data-structures/matrix/matrix.js +399 -16
- package/dist/mjs/data-structures/queue/deque.d.ts +2 -2
- package/dist/mjs/data-structures/queue/deque.js +7 -9
- package/dist/mjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/mjs/data-structures/queue/queue.js +1 -1
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/mjs/types/common.d.ts +3 -3
- package/dist/mjs/types/common.js +2 -2
- package/dist/mjs/types/data-structures/base/base.d.ts +1 -1
- package/dist/mjs/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/mjs/utils/utils.d.ts +1 -0
- package/dist/mjs/utils/utils.js +4 -0
- package/dist/umd/data-structure-typed.js +853 -943
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +4 -3
- package/src/data-structures/base/index.ts +1 -1
- package/src/data-structures/base/iterable-base.ts +7 -10
- package/src/data-structures/binary-tree/avl-tree.ts +73 -61
- package/src/data-structures/binary-tree/binary-tree.ts +301 -270
- package/src/data-structures/binary-tree/bst.ts +139 -115
- package/src/data-structures/binary-tree/rb-tree.ts +81 -73
- package/src/data-structures/binary-tree/tree-multimap.ts +72 -103
- package/src/data-structures/graph/abstract-graph.ts +13 -11
- package/src/data-structures/graph/directed-graph.ts +1 -3
- package/src/data-structures/graph/map-graph.ts +6 -1
- package/src/data-structures/graph/undirected-graph.ts +3 -6
- package/src/data-structures/hash/hash-map.ts +18 -16
- package/src/data-structures/heap/heap.ts +7 -10
- package/src/data-structures/heap/max-heap.ts +2 -1
- package/src/data-structures/heap/min-heap.ts +2 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -3
- package/src/data-structures/matrix/index.ts +0 -2
- package/src/data-structures/matrix/matrix.ts +442 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -10
- package/src/data-structures/queue/deque.ts +18 -39
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/interfaces/binary-tree.ts +9 -4
- package/src/types/common.ts +5 -5
- package/src/types/data-structures/base/base.ts +14 -3
- package/src/types/data-structures/base/index.ts +1 -1
- package/src/types/data-structures/graph/abstract-graph.ts +4 -2
- package/src/types/data-structures/hash/hash-map.ts +3 -3
- package/src/types/data-structures/heap/heap.ts +2 -2
- package/src/types/data-structures/priority-queue/priority-queue.ts +2 -2
- package/src/utils/utils.ts +7 -1
- package/test/integration/avl-tree.test.ts +18 -1
- package/test/integration/bst.test.ts +2 -2
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +4 -12
- package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +37 -0
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +6 -16
- package/test/performance/data-structures/binary-tree/bst.test.ts +5 -13
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +6 -16
- package/test/performance/data-structures/comparison/comparison.test.ts +19 -47
- package/test/performance/data-structures/graph/directed-graph.test.ts +3 -14
- package/test/performance/data-structures/hash/hash-map.test.ts +14 -38
- package/test/performance/data-structures/heap/heap.test.ts +5 -18
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +0 -2
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +0 -2
- package/test/performance/data-structures/priority-queue/max-priority-queue.test.ts +2 -4
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +4 -14
- package/test/performance/data-structures/queue/deque.test.ts +6 -7
- package/test/performance/data-structures/queue/queue.test.ts +13 -29
- package/test/performance/data-structures/stack/stack.test.ts +6 -18
- package/test/performance/data-structures/trie/trie.test.ts +2 -6
- package/test/performance/reportor.ts +14 -15
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +14 -3
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +65 -29
- package/test/unit/data-structures/binary-tree/bst.test.ts +69 -27
- package/test/unit/data-structures/binary-tree/overall.test.ts +16 -18
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +17 -6
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -21
- package/test/unit/data-structures/graph/directed-graph.test.ts +104 -45
- package/test/unit/data-structures/graph/undirected-graph.test.ts +57 -40
- package/test/unit/data-structures/hash/hash-map.test.ts +48 -28
- package/test/unit/data-structures/hash/hash-table.test.ts +0 -4
- package/test/unit/data-structures/heap/heap.test.ts +0 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +6 -5
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +10 -11
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +3 -4
- package/test/unit/data-structures/matrix/matrix.test.ts +345 -52
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +0 -1
- package/test/unit/data-structures/queue/deque.test.ts +0 -6
- package/test/unit/data-structures/queue/queue.test.ts +1 -1
- package/test/unit/data-structures/stack/stack.test.ts +3 -4
- package/test/unit/unrestricted-interconversion.test.ts +50 -44
- package/test/utils/performanc.ts +1 -1
- package/typedoc.json +30 -0
- package/dist/cjs/data-structures/matrix/matrix2d.d.ts +0 -107
- package/dist/cjs/data-structures/matrix/matrix2d.js +0 -200
- package/dist/cjs/data-structures/matrix/matrix2d.js.map +0 -1
- package/dist/cjs/data-structures/matrix/vector2d.d.ts +0 -200
- package/dist/cjs/data-structures/matrix/vector2d.js +0 -291
- package/dist/cjs/data-structures/matrix/vector2d.js.map +0 -1
- package/dist/mjs/data-structures/matrix/matrix2d.d.ts +0 -107
- package/dist/mjs/data-structures/matrix/matrix2d.js +0 -196
- package/dist/mjs/data-structures/matrix/vector2d.d.ts +0 -200
- package/dist/mjs/data-structures/matrix/vector2d.js +0 -289
- package/src/data-structures/matrix/matrix2d.ts +0 -211
- package/src/data-structures/matrix/vector2d.ts +0 -315
- package/test/performance/data-structures/binary-tree/overall.test.ts +0 -0
- package/test/performance/data-structures/matrix/matrix2d.test.ts +0 -0
- package/test/performance/data-structures/matrix/vector2d.test.ts +0 -0
- package/test/unit/data-structures/matrix/matrix2d.test.ts +0 -345
- package/test/unit/data-structures/matrix/vector2d.test.ts +0 -171
|
@@ -135,8 +135,7 @@ var dataStructureTyped = (() => {
|
|
|
135
135
|
MapEdge: () => MapEdge,
|
|
136
136
|
MapGraph: () => MapGraph,
|
|
137
137
|
MapVertex: () => MapVertex,
|
|
138
|
-
|
|
139
|
-
MatrixNTI2D: () => MatrixNTI2D,
|
|
138
|
+
Matrix: () => Matrix,
|
|
140
139
|
MaxHeap: () => MaxHeap,
|
|
141
140
|
MaxPriorityQueue: () => MaxPriorityQueue,
|
|
142
141
|
MinHeap: () => MinHeap,
|
|
@@ -163,13 +162,13 @@ var dataStructureTyped = (() => {
|
|
|
163
162
|
UndirectedEdge: () => UndirectedEdge,
|
|
164
163
|
UndirectedGraph: () => UndirectedGraph,
|
|
165
164
|
UndirectedVertex: () => UndirectedVertex,
|
|
166
|
-
Vector2D: () => Vector2D,
|
|
167
165
|
arrayRemove: () => arrayRemove,
|
|
168
166
|
calcMinUnitsRequired: () => calcMinUnitsRequired,
|
|
169
167
|
getMSB: () => getMSB,
|
|
170
168
|
isThunk: () => isThunk,
|
|
171
169
|
isWeakKey: () => isWeakKey,
|
|
172
170
|
rangeCheck: () => rangeCheck,
|
|
171
|
+
roundFixed: () => roundFixed,
|
|
173
172
|
throwRangeError: () => throwRangeError,
|
|
174
173
|
toThunk: () => toThunk,
|
|
175
174
|
trampoline: () => trampoline,
|
|
@@ -847,6 +846,10 @@ var dataStructureTyped = (() => {
|
|
|
847
846
|
return inputType === "object" && input !== null || inputType === "function";
|
|
848
847
|
};
|
|
849
848
|
var calcMinUnitsRequired = (totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize);
|
|
849
|
+
var roundFixed = (num, digit = 10) => {
|
|
850
|
+
const multiplier = Math.pow(10, digit);
|
|
851
|
+
return Math.round(num * multiplier) / multiplier;
|
|
852
|
+
};
|
|
850
853
|
|
|
851
854
|
// src/data-structures/hash/hash-map.ts
|
|
852
855
|
var HashMap = class _HashMap extends IterableEntryBase {
|
|
@@ -3859,10 +3862,7 @@ var dataStructureTyped = (() => {
|
|
|
3859
3862
|
*/
|
|
3860
3863
|
getAt(pos) {
|
|
3861
3864
|
rangeCheck(pos, 0, this.size - 1);
|
|
3862
|
-
const {
|
|
3863
|
-
bucketIndex,
|
|
3864
|
-
indexInBucket
|
|
3865
|
-
} = this._getBucketAndPosition(pos);
|
|
3865
|
+
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
3866
3866
|
return this._buckets[bucketIndex][indexInBucket];
|
|
3867
3867
|
}
|
|
3868
3868
|
/**
|
|
@@ -3881,10 +3881,7 @@ var dataStructureTyped = (() => {
|
|
|
3881
3881
|
*/
|
|
3882
3882
|
setAt(pos, element) {
|
|
3883
3883
|
rangeCheck(pos, 0, this.size - 1);
|
|
3884
|
-
const {
|
|
3885
|
-
bucketIndex,
|
|
3886
|
-
indexInBucket
|
|
3887
|
-
} = this._getBucketAndPosition(pos);
|
|
3884
|
+
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
3888
3885
|
this._buckets[bucketIndex][indexInBucket] = element;
|
|
3889
3886
|
return true;
|
|
3890
3887
|
}
|
|
@@ -3948,10 +3945,7 @@ var dataStructureTyped = (() => {
|
|
|
3948
3945
|
this.clear();
|
|
3949
3946
|
return 0;
|
|
3950
3947
|
}
|
|
3951
|
-
const {
|
|
3952
|
-
bucketIndex,
|
|
3953
|
-
indexInBucket
|
|
3954
|
-
} = this._getBucketAndPosition(pos);
|
|
3948
|
+
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
3955
3949
|
this._bucketLast = bucketIndex;
|
|
3956
3950
|
this._lastInBucket = indexInBucket;
|
|
3957
3951
|
this._size = pos + 1;
|
|
@@ -3980,15 +3974,9 @@ var dataStructureTyped = (() => {
|
|
|
3980
3974
|
this.pop();
|
|
3981
3975
|
else {
|
|
3982
3976
|
const length = this.size - 1;
|
|
3983
|
-
let {
|
|
3984
|
-
bucketIndex: curBucket,
|
|
3985
|
-
indexInBucket: curPointer
|
|
3986
|
-
} = this._getBucketAndPosition(pos);
|
|
3977
|
+
let { bucketIndex: curBucket, indexInBucket: curPointer } = this._getBucketAndPosition(pos);
|
|
3987
3978
|
for (let i = pos; i < length; ++i) {
|
|
3988
|
-
const {
|
|
3989
|
-
bucketIndex: nextBucket,
|
|
3990
|
-
indexInBucket: nextPointer
|
|
3991
|
-
} = this._getBucketAndPosition(pos + 1);
|
|
3979
|
+
const { bucketIndex: nextBucket, indexInBucket: nextPointer } = this._getBucketAndPosition(pos + 1);
|
|
3992
3980
|
this._buckets[curBucket][curPointer] = this._buckets[nextBucket][nextPointer];
|
|
3993
3981
|
curBucket = nextBucket;
|
|
3994
3982
|
curPointer = nextPointer;
|
|
@@ -6170,9 +6158,7 @@ var dataStructureTyped = (() => {
|
|
|
6170
6158
|
uniqueCycles.set(sorted, cycle);
|
|
6171
6159
|
}
|
|
6172
6160
|
}
|
|
6173
|
-
return [...uniqueCycles].map(
|
|
6174
|
-
(cycleString) => cycleString[1]
|
|
6175
|
-
);
|
|
6161
|
+
return [...uniqueCycles].map((cycleString) => cycleString[1]);
|
|
6176
6162
|
}
|
|
6177
6163
|
/**
|
|
6178
6164
|
* Time Complexity: O(n)
|
|
@@ -7255,8 +7241,8 @@ var dataStructureTyped = (() => {
|
|
|
7255
7241
|
|
|
7256
7242
|
// src/types/common.ts
|
|
7257
7243
|
var BSTVariant = /* @__PURE__ */ ((BSTVariant2) => {
|
|
7258
|
-
BSTVariant2["
|
|
7259
|
-
BSTVariant2["
|
|
7244
|
+
BSTVariant2["STANDARD"] = "STANDARD";
|
|
7245
|
+
BSTVariant2["INVERSE"] = "INVERSE";
|
|
7260
7246
|
return BSTVariant2;
|
|
7261
7247
|
})(BSTVariant || {});
|
|
7262
7248
|
var CP = /* @__PURE__ */ ((CP2) => {
|
|
@@ -7329,15 +7315,15 @@ var dataStructureTyped = (() => {
|
|
|
7329
7315
|
};
|
|
7330
7316
|
var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
7331
7317
|
/**
|
|
7332
|
-
* The constructor function initializes a binary tree object with optional
|
|
7333
|
-
* @param [
|
|
7334
|
-
*
|
|
7318
|
+
* The constructor function initializes a binary tree object with optional nodes and options.
|
|
7319
|
+
* @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the
|
|
7320
|
+
* nodes to be added to the binary tree.
|
|
7335
7321
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
7336
7322
|
* configuration options for the binary tree. In this case, it is of type
|
|
7337
7323
|
* `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
|
|
7338
7324
|
* required.
|
|
7339
7325
|
*/
|
|
7340
|
-
constructor(
|
|
7326
|
+
constructor(nodes, options) {
|
|
7341
7327
|
super();
|
|
7342
7328
|
__publicField(this, "iterationType", "ITERATIVE" /* ITERATIVE */);
|
|
7343
7329
|
__publicField(this, "_extractor", (key) => Number(key));
|
|
@@ -7354,8 +7340,8 @@ var dataStructureTyped = (() => {
|
|
|
7354
7340
|
}
|
|
7355
7341
|
}
|
|
7356
7342
|
this._size = 0;
|
|
7357
|
-
if (
|
|
7358
|
-
this.addMany(
|
|
7343
|
+
if (nodes)
|
|
7344
|
+
this.addMany(nodes);
|
|
7359
7345
|
}
|
|
7360
7346
|
get extractor() {
|
|
7361
7347
|
return this._extractor;
|
|
@@ -7386,29 +7372,21 @@ var dataStructureTyped = (() => {
|
|
|
7386
7372
|
return new _BinaryTree([], __spreadValues({ iterationType: this.iterationType }, options));
|
|
7387
7373
|
}
|
|
7388
7374
|
/**
|
|
7389
|
-
* The function
|
|
7390
|
-
* @param
|
|
7391
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the class N.
|
|
7392
|
-
*/
|
|
7393
|
-
isNode(exemplar) {
|
|
7394
|
-
return exemplar instanceof BinaryTreeNode;
|
|
7395
|
-
}
|
|
7396
|
-
/**
|
|
7397
|
-
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
7398
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
|
|
7375
|
+
* The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
|
|
7376
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
7399
7377
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
7400
|
-
* `exemplarToNode` function. It represents the value associated with the
|
|
7378
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If no value
|
|
7401
7379
|
* is provided, it will be `undefined`.
|
|
7402
7380
|
* @returns a value of type N (node), or null, or undefined.
|
|
7403
7381
|
*/
|
|
7404
|
-
exemplarToNode(
|
|
7405
|
-
if (
|
|
7382
|
+
exemplarToNode(keyOrNodeOrEntry, value) {
|
|
7383
|
+
if (keyOrNodeOrEntry === void 0)
|
|
7406
7384
|
return;
|
|
7407
7385
|
let node;
|
|
7408
|
-
if (
|
|
7386
|
+
if (keyOrNodeOrEntry === null) {
|
|
7409
7387
|
node = null;
|
|
7410
|
-
} else if (this.isEntry(
|
|
7411
|
-
const [key, value2] =
|
|
7388
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
7389
|
+
const [key, value2] = keyOrNodeOrEntry;
|
|
7412
7390
|
if (key === void 0) {
|
|
7413
7391
|
return;
|
|
7414
7392
|
} else if (key === null) {
|
|
@@ -7416,23 +7394,104 @@ var dataStructureTyped = (() => {
|
|
|
7416
7394
|
} else {
|
|
7417
7395
|
node = this.createNode(key, value2);
|
|
7418
7396
|
}
|
|
7419
|
-
} else if (this.isNode(
|
|
7420
|
-
node =
|
|
7421
|
-
} else if (this.isNotNodeInstance(
|
|
7422
|
-
node = this.createNode(
|
|
7397
|
+
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
7398
|
+
node = keyOrNodeOrEntry;
|
|
7399
|
+
} else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
7400
|
+
node = this.createNode(keyOrNodeOrEntry, value);
|
|
7423
7401
|
} else {
|
|
7424
7402
|
return;
|
|
7425
7403
|
}
|
|
7426
7404
|
return node;
|
|
7427
7405
|
}
|
|
7406
|
+
/**
|
|
7407
|
+
* Time Complexity: O(n)
|
|
7408
|
+
* Space Complexity: O(log n)
|
|
7409
|
+
*/
|
|
7410
|
+
/**
|
|
7411
|
+
* Time Complexity: O(n)
|
|
7412
|
+
* Space Complexity: O(log n)
|
|
7413
|
+
*
|
|
7414
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a valid node
|
|
7415
|
+
* key, otherwise it returns the key itself.
|
|
7416
|
+
* @param {K | N | null | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`,
|
|
7417
|
+
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
|
|
7418
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
7419
|
+
* type of iteration to be used when searching for a node by key. It has a default value of
|
|
7420
|
+
* `IterationType.ITERATIVE`.
|
|
7421
|
+
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
|
7422
|
+
* itself if it is not a valid node key.
|
|
7423
|
+
*/
|
|
7424
|
+
ensureNode(keyOrNodeOrEntry, iterationType = "ITERATIVE" /* ITERATIVE */) {
|
|
7425
|
+
let res;
|
|
7426
|
+
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
7427
|
+
res = keyOrNodeOrEntry;
|
|
7428
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
7429
|
+
if (keyOrNodeOrEntry[0] === null)
|
|
7430
|
+
res = null;
|
|
7431
|
+
else if (keyOrNodeOrEntry[0] !== void 0)
|
|
7432
|
+
res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
7433
|
+
} else {
|
|
7434
|
+
if (keyOrNodeOrEntry === null)
|
|
7435
|
+
res = null;
|
|
7436
|
+
else if (keyOrNodeOrEntry !== void 0)
|
|
7437
|
+
res = this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
7438
|
+
}
|
|
7439
|
+
return res;
|
|
7440
|
+
}
|
|
7441
|
+
/**
|
|
7442
|
+
* The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
|
|
7443
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,N>`.
|
|
7444
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the class N.
|
|
7445
|
+
*/
|
|
7446
|
+
isNode(keyOrNodeOrEntry) {
|
|
7447
|
+
return keyOrNodeOrEntry instanceof BinaryTreeNode;
|
|
7448
|
+
}
|
|
7428
7449
|
/**
|
|
7429
7450
|
* The function checks if a given value is an entry in a binary tree node.
|
|
7430
|
-
* @param
|
|
7451
|
+
* @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,N> - A generic type representing a node in a binary tree. It has
|
|
7431
7452
|
* two type parameters V and N, representing the value and node type respectively.
|
|
7432
7453
|
* @returns a boolean value.
|
|
7433
7454
|
*/
|
|
7434
|
-
isEntry(
|
|
7435
|
-
return Array.isArray(
|
|
7455
|
+
isEntry(keyOrNodeOrEntry) {
|
|
7456
|
+
return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
|
|
7457
|
+
}
|
|
7458
|
+
/**
|
|
7459
|
+
* Time complexity: O(n)
|
|
7460
|
+
* Space complexity: O(log n)
|
|
7461
|
+
*/
|
|
7462
|
+
/**
|
|
7463
|
+
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
7464
|
+
* BinaryTreeNode and its key is not NaN.
|
|
7465
|
+
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
7466
|
+
* @returns a boolean value.
|
|
7467
|
+
*/
|
|
7468
|
+
isRealNode(node) {
|
|
7469
|
+
return node instanceof BinaryTreeNode && String(node.key) !== "NaN";
|
|
7470
|
+
}
|
|
7471
|
+
/**
|
|
7472
|
+
* The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
|
|
7473
|
+
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
7474
|
+
* @returns a boolean value.
|
|
7475
|
+
*/
|
|
7476
|
+
isNIL(node) {
|
|
7477
|
+
return node instanceof BinaryTreeNode && String(node.key) === "NaN";
|
|
7478
|
+
}
|
|
7479
|
+
/**
|
|
7480
|
+
* The function checks if a given node is a real node or null.
|
|
7481
|
+
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
7482
|
+
* @returns a boolean value.
|
|
7483
|
+
*/
|
|
7484
|
+
isNodeOrNull(node) {
|
|
7485
|
+
return this.isRealNode(node) || node === null;
|
|
7486
|
+
}
|
|
7487
|
+
/**
|
|
7488
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
7489
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
7490
|
+
* data type.
|
|
7491
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
7492
|
+
*/
|
|
7493
|
+
isNotNodeInstance(potentialKey) {
|
|
7494
|
+
return !(potentialKey instanceof BinaryTreeNode);
|
|
7436
7495
|
}
|
|
7437
7496
|
/**
|
|
7438
7497
|
* Time Complexity O(log n) - O(n)
|
|
@@ -7451,11 +7510,11 @@ var dataStructureTyped = (() => {
|
|
|
7451
7510
|
add(keyOrNodeOrEntry, value) {
|
|
7452
7511
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
7453
7512
|
if (newNode === void 0)
|
|
7454
|
-
return;
|
|
7513
|
+
return false;
|
|
7455
7514
|
if (!this.root) {
|
|
7456
7515
|
this._root = newNode;
|
|
7457
7516
|
this._size = 1;
|
|
7458
|
-
return
|
|
7517
|
+
return true;
|
|
7459
7518
|
}
|
|
7460
7519
|
const queue = new Queue([this.root]);
|
|
7461
7520
|
let potentialParent;
|
|
@@ -7465,7 +7524,7 @@ var dataStructureTyped = (() => {
|
|
|
7465
7524
|
continue;
|
|
7466
7525
|
if (newNode !== null && cur.key === newNode.key) {
|
|
7467
7526
|
this._replaceNode(cur, newNode);
|
|
7468
|
-
return
|
|
7527
|
+
return true;
|
|
7469
7528
|
}
|
|
7470
7529
|
if (potentialParent === void 0 && (cur.left === void 0 || cur.right === void 0)) {
|
|
7471
7530
|
potentialParent = cur;
|
|
@@ -7484,9 +7543,9 @@ var dataStructureTyped = (() => {
|
|
|
7484
7543
|
potentialParent.right = newNode;
|
|
7485
7544
|
}
|
|
7486
7545
|
this._size++;
|
|
7487
|
-
return
|
|
7546
|
+
return true;
|
|
7488
7547
|
}
|
|
7489
|
-
return
|
|
7548
|
+
return false;
|
|
7490
7549
|
}
|
|
7491
7550
|
/**
|
|
7492
7551
|
* Time Complexity: O(k log n) - O(k * n)
|
|
@@ -7497,19 +7556,19 @@ var dataStructureTyped = (() => {
|
|
|
7497
7556
|
* Time Complexity: O(k log n) - O(k * n)
|
|
7498
7557
|
* Space Complexity: O(1)
|
|
7499
7558
|
*
|
|
7500
|
-
* The `addMany` function takes in a collection of
|
|
7559
|
+
* The `addMany` function takes in a collection of keysOrNodesOrEntries and an optional collection of values, and
|
|
7501
7560
|
* adds each node with its corresponding value to the data structure.
|
|
7502
|
-
* @param
|
|
7561
|
+
* @param keysOrNodesOrEntries - An iterable collection of KeyOrNodeOrEntry objects.
|
|
7503
7562
|
* @param [values] - An optional iterable of values that will be assigned to each node being added.
|
|
7504
7563
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
7505
7564
|
*/
|
|
7506
|
-
addMany(
|
|
7565
|
+
addMany(keysOrNodesOrEntries, values) {
|
|
7507
7566
|
const inserted = [];
|
|
7508
7567
|
let valuesIterator;
|
|
7509
7568
|
if (values) {
|
|
7510
7569
|
valuesIterator = values[Symbol.iterator]();
|
|
7511
7570
|
}
|
|
7512
|
-
for (const
|
|
7571
|
+
for (const keyOrNodeOrEntry of keysOrNodesOrEntries) {
|
|
7513
7572
|
let value = void 0;
|
|
7514
7573
|
if (valuesIterator) {
|
|
7515
7574
|
const valueResult = valuesIterator.next();
|
|
@@ -7517,17 +7576,30 @@ var dataStructureTyped = (() => {
|
|
|
7517
7576
|
value = valueResult.value;
|
|
7518
7577
|
}
|
|
7519
7578
|
}
|
|
7520
|
-
inserted.push(this.add(
|
|
7579
|
+
inserted.push(this.add(keyOrNodeOrEntry, value));
|
|
7521
7580
|
}
|
|
7522
7581
|
return inserted;
|
|
7523
7582
|
}
|
|
7524
7583
|
/**
|
|
7525
|
-
* Time Complexity: O(k * n)
|
|
7584
|
+
* Time Complexity: O(k * n)
|
|
7585
|
+
* Space Complexity: O(1)
|
|
7586
|
+
* "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
7587
|
+
*/
|
|
7588
|
+
/**
|
|
7589
|
+
* Time Complexity: O(k * n)
|
|
7526
7590
|
* Space Complexity: O(1)
|
|
7591
|
+
*
|
|
7592
|
+
* The `refill` function clears the current data and adds new key-value pairs to the data structure.
|
|
7593
|
+
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries. These can be of type
|
|
7594
|
+
* KeyOrNodeOrEntry<K, V, N>.
|
|
7595
|
+
* @param [values] - The `values` parameter is an optional iterable that contains the values to be
|
|
7596
|
+
* associated with the keys or nodes or entries in the `keysOrNodesOrEntries` parameter. If provided,
|
|
7597
|
+
* the values will be associated with the corresponding keys or nodes or entries in the
|
|
7598
|
+
* `keysOrNodesOrEntries` iterable
|
|
7527
7599
|
*/
|
|
7528
|
-
refill(
|
|
7600
|
+
refill(keysOrNodesOrEntries, values) {
|
|
7529
7601
|
this.clear();
|
|
7530
|
-
this.addMany(
|
|
7602
|
+
this.addMany(keysOrNodesOrEntries, values);
|
|
7531
7603
|
}
|
|
7532
7604
|
/**
|
|
7533
7605
|
* Time Complexity: O(n)
|
|
@@ -7597,24 +7669,24 @@ var dataStructureTyped = (() => {
|
|
|
7597
7669
|
* Space Complexity: O(1)
|
|
7598
7670
|
*
|
|
7599
7671
|
* The function calculates the depth of a given node in a binary tree.
|
|
7600
|
-
* @param {K | N | null | undefined}
|
|
7672
|
+
* @param {K | N | null | undefined} dist - The `dist` parameter represents the node in
|
|
7601
7673
|
* the binary tree whose depth we want to find. It can be of type `K`, `N`, `null`, or
|
|
7602
7674
|
* `undefined`.
|
|
7603
7675
|
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
7604
7676
|
* from which we want to calculate the depth. It can be either a `K` (binary tree node key) or
|
|
7605
7677
|
* `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
|
|
7606
|
-
* @returns the depth of the `
|
|
7678
|
+
* @returns the depth of the `dist` relative to the `beginRoot`.
|
|
7607
7679
|
*/
|
|
7608
|
-
getDepth(
|
|
7609
|
-
|
|
7680
|
+
getDepth(dist, beginRoot = this.root) {
|
|
7681
|
+
dist = this.ensureNode(dist);
|
|
7610
7682
|
beginRoot = this.ensureNode(beginRoot);
|
|
7611
7683
|
let depth = 0;
|
|
7612
|
-
while (
|
|
7613
|
-
if (
|
|
7684
|
+
while (dist == null ? void 0 : dist.parent) {
|
|
7685
|
+
if (dist === beginRoot) {
|
|
7614
7686
|
return depth;
|
|
7615
7687
|
}
|
|
7616
7688
|
depth++;
|
|
7617
|
-
|
|
7689
|
+
dist = dist.parent;
|
|
7618
7690
|
}
|
|
7619
7691
|
return depth;
|
|
7620
7692
|
}
|
|
@@ -7806,6 +7878,7 @@ var dataStructureTyped = (() => {
|
|
|
7806
7878
|
}
|
|
7807
7879
|
/**
|
|
7808
7880
|
* Time Complexity: O(n)
|
|
7881
|
+
* Space Complexity: O(log n).
|
|
7809
7882
|
*
|
|
7810
7883
|
* The function checks if a Binary Tree Node with a specific identifier exists in the tree.
|
|
7811
7884
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
@@ -7902,24 +7975,6 @@ var dataStructureTyped = (() => {
|
|
|
7902
7975
|
}
|
|
7903
7976
|
}
|
|
7904
7977
|
}
|
|
7905
|
-
/**
|
|
7906
|
-
* Time Complexity: O(n)
|
|
7907
|
-
* Space Complexity: O(log n)
|
|
7908
|
-
*/
|
|
7909
|
-
/**
|
|
7910
|
-
* The function `ensureNode` returns the node corresponding to the given key if it is a valid node
|
|
7911
|
-
* key, otherwise it returns the key itself.
|
|
7912
|
-
* @param {K | N | null | undefined} key - The `key` parameter can be of type `K`, `N`,
|
|
7913
|
-
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
|
|
7914
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
7915
|
-
* type of iteration to be used when searching for a node by key. It has a default value of
|
|
7916
|
-
* `IterationType.ITERATIVE`.
|
|
7917
|
-
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
|
7918
|
-
* itself if it is not a valid node key.
|
|
7919
|
-
*/
|
|
7920
|
-
ensureNode(key, iterationType = "ITERATIVE" /* ITERATIVE */) {
|
|
7921
|
-
return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
7922
|
-
}
|
|
7923
7978
|
/**
|
|
7924
7979
|
* Time Complexity: O(n)
|
|
7925
7980
|
* Space Complexity: O(log n)
|
|
@@ -7949,10 +8004,13 @@ var dataStructureTyped = (() => {
|
|
|
7949
8004
|
return (_b = (_a = this.getNode(identifier, callback, beginRoot, iterationType)) == null ? void 0 : _a.value) != null ? _b : void 0;
|
|
7950
8005
|
}
|
|
7951
8006
|
/**
|
|
7952
|
-
* Time Complexity: O(
|
|
7953
|
-
* Space Complexity: O(
|
|
8007
|
+
* Time Complexity: O(1)
|
|
8008
|
+
* Space Complexity: O(1)
|
|
7954
8009
|
*/
|
|
7955
8010
|
/**
|
|
8011
|
+
* Time Complexity: O(1)
|
|
8012
|
+
* Space Complexity: O(1)
|
|
8013
|
+
*
|
|
7956
8014
|
* Clear the binary tree, removing all nodes.
|
|
7957
8015
|
*/
|
|
7958
8016
|
clear() {
|
|
@@ -7960,6 +8018,13 @@ var dataStructureTyped = (() => {
|
|
|
7960
8018
|
this._size = 0;
|
|
7961
8019
|
}
|
|
7962
8020
|
/**
|
|
8021
|
+
* Time Complexity: O(1)
|
|
8022
|
+
* Space Complexity: O(1)
|
|
8023
|
+
*/
|
|
8024
|
+
/**
|
|
8025
|
+
* Time Complexity: O(1)
|
|
8026
|
+
* Space Complexity: O(1)
|
|
8027
|
+
*
|
|
7963
8028
|
* Check if the binary tree is empty.
|
|
7964
8029
|
* @returns {boolean} - True if the binary tree is empty, false otherwise.
|
|
7965
8030
|
*/
|
|
@@ -7994,7 +8059,7 @@ var dataStructureTyped = (() => {
|
|
|
7994
8059
|
}
|
|
7995
8060
|
/**
|
|
7996
8061
|
* Time Complexity: O(log n)
|
|
7997
|
-
* Space Complexity: O(
|
|
8062
|
+
* Space Complexity: O(1)
|
|
7998
8063
|
*/
|
|
7999
8064
|
/**
|
|
8000
8065
|
* Time Complexity: O(log n)
|
|
@@ -8085,7 +8150,7 @@ var dataStructureTyped = (() => {
|
|
|
8085
8150
|
* possible values:
|
|
8086
8151
|
* @returns a boolean value.
|
|
8087
8152
|
*/
|
|
8088
|
-
|
|
8153
|
+
isBST(beginRoot = this.root, iterationType = this.iterationType) {
|
|
8089
8154
|
beginRoot = this.ensureNode(beginRoot);
|
|
8090
8155
|
if (!beginRoot)
|
|
8091
8156
|
return true;
|
|
@@ -8098,45 +8163,32 @@ var dataStructureTyped = (() => {
|
|
|
8098
8163
|
return false;
|
|
8099
8164
|
return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
|
|
8100
8165
|
};
|
|
8101
|
-
|
|
8166
|
+
const isStandardBST = dfs(beginRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
8167
|
+
const isInverseBST = dfs(beginRoot, Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
|
|
8168
|
+
return isStandardBST || isInverseBST;
|
|
8102
8169
|
} else {
|
|
8103
|
-
const
|
|
8104
|
-
|
|
8105
|
-
|
|
8106
|
-
|
|
8107
|
-
|
|
8108
|
-
curr
|
|
8170
|
+
const checkBST = (checkMax = false) => {
|
|
8171
|
+
const stack = [];
|
|
8172
|
+
let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
|
|
8173
|
+
let curr = beginRoot;
|
|
8174
|
+
while (curr || stack.length > 0) {
|
|
8175
|
+
while (curr) {
|
|
8176
|
+
stack.push(curr);
|
|
8177
|
+
curr = curr.left;
|
|
8178
|
+
}
|
|
8179
|
+
curr = stack.pop();
|
|
8180
|
+
const numKey = this.extractor(curr.key);
|
|
8181
|
+
if (!curr || !checkMax && prev >= numKey || checkMax && prev <= numKey)
|
|
8182
|
+
return false;
|
|
8183
|
+
prev = numKey;
|
|
8184
|
+
curr = curr.right;
|
|
8109
8185
|
}
|
|
8110
|
-
|
|
8111
|
-
|
|
8112
|
-
|
|
8113
|
-
|
|
8114
|
-
prev = numKey;
|
|
8115
|
-
curr = curr.right;
|
|
8116
|
-
}
|
|
8117
|
-
return true;
|
|
8186
|
+
return true;
|
|
8187
|
+
};
|
|
8188
|
+
const isStandardBST = checkBST(false), isInverseBST = checkBST(true);
|
|
8189
|
+
return isStandardBST || isInverseBST;
|
|
8118
8190
|
}
|
|
8119
8191
|
}
|
|
8120
|
-
/**
|
|
8121
|
-
* Time Complexity: O(n)
|
|
8122
|
-
* Space Complexity: O(1)
|
|
8123
|
-
*/
|
|
8124
|
-
/**
|
|
8125
|
-
* Time Complexity: O(n)
|
|
8126
|
-
* Space Complexity: O(1)
|
|
8127
|
-
*
|
|
8128
|
-
* The function checks if a binary tree is a binary search tree.
|
|
8129
|
-
* @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
|
|
8130
|
-
* be used when checking if the binary tree is a binary search tree (BST). It is an optional
|
|
8131
|
-
* parameter with a default value of "this.iterationType". The value of "this.iterationType" is
|
|
8132
|
-
* expected to be
|
|
8133
|
-
* @returns a boolean value.
|
|
8134
|
-
*/
|
|
8135
|
-
isBST(iterationType = this.iterationType) {
|
|
8136
|
-
if (this.root === null)
|
|
8137
|
-
return true;
|
|
8138
|
-
return this.isSubtreeBST(this.root, iterationType);
|
|
8139
|
-
}
|
|
8140
8192
|
/**
|
|
8141
8193
|
* Time complexity: O(n)
|
|
8142
8194
|
* Space complexity: O(log n)
|
|
@@ -8156,7 +8208,7 @@ var dataStructureTyped = (() => {
|
|
|
8156
8208
|
* whether to include null values in the traversal. If `includeNull` is set to `true`, the
|
|
8157
8209
|
* traversal will include null values, otherwise it will skip them.
|
|
8158
8210
|
* @returns The function `subTreeTraverse` returns an array of values that are the result of invoking
|
|
8159
|
-
* the `callback` function on each node in the subtree. The type of the array
|
|
8211
|
+
* the `callback` function on each node in the subtree. The type of the array nodes is determined
|
|
8160
8212
|
* by the return type of the `callback` function.
|
|
8161
8213
|
*/
|
|
8162
8214
|
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
@@ -8196,44 +8248,6 @@ var dataStructureTyped = (() => {
|
|
|
8196
8248
|
}
|
|
8197
8249
|
return ans;
|
|
8198
8250
|
}
|
|
8199
|
-
/**
|
|
8200
|
-
* Time complexity: O(n)
|
|
8201
|
-
* Space complexity: O(log n)
|
|
8202
|
-
*/
|
|
8203
|
-
/**
|
|
8204
|
-
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
8205
|
-
* BinaryTreeNode and its key is not NaN.
|
|
8206
|
-
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
8207
|
-
* @returns a boolean value.
|
|
8208
|
-
*/
|
|
8209
|
-
isRealNode(node) {
|
|
8210
|
-
return node instanceof BinaryTreeNode && String(node.key) !== "NaN";
|
|
8211
|
-
}
|
|
8212
|
-
/**
|
|
8213
|
-
* The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
|
|
8214
|
-
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
8215
|
-
* @returns a boolean value.
|
|
8216
|
-
*/
|
|
8217
|
-
isNIL(node) {
|
|
8218
|
-
return node instanceof BinaryTreeNode && String(node.key) === "NaN";
|
|
8219
|
-
}
|
|
8220
|
-
/**
|
|
8221
|
-
* The function checks if a given node is a real node or null.
|
|
8222
|
-
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
8223
|
-
* @returns a boolean value.
|
|
8224
|
-
*/
|
|
8225
|
-
isNodeOrNull(node) {
|
|
8226
|
-
return this.isRealNode(node) || node === null;
|
|
8227
|
-
}
|
|
8228
|
-
/**
|
|
8229
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
8230
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
8231
|
-
* data type.
|
|
8232
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
8233
|
-
*/
|
|
8234
|
-
isNotNodeInstance(potentialKey) {
|
|
8235
|
-
return !(potentialKey instanceof BinaryTreeNode);
|
|
8236
|
-
}
|
|
8237
8251
|
/**
|
|
8238
8252
|
* Time complexity: O(n)
|
|
8239
8253
|
* Space complexity: O(n)
|
|
@@ -8541,7 +8555,12 @@ var dataStructureTyped = (() => {
|
|
|
8541
8555
|
}
|
|
8542
8556
|
/**
|
|
8543
8557
|
* Time complexity: O(n)
|
|
8544
|
-
* Space complexity: O(
|
|
8558
|
+
* Space complexity: O(n)
|
|
8559
|
+
*/
|
|
8560
|
+
/**
|
|
8561
|
+
* Time complexity: O(n)
|
|
8562
|
+
* Space complexity: O(n)
|
|
8563
|
+
*
|
|
8545
8564
|
* The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
|
|
8546
8565
|
* algorithm.
|
|
8547
8566
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
@@ -8554,7 +8573,7 @@ var dataStructureTyped = (() => {
|
|
|
8554
8573
|
* for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
|
|
8555
8574
|
* the root of the tree. If no value is provided, the default value is the root of the tree.
|
|
8556
8575
|
* @returns The function `morris` returns an array of values that are the result of invoking the
|
|
8557
|
-
* `callback` function on each node in the binary tree. The type of the array
|
|
8576
|
+
* `callback` function on each node in the binary tree. The type of the array nodes is determined
|
|
8558
8577
|
* by the return type of the `callback` function.
|
|
8559
8578
|
*/
|
|
8560
8579
|
morris(callback = this._defaultOneParamCallback, pattern = "in", beginRoot = this.root) {
|
|
@@ -8663,8 +8682,8 @@ var dataStructureTyped = (() => {
|
|
|
8663
8682
|
* Time Complexity: O(n)
|
|
8664
8683
|
* Space Complexity: O(n)
|
|
8665
8684
|
*
|
|
8666
|
-
* The `filter` function creates a new tree by iterating over the
|
|
8667
|
-
* adding only the
|
|
8685
|
+
* The `filter` function creates a new tree by iterating over the nodes of the current tree and
|
|
8686
|
+
* adding only the nodes that satisfy the given predicate function.
|
|
8668
8687
|
* @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
|
|
8669
8688
|
* `key`, and `index`. It should return a boolean value indicating whether the pair should be
|
|
8670
8689
|
* included in the filtered tree or not.
|
|
@@ -8721,6 +8740,13 @@ var dataStructureTyped = (() => {
|
|
|
8721
8740
|
// // }
|
|
8722
8741
|
//
|
|
8723
8742
|
/**
|
|
8743
|
+
* Time Complexity: O(n)
|
|
8744
|
+
* Space Complexity: O(n)
|
|
8745
|
+
*/
|
|
8746
|
+
/**
|
|
8747
|
+
* Time Complexity: O(n)
|
|
8748
|
+
* Space Complexity: O(n)
|
|
8749
|
+
*
|
|
8724
8750
|
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
|
8725
8751
|
* @param {K | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `K | N | null |
|
|
8726
8752
|
* undefined`. It represents the root node of a binary tree. The root node can have one of the
|
|
@@ -8787,7 +8813,12 @@ var dataStructureTyped = (() => {
|
|
|
8787
8813
|
return emptyDisplayLayout;
|
|
8788
8814
|
} else if (node !== null && node !== void 0) {
|
|
8789
8815
|
const key = node.key, line = isNaN(this.extractor(key)) ? "S" : this.extractor(key).toString(), width = line.length;
|
|
8790
|
-
return _buildNodeDisplay(
|
|
8816
|
+
return _buildNodeDisplay(
|
|
8817
|
+
line,
|
|
8818
|
+
width,
|
|
8819
|
+
this._displayAux(node.left, options),
|
|
8820
|
+
this._displayAux(node.right, options)
|
|
8821
|
+
);
|
|
8791
8822
|
} else {
|
|
8792
8823
|
const line = node === void 0 ? "U" : "N", width = line.length;
|
|
8793
8824
|
return _buildNodeDisplay(line, width, [[""], 1, 0, 0], [[""], 1, 0, 0]);
|
|
@@ -8803,7 +8834,12 @@ var dataStructureTyped = (() => {
|
|
|
8803
8834
|
const rightLine = i < rightHeight ? rightLines[i] : " ".repeat(rightWidth);
|
|
8804
8835
|
mergedLines.push(leftLine + " ".repeat(width) + rightLine);
|
|
8805
8836
|
}
|
|
8806
|
-
return [
|
|
8837
|
+
return [
|
|
8838
|
+
mergedLines,
|
|
8839
|
+
leftWidth + width + rightWidth,
|
|
8840
|
+
Math.max(leftHeight, rightHeight) + 2,
|
|
8841
|
+
leftWidth + Math.floor(width / 2)
|
|
8842
|
+
];
|
|
8807
8843
|
}
|
|
8808
8844
|
}
|
|
8809
8845
|
/**
|
|
@@ -8852,40 +8888,6 @@ var dataStructureTyped = (() => {
|
|
|
8852
8888
|
}
|
|
8853
8889
|
return newNode;
|
|
8854
8890
|
}
|
|
8855
|
-
/**
|
|
8856
|
-
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
8857
|
-
* @param {N | null | undefined} newNode - The `newNode` parameter represents the node that you want to add to
|
|
8858
|
-
* the binary tree. It can be either a node object or `null`.
|
|
8859
|
-
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will
|
|
8860
|
-
* be added as a child.
|
|
8861
|
-
* @returns either the left or right child node of the parent node, depending on which child is
|
|
8862
|
-
* available for adding the new node. If a new node is added, the function also updates the size of
|
|
8863
|
-
* the binary tree. If neither the left nor right child is available, the function returns undefined.
|
|
8864
|
-
* If the parent node is null, the function also returns undefined.
|
|
8865
|
-
*/
|
|
8866
|
-
_addTo(newNode, parent) {
|
|
8867
|
-
if (this.isNotNodeInstance(parent))
|
|
8868
|
-
parent = this.getNode(parent);
|
|
8869
|
-
if (parent) {
|
|
8870
|
-
if (parent.left === void 0) {
|
|
8871
|
-
parent.left = newNode;
|
|
8872
|
-
if (newNode) {
|
|
8873
|
-
this._size = this.size + 1;
|
|
8874
|
-
}
|
|
8875
|
-
return parent.left;
|
|
8876
|
-
} else if (parent.right === void 0) {
|
|
8877
|
-
parent.right = newNode;
|
|
8878
|
-
if (newNode) {
|
|
8879
|
-
this._size = this.size + 1;
|
|
8880
|
-
}
|
|
8881
|
-
return parent.right;
|
|
8882
|
-
} else {
|
|
8883
|
-
return;
|
|
8884
|
-
}
|
|
8885
|
-
} else {
|
|
8886
|
-
return;
|
|
8887
|
-
}
|
|
8888
|
-
}
|
|
8889
8891
|
/**
|
|
8890
8892
|
* The function sets the root property of an object to a given value, and if the value is not null,
|
|
8891
8893
|
* it also sets the parent property of the value to undefined.
|
|
@@ -8947,16 +8949,16 @@ var dataStructureTyped = (() => {
|
|
|
8947
8949
|
var BST = class _BST extends BinaryTree {
|
|
8948
8950
|
/**
|
|
8949
8951
|
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
|
8950
|
-
* the tree with optional
|
|
8951
|
-
* @param [
|
|
8952
|
+
* the tree with optional nodes and options.
|
|
8953
|
+
* @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the
|
|
8952
8954
|
* binary search tree.
|
|
8953
8955
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
8954
8956
|
* configuration options for the binary search tree. It can have the following properties:
|
|
8955
8957
|
*/
|
|
8956
|
-
constructor(
|
|
8958
|
+
constructor(nodes, options) {
|
|
8957
8959
|
super([], options);
|
|
8958
8960
|
__publicField(this, "_root");
|
|
8959
|
-
__publicField(this, "_variant", "
|
|
8961
|
+
__publicField(this, "_variant", "STANDARD" /* STANDARD */);
|
|
8960
8962
|
if (options) {
|
|
8961
8963
|
const { variant } = options;
|
|
8962
8964
|
if (variant) {
|
|
@@ -8964,8 +8966,8 @@ var dataStructureTyped = (() => {
|
|
|
8964
8966
|
}
|
|
8965
8967
|
}
|
|
8966
8968
|
this._root = void 0;
|
|
8967
|
-
if (
|
|
8968
|
-
this.addMany(
|
|
8969
|
+
if (nodes)
|
|
8970
|
+
this.addMany(nodes);
|
|
8969
8971
|
}
|
|
8970
8972
|
get root() {
|
|
8971
8973
|
return this._root;
|
|
@@ -8998,48 +9000,88 @@ var dataStructureTyped = (() => {
|
|
|
8998
9000
|
}, options));
|
|
8999
9001
|
}
|
|
9000
9002
|
/**
|
|
9001
|
-
* The function
|
|
9002
|
-
* @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V, N>`.
|
|
9003
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
9004
|
-
*/
|
|
9005
|
-
isNode(exemplar) {
|
|
9006
|
-
return exemplar instanceof BSTNode;
|
|
9007
|
-
}
|
|
9008
|
-
/**
|
|
9009
|
-
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
9003
|
+
* The function `exemplarToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
|
|
9010
9004
|
* otherwise it returns undefined.
|
|
9011
|
-
* @param
|
|
9005
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
|
|
9012
9006
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
9013
|
-
* `exemplarToNode` function. It represents the value associated with the
|
|
9007
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
|
|
9014
9008
|
* @returns a node of type N or undefined.
|
|
9015
9009
|
*/
|
|
9016
|
-
exemplarToNode(
|
|
9010
|
+
exemplarToNode(keyOrNodeOrEntry, value) {
|
|
9017
9011
|
let node;
|
|
9018
|
-
if (
|
|
9012
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === void 0) {
|
|
9019
9013
|
return;
|
|
9020
|
-
} else if (this.isNode(
|
|
9021
|
-
node =
|
|
9022
|
-
} else if (this.isEntry(
|
|
9023
|
-
const [key, value2] =
|
|
9014
|
+
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
9015
|
+
node = keyOrNodeOrEntry;
|
|
9016
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
9017
|
+
const [key, value2] = keyOrNodeOrEntry;
|
|
9024
9018
|
if (key === void 0 || key === null) {
|
|
9025
9019
|
return;
|
|
9026
9020
|
} else {
|
|
9027
9021
|
node = this.createNode(key, value2);
|
|
9028
9022
|
}
|
|
9029
|
-
} else if (this.isNotNodeInstance(
|
|
9030
|
-
node = this.createNode(
|
|
9023
|
+
} else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
9024
|
+
node = this.createNode(keyOrNodeOrEntry, value);
|
|
9031
9025
|
} else {
|
|
9032
9026
|
return;
|
|
9033
9027
|
}
|
|
9034
9028
|
return node;
|
|
9035
9029
|
}
|
|
9036
9030
|
/**
|
|
9037
|
-
* Time Complexity: O(log n)
|
|
9038
|
-
* Space Complexity: O(
|
|
9031
|
+
* Time Complexity: O(log n)
|
|
9032
|
+
* Space Complexity: O(log n)
|
|
9033
|
+
* Average case for a balanced tree. Space for the recursive call stack in the worst case.
|
|
9034
|
+
*/
|
|
9035
|
+
/**
|
|
9036
|
+
* Time Complexity: O(log n)
|
|
9037
|
+
* Space Complexity: O(log n)
|
|
9038
|
+
*
|
|
9039
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
9040
|
+
* otherwise it returns the key itself.
|
|
9041
|
+
* @param {K | N | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`, or
|
|
9042
|
+
* `undefined`.
|
|
9043
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
9044
|
+
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
9045
|
+
* @returns either a node object (N) or undefined.
|
|
9046
|
+
*/
|
|
9047
|
+
ensureNode(keyOrNodeOrEntry, iterationType = "ITERATIVE" /* ITERATIVE */) {
|
|
9048
|
+
let res;
|
|
9049
|
+
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
9050
|
+
res = keyOrNodeOrEntry;
|
|
9051
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
9052
|
+
if (keyOrNodeOrEntry[0])
|
|
9053
|
+
res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
9054
|
+
} else {
|
|
9055
|
+
if (keyOrNodeOrEntry)
|
|
9056
|
+
res = this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
9057
|
+
}
|
|
9058
|
+
return res;
|
|
9059
|
+
}
|
|
9060
|
+
/**
|
|
9061
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
9062
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
9063
|
+
* data type.
|
|
9064
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
9065
|
+
*/
|
|
9066
|
+
isNotNodeInstance(potentialKey) {
|
|
9067
|
+
return !(potentialKey instanceof BSTNode);
|
|
9068
|
+
}
|
|
9069
|
+
/**
|
|
9070
|
+
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
9071
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
9072
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
|
|
9073
|
+
*/
|
|
9074
|
+
isNode(keyOrNodeOrEntry) {
|
|
9075
|
+
return keyOrNodeOrEntry instanceof BSTNode;
|
|
9076
|
+
}
|
|
9077
|
+
/**
|
|
9078
|
+
* Time Complexity: O(log n)
|
|
9079
|
+
* Space Complexity: O(1)
|
|
9080
|
+
* - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
9039
9081
|
*/
|
|
9040
9082
|
/**
|
|
9041
|
-
* Time Complexity: O(log n)
|
|
9042
|
-
* Space Complexity: O(1)
|
|
9083
|
+
* Time Complexity: O(log n)
|
|
9084
|
+
* Space Complexity: O(1)
|
|
9043
9085
|
*
|
|
9044
9086
|
* The `add` function adds a new node to a binary tree, updating the value if the key already exists
|
|
9045
9087
|
* or inserting a new node if the key is unique.
|
|
@@ -9052,23 +9094,23 @@ var dataStructureTyped = (() => {
|
|
|
9052
9094
|
add(keyOrNodeOrEntry, value) {
|
|
9053
9095
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
9054
9096
|
if (newNode === void 0)
|
|
9055
|
-
return;
|
|
9097
|
+
return false;
|
|
9056
9098
|
if (this.root === void 0) {
|
|
9057
9099
|
this._setRoot(newNode);
|
|
9058
9100
|
this._size++;
|
|
9059
|
-
return
|
|
9101
|
+
return true;
|
|
9060
9102
|
}
|
|
9061
9103
|
let current = this.root;
|
|
9062
9104
|
while (current !== void 0) {
|
|
9063
9105
|
if (this._compare(current.key, newNode.key) === "eq" /* eq */) {
|
|
9064
9106
|
this._replaceNode(current, newNode);
|
|
9065
|
-
return
|
|
9107
|
+
return true;
|
|
9066
9108
|
} else if (this._compare(current.key, newNode.key) === "gt" /* gt */) {
|
|
9067
9109
|
if (current.left === void 0) {
|
|
9068
9110
|
current.left = newNode;
|
|
9069
9111
|
newNode.parent = current;
|
|
9070
9112
|
this._size++;
|
|
9071
|
-
return
|
|
9113
|
+
return true;
|
|
9072
9114
|
}
|
|
9073
9115
|
current = current.left;
|
|
9074
9116
|
} else {
|
|
@@ -9076,20 +9118,21 @@ var dataStructureTyped = (() => {
|
|
|
9076
9118
|
current.right = newNode;
|
|
9077
9119
|
newNode.parent = current;
|
|
9078
9120
|
this._size++;
|
|
9079
|
-
return
|
|
9121
|
+
return true;
|
|
9080
9122
|
}
|
|
9081
9123
|
current = current.right;
|
|
9082
9124
|
}
|
|
9083
9125
|
}
|
|
9084
|
-
return
|
|
9126
|
+
return false;
|
|
9085
9127
|
}
|
|
9086
9128
|
/**
|
|
9087
|
-
* Time Complexity: O(k log n)
|
|
9088
|
-
* Space Complexity: O(k)
|
|
9129
|
+
* Time Complexity: O(k log n)
|
|
9130
|
+
* Space Complexity: O(k)
|
|
9131
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
9089
9132
|
*/
|
|
9090
9133
|
/**
|
|
9091
|
-
* Time Complexity: O(k log n)
|
|
9092
|
-
* Space Complexity: O(k)
|
|
9134
|
+
* Time Complexity: O(k log n)
|
|
9135
|
+
* Space Complexity: O(k)
|
|
9093
9136
|
*
|
|
9094
9137
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
9095
9138
|
* balancing the tree after each addition.
|
|
@@ -9100,7 +9143,7 @@ var dataStructureTyped = (() => {
|
|
|
9100
9143
|
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
9101
9144
|
* @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
|
|
9102
9145
|
* balanced or not. If set to true, the add operation will be balanced using a binary search tree
|
|
9103
|
-
* algorithm. If set to false, the add operation will not be balanced and the
|
|
9146
|
+
* algorithm. If set to false, the add operation will not be balanced and the nodes will be added
|
|
9104
9147
|
* in the order they appear in the input.
|
|
9105
9148
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
9106
9149
|
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
@@ -9181,20 +9224,19 @@ var dataStructureTyped = (() => {
|
|
|
9181
9224
|
return inserted;
|
|
9182
9225
|
}
|
|
9183
9226
|
/**
|
|
9184
|
-
* Time Complexity: O(n log n)
|
|
9185
|
-
* Space Complexity: O(n)
|
|
9227
|
+
* Time Complexity: O(n log n)
|
|
9228
|
+
* Space Complexity: O(n)
|
|
9229
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
9186
9230
|
*/
|
|
9187
9231
|
/**
|
|
9188
|
-
* Time Complexity: O(log n)
|
|
9189
|
-
* Space Complexity: O(
|
|
9232
|
+
* Time Complexity: O(n log n)
|
|
9233
|
+
* Space Complexity: O(n)
|
|
9190
9234
|
*
|
|
9191
9235
|
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
9192
9236
|
* leftmost node if the comparison result is greater than.
|
|
9193
9237
|
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
9194
9238
|
* type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
9195
9239
|
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
9196
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
9197
|
-
* be performed. It can have one of the following values:
|
|
9198
9240
|
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
9199
9241
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
9200
9242
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
@@ -9203,7 +9245,7 @@ var dataStructureTyped = (() => {
|
|
|
9203
9245
|
let current = this.ensureNode(beginRoot);
|
|
9204
9246
|
if (!current)
|
|
9205
9247
|
return void 0;
|
|
9206
|
-
if (this._variant === "
|
|
9248
|
+
if (this._variant === "STANDARD" /* STANDARD */) {
|
|
9207
9249
|
while (current.right !== void 0) {
|
|
9208
9250
|
current = current.right;
|
|
9209
9251
|
}
|
|
@@ -9215,12 +9257,12 @@ var dataStructureTyped = (() => {
|
|
|
9215
9257
|
return current.key;
|
|
9216
9258
|
}
|
|
9217
9259
|
/**
|
|
9218
|
-
* Time Complexity: O(log n)
|
|
9219
|
-
* Space Complexity: O(1)
|
|
9260
|
+
* Time Complexity: O(log n)
|
|
9261
|
+
* Space Complexity: O(1)
|
|
9220
9262
|
*/
|
|
9221
9263
|
/**
|
|
9222
|
-
* Time Complexity: O(log n)
|
|
9223
|
-
* Space Complexity: O(
|
|
9264
|
+
* Time Complexity: O(log n)
|
|
9265
|
+
* Space Complexity: O(1)
|
|
9224
9266
|
*
|
|
9225
9267
|
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
9226
9268
|
* either recursive or iterative methods.
|
|
@@ -9263,53 +9305,34 @@ var dataStructureTyped = (() => {
|
|
|
9263
9305
|
}
|
|
9264
9306
|
}
|
|
9265
9307
|
/**
|
|
9266
|
-
|
|
9267
|
-
|
|
9268
|
-
|
|
9269
|
-
|
|
9270
|
-
|
|
9271
|
-
|
|
9272
|
-
|
|
9273
|
-
|
|
9274
|
-
|
|
9275
|
-
|
|
9276
|
-
|
|
9277
|
-
|
|
9278
|
-
|
|
9279
|
-
|
|
9280
|
-
|
|
9281
|
-
|
|
9282
|
-
|
|
9283
|
-
|
|
9284
|
-
|
|
9285
|
-
|
|
9286
|
-
|
|
9287
|
-
|
|
9288
|
-
|
|
9289
|
-
|
|
9290
|
-
|
|
9291
|
-
|
|
9292
|
-
|
|
9293
|
-
|
|
9294
|
-
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
9295
|
-
* recursive or iterative approach.
|
|
9296
|
-
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
9297
|
-
* want to search for in the nodes of the binary tree. It can be of any type that is returned by the
|
|
9298
|
-
* callback function `C`.
|
|
9299
|
-
* @param {C} callback - The `callback` parameter is a function that takes a node of type `N` as its
|
|
9300
|
-
* argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
|
|
9301
|
-
* function type that extends the `BTNCallback<N>` type. The `BTNCallback<N>` type is
|
|
9302
|
-
* @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
|
|
9303
|
-
* first node that matches the identifier. If set to true, the function will return an array
|
|
9304
|
-
* containing only the first matching node. If set to false (default), the function will continue
|
|
9305
|
-
* searching for all nodes that match the identifier and return an array containing
|
|
9306
|
-
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
|
|
9307
|
-
* for the traversal. It can be either a key value or a node object. If it is undefined, the
|
|
9308
|
-
* traversal will start from the root of the tree.
|
|
9309
|
-
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
9310
|
-
* performed on the binary tree. It can have two possible values:
|
|
9311
|
-
* @returns The method returns an array of nodes (`N[]`).
|
|
9312
|
-
*/
|
|
9308
|
+
* Time Complexity: O(log n)
|
|
9309
|
+
* Space Complexity: O(log n)
|
|
9310
|
+
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
9311
|
+
* /
|
|
9312
|
+
|
|
9313
|
+
/**
|
|
9314
|
+
* Time Complexity: O(log n)
|
|
9315
|
+
* Space Complexity: O(log n)
|
|
9316
|
+
*
|
|
9317
|
+
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
9318
|
+
* recursive or iterative approach.
|
|
9319
|
+
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
9320
|
+
* want to search for in the nodes of the binary tree. It can be of any type that is returned by the
|
|
9321
|
+
* callback function `C`.
|
|
9322
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `N` as its
|
|
9323
|
+
* argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
|
|
9324
|
+
* function type that extends the `BTNCallback<N>` type. The `BTNCallback<N>` type is
|
|
9325
|
+
* @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
|
|
9326
|
+
* first node that matches the identifier. If set to true, the function will return an array
|
|
9327
|
+
* containing only the first matching node. If set to false (default), the function will continue
|
|
9328
|
+
* searching for all nodes that match the identifier and return an array containing
|
|
9329
|
+
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
|
|
9330
|
+
* for the traversal. It can be either a key value or a node object. If it is undefined, the
|
|
9331
|
+
* traversal will start from the root of the tree.
|
|
9332
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
9333
|
+
* performed on the binary tree. It can have two possible values:
|
|
9334
|
+
* @returns The method returns an array of nodes (`N[]`).
|
|
9335
|
+
*/
|
|
9313
9336
|
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
9314
9337
|
beginRoot = this.ensureNode(beginRoot);
|
|
9315
9338
|
if (!beginRoot)
|
|
@@ -9362,12 +9385,13 @@ var dataStructureTyped = (() => {
|
|
|
9362
9385
|
return ans;
|
|
9363
9386
|
}
|
|
9364
9387
|
/**
|
|
9365
|
-
* Time Complexity: O(log n)
|
|
9366
|
-
* Space Complexity: O(log n)
|
|
9388
|
+
* Time Complexity: O(log n)
|
|
9389
|
+
* Space Complexity: O(log n)
|
|
9390
|
+
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
9367
9391
|
*/
|
|
9368
9392
|
/**
|
|
9369
|
-
* Time Complexity: O(log n)
|
|
9370
|
-
* Space Complexity: O(log n)
|
|
9393
|
+
* Time Complexity: O(log n)
|
|
9394
|
+
* Space Complexity: O(log n)
|
|
9371
9395
|
*
|
|
9372
9396
|
* The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
|
|
9373
9397
|
* are either lesser or greater than a target node, depending on the specified comparison type.
|
|
@@ -9426,12 +9450,12 @@ var dataStructureTyped = (() => {
|
|
|
9426
9450
|
}
|
|
9427
9451
|
}
|
|
9428
9452
|
/**
|
|
9429
|
-
* Time Complexity: O(log n)
|
|
9430
|
-
* Space Complexity: O(log n)
|
|
9453
|
+
* Time Complexity: O(log n)
|
|
9454
|
+
* Space Complexity: O(log n)
|
|
9431
9455
|
*/
|
|
9432
9456
|
/**
|
|
9433
|
-
* Time Complexity: O(n)
|
|
9434
|
-
* Space Complexity: O(n)
|
|
9457
|
+
* Time Complexity: O(log n)
|
|
9458
|
+
* Space Complexity: O(log n)
|
|
9435
9459
|
*
|
|
9436
9460
|
* The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
|
|
9437
9461
|
* ensures the tree is perfectly balanced.
|
|
@@ -9489,8 +9513,8 @@ var dataStructureTyped = (() => {
|
|
|
9489
9513
|
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
9490
9514
|
*/
|
|
9491
9515
|
/**
|
|
9492
|
-
* Time Complexity: O(n)
|
|
9493
|
-
* Space Complexity: O(log n)
|
|
9516
|
+
* Time Complexity: O(n)
|
|
9517
|
+
* Space Complexity: O(log n)
|
|
9494
9518
|
*
|
|
9495
9519
|
* The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
|
|
9496
9520
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
@@ -9557,7 +9581,7 @@ var dataStructureTyped = (() => {
|
|
|
9557
9581
|
_compare(a, b) {
|
|
9558
9582
|
const extractedA = this.extractor(a);
|
|
9559
9583
|
const extractedB = this.extractor(b);
|
|
9560
|
-
const compared = this.variant === "
|
|
9584
|
+
const compared = this.variant === "STANDARD" /* STANDARD */ ? extractedA - extractedB : extractedB - extractedA;
|
|
9561
9585
|
return compared > 0 ? "gt" /* gt */ : compared < 0 ? "lt" /* lt */ : "eq" /* eq */;
|
|
9562
9586
|
}
|
|
9563
9587
|
};
|
|
@@ -9998,18 +10022,18 @@ var dataStructureTyped = (() => {
|
|
|
9998
10022
|
};
|
|
9999
10023
|
var AVLTree = class _AVLTree extends BST {
|
|
10000
10024
|
/**
|
|
10001
|
-
* The constructor function initializes an AVLTree object with optional
|
|
10002
|
-
* @param [
|
|
10003
|
-
* objects. It represents a collection of
|
|
10025
|
+
* The constructor function initializes an AVLTree object with optional nodes and options.
|
|
10026
|
+
* @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
|
|
10027
|
+
* objects. It represents a collection of nodes that will be added to the AVL tree during
|
|
10004
10028
|
* initialization.
|
|
10005
10029
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
10006
10030
|
* behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
|
|
10007
10031
|
* provide only a subset of the properties defined in the `AVLTreeOptions` interface.
|
|
10008
10032
|
*/
|
|
10009
|
-
constructor(
|
|
10033
|
+
constructor(nodes, options) {
|
|
10010
10034
|
super([], options);
|
|
10011
|
-
if (
|
|
10012
|
-
super.addMany(
|
|
10035
|
+
if (nodes)
|
|
10036
|
+
super.addMany(nodes);
|
|
10013
10037
|
}
|
|
10014
10038
|
/**
|
|
10015
10039
|
* The function creates a new AVL tree node with the specified key and value.
|
|
@@ -10037,12 +10061,12 @@ var dataStructureTyped = (() => {
|
|
|
10037
10061
|
}, options));
|
|
10038
10062
|
}
|
|
10039
10063
|
/**
|
|
10040
|
-
* The function checks if an
|
|
10041
|
-
* @param
|
|
10042
|
-
* @returns a boolean value indicating whether the
|
|
10064
|
+
* The function checks if an keyOrNodeOrEntry is an instance of AVLTreeNode.
|
|
10065
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
10066
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeNode class.
|
|
10043
10067
|
*/
|
|
10044
|
-
isNode(
|
|
10045
|
-
return
|
|
10068
|
+
isNode(keyOrNodeOrEntry) {
|
|
10069
|
+
return keyOrNodeOrEntry instanceof AVLTreeNode;
|
|
10046
10070
|
}
|
|
10047
10071
|
/**
|
|
10048
10072
|
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
@@ -10054,12 +10078,13 @@ var dataStructureTyped = (() => {
|
|
|
10054
10078
|
return !(potentialKey instanceof AVLTreeNode);
|
|
10055
10079
|
}
|
|
10056
10080
|
/**
|
|
10057
|
-
* Time Complexity: O(log n)
|
|
10058
|
-
* Space Complexity: O(1)
|
|
10081
|
+
* Time Complexity: O(log n)
|
|
10082
|
+
* Space Complexity: O(1)
|
|
10083
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
10059
10084
|
*/
|
|
10060
10085
|
/**
|
|
10061
|
-
* Time Complexity: O(log n)
|
|
10062
|
-
* Space Complexity: O(1)
|
|
10086
|
+
* Time Complexity: O(log n)
|
|
10087
|
+
* Space Complexity: O(1)
|
|
10063
10088
|
*
|
|
10064
10089
|
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
10065
10090
|
* a new node.
|
|
@@ -10071,19 +10096,19 @@ var dataStructureTyped = (() => {
|
|
|
10071
10096
|
*/
|
|
10072
10097
|
add(keyOrNodeOrEntry, value) {
|
|
10073
10098
|
if (keyOrNodeOrEntry === null)
|
|
10074
|
-
return
|
|
10099
|
+
return false;
|
|
10075
10100
|
const inserted = super.add(keyOrNodeOrEntry, value);
|
|
10076
10101
|
if (inserted)
|
|
10077
|
-
this._balancePath(
|
|
10102
|
+
this._balancePath(keyOrNodeOrEntry);
|
|
10078
10103
|
return inserted;
|
|
10079
10104
|
}
|
|
10080
10105
|
/**
|
|
10081
|
-
* Time Complexity: O(log n)
|
|
10082
|
-
* Space Complexity: O(1)
|
|
10106
|
+
* Time Complexity: O(log n)
|
|
10107
|
+
* Space Complexity: O(1)
|
|
10083
10108
|
*/
|
|
10084
10109
|
/**
|
|
10085
|
-
* Time Complexity: O(log n)
|
|
10086
|
-
* Space Complexity: O(1)
|
|
10110
|
+
* Time Complexity: O(log n)
|
|
10111
|
+
* Space Complexity: O(1)
|
|
10087
10112
|
*
|
|
10088
10113
|
* The function overrides the delete method of a binary tree, performs the deletion, and then
|
|
10089
10114
|
* balances the tree if necessary.
|
|
@@ -10137,12 +10162,13 @@ var dataStructureTyped = (() => {
|
|
|
10137
10162
|
return void 0;
|
|
10138
10163
|
}
|
|
10139
10164
|
/**
|
|
10140
|
-
* Time Complexity: O(1)
|
|
10141
|
-
* Space Complexity: O(1)
|
|
10165
|
+
* Time Complexity: O(1)
|
|
10166
|
+
* Space Complexity: O(1)
|
|
10167
|
+
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
|
|
10142
10168
|
*/
|
|
10143
10169
|
/**
|
|
10144
|
-
* Time Complexity: O(1)
|
|
10145
|
-
* Space Complexity: O(1)
|
|
10170
|
+
* Time Complexity: O(1)
|
|
10171
|
+
* Space Complexity: O(1)
|
|
10146
10172
|
*
|
|
10147
10173
|
* The function calculates the balance factor of a node in a binary tree.
|
|
10148
10174
|
* @param {N} node - The parameter "node" represents a node in a binary tree data structure.
|
|
@@ -10158,12 +10184,13 @@ var dataStructureTyped = (() => {
|
|
|
10158
10184
|
return node.right.height - node.left.height;
|
|
10159
10185
|
}
|
|
10160
10186
|
/**
|
|
10161
|
-
* Time Complexity: O(1)
|
|
10162
|
-
* Space Complexity: O(1)
|
|
10187
|
+
* Time Complexity: O(1)
|
|
10188
|
+
* Space Complexity: O(1)
|
|
10189
|
+
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
|
|
10163
10190
|
*/
|
|
10164
10191
|
/**
|
|
10165
|
-
* Time Complexity: O(1)
|
|
10166
|
-
* Space Complexity: O(1)
|
|
10192
|
+
* Time Complexity: O(1)
|
|
10193
|
+
* Space Complexity: O(1)
|
|
10167
10194
|
*
|
|
10168
10195
|
* The function updates the height of a node in a binary tree based on the heights of its left and
|
|
10169
10196
|
* right children.
|
|
@@ -10181,12 +10208,13 @@ var dataStructureTyped = (() => {
|
|
|
10181
10208
|
node.height = 1 + Math.max(node.right.height, node.left.height);
|
|
10182
10209
|
}
|
|
10183
10210
|
/**
|
|
10184
|
-
* Time Complexity: O(log n)
|
|
10185
|
-
* Space Complexity: O(1)
|
|
10211
|
+
* Time Complexity: O(log n)
|
|
10212
|
+
* Space Complexity: O(1)
|
|
10213
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root. constant space, as it doesn't use additional data structures that scale with input size.
|
|
10186
10214
|
*/
|
|
10187
10215
|
/**
|
|
10188
|
-
* Time Complexity: O(log n)
|
|
10189
|
-
* Space Complexity: O(1)
|
|
10216
|
+
* Time Complexity: O(log n)
|
|
10217
|
+
* Space Complexity: O(1)
|
|
10190
10218
|
*
|
|
10191
10219
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
10192
10220
|
* to restore balance in an AVL tree after inserting a node.
|
|
@@ -10194,6 +10222,7 @@ var dataStructureTyped = (() => {
|
|
|
10194
10222
|
* AVL tree that needs to be balanced.
|
|
10195
10223
|
*/
|
|
10196
10224
|
_balancePath(node) {
|
|
10225
|
+
node = this.ensureNode(node);
|
|
10197
10226
|
const path = this.getPathToRoot(node, false);
|
|
10198
10227
|
for (let i = 0; i < path.length; i++) {
|
|
10199
10228
|
const A = path[i];
|
|
@@ -10220,12 +10249,13 @@ var dataStructureTyped = (() => {
|
|
|
10220
10249
|
}
|
|
10221
10250
|
}
|
|
10222
10251
|
/**
|
|
10223
|
-
* Time Complexity: O(1)
|
|
10224
|
-
* Space Complexity: O(1)
|
|
10252
|
+
* Time Complexity: O(1)
|
|
10253
|
+
* Space Complexity: O(1)
|
|
10254
|
+
* constant time, as these methods perform a fixed number of operations. constant space, as they only use a constant amount of memory.
|
|
10225
10255
|
*/
|
|
10226
10256
|
/**
|
|
10227
|
-
* Time Complexity: O(1)
|
|
10228
|
-
* Space Complexity: O(1)
|
|
10257
|
+
* Time Complexity: O(1)
|
|
10258
|
+
* Space Complexity: O(1)
|
|
10229
10259
|
*
|
|
10230
10260
|
* The function `_balanceLL` performs a left-left rotation to balance a binary tree.
|
|
10231
10261
|
* @param {N} A - A is a node in a binary tree.
|
|
@@ -10259,12 +10289,12 @@ var dataStructureTyped = (() => {
|
|
|
10259
10289
|
this._updateHeight(B);
|
|
10260
10290
|
}
|
|
10261
10291
|
/**
|
|
10262
|
-
* Time Complexity: O(1)
|
|
10263
|
-
* Space Complexity: O(1)
|
|
10292
|
+
* Time Complexity: O(1)
|
|
10293
|
+
* Space Complexity: O(1)
|
|
10264
10294
|
*/
|
|
10265
10295
|
/**
|
|
10266
|
-
* Time Complexity: O(1)
|
|
10267
|
-
* Space Complexity: O(1)
|
|
10296
|
+
* Time Complexity: O(1)
|
|
10297
|
+
* Space Complexity: O(1)
|
|
10268
10298
|
*
|
|
10269
10299
|
* The `_balanceLR` function performs a left-right rotation to balance a binary tree.
|
|
10270
10300
|
* @param {N} A - A is a node in a binary tree.
|
|
@@ -10313,12 +10343,12 @@ var dataStructureTyped = (() => {
|
|
|
10313
10343
|
C && this._updateHeight(C);
|
|
10314
10344
|
}
|
|
10315
10345
|
/**
|
|
10316
|
-
* Time Complexity: O(1)
|
|
10317
|
-
* Space Complexity: O(1)
|
|
10346
|
+
* Time Complexity: O(1)
|
|
10347
|
+
* Space Complexity: O(1)
|
|
10318
10348
|
*/
|
|
10319
10349
|
/**
|
|
10320
|
-
* Time Complexity: O(1)
|
|
10321
|
-
* Space Complexity: O(1)
|
|
10350
|
+
* Time Complexity: O(1)
|
|
10351
|
+
* Space Complexity: O(1)
|
|
10322
10352
|
*
|
|
10323
10353
|
* The function `_balanceRR` performs a right-right rotation to balance a binary tree.
|
|
10324
10354
|
* @param {N} A - A is a node in a binary tree.
|
|
@@ -10353,12 +10383,12 @@ var dataStructureTyped = (() => {
|
|
|
10353
10383
|
B && this._updateHeight(B);
|
|
10354
10384
|
}
|
|
10355
10385
|
/**
|
|
10356
|
-
* Time Complexity: O(1)
|
|
10357
|
-
* Space Complexity: O(1)
|
|
10386
|
+
* Time Complexity: O(1)
|
|
10387
|
+
* Space Complexity: O(1)
|
|
10358
10388
|
*/
|
|
10359
10389
|
/**
|
|
10360
|
-
* Time Complexity: O(1)
|
|
10361
|
-
* Space Complexity: O(1)
|
|
10390
|
+
* Time Complexity: O(1)
|
|
10391
|
+
* Space Complexity: O(1)
|
|
10362
10392
|
*
|
|
10363
10393
|
* The function `_balanceRL` performs a right-left rotation to balance a binary tree.
|
|
10364
10394
|
* @param {N} A - A is a node in a binary tree.
|
|
@@ -10423,23 +10453,23 @@ var dataStructureTyped = (() => {
|
|
|
10423
10453
|
var RedBlackTree = class _RedBlackTree extends BST {
|
|
10424
10454
|
/**
|
|
10425
10455
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
10426
|
-
* initializes the tree with optional
|
|
10427
|
-
* @param [
|
|
10428
|
-
* objects. It represents the initial
|
|
10456
|
+
* initializes the tree with optional nodes and options.
|
|
10457
|
+
* @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
|
|
10458
|
+
* objects. It represents the initial nodes that will be added to the RBTree during its
|
|
10429
10459
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
10430
|
-
*
|
|
10460
|
+
* nodes to the
|
|
10431
10461
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
10432
10462
|
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
|
10433
10463
|
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
10434
10464
|
*/
|
|
10435
|
-
constructor(
|
|
10465
|
+
constructor(nodes, options) {
|
|
10436
10466
|
super([], options);
|
|
10437
10467
|
__publicField(this, "Sentinel", new RedBlackTreeNode(NaN));
|
|
10438
10468
|
__publicField(this, "_root");
|
|
10439
10469
|
__publicField(this, "_size", 0);
|
|
10440
10470
|
this._root = this.Sentinel;
|
|
10441
|
-
if (
|
|
10442
|
-
super.addMany(
|
|
10471
|
+
if (nodes)
|
|
10472
|
+
super.addMany(nodes);
|
|
10443
10473
|
}
|
|
10444
10474
|
get root() {
|
|
10445
10475
|
return this._root;
|
|
@@ -10476,57 +10506,67 @@ var dataStructureTyped = (() => {
|
|
|
10476
10506
|
}, options));
|
|
10477
10507
|
}
|
|
10478
10508
|
/**
|
|
10479
|
-
* The function
|
|
10480
|
-
* @param
|
|
10481
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
|
|
10482
|
-
* class.
|
|
10483
|
-
*/
|
|
10484
|
-
isNode(exemplar) {
|
|
10485
|
-
return exemplar instanceof RedBlackTreeNode;
|
|
10486
|
-
}
|
|
10487
|
-
/**
|
|
10488
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
10489
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
10490
|
-
* data type.
|
|
10491
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
10492
|
-
*/
|
|
10493
|
-
isNotNodeInstance(potentialKey) {
|
|
10494
|
-
return !(potentialKey instanceof RedBlackTreeNode);
|
|
10495
|
-
}
|
|
10496
|
-
/**
|
|
10497
|
-
* The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
|
|
10498
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where:
|
|
10509
|
+
* The function `exemplarToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible.
|
|
10510
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
|
|
10499
10511
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
10500
|
-
* `exemplarToNode` function. It represents the value associated with the
|
|
10512
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If a value
|
|
10501
10513
|
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
10502
10514
|
* @returns a node of type N or undefined.
|
|
10503
10515
|
*/
|
|
10504
|
-
exemplarToNode(
|
|
10516
|
+
exemplarToNode(keyOrNodeOrEntry, value) {
|
|
10505
10517
|
let node;
|
|
10506
|
-
if (
|
|
10518
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === void 0) {
|
|
10507
10519
|
return;
|
|
10508
|
-
} else if (this.isNode(
|
|
10509
|
-
node =
|
|
10510
|
-
} else if (this.isEntry(
|
|
10511
|
-
const [key, value2] =
|
|
10520
|
+
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
10521
|
+
node = keyOrNodeOrEntry;
|
|
10522
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
10523
|
+
const [key, value2] = keyOrNodeOrEntry;
|
|
10512
10524
|
if (key === void 0 || key === null) {
|
|
10513
10525
|
return;
|
|
10514
10526
|
} else {
|
|
10515
10527
|
node = this.createNode(key, value2, 1 /* RED */);
|
|
10516
10528
|
}
|
|
10517
|
-
} else if (this.isNotNodeInstance(
|
|
10518
|
-
node = this.createNode(
|
|
10529
|
+
} else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
10530
|
+
node = this.createNode(keyOrNodeOrEntry, value, 1 /* RED */);
|
|
10519
10531
|
} else {
|
|
10520
10532
|
return;
|
|
10521
10533
|
}
|
|
10522
10534
|
return node;
|
|
10523
10535
|
}
|
|
10536
|
+
/**
|
|
10537
|
+
* The function checks if an keyOrNodeOrEntry is an instance of the RedBlackTreeNode class.
|
|
10538
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
10539
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the RedBlackTreeNode
|
|
10540
|
+
* class.
|
|
10541
|
+
*/
|
|
10542
|
+
isNode(keyOrNodeOrEntry) {
|
|
10543
|
+
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
|
|
10544
|
+
}
|
|
10524
10545
|
/**
|
|
10525
10546
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
10526
10547
|
* Space Complexity: O(1)
|
|
10527
10548
|
*/
|
|
10549
|
+
isRealNode(node) {
|
|
10550
|
+
if (node === this.Sentinel || node === void 0)
|
|
10551
|
+
return false;
|
|
10552
|
+
return node instanceof RedBlackTreeNode;
|
|
10553
|
+
}
|
|
10528
10554
|
/**
|
|
10529
|
-
*
|
|
10555
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
10556
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
10557
|
+
* data type.
|
|
10558
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
10559
|
+
*/
|
|
10560
|
+
isNotNodeInstance(potentialKey) {
|
|
10561
|
+
return !(potentialKey instanceof RedBlackTreeNode);
|
|
10562
|
+
}
|
|
10563
|
+
/**
|
|
10564
|
+
* Time Complexity: O(log n)
|
|
10565
|
+
* Space Complexity: O(1)
|
|
10566
|
+
* on average (where n is the number of nodes in the tree)
|
|
10567
|
+
*/
|
|
10568
|
+
/**
|
|
10569
|
+
* Time Complexity: O(log n)
|
|
10530
10570
|
* Space Complexity: O(1)
|
|
10531
10571
|
*
|
|
10532
10572
|
* The `add` function adds a new node to a binary search tree and performs necessary rotations and
|
|
@@ -10540,7 +10580,7 @@ var dataStructureTyped = (() => {
|
|
|
10540
10580
|
add(keyOrNodeOrEntry, value) {
|
|
10541
10581
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
10542
10582
|
if (newNode === void 0)
|
|
10543
|
-
return;
|
|
10583
|
+
return false;
|
|
10544
10584
|
newNode.left = this.Sentinel;
|
|
10545
10585
|
newNode.right = this.Sentinel;
|
|
10546
10586
|
let y = void 0;
|
|
@@ -10556,7 +10596,7 @@ var dataStructureTyped = (() => {
|
|
|
10556
10596
|
if (newNode !== x) {
|
|
10557
10597
|
this._replaceNode(x, newNode);
|
|
10558
10598
|
}
|
|
10559
|
-
return;
|
|
10599
|
+
return false;
|
|
10560
10600
|
}
|
|
10561
10601
|
}
|
|
10562
10602
|
}
|
|
@@ -10571,21 +10611,23 @@ var dataStructureTyped = (() => {
|
|
|
10571
10611
|
if (newNode.parent === void 0) {
|
|
10572
10612
|
newNode.color = 0 /* BLACK */;
|
|
10573
10613
|
this._size++;
|
|
10574
|
-
return;
|
|
10614
|
+
return false;
|
|
10575
10615
|
}
|
|
10576
10616
|
if (newNode.parent.parent === void 0) {
|
|
10577
10617
|
this._size++;
|
|
10578
|
-
return;
|
|
10618
|
+
return false;
|
|
10579
10619
|
}
|
|
10580
10620
|
this._fixInsert(newNode);
|
|
10581
10621
|
this._size++;
|
|
10622
|
+
return true;
|
|
10582
10623
|
}
|
|
10583
10624
|
/**
|
|
10584
|
-
* Time Complexity: O(log n)
|
|
10625
|
+
* Time Complexity: O(log n)
|
|
10585
10626
|
* Space Complexity: O(1)
|
|
10627
|
+
* on average (where n is the number of nodes in the tree)
|
|
10586
10628
|
*/
|
|
10587
10629
|
/**
|
|
10588
|
-
* Time Complexity: O(log n)
|
|
10630
|
+
* Time Complexity: O(log n)
|
|
10589
10631
|
* Space Complexity: O(1)
|
|
10590
10632
|
*
|
|
10591
10633
|
* The `delete` function removes a node from a binary tree based on a given identifier and updates
|
|
@@ -10653,20 +10695,11 @@ var dataStructureTyped = (() => {
|
|
|
10653
10695
|
return ans;
|
|
10654
10696
|
}
|
|
10655
10697
|
/**
|
|
10656
|
-
* Time Complexity: O(log n)
|
|
10657
|
-
* Space Complexity: O(1)
|
|
10658
|
-
*/
|
|
10659
|
-
isRealNode(node) {
|
|
10660
|
-
if (node === this.Sentinel || node === void 0)
|
|
10661
|
-
return false;
|
|
10662
|
-
return node instanceof RedBlackTreeNode;
|
|
10663
|
-
}
|
|
10664
|
-
/**
|
|
10665
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
10698
|
+
* Time Complexity: O(log n)
|
|
10666
10699
|
* Space Complexity: O(1)
|
|
10667
10700
|
*/
|
|
10668
10701
|
/**
|
|
10669
|
-
* Time Complexity: O(log n)
|
|
10702
|
+
* Time Complexity: O(log n)
|
|
10670
10703
|
* Space Complexity: O(1)
|
|
10671
10704
|
*
|
|
10672
10705
|
* The function `getNode` retrieves a single node from a binary tree based on a given identifier and
|
|
@@ -10718,7 +10751,7 @@ var dataStructureTyped = (() => {
|
|
|
10718
10751
|
return y;
|
|
10719
10752
|
}
|
|
10720
10753
|
/**
|
|
10721
|
-
* Time Complexity: O(
|
|
10754
|
+
* Time Complexity: O(1)
|
|
10722
10755
|
* Space Complexity: O(1)
|
|
10723
10756
|
*/
|
|
10724
10757
|
clear() {
|
|
@@ -10795,11 +10828,11 @@ var dataStructureTyped = (() => {
|
|
|
10795
10828
|
}
|
|
10796
10829
|
}
|
|
10797
10830
|
/**
|
|
10798
|
-
* Time Complexity: O(log n)
|
|
10831
|
+
* Time Complexity: O(log n)
|
|
10799
10832
|
* Space Complexity: O(1)
|
|
10800
10833
|
*/
|
|
10801
10834
|
/**
|
|
10802
|
-
* Time Complexity: O(log n)
|
|
10835
|
+
* Time Complexity: O(log n)
|
|
10803
10836
|
* Space Complexity: O(1)
|
|
10804
10837
|
*
|
|
10805
10838
|
* The function `_fixDelete` is used to fix the red-black tree after a node deletion.
|
|
@@ -10889,11 +10922,11 @@ var dataStructureTyped = (() => {
|
|
|
10889
10922
|
v.parent = u.parent;
|
|
10890
10923
|
}
|
|
10891
10924
|
/**
|
|
10892
|
-
* Time Complexity: O(log n)
|
|
10925
|
+
* Time Complexity: O(log n)
|
|
10893
10926
|
* Space Complexity: O(1)
|
|
10894
10927
|
*/
|
|
10895
10928
|
/**
|
|
10896
|
-
* Time Complexity: O(log n)
|
|
10929
|
+
* Time Complexity: O(log n)
|
|
10897
10930
|
* Space Complexity: O(1)
|
|
10898
10931
|
*
|
|
10899
10932
|
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
@@ -10976,11 +11009,11 @@ var dataStructureTyped = (() => {
|
|
|
10976
11009
|
}
|
|
10977
11010
|
};
|
|
10978
11011
|
var TreeMultimap = class _TreeMultimap extends AVLTree {
|
|
10979
|
-
constructor(
|
|
11012
|
+
constructor(nodes, options) {
|
|
10980
11013
|
super([], options);
|
|
10981
11014
|
__publicField(this, "_count", 0);
|
|
10982
|
-
if (
|
|
10983
|
-
this.addMany(
|
|
11015
|
+
if (nodes)
|
|
11016
|
+
this.addMany(nodes);
|
|
10984
11017
|
}
|
|
10985
11018
|
// TODO the _count is not accurate after nodes count modified
|
|
10986
11019
|
get count() {
|
|
@@ -11007,26 +11040,8 @@ var dataStructureTyped = (() => {
|
|
|
11007
11040
|
}, options));
|
|
11008
11041
|
}
|
|
11009
11042
|
/**
|
|
11010
|
-
* The function
|
|
11011
|
-
* @param
|
|
11012
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
11013
|
-
* class.
|
|
11014
|
-
*/
|
|
11015
|
-
isNode(exemplar) {
|
|
11016
|
-
return exemplar instanceof TreeMultimapNode;
|
|
11017
|
-
}
|
|
11018
|
-
/**
|
|
11019
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
11020
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
11021
|
-
* data type.
|
|
11022
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
11023
|
-
*/
|
|
11024
|
-
isNotNodeInstance(potentialKey) {
|
|
11025
|
-
return !(potentialKey instanceof TreeMultimapNode);
|
|
11026
|
-
}
|
|
11027
|
-
/**
|
|
11028
|
-
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
11029
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, which means it
|
|
11043
|
+
* The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
|
|
11044
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, which means it
|
|
11030
11045
|
* can be one of the following:
|
|
11031
11046
|
* @param {V} [value] - The `value` parameter is an optional argument that represents the value
|
|
11032
11047
|
* associated with the node. It is of type `V`, which can be any data type. If no value is provided,
|
|
@@ -11035,33 +11050,52 @@ var dataStructureTyped = (() => {
|
|
|
11035
11050
|
* times the value should be added to the node. If not provided, it defaults to 1.
|
|
11036
11051
|
* @returns a node of type `N` or `undefined`.
|
|
11037
11052
|
*/
|
|
11038
|
-
exemplarToNode(
|
|
11053
|
+
exemplarToNode(keyOrNodeOrEntry, value, count = 1) {
|
|
11039
11054
|
let node;
|
|
11040
|
-
if (
|
|
11055
|
+
if (keyOrNodeOrEntry === void 0 || keyOrNodeOrEntry === null) {
|
|
11041
11056
|
return;
|
|
11042
|
-
} else if (this.isNode(
|
|
11043
|
-
node =
|
|
11044
|
-
} else if (this.isEntry(
|
|
11045
|
-
const [key, value2] =
|
|
11057
|
+
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
11058
|
+
node = keyOrNodeOrEntry;
|
|
11059
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
11060
|
+
const [key, value2] = keyOrNodeOrEntry;
|
|
11046
11061
|
if (key === void 0 || key === null) {
|
|
11047
11062
|
return;
|
|
11048
11063
|
} else {
|
|
11049
11064
|
node = this.createNode(key, value2, count);
|
|
11050
11065
|
}
|
|
11051
|
-
} else if (this.isNotNodeInstance(
|
|
11052
|
-
node = this.createNode(
|
|
11066
|
+
} else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
11067
|
+
node = this.createNode(keyOrNodeOrEntry, value, count);
|
|
11053
11068
|
} else {
|
|
11054
11069
|
return;
|
|
11055
11070
|
}
|
|
11056
11071
|
return node;
|
|
11057
11072
|
}
|
|
11058
11073
|
/**
|
|
11059
|
-
*
|
|
11060
|
-
*
|
|
11074
|
+
* The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class.
|
|
11075
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
11076
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode
|
|
11077
|
+
* class.
|
|
11061
11078
|
*/
|
|
11079
|
+
isNode(keyOrNodeOrEntry) {
|
|
11080
|
+
return keyOrNodeOrEntry instanceof TreeMultimapNode;
|
|
11081
|
+
}
|
|
11062
11082
|
/**
|
|
11063
|
-
*
|
|
11064
|
-
*
|
|
11083
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
11084
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
11085
|
+
* data type.
|
|
11086
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
11087
|
+
*/
|
|
11088
|
+
isNotNodeInstance(potentialKey) {
|
|
11089
|
+
return !(potentialKey instanceof TreeMultimapNode);
|
|
11090
|
+
}
|
|
11091
|
+
/**
|
|
11092
|
+
* Time Complexity: O(log n)
|
|
11093
|
+
* Space Complexity: O(1)
|
|
11094
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
11095
|
+
*/
|
|
11096
|
+
/**
|
|
11097
|
+
* Time Complexity: O(log n)
|
|
11098
|
+
* Space Complexity: O(1)
|
|
11065
11099
|
*
|
|
11066
11100
|
* The function overrides the add method of a binary tree node and adds a new node to the tree.
|
|
11067
11101
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
@@ -11078,21 +11112,22 @@ var dataStructureTyped = (() => {
|
|
|
11078
11112
|
add(keyOrNodeOrEntry, value, count = 1) {
|
|
11079
11113
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count);
|
|
11080
11114
|
if (newNode === void 0)
|
|
11081
|
-
return;
|
|
11115
|
+
return false;
|
|
11082
11116
|
const orgNodeCount = (newNode == null ? void 0 : newNode.count) || 0;
|
|
11083
11117
|
const inserted = super.add(newNode);
|
|
11084
11118
|
if (inserted) {
|
|
11085
11119
|
this._count += orgNodeCount;
|
|
11086
11120
|
}
|
|
11087
|
-
return
|
|
11121
|
+
return true;
|
|
11088
11122
|
}
|
|
11089
11123
|
/**
|
|
11090
|
-
* Time Complexity: O(k log n)
|
|
11091
|
-
* Space Complexity: O(1)
|
|
11124
|
+
* Time Complexity: O(k log n)
|
|
11125
|
+
* Space Complexity: O(1)
|
|
11126
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
11092
11127
|
*/
|
|
11093
11128
|
/**
|
|
11094
|
-
* Time Complexity: O(k log n)
|
|
11095
|
-
* Space Complexity: O(1)
|
|
11129
|
+
* Time Complexity: O(k log n)
|
|
11130
|
+
* Space Complexity: O(1)
|
|
11096
11131
|
*
|
|
11097
11132
|
* The function overrides the addMany method to add multiple keys, nodes, or entries to a data
|
|
11098
11133
|
* structure.
|
|
@@ -11104,12 +11139,13 @@ var dataStructureTyped = (() => {
|
|
|
11104
11139
|
return super.addMany(keysOrNodesOrEntries);
|
|
11105
11140
|
}
|
|
11106
11141
|
/**
|
|
11107
|
-
* Time Complexity: O(
|
|
11108
|
-
* Space Complexity: O(
|
|
11142
|
+
* Time Complexity: O(n log n)
|
|
11143
|
+
* Space Complexity: O(n)
|
|
11144
|
+
* logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node. linear space, as it creates an array to store the sorted nodes.
|
|
11109
11145
|
*/
|
|
11110
11146
|
/**
|
|
11111
|
-
* Time Complexity: O(n log n)
|
|
11112
|
-
* Space Complexity: O(n)
|
|
11147
|
+
* Time Complexity: O(n log n)
|
|
11148
|
+
* Space Complexity: O(n)
|
|
11113
11149
|
*
|
|
11114
11150
|
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
11115
11151
|
* tree using either a recursive or iterative approach.
|
|
@@ -11154,12 +11190,13 @@ var dataStructureTyped = (() => {
|
|
|
11154
11190
|
}
|
|
11155
11191
|
}
|
|
11156
11192
|
/**
|
|
11157
|
-
* Time Complexity: O(k log n)
|
|
11158
|
-
* Space Complexity: O(1)
|
|
11193
|
+
* Time Complexity: O(k log n)
|
|
11194
|
+
* Space Complexity: O(1)
|
|
11195
|
+
* logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each. constant space, as it doesn't use additional data structures that scale with input size.
|
|
11159
11196
|
*/
|
|
11160
11197
|
/**
|
|
11161
|
-
* Time Complexity: O(log n)
|
|
11162
|
-
* Space Complexity: O(1)
|
|
11198
|
+
* Time Complexity: O(k log n)
|
|
11199
|
+
* Space Complexity: O(1)
|
|
11163
11200
|
*
|
|
11164
11201
|
* The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
|
|
11165
11202
|
* account the count of the node and balancing the tree if necessary.
|
|
@@ -11229,10 +11266,13 @@ var dataStructureTyped = (() => {
|
|
|
11229
11266
|
return deletedResult;
|
|
11230
11267
|
}
|
|
11231
11268
|
/**
|
|
11232
|
-
* Time Complexity: O(
|
|
11233
|
-
* Space Complexity: O(
|
|
11269
|
+
* Time Complexity: O(1)
|
|
11270
|
+
* Space Complexity: O(1)
|
|
11234
11271
|
*/
|
|
11235
11272
|
/**
|
|
11273
|
+
* Time Complexity: O(1)
|
|
11274
|
+
* Space Complexity: O(1)
|
|
11275
|
+
*
|
|
11236
11276
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
11237
11277
|
*/
|
|
11238
11278
|
clear() {
|
|
@@ -11255,45 +11295,6 @@ var dataStructureTyped = (() => {
|
|
|
11255
11295
|
this.bfs((node) => cloned.add(node.key, node.value, node.count));
|
|
11256
11296
|
return cloned;
|
|
11257
11297
|
}
|
|
11258
|
-
/**
|
|
11259
|
-
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
11260
|
-
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
11261
|
-
*
|
|
11262
|
-
* The function adds a new node to a binary tree, either as the left child or the right child of a
|
|
11263
|
-
* given parent node.
|
|
11264
|
-
* @param {N | undefined} newNode - The `newNode` parameter represents the node that needs to be
|
|
11265
|
-
* added to the binary tree. It can be of type `N` (which represents a node in the binary tree) or
|
|
11266
|
-
* `undefined` if there is no node to add.
|
|
11267
|
-
* @param {K | N | undefined} parent - The `parent` parameter represents the parent node to
|
|
11268
|
-
* which the new node will be added as a child. It can be either a node object (`N`) or a key value
|
|
11269
|
-
* (`K`).
|
|
11270
|
-
* @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
|
|
11271
|
-
* added, or `undefined` if no node was added.
|
|
11272
|
-
*/
|
|
11273
|
-
_addTo(newNode, parent) {
|
|
11274
|
-
parent = this.ensureNode(parent);
|
|
11275
|
-
if (parent) {
|
|
11276
|
-
if (parent.left === void 0) {
|
|
11277
|
-
parent.left = newNode;
|
|
11278
|
-
if (newNode !== void 0) {
|
|
11279
|
-
this._size = this.size + 1;
|
|
11280
|
-
this._count += newNode.count;
|
|
11281
|
-
}
|
|
11282
|
-
return parent.left;
|
|
11283
|
-
} else if (parent.right === void 0) {
|
|
11284
|
-
parent.right = newNode;
|
|
11285
|
-
if (newNode !== void 0) {
|
|
11286
|
-
this._size = this.size + 1;
|
|
11287
|
-
this._count += newNode.count;
|
|
11288
|
-
}
|
|
11289
|
-
return parent.right;
|
|
11290
|
-
} else {
|
|
11291
|
-
return;
|
|
11292
|
-
}
|
|
11293
|
-
} else {
|
|
11294
|
-
return;
|
|
11295
|
-
}
|
|
11296
|
-
}
|
|
11297
11298
|
/**
|
|
11298
11299
|
* The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
|
|
11299
11300
|
* @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node from
|
|
@@ -11408,488 +11409,397 @@ var dataStructureTyped = (() => {
|
|
|
11408
11409
|
};
|
|
11409
11410
|
|
|
11410
11411
|
// src/data-structures/matrix/matrix.ts
|
|
11411
|
-
var
|
|
11412
|
-
/**
|
|
11413
|
-
* The constructor
|
|
11414
|
-
*
|
|
11415
|
-
* @param
|
|
11416
|
-
|
|
11417
|
-
|
|
11418
|
-
|
|
11419
|
-
|
|
11420
|
-
this
|
|
11421
|
-
|
|
11422
|
-
|
|
11423
|
-
|
|
11424
|
-
|
|
11425
|
-
|
|
11426
|
-
|
|
11427
|
-
|
|
11428
|
-
|
|
11429
|
-
|
|
11430
|
-
|
|
11431
|
-
|
|
11432
|
-
|
|
11433
|
-
|
|
11434
|
-
|
|
11435
|
-
|
|
11436
|
-
|
|
11437
|
-
|
|
11438
|
-
|
|
11439
|
-
|
|
11440
|
-
|
|
11441
|
-
|
|
11442
|
-
|
|
11443
|
-
|
|
11444
|
-
|
|
11445
|
-
|
|
11446
|
-
|
|
11447
|
-
|
|
11448
|
-
|
|
11412
|
+
var Matrix = class _Matrix {
|
|
11413
|
+
/**
|
|
11414
|
+
* The constructor function initializes a matrix object with the provided data and options, or with
|
|
11415
|
+
* default values if no options are provided.
|
|
11416
|
+
* @param {number[][]} data - A 2D array of numbers representing the data for the matrix.
|
|
11417
|
+
* @param [options] - The `options` parameter is an optional object that can contain the following
|
|
11418
|
+
* properties:
|
|
11419
|
+
*/
|
|
11420
|
+
constructor(data, options) {
|
|
11421
|
+
__publicField(this, "_rows", 0);
|
|
11422
|
+
__publicField(this, "_cols", 0);
|
|
11423
|
+
__publicField(this, "_data");
|
|
11424
|
+
var _a, _b, _c;
|
|
11425
|
+
if (options) {
|
|
11426
|
+
const { rows, cols, addFn, subtractFn, multiplyFn } = options;
|
|
11427
|
+
if (typeof rows === "number" && rows > 0)
|
|
11428
|
+
this._rows = rows;
|
|
11429
|
+
else
|
|
11430
|
+
this._rows = data.length;
|
|
11431
|
+
if (typeof cols === "number" && cols > 0)
|
|
11432
|
+
this._cols = cols;
|
|
11433
|
+
else
|
|
11434
|
+
this._cols = ((_a = data[0]) == null ? void 0 : _a.length) || 0;
|
|
11435
|
+
if (addFn)
|
|
11436
|
+
this._addFn = addFn;
|
|
11437
|
+
if (subtractFn)
|
|
11438
|
+
this._subtractFn = subtractFn;
|
|
11439
|
+
if (multiplyFn)
|
|
11440
|
+
this._multiplyFn = multiplyFn;
|
|
11441
|
+
} else {
|
|
11442
|
+
this._rows = data.length;
|
|
11443
|
+
this._cols = (_c = (_b = data[0]) == null ? void 0 : _b.length) != null ? _c : 0;
|
|
11444
|
+
}
|
|
11445
|
+
if (data.length > 0) {
|
|
11446
|
+
this._data = data;
|
|
11447
|
+
} else {
|
|
11448
|
+
this._data = [];
|
|
11449
|
+
for (let i = 0; i < this.rows; i++) {
|
|
11450
|
+
this._data[i] = new Array(this.cols).fill(0);
|
|
11451
|
+
}
|
|
11452
|
+
}
|
|
11449
11453
|
}
|
|
11450
|
-
|
|
11451
|
-
|
|
11452
|
-
* @returns The method is returning the sum of the squares of the x and y values.
|
|
11453
|
-
*/
|
|
11454
|
-
get lengthSq() {
|
|
11455
|
-
return this.x * this.x + this.y * this.y;
|
|
11454
|
+
get rows() {
|
|
11455
|
+
return this._rows;
|
|
11456
11456
|
}
|
|
11457
|
-
|
|
11458
|
-
|
|
11459
|
-
* @returns The method is returning a new instance of the Vector2D class with the x and y values rounded to the nearest
|
|
11460
|
-
* whole number.
|
|
11461
|
-
*/
|
|
11462
|
-
get rounded() {
|
|
11463
|
-
return new _Vector2D(Math.round(this.x), Math.round(this.y));
|
|
11457
|
+
get cols() {
|
|
11458
|
+
return this._cols;
|
|
11464
11459
|
}
|
|
11465
|
-
|
|
11466
|
-
|
|
11467
|
-
* x and y components.
|
|
11468
|
-
* @param {Vector2D} vector1 - The parameter `vector1` is an instance of the `Vector2D` class. It represents a
|
|
11469
|
-
* 2-dimensional vector with an `x` and `y` component.
|
|
11470
|
-
* @param {Vector2D} vector2 - The parameter "vector2" is of type Vector2D. It represents a 2-dimensional vector with
|
|
11471
|
-
* an x and y component.
|
|
11472
|
-
* @returns The method is returning a new instance of the Vector2D class with the x and y components of the two input
|
|
11473
|
-
* vectors added together.
|
|
11474
|
-
*/
|
|
11475
|
-
static add(vector1, vector2) {
|
|
11476
|
-
return new _Vector2D(vector1.x + vector2.x, vector1.y + vector2.y);
|
|
11460
|
+
get data() {
|
|
11461
|
+
return this._data;
|
|
11477
11462
|
}
|
|
11478
|
-
|
|
11479
|
-
|
|
11480
|
-
* components subtracted.
|
|
11481
|
-
* @param {Vector2D} vector1 - The parameter `vector1` is an instance of the `Vector2D` class, representing a
|
|
11482
|
-
* 2-dimensional vector. It has properties `x` and `y` which represent the x and y components of the vector
|
|
11483
|
-
* respectively.
|
|
11484
|
-
* @param {Vector2D} vector2 - The parameter "vector2" is a Vector2D object. It represents the second vector that you
|
|
11485
|
-
* want to subtract from the first vector.
|
|
11486
|
-
* @returns The method is returning a new Vector2D object with the x and y components subtracted from vector1 and
|
|
11487
|
-
* vector2.
|
|
11488
|
-
*/
|
|
11489
|
-
static subtract(vector1, vector2) {
|
|
11490
|
-
return new _Vector2D(vector1.x - vector2.x, vector1.y - vector2.y);
|
|
11463
|
+
get addFn() {
|
|
11464
|
+
return this._addFn;
|
|
11491
11465
|
}
|
|
11492
|
-
|
|
11493
|
-
|
|
11494
|
-
* object.
|
|
11495
|
-
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector with
|
|
11496
|
-
* x and y components.
|
|
11497
|
-
* @param {number} value - The "value" parameter is a number that will be subtracted from both the x and y components
|
|
11498
|
-
* of the "vector" parameter.
|
|
11499
|
-
* @returns A new Vector2D object with the x and y values subtracted by the given value.
|
|
11500
|
-
*/
|
|
11501
|
-
static subtractValue(vector, value) {
|
|
11502
|
-
return new _Vector2D(vector.x - value, vector.y - value);
|
|
11466
|
+
get subtractFn() {
|
|
11467
|
+
return this._subtractFn;
|
|
11503
11468
|
}
|
|
11504
|
-
|
|
11505
|
-
|
|
11506
|
-
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector with
|
|
11507
|
-
* x and y components.
|
|
11508
|
-
* @param {number} value - The "value" parameter is a number that represents the value by which the x and y components
|
|
11509
|
-
* of the vector will be multiplied.
|
|
11510
|
-
* @returns A new Vector2D object with the x and y values multiplied by the given value.
|
|
11511
|
-
*/
|
|
11512
|
-
static multiply(vector, value) {
|
|
11513
|
-
return new _Vector2D(vector.x * value, vector.y * value);
|
|
11469
|
+
get multiplyFn() {
|
|
11470
|
+
return this._multiplyFn;
|
|
11514
11471
|
}
|
|
11515
11472
|
/**
|
|
11516
|
-
* The function
|
|
11517
|
-
* @param {
|
|
11518
|
-
*
|
|
11519
|
-
* @param {number}
|
|
11520
|
-
*
|
|
11521
|
-
* @returns
|
|
11473
|
+
* The `get` function returns the value at the specified row and column index if it is a valid index.
|
|
11474
|
+
* @param {number} row - The `row` parameter represents the row index of the element you want to
|
|
11475
|
+
* retrieve from the data array.
|
|
11476
|
+
* @param {number} col - The parameter "col" represents the column number of the element you want to
|
|
11477
|
+
* retrieve from the data array.
|
|
11478
|
+
* @returns The `get` function returns a number if the provided row and column indices are valid.
|
|
11479
|
+
* Otherwise, it returns `undefined`.
|
|
11522
11480
|
*/
|
|
11523
|
-
|
|
11524
|
-
|
|
11525
|
-
|
|
11526
|
-
|
|
11527
|
-
* The function checks if two Vector2D objects are equal by comparing their x and y values.
|
|
11528
|
-
* @param {Vector2D} vector1 - The parameter `vector1` is of type `Vector2D`, which represents a 2-dimensional vector.
|
|
11529
|
-
* It has two properties: `x` and `y`, which represent the x and y components of the vector, respectively.
|
|
11530
|
-
* @param {Vector2D} vector2 - The parameter "vector2" is of type Vector2D.
|
|
11531
|
-
* @returns a boolean value, which indicates whether the two input vectors are equal or not.
|
|
11532
|
-
*/
|
|
11533
|
-
static equals(vector1, vector2) {
|
|
11534
|
-
return vector1.x === vector2.x && vector1.y === vector2.y;
|
|
11481
|
+
get(row, col) {
|
|
11482
|
+
if (this.isValidIndex(row, col)) {
|
|
11483
|
+
return this.data[row][col];
|
|
11484
|
+
}
|
|
11535
11485
|
}
|
|
11536
11486
|
/**
|
|
11537
|
-
* The function
|
|
11538
|
-
* @param {
|
|
11539
|
-
*
|
|
11540
|
-
*
|
|
11541
|
-
*
|
|
11542
|
-
*
|
|
11543
|
-
*
|
|
11544
|
-
* @returns a boolean value.
|
|
11487
|
+
* The set function updates the value at a specified row and column in a two-dimensional array.
|
|
11488
|
+
* @param {number} row - The "row" parameter represents the row index of the element in a
|
|
11489
|
+
* two-dimensional array or matrix. It specifies the row where the value will be set.
|
|
11490
|
+
* @param {number} col - The "col" parameter represents the column index of the element in a
|
|
11491
|
+
* two-dimensional array.
|
|
11492
|
+
* @param {number} value - The value parameter represents the number that you want to set at the
|
|
11493
|
+
* specified row and column in the data array.
|
|
11494
|
+
* @returns a boolean value. It returns true if the index (row, col) is valid and the value is
|
|
11495
|
+
* successfully set in the data array. It returns false if the index is invalid and the value is not
|
|
11496
|
+
* set.
|
|
11545
11497
|
*/
|
|
11546
|
-
|
|
11547
|
-
|
|
11548
|
-
|
|
11498
|
+
set(row, col, value) {
|
|
11499
|
+
if (this.isValidIndex(row, col)) {
|
|
11500
|
+
this.data[row][col] = value;
|
|
11549
11501
|
return true;
|
|
11550
11502
|
}
|
|
11551
11503
|
return false;
|
|
11552
11504
|
}
|
|
11553
11505
|
/**
|
|
11554
|
-
* The
|
|
11555
|
-
*
|
|
11556
|
-
* @
|
|
11557
|
-
*
|
|
11506
|
+
* The function checks if the dimensions of the given matrix match the dimensions of the current
|
|
11507
|
+
* matrix.
|
|
11508
|
+
* @param {Matrix} matrix - The parameter `matrix` is of type `Matrix`.
|
|
11509
|
+
* @returns a boolean value.
|
|
11558
11510
|
*/
|
|
11559
|
-
|
|
11560
|
-
|
|
11561
|
-
if (length > 2220446049250313e-31) {
|
|
11562
|
-
return _Vector2D.divide(vector, length);
|
|
11563
|
-
}
|
|
11564
|
-
return vector;
|
|
11511
|
+
isMatchForCalculate(matrix) {
|
|
11512
|
+
return this.rows === matrix.rows && this.cols === matrix.cols;
|
|
11565
11513
|
}
|
|
11566
11514
|
/**
|
|
11567
|
-
* The function
|
|
11568
|
-
* @param {
|
|
11569
|
-
* @
|
|
11570
|
-
*
|
|
11571
|
-
* @returns either the original vector or a truncated version of the vector, depending on whether the length of the
|
|
11572
|
-
* vector is greater than the maximum value specified.
|
|
11515
|
+
* The `add` function adds two matrices together, returning a new matrix with the result.
|
|
11516
|
+
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
11517
|
+
* @returns The `add` method returns a new `Matrix` object that represents the result of adding the
|
|
11518
|
+
* current matrix with the provided `matrix` parameter.
|
|
11573
11519
|
*/
|
|
11574
|
-
|
|
11575
|
-
if (
|
|
11576
|
-
|
|
11520
|
+
add(matrix) {
|
|
11521
|
+
if (!this.isMatchForCalculate(matrix)) {
|
|
11522
|
+
throw new Error("Matrix dimensions must match for addition.");
|
|
11577
11523
|
}
|
|
11578
|
-
|
|
11579
|
-
|
|
11580
|
-
|
|
11581
|
-
|
|
11582
|
-
|
|
11583
|
-
|
|
11584
|
-
|
|
11585
|
-
|
|
11586
|
-
|
|
11587
|
-
|
|
11588
|
-
|
|
11589
|
-
|
|
11590
|
-
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector. It
|
|
11591
|
-
* has two properties: "x" and "y", which represent the x and y components of the vector, respectively.
|
|
11592
|
-
* @returns A new Vector2D object with the negated x and y values of the input vector. Returns the vector that is the reverse of this vector
|
|
11593
|
-
*/
|
|
11594
|
-
static reverse(vector) {
|
|
11595
|
-
return new _Vector2D(-vector.x, -vector.y);
|
|
11596
|
-
}
|
|
11597
|
-
/**
|
|
11598
|
-
* The function takes a Vector2D object as input and returns a new Vector2D object with the absolute values of its x
|
|
11599
|
-
* and y components.
|
|
11600
|
-
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector. It
|
|
11601
|
-
* has two properties: "x" and "y", which represent the x and y components of the vector, respectively.
|
|
11602
|
-
* @returns The method is returning a new Vector2D object with the absolute values of the x and y components of the
|
|
11603
|
-
* input vector.
|
|
11604
|
-
*/
|
|
11605
|
-
static abs(vector) {
|
|
11606
|
-
return new _Vector2D(Math.abs(vector.x), Math.abs(vector.y));
|
|
11607
|
-
}
|
|
11608
|
-
/**
|
|
11609
|
-
* The dot function calculates the dot product of two 2D vectors.The dot product of v1 and v2
|
|
11610
|
-
* @param {Vector2D} vector1 - The parameter `vector1` represents a 2D vector with its x and y components.
|
|
11611
|
-
* @param {Vector2D} vector2 - The "vector2" parameter is a Vector2D object. It represents a two-dimensional vector
|
|
11612
|
-
* with an x and y component.
|
|
11613
|
-
* @returns The dot product of the two input vectors.
|
|
11614
|
-
*/
|
|
11615
|
-
static dot(vector1, vector2) {
|
|
11616
|
-
return vector1.x * vector2.x + vector1.y * vector2.y;
|
|
11617
|
-
}
|
|
11618
|
-
// /**
|
|
11619
|
-
// * Transform vectors based on the current tranformation matrices: translation, rotation and scale
|
|
11620
|
-
// * @param vectors The vectors to transform
|
|
11621
|
-
// */
|
|
11622
|
-
// static transform(vector: Vector2D, transformation: Matrix2D): Vector2D {
|
|
11623
|
-
// return Matrix2D.multiplyByVector(transformation, vector)
|
|
11624
|
-
// }
|
|
11625
|
-
// /**
|
|
11626
|
-
// * Transform vectors based on the current tranformation matrices: translation, rotation and scale
|
|
11627
|
-
// * @param vectors The vectors to transform
|
|
11628
|
-
// */
|
|
11629
|
-
// static transformList(vectors: Vector2D[], transformation: Matrix2D): Vector2D[] {
|
|
11630
|
-
// return vectors.map(vector => Matrix2D.multiplyByVector(transformation, vector))
|
|
11631
|
-
// }
|
|
11632
|
-
/**
|
|
11633
|
-
* The function calculates the distance between two points in a two-dimensional space.
|
|
11634
|
-
* @param {Vector2D} vector1 - The parameter `vector1` represents the first vector in 2D space, while `vector2`
|
|
11635
|
-
* represents the second vector. Each vector has an `x` and `y` component, which represent their respective coordinates
|
|
11636
|
-
* in the 2D space.
|
|
11637
|
-
* @param {Vector2D} vector2 - The `vector2` parameter represents the second vector in the calculation of distance. It
|
|
11638
|
-
* is an instance of the `Vector2D` class, which typically has properties `x` and `y` representing the coordinates of
|
|
11639
|
-
* the vector in a 2D space.
|
|
11640
|
-
* @returns The distance between vector1 and vector2.
|
|
11641
|
-
*/
|
|
11642
|
-
static distance(vector1, vector2) {
|
|
11643
|
-
const ySeparation = vector2.y - vector1.y;
|
|
11644
|
-
const xSeparation = vector2.x - vector1.x;
|
|
11645
|
-
return Math.sqrt(ySeparation * ySeparation + xSeparation * xSeparation);
|
|
11646
|
-
}
|
|
11647
|
-
/**
|
|
11648
|
-
* The function calculates the squared distance between two 2D vectors.
|
|
11649
|
-
* @param {Vector2D} vector1 - The parameter `vector1` represents the first vector, which is an instance of the
|
|
11650
|
-
* `Vector2D` class. It contains the x and y coordinates of the vector.
|
|
11651
|
-
* @param {Vector2D} vector2 - The `vector2` parameter represents the second vector in a two-dimensional space. It has
|
|
11652
|
-
* properties `x` and `y` which represent the coordinates of the vector.
|
|
11653
|
-
* @returns the square of the distance between the two input vectors.
|
|
11654
|
-
*/
|
|
11655
|
-
static distanceSq(vector1, vector2) {
|
|
11656
|
-
const ySeparation = vector2.y - vector1.y;
|
|
11657
|
-
const xSeparation = vector2.x - vector1.x;
|
|
11658
|
-
return ySeparation * ySeparation + xSeparation * xSeparation;
|
|
11659
|
-
}
|
|
11660
|
-
/**
|
|
11661
|
-
* The sign function determines the sign of the cross product between two 2D vectors.
|
|
11662
|
-
* (assuming the Y axis is pointing down, X axis to right like a Window app)
|
|
11663
|
-
* @param {Vector2D} vector1 - The parameter `vector1` is of type `Vector2D`, which represents a 2-dimensional vector.
|
|
11664
|
-
* It likely has properties `x` and `y` representing the x and y components of the vector, respectively.
|
|
11665
|
-
* @param {Vector2D} vector2 - The above code defines a function called "sign" that takes two parameters: vector1 and
|
|
11666
|
-
* vector2. Both vector1 and vector2 are of type Vector2D.
|
|
11667
|
-
* @returns either -1 or 1. Returns positive if v2 is clockwise of this vector, negative if counterclockwise
|
|
11668
|
-
*/
|
|
11669
|
-
static sign(vector1, vector2) {
|
|
11670
|
-
if (vector1.y * vector2.x > vector1.x * vector2.y) {
|
|
11671
|
-
return -1;
|
|
11524
|
+
const resultData = [];
|
|
11525
|
+
for (let i = 0; i < this.rows; i++) {
|
|
11526
|
+
resultData[i] = [];
|
|
11527
|
+
for (let j = 0; j < this.cols; j++) {
|
|
11528
|
+
const a = this.get(i, j), b = matrix.get(i, j);
|
|
11529
|
+
if (a !== void 0 && b !== void 0) {
|
|
11530
|
+
const added = this._addFn(a, b);
|
|
11531
|
+
if (added) {
|
|
11532
|
+
resultData[i][j] = added;
|
|
11533
|
+
}
|
|
11534
|
+
}
|
|
11535
|
+
}
|
|
11672
11536
|
}
|
|
11673
|
-
return
|
|
11674
|
-
|
|
11675
|
-
|
|
11676
|
-
|
|
11677
|
-
|
|
11678
|
-
|
|
11679
|
-
|
|
11680
|
-
* @returns the angle between the given vector and the vector (0, -1) in radians.Returns the angle between origin and the given vector in radians
|
|
11681
|
-
*/
|
|
11682
|
-
static angle(vector) {
|
|
11683
|
-
const origin = new _Vector2D(0, -1);
|
|
11684
|
-
const radian = Math.acos(_Vector2D.dot(vector, origin) / (vector.length * origin.length));
|
|
11685
|
-
return _Vector2D.sign(vector, origin) === 1 ? Math.PI * 2 - radian : radian;
|
|
11686
|
-
}
|
|
11687
|
-
/**
|
|
11688
|
-
* The function "random" generates a random Vector2D object with x and y values within the specified range.
|
|
11689
|
-
* @param {number} maxX - The maxX parameter represents the maximum value for the x-coordinate of the random vector.
|
|
11690
|
-
* @param {number} maxY - The `maxY` parameter represents the maximum value for the y-coordinate of the generated
|
|
11691
|
-
* random vector.
|
|
11692
|
-
* @returns a new instance of the Vector2D class with random x and y values.
|
|
11693
|
-
*/
|
|
11694
|
-
static random(maxX, maxY) {
|
|
11695
|
-
const randX = Math.floor(Math.random() * maxX - maxX / 2);
|
|
11696
|
-
const randY = Math.floor(Math.random() * maxY - maxY / 2);
|
|
11697
|
-
return new _Vector2D(randX, randY);
|
|
11698
|
-
}
|
|
11699
|
-
/**
|
|
11700
|
-
* The function sets the values of x and y to zero.
|
|
11701
|
-
*/
|
|
11702
|
-
zero() {
|
|
11703
|
-
this.x = 0;
|
|
11704
|
-
this.y = 0;
|
|
11537
|
+
return new _Matrix(resultData, {
|
|
11538
|
+
rows: this.rows,
|
|
11539
|
+
cols: this.cols,
|
|
11540
|
+
addFn: this.addFn,
|
|
11541
|
+
subtractFn: this.subtractFn,
|
|
11542
|
+
multiplyFn: this.multiplyFn
|
|
11543
|
+
});
|
|
11705
11544
|
}
|
|
11706
|
-
};
|
|
11707
|
-
|
|
11708
|
-
// src/data-structures/matrix/matrix2d.ts
|
|
11709
|
-
var Matrix2D = class _Matrix2D {
|
|
11710
11545
|
/**
|
|
11711
|
-
* The
|
|
11712
|
-
*
|
|
11713
|
-
* @param {
|
|
11714
|
-
*
|
|
11715
|
-
|
|
11716
|
-
|
|
11717
|
-
|
|
11718
|
-
if (
|
|
11719
|
-
|
|
11720
|
-
}
|
|
11721
|
-
|
|
11722
|
-
|
|
11723
|
-
|
|
11724
|
-
|
|
11725
|
-
|
|
11726
|
-
|
|
11546
|
+
* The `subtract` function performs element-wise subtraction between two matrices and returns a new
|
|
11547
|
+
* matrix with the result.
|
|
11548
|
+
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class. It
|
|
11549
|
+
* represents the matrix that you want to subtract from the current matrix.
|
|
11550
|
+
* @returns a new Matrix object with the result of the subtraction operation.
|
|
11551
|
+
*/
|
|
11552
|
+
subtract(matrix) {
|
|
11553
|
+
if (!this.isMatchForCalculate(matrix)) {
|
|
11554
|
+
throw new Error("Matrix dimensions must match for subtraction.");
|
|
11555
|
+
}
|
|
11556
|
+
const resultData = [];
|
|
11557
|
+
for (let i = 0; i < this.rows; i++) {
|
|
11558
|
+
resultData[i] = [];
|
|
11559
|
+
for (let j = 0; j < this.cols; j++) {
|
|
11560
|
+
const a = this.get(i, j), b = matrix.get(i, j);
|
|
11561
|
+
if (a !== void 0 && b !== void 0) {
|
|
11562
|
+
const subtracted = this._subtractFn(a, b);
|
|
11563
|
+
if (subtracted) {
|
|
11564
|
+
resultData[i][j] = subtracted;
|
|
11565
|
+
}
|
|
11566
|
+
}
|
|
11567
|
+
}
|
|
11727
11568
|
}
|
|
11569
|
+
return new _Matrix(resultData, {
|
|
11570
|
+
rows: this.rows,
|
|
11571
|
+
cols: this.cols,
|
|
11572
|
+
addFn: this.addFn,
|
|
11573
|
+
subtractFn: this.subtractFn,
|
|
11574
|
+
multiplyFn: this.multiplyFn
|
|
11575
|
+
});
|
|
11728
11576
|
}
|
|
11729
11577
|
/**
|
|
11730
|
-
* The function
|
|
11731
|
-
*
|
|
11732
|
-
|
|
11733
|
-
|
|
11734
|
-
|
|
11735
|
-
|
|
11736
|
-
|
|
11737
|
-
|
|
11738
|
-
|
|
11739
|
-
|
|
11740
|
-
|
|
11741
|
-
|
|
11742
|
-
|
|
11743
|
-
|
|
11744
|
-
|
|
11745
|
-
|
|
11746
|
-
|
|
11747
|
-
|
|
11748
|
-
|
|
11749
|
-
|
|
11750
|
-
|
|
11751
|
-
|
|
11752
|
-
|
|
11753
|
-
|
|
11754
|
-
|
|
11755
|
-
/**
|
|
11756
|
-
* The function takes two 2D matrices as input and returns their sum as a new 2D matrix.
|
|
11757
|
-
* @param {Matrix2D} matrix1 - Matrix2D - The first matrix to be added.
|
|
11758
|
-
* @param {Matrix2D} matrix2 - The parameter `matrix2` is a Matrix2D object.
|
|
11759
|
-
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
11760
|
-
*/
|
|
11761
|
-
static add(matrix1, matrix2) {
|
|
11762
|
-
const result = _Matrix2D.empty;
|
|
11763
|
-
for (let i = 0; i < 3; i++) {
|
|
11764
|
-
for (let j = 0; j < 3; j++) {
|
|
11765
|
-
result[i][j] = matrix1.m[i][j] + matrix2.m[i][j];
|
|
11578
|
+
* The `multiply` function performs matrix multiplication between two matrices and returns the result
|
|
11579
|
+
* as a new matrix.
|
|
11580
|
+
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
11581
|
+
* @returns a new Matrix object.
|
|
11582
|
+
*/
|
|
11583
|
+
multiply(matrix) {
|
|
11584
|
+
if (this.cols !== matrix.rows) {
|
|
11585
|
+
throw new Error("Matrix dimensions must be compatible for multiplication (A.cols = B.rows).");
|
|
11586
|
+
}
|
|
11587
|
+
const resultData = [];
|
|
11588
|
+
for (let i = 0; i < this.rows; i++) {
|
|
11589
|
+
resultData[i] = [];
|
|
11590
|
+
for (let j = 0; j < matrix.cols; j++) {
|
|
11591
|
+
let sum;
|
|
11592
|
+
for (let k = 0; k < this.cols; k++) {
|
|
11593
|
+
const a = this.get(i, k), b = matrix.get(k, j);
|
|
11594
|
+
if (a !== void 0 && b !== void 0) {
|
|
11595
|
+
const multiplied = this.multiplyFn(a, b);
|
|
11596
|
+
if (multiplied !== void 0) {
|
|
11597
|
+
sum = this.addFn(sum, multiplied);
|
|
11598
|
+
}
|
|
11599
|
+
}
|
|
11600
|
+
}
|
|
11601
|
+
if (sum !== void 0)
|
|
11602
|
+
resultData[i][j] = sum;
|
|
11766
11603
|
}
|
|
11767
11604
|
}
|
|
11768
|
-
return new
|
|
11605
|
+
return new _Matrix(resultData, {
|
|
11606
|
+
rows: this.rows,
|
|
11607
|
+
cols: matrix.cols,
|
|
11608
|
+
addFn: this.addFn,
|
|
11609
|
+
subtractFn: this.subtractFn,
|
|
11610
|
+
multiplyFn: this.multiplyFn
|
|
11611
|
+
});
|
|
11769
11612
|
}
|
|
11770
11613
|
/**
|
|
11771
|
-
* The function
|
|
11772
|
-
*
|
|
11773
|
-
* @
|
|
11774
|
-
* representing the matrix elements.
|
|
11775
|
-
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
11614
|
+
* The transpose function takes a matrix and returns a new matrix that is the transpose of the
|
|
11615
|
+
* original matrix.
|
|
11616
|
+
* @returns The transpose() function returns a new Matrix object with the transposed data.
|
|
11776
11617
|
*/
|
|
11777
|
-
|
|
11778
|
-
|
|
11779
|
-
|
|
11780
|
-
|
|
11781
|
-
|
|
11618
|
+
transpose() {
|
|
11619
|
+
if (this.data.some((row) => row.length !== this.rows)) {
|
|
11620
|
+
throw new Error("Matrix must be rectangular for transposition.");
|
|
11621
|
+
}
|
|
11622
|
+
const resultData = [];
|
|
11623
|
+
for (let j = 0; j < this.cols; j++) {
|
|
11624
|
+
resultData[j] = [];
|
|
11625
|
+
for (let i = 0; i < this.rows; i++) {
|
|
11626
|
+
const trans = this.get(i, j);
|
|
11627
|
+
if (trans !== void 0)
|
|
11628
|
+
resultData[j][i] = trans;
|
|
11782
11629
|
}
|
|
11783
11630
|
}
|
|
11784
|
-
return new
|
|
11631
|
+
return new _Matrix(resultData, {
|
|
11632
|
+
rows: this.cols,
|
|
11633
|
+
cols: this.rows,
|
|
11634
|
+
addFn: this.addFn,
|
|
11635
|
+
subtractFn: this.subtractFn,
|
|
11636
|
+
multiplyFn: this.multiplyFn
|
|
11637
|
+
});
|
|
11785
11638
|
}
|
|
11786
11639
|
/**
|
|
11787
|
-
* The function
|
|
11788
|
-
* @
|
|
11789
|
-
* @param {Matrix2D} matrix2 - The parameter `matrix2` is a 2D matrix of size 3x3.
|
|
11790
|
-
* @returns a new instance of the Matrix2D class, created using the result array.
|
|
11640
|
+
* The `inverse` function calculates the inverse of a square matrix using Gaussian elimination.
|
|
11641
|
+
* @returns a Matrix object, which represents the inverse of the original matrix.
|
|
11791
11642
|
*/
|
|
11792
|
-
|
|
11793
|
-
|
|
11794
|
-
|
|
11795
|
-
|
|
11796
|
-
|
|
11797
|
-
|
|
11798
|
-
|
|
11643
|
+
inverse() {
|
|
11644
|
+
var _a;
|
|
11645
|
+
if (this.rows !== this.cols) {
|
|
11646
|
+
throw new Error("Matrix must be square for inversion.");
|
|
11647
|
+
}
|
|
11648
|
+
const augmentedMatrixData = [];
|
|
11649
|
+
for (let i = 0; i < this.rows; i++) {
|
|
11650
|
+
augmentedMatrixData[i] = this.data[i].slice();
|
|
11651
|
+
for (let j = 0; j < this.cols; j++) {
|
|
11652
|
+
augmentedMatrixData[i][this.cols + j] = i === j ? 1 : 0;
|
|
11653
|
+
}
|
|
11654
|
+
}
|
|
11655
|
+
const augmentedMatrix = new _Matrix(augmentedMatrixData, {
|
|
11656
|
+
rows: this.rows,
|
|
11657
|
+
cols: this.cols * 2,
|
|
11658
|
+
addFn: this.addFn,
|
|
11659
|
+
subtractFn: this.subtractFn,
|
|
11660
|
+
multiplyFn: this.multiplyFn
|
|
11661
|
+
});
|
|
11662
|
+
for (let i = 0; i < this.rows; i++) {
|
|
11663
|
+
let pivotRow = i;
|
|
11664
|
+
while (pivotRow < this.rows && augmentedMatrix.get(pivotRow, i) === 0) {
|
|
11665
|
+
pivotRow++;
|
|
11666
|
+
}
|
|
11667
|
+
if (pivotRow === this.rows) {
|
|
11668
|
+
throw new Error("Matrix is singular, and its inverse does not exist.");
|
|
11669
|
+
}
|
|
11670
|
+
augmentedMatrix._swapRows(i, pivotRow);
|
|
11671
|
+
const pivotElement = (_a = augmentedMatrix.get(i, i)) != null ? _a : 1;
|
|
11672
|
+
if (pivotElement === 0) {
|
|
11673
|
+
throw new Error("Matrix is singular, and its inverse does not exist (division by zero).");
|
|
11674
|
+
}
|
|
11675
|
+
augmentedMatrix._scaleRow(i, 1 / pivotElement);
|
|
11676
|
+
for (let j = 0; j < this.rows; j++) {
|
|
11677
|
+
if (j !== i) {
|
|
11678
|
+
let factor = augmentedMatrix.get(j, i);
|
|
11679
|
+
if (factor === void 0)
|
|
11680
|
+
factor = 0;
|
|
11681
|
+
augmentedMatrix._addScaledRow(j, i, -factor);
|
|
11799
11682
|
}
|
|
11800
11683
|
}
|
|
11801
11684
|
}
|
|
11802
|
-
|
|
11685
|
+
const inverseData = [];
|
|
11686
|
+
for (let i = 0; i < this.rows; i++) {
|
|
11687
|
+
inverseData[i] = augmentedMatrix.data[i].slice(this.cols);
|
|
11688
|
+
}
|
|
11689
|
+
return new _Matrix(inverseData, {
|
|
11690
|
+
rows: this.rows,
|
|
11691
|
+
cols: this.cols,
|
|
11692
|
+
addFn: this.addFn,
|
|
11693
|
+
subtractFn: this.subtractFn,
|
|
11694
|
+
multiplyFn: this.multiplyFn
|
|
11695
|
+
});
|
|
11803
11696
|
}
|
|
11804
11697
|
/**
|
|
11805
|
-
* The function
|
|
11806
|
-
* @param {
|
|
11807
|
-
*
|
|
11808
|
-
|
|
11809
|
-
|
|
11810
|
-
|
|
11811
|
-
|
|
11812
|
-
|
|
11813
|
-
|
|
11814
|
-
|
|
11815
|
-
|
|
11698
|
+
* The dot function calculates the dot product of two matrices and returns a new matrix.
|
|
11699
|
+
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
11700
|
+
* @returns a new Matrix object.
|
|
11701
|
+
*/
|
|
11702
|
+
dot(matrix) {
|
|
11703
|
+
if (this.cols !== matrix.rows) {
|
|
11704
|
+
throw new Error(
|
|
11705
|
+
"Number of columns in the first matrix must be equal to the number of rows in the second matrix for dot product."
|
|
11706
|
+
);
|
|
11707
|
+
}
|
|
11708
|
+
const resultData = [];
|
|
11709
|
+
for (let i = 0; i < this.rows; i++) {
|
|
11710
|
+
resultData[i] = [];
|
|
11711
|
+
for (let j = 0; j < matrix.cols; j++) {
|
|
11712
|
+
let sum;
|
|
11713
|
+
for (let k = 0; k < this.cols; k++) {
|
|
11714
|
+
const a = this.get(i, k), b = matrix.get(k, j);
|
|
11715
|
+
if (a !== void 0 && b !== void 0) {
|
|
11716
|
+
const multiplied = this.multiplyFn(a, b);
|
|
11717
|
+
if (multiplied !== void 0) {
|
|
11718
|
+
sum = this.addFn(sum, multiplied);
|
|
11719
|
+
}
|
|
11720
|
+
}
|
|
11721
|
+
}
|
|
11722
|
+
if (sum !== void 0)
|
|
11723
|
+
resultData[i][j] = sum;
|
|
11816
11724
|
}
|
|
11817
11725
|
}
|
|
11818
|
-
return new
|
|
11726
|
+
return new _Matrix(resultData, {
|
|
11727
|
+
rows: this.rows,
|
|
11728
|
+
cols: matrix.cols,
|
|
11729
|
+
addFn: this.addFn,
|
|
11730
|
+
subtractFn: this.subtractFn,
|
|
11731
|
+
multiplyFn: this.multiplyFn
|
|
11732
|
+
});
|
|
11819
11733
|
}
|
|
11820
|
-
|
|
11821
|
-
|
|
11822
|
-
|
|
11823
|
-
|
|
11824
|
-
* @returns a Vector2D.
|
|
11825
|
-
*/
|
|
11826
|
-
static multiplyByVector(matrix, vector) {
|
|
11827
|
-
const resultMatrix = _Matrix2D.multiply(matrix, new _Matrix2D(vector));
|
|
11828
|
-
return resultMatrix.toVector();
|
|
11734
|
+
_addFn(a, b) {
|
|
11735
|
+
if (a === void 0)
|
|
11736
|
+
return b;
|
|
11737
|
+
return a + b;
|
|
11829
11738
|
}
|
|
11830
|
-
|
|
11831
|
-
|
|
11832
|
-
|
|
11833
|
-
|
|
11834
|
-
|
|
11835
|
-
* calculate the centerY value, which is the vertical center of the view.
|
|
11836
|
-
* @returns a Matrix2D object.
|
|
11837
|
-
*/
|
|
11838
|
-
static view(width, height) {
|
|
11839
|
-
const scaleStep = 1;
|
|
11840
|
-
const centerX = width / 2;
|
|
11841
|
-
const centerY = height / 2;
|
|
11842
|
-
const flipX = Math.cos(Math.PI);
|
|
11843
|
-
return new _Matrix2D([
|
|
11844
|
-
[scaleStep, 0, centerX],
|
|
11845
|
-
[0, flipX * scaleStep, centerY],
|
|
11846
|
-
[0, 0, 1]
|
|
11847
|
-
]);
|
|
11739
|
+
_subtractFn(a, b) {
|
|
11740
|
+
return a - b;
|
|
11741
|
+
}
|
|
11742
|
+
_multiplyFn(a, b) {
|
|
11743
|
+
return a * b;
|
|
11848
11744
|
}
|
|
11849
11745
|
/**
|
|
11850
|
-
* The function
|
|
11851
|
-
* @param {number}
|
|
11852
|
-
*
|
|
11853
|
-
* @
|
|
11746
|
+
* The function checks if a given row and column index is valid within a specified range.
|
|
11747
|
+
* @param {number} row - The `row` parameter represents the row index of a two-dimensional array or
|
|
11748
|
+
* matrix. It is a number that indicates the specific row in the matrix.
|
|
11749
|
+
* @param {number} col - The "col" parameter represents the column index in a two-dimensional array
|
|
11750
|
+
* or grid. It is used to check if the given column index is valid within the bounds of the grid.
|
|
11751
|
+
* @returns A boolean value is being returned.
|
|
11854
11752
|
*/
|
|
11855
|
-
|
|
11856
|
-
return
|
|
11753
|
+
isValidIndex(row, col) {
|
|
11754
|
+
return row >= 0 && row < this.rows && col >= 0 && col < this.cols;
|
|
11857
11755
|
}
|
|
11858
11756
|
/**
|
|
11859
|
-
* The function
|
|
11860
|
-
* @param {number}
|
|
11861
|
-
* @
|
|
11757
|
+
* The function `_swapRows` swaps the positions of two rows in an array.
|
|
11758
|
+
* @param {number} row1 - The `row1` parameter is the index of the first row that you want to swap.
|
|
11759
|
+
* @param {number} row2 - The `row2` parameter is the index of the second row that you want to swap
|
|
11760
|
+
* with the first row.
|
|
11862
11761
|
*/
|
|
11863
|
-
|
|
11864
|
-
const
|
|
11865
|
-
|
|
11866
|
-
|
|
11867
|
-
[cos, -sin, 0],
|
|
11868
|
-
[sin, cos, 0],
|
|
11869
|
-
[0, 0, 1]
|
|
11870
|
-
]);
|
|
11762
|
+
_swapRows(row1, row2) {
|
|
11763
|
+
const temp = this.data[row1];
|
|
11764
|
+
this.data[row1] = this.data[row2];
|
|
11765
|
+
this.data[row2] = temp;
|
|
11871
11766
|
}
|
|
11872
11767
|
/**
|
|
11873
|
-
* The
|
|
11874
|
-
* @param {
|
|
11875
|
-
*
|
|
11876
|
-
* @
|
|
11768
|
+
* The function scales a specific row in a matrix by a given scalar value.
|
|
11769
|
+
* @param {number} row - The `row` parameter represents the index of the row in the matrix that you
|
|
11770
|
+
* want to scale. It is a number that indicates the position of the row within the matrix.
|
|
11771
|
+
* @param {number} scalar - The scalar parameter is a number that is used to multiply each element in
|
|
11772
|
+
* a specific row of a matrix.
|
|
11877
11773
|
*/
|
|
11878
|
-
|
|
11879
|
-
|
|
11880
|
-
|
|
11881
|
-
|
|
11882
|
-
|
|
11883
|
-
|
|
11774
|
+
_scaleRow(row, scalar) {
|
|
11775
|
+
for (let j = 0; j < this.cols; j++) {
|
|
11776
|
+
let multiplied = this.multiplyFn(this.data[row][j], scalar);
|
|
11777
|
+
if (multiplied === void 0)
|
|
11778
|
+
multiplied = 0;
|
|
11779
|
+
this.data[row][j] = multiplied;
|
|
11780
|
+
}
|
|
11884
11781
|
}
|
|
11885
11782
|
/**
|
|
11886
|
-
* The function
|
|
11887
|
-
*
|
|
11888
|
-
* @
|
|
11889
|
-
*
|
|
11783
|
+
* The function `_addScaledRow` multiplies a row in a matrix by a scalar value and adds it to another
|
|
11784
|
+
* row.
|
|
11785
|
+
* @param {number} targetRow - The targetRow parameter represents the index of the row in which the
|
|
11786
|
+
* scaled values will be added.
|
|
11787
|
+
* @param {number} sourceRow - The sourceRow parameter represents the index of the row from which the
|
|
11788
|
+
* values will be scaled and added to the targetRow.
|
|
11789
|
+
* @param {number} scalar - The scalar parameter is a number that is used to scale the values in the
|
|
11790
|
+
* source row before adding them to the target row.
|
|
11890
11791
|
*/
|
|
11891
|
-
|
|
11892
|
-
|
|
11792
|
+
_addScaledRow(targetRow, sourceRow, scalar) {
|
|
11793
|
+
for (let j = 0; j < this.cols; j++) {
|
|
11794
|
+
let multiplied = this.multiplyFn(this.data[sourceRow][j], scalar);
|
|
11795
|
+
if (multiplied === void 0)
|
|
11796
|
+
multiplied = 0;
|
|
11797
|
+
const scaledValue = multiplied;
|
|
11798
|
+
let added = this.addFn(this.data[targetRow][j], scaledValue);
|
|
11799
|
+
if (added === void 0)
|
|
11800
|
+
added = 0;
|
|
11801
|
+
this.data[targetRow][j] = added;
|
|
11802
|
+
}
|
|
11893
11803
|
}
|
|
11894
11804
|
};
|
|
11895
11805
|
|