min-heap-typed 1.40.0-rc → 1.41.0

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