data-structure-typed 1.50.4 → 1.50.6

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 (82) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +116 -55
  3. package/SPECIFICATION.md +2 -2
  4. package/SPECIFICATION_zh-CN.md +81 -0
  5. package/{SPONSOR-zh-CN.md → SPONSOR_zh-CN.md} +1 -1
  6. package/benchmark/report.html +24 -24
  7. package/benchmark/report.json +261 -237
  8. package/dist/cjs/data-structures/base/iterable-base.d.ts +10 -8
  9. package/dist/cjs/data-structures/base/iterable-base.js +8 -12
  10. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/binary-tree.js +19 -19
  12. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  13. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +158 -135
  14. package/dist/cjs/data-structures/binary-tree/rb-tree.js +415 -386
  15. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  16. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +1 -0
  17. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +84 -76
  18. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  19. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -0
  20. package/dist/cjs/data-structures/graph/abstract-graph.js +3 -0
  21. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  22. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  23. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +14 -76
  24. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +16 -86
  25. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  26. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +27 -69
  27. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +35 -79
  28. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  29. package/dist/cjs/data-structures/queue/deque.d.ts +0 -53
  30. package/dist/cjs/data-structures/queue/deque.js +0 -61
  31. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  32. package/dist/cjs/data-structures/queue/queue.d.ts +0 -70
  33. package/dist/cjs/data-structures/queue/queue.js +0 -87
  34. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  35. package/dist/mjs/data-structures/base/iterable-base.d.ts +10 -8
  36. package/dist/mjs/data-structures/base/iterable-base.js +8 -12
  37. package/dist/mjs/data-structures/binary-tree/binary-tree.js +19 -19
  38. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +158 -135
  39. package/dist/mjs/data-structures/binary-tree/rb-tree.js +412 -386
  40. package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +1 -0
  41. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +84 -76
  42. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -0
  43. package/dist/mjs/data-structures/graph/abstract-graph.js +3 -0
  44. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +14 -76
  45. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +16 -86
  46. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +27 -69
  47. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +33 -79
  48. package/dist/mjs/data-structures/queue/deque.d.ts +0 -53
  49. package/dist/mjs/data-structures/queue/deque.js +0 -61
  50. package/dist/mjs/data-structures/queue/queue.d.ts +0 -70
  51. package/dist/mjs/data-structures/queue/queue.js +0 -86
  52. package/dist/umd/data-structure-typed.js +539 -756
  53. package/dist/umd/data-structure-typed.min.js +2 -2
  54. package/dist/umd/data-structure-typed.min.js.map +1 -1
  55. package/package.json +1 -1
  56. package/src/data-structures/base/iterable-base.ts +14 -10
  57. package/src/data-structures/binary-tree/binary-tree.ts +19 -19
  58. package/src/data-structures/binary-tree/rb-tree.ts +437 -395
  59. package/src/data-structures/binary-tree/tree-multi-map.ts +85 -82
  60. package/src/data-structures/graph/abstract-graph.ts +4 -0
  61. package/src/data-structures/heap/heap.ts +1 -1
  62. package/src/data-structures/linked-list/doubly-linked-list.ts +16 -94
  63. package/src/data-structures/linked-list/singly-linked-list.ts +35 -87
  64. package/src/data-structures/queue/deque.ts +0 -67
  65. package/src/data-structures/queue/queue.ts +0 -98
  66. package/test/performance/data-structures/binary-tree/avl-tree.test.ts +3 -3
  67. package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +3 -3
  68. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +26 -16
  69. package/test/performance/data-structures/hash/hash-map.test.ts +6 -6
  70. package/test/performance/data-structures/heap/heap.test.ts +14 -14
  71. package/test/performance/data-structures/priority-queue/priority-queue.test.ts +11 -6
  72. package/test/performance/data-structures/queue/deque.test.ts +8 -8
  73. package/test/performance/data-structures/queue/queue.test.ts +5 -12
  74. package/test/performance/reportor.ts +43 -1
  75. package/test/unit/data-structures/binary-tree/overall.test.ts +23 -21
  76. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +168 -105
  77. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +311 -192
  78. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +6 -6
  79. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +10 -10
  80. package/test/unit/data-structures/linked-list/skip-list.test.ts +4 -4
  81. package/test/unit/data-structures/queue/deque.test.ts +26 -26
  82. package/test/unit/data-structures/queue/queue.test.ts +20 -20
