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
package/dist/esm/index.mjs
CHANGED
|
@@ -158,6 +158,14 @@ var IterableEntryBase = class {
|
|
|
158
158
|
}
|
|
159
159
|
return accumulator;
|
|
160
160
|
}
|
|
161
|
+
/**
|
|
162
|
+
* Converts data structure to `[key, value]` pairs.
|
|
163
|
+
* @returns Array of entries.
|
|
164
|
+
* @remarks Time O(n), Space O(n)
|
|
165
|
+
*/
|
|
166
|
+
toArray() {
|
|
167
|
+
return [...this];
|
|
168
|
+
}
|
|
161
169
|
/**
|
|
162
170
|
* Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
|
|
163
171
|
* @returns Array of entries (default) or a string.
|
|
@@ -6828,8 +6836,6 @@ var Range = class {
|
|
|
6828
6836
|
this.high = high;
|
|
6829
6837
|
this.includeLow = includeLow;
|
|
6830
6838
|
this.includeHigh = includeHigh;
|
|
6831
|
-
if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
|
|
6832
|
-
if (low > high) throw new RangeError("low must be less than or equal to high");
|
|
6833
6839
|
}
|
|
6834
6840
|
static {
|
|
6835
6841
|
__name(this, "Range");
|
|
@@ -8975,77 +8981,63 @@ var BST = class extends BinaryTree {
|
|
|
8975
8981
|
else _iterate();
|
|
8976
8982
|
return inserted;
|
|
8977
8983
|
}
|
|
8978
|
-
|
|
8979
|
-
|
|
8980
|
-
|
|
8981
|
-
|
|
8982
|
-
|
|
8983
|
-
|
|
8984
|
-
|
|
8985
|
-
|
|
8986
|
-
|
|
8987
|
-
|
|
8988
|
-
|
|
8989
|
-
|
|
8990
|
-
|
|
8991
|
-
|
|
8992
|
-
|
|
8993
|
-
|
|
8994
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
8995
|
-
* Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
8996
|
-
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
8997
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
8998
|
-
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
8999
|
-
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
9000
|
-
*/
|
|
9001
|
-
upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
9002
|
-
return this._bound(keyNodeEntryOrPredicate, false, iterationType);
|
|
9003
|
-
}
|
|
9004
|
-
/**
|
|
9005
|
-
* Returns the first node with a key greater than or equal to the given key.
|
|
9006
|
-
* This is equivalent to Java TreeMap.ceilingEntry().
|
|
9007
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
9008
|
-
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
9009
|
-
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
9010
|
-
*
|
|
9011
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
9012
|
-
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
9013
|
-
* @returns The first node with key >= given key, or undefined if no such node exists.
|
|
9014
|
-
*/
|
|
9015
|
-
ceilingEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
9016
|
-
return this.lowerBound(keyNodeEntryOrPredicate, iterationType);
|
|
8984
|
+
ceiling(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
8985
|
+
let actualCallback = void 0;
|
|
8986
|
+
let actualIterationType = this.iterationType;
|
|
8987
|
+
if (typeof callback === "string") {
|
|
8988
|
+
actualIterationType = callback;
|
|
8989
|
+
} else if (callback) {
|
|
8990
|
+
actualCallback = callback;
|
|
8991
|
+
if (iterationType) {
|
|
8992
|
+
actualIterationType = iterationType;
|
|
8993
|
+
}
|
|
8994
|
+
}
|
|
8995
|
+
const node = this._bound(keyNodeEntryOrPredicate, true, actualIterationType);
|
|
8996
|
+
if (!actualCallback) {
|
|
8997
|
+
return node?.key;
|
|
8998
|
+
}
|
|
8999
|
+
return node ? actualCallback(node) : void 0;
|
|
9017
9000
|
}
|
|
9018
|
-
|
|
9019
|
-
|
|
9020
|
-
|
|
9021
|
-
|
|
9022
|
-
|
|
9023
|
-
|
|
9024
|
-
|
|
9025
|
-
|
|
9026
|
-
|
|
9027
|
-
|
|
9028
|
-
|
|
9029
|
-
|
|
9030
|
-
|
|
9001
|
+
higher(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
9002
|
+
let actualCallback = void 0;
|
|
9003
|
+
let actualIterationType = this.iterationType;
|
|
9004
|
+
if (typeof callback === "string") {
|
|
9005
|
+
actualIterationType = callback;
|
|
9006
|
+
} else if (callback) {
|
|
9007
|
+
actualCallback = callback;
|
|
9008
|
+
if (iterationType) {
|
|
9009
|
+
actualIterationType = iterationType;
|
|
9010
|
+
}
|
|
9011
|
+
}
|
|
9012
|
+
const node = this._bound(keyNodeEntryOrPredicate, false, actualIterationType);
|
|
9013
|
+
if (!actualCallback) {
|
|
9014
|
+
return node?.key;
|
|
9015
|
+
}
|
|
9016
|
+
return node ? actualCallback(node) : void 0;
|
|
9031
9017
|
}
|
|
9032
|
-
|
|
9033
|
-
* Returns the first node with a key less than or equal to the given key.
|
|
9034
|
-
* This is equivalent to Java TreeMap.floorEntry().
|
|
9035
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
9036
|
-
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
9037
|
-
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
9038
|
-
*
|
|
9039
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
9040
|
-
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
9041
|
-
* @returns The first node with key <= given key, or undefined if no such node exists.
|
|
9042
|
-
*/
|
|
9043
|
-
floorEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
9018
|
+
floor(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
9044
9019
|
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
9020
|
+
if (typeof callback === "string" || !callback) {
|
|
9021
|
+
return void 0;
|
|
9022
|
+
}
|
|
9045
9023
|
return void 0;
|
|
9046
9024
|
}
|
|
9025
|
+
let actualCallback = void 0;
|
|
9026
|
+
let actualIterationType = this.iterationType;
|
|
9027
|
+
if (typeof callback === "string") {
|
|
9028
|
+
actualIterationType = callback;
|
|
9029
|
+
} else if (callback) {
|
|
9030
|
+
actualCallback = callback;
|
|
9031
|
+
if (iterationType) {
|
|
9032
|
+
actualIterationType = iterationType;
|
|
9033
|
+
}
|
|
9034
|
+
}
|
|
9047
9035
|
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
9048
|
-
|
|
9036
|
+
const node = this._floorByPredicate(keyNodeEntryOrPredicate, actualIterationType);
|
|
9037
|
+
if (!actualCallback) {
|
|
9038
|
+
return node?.key;
|
|
9039
|
+
}
|
|
9040
|
+
return node ? actualCallback(node) : void 0;
|
|
9049
9041
|
}
|
|
9050
9042
|
let targetKey;
|
|
9051
9043
|
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
@@ -9053,6 +9045,9 @@ var BST = class extends BinaryTree {
|
|
|
9053
9045
|
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
9054
9046
|
const key = keyNodeEntryOrPredicate[0];
|
|
9055
9047
|
if (key === null || key === void 0) {
|
|
9048
|
+
if (typeof callback === "string" || !callback) {
|
|
9049
|
+
return void 0;
|
|
9050
|
+
}
|
|
9056
9051
|
return void 0;
|
|
9057
9052
|
}
|
|
9058
9053
|
targetKey = key;
|
|
@@ -9060,27 +9055,40 @@ var BST = class extends BinaryTree {
|
|
|
9060
9055
|
targetKey = keyNodeEntryOrPredicate;
|
|
9061
9056
|
}
|
|
9062
9057
|
if (targetKey !== void 0) {
|
|
9063
|
-
|
|
9058
|
+
const node = this._floorByKey(targetKey, actualIterationType);
|
|
9059
|
+
if (!actualCallback) {
|
|
9060
|
+
return node?.key;
|
|
9061
|
+
}
|
|
9062
|
+
return node ? actualCallback(node) : void 0;
|
|
9063
|
+
}
|
|
9064
|
+
if (typeof callback === "string" || !callback) {
|
|
9065
|
+
return void 0;
|
|
9064
9066
|
}
|
|
9065
9067
|
return void 0;
|
|
9066
9068
|
}
|
|
9067
|
-
|
|
9068
|
-
* Returns the first node with a key strictly less than the given key.
|
|
9069
|
-
* This is equivalent to Java TreeMap.lowerEntry().
|
|
9070
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
9071
|
-
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
9072
|
-
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
9073
|
-
*
|
|
9074
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
9075
|
-
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
9076
|
-
* @returns The first node with key < given key, or undefined if no such node exists.
|
|
9077
|
-
*/
|
|
9078
|
-
lowerEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
9069
|
+
lower(keyNodeEntryOrPredicate, callback, iterationType) {
|
|
9079
9070
|
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
9071
|
+
if (typeof callback === "string" || !callback) {
|
|
9072
|
+
return void 0;
|
|
9073
|
+
}
|
|
9080
9074
|
return void 0;
|
|
9081
9075
|
}
|
|
9076
|
+
let actualCallback = void 0;
|
|
9077
|
+
let actualIterationType = this.iterationType;
|
|
9078
|
+
if (typeof callback === "string") {
|
|
9079
|
+
actualIterationType = callback;
|
|
9080
|
+
} else if (callback) {
|
|
9081
|
+
actualCallback = callback;
|
|
9082
|
+
if (iterationType) {
|
|
9083
|
+
actualIterationType = iterationType;
|
|
9084
|
+
}
|
|
9085
|
+
}
|
|
9082
9086
|
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
9083
|
-
|
|
9087
|
+
const node = this._lowerByPredicate(keyNodeEntryOrPredicate, actualIterationType);
|
|
9088
|
+
if (!actualCallback) {
|
|
9089
|
+
return node?.key;
|
|
9090
|
+
}
|
|
9091
|
+
return node ? actualCallback(node) : void 0;
|
|
9084
9092
|
}
|
|
9085
9093
|
let targetKey;
|
|
9086
9094
|
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
@@ -9088,6 +9096,9 @@ var BST = class extends BinaryTree {
|
|
|
9088
9096
|
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
9089
9097
|
const key = keyNodeEntryOrPredicate[0];
|
|
9090
9098
|
if (key === null || key === void 0) {
|
|
9099
|
+
if (typeof callback === "string" || !callback) {
|
|
9100
|
+
return void 0;
|
|
9101
|
+
}
|
|
9091
9102
|
return void 0;
|
|
9092
9103
|
}
|
|
9093
9104
|
targetKey = key;
|
|
@@ -9095,7 +9106,14 @@ var BST = class extends BinaryTree {
|
|
|
9095
9106
|
targetKey = keyNodeEntryOrPredicate;
|
|
9096
9107
|
}
|
|
9097
9108
|
if (targetKey !== void 0) {
|
|
9098
|
-
|
|
9109
|
+
const node = this._lowerByKey(targetKey, actualIterationType);
|
|
9110
|
+
if (!actualCallback) {
|
|
9111
|
+
return node?.key;
|
|
9112
|
+
}
|
|
9113
|
+
return node ? actualCallback(node) : void 0;
|
|
9114
|
+
}
|
|
9115
|
+
if (typeof callback === "string" || !callback) {
|
|
9116
|
+
return void 0;
|
|
9099
9117
|
}
|
|
9100
9118
|
return void 0;
|
|
9101
9119
|
}
|
|
@@ -9284,7 +9302,6 @@ var BST = class extends BinaryTree {
|
|
|
9284
9302
|
*/
|
|
9285
9303
|
_createDefaultComparator() {
|
|
9286
9304
|
return (a, b) => {
|
|
9287
|
-
debugger;
|
|
9288
9305
|
if (isComparable(a) && isComparable(b)) {
|
|
9289
9306
|
if (a > b) return 1;
|
|
9290
9307
|
if (a < b) return -1;
|