avl-tree-typed 2.2.7 → 2.2.8
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 +67 -69
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +67 -69
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +67 -69
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +67 -69
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +10 -10
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +22 -23
- package/dist/types/data-structures/binary-tree/bst.d.ts +11 -11
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
- package/dist/umd/avl-tree-typed.js +67 -69
- 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-counter.ts +6 -6
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +13 -13
- package/src/data-structures/binary-tree/avl-tree.ts +15 -15
- package/src/data-structures/binary-tree/binary-tree.ts +53 -55
- package/src/data-structures/binary-tree/bst.ts +21 -22
- package/src/data-structures/binary-tree/red-black-tree.ts +3 -3
- package/src/data-structures/binary-tree/tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/tree-multi-map.ts +13 -13
package/dist/esm/index.mjs
CHANGED
|
@@ -1187,9 +1187,9 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1187
1187
|
iterationType = "ITERATIVE";
|
|
1188
1188
|
/**
|
|
1189
1189
|
* Creates an instance of BinaryTree.
|
|
1190
|
-
* @remarks Time O(N * M), where N is the number of items in `keysNodesEntriesOrRaws` and M is the tree size at insertion time (due to O(M) `
|
|
1190
|
+
* @remarks Time O(N * M), where N is the number of items in `keysNodesEntriesOrRaws` and M is the tree size at insertion time (due to O(M) `set` operation). Space O(N) for storing the nodes.
|
|
1191
1191
|
*
|
|
1192
|
-
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to
|
|
1192
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
|
|
1193
1193
|
* @param [options] - Configuration options for the tree.
|
|
1194
1194
|
*/
|
|
1195
1195
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
@@ -1202,7 +1202,7 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1202
1202
|
if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
|
|
1203
1203
|
else if (toEntryFn) throw TypeError("toEntryFn must be a function type");
|
|
1204
1204
|
}
|
|
1205
|
-
if (keysNodesEntriesOrRaws) this.
|
|
1205
|
+
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
1206
1206
|
}
|
|
1207
1207
|
_isMapMode = true;
|
|
1208
1208
|
/**
|
|
@@ -1416,10 +1416,20 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1416
1416
|
* @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).
|
|
1417
1417
|
*
|
|
1418
1418
|
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
1419
|
+
* @returns True if the addition was successful, false otherwise.
|
|
1420
|
+
*/
|
|
1421
|
+
add(keyNodeOrEntry) {
|
|
1422
|
+
return this.set(keyNodeOrEntry);
|
|
1423
|
+
}
|
|
1424
|
+
/**
|
|
1425
|
+
* Adds or updates a new node to the tree.
|
|
1426
|
+
* @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation sets 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).
|
|
1427
|
+
*
|
|
1428
|
+
* @param keyNodeOrEntry - The key, node, or entry to set or update.
|
|
1419
1429
|
* @param [value] - The value, if providing just a key.
|
|
1420
1430
|
* @returns True if the addition was successful, false otherwise.
|
|
1421
1431
|
*/
|
|
1422
|
-
|
|
1432
|
+
set(keyNodeOrEntry, value) {
|
|
1423
1433
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
1424
1434
|
if (newNode === void 0) return false;
|
|
1425
1435
|
if (!this._root) {
|
|
@@ -1463,25 +1473,25 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1463
1473
|
return false;
|
|
1464
1474
|
}
|
|
1465
1475
|
/**
|
|
1466
|
-
* Adds
|
|
1467
|
-
* @remarks Time O(
|
|
1476
|
+
* Adds multiple items to the tree.
|
|
1477
|
+
* @remarks Time O(N * M), where N is the number of items to set and M is the size of the tree at insertion (due to O(M) `set` operation). Space O(M) (from `set`) + O(N) (for the `inserted` array).
|
|
1468
1478
|
*
|
|
1469
|
-
* @param
|
|
1470
|
-
* @param [
|
|
1471
|
-
* @returns
|
|
1479
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
1480
|
+
* @param [values] - An optional parallel iterable of values.
|
|
1481
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
1472
1482
|
*/
|
|
1473
|
-
|
|
1474
|
-
return this.
|
|
1483
|
+
addMany(keysNodesEntriesOrRaws) {
|
|
1484
|
+
return this.setMany(keysNodesEntriesOrRaws);
|
|
1475
1485
|
}
|
|
1476
1486
|
/**
|
|
1477
|
-
* Adds multiple items to the tree.
|
|
1478
|
-
* @remarks Time O(N * M), where N is the number of items to
|
|
1487
|
+
* Adds or updates multiple items to the tree.
|
|
1488
|
+
* @remarks Time O(N * M), where N is the number of items to set and M is the size of the tree at insertion (due to O(M) `set` operation). Space O(M) (from `set`) + O(N) (for the `inserted` array).
|
|
1479
1489
|
*
|
|
1480
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
1490
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set or update.
|
|
1481
1491
|
* @param [values] - An optional parallel iterable of values.
|
|
1482
|
-
* @returns An array of booleans indicating the success of each individual `
|
|
1492
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
1483
1493
|
*/
|
|
1484
|
-
|
|
1494
|
+
setMany(keysNodesEntriesOrRaws, values) {
|
|
1485
1495
|
const inserted = [];
|
|
1486
1496
|
let valuesIterator;
|
|
1487
1497
|
if (values) {
|
|
@@ -1496,40 +1506,29 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
1496
1506
|
}
|
|
1497
1507
|
}
|
|
1498
1508
|
if (this.isRaw(keyNodeEntryOrRaw)) keyNodeEntryOrRaw = this._toEntryFn(keyNodeEntryOrRaw);
|
|
1499
|
-
inserted.push(this.
|
|
1509
|
+
inserted.push(this.set(keyNodeEntryOrRaw, value));
|
|
1500
1510
|
}
|
|
1501
1511
|
return inserted;
|
|
1502
1512
|
}
|
|
1503
1513
|
/**
|
|
1504
|
-
*
|
|
1505
|
-
* @remarks Time O(N * M), where N is the
|
|
1506
|
-
*
|
|
1507
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to add or update.
|
|
1508
|
-
* @param [values] - An optional parallel iterable of values.
|
|
1509
|
-
* @returns An array of booleans indicating the success of each individual `add` operation.
|
|
1510
|
-
*/
|
|
1511
|
-
setMany(keysNodesEntriesOrRaws, values) {
|
|
1512
|
-
return this.addMany(keysNodesEntriesOrRaws, values);
|
|
1513
|
-
}
|
|
1514
|
-
/**
|
|
1515
|
-
* Merges another tree into this one by adding all its nodes.
|
|
1516
|
-
* @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`).
|
|
1514
|
+
* Merges another tree into this one by seting all its nodes.
|
|
1515
|
+
* @remarks Time O(N * M), same as `setMany`, where N is the size of `anotherTree` and M is the size of this tree. Space O(M) (from `set`).
|
|
1517
1516
|
*
|
|
1518
1517
|
* @param anotherTree - The tree to merge.
|
|
1519
1518
|
*/
|
|
1520
1519
|
merge(anotherTree) {
|
|
1521
|
-
this.
|
|
1520
|
+
this.setMany(anotherTree, []);
|
|
1522
1521
|
}
|
|
1523
1522
|
/**
|
|
1524
1523
|
* Clears the tree and refills it with new items.
|
|
1525
|
-
* @remarks Time O(N) (for `clear`) + O(N * M) (for `
|
|
1524
|
+
* @remarks Time O(N) (for `clear`) + O(N * M) (for `setMany`) = O(N * M). Space O(M) (from `setMany`).
|
|
1526
1525
|
*
|
|
1527
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
1526
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
1528
1527
|
* @param [values] - An optional parallel iterable of values.
|
|
1529
1528
|
*/
|
|
1530
1529
|
refill(keysNodesEntriesOrRaws, values) {
|
|
1531
1530
|
this.clear();
|
|
1532
|
-
this.
|
|
1531
|
+
this.setMany(keysNodesEntriesOrRaws, values);
|
|
1533
1532
|
}
|
|
1534
1533
|
/**
|
|
1535
1534
|
* Deletes a node from the tree.
|
|
@@ -2194,7 +2193,7 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
2194
2193
|
}
|
|
2195
2194
|
/**
|
|
2196
2195
|
* Clones the tree.
|
|
2197
|
-
* @remarks Time O(N * M), where N is the number of nodes and M is the tree size during insertion (due to `bfs` + `
|
|
2196
|
+
* @remarks Time O(N * M), where N is the number of nodes and M is the tree size during insertion (due to `bfs` + `set`, and `set` is O(M)). Space O(N) for the new tree and the BFS queue.
|
|
2198
2197
|
*
|
|
2199
2198
|
* @returns A new, cloned instance of the tree.
|
|
2200
2199
|
*/
|
|
@@ -2205,7 +2204,7 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
2205
2204
|
}
|
|
2206
2205
|
/**
|
|
2207
2206
|
* Creates a new tree containing only the entries that satisfy the predicate.
|
|
2208
|
-
* @remarks Time O(N * M), where N is nodes in this tree, and M is size of the new tree during insertion (O(N) iteration + O(M) `
|
|
2207
|
+
* @remarks Time O(N * M), where N is nodes in this tree, and M is size of the new tree during insertion (O(N) iteration + O(M) `set` for each item). Space O(N) for the new tree.
|
|
2209
2208
|
*
|
|
2210
2209
|
* @param predicate - A function to test each [key, value] pair.
|
|
2211
2210
|
* @param [thisArg] - `this` context for the predicate.
|
|
@@ -2214,7 +2213,7 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
2214
2213
|
filter(predicate, thisArg) {
|
|
2215
2214
|
const out = this._createInstance();
|
|
2216
2215
|
let i = 0;
|
|
2217
|
-
for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.
|
|
2216
|
+
for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.set([k, v]);
|
|
2218
2217
|
return out;
|
|
2219
2218
|
}
|
|
2220
2219
|
/**
|
|
@@ -2232,7 +2231,7 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
2232
2231
|
map(cb, options, thisArg) {
|
|
2233
2232
|
const out = this._createLike([], options);
|
|
2234
2233
|
let i = 0;
|
|
2235
|
-
for (const [k, v] of this) out.
|
|
2234
|
+
for (const [k, v] of this) out.set(cb.call(thisArg, v, k, i++, this));
|
|
2236
2235
|
return out;
|
|
2237
2236
|
}
|
|
2238
2237
|
/**
|
|
@@ -2484,18 +2483,18 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
2484
2483
|
return [this.createNode(keyNodeOrEntry, value), value];
|
|
2485
2484
|
}
|
|
2486
2485
|
/**
|
|
2487
|
-
* (Protected) Helper for cloning. Performs a BFS and
|
|
2488
|
-
* @remarks Time O(N * M) (O(N) BFS + O(M) `
|
|
2486
|
+
* (Protected) Helper for cloning. Performs a BFS and sets all nodes to the new tree.
|
|
2487
|
+
* @remarks Time O(N * M) (O(N) BFS + O(M) `set` for each node).
|
|
2489
2488
|
*
|
|
2490
2489
|
* @param cloned - The new, empty tree instance to populate.
|
|
2491
2490
|
*/
|
|
2492
2491
|
_clone(cloned) {
|
|
2493
2492
|
this.bfs(
|
|
2494
2493
|
(node) => {
|
|
2495
|
-
if (node === null) cloned.
|
|
2494
|
+
if (node === null) cloned.set(null);
|
|
2496
2495
|
else {
|
|
2497
|
-
if (this._isMapMode) cloned.
|
|
2498
|
-
else cloned.
|
|
2496
|
+
if (this._isMapMode) cloned.set([node.key, this._store.get(node.key)]);
|
|
2497
|
+
else cloned.set([node.key, node.value]);
|
|
2499
2498
|
}
|
|
2500
2499
|
},
|
|
2501
2500
|
this._root,
|
|
@@ -2828,7 +2827,7 @@ var BST = class extends BinaryTree {
|
|
|
2828
2827
|
* Creates an instance of BST.
|
|
2829
2828
|
* @remarks Time O(N log N) or O(N^2) depending on `isBalanceAdd` in `addMany` and input order. Space O(N).
|
|
2830
2829
|
*
|
|
2831
|
-
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to
|
|
2830
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
|
|
2832
2831
|
* @param [options] - Configuration options for the BST, including comparator.
|
|
2833
2832
|
*/
|
|
2834
2833
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
@@ -2842,7 +2841,7 @@ var BST = class extends BinaryTree {
|
|
|
2842
2841
|
} else {
|
|
2843
2842
|
this._comparator = this._createDefaultComparator();
|
|
2844
2843
|
}
|
|
2845
|
-
if (keysNodesEntriesOrRaws) this.
|
|
2844
|
+
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
2846
2845
|
}
|
|
2847
2846
|
_root = void 0;
|
|
2848
2847
|
/**
|
|
@@ -3058,11 +3057,11 @@ var BST = class extends BinaryTree {
|
|
|
3058
3057
|
* Adds a new node to the BST based on key comparison.
|
|
3059
3058
|
* @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
|
|
3060
3059
|
*
|
|
3061
|
-
* @param keyNodeOrEntry - The key, node, or entry to
|
|
3060
|
+
* @param keyNodeOrEntry - The key, node, or entry to set.
|
|
3062
3061
|
* @param [value] - The value, if providing just a key.
|
|
3063
3062
|
* @returns True if the addition was successful, false otherwise.
|
|
3064
3063
|
*/
|
|
3065
|
-
|
|
3064
|
+
set(keyNodeOrEntry, value) {
|
|
3066
3065
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
3067
3066
|
if (newNode === void 0) return false;
|
|
3068
3067
|
if (this._root === void 0) {
|
|
@@ -3099,24 +3098,24 @@ var BST = class extends BinaryTree {
|
|
|
3099
3098
|
}
|
|
3100
3099
|
/**
|
|
3101
3100
|
* Adds multiple items to the tree.
|
|
3102
|
-
* @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced
|
|
3101
|
+
* @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced set).
|
|
3103
3102
|
* If false, adds items one by one. Time O(N * H), which is O(N^2) worst-case.
|
|
3104
3103
|
* Space O(N) for sorting and recursion/iteration stack.
|
|
3105
3104
|
*
|
|
3106
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
3105
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
3107
3106
|
* @param [values] - An optional parallel iterable of values.
|
|
3108
3107
|
* @param [isBalanceAdd=true] - If true, builds a balanced tree from the items.
|
|
3109
|
-
* @param [iterationType=this.iterationType] - The traversal method for balanced
|
|
3110
|
-
* @returns An array of booleans indicating the success of each individual `
|
|
3108
|
+
* @param [iterationType=this.iterationType] - The traversal method for balanced set (recursive or iterative).
|
|
3109
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
3111
3110
|
*/
|
|
3112
|
-
|
|
3111
|
+
setMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
3113
3112
|
const inserted = [];
|
|
3114
3113
|
const valuesIterator = values?.[Symbol.iterator]();
|
|
3115
3114
|
if (!isBalanceAdd) {
|
|
3116
3115
|
for (let kve of keysNodesEntriesOrRaws) {
|
|
3117
3116
|
const val = valuesIterator?.next().value;
|
|
3118
3117
|
if (this.isRaw(kve)) kve = this._toEntryFn(kve);
|
|
3119
|
-
inserted.push(this.
|
|
3118
|
+
inserted.push(this.set(kve, val));
|
|
3120
3119
|
}
|
|
3121
3120
|
return inserted;
|
|
3122
3121
|
}
|
|
@@ -3144,9 +3143,9 @@ var BST = class extends BinaryTree {
|
|
|
3144
3143
|
const { key, value, orgIndex } = arr[mid];
|
|
3145
3144
|
if (this.isRaw(key)) {
|
|
3146
3145
|
const entry = this._toEntryFn(key);
|
|
3147
|
-
inserted[orgIndex] = this.
|
|
3146
|
+
inserted[orgIndex] = this.set(entry);
|
|
3148
3147
|
} else {
|
|
3149
|
-
inserted[orgIndex] = this.
|
|
3148
|
+
inserted[orgIndex] = this.set(key, value);
|
|
3150
3149
|
}
|
|
3151
3150
|
_dfs(arr.slice(0, mid));
|
|
3152
3151
|
_dfs(arr.slice(mid + 1));
|
|
@@ -3163,9 +3162,9 @@ var BST = class extends BinaryTree {
|
|
|
3163
3162
|
const { key, value, orgIndex } = sorted[m];
|
|
3164
3163
|
if (this.isRaw(key)) {
|
|
3165
3164
|
const entry = this._toEntryFn(key);
|
|
3166
|
-
inserted[orgIndex] = this.
|
|
3165
|
+
inserted[orgIndex] = this.set(entry);
|
|
3167
3166
|
} else {
|
|
3168
|
-
inserted[orgIndex] = this.
|
|
3167
|
+
inserted[orgIndex] = this.set(key, value);
|
|
3169
3168
|
}
|
|
3170
3169
|
stack.push([m + 1, r]);
|
|
3171
3170
|
stack.push([l, m - 1]);
|
|
@@ -3440,7 +3439,7 @@ var BST = class extends BinaryTree {
|
|
|
3440
3439
|
const out = this._createLike([], options);
|
|
3441
3440
|
let index = 0;
|
|
3442
3441
|
for (const [key, value] of this) {
|
|
3443
|
-
out.
|
|
3442
|
+
out.set(callback.call(thisArg, value, key, index++, this));
|
|
3444
3443
|
}
|
|
3445
3444
|
return out;
|
|
3446
3445
|
}
|
|
@@ -3496,7 +3495,6 @@ var BST = class extends BinaryTree {
|
|
|
3496
3495
|
*/
|
|
3497
3496
|
_createDefaultComparator() {
|
|
3498
3497
|
return (a, b) => {
|
|
3499
|
-
debugger;
|
|
3500
3498
|
if (isComparable(a) && isComparable(b)) {
|
|
3501
3499
|
if (a > b) return 1;
|
|
3502
3500
|
if (a < b) return -1;
|
|
@@ -4056,14 +4054,14 @@ var AVLTree = class extends BST {
|
|
|
4056
4054
|
}
|
|
4057
4055
|
/**
|
|
4058
4056
|
* Creates an instance of AVLTree.
|
|
4059
|
-
* @remarks Time O(N log N) (from `
|
|
4057
|
+
* @remarks Time O(N log N) (from `setMany` with balanced set). Space O(N).
|
|
4060
4058
|
*
|
|
4061
|
-
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to
|
|
4059
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
|
|
4062
4060
|
* @param [options] - Configuration options for the AVL tree.
|
|
4063
4061
|
*/
|
|
4064
4062
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
4065
4063
|
super([], options);
|
|
4066
|
-
if (keysNodesEntriesOrRaws) super.
|
|
4064
|
+
if (keysNodesEntriesOrRaws) super.setMany(keysNodesEntriesOrRaws);
|
|
4067
4065
|
}
|
|
4068
4066
|
/**
|
|
4069
4067
|
* (Protected) Creates a new AVL tree node.
|
|
@@ -4087,16 +4085,16 @@ var AVLTree = class extends BST {
|
|
|
4087
4085
|
return keyNodeOrEntry instanceof AVLTreeNode;
|
|
4088
4086
|
}
|
|
4089
4087
|
/**
|
|
4090
|
-
*
|
|
4091
|
-
* @remarks Time O(log N) (O(H) for BST
|
|
4088
|
+
* Sets a new node to the AVL tree and balances the tree path.
|
|
4089
|
+
* @remarks Time O(log N) (O(H) for BST set + O(H) for `_balancePath`). Space O(H) for path/recursion.
|
|
4092
4090
|
*
|
|
4093
|
-
* @param keyNodeOrEntry - The key, node, or entry to
|
|
4091
|
+
* @param keyNodeOrEntry - The key, node, or entry to set.
|
|
4094
4092
|
* @param [value] - The value, if providing just a key.
|
|
4095
4093
|
* @returns True if the addition was successful, false otherwise.
|
|
4096
4094
|
*/
|
|
4097
|
-
|
|
4095
|
+
set(keyNodeOrEntry, value) {
|
|
4098
4096
|
if (keyNodeOrEntry === null) return false;
|
|
4099
|
-
const inserted = super.
|
|
4097
|
+
const inserted = super.set(keyNodeOrEntry, value);
|
|
4100
4098
|
if (inserted) this._balancePath(keyNodeOrEntry);
|
|
4101
4099
|
return inserted;
|
|
4102
4100
|
}
|
|
@@ -4148,7 +4146,7 @@ var AVLTree = class extends BST {
|
|
|
4148
4146
|
}
|
|
4149
4147
|
/**
|
|
4150
4148
|
* Creates a new AVLTree by mapping each [key, value] pair.
|
|
4151
|
-
* @remarks Time O(N log N) (O(N) iteration + O(log M) `
|
|
4149
|
+
* @remarks Time O(N log N) (O(N) iteration + O(log M) `set` for each item into the new tree). Space O(N) for the new tree.
|
|
4152
4150
|
*
|
|
4153
4151
|
* @template MK - New key type.
|
|
4154
4152
|
* @template MV - New value type.
|
|
@@ -4162,7 +4160,7 @@ var AVLTree = class extends BST {
|
|
|
4162
4160
|
const out = this._createLike([], options);
|
|
4163
4161
|
let index = 0;
|
|
4164
4162
|
for (const [key, value] of this) {
|
|
4165
|
-
out.
|
|
4163
|
+
out.set(callback.call(thisArg, value, key, index++, this));
|
|
4166
4164
|
}
|
|
4167
4165
|
return out;
|
|
4168
4166
|
}
|