data-structure-typed 1.51.5 → 1.51.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.
Files changed (39) hide show
  1. package/CHANGELOG.md +3 -1
  2. package/README.md +17 -16
  3. package/benchmark/report.html +37 -1
  4. package/benchmark/report.json +405 -3
  5. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +1 -0
  6. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  7. package/dist/cjs/data-structures/binary-tree/avl-tree.js +0 -2
  8. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  9. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +2 -1
  10. package/dist/cjs/data-structures/binary-tree/binary-tree.js +24 -46
  11. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  12. package/dist/cjs/data-structures/binary-tree/bst.d.ts +19 -19
  13. package/dist/cjs/data-structures/binary-tree/bst.js +59 -89
  14. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  15. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +10 -1
  16. package/dist/cjs/data-structures/binary-tree/rb-tree.js +19 -0
  17. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +1 -0
  19. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  20. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +1 -0
  21. package/dist/mjs/data-structures/binary-tree/avl-tree.js +0 -2
  22. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +2 -1
  23. package/dist/mjs/data-structures/binary-tree/binary-tree.js +23 -45
  24. package/dist/mjs/data-structures/binary-tree/bst.d.ts +19 -19
  25. package/dist/mjs/data-structures/binary-tree/bst.js +59 -89
  26. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +10 -1
  27. package/dist/mjs/data-structures/binary-tree/rb-tree.js +19 -0
  28. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +1 -0
  29. package/dist/umd/data-structure-typed.js +98 -118
  30. package/dist/umd/data-structure-typed.min.js +2 -2
  31. package/dist/umd/data-structure-typed.min.js.map +1 -1
  32. package/package.json +6 -6
  33. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +1 -0
  34. package/src/data-structures/binary-tree/avl-tree.ts +0 -1
  35. package/src/data-structures/binary-tree/binary-tree.ts +27 -38
  36. package/src/data-structures/binary-tree/bst.ts +59 -81
  37. package/src/data-structures/binary-tree/rb-tree.ts +19 -2
  38. package/src/data-structures/binary-tree/tree-multi-map.ts +1 -0
  39. package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +7 -7
