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