data-structure-typed 1.52.8 → 1.52.9

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 (46) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +13 -13
  3. package/benchmark/report.html +13 -13
  4. package/benchmark/report.json +146 -146
  5. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +4 -4
  6. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +4 -4
  7. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +3 -3
  9. package/dist/cjs/data-structures/binary-tree/avl-tree.js +3 -3
  10. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +62 -5
  12. package/dist/cjs/data-structures/binary-tree/binary-tree.js +26 -11
  13. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/bst.js +9 -14
  15. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  16. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +3 -3
  17. package/dist/cjs/data-structures/binary-tree/rb-tree.js +8 -6
  18. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  19. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +4 -5
  20. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +9 -8
  21. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  22. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +4 -4
  23. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +4 -4
  24. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +3 -3
  25. package/dist/mjs/data-structures/binary-tree/avl-tree.js +3 -3
  26. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +62 -5
  27. package/dist/mjs/data-structures/binary-tree/binary-tree.js +26 -11
  28. package/dist/mjs/data-structures/binary-tree/bst.js +9 -14
  29. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +3 -3
  30. package/dist/mjs/data-structures/binary-tree/rb-tree.js +8 -6
  31. package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +4 -5
  32. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +9 -8
  33. package/dist/umd/data-structure-typed.js +58 -45
  34. package/dist/umd/data-structure-typed.min.js +5 -5
  35. package/dist/umd/data-structure-typed.min.js.map +1 -1
  36. package/package.json +6 -6
  37. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +4 -5
  38. package/src/data-structures/binary-tree/avl-tree.ts +3 -4
  39. package/src/data-structures/binary-tree/binary-tree.ts +26 -30
  40. package/src/data-structures/binary-tree/bst.ts +16 -19
  41. package/src/data-structures/binary-tree/rb-tree.ts +8 -6
  42. package/src/data-structures/binary-tree/tree-multi-map.ts +9 -8
  43. package/test/integration/bst.test.ts +2 -2
  44. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +20 -0
  45. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +37 -17
  46. package/test/unit/data-structures/binary-tree/bst.test.ts +11 -6