@@ -7860,21 +7860,24 @@ var dataStructureTyped = (() => {
7860
7860
  * itself if it is not a valid node key.
7861
7861
  */
7862
7862
  ensureNode(keyOrNodeOrEntry, iterationType = "ITERATIVE") {
7863
+ if (keyOrNodeOrEntry === this.NIL)
7864
+ return;
7863
7865
  if (this.isRealNode(keyOrNodeOrEntry)) {
7864
7866
  return keyOrNodeOrEntry;
7865
- } else if (this.isEntry(keyOrNodeOrEntry)) {
7866
- if (keyOrNodeOrEntry[0] === null)
7867
- return null;
7868
- if (keyOrNodeOrEntry[0] === void 0)
7869
- return;
7870
- return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
7871
- } else {
7872
- if (keyOrNodeOrEntry === null)
7867
+ }
7868
+ if (this.isEntry(keyOrNodeOrEntry)) {
7869
+ const key = keyOrNodeOrEntry[0];
7870
+ if (key === null)
7873
7871
  return null;
7874
- if (keyOrNodeOrEntry === void 0)
7872
+ if (key === void 0)
7875
7873
  return;
7876
- return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
7874
+ return this.getNodeByKey(key, iterationType);
7877
7875
  }
7876
+ if (keyOrNodeOrEntry === null)
7877
+ return null;
7878
+ if (keyOrNodeOrEntry === void 0)
7879
+ return;
7880
+ return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
7878
7881
  }
7879
7882
  /**
7880
7883
  * The function checks if a given node is a real node or null.
@@ -8052,8 +8055,7 @@ var dataStructureTyped = (() => {
8052
8055
  const deletedResult = [];
8053
8056
  if (!this.root)
8054
8057
  return deletedResult;
8055
- if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
8056
- callback = (node) => node;
8058
+ callback = this._ensureCallback(identifier, callback);
8057
8059
  const curr = this.getNode(identifier, callback);
8058
8060
  if (!curr)
8059
8061
  return deletedResult;
@@ -8121,11 +8123,10 @@ var dataStructureTyped = (() => {
8121
8123
  * @returns an array of nodes of type `NODE`.
8122
8124
  */
8123
8125
  getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
8124
- if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
8125
- callback = (node) => node;
8126
8126
  beginRoot = this.ensureNode(beginRoot);
8127
8127
  if (!beginRoot)
8128
8128
  return [];
8129
+ callback = this._ensureCallback(identifier, callback);
8129
8130
  const ans = [];
8130
8131
  if (iterationType === "RECURSIVE") {
8131
8132
  const dfs = (cur) => {
@@ -8205,32 +8206,7 @@ var dataStructureTyped = (() => {
8205
8206
  * found in the binary tree. If no node is found, it returns `undefined`.
8206
8207
  */
8207
8208
  getNodeByKey(key, iterationType = "ITERATIVE") {
8208
- if (!this.root)
8209
- return void 0;
8210
- if (iterationType === "RECURSIVE") {
8211
- const dfs = (cur) => {
8212
- if (cur.key === key)
8213
- return cur;
8214
- if (!cur.left && !cur.right)
8215
- return;
8216
- if (cur.left)
8217
- return dfs(cur.left);
8218
- if (cur.right)
8219
- return dfs(cur.right);
8220
- };
8221
- return dfs(this.root);
8222
- } else {
8223
- const stack = [this.root];
8224
- while (stack.length > 0) {
8225
- const cur = stack.pop();
8226
- if (cur) {
8227
- if (cur.key === key)
8228
- return cur;
8229
- cur.left && stack.push(cur.left);
8230
- cur.right && stack.push(cur.right);
8231
- }
8232
- }
8233
- }
8209
+ return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
8234
8210
  }
8235
8211
  /**
8236
8212
  * Time Complexity: O(n)
@@ -8259,8 +8235,8 @@ var dataStructureTyped = (() => {
8259
8235
  * found, `undefined` is returned.
8260
8236
  */
8261
8237
  get(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
8262
- var _a, _b;
8263
- return (_b = (_a = this.getNode(identifier, callback, beginRoot, iterationType)) == null ? void 0 : _a.value) != null ? _b : void 0;
8238
+ var _a;
8239
+ return (_a = this.getNode(identifier, callback, beginRoot, iterationType)) == null ? void 0 : _a.value;
8264
8240
  }
8265
8241
  /**
8266
8242
  * Time Complexity: O(n)
@@ -8288,8 +8264,7 @@ var dataStructureTyped = (() => {
8288
8264
  * @returns a boolean value.
8289
8265
  */
8290
8266
  has(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
8291
- if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
8292
- callback = (node) => node;
8267
+ callback = this._ensureCallback(identifier, callback);
8293
8268
  return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
8294
8269
  }
8295
8270
  /**
@@ -9344,6 +9319,12 @@ var dataStructureTyped = (() => {
9344
9319
  }
9345
9320
  this._root = v;
9346
9321
  }
9322
+ _ensureCallback(identifier, callback = this._DEFAULT_CALLBACK) {
9323
+ if ((!callback || callback === this._DEFAULT_CALLBACK) && this.isNode(identifier)) {
9324
+ callback = (node) => node;
9325
+ }
9326
+ return callback;
9327
+ }
9347
9328
  };
9348
9329
 
9349
9330
  // src/data-structures/binary-tree/bst.ts
@@ -9501,17 +9482,21 @@ var dataStructureTyped = (() => {
9501
9482
  * @returns either a node object (NODE) or undefined.
9502
9483
  */
9503
9484
  ensureNode(keyOrNodeOrEntry, iterationType = "ITERATIVE") {
9485
+ if (keyOrNodeOrEntry === this.NIL)
9486
+ return;
9504
9487
  if (this.isRealNode(keyOrNodeOrEntry)) {
9505
9488
  return keyOrNodeOrEntry;
9506
- } else if (this.isEntry(keyOrNodeOrEntry)) {
9507
- if (keyOrNodeOrEntry[0] === null || keyOrNodeOrEntry[0] === void 0)
9508
- return;
9509
- return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
9510
- } else {
9511
- if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === void 0)
9489
+ }
9490
+ if (this.isEntry(keyOrNodeOrEntry)) {
9491
+ const key2 = keyOrNodeOrEntry[0];
9492
+ if (key2 === null || key2 === void 0)
9512
9493
  return;
9513
- return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
9494
+ return this.getNodeByKey(key2, iterationType);
9514
9495
  }
9496
+ const key = keyOrNodeOrEntry;
9497
+ if (key === null || key === void 0)
9498
+ return;
9499
+ return this.getNodeByKey(key, iterationType);
9515
9500
  }
9516
9501
  /**
9517
9502
  * The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
@@ -9666,54 +9651,6 @@ var dataStructureTyped = (() => {
9666
9651
  }
9667
9652
  return inserted;
9668
9653
  }
9669
- /**
9670
- * Time Complexity: O(log n)
9671
- * Space Complexity: O(1)
9672
- */
9673
- /**
9674
- * Time Complexity: O(log n)
9675
- * Space Complexity: O(1)
9676
- *
9677
- * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
9678
- * either recursive or iterative methods.
9679
- * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
9680
- * It is used to identify the node that we want to retrieve.
9681
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
9682
- * type of iteration to use when searching for a node in the binary tree. It can have two possible
9683
- * values:
9684
- * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
9685
- * found in the binary tree. If no node is found, it returns `undefined`.
9686
- */
9687
- getNodeByKey(key, iterationType = "ITERATIVE") {
9688
- if (!this.isRealNode(this.root))
9689
- return;
9690
- if (iterationType === "RECURSIVE") {
9691
- const dfs = (cur) => {
9692
- if (cur.key === key)
9693
- return cur;
9694
- if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
9695
- return;
9696
- if (this.isRealNode(cur.left) && this._compare(cur.key, key) === "GT")
9697
- return dfs(cur.left);
9698
- if (this.isRealNode(cur.right) && this._compare(cur.key, key) === "LT")
9699
- return dfs(cur.right);
9700
- };
9701
- return dfs(this.root);
9702
- } else {
9703
- const stack = [this.root];
9704
- while (stack.length > 0) {
9705
- const cur = stack.pop();
9706
- if (this.isRealNode(cur)) {
9707
- if (this._compare(cur.key, key) === "EQ")
9708
- return cur;
9709
- if (this.isRealNode(cur.left) && this._compare(cur.key, key) === "GT")
9710
- stack.push(cur.left);
9711
- if (this.isRealNode(cur.right) && this._compare(cur.key, key) === "LT")
9712
- stack.push(cur.right);
9713
- }
9714
- }
9715
- }
9716
- }
9717
9654
  /**
9718
9655
  * Time Complexity: O(log n)
9719
9656
  * Space Complexity: O(k + log n)
@@ -9746,6 +9683,7 @@ var dataStructureTyped = (() => {
9746
9683
  beginRoot = this.ensureNode(beginRoot);
9747
9684
  if (!beginRoot)
9748
9685
  return [];
9686
+ callback = this._ensureCallback(identifier, callback);
9749
9687
  const ans = [];
9750
9688
  if (iterationType === "RECURSIVE") {
9751
9689
  const dfs = (cur) => {
@@ -9772,22 +9710,20 @@ var dataStructureTyped = (() => {
9772
9710
  const stack = [beginRoot];
9773
9711
  while (stack.length > 0) {
9774
9712
  const cur = stack.pop();
9775
- if (this.isRealNode(cur)) {
9776
- const callbackResult = callback(cur);
9777
- if (callbackResult === identifier) {
9778
- ans.push(cur);
9779
- if (onlyOne)
9780
- return ans;
9781
- }
9782
- if (callback === this._DEFAULT_CALLBACK) {
9783
- if (this.isRealNode(cur.right) && this._compare(cur.key, identifier) === "LT")
9784
- stack.push(cur.right);
9785
- if (this.isRealNode(cur.left) && this._compare(cur.key, identifier) === "GT")
9786
- stack.push(cur.left);
9787
- } else {
9788
- this.isRealNode(cur.right) && stack.push(cur.right);
9789
- this.isRealNode(cur.left) && stack.push(cur.left);
9790
- }
9713
+ const callbackResult = callback(cur);
9714
+ if (callbackResult === identifier) {
9715
+ ans.push(cur);
9716
+ if (onlyOne)
9717
+ return ans;
9718
+ }
9719
+ if (callback === this._DEFAULT_CALLBACK) {
9720
+ if (this.isRealNode(cur.right) && this._compare(cur.key, identifier) === "LT")
9721
+ stack.push(cur.right);
9722
+ if (this.isRealNode(cur.left) && this._compare(cur.key, identifier) === "GT")
9723
+ stack.push(cur.left);
9724
+ } else {
9725
+ this.isRealNode(cur.right) && stack.push(cur.right);
9726
+ this.isRealNode(cur.left) && stack.push(cur.left);
9791
9727
  }
9792
9728
  }
9793
9729
  }
@@ -9822,6 +9758,27 @@ var dataStructureTyped = (() => {
9822
9758
  var _a;
9823
9759
  return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) != null ? _a : void 0;
9824
9760
  }
9761
+ /**
9762
+ * Time Complexity: O(log n)
9763
+ * Space Complexity: O(1)
9764
+ */
9765
+ /**
9766
+ * Time Complexity: O(log n)
9767
+ * Space Complexity: O(1)
9768
+ *
9769
+ * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
9770
+ * either recursive or iterative methods.
9771
+ * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
9772
+ * It is used to identify the node that we want to retrieve.
9773
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
9774
+ * type of iteration to use when searching for a node in the binary tree. It can have two possible
9775
+ * values:
9776
+ * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
9777
+ * found in the binary tree. If no node is found, it returns `undefined`.
9778
+ */
9779
+ getNodeByKey(key, iterationType = "ITERATIVE") {
9780
+ return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
9781
+ }
9825
9782
  /**
9826
9783
  * Time complexity: O(n)
9827
9784
  * Space complexity: O(n)
@@ -10129,7 +10086,11 @@ var dataStructureTyped = (() => {
10129
10086
  const extractedA = this.extractor(a);
10130
10087
  const extractedB = this.extractor(b);
10131
10088
  const compared = this.variant === "STANDARD" ? extractedA - extractedB : extractedB - extractedA;
10132
- return compared > 0 ? "GT" : compared < 0 ? "LT" : "EQ";
10089
+ if (compared > 0)
10090
+ return "GT";
10091
+ if (compared < 0)
10092
+ return "LT";
10093
+ return "EQ";
10133
10094
  }
10134
10095
  /**
10135
10096
  * The function `_lt` compares two values `a` and `b` using an extractor function and returns true if
@@ -10849,8 +10810,6 @@ var dataStructureTyped = (() => {
10849
10810
  * @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
10850
10811
  */
10851
10812
  delete(identifier, callback = this._DEFAULT_CALLBACK) {
10852
- if (identifier instanceof AVLTreeNode)
10853
- callback = (node) => node;
10854
10813
  const deletedResults = super.delete(identifier, callback);
10855
10814
  for (const { needBalanced } of deletedResults) {
10856
10815
  if (needBalanced) {
@@ -11383,6 +11342,7 @@ var dataStructureTyped = (() => {
11383
11342
  if (identifier === null)
11384
11343
  return [];
11385
11344
  const results = [];
11345
+ callback = this._ensureCallback(identifier, callback);
11386
11346
  const nodeToDelete = this.isRealNode(identifier) ? identifier : this.getNode(identifier, callback);
11387
11347
  if (!nodeToDelete) {
11388
11348
  return results;
@@ -11726,6 +11686,24 @@ var dataStructureTyped = (() => {
11726
11686
  x.right = y;
11727
11687
  y.parent = x;
11728
11688
  }
11689
+ /**
11690
+ * The function compares two values using a comparator function and returns whether the first value
11691
+ * is greater than, less than, or equal to the second value.
11692
+ * @param {K} a - The parameter "a" is of type K.
11693
+ * @param {K} b - The parameter "b" in the above code represents a K.
11694
+ * @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
11695
+ * than), 'LT' (less than), or 'EQ' (equal).
11696
+ */
11697
+ _compare(a, b) {
11698
+ const extractedA = this.extractor(a);
11699
+ const extractedB = this.extractor(b);
11700
+ const compared = extractedA - extractedB;
11701
+ if (compared > 0)
11702
+ return "GT";
11703
+ if (compared < 0)
11704
+ return "LT";
11705
+ return "EQ";
11706
+ }
11729
11707
  };
11730
11708
 
11731
11709
  // src/data-structures/binary-tree/avl-tree-multi-map.ts
@@ -11920,6 +11898,7 @@ var dataStructureTyped = (() => {
11920
11898
  const deletedResult = [];
11921
11899
  if (!this.root)
11922
11900
  return deletedResult;
11901
+ callback = this._ensureCallback(identifier, callback);
11923
11902
  const curr = (_a = this.getNode(identifier, callback)) != null ? _a : void 0;
11924
11903
  if (!curr)
11925
11904
  return deletedResult;
@@ -12295,6 +12274,7 @@ var dataStructureTyped = (() => {
12295
12274
  if (identifier === null)
12296
12275
  return [];
12297
12276
  const results = [];
12277
+ callback = this._ensureCallback(identifier, callback);
12298
12278
  const nodeToDelete = this.isRealNode(identifier) ? identifier : this.getNode(identifier, callback);
12299
12279
  if (!nodeToDelete) {
12300
12280
  return results;