min-heap-typed 1.38.0 → 1.38.2

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 (334) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +100 -0
  2. package/dist/data-structures/binary-tree/avl-tree.js +341 -0
  3. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +144 -0
  4. package/dist/data-structures/binary-tree/binary-indexed-tree.js +261 -0
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +409 -0
  6. package/dist/data-structures/binary-tree/binary-tree.js +1065 -0
  7. package/dist/data-structures/binary-tree/bst.d.ts +167 -0
  8. package/dist/data-structures/binary-tree/bst.js +512 -0
  9. package/dist/data-structures/binary-tree/index.d.ts +7 -0
  10. package/dist/data-structures/binary-tree/index.js +23 -0
  11. package/dist/data-structures/binary-tree/rb-tree.d.ts +13 -0
  12. package/dist/data-structures/binary-tree/rb-tree.js +27 -0
  13. package/dist/data-structures/binary-tree/segment-tree.d.ts +83 -0
  14. package/dist/data-structures/binary-tree/segment-tree.js +228 -0
  15. package/dist/data-structures/binary-tree/tree-multiset.d.ts +122 -0
  16. package/dist/data-structures/binary-tree/tree-multiset.js +351 -0
  17. package/dist/data-structures/graph/abstract-graph.d.ts +332 -0
  18. package/dist/data-structures/graph/abstract-graph.js +923 -0
  19. package/dist/data-structures/graph/directed-graph.d.ts +200 -0
  20. package/dist/data-structures/graph/directed-graph.js +422 -0
  21. package/dist/data-structures/graph/index.d.ts +4 -0
  22. package/dist/data-structures/graph/index.js +20 -0
  23. package/dist/data-structures/graph/map-graph.d.ts +79 -0
  24. package/dist/data-structures/graph/map-graph.js +111 -0
  25. package/dist/data-structures/graph/undirected-graph.d.ts +127 -0
  26. package/dist/data-structures/graph/undirected-graph.js +252 -0
  27. package/dist/data-structures/hash/coordinate-map.d.ts +45 -0
  28. package/dist/data-structures/hash/coordinate-map.js +65 -0
  29. package/dist/data-structures/hash/coordinate-set.d.ts +37 -0
  30. package/dist/data-structures/hash/coordinate-set.js +55 -0
  31. package/dist/data-structures/hash/hash-map.d.ts +56 -0
  32. package/dist/data-structures/hash/hash-map.js +171 -0
  33. package/dist/data-structures/hash/hash-table.d.ts +106 -0
  34. package/dist/data-structures/hash/hash-table.js +245 -0
  35. package/dist/data-structures/hash/index.d.ts +6 -0
  36. package/dist/data-structures/hash/index.js +22 -0
  37. package/dist/data-structures/hash/tree-map.d.ts +2 -0
  38. package/dist/data-structures/hash/tree-map.js +6 -0
  39. package/dist/data-structures/hash/tree-set.d.ts +2 -0
  40. package/dist/data-structures/hash/tree-set.js +6 -0
  41. package/dist/data-structures/heap/heap.d.ts +224 -0
  42. package/dist/data-structures/heap/heap.js +497 -0
  43. package/dist/data-structures/heap/index.d.ts +3 -0
  44. package/dist/data-structures/heap/index.js +19 -0
  45. package/dist/data-structures/heap/max-heap.d.ts +12 -0
  46. package/dist/data-structures/heap/max-heap.js +24 -0
  47. package/dist/data-structures/heap/min-heap.d.ts +12 -0
  48. package/dist/data-structures/heap/min-heap.js +24 -0
  49. package/dist/data-structures/index.d.ts +11 -0
  50. package/dist/data-structures/index.js +27 -0
  51. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +234 -0
  52. package/dist/data-structures/linked-list/doubly-linked-list.js +583 -0
  53. package/dist/data-structures/linked-list/index.d.ts +3 -0
  54. package/dist/data-structures/linked-list/index.js +19 -0
  55. package/dist/data-structures/linked-list/singly-linked-list.d.ts +157 -0
  56. package/dist/data-structures/linked-list/singly-linked-list.js +448 -0
  57. package/dist/data-structures/linked-list/skip-linked-list.d.ts +61 -0
  58. package/dist/data-structures/linked-list/skip-linked-list.js +142 -0
  59. package/dist/data-structures/matrix/index.d.ts +4 -0
  60. package/dist/data-structures/matrix/index.js +20 -0
  61. package/dist/data-structures/matrix/matrix.d.ts +21 -0
  62. package/dist/data-structures/matrix/matrix.js +28 -0
  63. package/dist/data-structures/matrix/matrix2d.d.ts +108 -0
  64. package/dist/data-structures/matrix/matrix2d.js +203 -0
  65. package/dist/data-structures/matrix/navigator.d.ts +52 -0
  66. package/dist/data-structures/matrix/navigator.js +106 -0
  67. package/dist/data-structures/matrix/vector2d.d.ts +201 -0
  68. package/dist/data-structures/matrix/vector2d.js +291 -0
  69. package/dist/data-structures/priority-queue/index.d.ts +3 -0
  70. package/dist/data-structures/priority-queue/index.js +19 -0
  71. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -0
  72. package/dist/data-structures/priority-queue/max-priority-queue.js +24 -0
  73. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -0
  74. package/dist/data-structures/priority-queue/min-priority-queue.js +24 -0
  75. package/dist/data-structures/priority-queue/priority-queue.d.ts +12 -0
  76. package/dist/data-structures/priority-queue/priority-queue.js +17 -0
  77. package/dist/data-structures/queue/deque.d.ts +165 -0
  78. package/dist/data-structures/queue/deque.js +276 -0
  79. package/dist/data-structures/queue/index.d.ts +2 -0
  80. package/dist/data-structures/queue/index.js +18 -0
  81. package/dist/data-structures/queue/queue.d.ts +107 -0
  82. package/dist/data-structures/queue/queue.js +170 -0
  83. package/dist/data-structures/stack/index.d.ts +1 -0
  84. package/dist/data-structures/stack/index.js +17 -0
  85. package/dist/data-structures/stack/stack.d.ts +63 -0
  86. package/dist/data-structures/stack/stack.js +91 -0
  87. package/dist/data-structures/tree/index.d.ts +1 -0
  88. package/dist/data-structures/tree/index.js +17 -0
  89. package/dist/data-structures/tree/tree.d.ts +14 -0
  90. package/dist/data-structures/tree/tree.js +58 -0
  91. package/dist/data-structures/trie/index.d.ts +1 -0
  92. package/dist/data-structures/trie/index.js +17 -0
  93. package/dist/data-structures/trie/trie.d.ts +84 -0
  94. package/dist/data-structures/trie/trie.js +268 -0
  95. package/dist/index.d.ts +3 -3
  96. package/dist/index.js +3 -3
  97. package/dist/interfaces/binary-tree.d.ts +7 -0
  98. package/dist/interfaces/binary-tree.js +2 -0
  99. package/dist/interfaces/doubly-linked-list.d.ts +1 -0
  100. package/dist/interfaces/doubly-linked-list.js +2 -0
  101. package/dist/interfaces/graph.d.ts +5 -0
  102. package/dist/interfaces/graph.js +2 -0
  103. package/dist/interfaces/heap.d.ts +1 -0
  104. package/dist/interfaces/heap.js +2 -0
  105. package/dist/interfaces/index.d.ts +8 -0
  106. package/dist/interfaces/index.js +24 -0
  107. package/dist/interfaces/navigator.d.ts +1 -0
  108. package/dist/interfaces/navigator.js +2 -0
  109. package/dist/interfaces/priority-queue.d.ts +1 -0
  110. package/dist/interfaces/priority-queue.js +2 -0
  111. package/dist/interfaces/segment-tree.d.ts +1 -0
  112. package/dist/interfaces/segment-tree.js +2 -0
  113. package/dist/interfaces/singly-linked-list.d.ts +1 -0
  114. package/dist/interfaces/singly-linked-list.js +2 -0
  115. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +4 -0
  116. package/dist/types/data-structures/binary-tree/avl-tree.js +2 -0
  117. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
  118. package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +2 -0
  119. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +31 -0
  120. package/dist/types/data-structures/binary-tree/binary-tree.js +24 -0
  121. package/dist/types/data-structures/binary-tree/bst.d.ts +7 -0
  122. package/dist/types/data-structures/binary-tree/bst.js +2 -0
  123. package/dist/types/data-structures/binary-tree/index.d.ts +6 -0
  124. package/dist/types/data-structures/binary-tree/index.js +22 -0
  125. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +8 -0
  126. package/dist/types/data-structures/binary-tree/rb-tree.js +8 -0
  127. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
  128. package/dist/types/data-structures/binary-tree/segment-tree.js +2 -0
  129. package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +4 -0
  130. package/dist/types/data-structures/binary-tree/tree-multiset.js +2 -0
  131. package/dist/types/data-structures/graph/abstract-graph.d.ts +10 -0
  132. package/dist/types/data-structures/graph/abstract-graph.js +2 -0
  133. package/dist/types/data-structures/graph/directed-graph.d.ts +6 -0
  134. package/dist/types/data-structures/graph/directed-graph.js +9 -0
  135. package/dist/types/data-structures/graph/index.d.ts +3 -0
  136. package/dist/types/data-structures/graph/index.js +19 -0
  137. package/dist/types/data-structures/graph/map-graph.d.ts +1 -0
  138. package/dist/types/data-structures/graph/map-graph.js +2 -0
  139. package/dist/types/data-structures/graph/undirected-graph.d.ts +1 -0
  140. package/dist/types/data-structures/graph/undirected-graph.js +2 -0
  141. package/dist/types/data-structures/hash/coordinate-map.d.ts +1 -0
  142. package/dist/types/data-structures/hash/coordinate-map.js +2 -0
  143. package/dist/types/data-structures/hash/coordinate-set.d.ts +1 -0
  144. package/dist/types/data-structures/hash/coordinate-set.js +2 -0
  145. package/dist/types/data-structures/hash/hash-map.d.ts +1 -0
  146. package/dist/types/data-structures/hash/hash-map.js +2 -0
  147. package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
  148. package/dist/types/data-structures/hash/hash-table.js +2 -0
  149. package/dist/types/data-structures/hash/index.d.ts +1 -0
  150. package/dist/types/data-structures/hash/index.js +2 -0
  151. package/dist/types/data-structures/hash/tree-map.d.ts +1 -0
  152. package/dist/types/data-structures/hash/tree-map.js +2 -0
  153. package/dist/types/data-structures/hash/tree-set.d.ts +1 -0
  154. package/dist/types/data-structures/hash/tree-set.js +2 -0
  155. package/dist/types/data-structures/heap/heap.d.ts +1 -0
  156. package/dist/types/data-structures/heap/heap.js +2 -0
  157. package/dist/types/data-structures/heap/index.d.ts +1 -0
  158. package/dist/types/data-structures/heap/index.js +17 -0
  159. package/dist/types/data-structures/heap/max-heap.d.ts +1 -0
  160. package/dist/types/data-structures/heap/max-heap.js +2 -0
  161. package/dist/types/data-structures/heap/min-heap.d.ts +1 -0
  162. package/dist/types/data-structures/heap/min-heap.js +2 -0
  163. package/dist/types/data-structures/index.d.ts +11 -0
  164. package/dist/types/data-structures/index.js +27 -0
  165. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -0
  166. package/dist/types/data-structures/linked-list/doubly-linked-list.js +2 -0
  167. package/dist/types/data-structures/linked-list/index.d.ts +2 -0
  168. package/dist/types/data-structures/linked-list/index.js +18 -0
  169. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +1 -0
  170. package/dist/types/data-structures/linked-list/singly-linked-list.js +2 -0
  171. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
  172. package/dist/types/data-structures/linked-list/skip-linked-list.js +2 -0
  173. package/dist/types/data-structures/matrix/index.d.ts +1 -0
  174. package/dist/types/data-structures/matrix/index.js +17 -0
  175. package/dist/types/data-structures/matrix/matrix.d.ts +1 -0
  176. package/dist/types/data-structures/matrix/matrix.js +2 -0
  177. package/dist/types/data-structures/matrix/matrix2d.d.ts +1 -0
  178. package/dist/types/data-structures/matrix/matrix2d.js +2 -0
  179. package/dist/types/data-structures/matrix/navigator.d.ts +14 -0
  180. package/dist/types/data-structures/matrix/navigator.js +2 -0
  181. package/dist/types/data-structures/matrix/vector2d.d.ts +1 -0
  182. package/dist/types/data-structures/matrix/vector2d.js +2 -0
  183. package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
  184. package/dist/types/data-structures/priority-queue/index.js +19 -0
  185. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
  186. package/dist/types/data-structures/priority-queue/max-priority-queue.js +2 -0
  187. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
  188. package/dist/types/data-structures/priority-queue/min-priority-queue.js +2 -0
  189. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  190. package/dist/types/data-structures/priority-queue/priority-queue.js +2 -0
  191. package/dist/types/data-structures/queue/deque.d.ts +1 -0
  192. package/dist/types/data-structures/queue/deque.js +2 -0
  193. package/dist/types/data-structures/queue/index.d.ts +2 -0
  194. package/dist/types/data-structures/queue/index.js +18 -0
  195. package/dist/types/data-structures/queue/queue.d.ts +1 -0
  196. package/dist/types/data-structures/queue/queue.js +2 -0
  197. package/dist/types/data-structures/stack/index.d.ts +1 -0
  198. package/dist/types/data-structures/stack/index.js +17 -0
  199. package/dist/types/data-structures/stack/stack.d.ts +1 -0
  200. package/dist/types/data-structures/stack/stack.js +2 -0
  201. package/dist/types/data-structures/tree/index.d.ts +1 -0
  202. package/dist/types/data-structures/tree/index.js +17 -0
  203. package/dist/types/data-structures/tree/tree.d.ts +1 -0
  204. package/dist/types/data-structures/tree/tree.js +2 -0
  205. package/dist/types/data-structures/trie/index.d.ts +1 -0
  206. package/dist/types/data-structures/trie/index.js +17 -0
  207. package/dist/types/data-structures/trie/trie.d.ts +1 -0
  208. package/dist/types/data-structures/trie/trie.js +2 -0
  209. package/dist/types/helpers.d.ts +9 -0
  210. package/dist/types/helpers.js +9 -0
  211. package/dist/types/index.d.ts +3 -0
  212. package/dist/types/index.js +19 -0
  213. package/dist/types/utils/index.d.ts +2 -0
  214. package/dist/types/utils/index.js +18 -0
  215. package/dist/types/utils/utils.d.ts +7 -0
  216. package/dist/types/utils/utils.js +2 -0
  217. package/dist/types/utils/validate-type.d.ts +19 -0
  218. package/dist/types/utils/validate-type.js +2 -0
  219. package/dist/utils/index.d.ts +1 -0
  220. package/dist/utils/index.js +17 -0
  221. package/dist/utils/utils.d.ts +20 -0
  222. package/dist/utils/utils.js +73 -0
  223. package/package.json +1 -4
  224. package/src/data-structures/binary-tree/avl-tree.ts +342 -0
  225. package/src/data-structures/binary-tree/binary-indexed-tree.ts +298 -0
  226. package/src/data-structures/binary-tree/binary-tree.ts +1141 -0
  227. package/src/data-structures/binary-tree/bst.ts +529 -0
  228. package/src/data-structures/binary-tree/index.ts +7 -0
  229. package/src/data-structures/binary-tree/rb-tree.ts +366 -0
  230. package/src/data-structures/binary-tree/segment-tree.ts +257 -0
  231. package/src/data-structures/binary-tree/tree-multiset.ts +374 -0
  232. package/src/data-structures/graph/abstract-graph.ts +1043 -0
  233. package/src/data-structures/graph/directed-graph.ts +469 -0
  234. package/src/data-structures/graph/index.ts +4 -0
  235. package/src/data-structures/graph/map-graph.ts +134 -0
  236. package/src/data-structures/graph/undirected-graph.ts +273 -0
  237. package/src/data-structures/hash/coordinate-map.ts +67 -0
  238. package/src/data-structures/hash/coordinate-set.ts +56 -0
  239. package/src/data-structures/hash/hash-map.ts +209 -0
  240. package/src/data-structures/hash/hash-table.ts +280 -0
  241. package/src/data-structures/hash/index.ts +6 -0
  242. package/src/data-structures/hash/tree-map.ts +2 -0
  243. package/src/data-structures/hash/tree-set.ts +2 -0
  244. package/src/data-structures/heap/heap.ts +561 -0
  245. package/src/data-structures/heap/index.ts +3 -0
  246. package/src/data-structures/heap/max-heap.ts +24 -0
  247. package/src/data-structures/heap/min-heap.ts +24 -0
  248. package/src/data-structures/index.ts +11 -0
  249. package/src/data-structures/linked-list/doubly-linked-list.ts +636 -0
  250. package/src/data-structures/linked-list/index.ts +3 -0
  251. package/src/data-structures/linked-list/singly-linked-list.ts +501 -0
  252. package/src/data-structures/linked-list/skip-linked-list.ts +173 -0
  253. package/src/data-structures/matrix/index.ts +4 -0
  254. package/src/data-structures/matrix/matrix.ts +27 -0
  255. package/src/data-structures/matrix/matrix2d.ts +213 -0
  256. package/src/data-structures/matrix/navigator.ts +121 -0
  257. package/src/data-structures/matrix/vector2d.ts +317 -0
  258. package/src/data-structures/priority-queue/index.ts +3 -0
  259. package/src/data-structures/priority-queue/max-priority-queue.ts +23 -0
  260. package/src/data-structures/priority-queue/min-priority-queue.ts +23 -0
  261. package/src/data-structures/priority-queue/priority-queue.ts +16 -0
  262. package/src/data-structures/queue/deque.ts +298 -0
  263. package/src/data-structures/queue/index.ts +2 -0
  264. package/src/data-structures/queue/queue.ts +191 -0
  265. package/src/data-structures/stack/index.ts +1 -0
  266. package/src/data-structures/stack/stack.ts +98 -0
  267. package/src/data-structures/tree/index.ts +1 -0
  268. package/src/data-structures/tree/tree.ts +67 -0
  269. package/src/data-structures/trie/index.ts +1 -0
  270. package/src/data-structures/trie/trie.ts +286 -0
  271. package/src/index.ts +3 -3
  272. package/src/interfaces/binary-tree.ts +10 -0
  273. package/src/interfaces/doubly-linked-list.ts +1 -0
  274. package/src/interfaces/graph.ts +7 -0
  275. package/src/interfaces/heap.ts +1 -0
  276. package/src/interfaces/index.ts +8 -0
  277. package/src/interfaces/navigator.ts +1 -0
  278. package/src/interfaces/priority-queue.ts +1 -0
  279. package/src/interfaces/segment-tree.ts +1 -0
  280. package/src/interfaces/singly-linked-list.ts +1 -0
  281. package/src/types/data-structures/binary-tree/avl-tree.ts +5 -0
  282. package/src/types/data-structures/binary-tree/binary-indexed-tree.ts +1 -0
  283. package/src/types/data-structures/binary-tree/binary-tree.ts +35 -0
  284. package/src/types/data-structures/binary-tree/bst.ts +11 -0
  285. package/src/types/data-structures/binary-tree/index.ts +6 -0
  286. package/src/types/data-structures/binary-tree/rb-tree.ts +8 -0
  287. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -0
  288. package/src/types/data-structures/binary-tree/tree-multiset.ts +6 -0
  289. package/src/types/data-structures/graph/abstract-graph.ts +11 -0
  290. package/src/types/data-structures/graph/directed-graph.ts +8 -0
  291. package/src/types/data-structures/graph/index.ts +3 -0
  292. package/src/types/data-structures/graph/map-graph.ts +1 -0
  293. package/src/types/data-structures/graph/undirected-graph.ts +1 -0
  294. package/src/types/data-structures/hash/coordinate-map.ts +1 -0
  295. package/src/types/data-structures/hash/coordinate-set.ts +1 -0
  296. package/src/types/data-structures/hash/hash-map.ts +1 -0
  297. package/src/types/data-structures/hash/hash-table.ts +1 -0
  298. package/src/types/data-structures/hash/index.ts +1 -0
  299. package/src/types/data-structures/hash/tree-map.ts +1 -0
  300. package/src/types/data-structures/hash/tree-set.ts +1 -0
  301. package/src/types/data-structures/heap/heap.ts +1 -0
  302. package/src/types/data-structures/heap/index.ts +1 -0
  303. package/src/types/data-structures/heap/max-heap.ts +1 -0
  304. package/src/types/data-structures/heap/min-heap.ts +1 -0
  305. package/src/types/data-structures/index.ts +11 -0
  306. package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -0
  307. package/src/types/data-structures/linked-list/index.ts +2 -0
  308. package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -0
  309. package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -0
  310. package/src/types/data-structures/matrix/index.ts +1 -0
  311. package/src/types/data-structures/matrix/matrix.ts +1 -0
  312. package/src/types/data-structures/matrix/matrix2d.ts +1 -0
  313. package/src/types/data-structures/matrix/navigator.ts +14 -0
  314. package/src/types/data-structures/matrix/vector2d.ts +1 -0
  315. package/src/types/data-structures/priority-queue/index.ts +3 -0
  316. package/src/types/data-structures/priority-queue/max-priority-queue.ts +1 -0
  317. package/src/types/data-structures/priority-queue/min-priority-queue.ts +1 -0
  318. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  319. package/src/types/data-structures/queue/deque.ts +1 -0
  320. package/src/types/data-structures/queue/index.ts +2 -0
  321. package/src/types/data-structures/queue/queue.ts +1 -0
  322. package/src/types/data-structures/stack/index.ts +1 -0
  323. package/src/types/data-structures/stack/stack.ts +1 -0
  324. package/src/types/data-structures/tree/index.ts +1 -0
  325. package/src/types/data-structures/tree/tree.ts +1 -0
  326. package/src/types/data-structures/trie/index.ts +1 -0
  327. package/src/types/data-structures/trie/trie.ts +1 -0
  328. package/src/types/helpers.ts +13 -0
  329. package/src/types/index.ts +3 -0
  330. package/src/types/utils/index.ts +2 -0
  331. package/src/types/utils/utils.ts +6 -0
  332. package/src/types/utils/validate-type.ts +35 -0
  333. package/src/utils/index.ts +1 -0
  334. package/src/utils/utils.ts +86 -0
