avl-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/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/avl-tree-typed.js +164 -0
- 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/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
|
@@ -1459,6 +1459,17 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1459
1459
|
}
|
|
1460
1460
|
return false;
|
|
1461
1461
|
}
|
|
1462
|
+
/**
|
|
1463
|
+
* Adds or updates a new node to the tree.
|
|
1464
|
+
* @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).
|
|
1465
|
+
*
|
|
1466
|
+
* @param keyNodeOrEntry - The key, node, or entry to add or update.
|
|
1467
|
+
* @param [value] - The value, if providing just a key.
|
|
1468
|
+
* @returns True if the addition was successful, false otherwise.
|
|
1469
|
+
*/
|
|
1470
|
+
set(keyNodeOrEntry, value) {
|
|
1471
|
+
return this.add(keyNodeOrEntry, value);
|
|
1472
|
+
}
|
|
1462
1473
|
/**
|
|
1463
1474
|
* Adds multiple items to the tree.
|
|
1464
1475
|
* @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).
|
|
@@ -1486,6 +1497,17 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1486
1497
|
}
|
|
1487
1498
|
return inserted;
|
|
1488
1499
|
}
|
|
1500
|
+
/**
|
|
1501
|
+
* Adds or updates multiple items to the tree.
|
|
1502
|
+
* @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).
|
|
1503
|
+
*
|
|
1504
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to add or update.
|
|
1505
|
+
* @param [values] - An optional parallel iterable of values.
|
|
1506
|
+
* @returns An array of booleans indicating the success of each individual `add` operation.
|
|
1507
|
+
*/
|
|
1508
|
+
setMany(keysNodesEntriesOrRaws, values) {
|
|
1509
|
+
return this.addMany(keysNodesEntriesOrRaws, values);
|
|
1510
|
+
}
|
|
1489
1511
|
/**
|
|
1490
1512
|
* Merges another tree into this one by adding all its nodes.
|
|
1491
1513
|
* @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`).
|
|
@@ -3179,6 +3201,32 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3179
3201
|
else _iterate();
|
|
3180
3202
|
return inserted;
|
|
3181
3203
|
}
|
|
3204
|
+
/**
|
|
3205
|
+
* Returns the first node with a key greater than or equal to the given key.
|
|
3206
|
+
* This is equivalent to C++ std::lower_bound on a BST.
|
|
3207
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
3208
|
+
* Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
3209
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
3210
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3211
|
+
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
3212
|
+
* @returns The first node with key >= given key, or undefined if no such node exists.
|
|
3213
|
+
*/
|
|
3214
|
+
lowerBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
3215
|
+
return this._bound(keyNodeEntryOrPredicate, true, iterationType);
|
|
3216
|
+
}
|
|
3217
|
+
/**
|
|
3218
|
+
* Returns the first node with a key strictly greater than the given key.
|
|
3219
|
+
* This is equivalent to C++ std::upper_bound on a BST.
|
|
3220
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
3221
|
+
* Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
3222
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
3223
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3224
|
+
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
3225
|
+
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
3226
|
+
*/
|
|
3227
|
+
upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
3228
|
+
return this._bound(keyNodeEntryOrPredicate, false, iterationType);
|
|
3229
|
+
}
|
|
3182
3230
|
/**
|
|
3183
3231
|
* Traverses the tree and returns nodes that are lesser or greater than a target node.
|
|
3184
3232
|
* @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
|
|
@@ -3339,6 +3387,122 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3339
3387
|
}
|
|
3340
3388
|
return false;
|
|
3341
3389
|
}
|
|
3390
|
+
/**
|
|
3391
|
+
* (Protected) Core bound search implementation supporting all parameter types.
|
|
3392
|
+
* Unified logic for both lowerBound and upperBound.
|
|
3393
|
+
* Resolves various input types (Key, Node, Entry, Predicate) using parent class utilities.
|
|
3394
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3395
|
+
* @param isLower - True for lowerBound (>=), false for upperBound (>).
|
|
3396
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3397
|
+
* @returns The first matching node, or undefined if no such node exists.
|
|
3398
|
+
*/
|
|
3399
|
+
_bound(keyNodeEntryOrPredicate, isLower, iterationType) {
|
|
3400
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
3401
|
+
return void 0;
|
|
3402
|
+
}
|
|
3403
|
+
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
3404
|
+
return this._boundByPredicate(keyNodeEntryOrPredicate, iterationType);
|
|
3405
|
+
}
|
|
3406
|
+
let targetKey;
|
|
3407
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
3408
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
3409
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
3410
|
+
const key = keyNodeEntryOrPredicate[0];
|
|
3411
|
+
if (key === null || key === void 0) {
|
|
3412
|
+
return void 0;
|
|
3413
|
+
}
|
|
3414
|
+
targetKey = key;
|
|
3415
|
+
} else {
|
|
3416
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
3417
|
+
}
|
|
3418
|
+
if (targetKey !== void 0) {
|
|
3419
|
+
return this._boundByKey(targetKey, isLower, iterationType);
|
|
3420
|
+
}
|
|
3421
|
+
return void 0;
|
|
3422
|
+
}
|
|
3423
|
+
/**
|
|
3424
|
+
* (Protected) Binary search for bound by key with pruning optimization.
|
|
3425
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
3426
|
+
* For lowerBound: finds first node where key >= target.
|
|
3427
|
+
* For upperBound: finds first node where key > target.
|
|
3428
|
+
* @param key - The target key to search for.
|
|
3429
|
+
* @param isLower - True for lowerBound (>=), false for upperBound (>).
|
|
3430
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3431
|
+
* @returns The first node matching the bound condition, or undefined if none exists.
|
|
3432
|
+
*/
|
|
3433
|
+
_boundByKey(key, isLower, iterationType) {
|
|
3434
|
+
var _a, _b;
|
|
3435
|
+
if (iterationType === "RECURSIVE") {
|
|
3436
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
3437
|
+
if (!this.isRealNode(cur)) return void 0;
|
|
3438
|
+
const cmp = this.comparator(cur.key, key);
|
|
3439
|
+
const condition = isLower ? cmp >= 0 : cmp > 0;
|
|
3440
|
+
if (condition) {
|
|
3441
|
+
const leftResult = dfs(cur.left);
|
|
3442
|
+
return leftResult != null ? leftResult : cur;
|
|
3443
|
+
} else {
|
|
3444
|
+
return dfs(cur.right);
|
|
3445
|
+
}
|
|
3446
|
+
}, "dfs");
|
|
3447
|
+
return dfs(this.root);
|
|
3448
|
+
} else {
|
|
3449
|
+
let current = this.root;
|
|
3450
|
+
let result = void 0;
|
|
3451
|
+
while (this.isRealNode(current)) {
|
|
3452
|
+
const cmp = this.comparator(current.key, key);
|
|
3453
|
+
const condition = isLower ? cmp >= 0 : cmp > 0;
|
|
3454
|
+
if (condition) {
|
|
3455
|
+
result = current;
|
|
3456
|
+
current = (_a = current.left) != null ? _a : void 0;
|
|
3457
|
+
} else {
|
|
3458
|
+
current = (_b = current.right) != null ? _b : void 0;
|
|
3459
|
+
}
|
|
3460
|
+
}
|
|
3461
|
+
return result;
|
|
3462
|
+
}
|
|
3463
|
+
}
|
|
3464
|
+
/**
|
|
3465
|
+
* (Protected) In-order traversal search by predicate.
|
|
3466
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
3467
|
+
* Returns the first node that satisfies the predicate function.
|
|
3468
|
+
* Note: Predicate-based search cannot leverage BST's binary search optimization.
|
|
3469
|
+
* Time Complexity: O(n) since it may visit every node.
|
|
3470
|
+
* @param predicate - The predicate function to test nodes.
|
|
3471
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3472
|
+
* @returns The first node satisfying predicate, or undefined if none found.
|
|
3473
|
+
*/
|
|
3474
|
+
_boundByPredicate(predicate, iterationType) {
|
|
3475
|
+
if (iterationType === "RECURSIVE") {
|
|
3476
|
+
let result = void 0;
|
|
3477
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
3478
|
+
if (result || !this.isRealNode(cur)) return;
|
|
3479
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
3480
|
+
if (!result && predicate(cur)) {
|
|
3481
|
+
result = cur;
|
|
3482
|
+
}
|
|
3483
|
+
if (!result && this.isRealNode(cur.right)) dfs(cur.right);
|
|
3484
|
+
}, "dfs");
|
|
3485
|
+
dfs(this.root);
|
|
3486
|
+
return result;
|
|
3487
|
+
} else {
|
|
3488
|
+
const stack = [];
|
|
3489
|
+
let current = this.root;
|
|
3490
|
+
while (stack.length > 0 || this.isRealNode(current)) {
|
|
3491
|
+
if (this.isRealNode(current)) {
|
|
3492
|
+
stack.push(current);
|
|
3493
|
+
current = current.left;
|
|
3494
|
+
} else {
|
|
3495
|
+
const node = stack.pop();
|
|
3496
|
+
if (!this.isRealNode(node)) break;
|
|
3497
|
+
if (predicate(node)) {
|
|
3498
|
+
return node;
|
|
3499
|
+
}
|
|
3500
|
+
current = node.right;
|
|
3501
|
+
}
|
|
3502
|
+
}
|
|
3503
|
+
return void 0;
|
|
3504
|
+
}
|
|
3505
|
+
}
|
|
3342
3506
|
/**
|
|
3343
3507
|
* (Protected) Creates a new, empty instance of the same BST constructor.
|
|
3344
3508
|
* @remarks Time O(1)
|