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,7 +1,38 @@
1
1
  "use strict";
2
+ /**
3
+ * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
4
+ * @license MIT
5
+ */
6
+ var __values = (this && this.__values) || function(o) {
7
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
8
+ if (m) return m.call(o);
9
+ if (o && typeof o.length === "number") return {
10
+ next: function () {
11
+ if (o && i >= o.length) o = void 0;
12
+ return { value: o && o[i++], done: !o };
13
+ }
14
+ };
15
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
16
+ };
17
+ var __read = (this && this.__read) || function (o, n) {
18
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
19
+ if (!m) return o;
20
+ var i = m.call(o), r, ar = [], e;
21
+ try {
22
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
23
+ }
24
+ catch (error) { e = { error: error }; }
25
+ finally {
26
+ try {
27
+ if (r && !r.done && (m = i["return"])) m.call(i);
28
+ }
29
+ finally { if (e) throw e.error; }
30
+ }
31
+ return ar;
32
+ };
2
33
  Object.defineProperty(exports, "__esModule", { value: true });
3
34
  exports.BinaryTree = exports.BinaryTreeNode = exports.LoopType = exports.FamilyPosition = void 0;
4
- const trampoline_1 = require("../trampoline");
35
+ var trampoline_1 = require("../../utils/trampoline");
5
36
  var FamilyPosition;
6
37
  (function (FamilyPosition) {
7
38
  FamilyPosition[FamilyPosition["root"] = 0] = "root";
@@ -13,65 +44,8 @@ var LoopType;
13
44
  LoopType[LoopType["iterative"] = 1] = "iterative";
14
45
  LoopType[LoopType["recursive"] = 2] = "recursive";
15
46
  })(LoopType = exports.LoopType || (exports.LoopType = {}));
