data-structure-typed 1.46.8 → 1.47.0
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 +1 -1
- package/benchmark/report.html +1 -46
- package/benchmark/report.json +11 -422
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +4 -2
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +10 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +51 -13
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +153 -55
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +10 -9
- package/dist/cjs/data-structures/binary-tree/bst.js +16 -14
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +7 -5
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +32 -23
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +5 -3
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +11 -2
- 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 +5 -4
- package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +2 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +3 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.js +0 -6
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -1
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +2 -1
- package/dist/cjs/types/data-structures/binary-tree/tree-multimap.d.ts +2 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +4 -2
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +11 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +51 -13
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +154 -55
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +10 -9
- package/dist/mjs/data-structures/binary-tree/bst.js +17 -14
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +7 -5
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +34 -24
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +5 -3
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +12 -2
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/mjs/types/common.d.ts +5 -4
- package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +2 -1
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +3 -1
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.js +0 -6
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -1
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +2 -1
- package/dist/mjs/types/data-structures/binary-tree/tree-multimap.d.ts +2 -1
- package/dist/umd/data-structure-typed.js +230 -103
- package/dist/umd/data-structure-typed.min.js +4 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +5 -5
- package/src/data-structures/binary-tree/avl-tree.ts +17 -5
- package/src/data-structures/binary-tree/binary-tree.ts +175 -62
- package/src/data-structures/binary-tree/bst.ts +23 -19
- package/src/data-structures/binary-tree/rb-tree.ts +38 -27
- package/src/data-structures/binary-tree/tree-multimap.ts +19 -6
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +2 -5
- package/src/types/data-structures/binary-tree/avl-tree.ts +5 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +5 -7
- package/src/types/data-structures/binary-tree/bst.ts +3 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -1
- package/src/types/data-structures/binary-tree/tree-multimap.ts +3 -1
- package/test/integration/index.html +30 -28
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +7 -0
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +57 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +62 -7
- package/test/unit/data-structures/binary-tree/bst.test.ts +56 -1
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +98 -42
|
@@ -3,13 +3,26 @@ var dataStructureTyped = (() => {
|
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
6
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
9
|
var __knownSymbol = (name, symbol) => {
|
|
8
10
|
if (symbol = Symbol[name])
|
|
9
11
|
return symbol;
|
|
10
12
|
throw Error("Symbol." + name + " is not defined");
|
|
11
13
|
};
|
|
12
14
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
15
|
+
var __spreadValues = (a, b) => {
|
|
16
|
+
for (var prop in b || (b = {}))
|
|
17
|
+
if (__hasOwnProp.call(b, prop))
|
|
18
|
+
__defNormalProp(a, prop, b[prop]);
|
|
19
|
+
if (__getOwnPropSymbols)
|
|
20
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
21
|
+
if (__propIsEnum.call(b, prop))
|
|
22
|
+
__defNormalProp(a, prop, b[prop]);
|
|
23
|
+
}
|
|
24
|
+
return a;
|
|
25
|
+
};
|
|
13
26
|
var __export = (target, all) => {
|
|
14
27
|
for (var name in all)
|
|
15
28
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -113,7 +126,6 @@ var dataStructureTyped = (() => {
|
|
|
113
126
|
HashTable: () => HashTable,
|
|
114
127
|
HashTableNode: () => HashTableNode,
|
|
115
128
|
Heap: () => Heap,
|
|
116
|
-
IterateDirection: () => IterateDirection,
|
|
117
129
|
IterationType: () => IterationType,
|
|
118
130
|
LinkedListQueue: () => LinkedListQueue,
|
|
119
131
|
MapEdge: () => MapEdge,
|
|
@@ -6511,10 +6523,10 @@ var dataStructureTyped = (() => {
|
|
|
6511
6523
|
};
|
|
6512
6524
|
|
|
6513
6525
|
// src/types/data-structures/binary-tree/binary-tree.ts
|
|
6514
|
-
var IterationType = /* @__PURE__ */ ((
|
|
6515
|
-
|
|
6516
|
-
|
|
6517
|
-
return
|
|
6526
|
+
var IterationType = /* @__PURE__ */ ((IterationType2) => {
|
|
6527
|
+
IterationType2["ITERATIVE"] = "ITERATIVE";
|
|
6528
|
+
IterationType2["RECURSIVE"] = "RECURSIVE";
|
|
6529
|
+
return IterationType2;
|
|
6518
6530
|
})(IterationType || {});
|
|
6519
6531
|
var FamilyPosition = /* @__PURE__ */ ((FamilyPosition2) => {
|
|
6520
6532
|
FamilyPosition2["ROOT"] = "ROOT";
|
|
@@ -6541,11 +6553,6 @@ var dataStructureTyped = (() => {
|
|
|
6541
6553
|
CP2["gt"] = "gt";
|
|
6542
6554
|
return CP2;
|
|
6543
6555
|
})(CP || {});
|
|
6544
|
-
var IterateDirection = /* @__PURE__ */ ((IterateDirection2) => {
|
|
6545
|
-
IterateDirection2[IterateDirection2["DEFAULT"] = 0] = "DEFAULT";
|
|
6546
|
-
IterateDirection2[IterateDirection2["REVERSE"] = 1] = "REVERSE";
|
|
6547
|
-
return IterateDirection2;
|
|
6548
|
-
})(IterateDirection || {});
|
|
6549
6556
|
|
|
6550
6557
|
// src/data-structures/binary-tree/binary-tree.ts
|
|
6551
6558
|
var BinaryTreeNode = class {
|
|
@@ -6621,19 +6628,20 @@ var dataStructureTyped = (() => {
|
|
|
6621
6628
|
return "MAL_NODE" /* MAL_NODE */;
|
|
6622
6629
|
}
|
|
6623
6630
|
};
|
|
6624
|
-
var BinaryTree = class {
|
|
6631
|
+
var BinaryTree = class _BinaryTree {
|
|
6625
6632
|
/**
|
|
6626
6633
|
* Creates a new instance of BinaryTree.
|
|
6627
6634
|
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
|
6628
6635
|
*/
|
|
6629
6636
|
constructor(options) {
|
|
6630
|
-
__publicField(this, "
|
|
6637
|
+
__publicField(this, "options");
|
|
6631
6638
|
__publicField(this, "_root");
|
|
6632
6639
|
__publicField(this, "_size");
|
|
6633
6640
|
__publicField(this, "_defaultOneParamCallback", (node) => node.key);
|
|
6634
6641
|
if (options) {
|
|
6635
|
-
|
|
6636
|
-
|
|
6642
|
+
this.options = __spreadValues({ iterationType: "ITERATIVE" /* ITERATIVE */ }, options);
|
|
6643
|
+
} else {
|
|
6644
|
+
this.options = { iterationType: "ITERATIVE" /* ITERATIVE */ };
|
|
6637
6645
|
}
|
|
6638
6646
|
this._size = 0;
|
|
6639
6647
|
}
|
|
@@ -6658,6 +6666,9 @@ var dataStructureTyped = (() => {
|
|
|
6658
6666
|
createNode(key, value) {
|
|
6659
6667
|
return new BinaryTreeNode(key, value);
|
|
6660
6668
|
}
|
|
6669
|
+
createTree(options) {
|
|
6670
|
+
return new _BinaryTree(__spreadValues(__spreadValues({}, this.options), options));
|
|
6671
|
+
}
|
|
6661
6672
|
/**
|
|
6662
6673
|
* Time Complexity: O(n)
|
|
6663
6674
|
* Space Complexity: O(1)
|
|
@@ -6879,7 +6890,7 @@ var dataStructureTyped = (() => {
|
|
|
6879
6890
|
* values:
|
|
6880
6891
|
* @returns the height of the binary tree.
|
|
6881
6892
|
*/
|
|
6882
|
-
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
6893
|
+
getHeight(beginRoot = this.root, iterationType = this.options.iterationType) {
|
|
6883
6894
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
6884
6895
|
if (!beginRoot)
|
|
6885
6896
|
return -1;
|
|
@@ -6924,7 +6935,7 @@ var dataStructureTyped = (() => {
|
|
|
6924
6935
|
* to calculate the minimum height of a binary tree. It can have two possible values:
|
|
6925
6936
|
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
|
|
6926
6937
|
*/
|
|
6927
|
-
getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
6938
|
+
getMinHeight(beginRoot = this.root, iterationType = this.options.iterationType) {
|
|
6928
6939
|
var _a, _b, _c;
|
|
6929
6940
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
6930
6941
|
if (!beginRoot)
|
|
@@ -7013,7 +7024,7 @@ var dataStructureTyped = (() => {
|
|
|
7013
7024
|
* traverse the binary tree. It can have two possible values:
|
|
7014
7025
|
* @returns an array of nodes of type `N`.
|
|
7015
7026
|
*/
|
|
7016
|
-
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
7027
|
+
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.options.iterationType) {
|
|
7017
7028
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
7018
7029
|
callback = (node) => node;
|
|
7019
7030
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -7074,7 +7085,7 @@ var dataStructureTyped = (() => {
|
|
|
7074
7085
|
* be performed in a pre-order, in-order, or post-order manner.
|
|
7075
7086
|
* @returns a boolean value.
|
|
7076
7087
|
*/
|
|
7077
|
-
has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
7088
|
+
has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType) {
|
|
7078
7089
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
7079
7090
|
callback = (node) => node;
|
|
7080
7091
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
@@ -7104,7 +7115,7 @@ var dataStructureTyped = (() => {
|
|
|
7104
7115
|
* nodes are visited during the search.
|
|
7105
7116
|
* @returns a value of type `N | null | undefined`.
|
|
7106
7117
|
*/
|
|
7107
|
-
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
7118
|
+
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType) {
|
|
7108
7119
|
var _a;
|
|
7109
7120
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
7110
7121
|
callback = (node) => node;
|
|
@@ -7196,7 +7207,7 @@ var dataStructureTyped = (() => {
|
|
|
7196
7207
|
* @returns The value of the node with the given identifier is being returned. If the node is not
|
|
7197
7208
|
* found, `undefined` is returned.
|
|
7198
7209
|
*/
|
|
7199
|
-
get(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
7210
|
+
get(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType) {
|
|
7200
7211
|
var _a, _b;
|
|
7201
7212
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
7202
7213
|
callback = (node) => node;
|
|
@@ -7264,7 +7275,7 @@ var dataStructureTyped = (() => {
|
|
|
7264
7275
|
* @returns The function `getLeftMost` returns the leftmost node (`N`) in the binary tree. If there
|
|
7265
7276
|
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
7266
7277
|
*/
|
|
7267
|
-
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
7278
|
+
getLeftMost(beginRoot = this.root, iterationType = this.options.iterationType) {
|
|
7268
7279
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
7269
7280
|
if (!beginRoot)
|
|
7270
7281
|
return beginRoot;
|
|
@@ -7303,7 +7314,7 @@ var dataStructureTyped = (() => {
|
|
|
7303
7314
|
* @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If there
|
|
7304
7315
|
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
7305
7316
|
*/
|
|
7306
|
-
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
7317
|
+
getRightMost(beginRoot = this.root, iterationType = this.options.iterationType) {
|
|
7307
7318
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
7308
7319
|
if (!beginRoot)
|
|
7309
7320
|
return beginRoot;
|
|
@@ -7339,7 +7350,7 @@ var dataStructureTyped = (() => {
|
|
|
7339
7350
|
* possible values:
|
|
7340
7351
|
* @returns a boolean value.
|
|
7341
7352
|
*/
|
|
7342
|
-
isSubtreeBST(beginRoot, iterationType = this.iterationType) {
|
|
7353
|
+
isSubtreeBST(beginRoot, iterationType = this.options.iterationType) {
|
|
7343
7354
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
7344
7355
|
if (!beginRoot)
|
|
7345
7356
|
return true;
|
|
@@ -7384,7 +7395,7 @@ var dataStructureTyped = (() => {
|
|
|
7384
7395
|
* expected to be
|
|
7385
7396
|
* @returns a boolean value.
|
|
7386
7397
|
*/
|
|
7387
|
-
isBST(iterationType = this.iterationType) {
|
|
7398
|
+
isBST(iterationType = this.options.iterationType) {
|
|
7388
7399
|
if (this.root === null)
|
|
7389
7400
|
return true;
|
|
7390
7401
|
return this.isSubtreeBST(this.root, iterationType);
|
|
@@ -7409,13 +7420,13 @@ var dataStructureTyped = (() => {
|
|
|
7409
7420
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
7410
7421
|
* performed on the subtree. It can have two possible values:
|
|
7411
7422
|
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
7412
|
-
* whether
|
|
7423
|
+
* whether to include null values in the traversal. If `includeNull` is set to `true`, the
|
|
7413
7424
|
* traversal will include null values, otherwise it will skip them.
|
|
7414
7425
|
* @returns The function `subTreeTraverse` returns an array of values that are the result of invoking
|
|
7415
7426
|
* the `callback` function on each node in the subtree. The type of the array elements is determined
|
|
7416
7427
|
* by the return type of the `callback` function.
|
|
7417
7428
|
*/
|
|
7418
|
-
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
7429
|
+
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType, includeNull = false) {
|
|
7419
7430
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
7420
7431
|
const ans = [];
|
|
7421
7432
|
if (!beginRoot)
|
|
@@ -7632,12 +7643,12 @@ var dataStructureTyped = (() => {
|
|
|
7632
7643
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
7633
7644
|
* performed during the breadth-first search (BFS). It can have two possible values:
|
|
7634
7645
|
* @param [includeNull=false] - The `includeNull` parameter is a boolean flag that determines whether
|
|
7635
|
-
*
|
|
7646
|
+
* to include null values in the breadth-first search traversal. If `includeNull` is set to
|
|
7636
7647
|
* `true`, null values will be included in the traversal, otherwise they will be skipped.
|
|
7637
7648
|
* @returns an array of values that are the result of invoking the callback function on each node in
|
|
7638
7649
|
* the breadth-first traversal of a binary tree.
|
|
7639
7650
|
*/
|
|
7640
|
-
bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
7651
|
+
bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType, includeNull = false) {
|
|
7641
7652
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
7642
7653
|
if (!beginRoot)
|
|
7643
7654
|
return [];
|
|
@@ -7706,12 +7717,12 @@ var dataStructureTyped = (() => {
|
|
|
7706
7717
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
7707
7718
|
* performed on the tree. It can have two possible values:
|
|
7708
7719
|
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
7709
|
-
* whether
|
|
7720
|
+
* whether to include null values in the resulting levels. If `includeNull` is set to `true`,
|
|
7710
7721
|
* null values will be included in the levels. If `includeNull` is set to `false`, null values will
|
|
7711
7722
|
* be excluded
|
|
7712
7723
|
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
7713
7724
|
*/
|
|
7714
|
-
listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
7725
|
+
listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType, includeNull = false) {
|
|
7715
7726
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
7716
7727
|
const levelsNodes = [];
|
|
7717
7728
|
if (!beginRoot)
|
|
@@ -7902,6 +7913,74 @@ var dataStructureTyped = (() => {
|
|
|
7902
7913
|
}
|
|
7903
7914
|
return ans;
|
|
7904
7915
|
}
|
|
7916
|
+
/**
|
|
7917
|
+
* The `forEach` function iterates over each entry in a tree and calls a callback function with the
|
|
7918
|
+
* entry and the tree as arguments.
|
|
7919
|
+
* @param callback - The callback parameter is a function that will be called for each entry in the
|
|
7920
|
+
* tree. It takes two parameters: entry and tree.
|
|
7921
|
+
*/
|
|
7922
|
+
forEach(callback) {
|
|
7923
|
+
for (const entry of this) {
|
|
7924
|
+
callback(entry, this);
|
|
7925
|
+
}
|
|
7926
|
+
}
|
|
7927
|
+
/**
|
|
7928
|
+
* The `filter` function creates a new tree by iterating over the entries of the current tree and
|
|
7929
|
+
* adding the entries that satisfy the given predicate.
|
|
7930
|
+
* @param predicate - The `predicate` parameter is a function that takes two arguments: `entry` and
|
|
7931
|
+
* `tree`.
|
|
7932
|
+
* @returns The `filter` method is returning a new tree object that contains only the entries that
|
|
7933
|
+
* satisfy the given predicate function.
|
|
7934
|
+
*/
|
|
7935
|
+
filter(predicate) {
|
|
7936
|
+
const newTree = this.createTree();
|
|
7937
|
+
for (const [key, value] of this) {
|
|
7938
|
+
if (predicate([key, value], this)) {
|
|
7939
|
+
newTree.add(key, value);
|
|
7940
|
+
}
|
|
7941
|
+
}
|
|
7942
|
+
return newTree;
|
|
7943
|
+
}
|
|
7944
|
+
// TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
|
|
7945
|
+
// map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
|
|
7946
|
+
// const newTree = this.createTree();
|
|
7947
|
+
// for (const [key, value] of this) {
|
|
7948
|
+
// newTree.add(key, callback([key, value], this));
|
|
7949
|
+
// }
|
|
7950
|
+
// return newTree;
|
|
7951
|
+
// }
|
|
7952
|
+
/**
|
|
7953
|
+
* The `map` function creates a new tree by applying a callback function to each entry in the current
|
|
7954
|
+
* tree.
|
|
7955
|
+
* @param callback - The callback parameter is a function that takes two arguments: entry and tree.
|
|
7956
|
+
* @returns The `map` method is returning a new tree object.
|
|
7957
|
+
*/
|
|
7958
|
+
map(callback) {
|
|
7959
|
+
const newTree = this.createTree();
|
|
7960
|
+
for (const [key, value] of this) {
|
|
7961
|
+
newTree.add(key, callback([key, value], this));
|
|
7962
|
+
}
|
|
7963
|
+
return newTree;
|
|
7964
|
+
}
|
|
7965
|
+
/**
|
|
7966
|
+
* The `reduce` function iterates over the entries of a tree and applies a callback function to each
|
|
7967
|
+
* entry, accumulating a single value.
|
|
7968
|
+
* @param callback - The callback parameter is a function that takes three arguments: accumulator,
|
|
7969
|
+
* entry, and tree. It is called for each entry in the tree and is used to accumulate a single value
|
|
7970
|
+
* based on the logic defined in the callback function.
|
|
7971
|
+
* @param {T} initialValue - The initialValue parameter is the initial value of the accumulator. It
|
|
7972
|
+
* is the value that will be passed as the first argument to the callback function when reducing the
|
|
7973
|
+
* elements of the tree.
|
|
7974
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
7975
|
+
* all the entries in the tree and applying the callback function to each entry.
|
|
7976
|
+
*/
|
|
7977
|
+
reduce(callback, initialValue) {
|
|
7978
|
+
let accumulator = initialValue;
|
|
7979
|
+
for (const [key, value] of this) {
|
|
7980
|
+
accumulator = callback(accumulator, [key, value], this);
|
|
7981
|
+
}
|
|
7982
|
+
return accumulator;
|
|
7983
|
+
}
|
|
7905
7984
|
/**
|
|
7906
7985
|
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
7907
7986
|
* either an iterative or recursive manner.
|
|
@@ -7912,71 +7991,90 @@ var dataStructureTyped = (() => {
|
|
|
7912
7991
|
* binary tree nodes in a specific order.
|
|
7913
7992
|
*/
|
|
7914
7993
|
*[Symbol.iterator](node = this.root) {
|
|
7915
|
-
if (!node)
|
|
7994
|
+
if (!node)
|
|
7916
7995
|
return;
|
|
7917
|
-
|
|
7918
|
-
if (this.iterationType === "ITERATIVE" /* ITERATIVE */) {
|
|
7996
|
+
if (this.options.iterationType === "ITERATIVE" /* ITERATIVE */) {
|
|
7919
7997
|
const stack = [];
|
|
7920
7998
|
let current = node;
|
|
7921
7999
|
while (current || stack.length > 0) {
|
|
7922
|
-
while (current) {
|
|
8000
|
+
while (current && !isNaN(current.key)) {
|
|
7923
8001
|
stack.push(current);
|
|
7924
8002
|
current = current.left;
|
|
7925
8003
|
}
|
|
7926
8004
|
current = stack.pop();
|
|
7927
|
-
if (current)
|
|
7928
|
-
yield current.key;
|
|
7929
|
-
if (current)
|
|
8005
|
+
if (current && !isNaN(current.key)) {
|
|
8006
|
+
yield [current.key, current.value];
|
|
7930
8007
|
current = current.right;
|
|
8008
|
+
}
|
|
7931
8009
|
}
|
|
7932
8010
|
} else {
|
|
7933
|
-
if (node.left) {
|
|
8011
|
+
if (node.left && !isNaN(node.key)) {
|
|
7934
8012
|
yield* __yieldStar(this[Symbol.iterator](node.left));
|
|
7935
8013
|
}
|
|
7936
|
-
yield node.key;
|
|
7937
|
-
if (node.right) {
|
|
8014
|
+
yield [node.key, node.value];
|
|
8015
|
+
if (node.right && !isNaN(node.key)) {
|
|
7938
8016
|
yield* __yieldStar(this[Symbol.iterator](node.right));
|
|
7939
8017
|
}
|
|
7940
8018
|
}
|
|
7941
8019
|
}
|
|
7942
8020
|
/**
|
|
7943
8021
|
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
|
7944
|
-
* @param {N | null | undefined} beginRoot - The `root` parameter is of type `BTNKey | N | null |
|
|
8022
|
+
* @param {BTNKey | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `BTNKey | N | null |
|
|
7945
8023
|
* undefined`. It represents the root node of a binary tree. The root node can have one of the
|
|
7946
8024
|
* following types:
|
|
8025
|
+
* @param {BinaryTreePrintOptions} [options={ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false}] - Options object that controls printing behavior. You can specify whether to display undefined, null, or sentinel nodes.
|
|
7947
8026
|
*/
|
|
7948
|
-
print(beginRoot = this.root) {
|
|
8027
|
+
print(beginRoot = this.root, options) {
|
|
8028
|
+
const opts = __spreadValues({ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false }, options);
|
|
7949
8029
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
7950
8030
|
if (!beginRoot)
|
|
7951
8031
|
return;
|
|
8032
|
+
if (opts.isShowUndefined)
|
|
8033
|
+
console.log(`U for undefined
|
|
8034
|
+
`);
|
|
8035
|
+
if (opts.isShowNull)
|
|
8036
|
+
console.log(`N for null
|
|
8037
|
+
`);
|
|
8038
|
+
if (opts.isShowRedBlackNIL)
|
|
8039
|
+
console.log(`S for Sentinel Node
|
|
8040
|
+
`);
|
|
7952
8041
|
const display = (root) => {
|
|
7953
|
-
const [lines, , ,] = this._displayAux(root);
|
|
8042
|
+
const [lines, , ,] = this._displayAux(root, opts);
|
|
7954
8043
|
for (const line of lines) {
|
|
7955
8044
|
console.log(line);
|
|
7956
8045
|
}
|
|
7957
8046
|
};
|
|
7958
8047
|
display(beginRoot);
|
|
7959
8048
|
}
|
|
7960
|
-
_displayAux(node) {
|
|
7961
|
-
|
|
7962
|
-
|
|
7963
|
-
|
|
7964
|
-
|
|
7965
|
-
|
|
7966
|
-
|
|
7967
|
-
|
|
8049
|
+
_displayAux(node, options) {
|
|
8050
|
+
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
8051
|
+
const emptyDisplayLayout = [["\u2500"], 1, 0, 0];
|
|
8052
|
+
if (node === null && !isShowNull) {
|
|
8053
|
+
return emptyDisplayLayout;
|
|
8054
|
+
} else if (node === void 0 && !isShowUndefined) {
|
|
8055
|
+
return emptyDisplayLayout;
|
|
8056
|
+
} else if (node !== null && node !== void 0 && isNaN(node.key) && !isShowRedBlackNIL) {
|
|
8057
|
+
return emptyDisplayLayout;
|
|
8058
|
+
} else if (node !== null && node !== void 0) {
|
|
8059
|
+
const key = node.key, line = isNaN(key) ? "S" : key.toString(), width = line.length;
|
|
8060
|
+
return _buildNodeDisplay(line, width, this._displayAux(node.left, options), this._displayAux(node.right, options));
|
|
8061
|
+
} else {
|
|
8062
|
+
const line = node === void 0 ? "U" : "N", width = line.length;
|
|
8063
|
+
return _buildNodeDisplay(line, width, [[""], 1, 0, 0], [[""], 1, 0, 0]);
|
|
7968
8064
|
}
|
|
7969
|
-
|
|
7970
|
-
|
|
7971
|
-
|
|
7972
|
-
|
|
7973
|
-
|
|
7974
|
-
|
|
7975
|
-
|
|
7976
|
-
|
|
7977
|
-
|
|
8065
|
+
function _buildNodeDisplay(line, width, left, right) {
|
|
8066
|
+
const [leftLines, leftWidth, leftHeight, leftMiddle] = left;
|
|
8067
|
+
const [rightLines, rightWidth, rightHeight, rightMiddle] = right;
|
|
8068
|
+
const firstLine = " ".repeat(Math.max(0, leftMiddle + 1)) + "_".repeat(Math.max(0, leftWidth - leftMiddle - 1)) + line + "_".repeat(Math.max(0, rightMiddle)) + " ".repeat(Math.max(0, rightWidth - rightMiddle));
|
|
8069
|
+
const secondLine = (leftHeight > 0 ? " ".repeat(leftMiddle) + "/" + " ".repeat(leftWidth - leftMiddle - 1) : " ".repeat(leftWidth)) + " ".repeat(width) + (rightHeight > 0 ? " ".repeat(rightMiddle) + "\\" + " ".repeat(rightWidth - rightMiddle - 1) : " ".repeat(rightWidth));
|
|
8070
|
+
const mergedLines = [firstLine, secondLine];
|
|
8071
|
+
for (let i = 0; i < Math.max(leftHeight, rightHeight); i++) {
|
|
8072
|
+
const leftLine = i < leftHeight ? leftLines[i] : " ".repeat(leftWidth);
|
|
8073
|
+
const rightLine = i < rightHeight ? rightLines[i] : " ".repeat(rightWidth);
|
|
8074
|
+
mergedLines.push(leftLine + " ".repeat(width) + rightLine);
|
|
8075
|
+
}
|
|
8076
|
+
return [mergedLines, leftWidth + width + rightWidth, Math.max(leftHeight, rightHeight) + 2, leftWidth + Math.floor(width / 2)];
|
|
7978
8077
|
}
|
|
7979
|
-
return [mergedLines, leftWidth + width + rightWidth, Math.max(leftHeight, rightHeight) + 2, leftWidth + Math.floor(width / 2)];
|
|
7980
8078
|
}
|
|
7981
8079
|
/**
|
|
7982
8080
|
* Swap the data of two nodes in the binary tree.
|
|
@@ -8092,7 +8190,7 @@ var dataStructureTyped = (() => {
|
|
|
8092
8190
|
this._right = v;
|
|
8093
8191
|
}
|
|
8094
8192
|
};
|
|
8095
|
-
var BST = class extends BinaryTree {
|
|
8193
|
+
var BST = class _BST extends BinaryTree {
|
|
8096
8194
|
/**
|
|
8097
8195
|
* The constructor function initializes a binary search tree with an optional comparator function.
|
|
8098
8196
|
* @param {BSTOptions} [options] - An optional object that contains additional configuration options
|
|
@@ -8100,15 +8198,14 @@ var dataStructureTyped = (() => {
|
|
|
8100
8198
|
*/
|
|
8101
8199
|
constructor(options) {
|
|
8102
8200
|
super(options);
|
|
8201
|
+
__publicField(this, "options");
|
|
8103
8202
|
__publicField(this, "_root");
|
|
8104
|
-
|
|
8105
|
-
|
|
8106
|
-
|
|
8107
|
-
|
|
8108
|
-
if (comparator !== void 0) {
|
|
8109
|
-
this._comparator = comparator;
|
|
8110
|
-
}
|
|
8203
|
+
if (options) {
|
|
8204
|
+
this.options = __spreadValues({ iterationType: "ITERATIVE" /* ITERATIVE */, comparator: (a, b) => a - b }, options);
|
|
8205
|
+
} else {
|
|
8206
|
+
this.options = { iterationType: "ITERATIVE" /* ITERATIVE */, comparator: (a, b) => a - b };
|
|
8111
8207
|
}
|
|
8208
|
+
this._root = void 0;
|
|
8112
8209
|
}
|
|
8113
8210
|
/**
|
|
8114
8211
|
* Get the root node of the binary tree.
|
|
@@ -8127,6 +8224,9 @@ var dataStructureTyped = (() => {
|
|
|
8127
8224
|
createNode(key, value) {
|
|
8128
8225
|
return new BSTNode(key, value);
|
|
8129
8226
|
}
|
|
8227
|
+
createTree(options) {
|
|
8228
|
+
return new _BST(__spreadValues(__spreadValues({}, this.options), options));
|
|
8229
|
+
}
|
|
8130
8230
|
/**
|
|
8131
8231
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
8132
8232
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -8228,7 +8328,7 @@ var dataStructureTyped = (() => {
|
|
|
8228
8328
|
* current instance of the binary search tree
|
|
8229
8329
|
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
8230
8330
|
*/
|
|
8231
|
-
addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
8331
|
+
addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.options.iterationType) {
|
|
8232
8332
|
function hasNoUndefined(arr) {
|
|
8233
8333
|
return arr.indexOf(void 0) === -1;
|
|
8234
8334
|
}
|
|
@@ -8314,7 +8414,7 @@ var dataStructureTyped = (() => {
|
|
|
8314
8414
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
8315
8415
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
8316
8416
|
*/
|
|
8317
|
-
lastKey(beginRoot = this.root, iterationType = this.iterationType) {
|
|
8417
|
+
lastKey(beginRoot = this.root, iterationType = this.options.iterationType) {
|
|
8318
8418
|
var _a, _b, _c, _d, _e, _f;
|
|
8319
8419
|
if (this._compare(0, 1) === "lt" /* lt */)
|
|
8320
8420
|
return (_b = (_a = this.getRightMost(beginRoot, iterationType)) == null ? void 0 : _a.key) != null ? _b : 0;
|
|
@@ -8410,7 +8510,7 @@ var dataStructureTyped = (() => {
|
|
|
8410
8510
|
* performed on the binary tree. It can have two possible values:
|
|
8411
8511
|
* @returns The method returns an array of nodes (`N[]`).
|
|
8412
8512
|
*/
|
|
8413
|
-
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
8513
|
+
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.options.iterationType) {
|
|
8414
8514
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
8415
8515
|
if (!beginRoot)
|
|
8416
8516
|
return [];
|
|
@@ -8486,7 +8586,7 @@ var dataStructureTyped = (() => {
|
|
|
8486
8586
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
8487
8587
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
8488
8588
|
*/
|
|
8489
|
-
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = "lt" /* lt */, targetNode = this.root, iterationType = this.iterationType) {
|
|
8589
|
+
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = "lt" /* lt */, targetNode = this.root, iterationType = this.options.iterationType) {
|
|
8490
8590
|
targetNode = this.ensureNotKey(targetNode);
|
|
8491
8591
|
const ans = [];
|
|
8492
8592
|
if (!targetNode)
|
|
@@ -8549,7 +8649,7 @@ var dataStructureTyped = (() => {
|
|
|
8549
8649
|
* values:
|
|
8550
8650
|
* @returns The function `perfectlyBalance` returns a boolean value.
|
|
8551
8651
|
*/
|
|
8552
|
-
perfectlyBalance(iterationType = this.iterationType) {
|
|
8652
|
+
perfectlyBalance(iterationType = this.options.iterationType) {
|
|
8553
8653
|
const sorted = this.dfs((node) => node, "in"), n = sorted.length;
|
|
8554
8654
|
this.clear();
|
|
8555
8655
|
if (sorted.length < 1)
|
|
@@ -8598,7 +8698,7 @@ var dataStructureTyped = (() => {
|
|
|
8598
8698
|
* to check if the AVL tree is balanced. It can have two possible values:
|
|
8599
8699
|
* @returns a boolean value.
|
|
8600
8700
|
*/
|
|
8601
|
-
isAVLBalanced(iterationType = this.iterationType) {
|
|
8701
|
+
isAVLBalanced(iterationType = this.options.iterationType) {
|
|
8602
8702
|
var _a, _b;
|
|
8603
8703
|
if (!this.root)
|
|
8604
8704
|
return true;
|
|
@@ -8656,7 +8756,7 @@ var dataStructureTyped = (() => {
|
|
|
8656
8756
|
* than), CP.lt (less than), or CP.eq (equal).
|
|
8657
8757
|
*/
|
|
8658
8758
|
_compare(a, b) {
|
|
8659
|
-
const compared = this.
|
|
8759
|
+
const compared = this.options.comparator(a, b);
|
|
8660
8760
|
if (compared > 0)
|
|
8661
8761
|
return "gt" /* gt */;
|
|
8662
8762
|
else if (compared < 0)
|
|
@@ -9100,7 +9200,7 @@ var dataStructureTyped = (() => {
|
|
|
9100
9200
|
this.height = 0;
|
|
9101
9201
|
}
|
|
9102
9202
|
};
|
|
9103
|
-
var AVLTree = class extends BST {
|
|
9203
|
+
var AVLTree = class _AVLTree extends BST {
|
|
9104
9204
|
/**
|
|
9105
9205
|
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
9106
9206
|
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
@@ -9109,6 +9209,12 @@ var dataStructureTyped = (() => {
|
|
|
9109
9209
|
*/
|
|
9110
9210
|
constructor(options) {
|
|
9111
9211
|
super(options);
|
|
9212
|
+
__publicField(this, "options");
|
|
9213
|
+
if (options) {
|
|
9214
|
+
this.options = __spreadValues({ iterationType: "ITERATIVE" /* ITERATIVE */, comparator: (a, b) => a - b }, options);
|
|
9215
|
+
} else {
|
|
9216
|
+
this.options = { iterationType: "ITERATIVE" /* ITERATIVE */, comparator: (a, b) => a - b };
|
|
9217
|
+
}
|
|
9112
9218
|
}
|
|
9113
9219
|
/**
|
|
9114
9220
|
* The function creates a new AVL tree node with the specified key and value.
|
|
@@ -9122,6 +9228,9 @@ var dataStructureTyped = (() => {
|
|
|
9122
9228
|
createNode(key, value) {
|
|
9123
9229
|
return new AVLTreeNode(key, value);
|
|
9124
9230
|
}
|
|
9231
|
+
createTree(options) {
|
|
9232
|
+
return new _AVLTree(__spreadValues(__spreadValues({}, this.options), options));
|
|
9233
|
+
}
|
|
9125
9234
|
/**
|
|
9126
9235
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
9127
9236
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -9485,7 +9594,7 @@ var dataStructureTyped = (() => {
|
|
|
9485
9594
|
this.color = color;
|
|
9486
9595
|
}
|
|
9487
9596
|
};
|
|
9488
|
-
var RedBlackTree = class extends BST {
|
|
9597
|
+
var RedBlackTree = class _RedBlackTree extends BST {
|
|
9489
9598
|
/**
|
|
9490
9599
|
* The constructor function initializes a Red-Black Tree with an optional set of options.
|
|
9491
9600
|
* @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
@@ -9493,10 +9602,16 @@ var dataStructureTyped = (() => {
|
|
|
9493
9602
|
*/
|
|
9494
9603
|
constructor(options) {
|
|
9495
9604
|
super(options);
|
|
9496
|
-
__publicField(this, "
|
|
9605
|
+
__publicField(this, "Sentinel", new RedBlackTreeNode(NaN));
|
|
9606
|
+
__publicField(this, "options");
|
|
9497
9607
|
__publicField(this, "_root");
|
|
9498
9608
|
__publicField(this, "_size", 0);
|
|
9499
|
-
|
|
9609
|
+
if (options) {
|
|
9610
|
+
this.options = __spreadValues({ iterationType: "ITERATIVE" /* ITERATIVE */, comparator: (a, b) => a - b }, options);
|
|
9611
|
+
} else {
|
|
9612
|
+
this.options = { iterationType: "ITERATIVE" /* ITERATIVE */, comparator: (a, b) => a - b };
|
|
9613
|
+
}
|
|
9614
|
+
this._root = this.Sentinel;
|
|
9500
9615
|
}
|
|
9501
9616
|
get root() {
|
|
9502
9617
|
return this._root;
|
|
@@ -9504,6 +9619,12 @@ var dataStructureTyped = (() => {
|
|
|
9504
9619
|
get size() {
|
|
9505
9620
|
return this._size;
|
|
9506
9621
|
}
|
|
9622
|
+
createNode(key, value, color = 0 /* BLACK */) {
|
|
9623
|
+
return new RedBlackTreeNode(key, value, color);
|
|
9624
|
+
}
|
|
9625
|
+
createTree(options) {
|
|
9626
|
+
return new _RedBlackTree(__spreadValues(__spreadValues({}, this.options), options));
|
|
9627
|
+
}
|
|
9507
9628
|
/**
|
|
9508
9629
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
9509
9630
|
* Space Complexity: O(1)
|
|
@@ -9532,11 +9653,11 @@ var dataStructureTyped = (() => {
|
|
|
9532
9653
|
} else {
|
|
9533
9654
|
return;
|
|
9534
9655
|
}
|
|
9535
|
-
node.left = this.
|
|
9536
|
-
node.right = this.
|
|
9656
|
+
node.left = this.Sentinel;
|
|
9657
|
+
node.right = this.Sentinel;
|
|
9537
9658
|
let y = void 0;
|
|
9538
9659
|
let x = this.root;
|
|
9539
|
-
while (x !== this.
|
|
9660
|
+
while (x !== this.Sentinel) {
|
|
9540
9661
|
y = x;
|
|
9541
9662
|
if (x) {
|
|
9542
9663
|
if (node.key < x.key) {
|
|
@@ -9568,9 +9689,6 @@ var dataStructureTyped = (() => {
|
|
|
9568
9689
|
this._fixInsert(node);
|
|
9569
9690
|
this._size++;
|
|
9570
9691
|
}
|
|
9571
|
-
createNode(key, value, color = 0 /* BLACK */) {
|
|
9572
|
-
return new RedBlackTreeNode(key, value, color);
|
|
9573
|
-
}
|
|
9574
9692
|
/**
|
|
9575
9693
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
9576
9694
|
* Space Complexity: O(1)
|
|
@@ -9595,9 +9713,9 @@ var dataStructureTyped = (() => {
|
|
|
9595
9713
|
if (identifier === null)
|
|
9596
9714
|
return ans;
|
|
9597
9715
|
const helper = (node) => {
|
|
9598
|
-
let z = this.
|
|
9716
|
+
let z = this.Sentinel;
|
|
9599
9717
|
let x, y;
|
|
9600
|
-
while (node !== this.
|
|
9718
|
+
while (node !== this.Sentinel) {
|
|
9601
9719
|
if (node && callback(node) === identifier) {
|
|
9602
9720
|
z = node;
|
|
9603
9721
|
}
|
|
@@ -9607,16 +9725,16 @@ var dataStructureTyped = (() => {
|
|
|
9607
9725
|
node = node == null ? void 0 : node.left;
|
|
9608
9726
|
}
|
|
9609
9727
|
}
|
|
9610
|
-
if (z === this.
|
|
9728
|
+
if (z === this.Sentinel) {
|
|
9611
9729
|
this._size--;
|
|
9612
9730
|
return;
|
|
9613
9731
|
}
|
|
9614
9732
|
y = z;
|
|
9615
9733
|
let yOriginalColor = y.color;
|
|
9616
|
-
if (z.left === this.
|
|
9734
|
+
if (z.left === this.Sentinel) {
|
|
9617
9735
|
x = z.right;
|
|
9618
9736
|
this._rbTransplant(z, z.right);
|
|
9619
|
-
} else if (z.right === this.
|
|
9737
|
+
} else if (z.right === this.Sentinel) {
|
|
9620
9738
|
x = z.left;
|
|
9621
9739
|
this._rbTransplant(z, z.left);
|
|
9622
9740
|
} else {
|
|
@@ -9644,7 +9762,7 @@ var dataStructureTyped = (() => {
|
|
|
9644
9762
|
return ans;
|
|
9645
9763
|
}
|
|
9646
9764
|
isRealNode(node) {
|
|
9647
|
-
return node !== this.
|
|
9765
|
+
return node !== this.Sentinel && node !== void 0;
|
|
9648
9766
|
}
|
|
9649
9767
|
/**
|
|
9650
9768
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
@@ -9671,7 +9789,7 @@ var dataStructureTyped = (() => {
|
|
|
9671
9789
|
* `getNodes` method, which is called within the `getNode` method.
|
|
9672
9790
|
* @returns a value of type `N`, `null`, or `undefined`.
|
|
9673
9791
|
*/
|
|
9674
|
-
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
9792
|
+
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType) {
|
|
9675
9793
|
var _a;
|
|
9676
9794
|
if (identifier instanceof BinaryTreeNode)
|
|
9677
9795
|
callback = (node) => node;
|
|
@@ -9692,11 +9810,11 @@ var dataStructureTyped = (() => {
|
|
|
9692
9810
|
*/
|
|
9693
9811
|
getSuccessor(x) {
|
|
9694
9812
|
var _a;
|
|
9695
|
-
if (x.right !== this.
|
|
9813
|
+
if (x.right !== this.Sentinel) {
|
|
9696
9814
|
return (_a = this.getLeftMost(x.right)) != null ? _a : void 0;
|
|
9697
9815
|
}
|
|
9698
9816
|
let y = x.parent;
|
|
9699
|
-
while (y !== this.
|
|
9817
|
+
while (y !== this.Sentinel && y !== void 0 && x === y.right) {
|
|
9700
9818
|
x = y;
|
|
9701
9819
|
y = y.parent;
|
|
9702
9820
|
}
|
|
@@ -9716,18 +9834,18 @@ var dataStructureTyped = (() => {
|
|
|
9716
9834
|
* @returns the predecessor of the given RedBlackTreeNode 'x'.
|
|
9717
9835
|
*/
|
|
9718
9836
|
getPredecessor(x) {
|
|
9719
|
-
if (x.left !== this.
|
|
9837
|
+
if (x.left !== this.Sentinel) {
|
|
9720
9838
|
return this.getRightMost(x.left);
|
|
9721
9839
|
}
|
|
9722
9840
|
let y = x.parent;
|
|
9723
|
-
while (y !== this.
|
|
9841
|
+
while (y !== this.Sentinel && x === y.left) {
|
|
9724
9842
|
x = y;
|
|
9725
9843
|
y = y.parent;
|
|
9726
9844
|
}
|
|
9727
9845
|
return y;
|
|
9728
9846
|
}
|
|
9729
9847
|
clear() {
|
|
9730
|
-
this._root = this.
|
|
9848
|
+
this._root = this.Sentinel;
|
|
9731
9849
|
this._size = 0;
|
|
9732
9850
|
}
|
|
9733
9851
|
_setRoot(v) {
|
|
@@ -9751,7 +9869,7 @@ var dataStructureTyped = (() => {
|
|
|
9751
9869
|
if (x.right) {
|
|
9752
9870
|
const y = x.right;
|
|
9753
9871
|
x.right = y.left;
|
|
9754
|
-
if (y.left !== this.
|
|
9872
|
+
if (y.left !== this.Sentinel) {
|
|
9755
9873
|
if (y.left)
|
|
9756
9874
|
y.left.parent = x;
|
|
9757
9875
|
}
|
|
@@ -9783,7 +9901,7 @@ var dataStructureTyped = (() => {
|
|
|
9783
9901
|
if (x.left) {
|
|
9784
9902
|
const y = x.left;
|
|
9785
9903
|
x.left = y.right;
|
|
9786
|
-
if (y.right !== this.
|
|
9904
|
+
if (y.right !== this.Sentinel) {
|
|
9787
9905
|
if (y.right)
|
|
9788
9906
|
y.right.parent = x;
|
|
9789
9907
|
}
|
|
@@ -9967,16 +10085,22 @@ var dataStructureTyped = (() => {
|
|
|
9967
10085
|
this.count = count;
|
|
9968
10086
|
}
|
|
9969
10087
|
};
|
|
9970
|
-
var TreeMultimap = class extends AVLTree {
|
|
10088
|
+
var TreeMultimap = class _TreeMultimap extends AVLTree {
|
|
9971
10089
|
/**
|
|
9972
10090
|
* The constructor function for a TreeMultimap class in TypeScript, which extends another class and sets an option to
|
|
9973
10091
|
* merge duplicated values.
|
|
9974
10092
|
* @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
|
|
9975
10093
|
* TreeMultimap.
|
|
9976
10094
|
*/
|
|
9977
|
-
constructor(options) {
|
|
10095
|
+
constructor(options = { iterationType: "ITERATIVE" /* ITERATIVE */ }) {
|
|
9978
10096
|
super(options);
|
|
10097
|
+
__publicField(this, "options");
|
|
9979
10098
|
__publicField(this, "_count", 0);
|
|
10099
|
+
if (options) {
|
|
10100
|
+
this.options = __spreadValues({ iterationType: "ITERATIVE" /* ITERATIVE */, comparator: (a, b) => a - b }, options);
|
|
10101
|
+
} else {
|
|
10102
|
+
this.options = { iterationType: "ITERATIVE" /* ITERATIVE */, comparator: (a, b) => a - b };
|
|
10103
|
+
}
|
|
9980
10104
|
}
|
|
9981
10105
|
get count() {
|
|
9982
10106
|
return this._count;
|
|
@@ -9993,6 +10117,9 @@ var dataStructureTyped = (() => {
|
|
|
9993
10117
|
createNode(key, value, count) {
|
|
9994
10118
|
return new TreeMultimapNode(key, value, count);
|
|
9995
10119
|
}
|
|
10120
|
+
createTree(options) {
|
|
10121
|
+
return new _TreeMultimap(__spreadValues(__spreadValues({}, this.options), options));
|
|
10122
|
+
}
|
|
9996
10123
|
/**
|
|
9997
10124
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
9998
10125
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -10122,7 +10249,7 @@ var dataStructureTyped = (() => {
|
|
|
10122
10249
|
* values:
|
|
10123
10250
|
* @returns a boolean value.
|
|
10124
10251
|
*/
|
|
10125
|
-
perfectlyBalance(iterationType = this.iterationType) {
|
|
10252
|
+
perfectlyBalance(iterationType = this.options.iterationType) {
|
|
10126
10253
|
const sorted = this.dfs((node) => node, "in"), n = sorted.length;
|
|
10127
10254
|
if (sorted.length < 1)
|
|
10128
10255
|
return false;
|