data-structure-typed 1.18.0 → 1.18.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (290) hide show
  1. package/README.md +193 -66
  2. package/backup/recursive-type/src/assets/complexities-diff.jpg +0 -0
  3. package/backup/recursive-type/src/assets/data-structure-complexities.jpg +0 -0
  4. package/backup/recursive-type/src/assets/logo.png +0 -0
  5. package/backup/recursive-type/src/assets/overview-diagram-of-data-structures.png +0 -0
  6. package/backup/recursive-type/src/data-structures/binary-tree/aa-tree.ts +3 -0
  7. package/backup/recursive-type/src/data-structures/binary-tree/avl-tree.ts +288 -0
  8. package/backup/recursive-type/src/data-structures/binary-tree/b-tree.ts +3 -0
  9. package/backup/recursive-type/src/data-structures/binary-tree/binary-indexed-tree.ts +78 -0
  10. package/backup/recursive-type/src/data-structures/binary-tree/binary-tree.ts +1502 -0
  11. package/backup/recursive-type/src/data-structures/binary-tree/bst.ts +503 -0
  12. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  13. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  14. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  15. package/backup/recursive-type/src/data-structures/binary-tree/index.ts +11 -0
  16. package/backup/recursive-type/src/data-structures/binary-tree/rb-tree.ts +110 -0
  17. package/backup/recursive-type/src/data-structures/binary-tree/segment-tree.ts +243 -0
  18. package/backup/recursive-type/src/data-structures/binary-tree/splay-tree.ts +3 -0
  19. package/backup/recursive-type/src/data-structures/binary-tree/tree-multiset.ts +55 -0
  20. package/backup/recursive-type/src/data-structures/binary-tree/two-three-tree.ts +3 -0
  21. package/backup/recursive-type/src/data-structures/diagrams/README.md +5 -0
  22. package/backup/recursive-type/src/data-structures/graph/abstract-graph.ts +985 -0
  23. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  24. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  25. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  26. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  27. package/backup/recursive-type/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  28. package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  29. package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  30. package/backup/recursive-type/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  31. package/backup/recursive-type/src/data-structures/graph/diagrams/mst.jpg +0 -0
  32. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  33. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  34. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  35. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  36. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  37. package/backup/recursive-type/src/data-structures/graph/directed-graph.ts +478 -0
  38. package/backup/recursive-type/src/data-structures/graph/index.ts +3 -0
  39. package/backup/recursive-type/src/data-structures/graph/undirected-graph.ts +293 -0
  40. package/backup/recursive-type/src/data-structures/hash/coordinate-map.ts +67 -0
  41. package/backup/recursive-type/src/data-structures/hash/coordinate-set.ts +56 -0
  42. package/backup/recursive-type/src/data-structures/hash/hash-table.ts +3 -0
  43. package/backup/recursive-type/src/data-structures/hash/index.ts +6 -0
  44. package/backup/recursive-type/src/data-structures/hash/pair.ts +3 -0
  45. package/backup/recursive-type/src/data-structures/hash/tree-map.ts +3 -0
  46. package/backup/recursive-type/src/data-structures/hash/tree-set.ts +3 -0
  47. package/backup/recursive-type/src/data-structures/heap/heap.ts +176 -0
  48. package/backup/recursive-type/src/data-structures/heap/index.ts +3 -0
  49. package/backup/recursive-type/src/data-structures/heap/max-heap.ts +31 -0
  50. package/backup/recursive-type/src/data-structures/heap/min-heap.ts +34 -0
  51. package/backup/recursive-type/src/data-structures/index.ts +15 -0
  52. package/backup/recursive-type/src/data-structures/interfaces/abstract-graph.ts +42 -0
  53. package/backup/recursive-type/src/data-structures/interfaces/avl-tree.ts +1 -0
  54. package/backup/recursive-type/src/data-structures/interfaces/binary-tree.ts +56 -0
  55. package/backup/recursive-type/src/data-structures/interfaces/bst.ts +1 -0
  56. package/backup/recursive-type/src/data-structures/interfaces/directed-graph.ts +15 -0
  57. package/backup/recursive-type/src/data-structures/interfaces/doubly-linked-list.ts +1 -0
  58. package/backup/recursive-type/src/data-structures/interfaces/heap.ts +1 -0
  59. package/backup/recursive-type/src/data-structures/interfaces/index.ts +13 -0
  60. package/backup/recursive-type/src/data-structures/interfaces/navigator.ts +1 -0
  61. package/backup/recursive-type/src/data-structures/interfaces/priority-queue.ts +1 -0
  62. package/backup/recursive-type/src/data-structures/interfaces/segment-tree.ts +1 -0
  63. package/backup/recursive-type/src/data-structures/interfaces/singly-linked-list.ts +1 -0
  64. package/backup/recursive-type/src/data-structures/interfaces/tree-multiset.ts +1 -0
  65. package/backup/recursive-type/src/data-structures/interfaces/undirected-graph.ts +3 -0
  66. package/backup/recursive-type/src/data-structures/linked-list/doubly-linked-list.ts +573 -0
  67. package/backup/recursive-type/src/data-structures/linked-list/index.ts +3 -0
  68. package/backup/recursive-type/src/data-structures/linked-list/singly-linked-list.ts +490 -0
  69. package/backup/recursive-type/src/data-structures/linked-list/skip-linked-list.ts +3 -0
  70. package/backup/recursive-type/src/data-structures/matrix/index.ts +4 -0
  71. package/backup/recursive-type/src/data-structures/matrix/matrix.ts +27 -0
  72. package/backup/recursive-type/src/data-structures/matrix/matrix2d.ts +208 -0
  73. package/backup/recursive-type/src/data-structures/matrix/navigator.ts +122 -0
  74. package/backup/recursive-type/src/data-structures/matrix/vector2d.ts +316 -0
  75. package/backup/recursive-type/src/data-structures/priority-queue/index.ts +3 -0
  76. package/backup/recursive-type/src/data-structures/priority-queue/max-priority-queue.ts +49 -0
  77. package/backup/recursive-type/src/data-structures/priority-queue/min-priority-queue.ts +50 -0
  78. package/backup/recursive-type/src/data-structures/priority-queue/priority-queue.ts +354 -0
  79. package/backup/recursive-type/src/data-structures/queue/deque.ts +251 -0
  80. package/backup/recursive-type/src/data-structures/queue/index.ts +2 -0
  81. package/backup/recursive-type/src/data-structures/queue/queue.ts +120 -0
  82. package/backup/recursive-type/src/data-structures/stack/index.ts +1 -0
  83. package/backup/recursive-type/src/data-structures/stack/stack.ts +98 -0
  84. package/backup/recursive-type/src/data-structures/tree/index.ts +1 -0
  85. package/backup/recursive-type/src/data-structures/tree/tree.ts +80 -0
  86. package/backup/recursive-type/src/data-structures/trie/index.ts +1 -0
  87. package/backup/recursive-type/src/data-structures/trie/trie.ts +227 -0
  88. package/backup/recursive-type/src/data-structures/types/abstract-graph.ts +5 -0
  89. package/backup/recursive-type/src/data-structures/types/avl-tree.ts +8 -0
  90. package/backup/recursive-type/src/data-structures/types/binary-tree.ts +10 -0
  91. package/backup/recursive-type/src/data-structures/types/bst.ts +6 -0
  92. package/backup/recursive-type/src/data-structures/types/directed-graph.ts +8 -0
  93. package/backup/recursive-type/src/data-structures/types/doubly-linked-list.ts +1 -0
  94. package/backup/recursive-type/src/data-structures/types/heap.ts +5 -0
  95. package/backup/recursive-type/src/data-structures/types/index.ts +12 -0
  96. package/backup/recursive-type/src/data-structures/types/navigator.ts +13 -0
  97. package/backup/recursive-type/src/data-structures/types/priority-queue.ts +9 -0
  98. package/backup/recursive-type/src/data-structures/types/segment-tree.ts +1 -0
  99. package/backup/recursive-type/src/data-structures/types/singly-linked-list.ts +1 -0
  100. package/backup/recursive-type/src/data-structures/types/tree-multiset.ts +1 -0
  101. package/backup/recursive-type/src/index.ts +1 -0
  102. package/backup/recursive-type/src/utils/index.ts +2 -0
  103. package/backup/recursive-type/src/utils/types/index.ts +1 -0
  104. package/backup/recursive-type/src/utils/types/utils.ts +6 -0
  105. package/backup/recursive-type/src/utils/utils.ts +78 -0
  106. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +333 -0
  107. package/dist/data-structures/binary-tree/abstract-binary-tree.js +1455 -0
  108. package/dist/data-structures/binary-tree/avl-tree.d.ts +20 -25
  109. package/dist/data-structures/binary-tree/avl-tree.js +10 -18
  110. package/dist/data-structures/binary-tree/binary-tree.d.ts +18 -345
  111. package/dist/data-structures/binary-tree/binary-tree.js +39 -1444
  112. package/dist/data-structures/binary-tree/bst.d.ts +24 -31
  113. package/dist/data-structures/binary-tree/bst.js +46 -53
  114. package/dist/data-structures/binary-tree/index.d.ts +1 -0
  115. package/dist/data-structures/binary-tree/index.js +1 -0
  116. package/dist/data-structures/binary-tree/rb-tree.d.ts +1 -2
  117. package/dist/data-structures/binary-tree/rb-tree.js +64 -5
  118. package/dist/data-structures/binary-tree/tree-multiset.d.ts +11 -25
  119. package/dist/data-structures/binary-tree/tree-multiset.js +29 -31
  120. package/dist/data-structures/graph/abstract-graph.d.ts +56 -58
  121. package/dist/data-structures/graph/abstract-graph.js +84 -68
  122. package/dist/data-structures/graph/directed-graph.d.ts +124 -95
  123. package/dist/data-structures/graph/directed-graph.js +156 -108
  124. package/dist/data-structures/graph/undirected-graph.d.ts +83 -58
  125. package/dist/data-structures/graph/undirected-graph.js +98 -71
  126. package/dist/data-structures/hash/coordinate-set.d.ts +1 -1
  127. package/dist/data-structures/index.d.ts +1 -0
  128. package/dist/data-structures/index.js +1 -0
  129. package/dist/data-structures/interfaces/abstract-graph.d.ts +22 -0
  130. package/dist/data-structures/interfaces/abstract-graph.js +2 -0
  131. package/dist/data-structures/interfaces/avl-tree.d.ts +1 -0
  132. package/dist/data-structures/interfaces/avl-tree.js +2 -0
  133. package/dist/data-structures/interfaces/binary-tree.d.ts +26 -0
  134. package/dist/data-structures/interfaces/binary-tree.js +2 -0
  135. package/dist/data-structures/interfaces/bst.d.ts +1 -0
  136. package/dist/data-structures/interfaces/bst.js +2 -0
  137. package/dist/data-structures/interfaces/directed-graph.d.ts +9 -0
  138. package/dist/data-structures/interfaces/directed-graph.js +2 -0
  139. package/dist/data-structures/interfaces/doubly-linked-list.d.ts +1 -0
  140. package/dist/data-structures/interfaces/doubly-linked-list.js +2 -0
  141. package/dist/data-structures/interfaces/heap.d.ts +1 -0
  142. package/dist/data-structures/interfaces/heap.js +2 -0
  143. package/dist/data-structures/interfaces/index.d.ts +13 -0
  144. package/dist/data-structures/interfaces/index.js +29 -0
  145. package/dist/data-structures/interfaces/navigator.d.ts +1 -0
  146. package/dist/data-structures/interfaces/navigator.js +2 -0
  147. package/dist/data-structures/interfaces/priority-queue.d.ts +1 -0
  148. package/dist/data-structures/interfaces/priority-queue.js +2 -0
  149. package/dist/data-structures/interfaces/segment-tree.d.ts +1 -0
  150. package/dist/data-structures/interfaces/segment-tree.js +2 -0
  151. package/dist/data-structures/interfaces/singly-linked-list.d.ts +1 -0
  152. package/dist/data-structures/interfaces/singly-linked-list.js +2 -0
  153. package/dist/data-structures/interfaces/tree-multiset.d.ts +1 -0
  154. package/dist/data-structures/interfaces/tree-multiset.js +2 -0
  155. package/dist/data-structures/interfaces/undirected-graph.d.ts +2 -0
  156. package/dist/data-structures/interfaces/undirected-graph.js +2 -0
  157. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +1 -1
  158. package/dist/data-structures/linked-list/singly-linked-list.d.ts +1 -1
  159. package/dist/data-structures/priority-queue/priority-queue.d.ts +1 -1
  160. package/dist/data-structures/priority-queue/priority-queue.js +4 -4
  161. package/dist/data-structures/queue/deque.d.ts +5 -5
  162. package/dist/data-structures/queue/deque.js +6 -6
  163. package/dist/data-structures/queue/queue.d.ts +1 -1
  164. package/dist/data-structures/stack/stack.d.ts +1 -1
  165. package/dist/data-structures/types/abstract-binary-tree.d.ts +32 -0
  166. package/dist/data-structures/types/abstract-binary-tree.js +21 -0
  167. package/dist/data-structures/types/abstract-graph.d.ts +1 -20
  168. package/dist/data-structures/types/avl-tree.d.ts +3 -4
  169. package/dist/data-structures/types/binary-tree.d.ts +3 -10
  170. package/dist/data-structures/types/bst.d.ts +10 -4
  171. package/dist/data-structures/types/bst.js +7 -0
  172. package/dist/data-structures/types/directed-graph.d.ts +5 -9
  173. package/dist/data-structures/types/directed-graph.js +7 -0
  174. package/dist/data-structures/types/heap.d.ts +2 -2
  175. package/dist/data-structures/types/helpers.d.ts +8 -0
  176. package/dist/data-structures/types/helpers.js +2 -0
  177. package/dist/data-structures/types/index.d.ts +3 -1
  178. package/dist/data-structures/types/index.js +3 -1
  179. package/dist/data-structures/types/navigator.d.ts +2 -2
  180. package/dist/data-structures/types/priority-queue.d.ts +2 -2
  181. package/dist/data-structures/types/rb-tree.d.ts +6 -0
  182. package/dist/data-structures/types/rb-tree.js +8 -0
  183. package/dist/data-structures/types/tree-multiset.d.ts +5 -4
  184. package/docs/assets/search.js +1 -1
  185. package/docs/classes/AVLTree.html +310 -309
  186. package/docs/classes/AVLTreeNode.html +122 -68
  187. package/docs/classes/AaTree.html +30 -17
  188. package/docs/classes/AbstractBinaryTree.html +2023 -0
  189. package/docs/classes/AbstractBinaryTreeNode.html +491 -0
  190. package/docs/classes/AbstractEdge.html +84 -39
  191. package/docs/classes/AbstractGraph.html +235 -119
  192. package/docs/classes/AbstractVertex.html +87 -35
  193. package/docs/classes/ArrayDeque.html +43 -30
  194. package/docs/classes/BST.html +297 -285
  195. package/docs/classes/BSTNode.html +123 -62
  196. package/docs/classes/BTree.html +30 -17
  197. package/docs/classes/BinaryIndexedTree.html +38 -25
  198. package/docs/classes/BinaryTree.html +596 -589
  199. package/docs/classes/BinaryTreeNode.html +181 -161
  200. package/docs/classes/Character.html +33 -20
  201. package/docs/classes/CoordinateMap.html +38 -25
  202. package/docs/classes/CoordinateSet.html +39 -26
  203. package/docs/classes/Deque.html +63 -50
  204. package/docs/classes/DirectedEdge.html +90 -46
  205. package/docs/classes/DirectedGraph.html +374 -212
  206. package/docs/classes/DirectedVertex.html +79 -41
  207. package/docs/classes/DoublyLinkedList.html +68 -55
  208. package/docs/classes/DoublyLinkedListNode.html +40 -27
  209. package/docs/classes/HashTable.html +30 -17
  210. package/docs/classes/Heap.html +45 -32
  211. package/docs/classes/HeapItem.html +37 -24
  212. package/docs/classes/Matrix2D.html +45 -32
  213. package/docs/classes/MatrixNTI2D.html +33 -20
  214. package/docs/classes/MaxHeap.html +45 -32
  215. package/docs/classes/MaxPriorityQueue.html +83 -65
  216. package/docs/classes/MinHeap.html +45 -32
  217. package/docs/classes/MinPriorityQueue.html +83 -65
  218. package/docs/classes/Navigator.html +40 -27
  219. package/docs/classes/ObjectDeque.html +78 -55
  220. package/docs/classes/Pair.html +30 -17
  221. package/docs/classes/PriorityQueue.html +78 -60
  222. package/docs/classes/Queue.html +45 -32
  223. package/docs/classes/SegmentTree.html +46 -33
  224. package/docs/classes/SegmentTreeNode.html +49 -36
  225. package/docs/classes/SinglyLinkedList.html +65 -52
  226. package/docs/classes/SinglyLinkedListNode.html +37 -24
  227. package/docs/classes/SkipLinkedList.html +30 -17
  228. package/docs/classes/SplayTree.html +30 -17
  229. package/docs/classes/Stack.html +43 -30
  230. package/docs/classes/TreeMap.html +30 -17
  231. package/docs/classes/TreeMultiSet.html +330 -316
  232. package/docs/classes/TreeMultiSetNode.html +450 -0
  233. package/docs/classes/TreeNode.html +45 -32
  234. package/docs/classes/TreeSet.html +30 -17
  235. package/docs/classes/Trie.html +42 -29
  236. package/docs/classes/TrieNode.html +40 -27
  237. package/docs/classes/TwoThreeTree.html +30 -17
  238. package/docs/classes/UndirectedEdge.html +86 -56
  239. package/docs/classes/UndirectedGraph.html +286 -165
  240. package/docs/classes/UndirectedVertex.html +79 -41
  241. package/docs/classes/Vector2D.html +57 -44
  242. package/docs/enums/CP.html +36 -23
  243. package/docs/enums/FamilyPosition.html +48 -35
  244. package/docs/enums/LoopType.html +42 -29
  245. package/docs/{interfaces/PriorityQueueOptions.html → enums/RBColor.html} +52 -55
  246. package/docs/{interfaces/AVLTreeDeleted.html → enums/TopologicalProperty.html} +59 -48
  247. package/docs/index.html +211 -73
  248. package/docs/interfaces/{NavigatorParams.html → IBinaryTree.html} +56 -67
  249. package/docs/interfaces/IBinaryTreeNode.html +396 -0
  250. package/docs/interfaces/IDirectedGraph.html +36 -23
  251. package/docs/interfaces/IGraph.html +134 -93
  252. package/docs/interfaces/{HeapOptions.html → IUNDirectedGraph.html} +38 -57
  253. package/docs/modules.html +57 -31
  254. package/docs/types/{ToThunkFn.html → AVLTreeOptions.html} +35 -27
  255. package/docs/types/AbstractBinaryTreeOptions.html +150 -0
  256. package/docs/types/AbstractRecursiveBinaryTreeNode.html +146 -0
  257. package/docs/types/{ResultsByProperty.html → AbstractResultByProperty.html} +35 -22
  258. package/docs/types/{TreeMultiSetDeletedResult.html → AbstractResultsByProperty.html} +35 -29
  259. package/docs/types/BSTComparator.html +30 -17
  260. package/docs/types/{TrlAsyncFn.html → BSTOptions.html} +36 -31
  261. package/docs/types/BinaryTreeDeletedResult.html +153 -0
  262. package/docs/types/BinaryTreeNodeId.html +30 -17
  263. package/docs/types/BinaryTreeNodePropertyName.html +30 -17
  264. package/docs/types/{BinaryTreeDeleted.html → BinaryTreeOptions.html} +35 -31
  265. package/docs/types/DFSOrderPattern.html +30 -17
  266. package/docs/types/DijkstraResult.html +30 -17
  267. package/docs/types/Direction.html +30 -17
  268. package/docs/types/{SpecifyOptional.html → EdgeId.html} +34 -28
  269. package/docs/types/{TrlFn.html → HeapOptions.html} +45 -24
  270. package/docs/types/IdObject.html +151 -0
  271. package/docs/types/{BSTDeletedResult.html → KeyValObject.html} +36 -30
  272. package/docs/types/NavigatorParams.html +175 -0
  273. package/docs/types/NodeOrPropertyName.html +30 -17
  274. package/docs/types/PriorityQueueComparator.html +30 -17
  275. package/docs/types/PriorityQueueDFSOrderPattern.html +30 -17
  276. package/docs/types/PriorityQueueOptions.html +155 -0
  277. package/docs/types/{Thunk.html → RBTreeOptions.html} +35 -27
  278. package/docs/types/RecursiveAVLTreeNode.html +146 -0
  279. package/docs/types/RecursiveBSTNode.html +146 -0
  280. package/docs/types/RecursiveBinaryTreeNode.html +146 -0
  281. package/docs/types/RecursiveTreeMultiSetNode.html +146 -0
  282. package/docs/types/SegmentTreeNodeVal.html +30 -17
  283. package/docs/types/TopologicalStatus.html +30 -17
  284. package/docs/types/TreeMultiSetOptions.html +146 -0
  285. package/docs/types/Turning.html +30 -17
  286. package/docs/types/VertexId.html +30 -17
  287. package/notes/note.md +8 -1
  288. package/package.json +10 -2
  289. package/docs/classes/RBTree.html +0 -153
  290. package/docs/types/ResultByProperty.html +0 -133