@@ -179,25 +179,26 @@ export class TreeMultiMap extends RedBlackTree {
179
179
  *
180
180
  * The function `delete` in TypeScript overrides the deletion operation in a binary tree data
181
181
  * structure, handling cases where nodes have children and maintaining balance in the tree.
182
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
182
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `predicate`
183
183
  * parameter in the `delete` method is used to specify the condition or key based on which a node
184
- * should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
185
- * function.
184
+ * should be deleted from the binary tree. It can be a key, a node, or an entry.
186
185
  * @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
187
186
  * boolean flag that determines whether to ignore the count of nodes when performing deletion. If
188
187
  * `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If
189
188
  * `ignoreCount` is `false
190
189
  * @returns The `override delete` method returns an array of `BinaryTreeDeleteResult<NODE>` objects.
191
190
  */
192
- delete(predicate, ignoreCount = false) {
193
- if (predicate === null)
191
+ delete(keyOrNodeOrEntryOrRaw, ignoreCount = false) {
192
+ if (keyOrNodeOrEntryOrRaw === null)
194
193
  return [];
195
194
  const results = [];
196
195
  let nodeToDelete;
197
- if (this._isPredicated(predicate))
198
- nodeToDelete = this.getNode(predicate);
196
+ if (this._isPredicated(keyOrNodeOrEntryOrRaw))
197
+ nodeToDelete = this.getNode(keyOrNodeOrEntryOrRaw);
199
198
  else
200
- nodeToDelete = this.isRealNode(predicate) ? predicate : this.getNode(predicate);
199
+ nodeToDelete = this.isRealNode(keyOrNodeOrEntryOrRaw)
200
+ ? keyOrNodeOrEntryOrRaw
201
+ : this.getNode(keyOrNodeOrEntryOrRaw);
201
202
  if (!nodeToDelete) {
202
203
  return results;
203
204
  }
@@ -7280,19 +7280,19 @@ var dataStructureTyped = (() => {
7280
7280
  *
7281
7281
  * The function `delete` in TypeScript implements the deletion of a node in a binary tree and returns
7282
7282
  * the deleted node along with information for tree balancing.
7283
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
7283
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw
7284
7284
  * - The `delete` method you provided is used to delete a node from a binary tree based on the key,
7285
- * node, entry, raw data, or a custom predicate. The method returns an array of
7285
+ * node, entry or raw data. The method returns an array of
7286
7286
  * `BinaryTreeDeleteResult` objects containing information about the deleted node and whether
7287
7287
  * balancing is needed.
7288
7288
  * @returns The `delete` method returns an array of `BinaryTreeDeleteResult` objects. Each object in
7289
7289
  * the array contains information about the node that was deleted (`deleted`) and the node that may
7290
7290
  * need to be balanced (`needBalanced`).
7291
7291
  */
7292
- delete(keyOrNodeOrEntryOrRawOrPredicate) {
7292
+ delete(keyOrNodeOrEntryOrRaw) {
7293
7293
  const deletedResult = [];
7294
7294
  if (!this._root) return deletedResult;
7295
- const curr = this.getNode(keyOrNodeOrEntryOrRawOrPredicate);
7295
+ const curr = this.getNode(keyOrNodeOrEntryOrRaw);
7296
7296
  if (!curr) return deletedResult;
7297
7297
  const parent = curr == null ? void 0 : curr.parent;
7298
7298
  let needBalanced;
@@ -8251,19 +8251,16 @@ var dataStructureTyped = (() => {
8251
8251
  * binary tree with the specified options.
8252
8252
  */
8253
8253
  toVisual(beginRoot = this._root, options) {
8254
- const opts = __spreadValues({ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false }, options);
8254
+ const opts = __spreadValues({ isShowUndefined: false, isShowNull: true, isShowRedBlackNIL: false }, options);
8255
8255
  beginRoot = this.ensureNode(beginRoot);
8256
8256
  let output = "";
8257
8257
  if (!beginRoot) return output;
8258
- if (opts.isShowUndefined)
8259
- output += `U for undefined
8260
- `;
8261
- if (opts.isShowNull)
8262
- output += `N for null
8263
- `;
8264
- if (opts.isShowRedBlackNIL)
8265
- output += `S for Sentinel Node(NIL)
8266
- `;
8258
+ if (opts.isShowUndefined) output += `U for undefined
8259
+ `;
8260
+ if (opts.isShowNull) output += `N for null
8261
+ `;
8262
+ if (opts.isShowRedBlackNIL) output += `S for Sentinel Node(NIL)
8263
+ `;
8267
8264
  const display = (root) => {
8268
8265
  const [lines, , ,] = this._displayAux(root, opts);
8269
8266
  let paragraph = "";
@@ -8275,6 +8272,24 @@ var dataStructureTyped = (() => {
8275
8272
  display(beginRoot);
8276
8273
  return output;
8277
8274
  }
8275
+ /**
8276
+ * Time Complexity: O(n)
8277
+ * Space Complexity: O(n)
8278
+ *
8279
+ * The function `print` in TypeScript overrides the default print behavior to log a visual
8280
+ * representation of the binary tree to the console.
8281
+ * @param {BinaryTreePrintOptions} [options] - The `options` parameter is used to specify the
8282
+ * printing options for the binary tree. It is an optional parameter that allows you to customize how
8283
+ * the binary tree is printed, such as choosing between different traversal orders or formatting
8284
+ * options.
8285
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
8286
+ * `override print` method is used to specify the starting point for printing the binary tree. It can
8287
+ * be either a key, a node, an entry, or the root of the tree. If no specific starting point is
8288
+ * provided, the default value is set to
8289
+ */
8290
+ print(options, beginRoot = this._root) {
8291
+ console.log(this.toVisual(beginRoot, options));
8292
+ }
8278
8293
  /**
8279
8294
  * Time complexity: O(n)
8280
8295
  * Space complexity: O(n)
@@ -8846,21 +8861,18 @@ var dataStructureTyped = (() => {
8846
8861
  if (!isBalanceAdd) {
8847
8862
  for (const kve of keysOrNodesOrEntriesOrRaws) {
8848
8863
  const value = valuesIterator == null ? void 0 : valuesIterator.next().value;
8849
- const nn = this.add(kve, value);
8850
- inserted.push(nn);
8864
+ inserted.push(this.add(kve, value));
8851
8865
  }
8852
8866
  return inserted;
8853
8867
  }
8854
8868
  const realBTNExemplars = [];
8855
- const isRealBTNExemplar = (kve) => {
8856
- if (kve === void 0 || kve === null) return false;
8857
- return !(this.isEntry(kve) && (kve[0] === void 0 || kve[0] === null));
8858
- };
8869
+ let i = 0;
8859
8870
  for (const kve of keysOrNodesOrEntriesOrRaws) {
8860
- if (isRealBTNExemplar(kve)) realBTNExemplars.push(kve);
8871
+ realBTNExemplars.push({ key: kve, value: valuesIterator == null ? void 0 : valuesIterator.next().value, orgIndex: i });
8872
+ i++;
8861
8873
  }
8862
8874
  let sorted = [];
8863
- sorted = realBTNExemplars.sort((a, b) => {
8875
+ sorted = realBTNExemplars.sort(({ key: a }, { key: b }) => {
8864
8876
  let keyA, keyB;
8865
8877
  if (this.isEntry(a)) keyA = a[0];
8866
8878
  else if (this.isRealNode(a)) keyA = a.key;
@@ -8884,8 +8896,8 @@ var dataStructureTyped = (() => {
8884
8896
  const _dfs = (arr) => {
8885
8897
  if (arr.length === 0) return;
8886
8898
  const mid = Math.floor((arr.length - 1) / 2);
8887
- const newNode = this.add(arr[mid]);
8888
- inserted.push(newNode);
8899
+ const { key, value, orgIndex } = arr[mid];
8900
+ inserted[orgIndex] = this.add(key, value);
8889
8901
  _dfs(arr.slice(0, mid));
8890
8902
  _dfs(arr.slice(mid + 1));
8891
8903
  };
@@ -8898,8 +8910,8 @@ var dataStructureTyped = (() => {
8898
8910
  const [l, r] = popped;
8899
8911
  if (l <= r) {
8900
8912
  const m = l + Math.floor((r - l) / 2);
8901
- const newNode = this.add(sorted[m]);
8902
- inserted.push(newNode);
8913
+ const { key, value, orgIndex } = sorted[m];
8914
+ inserted[orgIndex] = this.add(key, value);
8903
8915
  stack.push([m + 1, r]);
8904
8916
  stack.push([l, m - 1]);
8905
8917
  }
@@ -9917,15 +9929,15 @@ var dataStructureTyped = (() => {
9917
9929
  *
9918
9930
  * The function overrides the delete method in a TypeScript class, performs deletion, and then
9919
9931
  * balances the tree if necessary.
9920
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
9932
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw`
9921
9933
  * parameter in the `override delete` method can be one of the following types:
9922
9934
  * @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
9923
9935
  * method from the superclass (presumably a parent class) with the provided `predicate`, which could
9924
9936
  * be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
9925
9937
  * `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
9926
9938
  */
9927
- delete(predicate) {
9928
- const deletedResults = super.delete(predicate);
9939
+ delete(keyOrNodeOrEntryOrRaw) {
9940
+ const deletedResults = super.delete(keyOrNodeOrEntryOrRaw);
9929
9941
  for (const { needBalanced } of deletedResults) {
9930
9942
  if (needBalanced) {
9931
9943
  this._balancePath(needBalanced);
@@ -10394,7 +10406,7 @@ var dataStructureTyped = (() => {
10394
10406
  *
10395
10407
  * The function overrides the delete method in a binary tree data structure to remove a node based on
10396
10408
  * a given predicate and maintain the binary search tree properties.
10397
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
10409
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw`
10398
10410
  * parameter in the `override delete` method is used to specify the condition or key based on which a
10399
10411
  * node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
10400
10412
  * function that determines which node(s) should be deleted.
@@ -10402,12 +10414,13 @@ var dataStructureTyped = (() => {
10402
10414
  * objects. Each object in the array contains information about the deleted node and whether
10403
10415
  * balancing is needed.
10404
10416
  */
10405
- delete(predicate) {
10406
- if (predicate === null) return [];
10417
+ delete(keyOrNodeOrEntryOrRaw) {
10418
+ if (keyOrNodeOrEntryOrRaw === null) return [];
10407
10419
  const results = [];
10408
10420
  let nodeToDelete;
10409
- if (this._isPredicated(predicate)) nodeToDelete = this.getNode(predicate);
10410
- else nodeToDelete = this.isRealNode(predicate) ? predicate : this.getNode(predicate);
10421
+ if (this._isPredicated(keyOrNodeOrEntryOrRaw)) nodeToDelete = this.getNode(keyOrNodeOrEntryOrRaw);
10422
+ else
10423
+ nodeToDelete = this.isRealNode(keyOrNodeOrEntryOrRaw) ? keyOrNodeOrEntryOrRaw : this.getNode(keyOrNodeOrEntryOrRaw);
10411
10424
  if (!nodeToDelete) {
10412
10425
  return results;
10413
10426
  }
@@ -10886,9 +10899,9 @@ var dataStructureTyped = (() => {
10886
10899
  *
10887
10900
  * The function overrides the delete method in a binary tree data structure, handling deletion of
10888
10901
  * nodes and maintaining balance in the tree.
10889
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
10902
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `predicate`
10890
10903
  * parameter in the `delete` method is used to specify the condition for deleting a node from the
10891
- * binary tree. It can be a key, node, entry, or a custom predicate function that determines which
10904
+ * binary tree. It can be a key, node, or entry that determines which
10892
10905
  * node(s) should be deleted.
10893
10906
  * @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
10894
10907
  * boolean flag that determines whether to ignore the count of the node being deleted. If
@@ -10899,11 +10912,11 @@ var dataStructureTyped = (() => {
10899
10912
  * method returns an array of `BinaryTreeDeleteResult` objects, each containing information about the
10900
10913
  * deleted node and whether balancing is needed in the tree.
10901
10914
  */
10902
- delete(predicate, ignoreCount = false) {
10915
+ delete(keyOrNodeOrEntryOrRaw, ignoreCount = false) {
10903
10916
  var _a;
10904
10917
  const deletedResult = [];
10905
10918
  if (!this.root) return deletedResult;
10906
- const curr = (_a = this.getNode(predicate)) != null ? _a : void 0;
10919
+ const curr = (_a = this.getNode(keyOrNodeOrEntryOrRaw)) != null ? _a : void 0;
10907
10920
  if (!curr) return deletedResult;
10908
10921
  const parent = (curr == null ? void 0 : curr.parent) ? curr.parent : void 0;
10909
10922
  let needBalanced = void 0, orgCurrent = curr;
@@ -11238,22 +11251,22 @@ var dataStructureTyped = (() => {
11238
11251
  *
11239
11252
  * The function `delete` in TypeScript overrides the deletion operation in a binary tree data
11240
11253
  * structure, handling cases where nodes have children and maintaining balance in the tree.
11241
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
11254
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `predicate`
11242
11255
  * parameter in the `delete` method is used to specify the condition or key based on which a node
11243
- * should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
11244
- * function.
11256
+ * should be deleted from the binary tree. It can be a key, a node, or an entry.
11245
11257
  * @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
11246
11258
  * boolean flag that determines whether to ignore the count of nodes when performing deletion. If
11247
11259
  * `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If
11248
11260
  * `ignoreCount` is `false
11249
11261
  * @returns The `override delete` method returns an array of `BinaryTreeDeleteResult<NODE>` objects.
11250
11262
  */
11251
- delete(predicate, ignoreCount = false) {
11252
- if (predicate === null) return [];
11263
+ delete(keyOrNodeOrEntryOrRaw, ignoreCount = false) {
11264
+ if (keyOrNodeOrEntryOrRaw === null) return [];
11253
11265
  const results = [];
11254
11266
  let nodeToDelete;
11255
- if (this._isPredicated(predicate)) nodeToDelete = this.getNode(predicate);
11256
- else nodeToDelete = this.isRealNode(predicate) ? predicate : this.getNode(predicate);
11267
+ if (this._isPredicated(keyOrNodeOrEntryOrRaw)) nodeToDelete = this.getNode(keyOrNodeOrEntryOrRaw);
11268
+ else
11269
+ nodeToDelete = this.isRealNode(keyOrNodeOrEntryOrRaw) ? keyOrNodeOrEntryOrRaw : this.getNode(keyOrNodeOrEntryOrRaw);
11257
11270
  if (!nodeToDelete) {
11258
11271
  return results;
11259
11272
  }