data-structure-typed 1.49.8 → 1.50.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/benchmark/report.html +1 -37
- package/benchmark/report.json +22 -370
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +0 -7
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +0 -9
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +5 -22
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +9 -80
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +89 -27
- package/dist/cjs/data-structures/binary-tree/bst.js +131 -46
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +0 -7
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +1 -10
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +0 -7
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +2 -11
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +16 -12
- package/dist/cjs/data-structures/hash/hash-map.js +36 -15
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +2 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +0 -7
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +0 -9
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +5 -22
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +9 -80
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +89 -27
- package/dist/mjs/data-structures/binary-tree/bst.js +131 -46
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +0 -7
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +1 -10
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +0 -7
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +2 -11
- package/dist/mjs/data-structures/hash/hash-map.d.ts +16 -12
- package/dist/mjs/data-structures/hash/hash-map.js +36 -15
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +2 -1
- package/dist/umd/data-structure-typed.js +176 -165
- 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 +0 -10
- package/src/data-structures/binary-tree/binary-tree.ts +104 -141
- package/src/data-structures/binary-tree/bst.ts +153 -49
- package/src/data-structures/binary-tree/rb-tree.ts +1 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +2 -12
- package/src/data-structures/hash/hash-map.ts +42 -16
- package/src/types/data-structures/hash/hash-map.ts +2 -1
- package/test/integration/all-in-one.test.ts +1 -1
- package/test/integration/index.html +2 -2
- package/test/performance/data-structures/queue/deque.test.ts +8 -2
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +38 -9
- package/test/unit/data-structures/binary-tree/bst.test.ts +8 -12
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +4 -4
- package/test/unit/data-structures/hash/hash-map.test.ts +17 -1
|
@@ -585,32 +585,49 @@ var dataStructureTyped = (() => {
|
|
|
585
585
|
// src/data-structures/hash/hash-map.ts
|
|
586
586
|
var HashMap = class _HashMap extends IterableEntryBase {
|
|
587
587
|
/**
|
|
588
|
-
* The constructor function initializes a
|
|
589
|
-
*
|
|
590
|
-
*
|
|
591
|
-
*
|
|
592
|
-
* @param [options] - The `options` parameter is an optional object that can contain
|
|
593
|
-
* configuration options for the constructor. In this case, it has one property:
|
|
588
|
+
* The constructor function initializes a HashMap object with an optional initial collection and
|
|
589
|
+
* options.
|
|
590
|
+
* @param rawCollection - The `rawCollection` parameter is an iterable collection of elements of type
|
|
591
|
+
* `T`. It is an optional parameter and its default value is an empty array `[]`.
|
|
592
|
+
* @param [options] - The `options` parameter is an optional object that can contain two properties:
|
|
594
593
|
*/
|
|
595
|
-
constructor(
|
|
594
|
+
constructor(rawCollection = [], options) {
|
|
596
595
|
super();
|
|
597
596
|
__publicField(this, "_store", {});
|
|
598
597
|
__publicField(this, "_objMap", /* @__PURE__ */ new Map());
|
|
598
|
+
__publicField(this, "_toEntryFn", (rawElement) => {
|
|
599
|
+
if (this.isEntry(rawElement)) {
|
|
600
|
+
return rawElement;
|
|
601
|
+
} else {
|
|
602
|
+
throw new Error(
|
|
603
|
+
"If the provided rawCollection does not adhere to the [key, value] type format, the toEntryFn in the constructor's options parameter needs to specified."
|
|
604
|
+
);
|
|
605
|
+
}
|
|
606
|
+
});
|
|
599
607
|
__publicField(this, "_size", 0);
|
|
600
608
|
__publicField(this, "_hashFn", (key) => String(key));
|
|
601
609
|
if (options) {
|
|
602
|
-
const { hashFn } = options;
|
|
610
|
+
const { hashFn, toEntryFn } = options;
|
|
603
611
|
if (hashFn) {
|
|
604
612
|
this._hashFn = hashFn;
|
|
605
613
|
}
|
|
614
|
+
if (toEntryFn) {
|
|
615
|
+
this._toEntryFn = toEntryFn;
|
|
616
|
+
}
|
|
606
617
|
}
|
|
607
|
-
if (
|
|
608
|
-
this.setMany(
|
|
618
|
+
if (rawCollection) {
|
|
619
|
+
this.setMany(rawCollection);
|
|
609
620
|
}
|
|
610
621
|
}
|
|
622
|
+
get toEntryFn() {
|
|
623
|
+
return this._toEntryFn;
|
|
624
|
+
}
|
|
611
625
|
get size() {
|
|
612
626
|
return this._size;
|
|
613
627
|
}
|
|
628
|
+
isEntry(rawElement) {
|
|
629
|
+
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
630
|
+
}
|
|
614
631
|
isEmpty() {
|
|
615
632
|
return this.size === 0;
|
|
616
633
|
}
|
|
@@ -644,14 +661,18 @@ var dataStructureTyped = (() => {
|
|
|
644
661
|
return true;
|
|
645
662
|
}
|
|
646
663
|
/**
|
|
647
|
-
* The function
|
|
648
|
-
*
|
|
649
|
-
*
|
|
664
|
+
* The function `setMany` takes an iterable collection of objects, maps each object to a key-value
|
|
665
|
+
* pair using a mapping function, and sets each key-value pair in the current object.
|
|
666
|
+
* @param rawCollection - The `rawCollection` parameter is an iterable collection of elements of type
|
|
667
|
+
* `T`.
|
|
668
|
+
* @returns The `setMany` function is returning an array of booleans.
|
|
650
669
|
*/
|
|
651
|
-
setMany(
|
|
670
|
+
setMany(rawCollection) {
|
|
652
671
|
const results = [];
|
|
653
|
-
for (const
|
|
672
|
+
for (const rawEle of rawCollection) {
|
|
673
|
+
const [key, value] = this.toEntryFn(rawEle);
|
|
654
674
|
results.push(this.set(key, value));
|
|
675
|
+
}
|
|
655
676
|
return results;
|
|
656
677
|
}
|
|
657
678
|
/**
|
|
@@ -7115,7 +7136,7 @@ var dataStructureTyped = (() => {
|
|
|
7115
7136
|
}
|
|
7116
7137
|
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
7117
7138
|
node = keyOrNodeOrEntry;
|
|
7118
|
-
} else if (this.
|
|
7139
|
+
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
7119
7140
|
node = this.createNode(keyOrNodeOrEntry, value);
|
|
7120
7141
|
} else {
|
|
7121
7142
|
return;
|
|
@@ -7203,15 +7224,6 @@ var dataStructureTyped = (() => {
|
|
|
7203
7224
|
isNodeOrNull(node) {
|
|
7204
7225
|
return this.isRealNode(node) || node === null;
|
|
7205
7226
|
}
|
|
7206
|
-
/**
|
|
7207
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
7208
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
7209
|
-
* data type.
|
|
7210
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
7211
|
-
*/
|
|
7212
|
-
isNotNodeInstance(potentialKey) {
|
|
7213
|
-
return !(potentialKey instanceof BinaryTreeNode);
|
|
7214
|
-
}
|
|
7215
7227
|
/**
|
|
7216
7228
|
* Time Complexity O(log n) - O(n)
|
|
7217
7229
|
* Space Complexity O(1)
|
|
@@ -7756,7 +7768,7 @@ var dataStructureTyped = (() => {
|
|
|
7756
7768
|
*
|
|
7757
7769
|
* The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
|
|
7758
7770
|
* structure, with the option to reverse the order of the nodes.
|
|
7759
|
-
* @param {K | N | null | undefined}
|
|
7771
|
+
* @param {K | N | null | undefined} beginNode - The `beginRoot` parameter represents the
|
|
7760
7772
|
* starting node from which you want to find the path to the root. It can be of type `K`, `N`,
|
|
7761
7773
|
* `null`, or `undefined`.
|
|
7762
7774
|
* @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
|
|
@@ -7764,16 +7776,16 @@ var dataStructureTyped = (() => {
|
|
|
7764
7776
|
* reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
|
|
7765
7777
|
* @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
|
|
7766
7778
|
*/
|
|
7767
|
-
getPathToRoot(
|
|
7779
|
+
getPathToRoot(beginNode, isReverse = true) {
|
|
7768
7780
|
const result = [];
|
|
7769
|
-
|
|
7770
|
-
if (!
|
|
7781
|
+
beginNode = this.ensureNode(beginNode);
|
|
7782
|
+
if (!beginNode)
|
|
7771
7783
|
return result;
|
|
7772
|
-
while (
|
|
7773
|
-
result.push(
|
|
7774
|
-
|
|
7784
|
+
while (beginNode.parent) {
|
|
7785
|
+
result.push(beginNode);
|
|
7786
|
+
beginNode = beginNode.parent;
|
|
7775
7787
|
}
|
|
7776
|
-
result.push(
|
|
7788
|
+
result.push(beginNode);
|
|
7777
7789
|
return isReverse ? result.reverse() : result;
|
|
7778
7790
|
}
|
|
7779
7791
|
/**
|
|
@@ -7908,65 +7920,6 @@ var dataStructureTyped = (() => {
|
|
|
7908
7920
|
return isStandardBST || isInverseBST;
|
|
7909
7921
|
}
|
|
7910
7922
|
}
|
|
7911
|
-
/**
|
|
7912
|
-
* Time complexity: O(n)
|
|
7913
|
-
* Space complexity: O(log n)
|
|
7914
|
-
*
|
|
7915
|
-
* The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
|
|
7916
|
-
* node, either recursively or iteratively.
|
|
7917
|
-
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
7918
|
-
* the subtree traversal. It takes a single parameter, which is the current node being traversed, and
|
|
7919
|
-
* returns a value of any type.
|
|
7920
|
-
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
7921
|
-
* starting node or key from which the subtree traversal should begin. It can be of type `K`,
|
|
7922
|
-
* `N`, `null`, or `undefined`. If not provided, the `root` property of the current object is used as
|
|
7923
|
-
* the default value.
|
|
7924
|
-
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
7925
|
-
* performed on the subtree. It can have two possible values:
|
|
7926
|
-
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
7927
|
-
* whether to include null values in the traversal. If `includeNull` is set to `true`, the
|
|
7928
|
-
* traversal will include null values, otherwise it will skip them.
|
|
7929
|
-
* @returns The function `subTreeTraverse` returns an array of values that are the result of invoking
|
|
7930
|
-
* the `callback` function on each node in the subtree. The type of the array nodes is determined
|
|
7931
|
-
* by the return type of the `callback` function.
|
|
7932
|
-
*/
|
|
7933
|
-
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
7934
|
-
beginRoot = this.ensureNode(beginRoot);
|
|
7935
|
-
const ans = [];
|
|
7936
|
-
if (!beginRoot)
|
|
7937
|
-
return ans;
|
|
7938
|
-
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
7939
|
-
const _traverse = (cur) => {
|
|
7940
|
-
if (cur !== void 0) {
|
|
7941
|
-
ans.push(callback(cur));
|
|
7942
|
-
if (includeNull) {
|
|
7943
|
-
cur && this.isNodeOrNull(cur.left) && _traverse(cur.left);
|
|
7944
|
-
cur && this.isNodeOrNull(cur.right) && _traverse(cur.right);
|
|
7945
|
-
} else {
|
|
7946
|
-
cur && cur.left && _traverse(cur.left);
|
|
7947
|
-
cur && cur.right && _traverse(cur.right);
|
|
7948
|
-
}
|
|
7949
|
-
}
|
|
7950
|
-
};
|
|
7951
|
-
_traverse(beginRoot);
|
|
7952
|
-
} else {
|
|
7953
|
-
const stack = [beginRoot];
|
|
7954
|
-
while (stack.length > 0) {
|
|
7955
|
-
const cur = stack.pop();
|
|
7956
|
-
if (cur !== void 0) {
|
|
7957
|
-
ans.push(callback(cur));
|
|
7958
|
-
if (includeNull) {
|
|
7959
|
-
cur && this.isNodeOrNull(cur.right) && stack.push(cur.right);
|
|
7960
|
-
cur && this.isNodeOrNull(cur.left) && stack.push(cur.left);
|
|
7961
|
-
} else {
|
|
7962
|
-
cur && cur.right && stack.push(cur.right);
|
|
7963
|
-
cur && cur.left && stack.push(cur.left);
|
|
7964
|
-
}
|
|
7965
|
-
}
|
|
7966
|
-
}
|
|
7967
|
-
}
|
|
7968
|
-
return ans;
|
|
7969
|
-
}
|
|
7970
7923
|
/**
|
|
7971
7924
|
* Time complexity: O(n)
|
|
7972
7925
|
* Space complexity: O(n)
|
|
@@ -8738,7 +8691,7 @@ var dataStructureTyped = (() => {
|
|
|
8738
8691
|
} else {
|
|
8739
8692
|
node = this.createNode(key, value2);
|
|
8740
8693
|
}
|
|
8741
|
-
} else if (this.
|
|
8694
|
+
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
8742
8695
|
node = this.createNode(keyOrNodeOrEntry, value);
|
|
8743
8696
|
} else {
|
|
8744
8697
|
return;
|
|
@@ -8775,15 +8728,6 @@ var dataStructureTyped = (() => {
|
|
|
8775
8728
|
}
|
|
8776
8729
|
return res;
|
|
8777
8730
|
}
|
|
8778
|
-
/**
|
|
8779
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
8780
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
8781
|
-
* data type.
|
|
8782
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
8783
|
-
*/
|
|
8784
|
-
isNotNodeInstance(potentialKey) {
|
|
8785
|
-
return !(potentialKey instanceof BSTNode);
|
|
8786
|
-
}
|
|
8787
8731
|
/**
|
|
8788
8732
|
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
8789
8733
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
@@ -8941,39 +8885,6 @@ var dataStructureTyped = (() => {
|
|
|
8941
8885
|
}
|
|
8942
8886
|
return inserted;
|
|
8943
8887
|
}
|
|
8944
|
-
/**
|
|
8945
|
-
* Time Complexity: O(n log n)
|
|
8946
|
-
* Space Complexity: O(n)
|
|
8947
|
-
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
8948
|
-
*/
|
|
8949
|
-
/**
|
|
8950
|
-
* Time Complexity: O(n log n)
|
|
8951
|
-
* Space Complexity: O(n)
|
|
8952
|
-
*
|
|
8953
|
-
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
8954
|
-
* leftmost node if the comparison result is greater than.
|
|
8955
|
-
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
8956
|
-
* type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
8957
|
-
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
8958
|
-
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
8959
|
-
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
8960
|
-
* rightmost node otherwise. If no node is found, it returns 0.
|
|
8961
|
-
*/
|
|
8962
|
-
lastKey(beginRoot = this.root) {
|
|
8963
|
-
let current = this.ensureNode(beginRoot);
|
|
8964
|
-
if (!current)
|
|
8965
|
-
return void 0;
|
|
8966
|
-
if (this._variant === "STANDARD" /* STANDARD */) {
|
|
8967
|
-
while (current.right !== void 0) {
|
|
8968
|
-
current = current.right;
|
|
8969
|
-
}
|
|
8970
|
-
} else {
|
|
8971
|
-
while (current.left !== void 0) {
|
|
8972
|
-
current = current.left;
|
|
8973
|
-
}
|
|
8974
|
-
}
|
|
8975
|
-
return current.key;
|
|
8976
|
-
}
|
|
8977
8888
|
/**
|
|
8978
8889
|
* Time Complexity: O(log n)
|
|
8979
8890
|
* Space Complexity: O(1)
|
|
@@ -9102,6 +9013,133 @@ var dataStructureTyped = (() => {
|
|
|
9102
9013
|
}
|
|
9103
9014
|
return ans;
|
|
9104
9015
|
}
|
|
9016
|
+
// /**
|
|
9017
|
+
// * The function overrides the subTreeTraverse method and returns the result of calling the super
|
|
9018
|
+
// * method with the provided arguments.
|
|
9019
|
+
// * @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
9020
|
+
// * the subtree traversal. It should accept a single parameter of type `N`, which represents a node in
|
|
9021
|
+
// * the tree. The return type of the callback function can be any type.
|
|
9022
|
+
// * @param beginRoot - The `beginRoot` parameter is the starting point for traversing the subtree. It
|
|
9023
|
+
// * can be either a key, a node, or an entry.
|
|
9024
|
+
// * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
9025
|
+
// * be performed during the traversal of the subtree. It can have one of the following values:
|
|
9026
|
+
// * @returns The method is returning an array of the return type of the callback function.
|
|
9027
|
+
// */
|
|
9028
|
+
// override subTreeTraverse<C extends BTNCallback<N>>(
|
|
9029
|
+
// callback: C = this._defaultOneParamCallback as C,
|
|
9030
|
+
// beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
9031
|
+
// iterationType = this.iterationType
|
|
9032
|
+
// ): ReturnType<C>[] {
|
|
9033
|
+
// return super.subTreeTraverse(callback, beginRoot, iterationType, false);
|
|
9034
|
+
// }
|
|
9035
|
+
/**
|
|
9036
|
+
* Time complexity: O(n)
|
|
9037
|
+
* Space complexity: O(n)
|
|
9038
|
+
*/
|
|
9039
|
+
/**
|
|
9040
|
+
* Time complexity: O(n)
|
|
9041
|
+
* Space complexity: O(n)
|
|
9042
|
+
*
|
|
9043
|
+
* The function overrides the depth-first search method and returns an array of the return types of
|
|
9044
|
+
* the callback function.
|
|
9045
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
9046
|
+
* during the depth-first search traversal. It is an optional parameter and if not provided, a
|
|
9047
|
+
* default callback function will be used.
|
|
9048
|
+
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter specifies the order in which the
|
|
9049
|
+
* nodes are visited during the depth-first search. It can have one of the following values:
|
|
9050
|
+
* @param beginRoot - The `beginRoot` parameter is used to specify the starting point for the
|
|
9051
|
+
* Depth-First Search (DFS) traversal. It can be either a key, a node, or an entry in the tree. If no
|
|
9052
|
+
* value is provided, the DFS traversal will start from the root of the tree.
|
|
9053
|
+
* @param {IterationType} iterationType - The `iterationType` parameter specifies the type of
|
|
9054
|
+
* iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the
|
|
9055
|
+
* following values:
|
|
9056
|
+
* @returns The method is returning an array of the return type of the callback function.
|
|
9057
|
+
*/
|
|
9058
|
+
dfs(callback = this._defaultOneParamCallback, pattern = "in", beginRoot = this.root, iterationType = "ITERATIVE" /* ITERATIVE */) {
|
|
9059
|
+
return super.dfs(callback, pattern, beginRoot, iterationType, false);
|
|
9060
|
+
}
|
|
9061
|
+
/**
|
|
9062
|
+
* Time complexity: O(n)
|
|
9063
|
+
* Space complexity: O(n)
|
|
9064
|
+
*/
|
|
9065
|
+
/**
|
|
9066
|
+
* Time complexity: O(n)
|
|
9067
|
+
* Space complexity: O(n)
|
|
9068
|
+
*
|
|
9069
|
+
* The function overrides the breadth-first search method and returns an array of the return types of
|
|
9070
|
+
* the callback function.
|
|
9071
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
9072
|
+
* visited during the breadth-first search traversal. It is an optional parameter and if not
|
|
9073
|
+
* provided, a default callback function will be used.
|
|
9074
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the breadth-first search
|
|
9075
|
+
* traversal. It can be either a key, a node, or an entry in the tree. If not specified, the root of
|
|
9076
|
+
* the tree is used as the starting point.
|
|
9077
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
9078
|
+
* be performed during the breadth-first search (BFS) traversal. It determines the order in which the
|
|
9079
|
+
* nodes are visited.
|
|
9080
|
+
* @returns The method is returning an array of the return type of the callback function.
|
|
9081
|
+
*/
|
|
9082
|
+
bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
9083
|
+
return super.bfs(callback, beginRoot, iterationType, false);
|
|
9084
|
+
}
|
|
9085
|
+
/**
|
|
9086
|
+
* Time complexity: O(n)
|
|
9087
|
+
* Space complexity: O(n)
|
|
9088
|
+
*/
|
|
9089
|
+
/**
|
|
9090
|
+
* Time complexity: O(n)
|
|
9091
|
+
* Space complexity: O(n)
|
|
9092
|
+
*
|
|
9093
|
+
* The function overrides the listLevels method and returns an array of arrays containing the return
|
|
9094
|
+
* type of the callback function for each level of the tree.
|
|
9095
|
+
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
9096
|
+
* `BTNCallback<N>`. It represents a callback function that will be called for each node in the tree
|
|
9097
|
+
* during the level listing process.
|
|
9098
|
+
* @param beginRoot - The `beginRoot` parameter is used to specify the starting point for listing the
|
|
9099
|
+
* levels of a binary tree. It can be either a key, a node, or an entry in the binary tree. If not
|
|
9100
|
+
* provided, the root of the binary tree is used as the starting point.
|
|
9101
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
9102
|
+
* be performed on the tree. It determines the order in which the nodes are visited during the
|
|
9103
|
+
* iteration.
|
|
9104
|
+
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
9105
|
+
* function.
|
|
9106
|
+
*/
|
|
9107
|
+
listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
9108
|
+
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
9109
|
+
}
|
|
9110
|
+
/**
|
|
9111
|
+
* Time Complexity: O(n log n)
|
|
9112
|
+
* Space Complexity: O(n)
|
|
9113
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
9114
|
+
*/
|
|
9115
|
+
/**
|
|
9116
|
+
* Time Complexity: O(n log n)
|
|
9117
|
+
* Space Complexity: O(n)
|
|
9118
|
+
*
|
|
9119
|
+
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
9120
|
+
* leftmost node if the comparison result is greater than.
|
|
9121
|
+
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
9122
|
+
* type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
9123
|
+
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
9124
|
+
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
9125
|
+
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
9126
|
+
* rightmost node otherwise. If no node is found, it returns 0.
|
|
9127
|
+
*/
|
|
9128
|
+
lastKey(beginRoot = this.root) {
|
|
9129
|
+
let current = this.ensureNode(beginRoot);
|
|
9130
|
+
if (!current)
|
|
9131
|
+
return void 0;
|
|
9132
|
+
if (this._variant === "STANDARD" /* STANDARD */) {
|
|
9133
|
+
while (current.right !== void 0) {
|
|
9134
|
+
current = current.right;
|
|
9135
|
+
}
|
|
9136
|
+
} else {
|
|
9137
|
+
while (current.left !== void 0) {
|
|
9138
|
+
current = current.left;
|
|
9139
|
+
}
|
|
9140
|
+
}
|
|
9141
|
+
return current.key;
|
|
9142
|
+
}
|
|
9105
9143
|
/**
|
|
9106
9144
|
* Time Complexity: O(log n)
|
|
9107
9145
|
* Space Complexity: O(log n)
|
|
@@ -9786,15 +9824,6 @@ var dataStructureTyped = (() => {
|
|
|
9786
9824
|
isNode(keyOrNodeOrEntry) {
|
|
9787
9825
|
return keyOrNodeOrEntry instanceof AVLTreeNode;
|
|
9788
9826
|
}
|
|
9789
|
-
/**
|
|
9790
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
9791
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
9792
|
-
* data type.
|
|
9793
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
9794
|
-
*/
|
|
9795
|
-
isNotNodeInstance(potentialKey) {
|
|
9796
|
-
return !(potentialKey instanceof AVLTreeNode);
|
|
9797
|
-
}
|
|
9798
9827
|
/**
|
|
9799
9828
|
* Time Complexity: O(log n)
|
|
9800
9829
|
* Space Complexity: O(1)
|
|
@@ -10243,7 +10272,7 @@ var dataStructureTyped = (() => {
|
|
|
10243
10272
|
} else {
|
|
10244
10273
|
node = this.createNode(key, value2, 1 /* RED */);
|
|
10245
10274
|
}
|
|
10246
|
-
} else if (this.
|
|
10275
|
+
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
10247
10276
|
node = this.createNode(keyOrNodeOrEntry, value, 1 /* RED */);
|
|
10248
10277
|
} else {
|
|
10249
10278
|
return;
|
|
@@ -10268,15 +10297,6 @@ var dataStructureTyped = (() => {
|
|
|
10268
10297
|
return false;
|
|
10269
10298
|
return node instanceof RedBlackTreeNode;
|
|
10270
10299
|
}
|
|
10271
|
-
/**
|
|
10272
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
10273
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
10274
|
-
* data type.
|
|
10275
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
10276
|
-
*/
|
|
10277
|
-
isNotNodeInstance(potentialKey) {
|
|
10278
|
-
return !(potentialKey instanceof RedBlackTreeNode);
|
|
10279
|
-
}
|
|
10280
10300
|
/**
|
|
10281
10301
|
* Time Complexity: O(log n)
|
|
10282
10302
|
* Space Complexity: O(1)
|
|
@@ -10736,7 +10756,7 @@ var dataStructureTyped = (() => {
|
|
|
10736
10756
|
// TODO the _count is not accurate after nodes count modified
|
|
10737
10757
|
get count() {
|
|
10738
10758
|
let sum = 0;
|
|
10739
|
-
this.
|
|
10759
|
+
this.dfs((node) => sum += node.count);
|
|
10740
10760
|
return sum;
|
|
10741
10761
|
}
|
|
10742
10762
|
/**
|
|
@@ -10781,7 +10801,7 @@ var dataStructureTyped = (() => {
|
|
|
10781
10801
|
} else {
|
|
10782
10802
|
node = this.createNode(key, value2, count);
|
|
10783
10803
|
}
|
|
10784
|
-
} else if (this.
|
|
10804
|
+
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
10785
10805
|
node = this.createNode(keyOrNodeOrEntry, value, count);
|
|
10786
10806
|
} else {
|
|
10787
10807
|
return;
|
|
@@ -10797,15 +10817,6 @@ var dataStructureTyped = (() => {
|
|
|
10797
10817
|
isNode(keyOrNodeOrEntry) {
|
|
10798
10818
|
return keyOrNodeOrEntry instanceof TreeMultimapNode;
|
|
10799
10819
|
}
|
|
10800
|
-
/**
|
|
10801
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
10802
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
10803
|
-
* data type.
|
|
10804
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
10805
|
-
*/
|
|
10806
|
-
isNotNodeInstance(potentialKey) {
|
|
10807
|
-
return !(potentialKey instanceof TreeMultimapNode);
|
|
10808
|
-
}
|
|
10809
10820
|
/**
|
|
10810
10821
|
* Time Complexity: O(log n)
|
|
10811
10822
|
* Space Complexity: O(1)
|