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