@@ -178,10 +178,6 @@ var dataStructureTyped = (() => {
178
178
 
179
179
  // src/data-structures/base/iterable-base.ts
180
180
  var IterableEntryBase = class {
181
- /**
182
- * Time Complexity: O(n)
183
- * Space Complexity: O(1)
184
- */
185
181
  /**
186
182
  * Time Complexity: O(n)
187
183
  * Space Complexity: O(1)
@@ -299,6 +295,10 @@ var dataStructureTyped = (() => {
299
295
  * Time Complexity: O(n)
300
296
  * Space Complexity: O(1)
301
297
  */
298
+ /**
299
+ * Time Complexity: O(n)
300
+ * Space Complexity: O(1)
301
+ */
302
302
  /**
303
303
  * Time Complexity: O(n)
304
304
  * Space Complexity: O(1)
@@ -413,10 +413,6 @@ var dataStructureTyped = (() => {
413
413
  }
414
414
  return;
415
415
  }
416
- /**
417
- * Time Complexity: O(n)
418
- * Space Complexity: O(1)
419
- */
420
416
  /**
421
417
  * Time Complexity: O(n)
422
418
  * Space Complexity: O(1)
@@ -513,6 +509,10 @@ var dataStructureTyped = (() => {
513
509
  * Time Complexity: O(n)
514
510
  * Space Complexity: O(1)
515
511
  */
512
+ /**
513
+ * Time Complexity: O(n)
514
+ * Space Complexity: O(1)
515
+ */
516
516
  /**
517
517
  * Time Complexity: O(n)
518
518
  * Space Complexity: O(1)
@@ -587,10 +587,6 @@ var dataStructureTyped = (() => {
587
587
  }
588
588
  return;
589
589
  }
590
- /**
591
- * Time Complexity: O(n)
592
- * Space Complexity: O(1)
593
- */
594
590
  /**
595
591
  * Time Complexity: O(n)
596
592
  * Space Complexity: O(1)
@@ -1663,6 +1659,24 @@ var dataStructureTyped = (() => {
1663
1659
  get tail() {
1664
1660
  return this._tail;
1665
1661
  }
1662
+ /**
1663
+ * The above function returns the value of the first element in a linked list, or undefined if the
1664
+ * list is empty.
1665
+ * @returns The value of the first node in the linked list, or undefined if the linked list is empty.
1666
+ */
1667
+ get first() {
1668
+ var _a;
1669
+ return (_a = this.head) == null ? void 0 : _a.value;
1670
+ }
1671
+ /**
1672
+ * The function returns the value of the last element in a linked list, or undefined if the list is
1673
+ * empty.
1674
+ * @returns The value of the last node in the linked list, or undefined if the linked list is empty.
1675
+ */
1676
+ get last() {
1677
+ var _a;
1678
+ return (_a = this.tail) == null ? void 0 : _a.value;
1679
+ }
1666
1680
  /**
1667
1681
  * The function returns the size of an object.
1668
1682
  * @returns The size of the object, which is a number.
@@ -1695,19 +1709,18 @@ var dataStructureTyped = (() => {
1695
1709
  /**
1696
1710
  * Time Complexity: O(1)
1697
1711
  * Space Complexity: O(1)
1698
- * Constant time, as it involves basic pointer adjustments.
1699
- * Constant space, as it only creates a new node.
1700
1712
  */
1701
1713
  /**
1702
1714
  * Time Complexity: O(1)
1703
1715
  * Space Complexity: O(1)
1704
1716
  *
1705
- * The `push` function adds a new node with the given value to the end of a singly linked list.
1706
- * @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
1707
- * any type (E) as specified in the generic type declaration of the class or function.
1717
+ * The push function adds a new element to the end of a singly linked list.
1718
+ * @param {E} element - The "element" parameter represents the value of the element that you want to
1719
+ * add to the linked list.
1720
+ * @returns The `push` method is returning a boolean value, `true`.
1708
1721
  */
1709
- push(value) {
1710
- const newNode = new SinglyLinkedListNode(value);
1722
+ push(element) {
1723
+ const newNode = new SinglyLinkedListNode(element);
1711
1724
  if (!this.head) {
1712
1725
  this._head = newNode;
1713
1726
  this._tail = newNode;
@@ -1718,21 +1731,6 @@ var dataStructureTyped = (() => {
1718
1731
  this._size++;
1719
1732
  return true;
1720
1733
  }
1721
- /**
1722
- * Time Complexity: O(1)
1723
- * Space Complexity: O(1)
1724
- */
1725
- /**
1726
- * Time Complexity: O(1)
1727
- * Space Complexity: O(1)
1728
- *
1729
- * The `push` function adds a new node with the given value to the end of a singly linked list.
1730
- * @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
1731
- * any type (E) as specified in the generic type declaration of the class or function.
1732
- */
1733
- addLast(value) {
1734
- return this.push(value);
1735
- }
1736
1734
  /**
1737
1735
  * Time Complexity: O(n)
1738
1736
  * Space Complexity: O(1)
@@ -1742,10 +1740,9 @@ var dataStructureTyped = (() => {
1742
1740
  * Time Complexity: O(n)
1743
1741
  * Space Complexity: O(1)
1744
1742
  *
1745
- * The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
1746
- * pointers accordingly.
1747
- * @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
1748
- * the linked list is empty, it returns `undefined`.
1743
+ * The `pop` function removes and returns the value of the last element in a linked list.
1744
+ * @returns The method is returning the value of the element that is being popped from the end of the
1745
+ * list.
1749
1746
  */
1750
1747
  pop() {
1751
1748
  if (!this.head)
@@ -1767,22 +1764,6 @@ var dataStructureTyped = (() => {
1767
1764
  this._size--;
1768
1765
  return value;
1769
1766
  }
1770
- /**
1771
- * Time Complexity: O(n)
1772
- * Space Complexity: O(1)
1773
- */
1774
- /**
1775
- * Time Complexity: O(n)
1776
- * Space Complexity: O(1)
1777
- *
1778
- * The `pollLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
1779
- * pointers accordingly.
1780
- * @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
1781
- * the linked list is empty, it returns `undefined`.
1782
- */
1783
- pollLast() {
1784
- return this.pop();
1785
- }
1786
1767
  /**
1787
1768
  * Time Complexity: O(1)
1788
1769
  * Space Complexity: O(1)
@@ -1791,8 +1772,8 @@ var dataStructureTyped = (() => {
1791
1772
  * Time Complexity: O(1)
1792
1773
  * Space Complexity: O(1)
1793
1774
  *
1794
- * The `shift()` function removes and returns the value of the first node in a linked list.
1795
- * @returns The value of the node that is being removed from the beginning of the linked list.
1775
+ * The `shift()` function removes and returns the value of the first element in a linked list.
1776
+ * @returns The value of the removed node.
1796
1777
  */
1797
1778
  shift() {
1798
1779
  if (!this.head)
@@ -1810,26 +1791,13 @@ var dataStructureTyped = (() => {
1810
1791
  * Time Complexity: O(1)
1811
1792
  * Space Complexity: O(1)
1812
1793
  *
1813
- * The `pollFirst()` function removes and returns the value of the first node in a linked list.
1814
- * @returns The value of the node that is being removed from the beginning of the linked list.
1794
+ * The unshift function adds a new element to the beginning of a singly linked list.
1795
+ * @param {E} element - The "element" parameter represents the value of the element that you want to
1796
+ * add to the beginning of the singly linked list.
1797
+ * @returns The `unshift` method is returning a boolean value, `true`.
1815
1798
  */
1816
- pollFirst() {
1817
- return this.shift();
1818
- }
1819
- /**
1820
- * Time Complexity: O(1)
1821
- * Space Complexity: O(1)
1822
- */
1823
- /**
1824
- * Time Complexity: O(1)
1825
- * Space Complexity: O(1)
1826
- *
1827
- * The unshift function adds a new node with the given value to the beginning of a singly linked list.
1828
- * @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
1829
- * linked list.
1830
- */
1831
- unshift(value) {
1832
- const newNode = new SinglyLinkedListNode(value);
1799
+ unshift(element) {
1800
+ const newNode = new SinglyLinkedListNode(element);
1833
1801
  if (!this.head) {
1834
1802
  this._head = newNode;
1835
1803
  this._tail = newNode;
@@ -1840,25 +1808,9 @@ var dataStructureTyped = (() => {
1840
1808
  this._size++;
1841
1809
  return true;
1842
1810
  }
1843
- /**
1844
- * Time Complexity: O(1)
1845
- * Space Complexity: O(1)
1846
- */
1847
- /**
1848
- * Time Complexity: O(1)
1849
- * Space Complexity: O(1)
1850
- *
1851
- * The addFirst function adds a new node with the given value to the beginning of a singly linked list.
1852
- * @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
1853
- * linked list.
1854
- */
1855
- addFirst(value) {
1856
- return this.unshift(value);
1857
- }
1858
1811
  /**
1859
1812
  * Time Complexity: O(n)
1860
1813
  * Space Complexity: O(1)
1861
- * Linear time, where n is the index, as it may need to traverse the list to find the desired node.
1862
1814
  */
1863
1815
  /**
1864
1816
  * Time Complexity: O(n)
@@ -2467,14 +2419,13 @@ var dataStructureTyped = (() => {
2467
2419
  * Space Complexity: O(1)
2468
2420
  */
2469
2421
  /**
2470
- * Time Complexity: O(1)
2471
- * Space Complexity: O(1)
2472
- *
2473
- * The push function adds a new node with the given value to the end of the doubly linked list.
2474
- * @param {E} value - The value to be added to the linked list.
2422
+ * The push function adds a new element to the end of a doubly linked list.
2423
+ * @param {E} element - The "element" parameter represents the value that you want to add to the
2424
+ * doubly linked list.
2425
+ * @returns The `push` method is returning a boolean value, `true`.
2475
2426
  */
2476
- push(value) {
2477
- const newNode = new DoublyLinkedListNode(value);
2427
+ push(element) {
2428
+ const newNode = new DoublyLinkedListNode(element);
2478
2429
  if (!this.head) {
2479
2430
  this._head = newNode;
2480
2431
  this._tail = newNode;
@@ -2491,12 +2442,8 @@ var dataStructureTyped = (() => {
2491
2442
  * Space Complexity: O(1)
2492
2443
  */
2493
2444
  /**
2494
- * Time Complexity: O(1)
2495
- * Space Complexity: O(1)
2496
- *
2497
- * The `pop()` function removes and returns the value of the last node in a doubly linked list.
2498
- * @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
2499
- * list is empty, it returns undefined.
2445
+ * The `pop()` function removes and returns the value of the last element in a linked list.
2446
+ * @returns The method is returning the value of the removed node.
2500
2447
  */
2501
2448
  pop() {
2502
2449
  if (!this.tail)
@@ -2517,12 +2464,8 @@ var dataStructureTyped = (() => {
2517
2464
  * Space Complexity: O(1)
2518
2465
  */
2519
2466
  /**
2520
- * Time Complexity: O(1)
2521
- * Space Complexity: O(1)
2522
- *
2523
- * The `shift()` function removes and returns the value of the first node in a doubly linked list.
2524
- * @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
2525
- * list.
2467
+ * The `shift()` function removes and returns the value of the first element in a doubly linked list.
2468
+ * @returns The value of the removed node.
2526
2469
  */
2527
2470
  shift() {
2528
2471
  if (!this.head)
@@ -2543,15 +2486,13 @@ var dataStructureTyped = (() => {
2543
2486
  * Space Complexity: O(1)
2544
2487
  */
2545
2488
  /**
2546
- * Time Complexity: O(1)
2547
- * Space Complexity: O(1)
2548
- *
2549
- * The unshift function adds a new node with the given value to the beginning of a doubly linked list.
2550
- * @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
2551
- * doubly linked list.
2489
+ * The unshift function adds a new element to the beginning of a doubly linked list.
2490
+ * @param {E} element - The "element" parameter represents the value of the element that you want to
2491
+ * add to the beginning of the doubly linked list.
2492
+ * @returns The `unshift` method is returning a boolean value, `true`.
2552
2493
  */
2553
- unshift(value) {
2554
- const newNode = new DoublyLinkedListNode(value);
2494
+ unshift(element) {
2495
+ const newNode = new DoublyLinkedListNode(element);
2555
2496
  if (!this.head) {
2556
2497
  this._head = newNode;
2557
2498
  this._tail = newNode;
@@ -3035,65 +2976,6 @@ var dataStructureTyped = (() => {
3035
2976
  }
3036
2977
  return mappedList;
3037
2978
  }
3038
- /**
3039
- * Time Complexity: O(1)
3040
- * Space Complexity: O(1)
3041
- */
3042
- /**
3043
- * Time Complexity: O(1)
3044
- * Space Complexity: O(1)
3045
- *
3046
- * The addLast function adds a new node with the given value to the end of the doubly linked list.
3047
- * @param {E} value - The value to be added to the linked list.
3048
- */
3049
- addLast(value) {
3050
- return this.push(value);
3051
- }
3052
- /**
3053
- * Time Complexity: O(1)
3054
- * Space Complexity: O(1)
3055
- */
3056
- /**
3057
- * Time Complexity: O(1)
3058
- * Space Complexity: O(1)
3059
- *
3060
- * The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
3061
- * @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
3062
- * list is empty, it returns undefined.
3063
- */
3064
- pollLast() {
3065
- return this.pop();
3066
- }
3067
- /**
3068
- * Time Complexity: O(1)
3069
- * Space Complexity: O(1)
3070
- */
3071
- /**
3072
- * Time Complexity: O(1)
3073
- * Space Complexity: O(1)
3074
- *
3075
- * The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
3076
- * @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
3077
- * list.
3078
- */
3079
- pollFirst() {
3080
- return this.shift();
3081
- }
3082
- /**
3083
- * Time Complexity: O(1)
3084
- * Space Complexity: O(1)
3085
- */
3086
- /**
3087
- * Time Complexity: O(1)
3088
- * Space Complexity: O(1)
3089
- *
3090
- * The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
3091
- * @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
3092
- * doubly linked list.
3093
- */
3094
- addFirst(value) {
3095
- this.unshift(value);
3096
- }
3097
2979
  /**
3098
2980
  * The function returns an iterator that iterates over the values of a linked list.
3099
2981
  */
@@ -3760,64 +3642,6 @@ var dataStructureTyped = (() => {
3760
3642
  const spliced = this.elements.splice(index, 1);
3761
3643
  return spliced.length === 1;
3762
3644
  }
3763
- /**
3764
- * Time Complexity: O(1)
3765
- * Space Complexity: O(1)
3766
- */
3767
- /**
3768
- * Time Complexity: O(1)
3769
- * Space Complexity: O(1)
3770
- *
3771
- * The `peek` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
3772
- * @returns The `peek()` method returns the first element of the data structure, represented by the `_elements` array at
3773
- * the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
3774
- */
3775
- peek() {
3776
- return this.first;
3777
- }
3778
- /**
3779
- * Time Complexity: O(1)
3780
- * Space Complexity: O(1)
3781
- */
3782
- /**
3783
- * Time Complexity: O(1)
3784
- * Space Complexity: O(1)
3785
- *
3786
- * The `peekLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
3787
- * @returns The method `peekLast()` returns the last element of the `_elements` array if the array is not empty. If the
3788
- * array is empty, it returns `undefined`.
3789
- */
3790
- peekLast() {
3791
- return this.last;
3792
- }
3793
- /**
3794
- * Time Complexity: O(1)
3795
- * Space Complexity: O(1)
3796
- */
3797
- /**
3798
- * Time Complexity: O(1)
3799
- * Space Complexity: O(1)
3800
- *
3801
- * The enqueue function adds a value to the end of a queue.
3802
- * @param {E} value - The value parameter represents the value that you want to add to the queue.
3803
- */
3804
- enqueue(value) {
3805
- return this.push(value);
3806
- }
3807
- /**
3808
- * Time Complexity: O(1)
3809
- * Space Complexity: O(1)
3810
- */
3811
- /**
3812
- * Time Complexity: O(1)
3813
- * Space Complexity: O(1)
3814
- *
3815
- * The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
3816
- * @returns The method is returning a value of type E or undefined.
3817
- */
3818
- dequeue() {
3819
- return this.shift();
3820
- }
3821
3645
  /**
3822
3646
  * Time Complexity: O(1)
3823
3647
  * Space Complexity: O(1)
@@ -3963,35 +3787,6 @@ var dataStructureTyped = (() => {
3963
3787
  }
3964
3788
  };
3965
3789
  var LinkedListQueue = class _LinkedListQueue extends SinglyLinkedList {
3966
- /**
3967
- * The `get first` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
3968
- * @returns The `get first()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
3969
- */
3970
- get first() {
3971
- var _a;
3972
- return (_a = this.head) == null ? void 0 : _a.value;
3973
- }
3974
- /**
3975
- * The enqueue function adds a value to the end of an array.
3976
- * @param {E} value - The value parameter represents the value that you want to add to the queue.
3977
- */
3978
- enqueue(value) {
3979
- return this.push(value);
3980
- }
3981
- /**
3982
- * The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
3983
- * @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
3984
- */
3985
- dequeue() {
3986
- return this.shift();
3987
- }
3988
- /**
3989
- * The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
3990
- * @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
3991
- */
3992
- peek() {
3993
- return this.first;
3994
- }
3995
3790
  /**
3996
3791
  * Time Complexity: O(n)
3997
3792
  * Space Complexity: O(n)
@@ -4753,67 +4548,6 @@ var dataStructureTyped = (() => {
4753
4548
  }
4754
4549
  return newDeque;
4755
4550
  }
4756
- /**
4757
- * Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
4758
- * Space Complexity: O(n) - Due to potential resizing.
4759
- */
4760
- /**
4761
- * Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
4762
- * Space Complexity: O(n) - Due to potential resizing.
4763
- *
4764
- * The addLast function adds an element to the end of an array.
4765
- * @param {E} element - The element parameter represents the element that you want to add to the end of the
4766
- * data structure.
4767
- */
4768
- addLast(element) {
4769
- return this.push(element);
4770
- }
4771
- /**
4772
- * Time Complexity: O(1)
4773
- * Space Complexity: O(1)
4774
- */
4775
- /**
4776
- * Time Complexity: O(1)
4777
- * Space Complexity: O(1)
4778
- *
4779
- * The function "pollLast" removes and returns the last element of an array.
4780
- * @returns The last element of the array is being returned.
4781
- */
4782
- pollLast() {
4783
- return this.pop();
4784
- }
4785
- /**
4786
- * Time Complexity: O(1)
4787
- * Space Complexity: O(1)
4788
- * /
4789
-
4790
- /**
4791
- * Time Complexity: O(1)
4792
- * Space Complexity: O(1)
4793
- *
4794
- * The "addFirst" function adds an element to the beginning of an array.
4795
- * @param {E} element - The parameter "element" represents the element that you want to add to the
4796
- * beginning of the data structure.
4797
- */
4798
- addFirst(element) {
4799
- return this.unshift(element);
4800
- }
4801
- /**
4802
- * Time Complexity: O(1)
4803
- * Space Complexity: O(1)
4804
- * /
4805
-
4806
- /**
4807
- * Time Complexity: O(1)
4808
- * Space Complexity: O(1)
4809
- *
4810
- * The function "pollFirst" removes and returns the first element of an array.
4811
- * @returns The method `pollFirst()` is returning the first element of the array after removing it
4812
- * from the beginning. If the array is empty, it will return `undefined`.
4813
- */
4814
- pollFirst() {
4815
- return this.shift();
4816
- }
4817
4551
  /**
4818
4552
  * Time Complexity: O(n)
4819
4553
  * Space Complexity: O(1)
@@ -5786,6 +5520,9 @@ var dataStructureTyped = (() => {
5786
5520
  set vertexMap(v) {
5787
5521
  this._vertexMap = v;
5788
5522
  }
5523
+ get size() {
5524
+ return this._vertexMap.size;
5525
+ }
5789
5526
  /**
5790
5527
  * Time Complexity: O(1) - Constant time for Map lookup.
5791
5528
  * Space Complexity: O(1) - Constant space, as it creates only a few variables.
@@ -8649,7 +8386,7 @@ var dataStructureTyped = (() => {
8649
8386
  return true;
8650
8387
  if (iterationType === "RECURSIVE" /* RECURSIVE */) {
8651
8388
  const dfs = (cur, min, max) => {
8652
- if (!cur)
8389
+ if (!this.isRealNode(cur))
8653
8390
  return true;
8654
8391
  const numKey = this.extractor(cur.key);
8655
8392
  if (numKey <= min || numKey >= max)
@@ -8664,14 +8401,14 @@ var dataStructureTyped = (() => {
8664
8401
  const stack = [];
8665
8402
  let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
8666
8403
  let curr = beginRoot;
8667
- while (curr || stack.length > 0) {
8668
- while (curr) {
8404
+ while (this.isRealNode(curr) || stack.length > 0) {
8405
+ while (this.isRealNode(curr)) {
8669
8406
  stack.push(curr);
8670
8407
  curr = curr.left;
8671
8408
  }
8672
8409
  curr = stack.pop();
8673
8410
  const numKey = this.extractor(curr.key);
8674
- if (!curr || !checkMax && prev >= numKey || checkMax && prev <= numKey)
8411
+ if (!this.isRealNode(curr) || !checkMax && prev >= numKey || checkMax && prev <= numKey)
8675
8412
  return false;
8676
8413
  prev = numKey;
8677
8414
  curr = curr.right;
@@ -8736,7 +8473,7 @@ var dataStructureTyped = (() => {
8736
8473
  return -1;
8737
8474
  if (iterationType === "RECURSIVE" /* RECURSIVE */) {
8738
8475
  const _getMaxHeight = (cur) => {
8739
- if (!cur)
8476
+ if (!this.isRealNode(cur))
8740
8477
  return -1;
8741
8478
  const leftHeight = _getMaxHeight(cur.left);
8742
8479
  const rightHeight = _getMaxHeight(cur.right);
@@ -8748,9 +8485,9 @@ var dataStructureTyped = (() => {
8748
8485
  let maxHeight = 0;
8749
8486
  while (stack.length > 0) {
8750
8487
  const { node, depth } = stack.pop();
8751
- if (node.left)
8488
+ if (this.isRealNode(node.left))
8752
8489
  stack.push({ node: node.left, depth: depth + 1 });
8753
- if (node.right)
8490
+ if (this.isRealNode(node.right))
8754
8491
  stack.push({ node: node.right, depth: depth + 1 });
8755
8492
  maxHeight = Math.max(maxHeight, depth);
8756
8493
  }
@@ -9015,45 +8752,45 @@ var dataStructureTyped = (() => {
9015
8752
  switch (pattern) {
9016
8753
  case "in":
9017
8754
  if (includeNull) {
9018
- if (node && this.isNodeOrNull(node.left))
8755
+ if (this.isRealNode(node) && this.isNodeOrNull(node.left))
9019
8756
  _traverse(node.left);
9020
8757
  this.isNodeOrNull(node) && ans.push(callback(node));
9021
- if (node && this.isNodeOrNull(node.right))
8758
+ if (this.isRealNode(node) && this.isNodeOrNull(node.right))
9022
8759
  _traverse(node.right);
9023
8760
  } else {
9024
- if (node && node.left)
8761
+ if (this.isRealNode(node) && this.isRealNode(node.left))
9025
8762
  _traverse(node.left);
9026
8763
  this.isRealNode(node) && ans.push(callback(node));
9027
- if (node && node.right)
8764
+ if (this.isRealNode(node) && this.isRealNode(node.right))
9028
8765
  _traverse(node.right);
9029
8766
  }
9030
8767
  break;
9031
8768
  case "pre":
9032
8769
  if (includeNull) {
9033
8770
  this.isNodeOrNull(node) && ans.push(callback(node));
9034
- if (node && this.isNodeOrNull(node.left))
8771
+ if (this.isRealNode(node) && this.isNodeOrNull(node.left))
9035
8772
  _traverse(node.left);
9036
- if (node && this.isNodeOrNull(node.right))
8773
+ if (this.isRealNode(node) && this.isNodeOrNull(node.right))
9037
8774
  _traverse(node.right);
9038
8775
  } else {
9039
8776
  this.isRealNode(node) && ans.push(callback(node));
9040
- if (node && node.left)
8777
+ if (this.isRealNode(node) && this.isRealNode(node.left))
9041
8778
  _traverse(node.left);
9042
- if (node && node.right)
8779
+ if (this.isRealNode(node) && this.isRealNode(node.right))
9043
8780
  _traverse(node.right);
9044
8781
  }
9045
8782
  break;
9046
8783
  case "post":
9047
8784
  if (includeNull) {
9048
- if (node && this.isNodeOrNull(node.left))
8785
+ if (this.isRealNode(node) && this.isNodeOrNull(node.left))
9049
8786
  _traverse(node.left);
9050
- if (node && this.isNodeOrNull(node.right))
8787
+ if (this.isRealNode(node) && this.isNodeOrNull(node.right))
9051
8788
  _traverse(node.right);
9052
8789
  this.isNodeOrNull(node) && ans.push(callback(node));
9053
8790
  } else {
9054
- if (node && node.left)
8791
+ if (this.isRealNode(node) && this.isRealNode(node.left))
9055
8792
  _traverse(node.left);
9056
- if (node && node.right)
8793
+ if (this.isRealNode(node) && this.isRealNode(node.right))
9057
8794
  _traverse(node.right);
9058
8795
  this.isRealNode(node) && ans.push(callback(node));
9059
8796
  }
@@ -11424,7 +11161,7 @@ var dataStructureTyped = (() => {
11424
11161
  }
11425
11162
  /**
11426
11163
  * The function returns the color value of a variable.
11427
- * @returns The color value stored in the protected variable `_color`.
11164
+ * @returns The color value stored in the private variable `_color`.
11428
11165
  */
11429
11166
  get color() {
11430
11167
  return this._color;
@@ -11439,35 +11176,35 @@ var dataStructureTyped = (() => {
11439
11176
  };
11440
11177
  var RedBlackTree = class _RedBlackTree extends BST {
11441
11178
  /**
11442
- * This is the constructor function for a Red-Black Tree data structure in TypeScript, which
11443
- * initializes the tree with optional nodes and options.
11444
- * @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, NODE>`
11445
- * objects. It represents the initial nodes that will be added to the RBTree during its
11446
- * construction. If this parameter is provided, the `addMany` method is called to add all the
11447
- * nodes to the
11448
- * @param [options] - The `options` parameter is an optional object that allows you to customize the
11449
- * behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
11450
- * only a subset of the properties defined in the `RBTreeOptions` interface.
11179
+ * This is the constructor function for a Red-Black Tree data structure in TypeScript.
11180
+ * @param keysOrNodesOrEntries - The `keysOrNodesOrEntries` parameter is an iterable object that can
11181
+ * contain keys, nodes, or entries. It is used to initialize the RBTree with the provided keys,
11182
+ * nodes, or entries.
11183
+ * @param [options] - The `options` parameter is an optional object that can be passed to the
11184
+ * constructor. It allows you to customize the behavior of the RBTree. It can include properties such
11185
+ * as `compareKeys`, `compareValues`, `allowDuplicates`, etc. These properties define how the RBTree
11186
+ * should compare keys and
11451
11187
  */
11452
11188
  constructor(keysOrNodesOrEntries = [], options) {
11453
11189
  super([], options);
11454
- __publicField(this, "_Sentinel", new RedBlackTreeNode(NaN));
11190
+ __publicField(this, "_SENTINEL", new RedBlackTreeNode(NaN));
11455
11191
  __publicField(this, "_root");
11456
11192
  __publicField(this, "_size", 0);
11457
- this._root = this._Sentinel;
11458
- if (keysOrNodesOrEntries)
11459
- super.addMany(keysOrNodesOrEntries);
11193
+ this._root = this.SENTINEL;
11194
+ if (keysOrNodesOrEntries) {
11195
+ this.addMany(keysOrNodesOrEntries);
11196
+ }
11460
11197
  }
11461
11198
  /**
11462
- * The function returns the value of the `_Sentinel` property.
11463
- * @returns The method is returning the value of the `_Sentinel` property.
11199
+ * The function returns the value of the _SENTINEL property.
11200
+ * @returns The method is returning the value of the `_SENTINEL` property.
11464
11201
  */
11465
- get Sentinel() {
11466
- return this._Sentinel;
11202
+ get SENTINEL() {
11203
+ return this._SENTINEL;
11467
11204
  }
11468
11205
  /**
11469
- * The function returns the root node.
11470
- * @returns The root node of the data structure.
11206
+ * The function returns the root node of a tree or undefined if there is no root.
11207
+ * @returns The root node of the tree structure, or undefined if there is no root node.
11471
11208
  */
11472
11209
  get root() {
11473
11210
  return this._root;
@@ -11481,13 +11218,13 @@ var dataStructureTyped = (() => {
11481
11218
  }
11482
11219
  /**
11483
11220
  * The function creates a new Red-Black Tree node with the specified key, value, and color.
11484
- * @param {K} key - The key parameter is the key value associated with the node. It is used to
11485
- * identify and compare nodes in the Red-Black Tree.
11221
+ * @param {K} key - The key parameter represents the key of the node being created. It is of type K,
11222
+ * which is a generic type representing the key's data type.
11486
11223
  * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
11487
- * associated with the node. It is of type `V`, which is a generic type that can be replaced with any
11488
- * specific type when using the `createNode` method.
11224
+ * associated with the key in the node. It is not required and can be omitted if not needed.
11489
11225
  * @param {RBTNColor} color - The "color" parameter is used to specify the color of the node in a
11490
- * Red-Black Tree. It can be either "RED" or "BLACK". By default, the color is set to "BLACK".
11226
+ * Red-Black Tree. It is an optional parameter with a default value of "RBTNColor.BLACK". The color
11227
+ * can be either "RBTNColor.RED" or "RBTNColor.BLACK".
11491
11228
  * @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
11492
11229
  * value, and color.
11493
11230
  */
@@ -11495,10 +11232,10 @@ var dataStructureTyped = (() => {
11495
11232
  return new RedBlackTreeNode(key, value, color);
11496
11233
  }
11497
11234
  /**
11498
- * The function creates a Red-Black Tree with the specified options and returns it.
11499
- * @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
11500
- * passed to the `createTree` function. It is used to customize the behavior of the `RedBlackTree`
11501
- * class.
11235
+ * The function creates a Red-Black Tree with the given options and returns it.
11236
+ * @param [options] - The `options` parameter is an optional object that contains configuration
11237
+ * options for creating the Red-Black Tree. It is of type `RBTreeOptions<K>`, where `K` represents
11238
+ * the type of keys in the tree.
11502
11239
  * @returns a new instance of a RedBlackTree object.
11503
11240
  */
11504
11241
  createTree(options) {
@@ -11507,12 +11244,18 @@ var dataStructureTyped = (() => {
11507
11244
  }, options));
11508
11245
  }
11509
11246
  /**
11510
- * The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible.
11511
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, where:
11512
- * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
11513
- * `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If a value
11514
- * is provided, it will be used when creating the new node. If no value is provided, the new node
11515
- * @returns a node of type NODE or undefined.
11247
+ * Time Complexity: O(1)
11248
+ * Space Complexity: O(1)
11249
+ */
11250
+ /**
11251
+ * Time Complexity: O(1)
11252
+ * Space Complexity: O(1)
11253
+ *
11254
+ * The function `keyValueOrEntryToNode` takes a key, value, or entry and returns a node if it is
11255
+ * valid, otherwise it returns undefined.
11256
+ * @param {KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntry - The key, value, or entry to convert.
11257
+ * @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntry` is a key).
11258
+ * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
11516
11259
  */
11517
11260
  keyValueOrEntryToNode(keyOrNodeOrEntry, value) {
11518
11261
  let node;
@@ -11535,183 +11278,63 @@ var dataStructureTyped = (() => {
11535
11278
  return node;
11536
11279
  }
11537
11280
  /**
11538
- * The function checks if an keyOrNodeOrEntry is an instance of the RedBlackTreeNode class.
11539
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
11540
- * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the RedBlackTreeNode
11541
- * class.
11542
- */
11281
+ * Time Complexity: O(1)
11282
+ * Space Complexity: O(1)
11283
+ * /
11284
+
11285
+ /**
11286
+ * Time Complexity: O(1)
11287
+ * Space Complexity: O(1)
11288
+ *
11289
+ * The function checks if the input is an instance of the RedBlackTreeNode class.
11290
+ * @param {KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntry - The object to check.
11291
+ * @returns {boolean} - `true` if the object is a Red-Black Tree node, `false` otherwise.
11292
+ */
11543
11293
  isNode(keyOrNodeOrEntry) {
11544
11294
  return keyOrNodeOrEntry instanceof RedBlackTreeNode;
11545
11295
  }
11546
11296
  /**
11297
+ * Time Complexity: O(1)
11298
+ * Space Complexity: O(1)
11299
+ */
11300
+ /**
11301
+ * Time Complexity: O(1)
11302
+ * Space Complexity: O(1)
11303
+ *
11547
11304
  * The function checks if a given node is a real node in a Red-Black Tree.
11548
11305
  * @param {NODE | undefined} node - The `node` parameter is of type `NODE | undefined`, which means
11549
11306
  * it can either be of type `NODE` or `undefined`.
11550
11307
  * @returns a boolean value.
11551
11308
  */
11552
11309
  isRealNode(node) {
11553
- if (node === this._Sentinel || node === void 0)
11310
+ if (node === this._SENTINEL || node === void 0)
11554
11311
  return false;
11555
11312
  return node instanceof RedBlackTreeNode;
11556
11313
  }
11557
11314
  /**
11558
11315
  * Time Complexity: O(log n)
11559
11316
  * Space Complexity: O(1)
11560
- * On average (where n is the number of nodes in the tree)
11561
11317
  */
11562
11318
  /**
11563
11319
  * Time Complexity: O(log n)
11564
11320
  * Space Complexity: O(1)
11565
11321
  *
11566
- * The `add` function adds a new node to a binary search tree and performs necessary rotations and
11567
- * color changes to maintain the red-black tree properties.
11568
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
11569
- * entry.
11570
- * @param {V} [value] - The `value` parameter represents the value associated with the key that is
11571
- * being added to the binary search tree.
11572
- * @returns The method `add` returns either the newly added node (`NODE`) or `undefined`.
11573
- */
11574
- add(keyOrNodeOrEntry, value) {
11575
- const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
11576
- if (newNode === void 0)
11577
- return false;
11578
- newNode.left = this._Sentinel;
11579
- newNode.right = this._Sentinel;
11580
- let y = void 0;
11581
- let x = this.root;
11582
- while (x !== this._Sentinel) {
11583
- y = x;
11584
- if (x) {
11585
- if (newNode.key < x.key) {
11586
- x = x.left;
11587
- } else if (newNode.key > x.key) {
11588
- x = x == null ? void 0 : x.right;
11589
- } else {
11590
- if (newNode !== x) {
11591
- this._replaceNode(x, newNode);
11592
- }
11593
- return false;
11594
- }
11595
- }
11596
- }
11597
- newNode.parent = y;
11598
- if (y === void 0) {
11599
- this._setRoot(newNode);
11600
- } else if (newNode.key < y.key) {
11601
- y.left = newNode;
11602
- } else {
11603
- y.right = newNode;
11604
- }
11605
- if (newNode.parent === void 0) {
11606
- newNode.color = 0 /* BLACK */;
11607
- this._size++;
11608
- return false;
11609
- }
11610
- if (newNode.parent.parent === void 0) {
11611
- this._size++;
11612
- return false;
11613
- }
11614
- this._fixInsert(newNode);
11615
- this._size++;
11616
- return true;
11617
- }
11618
- /**
11619
- * Time Complexity: O(log n)
11620
- * Space Complexity: O(1)
11621
- */
11622
- /**
11623
- * Time Complexity: O(log n)
11624
- * Space Complexity: O(1)
11625
- *
11626
- * The `delete` function removes a node from a binary tree based on a given identifier and updates
11627
- * the tree accordingly.
11628
- * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
11629
- * that you want to use to identify the node that you want to delete from the binary tree. It can be
11630
- * of any type that is returned by the callback function `C`. It can also be `null` or `undefined` if
11631
- * you don't want to
11632
- * @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` and
11633
- * returns a value of type `ReturnType<C>`. It is used to determine if a node should be deleted based
11634
- * on its identifier. The `callback` function is optional and defaults to `this._defaultOneParam
11635
- * @returns an array of `BinaryTreeDeleteResult<NODE>`.
11636
- */
11637
- delete(identifier, callback = this._defaultOneParamCallback) {
11638
- const ans = [];
11639
- if (identifier === null)
11640
- return ans;
11641
- const helper = (node) => {
11642
- let z = this._Sentinel;
11643
- let x, y;
11644
- while (node !== this._Sentinel) {
11645
- if (node && callback(node) === identifier) {
11646
- z = node;
11647
- }
11648
- if (node && identifier && callback(node) <= identifier) {
11649
- node = node.right;
11650
- } else {
11651
- node = node == null ? void 0 : node.left;
11652
- }
11653
- }
11654
- if (z === this._Sentinel) {
11655
- this._size--;
11656
- return;
11657
- }
11658
- y = z;
11659
- let yOriginalColor = y.color;
11660
- if (z.left === this._Sentinel) {
11661
- x = z.right;
11662
- this._rbTransplant(z, z.right);
11663
- } else if (z.right === this._Sentinel) {
11664
- x = z.left;
11665
- this._rbTransplant(z, z.left);
11666
- } else {
11667
- y = this.getLeftMost(z.right);
11668
- yOriginalColor = y.color;
11669
- x = y.right;
11670
- if (y.parent === z) {
11671
- x.parent = y;
11672
- } else {
11673
- this._rbTransplant(y, y.right);
11674
- y.right = z.right;
11675
- y.right.parent = y;
11676
- }
11677
- this._rbTransplant(z, y);
11678
- y.left = z.left;
11679
- y.left.parent = y;
11680
- y.color = z.color;
11681
- }
11682
- if (yOriginalColor === 0 /* BLACK */) {
11683
- this._fixDelete(x);
11684
- }
11685
- this._size--;
11686
- ans.push({ deleted: z, needBalanced: void 0 });
11687
- };
11688
- helper(this.root);
11689
- return ans;
11690
- }
11691
- /**
11692
- * Time Complexity: O(log n)
11693
- * Space Complexity: O(1)
11694
- */
11695
- /**
11696
- * Time Complexity: O(log n)
11697
- * Space Complexity: O(1)
11698
- *
11699
- * The function `getNode` retrieves a single node from a binary tree based on a given identifier and
11322
+ * The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
11700
11323
  * callback function.
11701
- * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value used to
11702
- * identify the node you want to retrieve. It can be of any type that is the return type of the `C`
11703
- * callback function. If the `identifier` is `undefined`, it means you want to retrieve the first
11704
- * node that matches the other criteria
11324
+ * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
11325
+ * that you want to search for in the binary search tree. It can be of any type that is compatible
11326
+ * with the type of nodes in the tree.
11705
11327
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
11706
- * the binary tree. It is used to determine if a node matches the given identifier. The `callback`
11707
- * function should take a single parameter of type `NODE` (the type of the nodes in the binary tree) and
11708
- * @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is the starting point for
11709
- * searching for a node in a binary tree. It can be either a key value or a node object. If it is not
11710
- * provided, the search will start from the root of the binary tree.
11711
- * @param iterationType - The `iterationType` parameter is a variable that determines the type of
11712
- * iteration to be performed when searching for nodes in the binary tree. It is used in the
11713
- * `getNodes` method, which is called within the `getNode` method.
11714
- * @returns a value of type `NODE`, `null`, or `undefined`.
11328
+ * the tree. It is used to determine whether a node matches the given identifier. The `callback`
11329
+ * function should take a node as its parameter and return a value that can be compared to the
11330
+ * `identifier` parameter.
11331
+ * @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
11332
+ * search tree. It can be either a key or a node. If it is a key, it will be converted to a node
11333
+ * using the `ensureNode` method. If it is not provided, the `root`
11334
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
11335
+ * be performed when searching for nodes in the binary search tree. It is an optional parameter and
11336
+ * its default value is taken from the `iterationType` property of the class.
11337
+ * @returns The method is returning a value of type `NODE | null | undefined`.
11715
11338
  */
11716
11339
  getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
11717
11340
  var _a;
@@ -11728,10 +11351,11 @@ var dataStructureTyped = (() => {
11728
11351
  * Time Complexity: O(1)
11729
11352
  * Space Complexity: O(1)
11730
11353
  *
11731
- * The "clear" function sets the root node to the sentinel node and resets the size to 0.
11354
+ * The "clear" function sets the root node of a data structure to a sentinel value and resets the
11355
+ * size counter to zero.
11732
11356
  */
11733
11357
  clear() {
11734
- this._root = this._Sentinel;
11358
+ this._root = this.SENTINEL;
11735
11359
  this._size = 0;
11736
11360
  }
11737
11361
  /**
@@ -11742,27 +11366,102 @@ var dataStructureTyped = (() => {
11742
11366
  * Time Complexity: O(log n)
11743
11367
  * Space Complexity: O(1)
11744
11368
  *
11745
- * The function returns the predecessor of a given node in a red-black tree.
11746
- * @param {RedBlackTreeNode} x - The parameter `x` is of type `RedBlackTreeNode`, which represents a node in a
11747
- * Red-Black Tree.
11748
- * @returns the predecessor of the given RedBlackTreeNode 'x'.
11369
+ * The function adds a new node to a Red-Black Tree data structure and returns a boolean indicating
11370
+ * whether the operation was successful.
11371
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
11372
+ * entry.
11373
+ * @param {V} [value] - The `value` parameter is the value associated with the key that is being
11374
+ * added to the tree.
11375
+ * @returns The method is returning a boolean value. It returns true if the node was successfully
11376
+ * added or updated, and false otherwise.
11377
+ */
11378
+ add(keyOrNodeOrEntry, value) {
11379
+ const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
11380
+ if (!this.isRealNode(newNode))
11381
+ return false;
11382
+ const insertStatus = this._insert(newNode);
11383
+ if (insertStatus === "inserted") {
11384
+ if (this.isRealNode(this._root)) {
11385
+ this._root.color = 0 /* BLACK */;
11386
+ } else {
11387
+ return false;
11388
+ }
11389
+ this._size++;
11390
+ return true;
11391
+ } else
11392
+ return insertStatus === "updated";
11393
+ }
11394
+ /**
11395
+ * Time Complexity: O(log n)
11396
+ * Space Complexity: O(1)
11397
+ */
11398
+ /**
11399
+ * Time Complexity: O(log n)
11400
+ * Space Complexity: O(1)
11401
+ *
11402
+ * The function `delete` in a binary tree class deletes a node from the tree and fixes the tree if
11403
+ * necessary.
11404
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the
11405
+ * identifier of the node that needs to be deleted from the binary tree. It can be of any type that
11406
+ * is returned by the callback function `C`. It can also be `null` or `undefined` if the node to be
11407
+ * deleted is not found.
11408
+ * @param {C} callback - The `callback` parameter is a function that is used to retrieve a node from
11409
+ * the binary tree based on its identifier. It is an optional parameter and if not provided, the
11410
+ * `_defaultOneParamCallback` function is used as the default callback. The callback function should
11411
+ * return the identifier of the node to
11412
+ * @returns an array of BinaryTreeDeleteResult<NODE> objects.
11749
11413
  */
11750
- getPredecessor(x) {
11751
- if (this.isRealNode(x.left)) {
11752
- return this.getRightMost(x.left);
11414
+ delete(identifier, callback = this._defaultOneParamCallback) {
11415
+ if (identifier === null)
11416
+ return [];
11417
+ const results = [];
11418
+ const nodeToDelete = this.isRealNode(identifier) ? identifier : this.getNode(identifier, callback);
11419
+ if (!nodeToDelete) {
11420
+ return results;
11421
+ }
11422
+ let originalColor = nodeToDelete.color;
11423
+ let replacementNode;
11424
+ if (!this.isRealNode(nodeToDelete.left)) {
11425
+ replacementNode = nodeToDelete.right;
11426
+ this._transplant(nodeToDelete, nodeToDelete.right);
11427
+ } else if (!this.isRealNode(nodeToDelete.right)) {
11428
+ replacementNode = nodeToDelete.left;
11429
+ this._transplant(nodeToDelete, nodeToDelete.left);
11430
+ } else {
11431
+ const successor = this.getLeftMost(nodeToDelete.right);
11432
+ if (successor) {
11433
+ originalColor = successor.color;
11434
+ replacementNode = successor.right;
11435
+ if (successor.parent === nodeToDelete) {
11436
+ if (this.isRealNode(replacementNode)) {
11437
+ replacementNode.parent = successor;
11438
+ }
11439
+ } else {
11440
+ this._transplant(successor, successor.right);
11441
+ successor.right = nodeToDelete.right;
11442
+ if (this.isRealNode(successor.right)) {
11443
+ successor.right.parent = successor;
11444
+ }
11445
+ }
11446
+ this._transplant(nodeToDelete, successor);
11447
+ successor.left = nodeToDelete.left;
11448
+ if (this.isRealNode(successor.left)) {
11449
+ successor.left.parent = successor;
11450
+ }
11451
+ successor.color = nodeToDelete.color;
11452
+ }
11753
11453
  }
11754
- let y = x.parent;
11755
- while (this.isRealNode(y) && x === y.left) {
11756
- x = y;
11757
- y = y.parent;
11454
+ this._size--;
11455
+ if (originalColor === 0 /* BLACK */) {
11456
+ this._deleteFixup(replacementNode);
11758
11457
  }
11759
- return y;
11458
+ results.push({ deleted: nodeToDelete, needBalanced: void 0 });
11459
+ return results;
11760
11460
  }
11761
11461
  /**
11762
- * The function sets the root node of a tree structure and updates the parent property of the new
11763
- * root node.
11764
- * @param {NODE} v - The parameter "v" is of type "NODE", which represents a node in a data
11765
- * structure.
11462
+ * The function sets the root of a tree-like structure and updates the parent property of the new
11463
+ * root.
11464
+ * @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
11766
11465
  */
11767
11466
  _setRoot(v) {
11768
11467
  if (v) {
@@ -11778,28 +11477,61 @@ var dataStructureTyped = (() => {
11778
11477
  * Time Complexity: O(1)
11779
11478
  * Space Complexity: O(1)
11780
11479
  *
11781
- * The function performs a left rotation on a binary tree node.
11782
- * @param {RedBlackTreeNode} x - The parameter `x` is of type `NODE`, which likely represents a node in a binary tree.
11480
+ * The function replaces an old node with a new node while preserving the color of the old node.
11481
+ * @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
11482
+ * the data structure.
11483
+ * @param {NODE} newNode - The `newNode` parameter is the new node that will replace the old node in
11484
+ * the data structure.
11485
+ * @returns The method is returning the result of calling the `_replaceNode` method from the
11486
+ * superclass, with the `oldNode` and `newNode` parameters.
11783
11487
  */
11784
- _leftRotate(x) {
11785
- if (x.right) {
11786
- const y = x.right;
11787
- x.right = y.left;
11788
- if (y.left !== this._Sentinel) {
11789
- if (y.left)
11790
- y.left.parent = x;
11791
- }
11792
- y.parent = x.parent;
11793
- if (x.parent === void 0) {
11794
- this._setRoot(y);
11795
- } else if (x === x.parent.left) {
11796
- x.parent.left = y;
11488
+ _replaceNode(oldNode, newNode) {
11489
+ newNode.color = oldNode.color;
11490
+ return super._replaceNode(oldNode, newNode);
11491
+ }
11492
+ /**
11493
+ * Time Complexity: O(log n)
11494
+ * Space Complexity: O(1)
11495
+ */
11496
+ /**
11497
+ * Time Complexity: O(log n)
11498
+ * Space Complexity: O(1)
11499
+ *
11500
+ * The `_insert` function inserts or updates a node in a binary search tree and performs necessary
11501
+ * fix-ups to maintain the red-black tree properties.
11502
+ * @param {NODE} node - The `node` parameter represents the node that needs to be inserted into a
11503
+ * binary search tree. It contains a `key` property that is used to determine the position of the
11504
+ * node in the tree.
11505
+ * @returns {'inserted' | 'updated'} - The result of the insertion.
11506
+ */
11507
+ _insert(node) {
11508
+ var _a, _b;
11509
+ let current = this.root;
11510
+ let parent = void 0;
11511
+ while (this.isRealNode(current)) {
11512
+ parent = current;
11513
+ if (node.key < current.key) {
11514
+ current = (_a = current.left) != null ? _a : this.SENTINEL;
11515
+ } else if (node.key > current.key) {
11516
+ current = (_b = current.right) != null ? _b : this.SENTINEL;
11797
11517
  } else {
11798
- x.parent.right = y;
11518
+ this._replaceNode(current, node);
11519
+ return "updated";
11799
11520
  }
11800
- y.left = x;
11801
- x.parent = y;
11802
11521
  }
11522
+ node.parent = parent;
11523
+ if (!parent) {
11524
+ this._setRoot(node);
11525
+ } else if (node.key < parent.key) {
11526
+ parent.left = node;
11527
+ } else {
11528
+ parent.right = node;
11529
+ }
11530
+ node.left = this.SENTINEL;
11531
+ node.right = this.SENTINEL;
11532
+ node.color = 1 /* RED */;
11533
+ this._insertFixup(node);
11534
+ return "inserted";
11803
11535
  }
11804
11536
  /**
11805
11537
  * Time Complexity: O(1)
@@ -11809,28 +11541,21 @@ var dataStructureTyped = (() => {
11809
11541
  * Time Complexity: O(1)
11810
11542
  * Space Complexity: O(1)
11811
11543
  *
11812
- * The function performs a right rotation on a red-black tree node.
11813
- * @param {RedBlackTreeNode} x - x is a RedBlackTreeNode, which represents the node that needs to be right
11814
- * rotated.
11544
+ * The function `_transplant` is used to replace a node `u` with another node `v` in a binary tree.
11545
+ * @param {NODE} u - The parameter "u" represents a node in a binary tree.
11546
+ * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`, which means it can
11547
+ * either be a `NODE` object or `undefined`.
11815
11548
  */
11816
- _rightRotate(x) {
11817
- if (x.left) {
11818
- const y = x.left;
11819
- x.left = y.right;
11820
- if (y.right !== this._Sentinel) {
11821
- if (y.right)
11822
- y.right.parent = x;
11823
- }
11824
- y.parent = x.parent;
11825
- if (x.parent === void 0) {
11826
- this._setRoot(y);
11827
- } else if (x === x.parent.right) {
11828
- x.parent.right = y;
11829
- } else {
11830
- x.parent.left = y;
11831
- }
11832
- y.right = x;
11833
- x.parent = y;
11549
+ _transplant(u, v) {
11550
+ if (!u.parent) {
11551
+ this._setRoot(v);
11552
+ } else if (u === u.parent.left) {
11553
+ u.parent.left = v;
11554
+ } else {
11555
+ u.parent.right = v;
11556
+ }
11557
+ if (v) {
11558
+ v.parent = u.parent;
11834
11559
  }
11835
11560
  }
11836
11561
  /**
@@ -11841,55 +11566,53 @@ var dataStructureTyped = (() => {
11841
11566
  * Time Complexity: O(log n)
11842
11567
  * Space Complexity: O(1)
11843
11568
  *
11844
- * The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
11845
- * @param {RedBlackTreeNode} k - The parameter `k` is a RedBlackTreeNode object, which represents a node in a
11846
- * red-black tree.
11847
- */
11848
- _fixInsert(k) {
11849
- let u;
11850
- while (k.parent && k.parent.color === 1 /* RED */) {
11851
- if (k.parent.parent && k.parent === k.parent.parent.right) {
11852
- u = k.parent.parent.left;
11853
- if (u && u.color === 1 /* RED */) {
11854
- k.parent.color = 0 /* BLACK */;
11855
- u.color = 0 /* BLACK */;
11856
- k.parent.parent.color = 1 /* RED */;
11857
- k = k.parent.parent;
11569
+ * The `_insertFixup` function is used to fix the Red-Black Tree after inserting a new node.
11570
+ * @param {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree. It can
11571
+ * either be a valid node object or `undefined`.
11572
+ */
11573
+ _insertFixup(z) {
11574
+ var _a, _b, _c, _d;
11575
+ while (((_a = z == null ? void 0 : z.parent) == null ? void 0 : _a.color) === 1 /* RED */) {
11576
+ if (z.parent === ((_b = z.parent.parent) == null ? void 0 : _b.left)) {
11577
+ const y = z.parent.parent.right;
11578
+ if ((y == null ? void 0 : y.color) === 1 /* RED */) {
11579
+ z.parent.color = 0 /* BLACK */;
11580
+ y.color = 0 /* BLACK */;
11581
+ z.parent.parent.color = 1 /* RED */;
11582
+ z = z.parent.parent;
11858
11583
  } else {
11859
- if (k === k.parent.left) {
11860
- k = k.parent;
11861
- this._rightRotate(k);
11584
+ if (z === z.parent.right) {
11585
+ z = z.parent;
11586
+ this._leftRotate(z);
11862
11587
  }
11863
- if (k.parent.color === 1 /* RED */) {
11864
- k.parent.color = 0 /* BLACK */;
11865
- k.parent.parent.color = 1 /* RED */;
11588
+ if (z && this.isRealNode(z.parent) && this.isRealNode(z.parent.parent)) {
11589
+ z.parent.color = 0 /* BLACK */;
11590
+ z.parent.parent.color = 1 /* RED */;
11591
+ this._rightRotate(z.parent.parent);
11866
11592
  }
11867
- this._leftRotate(k.parent.parent);
11868
11593
  }
11869
11594
  } else {
11870
- u = k.parent.parent.right;
11871
- if (u && u.color === 1 /* RED */) {
11872
- k.parent.color = 0 /* BLACK */;
11873
- u.color = 0 /* BLACK */;
11874
- k.parent.parent.color = 1 /* RED */;
11875
- k = k.parent.parent;
11595
+ const y = (_d = (_c = z == null ? void 0 : z.parent) == null ? void 0 : _c.parent) == null ? void 0 : _d.left;
11596
+ if ((y == null ? void 0 : y.color) === 1 /* RED */) {
11597
+ z.parent.color = 0 /* BLACK */;
11598
+ y.color = 0 /* BLACK */;
11599
+ z.parent.parent.color = 1 /* RED */;
11600
+ z = z.parent.parent;
11876
11601
  } else {
11877
- if (k === k.parent.right) {
11878
- k = k.parent;
11879
- this._leftRotate(k);
11602
+ if (z === z.parent.left) {
11603
+ z = z.parent;
11604
+ this._rightRotate(z);
11880
11605
  }
11881
- if (k.parent.color === 1 /* RED */) {
11882
- k.parent.color = 0 /* BLACK */;
11883
- k.parent.parent.color = 1 /* RED */;
11606
+ if (z && this.isRealNode(z.parent) && this.isRealNode(z.parent.parent)) {
11607
+ z.parent.color = 0 /* BLACK */;
11608
+ z.parent.parent.color = 1 /* RED */;
11609
+ this._leftRotate(z.parent.parent);
11884
11610
  }
11885
- this._rightRotate(k.parent.parent);
11886
11611
  }
11887
11612
  }
11888
- if (k === this.root) {
11889
- break;
11890
- }
11891
11613
  }
11892
- this.root.color = 0 /* BLACK */;
11614
+ if (this.isRealNode(this._root))
11615
+ this._root.color = 0 /* BLACK */;
11893
11616
  }
11894
11617
  /**
11895
11618
  * Time Complexity: O(log n)
@@ -11899,69 +11622,75 @@ var dataStructureTyped = (() => {
11899
11622
  * Time Complexity: O(log n)
11900
11623
  * Space Complexity: O(1)
11901
11624
  *
11902
- * The function `_fixDelete` is used to fix the red-black tree after a node deletion.
11903
- * @param {RedBlackTreeNode} x - The parameter `x` represents a node in a Red-Black Tree (RBT).
11625
+ * The `_deleteFixup` function is used to fix the red-black tree after a node deletion by adjusting
11626
+ * the colors and performing rotations.
11627
+ * @param {NODE | undefined} node - The `node` parameter represents a node in a Red-Black Tree data
11628
+ * structure. It can be either a valid node object or `undefined`.
11629
+ * @returns The function does not return any value. It has a return type of `void`.
11904
11630
  */
11905
- _fixDelete(x) {
11906
- let s;
11907
- while (x !== this.root && x.color === 0 /* BLACK */) {
11908
- if (x.parent && x === x.parent.left) {
11909
- s = x.parent.right;
11910
- if (s.color === 1) {
11911
- s.color = 0 /* BLACK */;
11912
- x.parent.color = 1 /* RED */;
11913
- this._leftRotate(x.parent);
11914
- s = x.parent.right;
11631
+ _deleteFixup(node) {
11632
+ var _a, _b, _c, _d;
11633
+ if (!node || node === this.root || node.color === 0 /* BLACK */) {
11634
+ if (node) {
11635
+ node.color = 0 /* BLACK */;
11636
+ }
11637
+ return;
11638
+ }
11639
+ while (node && node !== this.root && node.color === 0 /* BLACK */) {
11640
+ const parent = node.parent;
11641
+ if (!parent) {
11642
+ break;
11643
+ }
11644
+ if (node === parent.left) {
11645
+ let sibling = parent.right;
11646
+ if ((sibling == null ? void 0 : sibling.color) === 1 /* RED */) {
11647
+ sibling.color = 0 /* BLACK */;
11648
+ parent.color = 1 /* RED */;
11649
+ this._leftRotate(parent);
11650
+ sibling = parent.right;
11915
11651
  }
11916
- if (s.left !== void 0 && s.left.color === 0 /* BLACK */ && s.right && s.right.color === 0 /* BLACK */) {
11917
- s.color = 1 /* RED */;
11918
- x = x.parent;
11652
+ if (((_b = (_a = sibling == null ? void 0 : sibling.left) == null ? void 0 : _a.color) != null ? _b : 0 /* BLACK */) === 0 /* BLACK */) {
11653
+ if (sibling)
11654
+ sibling.color = 1 /* RED */;
11655
+ node = parent;
11919
11656
  } else {
11920
- if (s.right && s.right.color === 0 /* BLACK */) {
11921
- if (s.left)
11922
- s.left.color = 0 /* BLACK */;
11923
- s.color = 1 /* RED */;
11924
- this._rightRotate(s);
11925
- s = x.parent.right;
11926
- }
11927
- if (s)
11928
- s.color = x.parent.color;
11929
- x.parent.color = 0 /* BLACK */;
11930
- if (s && s.right)
11931
- s.right.color = 0 /* BLACK */;
11932
- this._leftRotate(x.parent);
11933
- x = this.root;
11657
+ if (sibling == null ? void 0 : sibling.left)
11658
+ sibling.left.color = 0 /* BLACK */;
11659
+ if (sibling)
11660
+ sibling.color = parent.color;
11661
+ parent.color = 0 /* BLACK */;
11662
+ this._rightRotate(parent);
11663
+ node = this.root;
11934
11664
  }
11935
11665
  } else {
11936
- s = x.parent.left;
11937
- if (s.color === 1) {
11938
- s.color = 0 /* BLACK */;
11939
- x.parent.color = 1 /* RED */;
11940
- this._rightRotate(x.parent);
11941
- s = x.parent.left;
11666
+ let sibling = parent.left;
11667
+ if ((sibling == null ? void 0 : sibling.color) === 1 /* RED */) {
11668
+ sibling.color = 0 /* BLACK */;
11669
+ if (parent)
11670
+ parent.color = 1 /* RED */;
11671
+ this._rightRotate(parent);
11672
+ if (parent)
11673
+ sibling = parent.left;
11942
11674
  }
11943
- if (s && s.right && s.right.color === 0 /* BLACK */ && s.right.color === 0 /* BLACK */) {
11944
- s.color = 1 /* RED */;
11945
- x = x.parent;
11675
+ if (((_d = (_c = sibling == null ? void 0 : sibling.right) == null ? void 0 : _c.color) != null ? _d : 0 /* BLACK */) === 0 /* BLACK */) {
11676
+ if (sibling)
11677
+ sibling.color = 1 /* RED */;
11678
+ node = parent;
11946
11679
  } else {
11947
- if (s && s.left && s.left.color === 0 /* BLACK */) {
11948
- if (s.right)
11949
- s.right.color = 0 /* BLACK */;
11950
- s.color = 1 /* RED */;
11951
- this._leftRotate(s);
11952
- s = x.parent.left;
11953
- }
11954
- if (s)
11955
- s.color = x.parent.color;
11956
- x.parent.color = 0 /* BLACK */;
11957
- if (s && s.left)
11958
- s.left.color = 0 /* BLACK */;
11959
- this._rightRotate(x.parent);
11960
- x = this.root;
11680
+ if (sibling == null ? void 0 : sibling.right)
11681
+ sibling.right.color = 0 /* BLACK */;
11682
+ if (sibling)
11683
+ sibling.color = parent.color;
11684
+ if (parent)
11685
+ parent.color = 0 /* BLACK */;
11686
+ this._leftRotate(parent);
11687
+ node = this.root;
11961
11688
  }
11962
11689
  }
11963
11690
  }
11964
- x.color = 0 /* BLACK */;
11691
+ if (node) {
11692
+ node.color = 0 /* BLACK */;
11693
+ }
11965
11694
  }
11966
11695
  /**
11967
11696
  * Time Complexity: O(1)
@@ -11971,32 +11700,63 @@ var dataStructureTyped = (() => {
11971
11700
  * Time Complexity: O(1)
11972
11701
  * Space Complexity: O(1)
11973
11702
  *
11974
- * The function `_rbTransplant` replaces one node in a red-black tree with another node.
11975
- * @param {RedBlackTreeNode} u - The parameter "u" represents a RedBlackTreeNode object.
11976
- * @param {RedBlackTreeNode} v - The parameter "v" is a RedBlackTreeNode object.
11703
+ * The `_leftRotate` function performs a left rotation on a given node in a binary tree.
11704
+ * @param {NODE | undefined} x - The parameter `x` is of type `NODE | undefined`. It represents a
11705
+ * node in a binary tree or `undefined` if there is no node.
11706
+ * @returns void, which means it does not return any value.
11977
11707
  */
11978
- _rbTransplant(u, v) {
11979
- if (u.parent === void 0) {
11980
- this._setRoot(v);
11981
- } else if (u === u.parent.left) {
11982
- u.parent.left = v;
11708
+ _leftRotate(x) {
11709
+ if (!x || !x.right) {
11710
+ return;
11711
+ }
11712
+ const y = x.right;
11713
+ x.right = y.left;
11714
+ if (this.isRealNode(y.left)) {
11715
+ y.left.parent = x;
11716
+ }
11717
+ y.parent = x.parent;
11718
+ if (!x.parent) {
11719
+ this._setRoot(y);
11720
+ } else if (x === x.parent.left) {
11721
+ x.parent.left = y;
11983
11722
  } else {
11984
- u.parent.right = v;
11723
+ x.parent.right = y;
11985
11724
  }
11986
- v.parent = u.parent;
11725
+ y.left = x;
11726
+ x.parent = y;
11987
11727
  }
11988
11728
  /**
11989
- * The function replaces an old node with a new node while preserving the color of the old node.
11990
- * @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in a
11991
- * data structure. It is of type `NODE`, which is the type of the nodes in the data structure.
11992
- * @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
11993
- * data structure.
11994
- * @returns The method is returning the result of calling the `_replaceNode` method from the
11995
- * superclass, passing in the `oldNode` and `newNode` as arguments.
11729
+ * Time Complexity: O(1)
11730
+ * Space Complexity: O(1)
11996
11731
  */
11997
- _replaceNode(oldNode, newNode) {
11998
- newNode.color = oldNode.color;
11999
- return super._replaceNode(oldNode, newNode);
11732
+ /**
11733
+ * Time Complexity: O(1)
11734
+ * Space Complexity: O(1)
11735
+ *
11736
+ * The `_rightRotate` function performs a right rotation on a given node in a binary tree.
11737
+ * @param {NODE | undefined} y - The parameter `y` is of type `NODE | undefined`. It represents a
11738
+ * node in a binary tree or `undefined` if there is no node.
11739
+ * @returns void, which means it does not return any value.
11740
+ */
11741
+ _rightRotate(y) {
11742
+ if (!y || !y.left) {
11743
+ return;
11744
+ }
11745
+ const x = y.left;
11746
+ y.left = x.right;
11747
+ if (this.isRealNode(x.right)) {
11748
+ x.right.parent = y;
11749
+ }
11750
+ x.parent = y.parent;
11751
+ if (!y.parent) {
11752
+ this._setRoot(x);
11753
+ } else if (y === y.parent.left) {
11754
+ y.parent.left = x;
11755
+ } else {
11756
+ y.parent.right = x;
11757
+ }
11758
+ x.right = y;
11759
+ y.parent = x;
12000
11760
  }
12001
11761
  };
12002
11762
 
@@ -12403,6 +12163,9 @@ var dataStructureTyped = (() => {
12403
12163
  * @returns the sum of the count property of all nodes in the tree.
12404
12164
  */
12405
12165
  get count() {
12166
+ return this._count;
12167
+ }
12168
+ getMutableCount() {
12406
12169
  let sum = 0;
12407
12170
  this.dfs((node) => sum += node.count);
12408
12171
  return sum;
@@ -12495,14 +12258,14 @@ var dataStructureTyped = (() => {
12495
12258
  */
12496
12259
  add(keyOrNodeOrEntry, value, count = 1) {
12497
12260
  const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value, count);
12498
- if (newNode === void 0)
12261
+ const orgCount = (newNode == null ? void 0 : newNode.count) || 0;
12262
+ const isSuccessAdded = super.add(newNode);
12263
+ if (isSuccessAdded) {
12264
+ this._count += orgCount;
12265
+ return true;
12266
+ } else {
12499
12267
  return false;
12500
- const orgNodeCount = (newNode == null ? void 0 : newNode.count) || 0;
12501
- const inserted = super.add(newNode);
12502
- if (inserted) {
12503
- this._count += orgNodeCount;
12504
12268
  }
12505
- return true;
12506
12269
  }
12507
12270
  /**
12508
12271
  * Time Complexity: O(log n)
@@ -12529,63 +12292,83 @@ var dataStructureTyped = (() => {
12529
12292
  * @returns an array of BinaryTreeDeleteResult<NODE> objects.
12530
12293
  */
12531
12294
  delete(identifier, callback = this._defaultOneParamCallback, ignoreCount = false) {
12532
- const deleteResults = [];
12533
12295
  if (identifier === null)
12534
- return deleteResults;
12535
- const deleteHelper = (node) => {
12536
- let targetNode = this._Sentinel;
12537
- let currentNode;
12538
- while (node !== this._Sentinel) {
12539
- if (node && callback(node) === identifier) {
12540
- targetNode = node;
12541
- }
12542
- if (node && identifier && callback(node) <= identifier) {
12543
- node = node.right;
12544
- } else {
12545
- node = node == null ? void 0 : node.left;
12546
- }
12547
- }
12548
- if (targetNode === this._Sentinel) {
12549
- return;
12296
+ return [];
12297
+ const results = [];
12298
+ const nodeToDelete = this.isRealNode(identifier) ? identifier : this.getNode(identifier, callback);
12299
+ if (!nodeToDelete) {
12300
+ return results;
12301
+ }
12302
+ let originalColor = nodeToDelete.color;
12303
+ let replacementNode;
12304
+ if (!this.isRealNode(nodeToDelete.left)) {
12305
+ replacementNode = nodeToDelete.right;
12306
+ if (ignoreCount || nodeToDelete.count <= 1) {
12307
+ this._transplant(nodeToDelete, nodeToDelete.right);
12308
+ this._count -= nodeToDelete.count;
12309
+ } else {
12310
+ nodeToDelete.count--;
12311
+ this._count--;
12312
+ results.push({ deleted: nodeToDelete, needBalanced: void 0 });
12313
+ return results;
12314
+ }
12315
+ } else if (!this.isRealNode(nodeToDelete.right)) {
12316
+ replacementNode = nodeToDelete.left;
12317
+ if (ignoreCount || nodeToDelete.count <= 1) {
12318
+ this._transplant(nodeToDelete, nodeToDelete.left);
12319
+ this._count -= nodeToDelete.count;
12320
+ } else {
12321
+ nodeToDelete.count--;
12322
+ this._count--;
12323
+ results.push({ deleted: nodeToDelete, needBalanced: void 0 });
12324
+ return results;
12550
12325
  }
12551
- if (ignoreCount || targetNode.count <= 1) {
12552
- let parentNode = targetNode;
12553
- let parentNodeOriginalColor = parentNode.color;
12554
- if (targetNode.left === this._Sentinel) {
12555
- currentNode = targetNode.right;
12556
- this._rbTransplant(targetNode, targetNode.right);
12557
- } else if (targetNode.right === this._Sentinel) {
12558
- currentNode = targetNode.left;
12559
- this._rbTransplant(targetNode, targetNode.left);
12326
+ } else {
12327
+ const successor = this.getLeftMost(nodeToDelete.right);
12328
+ if (successor) {
12329
+ originalColor = successor.color;
12330
+ replacementNode = successor.right;
12331
+ if (successor.parent === nodeToDelete) {
12332
+ if (this.isRealNode(replacementNode)) {
12333
+ replacementNode.parent = successor;
12334
+ }
12560
12335
  } else {
12561
- parentNode = this.getLeftMost(targetNode.right);
12562
- parentNodeOriginalColor = parentNode.color;
12563
- currentNode = parentNode.right;
12564
- if (parentNode.parent === targetNode) {
12565
- currentNode.parent = parentNode;
12336
+ if (ignoreCount || nodeToDelete.count <= 1) {
12337
+ this._transplant(successor, successor.right);
12338
+ this._count -= nodeToDelete.count;
12566
12339
  } else {
12567
- this._rbTransplant(parentNode, parentNode.right);
12568
- parentNode.right = targetNode.right;
12569
- parentNode.right.parent = parentNode;
12340
+ nodeToDelete.count--;
12341
+ this._count--;
12342
+ results.push({ deleted: nodeToDelete, needBalanced: void 0 });
12343
+ return results;
12344
+ }
12345
+ successor.right = nodeToDelete.right;
12346
+ if (this.isRealNode(successor.right)) {
12347
+ successor.right.parent = successor;
12570
12348
  }
12571
- this._rbTransplant(targetNode, parentNode);
12572
- parentNode.left = targetNode.left;
12573
- parentNode.left.parent = parentNode;
12574
- parentNode.color = targetNode.color;
12575
12349
  }
12576
- if (parentNodeOriginalColor === 0 /* BLACK */) {
12577
- this._fixDelete(currentNode);
12350
+ if (ignoreCount || nodeToDelete.count <= 1) {
12351
+ this._transplant(nodeToDelete, successor);
12352
+ this._count -= nodeToDelete.count;
12353
+ } else {
12354
+ nodeToDelete.count--;
12355
+ this._count--;
12356
+ results.push({ deleted: nodeToDelete, needBalanced: void 0 });
12357
+ return results;
12578
12358
  }
12579
- this._size--;
12580
- this._count -= targetNode.count;
12581
- deleteResults.push({ deleted: targetNode, needBalanced: void 0 });
12582
- } else {
12583
- targetNode.count--;
12584
- this._count--;
12359
+ successor.left = nodeToDelete.left;
12360
+ if (this.isRealNode(successor.left)) {
12361
+ successor.left.parent = successor;
12362
+ }
12363
+ successor.color = nodeToDelete.color;
12585
12364
  }
12586
- };
12587
- deleteHelper(this.root);
12588
- return deleteResults;
12365
+ }
12366
+ this._size--;
12367
+ if (originalColor === 0 /* BLACK */) {
12368
+ this._deleteFixup(replacementNode);
12369
+ }
12370
+ results.push({ deleted: nodeToDelete, needBalanced: void 0 });
12371
+ return results;
12589
12372
  }
12590
12373
  /**
12591
12374
  * Time Complexity: O(1)