data-structure-typed 1.48.5 → 1.48.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 (63) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +109 -59
  3. package/README_zh-CN.md +1028 -0
  4. package/benchmark/report.html +16 -16
  5. package/benchmark/report.json +204 -174
  6. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +7 -16
  7. package/dist/cjs/data-structures/binary-tree/binary-tree.js +24 -19
  8. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  9. package/dist/cjs/data-structures/binary-tree/bst.d.ts +35 -11
  10. package/dist/cjs/data-structures/binary-tree/bst.js +58 -39
  11. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  12. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  13. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +4 -4
  14. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +4 -4
  15. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  16. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +4 -4
  17. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +4 -4
  18. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  19. package/dist/cjs/data-structures/queue/deque.d.ts +9 -9
  20. package/dist/cjs/data-structures/queue/deque.js +9 -9
  21. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  22. package/dist/cjs/interfaces/binary-tree.d.ts +1 -1
  23. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +7 -16
  24. package/dist/mjs/data-structures/binary-tree/binary-tree.js +24 -19
  25. package/dist/mjs/data-structures/binary-tree/bst.d.ts +35 -11
  26. package/dist/mjs/data-structures/binary-tree/bst.js +58 -39
  27. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +4 -4
  28. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +4 -4
  29. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +4 -4
  30. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +4 -4
  31. package/dist/mjs/data-structures/queue/deque.d.ts +9 -9
  32. package/dist/mjs/data-structures/queue/deque.js +9 -9
  33. package/dist/mjs/interfaces/binary-tree.d.ts +1 -1
  34. package/dist/umd/data-structure-typed.js +99 -78
  35. package/dist/umd/data-structure-typed.min.js +2 -2
  36. package/dist/umd/data-structure-typed.min.js.map +1 -1
  37. package/package.json +2 -2
  38. package/src/data-structures/binary-tree/avl-tree.ts +1 -1
  39. package/src/data-structures/binary-tree/binary-tree.ts +31 -20
  40. package/src/data-structures/binary-tree/bst.ts +73 -46
  41. package/src/data-structures/binary-tree/rb-tree.ts +1 -1
  42. package/src/data-structures/binary-tree/tree-multimap.ts +1 -1
  43. package/src/data-structures/hash/hash-map.ts +1 -1
  44. package/src/data-structures/linked-list/doubly-linked-list.ts +4 -4
  45. package/src/data-structures/linked-list/singly-linked-list.ts +4 -4
  46. package/src/data-structures/queue/deque.ts +10 -10
  47. package/src/data-structures/queue/queue.ts +1 -1
  48. package/src/interfaces/binary-tree.ts +1 -1
  49. package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
  50. package/src/types/data-structures/heap/heap.ts +1 -1
  51. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
  52. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +1 -1
  53. package/test/performance/data-structures/hash/hash-map.test.ts +8 -8
  54. package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
  55. package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +12 -1
  56. package/test/performance/data-structures/priority-queue/priority-queue.test.ts +1 -1
  57. package/test/performance/data-structures/queue/deque.test.ts +27 -15
  58. package/test/performance/data-structures/queue/queue.test.ts +27 -4
  59. package/test/performance/data-structures/stack/stack.test.ts +2 -2
  60. package/test/unit/data-structures/binary-tree/bst.test.ts +29 -29
  61. package/test/unit/data-structures/binary-tree/overall.test.ts +1 -1
  62. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +2 -2
  63. package/test/unit/data-structures/queue/deque.test.ts +15 -15
