data-structure-typed 0.9.16 → 1.12.10

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 (199) hide show
  1. package/.dependency-cruiser.js +449 -0
  2. package/.idea/data-structure-typed.iml +2 -0
  3. package/README.md +102 -1
  4. package/dist/data-structures/binary-tree/avl-tree.d.ts +57 -0
  5. package/dist/data-structures/binary-tree/avl-tree.js +57 -0
  6. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +27 -0
  7. package/dist/data-structures/binary-tree/binary-indexed-tree.js +27 -0
  8. package/dist/data-structures/binary-tree/binary-tree.d.ts +215 -9
  9. package/dist/data-structures/binary-tree/binary-tree.js +283 -15
  10. package/dist/data-structures/binary-tree/bst.d.ts +16 -0
  11. package/dist/data-structures/binary-tree/bst.js +12 -0
  12. package/dist/data-structures/binary-tree/segment-tree.d.ts +4 -0
  13. package/dist/data-structures/binary-tree/segment-tree.js +4 -0
  14. package/dist/data-structures/binary-tree/tree-multiset.d.ts +4 -0
  15. package/dist/data-structures/binary-tree/tree-multiset.js +4 -0
  16. package/dist/data-structures/graph/abstract-graph.d.ts +163 -13
  17. package/dist/data-structures/graph/abstract-graph.js +166 -12
  18. package/dist/data-structures/graph/directed-graph.d.ts +111 -1
  19. package/dist/data-structures/graph/directed-graph.js +122 -0
  20. package/dist/data-structures/graph/undirected-graph.d.ts +63 -0
  21. package/dist/data-structures/graph/undirected-graph.js +67 -0
  22. package/dist/data-structures/hash/coordinate-map.d.ts +32 -0
  23. package/dist/data-structures/hash/coordinate-map.js +32 -0
  24. package/dist/data-structures/hash/coordinate-set.d.ts +25 -0
  25. package/dist/data-structures/hash/coordinate-set.js +25 -0
  26. package/dist/data-structures/heap/heap.d.ts +26 -31
  27. package/dist/data-structures/heap/heap.js +23 -32
  28. package/dist/data-structures/heap/max-heap.d.ts +6 -1
  29. package/dist/data-structures/heap/max-heap.js +6 -1
  30. package/dist/data-structures/heap/min-heap.d.ts +7 -1
  31. package/dist/data-structures/heap/min-heap.js +7 -1
  32. package/dist/data-structures/index.d.ts +0 -1
  33. package/dist/data-structures/index.js +0 -1
  34. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +14 -6
  35. package/dist/data-structures/linked-list/doubly-linked-list.js +45 -8
  36. package/dist/data-structures/linked-list/singly-linked-list.d.ts +11 -8
  37. package/dist/data-structures/linked-list/singly-linked-list.js +4 -0
  38. package/dist/data-structures/matrix/matrix.d.ts +9 -0
  39. package/dist/data-structures/matrix/matrix.js +11 -0
  40. package/dist/data-structures/matrix/matrix2d.d.ts +82 -2
  41. package/dist/data-structures/matrix/matrix2d.js +82 -2
  42. package/dist/data-structures/matrix/navigator.d.ts +32 -0
  43. package/dist/data-structures/matrix/navigator.js +28 -0
  44. package/dist/data-structures/matrix/vector2d.d.ts +139 -15
  45. package/dist/data-structures/matrix/vector2d.js +139 -15
  46. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +8 -0
  47. package/dist/data-structures/priority-queue/max-priority-queue.js +8 -0
  48. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +8 -0
  49. package/dist/data-structures/priority-queue/min-priority-queue.js +8 -0
  50. package/dist/data-structures/priority-queue/priority-queue.d.ts +130 -0
  51. package/dist/data-structures/priority-queue/priority-queue.js +126 -0
  52. package/dist/data-structures/queue/deque.d.ts +69 -0
  53. package/dist/data-structures/queue/deque.js +69 -0
  54. package/dist/data-structures/queue/queue.d.ts +30 -32
  55. package/dist/data-structures/queue/queue.js +30 -32
  56. package/dist/data-structures/stack/stack.d.ts +26 -31
  57. package/dist/data-structures/stack/stack.js +26 -31
  58. package/dist/data-structures/trie/trie.d.ts +26 -6
  59. package/dist/data-structures/trie/trie.js +26 -7
  60. package/dist/data-structures/types/avl-tree.d.ts +1 -1
  61. package/dist/data-structures/types/binary-tree.d.ts +1 -1
  62. package/dist/data-structures/types/index.d.ts +1 -1
  63. package/dist/data-structures/types/index.js +1 -1
  64. package/dist/data-structures/types/singly-linked-list.d.ts +1 -5
  65. package/dist/data-structures/types/tree-multiset.d.ts +1 -1
  66. package/dist/{data-structures → utils}/trampoline.d.ts +5 -7
  67. package/dist/utils/types/index.d.ts +1 -0
  68. package/dist/utils/types/index.js +17 -0
  69. package/dist/{data-structures → utils}/types/utils.d.ts +8 -0
  70. package/dist/{data-structures → utils}/types/utils.js +1 -0
  71. package/dist/utils/utils.d.ts +1 -1
  72. package/docs/.nojekyll +1 -0
  73. package/docs/assets/highlight.css +85 -0
  74. package/docs/assets/main.js +58 -0
  75. package/docs/assets/search.js +1 -0
  76. package/docs/assets/style.css +1367 -0
  77. package/docs/classes/AVLTree.html +2046 -0
  78. package/docs/classes/AVLTreeNode.html +423 -0
  79. package/docs/classes/AaTree.html +117 -0
  80. package/docs/classes/AbstractEdge.html +198 -0
  81. package/docs/classes/AbstractGraph.html +891 -0
  82. package/docs/classes/AbstractVertex.html +164 -0
  83. package/docs/classes/ArrayDeque.html +384 -0
  84. package/docs/classes/BST.html +1893 -0
  85. package/docs/classes/BSTNode.html +425 -0
  86. package/docs/classes/BTree.html +117 -0
  87. package/docs/classes/BinaryIndexedTree.html +244 -0
  88. package/docs/classes/BinaryTree.html +1754 -0
  89. package/docs/classes/BinaryTreeNode.html +396 -0
  90. package/docs/classes/Character.html +165 -0
  91. package/docs/classes/CoordinateMap.html +394 -0
  92. package/docs/classes/CoordinateSet.html +355 -0
  93. package/docs/classes/Deque.html +617 -0
  94. package/docs/classes/DirectedEdge.html +247 -0
  95. package/docs/classes/DirectedGraph.html +1207 -0
  96. package/docs/classes/DirectedVertex.html +154 -0
  97. package/docs/classes/DoublyLinkedList.html +619 -0
  98. package/docs/classes/DoublyLinkedListNode.html +160 -0
  99. package/docs/classes/Heap.html +315 -0
  100. package/docs/classes/Matrix2D.html +447 -0
  101. package/docs/classes/MatrixNTI2D.html +181 -0
  102. package/docs/classes/MaxHeap.html +325 -0
  103. package/docs/classes/MaxPriorityQueue.html +668 -0
  104. package/docs/classes/MinHeap.html +326 -0
  105. package/docs/classes/MinPriorityQueue.html +668 -0
  106. package/docs/classes/Navigator.html +285 -0
  107. package/docs/classes/ObjectDeque.html +289 -0
  108. package/docs/classes/PriorityQueue.html +643 -0
  109. package/docs/classes/Queue.html +337 -0
  110. package/docs/classes/RBTree.html +117 -0
  111. package/docs/classes/SegmentTree.html +234 -0
  112. package/docs/classes/SegmentTreeNode.html +302 -0
  113. package/docs/classes/SinglyLinkedList.html +1035 -0
  114. package/docs/classes/SinglyLinkedListNode.html +304 -0
  115. package/docs/classes/SplayTree.html +117 -0
  116. package/docs/classes/Stack.html +313 -0
  117. package/docs/classes/TreeMultiSet.html +1897 -0
  118. package/docs/classes/Trie.html +317 -0
  119. package/docs/classes/TrieNode.html +221 -0
  120. package/docs/classes/TwoThreeTree.html +117 -0
  121. package/docs/classes/UndirectedEdge.html +220 -0
  122. package/docs/classes/UndirectedGraph.html +1006 -0
  123. package/docs/classes/UndirectedVertex.html +154 -0
  124. package/docs/classes/Vector2D.html +746 -0
  125. package/docs/enums/CP.html +126 -0
  126. package/docs/enums/FamilyPosition.html +126 -0
  127. package/docs/enums/LoopType.html +119 -0
  128. package/docs/index.html +288 -0
  129. package/docs/modules.html +146 -0
  130. package/jest.config.js +5 -0
  131. package/package.json +19 -11
  132. package/rename_clear_files.sh +29 -0
  133. package/src/assets/complexities-diff.jpg +0 -0
  134. package/src/assets/data-structure-complexities.jpg +0 -0
  135. package/src/data-structures/binary-tree/avl-tree.ts +57 -0
  136. package/src/data-structures/binary-tree/binary-indexed-tree.ts +27 -0
  137. package/src/data-structures/binary-tree/binary-tree.ts +288 -18
  138. package/src/data-structures/binary-tree/bst.ts +16 -1
  139. package/src/data-structures/binary-tree/segment-tree.ts +5 -0
  140. package/src/data-structures/binary-tree/tree-multiset.ts +4 -0
  141. package/src/data-structures/diagrams/README.md +5 -7
  142. package/src/data-structures/graph/abstract-graph.ts +167 -15
  143. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  144. package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  145. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  146. package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  147. package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  148. package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  149. package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  150. package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  151. package/src/data-structures/graph/diagrams/mst.jpg +0 -0
  152. package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  153. package/src/data-structures/graph/directed-graph.ts +127 -5
  154. package/src/data-structures/graph/undirected-graph.ts +67 -1
  155. package/src/data-structures/hash/coordinate-map.ts +32 -0
  156. package/src/data-structures/hash/coordinate-set.ts +25 -0
  157. package/src/data-structures/heap/heap.ts +27 -32
  158. package/src/data-structures/heap/max-heap.ts +6 -1
  159. package/src/data-structures/heap/min-heap.ts +7 -1
  160. package/src/data-structures/index.ts +0 -1
  161. package/src/data-structures/linked-list/doubly-linked-list.ts +49 -8
  162. package/src/data-structures/linked-list/singly-linked-list.ts +39 -8
  163. package/src/data-structures/matrix/matrix.ts +11 -0
  164. package/src/data-structures/matrix/matrix2d.ts +82 -2
  165. package/src/data-structures/matrix/navigator.ts +32 -0
  166. package/src/data-structures/matrix/vector2d.ts +139 -15
  167. package/src/data-structures/priority-queue/max-priority-queue.ts +8 -0
  168. package/src/data-structures/priority-queue/min-priority-queue.ts +8 -0
  169. package/src/data-structures/priority-queue/priority-queue.ts +130 -0
  170. package/src/data-structures/queue/deque.ts +69 -0
  171. package/src/data-structures/queue/queue.ts +30 -32
  172. package/src/data-structures/stack/stack.ts +26 -31
  173. package/src/data-structures/trie/trie.ts +26 -7
  174. package/src/data-structures/types/avl-tree.ts +1 -1
  175. package/src/data-structures/types/binary-tree.ts +1 -1
  176. package/src/data-structures/types/bst.ts +1 -1
  177. package/src/data-structures/types/index.ts +1 -1
  178. package/src/data-structures/types/navigator.ts +1 -0
  179. package/src/data-structures/types/singly-linked-list.ts +1 -15
  180. package/src/data-structures/types/tree-multiset.ts +1 -1
  181. package/src/{data-structures → utils}/trampoline.ts +6 -6
  182. package/src/utils/types/index.ts +1 -0
  183. package/src/{data-structures → utils}/types/utils.ts +7 -0
  184. package/src/utils/utils.ts +12 -2
  185. package/tests/unit/data-structures/binary-tree/bst.test.ts +185 -0
  186. package/tests/unit/data-structures/graph/abstract-graph.ts +0 -0
  187. package/tests/unit/data-structures/graph/directed-graph.test.ts +71 -0
  188. package/tests/unit/data-structures/graph/index.ts +3 -0
  189. package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
  190. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
  191. package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
  192. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
  193. package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
  194. package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
  195. package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
  196. package/src/data-structures/graph/diagrams/edge-list.png +0 -0
  197. package/src/data-structures/graph/diagrams/max-flow.png +0 -0
  198. package/src/data-structures/graph/diagrams/mst.png +0 -0
  199. /package/dist/{data-structures → utils}/trampoline.js +0 -0