@@ -6,1465 +6,60 @@
6
6
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
7
7
  * @license MIT License
8
8
  */
9
- var __values = (this && this.__values) || function(o) {
10
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
11
- if (m) return m.call(o);
12
- if (o && typeof o.length === "number") return {
13
- next: function () {
14
- if (o && i >= o.length) o = void 0;
15
- return { value: o && o[i++], done: !o };
16
- }
17
- };
18
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
19
- };
20
- var __read = (this && this.__read) || function (o, n) {
21
- var m = typeof Symbol === "function" && o[Symbol.iterator];
22
- if (!m) return o;
23
- var i = m.call(o), r, ar = [], e;
24
- try {
25
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
26
- }
27
- catch (error) { e = { error: error }; }
28
- finally {
29
- try {
30
- if (r && !r.done && (m = i["return"])) m.call(i);
31
- }
32
- finally { if (e) throw e.error; }
33
- }
34
- return ar;
35
- };
9
+ var __extends = (this && this.__extends) || (function () {
10
+ var extendStatics = function (d, b) {
11
+ extendStatics = Object.setPrototypeOf ||
12
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
13
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
14
+ return extendStatics(d, b);
15
+ };
16
+ return function (d, b) {
17
+ if (typeof b !== "function" && b !== null)
18
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
19
+ extendStatics(d, b);
20
+ function __() { this.constructor = d; }
21
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
22
+ };
23
+ })();
36
24
  Object.defineProperty(exports, "__esModule", { value: true });
