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
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.
|
|
@@ -171,7 +179,6 @@ var IterableEntryBase = class {
|
|
|
171
179
|
* @remarks Time O(n), Space O(n)
|
|
172
180
|
*/
|
|
173
181
|
print() {
|
|
174
|
-
console.log(this.toVisual());
|
|
175
182
|
}
|
|
176
183
|
};
|
|
177
184
|
|
|
@@ -394,7 +401,6 @@ var IterableElementBase = class {
|
|
|
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
|
|
|
@@ -6828,8 +6834,6 @@ var Range = class {
|
|
|
6828
6834
|
this.high = high;
|
|
6829
6835
|
this.includeLow = includeLow;
|
|
6830
6836
|
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
6837
|
}
|
|
6834
6838
|
static {
|
|
6835
6839
|
__name(this, "Range");
|
|
@@ -8073,7 +8077,6 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
8073
8077
|
* @param [startNode=this._root] - The node to start printing from.
|
|
8074
8078
|
*/
|
|
8075
8079
|
print(options, startNode = this._root) {
|
|
8076
|
-
console.log(this.toVisual(startNode, options));
|
|
8077
8080
|
}
|
|
8078
8081
|
/**
|
|
8079
8082
|
* (Protected) Core DFS implementation.
|
|
@@ -8975,77 +8978,63 @@ var BST = class extends BinaryTree {
|
|
|
8975
8978
|
else _iterate();
|
|
8976
8979
|
return inserted;
|
|
8977
8980
|
}
|
|
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);
|
|
8981
|
+
ceiling(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
8982
|
+
let actualCallback = void 0;
|
|
8983
|
+
let actualIterationType = this.iterationType;
|
|
8984
|
+
if (typeof callback === "string") {
|
|
8985
|
+
actualIterationType = callback;
|
|
8986
|
+
} else if (callback) {
|
|
8987
|
+
actualCallback = callback;
|
|
8988
|
+
if (iterationType) {
|
|
8989
|
+
actualIterationType = iterationType;
|
|
8990
|
+
}
|
|
8991
|
+
}
|
|
8992
|
+
const node = this._bound(keyNodeEntryOrPredicate, true, actualIterationType);
|
|
8993
|
+
if (!actualCallback) {
|
|
8994
|
+
return node?.key;
|
|
8995
|
+
}
|
|
8996
|
+
return node ? actualCallback(node) : void 0;
|
|
9017
8997
|
}
|
|
9018
|
-
|
|
9019
|
-
|
|
9020
|
-
|
|
9021
|
-
|
|
9022
|
-
|
|
9023
|
-
|
|
9024
|
-
|
|
9025
|
-
|
|
9026
|
-
|
|
9027
|
-
|
|
9028
|
-
|
|
9029
|
-
|
|
9030
|
-
|
|
8998
|
+
higher(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
8999
|
+
let actualCallback = void 0;
|
|
9000
|
+
let actualIterationType = this.iterationType;
|
|
9001
|
+
if (typeof callback === "string") {
|
|
9002
|
+
actualIterationType = callback;
|
|
9003
|
+
} else if (callback) {
|
|
9004
|
+
actualCallback = callback;
|
|
9005
|
+
if (iterationType) {
|
|
9006
|
+
actualIterationType = iterationType;
|
|
9007
|
+
}
|
|
9008
|
+
}
|
|
9009
|
+
const node = this._bound(keyNodeEntryOrPredicate, false, actualIterationType);
|
|
9010
|
+
if (!actualCallback) {
|
|
9011
|
+
return node?.key;
|
|
9012
|
+
}
|
|
9013
|
+
return node ? actualCallback(node) : void 0;
|
|
9031
9014
|
}
|
|
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) {
|
|
9015
|
+
floor(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
9044
9016
|
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
9017
|
+
if (typeof callback === "string" || !callback) {
|
|
9018
|
+
return void 0;
|
|
9019
|
+
}
|
|
9045
9020
|
return void 0;
|
|
9046
9021
|
}
|
|
9022
|
+
let actualCallback = void 0;
|
|
9023
|
+
let actualIterationType = this.iterationType;
|
|
9024
|
+
if (typeof callback === "string") {
|
|
9025
|
+
actualIterationType = callback;
|
|
9026
|
+
} else if (callback) {
|
|
9027
|
+
actualCallback = callback;
|
|
9028
|
+
if (iterationType) {
|
|
9029
|
+
actualIterationType = iterationType;
|
|
9030
|
+
}
|
|
9031
|
+
}
|
|
9047
9032
|
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
9048
|
-
|
|
9033
|
+
const node = this._floorByPredicate(keyNodeEntryOrPredicate, actualIterationType);
|
|
9034
|
+
if (!actualCallback) {
|
|
9035
|
+
return node?.key;
|
|
9036
|
+
}
|
|
9037
|
+
return node ? actualCallback(node) : void 0;
|
|
9049
9038
|
}
|
|
9050
9039
|
let targetKey;
|
|
9051
9040
|
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
@@ -9053,6 +9042,9 @@ var BST = class extends BinaryTree {
|
|
|
9053
9042
|
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
9054
9043
|
const key = keyNodeEntryOrPredicate[0];
|
|
9055
9044
|
if (key === null || key === void 0) {
|
|
9045
|
+
if (typeof callback === "string" || !callback) {
|
|
9046
|
+
return void 0;
|
|
9047
|
+
}
|
|
9056
9048
|
return void 0;
|
|
9057
9049
|
}
|
|
9058
9050
|
targetKey = key;
|
|
@@ -9060,27 +9052,40 @@ var BST = class extends BinaryTree {
|
|
|
9060
9052
|
targetKey = keyNodeEntryOrPredicate;
|
|
9061
9053
|
}
|
|
9062
9054
|
if (targetKey !== void 0) {
|
|
9063
|
-
|
|
9055
|
+
const node = this._floorByKey(targetKey, actualIterationType);
|
|
9056
|
+
if (!actualCallback) {
|
|
9057
|
+
return node?.key;
|
|
9058
|
+
}
|
|
9059
|
+
return node ? actualCallback(node) : void 0;
|
|
9060
|
+
}
|
|
9061
|
+
if (typeof callback === "string" || !callback) {
|
|
9062
|
+
return void 0;
|
|
9064
9063
|
}
|
|
9065
9064
|
return void 0;
|
|
9066
9065
|
}
|
|
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) {
|
|
9066
|
+
lower(keyNodeEntryOrPredicate, callback, iterationType) {
|
|
9079
9067
|
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
9068
|
+
if (typeof callback === "string" || !callback) {
|
|
9069
|
+
return void 0;
|
|
9070
|
+
}
|
|
9080
9071
|
return void 0;
|
|
9081
9072
|
}
|
|
9073
|
+
let actualCallback = void 0;
|
|
9074
|
+
let actualIterationType = this.iterationType;
|
|
9075
|
+
if (typeof callback === "string") {
|
|
9076
|
+
actualIterationType = callback;
|
|
9077
|
+
} else if (callback) {
|
|
9078
|
+
actualCallback = callback;
|
|
9079
|
+
if (iterationType) {
|
|
9080
|
+
actualIterationType = iterationType;
|
|
9081
|
+
}
|
|
9082
|
+
}
|
|
9082
9083
|
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
9083
|
-
|
|
9084
|
+
const node = this._lowerByPredicate(keyNodeEntryOrPredicate, actualIterationType);
|
|
9085
|
+
if (!actualCallback) {
|
|
9086
|
+
return node?.key;
|
|
9087
|
+
}
|
|
9088
|
+
return node ? actualCallback(node) : void 0;
|
|
9084
9089
|
}
|
|
9085
9090
|
let targetKey;
|
|
9086
9091
|
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
@@ -9088,6 +9093,9 @@ var BST = class extends BinaryTree {
|
|
|
9088
9093
|
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
9089
9094
|
const key = keyNodeEntryOrPredicate[0];
|
|
9090
9095
|
if (key === null || key === void 0) {
|
|
9096
|
+
if (typeof callback === "string" || !callback) {
|
|
9097
|
+
return void 0;
|
|
9098
|
+
}
|
|
9091
9099
|
return void 0;
|
|
9092
9100
|
}
|
|
9093
9101
|
targetKey = key;
|
|
@@ -9095,7 +9103,14 @@ var BST = class extends BinaryTree {
|
|
|
9095
9103
|
targetKey = keyNodeEntryOrPredicate;
|
|
9096
9104
|
}
|
|
9097
9105
|
if (targetKey !== void 0) {
|
|
9098
|
-
|
|
9106
|
+
const node = this._lowerByKey(targetKey, actualIterationType);
|
|
9107
|
+
if (!actualCallback) {
|
|
9108
|
+
return node?.key;
|
|
9109
|
+
}
|
|
9110
|
+
return node ? actualCallback(node) : void 0;
|
|
9111
|
+
}
|
|
9112
|
+
if (typeof callback === "string" || !callback) {
|
|
9113
|
+
return void 0;
|
|
9099
9114
|
}
|
|
9100
9115
|
return void 0;
|
|
9101
9116
|
}
|
|
@@ -9284,7 +9299,6 @@ var BST = class extends BinaryTree {
|
|
|
9284
9299
|
*/
|
|
9285
9300
|
_createDefaultComparator() {
|
|
9286
9301
|
return (a, b) => {
|
|
9287
|
-
debugger;
|
|
9288
9302
|
if (isComparable(a) && isComparable(b)) {
|
|
9289
9303
|
if (a > b) return 1;
|
|
9290
9304
|
if (a < b) return -1;
|