data-structure-typed 1.49.5 → 1.49.6
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/CHANGELOG.md +1 -1
- package/README.md +14 -23
- package/benchmark/report.html +14 -23
- package/benchmark/report.json +163 -256
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +153 -130
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +192 -149
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/cjs/data-structures/binary-tree/bst.js +113 -89
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +46 -39
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +58 -51
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/cjs/types/common.d.ts +3 -3
- package/dist/cjs/types/common.js +2 -2
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +153 -130
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +192 -149
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/mjs/data-structures/binary-tree/bst.js +113 -89
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +46 -39
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +58 -51
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/mjs/types/common.d.ts +3 -3
- package/dist/mjs/types/common.js +2 -2
- package/dist/umd/data-structure-typed.js +497 -419
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +58 -53
- package/src/data-structures/binary-tree/binary-tree.ts +253 -205
- package/src/data-structures/binary-tree/bst.ts +125 -104
- package/src/data-structures/binary-tree/rb-tree.ts +66 -64
- package/src/data-structures/binary-tree/tree-multimap.ts +62 -56
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +3 -3
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +4 -12
- package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +37 -0
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +6 -16
- package/test/performance/data-structures/binary-tree/bst.test.ts +5 -13
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +5 -15
- package/test/performance/data-structures/comparison/comparison.test.ts +13 -36
- package/test/performance/data-structures/graph/directed-graph.test.ts +3 -14
- package/test/performance/data-structures/hash/hash-map.test.ts +11 -34
- package/test/performance/data-structures/heap/heap.test.ts +5 -18
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +0 -1
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +0 -2
- package/test/performance/data-structures/priority-queue/max-priority-queue.test.ts +2 -4
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +4 -14
- package/test/performance/data-structures/queue/queue.test.ts +8 -25
- package/test/performance/data-structures/stack/stack.test.ts +6 -18
- package/test/performance/data-structures/trie/trie.test.ts +2 -6
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +6 -5
- package/test/unit/data-structures/binary-tree/bst.test.ts +17 -1
- package/test/performance/data-structures/binary-tree/overall.test.ts +0 -0
- package/test/performance/data-structures/matrix/matrix2d.test.ts +0 -0
- package/test/performance/data-structures/matrix/vector2d.test.ts +0 -0
|
@@ -7241,8 +7241,8 @@ var dataStructureTyped = (() => {
|
|
|
7241
7241
|
|
|
7242
7242
|
// src/types/common.ts
|
|
7243
7243
|
var BSTVariant = /* @__PURE__ */ ((BSTVariant2) => {
|
|
7244
|
-
BSTVariant2["
|
|
7245
|
-
BSTVariant2["
|
|
7244
|
+
BSTVariant2["STANDARD"] = "STANDARD";
|
|
7245
|
+
BSTVariant2["INVERSE"] = "INVERSE";
|
|
7246
7246
|
return BSTVariant2;
|
|
7247
7247
|
})(BSTVariant || {});
|
|
7248
7248
|
var CP = /* @__PURE__ */ ((CP2) => {
|
|
@@ -7315,15 +7315,15 @@ var dataStructureTyped = (() => {
|
|
|
7315
7315
|
};
|
|
7316
7316
|
var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
7317
7317
|
/**
|
|
7318
|
-
* The constructor function initializes a binary tree object with optional
|
|
7319
|
-
* @param [
|
|
7320
|
-
*
|
|
7318
|
+
* The constructor function initializes a binary tree object with optional nodes and options.
|
|
7319
|
+
* @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the
|
|
7320
|
+
* nodes to be added to the binary tree.
|
|
7321
7321
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
7322
7322
|
* configuration options for the binary tree. In this case, it is of type
|
|
7323
7323
|
* `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
|
|
7324
7324
|
* required.
|
|
7325
7325
|
*/
|
|
7326
|
-
constructor(
|
|
7326
|
+
constructor(nodes, options) {
|
|
7327
7327
|
super();
|
|
7328
7328
|
__publicField(this, "iterationType", "ITERATIVE" /* ITERATIVE */);
|
|
7329
7329
|
__publicField(this, "_extractor", (key) => Number(key));
|
|
@@ -7340,8 +7340,8 @@ var dataStructureTyped = (() => {
|
|
|
7340
7340
|
}
|
|
7341
7341
|
}
|
|
7342
7342
|
this._size = 0;
|
|
7343
|
-
if (
|
|
7344
|
-
this.addMany(
|
|
7343
|
+
if (nodes)
|
|
7344
|
+
this.addMany(nodes);
|
|
7345
7345
|
}
|
|
7346
7346
|
get extractor() {
|
|
7347
7347
|
return this._extractor;
|
|
@@ -7372,29 +7372,21 @@ var dataStructureTyped = (() => {
|
|
|
7372
7372
|
return new _BinaryTree([], __spreadValues({ iterationType: this.iterationType }, options));
|
|
7373
7373
|
}
|
|
7374
7374
|
/**
|
|
7375
|
-
* The function
|
|
7376
|
-
* @param
|
|
7377
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the class N.
|
|
7378
|
-
*/
|
|
7379
|
-
isNode(exemplar) {
|
|
7380
|
-
return exemplar instanceof BinaryTreeNode;
|
|
7381
|
-
}
|
|
7382
|
-
/**
|
|
7383
|
-
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
7384
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
|
|
7375
|
+
* The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
|
|
7376
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
7385
7377
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
7386
|
-
* `exemplarToNode` function. It represents the value associated with the
|
|
7378
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If no value
|
|
7387
7379
|
* is provided, it will be `undefined`.
|
|
7388
7380
|
* @returns a value of type N (node), or null, or undefined.
|
|
7389
7381
|
*/
|
|
7390
|
-
exemplarToNode(
|
|
7391
|
-
if (
|
|
7382
|
+
exemplarToNode(keyOrNodeOrEntry, value) {
|
|
7383
|
+
if (keyOrNodeOrEntry === void 0)
|
|
7392
7384
|
return;
|
|
7393
7385
|
let node;
|
|
7394
|
-
if (
|
|
7386
|
+
if (keyOrNodeOrEntry === null) {
|
|
7395
7387
|
node = null;
|
|
7396
|
-
} else if (this.isEntry(
|
|
7397
|
-
const [key, value2] =
|
|
7388
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
7389
|
+
const [key, value2] = keyOrNodeOrEntry;
|
|
7398
7390
|
if (key === void 0) {
|
|
7399
7391
|
return;
|
|
7400
7392
|
} else if (key === null) {
|
|
@@ -7402,23 +7394,104 @@ var dataStructureTyped = (() => {
|
|
|
7402
7394
|
} else {
|
|
7403
7395
|
node = this.createNode(key, value2);
|
|
7404
7396
|
}
|
|
7405
|
-
} else if (this.isNode(
|
|
7406
|
-
node =
|
|
7407
|
-
} else if (this.isNotNodeInstance(
|
|
7408
|
-
node = this.createNode(
|
|
7397
|
+
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
7398
|
+
node = keyOrNodeOrEntry;
|
|
7399
|
+
} else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
7400
|
+
node = this.createNode(keyOrNodeOrEntry, value);
|
|
7409
7401
|
} else {
|
|
7410
7402
|
return;
|
|
7411
7403
|
}
|
|
7412
7404
|
return node;
|
|
7413
7405
|
}
|
|
7406
|
+
/**
|
|
7407
|
+
* Time Complexity: O(n)
|
|
7408
|
+
* Space Complexity: O(log n)
|
|
7409
|
+
*/
|
|
7410
|
+
/**
|
|
7411
|
+
* Time Complexity: O(n)
|
|
7412
|
+
* Space Complexity: O(log n)
|
|
7413
|
+
*
|
|
7414
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a valid node
|
|
7415
|
+
* key, otherwise it returns the key itself.
|
|
7416
|
+
* @param {K | N | null | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`,
|
|
7417
|
+
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
|
|
7418
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
7419
|
+
* type of iteration to be used when searching for a node by key. It has a default value of
|
|
7420
|
+
* `IterationType.ITERATIVE`.
|
|
7421
|
+
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
|
7422
|
+
* itself if it is not a valid node key.
|
|
7423
|
+
*/
|
|
7424
|
+
ensureNode(keyOrNodeOrEntry, iterationType = "ITERATIVE" /* ITERATIVE */) {
|
|
7425
|
+
let res;
|
|
7426
|
+
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
7427
|
+
res = keyOrNodeOrEntry;
|
|
7428
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
7429
|
+
if (keyOrNodeOrEntry[0] === null)
|
|
7430
|
+
res = null;
|
|
7431
|
+
else if (keyOrNodeOrEntry[0] !== void 0)
|
|
7432
|
+
res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
7433
|
+
} else {
|
|
7434
|
+
if (keyOrNodeOrEntry === null)
|
|
7435
|
+
res = null;
|
|
7436
|
+
else if (keyOrNodeOrEntry !== void 0)
|
|
7437
|
+
res = this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
7438
|
+
}
|
|
7439
|
+
return res;
|
|
7440
|
+
}
|
|
7441
|
+
/**
|
|
7442
|
+
* The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
|
|
7443
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,N>`.
|
|
7444
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the class N.
|
|
7445
|
+
*/
|
|
7446
|
+
isNode(keyOrNodeOrEntry) {
|
|
7447
|
+
return keyOrNodeOrEntry instanceof BinaryTreeNode;
|
|
7448
|
+
}
|
|
7414
7449
|
/**
|
|
7415
7450
|
* The function checks if a given value is an entry in a binary tree node.
|
|
7416
|
-
* @param
|
|
7451
|
+
* @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,N> - A generic type representing a node in a binary tree. It has
|
|
7417
7452
|
* two type parameters V and N, representing the value and node type respectively.
|
|
7418
7453
|
* @returns a boolean value.
|
|
7419
7454
|
*/
|
|
7420
|
-
isEntry(
|
|
7421
|
-
return Array.isArray(
|
|
7455
|
+
isEntry(keyOrNodeOrEntry) {
|
|
7456
|
+
return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
|
|
7457
|
+
}
|
|
7458
|
+
/**
|
|
7459
|
+
* Time complexity: O(n)
|
|
7460
|
+
* Space complexity: O(log n)
|
|
7461
|
+
*/
|
|
7462
|
+
/**
|
|
7463
|
+
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
7464
|
+
* BinaryTreeNode and its key is not NaN.
|
|
7465
|
+
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
7466
|
+
* @returns a boolean value.
|
|
7467
|
+
*/
|
|
7468
|
+
isRealNode(node) {
|
|
7469
|
+
return node instanceof BinaryTreeNode && String(node.key) !== "NaN";
|
|
7470
|
+
}
|
|
7471
|
+
/**
|
|
7472
|
+
* The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
|
|
7473
|
+
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
7474
|
+
* @returns a boolean value.
|
|
7475
|
+
*/
|
|
7476
|
+
isNIL(node) {
|
|
7477
|
+
return node instanceof BinaryTreeNode && String(node.key) === "NaN";
|
|
7478
|
+
}
|
|
7479
|
+
/**
|
|
7480
|
+
* The function checks if a given node is a real node or null.
|
|
7481
|
+
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
7482
|
+
* @returns a boolean value.
|
|
7483
|
+
*/
|
|
7484
|
+
isNodeOrNull(node) {
|
|
7485
|
+
return this.isRealNode(node) || node === null;
|
|
7486
|
+
}
|
|
7487
|
+
/**
|
|
7488
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
7489
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
7490
|
+
* data type.
|
|
7491
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
7492
|
+
*/
|
|
7493
|
+
isNotNodeInstance(potentialKey) {
|
|
7494
|
+
return !(potentialKey instanceof BinaryTreeNode);
|
|
7422
7495
|
}
|
|
7423
7496
|
/**
|
|
7424
7497
|
* Time Complexity O(log n) - O(n)
|
|
@@ -7437,11 +7510,11 @@ var dataStructureTyped = (() => {
|
|
|
7437
7510
|
add(keyOrNodeOrEntry, value) {
|
|
7438
7511
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
7439
7512
|
if (newNode === void 0)
|
|
7440
|
-
return;
|
|
7513
|
+
return false;
|
|
7441
7514
|
if (!this.root) {
|
|
7442
7515
|
this._root = newNode;
|
|
7443
7516
|
this._size = 1;
|
|
7444
|
-
return
|
|
7517
|
+
return true;
|
|
7445
7518
|
}
|
|
7446
7519
|
const queue = new Queue([this.root]);
|
|
7447
7520
|
let potentialParent;
|
|
@@ -7451,7 +7524,7 @@ var dataStructureTyped = (() => {
|
|
|
7451
7524
|
continue;
|
|
7452
7525
|
if (newNode !== null && cur.key === newNode.key) {
|
|
7453
7526
|
this._replaceNode(cur, newNode);
|
|
7454
|
-
return
|
|
7527
|
+
return true;
|
|
7455
7528
|
}
|
|
7456
7529
|
if (potentialParent === void 0 && (cur.left === void 0 || cur.right === void 0)) {
|
|
7457
7530
|
potentialParent = cur;
|
|
@@ -7470,9 +7543,9 @@ var dataStructureTyped = (() => {
|
|
|
7470
7543
|
potentialParent.right = newNode;
|
|
7471
7544
|
}
|
|
7472
7545
|
this._size++;
|
|
7473
|
-
return
|
|
7546
|
+
return true;
|
|
7474
7547
|
}
|
|
7475
|
-
return
|
|
7548
|
+
return false;
|
|
7476
7549
|
}
|
|
7477
7550
|
/**
|
|
7478
7551
|
* Time Complexity: O(k log n) - O(k * n)
|
|
@@ -7483,19 +7556,19 @@ var dataStructureTyped = (() => {
|
|
|
7483
7556
|
* Time Complexity: O(k log n) - O(k * n)
|
|
7484
7557
|
* Space Complexity: O(1)
|
|
7485
7558
|
*
|
|
7486
|
-
* The `addMany` function takes in a collection of
|
|
7559
|
+
* The `addMany` function takes in a collection of keysOrNodesOrEntries and an optional collection of values, and
|
|
7487
7560
|
* adds each node with its corresponding value to the data structure.
|
|
7488
|
-
* @param
|
|
7561
|
+
* @param keysOrNodesOrEntries - An iterable collection of KeyOrNodeOrEntry objects.
|
|
7489
7562
|
* @param [values] - An optional iterable of values that will be assigned to each node being added.
|
|
7490
7563
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
7491
7564
|
*/
|
|
7492
|
-
addMany(
|
|
7565
|
+
addMany(keysOrNodesOrEntries, values) {
|
|
7493
7566
|
const inserted = [];
|
|
7494
7567
|
let valuesIterator;
|
|
7495
7568
|
if (values) {
|
|
7496
7569
|
valuesIterator = values[Symbol.iterator]();
|
|
7497
7570
|
}
|
|
7498
|
-
for (const
|
|
7571
|
+
for (const keyOrNodeOrEntry of keysOrNodesOrEntries) {
|
|
7499
7572
|
let value = void 0;
|
|
7500
7573
|
if (valuesIterator) {
|
|
7501
7574
|
const valueResult = valuesIterator.next();
|
|
@@ -7503,17 +7576,30 @@ var dataStructureTyped = (() => {
|
|
|
7503
7576
|
value = valueResult.value;
|
|
7504
7577
|
}
|
|
7505
7578
|
}
|
|
7506
|
-
inserted.push(this.add(
|
|
7579
|
+
inserted.push(this.add(keyOrNodeOrEntry, value));
|
|
7507
7580
|
}
|
|
7508
7581
|
return inserted;
|
|
7509
7582
|
}
|
|
7510
7583
|
/**
|
|
7511
|
-
* Time Complexity: O(k * n)
|
|
7584
|
+
* Time Complexity: O(k * n)
|
|
7512
7585
|
* Space Complexity: O(1)
|
|
7586
|
+
* "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
7587
|
+
*/
|
|
7588
|
+
/**
|
|
7589
|
+
* Time Complexity: O(k * n)
|
|
7590
|
+
* Space Complexity: O(1)
|
|
7591
|
+
*
|
|
7592
|
+
* The `refill` function clears the current data and adds new key-value pairs to the data structure.
|
|
7593
|
+
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries. These can be of type
|
|
7594
|
+
* KeyOrNodeOrEntry<K, V, N>.
|
|
7595
|
+
* @param [values] - The `values` parameter is an optional iterable that contains the values to be
|
|
7596
|
+
* associated with the keys or nodes or entries in the `keysOrNodesOrEntries` parameter. If provided,
|
|
7597
|
+
* the values will be associated with the corresponding keys or nodes or entries in the
|
|
7598
|
+
* `keysOrNodesOrEntries` iterable
|
|
7513
7599
|
*/
|
|
7514
|
-
refill(
|
|
7600
|
+
refill(keysOrNodesOrEntries, values) {
|
|
7515
7601
|
this.clear();
|
|
7516
|
-
this.addMany(
|
|
7602
|
+
this.addMany(keysOrNodesOrEntries, values);
|
|
7517
7603
|
}
|
|
7518
7604
|
/**
|
|
7519
7605
|
* Time Complexity: O(n)
|
|
@@ -7583,24 +7669,24 @@ var dataStructureTyped = (() => {
|
|
|
7583
7669
|
* Space Complexity: O(1)
|
|
7584
7670
|
*
|
|
7585
7671
|
* The function calculates the depth of a given node in a binary tree.
|
|
7586
|
-
* @param {K | N | null | undefined}
|
|
7672
|
+
* @param {K | N | null | undefined} dist - The `dist` parameter represents the node in
|
|
7587
7673
|
* the binary tree whose depth we want to find. It can be of type `K`, `N`, `null`, or
|
|
7588
7674
|
* `undefined`.
|
|
7589
7675
|
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
7590
7676
|
* from which we want to calculate the depth. It can be either a `K` (binary tree node key) or
|
|
7591
7677
|
* `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
|
|
7592
|
-
* @returns the depth of the `
|
|
7678
|
+
* @returns the depth of the `dist` relative to the `beginRoot`.
|
|
7593
7679
|
*/
|
|
7594
|
-
getDepth(
|
|
7595
|
-
|
|
7680
|
+
getDepth(dist, beginRoot = this.root) {
|
|
7681
|
+
dist = this.ensureNode(dist);
|
|
7596
7682
|
beginRoot = this.ensureNode(beginRoot);
|
|
7597
7683
|
let depth = 0;
|
|
7598
|
-
while (
|
|
7599
|
-
if (
|
|
7684
|
+
while (dist == null ? void 0 : dist.parent) {
|
|
7685
|
+
if (dist === beginRoot) {
|
|
7600
7686
|
return depth;
|
|
7601
7687
|
}
|
|
7602
7688
|
depth++;
|
|
7603
|
-
|
|
7689
|
+
dist = dist.parent;
|
|
7604
7690
|
}
|
|
7605
7691
|
return depth;
|
|
7606
7692
|
}
|
|
@@ -7792,6 +7878,7 @@ var dataStructureTyped = (() => {
|
|
|
7792
7878
|
}
|
|
7793
7879
|
/**
|
|
7794
7880
|
* Time Complexity: O(n)
|
|
7881
|
+
* Space Complexity: O(log n).
|
|
7795
7882
|
*
|
|
7796
7883
|
* The function checks if a Binary Tree Node with a specific identifier exists in the tree.
|
|
7797
7884
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
@@ -7888,24 +7975,6 @@ var dataStructureTyped = (() => {
|
|
|
7888
7975
|
}
|
|
7889
7976
|
}
|
|
7890
7977
|
}
|
|
7891
|
-
/**
|
|
7892
|
-
* Time Complexity: O(n)
|
|
7893
|
-
* Space Complexity: O(log n)
|
|
7894
|
-
*/
|
|
7895
|
-
/**
|
|
7896
|
-
* The function `ensureNode` returns the node corresponding to the given key if it is a valid node
|
|
7897
|
-
* key, otherwise it returns the key itself.
|
|
7898
|
-
* @param {K | N | null | undefined} key - The `key` parameter can be of type `K`, `N`,
|
|
7899
|
-
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
|
|
7900
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
7901
|
-
* type of iteration to be used when searching for a node by key. It has a default value of
|
|
7902
|
-
* `IterationType.ITERATIVE`.
|
|
7903
|
-
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
|
7904
|
-
* itself if it is not a valid node key.
|
|
7905
|
-
*/
|
|
7906
|
-
ensureNode(key, iterationType = "ITERATIVE" /* ITERATIVE */) {
|
|
7907
|
-
return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
7908
|
-
}
|
|
7909
7978
|
/**
|
|
7910
7979
|
* Time Complexity: O(n)
|
|
7911
7980
|
* Space Complexity: O(log n)
|
|
@@ -7935,10 +8004,13 @@ var dataStructureTyped = (() => {
|
|
|
7935
8004
|
return (_b = (_a = this.getNode(identifier, callback, beginRoot, iterationType)) == null ? void 0 : _a.value) != null ? _b : void 0;
|
|
7936
8005
|
}
|
|
7937
8006
|
/**
|
|
7938
|
-
* Time Complexity: O(
|
|
7939
|
-
* Space Complexity: O(
|
|
8007
|
+
* Time Complexity: O(1)
|
|
8008
|
+
* Space Complexity: O(1)
|
|
7940
8009
|
*/
|
|
7941
8010
|
/**
|
|
8011
|
+
* Time Complexity: O(1)
|
|
8012
|
+
* Space Complexity: O(1)
|
|
8013
|
+
*
|
|
7942
8014
|
* Clear the binary tree, removing all nodes.
|
|
7943
8015
|
*/
|
|
7944
8016
|
clear() {
|
|
@@ -7946,6 +8018,13 @@ var dataStructureTyped = (() => {
|
|
|
7946
8018
|
this._size = 0;
|
|
7947
8019
|
}
|
|
7948
8020
|
/**
|
|
8021
|
+
* Time Complexity: O(1)
|
|
8022
|
+
* Space Complexity: O(1)
|
|
8023
|
+
*/
|
|
8024
|
+
/**
|
|
8025
|
+
* Time Complexity: O(1)
|
|
8026
|
+
* Space Complexity: O(1)
|
|
8027
|
+
*
|
|
7949
8028
|
* Check if the binary tree is empty.
|
|
7950
8029
|
* @returns {boolean} - True if the binary tree is empty, false otherwise.
|
|
7951
8030
|
*/
|
|
@@ -7980,7 +8059,7 @@ var dataStructureTyped = (() => {
|
|
|
7980
8059
|
}
|
|
7981
8060
|
/**
|
|
7982
8061
|
* Time Complexity: O(log n)
|
|
7983
|
-
* Space Complexity: O(
|
|
8062
|
+
* Space Complexity: O(1)
|
|
7984
8063
|
*/
|
|
7985
8064
|
/**
|
|
7986
8065
|
* Time Complexity: O(log n)
|
|
@@ -8071,7 +8150,7 @@ var dataStructureTyped = (() => {
|
|
|
8071
8150
|
* possible values:
|
|
8072
8151
|
* @returns a boolean value.
|
|
8073
8152
|
*/
|
|
8074
|
-
|
|
8153
|
+
isBST(beginRoot = this.root, iterationType = this.iterationType) {
|
|
8075
8154
|
beginRoot = this.ensureNode(beginRoot);
|
|
8076
8155
|
if (!beginRoot)
|
|
8077
8156
|
return true;
|
|
@@ -8084,45 +8163,32 @@ var dataStructureTyped = (() => {
|
|
|
8084
8163
|
return false;
|
|
8085
8164
|
return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
|
|
8086
8165
|
};
|
|
8087
|
-
|
|
8166
|
+
const isStandardBST = dfs(beginRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
8167
|
+
const isInverseBST = dfs(beginRoot, Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
|
|
8168
|
+
return isStandardBST || isInverseBST;
|
|
8088
8169
|
} else {
|
|
8089
|
-
const
|
|
8090
|
-
|
|
8091
|
-
|
|
8092
|
-
|
|
8093
|
-
|
|
8094
|
-
curr
|
|
8170
|
+
const checkBST = (checkMax = false) => {
|
|
8171
|
+
const stack = [];
|
|
8172
|
+
let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
|
|
8173
|
+
let curr = beginRoot;
|
|
8174
|
+
while (curr || stack.length > 0) {
|
|
8175
|
+
while (curr) {
|
|
8176
|
+
stack.push(curr);
|
|
8177
|
+
curr = curr.left;
|
|
8178
|
+
}
|
|
8179
|
+
curr = stack.pop();
|
|
8180
|
+
const numKey = this.extractor(curr.key);
|
|
8181
|
+
if (!curr || !checkMax && prev >= numKey || checkMax && prev <= numKey)
|
|
8182
|
+
return false;
|
|
8183
|
+
prev = numKey;
|
|
8184
|
+
curr = curr.right;
|
|
8095
8185
|
}
|
|
8096
|
-
|
|
8097
|
-
|
|
8098
|
-
|
|
8099
|
-
|
|
8100
|
-
prev = numKey;
|
|
8101
|
-
curr = curr.right;
|
|
8102
|
-
}
|
|
8103
|
-
return true;
|
|
8186
|
+
return true;
|
|
8187
|
+
};
|
|
8188
|
+
const isStandardBST = checkBST(false), isInverseBST = checkBST(true);
|
|
8189
|
+
return isStandardBST || isInverseBST;
|
|
8104
8190
|
}
|
|
8105
8191
|
}
|
|
8106
|
-
/**
|
|
8107
|
-
* Time Complexity: O(n)
|
|
8108
|
-
* Space Complexity: O(1)
|
|
8109
|
-
*/
|
|
8110
|
-
/**
|
|
8111
|
-
* Time Complexity: O(n)
|
|
8112
|
-
* Space Complexity: O(1)
|
|
8113
|
-
*
|
|
8114
|
-
* The function checks if a binary tree is a binary search tree.
|
|
8115
|
-
* @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
|
|
8116
|
-
* be used when checking if the binary tree is a binary search tree (BST). It is an optional
|
|
8117
|
-
* parameter with a default value of "this.iterationType". The value of "this.iterationType" is
|
|
8118
|
-
* expected to be
|
|
8119
|
-
* @returns a boolean value.
|
|
8120
|
-
*/
|
|
8121
|
-
isBST(iterationType = this.iterationType) {
|
|
8122
|
-
if (this.root === null)
|
|
8123
|
-
return true;
|
|
8124
|
-
return this.isSubtreeBST(this.root, iterationType);
|
|
8125
|
-
}
|
|
8126
8192
|
/**
|
|
8127
8193
|
* Time complexity: O(n)
|
|
8128
8194
|
* Space complexity: O(log n)
|
|
@@ -8142,7 +8208,7 @@ var dataStructureTyped = (() => {
|
|
|
8142
8208
|
* whether to include null values in the traversal. If `includeNull` is set to `true`, the
|
|
8143
8209
|
* traversal will include null values, otherwise it will skip them.
|
|
8144
8210
|
* @returns The function `subTreeTraverse` returns an array of values that are the result of invoking
|
|
8145
|
-
* the `callback` function on each node in the subtree. The type of the array
|
|
8211
|
+
* the `callback` function on each node in the subtree. The type of the array nodes is determined
|
|
8146
8212
|
* by the return type of the `callback` function.
|
|
8147
8213
|
*/
|
|
8148
8214
|
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
@@ -8182,44 +8248,6 @@ var dataStructureTyped = (() => {
|
|
|
8182
8248
|
}
|
|
8183
8249
|
return ans;
|
|
8184
8250
|
}
|
|
8185
|
-
/**
|
|
8186
|
-
* Time complexity: O(n)
|
|
8187
|
-
* Space complexity: O(log n)
|
|
8188
|
-
*/
|
|
8189
|
-
/**
|
|
8190
|
-
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
8191
|
-
* BinaryTreeNode and its key is not NaN.
|
|
8192
|
-
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
8193
|
-
* @returns a boolean value.
|
|
8194
|
-
*/
|
|
8195
|
-
isRealNode(node) {
|
|
8196
|
-
return node instanceof BinaryTreeNode && String(node.key) !== "NaN";
|
|
8197
|
-
}
|
|
8198
|
-
/**
|
|
8199
|
-
* The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
|
|
8200
|
-
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
8201
|
-
* @returns a boolean value.
|
|
8202
|
-
*/
|
|
8203
|
-
isNIL(node) {
|
|
8204
|
-
return node instanceof BinaryTreeNode && String(node.key) === "NaN";
|
|
8205
|
-
}
|
|
8206
|
-
/**
|
|
8207
|
-
* The function checks if a given node is a real node or null.
|
|
8208
|
-
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
8209
|
-
* @returns a boolean value.
|
|
8210
|
-
*/
|
|
8211
|
-
isNodeOrNull(node) {
|
|
8212
|
-
return this.isRealNode(node) || node === null;
|
|
8213
|
-
}
|
|
8214
|
-
/**
|
|
8215
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
8216
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
8217
|
-
* data type.
|
|
8218
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
8219
|
-
*/
|
|
8220
|
-
isNotNodeInstance(potentialKey) {
|
|
8221
|
-
return !(potentialKey instanceof BinaryTreeNode);
|
|
8222
|
-
}
|
|
8223
8251
|
/**
|
|
8224
8252
|
* Time complexity: O(n)
|
|
8225
8253
|
* Space complexity: O(n)
|
|
@@ -8527,7 +8555,12 @@ var dataStructureTyped = (() => {
|
|
|
8527
8555
|
}
|
|
8528
8556
|
/**
|
|
8529
8557
|
* Time complexity: O(n)
|
|
8530
|
-
* Space complexity: O(
|
|
8558
|
+
* Space complexity: O(n)
|
|
8559
|
+
*/
|
|
8560
|
+
/**
|
|
8561
|
+
* Time complexity: O(n)
|
|
8562
|
+
* Space complexity: O(n)
|
|
8563
|
+
*
|
|
8531
8564
|
* The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
|
|
8532
8565
|
* algorithm.
|
|
8533
8566
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
@@ -8540,7 +8573,7 @@ var dataStructureTyped = (() => {
|
|
|
8540
8573
|
* for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
|
|
8541
8574
|
* the root of the tree. If no value is provided, the default value is the root of the tree.
|
|
8542
8575
|
* @returns The function `morris` returns an array of values that are the result of invoking the
|
|
8543
|
-
* `callback` function on each node in the binary tree. The type of the array
|
|
8576
|
+
* `callback` function on each node in the binary tree. The type of the array nodes is determined
|
|
8544
8577
|
* by the return type of the `callback` function.
|
|
8545
8578
|
*/
|
|
8546
8579
|
morris(callback = this._defaultOneParamCallback, pattern = "in", beginRoot = this.root) {
|
|
@@ -8649,8 +8682,8 @@ var dataStructureTyped = (() => {
|
|
|
8649
8682
|
* Time Complexity: O(n)
|
|
8650
8683
|
* Space Complexity: O(n)
|
|
8651
8684
|
*
|
|
8652
|
-
* The `filter` function creates a new tree by iterating over the
|
|
8653
|
-
* adding only the
|
|
8685
|
+
* The `filter` function creates a new tree by iterating over the nodes of the current tree and
|
|
8686
|
+
* adding only the nodes that satisfy the given predicate function.
|
|
8654
8687
|
* @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
|
|
8655
8688
|
* `key`, and `index`. It should return a boolean value indicating whether the pair should be
|
|
8656
8689
|
* included in the filtered tree or not.
|
|
@@ -8707,6 +8740,13 @@ var dataStructureTyped = (() => {
|
|
|
8707
8740
|
// // }
|
|
8708
8741
|
//
|
|
8709
8742
|
/**
|
|
8743
|
+
* Time Complexity: O(n)
|
|
8744
|
+
* Space Complexity: O(n)
|
|
8745
|
+
*/
|
|
8746
|
+
/**
|
|
8747
|
+
* Time Complexity: O(n)
|
|
8748
|
+
* Space Complexity: O(n)
|
|
8749
|
+
*
|
|
8710
8750
|
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
|
8711
8751
|
* @param {K | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `K | N | null |
|
|
8712
8752
|
* undefined`. It represents the root node of a binary tree. The root node can have one of the
|
|
@@ -8909,16 +8949,16 @@ var dataStructureTyped = (() => {
|
|
|
8909
8949
|
var BST = class _BST extends BinaryTree {
|
|
8910
8950
|
/**
|
|
8911
8951
|
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
|
8912
|
-
* the tree with optional
|
|
8913
|
-
* @param [
|
|
8952
|
+
* the tree with optional nodes and options.
|
|
8953
|
+
* @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the
|
|
8914
8954
|
* binary search tree.
|
|
8915
8955
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
8916
8956
|
* configuration options for the binary search tree. It can have the following properties:
|
|
8917
8957
|
*/
|
|
8918
|
-
constructor(
|
|
8958
|
+
constructor(nodes, options) {
|
|
8919
8959
|
super([], options);
|
|
8920
8960
|
__publicField(this, "_root");
|
|
8921
|
-
__publicField(this, "_variant", "
|
|
8961
|
+
__publicField(this, "_variant", "STANDARD" /* STANDARD */);
|
|
8922
8962
|
if (options) {
|
|
8923
8963
|
const { variant } = options;
|
|
8924
8964
|
if (variant) {
|
|
@@ -8926,8 +8966,8 @@ var dataStructureTyped = (() => {
|
|
|
8926
8966
|
}
|
|
8927
8967
|
}
|
|
8928
8968
|
this._root = void 0;
|
|
8929
|
-
if (
|
|
8930
|
-
this.addMany(
|
|
8969
|
+
if (nodes)
|
|
8970
|
+
this.addMany(nodes);
|
|
8931
8971
|
}
|
|
8932
8972
|
get root() {
|
|
8933
8973
|
return this._root;
|
|
@@ -8960,48 +9000,88 @@ var dataStructureTyped = (() => {
|
|
|
8960
9000
|
}, options));
|
|
8961
9001
|
}
|
|
8962
9002
|
/**
|
|
8963
|
-
* The function
|
|
8964
|
-
* @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V, N>`.
|
|
8965
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
8966
|
-
*/
|
|
8967
|
-
isNode(exemplar) {
|
|
8968
|
-
return exemplar instanceof BSTNode;
|
|
8969
|
-
}
|
|
8970
|
-
/**
|
|
8971
|
-
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
9003
|
+
* The function `exemplarToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
|
|
8972
9004
|
* otherwise it returns undefined.
|
|
8973
|
-
* @param
|
|
9005
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
|
|
8974
9006
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
8975
|
-
* `exemplarToNode` function. It represents the value associated with the
|
|
9007
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
|
|
8976
9008
|
* @returns a node of type N or undefined.
|
|
8977
9009
|
*/
|
|
8978
|
-
exemplarToNode(
|
|
9010
|
+
exemplarToNode(keyOrNodeOrEntry, value) {
|
|
8979
9011
|
let node;
|
|
8980
|
-
if (
|
|
9012
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === void 0) {
|
|
8981
9013
|
return;
|
|
8982
|
-
} else if (this.isNode(
|
|
8983
|
-
node =
|
|
8984
|
-
} else if (this.isEntry(
|
|
8985
|
-
const [key, value2] =
|
|
9014
|
+
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
9015
|
+
node = keyOrNodeOrEntry;
|
|
9016
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
9017
|
+
const [key, value2] = keyOrNodeOrEntry;
|
|
8986
9018
|
if (key === void 0 || key === null) {
|
|
8987
9019
|
return;
|
|
8988
9020
|
} else {
|
|
8989
9021
|
node = this.createNode(key, value2);
|
|
8990
9022
|
}
|
|
8991
|
-
} else if (this.isNotNodeInstance(
|
|
8992
|
-
node = this.createNode(
|
|
9023
|
+
} else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
9024
|
+
node = this.createNode(keyOrNodeOrEntry, value);
|
|
8993
9025
|
} else {
|
|
8994
9026
|
return;
|
|
8995
9027
|
}
|
|
8996
9028
|
return node;
|
|
8997
9029
|
}
|
|
8998
9030
|
/**
|
|
8999
|
-
* Time Complexity: O(log n)
|
|
9000
|
-
* Space Complexity: O(
|
|
9031
|
+
* Time Complexity: O(log n)
|
|
9032
|
+
* Space Complexity: O(log n)
|
|
9033
|
+
* Average case for a balanced tree. Space for the recursive call stack in the worst case.
|
|
9034
|
+
*/
|
|
9035
|
+
/**
|
|
9036
|
+
* Time Complexity: O(log n)
|
|
9037
|
+
* Space Complexity: O(log n)
|
|
9038
|
+
*
|
|
9039
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
9040
|
+
* otherwise it returns the key itself.
|
|
9041
|
+
* @param {K | N | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`, or
|
|
9042
|
+
* `undefined`.
|
|
9043
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
9044
|
+
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
9045
|
+
* @returns either a node object (N) or undefined.
|
|
9046
|
+
*/
|
|
9047
|
+
ensureNode(keyOrNodeOrEntry, iterationType = "ITERATIVE" /* ITERATIVE */) {
|
|
9048
|
+
let res;
|
|
9049
|
+
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
9050
|
+
res = keyOrNodeOrEntry;
|
|
9051
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
9052
|
+
if (keyOrNodeOrEntry[0])
|
|
9053
|
+
res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
9054
|
+
} else {
|
|
9055
|
+
if (keyOrNodeOrEntry)
|
|
9056
|
+
res = this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
9057
|
+
}
|
|
9058
|
+
return res;
|
|
9059
|
+
}
|
|
9060
|
+
/**
|
|
9061
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
9062
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
9063
|
+
* data type.
|
|
9064
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
9001
9065
|
*/
|
|
9066
|
+
isNotNodeInstance(potentialKey) {
|
|
9067
|
+
return !(potentialKey instanceof BSTNode);
|
|
9068
|
+
}
|
|
9069
|
+
/**
|
|
9070
|
+
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
9071
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
9072
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
|
|
9073
|
+
*/
|
|
9074
|
+
isNode(keyOrNodeOrEntry) {
|
|
9075
|
+
return keyOrNodeOrEntry instanceof BSTNode;
|
|
9076
|
+
}
|
|
9002
9077
|
/**
|
|
9003
|
-
* Time Complexity: O(log n)
|
|
9004
|
-
* Space Complexity: O(1)
|
|
9078
|
+
* Time Complexity: O(log n)
|
|
9079
|
+
* Space Complexity: O(1)
|
|
9080
|
+
* - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
9081
|
+
*/
|
|
9082
|
+
/**
|
|
9083
|
+
* Time Complexity: O(log n)
|
|
9084
|
+
* Space Complexity: O(1)
|
|
9005
9085
|
*
|
|
9006
9086
|
* The `add` function adds a new node to a binary tree, updating the value if the key already exists
|
|
9007
9087
|
* or inserting a new node if the key is unique.
|
|
@@ -9014,23 +9094,23 @@ var dataStructureTyped = (() => {
|
|
|
9014
9094
|
add(keyOrNodeOrEntry, value) {
|
|
9015
9095
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
9016
9096
|
if (newNode === void 0)
|
|
9017
|
-
return;
|
|
9097
|
+
return false;
|
|
9018
9098
|
if (this.root === void 0) {
|
|
9019
9099
|
this._setRoot(newNode);
|
|
9020
9100
|
this._size++;
|
|
9021
|
-
return
|
|
9101
|
+
return true;
|
|
9022
9102
|
}
|
|
9023
9103
|
let current = this.root;
|
|
9024
9104
|
while (current !== void 0) {
|
|
9025
9105
|
if (this._compare(current.key, newNode.key) === "eq" /* eq */) {
|
|
9026
9106
|
this._replaceNode(current, newNode);
|
|
9027
|
-
return
|
|
9107
|
+
return true;
|
|
9028
9108
|
} else if (this._compare(current.key, newNode.key) === "gt" /* gt */) {
|
|
9029
9109
|
if (current.left === void 0) {
|
|
9030
9110
|
current.left = newNode;
|
|
9031
9111
|
newNode.parent = current;
|
|
9032
9112
|
this._size++;
|
|
9033
|
-
return
|
|
9113
|
+
return true;
|
|
9034
9114
|
}
|
|
9035
9115
|
current = current.left;
|
|
9036
9116
|
} else {
|
|
@@ -9038,20 +9118,21 @@ var dataStructureTyped = (() => {
|
|
|
9038
9118
|
current.right = newNode;
|
|
9039
9119
|
newNode.parent = current;
|
|
9040
9120
|
this._size++;
|
|
9041
|
-
return
|
|
9121
|
+
return true;
|
|
9042
9122
|
}
|
|
9043
9123
|
current = current.right;
|
|
9044
9124
|
}
|
|
9045
9125
|
}
|
|
9046
|
-
return
|
|
9126
|
+
return false;
|
|
9047
9127
|
}
|
|
9048
9128
|
/**
|
|
9049
|
-
* Time Complexity: O(k log n)
|
|
9050
|
-
* Space Complexity: O(k)
|
|
9129
|
+
* Time Complexity: O(k log n)
|
|
9130
|
+
* Space Complexity: O(k)
|
|
9131
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
9051
9132
|
*/
|
|
9052
9133
|
/**
|
|
9053
|
-
* Time Complexity: O(k log n)
|
|
9054
|
-
* Space Complexity: O(k)
|
|
9134
|
+
* Time Complexity: O(k log n)
|
|
9135
|
+
* Space Complexity: O(k)
|
|
9055
9136
|
*
|
|
9056
9137
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
9057
9138
|
* balancing the tree after each addition.
|
|
@@ -9062,7 +9143,7 @@ var dataStructureTyped = (() => {
|
|
|
9062
9143
|
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
9063
9144
|
* @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
|
|
9064
9145
|
* balanced or not. If set to true, the add operation will be balanced using a binary search tree
|
|
9065
|
-
* algorithm. If set to false, the add operation will not be balanced and the
|
|
9146
|
+
* algorithm. If set to false, the add operation will not be balanced and the nodes will be added
|
|
9066
9147
|
* in the order they appear in the input.
|
|
9067
9148
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
9068
9149
|
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
@@ -9143,20 +9224,19 @@ var dataStructureTyped = (() => {
|
|
|
9143
9224
|
return inserted;
|
|
9144
9225
|
}
|
|
9145
9226
|
/**
|
|
9146
|
-
* Time Complexity: O(n log n)
|
|
9147
|
-
* Space Complexity: O(n)
|
|
9227
|
+
* Time Complexity: O(n log n)
|
|
9228
|
+
* Space Complexity: O(n)
|
|
9229
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
9148
9230
|
*/
|
|
9149
9231
|
/**
|
|
9150
|
-
* Time Complexity: O(log n)
|
|
9151
|
-
* Space Complexity: O(
|
|
9232
|
+
* Time Complexity: O(n log n)
|
|
9233
|
+
* Space Complexity: O(n)
|
|
9152
9234
|
*
|
|
9153
9235
|
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
9154
9236
|
* leftmost node if the comparison result is greater than.
|
|
9155
9237
|
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
9156
9238
|
* type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
9157
9239
|
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
9158
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
9159
|
-
* be performed. It can have one of the following values:
|
|
9160
9240
|
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
9161
9241
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
9162
9242
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
@@ -9165,7 +9245,7 @@ var dataStructureTyped = (() => {
|
|
|
9165
9245
|
let current = this.ensureNode(beginRoot);
|
|
9166
9246
|
if (!current)
|
|
9167
9247
|
return void 0;
|
|
9168
|
-
if (this._variant === "
|
|
9248
|
+
if (this._variant === "STANDARD" /* STANDARD */) {
|
|
9169
9249
|
while (current.right !== void 0) {
|
|
9170
9250
|
current = current.right;
|
|
9171
9251
|
}
|
|
@@ -9177,12 +9257,12 @@ var dataStructureTyped = (() => {
|
|
|
9177
9257
|
return current.key;
|
|
9178
9258
|
}
|
|
9179
9259
|
/**
|
|
9180
|
-
* Time Complexity: O(log n)
|
|
9181
|
-
* Space Complexity: O(1)
|
|
9260
|
+
* Time Complexity: O(log n)
|
|
9261
|
+
* Space Complexity: O(1)
|
|
9182
9262
|
*/
|
|
9183
9263
|
/**
|
|
9184
|
-
* Time Complexity: O(log n)
|
|
9185
|
-
* Space Complexity: O(
|
|
9264
|
+
* Time Complexity: O(log n)
|
|
9265
|
+
* Space Complexity: O(1)
|
|
9186
9266
|
*
|
|
9187
9267
|
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
9188
9268
|
* either recursive or iterative methods.
|
|
@@ -9225,53 +9305,34 @@ var dataStructureTyped = (() => {
|
|
|
9225
9305
|
}
|
|
9226
9306
|
}
|
|
9227
9307
|
/**
|
|
9228
|
-
|
|
9229
|
-
|
|
9230
|
-
|
|
9231
|
-
|
|
9232
|
-
|
|
9233
|
-
|
|
9234
|
-
|
|
9235
|
-
|
|
9236
|
-
|
|
9237
|
-
|
|
9238
|
-
|
|
9239
|
-
|
|
9240
|
-
|
|
9241
|
-
|
|
9242
|
-
|
|
9243
|
-
|
|
9244
|
-
|
|
9245
|
-
|
|
9246
|
-
|
|
9247
|
-
|
|
9248
|
-
|
|
9249
|
-
|
|
9250
|
-
|
|
9251
|
-
|
|
9252
|
-
|
|
9253
|
-
|
|
9254
|
-
|
|
9255
|
-
|
|
9256
|
-
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
9257
|
-
* recursive or iterative approach.
|
|
9258
|
-
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
9259
|
-
* want to search for in the nodes of the binary tree. It can be of any type that is returned by the
|
|
9260
|
-
* callback function `C`.
|
|
9261
|
-
* @param {C} callback - The `callback` parameter is a function that takes a node of type `N` as its
|
|
9262
|
-
* argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
|
|
9263
|
-
* function type that extends the `BTNCallback<N>` type. The `BTNCallback<N>` type is
|
|
9264
|
-
* @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
|
|
9265
|
-
* first node that matches the identifier. If set to true, the function will return an array
|
|
9266
|
-
* containing only the first matching node. If set to false (default), the function will continue
|
|
9267
|
-
* searching for all nodes that match the identifier and return an array containing
|
|
9268
|
-
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
|
|
9269
|
-
* for the traversal. It can be either a key value or a node object. If it is undefined, the
|
|
9270
|
-
* traversal will start from the root of the tree.
|
|
9271
|
-
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
9272
|
-
* performed on the binary tree. It can have two possible values:
|
|
9273
|
-
* @returns The method returns an array of nodes (`N[]`).
|
|
9274
|
-
*/
|
|
9308
|
+
* Time Complexity: O(log n)
|
|
9309
|
+
* Space Complexity: O(log n)
|
|
9310
|
+
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
9311
|
+
* /
|
|
9312
|
+
|
|
9313
|
+
/**
|
|
9314
|
+
* Time Complexity: O(log n)
|
|
9315
|
+
* Space Complexity: O(log n)
|
|
9316
|
+
*
|
|
9317
|
+
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
9318
|
+
* recursive or iterative approach.
|
|
9319
|
+
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
9320
|
+
* want to search for in the nodes of the binary tree. It can be of any type that is returned by the
|
|
9321
|
+
* callback function `C`.
|
|
9322
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `N` as its
|
|
9323
|
+
* argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
|
|
9324
|
+
* function type that extends the `BTNCallback<N>` type. The `BTNCallback<N>` type is
|
|
9325
|
+
* @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
|
|
9326
|
+
* first node that matches the identifier. If set to true, the function will return an array
|
|
9327
|
+
* containing only the first matching node. If set to false (default), the function will continue
|
|
9328
|
+
* searching for all nodes that match the identifier and return an array containing
|
|
9329
|
+
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
|
|
9330
|
+
* for the traversal. It can be either a key value or a node object. If it is undefined, the
|
|
9331
|
+
* traversal will start from the root of the tree.
|
|
9332
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
9333
|
+
* performed on the binary tree. It can have two possible values:
|
|
9334
|
+
* @returns The method returns an array of nodes (`N[]`).
|
|
9335
|
+
*/
|
|
9275
9336
|
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
9276
9337
|
beginRoot = this.ensureNode(beginRoot);
|
|
9277
9338
|
if (!beginRoot)
|
|
@@ -9324,12 +9385,13 @@ var dataStructureTyped = (() => {
|
|
|
9324
9385
|
return ans;
|
|
9325
9386
|
}
|
|
9326
9387
|
/**
|
|
9327
|
-
* Time Complexity: O(log n)
|
|
9328
|
-
* Space Complexity: O(log n)
|
|
9388
|
+
* Time Complexity: O(log n)
|
|
9389
|
+
* Space Complexity: O(log n)
|
|
9390
|
+
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
9329
9391
|
*/
|
|
9330
9392
|
/**
|
|
9331
|
-
* Time Complexity: O(log n)
|
|
9332
|
-
* Space Complexity: O(log n)
|
|
9393
|
+
* Time Complexity: O(log n)
|
|
9394
|
+
* Space Complexity: O(log n)
|
|
9333
9395
|
*
|
|
9334
9396
|
* The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
|
|
9335
9397
|
* are either lesser or greater than a target node, depending on the specified comparison type.
|
|
@@ -9388,12 +9450,12 @@ var dataStructureTyped = (() => {
|
|
|
9388
9450
|
}
|
|
9389
9451
|
}
|
|
9390
9452
|
/**
|
|
9391
|
-
* Time Complexity: O(log n)
|
|
9392
|
-
* Space Complexity: O(log n)
|
|
9453
|
+
* Time Complexity: O(log n)
|
|
9454
|
+
* Space Complexity: O(log n)
|
|
9393
9455
|
*/
|
|
9394
9456
|
/**
|
|
9395
|
-
* Time Complexity: O(n)
|
|
9396
|
-
* Space Complexity: O(n)
|
|
9457
|
+
* Time Complexity: O(log n)
|
|
9458
|
+
* Space Complexity: O(log n)
|
|
9397
9459
|
*
|
|
9398
9460
|
* The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
|
|
9399
9461
|
* ensures the tree is perfectly balanced.
|
|
@@ -9451,8 +9513,8 @@ var dataStructureTyped = (() => {
|
|
|
9451
9513
|
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
9452
9514
|
*/
|
|
9453
9515
|
/**
|
|
9454
|
-
* Time Complexity: O(n)
|
|
9455
|
-
* Space Complexity: O(log n)
|
|
9516
|
+
* Time Complexity: O(n)
|
|
9517
|
+
* Space Complexity: O(log n)
|
|
9456
9518
|
*
|
|
9457
9519
|
* The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
|
|
9458
9520
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
@@ -9519,7 +9581,7 @@ var dataStructureTyped = (() => {
|
|
|
9519
9581
|
_compare(a, b) {
|
|
9520
9582
|
const extractedA = this.extractor(a);
|
|
9521
9583
|
const extractedB = this.extractor(b);
|
|
9522
|
-
const compared = this.variant === "
|
|
9584
|
+
const compared = this.variant === "STANDARD" /* STANDARD */ ? extractedA - extractedB : extractedB - extractedA;
|
|
9523
9585
|
return compared > 0 ? "gt" /* gt */ : compared < 0 ? "lt" /* lt */ : "eq" /* eq */;
|
|
9524
9586
|
}
|
|
9525
9587
|
};
|
|
@@ -9960,18 +10022,18 @@ var dataStructureTyped = (() => {
|
|
|
9960
10022
|
};
|
|
9961
10023
|
var AVLTree = class _AVLTree extends BST {
|
|
9962
10024
|
/**
|
|
9963
|
-
* The constructor function initializes an AVLTree object with optional
|
|
9964
|
-
* @param [
|
|
9965
|
-
* objects. It represents a collection of
|
|
10025
|
+
* The constructor function initializes an AVLTree object with optional nodes and options.
|
|
10026
|
+
* @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
|
|
10027
|
+
* objects. It represents a collection of nodes that will be added to the AVL tree during
|
|
9966
10028
|
* initialization.
|
|
9967
10029
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
9968
10030
|
* behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
|
|
9969
10031
|
* provide only a subset of the properties defined in the `AVLTreeOptions` interface.
|
|
9970
10032
|
*/
|
|
9971
|
-
constructor(
|
|
10033
|
+
constructor(nodes, options) {
|
|
9972
10034
|
super([], options);
|
|
9973
|
-
if (
|
|
9974
|
-
super.addMany(
|
|
10035
|
+
if (nodes)
|
|
10036
|
+
super.addMany(nodes);
|
|
9975
10037
|
}
|
|
9976
10038
|
/**
|
|
9977
10039
|
* The function creates a new AVL tree node with the specified key and value.
|
|
@@ -9999,12 +10061,12 @@ var dataStructureTyped = (() => {
|
|
|
9999
10061
|
}, options));
|
|
10000
10062
|
}
|
|
10001
10063
|
/**
|
|
10002
|
-
* The function checks if an
|
|
10003
|
-
* @param
|
|
10004
|
-
* @returns a boolean value indicating whether the
|
|
10064
|
+
* The function checks if an keyOrNodeOrEntry is an instance of AVLTreeNode.
|
|
10065
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
10066
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeNode class.
|
|
10005
10067
|
*/
|
|
10006
|
-
isNode(
|
|
10007
|
-
return
|
|
10068
|
+
isNode(keyOrNodeOrEntry) {
|
|
10069
|
+
return keyOrNodeOrEntry instanceof AVLTreeNode;
|
|
10008
10070
|
}
|
|
10009
10071
|
/**
|
|
10010
10072
|
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
@@ -10016,12 +10078,13 @@ var dataStructureTyped = (() => {
|
|
|
10016
10078
|
return !(potentialKey instanceof AVLTreeNode);
|
|
10017
10079
|
}
|
|
10018
10080
|
/**
|
|
10019
|
-
* Time Complexity: O(log n)
|
|
10020
|
-
* Space Complexity: O(1)
|
|
10081
|
+
* Time Complexity: O(log n)
|
|
10082
|
+
* Space Complexity: O(1)
|
|
10083
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
10021
10084
|
*/
|
|
10022
10085
|
/**
|
|
10023
|
-
* Time Complexity: O(log n)
|
|
10024
|
-
* Space Complexity: O(1)
|
|
10086
|
+
* Time Complexity: O(log n)
|
|
10087
|
+
* Space Complexity: O(1)
|
|
10025
10088
|
*
|
|
10026
10089
|
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
10027
10090
|
* a new node.
|
|
@@ -10033,19 +10096,19 @@ var dataStructureTyped = (() => {
|
|
|
10033
10096
|
*/
|
|
10034
10097
|
add(keyOrNodeOrEntry, value) {
|
|
10035
10098
|
if (keyOrNodeOrEntry === null)
|
|
10036
|
-
return
|
|
10099
|
+
return false;
|
|
10037
10100
|
const inserted = super.add(keyOrNodeOrEntry, value);
|
|
10038
10101
|
if (inserted)
|
|
10039
|
-
this._balancePath(
|
|
10102
|
+
this._balancePath(keyOrNodeOrEntry);
|
|
10040
10103
|
return inserted;
|
|
10041
10104
|
}
|
|
10042
10105
|
/**
|
|
10043
|
-
* Time Complexity: O(log n)
|
|
10044
|
-
* Space Complexity: O(1)
|
|
10106
|
+
* Time Complexity: O(log n)
|
|
10107
|
+
* Space Complexity: O(1)
|
|
10045
10108
|
*/
|
|
10046
10109
|
/**
|
|
10047
|
-
* Time Complexity: O(log n)
|
|
10048
|
-
* Space Complexity: O(1)
|
|
10110
|
+
* Time Complexity: O(log n)
|
|
10111
|
+
* Space Complexity: O(1)
|
|
10049
10112
|
*
|
|
10050
10113
|
* The function overrides the delete method of a binary tree, performs the deletion, and then
|
|
10051
10114
|
* balances the tree if necessary.
|
|
@@ -10099,12 +10162,13 @@ var dataStructureTyped = (() => {
|
|
|
10099
10162
|
return void 0;
|
|
10100
10163
|
}
|
|
10101
10164
|
/**
|
|
10102
|
-
* Time Complexity: O(1)
|
|
10103
|
-
* Space Complexity: O(1)
|
|
10165
|
+
* Time Complexity: O(1)
|
|
10166
|
+
* Space Complexity: O(1)
|
|
10167
|
+
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
|
|
10104
10168
|
*/
|
|
10105
10169
|
/**
|
|
10106
|
-
* Time Complexity: O(1)
|
|
10107
|
-
* Space Complexity: O(1)
|
|
10170
|
+
* Time Complexity: O(1)
|
|
10171
|
+
* Space Complexity: O(1)
|
|
10108
10172
|
*
|
|
10109
10173
|
* The function calculates the balance factor of a node in a binary tree.
|
|
10110
10174
|
* @param {N} node - The parameter "node" represents a node in a binary tree data structure.
|
|
@@ -10120,12 +10184,13 @@ var dataStructureTyped = (() => {
|
|
|
10120
10184
|
return node.right.height - node.left.height;
|
|
10121
10185
|
}
|
|
10122
10186
|
/**
|
|
10123
|
-
* Time Complexity: O(1)
|
|
10124
|
-
* Space Complexity: O(1)
|
|
10187
|
+
* Time Complexity: O(1)
|
|
10188
|
+
* Space Complexity: O(1)
|
|
10189
|
+
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
|
|
10125
10190
|
*/
|
|
10126
10191
|
/**
|
|
10127
|
-
* Time Complexity: O(1)
|
|
10128
|
-
* Space Complexity: O(1)
|
|
10192
|
+
* Time Complexity: O(1)
|
|
10193
|
+
* Space Complexity: O(1)
|
|
10129
10194
|
*
|
|
10130
10195
|
* The function updates the height of a node in a binary tree based on the heights of its left and
|
|
10131
10196
|
* right children.
|
|
@@ -10143,12 +10208,13 @@ var dataStructureTyped = (() => {
|
|
|
10143
10208
|
node.height = 1 + Math.max(node.right.height, node.left.height);
|
|
10144
10209
|
}
|
|
10145
10210
|
/**
|
|
10146
|
-
* Time Complexity: O(log n)
|
|
10147
|
-
* Space Complexity: O(1)
|
|
10211
|
+
* Time Complexity: O(log n)
|
|
10212
|
+
* Space Complexity: O(1)
|
|
10213
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root. constant space, as it doesn't use additional data structures that scale with input size.
|
|
10148
10214
|
*/
|
|
10149
10215
|
/**
|
|
10150
|
-
* Time Complexity: O(log n)
|
|
10151
|
-
* Space Complexity: O(1)
|
|
10216
|
+
* Time Complexity: O(log n)
|
|
10217
|
+
* Space Complexity: O(1)
|
|
10152
10218
|
*
|
|
10153
10219
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
10154
10220
|
* to restore balance in an AVL tree after inserting a node.
|
|
@@ -10156,6 +10222,7 @@ var dataStructureTyped = (() => {
|
|
|
10156
10222
|
* AVL tree that needs to be balanced.
|
|
10157
10223
|
*/
|
|
10158
10224
|
_balancePath(node) {
|
|
10225
|
+
node = this.ensureNode(node);
|
|
10159
10226
|
const path = this.getPathToRoot(node, false);
|
|
10160
10227
|
for (let i = 0; i < path.length; i++) {
|
|
10161
10228
|
const A = path[i];
|
|
@@ -10182,12 +10249,13 @@ var dataStructureTyped = (() => {
|
|
|
10182
10249
|
}
|
|
10183
10250
|
}
|
|
10184
10251
|
/**
|
|
10185
|
-
* Time Complexity: O(1)
|
|
10186
|
-
* Space Complexity: O(1)
|
|
10252
|
+
* Time Complexity: O(1)
|
|
10253
|
+
* Space Complexity: O(1)
|
|
10254
|
+
* constant time, as these methods perform a fixed number of operations. constant space, as they only use a constant amount of memory.
|
|
10187
10255
|
*/
|
|
10188
10256
|
/**
|
|
10189
|
-
* Time Complexity: O(1)
|
|
10190
|
-
* Space Complexity: O(1)
|
|
10257
|
+
* Time Complexity: O(1)
|
|
10258
|
+
* Space Complexity: O(1)
|
|
10191
10259
|
*
|
|
10192
10260
|
* The function `_balanceLL` performs a left-left rotation to balance a binary tree.
|
|
10193
10261
|
* @param {N} A - A is a node in a binary tree.
|
|
@@ -10221,12 +10289,12 @@ var dataStructureTyped = (() => {
|
|
|
10221
10289
|
this._updateHeight(B);
|
|
10222
10290
|
}
|
|
10223
10291
|
/**
|
|
10224
|
-
* Time Complexity: O(1)
|
|
10225
|
-
* Space Complexity: O(1)
|
|
10292
|
+
* Time Complexity: O(1)
|
|
10293
|
+
* Space Complexity: O(1)
|
|
10226
10294
|
*/
|
|
10227
10295
|
/**
|
|
10228
|
-
* Time Complexity: O(1)
|
|
10229
|
-
* Space Complexity: O(1)
|
|
10296
|
+
* Time Complexity: O(1)
|
|
10297
|
+
* Space Complexity: O(1)
|
|
10230
10298
|
*
|
|
10231
10299
|
* The `_balanceLR` function performs a left-right rotation to balance a binary tree.
|
|
10232
10300
|
* @param {N} A - A is a node in a binary tree.
|
|
@@ -10275,12 +10343,12 @@ var dataStructureTyped = (() => {
|
|
|
10275
10343
|
C && this._updateHeight(C);
|
|
10276
10344
|
}
|
|
10277
10345
|
/**
|
|
10278
|
-
* Time Complexity: O(1)
|
|
10279
|
-
* Space Complexity: O(1)
|
|
10346
|
+
* Time Complexity: O(1)
|
|
10347
|
+
* Space Complexity: O(1)
|
|
10280
10348
|
*/
|
|
10281
10349
|
/**
|
|
10282
|
-
* Time Complexity: O(1)
|
|
10283
|
-
* Space Complexity: O(1)
|
|
10350
|
+
* Time Complexity: O(1)
|
|
10351
|
+
* Space Complexity: O(1)
|
|
10284
10352
|
*
|
|
10285
10353
|
* The function `_balanceRR` performs a right-right rotation to balance a binary tree.
|
|
10286
10354
|
* @param {N} A - A is a node in a binary tree.
|
|
@@ -10315,12 +10383,12 @@ var dataStructureTyped = (() => {
|
|
|
10315
10383
|
B && this._updateHeight(B);
|
|
10316
10384
|
}
|
|
10317
10385
|
/**
|
|
10318
|
-
* Time Complexity: O(1)
|
|
10319
|
-
* Space Complexity: O(1)
|
|
10386
|
+
* Time Complexity: O(1)
|
|
10387
|
+
* Space Complexity: O(1)
|
|
10320
10388
|
*/
|
|
10321
10389
|
/**
|
|
10322
|
-
* Time Complexity: O(1)
|
|
10323
|
-
* Space Complexity: O(1)
|
|
10390
|
+
* Time Complexity: O(1)
|
|
10391
|
+
* Space Complexity: O(1)
|
|
10324
10392
|
*
|
|
10325
10393
|
* The function `_balanceRL` performs a right-left rotation to balance a binary tree.
|
|
10326
10394
|
* @param {N} A - A is a node in a binary tree.
|
|
@@ -10385,23 +10453,23 @@ var dataStructureTyped = (() => {
|
|
|
10385
10453
|
var RedBlackTree = class _RedBlackTree extends BST {
|
|
10386
10454
|
/**
|
|
10387
10455
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
10388
|
-
* initializes the tree with optional
|
|
10389
|
-
* @param [
|
|
10390
|
-
* objects. It represents the initial
|
|
10456
|
+
* initializes the tree with optional nodes and options.
|
|
10457
|
+
* @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
|
|
10458
|
+
* objects. It represents the initial nodes that will be added to the RBTree during its
|
|
10391
10459
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
10392
|
-
*
|
|
10460
|
+
* nodes to the
|
|
10393
10461
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
10394
10462
|
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
|
10395
10463
|
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
10396
10464
|
*/
|
|
10397
|
-
constructor(
|
|
10465
|
+
constructor(nodes, options) {
|
|
10398
10466
|
super([], options);
|
|
10399
10467
|
__publicField(this, "Sentinel", new RedBlackTreeNode(NaN));
|
|
10400
10468
|
__publicField(this, "_root");
|
|
10401
10469
|
__publicField(this, "_size", 0);
|
|
10402
10470
|
this._root = this.Sentinel;
|
|
10403
|
-
if (
|
|
10404
|
-
super.addMany(
|
|
10471
|
+
if (nodes)
|
|
10472
|
+
super.addMany(nodes);
|
|
10405
10473
|
}
|
|
10406
10474
|
get root() {
|
|
10407
10475
|
return this._root;
|
|
@@ -10438,57 +10506,67 @@ var dataStructureTyped = (() => {
|
|
|
10438
10506
|
}, options));
|
|
10439
10507
|
}
|
|
10440
10508
|
/**
|
|
10441
|
-
* The function
|
|
10442
|
-
* @param
|
|
10443
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
|
|
10444
|
-
* class.
|
|
10445
|
-
*/
|
|
10446
|
-
isNode(exemplar) {
|
|
10447
|
-
return exemplar instanceof RedBlackTreeNode;
|
|
10448
|
-
}
|
|
10449
|
-
/**
|
|
10450
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
10451
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
10452
|
-
* data type.
|
|
10453
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
10454
|
-
*/
|
|
10455
|
-
isNotNodeInstance(potentialKey) {
|
|
10456
|
-
return !(potentialKey instanceof RedBlackTreeNode);
|
|
10457
|
-
}
|
|
10458
|
-
/**
|
|
10459
|
-
* The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
|
|
10460
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where:
|
|
10509
|
+
* The function `exemplarToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible.
|
|
10510
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
|
|
10461
10511
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
10462
|
-
* `exemplarToNode` function. It represents the value associated with the
|
|
10512
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If a value
|
|
10463
10513
|
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
10464
10514
|
* @returns a node of type N or undefined.
|
|
10465
10515
|
*/
|
|
10466
|
-
exemplarToNode(
|
|
10516
|
+
exemplarToNode(keyOrNodeOrEntry, value) {
|
|
10467
10517
|
let node;
|
|
10468
|
-
if (
|
|
10518
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === void 0) {
|
|
10469
10519
|
return;
|
|
10470
|
-
} else if (this.isNode(
|
|
10471
|
-
node =
|
|
10472
|
-
} else if (this.isEntry(
|
|
10473
|
-
const [key, value2] =
|
|
10520
|
+
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
10521
|
+
node = keyOrNodeOrEntry;
|
|
10522
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
10523
|
+
const [key, value2] = keyOrNodeOrEntry;
|
|
10474
10524
|
if (key === void 0 || key === null) {
|
|
10475
10525
|
return;
|
|
10476
10526
|
} else {
|
|
10477
10527
|
node = this.createNode(key, value2, 1 /* RED */);
|
|
10478
10528
|
}
|
|
10479
|
-
} else if (this.isNotNodeInstance(
|
|
10480
|
-
node = this.createNode(
|
|
10529
|
+
} else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
10530
|
+
node = this.createNode(keyOrNodeOrEntry, value, 1 /* RED */);
|
|
10481
10531
|
} else {
|
|
10482
10532
|
return;
|
|
10483
10533
|
}
|
|
10484
10534
|
return node;
|
|
10485
10535
|
}
|
|
10536
|
+
/**
|
|
10537
|
+
* The function checks if an keyOrNodeOrEntry is an instance of the RedBlackTreeNode class.
|
|
10538
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
10539
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the RedBlackTreeNode
|
|
10540
|
+
* class.
|
|
10541
|
+
*/
|
|
10542
|
+
isNode(keyOrNodeOrEntry) {
|
|
10543
|
+
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
|
|
10544
|
+
}
|
|
10486
10545
|
/**
|
|
10487
10546
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
10488
10547
|
* Space Complexity: O(1)
|
|
10489
10548
|
*/
|
|
10549
|
+
isRealNode(node) {
|
|
10550
|
+
if (node === this.Sentinel || node === void 0)
|
|
10551
|
+
return false;
|
|
10552
|
+
return node instanceof RedBlackTreeNode;
|
|
10553
|
+
}
|
|
10490
10554
|
/**
|
|
10491
|
-
*
|
|
10555
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
10556
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
10557
|
+
* data type.
|
|
10558
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
10559
|
+
*/
|
|
10560
|
+
isNotNodeInstance(potentialKey) {
|
|
10561
|
+
return !(potentialKey instanceof RedBlackTreeNode);
|
|
10562
|
+
}
|
|
10563
|
+
/**
|
|
10564
|
+
* Time Complexity: O(log n)
|
|
10565
|
+
* Space Complexity: O(1)
|
|
10566
|
+
* on average (where n is the number of nodes in the tree)
|
|
10567
|
+
*/
|
|
10568
|
+
/**
|
|
10569
|
+
* Time Complexity: O(log n)
|
|
10492
10570
|
* Space Complexity: O(1)
|
|
10493
10571
|
*
|
|
10494
10572
|
* The `add` function adds a new node to a binary search tree and performs necessary rotations and
|
|
@@ -10502,7 +10580,7 @@ var dataStructureTyped = (() => {
|
|
|
10502
10580
|
add(keyOrNodeOrEntry, value) {
|
|
10503
10581
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
10504
10582
|
if (newNode === void 0)
|
|
10505
|
-
return;
|
|
10583
|
+
return false;
|
|
10506
10584
|
newNode.left = this.Sentinel;
|
|
10507
10585
|
newNode.right = this.Sentinel;
|
|
10508
10586
|
let y = void 0;
|
|
@@ -10518,7 +10596,7 @@ var dataStructureTyped = (() => {
|
|
|
10518
10596
|
if (newNode !== x) {
|
|
10519
10597
|
this._replaceNode(x, newNode);
|
|
10520
10598
|
}
|
|
10521
|
-
return;
|
|
10599
|
+
return false;
|
|
10522
10600
|
}
|
|
10523
10601
|
}
|
|
10524
10602
|
}
|
|
@@ -10533,21 +10611,23 @@ var dataStructureTyped = (() => {
|
|
|
10533
10611
|
if (newNode.parent === void 0) {
|
|
10534
10612
|
newNode.color = 0 /* BLACK */;
|
|
10535
10613
|
this._size++;
|
|
10536
|
-
return;
|
|
10614
|
+
return false;
|
|
10537
10615
|
}
|
|
10538
10616
|
if (newNode.parent.parent === void 0) {
|
|
10539
10617
|
this._size++;
|
|
10540
|
-
return;
|
|
10618
|
+
return false;
|
|
10541
10619
|
}
|
|
10542
10620
|
this._fixInsert(newNode);
|
|
10543
10621
|
this._size++;
|
|
10622
|
+
return true;
|
|
10544
10623
|
}
|
|
10545
10624
|
/**
|
|
10546
|
-
* Time Complexity: O(log n)
|
|
10625
|
+
* Time Complexity: O(log n)
|
|
10547
10626
|
* Space Complexity: O(1)
|
|
10627
|
+
* on average (where n is the number of nodes in the tree)
|
|
10548
10628
|
*/
|
|
10549
10629
|
/**
|
|
10550
|
-
* Time Complexity: O(log n)
|
|
10630
|
+
* Time Complexity: O(log n)
|
|
10551
10631
|
* Space Complexity: O(1)
|
|
10552
10632
|
*
|
|
10553
10633
|
* The `delete` function removes a node from a binary tree based on a given identifier and updates
|
|
@@ -10615,20 +10695,11 @@ var dataStructureTyped = (() => {
|
|
|
10615
10695
|
return ans;
|
|
10616
10696
|
}
|
|
10617
10697
|
/**
|
|
10618
|
-
* Time Complexity: O(log n)
|
|
10619
|
-
* Space Complexity: O(1)
|
|
10620
|
-
*/
|
|
10621
|
-
isRealNode(node) {
|
|
10622
|
-
if (node === this.Sentinel || node === void 0)
|
|
10623
|
-
return false;
|
|
10624
|
-
return node instanceof RedBlackTreeNode;
|
|
10625
|
-
}
|
|
10626
|
-
/**
|
|
10627
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
10698
|
+
* Time Complexity: O(log n)
|
|
10628
10699
|
* Space Complexity: O(1)
|
|
10629
10700
|
*/
|
|
10630
10701
|
/**
|
|
10631
|
-
* Time Complexity: O(log n)
|
|
10702
|
+
* Time Complexity: O(log n)
|
|
10632
10703
|
* Space Complexity: O(1)
|
|
10633
10704
|
*
|
|
10634
10705
|
* The function `getNode` retrieves a single node from a binary tree based on a given identifier and
|
|
@@ -10680,7 +10751,7 @@ var dataStructureTyped = (() => {
|
|
|
10680
10751
|
return y;
|
|
10681
10752
|
}
|
|
10682
10753
|
/**
|
|
10683
|
-
* Time Complexity: O(
|
|
10754
|
+
* Time Complexity: O(1)
|
|
10684
10755
|
* Space Complexity: O(1)
|
|
10685
10756
|
*/
|
|
10686
10757
|
clear() {
|
|
@@ -10757,11 +10828,11 @@ var dataStructureTyped = (() => {
|
|
|
10757
10828
|
}
|
|
10758
10829
|
}
|
|
10759
10830
|
/**
|
|
10760
|
-
* Time Complexity: O(log n)
|
|
10831
|
+
* Time Complexity: O(log n)
|
|
10761
10832
|
* Space Complexity: O(1)
|
|
10762
10833
|
*/
|
|
10763
10834
|
/**
|
|
10764
|
-
* Time Complexity: O(log n)
|
|
10835
|
+
* Time Complexity: O(log n)
|
|
10765
10836
|
* Space Complexity: O(1)
|
|
10766
10837
|
*
|
|
10767
10838
|
* The function `_fixDelete` is used to fix the red-black tree after a node deletion.
|
|
@@ -10851,11 +10922,11 @@ var dataStructureTyped = (() => {
|
|
|
10851
10922
|
v.parent = u.parent;
|
|
10852
10923
|
}
|
|
10853
10924
|
/**
|
|
10854
|
-
* Time Complexity: O(log n)
|
|
10925
|
+
* Time Complexity: O(log n)
|
|
10855
10926
|
* Space Complexity: O(1)
|
|
10856
10927
|
*/
|
|
10857
10928
|
/**
|
|
10858
|
-
* Time Complexity: O(log n)
|
|
10929
|
+
* Time Complexity: O(log n)
|
|
10859
10930
|
* Space Complexity: O(1)
|
|
10860
10931
|
*
|
|
10861
10932
|
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
@@ -10938,11 +11009,11 @@ var dataStructureTyped = (() => {
|
|
|
10938
11009
|
}
|
|
10939
11010
|
};
|
|
10940
11011
|
var TreeMultimap = class _TreeMultimap extends AVLTree {
|
|
10941
|
-
constructor(
|
|
11012
|
+
constructor(nodes, options) {
|
|
10942
11013
|
super([], options);
|
|
10943
11014
|
__publicField(this, "_count", 0);
|
|
10944
|
-
if (
|
|
10945
|
-
this.addMany(
|
|
11015
|
+
if (nodes)
|
|
11016
|
+
this.addMany(nodes);
|
|
10946
11017
|
}
|
|
10947
11018
|
// TODO the _count is not accurate after nodes count modified
|
|
10948
11019
|
get count() {
|
|
@@ -10969,26 +11040,8 @@ var dataStructureTyped = (() => {
|
|
|
10969
11040
|
}, options));
|
|
10970
11041
|
}
|
|
10971
11042
|
/**
|
|
10972
|
-
* The function
|
|
10973
|
-
* @param
|
|
10974
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
10975
|
-
* class.
|
|
10976
|
-
*/
|
|
10977
|
-
isNode(exemplar) {
|
|
10978
|
-
return exemplar instanceof TreeMultimapNode;
|
|
10979
|
-
}
|
|
10980
|
-
/**
|
|
10981
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
10982
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
10983
|
-
* data type.
|
|
10984
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
10985
|
-
*/
|
|
10986
|
-
isNotNodeInstance(potentialKey) {
|
|
10987
|
-
return !(potentialKey instanceof TreeMultimapNode);
|
|
10988
|
-
}
|
|
10989
|
-
/**
|
|
10990
|
-
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
10991
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, which means it
|
|
11043
|
+
* The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
|
|
11044
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, which means it
|
|
10992
11045
|
* can be one of the following:
|
|
10993
11046
|
* @param {V} [value] - The `value` parameter is an optional argument that represents the value
|
|
10994
11047
|
* associated with the node. It is of type `V`, which can be any data type. If no value is provided,
|
|
@@ -10997,33 +11050,52 @@ var dataStructureTyped = (() => {
|
|
|
10997
11050
|
* times the value should be added to the node. If not provided, it defaults to 1.
|
|
10998
11051
|
* @returns a node of type `N` or `undefined`.
|
|
10999
11052
|
*/
|
|
11000
|
-
exemplarToNode(
|
|
11053
|
+
exemplarToNode(keyOrNodeOrEntry, value, count = 1) {
|
|
11001
11054
|
let node;
|
|
11002
|
-
if (
|
|
11055
|
+
if (keyOrNodeOrEntry === void 0 || keyOrNodeOrEntry === null) {
|
|
11003
11056
|
return;
|
|
11004
|
-
} else if (this.isNode(
|
|
11005
|
-
node =
|
|
11006
|
-
} else if (this.isEntry(
|
|
11007
|
-
const [key, value2] =
|
|
11057
|
+
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
11058
|
+
node = keyOrNodeOrEntry;
|
|
11059
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
11060
|
+
const [key, value2] = keyOrNodeOrEntry;
|
|
11008
11061
|
if (key === void 0 || key === null) {
|
|
11009
11062
|
return;
|
|
11010
11063
|
} else {
|
|
11011
11064
|
node = this.createNode(key, value2, count);
|
|
11012
11065
|
}
|
|
11013
|
-
} else if (this.isNotNodeInstance(
|
|
11014
|
-
node = this.createNode(
|
|
11066
|
+
} else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
11067
|
+
node = this.createNode(keyOrNodeOrEntry, value, count);
|
|
11015
11068
|
} else {
|
|
11016
11069
|
return;
|
|
11017
11070
|
}
|
|
11018
11071
|
return node;
|
|
11019
11072
|
}
|
|
11020
11073
|
/**
|
|
11021
|
-
*
|
|
11022
|
-
*
|
|
11074
|
+
* The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class.
|
|
11075
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
11076
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode
|
|
11077
|
+
* class.
|
|
11078
|
+
*/
|
|
11079
|
+
isNode(keyOrNodeOrEntry) {
|
|
11080
|
+
return keyOrNodeOrEntry instanceof TreeMultimapNode;
|
|
11081
|
+
}
|
|
11082
|
+
/**
|
|
11083
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
11084
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
11085
|
+
* data type.
|
|
11086
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
11087
|
+
*/
|
|
11088
|
+
isNotNodeInstance(potentialKey) {
|
|
11089
|
+
return !(potentialKey instanceof TreeMultimapNode);
|
|
11090
|
+
}
|
|
11091
|
+
/**
|
|
11092
|
+
* Time Complexity: O(log n)
|
|
11093
|
+
* Space Complexity: O(1)
|
|
11094
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
11023
11095
|
*/
|
|
11024
11096
|
/**
|
|
11025
|
-
* Time Complexity: O(log n)
|
|
11026
|
-
* Space Complexity: O(1)
|
|
11097
|
+
* Time Complexity: O(log n)
|
|
11098
|
+
* Space Complexity: O(1)
|
|
11027
11099
|
*
|
|
11028
11100
|
* The function overrides the add method of a binary tree node and adds a new node to the tree.
|
|
11029
11101
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
@@ -11040,21 +11112,22 @@ var dataStructureTyped = (() => {
|
|
|
11040
11112
|
add(keyOrNodeOrEntry, value, count = 1) {
|
|
11041
11113
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count);
|
|
11042
11114
|
if (newNode === void 0)
|
|
11043
|
-
return;
|
|
11115
|
+
return false;
|
|
11044
11116
|
const orgNodeCount = (newNode == null ? void 0 : newNode.count) || 0;
|
|
11045
11117
|
const inserted = super.add(newNode);
|
|
11046
11118
|
if (inserted) {
|
|
11047
11119
|
this._count += orgNodeCount;
|
|
11048
11120
|
}
|
|
11049
|
-
return
|
|
11121
|
+
return true;
|
|
11050
11122
|
}
|
|
11051
11123
|
/**
|
|
11052
|
-
* Time Complexity: O(k log n)
|
|
11053
|
-
* Space Complexity: O(1)
|
|
11124
|
+
* Time Complexity: O(k log n)
|
|
11125
|
+
* Space Complexity: O(1)
|
|
11126
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
11054
11127
|
*/
|
|
11055
11128
|
/**
|
|
11056
|
-
* Time Complexity: O(k log n)
|
|
11057
|
-
* Space Complexity: O(1)
|
|
11129
|
+
* Time Complexity: O(k log n)
|
|
11130
|
+
* Space Complexity: O(1)
|
|
11058
11131
|
*
|
|
11059
11132
|
* The function overrides the addMany method to add multiple keys, nodes, or entries to a data
|
|
11060
11133
|
* structure.
|
|
@@ -11066,12 +11139,13 @@ var dataStructureTyped = (() => {
|
|
|
11066
11139
|
return super.addMany(keysOrNodesOrEntries);
|
|
11067
11140
|
}
|
|
11068
11141
|
/**
|
|
11069
|
-
* Time Complexity: O(
|
|
11070
|
-
* Space Complexity: O(
|
|
11142
|
+
* Time Complexity: O(n log n)
|
|
11143
|
+
* Space Complexity: O(n)
|
|
11144
|
+
* logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node. linear space, as it creates an array to store the sorted nodes.
|
|
11071
11145
|
*/
|
|
11072
11146
|
/**
|
|
11073
|
-
* Time Complexity: O(n log n)
|
|
11074
|
-
* Space Complexity: O(n)
|
|
11147
|
+
* Time Complexity: O(n log n)
|
|
11148
|
+
* Space Complexity: O(n)
|
|
11075
11149
|
*
|
|
11076
11150
|
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
11077
11151
|
* tree using either a recursive or iterative approach.
|
|
@@ -11116,12 +11190,13 @@ var dataStructureTyped = (() => {
|
|
|
11116
11190
|
}
|
|
11117
11191
|
}
|
|
11118
11192
|
/**
|
|
11119
|
-
* Time Complexity: O(k log n)
|
|
11120
|
-
* Space Complexity: O(1)
|
|
11193
|
+
* Time Complexity: O(k log n)
|
|
11194
|
+
* Space Complexity: O(1)
|
|
11195
|
+
* logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each. constant space, as it doesn't use additional data structures that scale with input size.
|
|
11121
11196
|
*/
|
|
11122
11197
|
/**
|
|
11123
|
-
* Time Complexity: O(log n)
|
|
11124
|
-
* Space Complexity: O(1)
|
|
11198
|
+
* Time Complexity: O(k log n)
|
|
11199
|
+
* Space Complexity: O(1)
|
|
11125
11200
|
*
|
|
11126
11201
|
* The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
|
|
11127
11202
|
* account the count of the node and balancing the tree if necessary.
|
|
@@ -11191,10 +11266,13 @@ var dataStructureTyped = (() => {
|
|
|
11191
11266
|
return deletedResult;
|
|
11192
11267
|
}
|
|
11193
11268
|
/**
|
|
11194
|
-
* Time Complexity: O(
|
|
11195
|
-
* Space Complexity: O(
|
|
11269
|
+
* Time Complexity: O(1)
|
|
11270
|
+
* Space Complexity: O(1)
|
|
11196
11271
|
*/
|
|
11197
11272
|
/**
|
|
11273
|
+
* Time Complexity: O(1)
|
|
11274
|
+
* Space Complexity: O(1)
|
|
11275
|
+
*
|
|
11198
11276
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
11199
11277
|
*/
|
|
11200
11278
|
clear() {
|