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