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