@@ -0,0 +1,1065 @@
1
+ "use strict";
2
+ /**
3
+ * data-structure-typed
4
+ *
5
+ * @author Tyler Zeng
6
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
7
+ * @license MIT License
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.BinaryTree = exports.BinaryTreeNode = void 0;
11
+ const types_1 = require("../../types");
12
+ const utils_1 = require("../../utils");
13
+ const queue_1 = require("../queue");
14
+ /**
15
+ * Represents a node in a binary tree.
16
+ * @template V - The type of data stored in the node.
17
+ * @template FAMILY - The type of the family relationship in the binary tree.
18
+ */
19
+ class BinaryTreeNode {
20
+ /**
21
+ * Creates a new instance of BinaryTreeNode.
22
+ * @param {BinaryTreeNodeKey} key - The key associated with the node.
23
+ * @param {V} val - The value stored in the node.
24
+ */
25
+ constructor(key, val) {
26
+ this.key = key;
27
+ this.val = val;
28
+ }
29
+ /**
30
+ * Get the left child node.
31
+ */
32
+ get left() {
33
+ return this._left;
34
+ }
35
+ /**
36
+ * Set the left child node.
37
+ * @param {FAMILY | null | undefined} v - The left child node.
38
+ */
39
+ set left(v) {
40
+ if (v) {
41
+ v.parent = this;
42
+ }
43
+ this._left = v;
44
+ }
45
+ /**
46
+ * Get the right child node.
47
+ */
48
+ get right() {
49
+ return this._right;
50
+ }
51
+ /**
52
+ * Set the right child node.
53
+ * @param {FAMILY | null | undefined} v - The right child node.
54
+ */
55
+ set right(v) {
56
+ if (v) {
57
+ v.parent = this;
58
+ }
59
+ this._right = v;
60
+ }
61
+ /**
62
+ * Get the position of the node within its family.
63
+ * @returns {FamilyPosition} - The family position of the node.
64
+ */
65
+ get familyPosition() {
66
+ const that = this;
67
+ if (that.parent) {
68
+ if (that.parent.left === that) {
69
+ if (that.left || that.right) {
70
+ return types_1.FamilyPosition.ROOT_LEFT;
71
+ }
72
+ else {
73
+ return types_1.FamilyPosition.LEFT;
74
+ }
75
+ }
76
+ else if (that.parent.right === that) {
77
+ if (that.left || that.right) {
78
+ return types_1.FamilyPosition.ROOT_RIGHT;
79
+ }
80
+ else {
81
+ return types_1.FamilyPosition.RIGHT;
82
+ }
83
+ }
84
+ else {
85
+ return types_1.FamilyPosition.MAL_NODE;
86
+ }
87
+ }
88
+ else {
89
+ if (that.left || that.right) {
90
+ return types_1.FamilyPosition.ROOT;
91
+ }
92
+ else {
93
+ return types_1.FamilyPosition.ISOLATED;
94
+ }
95
+ }
96
+ }
97
+ }
98
+ exports.BinaryTreeNode = BinaryTreeNode;
99
+ /**
100
+ * Represents a binary tree data structure.
101
+ * @template N - The type of the binary tree's nodes.
102
+ */
103
+ class BinaryTree {
104
+ /**
105
+ * Creates a new instance of BinaryTree.
106
+ * @param {BinaryTreeOptions} [options] - The options for the binary tree.
107
+ */
108
+ constructor(options) {
109
+ this._loopType = types_1.IterationType.ITERATIVE;
110
+ this._root = null;
111
+ this._size = 0;
112
+ /**
113
+ * Time complexity is O(n)
114
+ * Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
115
+ * The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
116
+ * the tree's structure should be restored to its original state to maintain the tree's integrity.
117
+ * This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
118
+ */
119
+ this._defaultCallbackByKey = node => node.key;
120
+ if (options !== undefined) {
121
+ const { iterationType = types_1.IterationType.ITERATIVE } = options;
122
+ this._loopType = iterationType;
123
+ }
124
+ }
125
+ /**
126
+ * Get the root node of the binary tree.
127
+ */
128
+ get root() {
129
+ return this._root;
130
+ }
131
+ /**
132
+ * Get the number of nodes in the binary tree.
133
+ */
134
+ get size() {
135
+ return this._size;
136
+ }
137
+ /**
138
+ * Get the iteration type used in the binary tree.
139
+ */
140
+ get iterationType() {
141
+ return this._loopType;
142
+ }
143
+ /**
144
+ * Set the iteration type for the binary tree.
145
+ * @param {IterationType} v - The new iteration type to set.
146
+ */
147
+ set iterationType(v) {
148
+ this._loopType = v;
149
+ }
150
+ /**
151
+ * Creates a new instance of BinaryTreeNode with the given key and value.
152
+ * @param {BinaryTreeNodeKey} key - The key for the new node.
153
+ * @param {N['val']} val - The value for the new node.
154
+ * @returns {N} - The newly created BinaryTreeNode.
155
+ */
156
+ createNode(key, val) {
157
+ return new BinaryTreeNode(key, val);
158
+ }
159
+ /**
160
+ * Clear the binary tree, removing all nodes.
161
+ */
162
+ clear() {
163
+ this._root = null;
164
+ this._size = 0;
165
+ }
166
+ /**
167
+ * Check if the binary tree is empty.
168
+ * @returns {boolean} - True if the binary tree is empty, false otherwise.
169
+ */
170
+ isEmpty() {
171
+ return this.size === 0;
172
+ }
173
+ /**
174
+ * Add a node with the given key and value to the binary tree.
175
+ * @param {BinaryTreeNodeKey | N | null} keyOrNode - The key or node to add to the binary tree.
176
+ * @param {N['val']} val - The value for the new node (optional).
177
+ * @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
178
+ */
179
+ add(keyOrNode, val) {
180
+ const _bfs = (root, newNode) => {
181
+ const queue = new queue_1.Queue([root]);
182
+ while (queue.size > 0) {
183
+ const cur = queue.shift();
184
+ if (cur) {
185
+ if (newNode && cur.key === newNode.key)
186
+ return;
187
+ const inserted = this._addTo(newNode, cur);
188
+ if (inserted !== undefined)
189
+ return inserted;
190
+ if (cur.left)
191
+ queue.push(cur.left);
192
+ if (cur.right)
193
+ queue.push(cur.right);
194
+ }
195
+ else
196
+ return;
197
+ }
198
+ return;
199
+ };
200
+ let inserted, needInsert;
201
+ if (keyOrNode === null) {
202
+ needInsert = null;
203
+ }
204
+ else if (typeof keyOrNode === 'number') {
205
+ needInsert = this.createNode(keyOrNode, val);
206
+ }
207
+ else if (keyOrNode instanceof BinaryTreeNode) {
208
+ needInsert = keyOrNode;
209
+ }
210
+ else {
211
+ return;
212
+ }
213
+ const existNode = keyOrNode ? this.get(keyOrNode, this._defaultCallbackByKey) : undefined;
214
+ if (this.root) {
215
+ if (existNode) {
216
+ existNode.val = val;
217
+ inserted = existNode;
218
+ }
219
+ else {
220
+ inserted = _bfs(this.root, needInsert);
221
+ }
222
+ }
223
+ else {
224
+ this._setRoot(needInsert);
225
+ if (needInsert !== null) {
226
+ this._setSize(1);
227
+ }
228
+ else {
229
+ this._setSize(0);
230
+ }
231
+ inserted = this.root;
232
+ }
233
+ return inserted;
234
+ }
235
+ /**
236
+ * The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
237
+ * values, and adds them to the binary tree.
238
+ * @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
239
+ * objects, or null values.
240
+ * @param {N['val'][]} [values] - The `values` parameter is an optional array of values (`N['val'][]`) that corresponds to
241
+ * the nodes or node IDs being added. It is used to set the value of each node being added. If `values` is not provided,
242
+ * the value of the nodes will be `undefined`.
243
+ * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
244
+ */
245
+ addMany(keysOrNodes, values) {
246
+ // TODO not sure addMany not be run multi times
247
+ const inserted = [];
248
+ for (let i = 0; i < keysOrNodes.length; i++) {
249
+ const keyOrNode = keysOrNodes[i];
250
+ if (keyOrNode instanceof BinaryTreeNode) {
251
+ inserted.push(this.add(keyOrNode.key, keyOrNode.val));
252
+ continue;
253
+ }
254
+ if (keyOrNode === null) {
255
+ inserted.push(this.add(null));
256
+ continue;
257
+ }
258
+ const val = values === null || values === void 0 ? void 0 : values[i];
259
+ inserted.push(this.add(keyOrNode, val));
260
+ }
261
+ return inserted;
262
+ }
263
+ /**
264
+ * The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
265
+ * @param {(BinaryTreeNodeKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
266
+ * `BinaryTreeNodeKey` or `N` values.
267
+ * @param {N[] | Array<N['val']>} [data] - The `data` parameter is an optional array of values that will be assigned to
268
+ * the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
269
+ * array. Each value in the `data` array will be assigned to the
270
+ * @returns The method is returning a boolean value.
271
+ */
272
+ refill(keysOrNodes, data) {
273
+ this.clear();
274
+ return keysOrNodes.length === this.addMany(keysOrNodes, data).length;
275
+ }
276
+ /**
277
+ * The `delete` function removes a node from a binary search tree and returns the deleted node along
278
+ * with the parent node that needs to be balanced.
279
+ * @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node (`N`) or
280
+ * a key (`BinaryTreeNodeKey`). If it is a key, the function will find the corresponding node in the
281
+ * binary tree.
282
+ * @returns an array of `BinaryTreeDeletedResult<N>` objects.
283
+ */
284
+ delete(nodeOrKey) {
285
+ const bstDeletedResult = [];
286
+ if (!this.root)
287
+ return bstDeletedResult;
288
+ const curr = typeof nodeOrKey === 'number' ? this.get(nodeOrKey) : nodeOrKey;
289
+ if (!curr)
290
+ return bstDeletedResult;
291
+ const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
292
+ let needBalanced = null, orgCurrent = curr;
293
+ if (!curr.left) {
294
+ if (!parent) {
295
+ if (curr.right !== undefined)
296
+ this._setRoot(curr.right);
297
+ }
298
+ else {
299
+ const { familyPosition: fp } = curr;
300
+ if (fp === types_1.FamilyPosition.LEFT || fp === types_1.FamilyPosition.ROOT_LEFT) {
301
+ parent.left = curr.right;
302
+ }
303
+ else if (fp === types_1.FamilyPosition.RIGHT || fp === types_1.FamilyPosition.ROOT_RIGHT) {
304
+ parent.right = curr.right;
305
+ }
306
+ needBalanced = parent;
307
+ }
308
+ }
309
+ else {
310
+ const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
311
+ if (leftSubTreeRightMost) {
312
+ const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
313
+ orgCurrent = this._swap(curr, leftSubTreeRightMost);
314
+ if (parentOfLeftSubTreeMax) {
315
+ if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
316
+ parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
317
+ else
318
+ parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
319
+ needBalanced = parentOfLeftSubTreeMax;
320
+ }
321
+ }
322
+ }
323
+ this._setSize(this.size - 1);
324
+ bstDeletedResult.push({ deleted: orgCurrent, needBalanced });
325
+ return bstDeletedResult;
326
+ }
327
+ /**
328
+ * The function `getDepth` calculates the depth of a given node in a binary tree relative to a
329
+ * specified root node.
330
+ * @param {N | BinaryTreeNodeKey | null} distNode - The `distNode` parameter represents the node
331
+ * whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value
332
+ * of the node (`BinaryTreeNodeKey`), or `null`.
333
+ * @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the
334
+ * starting node from which we want to calculate the depth. It can be either a node object or the key
335
+ * of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root
336
+ * node of the binary tree.
337
+ * @returns the depth of the `distNode` relative to the `beginRoot`.
338
+ */
339
+ getDepth(distNode, beginRoot = this.root) {
340
+ if (typeof distNode === 'number')
341
+ distNode = this.get(distNode);
342
+ if (typeof beginRoot === 'number')
343
+ beginRoot = this.get(beginRoot);
344
+ let depth = 0;
345
+ while (distNode === null || distNode === void 0 ? void 0 : distNode.parent) {
346
+ if (distNode === beginRoot) {
347
+ return depth;
348
+ }
349
+ depth++;
350
+ distNode = distNode.parent;
351
+ }
352
+ return depth;
353
+ }
354
+ /**
355
+ * The `getHeight` function calculates the maximum height of a binary tree using either recursive or
356
+ * iterative approach.
357
+ * @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the
358
+ * starting node from which the height of the binary tree is calculated. It can be either a node
359
+ * object (`N`), a key value of a node in the tree (`BinaryTreeNodeKey`), or `null` if no starting
360
+ * node is specified. If `
361
+ * @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
362
+ * height of the binary tree using a recursive approach or an iterative approach. It can have two
363
+ * possible values:
364
+ * @returns the height of the binary tree.
365
+ */
366
+ getHeight(beginRoot = this.root, iterationType = this.iterationType) {
367
+ if (typeof beginRoot === 'number')
368
+ beginRoot = this.get(beginRoot);
369
+ if (!beginRoot)
370
+ return -1;
371
+ if (iterationType === types_1.IterationType.RECURSIVE) {
372
+ const _getMaxHeight = (cur) => {
373
+ if (!cur)
374
+ return -1;
375
+ const leftHeight = _getMaxHeight(cur.left);
376
+ const rightHeight = _getMaxHeight(cur.right);
377
+ return Math.max(leftHeight, rightHeight) + 1;
378
+ };
379
+ return _getMaxHeight(beginRoot);
380
+ }
381
+ else {
382
+ if (!beginRoot) {
383
+ return -1;
384
+ }
385
+ const stack = [{ node: beginRoot, depth: 0 }];
386
+ let maxHeight = 0;
387
+ while (stack.length > 0) {
388
+ const { node, depth } = stack.pop();
389
+ if (node.left) {
390
+ stack.push({ node: node.left, depth: depth + 1 });
391
+ }
392
+ if (node.right) {
393
+ stack.push({ node: node.right, depth: depth + 1 });
394
+ }
395
+ maxHeight = Math.max(maxHeight, depth);
396
+ }
397
+ return maxHeight;
398
+ }
399
+ }
400
+ /**
401
+ * The `getMinHeight` function calculates the minimum height of a binary tree using either a
402
+ * recursive or iterative approach.
403
+ * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which we want to
404
+ * calculate the minimum height of the tree. It is optional and defaults to the root of the tree if
405
+ * not provided.
406
+ * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
407
+ * to calculate the minimum height of a binary tree. It can have two possible values:
408
+ * @returns The function `getMinHeight` returns the minimum height of a binary tree.
409
+ */
410
+ getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
411
+ var _a, _b, _c;
412
+ if (!beginRoot)
413
+ return -1;
414
+ if (iterationType === types_1.IterationType.RECURSIVE) {
415
+ const _getMinHeight = (cur) => {
416
+ if (!cur)
417
+ return 0;
418
+ if (!cur.left && !cur.right)
419
+ return 0;
420
+ const leftMinHeight = _getMinHeight(cur.left);
421
+ const rightMinHeight = _getMinHeight(cur.right);
422
+ return Math.min(leftMinHeight, rightMinHeight) + 1;
423
+ };
424
+ return _getMinHeight(beginRoot);
425
+ }
426
+ else {
427
+ const stack = [];
428
+ let node = beginRoot, last = null;
429
+ const depths = new Map();
430
+ while (stack.length > 0 || node) {
431
+ if (node) {
432
+ stack.push(node);
433
+ node = node.left;
434
+ }
435
+ else {
436
+ node = stack[stack.length - 1];
437
+ if (!node.right || last === node.right) {
438
+ node = stack.pop();
439
+ if (node) {
440
+ const leftMinHeight = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
441
+ const rightMinHeight = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
442
+ depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
443
+ last = node;
444
+ node = null;
445
+ }
446
+ }
447
+ else
448
+ node = node.right;
449
+ }
450
+ }
451
+ return (_c = depths.get(beginRoot)) !== null && _c !== void 0 ? _c : -1;
452
+ }
453
+ }
454
+ /**
455
+ * The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
456
+ * height of the tree.
457
+ * @param {N | null} beginRoot - The parameter `beginRoot` is of type `N | null`, which means it can
458
+ * either be of type `N` (representing a node in a tree) or `null` (representing an empty tree).
459
+ * @returns The method is returning a boolean value.
460
+ */
461
+ isPerfectlyBalanced(beginRoot = this.root) {
462
+ return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
463
+ }
464
+ /**
465
+ * The function `getNodes` returns an array of nodes that match a given node property, using either
466
+ * recursive or iterative traversal.
467
+ * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is either a
468
+ * `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
469
+ * searching for. It can be a specific key value or any other property of the node.
470
+ * @param callback - The `callback` parameter is a function that takes a node as input and returns a
471
+ * value. This value is compared with the `nodeProperty` parameter to determine if the node should be
472
+ * included in the result. The `callback` parameter has a default value of
473
+ * `this._defaultCallbackByKey`, which
474
+ * @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the
475
+ * first node that matches the nodeProperty. If set to true, the function will return an array with
476
+ * only one element (or an empty array if no matching node is found). If set to false (default), the
477
+ * function will continue searching for all
478
+ * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which the
479
+ * traversal of the binary tree will begin. It is optional and defaults to the root of the binary
480
+ * tree.
481
+ * @param iterationType - The `iterationType` parameter determines the type of iteration used to
482
+ * traverse the binary tree. It can have two possible values:
483
+ * @returns The function `getNodes` returns an array of nodes (`N[]`).
484
+ */
485
+ getNodes(nodeProperty, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
486
+ if (!beginRoot)
487
+ return [];
488
+ const ans = [];
489
+ if (iterationType === types_1.IterationType.RECURSIVE) {
490
+ const _traverse = (cur) => {
491
+ if (callback(cur) === nodeProperty) {
492
+ ans.push(cur);
493
+ if (onlyOne)
494
+ return;
495
+ }
496
+ if (!cur.left && !cur.right)
497
+ return;
498
+ cur.left && _traverse(cur.left);
499
+ cur.right && _traverse(cur.right);
500
+ };
501
+ _traverse(beginRoot);
502
+ }
503
+ else {
504
+ const queue = new queue_1.Queue([beginRoot]);
505
+ while (queue.size > 0) {
506
+ const cur = queue.shift();
507
+ if (cur) {
508
+ if (callback(cur) === nodeProperty) {
509
+ ans.push(cur);
510
+ if (onlyOne)
511
+ return ans;
512
+ }
513
+ cur.left && queue.push(cur.left);
514
+ cur.right && queue.push(cur.right);
515
+ }
516
+ }
517
+ }
518
+ return ans;
519
+ }
520
+ /**
521
+ * The function checks if a binary tree has a node with a given property or key.
522
+ * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is the key or value of
523
+ * the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or a
524
+ * generic type `N`.
525
+ * @param callback - The `callback` parameter is a function that is used to determine whether a node
526
+ * matches the desired criteria. It takes a node as input and returns a boolean value indicating
527
+ * whether the node matches the criteria or not. The default callback function
528
+ * `this._defaultCallbackByKey` is used if no callback function is
529
+ * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
530
+ * the node from which the search should begin. By default, it is set to `this.root`, which means the
531
+ * search will start from the root node of the binary tree. However, you can provide a different node
532
+ * as
533
+ * @param iterationType - The `iterationType` parameter specifies the type of iteration to be
534
+ * performed when searching for nodes in the binary tree. It can have one of the following values:
535
+ * @returns a boolean value.
536
+ */
537
+ has(nodeProperty, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
538
+ // TODO may support finding node by value equal
539
+ return this.getNodes(nodeProperty, callback, true, beginRoot, iterationType).length > 0;
540
+ }
541
+ /**
542
+ * The function `get` returns the first node in a binary tree that matches the given property or key.
543
+ * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is the key or value of
544
+ * the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or `N`
545
+ * type.
546
+ * @param callback - The `callback` parameter is a function that is used to determine whether a node
547
+ * matches the desired criteria. It takes a node as input and returns a boolean value indicating
548
+ * whether the node matches the criteria or not. The default callback function
549
+ * (`this._defaultCallbackByKey`) is used if no callback function is
550
+ * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
551
+ * the root node from which the search should begin.
552
+ * @param iterationType - The `iterationType` parameter specifies the type of iteration to be
553
+ * performed when searching for a node in the binary tree. It can have one of the following values:
554
+ * @returns either the found node (of type N) or null if no node is found.
555
+ */
556
+ get(nodeProperty, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
557
+ var _a;
558
+ // TODO may support finding node by value equal
559
+ return (_a = this.getNodes(nodeProperty, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
560
+ }
561
+ /**
562
+ * The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
563
+ * up to the root node, with the option to reverse the order of the nodes.
564
+ * @param {N} beginRoot - The `beginRoot` parameter represents the starting node from which you want
565
+ * to find the path to the root node.
566
+ * @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
567
+ * resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
568
+ * reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
569
+ * @returns The function `getPathToRoot` returns an array of type `N[]`.
570
+ */
571
+ getPathToRoot(beginRoot, isReverse = true) {
572
+ // TODO to support get path through passing key
573
+ const result = [];
574
+ while (beginRoot.parent) {
575
+ // Array.push + Array.reverse is more efficient than Array.unshift
576
+ // TODO may consider using Deque, so far this is not the performance bottleneck
577
+ result.push(beginRoot);
578
+ beginRoot = beginRoot.parent;
579
+ }
580
+ result.push(beginRoot);
581
+ return isReverse ? result.reverse() : result;
582
+ }
583
+ /**
584
+ * The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
585
+ * iterative traversal.
586
+ * @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter is the starting point
587
+ * for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value
588
+ * of a node (`BinaryTreeNodeKey`), or `null` if the tree is empty.
589
+ * @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
590
+ * be performed when finding the leftmost node in a binary tree. It can have two possible values:
591
+ * @returns The function `getLeftMost` returns the leftmost node (`N`) in a binary tree. If there is
592
+ * no leftmost node, it returns `null`.
593
+ */
594
+ getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
595
+ if (typeof beginRoot === 'number')
596
+ beginRoot = this.get(beginRoot);
597
+ if (!beginRoot)
598
+ return beginRoot;
599
+ if (iterationType === types_1.IterationType.RECURSIVE) {
600
+ const _traverse = (cur) => {
601
+ if (!cur.left)
602
+ return cur;
603
+ return _traverse(cur.left);
604
+ };
605
+ return _traverse(beginRoot);
606
+ }
607
+ else {
608
+ // Indirect implementation of iteration using tail recursion optimization
609
+ const _traverse = (0, utils_1.trampoline)((cur) => {
610
+ if (!cur.left)
611
+ return cur;
612
+ return _traverse.cont(cur.left);
613
+ });
614
+ return _traverse(beginRoot);
615
+ }
616
+ }
617
+ /**
618
+ * The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
619
+ * iteratively.
620
+ * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which we want to
621
+ * find the rightmost node. It is of type `N | null`, which means it can either be a node of type `N`
622
+ * or `null`. If it is `null`, it means there is no starting node
623
+ * @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
624
+ * be performed when finding the rightmost node in a binary tree. It can have two possible values:
625
+ * @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If the
626
+ * `beginRoot` parameter is `null`, it returns `null`.
627
+ */
628
+ getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
629
+ // TODO support get right most by passing key in
630
+ if (!beginRoot)
631
+ return beginRoot;
632
+ if (iterationType === types_1.IterationType.RECURSIVE) {
633
+ const _traverse = (cur) => {
634
+ if (!cur.right)
635
+ return cur;
636
+ return _traverse(cur.right);
637
+ };
638
+ return _traverse(beginRoot);
639
+ }
640
+ else {
641
+ // Indirect implementation of iteration using tail recursion optimization
642
+ const _traverse = (0, utils_1.trampoline)((cur) => {
643
+ if (!cur.right)
644
+ return cur;
645
+ return _traverse.cont(cur.right);
646
+ });
647
+ return _traverse(beginRoot);
648
+ }
649
+ }
650
+ /**
651
+ * The function `isSubtreeBST` checks if a given binary tree is a valid binary search tree.
652
+ * @param {N} beginRoot - The `beginRoot` parameter is the root node of the binary tree that you want
653
+ * to check if it is a binary search tree (BST) subtree.
654
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
655
+ * type of iteration to use when checking if a subtree is a binary search tree (BST). It can have two
656
+ * possible values:
657
+ * @returns The function `isSubtreeBST` returns a boolean value.
658
+ */
659
+ isSubtreeBST(beginRoot, iterationType = this.iterationType) {
660
+ // TODO there is a bug
661
+ if (!beginRoot)
662
+ return true;
663
+ if (iterationType === types_1.IterationType.RECURSIVE) {
664
+ const dfs = (cur, min, max) => {
665
+ if (!cur)
666
+ return true;
667
+ if (cur.key <= min || cur.key >= max)
668
+ return false;
669
+ return dfs(cur.left, min, cur.key) && dfs(cur.right, cur.key, max);
670
+ };
671
+ return dfs(beginRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
672
+ }
673
+ else {
674
+ const stack = [];
675
+ let prev = Number.MIN_SAFE_INTEGER, curr = beginRoot;
676
+ while (curr || stack.length > 0) {
677
+ while (curr) {
678
+ stack.push(curr);
679
+ curr = curr.left;
680
+ }
681
+ curr = stack.pop();
682
+ if (!curr || prev >= curr.key)
683
+ return false;
684
+ prev = curr.key;
685
+ curr = curr.right;
686
+ }
687
+ return true;
688
+ }
689
+ }
690
+ /**
691
+ * The function checks if a binary tree is a binary search tree.
692
+ * @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
693
+ * be used when checking if the binary tree is a binary search tree (BST). It is an optional
694
+ * parameter with a default value of "this.iterationType". The value of "this.iterationType" is not
695
+ * provided in
696
+ * @returns a boolean value.
697
+ */
698
+ isBST(iterationType = this.iterationType) {
699
+ if (this.root === null)
700
+ return true;
701
+ return this.isSubtreeBST(this.root, iterationType);
702
+ }
703
+ /**
704
+ * The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
705
+ * node, either recursively or iteratively.
706
+ * @param callback - The `callback` parameter is a function that will be called on each node in the
707
+ * subtree traversal. It takes a single argument, which is the current node being traversed, and
708
+ * returns a value. The return values from each callback invocation will be collected and returned as
709
+ * an array.
710
+ * @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter is the starting point
711
+ * for traversing the subtree. It can be either a node object, a key value of a node, or `null` to
712
+ * start from the root of the tree.
713
+ * @param iterationType - The `iterationType` parameter determines the type of traversal to be
714
+ * performed on the binary tree. It can have two possible values:
715
+ * @returns The function `subTreeTraverse` returns an array of `MapCallbackReturn<N>`.
716
+ */
717
+ subTreeTraverse(callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
718
+ if (typeof beginRoot === 'number')
719
+ beginRoot = this.get(beginRoot);
720
+ const ans = [];
721
+ if (!beginRoot)
722
+ return ans;
723
+ if (iterationType === types_1.IterationType.RECURSIVE) {
724
+ const _traverse = (cur) => {
725
+ ans.push(callback(cur));
726
+ cur.left && _traverse(cur.left);
727
+ cur.right && _traverse(cur.right);
728
+ };
729
+ _traverse(beginRoot);
730
+ }
731
+ else {
732
+ const stack = [beginRoot];
733
+ while (stack.length > 0) {
734
+ const cur = stack.pop();
735
+ ans.push(callback(cur));
736
+ cur.right && stack.push(cur.right);
737
+ cur.left && stack.push(cur.left);
738
+ }
739
+ }
740
+ return ans;
741
+ }
742
+ /**
743
+ * The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
744
+ * function on each node according to a specified order pattern.
745
+ * @param callback - The `callback` parameter is a function that will be called on each node during
746
+ * the depth-first search traversal. It takes a node as input and returns a value. The default value
747
+ * is `this._defaultCallbackByKey`, which is a callback function defined elsewhere in the code.
748
+ * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
749
+ * nodes are visited during the depth-first search. There are three possible values for `pattern`:
750
+ * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
751
+ * search. It determines where the search will begin in the tree or graph structure. If `beginRoot`
752
+ * is `null`, an empty array will be returned.
753
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
754
+ * iteration used in the depth-first search algorithm. It can have two possible values:
755
+ * @returns The function `dfs` returns an array of `MapCallbackReturn<N>` values.
756
+ */
757
+ dfs(callback = this._defaultCallbackByKey, pattern = 'in', beginRoot = this.root, iterationType = types_1.IterationType.ITERATIVE) {
758
+ if (!beginRoot)
759
+ return [];
760
+ const ans = [];
761
+ if (iterationType === types_1.IterationType.RECURSIVE) {
762
+ const _traverse = (node) => {
763
+ switch (pattern) {
764
+ case 'in':
765
+ if (node.left)
766
+ _traverse(node.left);
767
+ ans.push(callback(node));
768
+ if (node.right)
769
+ _traverse(node.right);
770
+ break;
771
+ case 'pre':
772
+ ans.push(callback(node));
773
+ if (node.left)
774
+ _traverse(node.left);
775
+ if (node.right)
776
+ _traverse(node.right);
777
+ break;
778
+ case 'post':
779
+ if (node.left)
780
+ _traverse(node.left);
781
+ if (node.right)
782
+ _traverse(node.right);
783
+ ans.push(callback(node));
784
+ break;
785
+ }
786
+ };
787
+ _traverse(beginRoot);
788
+ }
789
+ else {
790
+ // 0: visit, 1: print
791
+ const stack = [{ opt: 0, node: beginRoot }];
792
+ while (stack.length > 0) {
793
+ const cur = stack.pop();
794
+ if (!cur || !cur.node)
795
+ continue;
796
+ if (cur.opt === 1) {
797
+ ans.push(callback(cur.node));
798
+ }
799
+ else {
800
+ switch (pattern) {
801
+ case 'in':
802
+ stack.push({ opt: 0, node: cur.node.right });
803
+ stack.push({ opt: 1, node: cur.node });
804
+ stack.push({ opt: 0, node: cur.node.left });
805
+ break;
806
+ case 'pre':
807
+ stack.push({ opt: 0, node: cur.node.right });
808
+ stack.push({ opt: 0, node: cur.node.left });
809
+ stack.push({ opt: 1, node: cur.node });
810
+ break;
811
+ case 'post':
812
+ stack.push({ opt: 1, node: cur.node });
813
+ stack.push({ opt: 0, node: cur.node.right });
814
+ stack.push({ opt: 0, node: cur.node.left });
815
+ break;
816
+ default:
817
+ stack.push({ opt: 0, node: cur.node.right });
818
+ stack.push({ opt: 1, node: cur.node });
819
+ stack.push({ opt: 0, node: cur.node.left });
820
+ break;
821
+ }
822
+ }
823
+ }
824
+ }
825
+ return ans;
826
+ }
827
+ /**
828
+ * The bfs function performs a breadth-first search traversal on a binary tree, executing a callback
829
+ * function on each node.
830
+ * @param callback - The `callback` parameter is a function that will be called for each node in the
831
+ * breadth-first search. It takes a node of type `N` as its argument and returns a value of type
832
+ * `BFSCallbackReturn<N>`. The default value for this parameter is `this._defaultCallbackByKey
833
+ * @param {boolean} [withLevel=false] - The `withLevel` parameter is a boolean flag that determines
834
+ * whether or not to include the level of each node in the callback function. If `withLevel` is set
835
+ * to `true`, the level of each node will be passed as an argument to the callback function. If
836
+ * `withLevel` is
837
+ * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
838
+ * search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
839
+ * will not be performed and an empty array will be returned.
840
+ * @param iterationType - The `iterationType` parameter determines the type of iteration to be used
841
+ * in the breadth-first search (BFS) algorithm. It can have two possible values:
842
+ * @returns The function `bfs` returns an array of `BFSCallbackReturn<N>[]`.
843
+ */
844
+ bfs(callback = this._defaultCallbackByKey, withLevel = false, beginRoot = this.root, iterationType = this.iterationType) {
845
+ if (!beginRoot)
846
+ return [];
847
+ const ans = [];
848
+ if (iterationType === types_1.IterationType.RECURSIVE) {
849
+ const _recursive = (node, level) => {
850
+ callback && ans.push(callback(node, withLevel ? level : undefined));
851
+ if (node.left)
852
+ _recursive(node.left, level + 1);
853
+ if (node.right)
854
+ _recursive(node.right, level + 1);
855
+ };
856
+ _recursive(beginRoot, 0);
857
+ }
858
+ else {
859
+ const stack = [[beginRoot, 0]];
860
+ while (stack.length > 0) {
861
+ const head = stack.pop();
862
+ const [node, level] = head;
863
+ callback && ans.push(callback(node, withLevel ? level : undefined));
864
+ if (node.right)
865
+ stack.push([node.right, level + 1]);
866
+ if (node.left)
867
+ stack.push([node.left, level + 1]);
868
+ }
869
+ }
870
+ return ans;
871
+ }
872
+ /**
873
+ * The function returns the predecessor node of a given node in a binary tree.
874
+ * @param {N} node - The parameter "node" represents a node in a binary tree.
875
+ * @returns The function `getPredecessor` returns the predecessor node of the given node `node`.
876
+ */
877
+ getPredecessor(node) {
878
+ if (node.left) {
879
+ let predecessor = node.left;
880
+ while (!predecessor || (predecessor.right && predecessor.right !== node)) {
881
+ if (predecessor) {
882
+ predecessor = predecessor.right;
883
+ }
884
+ }
885
+ return predecessor;
886
+ }
887
+ else {
888
+ return node;
889
+ }
890
+ }
891
+ // --- start additional methods ---
892
+ /**
893
+ * The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
894
+ * algorithm and returns an array of values obtained by applying a callback function to each node.
895
+ * @param callback - The `callback` parameter is a function that will be called on each node in the
896
+ * tree. It takes a node of type `N` as input and returns a value of type `MapCallbackReturn<N>`. The
897
+ * default value for this parameter is `this._defaultCallbackByKey`.
898
+ * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
899
+ * determines the order in which the nodes of a binary tree are traversed. It can have one of the
900
+ * following values:
901
+ * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the Morris
902
+ * traversal. It specifies the root node of the tree from which the traversal should begin. If
903
+ * `beginRoot` is `null`, an empty array will be returned.
904
+ * @returns The `morris` function returns an array of `MapCallbackReturn<N>` values.
905
+ */
906
+ morris(callback = this._defaultCallbackByKey, pattern = 'in', beginRoot = this.root) {
907
+ if (beginRoot === null)
908
+ return [];
909
+ const ans = [];
910
+ let cur = beginRoot;
911
+ const _reverseEdge = (node) => {
912
+ let pre = null;
913
+ let next = null;
914
+ while (node) {
915
+ next = node.right;
916
+ node.right = pre;
917
+ pre = node;
918
+ node = next;
919
+ }
920
+ return pre;
921
+ };
922
+ const _printEdge = (node) => {
923
+ const tail = _reverseEdge(node);
924
+ let cur = tail;
925
+ while (cur) {
926
+ ans.push(callback(cur));
927
+ cur = cur.right;
928
+ }
929
+ _reverseEdge(tail);
930
+ };
931
+ switch (pattern) {
932
+ case 'in':
933
+ while (cur) {
934
+ if (cur.left) {
935
+ const predecessor = this.getPredecessor(cur);
936
+ if (!predecessor.right) {
937
+ predecessor.right = cur;
938
+ cur = cur.left;
939
+ continue;
940
+ }
941
+ else {
942
+ predecessor.right = null;
943
+ }
944
+ }
945
+ ans.push(callback(cur));
946
+ cur = cur.right;
947
+ }
948
+ break;
949
+ case 'pre':
950
+ while (cur) {
951
+ if (cur.left) {
952
+ const predecessor = this.getPredecessor(cur);
953
+ if (!predecessor.right) {
954
+ predecessor.right = cur;
955
+ ans.push(callback(cur));
956
+ cur = cur.left;
957
+ continue;
958
+ }
959
+ else {
960
+ predecessor.right = null;
961
+ }
962
+ }
963
+ else {
964
+ ans.push(callback(cur));
965
+ }
966
+ cur = cur.right;
967
+ }
968
+ break;
969
+ case 'post':
970
+ while (cur) {
971
+ if (cur.left) {
972
+ const predecessor = this.getPredecessor(cur);
973
+ if (predecessor.right === null) {
974
+ predecessor.right = cur;
975
+ cur = cur.left;
976
+ continue;
977
+ }
978
+ else {
979
+ predecessor.right = null;
980
+ _printEdge(cur.left);
981
+ }
982
+ }
983
+ cur = cur.right;
984
+ }
985
+ _printEdge(beginRoot);
986
+ break;
987
+ }
988
+ return ans;
989
+ }
990
+ /**
991
+ * Swap the data of two nodes in the binary tree.
992
+ * @param {N} srcNode - The source node to swap.
993
+ * @param {N} destNode - The destination node to swap.
994
+ * @returns {N} - The destination node after the swap.
995
+ */
996
+ _swap(srcNode, destNode) {
997
+ const { key, val } = destNode;
998
+ const tempNode = this.createNode(key, val);
999
+ if (tempNode) {
1000
+ destNode.key = srcNode.key;
1001
+ destNode.val = srcNode.val;
1002
+ srcNode.key = tempNode.key;
1003
+ srcNode.val = tempNode.val;
1004
+ }
1005
+ return destNode;
1006
+ }
1007
+ /**
1008
+ * The function `_addTo` adds a new node to a binary tree if there is an available position.
1009
+ * @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
1010
+ * the binary tree. It can be either a node object or `null`.
1011
+ * @param {N} parent - The `parent` parameter represents the parent node to which the new node will
1012
+ * be added as a child.
1013
+ * @returns either the left or right child node of the parent node, depending on which child is
1014
+ * available for adding the new node. If a new node is added, the function also updates the size of
1015
+ * the binary tree. If neither the left nor right child is available, the function returns undefined.
1016
+ * If the parent node is null, the function also returns undefined.
1017
+ */
1018
+ _addTo(newNode, parent) {
1019
+ if (parent) {
1020
+ // When all leaf nodes are null, it will no longer be possible to add new entity nodes to this binary tree.
1021
+ // In this scenario, null nodes serve as "sentinel nodes," "virtual nodes," or "placeholder nodes."
1022
+ if (parent.left === undefined) {
1023
+ parent.left = newNode;
1024
+ if (newNode) {
1025
+ this._setSize(this.size + 1);
1026
+ }
1027
+ return parent.left;
1028
+ }
1029
+ else if (parent.right === undefined) {
1030
+ parent.right = newNode;
1031
+ if (newNode) {
1032
+ this._setSize(this.size + 1);
1033
+ }
1034
+ return parent.right;
1035
+ }
1036
+ else {
1037
+ return;
1038
+ }
1039
+ }
1040
+ else {
1041
+ return;
1042
+ }
1043
+ }
1044
+ /**
1045
+ * The function sets the root property of an object to a given value, and if the value is not null,
1046
+ * it also sets the parent property of the value to undefined.
1047
+ * @param {N | null} v - The parameter `v` is of type `N | null`, which means it can either be of
1048
+ * type `N` or `null`.
1049
+ */
1050
+ _setRoot(v) {
1051
+ if (v) {
1052
+ v.parent = undefined;
1053
+ }
1054
+ this._root = v;
1055
+ }
1056
+ /**
1057
+ * The function sets the value of the protected property "_size" to the given number.
1058
+ * @param {number} v - The parameter "v" is a number that represents the size value that we want to
1059
+ * set.
1060
+ */
1061
+ _setSize(v) {
1062
+ this._size = v;
1063
+ }
1064
+ }
1065
+ exports.BinaryTree = BinaryTree;