avl-tree-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/dist/cjs/index.cjs +101 -83
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +101 -83
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +101 -83
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +101 -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/avl-tree-typed.js +101 -83
- package/dist/umd/avl-tree-typed.js.map +1 -1
- package/dist/umd/avl-tree-typed.min.js +3 -3
- package/dist/umd/avl-tree-typed.min.js.map +1 -1
- package/package.json +2 -2
- 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
|
@@ -998,6 +998,14 @@ var _IterableEntryBase = class _IterableEntryBase {
|
|
|
998
998
|
}
|
|
999
999
|
return accumulator;
|
|
1000
1000
|
}
|
|
1001
|
+
/**
|
|
1002
|
+
* Converts data structure to `[key, value]` pairs.
|
|
1003
|
+
* @returns Array of entries.
|
|
1004
|
+
* @remarks Time O(n), Space O(n)
|
|
1005
|
+
*/
|
|
1006
|
+
toArray() {
|
|
1007
|
+
return [...this];
|
|
1008
|
+
}
|
|
1001
1009
|
/**
|
|
1002
1010
|
* Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
|
|
1003
1011
|
* @returns Array of entries (default) or a string.
|
|
@@ -1024,8 +1032,6 @@ var _Range = class _Range {
|
|
|
1024
1032
|
this.high = high;
|
|
1025
1033
|
this.includeLow = includeLow;
|
|
1026
1034
|
this.includeHigh = includeHigh;
|
|
1027
|
-
if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
|
|
1028
|
-
if (low > high) throw new RangeError("low must be less than or equal to high");
|
|
1029
1035
|
}
|
|
1030
1036
|
// Determine whether a key is within the range
|
|
1031
1037
|
isInRange(key, comparator) {
|
|
@@ -3169,77 +3175,63 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3169
3175
|
else _iterate();
|
|
3170
3176
|
return inserted;
|
|
3171
3177
|
}
|
|
3172
|
-
|
|
3173
|
-
|
|
3174
|
-
|
|
3175
|
-
|
|
3176
|
-
|
|
3177
|
-
|
|
3178
|
-
|
|
3179
|
-
|
|
3180
|
-
|
|
3181
|
-
|
|
3182
|
-
|
|
3183
|
-
|
|
3184
|
-
|
|
3185
|
-
|
|
3186
|
-
|
|
3187
|
-
|
|
3188
|
-
|
|
3189
|
-
|
|
3190
|
-
|
|
3191
|
-
|
|
3192
|
-
|
|
3193
|
-
|
|
3194
|
-
|
|
3195
|
-
|
|
3196
|
-
|
|
3197
|
-
|
|
3198
|
-
|
|
3199
|
-
|
|
3200
|
-
|
|
3201
|
-
|
|
3202
|
-
|
|
3203
|
-
|
|
3204
|
-
|
|
3205
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3206
|
-
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
3207
|
-
* @returns The first node with key >= given key, or undefined if no such node exists.
|
|
3208
|
-
*/
|
|
3209
|
-
ceilingEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
3210
|
-
return this.lowerBound(keyNodeEntryOrPredicate, iterationType);
|
|
3211
|
-
}
|
|
3212
|
-
/**
|
|
3213
|
-
* Returns the first node with a key strictly greater than the given key.
|
|
3214
|
-
* This is equivalent to Java TreeMap.higherEntry().
|
|
3215
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
3216
|
-
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
3217
|
-
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
3218
|
-
*
|
|
3219
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3220
|
-
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
3221
|
-
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
3222
|
-
*/
|
|
3223
|
-
higherEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
3224
|
-
return this.upperBound(keyNodeEntryOrPredicate, iterationType);
|
|
3178
|
+
ceiling(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
3179
|
+
let actualCallback = void 0;
|
|
3180
|
+
let actualIterationType = this.iterationType;
|
|
3181
|
+
if (typeof callback === "string") {
|
|
3182
|
+
actualIterationType = callback;
|
|
3183
|
+
} else if (callback) {
|
|
3184
|
+
actualCallback = callback;
|
|
3185
|
+
if (iterationType) {
|
|
3186
|
+
actualIterationType = iterationType;
|
|
3187
|
+
}
|
|
3188
|
+
}
|
|
3189
|
+
const node = this._bound(keyNodeEntryOrPredicate, true, actualIterationType);
|
|
3190
|
+
if (!actualCallback) {
|
|
3191
|
+
return node == null ? void 0 : node.key;
|
|
3192
|
+
}
|
|
3193
|
+
return node ? actualCallback(node) : void 0;
|
|
3194
|
+
}
|
|
3195
|
+
higher(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
3196
|
+
let actualCallback = void 0;
|
|
3197
|
+
let actualIterationType = this.iterationType;
|
|
3198
|
+
if (typeof callback === "string") {
|
|
3199
|
+
actualIterationType = callback;
|
|
3200
|
+
} else if (callback) {
|
|
3201
|
+
actualCallback = callback;
|
|
3202
|
+
if (iterationType) {
|
|
3203
|
+
actualIterationType = iterationType;
|
|
3204
|
+
}
|
|
3205
|
+
}
|
|
3206
|
+
const node = this._bound(keyNodeEntryOrPredicate, false, actualIterationType);
|
|
3207
|
+
if (!actualCallback) {
|
|
3208
|
+
return node == null ? void 0 : node.key;
|
|
3209
|
+
}
|
|
3210
|
+
return node ? actualCallback(node) : void 0;
|
|
3225
3211
|
}
|
|
3226
|
-
|
|
3227
|
-
* Returns the first node with a key less than or equal to the given key.
|
|
3228
|
-
* This is equivalent to Java TreeMap.floorEntry().
|
|
3229
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
3230
|
-
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
3231
|
-
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
3232
|
-
*
|
|
3233
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3234
|
-
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
3235
|
-
* @returns The first node with key <= given key, or undefined if no such node exists.
|
|
3236
|
-
*/
|
|
3237
|
-
floorEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
3212
|
+
floor(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
3238
3213
|
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
3214
|
+
if (typeof callback === "string" || !callback) {
|
|
3215
|
+
return void 0;
|
|
3216
|
+
}
|
|
3239
3217
|
return void 0;
|
|
3240
3218
|
}
|
|
3219
|
+
let actualCallback = void 0;
|
|
3220
|
+
let actualIterationType = this.iterationType;
|
|
3221
|
+
if (typeof callback === "string") {
|
|
3222
|
+
actualIterationType = callback;
|
|
3223
|
+
} else if (callback) {
|
|
3224
|
+
actualCallback = callback;
|
|
3225
|
+
if (iterationType) {
|
|
3226
|
+
actualIterationType = iterationType;
|
|
3227
|
+
}
|
|
3228
|
+
}
|
|
3241
3229
|
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
3242
|
-
|
|
3230
|
+
const node = this._floorByPredicate(keyNodeEntryOrPredicate, actualIterationType);
|
|
3231
|
+
if (!actualCallback) {
|
|
3232
|
+
return node == null ? void 0 : node.key;
|
|
3233
|
+
}
|
|
3234
|
+
return node ? actualCallback(node) : void 0;
|
|
3243
3235
|
}
|
|
3244
3236
|
let targetKey;
|
|
3245
3237
|
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
@@ -3247,6 +3239,9 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3247
3239
|
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
3248
3240
|
const key = keyNodeEntryOrPredicate[0];
|
|
3249
3241
|
if (key === null || key === void 0) {
|
|
3242
|
+
if (typeof callback === "string" || !callback) {
|
|
3243
|
+
return void 0;
|
|
3244
|
+
}
|
|
3250
3245
|
return void 0;
|
|
3251
3246
|
}
|
|
3252
3247
|
targetKey = key;
|
|
@@ -3254,27 +3249,40 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3254
3249
|
targetKey = keyNodeEntryOrPredicate;
|
|
3255
3250
|
}
|
|
3256
3251
|
if (targetKey !== void 0) {
|
|
3257
|
-
|
|
3252
|
+
const node = this._floorByKey(targetKey, actualIterationType);
|
|
3253
|
+
if (!actualCallback) {
|
|
3254
|
+
return node == null ? void 0 : node.key;
|
|
3255
|
+
}
|
|
3256
|
+
return node ? actualCallback(node) : void 0;
|
|
3257
|
+
}
|
|
3258
|
+
if (typeof callback === "string" || !callback) {
|
|
3259
|
+
return void 0;
|
|
3258
3260
|
}
|
|
3259
3261
|
return void 0;
|
|
3260
3262
|
}
|
|
3261
|
-
|
|
3262
|
-
* Returns the first node with a key strictly less than the given key.
|
|
3263
|
-
* This is equivalent to Java TreeMap.lowerEntry().
|
|
3264
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
3265
|
-
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
3266
|
-
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
3267
|
-
*
|
|
3268
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3269
|
-
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
3270
|
-
* @returns The first node with key < given key, or undefined if no such node exists.
|
|
3271
|
-
*/
|
|
3272
|
-
lowerEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
3263
|
+
lower(keyNodeEntryOrPredicate, callback, iterationType) {
|
|
3273
3264
|
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
3265
|
+
if (typeof callback === "string" || !callback) {
|
|
3266
|
+
return void 0;
|
|
3267
|
+
}
|
|
3274
3268
|
return void 0;
|
|
3275
3269
|
}
|
|
3270
|
+
let actualCallback = void 0;
|
|
3271
|
+
let actualIterationType = this.iterationType;
|
|
3272
|
+
if (typeof callback === "string") {
|
|
3273
|
+
actualIterationType = callback;
|
|
3274
|
+
} else if (callback) {
|
|
3275
|
+
actualCallback = callback;
|
|
3276
|
+
if (iterationType) {
|
|
3277
|
+
actualIterationType = iterationType;
|
|
3278
|
+
}
|
|
3279
|
+
}
|
|
3276
3280
|
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
3277
|
-
|
|
3281
|
+
const node = this._lowerByPredicate(keyNodeEntryOrPredicate, actualIterationType);
|
|
3282
|
+
if (!actualCallback) {
|
|
3283
|
+
return node == null ? void 0 : node.key;
|
|
3284
|
+
}
|
|
3285
|
+
return node ? actualCallback(node) : void 0;
|
|
3278
3286
|
}
|
|
3279
3287
|
let targetKey;
|
|
3280
3288
|
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
@@ -3282,6 +3290,9 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3282
3290
|
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
3283
3291
|
const key = keyNodeEntryOrPredicate[0];
|
|
3284
3292
|
if (key === null || key === void 0) {
|
|
3293
|
+
if (typeof callback === "string" || !callback) {
|
|
3294
|
+
return void 0;
|
|
3295
|
+
}
|
|
3285
3296
|
return void 0;
|
|
3286
3297
|
}
|
|
3287
3298
|
targetKey = key;
|
|
@@ -3289,7 +3300,14 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3289
3300
|
targetKey = keyNodeEntryOrPredicate;
|
|
3290
3301
|
}
|
|
3291
3302
|
if (targetKey !== void 0) {
|
|
3292
|
-
|
|
3303
|
+
const node = this._lowerByKey(targetKey, actualIterationType);
|
|
3304
|
+
if (!actualCallback) {
|
|
3305
|
+
return node == null ? void 0 : node.key;
|
|
3306
|
+
}
|
|
3307
|
+
return node ? actualCallback(node) : void 0;
|
|
3308
|
+
}
|
|
3309
|
+
if (typeof callback === "string" || !callback) {
|
|
3310
|
+
return void 0;
|
|
3293
3311
|
}
|
|
3294
3312
|
return void 0;
|
|
3295
3313
|
}
|