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