data-structure-typed 1.52.8 → 1.53.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 +13 -13
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +151 -151
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +22 -22
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +64 -47
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +20 -20
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +28 -26
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +240 -141
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +395 -269
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +56 -56
- package/dist/cjs/data-structures/binary-tree/bst.js +114 -91
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +13 -13
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +35 -31
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +22 -23
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +59 -48
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.js +3 -3
- package/dist/cjs/interfaces/binary-tree.d.ts +5 -5
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +13 -13
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +22 -22
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +64 -46
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +20 -20
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +28 -25
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +240 -141
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +395 -268
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +56 -56
- package/dist/mjs/data-structures/binary-tree/bst.js +114 -89
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +13 -13
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +35 -30
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +22 -23
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +59 -47
- package/dist/mjs/data-structures/trie/trie.js +3 -3
- package/dist/mjs/interfaces/binary-tree.d.ts +5 -5
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +13 -13
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/umd/data-structure-typed.js +617 -482
- package/dist/umd/data-structure-typed.min.js +5 -5
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +6 -6
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +60 -55
- package/src/data-structures/binary-tree/avl-tree.ts +31 -35
- package/src/data-structures/binary-tree/binary-tree.ts +461 -385
- package/src/data-structures/binary-tree/bst.ts +155 -128
- package/src/data-structures/binary-tree/rb-tree.ts +37 -39
- package/src/data-structures/binary-tree/tree-multi-map.ts +57 -60
- package/src/data-structures/trie/trie.ts +3 -3
- package/src/interfaces/binary-tree.ts +6 -6
- package/src/types/data-structures/binary-tree/binary-tree.ts +14 -15
- package/src/types/data-structures/binary-tree/bst.ts +4 -4
- package/test/integration/bst.test.ts +2 -2
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +1 -1
- package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +2 -2
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +1 -1
- package/test/performance/data-structures/binary-tree/bst.test.ts +1 -1
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +113 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +107 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +169 -26
- package/test/unit/data-structures/binary-tree/bst.test.ts +326 -21
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +139 -1
|
@@ -6946,27 +6946,36 @@ var dataStructureTyped = (() => {
|
|
|
6946
6946
|
/**
|
|
6947
6947
|
* The constructor initializes a binary tree with optional options and adds keys, nodes, entries, or
|
|
6948
6948
|
* raw data if provided.
|
|
6949
|
-
* @param
|
|
6950
|
-
* is an iterable that can contain elements of type `
|
|
6949
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor
|
|
6950
|
+
* is an iterable that can contain elements of type `BTNRep<K, V, NODE>` or `R`. It is
|
|
6951
6951
|
* initialized with an empty array `[]` by default.
|
|
6952
6952
|
* @param [options] - The `options` parameter in the constructor is an object that can contain the
|
|
6953
6953
|
* following properties:
|
|
6954
6954
|
*/
|
|
6955
|
-
constructor(
|
|
6955
|
+
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
6956
6956
|
super();
|
|
6957
6957
|
__publicField(this, "iterationType", "ITERATIVE");
|
|
6958
|
+
__publicField(this, "_isMapMode", false);
|
|
6959
|
+
__publicField(this, "_store", /* @__PURE__ */ new Map());
|
|
6958
6960
|
__publicField(this, "_root");
|
|
6959
6961
|
__publicField(this, "_size", 0);
|
|
6960
6962
|
__publicField(this, "_NIL", new BinaryTreeNode(NaN));
|
|
6961
6963
|
__publicField(this, "_toEntryFn");
|
|
6962
|
-
__publicField(this, "
|
|
6964
|
+
__publicField(this, "_DEFAULT_NODE_CALLBACK", (node) => node ? node.key : void 0);
|
|
6963
6965
|
if (options) {
|
|
6964
|
-
const { iterationType, toEntryFn } = options;
|
|
6966
|
+
const { iterationType, toEntryFn, isMapMode } = options;
|
|
6965
6967
|
if (iterationType) this.iterationType = iterationType;
|
|
6968
|
+
if (isMapMode !== void 0) this._isMapMode = isMapMode;
|
|
6966
6969
|
if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
|
|
6967
6970
|
else if (toEntryFn) throw TypeError("toEntryFn must be a function type");
|
|
6968
6971
|
}
|
|
6969
|
-
if (
|
|
6972
|
+
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
6973
|
+
}
|
|
6974
|
+
get isMapMode() {
|
|
6975
|
+
return this._isMapMode;
|
|
6976
|
+
}
|
|
6977
|
+
get store() {
|
|
6978
|
+
return this._store;
|
|
6970
6979
|
}
|
|
6971
6980
|
get root() {
|
|
6972
6981
|
return this._root;
|
|
@@ -7003,41 +7012,46 @@ var dataStructureTyped = (() => {
|
|
|
7003
7012
|
createTree(options) {
|
|
7004
7013
|
return new _BinaryTree([], __spreadValues({
|
|
7005
7014
|
iterationType: this.iterationType,
|
|
7015
|
+
isMapMode: this._isMapMode,
|
|
7006
7016
|
toEntryFn: this._toEntryFn
|
|
7007
7017
|
}, options));
|
|
7008
7018
|
}
|
|
7009
7019
|
/**
|
|
7010
|
-
* The function `
|
|
7020
|
+
* The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
|
|
7011
7021
|
* or returns null.
|
|
7012
|
-
* @param {
|
|
7013
|
-
* `
|
|
7014
|
-
* can be of type `
|
|
7022
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
7023
|
+
* `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeEntryOrRaw`, which
|
|
7024
|
+
* can be of type `BTNRep<K, V, NODE>` or `R`. This parameter represents either a key, a
|
|
7015
7025
|
* node, an entry
|
|
7016
|
-
* @param {V} [value] - The `value` parameter in the `
|
|
7026
|
+
* @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
|
|
7017
7027
|
* an optional parameter of type `V`. It represents the value associated with the key in the node
|
|
7018
7028
|
* being created. If a `value` is provided, it will be used when creating the node. If
|
|
7019
|
-
* @returns The `
|
|
7020
|
-
* (`
|
|
7021
|
-
* input parameter (`
|
|
7029
|
+
* @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
|
|
7030
|
+
* (`OptNodeOrNull<NODE>`) based on the input parameters provided. The function checks the type of the
|
|
7031
|
+
* input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null
|
|
7022
7032
|
* value.
|
|
7023
7033
|
*/
|
|
7024
|
-
|
|
7025
|
-
if (
|
|
7026
|
-
if (
|
|
7027
|
-
if (this.isNode(
|
|
7028
|
-
if (this.isEntry(
|
|
7029
|
-
const [key, entryValue] =
|
|
7030
|
-
if (key === void 0) return;
|
|
7031
|
-
else if (key === null) return null;
|
|
7032
|
-
|
|
7034
|
+
keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
|
|
7035
|
+
if (keyNodeEntryOrRaw === void 0) return [void 0, void 0];
|
|
7036
|
+
if (keyNodeEntryOrRaw === null) return [null, void 0];
|
|
7037
|
+
if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
|
|
7038
|
+
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
7039
|
+
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
7040
|
+
if (key === void 0) return [void 0, void 0];
|
|
7041
|
+
else if (key === null) return [null, void 0];
|
|
7042
|
+
const finalValue = value != null ? value : entryValue;
|
|
7043
|
+
return [this.createNode(key, finalValue), finalValue];
|
|
7033
7044
|
}
|
|
7034
|
-
if (this.
|
|
7035
|
-
|
|
7036
|
-
if (this.
|
|
7037
|
-
|
|
7045
|
+
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value), value];
|
|
7046
|
+
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
7047
|
+
if (this._toEntryFn) {
|
|
7048
|
+
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
7049
|
+
const finalValue = value != null ? value : entryValue;
|
|
7050
|
+
if (this.isKey(key)) return [this.createNode(key, finalValue), finalValue];
|
|
7051
|
+
}
|
|
7052
|
+
return [void 0, void 0];
|
|
7038
7053
|
}
|
|
7039
|
-
|
|
7040
|
-
return;
|
|
7054
|
+
return [void 0, void 0];
|
|
7041
7055
|
}
|
|
7042
7056
|
/**
|
|
7043
7057
|
* Time Complexity: O(n)
|
|
@@ -7045,8 +7059,8 @@ var dataStructureTyped = (() => {
|
|
|
7045
7059
|
*
|
|
7046
7060
|
* The function `ensureNode` in TypeScript checks if a given input is a node, entry, key, or raw
|
|
7047
7061
|
* value and returns the corresponding node or null.
|
|
7048
|
-
* @param {
|
|
7049
|
-
* parameter in the `ensureNode` function can be of type `
|
|
7062
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw`
|
|
7063
|
+
* parameter in the `ensureNode` function can be of type `BTNRep<K, V, NODE>` or `R`. It
|
|
7050
7064
|
* is used to determine whether the input is a key, node, entry, or raw data. The
|
|
7051
7065
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `ensureNode` function
|
|
7052
7066
|
* is used to specify the type of iteration to be performed. It has a default value of
|
|
@@ -7054,103 +7068,105 @@ var dataStructureTyped = (() => {
|
|
|
7054
7068
|
* @returns The `ensureNode` function returns either a node, `null`, or `undefined` based on the
|
|
7055
7069
|
* conditions specified in the code snippet.
|
|
7056
7070
|
*/
|
|
7057
|
-
ensureNode(
|
|
7058
|
-
if (
|
|
7059
|
-
if (
|
|
7060
|
-
if (
|
|
7061
|
-
if (this.isNode(
|
|
7062
|
-
if (this.isEntry(
|
|
7063
|
-
const key =
|
|
7071
|
+
ensureNode(keyNodeEntryOrRaw, iterationType = this.iterationType) {
|
|
7072
|
+
if (keyNodeEntryOrRaw === null) return null;
|
|
7073
|
+
if (keyNodeEntryOrRaw === void 0) return;
|
|
7074
|
+
if (keyNodeEntryOrRaw === this._NIL) return;
|
|
7075
|
+
if (this.isNode(keyNodeEntryOrRaw)) return keyNodeEntryOrRaw;
|
|
7076
|
+
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
7077
|
+
const key = keyNodeEntryOrRaw[0];
|
|
7064
7078
|
if (key === null) return null;
|
|
7065
7079
|
if (key === void 0) return;
|
|
7066
7080
|
return this.getNodeByKey(key, iterationType);
|
|
7067
7081
|
}
|
|
7068
7082
|
if (this._toEntryFn) {
|
|
7069
|
-
const [key] = this._toEntryFn(
|
|
7083
|
+
const [key] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
7070
7084
|
if (this.isKey(key)) return this.getNodeByKey(key);
|
|
7071
7085
|
}
|
|
7072
|
-
if (this.isKey(
|
|
7086
|
+
if (this.isKey(keyNodeEntryOrRaw)) return this.getNodeByKey(keyNodeEntryOrRaw, iterationType);
|
|
7073
7087
|
return;
|
|
7074
7088
|
}
|
|
7075
7089
|
/**
|
|
7076
7090
|
* The function isNode checks if the input is an instance of BinaryTreeNode.
|
|
7077
|
-
* @param {
|
|
7078
|
-
* `
|
|
7091
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
7092
|
+
* `keyNodeEntryOrRaw` can be either a key, a node, an entry, or raw data. The function is
|
|
7079
7093
|
* checking if the input is an instance of a `BinaryTreeNode` and returning a boolean value
|
|
7080
7094
|
* accordingly.
|
|
7081
|
-
* @returns The function `isNode` is checking if the input `
|
|
7095
|
+
* @returns The function `isNode` is checking if the input `keyNodeEntryOrRaw` is an instance of
|
|
7082
7096
|
* `BinaryTreeNode`. If it is, the function returns `true`, indicating that the input is a node. If
|
|
7083
7097
|
* it is not an instance of `BinaryTreeNode`, the function returns `false`, indicating that the input
|
|
7084
7098
|
* is not a node.
|
|
7085
7099
|
*/
|
|
7086
|
-
isNode(
|
|
7087
|
-
return
|
|
7100
|
+
isNode(keyNodeEntryOrRaw) {
|
|
7101
|
+
return keyNodeEntryOrRaw instanceof BinaryTreeNode;
|
|
7102
|
+
}
|
|
7103
|
+
isRaw(keyNodeEntryOrRaw) {
|
|
7104
|
+
return typeof keyNodeEntryOrRaw === "object";
|
|
7088
7105
|
}
|
|
7089
7106
|
/**
|
|
7090
7107
|
* The function `isRealNode` checks if a given input is a valid node in a binary tree.
|
|
7091
|
-
* @param {
|
|
7092
|
-
* parameter in the `isRealNode` function can be of type `
|
|
7108
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw`
|
|
7109
|
+
* parameter in the `isRealNode` function can be of type `BTNRep<K, V, NODE>` or `R`.
|
|
7093
7110
|
* The function checks if the input parameter is a `NODE` type by verifying if it is not equal
|
|
7094
|
-
* @returns The function `isRealNode` is checking if the input `
|
|
7111
|
+
* @returns The function `isRealNode` is checking if the input `keyNodeEntryOrRaw` is a valid
|
|
7095
7112
|
* node by comparing it to `this._NIL`, `null`, and `undefined`. If the input is not one of these
|
|
7096
7113
|
* values, it then calls the `isNode` method to further determine if the input is a node. The
|
|
7097
7114
|
* function will return a boolean value indicating whether the
|
|
7098
7115
|
*/
|
|
7099
|
-
isRealNode(
|
|
7100
|
-
if (
|
|
7101
|
-
|
|
7102
|
-
return this.isNode(keyOrNodeOrEntryOrRaw);
|
|
7116
|
+
isRealNode(keyNodeEntryOrRaw) {
|
|
7117
|
+
if (keyNodeEntryOrRaw === this._NIL || keyNodeEntryOrRaw === null || keyNodeEntryOrRaw === void 0) return false;
|
|
7118
|
+
return this.isNode(keyNodeEntryOrRaw);
|
|
7103
7119
|
}
|
|
7104
7120
|
/**
|
|
7105
7121
|
* The function checks if a given input is a valid node or null.
|
|
7106
|
-
* @param {
|
|
7107
|
-
* `
|
|
7122
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
7123
|
+
* `keyNodeEntryOrRaw` in the `isRealNodeOrNull` function can be of type `BTNRep<K,
|
|
7108
7124
|
* V, NODE>` or `R`. It is a union type that can either be a key, a node, an entry, or
|
|
7109
7125
|
* @returns The function `isRealNodeOrNull` is returning a boolean value. It checks if the input
|
|
7110
|
-
* `
|
|
7126
|
+
* `keyNodeEntryOrRaw` is either `null` or a real node, and returns `true` if it is a node or
|
|
7111
7127
|
* `null`, and `false` otherwise.
|
|
7112
7128
|
*/
|
|
7113
|
-
isRealNodeOrNull(
|
|
7114
|
-
return
|
|
7129
|
+
isRealNodeOrNull(keyNodeEntryOrRaw) {
|
|
7130
|
+
return keyNodeEntryOrRaw === null || this.isRealNode(keyNodeEntryOrRaw);
|
|
7115
7131
|
}
|
|
7116
7132
|
/**
|
|
7117
7133
|
* The function isNIL checks if a given key, node, entry, or raw value is equal to the _NIL value.
|
|
7118
|
-
* @param {
|
|
7134
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - BTNRep<K, V,
|
|
7119
7135
|
* NODE> | R
|
|
7120
|
-
* @returns The function is checking if the `
|
|
7136
|
+
* @returns The function is checking if the `keyNodeEntryOrRaw` parameter is equal to the `_NIL`
|
|
7121
7137
|
* property of the current object and returning a boolean value based on that comparison.
|
|
7122
7138
|
*/
|
|
7123
|
-
isNIL(
|
|
7124
|
-
return
|
|
7139
|
+
isNIL(keyNodeEntryOrRaw) {
|
|
7140
|
+
return keyNodeEntryOrRaw === this._NIL;
|
|
7125
7141
|
}
|
|
7126
7142
|
/**
|
|
7127
7143
|
* The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
|
|
7128
7144
|
* tree.
|
|
7129
|
-
* @param {
|
|
7130
|
-
* `
|
|
7145
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
7146
|
+
* `keyNodeEntryOrRaw` can be of type `BTNRep<K, V, NODE>` or `R`. It represents a
|
|
7131
7147
|
* key, node, entry, or raw data in a binary tree structure. The function `isLeaf` checks whether the
|
|
7132
7148
|
* provided
|
|
7133
7149
|
* @returns The function `isLeaf` returns a boolean value indicating whether the input
|
|
7134
|
-
* `
|
|
7150
|
+
* `keyNodeEntryOrRaw` is a leaf node in a binary tree.
|
|
7135
7151
|
*/
|
|
7136
|
-
isLeaf(
|
|
7137
|
-
|
|
7138
|
-
if (
|
|
7139
|
-
if (
|
|
7140
|
-
return !this.isRealNode(
|
|
7152
|
+
isLeaf(keyNodeEntryOrRaw) {
|
|
7153
|
+
keyNodeEntryOrRaw = this.ensureNode(keyNodeEntryOrRaw);
|
|
7154
|
+
if (keyNodeEntryOrRaw === void 0) return false;
|
|
7155
|
+
if (keyNodeEntryOrRaw === null) return true;
|
|
7156
|
+
return !this.isRealNode(keyNodeEntryOrRaw.left) && !this.isRealNode(keyNodeEntryOrRaw.right);
|
|
7141
7157
|
}
|
|
7142
7158
|
/**
|
|
7143
7159
|
* The function `isEntry` checks if the input is a BTNEntry object by verifying if it is an array
|
|
7144
7160
|
* with a length of 2.
|
|
7145
|
-
* @param {
|
|
7146
|
-
* parameter in the `isEntry` function can be of type `
|
|
7147
|
-
* The function checks if the provided `
|
|
7148
|
-
* @returns The `isEntry` function is checking if the `
|
|
7161
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw`
|
|
7162
|
+
* parameter in the `isEntry` function can be of type `BTNRep<K, V, NODE>` or type `R`.
|
|
7163
|
+
* The function checks if the provided `keyNodeEntryOrRaw` is of type `BTN
|
|
7164
|
+
* @returns The `isEntry` function is checking if the `keyNodeEntryOrRaw` parameter is an array
|
|
7149
7165
|
* with a length of 2. If it is, then it returns `true`, indicating that the parameter is of type
|
|
7150
7166
|
* `BTNEntry<K, V>`. If the condition is not met, it returns `false`.
|
|
7151
7167
|
*/
|
|
7152
|
-
isEntry(
|
|
7153
|
-
return Array.isArray(
|
|
7168
|
+
isEntry(keyNodeEntryOrRaw) {
|
|
7169
|
+
return Array.isArray(keyNodeEntryOrRaw) && keyNodeEntryOrRaw.length === 2;
|
|
7154
7170
|
}
|
|
7155
7171
|
/**
|
|
7156
7172
|
* Time Complexity O(1)
|
|
@@ -7173,8 +7189,8 @@ var dataStructureTyped = (() => {
|
|
|
7173
7189
|
*
|
|
7174
7190
|
* The `add` function in TypeScript adds a new node to a binary tree while handling duplicate keys
|
|
7175
7191
|
* and finding the correct insertion position.
|
|
7176
|
-
* @param {
|
|
7177
|
-
* seems to be for adding a new node to a binary tree structure. The `
|
|
7192
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `add` method you provided
|
|
7193
|
+
* seems to be for adding a new node to a binary tree structure. The `keyNodeEntryOrRaw`
|
|
7178
7194
|
* parameter in the method can accept different types of values:
|
|
7179
7195
|
* @param {V} [value] - The `value` parameter in the `add` method represents the value associated
|
|
7180
7196
|
* with the key that you want to add to the binary tree. When adding a key-value pair to the binary
|
|
@@ -7184,11 +7200,12 @@ var dataStructureTyped = (() => {
|
|
|
7184
7200
|
* node was successful, and `false` if the insertion position could not be found or if a duplicate
|
|
7185
7201
|
* key was found and the node was replaced instead of inserted.
|
|
7186
7202
|
*/
|
|
7187
|
-
add(
|
|
7188
|
-
const newNode = this.
|
|
7203
|
+
add(keyNodeEntryOrRaw, value) {
|
|
7204
|
+
const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
7189
7205
|
if (newNode === void 0) return false;
|
|
7190
7206
|
if (!this._root) {
|
|
7191
7207
|
this._setRoot(newNode);
|
|
7208
|
+
if (this._isMapMode) this._setValue(newNode == null ? void 0 : newNode.key, newValue);
|
|
7192
7209
|
this._size = 1;
|
|
7193
7210
|
return true;
|
|
7194
7211
|
}
|
|
@@ -7199,6 +7216,7 @@ var dataStructureTyped = (() => {
|
|
|
7199
7216
|
if (!cur) continue;
|
|
7200
7217
|
if (newNode !== null && cur.key === newNode.key) {
|
|
7201
7218
|
this._replaceNode(cur, newNode);
|
|
7219
|
+
if (this._isMapMode) this._setValue(cur.key, newValue);
|
|
7202
7220
|
return true;
|
|
7203
7221
|
}
|
|
7204
7222
|
if (potentialParent === void 0 && (cur.left === void 0 || cur.right === void 0)) {
|
|
@@ -7217,6 +7235,7 @@ var dataStructureTyped = (() => {
|
|
|
7217
7235
|
} else if (potentialParent.right === void 0) {
|
|
7218
7236
|
potentialParent.right = newNode;
|
|
7219
7237
|
}
|
|
7238
|
+
if (this._isMapMode) this._setValue(newNode == null ? void 0 : newNode.key, newValue);
|
|
7220
7239
|
this._size++;
|
|
7221
7240
|
return true;
|
|
7222
7241
|
}
|
|
@@ -7229,24 +7248,24 @@ var dataStructureTyped = (() => {
|
|
|
7229
7248
|
* The `addMany` function takes in multiple keys or nodes or entries or raw values along with
|
|
7230
7249
|
* optional values, and adds them to a data structure while returning an array indicating whether
|
|
7231
7250
|
* each insertion was successful.
|
|
7232
|
-
* @param
|
|
7251
|
+
* @param keysNodesEntriesOrRaws - `keysNodesEntriesOrRaws` is an iterable that can contain a
|
|
7233
7252
|
* mix of keys, nodes, entries, or raw values. Each element in this iterable can be of type
|
|
7234
|
-
* `
|
|
7253
|
+
* `BTNRep<K, V, NODE>` or `R`.
|
|
7235
7254
|
* @param [values] - The `values` parameter in the `addMany` function is an optional parameter that
|
|
7236
7255
|
* accepts an iterable of values. These values correspond to the keys or nodes being added in the
|
|
7237
|
-
* `
|
|
7256
|
+
* `keysNodesEntriesOrRaws` parameter. If provided, the function will iterate over the values and
|
|
7238
7257
|
* assign them
|
|
7239
7258
|
* @returns The `addMany` method returns an array of boolean values indicating whether each key,
|
|
7240
7259
|
* node, entry, or raw value was successfully added to the data structure. Each boolean value
|
|
7241
7260
|
* corresponds to the success of adding the corresponding key or value in the input iterable.
|
|
7242
7261
|
*/
|
|
7243
|
-
addMany(
|
|
7262
|
+
addMany(keysNodesEntriesOrRaws, values) {
|
|
7244
7263
|
const inserted = [];
|
|
7245
7264
|
let valuesIterator;
|
|
7246
7265
|
if (values) {
|
|
7247
7266
|
valuesIterator = values[Symbol.iterator]();
|
|
7248
7267
|
}
|
|
7249
|
-
for (const
|
|
7268
|
+
for (const keyNodeEntryOrRaw of keysNodesEntriesOrRaws) {
|
|
7250
7269
|
let value = void 0;
|
|
7251
7270
|
if (valuesIterator) {
|
|
7252
7271
|
const valueResult = valuesIterator.next();
|
|
@@ -7254,7 +7273,7 @@ var dataStructureTyped = (() => {
|
|
|
7254
7273
|
value = valueResult.value;
|
|
7255
7274
|
}
|
|
7256
7275
|
}
|
|
7257
|
-
inserted.push(this.add(
|
|
7276
|
+
inserted.push(this.add(keyNodeEntryOrRaw, value));
|
|
7258
7277
|
}
|
|
7259
7278
|
return inserted;
|
|
7260
7279
|
}
|
|
@@ -7264,15 +7283,15 @@ var dataStructureTyped = (() => {
|
|
|
7264
7283
|
*
|
|
7265
7284
|
* The `refill` function clears the existing data structure and then adds new key-value pairs based
|
|
7266
7285
|
* on the provided input.
|
|
7267
|
-
* @param
|
|
7268
|
-
* method can accept an iterable containing a mix of `
|
|
7286
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the `refill`
|
|
7287
|
+
* method can accept an iterable containing a mix of `BTNRep<K, V, NODE>` objects or `R`
|
|
7269
7288
|
* objects.
|
|
7270
7289
|
* @param [values] - The `values` parameter in the `refill` method is an optional parameter that
|
|
7271
7290
|
* accepts an iterable of values of type `V` or `undefined`.
|
|
7272
7291
|
*/
|
|
7273
|
-
refill(
|
|
7292
|
+
refill(keysNodesEntriesOrRaws, values) {
|
|
7274
7293
|
this.clear();
|
|
7275
|
-
this.addMany(
|
|
7294
|
+
this.addMany(keysNodesEntriesOrRaws, values);
|
|
7276
7295
|
}
|
|
7277
7296
|
/**
|
|
7278
7297
|
* Time Complexity: O(n)
|
|
@@ -7280,19 +7299,19 @@ var dataStructureTyped = (() => {
|
|
|
7280
7299
|
*
|
|
7281
7300
|
* The function `delete` in TypeScript implements the deletion of a node in a binary tree and returns
|
|
7282
7301
|
* the deleted node along with information for tree balancing.
|
|
7283
|
-
* @param {
|
|
7302
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw
|
|
7284
7303
|
* - The `delete` method you provided is used to delete a node from a binary tree based on the key,
|
|
7285
|
-
* node, entry
|
|
7304
|
+
* node, entry or raw data. The method returns an array of
|
|
7286
7305
|
* `BinaryTreeDeleteResult` objects containing information about the deleted node and whether
|
|
7287
7306
|
* balancing is needed.
|
|
7288
7307
|
* @returns The `delete` method returns an array of `BinaryTreeDeleteResult` objects. Each object in
|
|
7289
7308
|
* the array contains information about the node that was deleted (`deleted`) and the node that may
|
|
7290
7309
|
* need to be balanced (`needBalanced`).
|
|
7291
7310
|
*/
|
|
7292
|
-
delete(
|
|
7311
|
+
delete(keyNodeEntryOrRaw) {
|
|
7293
7312
|
const deletedResult = [];
|
|
7294
7313
|
if (!this._root) return deletedResult;
|
|
7295
|
-
const curr = this.getNode(
|
|
7314
|
+
const curr = this.getNode(keyNodeEntryOrRaw);
|
|
7296
7315
|
if (!curr) return deletedResult;
|
|
7297
7316
|
const parent = curr == null ? void 0 : curr.parent;
|
|
7298
7317
|
let needBalanced;
|
|
@@ -7325,6 +7344,7 @@ var dataStructureTyped = (() => {
|
|
|
7325
7344
|
}
|
|
7326
7345
|
this._size = this._size - 1;
|
|
7327
7346
|
deletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
7347
|
+
if (this._isMapMode && orgCurrent) this._store.delete(orgCurrent.key);
|
|
7328
7348
|
return deletedResult;
|
|
7329
7349
|
}
|
|
7330
7350
|
/**
|
|
@@ -7333,12 +7353,12 @@ var dataStructureTyped = (() => {
|
|
|
7333
7353
|
*
|
|
7334
7354
|
* The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
|
|
7335
7355
|
* or predicate, with options for recursive or iterative traversal.
|
|
7336
|
-
* @param {
|
|
7356
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
|
|
7337
7357
|
* - The `getNodes` function you provided takes several parameters:
|
|
7338
7358
|
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
|
|
7339
7359
|
* determines whether to return only the first node that matches the criteria specified by the
|
|
7340
|
-
* `
|
|
7341
|
-
* @param {
|
|
7360
|
+
* `keyNodeEntryRawOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
|
|
7361
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
|
|
7342
7362
|
* `getNodes` function is used to specify the starting point for traversing the binary tree. It
|
|
7343
7363
|
* represents the root node of the binary tree or the node from which the traversal should begin. If
|
|
7344
7364
|
* not provided, the default value is set to `this._root
|
|
@@ -7348,12 +7368,12 @@ var dataStructureTyped = (() => {
|
|
|
7348
7368
|
* @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
|
|
7349
7369
|
* based on the input parameters and the iteration type specified.
|
|
7350
7370
|
*/
|
|
7351
|
-
getNodes(
|
|
7352
|
-
if (
|
|
7353
|
-
if (
|
|
7354
|
-
|
|
7355
|
-
if (!
|
|
7356
|
-
const callback = this._ensurePredicate(
|
|
7371
|
+
getNodes(keyNodeEntryRawOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
7372
|
+
if (keyNodeEntryRawOrPredicate === void 0) return [];
|
|
7373
|
+
if (keyNodeEntryRawOrPredicate === null) return [];
|
|
7374
|
+
startNode = this.ensureNode(startNode);
|
|
7375
|
+
if (!startNode) return [];
|
|
7376
|
+
const callback = this._ensurePredicate(keyNodeEntryRawOrPredicate);
|
|
7357
7377
|
const ans = [];
|
|
7358
7378
|
if (iterationType === "RECURSIVE") {
|
|
7359
7379
|
const dfs = (cur) => {
|
|
@@ -7365,9 +7385,9 @@ var dataStructureTyped = (() => {
|
|
|
7365
7385
|
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
7366
7386
|
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
7367
7387
|
};
|
|
7368
|
-
dfs(
|
|
7388
|
+
dfs(startNode);
|
|
7369
7389
|
} else {
|
|
7370
|
-
const stack = [
|
|
7390
|
+
const stack = [startNode];
|
|
7371
7391
|
while (stack.length > 0) {
|
|
7372
7392
|
const cur = stack.pop();
|
|
7373
7393
|
if (this.isRealNode(cur)) {
|
|
@@ -7388,10 +7408,10 @@ var dataStructureTyped = (() => {
|
|
|
7388
7408
|
*
|
|
7389
7409
|
* The `getNode` function retrieves a node based on the provided key, node, entry, raw data, or
|
|
7390
7410
|
* predicate.
|
|
7391
|
-
* @param {
|
|
7392
|
-
* - The `
|
|
7411
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
|
|
7412
|
+
* - The `keyNodeEntryRawOrPredicate` parameter in the `getNode` function can accept a key,
|
|
7393
7413
|
* node, entry, raw data, or a predicate function.
|
|
7394
|
-
* @param {
|
|
7414
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
|
|
7395
7415
|
* `getNode` function is used to specify the starting point for searching for a node in a binary
|
|
7396
7416
|
* tree. If no specific starting point is provided, the default value is set to `this._root`, which
|
|
7397
7417
|
* is typically the root node of the binary tree.
|
|
@@ -7402,9 +7422,9 @@ var dataStructureTyped = (() => {
|
|
|
7402
7422
|
* @returns The `getNode` function is returning the first node that matches the specified criteria,
|
|
7403
7423
|
* or `null` if no matching node is found.
|
|
7404
7424
|
*/
|
|
7405
|
-
getNode(
|
|
7425
|
+
getNode(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
7406
7426
|
var _a;
|
|
7407
|
-
return (_a = this.getNodes(
|
|
7427
|
+
return (_a = this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType)[0]) != null ? _a : null;
|
|
7408
7428
|
}
|
|
7409
7429
|
/**
|
|
7410
7430
|
* Time Complexity: O(n)
|
|
@@ -7417,7 +7437,7 @@ var dataStructureTyped = (() => {
|
|
|
7417
7437
|
* specifies how the tree nodes should be traversed when searching for a node with the given key. It
|
|
7418
7438
|
* is an optional parameter with a default value of `this.iterationType`.
|
|
7419
7439
|
* @returns The `getNodeByKey` function is returning an optional binary tree node
|
|
7420
|
-
* (`
|
|
7440
|
+
* (`OptNodeOrNull<NODE>`).
|
|
7421
7441
|
*/
|
|
7422
7442
|
getNodeByKey(key, iterationType = this.iterationType) {
|
|
7423
7443
|
return this.getNode(key, this._root, iterationType);
|
|
@@ -7428,10 +7448,10 @@ var dataStructureTyped = (() => {
|
|
|
7428
7448
|
*
|
|
7429
7449
|
* This function overrides the `get` method to retrieve the value associated with a specified key,
|
|
7430
7450
|
* node, entry, raw data, or predicate in a data structure.
|
|
7431
|
-
* @param {
|
|
7432
|
-
* - The `
|
|
7451
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
|
|
7452
|
+
* - The `keyNodeEntryRawOrPredicate` parameter in the `get` method can accept one of the
|
|
7433
7453
|
* following types:
|
|
7434
|
-
* @param {
|
|
7454
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `get`
|
|
7435
7455
|
* method is used to specify the starting point for searching for a key or node in the binary tree.
|
|
7436
7456
|
* If no specific starting point is provided, the default starting point is the root of the binary
|
|
7437
7457
|
* tree (`this._root`).
|
|
@@ -7444,9 +7464,14 @@ var dataStructureTyped = (() => {
|
|
|
7444
7464
|
* the method returns the corresponding value. If the key or node is not found, it returns
|
|
7445
7465
|
* `undefined`.
|
|
7446
7466
|
*/
|
|
7447
|
-
get(
|
|
7467
|
+
get(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
7448
7468
|
var _a;
|
|
7449
|
-
|
|
7469
|
+
if (this._isMapMode) {
|
|
7470
|
+
const key = this._getKey(keyNodeEntryRawOrPredicate);
|
|
7471
|
+
if (key === null || key === void 0) return;
|
|
7472
|
+
return this._store.get(key);
|
|
7473
|
+
}
|
|
7474
|
+
return (_a = this.getNode(keyNodeEntryRawOrPredicate, startNode, iterationType)) == null ? void 0 : _a.value;
|
|
7450
7475
|
}
|
|
7451
7476
|
/**
|
|
7452
7477
|
* Time Complexity: O(n)
|
|
@@ -7454,10 +7479,10 @@ var dataStructureTyped = (() => {
|
|
|
7454
7479
|
*
|
|
7455
7480
|
* The `has` function in TypeScript checks if a specified key, node, entry, raw data, or predicate
|
|
7456
7481
|
* exists in the data structure.
|
|
7457
|
-
* @param {
|
|
7458
|
-
* - The `
|
|
7482
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
|
|
7483
|
+
* - The `keyNodeEntryRawOrPredicate` parameter in the `override has` method can accept one of
|
|
7459
7484
|
* the following types:
|
|
7460
|
-
* @param {
|
|
7485
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
|
|
7461
7486
|
* `override` method is used to specify the starting point for the search operation within the data
|
|
7462
7487
|
* structure. It defaults to `this._root` if not provided explicitly.
|
|
7463
7488
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `override has` method
|
|
@@ -7469,8 +7494,8 @@ var dataStructureTyped = (() => {
|
|
|
7469
7494
|
* are matching nodes, it returns `true`, indicating that the tree contains the specified element.
|
|
7470
7495
|
* Otherwise, it returns `false`.
|
|
7471
7496
|
*/
|
|
7472
|
-
has(
|
|
7473
|
-
return this.getNodes(
|
|
7497
|
+
has(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
7498
|
+
return this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType).length > 0;
|
|
7474
7499
|
}
|
|
7475
7500
|
/**
|
|
7476
7501
|
* Time Complexity: O(1)
|
|
@@ -7479,8 +7504,8 @@ var dataStructureTyped = (() => {
|
|
|
7479
7504
|
* The `clear` function resets the root node and size of a data structure to empty.
|
|
7480
7505
|
*/
|
|
7481
7506
|
clear() {
|
|
7482
|
-
this.
|
|
7483
|
-
this.
|
|
7507
|
+
this._clearNodes();
|
|
7508
|
+
if (this._isMapMode) this._clearValues();
|
|
7484
7509
|
}
|
|
7485
7510
|
/**
|
|
7486
7511
|
* Time Complexity: O(1)
|
|
@@ -7500,17 +7525,17 @@ var dataStructureTyped = (() => {
|
|
|
7500
7525
|
*
|
|
7501
7526
|
* The function checks if a binary tree is perfectly balanced by comparing its minimum height with
|
|
7502
7527
|
* its height.
|
|
7503
|
-
* @param {
|
|
7528
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
7504
7529
|
* point for checking if the binary tree is perfectly balanced. It represents the root node of the
|
|
7505
7530
|
* binary tree or a specific node from which the balance check should begin.
|
|
7506
7531
|
* @returns The method `isPerfectlyBalanced` is returning a boolean value, which indicates whether
|
|
7507
|
-
* the tree starting from the `
|
|
7532
|
+
* the tree starting from the `startNode` node is perfectly balanced or not. The return value is
|
|
7508
7533
|
* determined by comparing the minimum height of the tree with the height of the tree. If the minimum
|
|
7509
7534
|
* height plus 1 is greater than or equal to the height of the tree, then it is considered perfectly
|
|
7510
7535
|
* balanced and
|
|
7511
7536
|
*/
|
|
7512
|
-
isPerfectlyBalanced(
|
|
7513
|
-
return this.getMinHeight(
|
|
7537
|
+
isPerfectlyBalanced(startNode = this._root) {
|
|
7538
|
+
return this.getMinHeight(startNode) + 1 >= this.getHeight(startNode);
|
|
7514
7539
|
}
|
|
7515
7540
|
/**
|
|
7516
7541
|
* Time Complexity: O(n)
|
|
@@ -7518,7 +7543,7 @@ var dataStructureTyped = (() => {
|
|
|
7518
7543
|
*
|
|
7519
7544
|
* The function `isBST` in TypeScript checks if a binary search tree is valid using either recursive
|
|
7520
7545
|
* or iterative methods.
|
|
7521
|
-
* @param {
|
|
7546
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `isBST`
|
|
7522
7547
|
* function represents the starting point for checking whether a binary search tree (BST) is valid.
|
|
7523
7548
|
* It can be a node in the BST or a reference to the root of the BST. If no specific node is
|
|
7524
7549
|
* provided, the function will default to
|
|
@@ -7530,9 +7555,9 @@ var dataStructureTyped = (() => {
|
|
|
7530
7555
|
* the tree satisfies the BST property, where for every node, all nodes in its left subtree have keys
|
|
7531
7556
|
* less than the node's key, and all nodes in its right subtree have keys greater than the node's
|
|
7532
7557
|
*/
|
|
7533
|
-
isBST(
|
|
7534
|
-
|
|
7535
|
-
if (!
|
|
7558
|
+
isBST(startNode = this._root, iterationType = this.iterationType) {
|
|
7559
|
+
startNode = this.ensureNode(startNode);
|
|
7560
|
+
if (!startNode) return true;
|
|
7536
7561
|
if (iterationType === "RECURSIVE") {
|
|
7537
7562
|
const dfs = (cur, min, max) => {
|
|
7538
7563
|
if (!this.isRealNode(cur)) return true;
|
|
@@ -7540,14 +7565,14 @@ var dataStructureTyped = (() => {
|
|
|
7540
7565
|
if (numKey <= min || numKey >= max) return false;
|
|
7541
7566
|
return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
|
|
7542
7567
|
};
|
|
7543
|
-
const isStandardBST = dfs(
|
|
7544
|
-
const isInverseBST = dfs(
|
|
7568
|
+
const isStandardBST = dfs(startNode, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
7569
|
+
const isInverseBST = dfs(startNode, Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
|
|
7545
7570
|
return isStandardBST || isInverseBST;
|
|
7546
7571
|
} else {
|
|
7547
7572
|
const checkBST = (checkMax = false) => {
|
|
7548
7573
|
const stack = [];
|
|
7549
7574
|
let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
|
|
7550
|
-
let curr =
|
|
7575
|
+
let curr = startNode;
|
|
7551
7576
|
while (this.isRealNode(curr) || stack.length > 0) {
|
|
7552
7577
|
while (this.isRealNode(curr)) {
|
|
7553
7578
|
stack.push(curr);
|
|
@@ -7570,20 +7595,20 @@ var dataStructureTyped = (() => {
|
|
|
7570
7595
|
* Space Complexity: O(1)
|
|
7571
7596
|
*
|
|
7572
7597
|
* The `getDepth` function calculates the depth between two nodes in a binary tree.
|
|
7573
|
-
* @param {
|
|
7598
|
+
* @param {BTNRep<K, V, NODE> | R} dist - The `dist` parameter in the `getDepth`
|
|
7574
7599
|
* function represents the node or entry in a binary tree map, or a reference to a node in the tree.
|
|
7575
|
-
* It is the target node for which you want to calculate the depth from the `
|
|
7576
|
-
* @param {
|
|
7600
|
+
* It is the target node for which you want to calculate the depth from the `startNode` node.
|
|
7601
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
|
|
7577
7602
|
* `getDepth` function represents the starting point from which you want to calculate the depth of a
|
|
7578
7603
|
* given node or entry in a binary tree. If no specific starting point is provided, the default value
|
|
7579
|
-
* for `
|
|
7604
|
+
* for `startNode` is set to the root of the binary
|
|
7580
7605
|
* @returns The `getDepth` method returns the depth of a given node `dist` relative to the
|
|
7581
|
-
* `
|
|
7606
|
+
* `startNode` node in a binary tree. If the `dist` node is not found in the path to the `startNode`
|
|
7582
7607
|
* node, it returns the depth of the `dist` node from the root of the tree.
|
|
7583
7608
|
*/
|
|
7584
|
-
getDepth(dist,
|
|
7609
|
+
getDepth(dist, startNode = this._root) {
|
|
7585
7610
|
let distEnsured = this.ensureNode(dist);
|
|
7586
|
-
const beginRootEnsured = this.ensureNode(
|
|
7611
|
+
const beginRootEnsured = this.ensureNode(startNode);
|
|
7587
7612
|
let depth = 0;
|
|
7588
7613
|
while (distEnsured == null ? void 0 : distEnsured.parent) {
|
|
7589
7614
|
if (distEnsured === beginRootEnsured) {
|
|
@@ -7600,7 +7625,7 @@ var dataStructureTyped = (() => {
|
|
|
7600
7625
|
*
|
|
7601
7626
|
* The `getHeight` function calculates the maximum height of a binary tree using either a recursive
|
|
7602
7627
|
* or iterative approach in TypeScript.
|
|
7603
|
-
* @param {
|
|
7628
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
7604
7629
|
* point from which the height of the binary tree will be calculated. It can be a node in the binary
|
|
7605
7630
|
* tree or a reference to the root of the tree. If not provided, it defaults to the root of the
|
|
7606
7631
|
* binary tree data structure.
|
|
@@ -7611,9 +7636,9 @@ var dataStructureTyped = (() => {
|
|
|
7611
7636
|
* root node. The height is calculated based on the maximum depth of the tree, considering either a
|
|
7612
7637
|
* recursive approach or an iterative approach depending on the `iterationType` parameter.
|
|
7613
7638
|
*/
|
|
7614
|
-
getHeight(
|
|
7615
|
-
|
|
7616
|
-
if (!this.isRealNode(
|
|
7639
|
+
getHeight(startNode = this._root, iterationType = this.iterationType) {
|
|
7640
|
+
startNode = this.ensureNode(startNode);
|
|
7641
|
+
if (!this.isRealNode(startNode)) return -1;
|
|
7617
7642
|
if (iterationType === "RECURSIVE") {
|
|
7618
7643
|
const _getMaxHeight = (cur) => {
|
|
7619
7644
|
if (!this.isRealNode(cur)) return -1;
|
|
@@ -7621,9 +7646,9 @@ var dataStructureTyped = (() => {
|
|
|
7621
7646
|
const rightHeight = _getMaxHeight(cur.right);
|
|
7622
7647
|
return Math.max(leftHeight, rightHeight) + 1;
|
|
7623
7648
|
};
|
|
7624
|
-
return _getMaxHeight(
|
|
7649
|
+
return _getMaxHeight(startNode);
|
|
7625
7650
|
} else {
|
|
7626
|
-
const stack = [{ node:
|
|
7651
|
+
const stack = [{ node: startNode, depth: 0 }];
|
|
7627
7652
|
let maxHeight = 0;
|
|
7628
7653
|
while (stack.length > 0) {
|
|
7629
7654
|
const { node, depth } = stack.pop();
|
|
@@ -7640,7 +7665,7 @@ var dataStructureTyped = (() => {
|
|
|
7640
7665
|
*
|
|
7641
7666
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
7642
7667
|
* recursive or iterative approach in TypeScript.
|
|
7643
|
-
* @param {
|
|
7668
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
|
|
7644
7669
|
* `getMinHeight` function represents the starting node from which the minimum height of the binary
|
|
7645
7670
|
* tree will be calculated. It is either a node in the binary tree or a reference to the root of the
|
|
7646
7671
|
* tree. If not provided, the default value is the root
|
|
@@ -7652,9 +7677,9 @@ var dataStructureTyped = (() => {
|
|
|
7652
7677
|
* leaf node in the tree. The method uses either a recursive approach or an iterative approach (using
|
|
7653
7678
|
* a stack) based on the `iterationType` parameter.
|
|
7654
7679
|
*/
|
|
7655
|
-
getMinHeight(
|
|
7656
|
-
|
|
7657
|
-
if (!
|
|
7680
|
+
getMinHeight(startNode = this._root, iterationType = this.iterationType) {
|
|
7681
|
+
startNode = this.ensureNode(startNode);
|
|
7682
|
+
if (!startNode) return -1;
|
|
7658
7683
|
if (iterationType === "RECURSIVE") {
|
|
7659
7684
|
const _getMinHeight = (cur) => {
|
|
7660
7685
|
if (!this.isRealNode(cur)) return 0;
|
|
@@ -7663,10 +7688,10 @@ var dataStructureTyped = (() => {
|
|
|
7663
7688
|
const rightMinHeight = _getMinHeight(cur.right);
|
|
7664
7689
|
return Math.min(leftMinHeight, rightMinHeight) + 1;
|
|
7665
7690
|
};
|
|
7666
|
-
return _getMinHeight(
|
|
7691
|
+
return _getMinHeight(startNode);
|
|
7667
7692
|
} else {
|
|
7668
7693
|
const stack = [];
|
|
7669
|
-
let node =
|
|
7694
|
+
let node = startNode, last = null;
|
|
7670
7695
|
const depths = /* @__PURE__ */ new Map();
|
|
7671
7696
|
while (stack.length > 0 || node) {
|
|
7672
7697
|
if (this.isRealNode(node)) {
|
|
@@ -7686,7 +7711,7 @@ var dataStructureTyped = (() => {
|
|
|
7686
7711
|
} else node = node.right;
|
|
7687
7712
|
}
|
|
7688
7713
|
}
|
|
7689
|
-
return depths.get(
|
|
7714
|
+
return depths.get(startNode);
|
|
7690
7715
|
}
|
|
7691
7716
|
}
|
|
7692
7717
|
/**
|
|
@@ -7699,7 +7724,7 @@ var dataStructureTyped = (() => {
|
|
|
7699
7724
|
* the path to the root. It is expected to be a function that takes a node as an argument and returns
|
|
7700
7725
|
* a value based on that node. The return type of the callback function is determined by the generic
|
|
7701
7726
|
* type `C
|
|
7702
|
-
* @param {
|
|
7727
|
+
* @param {BTNRep<K, V, NODE> | R} beginNode - The `beginNode` parameter in the
|
|
7703
7728
|
* `getPathToRoot` function can be either a key, a node, an entry, or any other value of type `R`.
|
|
7704
7729
|
* @param [isReverse=true] - The `isReverse` parameter in the `getPathToRoot` function determines
|
|
7705
7730
|
* whether the resulting path from the given `beginNode` to the root should be in reverse order or
|
|
@@ -7709,7 +7734,7 @@ var dataStructureTyped = (() => {
|
|
|
7709
7734
|
* array is either in reverse order or in the original order based on the value of the `isReverse`
|
|
7710
7735
|
* parameter.
|
|
7711
7736
|
*/
|
|
7712
|
-
getPathToRoot(callback = this.
|
|
7737
|
+
getPathToRoot(callback = this._DEFAULT_NODE_CALLBACK, beginNode, isReverse = true) {
|
|
7713
7738
|
const result = [];
|
|
7714
7739
|
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
7715
7740
|
if (!beginNodeEnsured) return result;
|
|
@@ -7728,8 +7753,8 @@ var dataStructureTyped = (() => {
|
|
|
7728
7753
|
* tail-recursive iteration.
|
|
7729
7754
|
* @param {C} callback - The `callback` parameter is a function that will be called with the leftmost
|
|
7730
7755
|
* node of a binary tree or with `undefined` if the tree is empty. It is provided with a default
|
|
7731
|
-
* value of `
|
|
7732
|
-
* @param {
|
|
7756
|
+
* value of `_DEFAULT_NODE_CALLBACK` if not specified.
|
|
7757
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
|
|
7733
7758
|
* `getLeftMost` function represents the starting point for finding the leftmost node in a binary
|
|
7734
7759
|
* tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
|
|
7735
7760
|
* starting point is provided, the function will default
|
|
@@ -7737,26 +7762,26 @@ var dataStructureTyped = (() => {
|
|
|
7737
7762
|
* specifies the type of iteration to be used when traversing the binary tree nodes. It can have two
|
|
7738
7763
|
* possible values:
|
|
7739
7764
|
* @returns The `getLeftMost` function returns the result of the callback function `C` applied to the
|
|
7740
|
-
* leftmost node in the binary tree starting from the `
|
|
7741
|
-
* `NIL`, it returns the result of the callback function applied to `undefined`. If the `
|
|
7765
|
+
* leftmost node in the binary tree starting from the `startNode` node. If the `startNode` node is
|
|
7766
|
+
* `NIL`, it returns the result of the callback function applied to `undefined`. If the `startNode`
|
|
7742
7767
|
* node is not a real node, it returns the result of the callback
|
|
7743
7768
|
*/
|
|
7744
|
-
getLeftMost(callback = this.
|
|
7745
|
-
if (this.isNIL(
|
|
7746
|
-
|
|
7747
|
-
if (!this.isRealNode(
|
|
7769
|
+
getLeftMost(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
7770
|
+
if (this.isNIL(startNode)) return callback(void 0);
|
|
7771
|
+
startNode = this.ensureNode(startNode);
|
|
7772
|
+
if (!this.isRealNode(startNode)) return callback(startNode);
|
|
7748
7773
|
if (iterationType === "RECURSIVE") {
|
|
7749
7774
|
const dfs = (cur) => {
|
|
7750
7775
|
if (!this.isRealNode(cur.left)) return cur;
|
|
7751
7776
|
return dfs(cur.left);
|
|
7752
7777
|
};
|
|
7753
|
-
return callback(dfs(
|
|
7778
|
+
return callback(dfs(startNode));
|
|
7754
7779
|
} else {
|
|
7755
7780
|
const dfs = trampoline((cur) => {
|
|
7756
7781
|
if (!this.isRealNode(cur.left)) return cur;
|
|
7757
7782
|
return dfs.cont(cur.left);
|
|
7758
7783
|
});
|
|
7759
|
-
return callback(dfs(
|
|
7784
|
+
return callback(dfs(startNode));
|
|
7760
7785
|
}
|
|
7761
7786
|
}
|
|
7762
7787
|
/**
|
|
@@ -7766,10 +7791,10 @@ var dataStructureTyped = (() => {
|
|
|
7766
7791
|
* The function `getRightMost` retrieves the rightmost node in a binary tree using either recursive
|
|
7767
7792
|
* or iterative traversal methods.
|
|
7768
7793
|
* @param {C} callback - The `callback` parameter is a function that will be called with the result
|
|
7769
|
-
* of finding the rightmost node in a binary tree. It is of type `
|
|
7794
|
+
* of finding the rightmost node in a binary tree. It is of type `NodeCallback<OptNodeOrNull<NODE>>`,
|
|
7770
7795
|
* which means it is a callback function that can accept either an optional binary tree node or null
|
|
7771
7796
|
* as
|
|
7772
|
-
* @param {
|
|
7797
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
|
|
7773
7798
|
* `getRightMost` function represents the starting point for finding the rightmost node in a binary
|
|
7774
7799
|
* tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
|
|
7775
7800
|
* starting point is provided, the function will default
|
|
@@ -7781,22 +7806,22 @@ var dataStructureTyped = (() => {
|
|
|
7781
7806
|
* the binary tree structure, determined based on the specified iteration type ('RECURSIVE' or
|
|
7782
7807
|
* other).
|
|
7783
7808
|
*/
|
|
7784
|
-
getRightMost(callback = this.
|
|
7785
|
-
if (this.isNIL(
|
|
7786
|
-
|
|
7787
|
-
if (!
|
|
7809
|
+
getRightMost(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
7810
|
+
if (this.isNIL(startNode)) return callback(void 0);
|
|
7811
|
+
startNode = this.ensureNode(startNode);
|
|
7812
|
+
if (!startNode) return callback(startNode);
|
|
7788
7813
|
if (iterationType === "RECURSIVE") {
|
|
7789
7814
|
const dfs = (cur) => {
|
|
7790
7815
|
if (!this.isRealNode(cur.right)) return cur;
|
|
7791
7816
|
return dfs(cur.right);
|
|
7792
7817
|
};
|
|
7793
|
-
return callback(dfs(
|
|
7818
|
+
return callback(dfs(startNode));
|
|
7794
7819
|
} else {
|
|
7795
7820
|
const dfs = trampoline((cur) => {
|
|
7796
7821
|
if (!this.isRealNode(cur.right)) return cur;
|
|
7797
7822
|
return dfs.cont(cur.right);
|
|
7798
7823
|
});
|
|
7799
|
-
return callback(dfs(
|
|
7824
|
+
return callback(dfs(startNode));
|
|
7800
7825
|
}
|
|
7801
7826
|
}
|
|
7802
7827
|
/**
|
|
@@ -7858,14 +7883,14 @@ var dataStructureTyped = (() => {
|
|
|
7858
7883
|
* The function `dfs` performs a depth-first search traversal on a binary tree structure based on the
|
|
7859
7884
|
* specified parameters.
|
|
7860
7885
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends the
|
|
7861
|
-
* `
|
|
7862
|
-
* `this.
|
|
7886
|
+
* `NodeCallback` interface with a type parameter of `OptNodeOrNull<NODE>`. It has a default value of
|
|
7887
|
+
* `this._DEFAULT_NODE_CALLBACK as C`.
|
|
7863
7888
|
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `dfs` method specifies the
|
|
7864
7889
|
* order in which the Depth-First Search (DFS) algorithm should traverse the nodes in the tree. The
|
|
7865
7890
|
* possible values for the `pattern` parameter are:
|
|
7866
|
-
* @param {
|
|
7891
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `dfs`
|
|
7867
7892
|
* method is used to specify the starting point for the Depth-First Search traversal. It can be
|
|
7868
|
-
* either a `
|
|
7893
|
+
* either a `BTNRep` object representing a key, node, or entry in the binary tree map,
|
|
7869
7894
|
* or it can be a
|
|
7870
7895
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `dfs` method specifies
|
|
7871
7896
|
* the type of iteration to be performed during the depth-first search traversal. It is used to
|
|
@@ -7877,10 +7902,10 @@ var dataStructureTyped = (() => {
|
|
|
7877
7902
|
* @returns The `dfs` method is returning an array of the return type specified by the generic type
|
|
7878
7903
|
* parameter `C`. The return type is determined by the callback function provided to the method.
|
|
7879
7904
|
*/
|
|
7880
|
-
dfs(callback = this.
|
|
7881
|
-
|
|
7882
|
-
if (!
|
|
7883
|
-
return this._dfs(callback, pattern,
|
|
7905
|
+
dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = "IN", startNode = this._root, iterationType = this.iterationType, includeNull = false) {
|
|
7906
|
+
startNode = this.ensureNode(startNode);
|
|
7907
|
+
if (!startNode) return [];
|
|
7908
|
+
return this._dfs(callback, pattern, startNode, iterationType, includeNull);
|
|
7884
7909
|
}
|
|
7885
7910
|
/**
|
|
7886
7911
|
* Time complexity: O(n)
|
|
@@ -7890,8 +7915,8 @@ var dataStructureTyped = (() => {
|
|
|
7890
7915
|
* tree, executing a specified callback function on each node visited.
|
|
7891
7916
|
* @param {C} callback - The `callback` parameter in the `bfs` function is a function that will be
|
|
7892
7917
|
* called on each node visited during the breadth-first search traversal. It is a generic type `C`
|
|
7893
|
-
* that extends the `
|
|
7894
|
-
* @param {
|
|
7918
|
+
* that extends the `NodeCallback` type, which takes a parameter of type `NODE` or `null`.
|
|
7919
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `bfs`
|
|
7895
7920
|
* function represents the starting point for the breadth-first search traversal in a binary tree. It
|
|
7896
7921
|
* can be specified as a key, node, or entry in the binary tree structure. If not provided, the
|
|
7897
7922
|
* default value is the root node of the binary
|
|
@@ -7905,12 +7930,12 @@ var dataStructureTyped = (() => {
|
|
|
7905
7930
|
* @returns The `bfs` function returns an array of values that are the result of applying the
|
|
7906
7931
|
* provided callback function to each node in the binary tree in a breadth-first search manner.
|
|
7907
7932
|
*/
|
|
7908
|
-
bfs(callback = this.
|
|
7909
|
-
|
|
7910
|
-
if (!
|
|
7933
|
+
bfs(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType, includeNull = false) {
|
|
7934
|
+
startNode = this.ensureNode(startNode);
|
|
7935
|
+
if (!startNode) return [];
|
|
7911
7936
|
const ans = [];
|
|
7912
7937
|
if (iterationType === "RECURSIVE") {
|
|
7913
|
-
const queue = new Queue([
|
|
7938
|
+
const queue = new Queue([startNode]);
|
|
7914
7939
|
const dfs = (level) => {
|
|
7915
7940
|
if (queue.size === 0) return;
|
|
7916
7941
|
const current = queue.shift();
|
|
@@ -7926,7 +7951,7 @@ var dataStructureTyped = (() => {
|
|
|
7926
7951
|
};
|
|
7927
7952
|
dfs(0);
|
|
7928
7953
|
} else {
|
|
7929
|
-
const queue = new Queue([
|
|
7954
|
+
const queue = new Queue([startNode]);
|
|
7930
7955
|
while (queue.size > 0) {
|
|
7931
7956
|
const levelSize = queue.size;
|
|
7932
7957
|
for (let i = 0; i < levelSize; i++) {
|
|
@@ -7952,7 +7977,7 @@ var dataStructureTyped = (() => {
|
|
|
7952
7977
|
* structure based on a specified callback and iteration type.
|
|
7953
7978
|
* @param {C} callback - The `callback` parameter is a function that will be called on each leaf node
|
|
7954
7979
|
* in the binary tree. It is optional and defaults to a default callback function if not provided.
|
|
7955
|
-
* @param {
|
|
7980
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `leaves`
|
|
7956
7981
|
* method is used to specify the starting point for finding and processing the leaves of a binary
|
|
7957
7982
|
* tree. It can be provided as either a key, a node, or an entry in the binary tree structure. If not
|
|
7958
7983
|
* explicitly provided, the default value
|
|
@@ -7962,10 +7987,10 @@ var dataStructureTyped = (() => {
|
|
|
7962
7987
|
* @returns The `leaves` method returns an array of values that are the result of applying the
|
|
7963
7988
|
* provided callback function to each leaf node in the binary tree.
|
|
7964
7989
|
*/
|
|
7965
|
-
leaves(callback = this.
|
|
7966
|
-
|
|
7990
|
+
leaves(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
7991
|
+
startNode = this.ensureNode(startNode);
|
|
7967
7992
|
const leaves = [];
|
|
7968
|
-
if (!this.isRealNode(
|
|
7993
|
+
if (!this.isRealNode(startNode)) return [];
|
|
7969
7994
|
if (iterationType === "RECURSIVE") {
|
|
7970
7995
|
const dfs = (cur) => {
|
|
7971
7996
|
if (this.isLeaf(cur)) {
|
|
@@ -7975,9 +8000,9 @@ var dataStructureTyped = (() => {
|
|
|
7975
8000
|
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
7976
8001
|
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
7977
8002
|
};
|
|
7978
|
-
dfs(
|
|
8003
|
+
dfs(startNode);
|
|
7979
8004
|
} else {
|
|
7980
|
-
const queue = new Queue([
|
|
8005
|
+
const queue = new Queue([startNode]);
|
|
7981
8006
|
while (queue.size > 0) {
|
|
7982
8007
|
const cur = queue.shift();
|
|
7983
8008
|
if (this.isRealNode(cur)) {
|
|
@@ -8000,7 +8025,7 @@ var dataStructureTyped = (() => {
|
|
|
8000
8025
|
* @param {C} callback - The `callback` parameter is a function that will be applied to each node in
|
|
8001
8026
|
* the binary tree during the traversal. It is used to process each node and determine what
|
|
8002
8027
|
* information to include in the output for each level of the tree.
|
|
8003
|
-
* @param {
|
|
8028
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
|
|
8004
8029
|
* `listLevels` function represents the starting point for traversing the binary tree. It can be
|
|
8005
8030
|
* either a key, a node, or an entry in the binary tree. If not provided, the default value is the
|
|
8006
8031
|
* root of the binary tree.
|
|
@@ -8015,10 +8040,10 @@ var dataStructureTyped = (() => {
|
|
|
8015
8040
|
* level in a binary tree. Each inner array contains the return value of the provided callback
|
|
8016
8041
|
* function applied to the nodes at that level.
|
|
8017
8042
|
*/
|
|
8018
|
-
listLevels(callback = this.
|
|
8019
|
-
|
|
8043
|
+
listLevels(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType, includeNull = false) {
|
|
8044
|
+
startNode = this.ensureNode(startNode);
|
|
8020
8045
|
const levelsNodes = [];
|
|
8021
|
-
if (!
|
|
8046
|
+
if (!startNode) return levelsNodes;
|
|
8022
8047
|
if (iterationType === "RECURSIVE") {
|
|
8023
8048
|
const _recursive = (node, level) => {
|
|
8024
8049
|
if (!levelsNodes[level]) levelsNodes[level] = [];
|
|
@@ -8031,9 +8056,9 @@ var dataStructureTyped = (() => {
|
|
|
8031
8056
|
if (node && node.right) _recursive(node.right, level + 1);
|
|
8032
8057
|
}
|
|
8033
8058
|
};
|
|
8034
|
-
_recursive(
|
|
8059
|
+
_recursive(startNode, 0);
|
|
8035
8060
|
} else {
|
|
8036
|
-
const stack = [[
|
|
8061
|
+
const stack = [[startNode, 0]];
|
|
8037
8062
|
while (stack.length > 0) {
|
|
8038
8063
|
const head = stack.pop();
|
|
8039
8064
|
const [node, level] = head;
|
|
@@ -8058,11 +8083,11 @@ var dataStructureTyped = (() => {
|
|
|
8058
8083
|
* Morris Traversal algorithm with different order patterns.
|
|
8059
8084
|
* @param {C} callback - The `callback` parameter in the `morris` function is a function that will be
|
|
8060
8085
|
* called on each node in the binary tree during the traversal. It is of type `C`, which extends the
|
|
8061
|
-
* `
|
|
8086
|
+
* `NodeCallback<NODE>` type. The default value for `callback` is `this._DEFAULT
|
|
8062
8087
|
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function specifies
|
|
8063
8088
|
* the type of Depth-First Search (DFS) order pattern to traverse the binary tree. The possible
|
|
8064
8089
|
* values for the `pattern` parameter are:
|
|
8065
|
-
* @param {
|
|
8090
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `morris`
|
|
8066
8091
|
* function is the starting point for the Morris traversal algorithm. It represents the root node of
|
|
8067
8092
|
* the binary tree or the node from which the traversal should begin. It can be provided as either a
|
|
8068
8093
|
* key, a node, an entry, or a reference
|
|
@@ -8070,11 +8095,11 @@ var dataStructureTyped = (() => {
|
|
|
8070
8095
|
* provided callback function to each node in the binary tree in the specified order pattern (IN,
|
|
8071
8096
|
* PRE, or POST).
|
|
8072
8097
|
*/
|
|
8073
|
-
morris(callback = this.
|
|
8074
|
-
|
|
8075
|
-
if (!
|
|
8098
|
+
morris(callback = this._DEFAULT_NODE_CALLBACK, pattern = "IN", startNode = this._root) {
|
|
8099
|
+
startNode = this.ensureNode(startNode);
|
|
8100
|
+
if (!startNode) return [];
|
|
8076
8101
|
const ans = [];
|
|
8077
|
-
let cur =
|
|
8102
|
+
let cur = startNode;
|
|
8078
8103
|
const _reverseEdge = (node) => {
|
|
8079
8104
|
let pre = null;
|
|
8080
8105
|
let next = null;
|
|
@@ -8145,7 +8170,7 @@ var dataStructureTyped = (() => {
|
|
|
8145
8170
|
}
|
|
8146
8171
|
cur = cur.right;
|
|
8147
8172
|
}
|
|
8148
|
-
_printEdge(
|
|
8173
|
+
_printEdge(startNode);
|
|
8149
8174
|
break;
|
|
8150
8175
|
}
|
|
8151
8176
|
return ans;
|
|
@@ -8166,12 +8191,16 @@ var dataStructureTyped = (() => {
|
|
|
8166
8191
|
this.bfs(
|
|
8167
8192
|
(node) => {
|
|
8168
8193
|
if (node === null) cloned.add(null);
|
|
8169
|
-
else
|
|
8194
|
+
else {
|
|
8195
|
+
if (this._isMapMode) cloned.add([node.key, this._store.get(node.key)]);
|
|
8196
|
+
else cloned.add([node.key, node.value]);
|
|
8197
|
+
}
|
|
8170
8198
|
},
|
|
8171
8199
|
this._root,
|
|
8172
8200
|
this.iterationType,
|
|
8173
8201
|
true
|
|
8174
8202
|
);
|
|
8203
|
+
if (this._isMapMode) cloned._store = this._store;
|
|
8175
8204
|
return cloned;
|
|
8176
8205
|
}
|
|
8177
8206
|
/**
|
|
@@ -8238,7 +8267,7 @@ var dataStructureTyped = (() => {
|
|
|
8238
8267
|
*
|
|
8239
8268
|
* The function `toVisual` in TypeScript overrides the visual representation of a binary tree with
|
|
8240
8269
|
* customizable options for displaying undefined, null, and sentinel nodes.
|
|
8241
|
-
* @param {
|
|
8270
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
|
|
8242
8271
|
* `toVisual` method is used to specify the starting point for visualizing the binary tree structure.
|
|
8243
8272
|
* It can be a node, key, entry, or the root of the tree. If no specific starting point is provided,
|
|
8244
8273
|
* the default is set to the root
|
|
@@ -8250,20 +8279,17 @@ var dataStructureTyped = (() => {
|
|
|
8250
8279
|
* the lines to the output string. The final output string contains the visual representation of the
|
|
8251
8280
|
* binary tree with the specified options.
|
|
8252
8281
|
*/
|
|
8253
|
-
toVisual(
|
|
8254
|
-
const opts = __spreadValues({ isShowUndefined: false, isShowNull:
|
|
8255
|
-
|
|
8282
|
+
toVisual(startNode = this._root, options) {
|
|
8283
|
+
const opts = __spreadValues({ isShowUndefined: false, isShowNull: true, isShowRedBlackNIL: false }, options);
|
|
8284
|
+
startNode = this.ensureNode(startNode);
|
|
8256
8285
|
let output = "";
|
|
8257
|
-
if (!
|
|
8258
|
-
if (opts.isShowUndefined)
|
|
8259
|
-
|
|
8260
|
-
|
|
8261
|
-
|
|
8262
|
-
|
|
8263
|
-
|
|
8264
|
-
if (opts.isShowRedBlackNIL)
|
|
8265
|
-
output += `S for Sentinel Node(NIL)
|
|
8266
|
-
`;
|
|
8286
|
+
if (!startNode) return output;
|
|
8287
|
+
if (opts.isShowUndefined) output += `U for undefined
|
|
8288
|
+
`;
|
|
8289
|
+
if (opts.isShowNull) output += `N for null
|
|
8290
|
+
`;
|
|
8291
|
+
if (opts.isShowRedBlackNIL) output += `S for Sentinel Node(NIL)
|
|
8292
|
+
`;
|
|
8267
8293
|
const display = (root) => {
|
|
8268
8294
|
const [lines, , ,] = this._displayAux(root, opts);
|
|
8269
8295
|
let paragraph = "";
|
|
@@ -8272,9 +8298,27 @@ var dataStructureTyped = (() => {
|
|
|
8272
8298
|
}
|
|
8273
8299
|
output += paragraph;
|
|
8274
8300
|
};
|
|
8275
|
-
display(
|
|
8301
|
+
display(startNode);
|
|
8276
8302
|
return output;
|
|
8277
8303
|
}
|
|
8304
|
+
/**
|
|
8305
|
+
* Time Complexity: O(n)
|
|
8306
|
+
* Space Complexity: O(n)
|
|
8307
|
+
*
|
|
8308
|
+
* The function `print` in TypeScript overrides the default print behavior to log a visual
|
|
8309
|
+
* representation of the binary tree to the console.
|
|
8310
|
+
* @param {BinaryTreePrintOptions} [options] - The `options` parameter is used to specify the
|
|
8311
|
+
* printing options for the binary tree. It is an optional parameter that allows you to customize how
|
|
8312
|
+
* the binary tree is printed, such as choosing between different traversal orders or formatting
|
|
8313
|
+
* options.
|
|
8314
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
|
|
8315
|
+
* `override print` method is used to specify the starting point for printing the binary tree. It can
|
|
8316
|
+
* be either a key, a node, an entry, or the root of the tree. If no specific starting point is
|
|
8317
|
+
* provided, the default value is set to
|
|
8318
|
+
*/
|
|
8319
|
+
print(options, startNode = this._root) {
|
|
8320
|
+
console.log(this.toVisual(startNode, options));
|
|
8321
|
+
}
|
|
8278
8322
|
/**
|
|
8279
8323
|
* Time complexity: O(n)
|
|
8280
8324
|
* Space complexity: O(n)
|
|
@@ -8283,13 +8327,13 @@ var dataStructureTyped = (() => {
|
|
|
8283
8327
|
* the specified order pattern and callback function.
|
|
8284
8328
|
* @param {C} callback - The `callback` parameter in the `_dfs` method is a function that will be
|
|
8285
8329
|
* called on each node visited during the depth-first search traversal. It is of type `C`, which
|
|
8286
|
-
* extends `
|
|
8330
|
+
* extends `NodeCallback<OptNodeOrNull<NODE>>`. The default value for this parameter is `this._DEFAULT
|
|
8287
8331
|
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `_dfs` method specifies the
|
|
8288
8332
|
* order in which the nodes are visited during the Depth-First Search traversal. It can have one of
|
|
8289
8333
|
* the following values:
|
|
8290
|
-
* @param {
|
|
8334
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `_dfs`
|
|
8291
8335
|
* method is used to specify the starting point for the depth-first search traversal in a binary
|
|
8292
|
-
* tree. It can be provided as either a `
|
|
8336
|
+
* tree. It can be provided as either a `BTNRep` object or a reference to the root node
|
|
8293
8337
|
* of the tree. If no specific
|
|
8294
8338
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `_dfs` method
|
|
8295
8339
|
* specifies the type of iteration to be performed during the Depth-First Search (DFS) traversal of a
|
|
@@ -8317,12 +8361,12 @@ var dataStructureTyped = (() => {
|
|
|
8317
8361
|
* @returns The function `_dfs` returns an array of the return type of the callback function provided
|
|
8318
8362
|
* as input.
|
|
8319
8363
|
*/
|
|
8320
|
-
_dfs(callback = this.
|
|
8364
|
+
_dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = "IN", startNode = this._root, iterationType = this.iterationType, includeNull = false, shouldVisitLeft = (node) => !!node, shouldVisitRight = (node) => !!node, shouldVisitRoot = (node) => {
|
|
8321
8365
|
if (includeNull) return this.isRealNodeOrNull(node);
|
|
8322
8366
|
return this.isRealNode(node);
|
|
8323
8367
|
}, shouldProcessRoot = (node) => this.isRealNodeOrNull(node)) {
|
|
8324
|
-
|
|
8325
|
-
if (!
|
|
8368
|
+
startNode = this.ensureNode(startNode);
|
|
8369
|
+
if (!startNode) return [];
|
|
8326
8370
|
const ans = [];
|
|
8327
8371
|
if (iterationType === "RECURSIVE") {
|
|
8328
8372
|
const dfs = (node) => {
|
|
@@ -8351,9 +8395,9 @@ var dataStructureTyped = (() => {
|
|
|
8351
8395
|
break;
|
|
8352
8396
|
}
|
|
8353
8397
|
};
|
|
8354
|
-
dfs(
|
|
8398
|
+
dfs(startNode);
|
|
8355
8399
|
} else {
|
|
8356
|
-
const stack = [{ opt: 0 /* VISIT */, node:
|
|
8400
|
+
const stack = [{ opt: 0 /* VISIT */, node: startNode }];
|
|
8357
8401
|
const pushLeft = (cur) => {
|
|
8358
8402
|
var _a;
|
|
8359
8403
|
if (shouldVisitLeft(cur.node)) stack.push({ opt: 0 /* VISIT */, node: (_a = cur.node) == null ? void 0 : _a.left });
|
|
@@ -8421,7 +8465,8 @@ var dataStructureTyped = (() => {
|
|
|
8421
8465
|
}
|
|
8422
8466
|
current = stack.pop();
|
|
8423
8467
|
if (this.isRealNode(current)) {
|
|
8424
|
-
yield [current.key, current.
|
|
8468
|
+
if (this._isMapMode) yield [current.key, this._store.get(current.key)];
|
|
8469
|
+
else yield [current.key, current.value];
|
|
8425
8470
|
current = current.right;
|
|
8426
8471
|
}
|
|
8427
8472
|
}
|
|
@@ -8429,7 +8474,8 @@ var dataStructureTyped = (() => {
|
|
|
8429
8474
|
if (node.left && this.isRealNode(node)) {
|
|
8430
8475
|
yield* __yieldStar(this[Symbol.iterator](node.left));
|
|
8431
8476
|
}
|
|
8432
|
-
yield [node.key, node.
|
|
8477
|
+
if (this._isMapMode) yield [node.key, this._store.get(node.key)];
|
|
8478
|
+
else yield [node.key, node.value];
|
|
8433
8479
|
if (node.right && this.isRealNode(node)) {
|
|
8434
8480
|
yield* __yieldStar(this[Symbol.iterator](node.right));
|
|
8435
8481
|
}
|
|
@@ -8495,12 +8541,12 @@ var dataStructureTyped = (() => {
|
|
|
8495
8541
|
* Space Complexity: O(1)
|
|
8496
8542
|
*
|
|
8497
8543
|
* The _swapProperties function swaps key and value properties between two nodes in a binary tree.
|
|
8498
|
-
* @param {
|
|
8499
|
-
* `_swapProperties` method can be either a
|
|
8544
|
+
* @param {BTNRep<K, V, NODE> | R} srcNode - The `srcNode` parameter in the
|
|
8545
|
+
* `_swapProperties` method can be either a BTNRep object containing key and value
|
|
8500
8546
|
* properties, or it can be of type R.
|
|
8501
|
-
* @param {
|
|
8547
|
+
* @param {BTNRep<K, V, NODE> | R} destNode - The `destNode` parameter in the
|
|
8502
8548
|
* `_swapProperties` method represents the node or entry where the properties will be swapped with
|
|
8503
|
-
* the `srcNode`. It can be of type `
|
|
8549
|
+
* the `srcNode`. It can be of type `BTNRep<K, V, NODE>` or `R`. The method ensures that
|
|
8504
8550
|
* both `srcNode
|
|
8505
8551
|
* @returns The `_swapProperties` method returns either the `destNode` with its key and value swapped
|
|
8506
8552
|
* with the `srcNode`, or `undefined` if either `srcNode` or `destNode` is falsy.
|
|
@@ -8513,9 +8559,9 @@ var dataStructureTyped = (() => {
|
|
|
8513
8559
|
const tempNode = this.createNode(key, value);
|
|
8514
8560
|
if (tempNode) {
|
|
8515
8561
|
destNode.key = srcNode.key;
|
|
8516
|
-
destNode.value = srcNode.value;
|
|
8562
|
+
if (!this._isMapMode) destNode.value = srcNode.value;
|
|
8517
8563
|
srcNode.key = tempNode.key;
|
|
8518
|
-
srcNode.value = tempNode.value;
|
|
8564
|
+
if (!this._isMapMode) srcNode.value = tempNode.value;
|
|
8519
8565
|
}
|
|
8520
8566
|
return destNode;
|
|
8521
8567
|
}
|
|
@@ -8557,7 +8603,7 @@ var dataStructureTyped = (() => {
|
|
|
8557
8603
|
*
|
|
8558
8604
|
* The function _setRoot sets the root node of a data structure while updating the parent reference
|
|
8559
8605
|
* of the previous root node.
|
|
8560
|
-
* @param v - The parameter `v` in the `_setRoot` method is of type `
|
|
8606
|
+
* @param v - The parameter `v` in the `_setRoot` method is of type `OptNodeOrNull<NODE>`, which means
|
|
8561
8607
|
* it can either be an optional `NODE` type or `null`.
|
|
8562
8608
|
*/
|
|
8563
8609
|
_setRoot(v) {
|
|
@@ -8572,43 +8618,103 @@ var dataStructureTyped = (() => {
|
|
|
8572
8618
|
*
|
|
8573
8619
|
* The function `_ensurePredicate` in TypeScript ensures that the input is converted into a valid
|
|
8574
8620
|
* predicate function for a binary tree node.
|
|
8575
|
-
* @param {
|
|
8621
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The
|
|
8576
8622
|
* `_ensurePredicate` method in the provided code snippet is responsible for ensuring that the input
|
|
8577
|
-
* parameter `
|
|
8623
|
+
* parameter `keyNodeEntryRawOrPredicate` is transformed into a valid predicate function that can be
|
|
8578
8624
|
* used for filtering nodes in a binary tree.
|
|
8579
|
-
* @returns A
|
|
8625
|
+
* @returns A NodePredicate<NODE> function is being returned.
|
|
8580
8626
|
*/
|
|
8581
|
-
_ensurePredicate(
|
|
8582
|
-
if (
|
|
8627
|
+
_ensurePredicate(keyNodeEntryRawOrPredicate) {
|
|
8628
|
+
if (keyNodeEntryRawOrPredicate === null || keyNodeEntryRawOrPredicate === void 0)
|
|
8583
8629
|
return (node) => node ? false : false;
|
|
8584
|
-
if (this.
|
|
8585
|
-
if (this.isRealNode(
|
|
8586
|
-
if (this.isEntry(
|
|
8587
|
-
const [key] =
|
|
8630
|
+
if (this._isPredicate(keyNodeEntryRawOrPredicate)) return keyNodeEntryRawOrPredicate;
|
|
8631
|
+
if (this.isRealNode(keyNodeEntryRawOrPredicate)) return (node) => node === keyNodeEntryRawOrPredicate;
|
|
8632
|
+
if (this.isEntry(keyNodeEntryRawOrPredicate)) {
|
|
8633
|
+
const [key] = keyNodeEntryRawOrPredicate;
|
|
8588
8634
|
return (node) => node.key === key;
|
|
8589
8635
|
}
|
|
8590
|
-
if (this.isKey(
|
|
8636
|
+
if (this.isKey(keyNodeEntryRawOrPredicate)) return (node) => node.key === keyNodeEntryRawOrPredicate;
|
|
8591
8637
|
if (this._toEntryFn) {
|
|
8592
|
-
const [key] = this._toEntryFn(
|
|
8638
|
+
const [key] = this._toEntryFn(keyNodeEntryRawOrPredicate);
|
|
8593
8639
|
return (node) => node.key === key;
|
|
8594
8640
|
}
|
|
8595
|
-
return (node) => node.key ===
|
|
8641
|
+
return (node) => node.key === keyNodeEntryRawOrPredicate;
|
|
8596
8642
|
}
|
|
8597
8643
|
/**
|
|
8598
8644
|
* Time Complexity: O(1)
|
|
8599
8645
|
* Space Complexity: O(1)
|
|
8600
8646
|
*
|
|
8601
|
-
* The function `
|
|
8647
|
+
* The function `_isPredicate` checks if a given parameter is a function.
|
|
8602
8648
|
* @param {any} p - The parameter `p` is a variable of type `any`, which means it can hold any type
|
|
8603
|
-
* of value. In this context, the function `
|
|
8604
|
-
* satisfies the type `
|
|
8649
|
+
* of value. In this context, the function `_isPredicate` is checking if `p` is a function that
|
|
8650
|
+
* satisfies the type `NodePredicate<NODE>`.
|
|
8605
8651
|
* @returns The function is checking if the input `p` is a function and returning a boolean value
|
|
8606
8652
|
* based on that check. If `p` is a function, it will return `true`, indicating that `p` is a
|
|
8607
8653
|
* predicate function for a binary tree node. If `p` is not a function, it will return `false`.
|
|
8608
8654
|
*/
|
|
8609
|
-
|
|
8655
|
+
_isPredicate(p) {
|
|
8610
8656
|
return typeof p === "function";
|
|
8611
8657
|
}
|
|
8658
|
+
/**
|
|
8659
|
+
* Time Complexity: O(1)
|
|
8660
|
+
* Space Complexity: O(1)
|
|
8661
|
+
*
|
|
8662
|
+
* The function `_getKey` in TypeScript returns the key from a given input, which can be a node,
|
|
8663
|
+
* entry, raw data, or null/undefined.
|
|
8664
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `_getKey` method you provided is a
|
|
8665
|
+
* TypeScript method that takes in a parameter `keyNodeEntryOrRaw` of type `BTNRep<K, V, NODE> | R`,
|
|
8666
|
+
* where `BTNRep` is a generic type with keys `K`, `V`, and `NODE`, and `
|
|
8667
|
+
* @returns The `_getKey` method returns the key value extracted from the `keyNodeEntryOrRaw`
|
|
8668
|
+
* parameter. The return value can be a key value of type `K`, `null`, or `undefined`, depending on
|
|
8669
|
+
* the conditions checked in the method.
|
|
8670
|
+
*/
|
|
8671
|
+
_getKey(keyNodeEntryOrRaw) {
|
|
8672
|
+
if (keyNodeEntryOrRaw === null) return null;
|
|
8673
|
+
if (keyNodeEntryOrRaw === void 0) return;
|
|
8674
|
+
if (keyNodeEntryOrRaw === this._NIL) return;
|
|
8675
|
+
if (this.isNode(keyNodeEntryOrRaw)) return keyNodeEntryOrRaw.key;
|
|
8676
|
+
if (this.isEntry(keyNodeEntryOrRaw)) return keyNodeEntryOrRaw[0];
|
|
8677
|
+
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
8678
|
+
if (this._toEntryFn) {
|
|
8679
|
+
const [key] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
8680
|
+
return key;
|
|
8681
|
+
}
|
|
8682
|
+
return;
|
|
8683
|
+
}
|
|
8684
|
+
return keyNodeEntryOrRaw;
|
|
8685
|
+
}
|
|
8686
|
+
/**
|
|
8687
|
+
* Time Complexity: O(1)
|
|
8688
|
+
* Space Complexity: O(1)
|
|
8689
|
+
*
|
|
8690
|
+
* The function `_setValue` sets a value in a store based on a key, handling cases where the key or
|
|
8691
|
+
* value is null or undefined.
|
|
8692
|
+
* @param {K | null | undefined} key - The `key` parameter can be of type `K`, `null`, or
|
|
8693
|
+
* `undefined`.
|
|
8694
|
+
* @param {V | undefined} value - The `value` parameter in the `_setValue` method can be of type `V`
|
|
8695
|
+
* or `undefined`.
|
|
8696
|
+
* @returns The method `_setValue` returns `false` if either the `key` is `null` or `undefined`, or
|
|
8697
|
+
* if the `value` is `undefined`. Otherwise, it returns the result of calling the `set` method on the
|
|
8698
|
+
* `_store` object with the `key` and `value` arguments.
|
|
8699
|
+
*/
|
|
8700
|
+
_setValue(key, value) {
|
|
8701
|
+
if (key === null || key === void 0) return false;
|
|
8702
|
+
if (value === void 0) return false;
|
|
8703
|
+
return this._store.set(key, value);
|
|
8704
|
+
}
|
|
8705
|
+
/**
|
|
8706
|
+
* The _clearNodes function sets the root node to undefined and resets the size to 0.
|
|
8707
|
+
*/
|
|
8708
|
+
_clearNodes() {
|
|
8709
|
+
this._setRoot(void 0);
|
|
8710
|
+
this._size = 0;
|
|
8711
|
+
}
|
|
8712
|
+
/**
|
|
8713
|
+
* The _clearValues function clears all values stored in the _store object.
|
|
8714
|
+
*/
|
|
8715
|
+
_clearValues() {
|
|
8716
|
+
this._store.clear();
|
|
8717
|
+
}
|
|
8612
8718
|
};
|
|
8613
8719
|
|
|
8614
8720
|
// src/data-structures/binary-tree/bst.ts
|
|
@@ -8631,7 +8737,7 @@ var dataStructureTyped = (() => {
|
|
|
8631
8737
|
}
|
|
8632
8738
|
/**
|
|
8633
8739
|
* The function sets the left child of a node and updates the parent reference of the child.
|
|
8634
|
-
* @param {
|
|
8740
|
+
* @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be an
|
|
8635
8741
|
* instance of the `NODE` class or `undefined`.
|
|
8636
8742
|
*/
|
|
8637
8743
|
set left(v) {
|
|
@@ -8650,7 +8756,7 @@ var dataStructureTyped = (() => {
|
|
|
8650
8756
|
}
|
|
8651
8757
|
/**
|
|
8652
8758
|
* The function sets the right child of a node and updates the parent reference of the child.
|
|
8653
|
-
* @param {
|
|
8759
|
+
* @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be a
|
|
8654
8760
|
* `NODE` object or `undefined`.
|
|
8655
8761
|
*/
|
|
8656
8762
|
set right(v) {
|
|
@@ -8663,13 +8769,13 @@ var dataStructureTyped = (() => {
|
|
|
8663
8769
|
var BST = class _BST extends BinaryTree {
|
|
8664
8770
|
/**
|
|
8665
8771
|
* This is the constructor function for a Binary Search Tree class in TypeScript.
|
|
8666
|
-
* @param
|
|
8772
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
8667
8773
|
* iterable that can contain either keys, nodes, entries, or raw elements. These elements will be
|
|
8668
8774
|
* added to the binary search tree during the construction of the object.
|
|
8669
8775
|
* @param [options] - An optional object that contains additional options for the Binary Search Tree.
|
|
8670
8776
|
* It can include a comparator function that defines the order of the elements in the tree.
|
|
8671
8777
|
*/
|
|
8672
|
-
constructor(
|
|
8778
|
+
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
8673
8779
|
super([], options);
|
|
8674
8780
|
__publicField(this, "_root");
|
|
8675
8781
|
__publicField(this, "_DEFAULT_COMPARATOR", (a, b) => {
|
|
@@ -8687,7 +8793,7 @@ var dataStructureTyped = (() => {
|
|
|
8687
8793
|
const { comparator } = options;
|
|
8688
8794
|
if (comparator) this._comparator = comparator;
|
|
8689
8795
|
}
|
|
8690
|
-
if (
|
|
8796
|
+
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
8691
8797
|
}
|
|
8692
8798
|
/**
|
|
8693
8799
|
* The function returns the root node of a tree structure.
|
|
@@ -8717,22 +8823,24 @@ var dataStructureTyped = (() => {
|
|
|
8717
8823
|
createTree(options) {
|
|
8718
8824
|
return new _BST([], __spreadValues({
|
|
8719
8825
|
iterationType: this.iterationType,
|
|
8826
|
+
isMapMode: this._isMapMode,
|
|
8720
8827
|
comparator: this._comparator,
|
|
8721
8828
|
toEntryFn: this._toEntryFn
|
|
8722
8829
|
}, options));
|
|
8723
8830
|
}
|
|
8724
8831
|
/**
|
|
8725
8832
|
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
8726
|
-
* @param {
|
|
8727
|
-
* type R or
|
|
8833
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of
|
|
8834
|
+
* type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
8728
8835
|
* element.
|
|
8729
8836
|
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
8730
8837
|
* value associated with a key in a key-value pair.
|
|
8731
8838
|
* @returns either a NODE object or undefined.
|
|
8732
8839
|
*/
|
|
8733
|
-
|
|
8734
|
-
|
|
8735
|
-
|
|
8840
|
+
keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
|
|
8841
|
+
const [node, tValue] = super.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
8842
|
+
if (node === null) return [void 0, void 0];
|
|
8843
|
+
return [node, tValue != null ? tValue : value];
|
|
8736
8844
|
}
|
|
8737
8845
|
/**
|
|
8738
8846
|
* Time Complexity: O(log n)
|
|
@@ -8740,8 +8848,8 @@ var dataStructureTyped = (() => {
|
|
|
8740
8848
|
*
|
|
8741
8849
|
* The function ensures the existence of a node in a data structure and returns it, or undefined if
|
|
8742
8850
|
* it doesn't exist.
|
|
8743
|
-
* @param {
|
|
8744
|
-
* `
|
|
8851
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
8852
|
+
* `keyNodeEntryOrRaw` can accept a value of type `R`, which represents the key, node,
|
|
8745
8853
|
* entry, or raw element that needs to be ensured in the tree.
|
|
8746
8854
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
8747
8855
|
* parameter that specifies the type of iteration to be used when ensuring a node. It has a default
|
|
@@ -8749,19 +8857,19 @@ var dataStructureTyped = (() => {
|
|
|
8749
8857
|
* @returns The method is returning either the node that was ensured or `undefined` if the node could
|
|
8750
8858
|
* not be ensured.
|
|
8751
8859
|
*/
|
|
8752
|
-
ensureNode(
|
|
8860
|
+
ensureNode(keyNodeEntryOrRaw, iterationType = this.iterationType) {
|
|
8753
8861
|
var _a;
|
|
8754
|
-
return (_a = super.ensureNode(
|
|
8862
|
+
return (_a = super.ensureNode(keyNodeEntryOrRaw, iterationType)) != null ? _a : void 0;
|
|
8755
8863
|
}
|
|
8756
8864
|
/**
|
|
8757
8865
|
* The function checks if the input is an instance of the BSTNode class.
|
|
8758
|
-
* @param {
|
|
8759
|
-
* `
|
|
8760
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
8866
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
8867
|
+
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
8868
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
|
|
8761
8869
|
* an instance of the `BSTNode` class.
|
|
8762
8870
|
*/
|
|
8763
|
-
isNode(
|
|
8764
|
-
return
|
|
8871
|
+
isNode(keyNodeEntryOrRaw) {
|
|
8872
|
+
return keyNodeEntryOrRaw instanceof BSTNode;
|
|
8765
8873
|
}
|
|
8766
8874
|
/**
|
|
8767
8875
|
* The function "override isKey" checks if a key is comparable based on a given comparator.
|
|
@@ -8779,17 +8887,18 @@ var dataStructureTyped = (() => {
|
|
|
8779
8887
|
* Space Complexity: O(1)
|
|
8780
8888
|
*
|
|
8781
8889
|
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
|
|
8782
|
-
* @param {
|
|
8783
|
-
* `
|
|
8890
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
8891
|
+
* `keyNodeEntryOrRaw` can accept a value of type `R` or `BTNRep<K, V, NODE>`.
|
|
8784
8892
|
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
8785
8893
|
* key in the binary search tree. If provided, it will be stored in the node along with the key.
|
|
8786
8894
|
* @returns a boolean value.
|
|
8787
8895
|
*/
|
|
8788
|
-
add(
|
|
8789
|
-
const newNode = this.
|
|
8896
|
+
add(keyNodeEntryOrRaw, value) {
|
|
8897
|
+
const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
8790
8898
|
if (newNode === void 0) return false;
|
|
8791
8899
|
if (this._root === void 0) {
|
|
8792
8900
|
this._setRoot(newNode);
|
|
8901
|
+
if (this._isMapMode) this._setValue(newNode == null ? void 0 : newNode.key, newValue);
|
|
8793
8902
|
this._size++;
|
|
8794
8903
|
return true;
|
|
8795
8904
|
}
|
|
@@ -8801,6 +8910,7 @@ var dataStructureTyped = (() => {
|
|
|
8801
8910
|
} else if (this.comparator(current.key, newNode.key) > 0) {
|
|
8802
8911
|
if (current.left === void 0) {
|
|
8803
8912
|
current.left = newNode;
|
|
8913
|
+
if (this._isMapMode) this._setValue(newNode == null ? void 0 : newNode.key, newValue);
|
|
8804
8914
|
this._size++;
|
|
8805
8915
|
return true;
|
|
8806
8916
|
}
|
|
@@ -8808,6 +8918,7 @@ var dataStructureTyped = (() => {
|
|
|
8808
8918
|
} else {
|
|
8809
8919
|
if (current.right === void 0) {
|
|
8810
8920
|
current.right = newNode;
|
|
8921
|
+
if (this._isMapMode) this._setValue(newNode == null ? void 0 : newNode.key, newValue);
|
|
8811
8922
|
this._size++;
|
|
8812
8923
|
return true;
|
|
8813
8924
|
}
|
|
@@ -8822,7 +8933,7 @@ var dataStructureTyped = (() => {
|
|
|
8822
8933
|
*
|
|
8823
8934
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure and returns
|
|
8824
8935
|
* an array indicating whether each key or node was successfully inserted.
|
|
8825
|
-
* @param
|
|
8936
|
+
* @param keysNodesEntriesOrRaws - An iterable containing keys, nodes, entries, or raw
|
|
8826
8937
|
* elements to be added to the data structure.
|
|
8827
8938
|
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
8828
8939
|
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
@@ -8837,30 +8948,27 @@ var dataStructureTyped = (() => {
|
|
|
8837
8948
|
* @returns The function `addMany` returns an array of booleans indicating whether each element was
|
|
8838
8949
|
* successfully inserted into the data structure.
|
|
8839
8950
|
*/
|
|
8840
|
-
addMany(
|
|
8951
|
+
addMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
8841
8952
|
const inserted = [];
|
|
8842
8953
|
let valuesIterator;
|
|
8843
8954
|
if (values) {
|
|
8844
8955
|
valuesIterator = values[Symbol.iterator]();
|
|
8845
8956
|
}
|
|
8846
8957
|
if (!isBalanceAdd) {
|
|
8847
|
-
for (const kve of
|
|
8958
|
+
for (const kve of keysNodesEntriesOrRaws) {
|
|
8848
8959
|
const value = valuesIterator == null ? void 0 : valuesIterator.next().value;
|
|
8849
|
-
|
|
8850
|
-
inserted.push(nn);
|
|
8960
|
+
inserted.push(this.add(kve, value));
|
|
8851
8961
|
}
|
|
8852
8962
|
return inserted;
|
|
8853
8963
|
}
|
|
8854
8964
|
const realBTNExemplars = [];
|
|
8855
|
-
|
|
8856
|
-
|
|
8857
|
-
|
|
8858
|
-
|
|
8859
|
-
for (const kve of keysOrNodesOrEntriesOrRaws) {
|
|
8860
|
-
if (isRealBTNExemplar(kve)) realBTNExemplars.push(kve);
|
|
8965
|
+
let i = 0;
|
|
8966
|
+
for (const kve of keysNodesEntriesOrRaws) {
|
|
8967
|
+
realBTNExemplars.push({ key: kve, value: valuesIterator == null ? void 0 : valuesIterator.next().value, orgIndex: i });
|
|
8968
|
+
i++;
|
|
8861
8969
|
}
|
|
8862
8970
|
let sorted = [];
|
|
8863
|
-
sorted = realBTNExemplars.sort((a, b) => {
|
|
8971
|
+
sorted = realBTNExemplars.sort(({ key: a }, { key: b }) => {
|
|
8864
8972
|
let keyA, keyB;
|
|
8865
8973
|
if (this.isEntry(a)) keyA = a[0];
|
|
8866
8974
|
else if (this.isRealNode(a)) keyA = a.key;
|
|
@@ -8884,8 +8992,8 @@ var dataStructureTyped = (() => {
|
|
|
8884
8992
|
const _dfs = (arr) => {
|
|
8885
8993
|
if (arr.length === 0) return;
|
|
8886
8994
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
8887
|
-
const
|
|
8888
|
-
inserted.
|
|
8995
|
+
const { key, value, orgIndex } = arr[mid];
|
|
8996
|
+
inserted[orgIndex] = this.add(key, value);
|
|
8889
8997
|
_dfs(arr.slice(0, mid));
|
|
8890
8998
|
_dfs(arr.slice(mid + 1));
|
|
8891
8999
|
};
|
|
@@ -8898,8 +9006,8 @@ var dataStructureTyped = (() => {
|
|
|
8898
9006
|
const [l, r] = popped;
|
|
8899
9007
|
if (l <= r) {
|
|
8900
9008
|
const m = l + Math.floor((r - l) / 2);
|
|
8901
|
-
const
|
|
8902
|
-
inserted.
|
|
9009
|
+
const { key, value, orgIndex } = sorted[m];
|
|
9010
|
+
inserted[orgIndex] = this.add(key, value);
|
|
8903
9011
|
stack.push([m + 1, r]);
|
|
8904
9012
|
stack.push([l, m - 1]);
|
|
8905
9013
|
}
|
|
@@ -8918,30 +9026,30 @@ var dataStructureTyped = (() => {
|
|
|
8918
9026
|
* Space Complexity: O(k + log n)
|
|
8919
9027
|
*
|
|
8920
9028
|
* The function `getNodes` in TypeScript overrides the base class method to retrieve nodes based on a
|
|
8921
|
-
* given
|
|
8922
|
-
* @param {
|
|
9029
|
+
* given keyNodeEntryRawOrPredicate and iteration type.
|
|
9030
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate`
|
|
8923
9031
|
* parameter in the `getNodes` method is used to filter the nodes that will be returned. It can be a
|
|
8924
|
-
* key, a node, an entry, or a custom
|
|
9032
|
+
* key, a node, an entry, or a custom keyNodeEntryRawOrPredicate function that determines whether a node should be
|
|
8925
9033
|
* included in the result.
|
|
8926
9034
|
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` method is a boolean flag that
|
|
8927
|
-
* determines whether to return only the first node that matches the
|
|
8928
|
-
* that match the
|
|
9035
|
+
* determines whether to return only the first node that matches the keyNodeEntryRawOrPredicate (`true`) or all nodes
|
|
9036
|
+
* that match the keyNodeEntryRawOrPredicate (`false`). If `onlyOne` is set to `true`, the method will stop iterating
|
|
8929
9037
|
* and
|
|
8930
|
-
* @param {
|
|
9038
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
|
|
8931
9039
|
* `getNodes` method is used to specify the starting point for traversing the tree when searching for
|
|
8932
|
-
* nodes that match a given
|
|
9040
|
+
* nodes that match a given keyNodeEntryRawOrPredicate. It represents the root node of the subtree where the search
|
|
8933
9041
|
* should begin. If not explicitly provided, the default value for `begin
|
|
8934
9042
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` method
|
|
8935
9043
|
* specifies the type of iteration to be performed when traversing the nodes of a binary tree. It can
|
|
8936
9044
|
* have two possible values:
|
|
8937
|
-
* @returns The `getNodes` method returns an array of nodes that satisfy the given
|
|
8938
|
-
*/
|
|
8939
|
-
getNodes(
|
|
8940
|
-
if (
|
|
8941
|
-
if (
|
|
8942
|
-
|
|
8943
|
-
if (!
|
|
8944
|
-
const callback = this._ensurePredicate(
|
|
9045
|
+
* @returns The `getNodes` method returns an array of nodes that satisfy the given keyNodeEntryRawOrPredicate.
|
|
9046
|
+
*/
|
|
9047
|
+
getNodes(keyNodeEntryRawOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
9048
|
+
if (keyNodeEntryRawOrPredicate === void 0) return [];
|
|
9049
|
+
if (keyNodeEntryRawOrPredicate === null) return [];
|
|
9050
|
+
startNode = this.ensureNode(startNode);
|
|
9051
|
+
if (!startNode) return [];
|
|
9052
|
+
const callback = this._ensurePredicate(keyNodeEntryRawOrPredicate);
|
|
8945
9053
|
const ans = [];
|
|
8946
9054
|
if (iterationType === "RECURSIVE") {
|
|
8947
9055
|
const dfs = (cur) => {
|
|
@@ -8950,26 +9058,32 @@ var dataStructureTyped = (() => {
|
|
|
8950
9058
|
if (onlyOne) return;
|
|
8951
9059
|
}
|
|
8952
9060
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
|
|
8953
|
-
if (this.
|
|
8954
|
-
|
|
8955
|
-
if (this.isRealNode(cur.
|
|
9061
|
+
if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
|
|
9062
|
+
const benchmarkKey = this._getKey(keyNodeEntryRawOrPredicate);
|
|
9063
|
+
if (this.isRealNode(cur.left) && benchmarkKey !== null && benchmarkKey !== void 0 && this.comparator(cur.key, benchmarkKey) > 0)
|
|
9064
|
+
dfs(cur.left);
|
|
9065
|
+
if (this.isRealNode(cur.right) && benchmarkKey !== null && benchmarkKey !== void 0 && this.comparator(cur.key, benchmarkKey) < 0)
|
|
9066
|
+
dfs(cur.right);
|
|
8956
9067
|
} else {
|
|
8957
9068
|
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
8958
9069
|
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
8959
9070
|
}
|
|
8960
9071
|
};
|
|
8961
|
-
dfs(
|
|
9072
|
+
dfs(startNode);
|
|
8962
9073
|
} else {
|
|
8963
|
-
const stack = [
|
|
9074
|
+
const stack = [startNode];
|
|
8964
9075
|
while (stack.length > 0) {
|
|
8965
9076
|
const cur = stack.pop();
|
|
8966
9077
|
if (callback(cur)) {
|
|
8967
9078
|
ans.push(cur);
|
|
8968
9079
|
if (onlyOne) return ans;
|
|
8969
9080
|
}
|
|
8970
|
-
if (this.
|
|
8971
|
-
|
|
8972
|
-
if (this.isRealNode(cur.
|
|
9081
|
+
if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
|
|
9082
|
+
const benchmarkKey = this._getKey(keyNodeEntryRawOrPredicate);
|
|
9083
|
+
if (this.isRealNode(cur.right) && benchmarkKey !== null && benchmarkKey !== void 0 && this.comparator(cur.key, benchmarkKey) < 0)
|
|
9084
|
+
stack.push(cur.right);
|
|
9085
|
+
if (this.isRealNode(cur.left) && benchmarkKey !== null && benchmarkKey !== void 0 && this.comparator(cur.key, benchmarkKey) > 0)
|
|
9086
|
+
stack.push(cur.left);
|
|
8973
9087
|
} else {
|
|
8974
9088
|
if (this.isRealNode(cur.right)) stack.push(cur.right);
|
|
8975
9089
|
if (this.isRealNode(cur.left)) stack.push(cur.left);
|
|
@@ -8982,10 +9096,10 @@ var dataStructureTyped = (() => {
|
|
|
8982
9096
|
* Time Complexity: O(log n)
|
|
8983
9097
|
* Space Complexity: O(1)
|
|
8984
9098
|
*
|
|
8985
|
-
* This function retrieves a node based on a given
|
|
8986
|
-
* @param {
|
|
8987
|
-
* parameter can be of type `
|
|
8988
|
-
* @param {R |
|
|
9099
|
+
* This function retrieves a node based on a given keyNodeEntryRawOrPredicate within a binary search tree structure.
|
|
9100
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate`
|
|
9101
|
+
* parameter can be of type `BTNRep<K, V, NODE>`, `R`, or `NodePredicate<NODE>`.
|
|
9102
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} startNode - The `startNode` parameter in the `getNode` method
|
|
8989
9103
|
* is used to specify the starting point for searching nodes in the binary search tree. If no
|
|
8990
9104
|
* specific starting point is provided, the default value is set to `this._root`, which is the root
|
|
8991
9105
|
* node of the binary search tree.
|
|
@@ -8993,14 +9107,14 @@ var dataStructureTyped = (() => {
|
|
|
8993
9107
|
* parameter that specifies the type of iteration to be used. It has a default value of
|
|
8994
9108
|
* `this.iterationType`, which means it will use the iteration type defined in the class instance if
|
|
8995
9109
|
* no value is provided when calling the method.
|
|
8996
|
-
* @returns The `getNode` method is returning an optional binary search tree node (`
|
|
8997
|
-
* It is using the `getNodes` method to find the node based on the provided
|
|
8998
|
-
* the specified root node (`
|
|
9110
|
+
* @returns The `getNode` method is returning an optional binary search tree node (`OptNode<NODE>`).
|
|
9111
|
+
* It is using the `getNodes` method to find the node based on the provided keyNodeEntryRawOrPredicate, beginning at
|
|
9112
|
+
* the specified root node (`startNode`) and using the specified iteration type. The method then
|
|
8999
9113
|
* returns the first node found or `undefined` if no node is found.
|
|
9000
9114
|
*/
|
|
9001
|
-
getNode(
|
|
9115
|
+
getNode(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
9002
9116
|
var _a;
|
|
9003
|
-
return (_a = this.getNodes(
|
|
9117
|
+
return (_a = this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType)[0]) != null ? _a : void 0;
|
|
9004
9118
|
}
|
|
9005
9119
|
/**
|
|
9006
9120
|
* Time Complexity: O(log n)
|
|
@@ -9026,11 +9140,11 @@ var dataStructureTyped = (() => {
|
|
|
9026
9140
|
* the callback function.
|
|
9027
9141
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
9028
9142
|
* during the depth-first search traversal. It is an optional parameter and defaults to
|
|
9029
|
-
* `this.
|
|
9143
|
+
* `this._DEFAULT_NODE_CALLBACK`. The type `C` represents the type of the callback function.
|
|
9030
9144
|
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
9031
9145
|
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
9032
9146
|
* take one of the following values:
|
|
9033
|
-
* @param {
|
|
9147
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
9034
9148
|
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
9035
9149
|
* node entry. If not specified, the default value is the root of the tree.
|
|
9036
9150
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
@@ -9038,8 +9152,8 @@ var dataStructureTyped = (() => {
|
|
|
9038
9152
|
* following values:
|
|
9039
9153
|
* @returns The method is returning an array of the return type of the callback function.
|
|
9040
9154
|
*/
|
|
9041
|
-
dfs(callback = this.
|
|
9042
|
-
return super.dfs(callback, pattern,
|
|
9155
|
+
dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = "IN", startNode = this._root, iterationType = this.iterationType) {
|
|
9156
|
+
return super.dfs(callback, pattern, startNode, iterationType);
|
|
9043
9157
|
}
|
|
9044
9158
|
/**
|
|
9045
9159
|
* Time complexity: O(n)
|
|
@@ -9050,7 +9164,7 @@ var dataStructureTyped = (() => {
|
|
|
9050
9164
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
9051
9165
|
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
9052
9166
|
* node being visited, and it can return a value of any type.
|
|
9053
|
-
* @param {
|
|
9167
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
9054
9168
|
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
9055
9169
|
* object. If no value is provided, the default value is the root of the tree.
|
|
9056
9170
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
@@ -9058,8 +9172,8 @@ var dataStructureTyped = (() => {
|
|
|
9058
9172
|
* the following values:
|
|
9059
9173
|
* @returns an array of the return type of the callback function.
|
|
9060
9174
|
*/
|
|
9061
|
-
bfs(callback = this.
|
|
9062
|
-
return super.bfs(callback,
|
|
9175
|
+
bfs(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
9176
|
+
return super.bfs(callback, startNode, iterationType, false);
|
|
9063
9177
|
}
|
|
9064
9178
|
/**
|
|
9065
9179
|
* Time complexity: O(n)
|
|
@@ -9068,9 +9182,9 @@ var dataStructureTyped = (() => {
|
|
|
9068
9182
|
* The function overrides the listLevels method from the superclass and returns an array of arrays
|
|
9069
9183
|
* containing the results of the callback function applied to each level of the tree.
|
|
9070
9184
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
9071
|
-
* `
|
|
9185
|
+
* `NodeCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
9072
9186
|
* tree during the iteration process.
|
|
9073
|
-
* @param {
|
|
9187
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
9074
9188
|
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
9075
9189
|
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
9076
9190
|
* value is provided, the root of
|
|
@@ -9079,8 +9193,8 @@ var dataStructureTyped = (() => {
|
|
|
9079
9193
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
9080
9194
|
* function.
|
|
9081
9195
|
*/
|
|
9082
|
-
listLevels(callback = this.
|
|
9083
|
-
return super.listLevels(callback,
|
|
9196
|
+
listLevels(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
9197
|
+
return super.listLevels(callback, startNode, iterationType, false);
|
|
9084
9198
|
}
|
|
9085
9199
|
/**
|
|
9086
9200
|
* Time complexity: O(n)
|
|
@@ -9094,7 +9208,7 @@ var dataStructureTyped = (() => {
|
|
|
9094
9208
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
9095
9209
|
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
9096
9210
|
* 0, or 1, where:
|
|
9097
|
-
* @param {
|
|
9211
|
+
* @param {BTNRep<K, V, NODE> | R} targetNode - The `targetNode` parameter is the node in
|
|
9098
9212
|
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
9099
9213
|
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
9100
9214
|
* `targetNode` is provided,
|
|
@@ -9103,7 +9217,7 @@ var dataStructureTyped = (() => {
|
|
|
9103
9217
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
9104
9218
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
9105
9219
|
*/
|
|
9106
|
-
lesserOrGreaterTraverse(callback = this.
|
|
9220
|
+
lesserOrGreaterTraverse(callback = this._DEFAULT_NODE_CALLBACK, lesserOrGreater = -1, targetNode = this._root, iterationType = this.iterationType) {
|
|
9107
9221
|
const targetNodeEnsured = this.ensureNode(targetNode);
|
|
9108
9222
|
const ans = [];
|
|
9109
9223
|
if (!this._root) return ans;
|
|
@@ -9146,14 +9260,15 @@ var dataStructureTyped = (() => {
|
|
|
9146
9260
|
*/
|
|
9147
9261
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
9148
9262
|
const sorted = this.dfs((node) => node, "IN"), n = sorted.length;
|
|
9149
|
-
this.
|
|
9263
|
+
this._clearNodes();
|
|
9150
9264
|
if (sorted.length < 1) return false;
|
|
9151
9265
|
if (iterationType === "RECURSIVE") {
|
|
9152
9266
|
const buildBalanceBST = (l, r) => {
|
|
9153
9267
|
if (l > r) return;
|
|
9154
9268
|
const m = l + Math.floor((r - l) / 2);
|
|
9155
9269
|
const midNode = sorted[m];
|
|
9156
|
-
this.add(
|
|
9270
|
+
if (this._isMapMode) this.add(midNode.key);
|
|
9271
|
+
else this.add([midNode.key, midNode.value]);
|
|
9157
9272
|
buildBalanceBST(l, m - 1);
|
|
9158
9273
|
buildBalanceBST(m + 1, r);
|
|
9159
9274
|
};
|
|
@@ -9168,7 +9283,8 @@ var dataStructureTyped = (() => {
|
|
|
9168
9283
|
if (l <= r) {
|
|
9169
9284
|
const m = l + Math.floor((r - l) / 2);
|
|
9170
9285
|
const midNode = sorted[m];
|
|
9171
|
-
this.add(
|
|
9286
|
+
if (this._isMapMode) this.add(midNode.key);
|
|
9287
|
+
else this.add([midNode.key, midNode.value]);
|
|
9172
9288
|
stack.push([m + 1, r]);
|
|
9173
9289
|
stack.push([l, m - 1]);
|
|
9174
9290
|
}
|
|
@@ -9236,7 +9352,7 @@ var dataStructureTyped = (() => {
|
|
|
9236
9352
|
/**
|
|
9237
9353
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
9238
9354
|
* root.
|
|
9239
|
-
* @param {
|
|
9355
|
+
* @param {OptNode<NODE>} v - v is a parameter of type NODE or undefined.
|
|
9240
9356
|
*/
|
|
9241
9357
|
_setRoot(v) {
|
|
9242
9358
|
if (v) {
|
|
@@ -9844,7 +9960,7 @@ var dataStructureTyped = (() => {
|
|
|
9844
9960
|
/**
|
|
9845
9961
|
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
|
|
9846
9962
|
* entries, or raw elements.
|
|
9847
|
-
* @param
|
|
9963
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
9848
9964
|
* iterable object that can contain either keys, nodes, entries, or raw elements. These elements will
|
|
9849
9965
|
* be used to initialize the AVLTree.
|
|
9850
9966
|
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
@@ -9852,9 +9968,9 @@ var dataStructureTyped = (() => {
|
|
|
9852
9968
|
* keys), `allowDuplicates` (a boolean indicating whether duplicate keys are allowed), and
|
|
9853
9969
|
* `nodeBuilder` (
|
|
9854
9970
|
*/
|
|
9855
|
-
constructor(
|
|
9971
|
+
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
9856
9972
|
super([], options);
|
|
9857
|
-
if (
|
|
9973
|
+
if (keysNodesEntriesOrRaws) super.addMany(keysNodesEntriesOrRaws);
|
|
9858
9974
|
}
|
|
9859
9975
|
/**
|
|
9860
9976
|
* The function creates a new AVL tree node with the given key and value.
|
|
@@ -9878,19 +9994,20 @@ var dataStructureTyped = (() => {
|
|
|
9878
9994
|
createTree(options) {
|
|
9879
9995
|
return new _AVLTree([], __spreadValues({
|
|
9880
9996
|
iterationType: this.iterationType,
|
|
9997
|
+
isMapMode: this._isMapMode,
|
|
9881
9998
|
comparator: this._comparator,
|
|
9882
9999
|
toEntryFn: this._toEntryFn
|
|
9883
10000
|
}, options));
|
|
9884
10001
|
}
|
|
9885
10002
|
/**
|
|
9886
10003
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
9887
|
-
* @param {
|
|
9888
|
-
* `
|
|
9889
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
10004
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
10005
|
+
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
10006
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
|
|
9890
10007
|
* an instance of the `AVLTreeNode` class.
|
|
9891
10008
|
*/
|
|
9892
|
-
isNode(
|
|
9893
|
-
return
|
|
10009
|
+
isNode(keyNodeEntryOrRaw) {
|
|
10010
|
+
return keyNodeEntryOrRaw instanceof AVLTreeNode;
|
|
9894
10011
|
}
|
|
9895
10012
|
/**
|
|
9896
10013
|
* Time Complexity: O(log n)
|
|
@@ -9898,17 +10015,17 @@ var dataStructureTyped = (() => {
|
|
|
9898
10015
|
*
|
|
9899
10016
|
* The function overrides the add method of a class and inserts a key-value pair into a data
|
|
9900
10017
|
* structure, then balances the path.
|
|
9901
|
-
* @param {
|
|
9902
|
-
* `
|
|
10018
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
10019
|
+
* `keyNodeEntryOrRaw` can accept values of type `R`, `BTNRep<K, V, NODE>`, or
|
|
9903
10020
|
* `RawElement`.
|
|
9904
10021
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
9905
10022
|
* the key or node being added to the data structure.
|
|
9906
10023
|
* @returns The method is returning a boolean value.
|
|
9907
10024
|
*/
|
|
9908
|
-
add(
|
|
9909
|
-
if (
|
|
9910
|
-
const inserted = super.add(
|
|
9911
|
-
if (inserted) this._balancePath(
|
|
10025
|
+
add(keyNodeEntryOrRaw, value) {
|
|
10026
|
+
if (keyNodeEntryOrRaw === null) return false;
|
|
10027
|
+
const inserted = super.add(keyNodeEntryOrRaw, value);
|
|
10028
|
+
if (inserted) this._balancePath(keyNodeEntryOrRaw);
|
|
9912
10029
|
return inserted;
|
|
9913
10030
|
}
|
|
9914
10031
|
/**
|
|
@@ -9917,15 +10034,15 @@ var dataStructureTyped = (() => {
|
|
|
9917
10034
|
*
|
|
9918
10035
|
* The function overrides the delete method in a TypeScript class, performs deletion, and then
|
|
9919
10036
|
* balances the tree if necessary.
|
|
9920
|
-
* @param {
|
|
10037
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw`
|
|
9921
10038
|
* parameter in the `override delete` method can be one of the following types:
|
|
9922
10039
|
* @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
|
|
9923
10040
|
* method from the superclass (presumably a parent class) with the provided `predicate`, which could
|
|
9924
10041
|
* be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
|
|
9925
10042
|
* `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
|
|
9926
10043
|
*/
|
|
9927
|
-
delete(
|
|
9928
|
-
const deletedResults = super.delete(
|
|
10044
|
+
delete(keyNodeEntryOrRaw) {
|
|
10045
|
+
const deletedResults = super.delete(keyNodeEntryOrRaw);
|
|
9929
10046
|
for (const { needBalanced } of deletedResults) {
|
|
9930
10047
|
if (needBalanced) {
|
|
9931
10048
|
this._balancePath(needBalanced);
|
|
@@ -9939,10 +10056,10 @@ var dataStructureTyped = (() => {
|
|
|
9939
10056
|
*
|
|
9940
10057
|
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a
|
|
9941
10058
|
* binary search tree.
|
|
9942
|
-
* @param {R |
|
|
10059
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents either a node
|
|
9943
10060
|
* object (`NODE`) or a key-value pair (`R`) that is being swapped with another node.
|
|
9944
|
-
* @param {R |
|
|
9945
|
-
* `R` or an instance of `
|
|
10061
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter is either an instance of
|
|
10062
|
+
* `R` or an instance of `BSTNOptKeyOrNode<K, NODE>`.
|
|
9946
10063
|
* @returns The method is returning the `destNodeEnsured` object if both `srcNodeEnsured` and
|
|
9947
10064
|
* `destNodeEnsured` are truthy. Otherwise, it returns `undefined`.
|
|
9948
10065
|
*/
|
|
@@ -9955,10 +10072,10 @@ var dataStructureTyped = (() => {
|
|
|
9955
10072
|
if (tempNode) {
|
|
9956
10073
|
tempNode.height = height;
|
|
9957
10074
|
destNodeEnsured.key = srcNodeEnsured.key;
|
|
9958
|
-
destNodeEnsured.value = srcNodeEnsured.value;
|
|
10075
|
+
if (!this._isMapMode) destNodeEnsured.value = srcNodeEnsured.value;
|
|
9959
10076
|
destNodeEnsured.height = srcNodeEnsured.height;
|
|
9960
10077
|
srcNodeEnsured.key = tempNode.key;
|
|
9961
|
-
srcNodeEnsured.value = tempNode.value;
|
|
10078
|
+
if (!this._isMapMode) srcNodeEnsured.value = tempNode.value;
|
|
9962
10079
|
srcNodeEnsured.height = tempNode.height;
|
|
9963
10080
|
}
|
|
9964
10081
|
return destNodeEnsured;
|
|
@@ -10160,8 +10277,8 @@ var dataStructureTyped = (() => {
|
|
|
10160
10277
|
*
|
|
10161
10278
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
10162
10279
|
* to restore balance in an AVL tree after inserting a node.
|
|
10163
|
-
* @param {
|
|
10164
|
-
* `
|
|
10280
|
+
* @param {BTNRep<K, V, NODE> | R} node - The `node` parameter can be of type `R` or
|
|
10281
|
+
* `BTNRep<K, V, NODE>`.
|
|
10165
10282
|
*/
|
|
10166
10283
|
_balancePath(node) {
|
|
10167
10284
|
node = this.ensureNode(node);
|
|
@@ -10247,7 +10364,7 @@ var dataStructureTyped = (() => {
|
|
|
10247
10364
|
var RedBlackTree = class _RedBlackTree extends BST {
|
|
10248
10365
|
/**
|
|
10249
10366
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
|
|
10250
|
-
* @param
|
|
10367
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
10251
10368
|
* iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
|
|
10252
10369
|
* initialize the RBTree with the provided elements.
|
|
10253
10370
|
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
@@ -10255,12 +10372,12 @@ var dataStructureTyped = (() => {
|
|
|
10255
10372
|
* configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
|
|
10256
10373
|
* depend on the implementation
|
|
10257
10374
|
*/
|
|
10258
|
-
constructor(
|
|
10375
|
+
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
10259
10376
|
super([], options);
|
|
10260
10377
|
__publicField(this, "_root");
|
|
10261
10378
|
this._root = this.NIL;
|
|
10262
|
-
if (
|
|
10263
|
-
this.addMany(
|
|
10379
|
+
if (keysNodesEntriesOrRaws) {
|
|
10380
|
+
this.addMany(keysNodesEntriesOrRaws);
|
|
10264
10381
|
}
|
|
10265
10382
|
}
|
|
10266
10383
|
/**
|
|
@@ -10296,6 +10413,7 @@ var dataStructureTyped = (() => {
|
|
|
10296
10413
|
createTree(options) {
|
|
10297
10414
|
return new _RedBlackTree([], __spreadValues({
|
|
10298
10415
|
iterationType: this.iterationType,
|
|
10416
|
+
isMapMode: this._isMapMode,
|
|
10299
10417
|
comparator: this._comparator,
|
|
10300
10418
|
toEntryFn: this._toEntryFn
|
|
10301
10419
|
}, options));
|
|
@@ -10305,13 +10423,13 @@ var dataStructureTyped = (() => {
|
|
|
10305
10423
|
* Space Complexity: O(1)
|
|
10306
10424
|
*
|
|
10307
10425
|
* The function checks if the input is an instance of the RedBlackTreeNode class.
|
|
10308
|
-
* @param {
|
|
10309
|
-
* `
|
|
10310
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
10426
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
10427
|
+
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
10428
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
|
|
10311
10429
|
* an instance of the `RedBlackTreeNode` class.
|
|
10312
10430
|
*/
|
|
10313
|
-
isNode(
|
|
10314
|
-
return
|
|
10431
|
+
isNode(keyNodeEntryOrRaw) {
|
|
10432
|
+
return keyNodeEntryOrRaw instanceof RedBlackTreeNode;
|
|
10315
10433
|
}
|
|
10316
10434
|
// /**
|
|
10317
10435
|
// * Time Complexity: O(1)
|
|
@@ -10322,29 +10440,29 @@ var dataStructureTyped = (() => {
|
|
|
10322
10440
|
// * Time Complexity: O(1)
|
|
10323
10441
|
// * Space Complexity: O(1)
|
|
10324
10442
|
// *
|
|
10325
|
-
// * The function `
|
|
10443
|
+
// * The function `keyValueNodeEntryRawToNodeAndValue` takes a key, value, or entry and returns a node if it is
|
|
10326
10444
|
// * valid, otherwise it returns undefined.
|
|
10327
|
-
// * @param {
|
|
10328
|
-
// * @param {V} [value] - The value associated with the key (if `
|
|
10445
|
+
// * @param {BTNRep<K, V, NODE>} keyNodeEntryOrRaw - The key, value, or entry to convert.
|
|
10446
|
+
// * @param {V} [value] - The value associated with the key (if `keyNodeEntryOrRaw` is a key).
|
|
10329
10447
|
// * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
|
|
10330
10448
|
// */
|
|
10331
|
-
// override
|
|
10449
|
+
// override keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): NODE | undefined {
|
|
10332
10450
|
//
|
|
10333
|
-
// if (
|
|
10334
|
-
// if (this.isNode(
|
|
10451
|
+
// if (keyNodeEntryOrRaw === null || keyNodeEntryOrRaw === undefined) return;
|
|
10452
|
+
// if (this.isNode(keyNodeEntryOrRaw)) return keyNodeEntryOrRaw;
|
|
10335
10453
|
//
|
|
10336
10454
|
// if (this._toEntryFn) {
|
|
10337
|
-
// const [key, entryValue] = this._toEntryFn(
|
|
10338
|
-
// if (this.isKey(key)) return this.createNode(key,
|
|
10455
|
+
// const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw as R);
|
|
10456
|
+
// if (this.isKey(key)) return this.createNode(key, value ?? entryValue, 'RED');
|
|
10339
10457
|
// }
|
|
10340
10458
|
//
|
|
10341
|
-
// if (this.isEntry(
|
|
10342
|
-
// const [key, value] =
|
|
10459
|
+
// if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
10460
|
+
// const [key, value] = keyNodeEntryOrRaw;
|
|
10343
10461
|
// if (key === undefined || key === null) return;
|
|
10344
10462
|
// else return this.createNode(key, value, 'RED');
|
|
10345
10463
|
// }
|
|
10346
10464
|
//
|
|
10347
|
-
// if (this.isKey(
|
|
10465
|
+
// if (this.isKey(keyNodeEntryOrRaw)) return this.createNode(keyNodeEntryOrRaw, value, 'RED');
|
|
10348
10466
|
//
|
|
10349
10467
|
// return ;
|
|
10350
10468
|
// }
|
|
@@ -10365,8 +10483,8 @@ var dataStructureTyped = (() => {
|
|
|
10365
10483
|
*
|
|
10366
10484
|
* The function adds a new node to a binary search tree and returns true if the node was successfully
|
|
10367
10485
|
* added.
|
|
10368
|
-
* @param {
|
|
10369
|
-
* `
|
|
10486
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
10487
|
+
* `keyNodeEntryOrRaw` can accept a value of type `R` or `BTNRep<K, V, NODE>`.
|
|
10370
10488
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
10371
10489
|
* the key in the data structure. It represents the value that you want to add or update in the data
|
|
10372
10490
|
* structure.
|
|
@@ -10374,8 +10492,8 @@ var dataStructureTyped = (() => {
|
|
|
10374
10492
|
* the method returns true. If the node already exists and its value is updated, the method also
|
|
10375
10493
|
* returns true. If the node cannot be added or updated, the method returns false.
|
|
10376
10494
|
*/
|
|
10377
|
-
add(
|
|
10378
|
-
const newNode = this.
|
|
10495
|
+
add(keyNodeEntryOrRaw, value) {
|
|
10496
|
+
const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
10379
10497
|
if (!this.isRealNode(newNode)) return false;
|
|
10380
10498
|
const insertStatus = this._insert(newNode);
|
|
10381
10499
|
if (insertStatus === "CREATED") {
|
|
@@ -10384,6 +10502,7 @@ var dataStructureTyped = (() => {
|
|
|
10384
10502
|
} else {
|
|
10385
10503
|
return false;
|
|
10386
10504
|
}
|
|
10505
|
+
if (this._isMapMode) this._setValue(newNode.key, newValue);
|
|
10387
10506
|
this._size++;
|
|
10388
10507
|
return true;
|
|
10389
10508
|
} else return insertStatus === "UPDATED";
|
|
@@ -10394,7 +10513,7 @@ var dataStructureTyped = (() => {
|
|
|
10394
10513
|
*
|
|
10395
10514
|
* The function overrides the delete method in a binary tree data structure to remove a node based on
|
|
10396
10515
|
* a given predicate and maintain the binary search tree properties.
|
|
10397
|
-
* @param {
|
|
10516
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw`
|
|
10398
10517
|
* parameter in the `override delete` method is used to specify the condition or key based on which a
|
|
10399
10518
|
* node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
|
|
10400
10519
|
* function that determines which node(s) should be deleted.
|
|
@@ -10402,12 +10521,12 @@ var dataStructureTyped = (() => {
|
|
|
10402
10521
|
* objects. Each object in the array contains information about the deleted node and whether
|
|
10403
10522
|
* balancing is needed.
|
|
10404
10523
|
*/
|
|
10405
|
-
delete(
|
|
10406
|
-
if (
|
|
10524
|
+
delete(keyNodeEntryOrRaw) {
|
|
10525
|
+
if (keyNodeEntryOrRaw === null) return [];
|
|
10407
10526
|
const results = [];
|
|
10408
10527
|
let nodeToDelete;
|
|
10409
|
-
if (this.
|
|
10410
|
-
else nodeToDelete = this.isRealNode(
|
|
10528
|
+
if (this._isPredicate(keyNodeEntryOrRaw)) nodeToDelete = this.getNode(keyNodeEntryOrRaw);
|
|
10529
|
+
else nodeToDelete = this.isRealNode(keyNodeEntryOrRaw) ? keyNodeEntryOrRaw : this.getNode(keyNodeEntryOrRaw);
|
|
10411
10530
|
if (!nodeToDelete) {
|
|
10412
10531
|
return results;
|
|
10413
10532
|
}
|
|
@@ -10443,6 +10562,7 @@ var dataStructureTyped = (() => {
|
|
|
10443
10562
|
successor.color = nodeToDelete.color;
|
|
10444
10563
|
}
|
|
10445
10564
|
}
|
|
10565
|
+
if (this._isMapMode) this._store.delete(nodeToDelete.key);
|
|
10446
10566
|
this._size--;
|
|
10447
10567
|
if (originalColor === "BLACK") {
|
|
10448
10568
|
this._deleteFixup(replacementNode);
|
|
@@ -10756,16 +10876,16 @@ var dataStructureTyped = (() => {
|
|
|
10756
10876
|
var AVLTreeMultiMap = class _AVLTreeMultiMap extends AVLTree {
|
|
10757
10877
|
/**
|
|
10758
10878
|
* The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
|
|
10759
|
-
* @param
|
|
10879
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
10760
10880
|
* iterable object that can contain either keys, nodes, entries, or raw elements.
|
|
10761
10881
|
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
10762
10882
|
* behavior of the AVLTreeMultiMap. It can include properties such as `compareKeys` and
|
|
10763
10883
|
* `compareValues` functions to define custom comparison logic for keys and values, respectively.
|
|
10764
10884
|
*/
|
|
10765
|
-
constructor(
|
|
10885
|
+
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
10766
10886
|
super([], options);
|
|
10767
10887
|
__publicField(this, "_count", 0);
|
|
10768
|
-
if (
|
|
10888
|
+
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
10769
10889
|
}
|
|
10770
10890
|
/**
|
|
10771
10891
|
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
@@ -10812,25 +10932,26 @@ var dataStructureTyped = (() => {
|
|
|
10812
10932
|
createTree(options) {
|
|
10813
10933
|
return new _AVLTreeMultiMap([], __spreadValues({
|
|
10814
10934
|
iterationType: this.iterationType,
|
|
10935
|
+
isMapMode: this._isMapMode,
|
|
10815
10936
|
comparator: this._comparator,
|
|
10816
10937
|
toEntryFn: this._toEntryFn
|
|
10817
10938
|
}, options));
|
|
10818
10939
|
}
|
|
10819
10940
|
/**
|
|
10820
10941
|
* The function checks if the input is an instance of AVLTreeMultiMapNode.
|
|
10821
|
-
* @param {
|
|
10822
|
-
* `
|
|
10823
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
10942
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
10943
|
+
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
10944
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
|
|
10824
10945
|
* an instance of the `AVLTreeMultiMapNode` class.
|
|
10825
10946
|
*/
|
|
10826
|
-
isNode(
|
|
10827
|
-
return
|
|
10947
|
+
isNode(keyNodeEntryOrRaw) {
|
|
10948
|
+
return keyNodeEntryOrRaw instanceof AVLTreeMultiMapNode;
|
|
10828
10949
|
}
|
|
10829
10950
|
/**
|
|
10830
|
-
* The function `
|
|
10951
|
+
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
|
|
10831
10952
|
* a node object.
|
|
10832
|
-
* @param {
|
|
10833
|
-
* `
|
|
10953
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
10954
|
+
* `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
10834
10955
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
10835
10956
|
* `override` function. It represents the value associated with the key in the data structure. If no
|
|
10836
10957
|
* value is provided, it will default to `undefined`.
|
|
@@ -10838,20 +10959,25 @@ var dataStructureTyped = (() => {
|
|
|
10838
10959
|
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
10839
10960
|
* @returns either a NODE object or undefined.
|
|
10840
10961
|
*/
|
|
10841
|
-
|
|
10842
|
-
if (
|
|
10843
|
-
if (this.isNode(
|
|
10844
|
-
if (this.isEntry(
|
|
10845
|
-
const [key, entryValue] =
|
|
10846
|
-
if (key === void 0 || key === null) return;
|
|
10847
|
-
|
|
10962
|
+
keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count = 1) {
|
|
10963
|
+
if (keyNodeEntryOrRaw === void 0 || keyNodeEntryOrRaw === null) return [void 0, void 0];
|
|
10964
|
+
if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
|
|
10965
|
+
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
10966
|
+
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
10967
|
+
if (key === void 0 || key === null) return [void 0, void 0];
|
|
10968
|
+
const finalValue = value != null ? value : entryValue;
|
|
10969
|
+
return [this.createNode(key, finalValue, count), finalValue];
|
|
10848
10970
|
}
|
|
10849
|
-
if (this.
|
|
10850
|
-
|
|
10851
|
-
if (this.
|
|
10971
|
+
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, count), value];
|
|
10972
|
+
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
10973
|
+
if (this._toEntryFn) {
|
|
10974
|
+
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
10975
|
+
const finalValue = value != null ? value : entryValue;
|
|
10976
|
+
if (this.isKey(key)) return [this.createNode(key, finalValue, count), finalValue];
|
|
10977
|
+
}
|
|
10978
|
+
return [void 0, void 0];
|
|
10852
10979
|
}
|
|
10853
|
-
|
|
10854
|
-
return;
|
|
10980
|
+
return [void 0, void 0];
|
|
10855
10981
|
}
|
|
10856
10982
|
/**
|
|
10857
10983
|
* Time Complexity: O(log n)
|
|
@@ -10859,9 +10985,9 @@ var dataStructureTyped = (() => {
|
|
|
10859
10985
|
*
|
|
10860
10986
|
* The function overrides the add method of a TypeScript class to add a new node to a data structure
|
|
10861
10987
|
* and update the count.
|
|
10862
|
-
* @param {
|
|
10863
|
-
* `
|
|
10864
|
-
* can also accept a value of type `
|
|
10988
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
10989
|
+
* `keyNodeEntryOrRaw` parameter can accept a value of type `R`, which can be any type. It
|
|
10990
|
+
* can also accept a value of type `BTNRep<K, V, NODE>`, which represents a key, node,
|
|
10865
10991
|
* entry, or raw element
|
|
10866
10992
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
10867
10993
|
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
@@ -10870,11 +10996,11 @@ var dataStructureTyped = (() => {
|
|
|
10870
10996
|
* be added once. However, you can specify a different value for `count` if you want to add
|
|
10871
10997
|
* @returns a boolean value.
|
|
10872
10998
|
*/
|
|
10873
|
-
add(
|
|
10874
|
-
const newNode = this.
|
|
10999
|
+
add(keyNodeEntryOrRaw, value, count = 1) {
|
|
11000
|
+
const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
|
|
10875
11001
|
if (newNode === void 0) return false;
|
|
10876
11002
|
const orgNodeCount = (newNode == null ? void 0 : newNode.count) || 0;
|
|
10877
|
-
const inserted = super.add(newNode);
|
|
11003
|
+
const inserted = super.add(newNode, newValue);
|
|
10878
11004
|
if (inserted) {
|
|
10879
11005
|
this._count += orgNodeCount;
|
|
10880
11006
|
}
|
|
@@ -10886,9 +11012,9 @@ var dataStructureTyped = (() => {
|
|
|
10886
11012
|
*
|
|
10887
11013
|
* The function overrides the delete method in a binary tree data structure, handling deletion of
|
|
10888
11014
|
* nodes and maintaining balance in the tree.
|
|
10889
|
-
* @param {
|
|
11015
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `predicate`
|
|
10890
11016
|
* parameter in the `delete` method is used to specify the condition for deleting a node from the
|
|
10891
|
-
* binary tree. It can be a key, node,
|
|
11017
|
+
* binary tree. It can be a key, node, or entry that determines which
|
|
10892
11018
|
* node(s) should be deleted.
|
|
10893
11019
|
* @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
|
|
10894
11020
|
* boolean flag that determines whether to ignore the count of the node being deleted. If
|
|
@@ -10899,11 +11025,11 @@ var dataStructureTyped = (() => {
|
|
|
10899
11025
|
* method returns an array of `BinaryTreeDeleteResult` objects, each containing information about the
|
|
10900
11026
|
* deleted node and whether balancing is needed in the tree.
|
|
10901
11027
|
*/
|
|
10902
|
-
delete(
|
|
11028
|
+
delete(keyNodeEntryOrRaw, ignoreCount = false) {
|
|
10903
11029
|
var _a;
|
|
10904
11030
|
const deletedResult = [];
|
|
10905
11031
|
if (!this.root) return deletedResult;
|
|
10906
|
-
const curr = (_a = this.getNode(
|
|
11032
|
+
const curr = (_a = this.getNode(keyNodeEntryOrRaw)) != null ? _a : void 0;
|
|
10907
11033
|
if (!curr) return deletedResult;
|
|
10908
11034
|
const parent = (curr == null ? void 0 : curr.parent) ? curr.parent : void 0;
|
|
10909
11035
|
let needBalanced = void 0, orgCurrent = curr;
|
|
@@ -10979,7 +11105,8 @@ var dataStructureTyped = (() => {
|
|
|
10979
11105
|
if (l > r) return;
|
|
10980
11106
|
const m = l + Math.floor((r - l) / 2);
|
|
10981
11107
|
const midNode = sorted[m];
|
|
10982
|
-
this.add(midNode.key,
|
|
11108
|
+
if (this._isMapMode) this.add(midNode.key, void 0, midNode.count);
|
|
11109
|
+
else this.add(midNode.key, midNode.value, midNode.count);
|
|
10983
11110
|
buildBalanceBST(l, m - 1);
|
|
10984
11111
|
buildBalanceBST(m + 1, r);
|
|
10985
11112
|
};
|
|
@@ -10994,7 +11121,8 @@ var dataStructureTyped = (() => {
|
|
|
10994
11121
|
if (l <= r) {
|
|
10995
11122
|
const m = l + Math.floor((r - l) / 2);
|
|
10996
11123
|
const midNode = sorted[m];
|
|
10997
|
-
this.add(midNode.key,
|
|
11124
|
+
if (this._isMapMode) this.add(midNode.key, void 0, midNode.count);
|
|
11125
|
+
else this.add(midNode.key, midNode.value, midNode.count);
|
|
10998
11126
|
stack.push([m + 1, r]);
|
|
10999
11127
|
stack.push([l, m - 1]);
|
|
11000
11128
|
}
|
|
@@ -11012,7 +11140,9 @@ var dataStructureTyped = (() => {
|
|
|
11012
11140
|
*/
|
|
11013
11141
|
clone() {
|
|
11014
11142
|
const cloned = this.createTree();
|
|
11015
|
-
this.bfs((node) => cloned.add(node.key,
|
|
11143
|
+
if (this._isMapMode) this.bfs((node) => cloned.add(node.key, void 0, node.count));
|
|
11144
|
+
else this.bfs((node) => cloned.add(node.key, node.value, node.count));
|
|
11145
|
+
if (this._isMapMode) cloned._store = this._store;
|
|
11016
11146
|
return cloned;
|
|
11017
11147
|
}
|
|
11018
11148
|
/**
|
|
@@ -11021,9 +11151,9 @@ var dataStructureTyped = (() => {
|
|
|
11021
11151
|
*
|
|
11022
11152
|
* The `_swapProperties` function swaps the properties (key, value, count, height) between two nodes
|
|
11023
11153
|
* in a binary search tree.
|
|
11024
|
-
* @param {R |
|
|
11154
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
|
|
11025
11155
|
* that will be swapped with the `destNode`.
|
|
11026
|
-
* @param {R |
|
|
11156
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
|
|
11027
11157
|
* node where the properties will be swapped with the source node.
|
|
11028
11158
|
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
11029
11159
|
* If either `srcNode` or `destNode` is undefined, it returns `undefined`.
|
|
@@ -11037,11 +11167,11 @@ var dataStructureTyped = (() => {
|
|
|
11037
11167
|
if (tempNode) {
|
|
11038
11168
|
tempNode.height = height;
|
|
11039
11169
|
destNode.key = srcNode.key;
|
|
11040
|
-
destNode.value = srcNode.value;
|
|
11170
|
+
if (!this._isMapMode) destNode.value = srcNode.value;
|
|
11041
11171
|
destNode.count = srcNode.count;
|
|
11042
11172
|
destNode.height = srcNode.height;
|
|
11043
11173
|
srcNode.key = tempNode.key;
|
|
11044
|
-
srcNode.value = tempNode.value;
|
|
11174
|
+
if (!this._isMapMode) srcNode.value = tempNode.value;
|
|
11045
11175
|
srcNode.count = tempNode.count;
|
|
11046
11176
|
srcNode.height = tempNode.height;
|
|
11047
11177
|
}
|
|
@@ -11104,17 +11234,17 @@ var dataStructureTyped = (() => {
|
|
|
11104
11234
|
var TreeMultiMap = class _TreeMultiMap extends RedBlackTree {
|
|
11105
11235
|
/**
|
|
11106
11236
|
* The constructor function initializes a TreeMultiMap object with optional initial data.
|
|
11107
|
-
* @param
|
|
11237
|
+
* @param keysNodesEntriesOrRaws - The parameter `keysNodesEntriesOrRaws` is an
|
|
11108
11238
|
* iterable that can contain keys, nodes, entries, or raw elements. It is used to initialize the
|
|
11109
11239
|
* TreeMultiMap with initial data.
|
|
11110
11240
|
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
11111
11241
|
* behavior of the `TreeMultiMap` constructor. It can include properties such as `compareKeys` and
|
|
11112
11242
|
* `compareValues`, which are functions used to compare keys and values respectively.
|
|
11113
11243
|
*/
|
|
11114
|
-
constructor(
|
|
11244
|
+
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
11115
11245
|
super([], options);
|
|
11116
11246
|
__publicField(this, "_count", 0);
|
|
11117
|
-
if (
|
|
11247
|
+
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
11118
11248
|
}
|
|
11119
11249
|
// TODO the _count is not accurate after nodes count modified
|
|
11120
11250
|
/**
|
|
@@ -11164,15 +11294,16 @@ var dataStructureTyped = (() => {
|
|
|
11164
11294
|
createTree(options) {
|
|
11165
11295
|
return new _TreeMultiMap([], __spreadValues({
|
|
11166
11296
|
iterationType: this.iterationType,
|
|
11297
|
+
isMapMode: this._isMapMode,
|
|
11167
11298
|
comparator: this._comparator,
|
|
11168
11299
|
toEntryFn: this._toEntryFn
|
|
11169
11300
|
}, options));
|
|
11170
11301
|
}
|
|
11171
11302
|
/**
|
|
11172
|
-
* The function `
|
|
11303
|
+
* The function `keyValueNodeEntryRawToNodeAndValue` takes in a key, value, and count and returns a
|
|
11173
11304
|
* node based on the input.
|
|
11174
|
-
* @param {
|
|
11175
|
-
* `
|
|
11305
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
11306
|
+
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
11176
11307
|
* @param {V} [value] - The `value` parameter is an optional value that represents the value
|
|
11177
11308
|
* associated with the key in the node. It is used when creating a new node or updating the value of
|
|
11178
11309
|
* an existing node.
|
|
@@ -11180,30 +11311,32 @@ var dataStructureTyped = (() => {
|
|
|
11180
11311
|
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
11181
11312
|
* @returns either a NODE object or undefined.
|
|
11182
11313
|
*/
|
|
11183
|
-
|
|
11184
|
-
if (
|
|
11185
|
-
if (this.isNode(
|
|
11186
|
-
if (this.isEntry(
|
|
11187
|
-
const [key, entryValue] =
|
|
11188
|
-
if (key === void 0 || key === null) return;
|
|
11189
|
-
|
|
11314
|
+
keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count = 1) {
|
|
11315
|
+
if (keyNodeEntryOrRaw === void 0 || keyNodeEntryOrRaw === null) return [void 0, void 0];
|
|
11316
|
+
if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
|
|
11317
|
+
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
11318
|
+
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
11319
|
+
if (key === void 0 || key === null) return [void 0, void 0];
|
|
11320
|
+
const finalValue = value != null ? value : entryValue;
|
|
11321
|
+
if (this.isKey(key)) return [this.createNode(key, finalValue, "BLACK", count), finalValue];
|
|
11190
11322
|
}
|
|
11191
11323
|
if (this._toEntryFn) {
|
|
11192
|
-
const [key, entryValue] = this._toEntryFn(
|
|
11193
|
-
|
|
11324
|
+
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
11325
|
+
const finalValue = value != null ? value : entryValue;
|
|
11326
|
+
if (this.isKey(key)) return [this.createNode(key, finalValue, "BLACK", count), finalValue];
|
|
11194
11327
|
}
|
|
11195
|
-
if (this.isKey(
|
|
11196
|
-
return;
|
|
11328
|
+
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, "BLACK", count), value];
|
|
11329
|
+
return [void 0, void 0];
|
|
11197
11330
|
}
|
|
11198
11331
|
/**
|
|
11199
11332
|
* The function checks if the input is an instance of the TreeMultiMapNode class.
|
|
11200
|
-
* @param {
|
|
11201
|
-
* `
|
|
11202
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
11333
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
11334
|
+
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
11335
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
|
|
11203
11336
|
* an instance of the `TreeMultiMapNode` class.
|
|
11204
11337
|
*/
|
|
11205
|
-
isNode(
|
|
11206
|
-
return
|
|
11338
|
+
isNode(keyNodeEntryOrRaw) {
|
|
11339
|
+
return keyNodeEntryOrRaw instanceof TreeMultiMapNode;
|
|
11207
11340
|
}
|
|
11208
11341
|
/**
|
|
11209
11342
|
* Time Complexity: O(log n)
|
|
@@ -11211,8 +11344,8 @@ var dataStructureTyped = (() => {
|
|
|
11211
11344
|
*
|
|
11212
11345
|
* The function overrides the add method of a class and adds a new node to a data structure, updating
|
|
11213
11346
|
* the count and returning a boolean indicating success.
|
|
11214
|
-
* @param {
|
|
11215
|
-
* `
|
|
11347
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
11348
|
+
* `keyNodeEntryOrRaw` parameter can accept one of the following types:
|
|
11216
11349
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
11217
11350
|
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
11218
11351
|
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
@@ -11221,10 +11354,10 @@ var dataStructureTyped = (() => {
|
|
|
11221
11354
|
* @returns The method is returning a boolean value. It returns true if the addition of the new node
|
|
11222
11355
|
* was successful, and false otherwise.
|
|
11223
11356
|
*/
|
|
11224
|
-
add(
|
|
11225
|
-
const newNode = this.
|
|
11357
|
+
add(keyNodeEntryOrRaw, value, count = 1) {
|
|
11358
|
+
const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
|
|
11226
11359
|
const orgCount = (newNode == null ? void 0 : newNode.count) || 0;
|
|
11227
|
-
const isSuccessAdded = super.add(newNode);
|
|
11360
|
+
const isSuccessAdded = super.add(newNode, newValue);
|
|
11228
11361
|
if (isSuccessAdded) {
|
|
11229
11362
|
this._count += orgCount;
|
|
11230
11363
|
return true;
|
|
@@ -11238,22 +11371,21 @@ var dataStructureTyped = (() => {
|
|
|
11238
11371
|
*
|
|
11239
11372
|
* The function `delete` in TypeScript overrides the deletion operation in a binary tree data
|
|
11240
11373
|
* structure, handling cases where nodes have children and maintaining balance in the tree.
|
|
11241
|
-
* @param {
|
|
11374
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `predicate`
|
|
11242
11375
|
* parameter in the `delete` method is used to specify the condition or key based on which a node
|
|
11243
|
-
* should be deleted from the binary tree. It can be a key, a node, an entry
|
|
11244
|
-
* function.
|
|
11376
|
+
* should be deleted from the binary tree. It can be a key, a node, or an entry.
|
|
11245
11377
|
* @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
|
|
11246
11378
|
* boolean flag that determines whether to ignore the count of nodes when performing deletion. If
|
|
11247
11379
|
* `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If
|
|
11248
11380
|
* `ignoreCount` is `false
|
|
11249
11381
|
* @returns The `override delete` method returns an array of `BinaryTreeDeleteResult<NODE>` objects.
|
|
11250
11382
|
*/
|
|
11251
|
-
delete(
|
|
11252
|
-
if (
|
|
11383
|
+
delete(keyNodeEntryOrRaw, ignoreCount = false) {
|
|
11384
|
+
if (keyNodeEntryOrRaw === null) return [];
|
|
11253
11385
|
const results = [];
|
|
11254
11386
|
let nodeToDelete;
|
|
11255
|
-
if (this.
|
|
11256
|
-
else nodeToDelete = this.isRealNode(
|
|
11387
|
+
if (this._isPredicate(keyNodeEntryOrRaw)) nodeToDelete = this.getNode(keyNodeEntryOrRaw);
|
|
11388
|
+
else nodeToDelete = this.isRealNode(keyNodeEntryOrRaw) ? keyNodeEntryOrRaw : this.getNode(keyNodeEntryOrRaw);
|
|
11257
11389
|
if (!nodeToDelete) {
|
|
11258
11390
|
return results;
|
|
11259
11391
|
}
|
|
@@ -11361,7 +11493,8 @@ var dataStructureTyped = (() => {
|
|
|
11361
11493
|
if (l > r) return;
|
|
11362
11494
|
const m = l + Math.floor((r - l) / 2);
|
|
11363
11495
|
const midNode = sorted[m];
|
|
11364
|
-
this.add(midNode.key,
|
|
11496
|
+
if (this._isMapMode) this.add(midNode.key, void 0, midNode.count);
|
|
11497
|
+
else this.add(midNode.key, midNode.value, midNode.count);
|
|
11365
11498
|
buildBalanceBST(l, m - 1);
|
|
11366
11499
|
buildBalanceBST(m + 1, r);
|
|
11367
11500
|
};
|
|
@@ -11376,7 +11509,8 @@ var dataStructureTyped = (() => {
|
|
|
11376
11509
|
if (l <= r) {
|
|
11377
11510
|
const m = l + Math.floor((r - l) / 2);
|
|
11378
11511
|
const midNode = sorted[m];
|
|
11379
|
-
this.add(midNode.key,
|
|
11512
|
+
if (this._isMapMode) this.add(midNode.key, void 0, midNode.count);
|
|
11513
|
+
else this.add(midNode.key, midNode.value, midNode.count);
|
|
11380
11514
|
stack.push([m + 1, r]);
|
|
11381
11515
|
stack.push([l, m - 1]);
|
|
11382
11516
|
}
|
|
@@ -11394,7 +11528,8 @@ var dataStructureTyped = (() => {
|
|
|
11394
11528
|
*/
|
|
11395
11529
|
clone() {
|
|
11396
11530
|
const cloned = this.createTree();
|
|
11397
|
-
this.bfs((node) => cloned.add(node.key,
|
|
11531
|
+
this.bfs((node) => cloned.add(node.key, void 0, node.count));
|
|
11532
|
+
if (this._isMapMode) cloned._store = this._store;
|
|
11398
11533
|
return cloned;
|
|
11399
11534
|
}
|
|
11400
11535
|
/**
|
|
@@ -11403,10 +11538,10 @@ var dataStructureTyped = (() => {
|
|
|
11403
11538
|
*
|
|
11404
11539
|
* The `_swapProperties` function swaps the properties (key, value, count, color) between two nodes
|
|
11405
11540
|
* in a binary search tree.
|
|
11406
|
-
* @param {R |
|
|
11541
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
|
|
11407
11542
|
* that will be swapped with the `destNode`. It can be either an instance of the `R` class or an
|
|
11408
|
-
* instance of the `
|
|
11409
|
-
* @param {R |
|
|
11543
|
+
* instance of the `BSTNOptKeyOrNode<K, NODE>` class.
|
|
11544
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
|
|
11410
11545
|
* node where the properties will be swapped with the source node.
|
|
11411
11546
|
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
11412
11547
|
* If either `srcNode` or `destNode` is undefined, it returns undefined.
|
|
@@ -11420,11 +11555,11 @@ var dataStructureTyped = (() => {
|
|
|
11420
11555
|
if (tempNode) {
|
|
11421
11556
|
tempNode.color = color;
|
|
11422
11557
|
destNode.key = srcNode.key;
|
|
11423
|
-
destNode.value = srcNode.value;
|
|
11558
|
+
if (!this._isMapMode) destNode.value = srcNode.value;
|
|
11424
11559
|
destNode.count = srcNode.count;
|
|
11425
11560
|
destNode.color = srcNode.color;
|
|
11426
11561
|
srcNode.key = tempNode.key;
|
|
11427
|
-
srcNode.value = tempNode.value;
|
|
11562
|
+
if (!this._isMapMode) srcNode.value = tempNode.value;
|
|
11428
11563
|
srcNode.count = tempNode.count;
|
|
11429
11564
|
srcNode.color = tempNode.color;
|
|
11430
11565
|
}
|
|
@@ -12462,9 +12597,9 @@ var dataStructureTyped = (() => {
|
|
|
12462
12597
|
*
|
|
12463
12598
|
*/
|
|
12464
12599
|
getHeight() {
|
|
12465
|
-
const
|
|
12600
|
+
const startNode = this.root;
|
|
12466
12601
|
let maxDepth = 0;
|
|
12467
|
-
if (
|
|
12602
|
+
if (startNode) {
|
|
12468
12603
|
const bfs = (node, level) => {
|
|
12469
12604
|
if (level > maxDepth) {
|
|
12470
12605
|
maxDepth = level;
|
|
@@ -12476,7 +12611,7 @@ var dataStructureTyped = (() => {
|
|
|
12476
12611
|
}
|
|
12477
12612
|
}
|
|
12478
12613
|
};
|
|
12479
|
-
bfs(
|
|
12614
|
+
bfs(startNode, 0);
|
|
12480
12615
|
}
|
|
12481
12616
|
return maxDepth;
|
|
12482
12617
|
}
|