16
- class BinaryTreeNode {
17
- get id() {
18
- return this._id;
19
- }
20
- set id(v) {
21
- this._id = v;
22
- }
23
- get val() {
24
- return this._val;
25
- }
26
- set val(v) {
27
- this._val = v;
28
- }
29
- get left() {
30
- return this._left;
31
- }
32
- set left(v) {
33
- if (v) {
34
- v.parent = this;
35
- v.familyPosition = FamilyPosition.left;
36
- }
37
- this._left = v;
38
- }
39
- get right() {
40
- return this._right;
41
- }
42
- set right(v) {
43
- if (v) {
44
- v.parent = this;
45
- v.familyPosition = FamilyPosition.right;
46
- }
47
- this._right = v;
48
- }
49
- get parent() {
50
- return this._parent;
51
- }
52
- set parent(v) {
53
- this._parent = v;
54
- }
55
- get familyPosition() {
56
- return this._familyPosition;
57
- }
58
- set familyPosition(v) {
59
- this._familyPosition = v;
60
- }
61
- get count() {
62
- return this._count;
63
- }
64
- set count(v) {
65
- this._count = v;
66
- }
67
- get height() {
68
- return this._height;
69
- }
70
- set height(v) {
71
- this._height = v;
72
- }
73
- constructor(id, val, count) {
74
- this._parent = undefined;
47
+ var BinaryTreeNode = /** @class */ (function () {
48
+ function BinaryTreeNode(id, val, count) {
75
49
  this._familyPosition = FamilyPosition.root;
76
50
  this._count = 1;
77
51
  this._height = 0;
@@ -79,9 +53,97 @@ class BinaryTreeNode {
79
53
  this._val = val;
80
54
  this._count = count !== null && count !== void 0 ? count : 1;
81
55
  }
82
- swapLocation(swapNode) {
83
- const { val, count, height } = swapNode;
84
- const tempNode = new BinaryTreeNode(swapNode.id, val);
56
+ Object.defineProperty(BinaryTreeNode.prototype, "id", {
57
+ get: function () {
58
+ return this._id;
59
+ },
60
+ set: function (v) {
61
+ this._id = v;
62
+ },
63
+ enumerable: false,
64
+ configurable: true
65
+ });
66
+ Object.defineProperty(BinaryTreeNode.prototype, "val", {
67
+ get: function () {
68
+ return this._val;
69
+ },
70
+ set: function (v) {
71
+ this._val = v;
72
+ },
73
+ enumerable: false,
74
+ configurable: true
75
+ });
76
+ Object.defineProperty(BinaryTreeNode.prototype, "left", {
77
+ get: function () {
78
+ return this._left;
79
+ },
80
+ set: function (v) {
81
+ if (v) {
82
+ v.parent = this;
83
+ v.familyPosition = FamilyPosition.left;
84
+ }
85
+ this._left = v;
86
+ },
87
+ enumerable: false,
88
+ configurable: true
89
+ });
90
+ Object.defineProperty(BinaryTreeNode.prototype, "right", {
91
+ get: function () {
92
+ return this._right;
93
+ },
94
+ set: function (v) {
95
+ if (v) {
96
+ v.parent = this;
97
+ v.familyPosition = FamilyPosition.right;
98
+ }
99
+ this._right = v;
100
+ },
101
+ enumerable: false,
102
+ configurable: true
103
+ });
104
+ Object.defineProperty(BinaryTreeNode.prototype, "parent", {
105
+ get: function () {
106
+ return this._parent;
107
+ },
108
+ set: function (v) {
109
+ this._parent = v;
110
+ },
111
+ enumerable: false,
112
+ configurable: true
113
+ });
114
+ Object.defineProperty(BinaryTreeNode.prototype, "familyPosition", {
115
+ get: function () {
116
+ return this._familyPosition;
117
+ },
118
+ set: function (v) {
119
+ this._familyPosition = v;
120
+ },
121
+ enumerable: false,
122
+ configurable: true
123
+ });
124
+ Object.defineProperty(BinaryTreeNode.prototype, "count", {
125
+ get: function () {
126
+ return this._count;
127
+ },
128
+ set: function (v) {
129
+ this._count = v;
130
+ },
131
+ enumerable: false,
132
+ configurable: true
133
+ });
134
+ Object.defineProperty(BinaryTreeNode.prototype, "height", {
135
+ get: function () {
136
+ return this._height;
137
+ },
138
+ set: function (v) {
139
+ this._height = v;
140
+ },
141
+ enumerable: false,
142
+ configurable: true
143
+ });
144
+ BinaryTreeNode.prototype.swapLocation = function (swapNode) {
145
+ var val = swapNode.val, count = swapNode.count, height = swapNode.height;
146
+ var tempNode = new BinaryTreeNode(swapNode.id, val);
85
147
  tempNode.val = val;
86
148
  tempNode.count = count;
87
149
  tempNode.height = height;
@@ -94,75 +156,113 @@ class BinaryTreeNode {
94
156
  this.count = tempNode.count;
95
157
  this.height = tempNode.height;
96
158
  return swapNode;
97
- }
98
- clone() {
159
+ };
160
+ BinaryTreeNode.prototype.clone = function () {
99
161
  return new BinaryTreeNode(this.id, this.val, this.count);
100
- }
101
- }
162
+ };
163
+ return BinaryTreeNode;
164
+ }());
102
165
  exports.BinaryTreeNode = BinaryTreeNode;
103
- class BinaryTree {
104
- get root() {
105
- return this._root;
106
- }
107
- set root(v) {
108
- if (v) {
109
- v.parent = null;
110
- v.familyPosition = FamilyPosition.root;
111
- }
112
- this._root = v;
113
- }
114
- get size() {
115
- return this._size;
116
- }
117
- set size(v) {
118
- this._size = v;
119
- }
120
- get count() {
121
- return this._count;
122
- }
123
- set count(v) {
124
- this._count = v;
125
- }
126
- _resetResults() {
166
+ var BinaryTree = /** @class */ (function () {
167
+ /**
168
+ * The constructor function accepts an optional options object and sets the values of loopType, autoIncrementId, and
169
+ * isDuplicatedVal based on the provided options.
170
+ * @param [options] - An optional object that can contain the following properties:
171
+ */
172
+ function BinaryTree(options) {
173
+ this._loopType = LoopType.iterative;
127
174
  this._visitedId = [];
128
175
  this._visitedVal = [];
129
176
  this._visitedNode = [];
130
177
  this._visitedCount = [];
131
178
  this._visitedLeftSum = [];
132
- }
133
- constructor(options) {
134
- this._root = null;
135
- this._size = 0;
136
- this._count = 0;
137
179
  this._autoIncrementId = false;
138
180
  this._maxId = -1;
139
181
  this._isDuplicatedVal = false;
140
- this._loopType = LoopType.iterative;
141
- this._visitedId = [];
142
- this._visitedVal = [];
143
- this._visitedNode = [];
144
- this._visitedCount = [];
145
- this._visitedLeftSum = [];
182
+ this._root = null;
183
+ this._size = 0;
184
+ this._count = 0;
146
185
  if (options !== undefined) {
147
- const { loopType = LoopType.iterative, autoIncrementId = false, isDuplicatedVal = false } = options;
186
+ var _a = options.loopType, loopType = _a === void 0 ? LoopType.iterative : _a, _b = options.autoIncrementId, autoIncrementId = _b === void 0 ? false : _b, _c = options.isDuplicatedVal, isDuplicatedVal = _c === void 0 ? false : _c;
148
187
  this._isDuplicatedVal = isDuplicatedVal;
149
188
  this._autoIncrementId = autoIncrementId;
150
189
  this._loopType = loopType;
151
190
  }
152
191
  }
153
- createNode(id, val, count) {
192
+ Object.defineProperty(BinaryTree.prototype, "root", {
193
+ get: function () {
194
+ return this._root;
195
+ },
196
+ set: function (v) {
197
+ if (v) {
198
+ v.parent = null;
199
+ v.familyPosition = FamilyPosition.root;
200
+ }
201
+ this._root = v;
202
+ },
203
+ enumerable: false,
204
+ configurable: true
205
+ });
206
+ Object.defineProperty(BinaryTree.prototype, "size", {
207
+ get: function () {
208
+ return this._size;
209
+ },
210
+ set: function (v) {
211
+ this._size = v;
212
+ },
213
+ enumerable: false,
214
+ configurable: true
215
+ });
216
+ Object.defineProperty(BinaryTree.prototype, "count", {
217
+ get: function () {
218
+ return this._count;
219
+ },
220
+ set: function (v) {
221
+ this._count = v;
222
+ },
223
+ enumerable: false,
224
+ configurable: true
225
+ });
226
+ /**
227
+ * The function creates a new binary tree node with the given id, value, and count, or returns null if the value is
228
+ * null.
229
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is of type
230
+ * `BinaryTreeNodeId`, which could be a string or a number, depending on how you want to identify your nodes.
231
+ * @param {T | null} val - The `val` parameter represents the value to be stored in the binary tree node. It can be of
232
+ * any type `T` or `null`.
233
+ * @param {number} [count] - The count parameter is an optional parameter that represents the number of occurrences of
234
+ * the value in the binary tree node. It is of type number.
235
+ * @returns The function `createNode` returns a `BinaryTreeNode<T>` object if the `val` parameter is not null.
236
+ * Otherwise, it returns null.
237
+ */
238
+ BinaryTree.prototype.createNode = function (id, val, count) {
154
239
  return val !== null ? new BinaryTreeNode(id, val, count) : null;
155
- }
156
- clear() {
240
+ };
241
+ /**
242
+ * The clear function resets the state of an object by setting its properties to their initial values.
243
+ */
244
+ BinaryTree.prototype.clear = function () {
157
245
  this.root = null;
158
246
  this.size = 0;
159
247
  this.count = 0;
160
248
  this._maxId = -1;
161
- }
162
- isEmpty() {
249
+ };
250
+ /**
251
+ * The function checks if the size of an object is equal to zero and returns a boolean value.
252
+ * @returns A boolean value indicating whether the size of the object is 0 or not.
253
+ */
254
+ BinaryTree.prototype.isEmpty = function () {
163
255
  return this.size === 0;
164
- }
165
- insertTo({ newNode, parent }) {
256
+ };
257
+ /**
258
+ * The function inserts a new node into a binary tree as the left or right child of a given parent node.
259
+ * @param {BinaryTreeNode<T> | null} newNode - The `newNode` parameter is an instance of the `BinaryTreeNode` class or
260
+ * `null`. It represents the node that needs to be inserted into the binary tree.
261
+ * @param parent - The `parent` parameter is a BinaryTreeNode object representing the parent node to which the new node
262
+ * will be inserted as a child.
263
+ * @returns The method returns the newly inserted node, either as the left child or the right child of the parent node.
264
+ */
265
+ BinaryTree.prototype.putTo = function (newNode, parent) {
166
266
  var _a, _b;
167
267
  if (parent) {
168
268
  if (parent.left === undefined) {
@@ -196,17 +296,29 @@ class BinaryTree {
196
296
  else {
197
297
  return;
198
298
  }
199
- }
200
- put(id, val, count) {
299
+ };
300
+ /**
301
+ * The `put` function inserts a new node with a given ID and value into a binary tree, updating the count if the node
302
+ * already exists.
303
+ * @param {BinaryTreeNodeId} id - The id parameter is the identifier of the binary tree node. It is used to uniquely
304
+ * identify each node in the binary tree.
305
+ * @param {T} val - The value to be inserted into the binary tree.
306
+ * @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the
307
+ * value should be inserted into the binary tree. If not provided, it defaults to 1.
308
+ * @returns The function `put` returns a `BinaryTreeNode<T>` object if a new node is inserted, or `null` if no new node
309
+ * is inserted, or `undefined` if the insertion fails.
310
+ */
311
+ BinaryTree.prototype.put = function (id, val, count) {
312
+ var _this = this;
201
313
  count = count !== null && count !== void 0 ? count : 1;
202
- const _bfs = (root, newNode) => {
203
- const queue = [root];
314
+ var _bfs = function (root, newNode) {
315
+ var queue = [root];
204
316
  while (queue.length > 0) {
205
- const cur = queue.shift();
317
+ var cur = queue.shift();
206
318
  if (cur) {
207
- const inserted = this.insertTo({ newNode, parent: cur });
208
- if (inserted !== undefined)
209
- return inserted;
319
+ var inserted_1 = _this.putTo(newNode, cur);
320
+ if (inserted_1 !== undefined)
321
+ return inserted_1;
210
322
  if (cur.left)
211
323
  queue.push(cur.left);
212
324
  if (cur.right)
@@ -217,9 +329,9 @@ class BinaryTree {
217
329
  }
218
330
  return;
219
331
  };
220
- let inserted;
221
- const needInsert = val !== null ? new BinaryTreeNode(id, val, count) : null;
222
- const existNode = val !== null ? this.get(id, 'id') : null;
332
+ var inserted;
333
+ var needInsert = val !== null ? new BinaryTreeNode(id, val, count) : null;
334
+ var existNode = val !== null ? this.get(id, 'id') : null;
223
335
  if (this.root) {
224
336
  if (existNode) {
225
337
  existNode.count += count;
@@ -242,57 +354,104 @@ class BinaryTree {
242
354
  inserted = this.root;
243
355
  }
244
356
  return inserted;
245
- }
246
- insertMany(data) {
247
- var _a;
248
- const inserted = [];
249
- const map = new Map();
357
+ };
358
+ /**
359
+ * The `insertMany` function inserts multiple items into a binary tree and returns an array of the inserted nodes or
360
+ * null/undefined values.
361
+ * @param {T[] | BinaryTreeNode<T>[]} data - The `data` parameter can be either an array of elements of type `T` or an
362
+ * array of `BinaryTreeNode<T>` objects.
363
+ * @returns The function `insertMany` returns an array of `BinaryTreeNode<T>`, `null`, or `undefined` values.
364
+ */
365
+ BinaryTree.prototype.insertMany = function (data) {
366
+ var e_1, _a, e_2, _b;
367
+ var _c;
368
+ var inserted = [];
369
+ var map = new Map();
250
370
  if (!this._isDuplicatedVal) {
251
- for (const i of data)
252
- map.set(i, ((_a = map.get(i)) !== null && _a !== void 0 ? _a : 0) + 1);
253
- }
254
- for (const item of data) {
255
- const count = this._isDuplicatedVal ? 1 : map.get(item);
256
- if (item instanceof BinaryTreeNode) {
257
- inserted.push(this.put(item.id, item.val, item.count));
258
- }
259
- else if (typeof item === 'number' && !this._autoIncrementId) {
260
- if (!this._isDuplicatedVal) {
261
- if (map.get(item) !== undefined) {
262
- inserted.push(this.put(item, item, count));
263
- map.delete(item);
264
- }
371
+ try {
372
+ for (var data_1 = __values(data), data_1_1 = data_1.next(); !data_1_1.done; data_1_1 = data_1.next()) {
373
+ var i = data_1_1.value;
374
+ map.set(i, ((_c = map.get(i)) !== null && _c !== void 0 ? _c : 0) + 1);
265
375
  }
266
- else {
267
- inserted.push(this.put(item, item, 1));
376
+ }
377
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
378
+ finally {
379
+ try {
380
+ if (data_1_1 && !data_1_1.done && (_a = data_1.return)) _a.call(data_1);
268
381
  }
382
+ finally { if (e_1) throw e_1.error; }
269
383
  }
270
- else {
271
- if (item !== null) {
384
+ }
385
+ try {
386
+ for (var data_2 = __values(data), data_2_1 = data_2.next(); !data_2_1.done; data_2_1 = data_2.next()) {
387
+ var item = data_2_1.value;
388
+ var count = this._isDuplicatedVal ? 1 : map.get(item);
389
+ if (item instanceof BinaryTreeNode) {
390
+ inserted.push(this.put(item.id, item.val, item.count));
391
+ }
392
+ else if (typeof item === 'number' && !this._autoIncrementId) {
272
393
  if (!this._isDuplicatedVal) {
273
394
  if (map.get(item) !== undefined) {
274
- inserted.push(this.put(++this._maxId, item, count));
395
+ inserted.push(this.put(item, item, count));
275
396
  map.delete(item);
276
397
  }
277
398
  }
278
399
  else {
279
- inserted.push(this.put(++this._maxId, item, 1));
400
+ inserted.push(this.put(item, item, 1));
280
401
  }
281
402
  }
282
403
  else {
283
- inserted.push(this.put(Number.MAX_SAFE_INTEGER, item, 0));
404
+ if (item !== null) {
405
+ if (!this._isDuplicatedVal) {
406
+ if (map.get(item) !== undefined) {
407
+ inserted.push(this.put(++this._maxId, item, count));
408
+ map.delete(item);
409
+ }
410
+ }
411
+ else {
412
+ inserted.push(this.put(++this._maxId, item, 1));
413
+ }
414
+ }
415
+ else {
416
+ inserted.push(this.put(Number.MAX_SAFE_INTEGER, item, 0));
417
+ }
284
418
  }
285
419
  }
286
420
  }
421
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
422
+ finally {
423
+ try {
424
+ if (data_2_1 && !data_2_1.done && (_b = data_2.return)) _b.call(data_2);
425
+ }
426
+ finally { if (e_2) throw e_2.error; }
427
+ }
287
428
  return inserted;
288
- }
289
- fill(data) {
429
+ };
430
+ /**
431
+ * The `fill` function clears the current data and inserts new data, returning a boolean indicating if the insertion
432
+ * was successful.
433
+ * @param {T[] | BinaryTreeNode<T>[]} data - The `data` parameter can be either an array of elements of type `T` or an
434
+ * array of `BinaryTreeNode<T>` objects.
435
+ * @returns The method is returning a boolean value.
436
+ */
437
+ BinaryTree.prototype.fill = function (data) {
290
438
  this.clear();
291
439
  return data.length === this.insertMany(data).length;
292
- }
293
- remove(id, ignoreCount) {
294
- const nodes = this.getNodes(id, 'id', true);
295
- let node = nodes[0];
440
+ };
441
+ /**
442
+ * The function removes a node from a binary tree and returns information about the deleted node.
443
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that you want to remove.
444
+ * It is of type `BinaryTreeNodeId`.
445
+ * @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
446
+ * whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
447
+ * not be decremented and the overall count of the binary tree will not be updated. If `
448
+ * @returns An array of objects is being returned. Each object in the array has two properties: "deleted" and
449
+ * "needBalanced". The "deleted" property contains the deleted node or undefined if no node was deleted. The
450
+ * "needBalanced" property is always null.
451
+ */
452
+ BinaryTree.prototype.remove = function (id, ignoreCount) {
453
+ var nodes = this.getNodes(id, 'id', true);
454
+ var node = nodes[0];
296
455
  if (!node)
297
456
  node = undefined;
298
457
  else if (node.count > 1 && !ignoreCount) {
@@ -300,7 +459,7 @@ class BinaryTree {
300
459
  this.count--;
301
460
  }
302
461
  else if (node instanceof BinaryTreeNode) {
303
- const [subSize, subCount] = this.getSubTreeSizeAndCount(node);
462
+ var _a = __read(this.getSubTreeSizeAndCount(node), 2), subSize = _a[0], subCount = _a[1];
304
463
  switch (node.familyPosition) {
305
464
  case 0:
306
465
  this.size -= subSize;
@@ -324,33 +483,48 @@ class BinaryTree {
324
483
  }
325
484
  }
326
485
  return [{ deleted: node, needBalanced: null }];
327
- }
328
- getDepth(node) {
329
- let depth = 0;
486
+ };
487
+ /**
488
+ * The function calculates the depth of a binary tree node by traversing its parent nodes.
489
+ * @param node - BinaryTreeNode<T> - This is the node for which we want to calculate the depth. It is a generic type,
490
+ * meaning it can represent any type of data that we want to store in the node.
491
+ * @returns The depth of the given binary tree node.
492
+ */
493
+ BinaryTree.prototype.getDepth = function (node) {
494
+ var depth = 0;
330
495
  while (node.parent) {
331
496
  depth++;
332
497
  node = node.parent;
333
498
  }
334
499
  return depth;
335
- }
336
- getHeight(beginRoot) {
500
+ };
501
+ /**
502
+ * The `getHeight` function calculates the maximum height of a binary tree using either a recursive or iterative
503
+ * approach.
504
+ * @param {BinaryTreeNode<T> | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type
505
+ * `BinaryTreeNode<T> | null`. It represents the starting node from which to calculate the height of the binary tree.
506
+ * If no value is provided for `beginRoot`, the function will use the `root` property of the class instance as
507
+ * @returns the height of the binary tree.
508
+ */
509
+ BinaryTree.prototype.getHeight = function (beginRoot) {
337
510
  var _a, _b, _c;
338
511
  beginRoot = beginRoot !== null && beginRoot !== void 0 ? beginRoot : this.root;
339
512
  if (!beginRoot)
340
513
  return -1;
341
514
  if (this._loopType === LoopType.recursive) {
342
- const _getMaxHeight = (cur) => {
515
+ var _getMaxHeight_1 = function (cur) {
343
516
  if (!cur)
344
517
  return -1;
345
- const leftHeight = _getMaxHeight(cur.left);
346
- const rightHeight = _getMaxHeight(cur.right);
518
+ var leftHeight = _getMaxHeight_1(cur.left);
519
+ var rightHeight = _getMaxHeight_1(cur.right);
347
520
  return Math.max(leftHeight, rightHeight) + 1;
348
521
  };
349
- return _getMaxHeight(beginRoot);
522
+ return _getMaxHeight_1(beginRoot);
350
523
  }
351
524
  else {
352
- const stack = [];
353
- let node = beginRoot, last = null, depths = new Map();
525
+ var stack = [];
526
+ var node = beginRoot, last = null;
527
+ var depths = new Map();
354
528
  while (stack.length > 0 || node) {
355
529
  if (node) {
356
530
  stack.push(node);
@@ -361,8 +535,8 @@ class BinaryTree {
361
535
  if (!node.right || last === node.right) {
362
536
  node = stack.pop();
363
537
  if (node) {
364
- let leftHeight = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
365
- let rightHeight = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
538
+ var leftHeight = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
539
+ var rightHeight = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
366
540
  depths.set(node, 1 + Math.max(leftHeight, rightHeight));
367
541
  last = node;
368
542
  node = null;
@@ -374,27 +548,36 @@ class BinaryTree {
374
548
  }
375
549
  return (_c = depths.get(beginRoot)) !== null && _c !== void 0 ? _c : -1;
376
550
  }
377
- }
378
- getMinHeight(beginRoot) {
551
+ };
552
+ /**
553
+ * The `getMinHeight` function calculates the minimum height of a binary tree using either a recursive or iterative
554
+ * approach.
555
+ * @param {BinaryTreeNode<T> | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type
556
+ * `BinaryTreeNode<T> | null`. It represents the starting node from which to calculate the minimum height of the binary
557
+ * tree. If no value is provided for `beginRoot`, the function will use the root node of the binary tree.
558
+ * @returns The function `getMinHeight` returns the minimum height of the binary tree.
559
+ */
560
+ BinaryTree.prototype.getMinHeight = function (beginRoot) {
379
561
  var _a, _b, _c;
380
562
  beginRoot = beginRoot || this.root;
381
563
  if (!beginRoot)
382
564
  return -1;
383
565
  if (this._loopType === LoopType.recursive) {
384
- const _getMinHeight = (cur) => {
566
+ var _getMinHeight_1 = function (cur) {
385
567
  if (!cur)
386
568
  return 0;
387
569
  if (!cur.left && !cur.right)
388
570
  return 0;
389
- const leftMinHeight = _getMinHeight(cur.left);
390
- const rightMinHeight = _getMinHeight(cur.right);
571
+ var leftMinHeight = _getMinHeight_1(cur.left);
572
+ var rightMinHeight = _getMinHeight_1(cur.right);
391
573
  return Math.min(leftMinHeight, rightMinHeight) + 1;
392
574
  };
393
- return _getMinHeight(beginRoot);
575
+ return _getMinHeight_1(beginRoot);
394
576
  }
395
577
  else {
396
- const stack = [];
397
- let node = beginRoot, last = null, depths = new Map();
578
+ var stack = [];
579
+ var node = beginRoot, last = null;
580
+ var depths = new Map();
398
581
  while (stack.length > 0 || node) {
399
582
  if (node) {
400
583
  stack.push(node);
@@ -405,8 +588,8 @@ class BinaryTree {
405
588
  if (!node.right || last === node.right) {
406
589
  node = stack.pop();
407
590
  if (node) {
408
- let leftMinHeight = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
409
- let rightMinHeight = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
591
+ var leftMinHeight = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
592
+ var rightMinHeight = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
410
593
  depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
411
594
  last = node;
412
595
  node = null;
@@ -418,30 +601,49 @@ class BinaryTree {
418
601
  }
419
602
  return (_c = depths.get(beginRoot)) !== null && _c !== void 0 ? _c : -1;
420
603
  }
421
- }
422
- isBalanced(beginRoot) {
604
+ };
605
+ /**
606
+ * The function checks if a binary tree is balanced by comparing the minimum height and the maximum height of the tree.
607
+ * @param {BinaryTreeNode<T> | null} [beginRoot] - The `beginRoot` parameter is the root node of a binary tree. It is
608
+ * of type `BinaryTreeNode<T> | null`, which means it can either be a `BinaryTreeNode` object or `null`.
609
+ * @returns The method is returning a boolean value.
610
+ */
611
+ BinaryTree.prototype.isBalanced = function (beginRoot) {
423
612
  return (this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot));
424
- }
425
- getNodes(nodeProperty, propertyName, onlyOne) {
613
+ };
614
+ /**
615
+ * The function `getNodes` returns an array of binary tree nodes that match a given property value, with options for
616
+ * searching recursively or iteratively.
617
+ * @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
618
+ * generic type `T`. It represents the property of the binary tree node that you want to search for.
619
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
620
+ * specifies the property name to use when searching for nodes. If not provided, it defaults to 'id'.
621
+ * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
622
+ * return only one node that matches the `nodeProperty` or `propertyName` criteria. If `onlyOne` is set to `true`, the
623
+ * function will stop traversing the tree and return the first matching node. If `
624
+ * @returns The function `getNodes` returns an array of `BinaryTreeNode<T> | null | undefined` objects.
625
+ */
626
+ BinaryTree.prototype.getNodes = function (nodeProperty, propertyName, onlyOne) {
627
+ var _this = this;
426
628
  if (!this.root)
427
629
  return [];
428
630
  propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
429
- const result = [];
631
+ var result = [];
430
632
  if (this._loopType === LoopType.recursive) {
431
- const _traverse = (cur) => {
432
- if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
633
+ var _traverse_1 = function (cur) {
634
+ if (_this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
433
635
  return;
434
636
  if (!cur.left && !cur.right)
435
637
  return;
436
- cur.left && _traverse(cur.left);
437
- cur.right && _traverse(cur.right);
638
+ cur.left && _traverse_1(cur.left);
639
+ cur.right && _traverse_1(cur.right);
438
640
  };
439
- _traverse(this.root);
641
+ _traverse_1(this.root);
440
642
  }
441
643
  else {
442
- const queue = [this.root];
644
+ var queue = [this.root];
443
645
  while (queue.length > 0) {
444
- const cur = queue.shift();
646
+ var cur = queue.shift();
445
647
  if (cur) {
446
648
  if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
447
649
  return result;
@@ -451,181 +653,175 @@ class BinaryTree {
451
653
  }
452
654
  }
453
655
  return result;
454
- }
455
- has(nodeProperty, propertyName) {
656
+ };
657
+ /**
658
+ * The function checks if a binary tree node has a specific property or if any node in the tree has a specific
659
+ * property.
660
+ * @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
661
+ * generic type `T`. It represents the property of a binary tree node that you want to check.
662
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
663
+ * specifies the name of the property to check for in the nodes.
664
+ * @returns a boolean value.
665
+ */
666
+ BinaryTree.prototype.has = function (nodeProperty, propertyName) {
456
667
  return this.getNodes(nodeProperty, propertyName).length > 0;
457
- }
458
- get(nodeProperty, propertyName) {
668
+ };
669
+ /**
670
+ * The function returns the first binary tree node that matches the given property name and value, or null if no match
671
+ * is found.
672
+ * @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
673
+ * generic type `T`. It represents the property of the binary tree node that you want to search for.
674
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
675
+ * specifies the property of the binary tree node to search for. If not provided, it defaults to `'id'`.
676
+ * @returns a BinaryTreeNode object or null.
677
+ */
678
+ BinaryTree.prototype.get = function (nodeProperty, propertyName) {
459
679
  var _a;
460
680
  propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
461
681
  return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
462
- }
463
- getPathToRoot(node) {
464
- const result = [];
682
+ };
683
+ /**
684
+ * The function getPathToRoot returns an array of BinaryTreeNode objects representing the path from a given node to the
685
+ * root of a binary tree.
686
+ * @param node - The `node` parameter is a BinaryTreeNode object.
687
+ * @returns The function `getPathToRoot` returns an array of `BinaryTreeNode<T>` objects, representing the path from
688
+ * the given `node` to the root of the binary tree.
689
+ */
690
+ BinaryTree.prototype.getPathToRoot = function (node) {
691
+ var result = [];
465
692
  while (node.parent) {
466
693
  result.unshift(node);
467
694
  node = node.parent;
468
695
  }
469
696
  result.unshift(node);
470
697
  return result;
471
- }
472
- _pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne) {
473
- switch (propertyName) {
474
- case 'id':
475
- if (cur.id === nodeProperty) {
476
- result.push(cur);
477
- return !!onlyOne;
478
- }
479
- break;
480
- case 'count':
481
- if (cur.count === nodeProperty) {
482
- result.push(cur);
483
- return !!onlyOne;
484
- }
485
- break;
486
- case 'val':
487
- if (cur.val === nodeProperty) {
488
- result.push(cur);
489
- return !!onlyOne;
490
- }
491
- break;
492
- default:
493
- if (cur.id === nodeProperty) {
494
- result.push(cur);
495
- return !!onlyOne;
496
- }
497
- break;
498
- }
499
- }
500
- _accumulatedByPropertyName(node, nodeOrPropertyName) {
501
- nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
502
- switch (nodeOrPropertyName) {
503
- case 'id':
504
- this._visitedId.push(node.id);
505
- break;
506
- case 'val':
507
- this._visitedVal.push(node.val);
508
- break;
509
- case 'node':
510
- this._visitedNode.push(node);
511
- break;
512
- case 'count':
513
- this._visitedCount.push(node.count);
514
- break;
515
- default:
516
- this._visitedId.push(node.id);
517
- break;
518
- }
519
- }
520
- _getResultByPropertyName(nodeOrPropertyName) {
521
- nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
522
- switch (nodeOrPropertyName) {
523
- case 'id':
524
- return this._visitedId;
525
- case 'val':
526
- return this._visitedVal;
527
- case 'node':
528
- return this._visitedNode;
529
- case 'count':
530
- return this._visitedCount;
531
- default:
532
- return this._visitedId;
533
- }
534
- }
535
- getLeftMost(node) {
698
+ };
699
+ /**
700
+ * The `getLeftMost` function returns the leftmost node in a binary tree, either recursively or iteratively using tail
701
+ * recursion optimization.
702
+ * @param {BinaryTreeNode<T> | null} [node] - The `node` parameter is an optional parameter of type `BinaryTreeNode<T>
703
+ * | null`. It represents the starting node from which to find the leftmost node in a binary tree. If no node is
704
+ * provided, the function will use the root node of the binary tree.
705
+ * @returns The `getLeftMost` function returns the leftmost node in a binary tree.
706
+ */
707
+ BinaryTree.prototype.getLeftMost = function (node) {
536
708
  node = node !== null && node !== void 0 ? node : this.root;
537
709
  if (!node)
538
710
  return node;
539
711
  if (this._loopType === LoopType.recursive) {
540
- const _traverse = (cur) => {
712
+ var _traverse_2 = function (cur) {
541
713
  if (!cur.left)
542
714
  return cur;
543
- return _traverse(cur.left);
715
+ return _traverse_2(cur.left);
544
716
  };
545
- return _traverse(node);
717
+ return _traverse_2(node);
546
718
  }
547
719
  else {
548
720
  // Indirect implementation of iteration using tail recursion optimization
549
- const _traverse = (0, trampoline_1.trampoline)((cur) => {
721
+ var _traverse_3 = (0, trampoline_1.trampoline)(function (cur) {
550
722
  if (!cur.left)
551
723
  return cur;
552
- return _traverse.cont(cur.left);
724
+ return _traverse_3.cont(cur.left);
553
725
  });
554
- return _traverse(node);
726
+ return _traverse_3(node);
555
727
  }
556
- }
557
- getRightMost(node) {
728
+ };
729
+ /**
730
+ * The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using
731
+ * tail recursion optimization.
732
+ * @param {BinaryTreeNode<T> | null} [node] - The `node` parameter is an optional parameter of type `BinaryTreeNode<T>
733
+ * | null`. It represents the starting node from which to find the rightmost node in a binary tree. If no node is
734
+ * provided, the function will use the root node of the binary tree.
735
+ * @returns The `getRightMost` function returns the rightmost node in a binary tree.
736
+ */
737
+ BinaryTree.prototype.getRightMost = function (node) {
558
738
  node = node !== null && node !== void 0 ? node : this.root;
559
739
  if (!node)
560
740
  return node;
561
741
  if (this._loopType === LoopType.recursive) {
562
- const _traverse = (cur) => {
742
+ var _traverse_4 = function (cur) {
563
743
  if (!cur.right)
564
744
  return cur;
565
- return _traverse(cur.right);
745
+ return _traverse_4(cur.right);
566
746
  };
567
- return _traverse(node);
747
+ return _traverse_4(node);
568
748
  }
569
749
  else {
570
750
  // Indirect implementation of iteration using tail recursion optimization
571
- const _traverse = (0, trampoline_1.trampoline)((cur) => {
751
+ var _traverse_5 = (0, trampoline_1.trampoline)(function (cur) {
572
752
  if (!cur.right)
573
753
  return cur;
574
- return _traverse.cont(cur.right);
754
+ return _traverse_5.cont(cur.right);
575
755
  });
576
- return _traverse(node);
756
+ return _traverse_5(node);
577
757
  }
578
- }
758
+ };
579
759
  // --- start additional methods ---
580
- isBST(node) {
760
+ /**
761
+ * The `isBST` function checks if a binary tree is a binary search tree.
762
+ * @param {BinaryTreeNode<T> | null} [node] - The `node` parameter is an optional parameter of type `BinaryTreeNode<T>
763
+ * | null`. It represents the root node of the binary search tree (BST) that we want to check for validity. If no node
764
+ * is provided, the function will default to using the root node of the BST instance that
765
+ * @returns The `isBST` function returns a boolean value. It returns `true` if the binary tree is a valid binary search
766
+ * tree, and `false` otherwise.
767
+ */
768
+ BinaryTree.prototype.isBST = function (node) {
581
769
  node = node !== null && node !== void 0 ? node : this.root;
582
770
  if (!node)
583
771
  return true;
584
772
  if (this._loopType === LoopType.recursive) {
585
- const dfs = (cur, min, max) => {
773
+ var dfs_1 = function (cur, min, max) {
586
774
  if (!cur)
587
775
  return true;
588
776
  if (cur.id <= min || cur.id >= max)
589
777
  return false;
590
- return dfs(cur.left, min, cur.id) && dfs(cur.right, cur.id, max);
778
+ return dfs_1(cur.left, min, cur.id) && dfs_1(cur.right, cur.id, max);
591
779
  };
592
- return dfs(node, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
780
+ return dfs_1(node, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
593
781
  }
594
782
  else {
595
- const stack = [];
596
- let prev = Number.MIN_SAFE_INTEGER, curr = node;
783
+ var stack = [];
784
+ var prev = Number.MIN_SAFE_INTEGER, curr = node;
597
785
  while (curr || stack.length > 0) {
598
786
  while (curr) {
599
787
  stack.push(curr);
600
788
  curr = curr.left;
601
789
  }
602
790
  curr = stack.pop();
603
- if (prev >= curr.id)
791
+ if (!(curr) || prev >= curr.id)
604
792
  return false;
605
793
  prev = curr.id;
606
794
  curr = curr.right;
607
795
  }
608
796
  return true;
609
797
  }
610
- }
611
- getSubTreeSizeAndCount(subTreeRoot) {
612
- const res = [0, 0];
798
+ };
799
+ /**
800
+ * The function calculates the size and count of a subtree in a binary tree using either recursive or iterative
801
+ * traversal.
802
+ * @param {BinaryTreeNode<T> | null | undefined} subTreeRoot - The `subTreeRoot` parameter is the root node of a binary
803
+ * tree.
804
+ * @returns The function `getSubTreeSizeAndCount` returns an array `[number, number]`. The first element of the array
805
+ * represents the size of the subtree, and the second element represents the count of the nodes in the subtree.
806
+ */
807
+ BinaryTree.prototype.getSubTreeSizeAndCount = function (subTreeRoot) {
808
+ var res = [0, 0];
613
809
  if (!subTreeRoot)
614
810
  return res;
615
811
  if (this._loopType === LoopType.recursive) {
616
- const _traverse = (cur) => {
812
+ var _traverse_6 = function (cur) {
617
813
  res[0]++;
618
814
  res[1] += cur.count;
619
- cur.left && _traverse(cur.left);
620
- cur.right && _traverse(cur.right);
815
+ cur.left && _traverse_6(cur.left);
816
+ cur.right && _traverse_6(cur.right);
621
817
  };
622
- _traverse(subTreeRoot);
818
+ _traverse_6(subTreeRoot);
623
819
  return res;
624
820
  }
625
821
  else {
626
- const stack = [subTreeRoot];
822
+ var stack = [subTreeRoot];
627
823
  while (stack.length > 0) {
628
- const cur = stack.pop();
824
+ var cur = stack.pop();
629
825
  res[0]++;
630
826
  res[1] += cur.count;
631
827
  cur.right && stack.push(cur.right);
@@ -633,14 +829,24 @@ class BinaryTree {
633
829
  }
634
830
  return res;
635
831
  }
636
- }
637
- subTreeSum(subTreeRoot, propertyName) {
832
+ };
833
+ /**
834
+ * The function `subTreeSum` calculates the sum of a specified property in a binary tree, either recursively or
835
+ * iteratively.
836
+ * @param subTreeRoot - The subTreeRoot parameter is the root node of the subtree for which you want to calculate the
837
+ * sum.
838
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
839
+ * specifies the property of the `BinaryTreeNode` object to use for calculating the sum. If `propertyName` is not
840
+ * provided, it defaults to `'val'`.
841
+ * @returns a number, which is the sum of the values of the nodes in the subtree rooted at `subTreeRoot`.
842
+ */
843
+ BinaryTree.prototype.subTreeSum = function (subTreeRoot, propertyName) {
638
844
  propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'val';
639
845
  if (!subTreeRoot)
640
846
  return 0;
641
- let sum = 0;
642
- const _sumByProperty = (cur) => {
643
- let needSum;
847
+ var sum = 0;
848
+ var _sumByProperty = function (cur) {
849
+ var needSum;
644
850
  switch (propertyName) {
645
851
  case 'id':
646
852
  needSum = cur.id;
@@ -658,36 +864,46 @@ class BinaryTree {
658
864
  return needSum;
659
865
  };
660
866
  if (this._loopType === LoopType.recursive) {
661
- const _traverse = (cur) => {
867
+ var _traverse_7 = function (cur) {
662
868
  sum += _sumByProperty(cur);
663
- cur.left && _traverse(cur.left);
664
- cur.right && _traverse(cur.right);
869
+ cur.left && _traverse_7(cur.left);
870
+ cur.right && _traverse_7(cur.right);
665
871
  };
666
- _traverse(subTreeRoot);
872
+ _traverse_7(subTreeRoot);
667
873
  }
668
874
  else {
669
- const stack = [subTreeRoot];
875
+ var stack = [subTreeRoot];
670
876
  while (stack.length > 0) {
671
- const cur = stack.pop();
877
+ var cur = stack.pop();
672
878
  sum += _sumByProperty(cur);
673
879
  cur.right && stack.push(cur.right);
674
880
  cur.left && stack.push(cur.left);
675
881
  }
676
882
  }
677
883
  return sum;
678
- }
679
- subTreeAdd(subTreeRoot, delta, propertyName) {
884
+ };
885
+ /**
886
+ * The function `subTreeAdd` adds a specified delta value to a property of each node in a binary tree.
887
+ * @param subTreeRoot - The `subTreeRoot` parameter is the root node of the subtree where the values will be modified.
888
+ * @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
889
+ * each node in the subtree should be increased or decreased.
890
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
891
+ * specifies the property of the `BinaryTreeNode` that should be modified. It defaults to `'id'` if not provided.
892
+ * @returns a boolean value, which is `true`.
893
+ */
894
+ BinaryTree.prototype.subTreeAdd = function (subTreeRoot, delta, propertyName) {
895
+ var _this = this;
680
896
  propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
681
897
  if (!subTreeRoot)
682
898
  return false;
683
- const _addByProperty = (cur) => {
899
+ var _addByProperty = function (cur) {
684
900
  switch (propertyName) {
685
901
  case 'id':
686
902
  cur.id += delta;
687
903
  break;
688
904
  case 'count':
689
905
  cur.count += delta;
690
- this.count += delta;
906
+ _this.count += delta;
691
907
  break;
692
908
  default:
693
909
  cur.id += delta;
@@ -695,30 +911,39 @@ class BinaryTree {
695
911
  }
696
912
  };
697
913
  if (this._loopType === LoopType.recursive) {
698
- const _traverse = (cur) => {
914
+ var _traverse_8 = function (cur) {
699
915
  _addByProperty(cur);
700
- cur.left && _traverse(cur.left);
701
- cur.right && _traverse(cur.right);
916
+ cur.left && _traverse_8(cur.left);
917
+ cur.right && _traverse_8(cur.right);
702
918
  };
703
- _traverse(subTreeRoot);
919
+ _traverse_8(subTreeRoot);
704
920
  }
705
921
  else {
706
- const stack = [subTreeRoot];
922
+ var stack = [subTreeRoot];
707
923
  while (stack.length > 0) {
708
- const cur = stack.pop();
924
+ var cur = stack.pop();
709
925
  _addByProperty(cur);
710
926
  cur.right && stack.push(cur.right);
711
927
  cur.left && stack.push(cur.left);
712
928
  }
713
929
  }
714
930
  return true;
715
- }
716
- BFS(nodeOrPropertyName) {
931
+ };
932
+ /**
933
+ * The BFS function performs a breadth-first search on a binary tree and returns the results based on a specified node
934
+ * or property name.
935
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
936
+ * represents either a node or a property name. If a node is provided, the breadth-first search algorithm will be
937
+ * performed starting from that node. If a property name is provided, the breadth-first search algorithm will be
938
+ * performed starting from the root node
939
+ * @returns an object of type `ResultsByProperty<T>`.
940
+ */
941
+ BinaryTree.prototype.BFS = function (nodeOrPropertyName) {
717
942
  nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
718
943
  this._resetResults();
719
- const queue = [this.root];
944
+ var queue = [this.root];
720
945
  while (queue.length !== 0) {
721
- const cur = queue.shift();
946
+ var cur = queue.shift();
722
947
  if (cur) {
723
948
  this._accumulatedByPropertyName(cur, nodeOrPropertyName);
724
949
  if ((cur === null || cur === void 0 ? void 0 : cur.left) !== null)
@@ -728,22 +953,35 @@ class BinaryTree {
728
953
  }
729
954
  }
730
955
  return this._getResultByPropertyName(nodeOrPropertyName);
731
- }
732
- DFS(pattern, nodeOrPropertyName) {
956
+ };
957
+ /**
958
+ * The DFS function performs a depth-first search traversal on a binary tree and returns the results based on the
959
+ * specified pattern and node or property name.
960
+ * @param {'in' | 'pre' | 'post'} [pattern] - The "pattern" parameter is used to specify the order in which the nodes
961
+ * of a binary tree are traversed during the Depth-First Search (DFS) algorithm. It can take one of three values: 'in',
962
+ * 'pre', or 'post'.
963
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is a string that represents
964
+ * either the name of a property in the `BinaryTreeNode` object or the value of the `id` property in the
965
+ * `BinaryTreeNode` object. This parameter is used to accumulate the results based on the specified property name. If
966
+ * no value
967
+ * @returns an object of type `ResultsByProperty<T>`.
968
+ */
969
+ BinaryTree.prototype.DFS = function (pattern, nodeOrPropertyName) {
970
+ var _this = this;
733
971
  pattern = pattern !== null && pattern !== void 0 ? pattern : 'in';
734
972
  nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
735
973
  this._resetResults();
736
- const _traverse = (node) => {
974
+ var _traverse = function (node) {
737
975
  switch (pattern) {
738
976
  case 'in':
739
977
  if (node.left)
740
978
  _traverse(node.left);
741
- this._accumulatedByPropertyName(node, nodeOrPropertyName);
979
+ _this._accumulatedByPropertyName(node, nodeOrPropertyName);
742
980
  if (node.right)
743
981
  _traverse(node.right);
744
982
  break;
745
983
  case 'pre':
746
- this._accumulatedByPropertyName(node, nodeOrPropertyName);
984
+ _this._accumulatedByPropertyName(node, nodeOrPropertyName);
747
985
  if (node.left)
748
986
  _traverse(node.left);
749
987
  if (node.right)
@@ -754,13 +992,13 @@ class BinaryTree {
754
992
  _traverse(node.left);
755
993
  if (node.right)
756
994
  _traverse(node.right);
757
- this._accumulatedByPropertyName(node, nodeOrPropertyName);
995
+ _this._accumulatedByPropertyName(node, nodeOrPropertyName);
758
996
  break;
759
997
  }
760
998
  };
761
999
  this.root && _traverse(this.root);
762
1000
  return this._getResultByPropertyName(nodeOrPropertyName);
763
- }
1001
+ };
764
1002
  /**
765
1003
  * Time complexity is O(n)
766
1004
  * Space complexity of Iterative DFS equals to recursive DFS which is O(n) because of the stack
@@ -768,16 +1006,16 @@ class BinaryTree {
768
1006
  * @param nodeOrPropertyName
769
1007
  * @constructor
770
1008
  */
771
- DFSIterative(pattern, nodeOrPropertyName) {
1009
+ BinaryTree.prototype.DFSIterative = function (pattern, nodeOrPropertyName) {
772
1010
  pattern = pattern || 'in';
773
1011
  nodeOrPropertyName = nodeOrPropertyName || 'id';
774
1012
  this._resetResults();
775
1013
  if (!this.root)
776
1014
  return this._getResultByPropertyName(nodeOrPropertyName);
777
1015
  // 0: visit, 1: print
778
- const stack = [{ opt: 0, node: this.root }];
1016
+ var stack = [{ opt: 0, node: this.root }];
779
1017
  while (stack.length > 0) {
780
- const cur = stack.pop();
1018
+ var cur = stack.pop();
781
1019
  if (!cur || !cur.node)
782
1020
  continue;
783
1021
  if (cur.opt === 1) {
@@ -809,16 +1047,28 @@ class BinaryTree {
809
1047
  }
810
1048
  }
811
1049
  return this._getResultByPropertyName(nodeOrPropertyName);
812
- }
813
- levelIterative(node, nodeOrPropertyName) {
1050
+ };
1051
+ /**
1052
+ * The `levelIterative` function performs a level-order traversal on a binary tree and returns the values of the nodes
1053
+ * in an array, based on a specified property name.
1054
+ * @param {BinaryTreeNode<T> | null} node - The `node` parameter is a BinaryTreeNode object representing the starting
1055
+ * node for the level order traversal. It can be null if no specific node is provided, in which case the root node of
1056
+ * the tree is used as the starting node.
1057
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
1058
+ * can be either a `BinaryTreeNode` property name or the string `'id'`. If a property name is provided, the function
1059
+ * will accumulate results based on that property. If no property name is provided, the function will default to
1060
+ * accumulating results
1061
+ * @returns The function `levelIterative` returns an object of type `ResultsByProperty<T>`.
1062
+ */
1063
+ BinaryTree.prototype.levelIterative = function (node, nodeOrPropertyName) {
814
1064
  nodeOrPropertyName = nodeOrPropertyName || 'id';
815
1065
  node = node || this.root;
816
1066
  if (!node)
817
1067
  return [];
818
1068
  this._resetResults();
819
- const queue = [node];
1069
+ var queue = [node];
820
1070
  while (queue.length > 0) {
821
- const cur = queue.shift();
1071
+ var cur = queue.shift();
822
1072
  if (cur) {
823
1073
  this._accumulatedByPropertyName(cur, nodeOrPropertyName);
824
1074
  if (cur.left) {
@@ -830,14 +1080,23 @@ class BinaryTree {
830
1080
  }
831
1081
  }
832
1082
  return this._getResultByPropertyName(nodeOrPropertyName);
833
- }
834
- listLevels(node, nodeOrPropertyName) {
1083
+ };
1084
+ /**
1085
+ * The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
1086
+ * @param {BinaryTreeNode<T> | null} node - The `node` parameter is a BinaryTreeNode object or null. It represents the
1087
+ * root node of a binary tree. If it is null, the function will use the root node of the current binary tree instance.
1088
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
1089
+ * specifies the property of the `BinaryTreeNode` object to collect at each level. It can be one of the following
1090
+ * values:
1091
+ * @returns The function `listLevels` returns a 2D array of `ResultByProperty<T>` objects.
1092
+ */
1093
+ BinaryTree.prototype.listLevels = function (node, nodeOrPropertyName) {
835
1094
  nodeOrPropertyName = nodeOrPropertyName || 'id';
836
1095
  node = node || this.root;
837
1096
  if (!node)
838
1097
  return [];
839
- const levelsNodes = [];
840
- const collectByProperty = (node, level) => {
1098
+ var levelsNodes = [];
1099
+ var collectByProperty = function (node, level) {
841
1100
  switch (nodeOrPropertyName) {
842
1101
  case 'id':
843
1102
  levelsNodes[level].push(node.id);
@@ -857,61 +1116,75 @@ class BinaryTree {
857
1116
  }
858
1117
  };
859
1118
  if (this._loopType === LoopType.recursive) {
860
- const _recursive = (node, level) => {
1119
+ var _recursive_1 = function (node, level) {
861
1120
  if (!levelsNodes[level])
862
1121
  levelsNodes[level] = [];
863
1122
  collectByProperty(node, level);
864
1123
  if (node.left)
865
- _recursive(node.left, level + 1);
1124
+ _recursive_1(node.left, level + 1);
866
1125
  if (node.right)
867
- _recursive(node.right, level + 1);
1126
+ _recursive_1(node.right, level + 1);
868
1127
  };
869
- _recursive(node, 0);
1128
+ _recursive_1(node, 0);
870
1129
  }
871
1130
  else {
872
- const stack = [[node, 0]];
1131
+ var stack = [[node, 0]];
873
1132
  while (stack.length > 0) {
874
- const head = stack.pop();
875
- const [node, level] = head;
1133
+ var head = stack.pop();
1134
+ var _a = __read(head, 2), node_1 = _a[0], level = _a[1];
876
1135
  if (!levelsNodes[level])
877
1136
  levelsNodes[level] = [];
878
- collectByProperty(node, level);
879
- if (node.right)
880
- stack.push([node.right, level + 1]);
881
- if (node.left)
882
- stack.push([node.left, level + 1]);
1137
+ collectByProperty(node_1, level);
1138
+ if (node_1.right)
1139
+ stack.push([node_1.right, level + 1]);
1140
+ if (node_1.left)
1141
+ stack.push([node_1.left, level + 1]);
883
1142
  }
884
1143
  }
885
1144
  return levelsNodes;
886
- }
887
- getPredecessor(node) {
1145
+ };
1146
+ /**
1147
+ * The function returns the predecessor of a given node in a binary tree.
1148
+ * @param node - The parameter `node` is a BinaryTreeNode object, representing a node in a binary tree.
1149
+ * @returns the predecessor of the given node in a binary tree.
1150
+ */
1151
+ BinaryTree.prototype.getPredecessor = function (node) {
888
1152
  if (node.left) {
889
- let predecessor = node.left;
890
- while (predecessor.right && predecessor.right !== node) {
891
- predecessor = predecessor.right;
1153
+ var predecessor = node.left;
1154
+ while (!(predecessor) || predecessor.right && predecessor.right !== node) {
1155
+ if (predecessor) {
1156
+ predecessor = predecessor.right;
1157
+ }
892
1158
  }
893
1159
  return predecessor;
894
1160
  }
895
1161
  else {
896
1162
  return node;
897
1163
  }
898
- }
1164
+ };
899
1165
  /**
1166
+ * The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris
1167
+ * traversal algorithm and returns the results based on the specified property name.
900
1168
  * The time complexity of Morris traversal is O(n), it's may slower than others
901
1169
  * The space complexity Morris traversal is O(1) because no using stack
902
- * @param pattern
903
- * @param nodeOrPropertyName
1170
+ * @param {'in' | 'pre' | 'post'} [pattern] - The `pattern` parameter is an optional parameter that determines the
1171
+ * traversal pattern of the binary tree. It can have one of three values:
1172
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is used to specify the
1173
+ * property of the nodes that you want to retrieve in the results. It can be either the node itself or the name of the
1174
+ * property. If not provided, it defaults to `'id'`.
1175
+ * @returns The function `morris` returns an object of type `ResultsByProperty<T>`.
904
1176
  */
905
- morris(pattern, nodeOrPropertyName) {
1177
+ BinaryTree.prototype.morris = function (pattern, nodeOrPropertyName) {
1178
+ var _this = this;
906
1179
  if (this.root === null)
907
1180
  return [];
908
1181
  pattern = pattern || 'in';
909
1182
  nodeOrPropertyName = nodeOrPropertyName || 'id';
910
1183
  this._resetResults();
911
- let cur = this.root;
912
- const _reverseEdge = (node) => {
913
- let pre = null;
914
- let next = null;
1184
+ var cur = this.root;
1185
+ var _reverseEdge = function (node) {
1186
+ var pre = null;
1187
+ var next = null;
915
1188
  while (node) {
916
1189
  next = node.right;
917
1190
  node.right = pre;
@@ -920,11 +1193,11 @@ class BinaryTree {
920
1193
  }
921
1194
  return pre;
922
1195
  };
923
- const _printEdge = (node) => {
924
- const tail = _reverseEdge(node);
925
- let cur = tail;
1196
+ var _printEdge = function (node) {
1197
+ var tail = _reverseEdge(node);
1198
+ var cur = tail;
926
1199
  while (cur) {
927
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1200
+ _this._accumulatedByPropertyName(cur, nodeOrPropertyName);
928
1201
  cur = cur.right;
929
1202
  }
930
1203
  _reverseEdge(tail);
@@ -933,7 +1206,7 @@ class BinaryTree {
933
1206
  case 'in':
934
1207
  while (cur) {
935
1208
  if (cur.left) {
936
- const predecessor = this.getPredecessor(cur);
1209
+ var predecessor = this.getPredecessor(cur);
937
1210
  if (!predecessor.right) {
938
1211
  predecessor.right = cur;
939
1212
  cur = cur.left;
@@ -950,7 +1223,7 @@ class BinaryTree {
950
1223
  case 'pre':
951
1224
  while (cur) {
952
1225
  if (cur.left) {
953
- const predecessor = this.getPredecessor(cur);
1226
+ var predecessor = this.getPredecessor(cur);
954
1227
  if (!predecessor.right) {
955
1228
  predecessor.right = cur;
956
1229
  this._accumulatedByPropertyName(cur, nodeOrPropertyName);
@@ -970,7 +1243,7 @@ class BinaryTree {
970
1243
  case 'post':
971
1244
  while (cur) {
972
1245
  if (cur.left) {
973
- const predecessor = this.getPredecessor(cur);
1246
+ var predecessor = this.getPredecessor(cur);
974
1247
  if (predecessor.right === null) {
975
1248
  predecessor.right = cur;
976
1249
  cur = cur.left;
@@ -987,6 +1260,111 @@ class BinaryTree {
987
1260
  break;
988
1261
  }
989
1262
  return this._getResultByPropertyName(nodeOrPropertyName);
990
- }
991
- }
1263
+ };
1264
+ /**
1265
+ * The function resets the values of several arrays used for tracking visited nodes and their properties.
1266
+ */
1267
+ BinaryTree.prototype._resetResults = function () {
1268
+ this._visitedId = [];
1269
+ this._visitedVal = [];
1270
+ this._visitedNode = [];
1271
+ this._visitedCount = [];
1272
+ this._visitedLeftSum = [];
1273
+ };
1274
+ /**
1275
+ * The function checks if a given property of a binary tree node matches a specified value, and if so, adds the node to
1276
+ * a result array.
1277
+ * @param cur - The current binary tree node that is being checked.
1278
+ * @param {(BinaryTreeNode<T> | null | undefined)[]} result - An array that stores the matching nodes found during the
1279
+ * traversal.
1280
+ * @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter is the value that we are searching for in
1281
+ * the binary tree nodes. It can be either the `id`, `count`, or `val` property of the node.
1282
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
1283
+ * specifies the property of the `BinaryTreeNode` object that you want to compare with the `nodeProperty` value. It can
1284
+ * be one of the following values: 'id', 'count', or 'val'. If no `propertyName` is provided,
1285
+ * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
1286
+ * stop after finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set to
1287
+ * `true`, the function will stop after finding the first matching node and return `true`. If `onlyOne
1288
+ * @returns a boolean value indicating whether or not a node was pushed into the result array.
1289
+ */
1290
+ BinaryTree.prototype._pushByPropertyNameStopOrNot = function (cur, result, nodeProperty, propertyName, onlyOne) {
1291
+ switch (propertyName) {
1292
+ case 'id':
1293
+ if (cur.id === nodeProperty) {
1294
+ result.push(cur);
1295
+ return !!onlyOne;
1296
+ }
1297
+ break;
1298
+ case 'count':
1299
+ if (cur.count === nodeProperty) {
1300
+ result.push(cur);
1301
+ return !!onlyOne;
1302
+ }
1303
+ break;
1304
+ case 'val':
1305
+ if (cur.val === nodeProperty) {
1306
+ result.push(cur);
1307
+ return !!onlyOne;
1308
+ }
1309
+ break;
1310
+ default:
1311
+ if (cur.id === nodeProperty) {
1312
+ result.push(cur);
1313
+ return !!onlyOne;
1314
+ }
1315
+ break;
1316
+ }
1317
+ };
1318
+ /**
1319
+ * The function `_accumulatedByPropertyName` pushes a property value of a binary tree node into an array based on the
1320
+ * provided property name or a default property name.
1321
+ * @param node - The `node` parameter is of type `BinaryTreeNode<T>`, which represents a node in a binary tree.
1322
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
1323
+ * can be either a string representing a property name or a reference to a node object. If it is a string, it specifies
1324
+ * the property name of the node that should be accumulated. If it is a node object, it specifies the node itself
1325
+ */
1326
+ BinaryTree.prototype._accumulatedByPropertyName = function (node, nodeOrPropertyName) {
1327
+ nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
1328
+ switch (nodeOrPropertyName) {
1329
+ case 'id':
1330
+ this._visitedId.push(node.id);
1331
+ break;
1332
+ case 'val':
1333
+ this._visitedVal.push(node.val);
1334
+ break;
1335
+ case 'node':
1336
+ this._visitedNode.push(node);
1337
+ break;
1338
+ case 'count':
1339
+ this._visitedCount.push(node.count);
1340
+ break;
1341
+ default:
1342
+ this._visitedId.push(node.id);
1343
+ break;
1344
+ }
1345
+ };
1346
+ /**
1347
+ * The function `_getResultByPropertyName` returns different results based on the provided property name or defaulting
1348
+ * to 'id'.
1349
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
1350
+ * can accept a value of type `NodeOrPropertyName`.
1351
+ * @returns The method returns an object of type `ResultsByProperty<T>`.
1352
+ */
1353
+ BinaryTree.prototype._getResultByPropertyName = function (nodeOrPropertyName) {
1354
+ nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
1355
+ switch (nodeOrPropertyName) {
1356
+ case 'id':
1357
+ return this._visitedId;
1358
+ case 'val':
1359
+ return this._visitedVal;
1360
+ case 'node':
1361
+ return this._visitedNode;
1362
+ case 'count':
1363
+ return this._visitedCount;
1364
+ default:
1365
+ return this._visitedId;
1366
+ }
1367
+ };
1368
+ return BinaryTree;
1369
+ }());
992
1370
  exports.BinaryTree = BinaryTree;