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