data-structure-typed 2.2.5 → 2.2.7
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/CONTRIBUTING.md +47 -1
- package/README.md +6 -6
- package/dist/cjs/index.cjs +100 -83
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +100 -83
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +100 -83
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +100 -83
- 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 -83
- 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.
|
|
@@ -6814,8 +6822,6 @@ var _Range = class _Range {
|
|
|
6814
6822
|
this.high = high;
|
|
6815
6823
|
this.includeLow = includeLow;
|
|
6816
6824
|
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
6825
|
}
|
|
6820
6826
|
// Determine whether a key is within the range
|
|
6821
6827
|
isInRange(key, comparator) {
|
|
@@ -8959,77 +8965,63 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8959
8965
|
else _iterate();
|
|
8960
8966
|
return inserted;
|
|
8961
8967
|
}
|
|
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);
|
|
8968
|
+
ceiling(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
8969
|
+
let actualCallback = void 0;
|
|
8970
|
+
let actualIterationType = this.iterationType;
|
|
8971
|
+
if (typeof callback === "string") {
|
|
8972
|
+
actualIterationType = callback;
|
|
8973
|
+
} else if (callback) {
|
|
8974
|
+
actualCallback = callback;
|
|
8975
|
+
if (iterationType) {
|
|
8976
|
+
actualIterationType = iterationType;
|
|
8977
|
+
}
|
|
8978
|
+
}
|
|
8979
|
+
const node = this._bound(keyNodeEntryOrPredicate, true, actualIterationType);
|
|
8980
|
+
if (!actualCallback) {
|
|
8981
|
+
return node == null ? void 0 : node.key;
|
|
8982
|
+
}
|
|
8983
|
+
return node ? actualCallback(node) : void 0;
|
|
9001
8984
|
}
|
|
9002
|
-
|
|
9003
|
-
|
|
9004
|
-
|
|
9005
|
-
|
|
9006
|
-
|
|
9007
|
-
|
|
9008
|
-
|
|
9009
|
-
|
|
9010
|
-
|
|
9011
|
-
|
|
9012
|
-
|
|
9013
|
-
|
|
9014
|
-
|
|
8985
|
+
higher(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
8986
|
+
let actualCallback = void 0;
|
|
8987
|
+
let actualIterationType = this.iterationType;
|
|
8988
|
+
if (typeof callback === "string") {
|
|
8989
|
+
actualIterationType = callback;
|
|
8990
|
+
} else if (callback) {
|
|
8991
|
+
actualCallback = callback;
|
|
8992
|
+
if (iterationType) {
|
|
8993
|
+
actualIterationType = iterationType;
|
|
8994
|
+
}
|
|
8995
|
+
}
|
|
8996
|
+
const node = this._bound(keyNodeEntryOrPredicate, false, actualIterationType);
|
|
8997
|
+
if (!actualCallback) {
|
|
8998
|
+
return node == null ? void 0 : node.key;
|
|
8999
|
+
}
|
|
9000
|
+
return node ? actualCallback(node) : void 0;
|
|
9015
9001
|
}
|
|
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) {
|
|
9002
|
+
floor(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
9028
9003
|
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
9004
|
+
if (typeof callback === "string" || !callback) {
|
|
9005
|
+
return void 0;
|
|
9006
|
+
}
|
|
9029
9007
|
return void 0;
|
|
9030
9008
|
}
|
|
9009
|
+
let actualCallback = void 0;
|
|
9010
|
+
let actualIterationType = this.iterationType;
|
|
9011
|
+
if (typeof callback === "string") {
|
|
9012
|
+
actualIterationType = callback;
|
|
9013
|
+
} else if (callback) {
|
|
9014
|
+
actualCallback = callback;
|
|
9015
|
+
if (iterationType) {
|
|
9016
|
+
actualIterationType = iterationType;
|
|
9017
|
+
}
|
|
9018
|
+
}
|
|
9031
9019
|
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
9032
|
-
|
|
9020
|
+
const node = this._floorByPredicate(keyNodeEntryOrPredicate, actualIterationType);
|
|
9021
|
+
if (!actualCallback) {
|
|
9022
|
+
return node == null ? void 0 : node.key;
|
|
9023
|
+
}
|
|
9024
|
+
return node ? actualCallback(node) : void 0;
|
|
9033
9025
|
}
|
|
9034
9026
|
let targetKey;
|
|
9035
9027
|
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
@@ -9037,6 +9029,9 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9037
9029
|
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
9038
9030
|
const key = keyNodeEntryOrPredicate[0];
|
|
9039
9031
|
if (key === null || key === void 0) {
|
|
9032
|
+
if (typeof callback === "string" || !callback) {
|
|
9033
|
+
return void 0;
|
|
9034
|
+
}
|
|
9040
9035
|
return void 0;
|
|
9041
9036
|
}
|
|
9042
9037
|
targetKey = key;
|
|
@@ -9044,27 +9039,40 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9044
9039
|
targetKey = keyNodeEntryOrPredicate;
|
|
9045
9040
|
}
|
|
9046
9041
|
if (targetKey !== void 0) {
|
|
9047
|
-
|
|
9042
|
+
const node = this._floorByKey(targetKey, actualIterationType);
|
|
9043
|
+
if (!actualCallback) {
|
|
9044
|
+
return node == null ? void 0 : node.key;
|
|
9045
|
+
}
|
|
9046
|
+
return node ? actualCallback(node) : void 0;
|
|
9047
|
+
}
|
|
9048
|
+
if (typeof callback === "string" || !callback) {
|
|
9049
|
+
return void 0;
|
|
9048
9050
|
}
|
|
9049
9051
|
return void 0;
|
|
9050
9052
|
}
|
|
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) {
|
|
9053
|
+
lower(keyNodeEntryOrPredicate, callback, iterationType) {
|
|
9063
9054
|
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
9055
|
+
if (typeof callback === "string" || !callback) {
|
|
9056
|
+
return void 0;
|
|
9057
|
+
}
|
|
9064
9058
|
return void 0;
|
|
9065
9059
|
}
|
|
9060
|
+
let actualCallback = void 0;
|
|
9061
|
+
let actualIterationType = this.iterationType;
|
|
9062
|
+
if (typeof callback === "string") {
|
|
9063
|
+
actualIterationType = callback;
|
|
9064
|
+
} else if (callback) {
|
|
9065
|
+
actualCallback = callback;
|
|
9066
|
+
if (iterationType) {
|
|
9067
|
+
actualIterationType = iterationType;
|
|
9068
|
+
}
|
|
9069
|
+
}
|
|
9066
9070
|
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
9067
|
-
|
|
9071
|
+
const node = this._lowerByPredicate(keyNodeEntryOrPredicate, actualIterationType);
|
|
9072
|
+
if (!actualCallback) {
|
|
9073
|
+
return node == null ? void 0 : node.key;
|
|
9074
|
+
}
|
|
9075
|
+
return node ? actualCallback(node) : void 0;
|
|
9068
9076
|
}
|
|
9069
9077
|
let targetKey;
|
|
9070
9078
|
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
@@ -9072,6 +9080,9 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9072
9080
|
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
9073
9081
|
const key = keyNodeEntryOrPredicate[0];
|
|
9074
9082
|
if (key === null || key === void 0) {
|
|
9083
|
+
if (typeof callback === "string" || !callback) {
|
|
9084
|
+
return void 0;
|
|
9085
|
+
}
|
|
9075
9086
|
return void 0;
|
|
9076
9087
|
}
|
|
9077
9088
|
targetKey = key;
|
|
@@ -9079,7 +9090,14 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9079
9090
|
targetKey = keyNodeEntryOrPredicate;
|
|
9080
9091
|
}
|
|
9081
9092
|
if (targetKey !== void 0) {
|
|
9082
|
-
|
|
9093
|
+
const node = this._lowerByKey(targetKey, actualIterationType);
|
|
9094
|
+
if (!actualCallback) {
|
|
9095
|
+
return node == null ? void 0 : node.key;
|
|
9096
|
+
}
|
|
9097
|
+
return node ? actualCallback(node) : void 0;
|
|
9098
|
+
}
|
|
9099
|
+
if (typeof callback === "string" || !callback) {
|
|
9100
|
+
return void 0;
|
|
9083
9101
|
}
|
|
9084
9102
|
return void 0;
|
|
9085
9103
|
}
|
|
@@ -9268,7 +9286,6 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9268
9286
|
*/
|
|
9269
9287
|
_createDefaultComparator() {
|
|
9270
9288
|
return (a, b) => {
|
|
9271
|
-
debugger;
|
|
9272
9289
|
if (isComparable(a) && isComparable(b)) {
|
|
9273
9290
|
if (a > b) return 1;
|
|
9274
9291
|
if (a < b) return -1;
|