red-black-tree-typed 2.2.2 → 2.2.3
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/README.md +92 -37
- package/dist/cjs/index.cjs +163 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +164 -0
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +163 -0
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +164 -0
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +96 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
- package/dist/types/data-structures/binary-tree/bst.d.ts +156 -13
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +84 -35
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
- package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
- package/dist/types/data-structures/heap/heap.d.ts +107 -58
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
- package/dist/types/data-structures/queue/deque.d.ts +95 -67
- package/dist/types/data-structures/queue/queue.d.ts +90 -34
- package/dist/types/data-structures/stack/stack.d.ts +58 -40
- package/dist/types/data-structures/trie/trie.d.ts +109 -47
- package/dist/types/interfaces/binary-tree.d.ts +1 -0
- package/dist/umd/red-black-tree-typed.js +164 -0
- package/dist/umd/red-black-tree-typed.js.map +1 -1
- package/dist/umd/red-black-tree-typed.min.js +3 -3
- package/dist/umd/red-black-tree-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +96 -2
- package/src/data-structures/binary-tree/binary-tree.ts +117 -7
- package/src/data-structures/binary-tree/bst.ts +322 -13
- package/src/data-structures/binary-tree/red-black-tree.ts +84 -35
- package/src/data-structures/binary-tree/tree-multi-map.ts +2 -2
- package/src/data-structures/graph/directed-graph.ts +126 -1
- package/src/data-structures/graph/undirected-graph.ts +160 -1
- package/src/data-structures/hash/hash-map.ts +110 -27
- package/src/data-structures/heap/heap.ts +107 -58
- package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
- package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
- package/src/data-structures/queue/deque.ts +95 -67
- package/src/data-structures/queue/queue.ts +90 -34
- package/src/data-structures/stack/stack.ts +58 -40
- package/src/data-structures/trie/trie.ts +109 -47
- package/src/interfaces/binary-tree.ts +2 -0
package/dist/esm/index.mjs
CHANGED
|
@@ -1461,6 +1461,17 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1461
1461
|
}
|
|
1462
1462
|
return false;
|
|
1463
1463
|
}
|
|
1464
|
+
/**
|
|
1465
|
+
* Adds or updates a new node to the tree.
|
|
1466
|
+
* @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation adds the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
|
|
1467
|
+
*
|
|
1468
|
+
* @param keyNodeOrEntry - The key, node, or entry to add or update.
|
|
1469
|
+
* @param [value] - The value, if providing just a key.
|
|
1470
|
+
* @returns True if the addition was successful, false otherwise.
|
|
1471
|
+
*/
|
|
1472
|
+
set(keyNodeOrEntry, value) {
|
|
1473
|
+
return this.add(keyNodeOrEntry, value);
|
|
1474
|
+
}
|
|
1464
1475
|
/**
|
|
1465
1476
|
* Adds multiple items to the tree.
|
|
1466
1477
|
* @remarks Time O(N * M), where N is the number of items to add and M is the size of the tree at insertion (due to O(M) `add` operation). Space O(M) (from `add`) + O(N) (for the `inserted` array).
|
|
@@ -1488,6 +1499,17 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1488
1499
|
}
|
|
1489
1500
|
return inserted;
|
|
1490
1501
|
}
|
|
1502
|
+
/**
|
|
1503
|
+
* Adds or updates multiple items to the tree.
|
|
1504
|
+
* @remarks Time O(N * M), where N is the number of items to add and M is the size of the tree at insertion (due to O(M) `add` operation). Space O(M) (from `add`) + O(N) (for the `inserted` array).
|
|
1505
|
+
*
|
|
1506
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to add or update.
|
|
1507
|
+
* @param [values] - An optional parallel iterable of values.
|
|
1508
|
+
* @returns An array of booleans indicating the success of each individual `add` operation.
|
|
1509
|
+
*/
|
|
1510
|
+
setMany(keysNodesEntriesOrRaws, values) {
|
|
1511
|
+
return this.addMany(keysNodesEntriesOrRaws, values);
|
|
1512
|
+
}
|
|
1491
1513
|
/**
|
|
1492
1514
|
* Merges another tree into this one by adding all its nodes.
|
|
1493
1515
|
* @remarks Time O(N * M), same as `addMany`, where N is the size of `anotherTree` and M is the size of this tree. Space O(M) (from `add`).
|
|
@@ -3186,6 +3208,32 @@ var BST = class extends BinaryTree {
|
|
|
3186
3208
|
else _iterate();
|
|
3187
3209
|
return inserted;
|
|
3188
3210
|
}
|
|
3211
|
+
/**
|
|
3212
|
+
* Returns the first node with a key greater than or equal to the given key.
|
|
3213
|
+
* This is equivalent to C++ std::lower_bound on a BST.
|
|
3214
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
3215
|
+
* Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
3216
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
3217
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3218
|
+
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
3219
|
+
* @returns The first node with key >= given key, or undefined if no such node exists.
|
|
3220
|
+
*/
|
|
3221
|
+
lowerBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
3222
|
+
return this._bound(keyNodeEntryOrPredicate, true, iterationType);
|
|
3223
|
+
}
|
|
3224
|
+
/**
|
|
3225
|
+
* Returns the first node with a key strictly greater than the given key.
|
|
3226
|
+
* This is equivalent to C++ std::upper_bound on a BST.
|
|
3227
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
3228
|
+
* 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
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3231
|
+
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
3232
|
+
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
3233
|
+
*/
|
|
3234
|
+
upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
3235
|
+
return this._bound(keyNodeEntryOrPredicate, false, iterationType);
|
|
3236
|
+
}
|
|
3189
3237
|
/**
|
|
3190
3238
|
* Traverses the tree and returns nodes that are lesser or greater than a target node.
|
|
3191
3239
|
* @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
|
|
@@ -3346,6 +3394,121 @@ var BST = class extends BinaryTree {
|
|
|
3346
3394
|
}
|
|
3347
3395
|
return false;
|
|
3348
3396
|
}
|
|
3397
|
+
/**
|
|
3398
|
+
* (Protected) Core bound search implementation supporting all parameter types.
|
|
3399
|
+
* Unified logic for both lowerBound and upperBound.
|
|
3400
|
+
* Resolves various input types (Key, Node, Entry, Predicate) using parent class utilities.
|
|
3401
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3402
|
+
* @param isLower - True for lowerBound (>=), false for upperBound (>).
|
|
3403
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3404
|
+
* @returns The first matching node, or undefined if no such node exists.
|
|
3405
|
+
*/
|
|
3406
|
+
_bound(keyNodeEntryOrPredicate, isLower, iterationType) {
|
|
3407
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
3408
|
+
return void 0;
|
|
3409
|
+
}
|
|
3410
|
+
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
3411
|
+
return this._boundByPredicate(keyNodeEntryOrPredicate, iterationType);
|
|
3412
|
+
}
|
|
3413
|
+
let targetKey;
|
|
3414
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
3415
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
3416
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
3417
|
+
const key = keyNodeEntryOrPredicate[0];
|
|
3418
|
+
if (key === null || key === void 0) {
|
|
3419
|
+
return void 0;
|
|
3420
|
+
}
|
|
3421
|
+
targetKey = key;
|
|
3422
|
+
} else {
|
|
3423
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
3424
|
+
}
|
|
3425
|
+
if (targetKey !== void 0) {
|
|
3426
|
+
return this._boundByKey(targetKey, isLower, iterationType);
|
|
3427
|
+
}
|
|
3428
|
+
return void 0;
|
|
3429
|
+
}
|
|
3430
|
+
/**
|
|
3431
|
+
* (Protected) Binary search for bound by key with pruning optimization.
|
|
3432
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
3433
|
+
* For lowerBound: finds first node where key >= target.
|
|
3434
|
+
* For upperBound: finds first node where key > target.
|
|
3435
|
+
* @param key - The target key to search for.
|
|
3436
|
+
* @param isLower - True for lowerBound (>=), false for upperBound (>).
|
|
3437
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3438
|
+
* @returns The first node matching the bound condition, or undefined if none exists.
|
|
3439
|
+
*/
|
|
3440
|
+
_boundByKey(key, isLower, iterationType) {
|
|
3441
|
+
if (iterationType === "RECURSIVE") {
|
|
3442
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
3443
|
+
if (!this.isRealNode(cur)) return void 0;
|
|
3444
|
+
const cmp = this.comparator(cur.key, key);
|
|
3445
|
+
const condition = isLower ? cmp >= 0 : cmp > 0;
|
|
3446
|
+
if (condition) {
|
|
3447
|
+
const leftResult = dfs(cur.left);
|
|
3448
|
+
return leftResult ?? cur;
|
|
3449
|
+
} else {
|
|
3450
|
+
return dfs(cur.right);
|
|
3451
|
+
}
|
|
3452
|
+
}, "dfs");
|
|
3453
|
+
return dfs(this.root);
|
|
3454
|
+
} else {
|
|
3455
|
+
let current = this.root;
|
|
3456
|
+
let result = void 0;
|
|
3457
|
+
while (this.isRealNode(current)) {
|
|
3458
|
+
const cmp = this.comparator(current.key, key);
|
|
3459
|
+
const condition = isLower ? cmp >= 0 : cmp > 0;
|
|
3460
|
+
if (condition) {
|
|
3461
|
+
result = current;
|
|
3462
|
+
current = current.left ?? void 0;
|
|
3463
|
+
} else {
|
|
3464
|
+
current = current.right ?? void 0;
|
|
3465
|
+
}
|
|
3466
|
+
}
|
|
3467
|
+
return result;
|
|
3468
|
+
}
|
|
3469
|
+
}
|
|
3470
|
+
/**
|
|
3471
|
+
* (Protected) In-order traversal search by predicate.
|
|
3472
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
3473
|
+
* Returns the first node that satisfies the predicate function.
|
|
3474
|
+
* Note: Predicate-based search cannot leverage BST's binary search optimization.
|
|
3475
|
+
* Time Complexity: O(n) since it may visit every node.
|
|
3476
|
+
* @param predicate - The predicate function to test nodes.
|
|
3477
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3478
|
+
* @returns The first node satisfying predicate, or undefined if none found.
|
|
3479
|
+
*/
|
|
3480
|
+
_boundByPredicate(predicate, iterationType) {
|
|
3481
|
+
if (iterationType === "RECURSIVE") {
|
|
3482
|
+
let result = void 0;
|
|
3483
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
3484
|
+
if (result || !this.isRealNode(cur)) return;
|
|
3485
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
3486
|
+
if (!result && predicate(cur)) {
|
|
3487
|
+
result = cur;
|
|
3488
|
+
}
|
|
3489
|
+
if (!result && this.isRealNode(cur.right)) dfs(cur.right);
|
|
3490
|
+
}, "dfs");
|
|
3491
|
+
dfs(this.root);
|
|
3492
|
+
return result;
|
|
3493
|
+
} else {
|
|
3494
|
+
const stack = [];
|
|
3495
|
+
let current = this.root;
|
|
3496
|
+
while (stack.length > 0 || this.isRealNode(current)) {
|
|
3497
|
+
if (this.isRealNode(current)) {
|
|
3498
|
+
stack.push(current);
|
|
3499
|
+
current = current.left;
|
|
3500
|
+
} else {
|
|
3501
|
+
const node = stack.pop();
|
|
3502
|
+
if (!this.isRealNode(node)) break;
|
|
3503
|
+
if (predicate(node)) {
|
|
3504
|
+
return node;
|
|
3505
|
+
}
|
|
3506
|
+
current = node.right;
|
|
3507
|
+
}
|
|
3508
|
+
}
|
|
3509
|
+
return void 0;
|
|
3510
|
+
}
|
|
3511
|
+
}
|
|
3349
3512
|
/**
|
|
3350
3513
|
* (Protected) Creates a new, empty instance of the same BST constructor.
|
|
3351
3514
|
* @remarks Time O(1)
|