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