@@ -1539,12 +1539,12 @@ var dataStructureTyped = (() => {
1539
1539
  * Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element.
1540
1540
  * Space Complexity: O(1) - Constant space.
1541
1541
  *
1542
- * The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
1542
+ * The `pollLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
1543
1543
  * pointers accordingly.
1544
1544
  * @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
1545
1545
  * the linked list is empty, it returns `undefined`.
1546
1546
  */
1547
- popLast() {
1547
+ pollLast() {
1548
1548
  return this.pop();
1549
1549
  }
1550
1550
  /**
@@ -1574,10 +1574,10 @@ var dataStructureTyped = (() => {
1574
1574
  * Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
1575
1575
  * Space Complexity: O(1) - Constant space.
1576
1576
  *
1577
- * The `popFirst()` function removes and returns the value of the first node in a linked list.
1577
+ * The `pollFirst()` function removes and returns the value of the first node in a linked list.
1578
1578
  * @returns The value of the node that is being removed from the beginning of the linked list.
1579
1579
  */
1580
- popFirst() {
1580
+ pollFirst() {
1581
1581
  return this.shift();
1582
1582
  }
1583
1583
  /**
@@ -2212,11 +2212,11 @@ var dataStructureTyped = (() => {
2212
2212
  * Time Complexity: O(1)
2213
2213
  * Space Complexity: O(1)
2214
2214
  *
2215
- * The `popLast()` function removes and returns the value of the last node in a doubly linked list.
2215
+ * The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
2216
2216
  * @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
2217
2217
  * list is empty, it returns undefined.
2218
2218
  */
2219
- popLast() {
2219
+ pollLast() {
2220
2220
  return this.pop();
2221
2221
  }
2222
2222
  /**
@@ -2253,11 +2253,11 @@ var dataStructureTyped = (() => {
2253
2253
  * Time Complexity: O(1)
2254
2254
  * Space Complexity: O(1)
2255
2255
  *
2256
- * The `popFirst()` function removes and returns the value of the first node in a doubly linked list.
2256
+ * The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
2257
2257
  * @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
2258
2258
  * list.
2259
2259
  */
2260
- popFirst() {
2260
+ pollFirst() {
2261
2261
  return this.shift();
2262
2262
  }
2263
2263
  /**
@@ -3692,10 +3692,10 @@ var dataStructureTyped = (() => {
3692
3692
  * Time Complexity: O(1) - Removes the last element.
3693
3693
  * Space Complexity: O(1) - Operates in-place.
3694
3694
  *
3695
- * The function "popLast" removes and returns the last element of an array.
3695
+ * The function "pollLast" removes and returns the last element of an array.
3696
3696
  * @returns The last element of the array is being returned.
3697
3697
  */
3698
- popLast() {
3698
+ pollLast() {
3699
3699
  return this.pop();
3700
3700
  }
3701
3701
  /**
@@ -3713,11 +3713,11 @@ var dataStructureTyped = (() => {
3713
3713
  * Time Complexity: O(1) - Removes the first element.
3714
3714
  * Space Complexity: O(1) - In-place operation.
3715
3715
  *
3716
- * The function "popFirst" removes and returns the first element of an array.
3717
- * @returns The method `popFirst()` is returning the first element of the array after removing it
3716
+ * The function "pollFirst" removes and returns the first element of an array.
3717
+ * @returns The method `pollFirst()` is returning the first element of the array after removing it
3718
3718
  * from the beginning. If the array is empty, it will return `undefined`.
3719
3719
  */
3720
- popFirst() {
3720
+ pollFirst() {
3721
3721
  return this.shift();
3722
3722
  }
3723
3723
  /**
@@ -4452,10 +4452,10 @@ var dataStructureTyped = (() => {
4452
4452
  * Time Complexity: O(1)
4453
4453
  * Space Complexity: O(1)
4454
4454
  *
4455
- * The function `popFirst()` removes and returns the first element in a data structure.
4455
+ * The function `pollFirst()` removes and returns the first element in a data structure.
4456
4456
  * @returns The element of the first element in the data structure.
4457
4457
  */
4458
- popFirst() {
4458
+ pollFirst() {
4459
4459
  if (!this.size)
4460
4460
  return;
4461
4461
  const element = this.getFirst();
@@ -4487,10 +4487,10 @@ var dataStructureTyped = (() => {
4487
4487
  * Time Complexity: O(1)
4488
4488
  * Space Complexity: O(1)
4489
4489
  *
4490
- * The `popLast()` function removes and returns the last element in a data structure.
4490
+ * The `pollLast()` function removes and returns the last element in a data structure.
4491
4491
  * @returns The element that was removed from the data structure.
4492
4492
  */
4493
- popLast() {
4493
+ pollLast() {
4494
4494
  if (!this.size)
4495
4495
  return;
4496
4496
  const element = this.getLast();
@@ -7648,7 +7648,7 @@ var dataStructureTyped = (() => {
7648
7648
  /**
7649
7649
  * Time Complexity O(log n) - O(n)
7650
7650
  * Space Complexity O(1)
7651
- *
7651
+ *
7652
7652
  * The `add` function adds a new node to a binary tree, either by creating a new node or replacing an
7653
7653
  * existing node with the same key.
7654
7654
  * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
@@ -7660,6 +7660,8 @@ var dataStructureTyped = (() => {
7660
7660
  const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
7661
7661
  if (newNode === void 0)
7662
7662
  return;
7663
+ if (newNode !== null && this.has(newNode.key))
7664
+ return void 0;
7663
7665
  const _bfs = (root, newNode2) => {
7664
7666
  const queue = new Queue([root]);
7665
7667
  while (queue.size > 0) {
@@ -7699,17 +7701,27 @@ var dataStructureTyped = (() => {
7699
7701
  * Time Complexity: O(k log n) - O(k * n)
7700
7702
  * Space Complexity: O(1)
7701
7703
  *
7702
- * The function `addMany` takes in an iterable of `BTNodeExemplar` objects, adds each object to the
7703
- * current instance, and returns an array of the inserted nodes.
7704
- * @param nodes - The `nodes` parameter is an iterable (such as an array or a set) of
7705
- * `BTNodeExemplar<K, V,N>` objects.
7706
- * @returns The function `addMany` returns an array of values, where each value is either of type
7707
- * `N`, `null`, or `undefined`.
7704
+ * The `addMany` function takes in a collection of nodes and an optional collection of values, and
7705
+ * adds each node with its corresponding value to the data structure.
7706
+ * @param nodes - An iterable collection of BTNodeExemplar objects.
7707
+ * @param [values] - An optional iterable of values that will be assigned to each node being added.
7708
+ * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
7708
7709
  */
7709
- addMany(nodes) {
7710
+ addMany(nodes, values) {
7710
7711
  const inserted = [];
7712
+ let valuesIterator;
7713
+ if (values) {
7714
+ valuesIterator = values[Symbol.iterator]();
7715
+ }
7711
7716
  for (const kne of nodes) {
7712
- inserted.push(this.add(kne));
7717
+ let value = void 0;
7718
+ if (valuesIterator) {
7719
+ const valueResult = valuesIterator.next();
7720
+ if (!valueResult.done) {
7721
+ value = valueResult.value;
7722
+ }
7723
+ }
7724
+ inserted.push(this.add(kne, value));
7713
7725
  }
7714
7726
  return inserted;
7715
7727
  }
@@ -7717,17 +7729,9 @@ var dataStructureTyped = (() => {
7717
7729
  * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
7718
7730
  * Space Complexity: O(1)
7719
7731
  */
7720
- /**
7721
- * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
7722
- * Space Complexity: O(1)
7723
- *
7724
- * The `refill` function clears the current collection and adds new nodes, keys, or entries to it.
7725
- * @param nodesOrKeysOrEntries - The parameter `nodesOrKeysOrEntries` is an iterable object that can
7726
- * contain either `BTNodeExemplar` objects, keys, or entries.
7727
- */
7728
- refill(nodesOrKeysOrEntries) {
7732
+ refill(nodesOrKeysOrEntries, values) {
7729
7733
  this.clear();
7730
- this.addMany(nodesOrKeysOrEntries);
7734
+ this.addMany(nodesOrKeysOrEntries, values);
7731
7735
  }
7732
7736
  /**
7733
7737
  * Time Complexity: O(n)
@@ -9236,7 +9240,7 @@ var dataStructureTyped = (() => {
9236
9240
  /**
9237
9241
  * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
9238
9242
  * Space Complexity: O(1) - Constant space is used.
9239
- *
9243
+ *
9240
9244
  * The `add` function adds a new node to a binary tree, updating the value if the key already exists
9241
9245
  * or inserting a new node if the key is unique.
9242
9246
  * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can accept three types of values:
@@ -9287,23 +9291,32 @@ var dataStructureTyped = (() => {
9287
9291
  * Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
9288
9292
  * Space Complexity: O(k) - Additional space is required for the sorted array.
9289
9293
  *
9290
- * The `addMany` function in TypeScript adds multiple nodes to a binary tree, either in a balanced or
9291
- * unbalanced manner, and returns an array of the inserted nodes.
9292
- * @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to the
9293
- * binary tree.
9294
- * @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
9295
- * adding the nodes. The default value is true.
9294
+ * The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
9295
+ * balancing the tree after each addition.
9296
+ * @param keysOrNodesOrEntries - An iterable containing the keys, nodes, or entries to be added to
9297
+ * the binary tree.
9298
+ * @param [values] - An optional iterable of values to be associated with the keys or nodes being
9299
+ * added. If provided, the values will be assigned to the corresponding keys or nodes in the same
9300
+ * order. If not provided, undefined will be assigned as the value for each key or node.
9301
+ * @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
9302
+ * balanced or not. If set to true, the add operation will be balanced using a binary search tree
9303
+ * algorithm. If set to false, the add operation will not be balanced and the elements will be added
9304
+ * in the order they appear in the input.
9296
9305
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
9297
- * type of iteration to use when adding multiple keys or nodes to the binary tree. It has a default
9298
- * value of `this.iterationType`, which means it will use the iteration type specified by the binary
9299
- * tree instance.
9300
- * @returns The `addMany` function returns an array of `N` or `undefined` values.
9306
+ * type of iteration to use when adding multiple keys or nodes. It has a default value of
9307
+ * `this.iterationType`, which suggests that it is a property of the current object.
9308
+ * @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
9301
9309
  */
9302
- addMany(keysOrNodesOrEntries, isBalanceAdd = true, iterationType = this.iterationType) {
9310
+ addMany(keysOrNodesOrEntries, values, isBalanceAdd = true, iterationType = this.iterationType) {
9303
9311
  const inserted = [];
9312
+ let valuesIterator;
9313
+ if (values) {
9314
+ valuesIterator = values[Symbol.iterator]();
9315
+ }
9304
9316
  if (!isBalanceAdd) {
9305
9317
  for (const kve of keysOrNodesOrEntries) {
9306
- const nn = this.add(kve);
9318
+ const value = valuesIterator == null ? void 0 : valuesIterator.next().value;
9319
+ const nn = this.add(kve, value);
9307
9320
  inserted.push(nn);
9308
9321
  }
9309
9322
  return inserted;
@@ -9367,31 +9380,40 @@ var dataStructureTyped = (() => {
9367
9380
  }
9368
9381
  return inserted;
9369
9382
  }
9370
- // /**
9371
- // * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
9372
- // * Space Complexity: O(n) - Additional space is required for the sorted array.
9373
- // */
9374
- //
9375
- // /**
9376
- // * Time Complexity: O(log n) - Average case for a balanced tree.
9377
- // * Space Complexity: O(1) - Constant space is used.
9378
- // *
9379
- // * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
9380
- // * leftmost node if the comparison result is greater than.
9381
- // * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
9382
- // * type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
9383
- // * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
9384
- // * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
9385
- // * be performed. It can have one of the following values:
9386
- // * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
9387
- // * the key of the leftmost node if the comparison result is greater than, and the key of the
9388
- // * rightmost node otherwise. If no node is found, it returns 0.
9389
- // */
9390
- // lastKey(beginRoot: BSTNodeKeyOrNode<K,N> = this.root, iterationType = this.iterationType): K {
9391
- // if (this._compare(0, 1) === CP.lt) return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
9392
- // else if (this._compare(0, 1) === CP.gt) return this.getLeftMost(beginRoot, iterationType)?.key ?? 0;
9393
- // else return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
9394
- // }
9383
+ /**
9384
+ * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
9385
+ * Space Complexity: O(n) - Additional space is required for the sorted array.
9386
+ */
9387
+ /**
9388
+ * Time Complexity: O(log n) - Average case for a balanced tree.
9389
+ * Space Complexity: O(1) - Constant space is used.
9390
+ *
9391
+ * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
9392
+ * leftmost node if the comparison result is greater than.
9393
+ * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
9394
+ * type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
9395
+ * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
9396
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
9397
+ * be performed. It can have one of the following values:
9398
+ * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
9399
+ * the key of the leftmost node if the comparison result is greater than, and the key of the
9400
+ * rightmost node otherwise. If no node is found, it returns 0.
9401
+ */
9402
+ lastKey(beginRoot = this.root) {
9403
+ let current = this.ensureNode(beginRoot);
9404
+ if (!current)
9405
+ return void 0;
9406
+ if (this._variant === "MIN" /* MIN */) {
9407
+ while (current.right !== void 0) {
9408
+ current = current.right;
9409
+ }
9410
+ } else {
9411
+ while (current.left !== void 0) {
9412
+ current = current.left;
9413
+ }
9414
+ }
9415
+ return current.key;
9416
+ }
9395
9417
  /**
9396
9418
  * Time Complexity: O(log n) - Average case for a balanced tree.
9397
9419
  * Space Complexity: O(1) - Constant space is used.
@@ -9635,7 +9657,6 @@ var dataStructureTyped = (() => {
9635
9657
  if (l <= r) {
9636
9658
  const m = l + Math.floor((r - l) / 2);
9637
9659
  const midNode = sorted[m];
9638
- debugger;
9639
9660
  this.add([midNode.key, midNode.value]);
9640
9661
  stack.push([m + 1, r]);
9641
9662
  stack.push([l, m - 1]);
@@ -10221,7 +10242,7 @@ var dataStructureTyped = (() => {
10221
10242
  /**
10222
10243
  * 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.
10223
10244
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
10224
- *
10245
+ *
10225
10246
  * The function overrides the add method of a binary tree node and balances the tree after inserting
10226
10247
  * a new node.
10227
10248
  * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
@@ -10680,7 +10701,7 @@ var dataStructureTyped = (() => {
10680
10701
  /**
10681
10702
  * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
10682
10703
  * Space Complexity: O(1)
10683
- *
10704
+ *
10684
10705
  * The `add` function adds a new node to a binary search tree and performs necessary rotations and
10685
10706
  * color changes to maintain the red-black tree properties.
10686
10707
  * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
@@ -11227,7 +11248,7 @@ var dataStructureTyped = (() => {
11227
11248
  /**
11228
11249
  * 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.
11229
11250
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
11230
- *
11251
+ *
11231
11252
  * The function overrides the add method of a binary tree node and adds a new node to the tree.
11232
11253
  * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
11233
11254
  * entry. It represents the key, node, or entry that you want to add to the binary tree.