data-structure-typed 0.8.18 → 1.12.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (287) hide show
  1. package/.dependency-cruiser.js +449 -0
  2. package/.idea/data-structure-typed.iml +2 -0
  3. package/.idea/modules.xml +1 -1
  4. package/README.md +298 -2
  5. package/dist/data-structures/binary-tree/aa-tree.js +5 -2
  6. package/dist/data-structures/binary-tree/avl-tree.d.ts +58 -5
  7. package/dist/data-structures/binary-tree/avl-tree.js +150 -46
  8. package/dist/data-structures/binary-tree/b-tree.js +5 -2
  9. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +28 -1
  10. package/dist/data-structures/binary-tree/binary-indexed-tree.js +41 -13
  11. package/dist/data-structures/binary-tree/binary-tree.d.ts +230 -36
  12. package/dist/data-structures/binary-tree/binary-tree.js +747 -369
  13. package/dist/data-structures/binary-tree/bst.d.ts +20 -8
  14. package/dist/data-structures/binary-tree/bst.js +164 -107
  15. package/dist/data-structures/binary-tree/rb-tree.js +5 -2
  16. package/dist/data-structures/binary-tree/segment-tree.d.ts +7 -3
  17. package/dist/data-structures/binary-tree/segment-tree.js +95 -61
  18. package/dist/data-structures/binary-tree/splay-tree.js +5 -2
  19. package/dist/data-structures/binary-tree/tree-multiset.d.ts +5 -5
  20. package/dist/data-structures/binary-tree/tree-multiset.js +35 -11
  21. package/dist/data-structures/binary-tree/two-three-tree.js +5 -2
  22. package/dist/data-structures/graph/abstract-graph.d.ts +168 -46
  23. package/dist/data-structures/graph/abstract-graph.js +712 -323
  24. package/dist/data-structures/graph/directed-graph.d.ts +114 -12
  25. package/dist/data-structures/graph/directed-graph.js +372 -128
  26. package/dist/data-structures/graph/undirected-graph.d.ts +67 -3
  27. package/dist/data-structures/graph/undirected-graph.js +233 -81
  28. package/dist/data-structures/hash/coordinate-map.d.ts +33 -1
  29. package/dist/data-structures/hash/coordinate-map.js +70 -20
  30. package/dist/data-structures/hash/coordinate-set.d.ts +25 -0
  31. package/dist/data-structures/hash/coordinate-set.js +58 -15
  32. package/dist/data-structures/hash/index.d.ts +5 -0
  33. package/dist/data-structures/hash/index.js +5 -0
  34. package/dist/data-structures/heap/heap.d.ts +26 -37
  35. package/dist/data-structures/heap/heap.js +56 -60
  36. package/dist/data-structures/heap/max-heap.d.ts +8 -2
  37. package/dist/data-structures/heap/max-heap.js +32 -9
  38. package/dist/data-structures/heap/min-heap.d.ts +9 -2
  39. package/dist/data-structures/heap/min-heap.js +33 -9
  40. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +14 -7
  41. package/dist/data-structures/linked-list/doubly-linked-list.js +101 -61
  42. package/dist/data-structures/linked-list/singly-linked-list.d.ts +18 -19
  43. package/dist/data-structures/linked-list/singly-linked-list.js +312 -174
  44. package/dist/data-structures/matrix/matrix.d.ts +9 -0
  45. package/dist/data-structures/matrix/matrix.js +19 -7
  46. package/dist/data-structures/matrix/matrix2d.d.ts +84 -4
  47. package/dist/data-structures/matrix/matrix2d.js +158 -61
  48. package/dist/data-structures/matrix/navigator.d.ts +34 -16
  49. package/dist/data-structures/matrix/navigator.js +65 -18
  50. package/dist/data-structures/matrix/vector2d.d.ts +153 -29
  51. package/dist/data-structures/matrix/vector2d.js +249 -102
  52. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +11 -2
  53. package/dist/data-structures/priority-queue/max-priority-queue.js +33 -8
  54. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -2
  55. package/dist/data-structures/priority-queue/min-priority-queue.js +33 -8
  56. package/dist/data-structures/priority-queue/priority-queue.d.ts +145 -21
  57. package/dist/data-structures/priority-queue/priority-queue.js +285 -116
  58. package/dist/data-structures/queue/deque.d.ts +69 -0
  59. package/dist/data-structures/queue/deque.js +151 -56
  60. package/dist/data-structures/queue/queue.d.ts +34 -37
  61. package/dist/data-structures/queue/queue.js +59 -61
  62. package/dist/data-structures/stack/stack.d.ts +29 -35
  63. package/dist/data-structures/stack/stack.js +51 -56
  64. package/dist/data-structures/trie/trie.d.ts +36 -6
  65. package/dist/data-structures/trie/trie.js +256 -83
  66. package/dist/data-structures/types/abstract-graph.d.ts +29 -0
  67. package/dist/data-structures/types/abstract-graph.js +2 -0
  68. package/dist/data-structures/types/avl-tree.d.ts +5 -0
  69. package/dist/data-structures/types/avl-tree.js +2 -0
  70. package/dist/data-structures/types/binary-tree.d.ts +16 -0
  71. package/dist/data-structures/types/binary-tree.js +2 -0
  72. package/dist/data-structures/types/bst.d.ts +7 -0
  73. package/dist/data-structures/types/bst.js +2 -0
  74. package/dist/data-structures/types/directed-graph.d.ts +10 -0
  75. package/dist/data-structures/types/directed-graph.js +2 -0
  76. package/dist/data-structures/types/doubly-linked-list.d.ts +1 -0
  77. package/dist/data-structures/types/doubly-linked-list.js +2 -0
  78. package/dist/data-structures/types/heap.d.ts +7 -0
  79. package/dist/data-structures/types/heap.js +2 -0
  80. package/dist/data-structures/types/index.d.ts +13 -0
  81. package/dist/data-structures/types/index.js +29 -0
  82. package/dist/data-structures/types/navigator.d.ts +14 -0
  83. package/dist/data-structures/types/navigator.js +2 -0
  84. package/dist/data-structures/types/priority-queue.d.ts +7 -0
  85. package/dist/data-structures/types/priority-queue.js +2 -0
  86. package/dist/data-structures/types/segment-tree.d.ts +1 -0
  87. package/dist/data-structures/types/segment-tree.js +2 -0
  88. package/dist/data-structures/types/singly-linked-list.js +2 -0
  89. package/dist/data-structures/types/tree-multiset.d.ts +5 -0
  90. package/dist/data-structures/types/tree-multiset.js +2 -0
  91. package/dist/utils/trampoline.d.ts +14 -0
  92. package/dist/utils/trampoline.js +130 -0
  93. package/dist/utils/types/index.js +17 -0
  94. package/dist/{types → utils}/types/utils.d.ts +15 -1
  95. package/dist/{types → utils/types}/utils.js +21 -19
  96. package/dist/{utils.d.ts → utils/utils.d.ts} +5 -22
  97. package/dist/utils/utils.js +651 -0
  98. package/docs/.nojekyll +1 -0
  99. package/docs/assets/highlight.css +85 -0
  100. package/docs/assets/main.js +58 -0
  101. package/docs/assets/search.js +1 -0
  102. package/docs/assets/style.css +1367 -0
  103. package/docs/classes/AVLTree.html +2046 -0
  104. package/docs/classes/AVLTreeNode.html +423 -0
  105. package/docs/classes/AaTree.html +117 -0
  106. package/docs/classes/AbstractEdge.html +198 -0
  107. package/docs/classes/AbstractGraph.html +891 -0
  108. package/docs/classes/AbstractVertex.html +164 -0
  109. package/docs/classes/ArrayDeque.html +384 -0
  110. package/docs/classes/BST.html +1893 -0
  111. package/docs/classes/BSTNode.html +425 -0
  112. package/docs/classes/BTree.html +117 -0
  113. package/docs/classes/BinaryIndexedTree.html +244 -0
  114. package/docs/classes/BinaryTree.html +1754 -0
  115. package/docs/classes/BinaryTreeNode.html +396 -0
  116. package/docs/classes/Character.html +165 -0
  117. package/docs/classes/CoordinateMap.html +394 -0
  118. package/docs/classes/CoordinateSet.html +355 -0
  119. package/docs/classes/Deque.html +617 -0
  120. package/docs/classes/DirectedEdge.html +247 -0
  121. package/docs/classes/DirectedGraph.html +1207 -0
  122. package/docs/classes/DirectedVertex.html +154 -0
  123. package/docs/classes/DoublyLinkedList.html +619 -0
  124. package/docs/classes/DoublyLinkedListNode.html +160 -0
  125. package/docs/classes/Heap.html +315 -0
  126. package/docs/classes/Matrix2D.html +447 -0
  127. package/docs/classes/MatrixNTI2D.html +181 -0
  128. package/docs/classes/MaxHeap.html +325 -0
  129. package/docs/classes/MaxPriorityQueue.html +668 -0
  130. package/docs/classes/MinHeap.html +326 -0
  131. package/docs/classes/MinPriorityQueue.html +668 -0
  132. package/docs/classes/Navigator.html +285 -0
  133. package/docs/classes/ObjectDeque.html +289 -0
  134. package/docs/classes/PriorityQueue.html +643 -0
  135. package/docs/classes/Queue.html +337 -0
  136. package/docs/classes/RBTree.html +117 -0
  137. package/docs/classes/SegmentTree.html +234 -0
  138. package/docs/classes/SegmentTreeNode.html +302 -0
  139. package/docs/classes/SinglyLinkedList.html +1035 -0
  140. package/docs/classes/SinglyLinkedListNode.html +304 -0
  141. package/docs/classes/SplayTree.html +117 -0
  142. package/docs/classes/Stack.html +313 -0
  143. package/docs/classes/TreeMultiSet.html +1897 -0
  144. package/docs/classes/Trie.html +317 -0
  145. package/docs/classes/TrieNode.html +221 -0
  146. package/docs/classes/TwoThreeTree.html +117 -0
  147. package/docs/classes/UndirectedEdge.html +220 -0
  148. package/docs/classes/UndirectedGraph.html +1006 -0
  149. package/docs/classes/UndirectedVertex.html +154 -0
  150. package/docs/classes/Vector2D.html +746 -0
  151. package/docs/enums/CP.html +126 -0
  152. package/docs/enums/FamilyPosition.html +126 -0
  153. package/docs/enums/LoopType.html +119 -0
  154. package/docs/index.html +288 -0
  155. package/docs/modules.html +146 -0
  156. package/jest.config.js +5 -0
  157. package/package.json +33 -47
  158. package/rename_clear_files.sh +29 -0
  159. package/src/assets/complexities-diff.jpg +0 -0
  160. package/src/assets/data-structure-complexities.jpg +0 -0
  161. package/src/data-structures/binary-tree/avl-tree.ts +58 -6
  162. package/src/data-structures/binary-tree/binary-indexed-tree.ts +31 -4
  163. package/src/data-structures/binary-tree/binary-tree.ts +460 -145
  164. package/src/data-structures/binary-tree/bst.ts +31 -25
  165. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  166. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  167. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  168. package/src/data-structures/binary-tree/segment-tree.ts +25 -12
  169. package/src/data-structures/binary-tree/tree-multiset.ts +5 -4
  170. package/src/data-structures/diagrams/README.md +5 -0
  171. package/src/data-structures/graph/abstract-graph.ts +224 -108
  172. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  173. package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  174. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  175. package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  176. package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  177. package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  178. package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  179. package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  180. package/src/data-structures/graph/diagrams/mst.jpg +0 -0
  181. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  182. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  183. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  184. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  185. package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  186. package/src/data-structures/graph/directed-graph.ts +132 -26
  187. package/src/data-structures/graph/undirected-graph.ts +78 -11
  188. package/src/data-structures/hash/coordinate-map.ts +33 -1
  189. package/src/data-structures/hash/coordinate-set.ts +25 -0
  190. package/src/data-structures/hash/index.ts +5 -0
  191. package/src/data-structures/heap/heap.ts +27 -41
  192. package/src/data-structures/heap/max-heap.ts +8 -2
  193. package/src/data-structures/heap/min-heap.ts +9 -2
  194. package/src/data-structures/linked-list/doubly-linked-list.ts +50 -17
  195. package/src/data-structures/linked-list/singly-linked-list.ts +56 -39
  196. package/src/data-structures/matrix/matrix.ts +11 -0
  197. package/src/data-structures/matrix/matrix2d.ts +90 -10
  198. package/src/data-structures/matrix/navigator.ts +34 -14
  199. package/src/data-structures/matrix/vector2d.ts +187 -63
  200. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -3
  201. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -3
  202. package/src/data-structures/priority-queue/priority-queue.ts +200 -78
  203. package/src/data-structures/queue/deque.ts +71 -2
  204. package/src/data-structures/queue/queue.ts +37 -40
  205. package/src/data-structures/stack/stack.ts +32 -38
  206. package/src/data-structures/trie/trie.ts +83 -14
  207. package/src/data-structures/types/abstract-graph.ts +51 -0
  208. package/src/data-structures/types/avl-tree.ts +6 -0
  209. package/src/data-structures/types/binary-tree.ts +15 -0
  210. package/src/data-structures/types/bst.ts +5 -0
  211. package/src/data-structures/types/directed-graph.ts +18 -0
  212. package/src/data-structures/types/doubly-linked-list.ts +1 -0
  213. package/src/data-structures/types/heap.ts +8 -0
  214. package/src/data-structures/types/index.ts +13 -0
  215. package/src/data-structures/types/navigator.ts +13 -0
  216. package/src/data-structures/types/priority-queue.ts +9 -0
  217. package/src/data-structures/types/segment-tree.ts +1 -0
  218. package/src/data-structures/types/singly-linked-list.ts +1 -0
  219. package/src/data-structures/types/tree-multiset.ts +3 -0
  220. package/src/utils/index.ts +1 -0
  221. package/src/utils/trampoline.ts +51 -0
  222. package/src/utils/types/index.ts +1 -0
  223. package/src/{types → utils/types}/utils.ts +27 -5
  224. package/src/{utils.ts → utils/utils.ts} +41 -131
  225. package/tests/unit/data-structures/binary-tree/bst.test.ts +185 -0
  226. package/tests/unit/data-structures/graph/directed-graph.test.ts +71 -0
  227. package/{dist/types/data-structures/graph/index.d.ts → tests/unit/data-structures/graph/index.ts} +1 -1
  228. package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
  229. package/tsconfig.json +9 -6
  230. package/dist/data-structures/trampoline.d.ts +0 -25
  231. package/dist/data-structures/trampoline.js +0 -52
  232. package/dist/types/data-structures/binary-tree/aa-tree.d.ts +0 -2
  233. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -21
  234. package/dist/types/data-structures/binary-tree/b-tree.d.ts +0 -2
  235. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -8
  236. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -140
  237. package/dist/types/data-structures/binary-tree/bst.d.ts +0 -32
  238. package/dist/types/data-structures/binary-tree/index.d.ts +0 -4
  239. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -2
  240. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +0 -33
  241. package/dist/types/data-structures/binary-tree/splay-tree.d.ts +0 -2
  242. package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +0 -11
  243. package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +0 -2
  244. package/dist/types/data-structures/graph/abstract-graph.d.ts +0 -126
  245. package/dist/types/data-structures/graph/directed-graph.d.ts +0 -51
  246. package/dist/types/data-structures/graph/undirected-graph.d.ts +0 -24
  247. package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -8
  248. package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -7
  249. package/dist/types/data-structures/hash/index.d.ts +0 -1
  250. package/dist/types/data-structures/hash/pair.d.ts +0 -1
  251. package/dist/types/data-structures/hash/tree-map.d.ts +0 -1
  252. package/dist/types/data-structures/hash/tree-set.d.ts +0 -1
  253. package/dist/types/data-structures/heap/heap.d.ts +0 -72
  254. package/dist/types/data-structures/heap/index.d.ts +0 -3
  255. package/dist/types/data-structures/heap/max-heap.d.ts +0 -14
  256. package/dist/types/data-structures/heap/min-heap.d.ts +0 -14
  257. package/dist/types/data-structures/index.d.ts +0 -9
  258. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -59
  259. package/dist/types/data-structures/linked-list/index.d.ts +0 -2
  260. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +0 -358
  261. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +0 -1
  262. package/dist/types/data-structures/matrix/index.d.ts +0 -3
  263. package/dist/types/data-structures/matrix/matrix.d.ts +0 -9
  264. package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -25
  265. package/dist/types/data-structures/matrix/navigator.d.ts +0 -31
  266. package/dist/types/data-structures/matrix/vector2d.d.ts +0 -74
  267. package/dist/types/data-structures/priority-queue/index.d.ts +0 -3
  268. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -4
  269. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -4
  270. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +0 -36
  271. package/dist/types/data-structures/queue/deque.d.ts +0 -37
  272. package/dist/types/data-structures/queue/index.d.ts +0 -1
  273. package/dist/types/data-structures/queue/queue.d.ts +0 -76
  274. package/dist/types/data-structures/stack/index.d.ts +0 -1
  275. package/dist/types/data-structures/stack/stack.d.ts +0 -69
  276. package/dist/types/data-structures/trampoline.d.ts +0 -25
  277. package/dist/types/data-structures/trie/index.d.ts +0 -1
  278. package/dist/types/data-structures/trie/trie.d.ts +0 -28
  279. package/dist/types/utils.d.ts +0 -46
  280. package/dist/utils.js +0 -569
  281. package/src/data-structures/trampoline.ts +0 -91
  282. package/src/types/index.ts +0 -1
  283. /package/dist/{types/data-structures/hash/hash-table.d.ts → data-structures/types/singly-linked-list.d.ts} +0 -0
  284. /package/dist/{types → utils}/index.d.ts +0 -0
  285. /package/dist/{types → utils}/index.js +0 -0
  286. /package/dist/{types → utils}/types/index.d.ts +0 -0
  287. /package/{src/types/patches/index.d.ts → tests/unit/data-structures/graph/abstract-graph.ts} +0 -0