37
- exports.BinaryTree = exports.BinaryTreeNode = exports.LoopType = exports.FamilyPosition = void 0;
38
- var utils_1 = require("../../utils");
39
- /* This enumeration defines the position of a node within a family tree composed of three associated nodes, where 'root' represents the root node of the family tree, 'left' represents the left child node, and 'right' represents the right child node. */
40
- var FamilyPosition;
41
- (function (FamilyPosition) {
42
- FamilyPosition[FamilyPosition["root"] = 0] = "root";
43
- FamilyPosition[FamilyPosition["left"] = 1] = "left";
44
- FamilyPosition[FamilyPosition["right"] = 2] = "right";
45
- })(FamilyPosition || (exports.FamilyPosition = FamilyPosition = {}));
46
- /**
47
- * Enum representing different loop types.
48
- *
49
- * - `iterative`: Indicates the iterative loop type (with loops that use iterations).
50
- * - `recursive`: Indicates the recursive loop type (with loops that call themselves).
51
- */
52
- var LoopType;
53
- (function (LoopType) {
54
- LoopType[LoopType["iterative"] = 1] = "iterative";
55
- LoopType[LoopType["recursive"] = 2] = "recursive";
56
- })(LoopType || (exports.LoopType = LoopType = {}));
57
- var BinaryTreeNode = /** @class */ (function () {
58
- function BinaryTreeNode(id, val, count) {
59
- this._familyPosition = FamilyPosition.root;
60
- this._count = 1;
61
- this._height = 0;
62
- this._id = id;
63
- this._val = val;
64
- this._count = count !== null && count !== void 0 ? count : 1;
25
+ exports.BinaryTree = exports.BinaryTreeNode = void 0;
26
+ var abstract_binary_tree_1 = require("./abstract-binary-tree");
27
+ var BinaryTreeNode = /** @class */ (function (_super) {
28
+ __extends(BinaryTreeNode, _super);
29
+ function BinaryTreeNode() {
30
+ return _super !== null && _super.apply(this, arguments) || this;
65
31
  }
66
- Object.defineProperty(BinaryTreeNode.prototype, "id", {
67
- get: function () {
68
- return this._id;
69
- },
70
- set: function (v) {
71
- this._id = v;
72
- },
73
- enumerable: false,
74
- configurable: true
75
- });
76
- Object.defineProperty(BinaryTreeNode.prototype, "val", {
77
- get: function () {
78
- return this._val;
79
- },
80
- set: function (v) {
81
- this._val = v;
82
- },
83
- enumerable: false,
84
- configurable: true
85
- });
86
- Object.defineProperty(BinaryTreeNode.prototype, "left", {
87
- get: function () {
88
- return this._left;
89
- },
90
- set: function (v) {
91
- if (v) {
92
- v.parent = this;
93
- v.familyPosition = FamilyPosition.left;
94
- }
95
- this._left = v;
96
- },
97
- enumerable: false,
98
- configurable: true
99
- });
100
- Object.defineProperty(BinaryTreeNode.prototype, "right", {
101
- get: function () {
102
- return this._right;
103
- },
104
- set: function (v) {
105
- if (v) {
106
- v.parent = this;
107
- v.familyPosition = FamilyPosition.right;
108
- }
109
- this._right = v;
110
- },
111
- enumerable: false,
112
- configurable: true
113
- });
114
- Object.defineProperty(BinaryTreeNode.prototype, "parent", {
115
- get: function () {
116
- return this._parent;
117
- },
118
- set: function (v) {
119
- this._parent = v;
120
- },
121
- enumerable: false,
122
- configurable: true
123
- });
124
- Object.defineProperty(BinaryTreeNode.prototype, "familyPosition", {
125
- get: function () {
126
- return this._familyPosition;
127
- },
128
- set: function (v) {
129
- this._familyPosition = v;
130
- },
131
- enumerable: false,
132
- configurable: true
133
- });
134
- Object.defineProperty(BinaryTreeNode.prototype, "count", {
135
- get: function () {
136
- return this._count;
137
- },
138
- set: function (v) {
139
- this._count = v;
140
- },
141
- enumerable: false,
142
- configurable: true
143
- });
144
- Object.defineProperty(BinaryTreeNode.prototype, "height", {
145
- get: function () {
146
- return this._height;
147
- },
148
- set: function (v) {
149
- this._height = v;
150
- },
151
- enumerable: false,
152
- configurable: true
153
- });
154
- BinaryTreeNode.prototype.swapLocation = function (swapNode) {
155
- var val = swapNode.val, count = swapNode.count, height = swapNode.height;
156
- var tempNode = new BinaryTreeNode(swapNode.id, val);
157
- tempNode.val = val;
158
- tempNode.count = count;
159
- tempNode.height = height;
160
- swapNode.id = this.id;
161
- swapNode.val = this.val;
162
- swapNode.count = this.count;
163
- swapNode.height = this.height;
164
- this.id = tempNode.id;
165
- this.val = tempNode.val;
166
- this.count = tempNode.count;
167
- this.height = tempNode.height;
168
- return swapNode;
169
- };
170
- BinaryTreeNode.prototype.clone = function () {
171
- return new BinaryTreeNode(this.id, this.val, this.count);
32
+ BinaryTreeNode.prototype._createNode = function (id, val, count) {
33
+ return val !== null ? new BinaryTreeNode(id, val, count) : null;
172
34
  };
173
35
  return BinaryTreeNode;
174
- }());
36
+ }(abstract_binary_tree_1.AbstractBinaryTreeNode));
175
37
  exports.BinaryTreeNode = BinaryTreeNode;
176
- var BinaryTree = /** @class */ (function () {
38
+ var BinaryTree = /** @class */ (function (_super) {
39
+ __extends(BinaryTree, _super);
177
40
  /**
178
41
  * The constructor function accepts an optional options object and sets the values of loopType, autoIncrementId, and
179
42
  * isDuplicatedVal based on the provided options.
180
43
  * @param [options] - An optional object that can contain the following properties:
181
44
  */
182
45
  function BinaryTree(options) {
183
- this._loopType = LoopType.iterative;
184
- this._visitedId = [];
185
- this._visitedVal = [];
186
- this._visitedNode = [];
187
- this._visitedCount = [];
188
- this._visitedLeftSum = [];
189
- this._autoIncrementId = false;
190
- this._maxId = -1;
191
- this._isDuplicatedVal = false;
192
- this._root = null;
193
- this._size = 0;
194
- this._count = 0;
195
- if (options !== undefined) {
196
- 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;
197
- this._isDuplicatedVal = isDuplicatedVal;
198
- this._autoIncrementId = autoIncrementId;
199
- this._loopType = loopType;
200
- }
46
+ return _super.call(this) || this;
201
47
  }
202
- Object.defineProperty(BinaryTree.prototype, "loopType", {
203
- get: function () {
204
- return this._loopType;
205
- },
206
- enumerable: false,
207
- configurable: true
208
- });
209
- Object.defineProperty(BinaryTree.prototype, "visitedId", {
210
- get: function () {
211
- return this._visitedId;
212
- },
213
- enumerable: false,
214
- configurable: true
215
- });
216
- Object.defineProperty(BinaryTree.prototype, "visitedVal", {
217
- get: function () {
218
- return this._visitedVal;
219
- },
220
- enumerable: false,
221
- configurable: true
222
- });
223
- Object.defineProperty(BinaryTree.prototype, "visitedNode", {
224
- get: function () {
225
- return this._visitedNode;
226
- },
227
- enumerable: false,
228
- configurable: true
229
- });
230
- Object.defineProperty(BinaryTree.prototype, "visitedCount", {
231
- get: function () {
232
- return this._visitedCount;
233
- },
234
- enumerable: false,
235
- configurable: true
236
- });
237
- Object.defineProperty(BinaryTree.prototype, "visitedLeftSum", {
238
- get: function () {
239
- return this._visitedLeftSum;
240
- },
241
- enumerable: false,
242
- configurable: true
243
- });
244
- Object.defineProperty(BinaryTree.prototype, "autoIncrementId", {
245
- get: function () {
246
- return this._autoIncrementId;
247
- },
248
- enumerable: false,
249
- configurable: true
250
- });
251
- Object.defineProperty(BinaryTree.prototype, "maxId", {
252
- get: function () {
253
- return this._maxId;
254
- },
255
- enumerable: false,
256
- configurable: true
257
- });
258
- Object.defineProperty(BinaryTree.prototype, "isDuplicatedVal", {
259
- get: function () {
260
- return this._isDuplicatedVal;
261
- },
262
- enumerable: false,
263
- configurable: true
264
- });
265
- Object.defineProperty(BinaryTree.prototype, "root", {
266
- get: function () {
267
- return this._root;
268
- },
269
- enumerable: false,
270
- configurable: true
271
- });
272
- Object.defineProperty(BinaryTree.prototype, "size", {
273
- get: function () {
274
- return this._size;
275
- },
276
- enumerable: false,
277
- configurable: true
278
- });
279
- Object.defineProperty(BinaryTree.prototype, "count", {
280
- get: function () {
281
- return this._count;
282
- },
283
- enumerable: false,
284
- configurable: true
285
- });
286
48
  /**
287
- * The function creates a new binary tree node with the given id, value, and count, or returns null if the value is
288
- * null.
49
+ * The function creates a new binary tree node with the given id, value, and count if the value is not null, otherwise
50
+ * it returns null.
289
51
  * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is of type
290
- * `BinaryTreeNodeId`, which could be a string or a number, depending on how you want to identify your nodes.
291
- * @param {T | null} val - The `val` parameter represents the value to be stored in the binary tree node. It can be of
292
- * any type `T` or `null`.
293
- * @param {number} [count] - The count parameter is an optional parameter that represents the number of occurrences of
294
- * the value in the binary tree node. It is of type number.
295
- * @returns The function `createNode` returns a `BinaryTreeNode<T>` object if the `val` parameter is not null.
296
- * Otherwise, it returns null.
297
- */
298
- BinaryTree.prototype.createNode = function (id, val, count) {
299
- return val !== null ? new BinaryTreeNode(id, val, count) : null;
300
- };
301
- /**
302
- * The clear function resets the state of an object by setting its properties to their initial values.
303
- */
304
- BinaryTree.prototype.clear = function () {
305
- this._setRoot(null);
306
- this._setSize(0);
307
- this._setCount(0);
308
- this._setMaxId(-1);
309
- };
310
- /**
311
- * The function checks if the size of an object is equal to zero and returns a boolean value.
312
- * @returns A boolean value indicating whether the size of the object is 0 or not.
313
- */
314
- BinaryTree.prototype.isEmpty = function () {
315
- return this.size === 0;
316
- };
317
- /**
318
- * The `add` function inserts a new node with a given ID and value into a binary tree, updating the count if the node
319
- * already exists.
320
- * @param {BinaryTreeNodeId} id - The id parameter is the identifier of the binary tree node. It is used to uniquely
321
- * identify each node in the binary tree.
322
- * @param {T} val - The value to be inserted into the binary tree.
323
- * @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the
324
- * value should be inserted into the binary tree. If not provided, it defaults to 1.
325
- * @returns The function `add` returns a `BinaryTreeNode<T>` object if a new node is inserted, or `null` if no new node
326
- * is inserted, or `undefined` if the insertion fails.
327
- */
328
- BinaryTree.prototype.add = function (id, val, count) {
329
- var _this = this;
330
- count = count !== null && count !== void 0 ? count : 1;
331
- var _bfs = function (root, newNode) {
332
- var queue = [root];
333
- while (queue.length > 0) {
334
- var cur = queue.shift();
335
- if (cur) {
336
- var inserted_1 = _this.addTo(newNode, cur);
337
- if (inserted_1 !== undefined)
338
- return inserted_1;
339
- if (cur.left)
340
- queue.push(cur.left);
341
- if (cur.right)
342
- queue.push(cur.right);
343
- }
344
- else
345
- return;
346
- }
347
- return;
348
- };
349
- var inserted;
350
- var needInsert = val !== null ? new BinaryTreeNode(id, val, count) : null;
351
- var existNode = val !== null ? this.get(id, 'id') : null;
352
- if (this.root) {
353
- if (existNode) {
354
- existNode.count += count;
355
- existNode.val = val;
356
- if (needInsert !== null) {
357
- this._setCount(this.count + count);
358
- inserted = existNode;
359
- }
360
- }
361
- else {
362
- inserted = _bfs(this.root, needInsert);
363
- }
364
- }
365
- else {
366
- this._setRoot(val !== null ? new BinaryTreeNode(id, val, count) : null);
367
- if (needInsert !== null) {
368
- this._setSize(1);
369
- this._setCount(count);
370
- }
371
- inserted = this.root;
372
- }
373
- return inserted;
374
- };
375
- /**
376
- * The function inserts a new node into a binary tree as the left or right child of a given parent node.
377
- * @param {BinaryTreeNode<T> | null} newNode - The `newNode` parameter is an instance of the `BinaryTreeNode` class or
378
- * `null`. It represents the node that needs to be inserted into the binary tree.
379
- * @param parent - The `parent` parameter is a BinaryTreeNode object representing the parent node to which the new node
380
- * will be inserted as a child.
381
- * @returns The method returns the newly inserted node, either as the left child or the right child of the parent node.
382
- */
383
- BinaryTree.prototype.addTo = function (newNode, parent) {
384
- var _a, _b;
385
- if (parent) {
386
- if (parent.left === undefined) {
387
- if (newNode) {
388
- newNode.parent = parent;
389
- newNode.familyPosition = FamilyPosition.left;
390
- }
391
- parent.left = newNode;
392
- if (newNode !== null) {
393
- this._setSize(this.size + 1);
394
- this._setCount((_a = this.count + (newNode === null || newNode === void 0 ? void 0 : newNode.count)) !== null && _a !== void 0 ? _a : 0);
395
- }
396
- return parent.left;
397
- }
398
- else if (parent.right === undefined) {
399
- if (newNode) {
400
- newNode.parent = parent;
401
- newNode.familyPosition = FamilyPosition.right;
402
- }
403
- parent.right = newNode;
404
- if (newNode !== null) {
405
- this._setSize(this.size + 1);
406
- this._setCount((_b = this.count + (newNode === null || newNode === void 0 ? void 0 : newNode.count)) !== null && _b !== void 0 ? _b : 0);
407
- }
408
- return parent.right;
409
- }
410
- else {
411
- return;
412
- }
413
- }
414
- else {
415
- return;
416
- }
417
- };
418
- /**
419
- * The `addMany` function inserts multiple items into a binary tree and returns an array of the inserted nodes or
420
- * null/undefined values.
421
- * @param {T[] | BinaryTreeNode<T>[]} data - The `data` parameter can be either an array of elements of type `T` or an
422
- * array of `BinaryTreeNode<T>` objects.
423
- * @returns The function `addMany` returns an array of `BinaryTreeNode<T>`, `null`, or `undefined` values.
424
- */
425
- BinaryTree.prototype.addMany = function (data) {
426
- var e_1, _a, e_2, _b;
427
- var _c;
428
- var inserted = [];
429
- var map = new Map();
430
- if (!this._isDuplicatedVal) {
431
- try {
432
- for (var data_1 = __values(data), data_1_1 = data_1.next(); !data_1_1.done; data_1_1 = data_1.next()) {
433
- var i = data_1_1.value;
434
- map.set(i, ((_c = map.get(i)) !== null && _c !== void 0 ? _c : 0) + 1);
435
- }
436
- }
437
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
438
- finally {
439
- try {
440
- if (data_1_1 && !data_1_1.done && (_a = data_1.return)) _a.call(data_1);
441
- }
442
- finally { if (e_1) throw e_1.error; }
443
- }
444
- }
445
- try {
446
- for (var data_2 = __values(data), data_2_1 = data_2.next(); !data_2_1.done; data_2_1 = data_2.next()) {
447
- var item = data_2_1.value;
448
- var count = this._isDuplicatedVal ? 1 : map.get(item);
449
- if (item instanceof BinaryTreeNode) {
450
- inserted.push(this.add(item.id, item.val, item.count));
451
- }
452
- else if (typeof item === 'number' && !this._autoIncrementId) {
453
- if (!this._isDuplicatedVal) {
454
- if (map.get(item) !== undefined) {
455
- inserted.push(this.add(item, item, count));
456
- map.delete(item);
457
- }
458
- }
459
- else {
460
- inserted.push(this.add(item, item, 1));
461
- }
462
- }
463
- else {
464
- if (item !== null) {
465
- if (!this._isDuplicatedVal) {
466
- if (map.get(item) !== undefined) {
467
- inserted.push(this.add(++this._maxId, item, count));
468
- map.delete(item);
469
- }
470
- }
471
- else {
472
- inserted.push(this.add(++this._maxId, item, 1));
473
- }
474
- }
475
- else {
476
- inserted.push(this.add(Number.MAX_SAFE_INTEGER, item, 0));
477
- }
478
- }
479
- }
480
- }
481
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
482
- finally {
483
- try {
484
- if (data_2_1 && !data_2_1.done && (_b = data_2.return)) _b.call(data_2);
485
- }
486
- finally { if (e_2) throw e_2.error; }
487
- }
488
- return inserted;
489
- };
490
- /**
491
- * The `fill` function clears the current data and inserts new data, returning a boolean indicating if the insertion
492
- * was successful.
493
- * @param {T[] | BinaryTreeNode<T>[]} data - The `data` parameter can be either an array of elements of type `T` or an
494
- * array of `BinaryTreeNode<T>` objects.
495
- * @returns The method is returning a boolean value.
496
- */
497
- BinaryTree.prototype.fill = function (data) {
498
- this.clear();
499
- return data.length === this.addMany(data).length;
500
- };
501
- /**
502
- * The function removes a node from a binary tree and returns information about the deleted node.
503
- * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that you want to remove.
504
- * It is of type `BinaryTreeNodeId`.
505
- * @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
506
- * whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
507
- * not be decremented and the overall count of the binary tree will not be updated. If `
508
- * @returns An array of objects is being returned. Each object in the array has two properties: "deleted" and
509
- * "needBalanced". The "deleted" property contains the deleted node or undefined if no node was deleted. The
510
- * "needBalanced" property is always null.
511
- */
512
- BinaryTree.prototype.remove = function (id, ignoreCount) {
513
- var nodes = this.getNodes(id, 'id', true);
514
- var node = nodes[0];
515
- if (!node)
516
- node = undefined;
517
- else if (node.count > 1 && !ignoreCount) {
518
- node.count--;
519
- this._setCount(this.count - 1);
520
- }
521
- else if (node instanceof BinaryTreeNode) {
522
- var _a = __read(this.getSubTreeSizeAndCount(node), 2), subSize = _a[0], subCount = _a[1];
523
- switch (node.familyPosition) {
524
- case 0:
525
- this._setSize(this.size - subSize);
526
- this._setCount(this.count - subCount);
527
- node = undefined;
528
- break;
529
- case 1:
530
- if (node.parent) {
531
- this._setSize(this.size - subSize);
532
- this._setCount(this.count - subCount);
533
- node.parent.left = null;
534
- }
535
- break;
536
- case 2:
537
- if (node.parent) {
538
- this._setSize(this.size - subSize);
539
- this._setCount(this.count - subCount);
540
- node.parent.right = null;
541
- }
542
- break;
543
- }
544
- }
545
- return [{ deleted: node, needBalanced: null }];
546
- };
547
- /**
548
- * The function calculates the depth of a binary tree node by traversing its parent nodes.
549
- * @param node - BinaryTreeNode<T> - This is the node for which we want to calculate the depth. It is a generic type,
550
- * meaning it can represent any type of data that we want to store in the node.
551
- * @returns The depth of the given binary tree node.
552
- */
553
- BinaryTree.prototype.getDepth = function (node) {
554
- var depth = 0;
555
- while (node.parent) {
556
- depth++;
557
- node = node.parent;
558
- }
559
- return depth;
560
- };
561
- /**
562
- * The `getHeight` function calculates the maximum height of a binary tree using either a recursive or iterative
563
- * approach.
564
- * @param {BinaryTreeNode<T> | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type
565
- * `BinaryTreeNode<T> | null`. It represents the starting node from which to calculate the height of the binary tree.
566
- * If no value is provided for `beginRoot`, the function will use the `root` property of the class instance as
567
- * @returns the height of the binary tree.
568
- */
569
- BinaryTree.prototype.getHeight = function (beginRoot) {
570
- var _a, _b, _c;
571
- beginRoot = beginRoot !== null && beginRoot !== void 0 ? beginRoot : this.root;
572
- if (!beginRoot)
573
- return -1;
574
- if (this._loopType === LoopType.recursive) {
575
- var _getMaxHeight_1 = function (cur) {
576
- if (!cur)
577
- return -1;
578
- var leftHeight = _getMaxHeight_1(cur.left);
579
- var rightHeight = _getMaxHeight_1(cur.right);
580
- return Math.max(leftHeight, rightHeight) + 1;
581
- };
582
- return _getMaxHeight_1(beginRoot);
583
- }
584
- else {
585
- var stack = [];
586
- var node = beginRoot, last = null;
587
- var depths = new Map();
588
- while (stack.length > 0 || node) {
589
- if (node) {
590
- stack.push(node);
591
- node = node.left;
592
- }
593
- else {
594
- node = stack[stack.length - 1];
595
- if (!node.right || last === node.right) {
596
- node = stack.pop();
597
- if (node) {
598
- var leftHeight = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
599
- var rightHeight = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
600
- depths.set(node, 1 + Math.max(leftHeight, rightHeight));
601
- last = node;
602
- node = null;
603
- }
604
- }
605
- else
606
- node = node.right;
607
- }
608
- }
609
- return (_c = depths.get(beginRoot)) !== null && _c !== void 0 ? _c : -1;
610
- }
611
- };
612
- /**
613
- * The `getMinHeight` function calculates the minimum height of a binary tree using either a recursive or iterative
614
- * approach.
615
- * @param {BinaryTreeNode<T> | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type
616
- * `BinaryTreeNode<T> | null`. It represents the starting node from which to calculate the minimum height of the binary
617
- * tree. If no value is provided for `beginRoot`, the function will use the root node of the binary tree.
618
- * @returns The function `getMinHeight` returns the minimum height of the binary tree.
619
- */
620
- BinaryTree.prototype.getMinHeight = function (beginRoot) {
621
- var _a, _b, _c;
622
- beginRoot = beginRoot || this.root;
623
- if (!beginRoot)
624
- return -1;
625
- if (this._loopType === LoopType.recursive) {
626
- var _getMinHeight_1 = function (cur) {
627
- if (!cur)
628
- return 0;
629
- if (!cur.left && !cur.right)
630
- return 0;
631
- var leftMinHeight = _getMinHeight_1(cur.left);
632
- var rightMinHeight = _getMinHeight_1(cur.right);
633
- return Math.min(leftMinHeight, rightMinHeight) + 1;
634
- };
635
- return _getMinHeight_1(beginRoot);
636
- }
637
- else {
638
- var stack = [];
639
- var node = beginRoot, last = null;
640
- var depths = new Map();
641
- while (stack.length > 0 || node) {
642
- if (node) {
643
- stack.push(node);
644
- node = node.left;
645
- }
646
- else {
647
- node = stack[stack.length - 1];
648
- if (!node.right || last === node.right) {
649
- node = stack.pop();
650
- if (node) {
651
- var leftMinHeight = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
652
- var rightMinHeight = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
653
- depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
654
- last = node;
655
- node = null;
656
- }
657
- }
658
- else
659
- node = node.right;
660
- }
661
- }
662
- return (_c = depths.get(beginRoot)) !== null && _c !== void 0 ? _c : -1;
663
- }
664
- };
665
- /**
666
- * The function checks if a binary tree is balanced by comparing the minimum height and the maximum height of the tree.
667
- * @param {BinaryTreeNode<T> | null} [beginRoot] - The `beginRoot` parameter is the root node of a binary tree. It is
668
- * of type `BinaryTreeNode<T> | null`, which means it can either be a `BinaryTreeNode` object or `null`.
669
- * @returns The method is returning a boolean value.
670
- */
671
- BinaryTree.prototype.isBalanced = function (beginRoot) {
672
- return (this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot));
673
- };
674
- /**
675
- * The function `getNodes` returns an array of binary tree nodes that match a given property value, with options for
676
- * searching recursively or iteratively.
677
- * @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
678
- * generic type `T`. It represents the property of the binary tree node that you want to search for.
679
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
680
- * specifies the property name to use when searching for nodes. If not provided, it defaults to 'id'.
681
- * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
682
- * return only one node that matches the `nodeProperty` or `propertyName` criteria. If `onlyOne` is set to `true`, the
683
- * function will stop traversing the tree and return the first matching node. If `
684
- * @returns The function `getNodes` returns an array of `BinaryTreeNode<T> | null | undefined` objects.
685
- */
686
- BinaryTree.prototype.getNodes = function (nodeProperty, propertyName, onlyOne) {
687
- var _this = this;
688
- if (!this.root)
689
- return [];
690
- propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
691
- var result = [];
692
- if (this._loopType === LoopType.recursive) {
693
- var _traverse_1 = function (cur) {
694
- if (_this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
695
- return;
696
- if (!cur.left && !cur.right)
697
- return;
698
- cur.left && _traverse_1(cur.left);
699
- cur.right && _traverse_1(cur.right);
700
- };
701
- _traverse_1(this.root);
702
- }
703
- else {
704
- var queue = [this.root];
705
- while (queue.length > 0) {
706
- var cur = queue.shift();
707
- if (cur) {
708
- if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
709
- return result;
710
- cur.left && queue.push(cur.left);
711
- cur.right && queue.push(cur.right);
712
- }
713
- }
714
- }
715
- return result;
716
- };
717
- /**
718
- * The function checks if a binary tree node has a specific property or if any node in the tree has a specific
719
- * property.
720
- * @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
721
- * generic type `T`. It represents the property of a binary tree node that you want to check.
722
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
723
- * specifies the name of the property to check for in the nodes.
724
- * @returns a boolean value.
725
- */
726
- BinaryTree.prototype.has = function (nodeProperty, propertyName) {
727
- return this.getNodes(nodeProperty, propertyName).length > 0;
728
- };
729
- /**
730
- * The function returns the first binary tree node that matches the given property name and value, or null if no match
731
- * is found.
732
- * @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
733
- * generic type `T`. It represents the property of the binary tree node that you want to search for.
734
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
735
- * specifies the property of the binary tree node to search for. If not provided, it defaults to `'id'`.
736
- * @returns a BinaryTreeNode object or null.
737
- */
738
- BinaryTree.prototype.get = function (nodeProperty, propertyName) {
739
- var _a;
740
- propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
741
- return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
742
- };
743
- /**
744
- * The function getPathToRoot returns an array of BinaryTreeNode objects representing the path from a given node to the
745
- * root of a binary tree.
746
- * @param node - The `node` parameter is a BinaryTreeNode object.
747
- * @returns The function `getPathToRoot` returns an array of `BinaryTreeNode<T>` objects, representing the path from
748
- * the given `node` to the root of the binary tree.
749
- */
750
- BinaryTree.prototype.getPathToRoot = function (node) {
751
- var result = [];
752
- while (node.parent) {
753
- result.unshift(node);
754
- node = node.parent;
755
- }
756
- result.unshift(node);
757
- return result;
758
- };
759
- /**
760
- * The `getLeftMost` function returns the leftmost node in a binary tree, either recursively or iteratively using tail
761
- * recursion optimization.
762
- * @param {BinaryTreeNode<T> | null} [node] - The `node` parameter is an optional parameter of type `BinaryTreeNode<T>
763
- * | null`. It represents the starting node from which to find the leftmost node in a binary tree. If no node is
764
- * provided, the function will use the root node of the binary tree.
765
- * @returns The `getLeftMost` function returns the leftmost node in a binary tree.
766
- */
767
- BinaryTree.prototype.getLeftMost = function (node) {
768
- node = node !== null && node !== void 0 ? node : this.root;
769
- if (!node)
770
- return node;
771
- if (this._loopType === LoopType.recursive) {
772
- var _traverse_2 = function (cur) {
773
- if (!cur.left)
774
- return cur;
775
- return _traverse_2(cur.left);
776
- };
777
- return _traverse_2(node);
778
- }
779
- else {
780
- // Indirect implementation of iteration using tail recursion optimization
781
- var _traverse_3 = (0, utils_1.trampoline)(function (cur) {
782
- if (!cur.left)
783
- return cur;
784
- return _traverse_3.cont(cur.left);
785
- });
786
- return _traverse_3(node);
787
- }
788
- };
789
- /**
790
- * The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using
791
- * tail recursion optimization.
792
- * @param {BinaryTreeNode<T> | null} [node] - The `node` parameter is an optional parameter of type `BinaryTreeNode<T>
793
- * | null`. It represents the starting node from which to find the rightmost node in a binary tree. If no node is
794
- * provided, the function will use the root node of the binary tree.
795
- * @returns The `getRightMost` function returns the rightmost node in a binary tree.
796
- */
797
- BinaryTree.prototype.getRightMost = function (node) {
798
- node = node !== null && node !== void 0 ? node : this.root;
799
- if (!node)
800
- return node;
801
- if (this._loopType === LoopType.recursive) {
802
- var _traverse_4 = function (cur) {
803
- if (!cur.right)
804
- return cur;
805
- return _traverse_4(cur.right);
806
- };
807
- return _traverse_4(node);
808
- }
809
- else {
810
- // Indirect implementation of iteration using tail recursion optimization
811
- var _traverse_5 = (0, utils_1.trampoline)(function (cur) {
812
- if (!cur.right)
813
- return cur;
814
- return _traverse_5.cont(cur.right);
815
- });
816
- return _traverse_5(node);
817
- }
818
- };
819
- /**
820
- * The `isBST` function checks if a binary tree is a binary search tree.
821
- * @param {BinaryTreeNode<T> | null} [node] - The `node` parameter is an optional parameter of type `BinaryTreeNode<T>
822
- * | null`. It represents the root node of the binary search tree (BST) that we want to check for validity. If no node
823
- * is provided, the function will default to using the root node of the BST instance that
824
- * @returns The `isBST` function returns a boolean value. It returns `true` if the binary tree is a valid binary search
825
- * tree, and `false` otherwise.
826
- */
827
- BinaryTree.prototype.isBST = function (node) {
828
- node = node !== null && node !== void 0 ? node : this.root;
829
- if (!node)
830
- return true;
831
- if (this._loopType === LoopType.recursive) {
832
- var dfs_1 = function (cur, min, max) {
833
- if (!cur)
834
- return true;
835
- if (cur.id <= min || cur.id >= max)
836
- return false;
837
- return dfs_1(cur.left, min, cur.id) && dfs_1(cur.right, cur.id, max);
838
- };
839
- return dfs_1(node, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
840
- }
841
- else {
842
- var stack = [];
843
- var prev = Number.MIN_SAFE_INTEGER, curr = node;
844
- while (curr || stack.length > 0) {
845
- while (curr) {
846
- stack.push(curr);
847
- curr = curr.left;
848
- }
849
- curr = stack.pop();
850
- if (!(curr) || prev >= curr.id)
851
- return false;
852
- prev = curr.id;
853
- curr = curr.right;
854
- }
855
- return true;
856
- }
857
- };
858
- /**
859
- * The function calculates the size and count of a subtree in a binary tree using either recursive or iterative
860
- * traversal.
861
- * @param {BinaryTreeNode<T> | null | undefined} subTreeRoot - The `subTreeRoot` parameter is the root node of a binary
862
- * tree.
863
- * @returns The function `getSubTreeSizeAndCount` returns an array `[number, number]`. The first element of the array
864
- * represents the size of the subtree, and the second element represents the count of the nodes in the subtree.
865
- */
866
- BinaryTree.prototype.getSubTreeSizeAndCount = function (subTreeRoot) {
867
- var res = [0, 0];
868
- if (!subTreeRoot)
869
- return res;
870
- if (this._loopType === LoopType.recursive) {
871
- var _traverse_6 = function (cur) {
872
- res[0]++;
873
- res[1] += cur.count;
874
- cur.left && _traverse_6(cur.left);
875
- cur.right && _traverse_6(cur.right);
876
- };
877
- _traverse_6(subTreeRoot);
878
- return res;
879
- }
880
- else {
881
- var stack = [subTreeRoot];
882
- while (stack.length > 0) {
883
- var cur = stack.pop();
884
- res[0]++;
885
- res[1] += cur.count;
886
- cur.right && stack.push(cur.right);
887
- cur.left && stack.push(cur.left);
888
- }
889
- return res;
890
- }
891
- };
892
- // --- start additional methods ---
893
- /**
894
- * The function `subTreeSum` calculates the sum of a specified property in a binary tree, either recursively or
895
- * iteratively.
896
- * @param subTreeRoot - The subTreeRoot parameter is the root node of the subtree for which you want to calculate the
897
- * sum.
898
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
899
- * specifies the property of the `BinaryTreeNode` object to use for calculating the sum. If `propertyName` is not
900
- * provided, it defaults to `'val'`.
901
- * @returns a number, which is the sum of the values of the nodes in the subtree rooted at `subTreeRoot`.
902
- */
903
- BinaryTree.prototype.subTreeSum = function (subTreeRoot, propertyName) {
904
- propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'val';
905
- if (!subTreeRoot)
906
- return 0;
907
- var sum = 0;
908
- var _sumByProperty = function (cur) {
909
- var needSum;
910
- switch (propertyName) {
911
- case 'id':
912
- needSum = cur.id;
913
- break;
914
- case 'count':
915
- needSum = cur.count;
916
- break;
917
- case 'val':
918
- needSum = typeof cur.val === 'number' ? cur.val : 0;
919
- break;
920
- default:
921
- needSum = cur.id;
922
- break;
923
- }
924
- return needSum;
925
- };
926
- if (this._loopType === LoopType.recursive) {
927
- var _traverse_7 = function (cur) {
928
- sum += _sumByProperty(cur);
929
- cur.left && _traverse_7(cur.left);
930
- cur.right && _traverse_7(cur.right);
931
- };
932
- _traverse_7(subTreeRoot);
933
- }
934
- else {
935
- var stack = [subTreeRoot];
936
- while (stack.length > 0) {
937
- var cur = stack.pop();
938
- sum += _sumByProperty(cur);
939
- cur.right && stack.push(cur.right);
940
- cur.left && stack.push(cur.left);
941
- }
942
- }
943
- return sum;
944
- };
945
- /**
946
- * The function `subTreeAdd` adds a specified delta value to a property of each node in a binary tree.
947
- * @param subTreeRoot - The `subTreeRoot` parameter is the root node of the subtree where the values will be modified.
948
- * @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
949
- * each node in the subtree should be increased or decreased.
950
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
951
- * specifies the property of the `BinaryTreeNode` that should be modified. It defaults to `'id'` if not provided.
952
- * @returns a boolean value, which is `true`.
953
- */
954
- BinaryTree.prototype.subTreeAdd = function (subTreeRoot, delta, propertyName) {
955
- var _this = this;
956
- propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
957
- if (!subTreeRoot)
958
- return false;
959
- var _addByProperty = function (cur) {
960
- switch (propertyName) {
961
- case 'id':
962
- cur.id += delta;
963
- break;
964
- case 'count':
965
- cur.count += delta;
966
- _this._setCount(_this.count + delta);
967
- break;
968
- default:
969
- cur.id += delta;
970
- break;
971
- }
972
- };
973
- if (this._loopType === LoopType.recursive) {
974
- var _traverse_8 = function (cur) {
975
- _addByProperty(cur);
976
- cur.left && _traverse_8(cur.left);
977
- cur.right && _traverse_8(cur.right);
978
- };
979
- _traverse_8(subTreeRoot);
980
- }
981
- else {
982
- var stack = [subTreeRoot];
983
- while (stack.length > 0) {
984
- var cur = stack.pop();
985
- _addByProperty(cur);
986
- cur.right && stack.push(cur.right);
987
- cur.left && stack.push(cur.left);
988
- }
989
- }
990
- return true;
991
- };
992
- /**
993
- * The BFS function performs a breadth-first search on a binary tree and returns the results based on a specified node
994
- * or property name.
995
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
996
- * represents either a node or a property name. If a node is provided, the breadth-first search algorithm will be
997
- * performed starting from that node. If a property name is provided, the breadth-first search algorithm will be
998
- * performed starting from the root node
999
- * @returns an object of type `ResultsByProperty<T>`.
1000
- */
1001
- BinaryTree.prototype.BFS = function (nodeOrPropertyName) {
1002
- nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
1003
- this._resetResults();
1004
- var queue = [this.root];
1005
- while (queue.length !== 0) {
1006
- var cur = queue.shift();
1007
- if (cur) {
1008
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1009
- if ((cur === null || cur === void 0 ? void 0 : cur.left) !== null)
1010
- queue.push(cur.left);
1011
- if ((cur === null || cur === void 0 ? void 0 : cur.right) !== null)
1012
- queue.push(cur.right);
1013
- }
1014
- }
1015
- return this._getResultByPropertyName(nodeOrPropertyName);
1016
- };
1017
- /**
1018
- * The DFS function performs a depth-first search traversal on a binary tree and returns the results based on the
1019
- * specified pattern and node or property name.
1020
- * @param {'in' | 'pre' | 'post'} [pattern] - The "pattern" parameter is used to specify the order in which the nodes
1021
- * of a binary tree are traversed during the Depth-First Search (DFS) algorithm. It can take one of three values: 'in',
1022
- * 'pre', or 'post'.
1023
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is a string that represents
1024
- * either the name of a property in the `BinaryTreeNode` object or the value of the `id` property in the
1025
- * `BinaryTreeNode` object. This parameter is used to accumulate the results based on the specified property name. If
1026
- * no value
1027
- * @returns an object of type `ResultsByProperty<T>`.
1028
- */
1029
- BinaryTree.prototype.DFS = function (pattern, nodeOrPropertyName) {
1030
- var _this = this;
1031
- pattern = pattern !== null && pattern !== void 0 ? pattern : 'in';
1032
- nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
1033
- this._resetResults();
1034
- var _traverse = function (node) {
1035
- switch (pattern) {
1036
- case 'in':
1037
- if (node.left)
1038
- _traverse(node.left);
1039
- _this._accumulatedByPropertyName(node, nodeOrPropertyName);
1040
- if (node.right)
1041
- _traverse(node.right);
1042
- break;
1043
- case 'pre':
1044
- _this._accumulatedByPropertyName(node, nodeOrPropertyName);
1045
- if (node.left)
1046
- _traverse(node.left);
1047
- if (node.right)
1048
- _traverse(node.right);
1049
- break;
1050
- case 'post':
1051
- if (node.left)
1052
- _traverse(node.left);
1053
- if (node.right)
1054
- _traverse(node.right);
1055
- _this._accumulatedByPropertyName(node, nodeOrPropertyName);
1056
- break;
1057
- }
1058
- };
1059
- this.root && _traverse(this.root);
1060
- return this._getResultByPropertyName(nodeOrPropertyName);
1061
- };
1062
- /**
1063
- * Time complexity is O(n)
1064
- * Space complexity of Iterative DFS equals to recursive DFS which is O(n) because of the stack
1065
- * @param pattern
1066
- * @param nodeOrPropertyName
1067
- * @constructor
1068
- */
1069
- BinaryTree.prototype.DFSIterative = function (pattern, nodeOrPropertyName) {
1070
- pattern = pattern || 'in';
1071
- nodeOrPropertyName = nodeOrPropertyName || 'id';
1072
- this._resetResults();
1073
- if (!this.root)
1074
- return this._getResultByPropertyName(nodeOrPropertyName);
1075
- // 0: visit, 1: print
1076
- var stack = [{ opt: 0, node: this.root }];
1077
- while (stack.length > 0) {
1078
- var cur = stack.pop();
1079
- if (!cur || !cur.node)
1080
- continue;
1081
- if (cur.opt === 1) {
1082
- this._accumulatedByPropertyName(cur.node, nodeOrPropertyName);
1083
- }
1084
- else {
1085
- switch (pattern) {
1086
- case 'in':
1087
- stack.push({ opt: 0, node: cur.node.right });
1088
- stack.push({ opt: 1, node: cur.node });
1089
- stack.push({ opt: 0, node: cur.node.left });
1090
- break;
1091
- case 'pre':
1092
- stack.push({ opt: 0, node: cur.node.right });
1093
- stack.push({ opt: 0, node: cur.node.left });
1094
- stack.push({ opt: 1, node: cur.node });
1095
- break;
1096
- case 'post':
1097
- stack.push({ opt: 1, node: cur.node });
1098
- stack.push({ opt: 0, node: cur.node.right });
1099
- stack.push({ opt: 0, node: cur.node.left });
1100
- break;
1101
- default:
1102
- stack.push({ opt: 0, node: cur.node.right });
1103
- stack.push({ opt: 1, node: cur.node });
1104
- stack.push({ opt: 0, node: cur.node.left });
1105
- break;
1106
- }
1107
- }
1108
- }
1109
- return this._getResultByPropertyName(nodeOrPropertyName);
1110
- };
1111
- /**
1112
- * The `levelIterative` function performs a level-order traversal on a binary tree and returns the values of the nodes
1113
- * in an array, based on a specified property name.
1114
- * @param {BinaryTreeNode<T> | null} node - The `node` parameter is a BinaryTreeNode object representing the starting
1115
- * node for the level order traversal. It can be null if no specific node is provided, in which case the root node of
1116
- * the tree is used as the starting node.
1117
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
1118
- * can be either a `BinaryTreeNode` property name or the string `'id'`. If a property name is provided, the function
1119
- * will accumulate results based on that property. If no property name is provided, the function will default to
1120
- * accumulating results
1121
- * @returns The function `levelIterative` returns an object of type `ResultsByProperty<T>`.
1122
- */
1123
- BinaryTree.prototype.levelIterative = function (node, nodeOrPropertyName) {
1124
- nodeOrPropertyName = nodeOrPropertyName || 'id';
1125
- node = node || this.root;
1126
- if (!node)
1127
- return [];
1128
- this._resetResults();
1129
- var queue = [node];
1130
- while (queue.length > 0) {
1131
- var cur = queue.shift();
1132
- if (cur) {
1133
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1134
- if (cur.left) {
1135
- queue.push(cur.left);
1136
- }
1137
- if (cur.right) {
1138
- queue.push(cur.right);
1139
- }
1140
- }
1141
- }
1142
- return this._getResultByPropertyName(nodeOrPropertyName);
1143
- };
1144
- /**
1145
- * The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
1146
- * @param {BinaryTreeNode<T> | null} node - The `node` parameter is a BinaryTreeNode object or null. It represents the
1147
- * root node of a binary tree. If it is null, the function will use the root node of the current binary tree instance.
1148
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
1149
- * specifies the property of the `BinaryTreeNode` object to collect at each level. It can be one of the following
1150
- * values:
1151
- * @returns The function `listLevels` returns a 2D array of `ResultByProperty<T>` objects.
1152
- */
1153
- BinaryTree.prototype.listLevels = function (node, nodeOrPropertyName) {
1154
- nodeOrPropertyName = nodeOrPropertyName || 'id';
1155
- node = node || this.root;
1156
- if (!node)
1157
- return [];
1158
- var levelsNodes = [];
1159
- var collectByProperty = function (node, level) {
1160
- switch (nodeOrPropertyName) {
1161
- case 'id':
1162
- levelsNodes[level].push(node.id);
1163
- break;
1164
- case 'val':
1165
- levelsNodes[level].push(node.val);
1166
- break;
1167
- case 'node':
1168
- levelsNodes[level].push(node);
1169
- break;
1170
- case 'count':
1171
- levelsNodes[level].push(node.count);
1172
- break;
1173
- default:
1174
- levelsNodes[level].push(node.id);
1175
- break;
1176
- }
1177
- };
1178
- if (this._loopType === LoopType.recursive) {
1179
- var _recursive_1 = function (node, level) {
1180
- if (!levelsNodes[level])
1181
- levelsNodes[level] = [];
1182
- collectByProperty(node, level);
1183
- if (node.left)
1184
- _recursive_1(node.left, level + 1);
1185
- if (node.right)
1186
- _recursive_1(node.right, level + 1);
1187
- };
1188
- _recursive_1(node, 0);
1189
- }
1190
- else {
1191
- var stack = [[node, 0]];
1192
- while (stack.length > 0) {
1193
- var head = stack.pop();
1194
- var _a = __read(head, 2), node_1 = _a[0], level = _a[1];
1195
- if (!levelsNodes[level])
1196
- levelsNodes[level] = [];
1197
- collectByProperty(node_1, level);
1198
- if (node_1.right)
1199
- stack.push([node_1.right, level + 1]);
1200
- if (node_1.left)
1201
- stack.push([node_1.left, level + 1]);
1202
- }
1203
- }
1204
- return levelsNodes;
1205
- };
1206
- /**
1207
- * The function returns the predecessor of a given node in a binary tree.
1208
- * @param node - The parameter `node` is a BinaryTreeNode object, representing a node in a binary tree.
1209
- * @returns the predecessor of the given node in a binary tree.
1210
- */
1211
- BinaryTree.prototype.getPredecessor = function (node) {
1212
- if (node.left) {
1213
- var predecessor = node.left;
1214
- while (!(predecessor) || predecessor.right && predecessor.right !== node) {
1215
- if (predecessor) {
1216
- predecessor = predecessor.right;
1217
- }
1218
- }
1219
- return predecessor;
1220
- }
1221
- else {
1222
- return node;
1223
- }
1224
- };
1225
- /**
1226
- * The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris
1227
- * traversal algorithm and returns the results based on the specified property name.
1228
- * The time complexity of Morris traversal is O(n), it's may slower than others
1229
- * The space complexity Morris traversal is O(1) because no using stack
1230
- * @param {'in' | 'pre' | 'post'} [pattern] - The `pattern` parameter is an optional parameter that determines the
1231
- * traversal pattern of the binary tree. It can have one of three values:
1232
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is used to specify the
1233
- * property of the nodes that you want to retrieve in the results. It can be either the node itself or the name of the
1234
- * property. If not provided, it defaults to `'id'`.
1235
- * @returns The function `morris` returns an object of type `ResultsByProperty<T>`.
1236
- */
1237
- BinaryTree.prototype.morris = function (pattern, nodeOrPropertyName) {
1238
- var _this = this;
1239
- if (this.root === null)
1240
- return [];
1241
- pattern = pattern || 'in';
1242
- nodeOrPropertyName = nodeOrPropertyName || 'id';
1243
- this._resetResults();
1244
- var cur = this.root;
1245
- var _reverseEdge = function (node) {
1246
- var pre = null;
1247
- var next = null;
1248
- while (node) {
1249
- next = node.right;
1250
- node.right = pre;
1251
- pre = node;
1252
- node = next;
1253
- }
1254
- return pre;
1255
- };
1256
- var _printEdge = function (node) {
1257
- var tail = _reverseEdge(node);
1258
- var cur = tail;
1259
- while (cur) {
1260
- _this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1261
- cur = cur.right;
1262
- }
1263
- _reverseEdge(tail);
1264
- };
1265
- switch (pattern) {
1266
- case 'in':
1267
- while (cur) {
1268
- if (cur.left) {
1269
- var predecessor = this.getPredecessor(cur);
1270
- if (!predecessor.right) {
1271
- predecessor.right = cur;
1272
- cur = cur.left;
1273
- continue;
1274
- }
1275
- else {
1276
- predecessor.right = null;
1277
- }
1278
- }
1279
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1280
- cur = cur.right;
1281
- }
1282
- break;
1283
- case 'pre':
1284
- while (cur) {
1285
- if (cur.left) {
1286
- var predecessor = this.getPredecessor(cur);
1287
- if (!predecessor.right) {
1288
- predecessor.right = cur;
1289
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1290
- cur = cur.left;
1291
- continue;
1292
- }
1293
- else {
1294
- predecessor.right = null;
1295
- }
1296
- }
1297
- else {
1298
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1299
- }
1300
- cur = cur.right;
1301
- }
1302
- break;
1303
- case 'post':
1304
- while (cur) {
1305
- if (cur.left) {
1306
- var predecessor = this.getPredecessor(cur);
1307
- if (predecessor.right === null) {
1308
- predecessor.right = cur;
1309
- cur = cur.left;
1310
- continue;
1311
- }
1312
- else {
1313
- predecessor.right = null;
1314
- _printEdge(cur.left);
1315
- }
1316
- }
1317
- cur = cur.right;
1318
- }
1319
- _printEdge(this.root);
1320
- break;
1321
- }
1322
- return this._getResultByPropertyName(nodeOrPropertyName);
1323
- };
1324
- BinaryTree.prototype._setLoopType = function (value) {
1325
- this._loopType = value;
1326
- };
1327
- BinaryTree.prototype._setVisitedId = function (value) {
1328
- this._visitedId = value;
1329
- };
1330
- BinaryTree.prototype._setVisitedVal = function (value) {
1331
- this._visitedVal = value;
1332
- };
1333
- BinaryTree.prototype._setVisitedNode = function (value) {
1334
- this._visitedNode = value;
1335
- };
1336
- BinaryTree.prototype.setVisitedCount = function (value) {
1337
- this._visitedCount = value;
1338
- };
1339
- BinaryTree.prototype._setVisitedLeftSum = function (value) {
1340
- this._visitedLeftSum = value;
1341
- };
1342
- BinaryTree.prototype._setAutoIncrementId = function (value) {
1343
- this._autoIncrementId = value;
1344
- };
1345
- BinaryTree.prototype._setMaxId = function (value) {
1346
- this._maxId = value;
1347
- };
1348
- BinaryTree.prototype._setIsDuplicatedVal = function (value) {
1349
- this._isDuplicatedVal = value;
1350
- };
1351
- BinaryTree.prototype._setRoot = function (v) {
1352
- if (v) {
1353
- v.parent = null;
1354
- v.familyPosition = FamilyPosition.root;
1355
- }
1356
- this._root = v;
1357
- };
1358
- BinaryTree.prototype._setSize = function (v) {
1359
- this._size = v;
1360
- };
1361
- BinaryTree.prototype._setCount = function (v) {
1362
- this._count = v;
1363
- };
1364
- /**
1365
- * The function resets the values of several arrays used for tracking visited nodes and their properties.
1366
- */
1367
- BinaryTree.prototype._resetResults = function () {
1368
- this._visitedId = [];
1369
- this._visitedVal = [];
1370
- this._visitedNode = [];
1371
- this._visitedCount = [];
1372
- this._visitedLeftSum = [];
1373
- };
1374
- /**
1375
- * The function checks if a given property of a binary tree node matches a specified value, and if so, adds the node to
1376
- * a result array.
1377
- * @param cur - The current binary tree node that is being checked.
1378
- * @param {(BinaryTreeNode<T> | null | undefined)[]} result - An array that stores the matching nodes found during the
1379
- * traversal.
1380
- * @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter is the value that we are searching for in
1381
- * the binary tree nodes. It can be either the `id`, `count`, or `val` property of the node.
1382
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
1383
- * specifies the property of the `BinaryTreeNode` object that you want to compare with the `nodeProperty` value. It can
1384
- * be one of the following values: 'id', 'count', or 'val'. If no `propertyName` is provided,
1385
- * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
1386
- * stop after finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set to
1387
- * `true`, the function will stop after finding the first matching node and return `true`. If `onlyOne
1388
- * @returns a boolean value indicating whether or not a node was pushed into the result array.
1389
- */
1390
- BinaryTree.prototype._pushByPropertyNameStopOrNot = function (cur, result, nodeProperty, propertyName, onlyOne) {
1391
- switch (propertyName) {
1392
- case 'id':
1393
- if (cur.id === nodeProperty) {
1394
- result.push(cur);
1395
- return !!onlyOne;
1396
- }
1397
- break;
1398
- case 'count':
1399
- if (cur.count === nodeProperty) {
1400
- result.push(cur);
1401
- return !!onlyOne;
1402
- }
1403
- break;
1404
- case 'val':
1405
- if (cur.val === nodeProperty) {
1406
- result.push(cur);
1407
- return !!onlyOne;
1408
- }
1409
- break;
1410
- default:
1411
- if (cur.id === nodeProperty) {
1412
- result.push(cur);
1413
- return !!onlyOne;
1414
- }
1415
- break;
1416
- }
1417
- };
1418
- /**
1419
- * The function `_accumulatedByPropertyName` pushes a property value of a binary tree node into an array based on the
1420
- * provided property name or a default property name.
1421
- * @param node - The `node` parameter is of type `BinaryTreeNode<T>`, which represents a node in a binary tree.
1422
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
1423
- * can be either a string representing a property name or a reference to a node object. If it is a string, it specifies
1424
- * the property name of the node that should be accumulated. If it is a node object, it specifies the node itself
1425
- */
1426
- BinaryTree.prototype._accumulatedByPropertyName = function (node, nodeOrPropertyName) {
1427
- nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
1428
- switch (nodeOrPropertyName) {
1429
- case 'id':
1430
- this._visitedId.push(node.id);
1431
- break;
1432
- case 'val':
1433
- this._visitedVal.push(node.val);
1434
- break;
1435
- case 'node':
1436
- this._visitedNode.push(node);
1437
- break;
1438
- case 'count':
1439
- this._visitedCount.push(node.count);
1440
- break;
1441
- default:
1442
- this._visitedId.push(node.id);
1443
- break;
1444
- }
1445
- };
1446
- /**
1447
- * The function `_getResultByPropertyName` returns different results based on the provided property name or defaulting
1448
- * to 'id'.
1449
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
1450
- * can accept a value of type `NodeOrPropertyName`.
1451
- * @returns The method returns an object of type `ResultsByProperty<T>`.
52
+ * `BinaryTreeNodeId`.
53
+ * @param {N | null} val - The `val` parameter represents the value of the node. It can be of type `N` (generic type)
54
+ * or `null`.
55
+ * @param {number} [count] - The `count` parameter is an optional parameter of type `number`. It represents the number
56
+ * of occurrences of the value in the binary tree node. If not provided, the default value is `undefined`.
57
+ * @returns a BinaryTreeNode object if the value is not null, otherwise it returns null.
1452
58
  */
1453
- BinaryTree.prototype._getResultByPropertyName = function (nodeOrPropertyName) {
1454
- nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
1455
- switch (nodeOrPropertyName) {
1456
- case 'id':
1457
- return this._visitedId;
1458
- case 'val':
1459
- return this._visitedVal;
1460
- case 'node':
1461
- return this._visitedNode;
1462
- case 'count':
1463
- return this._visitedCount;
1464
- default:
1465
- return this._visitedId;
1466
- }
59
+ BinaryTree.prototype._createNode = function (id, val, count) {
60
+ var node = new BinaryTreeNode(id, val, count);
61
+ return node;
1467
62
  };
1468
63
  return BinaryTree;
1469
- }());
64
+ }(abstract_binary_tree_1.AbstractBinaryTree));
1470
65
  exports.BinaryTree = BinaryTree;