data-structure-typed 2.2.4 → 2.2.5
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 +284 -28
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +286 -28
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +284 -28
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +286 -28
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +100 -6
- package/dist/umd/data-structure-typed.js +286 -28
- 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 +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +3 -1
- package/src/data-structures/binary-tree/bst.ts +429 -48
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/bst.test.ts +456 -2
- package/test/unit/data-structures/binary-tree/overall.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +1 -1
package/dist/esm/index.mjs
CHANGED
|
@@ -8654,27 +8654,6 @@ var BST = class extends BinaryTree {
|
|
|
8654
8654
|
get root() {
|
|
8655
8655
|
return this._root;
|
|
8656
8656
|
}
|
|
8657
|
-
/**
|
|
8658
|
-
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
8659
|
-
* @remarks Time O(1) Space O(1)
|
|
8660
|
-
* @returns The default comparator function.
|
|
8661
|
-
*/
|
|
8662
|
-
_createDefaultComparator() {
|
|
8663
|
-
return (a, b) => {
|
|
8664
|
-
debugger;
|
|
8665
|
-
if (isComparable(a) && isComparable(b)) {
|
|
8666
|
-
if (a > b) return 1;
|
|
8667
|
-
if (a < b) return -1;
|
|
8668
|
-
return 0;
|
|
8669
|
-
}
|
|
8670
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
8671
|
-
throw TypeError(
|
|
8672
|
-
`When comparing object type keys, a custom comparator must be provided in the constructor's options!`
|
|
8673
|
-
);
|
|
8674
|
-
}
|
|
8675
|
-
return 0;
|
|
8676
|
-
};
|
|
8677
|
-
}
|
|
8678
8657
|
/**
|
|
8679
8658
|
* The comparator function used to determine the order of keys in the tree.
|
|
8680
8659
|
|
|
@@ -9022,6 +9001,104 @@ var BST = class extends BinaryTree {
|
|
|
9022
9001
|
upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
9023
9002
|
return this._bound(keyNodeEntryOrPredicate, false, iterationType);
|
|
9024
9003
|
}
|
|
9004
|
+
/**
|
|
9005
|
+
* Returns the first node with a key greater than or equal to the given key.
|
|
9006
|
+
* This is equivalent to Java TreeMap.ceilingEntry().
|
|
9007
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
9008
|
+
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
9009
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
9010
|
+
*
|
|
9011
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
9012
|
+
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
9013
|
+
* @returns The first node with key >= given key, or undefined if no such node exists.
|
|
9014
|
+
*/
|
|
9015
|
+
ceilingEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
9016
|
+
return this.lowerBound(keyNodeEntryOrPredicate, iterationType);
|
|
9017
|
+
}
|
|
9018
|
+
/**
|
|
9019
|
+
* Returns the first node with a key strictly greater than the given key.
|
|
9020
|
+
* This is equivalent to Java TreeMap.higherEntry().
|
|
9021
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
9022
|
+
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
9023
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
9024
|
+
*
|
|
9025
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
9026
|
+
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
9027
|
+
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
9028
|
+
*/
|
|
9029
|
+
higherEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
9030
|
+
return this.upperBound(keyNodeEntryOrPredicate, iterationType);
|
|
9031
|
+
}
|
|
9032
|
+
/**
|
|
9033
|
+
* Returns the first node with a key less than or equal to the given key.
|
|
9034
|
+
* This is equivalent to Java TreeMap.floorEntry().
|
|
9035
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
9036
|
+
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
9037
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
9038
|
+
*
|
|
9039
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
9040
|
+
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
9041
|
+
* @returns The first node with key <= given key, or undefined if no such node exists.
|
|
9042
|
+
*/
|
|
9043
|
+
floorEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
9044
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
9045
|
+
return void 0;
|
|
9046
|
+
}
|
|
9047
|
+
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
9048
|
+
return this._floorByPredicate(keyNodeEntryOrPredicate, iterationType);
|
|
9049
|
+
}
|
|
9050
|
+
let targetKey;
|
|
9051
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
9052
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
9053
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
9054
|
+
const key = keyNodeEntryOrPredicate[0];
|
|
9055
|
+
if (key === null || key === void 0) {
|
|
9056
|
+
return void 0;
|
|
9057
|
+
}
|
|
9058
|
+
targetKey = key;
|
|
9059
|
+
} else {
|
|
9060
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
9061
|
+
}
|
|
9062
|
+
if (targetKey !== void 0) {
|
|
9063
|
+
return this._floorByKey(targetKey, iterationType);
|
|
9064
|
+
}
|
|
9065
|
+
return void 0;
|
|
9066
|
+
}
|
|
9067
|
+
/**
|
|
9068
|
+
* Returns the first node with a key strictly less than the given key.
|
|
9069
|
+
* This is equivalent to Java TreeMap.lowerEntry().
|
|
9070
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
9071
|
+
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
9072
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
9073
|
+
*
|
|
9074
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
9075
|
+
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
9076
|
+
* @returns The first node with key < given key, or undefined if no such node exists.
|
|
9077
|
+
*/
|
|
9078
|
+
lowerEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
9079
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
9080
|
+
return void 0;
|
|
9081
|
+
}
|
|
9082
|
+
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
9083
|
+
return this._lowerByPredicate(keyNodeEntryOrPredicate, iterationType);
|
|
9084
|
+
}
|
|
9085
|
+
let targetKey;
|
|
9086
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
9087
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
9088
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
9089
|
+
const key = keyNodeEntryOrPredicate[0];
|
|
9090
|
+
if (key === null || key === void 0) {
|
|
9091
|
+
return void 0;
|
|
9092
|
+
}
|
|
9093
|
+
targetKey = key;
|
|
9094
|
+
} else {
|
|
9095
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
9096
|
+
}
|
|
9097
|
+
if (targetKey !== void 0) {
|
|
9098
|
+
return this._lowerByKey(targetKey, iterationType);
|
|
9099
|
+
}
|
|
9100
|
+
return void 0;
|
|
9101
|
+
}
|
|
9025
9102
|
/**
|
|
9026
9103
|
* Traverses the tree and returns nodes that are lesser or greater than a target node.
|
|
9027
9104
|
* @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
|
|
@@ -9192,13 +9269,7 @@ var BST = class extends BinaryTree {
|
|
|
9192
9269
|
* - If no nodes match the search criteria, the returned map is empty.
|
|
9193
9270
|
*/
|
|
9194
9271
|
deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
9195
|
-
const toDelete = this.search(
|
|
9196
|
-
keyNodeEntryOrPredicate,
|
|
9197
|
-
onlyOne,
|
|
9198
|
-
(node) => node,
|
|
9199
|
-
startNode,
|
|
9200
|
-
iterationType
|
|
9201
|
-
);
|
|
9272
|
+
const toDelete = this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
|
|
9202
9273
|
let results = [];
|
|
9203
9274
|
for (const node of toDelete) {
|
|
9204
9275
|
const deleteInfo = this.delete(node);
|
|
@@ -9206,6 +9277,191 @@ var BST = class extends BinaryTree {
|
|
|
9206
9277
|
}
|
|
9207
9278
|
return results;
|
|
9208
9279
|
}
|
|
9280
|
+
/**
|
|
9281
|
+
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
9282
|
+
* @remarks Time O(1) Space O(1)
|
|
9283
|
+
* @returns The default comparator function.
|
|
9284
|
+
*/
|
|
9285
|
+
_createDefaultComparator() {
|
|
9286
|
+
return (a, b) => {
|
|
9287
|
+
debugger;
|
|
9288
|
+
if (isComparable(a) && isComparable(b)) {
|
|
9289
|
+
if (a > b) return 1;
|
|
9290
|
+
if (a < b) return -1;
|
|
9291
|
+
return 0;
|
|
9292
|
+
}
|
|
9293
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
9294
|
+
throw TypeError(
|
|
9295
|
+
`When comparing object type keys, a custom comparator must be provided in the constructor's options!`
|
|
9296
|
+
);
|
|
9297
|
+
}
|
|
9298
|
+
return 0;
|
|
9299
|
+
};
|
|
9300
|
+
}
|
|
9301
|
+
/**
|
|
9302
|
+
* (Protected) Binary search for floor by key with pruning optimization.
|
|
9303
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
9304
|
+
* Finds first node where key <= target.
|
|
9305
|
+
* @remarks Time O(h) where h is tree height.
|
|
9306
|
+
*
|
|
9307
|
+
* @param key - The target key to search for.
|
|
9308
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
9309
|
+
* @returns The first node with key <= target, or undefined if none exists.
|
|
9310
|
+
*/
|
|
9311
|
+
_floorByKey(key, iterationType) {
|
|
9312
|
+
if (iterationType === "RECURSIVE") {
|
|
9313
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
9314
|
+
if (!this.isRealNode(cur)) return void 0;
|
|
9315
|
+
const cmp = this.comparator(cur.key, key);
|
|
9316
|
+
if (cmp <= 0) {
|
|
9317
|
+
const rightResult = dfs(cur.right);
|
|
9318
|
+
return rightResult ?? cur;
|
|
9319
|
+
} else {
|
|
9320
|
+
return dfs(cur.left);
|
|
9321
|
+
}
|
|
9322
|
+
}, "dfs");
|
|
9323
|
+
return dfs(this.root);
|
|
9324
|
+
} else {
|
|
9325
|
+
let current = this.root;
|
|
9326
|
+
let result = void 0;
|
|
9327
|
+
while (this.isRealNode(current)) {
|
|
9328
|
+
const cmp = this.comparator(current.key, key);
|
|
9329
|
+
if (cmp <= 0) {
|
|
9330
|
+
result = current;
|
|
9331
|
+
current = current.right ?? void 0;
|
|
9332
|
+
} else {
|
|
9333
|
+
current = current.left ?? void 0;
|
|
9334
|
+
}
|
|
9335
|
+
}
|
|
9336
|
+
return result;
|
|
9337
|
+
}
|
|
9338
|
+
}
|
|
9339
|
+
/**
|
|
9340
|
+
* (Protected) In-order traversal search for floor by predicate.
|
|
9341
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
9342
|
+
* Returns the last node that satisfies the predicate function.
|
|
9343
|
+
* @remarks Time Complexity: O(n) since it may visit every node.
|
|
9344
|
+
* Space Complexity: O(h) for recursion, O(h) for iterative stack.
|
|
9345
|
+
*
|
|
9346
|
+
* @param predicate - The predicate function to test nodes.
|
|
9347
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
9348
|
+
* @returns The last node satisfying predicate (highest key), or undefined if none found.
|
|
9349
|
+
*/
|
|
9350
|
+
_floorByPredicate(predicate, iterationType) {
|
|
9351
|
+
if (iterationType === "RECURSIVE") {
|
|
9352
|
+
let result = void 0;
|
|
9353
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
9354
|
+
if (!this.isRealNode(cur)) return;
|
|
9355
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
9356
|
+
if (predicate(cur)) {
|
|
9357
|
+
result = cur;
|
|
9358
|
+
}
|
|
9359
|
+
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
9360
|
+
}, "dfs");
|
|
9361
|
+
dfs(this.root);
|
|
9362
|
+
return result;
|
|
9363
|
+
} else {
|
|
9364
|
+
const stack = [];
|
|
9365
|
+
let current = this.root;
|
|
9366
|
+
let result = void 0;
|
|
9367
|
+
while (stack.length > 0 || this.isRealNode(current)) {
|
|
9368
|
+
if (this.isRealNode(current)) {
|
|
9369
|
+
stack.push(current);
|
|
9370
|
+
current = current.left;
|
|
9371
|
+
} else {
|
|
9372
|
+
const node = stack.pop();
|
|
9373
|
+
if (!this.isRealNode(node)) break;
|
|
9374
|
+
if (predicate(node)) {
|
|
9375
|
+
result = node;
|
|
9376
|
+
}
|
|
9377
|
+
current = node.right;
|
|
9378
|
+
}
|
|
9379
|
+
}
|
|
9380
|
+
return result;
|
|
9381
|
+
}
|
|
9382
|
+
}
|
|
9383
|
+
/**
|
|
9384
|
+
* (Protected) Binary search for lower by key with pruning optimization.
|
|
9385
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
9386
|
+
* Finds first node where key < target.
|
|
9387
|
+
* @remarks Time O(h) where h is tree height.
|
|
9388
|
+
*
|
|
9389
|
+
* @param key - The target key to search for.
|
|
9390
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
9391
|
+
* @returns The first node with key < target, or undefined if none exists.
|
|
9392
|
+
*/
|
|
9393
|
+
_lowerByKey(key, iterationType) {
|
|
9394
|
+
if (iterationType === "RECURSIVE") {
|
|
9395
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
9396
|
+
if (!this.isRealNode(cur)) return void 0;
|
|
9397
|
+
const cmp = this.comparator(cur.key, key);
|
|
9398
|
+
if (cmp < 0) {
|
|
9399
|
+
const rightResult = dfs(cur.right);
|
|
9400
|
+
return rightResult ?? cur;
|
|
9401
|
+
} else {
|
|
9402
|
+
return dfs(cur.left);
|
|
9403
|
+
}
|
|
9404
|
+
}, "dfs");
|
|
9405
|
+
return dfs(this.root);
|
|
9406
|
+
} else {
|
|
9407
|
+
let current = this.root;
|
|
9408
|
+
let result = void 0;
|
|
9409
|
+
while (this.isRealNode(current)) {
|
|
9410
|
+
const cmp = this.comparator(current.key, key);
|
|
9411
|
+
if (cmp < 0) {
|
|
9412
|
+
result = current;
|
|
9413
|
+
current = current.right ?? void 0;
|
|
9414
|
+
} else {
|
|
9415
|
+
current = current.left ?? void 0;
|
|
9416
|
+
}
|
|
9417
|
+
}
|
|
9418
|
+
return result;
|
|
9419
|
+
}
|
|
9420
|
+
}
|
|
9421
|
+
/**
|
|
9422
|
+
* (Protected) In-order traversal search for lower by predicate.
|
|
9423
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
9424
|
+
* Returns the node that satisfies the predicate and appears last in in-order traversal.
|
|
9425
|
+
* @remarks Time Complexity: O(n) since it may visit every node.
|
|
9426
|
+
* Space Complexity: O(h) for recursion, O(h) for iterative stack.
|
|
9427
|
+
*
|
|
9428
|
+
* @param predicate - The predicate function to test nodes.
|
|
9429
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
9430
|
+
* @returns The last node satisfying predicate (highest key < target), or undefined if none found.
|
|
9431
|
+
*/
|
|
9432
|
+
_lowerByPredicate(predicate, iterationType) {
|
|
9433
|
+
if (iterationType === "RECURSIVE") {
|
|
9434
|
+
let result = void 0;
|
|
9435
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
9436
|
+
if (!this.isRealNode(cur)) return;
|
|
9437
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
9438
|
+
if (predicate(cur)) {
|
|
9439
|
+
result = cur;
|
|
9440
|
+
}
|
|
9441
|
+
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
9442
|
+
}, "dfs");
|
|
9443
|
+
dfs(this.root);
|
|
9444
|
+
return result;
|
|
9445
|
+
} else {
|
|
9446
|
+
const stack = [];
|
|
9447
|
+
let current = this.root;
|
|
9448
|
+
let result = void 0;
|
|
9449
|
+
while (stack.length > 0 || this.isRealNode(current)) {
|
|
9450
|
+
if (this.isRealNode(current)) {
|
|
9451
|
+
stack.push(current);
|
|
9452
|
+
current = current.left;
|
|
9453
|
+
} else {
|
|
9454
|
+
const node = stack.pop();
|
|
9455
|
+
if (!this.isRealNode(node)) break;
|
|
9456
|
+
if (predicate(node)) {
|
|
9457
|
+
result = node;
|
|
9458
|
+
}
|
|
9459
|
+
current = node.right;
|
|
9460
|
+
}
|
|
9461
|
+
}
|
|
9462
|
+
return result;
|
|
9463
|
+
}
|
|
9464
|
+
}
|
|
9209
9465
|
/**
|
|
9210
9466
|
* (Protected) Core bound search implementation supporting all parameter types.
|
|
9211
9467
|
* Unified logic for both lowerBound and upperBound.
|