@@ -1,6 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BTree = void 0;
4
- class BTree {
5
- }
4
+ var BTree = /** @class */ (function () {
5
+ function BTree() {
6
+ }
7
+ return BTree;
8
+ }());
6
9
  exports.BTree = BTree;
@@ -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);
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
+ */
4
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
+ */
5
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
+ */
6
34
  getRangeSum(start: number, end: number): number;
7
- static lowBit(x: number): number;
8
35
  }
@@ -1,31 +1,59 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BinaryIndexedTree = void 0;
4
- class BinaryIndexedTree {
5
- constructor(n) {
4
+ /**
5
+ * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT
7
+ */
8
+ var BinaryIndexedTree = /** @class */ (function () {
9
+ function BinaryIndexedTree(n) {
6
10
  this._sumTree = new Array(n + 1).fill(0);
7
11
  }
8
- update(i, delta) {
12
+ BinaryIndexedTree.lowBit = function (x) {
13
+ return x & (-x);
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
+ */
23
+ BinaryIndexedTree.prototype.update = function (i, delta) {
9
24
  while (i < this._sumTree.length) {
10
25
  this._sumTree[i] += delta;
11
26
  i += BinaryIndexedTree.lowBit(i);
12
27
  }
13
- }
14
- getPrefixSum(i) {
15
- let sum = 0;
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
+ */
36
+ BinaryIndexedTree.prototype.getPrefixSum = function (i) {
37
+ var sum = 0;
16
38
  while (i > 0) {
17
39
  sum += this._sumTree[i];
18
40
  i -= BinaryIndexedTree.lowBit(i);
19
41
  }
20
42
  return sum;
21
- }
22
- getRangeSum(start, end) {
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
+ */
52
+ BinaryIndexedTree.prototype.getRangeSum = function (start, end) {
23
53
  if (!(0 <= start && start <= end && end <= this._sumTree.length))
24
54
  throw 'Index out of bounds';
25
55
  return this.getPrefixSum(end) - this.getPrefixSum(start);
26
- }
27
- static lowBit(x) {
28
- return x & (-x);
29
- }
30
- }
56
+ };
57
+ return BinaryIndexedTree;
58
+ }());
31
59
  exports.BinaryIndexedTree = BinaryIndexedTree;
@@ -1,18 +1,8 @@
1
- export type BinaryTreeNodePropertyName = 'id' | 'val' | 'count';
2
- export type NodeOrPropertyName = 'node' | BinaryTreeNodePropertyName;
3
- export type DFSOrderPattern = 'in' | 'pre' | 'post';
4
- export type BinaryTreeNodeId = number;
5
- export type BinaryTreeDeleted<T> = {
6
- deleted: BinaryTreeNode<T> | null | undefined;
7
- needBalanced: BinaryTreeNode<T> | null;
8
- };
9
- export type ResultByProperty<T> = T | BinaryTreeNode<T> | number | BinaryTreeNodeId;
10
- export type ResultsByProperty<T> = ResultByProperty<T>[];
11
- export interface BinaryTreeNodeObj<T> {
12
- id: BinaryTreeNodeId;
13
- val: T;
14
- count?: number;
15
- }
1
+ /**
2
+ * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
+ * @license MIT
4
+ */
5
+ import type { BinaryTreeDeleted, BinaryTreeNodeId, BinaryTreeNodePropertyName, DFSOrderPattern, NodeOrPropertyName, ResultsByProperty } from '../types';
16
6
  export declare enum FamilyPosition {
17
7
  root = 0,
18
8
  left = 1,
@@ -23,6 +13,7 @@ export declare enum LoopType {
23
13
  recursive = 2
24
14
  }
25
15
  export declare class BinaryTreeNode<T> {
16
+ constructor(id: BinaryTreeNodeId, val: T, count?: number);
26
17
  protected _id: BinaryTreeNodeId;
27
18
  get id(): BinaryTreeNodeId;
28
19
  set id(v: BinaryTreeNodeId);
@@ -47,64 +38,224 @@ export declare class BinaryTreeNode<T> {
47
38
  protected _height: number;
48
39
  get height(): number;
49
40
  set height(v: number);
50
- constructor(id: BinaryTreeNodeId, val: T, count?: number);
51
41
  swapLocation(swapNode: BinaryTreeNode<T>): BinaryTreeNode<T>;
52
42
  clone(): BinaryTreeNode<T>;
53
43
  }
54
44
  export declare class BinaryTree<T> {
55
- protected _root: BinaryTreeNode<T> | null;
56
- get root(): BinaryTreeNode<T> | null;
57
- protected set root(v: BinaryTreeNode<T> | null);
58
- protected _size: number;
59
- get size(): number;
60
- protected set size(v: number);
61
- protected _count: number;
62
- get count(): number;
63
- protected set count(v: number);
64
- private readonly _autoIncrementId;
65
- private _maxId;
66
- private readonly _isDuplicatedVal;
67
45
  protected _loopType: LoopType;
68
46
  protected _visitedId: BinaryTreeNodeId[];
69
47
  protected _visitedVal: Array<T>;
70
48
  protected _visitedNode: BinaryTreeNode<T>[];
71
49
  protected _visitedCount: number[];
72
50
  protected _visitedLeftSum: number[];
73
- protected _resetResults(): void;
51
+ private readonly _autoIncrementId;
52
+ private _maxId;
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
+ */
74
59
  constructor(options?: {
75
60
  loopType?: LoopType;
76
61
  autoIncrementId?: boolean;
77
62
  isDuplicatedVal?: boolean;
78
63
  });
64
+ protected _root: BinaryTreeNode<T> | null;
65
+ get root(): BinaryTreeNode<T> | null;
66
+ protected set root(v: BinaryTreeNode<T> | null);
67
+ protected _size: number;
68
+ get size(): number;
69
+ protected set size(v: number);
70
+ protected _count: number;
71
+ get count(): number;
72
+ protected set count(v: 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
+ */
79
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
+ */
80
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
+ */
81
94
  isEmpty(): boolean;
82
- insertTo({ newNode, parent }: {
83
- newNode: BinaryTreeNode<T> | null;
84
- parent: BinaryTreeNode<T>;
85
- }): 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
+ */
86
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
+ */
87
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
+ */
88
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
+ */
89
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
+ */
90
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
+ */
91
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
+ */
92
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
+ */
93
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
+ */
94
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
+ */
95
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
+ */
96
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
+ */
97
216
  getPathToRoot(node: BinaryTreeNode<T>): BinaryTreeNode<T>[];
98
- protected _pushByPropertyNameStopOrNot(cur: BinaryTreeNode<T>, result: (BinaryTreeNode<T> | null | undefined)[], nodeProperty: BinaryTreeNodeId | T, propertyName?: BinaryTreeNodePropertyName, onlyOne?: boolean): boolean | undefined;
99
- protected _accumulatedByPropertyName(node: BinaryTreeNode<T>, nodeOrPropertyName?: NodeOrPropertyName): void;
100
- protected _getResultByPropertyName(nodeOrPropertyName?: NodeOrPropertyName): ResultsByProperty<T>;
101
217
  getLeftMost(): BinaryTreeNode<T> | null;
102
218
  getLeftMost(node: BinaryTreeNode<T>): BinaryTreeNode<T>;
103
219
  getRightMost(): BinaryTreeNode<T> | null;
104
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
+ */
105
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
+ */
106
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
+ */
107
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
+ */
108
259
  subTreeAdd(subTreeRoot: BinaryTreeNode<T>, delta: number, propertyName?: BinaryTreeNodePropertyName): boolean;
109
260
  BFS(): BinaryTreeNodeId[];
110
261
  BFS(nodeOrPropertyName: 'id'): BinaryTreeNodeId[];
@@ -131,10 +282,53 @@ export declare class BinaryTree<T> {
131
282
  listLevels(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'val'): T[][];
132
283
  listLevels(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'node'): BinaryTreeNode<T>[][];
133
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
+ */
134
290
  getPredecessor(node: BinaryTreeNode<T>): BinaryTreeNode<T>;
135
291
  morris(): BinaryTreeNodeId[];
136
292
  morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
137
293
  morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): T[];
138
294
  morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): BinaryTreeNode<T>[];
139
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
+ */
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
+ */
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
+ */
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
+ */
333
+ protected _getResultByPropertyName(nodeOrPropertyName?: NodeOrPropertyName): ResultsByProperty<T>;
140
334
  }