@@ -27,6 +27,10 @@ var __values = (this && this.__values) || function(o) {
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.AVLTree = exports.AVLTreeNode = void 0;
30
+ /**
31
+ * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
32
+ * @license MIT
33
+ */
30
34
  var bst_1 = require("./bst");
31
35
  var AVLTreeNode = /** @class */ (function (_super) {
32
36
  __extends(AVLTreeNode, _super);
@@ -47,12 +51,34 @@ var AVLTree = /** @class */ (function (_super) {
47
51
  AVLTree.prototype.createNode = function (id, val, count) {
48
52
  return new AVLTreeNode(id, val, count);
49
53
  };
54
+ /**
55
+ * The function overrides the put method of a Binary Search Tree to insert a node with a given id and value, and then
56
+ * balances the tree.
57
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to put or
58
+ * update in the AVL tree.
59
+ * @param {T | null} val - The `val` parameter represents the value that you want to assign to the node with the given
60
+ * `id`. It can be of type `T` (the generic type) or `null`.
61
+ * @param {number} [count] - The `count` parameter is an optional parameter of type `number`. It represents the number
62
+ * of times the value `val` should be inserted into the AVL tree. If the `count` parameter is not provided, it defaults
63
+ * to `1`, indicating that the value should be inserted once.
64
+ * @returns The method is returning either an AVLTreeNode<T> object or null.
65
+ */
50
66
  AVLTree.prototype.put = function (id, val, count) {
51
67
  var inserted = _super.prototype.put.call(this, id, val, count);
52
68
  if (inserted)
53
69
  this.balancePath(inserted);
54
70
  return inserted;
55
71
  };
72
+ /**
73
+ * The function overrides the remove method of the Binary Search Tree class, performs the removal operation, and
74
+ * then balances the tree if necessary.
75
+ * @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node that needs to be
76
+ * removed from the AVL tree.
77
+ * @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean parameter that
78
+ * determines whether the left sum of all nodes in the AVL tree should be updated after removing a node. If
79
+ * `isUpdateAllLeftSum` is set to `true`, the left sum of all nodes will be recalculated.
80
+ * @returns The method is returning an array of `AVLTreeDeleted<T>` objects.
81
+ */
56
82
  AVLTree.prototype.remove = function (id, isUpdateAllLeftSum) {
57
83
  var e_1, _a;
58
84
  var deletedResults = _super.prototype.remove.call(this, id, isUpdateAllLeftSum);
@@ -73,6 +99,12 @@ var AVLTree = /** @class */ (function (_super) {
73
99
  }
74
100
  return deletedResults;
75
101
  };
102
+ /**
103
+ * The balance factor of a given AVL tree node is calculated by subtracting the height of its left subtree from the
104
+ * height of its right subtree.
105
+ * @param node - The parameter "node" is of type AVLTreeNode<T>, which represents a node in an AVL tree.
106
+ * @returns The balance factor of the given AVL tree node.
107
+ */
76
108
  AVLTree.prototype.balanceFactor = function (node) {
77
109
  if (!node.right) // node has no right subtree
78
110
  return -node.height;
@@ -81,6 +113,10 @@ var AVLTree = /** @class */ (function (_super) {
81
113
  else
82
114
  return node.right.height - node.left.height;
83
115
  };
116
+ /**
117
+ * The function updates the height of a node in an AVL tree based on the heights of its left and right subtrees.
118
+ * @param node - The parameter `node` is an AVLTreeNode object, which represents a node in an AVL tree.
119
+ */
84
120
  AVLTree.prototype.updateHeight = function (node) {
85
121
  if (!node.left && !node.right) // node is a leaf
86
122
  node.height = 0;
@@ -94,6 +130,11 @@ var AVLTree = /** @class */ (function (_super) {
94
130
  else
95
131
  node.height = 1 + Math.max(node.right.height, node.left.height);
96
132
  };
133
+ /**
134
+ * The `balancePath` function balances the AVL tree by performing appropriate rotations based on the balance factor of
135
+ * each node in the path from the given node to the root.
136
+ * @param node - The `node` parameter is an AVLTreeNode object, which represents a node in an AVL tree.
137
+ */
97
138
  AVLTree.prototype.balancePath = function (node) {
98
139
  var path = this.getPathToRoot(node);
99
140
  for (var i = path.length - 1; i >= 0; i--) {
@@ -122,6 +163,10 @@ var AVLTree = /** @class */ (function (_super) {
122
163
  }
123
164
  }
124
165
  };
166
+ /**
167
+ * The `balanceLL` function performs a left-left rotation on an AVL tree to balance it.
168
+ * @param A - The parameter A is an AVLTreeNode object.
169
+ */
125
170
  AVLTree.prototype.balanceLL = function (A) {
126
171
  var parentOfA = A.parent;
127
172
  var B = A.left; // A is left-heavy and B is left-heavy
@@ -152,6 +197,10 @@ var AVLTree = /** @class */ (function (_super) {
152
197
  if (B)
153
198
  this.updateHeight(B);
154
199
  };
200
+ /**
201
+ * The `balanceLR` function performs a left-right rotation to balance an AVL tree.
202
+ * @param A - A is an AVLTreeNode object.
203
+ */
155
204
  AVLTree.prototype.balanceLR = function (A) {
156
205
  var parentOfA = A.parent;
157
206
  var B = A.left; // A is left-heavy
@@ -197,6 +246,10 @@ var AVLTree = /** @class */ (function (_super) {
197
246
  B && this.updateHeight(B);
198
247
  C && this.updateHeight(C);
199
248
  };
249
+ /**
250
+ * The `balanceRR` function performs a right-right rotation on an AVL tree to balance it.
251
+ * @param A - The parameter A is an AVLTreeNode object.
252
+ */
200
253
  AVLTree.prototype.balanceRR = function (A) {
201
254
  var parentOfA = A.parent;
202
255
  var B = A.right; // A is right-heavy and B is right-heavy
@@ -228,6 +281,10 @@ var AVLTree = /** @class */ (function (_super) {
228
281
  this.updateHeight(A);
229
282
  B && this.updateHeight(B);
230
283
  };
284
+ /**
285
+ * The `balanceRL` function performs a right-left rotation to balance an AVL tree.
286
+ * @param A - A is an AVLTreeNode object.
287
+ */
231
288
  AVLTree.prototype.balanceRL = function (A) {
232
289
  var parentOfA = A.parent;
233
290
  var B = A.right; // A is right-heavy
@@ -1,8 +1,35 @@
1
+ /**
2
+ * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
+ * @license MIT
4
+ */
1
5
  export declare class BinaryIndexedTree {
2
6
  private readonly _sumTree;
3
7
  constructor(n: number);
4
8
  static lowBit(x: number): number;
9
+ /**
10
+ * The update function updates the values in a binary indexed tree by adding a delta value to the specified index and
11
+ * its ancestors.
12
+ * @param {number} i - The parameter `i` represents the index of the element in the `_sumTree` array that needs to be
13
+ * updated.
14
+ * @param {number} delta - The "delta" parameter represents the change in value that needs to be added to the element
15
+ * at index "i" in the "_sumTree" array.
16
+ */
5
17
  update(i: number, delta: number): void;
18
+ /**
19
+ * The function calculates the prefix sum of an array using a binary indexed tree.
20
+ * @param {number} i - The parameter "i" in the function "getPrefixSum" represents the index of the element in the
21
+ * array for which we want to calculate the prefix sum.
22
+ * @returns The function `getPrefixSum` returns the prefix sum of the elements in the binary indexed tree up to index
23
+ * `i`.
24
+ */
6
25
  getPrefixSum(i: number): number;
26
+ /**
27
+ * The function `getRangeSum` calculates the sum of a range of numbers in an array.
28
+ * @param {number} start - The start parameter is the starting index of the range for which we want to calculate the
29
+ * sum.
30
+ * @param {number} end - The "end" parameter represents the ending index of the range for which we want to calculate
31
+ * the sum.
32
+ * @returns the sum of the elements in the range specified by the start and end indices.
33
+ */
7
34
  getRangeSum(start: number, end: number): number;
8
35
  }
@@ -1,6 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BinaryIndexedTree = void 0;
4
+ /**
5
+ * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT
7
+ */
4
8
  var BinaryIndexedTree = /** @class */ (function () {
5
9
  function BinaryIndexedTree(n) {
6
10
  this._sumTree = new Array(n + 1).fill(0);
@@ -8,12 +12,27 @@ var BinaryIndexedTree = /** @class */ (function () {
8
12
  BinaryIndexedTree.lowBit = function (x) {
9
13
  return x & (-x);
10
14
  };
15
+ /**
16
+ * The update function updates the values in a binary indexed tree by adding a delta value to the specified index and
17
+ * its ancestors.
18
+ * @param {number} i - The parameter `i` represents the index of the element in the `_sumTree` array that needs to be
19
+ * updated.
20
+ * @param {number} delta - The "delta" parameter represents the change in value that needs to be added to the element
21
+ * at index "i" in the "_sumTree" array.
22
+ */
11
23
  BinaryIndexedTree.prototype.update = function (i, delta) {
12
24
  while (i < this._sumTree.length) {
13
25
  this._sumTree[i] += delta;
14
26
  i += BinaryIndexedTree.lowBit(i);
15
27
  }
16
28
  };
29
+ /**
30
+ * The function calculates the prefix sum of an array using a binary indexed tree.
31
+ * @param {number} i - The parameter "i" in the function "getPrefixSum" represents the index of the element in the
32
+ * array for which we want to calculate the prefix sum.
33
+ * @returns The function `getPrefixSum` returns the prefix sum of the elements in the binary indexed tree up to index
34
+ * `i`.
35
+ */
17
36
  BinaryIndexedTree.prototype.getPrefixSum = function (i) {
18
37
  var sum = 0;
19
38
  while (i > 0) {
@@ -22,6 +41,14 @@ var BinaryIndexedTree = /** @class */ (function () {
22
41
  }
23
42
  return sum;
24
43
  };
44
+ /**
45
+ * The function `getRangeSum` calculates the sum of a range of numbers in an array.
46
+ * @param {number} start - The start parameter is the starting index of the range for which we want to calculate the
47
+ * sum.
48
+ * @param {number} end - The "end" parameter represents the ending index of the range for which we want to calculate
49
+ * the sum.
50
+ * @returns the sum of the elements in the range specified by the start and end indices.
51
+ */
25
52
  BinaryIndexedTree.prototype.getRangeSum = function (start, end) {
26
53
  if (!(0 <= start && start <= end && end <= this._sumTree.length))
27
54
  throw 'Index out of bounds';
@@ -1,3 +1,7 @@
1
+ /**
2
+ * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
+ * @license MIT
4
+ */
1
5
  import type { BinaryTreeDeleted, BinaryTreeNodeId, BinaryTreeNodePropertyName, DFSOrderPattern, NodeOrPropertyName, ResultsByProperty } from '../types';
2
6
  export declare enum FamilyPosition {
3
7
  root = 0,
@@ -47,48 +51,211 @@ export declare class BinaryTree<T> {
47
51
  private readonly _autoIncrementId;
48
52
  private _maxId;
49
53
  private readonly _isDuplicatedVal;
54
+ /**
55
+ * The constructor function accepts an optional options object and sets the values of loopType, autoIncrementId, and
56
+ * isDuplicatedVal based on the provided options.
57
+ * @param [options] - An optional object that can contain the following properties:
58
+ */
50
59
  constructor(options?: {
51
60
  loopType?: LoopType;
52
61
  autoIncrementId?: boolean;
53
62
  isDuplicatedVal?: boolean;
54
63
  });
55
64
  protected _root: BinaryTreeNode<T> | null;
56
- protected get root(): BinaryTreeNode<T> | null;
65
+ get root(): BinaryTreeNode<T> | null;
57
66
  protected set root(v: BinaryTreeNode<T> | null);
58
67
  protected _size: number;
59
- protected get size(): number;
68
+ get size(): number;
60
69
  protected set size(v: number);
61
70
  protected _count: number;
62
- protected get count(): number;
71
+ get count(): number;
63
72
  protected set count(v: number);
64
- getCount(): number;
73
+ /**
74
+ * The function creates a new binary tree node with the given id, value, and count, or returns null if the value is
75
+ * null.
76
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is of type
77
+ * `BinaryTreeNodeId`, which could be a string or a number, depending on how you want to identify your nodes.
78
+ * @param {T | null} val - The `val` parameter represents the value to be stored in the binary tree node. It can be of
79
+ * any type `T` or `null`.
80
+ * @param {number} [count] - The count parameter is an optional parameter that represents the number of occurrences of
81
+ * the value in the binary tree node. It is of type number.
82
+ * @returns The function `createNode` returns a `BinaryTreeNode<T>` object if the `val` parameter is not null.
83
+ * Otherwise, it returns null.
84
+ */
65
85
  createNode(id: BinaryTreeNodeId, val: T | null, count?: number): BinaryTreeNode<T> | null;
86
+ /**
87
+ * The clear function resets the state of an object by setting its properties to their initial values.
88
+ */
66
89
  clear(): void;
90
+ /**
91
+ * The function checks if the size of an object is equal to zero and returns a boolean value.
92
+ * @returns A boolean value indicating whether the size of the object is 0 or not.
93
+ */
67
94
  isEmpty(): boolean;
68
- insertTo({ newNode, parent }: {
69
- newNode: BinaryTreeNode<T> | null;
70
- parent: BinaryTreeNode<T>;
71
- }): BinaryTreeNode<T> | null | undefined;
95
+ /**
96
+ * The function inserts a new node into a binary tree as the left or right child of a given parent node.
97
+ * @param {BinaryTreeNode<T> | null} newNode - The `newNode` parameter is an instance of the `BinaryTreeNode` class or
98
+ * `null`. It represents the node that needs to be inserted into the binary tree.
99
+ * @param parent - The `parent` parameter is a BinaryTreeNode object representing the parent node to which the new node
100
+ * will be inserted as a child.
101
+ * @returns The method returns the newly inserted node, either as the left child or the right child of the parent node.
102
+ */
103
+ putTo(newNode: BinaryTreeNode<T> | null, parent: BinaryTreeNode<T>): BinaryTreeNode<T> | null | undefined;
104
+ /**
105
+ * The `put` function inserts a new node with a given ID and value into a binary tree, updating the count if the node
106
+ * already exists.
107
+ * @param {BinaryTreeNodeId} id - The id parameter is the identifier of the binary tree node. It is used to uniquely
108
+ * identify each node in the binary tree.
109
+ * @param {T} val - The value to be inserted into the binary tree.
110
+ * @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the
111
+ * value should be inserted into the binary tree. If not provided, it defaults to 1.
112
+ * @returns The function `put` returns a `BinaryTreeNode<T>` object if a new node is inserted, or `null` if no new node
113
+ * is inserted, or `undefined` if the insertion fails.
114
+ */
72
115
  put(id: BinaryTreeNodeId, val: T, count?: number): BinaryTreeNode<T> | null | undefined;
116
+ /**
117
+ * The `insertMany` function inserts multiple items into a binary tree and returns an array of the inserted nodes or
118
+ * null/undefined values.
119
+ * @param {T[] | BinaryTreeNode<T>[]} data - The `data` parameter can be either an array of elements of type `T` or an
120
+ * array of `BinaryTreeNode<T>` objects.
121
+ * @returns The function `insertMany` returns an array of `BinaryTreeNode<T>`, `null`, or `undefined` values.
122
+ */
73
123
  insertMany(data: T[] | BinaryTreeNode<T>[]): (BinaryTreeNode<T> | null | undefined)[];
124
+ /**
125
+ * The `fill` function clears the current data and inserts new data, returning a boolean indicating if the insertion
126
+ * was successful.
127
+ * @param {T[] | BinaryTreeNode<T>[]} data - The `data` parameter can be either an array of elements of type `T` or an
128
+ * array of `BinaryTreeNode<T>` objects.
129
+ * @returns The method is returning a boolean value.
130
+ */
74
131
  fill(data: T[] | BinaryTreeNode<T>[]): boolean;
132
+ /**
133
+ * The function removes a node from a binary tree and returns information about the deleted node.
134
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that you want to remove.
135
+ * It is of type `BinaryTreeNodeId`.
136
+ * @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
137
+ * whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
138
+ * not be decremented and the overall count of the binary tree will not be updated. If `
139
+ * @returns An array of objects is being returned. Each object in the array has two properties: "deleted" and
140
+ * "needBalanced". The "deleted" property contains the deleted node or undefined if no node was deleted. The
141
+ * "needBalanced" property is always null.
142
+ */
75
143
  remove(id: BinaryTreeNodeId, ignoreCount?: boolean): BinaryTreeDeleted<T>[];
144
+ /**
145
+ * The function calculates the depth of a binary tree node by traversing its parent nodes.
146
+ * @param node - BinaryTreeNode<T> - This is the node for which we want to calculate the depth. It is a generic type,
147
+ * meaning it can represent any type of data that we want to store in the node.
148
+ * @returns The depth of the given binary tree node.
149
+ */
76
150
  getDepth(node: BinaryTreeNode<T>): number;
151
+ /**
152
+ * The `getHeight` function calculates the maximum height of a binary tree using either a recursive or iterative
153
+ * approach.
154
+ * @param {BinaryTreeNode<T> | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type
155
+ * `BinaryTreeNode<T> | null`. It represents the starting node from which to calculate the height of the binary tree.
156
+ * If no value is provided for `beginRoot`, the function will use the `root` property of the class instance as
157
+ * @returns the height of the binary tree.
158
+ */
77
159
  getHeight(beginRoot?: BinaryTreeNode<T> | null): number;
160
+ /**
161
+ * The `getMinHeight` function calculates the minimum height of a binary tree using either a recursive or iterative
162
+ * approach.
163
+ * @param {BinaryTreeNode<T> | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type
164
+ * `BinaryTreeNode<T> | null`. It represents the starting node from which to calculate the minimum height of the binary
165
+ * tree. If no value is provided for `beginRoot`, the function will use the root node of the binary tree.
166
+ * @returns The function `getMinHeight` returns the minimum height of the binary tree.
167
+ */
78
168
  getMinHeight(beginRoot?: BinaryTreeNode<T> | null): number;
169
+ /**
170
+ * The function checks if a binary tree is balanced by comparing the minimum height and the maximum height of the tree.
171
+ * @param {BinaryTreeNode<T> | null} [beginRoot] - The `beginRoot` parameter is the root node of a binary tree. It is
172
+ * of type `BinaryTreeNode<T> | null`, which means it can either be a `BinaryTreeNode` object or `null`.
173
+ * @returns The method is returning a boolean value.
174
+ */
79
175
  isBalanced(beginRoot?: BinaryTreeNode<T> | null): boolean;
176
+ /**
177
+ * The function `getNodes` returns an array of binary tree nodes that match a given property value, with options for
178
+ * searching recursively or iteratively.
179
+ * @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
180
+ * generic type `T`. It represents the property of the binary tree node that you want to search for.
181
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
182
+ * specifies the property name to use when searching for nodes. If not provided, it defaults to 'id'.
183
+ * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
184
+ * return only one node that matches the `nodeProperty` or `propertyName` criteria. If `onlyOne` is set to `true`, the
185
+ * function will stop traversing the tree and return the first matching node. If `
186
+ * @returns The function `getNodes` returns an array of `BinaryTreeNode<T> | null | undefined` objects.
187
+ */
80
188
  getNodes(nodeProperty: BinaryTreeNodeId | T, propertyName?: BinaryTreeNodePropertyName, onlyOne?: boolean): (BinaryTreeNode<T> | null | undefined)[];
189
+ /**
190
+ * The function checks if a binary tree node has a specific property or if any node in the tree has a specific
191
+ * property.
192
+ * @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
193
+ * generic type `T`. It represents the property of a binary tree node that you want to check.
194
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
195
+ * specifies the name of the property to check for in the nodes.
196
+ * @returns a boolean value.
197
+ */
81
198
  has(nodeProperty: BinaryTreeNodeId | T, propertyName?: BinaryTreeNodePropertyName): boolean;
199
+ /**
200
+ * The function returns the first binary tree node that matches the given property name and value, or null if no match
201
+ * is found.
202
+ * @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
203
+ * generic type `T`. It represents the property of the binary tree node that you want to search for.
204
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
205
+ * specifies the property of the binary tree node to search for. If not provided, it defaults to `'id'`.
206
+ * @returns a BinaryTreeNode object or null.
207
+ */
82
208
  get(nodeProperty: BinaryTreeNodeId | T, propertyName?: BinaryTreeNodePropertyName): BinaryTreeNode<T> | null;
209
+ /**
210
+ * The function getPathToRoot returns an array of BinaryTreeNode objects representing the path from a given node to the
211
+ * root of a binary tree.
212
+ * @param node - The `node` parameter is a BinaryTreeNode object.
213
+ * @returns The function `getPathToRoot` returns an array of `BinaryTreeNode<T>` objects, representing the path from
214
+ * the given `node` to the root of the binary tree.
215
+ */
83
216
  getPathToRoot(node: BinaryTreeNode<T>): BinaryTreeNode<T>[];
84
- getRoot(): BinaryTreeNode<T> | null;
85
217
  getLeftMost(): BinaryTreeNode<T> | null;
86
218
  getLeftMost(node: BinaryTreeNode<T>): BinaryTreeNode<T>;
87
219
  getRightMost(): BinaryTreeNode<T> | null;
88
220
  getRightMost(node: BinaryTreeNode<T>): BinaryTreeNode<T>;
221
+ /**
222
+ * The `isBST` function checks if a binary tree is a binary search tree.
223
+ * @param {BinaryTreeNode<T> | null} [node] - The `node` parameter is an optional parameter of type `BinaryTreeNode<T>
224
+ * | null`. It represents the root node of the binary search tree (BST) that we want to check for validity. If no node
225
+ * is provided, the function will default to using the root node of the BST instance that
226
+ * @returns The `isBST` function returns a boolean value. It returns `true` if the binary tree is a valid binary search
227
+ * tree, and `false` otherwise.
228
+ */
89
229
  isBST(node?: BinaryTreeNode<T> | null): boolean;
230
+ /**
231
+ * The function calculates the size and count of a subtree in a binary tree using either recursive or iterative
232
+ * traversal.
233
+ * @param {BinaryTreeNode<T> | null | undefined} subTreeRoot - The `subTreeRoot` parameter is the root node of a binary
234
+ * tree.
235
+ * @returns The function `getSubTreeSizeAndCount` returns an array `[number, number]`. The first element of the array
236
+ * represents the size of the subtree, and the second element represents the count of the nodes in the subtree.
237
+ */
90
238
  getSubTreeSizeAndCount(subTreeRoot: BinaryTreeNode<T> | null | undefined): [number, number];
239
+ /**
240
+ * The function `subTreeSum` calculates the sum of a specified property in a binary tree, either recursively or
241
+ * iteratively.
242
+ * @param subTreeRoot - The subTreeRoot parameter is the root node of the subtree for which you want to calculate the
243
+ * sum.
244
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
245
+ * specifies the property of the `BinaryTreeNode` object to use for calculating the sum. If `propertyName` is not
246
+ * provided, it defaults to `'val'`.
247
+ * @returns a number, which is the sum of the values of the nodes in the subtree rooted at `subTreeRoot`.
248
+ */
91
249
  subTreeSum(subTreeRoot: BinaryTreeNode<T>, propertyName?: BinaryTreeNodePropertyName): number;
250
+ /**
251
+ * The function `subTreeAdd` adds a specified delta value to a property of each node in a binary tree.
252
+ * @param subTreeRoot - The `subTreeRoot` parameter is the root node of the subtree where the values will be modified.
253
+ * @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
254
+ * each node in the subtree should be increased or decreased.
255
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
256
+ * specifies the property of the `BinaryTreeNode` that should be modified. It defaults to `'id'` if not provided.
257
+ * @returns a boolean value, which is `true`.
258
+ */
92
259
  subTreeAdd(subTreeRoot: BinaryTreeNode<T>, delta: number, propertyName?: BinaryTreeNodePropertyName): boolean;
93
260
  BFS(): BinaryTreeNodeId[];
94
261
  BFS(nodeOrPropertyName: 'id'): BinaryTreeNodeId[];
@@ -115,14 +282,53 @@ export declare class BinaryTree<T> {
115
282
  listLevels(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'val'): T[][];
116
283
  listLevels(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'node'): BinaryTreeNode<T>[][];
117
284
  listLevels(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'count'): number[][];
285
+ /**
286
+ * The function returns the predecessor of a given node in a binary tree.
287
+ * @param node - The parameter `node` is a BinaryTreeNode object, representing a node in a binary tree.
288
+ * @returns the predecessor of the given node in a binary tree.
289
+ */
118
290
  getPredecessor(node: BinaryTreeNode<T>): BinaryTreeNode<T>;
119
291
  morris(): BinaryTreeNodeId[];
120
292
  morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
121
293
  morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): T[];
122
294
  morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): BinaryTreeNode<T>[];
123
295
  morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'count'): number[];
296
+ /**
297
+ * The function resets the values of several arrays used for tracking visited nodes and their properties.
298
+ */
124
299
  protected _resetResults(): void;
300
+ /**
301
+ * The function checks if a given property of a binary tree node matches a specified value, and if so, adds the node to
302
+ * a result array.
303
+ * @param cur - The current binary tree node that is being checked.
304
+ * @param {(BinaryTreeNode<T> | null | undefined)[]} result - An array that stores the matching nodes found during the
305
+ * traversal.
306
+ * @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter is the value that we are searching for in
307
+ * the binary tree nodes. It can be either the `id`, `count`, or `val` property of the node.
308
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
309
+ * specifies the property of the `BinaryTreeNode` object that you want to compare with the `nodeProperty` value. It can
310
+ * be one of the following values: 'id', 'count', or 'val'. If no `propertyName` is provided,
311
+ * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
312
+ * stop after finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set to
313
+ * `true`, the function will stop after finding the first matching node and return `true`. If `onlyOne
314
+ * @returns a boolean value indicating whether or not a node was pushed into the result array.
315
+ */
125
316
  protected _pushByPropertyNameStopOrNot(cur: BinaryTreeNode<T>, result: (BinaryTreeNode<T> | null | undefined)[], nodeProperty: BinaryTreeNodeId | T, propertyName?: BinaryTreeNodePropertyName, onlyOne?: boolean): boolean | undefined;
317
+ /**
318
+ * The function `_accumulatedByPropertyName` pushes a property value of a binary tree node into an array based on the
319
+ * provided property name or a default property name.
320
+ * @param node - The `node` parameter is of type `BinaryTreeNode<T>`, which represents a node in a binary tree.
321
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
322
+ * can be either a string representing a property name or a reference to a node object. If it is a string, it specifies
323
+ * the property name of the node that should be accumulated. If it is a node object, it specifies the node itself
324
+ */
126
325
  protected _accumulatedByPropertyName(node: BinaryTreeNode<T>, nodeOrPropertyName?: NodeOrPropertyName): void;
326
+ /**
327
+ * The function `_getResultByPropertyName` returns different results based on the provided property name or defaulting
328
+ * to 'id'.
329
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
330
+ * can accept a value of type `NodeOrPropertyName`.
331
+ * @returns The method returns an object of type `ResultsByProperty<T>`.
332
+ */
127
333
  protected _getResultByPropertyName(nodeOrPropertyName?: NodeOrPropertyName): ResultsByProperty<T>;
128
334
  }