data-structure-typed 1.47.6 → 1.47.7
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/.github/ISSUE_TEMPLATE/bug_report.md +10 -7
- package/.github/workflows/ci.yml +1 -1
- package/.github/workflows/release-package.yml +1 -1
- package/CHANGELOG.md +1 -1
- package/CODE_OF_CONDUCT.md +32 -10
- package/COMMANDS.md +3 -1
- package/CONTRIBUTING.md +4 -3
- package/README.md +103 -28
- package/SECURITY.md +1 -1
- package/benchmark/report.html +46 -1
- package/benchmark/report.json +563 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +40 -22
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +133 -119
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/cjs/data-structures/binary-tree/bst.js +137 -154
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +58 -137
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/cjs/data-structures/hash/hash-map.js +5 -8
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/cjs/data-structures/trie/trie.js +19 -4
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/cjs/types/common.d.ts +6 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +40 -22
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +133 -128
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/mjs/data-structures/binary-tree/bst.js +137 -154
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +59 -138
- package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/mjs/data-structures/hash/hash-map.js +5 -8
- package/dist/mjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/mjs/data-structures/trie/trie.js +20 -4
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/mjs/types/common.d.ts +6 -1
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/umd/data-structure-typed.js +422 -466
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +59 -39
- package/src/data-structures/binary-tree/binary-tree.ts +192 -180
- package/src/data-structures/binary-tree/bst.ts +157 -154
- package/src/data-structures/binary-tree/rb-tree.ts +78 -37
- package/src/data-structures/binary-tree/tree-multimap.ts +67 -145
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/data-structures/trie/trie.ts +23 -4
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +11 -1
- package/src/types/data-structures/hash/hash-map.ts +1 -2
- package/test/integration/{all-in-one.ts → all-in-one.test.ts} +1 -1
- package/test/integration/index.html +87 -0
- package/test/performance/data-structures/comparison/comparison.test.ts +5 -5
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +19 -19
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +50 -51
- package/test/unit/data-structures/binary-tree/bst.test.ts +49 -54
- package/test/unit/data-structures/binary-tree/overall.test.ts +17 -18
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +118 -66
- package/test/unit/unrestricted-interconversion.test.ts +61 -5
- package/tsconfig-cjs.json +1 -1
|
@@ -517,13 +517,7 @@ var dataStructureTyped = (() => {
|
|
|
517
517
|
|
|
518
518
|
// src/data-structures/hash/hash-map.ts
|
|
519
519
|
var HashMap = class _HashMap {
|
|
520
|
-
|
|
521
|
-
* The constructor initializes a HashMapLinkedNode with an optional iterable of key-value pairs.
|
|
522
|
-
* @param options - The `options` parameter is an object that contains the `elements` property. The
|
|
523
|
-
* `elements` property is an iterable that contains key-value pairs represented as arrays `[K, V]`.
|
|
524
|
-
*/
|
|
525
|
-
constructor(options = {
|
|
526
|
-
elements: [],
|
|
520
|
+
constructor(elements, options = {
|
|
527
521
|
hashFn: (key) => String(key),
|
|
528
522
|
objHashFn: (key) => key
|
|
529
523
|
}) {
|
|
@@ -537,7 +531,7 @@ var dataStructureTyped = (() => {
|
|
|
537
531
|
__publicField(this, "_size", 0);
|
|
538
532
|
this._sentinel = {};
|
|
539
533
|
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
540
|
-
const {
|
|
534
|
+
const { hashFn, objHashFn } = options;
|
|
541
535
|
this._hashFn = hashFn;
|
|
542
536
|
this._objHashFn = objHashFn;
|
|
543
537
|
if (elements) {
|
|
@@ -844,6 +838,9 @@ var dataStructureTyped = (() => {
|
|
|
844
838
|
node = node.next;
|
|
845
839
|
}
|
|
846
840
|
}
|
|
841
|
+
print() {
|
|
842
|
+
console.log([...this]);
|
|
843
|
+
}
|
|
847
844
|
/**
|
|
848
845
|
* Time Complexity: O(1)
|
|
849
846
|
* Space Complexity: O(1)
|
|
@@ -6849,55 +6846,27 @@ var dataStructureTyped = (() => {
|
|
|
6849
6846
|
|
|
6850
6847
|
// src/data-structures/binary-tree/binary-tree.ts
|
|
6851
6848
|
var BinaryTreeNode = class {
|
|
6852
|
-
/**
|
|
6853
|
-
* Creates a new instance of BinaryTreeNode.
|
|
6854
|
-
* @param {BTNKey} key - The key associated with the node.
|
|
6855
|
-
* @param {V} value - The value stored in the node.
|
|
6856
|
-
*/
|
|
6857
6849
|
constructor(key, value) {
|
|
6858
|
-
/**
|
|
6859
|
-
* The key associated with the node.
|
|
6860
|
-
*/
|
|
6861
6850
|
__publicField(this, "key");
|
|
6862
|
-
/**
|
|
6863
|
-
* The value stored in the node.
|
|
6864
|
-
*/
|
|
6865
6851
|
__publicField(this, "value");
|
|
6866
|
-
/**
|
|
6867
|
-
* The parent node of the current node.
|
|
6868
|
-
*/
|
|
6869
6852
|
__publicField(this, "parent");
|
|
6870
6853
|
__publicField(this, "_left");
|
|
6871
6854
|
__publicField(this, "_right");
|
|
6872
6855
|
this.key = key;
|
|
6873
6856
|
this.value = value;
|
|
6874
6857
|
}
|
|
6875
|
-
/**
|
|
6876
|
-
* Get the left child node.
|
|
6877
|
-
*/
|
|
6878
6858
|
get left() {
|
|
6879
6859
|
return this._left;
|
|
6880
6860
|
}
|
|
6881
|
-
/**
|
|
6882
|
-
* Set the left child node.
|
|
6883
|
-
* @param {N | null | undefined} v - The left child node.
|
|
6884
|
-
*/
|
|
6885
6861
|
set left(v) {
|
|
6886
6862
|
if (v) {
|
|
6887
6863
|
v.parent = this;
|
|
6888
6864
|
}
|
|
6889
6865
|
this._left = v;
|
|
6890
6866
|
}
|
|
6891
|
-
/**
|
|
6892
|
-
* Get the right child node.
|
|
6893
|
-
*/
|
|
6894
6867
|
get right() {
|
|
6895
6868
|
return this._right;
|
|
6896
6869
|
}
|
|
6897
|
-
/**
|
|
6898
|
-
* Set the right child node.
|
|
6899
|
-
* @param {N | null | undefined} v - The right child node.
|
|
6900
|
-
*/
|
|
6901
6870
|
set right(v) {
|
|
6902
6871
|
if (v) {
|
|
6903
6872
|
v.parent = this;
|
|
@@ -6923,8 +6892,13 @@ var dataStructureTyped = (() => {
|
|
|
6923
6892
|
};
|
|
6924
6893
|
var BinaryTree = class _BinaryTree {
|
|
6925
6894
|
/**
|
|
6926
|
-
*
|
|
6927
|
-
* @param
|
|
6895
|
+
* The constructor function initializes a binary tree object with optional elements and options.
|
|
6896
|
+
* @param [elements] - An optional iterable of BTNodeExemplar objects. These objects represent the
|
|
6897
|
+
* elements to be added to the binary tree.
|
|
6898
|
+
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
6899
|
+
* configuration options for the binary tree. In this case, it is of type
|
|
6900
|
+
* `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
|
|
6901
|
+
* required.
|
|
6928
6902
|
*/
|
|
6929
6903
|
constructor(elements, options) {
|
|
6930
6904
|
__publicField(this, "iterationType", "ITERATIVE" /* ITERATIVE */);
|
|
@@ -6939,17 +6913,11 @@ var dataStructureTyped = (() => {
|
|
|
6939
6913
|
}
|
|
6940
6914
|
this._size = 0;
|
|
6941
6915
|
if (elements)
|
|
6942
|
-
this.
|
|
6916
|
+
this.addMany(elements);
|
|
6943
6917
|
}
|
|
6944
|
-
/**
|
|
6945
|
-
* Get the root node of the binary tree.
|
|
6946
|
-
*/
|
|
6947
6918
|
get root() {
|
|
6948
6919
|
return this._root;
|
|
6949
6920
|
}
|
|
6950
|
-
/**
|
|
6951
|
-
* Get the number of nodes in the binary tree.
|
|
6952
|
-
*/
|
|
6953
6921
|
get size() {
|
|
6954
6922
|
return this._size;
|
|
6955
6923
|
}
|
|
@@ -6962,30 +6930,46 @@ var dataStructureTyped = (() => {
|
|
|
6962
6930
|
createNode(key, value) {
|
|
6963
6931
|
return new BinaryTreeNode(key, value);
|
|
6964
6932
|
}
|
|
6933
|
+
/**
|
|
6934
|
+
* The function creates a binary tree with the given options.
|
|
6935
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
6936
|
+
* behavior of the `BinaryTree` class. It is of type `Partial<BinaryTreeOptions>`, which means that
|
|
6937
|
+
* you can provide only a subset of the properties defined in the `BinaryTreeOptions` interface.
|
|
6938
|
+
* @returns a new instance of a binary tree.
|
|
6939
|
+
*/
|
|
6965
6940
|
createTree(options) {
|
|
6966
6941
|
return new _BinaryTree([], __spreadValues({ iterationType: this.iterationType }, options));
|
|
6967
6942
|
}
|
|
6968
6943
|
/**
|
|
6969
|
-
*
|
|
6970
|
-
*
|
|
6944
|
+
* The function checks if a given value is an entry in a binary tree node.
|
|
6945
|
+
* @param kne - BTNodeExemplar<V, N> - A generic type representing a node in a binary tree. It has
|
|
6946
|
+
* two type parameters V and N, representing the value and node type respectively.
|
|
6947
|
+
* @returns a boolean value.
|
|
6948
|
+
*/
|
|
6949
|
+
isEntry(kne) {
|
|
6950
|
+
return Array.isArray(kne) && kne.length === 2;
|
|
6951
|
+
}
|
|
6952
|
+
/**
|
|
6953
|
+
* Time Complexity O(log n) - O(n)
|
|
6954
|
+
* Space Complexity O(1)
|
|
6955
|
+
*/
|
|
6956
|
+
/**
|
|
6957
|
+
* Time Complexity O(log n) - O(n)
|
|
6958
|
+
* Space Complexity O(1)
|
|
6971
6959
|
*
|
|
6972
|
-
* The `add` function adds a new node
|
|
6973
|
-
*
|
|
6974
|
-
* @
|
|
6975
|
-
* following types:
|
|
6976
|
-
* @param {V} [value] - The value to be associated with the key or node being added to the binary
|
|
6977
|
-
* tree.
|
|
6978
|
-
* @returns The function `add` returns a node (`N`) if it was successfully inserted into the binary
|
|
6979
|
-
* tree, or `null` or `undefined` if the insertion was not successful.
|
|
6960
|
+
* The `add` function adds a new node to a binary tree, either by key or by providing a node object.
|
|
6961
|
+
* @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be one of the following:
|
|
6962
|
+
* @returns The function `add` returns the inserted node (`N`), `null`, or `undefined`.
|
|
6980
6963
|
*/
|
|
6981
|
-
add(
|
|
6964
|
+
add(keyOrNodeOrEntry) {
|
|
6965
|
+
let inserted, needInsert;
|
|
6982
6966
|
const _bfs = (root, newNode) => {
|
|
6983
6967
|
const queue = new Queue([root]);
|
|
6984
6968
|
while (queue.size > 0) {
|
|
6985
6969
|
const cur = queue.shift();
|
|
6986
6970
|
if (newNode && cur.key === newNode.key) {
|
|
6987
|
-
cur
|
|
6988
|
-
return;
|
|
6971
|
+
this._replaceNode(cur, newNode);
|
|
6972
|
+
return newNode;
|
|
6989
6973
|
}
|
|
6990
6974
|
const inserted2 = this._addTo(newNode, cur);
|
|
6991
6975
|
if (inserted2 !== void 0)
|
|
@@ -6996,13 +6980,21 @@ var dataStructureTyped = (() => {
|
|
|
6996
6980
|
queue.push(cur.right);
|
|
6997
6981
|
}
|
|
6998
6982
|
};
|
|
6999
|
-
|
|
7000
|
-
if (keyOrNode === null) {
|
|
6983
|
+
if (keyOrNodeOrEntry === null) {
|
|
7001
6984
|
needInsert = null;
|
|
7002
|
-
} else if (this.isNodeKey(
|
|
7003
|
-
needInsert = this.createNode(
|
|
7004
|
-
} else if (
|
|
7005
|
-
needInsert =
|
|
6985
|
+
} else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
6986
|
+
needInsert = this.createNode(keyOrNodeOrEntry);
|
|
6987
|
+
} else if (keyOrNodeOrEntry instanceof BinaryTreeNode) {
|
|
6988
|
+
needInsert = keyOrNodeOrEntry;
|
|
6989
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
6990
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
6991
|
+
if (key === void 0) {
|
|
6992
|
+
return;
|
|
6993
|
+
} else if (key === null) {
|
|
6994
|
+
needInsert = null;
|
|
6995
|
+
} else {
|
|
6996
|
+
needInsert = this.createNode(key, value);
|
|
6997
|
+
}
|
|
7006
6998
|
} else {
|
|
7007
6999
|
return;
|
|
7008
7000
|
}
|
|
@@ -7020,35 +7012,27 @@ var dataStructureTyped = (() => {
|
|
|
7020
7012
|
return inserted;
|
|
7021
7013
|
}
|
|
7022
7014
|
/**
|
|
7023
|
-
* Time Complexity: O(n)
|
|
7015
|
+
* Time Complexity: O(k log n) - O(k * n)
|
|
7024
7016
|
* Space Complexity: O(1)
|
|
7025
7017
|
* Comments: The time complexity for adding a node depends on the depth of the tree. In the best case (when the tree is empty), it's O(1). In the worst case (when the tree is a degenerate tree), it's O(n). The space complexity is constant.
|
|
7026
7018
|
*/
|
|
7027
7019
|
/**
|
|
7028
|
-
* Time Complexity: O(k
|
|
7020
|
+
* Time Complexity: O(k log n) - O(k * n)
|
|
7029
7021
|
* Space Complexity: O(1)
|
|
7030
7022
|
*
|
|
7031
|
-
* The `addMany`
|
|
7032
|
-
*
|
|
7033
|
-
* @param
|
|
7034
|
-
*
|
|
7035
|
-
*
|
|
7036
|
-
*
|
|
7037
|
-
* correspond to the keys or nodes being added. If provided, the values will be associated with the
|
|
7038
|
-
* keys or nodes during the add operation.
|
|
7039
|
-
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
7023
|
+
* The function `addMany` takes in an iterable of `BTNodeExemplar` objects, adds each object to the
|
|
7024
|
+
* current instance, and returns an array of the inserted nodes.
|
|
7025
|
+
* @param nodes - The `nodes` parameter is an iterable (such as an array or a set) of
|
|
7026
|
+
* `BTNodeExemplar<V, N>` objects.
|
|
7027
|
+
* @returns The function `addMany` returns an array of values, where each value is either of type
|
|
7028
|
+
* `N`, `null`, or `undefined`.
|
|
7040
7029
|
*/
|
|
7041
|
-
addMany(
|
|
7042
|
-
|
|
7043
|
-
|
|
7044
|
-
|
|
7045
|
-
|
|
7046
|
-
|
|
7047
|
-
return this.add(null);
|
|
7048
|
-
}
|
|
7049
|
-
const value = values == null ? void 0 : values[i];
|
|
7050
|
-
return this.add(keyOrNode, value);
|
|
7051
|
-
});
|
|
7030
|
+
addMany(nodes) {
|
|
7031
|
+
const inserted = [];
|
|
7032
|
+
for (const kne of nodes) {
|
|
7033
|
+
inserted.push(this.add(kne));
|
|
7034
|
+
}
|
|
7035
|
+
return inserted;
|
|
7052
7036
|
}
|
|
7053
7037
|
/**
|
|
7054
7038
|
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
@@ -7058,17 +7042,13 @@ var dataStructureTyped = (() => {
|
|
|
7058
7042
|
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
7059
7043
|
* Space Complexity: O(1)
|
|
7060
7044
|
*
|
|
7061
|
-
* The `refill` function clears the
|
|
7062
|
-
* @param
|
|
7063
|
-
* `
|
|
7064
|
-
* @param {N[] | Array<V>} [values] - The `data` parameter is an optional array of values that will be assigned to
|
|
7065
|
-
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
|
|
7066
|
-
* array. Each value in the `data` array will be assigned to the
|
|
7067
|
-
* @returns The method is returning a boolean value.
|
|
7045
|
+
* The `refill` function clears the current collection and adds new nodes, keys, or entries to it.
|
|
7046
|
+
* @param nodesOrKeysOrEntries - The parameter `nodesOrKeysOrEntries` is an iterable object that can
|
|
7047
|
+
* contain either `BTNodeExemplar` objects, keys, or entries.
|
|
7068
7048
|
*/
|
|
7069
|
-
refill(
|
|
7049
|
+
refill(nodesOrKeysOrEntries) {
|
|
7070
7050
|
this.clear();
|
|
7071
|
-
|
|
7051
|
+
this.addMany(nodesOrKeysOrEntries);
|
|
7072
7052
|
}
|
|
7073
7053
|
/**
|
|
7074
7054
|
* Time Complexity: O(n)
|
|
@@ -7114,7 +7094,7 @@ var dataStructureTyped = (() => {
|
|
|
7114
7094
|
const leftSubTreeRightMost = this.getRightMost(curr.left);
|
|
7115
7095
|
if (leftSubTreeRightMost) {
|
|
7116
7096
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
7117
|
-
orgCurrent = this.
|
|
7097
|
+
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
7118
7098
|
if (parentOfLeftSubTreeMax) {
|
|
7119
7099
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
7120
7100
|
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
@@ -7147,8 +7127,8 @@ var dataStructureTyped = (() => {
|
|
|
7147
7127
|
* @returns the depth of the `distNode` relative to the `beginRoot`.
|
|
7148
7128
|
*/
|
|
7149
7129
|
getDepth(distNode, beginRoot = this.root) {
|
|
7150
|
-
distNode = this.
|
|
7151
|
-
beginRoot = this.
|
|
7130
|
+
distNode = this.ensureNode(distNode);
|
|
7131
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7152
7132
|
let depth = 0;
|
|
7153
7133
|
while (distNode == null ? void 0 : distNode.parent) {
|
|
7154
7134
|
if (distNode === beginRoot) {
|
|
@@ -7178,7 +7158,7 @@ var dataStructureTyped = (() => {
|
|
|
7178
7158
|
* @returns the height of the binary tree.
|
|
7179
7159
|
*/
|
|
7180
7160
|
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
7181
|
-
beginRoot = this.
|
|
7161
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7182
7162
|
if (!beginRoot)
|
|
7183
7163
|
return -1;
|
|
7184
7164
|
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
@@ -7224,7 +7204,7 @@ var dataStructureTyped = (() => {
|
|
|
7224
7204
|
*/
|
|
7225
7205
|
getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
7226
7206
|
var _a, _b, _c;
|
|
7227
|
-
beginRoot = this.
|
|
7207
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7228
7208
|
if (!beginRoot)
|
|
7229
7209
|
return -1;
|
|
7230
7210
|
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
@@ -7311,7 +7291,7 @@ var dataStructureTyped = (() => {
|
|
|
7311
7291
|
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
7312
7292
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
7313
7293
|
callback = (node) => node;
|
|
7314
|
-
beginRoot = this.
|
|
7294
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7315
7295
|
if (!beginRoot)
|
|
7316
7296
|
return [];
|
|
7317
7297
|
const ans = [];
|
|
@@ -7448,7 +7428,7 @@ var dataStructureTyped = (() => {
|
|
|
7448
7428
|
* Space Complexity: O(log n)
|
|
7449
7429
|
*/
|
|
7450
7430
|
/**
|
|
7451
|
-
* The function `
|
|
7431
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a valid node
|
|
7452
7432
|
* key, otherwise it returns the key itself.
|
|
7453
7433
|
* @param {BTNKey | N | null | undefined} key - The `key` parameter can be of type `BTNKey`, `N`,
|
|
7454
7434
|
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
|
|
@@ -7458,7 +7438,7 @@ var dataStructureTyped = (() => {
|
|
|
7458
7438
|
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
|
7459
7439
|
* itself if it is not a valid node key.
|
|
7460
7440
|
*/
|
|
7461
|
-
|
|
7441
|
+
ensureNode(key, iterationType = "ITERATIVE" /* ITERATIVE */) {
|
|
7462
7442
|
return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
7463
7443
|
}
|
|
7464
7444
|
/**
|
|
@@ -7523,7 +7503,7 @@ var dataStructureTyped = (() => {
|
|
|
7523
7503
|
*/
|
|
7524
7504
|
getPathToRoot(beginRoot, isReverse = true) {
|
|
7525
7505
|
const result = [];
|
|
7526
|
-
beginRoot = this.
|
|
7506
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7527
7507
|
if (!beginRoot)
|
|
7528
7508
|
return result;
|
|
7529
7509
|
while (beginRoot.parent) {
|
|
@@ -7552,7 +7532,7 @@ var dataStructureTyped = (() => {
|
|
|
7552
7532
|
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
7553
7533
|
*/
|
|
7554
7534
|
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
7555
|
-
beginRoot = this.
|
|
7535
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7556
7536
|
if (!beginRoot)
|
|
7557
7537
|
return beginRoot;
|
|
7558
7538
|
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
@@ -7591,7 +7571,7 @@ var dataStructureTyped = (() => {
|
|
|
7591
7571
|
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
7592
7572
|
*/
|
|
7593
7573
|
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
7594
|
-
beginRoot = this.
|
|
7574
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7595
7575
|
if (!beginRoot)
|
|
7596
7576
|
return beginRoot;
|
|
7597
7577
|
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
@@ -7627,7 +7607,7 @@ var dataStructureTyped = (() => {
|
|
|
7627
7607
|
* @returns a boolean value.
|
|
7628
7608
|
*/
|
|
7629
7609
|
isSubtreeBST(beginRoot, iterationType = this.iterationType) {
|
|
7630
|
-
beginRoot = this.
|
|
7610
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7631
7611
|
if (!beginRoot)
|
|
7632
7612
|
return true;
|
|
7633
7613
|
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
@@ -7699,7 +7679,7 @@ var dataStructureTyped = (() => {
|
|
|
7699
7679
|
* by the return type of the `callback` function.
|
|
7700
7680
|
*/
|
|
7701
7681
|
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
7702
|
-
beginRoot = this.
|
|
7682
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7703
7683
|
const ans = [];
|
|
7704
7684
|
if (!beginRoot)
|
|
7705
7685
|
return ans;
|
|
@@ -7797,7 +7777,7 @@ var dataStructureTyped = (() => {
|
|
|
7797
7777
|
* @returns an array of values that are the return values of the callback function.
|
|
7798
7778
|
*/
|
|
7799
7779
|
dfs(callback = this._defaultOneParamCallback, pattern = "in", beginRoot = this.root, iterationType = "ITERATIVE" /* ITERATIVE */, includeNull = false) {
|
|
7800
|
-
beginRoot = this.
|
|
7780
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7801
7781
|
if (!beginRoot)
|
|
7802
7782
|
return [];
|
|
7803
7783
|
const ans = [];
|
|
@@ -7917,7 +7897,7 @@ var dataStructureTyped = (() => {
|
|
|
7917
7897
|
* the breadth-first traversal of a binary tree.
|
|
7918
7898
|
*/
|
|
7919
7899
|
bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
7920
|
-
beginRoot = this.
|
|
7900
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7921
7901
|
if (!beginRoot)
|
|
7922
7902
|
return [];
|
|
7923
7903
|
const ans = [];
|
|
@@ -7987,7 +7967,7 @@ var dataStructureTyped = (() => {
|
|
|
7987
7967
|
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
7988
7968
|
*/
|
|
7989
7969
|
listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
7990
|
-
beginRoot = this.
|
|
7970
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7991
7971
|
const levelsNodes = [];
|
|
7992
7972
|
if (!beginRoot)
|
|
7993
7973
|
return levelsNodes;
|
|
@@ -8039,7 +8019,7 @@ var dataStructureTyped = (() => {
|
|
|
8039
8019
|
* @returns The function `getPredecessor` returns a value of type `N | undefined`.
|
|
8040
8020
|
*/
|
|
8041
8021
|
getPredecessor(node) {
|
|
8042
|
-
node = this.
|
|
8022
|
+
node = this.ensureNode(node);
|
|
8043
8023
|
if (!this.isRealNode(node))
|
|
8044
8024
|
return void 0;
|
|
8045
8025
|
if (node.left) {
|
|
@@ -8061,7 +8041,7 @@ var dataStructureTyped = (() => {
|
|
|
8061
8041
|
* after the given node in the inorder traversal of the binary tree.
|
|
8062
8042
|
*/
|
|
8063
8043
|
getSuccessor(x) {
|
|
8064
|
-
x = this.
|
|
8044
|
+
x = this.ensureNode(x);
|
|
8065
8045
|
if (!x)
|
|
8066
8046
|
return void 0;
|
|
8067
8047
|
if (x.right) {
|
|
@@ -8093,7 +8073,7 @@ var dataStructureTyped = (() => {
|
|
|
8093
8073
|
* by the return type of the `callback` function.
|
|
8094
8074
|
*/
|
|
8095
8075
|
morris(callback = this._defaultOneParamCallback, pattern = "in", beginRoot = this.root) {
|
|
8096
|
-
beginRoot = this.
|
|
8076
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
8097
8077
|
if (beginRoot === null)
|
|
8098
8078
|
return [];
|
|
8099
8079
|
const ans = [];
|
|
@@ -8200,7 +8180,7 @@ var dataStructureTyped = (() => {
|
|
|
8200
8180
|
const newTree = this.createTree();
|
|
8201
8181
|
for (const [key, value] of this) {
|
|
8202
8182
|
if (predicate([key, value], this)) {
|
|
8203
|
-
newTree.add(key, value);
|
|
8183
|
+
newTree.add([key, value]);
|
|
8204
8184
|
}
|
|
8205
8185
|
}
|
|
8206
8186
|
return newTree;
|
|
@@ -8214,7 +8194,7 @@ var dataStructureTyped = (() => {
|
|
|
8214
8194
|
map(callback) {
|
|
8215
8195
|
const newTree = this.createTree();
|
|
8216
8196
|
for (const [key, value] of this) {
|
|
8217
|
-
newTree.add(key, callback([key, value], this));
|
|
8197
|
+
newTree.add([key, callback([key, value], this)]);
|
|
8218
8198
|
}
|
|
8219
8199
|
return newTree;
|
|
8220
8200
|
}
|
|
@@ -8290,7 +8270,7 @@ var dataStructureTyped = (() => {
|
|
|
8290
8270
|
*/
|
|
8291
8271
|
print(beginRoot = this.root, options) {
|
|
8292
8272
|
const opts = __spreadValues({ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false }, options);
|
|
8293
|
-
beginRoot = this.
|
|
8273
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
8294
8274
|
if (!beginRoot)
|
|
8295
8275
|
return;
|
|
8296
8276
|
if (opts.isShowUndefined)
|
|
@@ -8310,18 +8290,6 @@ var dataStructureTyped = (() => {
|
|
|
8310
8290
|
};
|
|
8311
8291
|
display(beginRoot);
|
|
8312
8292
|
}
|
|
8313
|
-
init(elements) {
|
|
8314
|
-
if (elements) {
|
|
8315
|
-
for (const entryOrKey of elements) {
|
|
8316
|
-
if (Array.isArray(entryOrKey)) {
|
|
8317
|
-
const [key, value] = entryOrKey;
|
|
8318
|
-
this.add(key, value);
|
|
8319
|
-
} else {
|
|
8320
|
-
this.add(entryOrKey);
|
|
8321
|
-
}
|
|
8322
|
-
}
|
|
8323
|
-
}
|
|
8324
|
-
}
|
|
8325
8293
|
_displayAux(node, options) {
|
|
8326
8294
|
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
8327
8295
|
const emptyDisplayLayout = [["\u2500"], 1, 0, 0];
|
|
@@ -8358,9 +8326,9 @@ var dataStructureTyped = (() => {
|
|
|
8358
8326
|
* @param {N} destNode - The destination node to swap.
|
|
8359
8327
|
* @returns {N} - The destination node after the swap.
|
|
8360
8328
|
*/
|
|
8361
|
-
|
|
8362
|
-
srcNode = this.
|
|
8363
|
-
destNode = this.
|
|
8329
|
+
_swapProperties(srcNode, destNode) {
|
|
8330
|
+
srcNode = this.ensureNode(srcNode);
|
|
8331
|
+
destNode = this.ensureNode(destNode);
|
|
8364
8332
|
if (srcNode && destNode) {
|
|
8365
8333
|
const { key, value } = destNode;
|
|
8366
8334
|
const tempNode = this.createNode(key, value);
|
|
@@ -8374,6 +8342,30 @@ var dataStructureTyped = (() => {
|
|
|
8374
8342
|
}
|
|
8375
8343
|
return void 0;
|
|
8376
8344
|
}
|
|
8345
|
+
/**
|
|
8346
|
+
* The function replaces an old node with a new node in a binary tree.
|
|
8347
|
+
* @param {N} oldNode - The oldNode parameter represents the node that needs to be replaced in the
|
|
8348
|
+
* tree.
|
|
8349
|
+
* @param {N} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
8350
|
+
* tree.
|
|
8351
|
+
* @returns The method is returning the newNode.
|
|
8352
|
+
*/
|
|
8353
|
+
_replaceNode(oldNode, newNode) {
|
|
8354
|
+
if (oldNode.parent) {
|
|
8355
|
+
if (oldNode.parent.left === oldNode) {
|
|
8356
|
+
oldNode.parent.left = newNode;
|
|
8357
|
+
} else if (oldNode.parent.right === oldNode) {
|
|
8358
|
+
oldNode.parent.right = newNode;
|
|
8359
|
+
}
|
|
8360
|
+
}
|
|
8361
|
+
newNode.left = oldNode.left;
|
|
8362
|
+
newNode.right = oldNode.right;
|
|
8363
|
+
newNode.parent = oldNode.parent;
|
|
8364
|
+
if (this.root === oldNode) {
|
|
8365
|
+
this._root = newNode;
|
|
8366
|
+
}
|
|
8367
|
+
return newNode;
|
|
8368
|
+
}
|
|
8377
8369
|
/**
|
|
8378
8370
|
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
8379
8371
|
* @param {N | null | undefined} newNode - The `newNode` parameter represents the node that you want to add to
|
|
@@ -8468,9 +8460,12 @@ var dataStructureTyped = (() => {
|
|
|
8468
8460
|
};
|
|
8469
8461
|
var BST = class _BST extends BinaryTree {
|
|
8470
8462
|
/**
|
|
8471
|
-
*
|
|
8472
|
-
*
|
|
8473
|
-
*
|
|
8463
|
+
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
|
8464
|
+
* the tree with optional elements and options.
|
|
8465
|
+
* @param [elements] - An optional iterable of BTNodeExemplar objects that will be added to the
|
|
8466
|
+
* binary search tree.
|
|
8467
|
+
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
8468
|
+
* configuration options for the binary search tree. It can have the following properties:
|
|
8474
8469
|
*/
|
|
8475
8470
|
constructor(elements, options) {
|
|
8476
8471
|
super([], options);
|
|
@@ -8484,11 +8479,8 @@ var dataStructureTyped = (() => {
|
|
|
8484
8479
|
}
|
|
8485
8480
|
this._root = void 0;
|
|
8486
8481
|
if (elements)
|
|
8487
|
-
this.
|
|
8482
|
+
this.addMany(elements);
|
|
8488
8483
|
}
|
|
8489
|
-
/**
|
|
8490
|
-
* Get the root node of the binary tree.
|
|
8491
|
-
*/
|
|
8492
8484
|
get root() {
|
|
8493
8485
|
return this._root;
|
|
8494
8486
|
}
|
|
@@ -8503,151 +8495,145 @@ var dataStructureTyped = (() => {
|
|
|
8503
8495
|
createNode(key, value) {
|
|
8504
8496
|
return new BSTNode(key, value);
|
|
8505
8497
|
}
|
|
8498
|
+
/**
|
|
8499
|
+
* The function creates a new binary search tree with the specified options.
|
|
8500
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
8501
|
+
* behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which is a type
|
|
8502
|
+
* that defines various options for creating a binary search tree.
|
|
8503
|
+
* @returns a new instance of the BST class with the specified options.
|
|
8504
|
+
*/
|
|
8506
8505
|
createTree(options) {
|
|
8507
8506
|
return new _BST([], __spreadValues({
|
|
8508
8507
|
iterationType: this.iterationType,
|
|
8509
8508
|
comparator: this.comparator
|
|
8510
8509
|
}, options));
|
|
8511
8510
|
}
|
|
8511
|
+
/**
|
|
8512
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
8513
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
8514
|
+
*/
|
|
8512
8515
|
/**
|
|
8513
8516
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
8514
8517
|
* Space Complexity: O(1) - Constant space is used.
|
|
8515
8518
|
*
|
|
8516
|
-
* The `add` function adds a new node to a binary search tree
|
|
8517
|
-
*
|
|
8518
|
-
* following
|
|
8519
|
-
* @
|
|
8520
|
-
*
|
|
8521
|
-
* @returns The method `add` returns a node (`N`) that was inserted into the binary search tree. If
|
|
8522
|
-
* no node was inserted, it returns `undefined`.
|
|
8519
|
+
* The `add` function adds a new node to a binary search tree, either by key or by providing a node
|
|
8520
|
+
* object.
|
|
8521
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
8522
|
+
* @returns The method returns either the newly added node (`newNode`) or `undefined` if the input
|
|
8523
|
+
* (`keyOrNodeOrEntry`) is null, undefined, or does not match any of the expected types.
|
|
8523
8524
|
*/
|
|
8524
|
-
add(
|
|
8525
|
-
if (
|
|
8525
|
+
add(keyOrNodeOrEntry) {
|
|
8526
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === void 0) {
|
|
8526
8527
|
return void 0;
|
|
8527
|
-
|
|
8528
|
+
}
|
|
8528
8529
|
let newNode;
|
|
8529
|
-
if (
|
|
8530
|
-
newNode =
|
|
8531
|
-
} else if (this.isNodeKey(
|
|
8532
|
-
newNode = this.createNode(
|
|
8530
|
+
if (keyOrNodeOrEntry instanceof BSTNode) {
|
|
8531
|
+
newNode = keyOrNodeOrEntry;
|
|
8532
|
+
} else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
8533
|
+
newNode = this.createNode(keyOrNodeOrEntry);
|
|
8534
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
8535
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
8536
|
+
if (key === void 0 || key === null) {
|
|
8537
|
+
return;
|
|
8538
|
+
} else {
|
|
8539
|
+
newNode = this.createNode(key, value);
|
|
8540
|
+
}
|
|
8533
8541
|
} else {
|
|
8534
|
-
|
|
8542
|
+
return;
|
|
8535
8543
|
}
|
|
8536
8544
|
if (this.root === void 0) {
|
|
8537
8545
|
this._setRoot(newNode);
|
|
8538
|
-
this._size
|
|
8539
|
-
|
|
8540
|
-
}
|
|
8541
|
-
|
|
8542
|
-
|
|
8543
|
-
|
|
8544
|
-
|
|
8545
|
-
|
|
8546
|
-
|
|
8547
|
-
|
|
8548
|
-
|
|
8549
|
-
|
|
8550
|
-
|
|
8551
|
-
|
|
8552
|
-
|
|
8553
|
-
|
|
8554
|
-
|
|
8555
|
-
|
|
8556
|
-
|
|
8557
|
-
|
|
8558
|
-
|
|
8559
|
-
|
|
8560
|
-
} else {
|
|
8561
|
-
if (cur.left)
|
|
8562
|
-
cur = cur.left;
|
|
8563
|
-
}
|
|
8564
|
-
} else if (this._compare(cur.key, newNode.key) === "lt" /* lt */) {
|
|
8565
|
-
if (cur.right === void 0) {
|
|
8566
|
-
if (newNode) {
|
|
8567
|
-
newNode.parent = cur;
|
|
8568
|
-
}
|
|
8569
|
-
cur.right = newNode;
|
|
8570
|
-
this._size = this.size + 1;
|
|
8571
|
-
traversing = false;
|
|
8572
|
-
inserted = cur.right;
|
|
8573
|
-
} else {
|
|
8574
|
-
if (cur.right)
|
|
8575
|
-
cur = cur.right;
|
|
8576
|
-
}
|
|
8577
|
-
}
|
|
8578
|
-
} else {
|
|
8579
|
-
traversing = false;
|
|
8546
|
+
this._size++;
|
|
8547
|
+
return this.root;
|
|
8548
|
+
}
|
|
8549
|
+
let current = this.root;
|
|
8550
|
+
while (current !== void 0) {
|
|
8551
|
+
if (this._compare(current.key, newNode.key) === "eq" /* eq */) {
|
|
8552
|
+
this._replaceNode(current, newNode);
|
|
8553
|
+
return newNode;
|
|
8554
|
+
} else if (this._compare(current.key, newNode.key) === "gt" /* gt */) {
|
|
8555
|
+
if (current.left === void 0) {
|
|
8556
|
+
current.left = newNode;
|
|
8557
|
+
newNode.parent = current;
|
|
8558
|
+
this._size++;
|
|
8559
|
+
return newNode;
|
|
8560
|
+
}
|
|
8561
|
+
current = current.left;
|
|
8562
|
+
} else {
|
|
8563
|
+
if (current.right === void 0) {
|
|
8564
|
+
current.right = newNode;
|
|
8565
|
+
newNode.parent = current;
|
|
8566
|
+
this._size++;
|
|
8567
|
+
return newNode;
|
|
8580
8568
|
}
|
|
8569
|
+
current = current.right;
|
|
8581
8570
|
}
|
|
8582
8571
|
}
|
|
8583
|
-
return
|
|
8572
|
+
return void 0;
|
|
8584
8573
|
}
|
|
8585
8574
|
/**
|
|
8586
|
-
* Time Complexity: O(log n) -
|
|
8587
|
-
* Space Complexity: O(
|
|
8575
|
+
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
|
|
8576
|
+
* Space Complexity: O(k) - Additional space is required for the sorted array.
|
|
8588
8577
|
*/
|
|
8589
8578
|
/**
|
|
8590
|
-
* Time Complexity: O(
|
|
8591
|
-
* Space Complexity: O(
|
|
8579
|
+
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
|
|
8580
|
+
* Space Complexity: O(k) - Additional space is required for the sorted array.
|
|
8592
8581
|
*
|
|
8593
|
-
* The `addMany` function
|
|
8594
|
-
*
|
|
8595
|
-
* @param
|
|
8596
|
-
* binary
|
|
8597
|
-
* node), or `undefined`.
|
|
8598
|
-
* @param {(V | undefined)[]} [data] - An optional array of values to associate with the keys or
|
|
8599
|
-
* nodes being added. If provided, the length of the `data` array must be the same as the length of
|
|
8600
|
-
* the `keysOrNodes` array.
|
|
8582
|
+
* The `addMany` function in TypeScript adds multiple nodes to a binary tree, either in a balanced or
|
|
8583
|
+
* unbalanced manner, and returns an array of the inserted nodes.
|
|
8584
|
+
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to the
|
|
8585
|
+
* binary tree.
|
|
8601
8586
|
* @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
|
|
8602
|
-
* adding the nodes. The default value is
|
|
8587
|
+
* adding the nodes. The default value is true.
|
|
8603
8588
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
8604
|
-
* type of iteration to use when adding multiple keys or nodes to the binary
|
|
8605
|
-
*
|
|
8606
|
-
*
|
|
8607
|
-
* @returns The
|
|
8589
|
+
* type of iteration to use when adding multiple keys or nodes to the binary tree. It has a default
|
|
8590
|
+
* value of `this.iterationType`, which means it will use the iteration type specified by the binary
|
|
8591
|
+
* tree instance.
|
|
8592
|
+
* @returns The `addMany` function returns an array of `N` or `undefined` values.
|
|
8608
8593
|
*/
|
|
8609
|
-
addMany(
|
|
8610
|
-
function hasNoUndefined(arr) {
|
|
8611
|
-
return arr.indexOf(void 0) === -1;
|
|
8612
|
-
}
|
|
8613
|
-
if (!isBalanceAdd || !hasNoUndefined(keysOrNodes)) {
|
|
8614
|
-
return super.addMany(keysOrNodes, data).map((n) => n != null ? n : void 0);
|
|
8615
|
-
}
|
|
8594
|
+
addMany(keysOrNodesOrEntries, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
8616
8595
|
const inserted = [];
|
|
8617
|
-
|
|
8618
|
-
(
|
|
8619
|
-
|
|
8620
|
-
|
|
8621
|
-
|
|
8622
|
-
|
|
8623
|
-
if (keyOrNode instanceof BSTNode)
|
|
8624
|
-
return true;
|
|
8625
|
-
return false;
|
|
8596
|
+
if (!isBalanceAdd) {
|
|
8597
|
+
for (const kve of keysOrNodesOrEntries) {
|
|
8598
|
+
const nn = this.add(kve);
|
|
8599
|
+
inserted.push(nn);
|
|
8600
|
+
}
|
|
8601
|
+
return inserted;
|
|
8626
8602
|
}
|
|
8627
|
-
const
|
|
8628
|
-
|
|
8629
|
-
|
|
8630
|
-
|
|
8631
|
-
return
|
|
8603
|
+
const realBTNExemplars = [];
|
|
8604
|
+
const isRealBTNExemplar = (kve) => {
|
|
8605
|
+
if (kve === void 0 || kve === null)
|
|
8606
|
+
return false;
|
|
8607
|
+
return !(this.isEntry(kve) && (kve[0] === void 0 || kve[0] === null));
|
|
8632
8608
|
};
|
|
8633
|
-
|
|
8634
|
-
|
|
8635
|
-
sorted = combinedArr.sort((a, b) => a[0].key - b[0].key);
|
|
8636
|
-
} else if (_isBinaryTreeKeyOrNullTuple(combinedArr)) {
|
|
8637
|
-
sorted = combinedArr.sort((a, b) => a[0] - b[0]);
|
|
8638
|
-
} else {
|
|
8639
|
-
throw new Error("Invalid input keysOrNodes");
|
|
8609
|
+
for (const kve of keysOrNodesOrEntries) {
|
|
8610
|
+
isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
|
|
8640
8611
|
}
|
|
8641
|
-
|
|
8642
|
-
|
|
8643
|
-
|
|
8612
|
+
let sorted = [];
|
|
8613
|
+
sorted = realBTNExemplars.sort((a, b) => {
|
|
8614
|
+
let aR, bR;
|
|
8615
|
+
if (this.isEntry(a))
|
|
8616
|
+
aR = a[0];
|
|
8617
|
+
else if (this.isRealNode(a))
|
|
8618
|
+
aR = a.key;
|
|
8619
|
+
else
|
|
8620
|
+
aR = a;
|
|
8621
|
+
if (this.isEntry(b))
|
|
8622
|
+
bR = b[0];
|
|
8623
|
+
else if (this.isRealNode(b))
|
|
8624
|
+
bR = b.key;
|
|
8625
|
+
else
|
|
8626
|
+
bR = b;
|
|
8627
|
+
return aR - bR;
|
|
8628
|
+
});
|
|
8629
|
+
const _dfs = (arr) => {
|
|
8644
8630
|
if (arr.length === 0)
|
|
8645
8631
|
return;
|
|
8646
8632
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
8647
|
-
const newNode = this.add(arr[mid]
|
|
8633
|
+
const newNode = this.add(arr[mid]);
|
|
8648
8634
|
inserted.push(newNode);
|
|
8649
|
-
_dfs(arr.slice(0, mid)
|
|
8650
|
-
_dfs(arr.slice(mid + 1)
|
|
8635
|
+
_dfs(arr.slice(0, mid));
|
|
8636
|
+
_dfs(arr.slice(mid + 1));
|
|
8651
8637
|
};
|
|
8652
8638
|
const _iterate = () => {
|
|
8653
8639
|
const n = sorted.length;
|
|
@@ -8658,7 +8644,7 @@ var dataStructureTyped = (() => {
|
|
|
8658
8644
|
const [l, r] = popped;
|
|
8659
8645
|
if (l <= r) {
|
|
8660
8646
|
const m = l + Math.floor((r - l) / 2);
|
|
8661
|
-
const newNode = this.add(
|
|
8647
|
+
const newNode = this.add(sorted[m]);
|
|
8662
8648
|
inserted.push(newNode);
|
|
8663
8649
|
stack.push([m + 1, r]);
|
|
8664
8650
|
stack.push([l, m - 1]);
|
|
@@ -8667,7 +8653,7 @@ var dataStructureTyped = (() => {
|
|
|
8667
8653
|
}
|
|
8668
8654
|
};
|
|
8669
8655
|
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
8670
|
-
_dfs(
|
|
8656
|
+
_dfs(sorted);
|
|
8671
8657
|
} else {
|
|
8672
8658
|
_iterate();
|
|
8673
8659
|
}
|
|
@@ -8754,7 +8740,7 @@ var dataStructureTyped = (() => {
|
|
|
8754
8740
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
8755
8741
|
*/
|
|
8756
8742
|
/**
|
|
8757
|
-
* The function `
|
|
8743
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
8758
8744
|
* otherwise it returns the key itself.
|
|
8759
8745
|
* @param {BTNKey | N | undefined} key - The `key` parameter can be of type `BTNKey`, `N`, or
|
|
8760
8746
|
* `undefined`.
|
|
@@ -8762,7 +8748,7 @@ var dataStructureTyped = (() => {
|
|
|
8762
8748
|
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
8763
8749
|
* @returns either a node object (N) or undefined.
|
|
8764
8750
|
*/
|
|
8765
|
-
|
|
8751
|
+
ensureNode(key, iterationType = "ITERATIVE" /* ITERATIVE */) {
|
|
8766
8752
|
return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
8767
8753
|
}
|
|
8768
8754
|
/**
|
|
@@ -8789,7 +8775,7 @@ var dataStructureTyped = (() => {
|
|
|
8789
8775
|
* @returns The method returns an array of nodes (`N[]`).
|
|
8790
8776
|
*/
|
|
8791
8777
|
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
8792
|
-
beginRoot = this.
|
|
8778
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
8793
8779
|
if (!beginRoot)
|
|
8794
8780
|
return [];
|
|
8795
8781
|
const ans = [];
|
|
@@ -8865,7 +8851,7 @@ var dataStructureTyped = (() => {
|
|
|
8865
8851
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
8866
8852
|
*/
|
|
8867
8853
|
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = "lt" /* lt */, targetNode = this.root, iterationType = this.iterationType) {
|
|
8868
|
-
targetNode = this.
|
|
8854
|
+
targetNode = this.ensureNode(targetNode);
|
|
8869
8855
|
const ans = [];
|
|
8870
8856
|
if (!targetNode)
|
|
8871
8857
|
return ans;
|
|
@@ -8929,7 +8915,7 @@ var dataStructureTyped = (() => {
|
|
|
8929
8915
|
return;
|
|
8930
8916
|
const m = l + Math.floor((r - l) / 2);
|
|
8931
8917
|
const midNode = sorted[m];
|
|
8932
|
-
this.add(midNode.key, midNode.value);
|
|
8918
|
+
this.add([midNode.key, midNode.value]);
|
|
8933
8919
|
buildBalanceBST(l, m - 1);
|
|
8934
8920
|
buildBalanceBST(m + 1, r);
|
|
8935
8921
|
};
|
|
@@ -8945,7 +8931,7 @@ var dataStructureTyped = (() => {
|
|
|
8945
8931
|
const m = l + Math.floor((r - l) / 2);
|
|
8946
8932
|
const midNode = sorted[m];
|
|
8947
8933
|
debugger;
|
|
8948
|
-
this.add(midNode.key, midNode.value);
|
|
8934
|
+
this.add([midNode.key, midNode.value]);
|
|
8949
8935
|
stack.push([m + 1, r]);
|
|
8950
8936
|
stack.push([l, m - 1]);
|
|
8951
8937
|
}
|
|
@@ -9019,22 +9005,6 @@ var dataStructureTyped = (() => {
|
|
|
9019
9005
|
}
|
|
9020
9006
|
return balanced;
|
|
9021
9007
|
}
|
|
9022
|
-
/**
|
|
9023
|
-
* Time Complexity: O(n) - Visiting each node once.
|
|
9024
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
9025
|
-
*/
|
|
9026
|
-
init(elements) {
|
|
9027
|
-
if (elements) {
|
|
9028
|
-
for (const entryOrKey of elements) {
|
|
9029
|
-
if (Array.isArray(entryOrKey)) {
|
|
9030
|
-
const [key, value] = entryOrKey;
|
|
9031
|
-
this.add(key, value);
|
|
9032
|
-
} else {
|
|
9033
|
-
this.add(entryOrKey);
|
|
9034
|
-
}
|
|
9035
|
-
}
|
|
9036
|
-
}
|
|
9037
|
-
}
|
|
9038
9008
|
_setRoot(v) {
|
|
9039
9009
|
if (v) {
|
|
9040
9010
|
v.parent = void 0;
|
|
@@ -9496,15 +9466,18 @@ var dataStructureTyped = (() => {
|
|
|
9496
9466
|
};
|
|
9497
9467
|
var AVLTree = class _AVLTree extends BST {
|
|
9498
9468
|
/**
|
|
9499
|
-
*
|
|
9500
|
-
* @param
|
|
9501
|
-
*
|
|
9502
|
-
*
|
|
9469
|
+
* The constructor function initializes an AVLTree object with optional elements and options.
|
|
9470
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
|
|
9471
|
+
* objects. It represents a collection of elements that will be added to the AVL tree during
|
|
9472
|
+
* initialization.
|
|
9473
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
9474
|
+
* behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
|
|
9475
|
+
* provide only a subset of the properties defined in the `AVLTreeOptions` interface.
|
|
9503
9476
|
*/
|
|
9504
9477
|
constructor(elements, options) {
|
|
9505
9478
|
super([], options);
|
|
9506
9479
|
if (elements)
|
|
9507
|
-
|
|
9480
|
+
super.addMany(elements);
|
|
9508
9481
|
}
|
|
9509
9482
|
/**
|
|
9510
9483
|
* The function creates a new AVL tree node with the specified key and value.
|
|
@@ -9518,28 +9491,37 @@ var dataStructureTyped = (() => {
|
|
|
9518
9491
|
createNode(key, value) {
|
|
9519
9492
|
return new AVLTreeNode(key, value);
|
|
9520
9493
|
}
|
|
9494
|
+
/**
|
|
9495
|
+
* The function creates a new AVL tree with the specified options and returns it.
|
|
9496
|
+
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
9497
|
+
* passed to the `createTree` function. It is used to customize the behavior of the AVL tree that is
|
|
9498
|
+
* being created.
|
|
9499
|
+
* @returns a new AVLTree object.
|
|
9500
|
+
*/
|
|
9521
9501
|
createTree(options) {
|
|
9522
9502
|
return new _AVLTree([], __spreadValues({
|
|
9523
9503
|
iterationType: this.iterationType,
|
|
9524
9504
|
comparator: this.comparator
|
|
9525
9505
|
}, options));
|
|
9526
9506
|
}
|
|
9507
|
+
/**
|
|
9508
|
+
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
9509
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
9510
|
+
*/
|
|
9527
9511
|
/**
|
|
9528
9512
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
9529
9513
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
9530
9514
|
*
|
|
9531
|
-
* The function overrides the add method of a
|
|
9532
|
-
*
|
|
9533
|
-
* @param
|
|
9534
|
-
*
|
|
9535
|
-
* @
|
|
9536
|
-
* added to the binary search tree.
|
|
9537
|
-
* @returns The method is returning either a node (N) or undefined.
|
|
9515
|
+
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
9516
|
+
* a new node.
|
|
9517
|
+
* @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be either a key, a node, or an
|
|
9518
|
+
* entry.
|
|
9519
|
+
* @returns The method is returning either the inserted node or `undefined`.
|
|
9538
9520
|
*/
|
|
9539
|
-
add(
|
|
9540
|
-
if (
|
|
9521
|
+
add(keyOrNodeOrEntry) {
|
|
9522
|
+
if (keyOrNodeOrEntry === null)
|
|
9541
9523
|
return void 0;
|
|
9542
|
-
const inserted = super.add(
|
|
9524
|
+
const inserted = super.add(keyOrNodeOrEntry);
|
|
9543
9525
|
if (inserted)
|
|
9544
9526
|
this._balancePath(inserted);
|
|
9545
9527
|
return inserted;
|
|
@@ -9575,23 +9557,7 @@ var dataStructureTyped = (() => {
|
|
|
9575
9557
|
return deletedResults;
|
|
9576
9558
|
}
|
|
9577
9559
|
/**
|
|
9578
|
-
*
|
|
9579
|
-
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
9580
|
-
*/
|
|
9581
|
-
init(elements) {
|
|
9582
|
-
if (elements) {
|
|
9583
|
-
for (const entryOrKey of elements) {
|
|
9584
|
-
if (Array.isArray(entryOrKey)) {
|
|
9585
|
-
const [key, value] = entryOrKey;
|
|
9586
|
-
this.add(key, value);
|
|
9587
|
-
} else {
|
|
9588
|
-
this.add(entryOrKey);
|
|
9589
|
-
}
|
|
9590
|
-
}
|
|
9591
|
-
}
|
|
9592
|
-
}
|
|
9593
|
-
/**
|
|
9594
|
-
* The `_swap` function swaps the key, value, and height properties between two nodes in a binary
|
|
9560
|
+
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
|
|
9595
9561
|
* tree.
|
|
9596
9562
|
* @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node that
|
|
9597
9563
|
* needs to be swapped with the destination node. It can be of type `BTNKey`, `N`, or `undefined`.
|
|
@@ -9600,9 +9566,9 @@ var dataStructureTyped = (() => {
|
|
|
9600
9566
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
9601
9567
|
* if either `srcNode` or `destNode` is undefined.
|
|
9602
9568
|
*/
|
|
9603
|
-
|
|
9604
|
-
srcNode = this.
|
|
9605
|
-
destNode = this.
|
|
9569
|
+
_swapProperties(srcNode, destNode) {
|
|
9570
|
+
srcNode = this.ensureNode(srcNode);
|
|
9571
|
+
destNode = this.ensureNode(destNode);
|
|
9606
9572
|
if (srcNode && destNode) {
|
|
9607
9573
|
const { key, value, height } = destNode;
|
|
9608
9574
|
const tempNode = this.createNode(key, value);
|
|
@@ -9889,6 +9855,10 @@ var dataStructureTyped = (() => {
|
|
|
9889
9855
|
B && this._updateHeight(B);
|
|
9890
9856
|
C && this._updateHeight(C);
|
|
9891
9857
|
}
|
|
9858
|
+
_replaceNode(oldNode, newNode) {
|
|
9859
|
+
newNode.height = oldNode.height;
|
|
9860
|
+
return super._replaceNode(oldNode, newNode);
|
|
9861
|
+
}
|
|
9892
9862
|
};
|
|
9893
9863
|
|
|
9894
9864
|
// src/data-structures/binary-tree/rb-tree.ts
|
|
@@ -9901,9 +9871,15 @@ var dataStructureTyped = (() => {
|
|
|
9901
9871
|
};
|
|
9902
9872
|
var RedBlackTree = class _RedBlackTree extends BST {
|
|
9903
9873
|
/**
|
|
9904
|
-
*
|
|
9905
|
-
*
|
|
9906
|
-
*
|
|
9874
|
+
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
9875
|
+
* initializes the tree with optional elements and options.
|
|
9876
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
|
|
9877
|
+
* objects. It represents the initial elements that will be added to the RBTree during its
|
|
9878
|
+
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
9879
|
+
* elements to the
|
|
9880
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
9881
|
+
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
|
9882
|
+
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
9907
9883
|
*/
|
|
9908
9884
|
constructor(elements, options) {
|
|
9909
9885
|
super([], options);
|
|
@@ -9912,7 +9888,7 @@ var dataStructureTyped = (() => {
|
|
|
9912
9888
|
__publicField(this, "_size", 0);
|
|
9913
9889
|
this._root = this.Sentinel;
|
|
9914
9890
|
if (elements)
|
|
9915
|
-
|
|
9891
|
+
super.addMany(elements);
|
|
9916
9892
|
}
|
|
9917
9893
|
get root() {
|
|
9918
9894
|
return this._root;
|
|
@@ -9920,9 +9896,28 @@ var dataStructureTyped = (() => {
|
|
|
9920
9896
|
get size() {
|
|
9921
9897
|
return this._size;
|
|
9922
9898
|
}
|
|
9899
|
+
/**
|
|
9900
|
+
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
9901
|
+
* @param {BTNKey} key - The key parameter is the key value associated with the node. It is used to
|
|
9902
|
+
* identify and compare nodes in the Red-Black Tree.
|
|
9903
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
9904
|
+
* associated with the node. It is of type `V`, which is a generic type that can be replaced with any
|
|
9905
|
+
* specific type when using the `createNode` method.
|
|
9906
|
+
* @param {RBTNColor} color - The "color" parameter is used to specify the color of the node in a
|
|
9907
|
+
* Red-Black Tree. It can be either "RED" or "BLACK". By default, the color is set to "BLACK".
|
|
9908
|
+
* @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
|
|
9909
|
+
* value, and color.
|
|
9910
|
+
*/
|
|
9923
9911
|
createNode(key, value, color = 0 /* BLACK */) {
|
|
9924
9912
|
return new RedBlackTreeNode(key, value, color);
|
|
9925
9913
|
}
|
|
9914
|
+
/**
|
|
9915
|
+
* The function creates a Red-Black Tree with the specified options and returns it.
|
|
9916
|
+
* @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
9917
|
+
* passed to the `createTree` function. It is used to customize the behavior of the `RedBlackTree`
|
|
9918
|
+
* class.
|
|
9919
|
+
* @returns a new instance of a RedBlackTree object.
|
|
9920
|
+
*/
|
|
9926
9921
|
createTree(options) {
|
|
9927
9922
|
return new _RedBlackTree([], __spreadValues({
|
|
9928
9923
|
iterationType: this.iterationType,
|
|
@@ -9932,24 +9927,28 @@ var dataStructureTyped = (() => {
|
|
|
9932
9927
|
/**
|
|
9933
9928
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
9934
9929
|
* Space Complexity: O(1)
|
|
9935
|
-
*
|
|
9936
|
-
* The `add` function adds a new node to a Red-Black Tree data structure.
|
|
9937
|
-
* @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
|
|
9938
|
-
* following types:
|
|
9939
|
-
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
9940
|
-
* key in the node being added to the Red-Black Tree.
|
|
9941
|
-
* @returns The method returns either a node (`N`) or `undefined`.
|
|
9942
9930
|
*/
|
|
9943
|
-
|
|
9931
|
+
/**
|
|
9932
|
+
* The function adds a node to a Red-Black Tree data structure.
|
|
9933
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
9934
|
+
* @returns The method `add` returns either an instance of `N` (the node that was added) or
|
|
9935
|
+
* `undefined`.
|
|
9936
|
+
*/
|
|
9937
|
+
add(keyOrNodeOrEntry) {
|
|
9944
9938
|
let node;
|
|
9945
|
-
if (this.isNodeKey(
|
|
9946
|
-
node = this.createNode(
|
|
9947
|
-
} else if (
|
|
9948
|
-
node =
|
|
9949
|
-
} else if (
|
|
9950
|
-
return;
|
|
9951
|
-
} else if (keyOrNode === void 0) {
|
|
9939
|
+
if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
9940
|
+
node = this.createNode(keyOrNodeOrEntry, void 0, 1 /* RED */);
|
|
9941
|
+
} else if (keyOrNodeOrEntry instanceof RedBlackTreeNode) {
|
|
9942
|
+
node = keyOrNodeOrEntry;
|
|
9943
|
+
} else if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === void 0) {
|
|
9952
9944
|
return;
|
|
9945
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
9946
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
9947
|
+
if (key === void 0 || key === null) {
|
|
9948
|
+
return;
|
|
9949
|
+
} else {
|
|
9950
|
+
node = this.createNode(key, value, 1 /* RED */);
|
|
9951
|
+
}
|
|
9953
9952
|
} else {
|
|
9954
9953
|
return;
|
|
9955
9954
|
}
|
|
@@ -9965,6 +9964,9 @@ var dataStructureTyped = (() => {
|
|
|
9965
9964
|
} else if (node.key > x.key) {
|
|
9966
9965
|
x = x == null ? void 0 : x.right;
|
|
9967
9966
|
} else {
|
|
9967
|
+
if (node !== x) {
|
|
9968
|
+
this._replaceNode(x, node);
|
|
9969
|
+
}
|
|
9968
9970
|
return;
|
|
9969
9971
|
}
|
|
9970
9972
|
}
|
|
@@ -10068,6 +10070,10 @@ var dataStructureTyped = (() => {
|
|
|
10068
10070
|
isRealNode(node) {
|
|
10069
10071
|
return node !== this.Sentinel && node !== void 0;
|
|
10070
10072
|
}
|
|
10073
|
+
/**
|
|
10074
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
10075
|
+
* Space Complexity: O(1)
|
|
10076
|
+
*/
|
|
10071
10077
|
/**
|
|
10072
10078
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
10073
10079
|
* Space Complexity: O(1)
|
|
@@ -10093,7 +10099,7 @@ var dataStructureTyped = (() => {
|
|
|
10093
10099
|
var _a;
|
|
10094
10100
|
if (identifier instanceof BinaryTreeNode)
|
|
10095
10101
|
callback = (node) => node;
|
|
10096
|
-
beginRoot = this.
|
|
10102
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
10097
10103
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) != null ? _a : void 0;
|
|
10098
10104
|
}
|
|
10099
10105
|
/**
|
|
@@ -10152,18 +10158,6 @@ var dataStructureTyped = (() => {
|
|
|
10152
10158
|
this._root = this.Sentinel;
|
|
10153
10159
|
this._size = 0;
|
|
10154
10160
|
}
|
|
10155
|
-
init(elements) {
|
|
10156
|
-
if (elements) {
|
|
10157
|
-
for (const entryOrKey of elements) {
|
|
10158
|
-
if (Array.isArray(entryOrKey)) {
|
|
10159
|
-
const [key, value] = entryOrKey;
|
|
10160
|
-
this.add(key, value);
|
|
10161
|
-
} else {
|
|
10162
|
-
this.add(entryOrKey);
|
|
10163
|
-
}
|
|
10164
|
-
}
|
|
10165
|
-
}
|
|
10166
|
-
}
|
|
10167
10161
|
_setRoot(v) {
|
|
10168
10162
|
if (v) {
|
|
10169
10163
|
v.parent = void 0;
|
|
@@ -10381,6 +10375,19 @@ var dataStructureTyped = (() => {
|
|
|
10381
10375
|
}
|
|
10382
10376
|
this.root.color = 0 /* BLACK */;
|
|
10383
10377
|
}
|
|
10378
|
+
/**
|
|
10379
|
+
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
10380
|
+
* @param {N} oldNode - The `oldNode` parameter represents the node that needs to be replaced in a
|
|
10381
|
+
* data structure. It is of type `N`, which is the type of the nodes in the data structure.
|
|
10382
|
+
* @param {N} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
10383
|
+
* data structure.
|
|
10384
|
+
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
10385
|
+
* superclass, passing in the `oldNode` and `newNode` as arguments.
|
|
10386
|
+
*/
|
|
10387
|
+
_replaceNode(oldNode, newNode) {
|
|
10388
|
+
newNode.color = oldNode.color;
|
|
10389
|
+
return super._replaceNode(oldNode, newNode);
|
|
10390
|
+
}
|
|
10384
10391
|
};
|
|
10385
10392
|
|
|
10386
10393
|
// src/data-structures/binary-tree/tree-multimap.ts
|
|
@@ -10402,20 +10409,17 @@ var dataStructureTyped = (() => {
|
|
|
10402
10409
|
}
|
|
10403
10410
|
};
|
|
10404
10411
|
var TreeMultimap = class _TreeMultimap extends AVLTree {
|
|
10405
|
-
/**
|
|
10406
|
-
* The constructor function for a TreeMultimap class in TypeScript, which extends another class and sets an option to
|
|
10407
|
-
* merge duplicated values.
|
|
10408
|
-
* @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
|
|
10409
|
-
* TreeMultimap.
|
|
10410
|
-
*/
|
|
10411
10412
|
constructor(elements, options) {
|
|
10412
10413
|
super([], options);
|
|
10413
10414
|
__publicField(this, "_count", 0);
|
|
10414
10415
|
if (elements)
|
|
10415
|
-
this.
|
|
10416
|
+
this.addMany(elements);
|
|
10416
10417
|
}
|
|
10418
|
+
// TODO the _count is not accurate after nodes count modified
|
|
10417
10419
|
get count() {
|
|
10418
|
-
|
|
10420
|
+
let sum = 0;
|
|
10421
|
+
this.subTreeTraverse((node) => sum += node.count);
|
|
10422
|
+
return sum;
|
|
10419
10423
|
}
|
|
10420
10424
|
/**
|
|
10421
10425
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
@@ -10435,115 +10439,63 @@ var dataStructureTyped = (() => {
|
|
|
10435
10439
|
comparator: this.comparator
|
|
10436
10440
|
}, options));
|
|
10437
10441
|
}
|
|
10442
|
+
/**
|
|
10443
|
+
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
10444
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
10445
|
+
*/
|
|
10438
10446
|
/**
|
|
10439
10447
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
10440
10448
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
10441
10449
|
*
|
|
10442
|
-
* The `add` function
|
|
10443
|
-
*
|
|
10444
|
-
* @param
|
|
10445
|
-
* following types:
|
|
10446
|
-
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
10447
|
-
* being added to the tree. It is an optional parameter, so it can be omitted if not needed.
|
|
10450
|
+
* The `add` function overrides the base class `add` function to add a new node to the tree multimap
|
|
10451
|
+
* and update the count.
|
|
10452
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
10448
10453
|
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
10449
|
-
* times the key
|
|
10450
|
-
*
|
|
10454
|
+
* times the key or node or entry should be added to the multimap. If not provided, the default value
|
|
10455
|
+
* is 1.
|
|
10456
|
+
* @returns either a node (`N`) or `undefined`.
|
|
10451
10457
|
*/
|
|
10452
|
-
add(
|
|
10453
|
-
|
|
10454
|
-
|
|
10455
|
-
|
|
10456
|
-
if (
|
|
10457
|
-
newNode =
|
|
10458
|
-
} else if (
|
|
10459
|
-
newNode = void 0;
|
|
10458
|
+
add(keyOrNodeOrEntry, count = 1) {
|
|
10459
|
+
let newNode;
|
|
10460
|
+
if (keyOrNodeOrEntry === void 0 || keyOrNodeOrEntry === null) {
|
|
10461
|
+
return;
|
|
10462
|
+
} else if (keyOrNodeOrEntry instanceof TreeMultimapNode) {
|
|
10463
|
+
newNode = keyOrNodeOrEntry;
|
|
10464
|
+
} else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
10465
|
+
newNode = this.createNode(keyOrNodeOrEntry, void 0, count);
|
|
10466
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
10467
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
10468
|
+
if (key === void 0 || key === null) {
|
|
10469
|
+
return;
|
|
10470
|
+
} else {
|
|
10471
|
+
newNode = this.createNode(key, value, count);
|
|
10472
|
+
}
|
|
10460
10473
|
} else {
|
|
10461
|
-
|
|
10474
|
+
return;
|
|
10462
10475
|
}
|
|
10463
|
-
|
|
10464
|
-
|
|
10465
|
-
|
|
10466
|
-
|
|
10467
|
-
this._count += newNode.count;
|
|
10468
|
-
inserted = this.root;
|
|
10469
|
-
} else {
|
|
10470
|
-
let cur = this.root;
|
|
10471
|
-
let traversing = true;
|
|
10472
|
-
while (traversing) {
|
|
10473
|
-
if (cur) {
|
|
10474
|
-
if (newNode) {
|
|
10475
|
-
if (this._compare(cur.key, newNode.key) === "eq" /* eq */) {
|
|
10476
|
-
cur.value = newNode.value;
|
|
10477
|
-
cur.count += newNode.count;
|
|
10478
|
-
this._count += newNode.count;
|
|
10479
|
-
traversing = false;
|
|
10480
|
-
inserted = cur;
|
|
10481
|
-
} else if (this._compare(cur.key, newNode.key) === "gt" /* gt */) {
|
|
10482
|
-
if (cur.left === void 0) {
|
|
10483
|
-
cur.left = newNode;
|
|
10484
|
-
this._size = this.size + 1;
|
|
10485
|
-
this._count += newNode.count;
|
|
10486
|
-
traversing = false;
|
|
10487
|
-
inserted = cur.left;
|
|
10488
|
-
} else {
|
|
10489
|
-
if (cur.left)
|
|
10490
|
-
cur = cur.left;
|
|
10491
|
-
}
|
|
10492
|
-
} else if (this._compare(cur.key, newNode.key) === "lt" /* lt */) {
|
|
10493
|
-
if (cur.right === void 0) {
|
|
10494
|
-
cur.right = newNode;
|
|
10495
|
-
this._size = this.size + 1;
|
|
10496
|
-
this._count += newNode.count;
|
|
10497
|
-
traversing = false;
|
|
10498
|
-
inserted = cur.right;
|
|
10499
|
-
} else {
|
|
10500
|
-
if (cur.right)
|
|
10501
|
-
cur = cur.right;
|
|
10502
|
-
}
|
|
10503
|
-
}
|
|
10504
|
-
} else {
|
|
10505
|
-
}
|
|
10506
|
-
} else {
|
|
10507
|
-
traversing = false;
|
|
10508
|
-
}
|
|
10509
|
-
}
|
|
10476
|
+
const orgNodeCount = (newNode == null ? void 0 : newNode.count) || 0;
|
|
10477
|
+
const inserted = super.add(newNode);
|
|
10478
|
+
if (inserted) {
|
|
10479
|
+
this._count += orgNodeCount;
|
|
10510
10480
|
}
|
|
10511
|
-
if (inserted)
|
|
10512
|
-
this._balancePath(inserted);
|
|
10513
10481
|
return inserted;
|
|
10514
10482
|
}
|
|
10515
10483
|
/**
|
|
10516
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
10484
|
+
* Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
10517
10485
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
10518
10486
|
*/
|
|
10519
10487
|
/**
|
|
10520
|
-
* Time Complexity: O(k log n) - logarithmic time
|
|
10488
|
+
* Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
10521
10489
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
10522
10490
|
*
|
|
10523
|
-
* The function
|
|
10524
|
-
*
|
|
10525
|
-
* @param
|
|
10526
|
-
*
|
|
10527
|
-
* @
|
|
10528
|
-
* keys or nodes being added. It is used to associate data with each key or node being added to the
|
|
10529
|
-
* TreeMultimap. If provided, the length of the `data` array should be the same as the length of the
|
|
10530
|
-
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
10491
|
+
* The function overrides the addMany method to add multiple keys, nodes, or entries to a data
|
|
10492
|
+
* structure.
|
|
10493
|
+
* @param keysOrNodesOrEntries - The parameter `keysOrNodesOrEntries` is an iterable that can contain
|
|
10494
|
+
* either keys, nodes, or entries.
|
|
10495
|
+
* @returns The method is returning an array of type `N | undefined`.
|
|
10531
10496
|
*/
|
|
10532
|
-
addMany(
|
|
10533
|
-
|
|
10534
|
-
for (let i = 0; i < keysOrNodes.length; i++) {
|
|
10535
|
-
const keyOrNode = keysOrNodes[i];
|
|
10536
|
-
if (keyOrNode instanceof TreeMultimapNode) {
|
|
10537
|
-
inserted.push(this.add(keyOrNode.key, keyOrNode.value, keyOrNode.count));
|
|
10538
|
-
continue;
|
|
10539
|
-
}
|
|
10540
|
-
if (keyOrNode === void 0) {
|
|
10541
|
-
inserted.push(this.add(NaN, void 0, 0));
|
|
10542
|
-
continue;
|
|
10543
|
-
}
|
|
10544
|
-
inserted.push(this.add(keyOrNode, data == null ? void 0 : data[i], 1));
|
|
10545
|
-
}
|
|
10546
|
-
return inserted;
|
|
10497
|
+
addMany(keysOrNodesOrEntries) {
|
|
10498
|
+
return super.addMany(keysOrNodesOrEntries);
|
|
10547
10499
|
}
|
|
10548
10500
|
/**
|
|
10549
10501
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
@@ -10571,7 +10523,7 @@ var dataStructureTyped = (() => {
|
|
|
10571
10523
|
return;
|
|
10572
10524
|
const m = l + Math.floor((r - l) / 2);
|
|
10573
10525
|
const midNode = sorted[m];
|
|
10574
|
-
this.add(midNode.key, midNode.value, midNode.count);
|
|
10526
|
+
this.add([midNode.key, midNode.value], midNode.count);
|
|
10575
10527
|
buildBalanceBST(l, m - 1);
|
|
10576
10528
|
buildBalanceBST(m + 1, r);
|
|
10577
10529
|
};
|
|
@@ -10586,7 +10538,7 @@ var dataStructureTyped = (() => {
|
|
|
10586
10538
|
if (l <= r) {
|
|
10587
10539
|
const m = l + Math.floor((r - l) / 2);
|
|
10588
10540
|
const midNode = sorted[m];
|
|
10589
|
-
this.add(midNode.key, midNode.value, midNode.count);
|
|
10541
|
+
this.add([midNode.key, midNode.value], midNode.count);
|
|
10590
10542
|
stack.push([m + 1, r]);
|
|
10591
10543
|
stack.push([l, m - 1]);
|
|
10592
10544
|
}
|
|
@@ -10649,7 +10601,7 @@ var dataStructureTyped = (() => {
|
|
|
10649
10601
|
const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : void 0;
|
|
10650
10602
|
if (leftSubTreeRightMost) {
|
|
10651
10603
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
10652
|
-
orgCurrent = this.
|
|
10604
|
+
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
10653
10605
|
if (parentOfLeftSubTreeMax) {
|
|
10654
10606
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) {
|
|
10655
10607
|
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
@@ -10681,22 +10633,6 @@ var dataStructureTyped = (() => {
|
|
|
10681
10633
|
super.clear();
|
|
10682
10634
|
this._count = 0;
|
|
10683
10635
|
}
|
|
10684
|
-
/**
|
|
10685
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
|
|
10686
|
-
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
10687
|
-
*/
|
|
10688
|
-
init(elements) {
|
|
10689
|
-
if (elements) {
|
|
10690
|
-
for (const entryOrKey of elements) {
|
|
10691
|
-
if (Array.isArray(entryOrKey)) {
|
|
10692
|
-
const [key, value] = entryOrKey;
|
|
10693
|
-
this.add(key, value);
|
|
10694
|
-
} else {
|
|
10695
|
-
this.add(entryOrKey);
|
|
10696
|
-
}
|
|
10697
|
-
}
|
|
10698
|
-
}
|
|
10699
|
-
}
|
|
10700
10636
|
/**
|
|
10701
10637
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
10702
10638
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -10713,7 +10649,7 @@ var dataStructureTyped = (() => {
|
|
|
10713
10649
|
* added, or `undefined` if no node was added.
|
|
10714
10650
|
*/
|
|
10715
10651
|
_addTo(newNode, parent) {
|
|
10716
|
-
parent = this.
|
|
10652
|
+
parent = this.ensureNode(parent);
|
|
10717
10653
|
if (parent) {
|
|
10718
10654
|
if (parent.left === void 0) {
|
|
10719
10655
|
parent.left = newNode;
|
|
@@ -10737,7 +10673,7 @@ var dataStructureTyped = (() => {
|
|
|
10737
10673
|
}
|
|
10738
10674
|
}
|
|
10739
10675
|
/**
|
|
10740
|
-
* The `
|
|
10676
|
+
* The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
|
|
10741
10677
|
* @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node from
|
|
10742
10678
|
* which the values will be swapped. It can be of type `BTNKey`, `N`, or `undefined`.
|
|
10743
10679
|
* @param {BTNKey | N | undefined} destNode - The `destNode` parameter represents the destination
|
|
@@ -10745,9 +10681,9 @@ var dataStructureTyped = (() => {
|
|
|
10745
10681
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
10746
10682
|
* if either `srcNode` or `destNode` is undefined.
|
|
10747
10683
|
*/
|
|
10748
|
-
|
|
10749
|
-
srcNode = this.
|
|
10750
|
-
destNode = this.
|
|
10684
|
+
_swapProperties(srcNode, destNode) {
|
|
10685
|
+
srcNode = this.ensureNode(srcNode);
|
|
10686
|
+
destNode = this.ensureNode(destNode);
|
|
10751
10687
|
if (srcNode && destNode) {
|
|
10752
10688
|
const { key, value, count, height } = destNode;
|
|
10753
10689
|
const tempNode = this.createNode(key, value, count);
|
|
@@ -10766,6 +10702,10 @@ var dataStructureTyped = (() => {
|
|
|
10766
10702
|
}
|
|
10767
10703
|
return void 0;
|
|
10768
10704
|
}
|
|
10705
|
+
_replaceNode(oldNode, newNode) {
|
|
10706
|
+
newNode.count = oldNode.count + newNode.count;
|
|
10707
|
+
return super._replaceNode(oldNode, newNode);
|
|
10708
|
+
}
|
|
10769
10709
|
};
|
|
10770
10710
|
|
|
10771
10711
|
// src/data-structures/tree/tree.ts
|
|
@@ -11416,16 +11356,21 @@ var dataStructureTyped = (() => {
|
|
|
11416
11356
|
};
|
|
11417
11357
|
var Trie = class _Trie {
|
|
11418
11358
|
constructor(words, caseSensitive = true) {
|
|
11359
|
+
__publicField(this, "_size");
|
|
11419
11360
|
__publicField(this, "_caseSensitive");
|
|
11420
11361
|
__publicField(this, "_root");
|
|
11421
11362
|
this._root = new TrieNode("");
|
|
11422
11363
|
this._caseSensitive = caseSensitive;
|
|
11364
|
+
this._size = 0;
|
|
11423
11365
|
if (words) {
|
|
11424
|
-
for (const
|
|
11425
|
-
this.add(
|
|
11366
|
+
for (const word of words) {
|
|
11367
|
+
this.add(word);
|
|
11426
11368
|
}
|
|
11427
11369
|
}
|
|
11428
11370
|
}
|
|
11371
|
+
get size() {
|
|
11372
|
+
return this._size;
|
|
11373
|
+
}
|
|
11429
11374
|
get caseSensitive() {
|
|
11430
11375
|
return this._caseSensitive;
|
|
11431
11376
|
}
|
|
@@ -11447,6 +11392,7 @@ var dataStructureTyped = (() => {
|
|
|
11447
11392
|
add(word) {
|
|
11448
11393
|
word = this._caseProcess(word);
|
|
11449
11394
|
let cur = this.root;
|
|
11395
|
+
let isNewWord = false;
|
|
11450
11396
|
for (const c of word) {
|
|
11451
11397
|
let nodeC = cur.children.get(c);
|
|
11452
11398
|
if (!nodeC) {
|
|
@@ -11455,8 +11401,12 @@ var dataStructureTyped = (() => {
|
|
|
11455
11401
|
}
|
|
11456
11402
|
cur = nodeC;
|
|
11457
11403
|
}
|
|
11458
|
-
cur.isEnd
|
|
11459
|
-
|
|
11404
|
+
if (!cur.isEnd) {
|
|
11405
|
+
isNewWord = true;
|
|
11406
|
+
cur.isEnd = true;
|
|
11407
|
+
this._size++;
|
|
11408
|
+
}
|
|
11409
|
+
return isNewWord;
|
|
11460
11410
|
}
|
|
11461
11411
|
/**
|
|
11462
11412
|
* Time Complexity: O(M), where M is the length of the input word.
|
|
@@ -11522,6 +11472,9 @@ var dataStructureTyped = (() => {
|
|
|
11522
11472
|
return false;
|
|
11523
11473
|
};
|
|
11524
11474
|
dfs(this.root, 0);
|
|
11475
|
+
if (isDeleted) {
|
|
11476
|
+
this._size--;
|
|
11477
|
+
}
|
|
11525
11478
|
return isDeleted;
|
|
11526
11479
|
}
|
|
11527
11480
|
/**
|
|
@@ -11744,6 +11697,9 @@ var dataStructureTyped = (() => {
|
|
|
11744
11697
|
}
|
|
11745
11698
|
return accumulator;
|
|
11746
11699
|
}
|
|
11700
|
+
print() {
|
|
11701
|
+
console.log([...this]);
|
|
11702
|
+
}
|
|
11747
11703
|
/**
|
|
11748
11704
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
11749
11705
|
* Space Complexity: O(1) - Constant space.
|