data-structure-typed 2.2.5 → 2.2.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/dist/cjs/index.cjs +100 -86
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +100 -86
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +100 -86
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +100 -86
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +14 -57
- package/dist/types/data-structures/binary-tree/bst.d.ts +46 -126
- package/dist/umd/data-structure-typed.js +100 -86
- package/dist/umd/data-structure-typed.js.map +1 -1
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +5 -3
- package/src/common/index.ts +2 -4
- package/src/data-structures/base/iterable-entry-base.ts +9 -0
- package/src/data-structures/binary-tree/binary-tree.ts +67 -0
- package/src/data-structures/binary-tree/bst.ts +295 -89
- package/test/unit/data-structures/binary-tree/bst.test.ts +1138 -525
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +12 -10
- package/tsup.config.js +6 -0
- package/tsup.leetcode.config.js +3 -0
- package/tsup.node.config.js +12 -0
|
@@ -159,6 +159,14 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
159
159
|
}
|
|
160
160
|
return accumulator;
|
|
161
161
|
}
|
|
162
|
+
/**
|
|
163
|
+
* Converts data structure to `[key, value]` pairs.
|
|
164
|
+
* @returns Array of entries.
|
|
165
|
+
* @remarks Time O(n), Space O(n)
|
|
166
|
+
*/
|
|
167
|
+
toArray() {
|
|
168
|
+
return [...this];
|
|
169
|
+
}
|
|
162
170
|
/**
|
|
163
171
|
* Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
|
|
164
172
|
* @returns Array of entries (default) or a string.
|
|
@@ -172,7 +180,6 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
172
180
|
* @remarks Time O(n), Space O(n)
|
|
173
181
|
*/
|
|
174
182
|
print() {
|
|
175
|
-
console.log(this.toVisual());
|
|
176
183
|
}
|
|
177
184
|
};
|
|
178
185
|
__name(_IterableEntryBase, "IterableEntryBase");
|
|
@@ -394,7 +401,6 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
394
401
|
* Time O(n) due to materialization, Space O(n) for the intermediate representation.
|
|
395
402
|
*/
|
|
396
403
|
print() {
|
|
397
|
-
console.log(this.toVisual());
|
|
398
404
|
}
|
|
399
405
|
};
|
|
400
406
|
__name(_IterableElementBase, "IterableElementBase");
|
|
@@ -6814,8 +6820,6 @@ var _Range = class _Range {
|
|
|
6814
6820
|
this.high = high;
|
|
6815
6821
|
this.includeLow = includeLow;
|
|
6816
6822
|
this.includeHigh = includeHigh;
|
|
6817
|
-
if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
|
|
6818
|
-
if (low > high) throw new RangeError("low must be less than or equal to high");
|
|
6819
6823
|
}
|
|
6820
6824
|
// Determine whether a key is within the range
|
|
6821
6825
|
isInRange(key, comparator) {
|
|
@@ -8063,7 +8067,6 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
8063
8067
|
* @param [startNode=this._root] - The node to start printing from.
|
|
8064
8068
|
*/
|
|
8065
8069
|
print(options, startNode = this._root) {
|
|
8066
|
-
console.log(this.toVisual(startNode, options));
|
|
8067
8070
|
}
|
|
8068
8071
|
/**
|
|
8069
8072
|
* (Protected) Core DFS implementation.
|
|
@@ -8959,77 +8962,63 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8959
8962
|
else _iterate();
|
|
8960
8963
|
return inserted;
|
|
8961
8964
|
}
|
|
8962
|
-
|
|
8963
|
-
|
|
8964
|
-
|
|
8965
|
-
|
|
8966
|
-
|
|
8967
|
-
|
|
8968
|
-
|
|
8969
|
-
|
|
8970
|
-
|
|
8971
|
-
|
|
8972
|
-
|
|
8973
|
-
|
|
8974
|
-
|
|
8975
|
-
|
|
8976
|
-
|
|
8977
|
-
|
|
8978
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
8979
|
-
* Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
8980
|
-
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
8981
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
8982
|
-
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
8983
|
-
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
8984
|
-
*/
|
|
8985
|
-
upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
8986
|
-
return this._bound(keyNodeEntryOrPredicate, false, iterationType);
|
|
8987
|
-
}
|
|
8988
|
-
/**
|
|
8989
|
-
* Returns the first node with a key greater than or equal to the given key.
|
|
8990
|
-
* This is equivalent to Java TreeMap.ceilingEntry().
|
|
8991
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
8992
|
-
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
8993
|
-
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
8994
|
-
*
|
|
8995
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
8996
|
-
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
8997
|
-
* @returns The first node with key >= given key, or undefined if no such node exists.
|
|
8998
|
-
*/
|
|
8999
|
-
ceilingEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
9000
|
-
return this.lowerBound(keyNodeEntryOrPredicate, iterationType);
|
|
8965
|
+
ceiling(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
8966
|
+
let actualCallback = void 0;
|
|
8967
|
+
let actualIterationType = this.iterationType;
|
|
8968
|
+
if (typeof callback === "string") {
|
|
8969
|
+
actualIterationType = callback;
|
|
8970
|
+
} else if (callback) {
|
|
8971
|
+
actualCallback = callback;
|
|
8972
|
+
if (iterationType) {
|
|
8973
|
+
actualIterationType = iterationType;
|
|
8974
|
+
}
|
|
8975
|
+
}
|
|
8976
|
+
const node = this._bound(keyNodeEntryOrPredicate, true, actualIterationType);
|
|
8977
|
+
if (!actualCallback) {
|
|
8978
|
+
return node == null ? void 0 : node.key;
|
|
8979
|
+
}
|
|
8980
|
+
return node ? actualCallback(node) : void 0;
|
|
9001
8981
|
}
|
|
9002
|
-
|
|
9003
|
-
|
|
9004
|
-
|
|
9005
|
-
|
|
9006
|
-
|
|
9007
|
-
|
|
9008
|
-
|
|
9009
|
-
|
|
9010
|
-
|
|
9011
|
-
|
|
9012
|
-
|
|
9013
|
-
|
|
9014
|
-
|
|
8982
|
+
higher(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
8983
|
+
let actualCallback = void 0;
|
|
8984
|
+
let actualIterationType = this.iterationType;
|
|
8985
|
+
if (typeof callback === "string") {
|
|
8986
|
+
actualIterationType = callback;
|
|
8987
|
+
} else if (callback) {
|
|
8988
|
+
actualCallback = callback;
|
|
8989
|
+
if (iterationType) {
|
|
8990
|
+
actualIterationType = iterationType;
|
|
8991
|
+
}
|
|
8992
|
+
}
|
|
8993
|
+
const node = this._bound(keyNodeEntryOrPredicate, false, actualIterationType);
|
|
8994
|
+
if (!actualCallback) {
|
|
8995
|
+
return node == null ? void 0 : node.key;
|
|
8996
|
+
}
|
|
8997
|
+
return node ? actualCallback(node) : void 0;
|
|
9015
8998
|
}
|
|
9016
|
-
|
|
9017
|
-
* Returns the first node with a key less than or equal to the given key.
|
|
9018
|
-
* This is equivalent to Java TreeMap.floorEntry().
|
|
9019
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
9020
|
-
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
9021
|
-
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
9022
|
-
*
|
|
9023
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
9024
|
-
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
9025
|
-
* @returns The first node with key <= given key, or undefined if no such node exists.
|
|
9026
|
-
*/
|
|
9027
|
-
floorEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
8999
|
+
floor(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
9028
9000
|
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
9001
|
+
if (typeof callback === "string" || !callback) {
|
|
9002
|
+
return void 0;
|
|
9003
|
+
}
|
|
9029
9004
|
return void 0;
|
|
9030
9005
|
}
|
|
9006
|
+
let actualCallback = void 0;
|
|
9007
|
+
let actualIterationType = this.iterationType;
|
|
9008
|
+
if (typeof callback === "string") {
|
|
9009
|
+
actualIterationType = callback;
|
|
9010
|
+
} else if (callback) {
|
|
9011
|
+
actualCallback = callback;
|
|
9012
|
+
if (iterationType) {
|
|
9013
|
+
actualIterationType = iterationType;
|
|
9014
|
+
}
|
|
9015
|
+
}
|
|
9031
9016
|
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
9032
|
-
|
|
9017
|
+
const node = this._floorByPredicate(keyNodeEntryOrPredicate, actualIterationType);
|
|
9018
|
+
if (!actualCallback) {
|
|
9019
|
+
return node == null ? void 0 : node.key;
|
|
9020
|
+
}
|
|
9021
|
+
return node ? actualCallback(node) : void 0;
|
|
9033
9022
|
}
|
|
9034
9023
|
let targetKey;
|
|
9035
9024
|
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
@@ -9037,6 +9026,9 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9037
9026
|
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
9038
9027
|
const key = keyNodeEntryOrPredicate[0];
|
|
9039
9028
|
if (key === null || key === void 0) {
|
|
9029
|
+
if (typeof callback === "string" || !callback) {
|
|
9030
|
+
return void 0;
|
|
9031
|
+
}
|
|
9040
9032
|
return void 0;
|
|
9041
9033
|
}
|
|
9042
9034
|
targetKey = key;
|
|
@@ -9044,27 +9036,40 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9044
9036
|
targetKey = keyNodeEntryOrPredicate;
|
|
9045
9037
|
}
|
|
9046
9038
|
if (targetKey !== void 0) {
|
|
9047
|
-
|
|
9039
|
+
const node = this._floorByKey(targetKey, actualIterationType);
|
|
9040
|
+
if (!actualCallback) {
|
|
9041
|
+
return node == null ? void 0 : node.key;
|
|
9042
|
+
}
|
|
9043
|
+
return node ? actualCallback(node) : void 0;
|
|
9044
|
+
}
|
|
9045
|
+
if (typeof callback === "string" || !callback) {
|
|
9046
|
+
return void 0;
|
|
9048
9047
|
}
|
|
9049
9048
|
return void 0;
|
|
9050
9049
|
}
|
|
9051
|
-
|
|
9052
|
-
* Returns the first node with a key strictly less than the given key.
|
|
9053
|
-
* This is equivalent to Java TreeMap.lowerEntry().
|
|
9054
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
9055
|
-
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
9056
|
-
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
9057
|
-
*
|
|
9058
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
9059
|
-
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
9060
|
-
* @returns The first node with key < given key, or undefined if no such node exists.
|
|
9061
|
-
*/
|
|
9062
|
-
lowerEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
9050
|
+
lower(keyNodeEntryOrPredicate, callback, iterationType) {
|
|
9063
9051
|
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
9052
|
+
if (typeof callback === "string" || !callback) {
|
|
9053
|
+
return void 0;
|
|
9054
|
+
}
|
|
9064
9055
|
return void 0;
|
|
9065
9056
|
}
|
|
9057
|
+
let actualCallback = void 0;
|
|
9058
|
+
let actualIterationType = this.iterationType;
|
|
9059
|
+
if (typeof callback === "string") {
|
|
9060
|
+
actualIterationType = callback;
|
|
9061
|
+
} else if (callback) {
|
|
9062
|
+
actualCallback = callback;
|
|
9063
|
+
if (iterationType) {
|
|
9064
|
+
actualIterationType = iterationType;
|
|
9065
|
+
}
|
|
9066
|
+
}
|
|
9066
9067
|
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
9067
|
-
|
|
9068
|
+
const node = this._lowerByPredicate(keyNodeEntryOrPredicate, actualIterationType);
|
|
9069
|
+
if (!actualCallback) {
|
|
9070
|
+
return node == null ? void 0 : node.key;
|
|
9071
|
+
}
|
|
9072
|
+
return node ? actualCallback(node) : void 0;
|
|
9068
9073
|
}
|
|
9069
9074
|
let targetKey;
|
|
9070
9075
|
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
@@ -9072,6 +9077,9 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9072
9077
|
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
9073
9078
|
const key = keyNodeEntryOrPredicate[0];
|
|
9074
9079
|
if (key === null || key === void 0) {
|
|
9080
|
+
if (typeof callback === "string" || !callback) {
|
|
9081
|
+
return void 0;
|
|
9082
|
+
}
|
|
9075
9083
|
return void 0;
|
|
9076
9084
|
}
|
|
9077
9085
|
targetKey = key;
|
|
@@ -9079,7 +9087,14 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9079
9087
|
targetKey = keyNodeEntryOrPredicate;
|
|
9080
9088
|
}
|
|
9081
9089
|
if (targetKey !== void 0) {
|
|
9082
|
-
|
|
9090
|
+
const node = this._lowerByKey(targetKey, actualIterationType);
|
|
9091
|
+
if (!actualCallback) {
|
|
9092
|
+
return node == null ? void 0 : node.key;
|
|
9093
|
+
}
|
|
9094
|
+
return node ? actualCallback(node) : void 0;
|
|
9095
|
+
}
|
|
9096
|
+
if (typeof callback === "string" || !callback) {
|
|
9097
|
+
return void 0;
|
|
9083
9098
|
}
|
|
9084
9099
|
return void 0;
|
|
9085
9100
|
}
|
|
@@ -9268,7 +9283,6 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9268
9283
|
*/
|
|
9269
9284
|
_createDefaultComparator() {
|
|
9270
9285
|
return (a, b) => {
|
|
9271
|
-
debugger;
|
|
9272
9286
|
if (isComparable(a) && isComparable(b)) {
|
|
9273
9287
|
if (a > b) return 1;
|
|
9274
9288
|
if (a < b) return -1;
|