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/CHANGELOG.md
CHANGED
|
@@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file.
|
|
|
8
8
|
- [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
|
|
9
9
|
- [`auto-changelog`](https://github.com/CookPete/auto-changelog)
|
|
10
10
|
|
|
11
|
-
## [v2.2.
|
|
11
|
+
## [v2.2.6](https://github.com/zrwusa/data-structure-typed/compare/v2.2.3...main) (upcoming)
|
|
12
12
|
|
|
13
13
|
## [v2.2.3](https://github.com/zrwusa/data-structure-typed/compare/v2.2.2...v2.2.3) (6 January 2026)
|
|
14
14
|
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -160,6 +160,14 @@ var IterableEntryBase = class {
|
|
|
160
160
|
}
|
|
161
161
|
return accumulator;
|
|
162
162
|
}
|
|
163
|
+
/**
|
|
164
|
+
* Converts data structure to `[key, value]` pairs.
|
|
165
|
+
* @returns Array of entries.
|
|
166
|
+
* @remarks Time O(n), Space O(n)
|
|
167
|
+
*/
|
|
168
|
+
toArray() {
|
|
169
|
+
return [...this];
|
|
170
|
+
}
|
|
163
171
|
/**
|
|
164
172
|
* Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
|
|
165
173
|
* @returns Array of entries (default) or a string.
|
|
@@ -173,7 +181,6 @@ var IterableEntryBase = class {
|
|
|
173
181
|
* @remarks Time O(n), Space O(n)
|
|
174
182
|
*/
|
|
175
183
|
print() {
|
|
176
|
-
console.log(this.toVisual());
|
|
177
184
|
}
|
|
178
185
|
};
|
|
179
186
|
|
|
@@ -396,7 +403,6 @@ var IterableElementBase = class {
|
|
|
396
403
|
* Time O(n) due to materialization, Space O(n) for the intermediate representation.
|
|
397
404
|
*/
|
|
398
405
|
print() {
|
|
399
|
-
console.log(this.toVisual());
|
|
400
406
|
}
|
|
401
407
|
};
|
|
402
408
|
|
|
@@ -6830,8 +6836,6 @@ var Range = class {
|
|
|
6830
6836
|
this.high = high;
|
|
6831
6837
|
this.includeLow = includeLow;
|
|
6832
6838
|
this.includeHigh = includeHigh;
|
|
6833
|
-
if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
|
|
6834
|
-
if (low > high) throw new RangeError("low must be less than or equal to high");
|
|
6835
6839
|
}
|
|
6836
6840
|
static {
|
|
6837
6841
|
__name(this, "Range");
|
|
@@ -8075,7 +8079,6 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
8075
8079
|
* @param [startNode=this._root] - The node to start printing from.
|
|
8076
8080
|
*/
|
|
8077
8081
|
print(options, startNode = this._root) {
|
|
8078
|
-
console.log(this.toVisual(startNode, options));
|
|
8079
8082
|
}
|
|
8080
8083
|
/**
|
|
8081
8084
|
* (Protected) Core DFS implementation.
|
|
@@ -8977,77 +8980,63 @@ var BST = class extends BinaryTree {
|
|
|
8977
8980
|
else _iterate();
|
|
8978
8981
|
return inserted;
|
|
8979
8982
|
}
|
|
8980
|
-
|
|
8981
|
-
|
|
8982
|
-
|
|
8983
|
-
|
|
8984
|
-
|
|
8985
|
-
|
|
8986
|
-
|
|
8987
|
-
|
|
8988
|
-
|
|
8989
|
-
|
|
8990
|
-
|
|
8991
|
-
|
|
8992
|
-
|
|
8993
|
-
|
|
8994
|
-
|
|
8995
|
-
|
|
8996
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
8997
|
-
* Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
8998
|
-
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
8999
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
9000
|
-
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
9001
|
-
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
9002
|
-
*/
|
|
9003
|
-
upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
9004
|
-
return this._bound(keyNodeEntryOrPredicate, false, iterationType);
|
|
9005
|
-
}
|
|
9006
|
-
/**
|
|
9007
|
-
* Returns the first node with a key greater than or equal to the given key.
|
|
9008
|
-
* This is equivalent to Java TreeMap.ceilingEntry().
|
|
9009
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
9010
|
-
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
9011
|
-
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
9012
|
-
*
|
|
9013
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
9014
|
-
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
9015
|
-
* @returns The first node with key >= given key, or undefined if no such node exists.
|
|
9016
|
-
*/
|
|
9017
|
-
ceilingEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
9018
|
-
return this.lowerBound(keyNodeEntryOrPredicate, iterationType);
|
|
8983
|
+
ceiling(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
8984
|
+
let actualCallback = void 0;
|
|
8985
|
+
let actualIterationType = this.iterationType;
|
|
8986
|
+
if (typeof callback === "string") {
|
|
8987
|
+
actualIterationType = callback;
|
|
8988
|
+
} else if (callback) {
|
|
8989
|
+
actualCallback = callback;
|
|
8990
|
+
if (iterationType) {
|
|
8991
|
+
actualIterationType = iterationType;
|
|
8992
|
+
}
|
|
8993
|
+
}
|
|
8994
|
+
const node = this._bound(keyNodeEntryOrPredicate, true, actualIterationType);
|
|
8995
|
+
if (!actualCallback) {
|
|
8996
|
+
return node?.key;
|
|
8997
|
+
}
|
|
8998
|
+
return node ? actualCallback(node) : void 0;
|
|
9019
8999
|
}
|
|
9020
|
-
|
|
9021
|
-
|
|
9022
|
-
|
|
9023
|
-
|
|
9024
|
-
|
|
9025
|
-
|
|
9026
|
-
|
|
9027
|
-
|
|
9028
|
-
|
|
9029
|
-
|
|
9030
|
-
|
|
9031
|
-
|
|
9032
|
-
|
|
9000
|
+
higher(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
9001
|
+
let actualCallback = void 0;
|
|
9002
|
+
let actualIterationType = this.iterationType;
|
|
9003
|
+
if (typeof callback === "string") {
|
|
9004
|
+
actualIterationType = callback;
|
|
9005
|
+
} else if (callback) {
|
|
9006
|
+
actualCallback = callback;
|
|
9007
|
+
if (iterationType) {
|
|
9008
|
+
actualIterationType = iterationType;
|
|
9009
|
+
}
|
|
9010
|
+
}
|
|
9011
|
+
const node = this._bound(keyNodeEntryOrPredicate, false, actualIterationType);
|
|
9012
|
+
if (!actualCallback) {
|
|
9013
|
+
return node?.key;
|
|
9014
|
+
}
|
|
9015
|
+
return node ? actualCallback(node) : void 0;
|
|
9033
9016
|
}
|
|
9034
|
-
|
|
9035
|
-
* Returns the first node with a key less than or equal to the given key.
|
|
9036
|
-
* This is equivalent to Java TreeMap.floorEntry().
|
|
9037
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
9038
|
-
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
9039
|
-
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
9040
|
-
*
|
|
9041
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
9042
|
-
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
9043
|
-
* @returns The first node with key <= given key, or undefined if no such node exists.
|
|
9044
|
-
*/
|
|
9045
|
-
floorEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
9017
|
+
floor(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
9046
9018
|
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
9019
|
+
if (typeof callback === "string" || !callback) {
|
|
9020
|
+
return void 0;
|
|
9021
|
+
}
|
|
9047
9022
|
return void 0;
|
|
9048
9023
|
}
|
|
9024
|
+
let actualCallback = void 0;
|
|
9025
|
+
let actualIterationType = this.iterationType;
|
|
9026
|
+
if (typeof callback === "string") {
|
|
9027
|
+
actualIterationType = callback;
|
|
9028
|
+
} else if (callback) {
|
|
9029
|
+
actualCallback = callback;
|
|
9030
|
+
if (iterationType) {
|
|
9031
|
+
actualIterationType = iterationType;
|
|
9032
|
+
}
|
|
9033
|
+
}
|
|
9049
9034
|
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
9050
|
-
|
|
9035
|
+
const node = this._floorByPredicate(keyNodeEntryOrPredicate, actualIterationType);
|
|
9036
|
+
if (!actualCallback) {
|
|
9037
|
+
return node?.key;
|
|
9038
|
+
}
|
|
9039
|
+
return node ? actualCallback(node) : void 0;
|
|
9051
9040
|
}
|
|
9052
9041
|
let targetKey;
|
|
9053
9042
|
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
@@ -9055,6 +9044,9 @@ var BST = class extends BinaryTree {
|
|
|
9055
9044
|
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
9056
9045
|
const key = keyNodeEntryOrPredicate[0];
|
|
9057
9046
|
if (key === null || key === void 0) {
|
|
9047
|
+
if (typeof callback === "string" || !callback) {
|
|
9048
|
+
return void 0;
|
|
9049
|
+
}
|
|
9058
9050
|
return void 0;
|
|
9059
9051
|
}
|
|
9060
9052
|
targetKey = key;
|
|
@@ -9062,27 +9054,40 @@ var BST = class extends BinaryTree {
|
|
|
9062
9054
|
targetKey = keyNodeEntryOrPredicate;
|
|
9063
9055
|
}
|
|
9064
9056
|
if (targetKey !== void 0) {
|
|
9065
|
-
|
|
9057
|
+
const node = this._floorByKey(targetKey, actualIterationType);
|
|
9058
|
+
if (!actualCallback) {
|
|
9059
|
+
return node?.key;
|
|
9060
|
+
}
|
|
9061
|
+
return node ? actualCallback(node) : void 0;
|
|
9062
|
+
}
|
|
9063
|
+
if (typeof callback === "string" || !callback) {
|
|
9064
|
+
return void 0;
|
|
9066
9065
|
}
|
|
9067
9066
|
return void 0;
|
|
9068
9067
|
}
|
|
9069
|
-
|
|
9070
|
-
* Returns the first node with a key strictly less than the given key.
|
|
9071
|
-
* This is equivalent to Java TreeMap.lowerEntry().
|
|
9072
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
9073
|
-
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
9074
|
-
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
9075
|
-
*
|
|
9076
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
9077
|
-
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
9078
|
-
* @returns The first node with key < given key, or undefined if no such node exists.
|
|
9079
|
-
*/
|
|
9080
|
-
lowerEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
9068
|
+
lower(keyNodeEntryOrPredicate, callback, iterationType) {
|
|
9081
9069
|
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
9070
|
+
if (typeof callback === "string" || !callback) {
|
|
9071
|
+
return void 0;
|
|
9072
|
+
}
|
|
9082
9073
|
return void 0;
|
|
9083
9074
|
}
|
|
9075
|
+
let actualCallback = void 0;
|
|
9076
|
+
let actualIterationType = this.iterationType;
|
|
9077
|
+
if (typeof callback === "string") {
|
|
9078
|
+
actualIterationType = callback;
|
|
9079
|
+
} else if (callback) {
|
|
9080
|
+
actualCallback = callback;
|
|
9081
|
+
if (iterationType) {
|
|
9082
|
+
actualIterationType = iterationType;
|
|
9083
|
+
}
|
|
9084
|
+
}
|
|
9084
9085
|
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
9085
|
-
|
|
9086
|
+
const node = this._lowerByPredicate(keyNodeEntryOrPredicate, actualIterationType);
|
|
9087
|
+
if (!actualCallback) {
|
|
9088
|
+
return node?.key;
|
|
9089
|
+
}
|
|
9090
|
+
return node ? actualCallback(node) : void 0;
|
|
9086
9091
|
}
|
|
9087
9092
|
let targetKey;
|
|
9088
9093
|
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
@@ -9090,6 +9095,9 @@ var BST = class extends BinaryTree {
|
|
|
9090
9095
|
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
9091
9096
|
const key = keyNodeEntryOrPredicate[0];
|
|
9092
9097
|
if (key === null || key === void 0) {
|
|
9098
|
+
if (typeof callback === "string" || !callback) {
|
|
9099
|
+
return void 0;
|
|
9100
|
+
}
|
|
9093
9101
|
return void 0;
|
|
9094
9102
|
}
|
|
9095
9103
|
targetKey = key;
|
|
@@ -9097,7 +9105,14 @@ var BST = class extends BinaryTree {
|
|
|
9097
9105
|
targetKey = keyNodeEntryOrPredicate;
|
|
9098
9106
|
}
|
|
9099
9107
|
if (targetKey !== void 0) {
|
|
9100
|
-
|
|
9108
|
+
const node = this._lowerByKey(targetKey, actualIterationType);
|
|
9109
|
+
if (!actualCallback) {
|
|
9110
|
+
return node?.key;
|
|
9111
|
+
}
|
|
9112
|
+
return node ? actualCallback(node) : void 0;
|
|
9113
|
+
}
|
|
9114
|
+
if (typeof callback === "string" || !callback) {
|
|
9115
|
+
return void 0;
|
|
9101
9116
|
}
|
|
9102
9117
|
return void 0;
|
|
9103
9118
|
}
|
|
@@ -9286,7 +9301,6 @@ var BST = class extends BinaryTree {
|
|
|
9286
9301
|
*/
|
|
9287
9302
|
_createDefaultComparator() {
|
|
9288
9303
|
return (a, b) => {
|
|
9289
|
-
debugger;
|
|
9290
9304
|
if (isComparable(a) && isComparable(b)) {
|
|
9291
9305
|
if (a > b) return 1;
|
|
9292
9306
|
if (a < b) return -1;
|