data-structure-typed 1.21.4 → 1.32.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 (660) hide show
  1. package/.auto-changelog +9 -0
  2. package/.auto-changelog-template.hbs +36 -0
  3. package/.eslintrc.js +61 -0
  4. package/.gitattributes +112 -0
  5. package/.github/workflows/ci.yml +30 -0
  6. package/.prettierignore +6 -0
  7. package/.prettierrc.js +16 -0
  8. package/CHANGELOG.md +17 -0
  9. package/CODE-OF-CONDUCT.md +80 -0
  10. package/COMMANDS.md +28 -0
  11. package/README.md +147 -95
  12. package/SECURITY.md +15 -0
  13. package/coverage/clover.xml +3393 -0
  14. package/coverage/coverage-final.json +67 -0
  15. package/coverage/lcov-report/base.css +224 -0
  16. package/coverage/lcov-report/block-navigation.js +87 -0
  17. package/coverage/lcov-report/favicon.png +0 -0
  18. package/coverage/lcov-report/index.html +386 -0
  19. package/coverage/lcov-report/prettify.css +1 -0
  20. package/coverage/lcov-report/prettify.js +2 -0
  21. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  22. package/coverage/lcov-report/sorter.js +196 -0
  23. package/coverage/lcov-report/src/data-structures/binary-tree/aa-tree.ts.html +88 -0
  24. package/coverage/lcov-report/src/data-structures/binary-tree/abstract-binary-tree.ts.html +4966 -0
  25. package/coverage/lcov-report/src/data-structures/binary-tree/avl-tree.ts.html +1015 -0
  26. package/coverage/lcov-report/src/data-structures/binary-tree/b-tree.ts.html +88 -0
  27. package/coverage/lcov-report/src/data-structures/binary-tree/binary-indexed-tree.ts.html +313 -0
  28. package/coverage/lcov-report/src/data-structures/binary-tree/binary-tree.ts.html +226 -0
  29. package/coverage/lcov-report/src/data-structures/binary-tree/bst.ts.html +1714 -0
  30. package/coverage/lcov-report/src/data-structures/binary-tree/index.html +296 -0
  31. package/coverage/lcov-report/src/data-structures/binary-tree/index.ts.html +121 -0
  32. package/coverage/lcov-report/src/data-structures/binary-tree/rb-tree.ts.html +388 -0
  33. package/coverage/lcov-report/src/data-structures/binary-tree/segment-tree.ts.html +811 -0
  34. package/coverage/lcov-report/src/data-structures/binary-tree/splay-tree.ts.html +88 -0
  35. package/coverage/lcov-report/src/data-structures/binary-tree/tree-multiset.ts.html +2185 -0
  36. package/coverage/lcov-report/src/data-structures/binary-tree/two-three-tree.ts.html +88 -0
  37. package/coverage/lcov-report/src/data-structures/graph/abstract-graph.ts.html +3205 -0
  38. package/coverage/lcov-report/src/data-structures/graph/directed-graph.ts.html +1495 -0
  39. package/coverage/lcov-report/src/data-structures/graph/index.html +176 -0
  40. package/coverage/lcov-report/src/data-structures/graph/index.ts.html +97 -0
  41. package/coverage/lcov-report/src/data-structures/graph/map-graph.ts.html +472 -0
  42. package/coverage/lcov-report/src/data-structures/graph/undirected-graph.ts.html +907 -0
  43. package/coverage/lcov-report/src/data-structures/hash/coordinate-map.ts.html +286 -0
  44. package/coverage/lcov-report/src/data-structures/hash/coordinate-set.ts.html +253 -0
  45. package/coverage/lcov-report/src/data-structures/hash/hash-table.ts.html +88 -0
  46. package/coverage/lcov-report/src/data-structures/hash/index.html +206 -0
  47. package/coverage/lcov-report/src/data-structures/hash/index.ts.html +103 -0
  48. package/coverage/lcov-report/src/data-structures/hash/pair.ts.html +88 -0
  49. package/coverage/lcov-report/src/data-structures/hash/tree-map.ts.html +88 -0
  50. package/coverage/lcov-report/src/data-structures/hash/tree-set.ts.html +88 -0
  51. package/coverage/lcov-report/src/data-structures/heap/heap.ts.html +721 -0
  52. package/coverage/lcov-report/src/data-structures/heap/index.html +161 -0
  53. package/coverage/lcov-report/src/data-structures/heap/index.ts.html +94 -0
  54. package/coverage/lcov-report/src/data-structures/heap/max-heap.ts.html +178 -0
  55. package/coverage/lcov-report/src/data-structures/heap/min-heap.ts.html +181 -0
  56. package/coverage/lcov-report/src/data-structures/index.html +116 -0
  57. package/coverage/lcov-report/src/data-structures/index.ts.html +118 -0
  58. package/coverage/lcov-report/src/data-structures/linked-list/doubly-linked-list.ts.html +1804 -0
  59. package/coverage/lcov-report/src/data-structures/linked-list/index.html +161 -0
  60. package/coverage/lcov-report/src/data-structures/linked-list/index.ts.html +94 -0
  61. package/coverage/lcov-report/src/data-structures/linked-list/singly-linked-list.ts.html +1588 -0
  62. package/coverage/lcov-report/src/data-structures/linked-list/skip-linked-list.ts.html +88 -0
  63. package/coverage/lcov-report/src/data-structures/matrix/index.html +176 -0
  64. package/coverage/lcov-report/src/data-structures/matrix/index.ts.html +97 -0
  65. package/coverage/lcov-report/src/data-structures/matrix/matrix.ts.html +166 -0
  66. package/coverage/lcov-report/src/data-structures/matrix/matrix2d.ts.html +721 -0
  67. package/coverage/lcov-report/src/data-structures/matrix/navigator.ts.html +448 -0
  68. package/coverage/lcov-report/src/data-structures/matrix/vector2d.ts.html +1033 -0
  69. package/coverage/lcov-report/src/data-structures/priority-queue/index.html +161 -0
  70. package/coverage/lcov-report/src/data-structures/priority-queue/index.ts.html +94 -0
  71. package/coverage/lcov-report/src/data-structures/priority-queue/max-priority-queue.ts.html +253 -0
  72. package/coverage/lcov-report/src/data-structures/priority-queue/min-priority-queue.ts.html +256 -0
  73. package/coverage/lcov-report/src/data-structures/priority-queue/priority-queue.ts.html +1162 -0
  74. package/coverage/lcov-report/src/data-structures/queue/deque.ts.html +976 -0
  75. package/coverage/lcov-report/src/data-structures/queue/index.html +146 -0
  76. package/coverage/lcov-report/src/data-structures/queue/index.ts.html +91 -0
  77. package/coverage/lcov-report/src/data-structures/queue/queue.ts.html +658 -0
  78. package/coverage/lcov-report/src/data-structures/stack/index.html +131 -0
  79. package/coverage/lcov-report/src/data-structures/stack/index.ts.html +88 -0
  80. package/coverage/lcov-report/src/data-structures/stack/stack.ts.html +379 -0
  81. package/coverage/lcov-report/src/data-structures/tree/index.html +131 -0
  82. package/coverage/lcov-report/src/data-structures/tree/index.ts.html +88 -0
  83. package/coverage/lcov-report/src/data-structures/tree/tree.ts.html +292 -0
  84. package/coverage/lcov-report/src/data-structures/trie/index.html +131 -0
  85. package/coverage/lcov-report/src/data-structures/trie/index.ts.html +88 -0
  86. package/coverage/lcov-report/src/data-structures/trie/trie.ts.html +760 -0
  87. package/coverage/lcov-report/src/index.html +116 -0
  88. package/coverage/lcov-report/src/index.ts.html +97 -0
  89. package/coverage/lcov-report/src/interfaces/index.html +116 -0
  90. package/coverage/lcov-report/src/interfaces/index.ts.html +130 -0
  91. package/coverage/lcov-report/src/types/data-structures/abstract-binary-tree.ts.html +235 -0
  92. package/coverage/lcov-report/src/types/data-structures/bst.ts.html +124 -0
  93. package/coverage/lcov-report/src/types/data-structures/directed-graph.ts.html +109 -0
  94. package/coverage/lcov-report/src/types/data-structures/index.html +176 -0
  95. package/coverage/lcov-report/src/types/data-structures/index.ts.html +130 -0
  96. package/coverage/lcov-report/src/types/data-structures/rb-tree.ts.html +106 -0
  97. package/coverage/lcov-report/src/types/index.html +116 -0
  98. package/coverage/lcov-report/src/types/index.ts.html +94 -0
  99. package/coverage/lcov-report/src/types/utils/index.html +116 -0
  100. package/coverage/lcov-report/src/types/utils/index.ts.html +91 -0
  101. package/coverage/lcov-report/src/utils/index.html +131 -0
  102. package/coverage/lcov-report/src/utils/index.ts.html +88 -0
  103. package/coverage/lcov-report/src/utils/utils.ts.html +322 -0
  104. package/coverage/lcov-report/test/utils/index.html +146 -0
  105. package/coverage/lcov-report/test/utils/index.ts.html +91 -0
  106. package/coverage/lcov-report/test/utils/magnitude.ts.html +148 -0
  107. package/coverage/lcov-report/test/utils/number.ts.html +94 -0
  108. package/coverage/lcov.info +6676 -0
  109. package/dist/data-structures/binary-tree/aa-tree.js +6 -2
  110. package/dist/data-structures/binary-tree/aa-tree.js.map +1 -0
  111. package/dist/data-structures/binary-tree/abstract-binary-tree.js +398 -672
  112. package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -0
  113. package/dist/data-structures/binary-tree/avl-tree.js +122 -151
  114. package/dist/data-structures/binary-tree/avl-tree.js.map +1 -0
  115. package/dist/data-structures/binary-tree/b-tree.js +6 -2
  116. package/dist/data-structures/binary-tree/b-tree.js.map +1 -0
  117. package/dist/data-structures/binary-tree/binary-indexed-tree.js +24 -54
  118. package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -0
  119. package/dist/data-structures/binary-tree/binary-tree.js +31 -32
  120. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -0
  121. package/dist/data-structures/binary-tree/bst.js +266 -208
  122. package/dist/data-structures/binary-tree/bst.js.map +1 -0
  123. package/dist/data-structures/binary-tree/index.js +1 -0
  124. package/dist/data-structures/binary-tree/index.js.map +1 -0
  125. package/dist/data-structures/binary-tree/rb-tree.js +46 -37
  126. package/dist/data-structures/binary-tree/rb-tree.js.map +1 -0
  127. package/dist/data-structures/binary-tree/segment-tree.js +124 -127
  128. package/dist/data-structures/binary-tree/segment-tree.js.map +1 -0
  129. package/dist/data-structures/binary-tree/splay-tree.js +6 -2
  130. package/dist/data-structures/binary-tree/splay-tree.js.map +1 -0
  131. package/dist/data-structures/binary-tree/tree-multiset.js +227 -354
  132. package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -0
  133. package/dist/data-structures/binary-tree/two-three-tree.js +6 -2
  134. package/dist/data-structures/binary-tree/two-three-tree.js.map +1 -0
  135. package/dist/data-structures/graph/abstract-graph.js +582 -609
  136. package/dist/data-structures/graph/abstract-graph.js.map +1 -0
  137. package/dist/data-structures/graph/directed-graph.js +276 -312
  138. package/dist/data-structures/graph/directed-graph.js.map +1 -0
  139. package/dist/data-structures/graph/index.js +1 -0
  140. package/dist/data-structures/graph/index.js.map +1 -0
  141. package/dist/data-structures/graph/map-graph.js +88 -100
  142. package/dist/data-structures/graph/map-graph.js.map +1 -0
  143. package/dist/data-structures/graph/undirected-graph.js +180 -195
  144. package/dist/data-structures/graph/undirected-graph.js.map +1 -0
  145. package/dist/data-structures/hash/coordinate-map.js +46 -58
  146. package/dist/data-structures/hash/coordinate-map.js.map +1 -0
  147. package/dist/data-structures/hash/coordinate-set.js +43 -48
  148. package/dist/data-structures/hash/coordinate-set.js.map +1 -0
  149. package/dist/data-structures/hash/hash-table.js +6 -2
  150. package/dist/data-structures/hash/hash-table.js.map +1 -0
  151. package/dist/data-structures/hash/index.js +1 -0
  152. package/dist/data-structures/hash/index.js.map +1 -0
  153. package/dist/data-structures/hash/pair.js +6 -2
  154. package/dist/data-structures/hash/pair.js.map +1 -0
  155. package/dist/data-structures/hash/tree-map.js +6 -2
  156. package/dist/data-structures/hash/tree-map.js.map +1 -0
  157. package/dist/data-structures/hash/tree-set.js +6 -2
  158. package/dist/data-structures/hash/tree-set.js.map +1 -0
  159. package/dist/data-structures/heap/heap.js +83 -120
  160. package/dist/data-structures/heap/heap.js.map +1 -0
  161. package/dist/data-structures/heap/index.js +1 -0
  162. package/dist/data-structures/heap/index.js.map +1 -0
  163. package/dist/data-structures/heap/max-heap.js +27 -24
  164. package/dist/data-structures/heap/max-heap.js.map +1 -0
  165. package/dist/data-structures/heap/min-heap.js +27 -25
  166. package/dist/data-structures/heap/min-heap.js.map +1 -0
  167. package/dist/data-structures/index.js +1 -0
  168. package/dist/data-structures/index.js.map +1 -0
  169. package/dist/data-structures/linked-list/doubly-linked-list.js +202 -307
  170. package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -0
  171. package/dist/data-structures/linked-list/index.js +1 -0
  172. package/dist/data-structures/linked-list/index.js.map +1 -0
  173. package/dist/data-structures/linked-list/singly-linked-list.js +223 -251
  174. package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -0
  175. package/dist/data-structures/linked-list/skip-linked-list.js +6 -2
  176. package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -0
  177. package/dist/data-structures/matrix/index.js +1 -0
  178. package/dist/data-structures/matrix/index.js.map +1 -0
  179. package/dist/data-structures/matrix/matrix.js +9 -22
  180. package/dist/data-structures/matrix/matrix.js.map +1 -0
  181. package/dist/data-structures/matrix/matrix2d.js +75 -149
  182. package/dist/data-structures/matrix/matrix2d.js.map +1 -0
  183. package/dist/data-structures/matrix/navigator.js +38 -46
  184. package/dist/data-structures/matrix/navigator.js.map +1 -0
  185. package/dist/data-structures/matrix/vector2d.js +90 -254
  186. package/dist/data-structures/matrix/vector2d.js.map +1 -0
  187. package/dist/data-structures/priority-queue/index.js +1 -0
  188. package/dist/data-structures/priority-queue/index.js.map +1 -0
  189. package/dist/data-structures/priority-queue/max-priority-queue.js +47 -32
  190. package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -0
  191. package/dist/data-structures/priority-queue/min-priority-queue.js +47 -33
  192. package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -0
  193. package/dist/data-structures/priority-queue/priority-queue.js +150 -241
  194. package/dist/data-structures/priority-queue/priority-queue.js.map +1 -0
  195. package/dist/data-structures/queue/deque.js +130 -162
  196. package/dist/data-structures/queue/deque.js.map +1 -0
  197. package/dist/data-structures/queue/index.js +1 -0
  198. package/dist/data-structures/queue/index.js.map +1 -0
  199. package/dist/data-structures/queue/queue.js +181 -100
  200. package/dist/data-structures/queue/queue.js.map +1 -0
  201. package/dist/data-structures/stack/index.js +1 -0
  202. package/dist/data-structures/stack/index.js.map +1 -0
  203. package/dist/data-structures/stack/stack.js +23 -71
  204. package/dist/data-structures/stack/stack.js.map +1 -0
  205. package/dist/data-structures/tree/index.js +1 -0
  206. package/dist/data-structures/tree/index.js.map +1 -0
  207. package/dist/data-structures/tree/tree.js +46 -33
  208. package/dist/data-structures/tree/tree.js.map +1 -0
  209. package/dist/data-structures/trie/index.js +1 -0
  210. package/dist/data-structures/trie/index.js.map +1 -0
  211. package/dist/data-structures/trie/trie.js +201 -129
  212. package/dist/data-structures/trie/trie.js.map +1 -0
  213. package/dist/index.js +1 -0
  214. package/dist/index.js.map +1 -0
  215. package/dist/interfaces/abstract-binary-tree.js +1 -0
  216. package/dist/interfaces/abstract-binary-tree.js.map +1 -0
  217. package/dist/interfaces/abstract-graph.js +1 -0
  218. package/dist/interfaces/abstract-graph.js.map +1 -0
  219. package/dist/interfaces/avl-tree.js +1 -0
  220. package/dist/interfaces/avl-tree.js.map +1 -0
  221. package/dist/interfaces/binary-tree.js +1 -0
  222. package/dist/interfaces/binary-tree.js.map +1 -0
  223. package/dist/interfaces/bst.js +1 -0
  224. package/dist/interfaces/bst.js.map +1 -0
  225. package/dist/interfaces/directed-graph.js +1 -0
  226. package/dist/interfaces/directed-graph.js.map +1 -0
  227. package/dist/interfaces/doubly-linked-list.js +1 -0
  228. package/dist/interfaces/doubly-linked-list.js.map +1 -0
  229. package/dist/interfaces/heap.js +1 -0
  230. package/dist/interfaces/heap.js.map +1 -0
  231. package/dist/interfaces/index.js +1 -0
  232. package/dist/interfaces/index.js.map +1 -0
  233. package/dist/interfaces/navigator.js +1 -0
  234. package/dist/interfaces/navigator.js.map +1 -0
  235. package/dist/interfaces/priority-queue.js +1 -0
  236. package/dist/interfaces/priority-queue.js.map +1 -0
  237. package/dist/interfaces/rb-tree.js +1 -0
  238. package/dist/interfaces/rb-tree.js.map +1 -0
  239. package/dist/interfaces/segment-tree.js +1 -0
  240. package/dist/interfaces/segment-tree.js.map +1 -0
  241. package/dist/interfaces/singly-linked-list.js +1 -0
  242. package/dist/interfaces/singly-linked-list.js.map +1 -0
  243. package/dist/interfaces/tree-multiset.js +1 -0
  244. package/dist/interfaces/tree-multiset.js.map +1 -0
  245. package/dist/interfaces/undirected-graph.js +1 -0
  246. package/dist/interfaces/undirected-graph.js.map +1 -0
  247. package/dist/types/data-structures/abstract-binary-tree.js +1 -7
  248. package/dist/types/data-structures/abstract-binary-tree.js.map +1 -0
  249. package/dist/types/data-structures/abstract-graph.js +1 -0
  250. package/dist/types/data-structures/abstract-graph.js.map +1 -0
  251. package/dist/types/data-structures/avl-tree.js +1 -0
  252. package/dist/types/data-structures/avl-tree.js.map +1 -0
  253. package/dist/types/data-structures/binary-tree.js +1 -0
  254. package/dist/types/data-structures/binary-tree.js.map +1 -0
  255. package/dist/types/data-structures/bst.js +1 -0
  256. package/dist/types/data-structures/bst.js.map +1 -0
  257. package/dist/types/data-structures/directed-graph.js +1 -0
  258. package/dist/types/data-structures/directed-graph.js.map +1 -0
  259. package/dist/types/data-structures/doubly-linked-list.js +1 -0
  260. package/dist/types/data-structures/doubly-linked-list.js.map +1 -0
  261. package/dist/types/data-structures/heap.js +1 -0
  262. package/dist/types/data-structures/heap.js.map +1 -0
  263. package/dist/types/data-structures/index.js +1 -0
  264. package/dist/types/data-structures/index.js.map +1 -0
  265. package/dist/types/data-structures/map-graph.js +1 -0
  266. package/dist/types/data-structures/map-graph.js.map +1 -0
  267. package/dist/types/data-structures/navigator.js +1 -0
  268. package/dist/types/data-structures/navigator.js.map +1 -0
  269. package/dist/types/data-structures/priority-queue.js +1 -0
  270. package/dist/types/data-structures/priority-queue.js.map +1 -0
  271. package/dist/types/data-structures/rb-tree.js +1 -0
  272. package/dist/types/data-structures/rb-tree.js.map +1 -0
  273. package/dist/types/data-structures/segment-tree.js +1 -0
  274. package/dist/types/data-structures/segment-tree.js.map +1 -0
  275. package/dist/types/data-structures/singly-linked-list.js +1 -0
  276. package/dist/types/data-structures/singly-linked-list.js.map +1 -0
  277. package/dist/types/data-structures/tree-multiset.js +1 -0
  278. package/dist/types/data-structures/tree-multiset.js.map +1 -0
  279. package/dist/types/helpers.js +1 -0
  280. package/dist/types/helpers.js.map +1 -0
  281. package/dist/types/index.js +1 -0
  282. package/dist/types/index.js.map +1 -0
  283. package/dist/types/utils/index.js +1 -0
  284. package/dist/types/utils/index.js.map +1 -0
  285. package/dist/types/utils/utils.js +1 -0
  286. package/dist/types/utils/utils.js.map +1 -0
  287. package/dist/types/utils/validate-type.js +1 -0
  288. package/dist/types/utils/validate-type.js.map +1 -0
  289. package/dist/utils/index.js +1 -0
  290. package/dist/utils/index.js.map +1 -0
  291. package/dist/utils/utils.js +108 -22
  292. package/dist/utils/utils.js.map +1 -0
  293. package/docs/.nojekyll +1 -0
  294. package/docs/assets/highlight.css +127 -0
  295. package/docs/assets/main.js +58 -0
  296. package/docs/assets/search.js +1 -0
  297. package/docs/assets/style.css +1367 -0
  298. package/docs/classes/AVLTree.html +2182 -0
  299. package/docs/classes/AVLTreeNode.html +399 -0
  300. package/docs/classes/AaTree.html +202 -0
  301. package/docs/classes/AbstractBinaryTree.html +1836 -0
  302. package/docs/classes/AbstractBinaryTreeNode.html +441 -0
  303. package/docs/classes/AbstractEdge.html +345 -0
  304. package/docs/classes/AbstractGraph.html +1105 -0
  305. package/docs/classes/AbstractVertex.html +299 -0
  306. package/docs/classes/ArrayDeque.html +469 -0
  307. package/docs/classes/BST.html +2026 -0
  308. package/docs/classes/BSTNode.html +400 -0
  309. package/docs/classes/BTree.html +202 -0
  310. package/docs/classes/BinaryIndexedTree.html +371 -0
  311. package/docs/classes/BinaryTree.html +1867 -0
  312. package/docs/classes/BinaryTreeNode.html +399 -0
  313. package/docs/classes/Character.html +250 -0
  314. package/docs/classes/CoordinateMap.html +513 -0
  315. package/docs/classes/CoordinateSet.html +474 -0
  316. package/docs/classes/Deque.html +1005 -0
  317. package/docs/classes/DirectedEdge.html +404 -0
  318. package/docs/classes/DirectedGraph.html +1530 -0
  319. package/docs/classes/DirectedVertex.html +286 -0
  320. package/docs/classes/DoublyLinkedList.html +998 -0
  321. package/docs/classes/DoublyLinkedListNode.html +327 -0
  322. package/docs/classes/HashTable.html +202 -0
  323. package/docs/classes/Heap.html +647 -0
  324. package/docs/classes/HeapItem.html +296 -0
  325. package/docs/classes/LinkedListQueue.html +884 -0
  326. package/docs/classes/MapEdge.html +391 -0
  327. package/docs/classes/MapGraph.html +1583 -0
  328. package/docs/classes/MapVertex.html +356 -0
  329. package/docs/classes/Matrix2D.html +532 -0
  330. package/docs/classes/MatrixNTI2D.html +270 -0
  331. package/docs/classes/MaxHeap.html +671 -0
  332. package/docs/classes/MaxPriorityQueue.html +866 -0
  333. package/docs/classes/MinHeap.html +672 -0
  334. package/docs/classes/MinPriorityQueue.html +868 -0
  335. package/docs/classes/Navigator.html +343 -0
  336. package/docs/classes/ObjectDeque.html +527 -0
  337. package/docs/classes/Pair.html +202 -0
  338. package/docs/classes/PriorityQueue.html +790 -0
  339. package/docs/classes/Queue.html +521 -0
  340. package/docs/classes/RBTree.html +2024 -0
  341. package/docs/classes/RBTreeNode.html +431 -0
  342. package/docs/classes/SegmentTree.html +464 -0
  343. package/docs/classes/SegmentTreeNode.html +387 -0
  344. package/docs/classes/SinglyLinkedList.html +830 -0
  345. package/docs/classes/SinglyLinkedListNode.html +300 -0
  346. package/docs/classes/SkipLinkedList.html +202 -0
  347. package/docs/classes/SplayTree.html +202 -0
  348. package/docs/classes/Stack.html +398 -0
  349. package/docs/classes/TreeMap.html +202 -0
  350. package/docs/classes/TreeMultiset.html +2510 -0
  351. package/docs/classes/TreeMultisetNode.html +447 -0
  352. package/docs/classes/TreeNode.html +344 -0
  353. package/docs/classes/TreeSet.html +202 -0
  354. package/docs/classes/Trie.html +402 -0
  355. package/docs/classes/TrieNode.html +310 -0
  356. package/docs/classes/TwoThreeTree.html +202 -0
  357. package/docs/classes/UndirectedEdge.html +374 -0
  358. package/docs/classes/UndirectedGraph.html +1285 -0
  359. package/docs/classes/UndirectedVertex.html +284 -0
  360. package/docs/classes/Vector2D.html +835 -0
  361. package/docs/enums/CP.html +211 -0
  362. package/docs/enums/FamilyPosition.html +239 -0
  363. package/docs/enums/LoopType.html +212 -0
  364. package/docs/enums/RBColor.html +204 -0
  365. package/docs/enums/TopologicalProperty.html +211 -0
  366. package/docs/functions/arrayRemove.html +208 -0
  367. package/docs/functions/isThunk.html +186 -0
  368. package/docs/functions/toThunk.html +186 -0
  369. package/docs/functions/trampoline.html +186 -0
  370. package/docs/functions/trampolineAsync.html +186 -0
  371. package/docs/functions/uuidV4.html +181 -0
  372. package/docs/index.html +693 -0
  373. package/docs/interfaces/IAVLTree.html +1245 -0
  374. package/docs/interfaces/IAbstractBinaryTree.html +1101 -0
  375. package/docs/interfaces/IAbstractBinaryTreeNode.html +335 -0
  376. package/docs/interfaces/IAbstractGraph.html +433 -0
  377. package/docs/interfaces/IBST.html +1245 -0
  378. package/docs/interfaces/IDirectedGraph.html +570 -0
  379. package/docs/interfaces/IRBTree.html +1247 -0
  380. package/docs/interfaces/IUNDirectedGraph.html +463 -0
  381. package/docs/modules.html +328 -0
  382. package/docs/types/AVLTreeNodeNested.html +182 -0
  383. package/docs/types/AVLTreeOptions.html +180 -0
  384. package/docs/types/AbstractBinaryTreeNodeNested.html +182 -0
  385. package/docs/types/AbstractBinaryTreeNodeProperties.html +182 -0
  386. package/docs/types/AbstractBinaryTreeNodeProperty.html +182 -0
  387. package/docs/types/AbstractBinaryTreeOptions.html +182 -0
  388. package/docs/types/BSTComparator.html +192 -0
  389. package/docs/types/BSTNodeNested.html +182 -0
  390. package/docs/types/BSTOptions.html +182 -0
  391. package/docs/types/BinaryTreeDeletedResult.html +189 -0
  392. package/docs/types/BinaryTreeNodeId.html +177 -0
  393. package/docs/types/BinaryTreeNodeNested.html +182 -0
  394. package/docs/types/BinaryTreeNodePropertyName.html +177 -0
  395. package/docs/types/BinaryTreeOptions.html +180 -0
  396. package/docs/types/DFSOrderPattern.html +177 -0
  397. package/docs/types/DijkstraResult.html +199 -0
  398. package/docs/types/Direction.html +177 -0
  399. package/docs/types/DummyAny.html +190 -0
  400. package/docs/types/EdgeId.html +177 -0
  401. package/docs/types/HeapOptions.html +198 -0
  402. package/docs/types/IAVLTreeNode.html +184 -0
  403. package/docs/types/IBSTNode.html +184 -0
  404. package/docs/types/IBinaryTree.html +182 -0
  405. package/docs/types/IBinaryTreeNode.html +184 -0
  406. package/docs/types/IRBTreeNode.html +184 -0
  407. package/docs/types/ITreeMultiset.html +182 -0
  408. package/docs/types/ITreeMultisetNode.html +184 -0
  409. package/docs/types/KeyValueObject.html +182 -0
  410. package/docs/types/KeyValueObjectWithId.html +184 -0
  411. package/docs/types/MapGraphCoordinate.html +177 -0
  412. package/docs/types/NavigatorParams.html +211 -0
  413. package/docs/types/NodeOrPropertyName.html +177 -0
  414. package/docs/types/NonNumberNonObjectButDefined.html +177 -0
  415. package/docs/types/ObjectWithNonNumberId.html +184 -0
  416. package/docs/types/ObjectWithNumberId.html +184 -0
  417. package/docs/types/ObjectWithoutId.html +177 -0
  418. package/docs/types/PriorityQueueComparator.html +197 -0
  419. package/docs/types/PriorityQueueDFSOrderPattern.html +177 -0
  420. package/docs/types/PriorityQueueOptions.html +191 -0
  421. package/docs/types/RBTreeNodeNested.html +182 -0
  422. package/docs/types/RBTreeOptions.html +180 -0
  423. package/docs/types/RestrictValById.html +177 -0
  424. package/docs/types/SegmentTreeNodeVal.html +177 -0
  425. package/docs/types/SpecifyOptional.html +184 -0
  426. package/docs/types/Thunk.html +185 -0
  427. package/docs/types/ToThunkFn.html +185 -0
  428. package/docs/types/TopologicalStatus.html +177 -0
  429. package/docs/types/TreeMultisetNodeNested.html +182 -0
  430. package/docs/types/TreeMultisetOptions.html +180 -0
  431. package/docs/types/TrlAsyncFn.html +190 -0
  432. package/docs/types/TrlFn.html +190 -0
  433. package/docs/types/Turning.html +177 -0
  434. package/docs/types/VertexId.html +177 -0
  435. package/docs/variables/THUNK_SYMBOL.html +177 -0
  436. package/jest.config.js +7 -0
  437. package/lib/data-structures/binary-tree/aa-tree.js +2 -0
  438. package/{dist → lib}/data-structures/binary-tree/abstract-binary-tree.d.ts +194 -22
  439. package/lib/data-structures/binary-tree/abstract-binary-tree.js +1291 -0
  440. package/{dist → lib}/data-structures/binary-tree/avl-tree.d.ts +12 -12
  441. package/lib/data-structures/binary-tree/avl-tree.js +311 -0
  442. package/lib/data-structures/binary-tree/b-tree.js +2 -0
  443. package/lib/data-structures/binary-tree/binary-indexed-tree.js +69 -0
  444. package/lib/data-structures/binary-tree/binary-tree.js +35 -0
  445. package/{dist → lib}/data-structures/binary-tree/bst.d.ts +19 -8
  446. package/lib/data-structures/binary-tree/bst.js +551 -0
  447. package/lib/data-structures/binary-tree/index.js +12 -0
  448. package/{dist → lib}/data-structures/binary-tree/rb-tree.d.ts +0 -6
  449. package/lib/data-structures/binary-tree/rb-tree.js +22 -0
  450. package/lib/data-structures/binary-tree/segment-tree.js +210 -0
  451. package/lib/data-structures/binary-tree/splay-tree.js +2 -0
  452. package/{dist → lib}/data-structures/binary-tree/tree-multiset.d.ts +20 -23
  453. package/lib/data-structures/binary-tree/tree-multiset.js +673 -0
  454. package/lib/data-structures/binary-tree/two-three-tree.js +2 -0
  455. package/lib/data-structures/graph/abstract-graph.js +918 -0
  456. package/lib/data-structures/graph/directed-graph.js +416 -0
  457. package/lib/data-structures/graph/index.js +4 -0
  458. package/lib/data-structures/graph/map-graph.js +105 -0
  459. package/lib/data-structures/graph/undirected-graph.js +246 -0
  460. package/lib/data-structures/hash/coordinate-map.js +61 -0
  461. package/lib/data-structures/hash/coordinate-set.js +51 -0
  462. package/lib/data-structures/hash/hash-table.js +2 -0
  463. package/lib/data-structures/hash/index.js +6 -0
  464. package/lib/data-structures/hash/pair.js +2 -0
  465. package/lib/data-structures/hash/tree-map.js +2 -0
  466. package/lib/data-structures/hash/tree-set.js +2 -0
  467. package/lib/data-structures/heap/heap.js +152 -0
  468. package/lib/data-structures/heap/index.js +3 -0
  469. package/lib/data-structures/heap/max-heap.js +26 -0
  470. package/lib/data-structures/heap/min-heap.js +27 -0
  471. package/lib/data-structures/index.js +11 -0
  472. package/{dist → lib}/data-structures/linked-list/doubly-linked-list.d.ts +5 -5
  473. package/lib/data-structures/linked-list/doubly-linked-list.js +521 -0
  474. package/lib/data-structures/linked-list/index.js +3 -0
  475. package/{dist → lib}/data-structures/linked-list/singly-linked-list.d.ts +6 -5
  476. package/lib/data-structures/linked-list/singly-linked-list.js +443 -0
  477. package/lib/data-structures/linked-list/skip-linked-list.js +2 -0
  478. package/lib/data-structures/matrix/index.js +4 -0
  479. package/lib/data-structures/matrix/matrix.js +24 -0
  480. package/lib/data-structures/matrix/matrix2d.js +195 -0
  481. package/lib/data-structures/matrix/navigator.js +101 -0
  482. package/lib/data-structures/matrix/vector2d.js +287 -0
  483. package/lib/data-structures/priority-queue/index.js +3 -0
  484. package/lib/data-structures/priority-queue/max-priority-queue.js +39 -0
  485. package/lib/data-structures/priority-queue/min-priority-queue.js +40 -0
  486. package/lib/data-structures/priority-queue/priority-queue.js +317 -0
  487. package/{dist → lib}/data-structures/queue/deque.d.ts +45 -0
  488. package/lib/data-structures/queue/deque.js +270 -0
  489. package/lib/data-structures/queue/index.js +2 -0
  490. package/{dist → lib}/data-structures/queue/queue.d.ts +47 -13
  491. package/lib/data-structures/queue/queue.js +165 -0
  492. package/lib/data-structures/stack/index.js +1 -0
  493. package/lib/data-structures/stack/stack.js +87 -0
  494. package/lib/data-structures/tree/index.js +1 -0
  495. package/lib/data-structures/tree/tree.js +56 -0
  496. package/lib/data-structures/trie/index.js +1 -0
  497. package/lib/data-structures/trie/trie.js +205 -0
  498. package/lib/index.js +4 -0
  499. package/{dist → lib}/interfaces/avl-tree.d.ts +1 -9
  500. package/lib/interfaces/binary-tree.d.ts +4 -0
  501. package/{dist → lib}/interfaces/bst.d.ts +1 -2
  502. package/lib/interfaces/heap.js +1 -0
  503. package/lib/interfaces/index.js +15 -0
  504. package/lib/interfaces/navigator.d.ts +1 -0
  505. package/lib/interfaces/navigator.js +1 -0
  506. package/lib/interfaces/priority-queue.d.ts +1 -0
  507. package/lib/interfaces/priority-queue.js +1 -0
  508. package/{dist → lib}/interfaces/rb-tree.d.ts +1 -2
  509. package/lib/interfaces/rb-tree.js +1 -0
  510. package/lib/interfaces/segment-tree.d.ts +1 -0
  511. package/lib/interfaces/segment-tree.js +1 -0
  512. package/lib/interfaces/singly-linked-list.d.ts +1 -0
  513. package/lib/interfaces/singly-linked-list.js +1 -0
  514. package/lib/interfaces/tree-multiset.d.ts +5 -0
  515. package/lib/interfaces/tree-multiset.js +1 -0
  516. package/lib/interfaces/undirected-graph.js +1 -0
  517. package/{dist → lib}/types/data-structures/abstract-binary-tree.d.ts +1 -1
  518. package/lib/types/data-structures/abstract-binary-tree.js +22 -0
  519. package/lib/types/data-structures/abstract-graph.js +1 -0
  520. package/{dist → lib}/types/data-structures/avl-tree.d.ts +1 -1
  521. package/lib/types/data-structures/avl-tree.js +1 -0
  522. package/lib/types/data-structures/binary-tree.js +1 -0
  523. package/lib/types/data-structures/bst.js +6 -0
  524. package/lib/types/data-structures/directed-graph.js +6 -0
  525. package/lib/types/data-structures/doubly-linked-list.d.ts +1 -0
  526. package/lib/types/data-structures/doubly-linked-list.js +1 -0
  527. package/lib/types/data-structures/heap.js +1 -0
  528. package/lib/types/data-structures/index.js +15 -0
  529. package/lib/types/data-structures/map-graph.js +1 -0
  530. package/lib/types/data-structures/navigator.js +1 -0
  531. package/lib/types/data-structures/priority-queue.js +1 -0
  532. package/lib/types/data-structures/rb-tree.js +5 -0
  533. package/lib/types/data-structures/segment-tree.js +1 -0
  534. package/lib/types/data-structures/singly-linked-list.d.ts +1 -0
  535. package/lib/types/data-structures/singly-linked-list.js +1 -0
  536. package/lib/types/data-structures/tree-multiset.js +1 -0
  537. package/lib/types/helpers.d.ts +1 -0
  538. package/lib/types/helpers.js +1 -0
  539. package/lib/types/index.js +3 -0
  540. package/lib/types/utils/index.js +2 -0
  541. package/{dist → lib}/types/utils/utils.d.ts +1 -1
  542. package/lib/types/utils/utils.js +1 -0
  543. package/{dist → lib}/types/utils/validate-type.d.ts +1 -1
  544. package/lib/types/utils/validate-type.js +1 -0
  545. package/lib/utils/index.js +1 -0
  546. package/lib/utils/utils.js +57 -0
  547. package/package.json +150 -56
  548. package/rename_clear_files.sh +29 -0
  549. package/test/integration/avl-tree.test.ts +111 -0
  550. package/test/integration/bst.test.ts +371 -0
  551. package/test/integration/heap.test.js +19 -0
  552. package/test/integration/index.html +44 -0
  553. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +111 -0
  554. package/test/unit/data-structures/binary-tree/bst.test.ts +371 -0
  555. package/test/unit/data-structures/binary-tree/overall.test.ts +57 -0
  556. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +405 -0
  557. package/test/unit/data-structures/graph/abstract-graph.test.ts +5 -0
  558. package/test/unit/data-structures/graph/directed-graph.test.ts +517 -0
  559. package/test/unit/data-structures/graph/index.ts +2 -0
  560. package/test/unit/data-structures/graph/map-graph.test.ts +46 -0
  561. package/test/unit/data-structures/graph/overall.test.ts +50 -0
  562. package/test/unit/data-structures/graph/undirected-graph.test.ts +60 -0
  563. package/test/unit/data-structures/heap/heap.test.ts +56 -0
  564. package/test/unit/data-structures/heap/max-heap.test.ts +42 -0
  565. package/test/unit/data-structures/heap/min-heap.test.ts +81 -0
  566. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +365 -0
  567. package/test/unit/data-structures/linked-list/index.ts +4 -0
  568. package/test/unit/data-structures/linked-list/linked-list.test.ts +37 -0
  569. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +401 -0
  570. package/test/unit/data-structures/linked-list/skip-linked-list.test.ts +13 -0
  571. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +108 -0
  572. package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +105 -0
  573. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +27 -0
  574. package/test/unit/data-structures/queue/queue.test.ts +36 -0
  575. package/test/utils/index.ts +2 -0
  576. package/test/utils/magnitude.ts +21 -0
  577. package/test/utils/number.ts +3 -0
  578. package/tsconfig.build.json +33 -0
  579. package/tsconfig.json +38 -0
  580. package/umd/bundle.min.js +3 -0
  581. package/umd/bundle.min.js.map +1 -0
  582. package/webpack.config.js +28 -0
  583. package/dist/bundle.js +0 -2
  584. package/dist/interfaces/binary-tree.d.ts +0 -6
  585. package/dist/interfaces/tree-multiset.d.ts +0 -7
  586. /package/{dist → lib}/data-structures/binary-tree/aa-tree.d.ts +0 -0
  587. /package/{dist → lib}/data-structures/binary-tree/b-tree.d.ts +0 -0
  588. /package/{dist → lib}/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -0
  589. /package/{dist → lib}/data-structures/binary-tree/binary-tree.d.ts +0 -0
  590. /package/{dist → lib}/data-structures/binary-tree/index.d.ts +0 -0
  591. /package/{dist → lib}/data-structures/binary-tree/segment-tree.d.ts +0 -0
  592. /package/{dist → lib}/data-structures/binary-tree/splay-tree.d.ts +0 -0
  593. /package/{dist → lib}/data-structures/binary-tree/two-three-tree.d.ts +0 -0
  594. /package/{dist → lib}/data-structures/graph/abstract-graph.d.ts +0 -0
  595. /package/{dist → lib}/data-structures/graph/directed-graph.d.ts +0 -0
  596. /package/{dist → lib}/data-structures/graph/index.d.ts +0 -0
  597. /package/{dist → lib}/data-structures/graph/map-graph.d.ts +0 -0
  598. /package/{dist → lib}/data-structures/graph/undirected-graph.d.ts +0 -0
  599. /package/{dist → lib}/data-structures/hash/coordinate-map.d.ts +0 -0
  600. /package/{dist → lib}/data-structures/hash/coordinate-set.d.ts +0 -0
  601. /package/{dist → lib}/data-structures/hash/hash-table.d.ts +0 -0
  602. /package/{dist → lib}/data-structures/hash/index.d.ts +0 -0
  603. /package/{dist → lib}/data-structures/hash/pair.d.ts +0 -0
  604. /package/{dist → lib}/data-structures/hash/tree-map.d.ts +0 -0
  605. /package/{dist → lib}/data-structures/hash/tree-set.d.ts +0 -0
  606. /package/{dist → lib}/data-structures/heap/heap.d.ts +0 -0
  607. /package/{dist → lib}/data-structures/heap/index.d.ts +0 -0
  608. /package/{dist → lib}/data-structures/heap/max-heap.d.ts +0 -0
  609. /package/{dist → lib}/data-structures/heap/min-heap.d.ts +0 -0
  610. /package/{dist → lib}/data-structures/index.d.ts +0 -0
  611. /package/{dist → lib}/data-structures/linked-list/index.d.ts +0 -0
  612. /package/{dist → lib}/data-structures/linked-list/skip-linked-list.d.ts +0 -0
  613. /package/{dist → lib}/data-structures/matrix/index.d.ts +0 -0
  614. /package/{dist → lib}/data-structures/matrix/matrix.d.ts +0 -0
  615. /package/{dist → lib}/data-structures/matrix/matrix2d.d.ts +0 -0
  616. /package/{dist → lib}/data-structures/matrix/navigator.d.ts +0 -0
  617. /package/{dist → lib}/data-structures/matrix/vector2d.d.ts +0 -0
  618. /package/{dist → lib}/data-structures/priority-queue/index.d.ts +0 -0
  619. /package/{dist → lib}/data-structures/priority-queue/max-priority-queue.d.ts +0 -0
  620. /package/{dist → lib}/data-structures/priority-queue/min-priority-queue.d.ts +0 -0
  621. /package/{dist → lib}/data-structures/priority-queue/priority-queue.d.ts +0 -0
  622. /package/{dist → lib}/data-structures/queue/index.d.ts +0 -0
  623. /package/{dist → lib}/data-structures/stack/index.d.ts +0 -0
  624. /package/{dist → lib}/data-structures/stack/stack.d.ts +0 -0
  625. /package/{dist → lib}/data-structures/tree/index.d.ts +0 -0
  626. /package/{dist → lib}/data-structures/tree/tree.d.ts +0 -0
  627. /package/{dist → lib}/data-structures/trie/index.d.ts +0 -0
  628. /package/{dist → lib}/data-structures/trie/trie.d.ts +0 -0
  629. /package/{dist → lib}/index.d.ts +0 -0
  630. /package/{dist → lib}/interfaces/abstract-binary-tree.d.ts +0 -0
  631. /package/{dist/interfaces/doubly-linked-list.d.ts → lib/interfaces/abstract-binary-tree.js} +0 -0
  632. /package/{dist → lib}/interfaces/abstract-graph.d.ts +0 -0
  633. /package/{dist/interfaces/heap.d.ts → lib/interfaces/abstract-graph.js} +0 -0
  634. /package/{dist/interfaces/navigator.d.ts → lib/interfaces/avl-tree.js} +0 -0
  635. /package/{dist/interfaces/priority-queue.d.ts → lib/interfaces/binary-tree.js} +0 -0
  636. /package/{dist/interfaces/segment-tree.d.ts → lib/interfaces/bst.js} +0 -0
  637. /package/{dist → lib}/interfaces/directed-graph.d.ts +0 -0
  638. /package/{dist/interfaces/singly-linked-list.d.ts → lib/interfaces/directed-graph.js} +0 -0
  639. /package/{dist/types/data-structures → lib/interfaces}/doubly-linked-list.d.ts +0 -0
  640. /package/{dist/types/data-structures/singly-linked-list.d.ts → lib/interfaces/doubly-linked-list.js} +0 -0
  641. /package/{dist/types/helpers.d.ts → lib/interfaces/heap.d.ts} +0 -0
  642. /package/{dist → lib}/interfaces/index.d.ts +0 -0
  643. /package/{dist → lib}/interfaces/undirected-graph.d.ts +0 -0
  644. /package/{dist → lib}/types/data-structures/abstract-graph.d.ts +0 -0
  645. /package/{dist → lib}/types/data-structures/binary-tree.d.ts +0 -0
  646. /package/{dist → lib}/types/data-structures/bst.d.ts +0 -0
  647. /package/{dist → lib}/types/data-structures/directed-graph.d.ts +0 -0
  648. /package/{dist → lib}/types/data-structures/heap.d.ts +0 -0
  649. /package/{dist → lib}/types/data-structures/index.d.ts +0 -0
  650. /package/{dist → lib}/types/data-structures/map-graph.d.ts +0 -0
  651. /package/{dist → lib}/types/data-structures/navigator.d.ts +0 -0
  652. /package/{dist → lib}/types/data-structures/priority-queue.d.ts +0 -0
  653. /package/{dist → lib}/types/data-structures/rb-tree.d.ts +0 -0
  654. /package/{dist → lib}/types/data-structures/segment-tree.d.ts +0 -0
  655. /package/{dist → lib}/types/data-structures/tree-multiset.d.ts +0 -0
  656. /package/{dist → lib}/types/index.d.ts +0 -0
  657. /package/{dist → lib}/types/utils/index.d.ts +0 -0
  658. /package/{dist → lib}/utils/index.d.ts +0 -0
  659. /package/{dist → lib}/utils/utils.d.ts +0 -0
  660. /package/{dist/bundle.js.LICENSE.txt → umd/bundle.min.js.LICENSE.txt} +0 -0
@@ -0,0 +1,1291 @@
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
+ import { trampoline } from '../../utils';
9
+ import { FamilyPosition, LoopType } from '../../types';
10
+ export class AbstractBinaryTreeNode {
11
+ /**
12
+ * The constructor function initializes a BinaryTreeNode object with an id and an optional value.
13
+ * @param {BinaryTreeNodeId} id - The `id` parameter is of type `BinaryTreeNodeId` and represents the unique identifier
14
+ * of the binary tree node. It is used to distinguish one node from another in the binary tree.
15
+ * @param {T} [val] - The "val" parameter is an optional parameter of type T. It represents the value that will be
16
+ * stored in the binary tree node. If no value is provided, it will be set to undefined.
17
+ */
18
+ constructor(id, val) {
19
+ this._height = 0;
20
+ this._id = id;
21
+ this._val = val;
22
+ }
23
+ get id() {
24
+ return this._id;
25
+ }
26
+ set id(v) {
27
+ this._id = v;
28
+ }
29
+ get val() {
30
+ return this._val;
31
+ }
32
+ set val(value) {
33
+ this._val = value;
34
+ }
35
+ get left() {
36
+ return this._left;
37
+ }
38
+ set left(v) {
39
+ if (v) {
40
+ v.parent = this;
41
+ }
42
+ this._left = v;
43
+ }
44
+ get right() {
45
+ return this._right;
46
+ }
47
+ set right(v) {
48
+ if (v) {
49
+ v.parent = this;
50
+ }
51
+ this._right = v;
52
+ }
53
+ get parent() {
54
+ return this._parent;
55
+ }
56
+ set parent(v) {
57
+ this._parent = v;
58
+ }
59
+ get height() {
60
+ return this._height;
61
+ }
62
+ set height(v) {
63
+ this._height = v;
64
+ }
65
+ /**
66
+ * The function determines the position of a node in a family tree structure.
67
+ * @returns a value of type `FamilyPosition`.
68
+ */
69
+ get familyPosition() {
70
+ const that = this;
71
+ if (that.parent) {
72
+ if (that.parent.left === that) {
73
+ if (that.left || that.right) {
74
+ return FamilyPosition.ROOT_LEFT;
75
+ }
76
+ else {
77
+ return FamilyPosition.LEFT;
78
+ }
79
+ }
80
+ else if (that.parent.right === that) {
81
+ if (that.left || that.right) {
82
+ return FamilyPosition.ROOT_RIGHT;
83
+ }
84
+ else {
85
+ return FamilyPosition.RIGHT;
86
+ }
87
+ }
88
+ else {
89
+ return FamilyPosition.MAL_NODE;
90
+ }
91
+ }
92
+ else {
93
+ if (that.left || that.right) {
94
+ return FamilyPosition.ROOT;
95
+ }
96
+ else {
97
+ return FamilyPosition.ISOLATED;
98
+ }
99
+ }
100
+ }
101
+ }
102
+ export class AbstractBinaryTree {
103
+ /**
104
+ * The protected constructor initializes the options for an abstract binary tree.
105
+ * @param {AbstractBinaryTreeOptions} [options] - An optional object that contains configuration options for the binary
106
+ * tree.
107
+ */
108
+ constructor(options) {
109
+ this._root = null;
110
+ this._size = 0;
111
+ this._loopType = LoopType.ITERATIVE;
112
+ this._visitedId = [];
113
+ this._visitedVal = [];
114
+ this._visitedNode = [];
115
+ this._visitedLeftSum = [];
116
+ if (options !== undefined) {
117
+ const { loopType = LoopType.ITERATIVE } = options;
118
+ this._loopType = loopType;
119
+ }
120
+ this.clear();
121
+ }
122
+ get root() {
123
+ return this._root;
124
+ }
125
+ get size() {
126
+ return this._size;
127
+ }
128
+ get loopType() {
129
+ return this._loopType;
130
+ }
131
+ get visitedId() {
132
+ return this._visitedId;
133
+ }
134
+ get visitedVal() {
135
+ return this._visitedVal;
136
+ }
137
+ get visitedNode() {
138
+ return this._visitedNode;
139
+ }
140
+ get visitedLeftSum() {
141
+ return this._visitedLeftSum;
142
+ }
143
+ /**
144
+ * The `swapLocation` function swaps the location of two nodes in a binary tree.
145
+ * @param {N} srcNode - The source node that you want to swap with the destination node.
146
+ * @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
147
+ * be swapped to.
148
+ * @returns The `destNode` is being returned.
149
+ */
150
+ swapLocation(srcNode, destNode) {
151
+ const { id, val, height } = destNode;
152
+ const tempNode = this.createNode(id, val);
153
+ if (tempNode) {
154
+ tempNode.height = height;
155
+ destNode.id = srcNode.id;
156
+ destNode.val = srcNode.val;
157
+ destNode.height = srcNode.height;
158
+ srcNode.id = tempNode.id;
159
+ srcNode.val = tempNode.val;
160
+ srcNode.height = tempNode.height;
161
+ }
162
+ return destNode;
163
+ }
164
+ /**
165
+ * The clear() function resets the root, size, and maxId properties to their initial values.
166
+ */
167
+ clear() {
168
+ this._root = null;
169
+ this._size = 0;
170
+ this._clearResults();
171
+ }
172
+ /**
173
+ * The function checks if the size of an object is equal to zero and returns a boolean value.
174
+ * @returns A boolean value indicating whether the size of the object is 0 or not.
175
+ */
176
+ isEmpty() {
177
+ return this.size === 0;
178
+ }
179
+ /**
180
+ * When all leaf nodes are null, it will no longer be possible to add new entity nodes to this binary tree.
181
+ * In this scenario, null nodes serve as "sentinel nodes," "virtual nodes," or "placeholder nodes."
182
+ */
183
+ /**
184
+ * The `add` function adds a new node to a binary tree, either by ID or by creating a new node with a given value.
185
+ * @param {BinaryTreeNodeId | N | null} idOrNode - The `idOrNode` parameter can be either a `BinaryTreeNodeId`, which
186
+ * is a number representing the ID of a binary tree node, or it can be a `N` object, which represents a binary tree
187
+ * node itself. It can also be `null` if no node is specified.
188
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
189
+ * being added to the binary tree.
190
+ * @returns The function `add` returns either the inserted node (`N`), `null`, or `undefined`.
191
+ */
192
+ add(idOrNode, val) {
193
+ const _bfs = (root, newNode) => {
194
+ const queue = [root];
195
+ while (queue.length > 0) {
196
+ const cur = queue.shift();
197
+ if (cur) {
198
+ if (newNode && cur.id === newNode.id)
199
+ return;
200
+ const inserted = this._addTo(newNode, cur);
201
+ if (inserted !== undefined)
202
+ return inserted;
203
+ if (cur.left)
204
+ queue.push(cur.left);
205
+ if (cur.right)
206
+ queue.push(cur.right);
207
+ }
208
+ else
209
+ return;
210
+ }
211
+ return;
212
+ };
213
+ let inserted, needInsert;
214
+ if (idOrNode === null) {
215
+ needInsert = null;
216
+ }
217
+ else if (typeof idOrNode === 'number') {
218
+ needInsert = this.createNode(idOrNode, val);
219
+ }
220
+ else if (idOrNode instanceof AbstractBinaryTreeNode) {
221
+ needInsert = idOrNode;
222
+ }
223
+ else {
224
+ return;
225
+ }
226
+ const existNode = idOrNode ? this.get(idOrNode, 'id') : undefined;
227
+ if (this.root) {
228
+ if (existNode) {
229
+ existNode.val = val;
230
+ inserted = existNode;
231
+ }
232
+ else {
233
+ inserted = _bfs(this.root, needInsert);
234
+ }
235
+ }
236
+ else {
237
+ this._setRoot(needInsert);
238
+ if (needInsert !== null) {
239
+ this._setSize(1);
240
+ }
241
+ else {
242
+ this._setSize(0);
243
+ }
244
+ inserted = this.root;
245
+ }
246
+ return inserted;
247
+ }
248
+ /**
249
+ * The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
250
+ * values, and adds them to the binary tree.
251
+ * @param {(BinaryTreeNodeId | null)[] | (N | null)[]} idsOrNodes - An array of BinaryTreeNodeId or BinaryTreeNode
252
+ * objects, or null values.
253
+ * @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
254
+ * the nodes or node IDs being added. It is used to set the value of each node being added. If `data` is not provided,
255
+ * the value of the nodes will be `undefined`.
256
+ * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
257
+ */
258
+ addMany(idsOrNodes, data) {
259
+ // TODO not sure addMany not be run multi times
260
+ const inserted = [];
261
+ for (let i = 0; i < idsOrNodes.length; i++) {
262
+ const idOrNode = idsOrNodes[i];
263
+ if (idOrNode instanceof AbstractBinaryTreeNode) {
264
+ inserted.push(this.add(idOrNode.id, idOrNode.val));
265
+ continue;
266
+ }
267
+ if (idOrNode === null) {
268
+ inserted.push(this.add(null));
269
+ continue;
270
+ }
271
+ const val = data === null || data === void 0 ? void 0 : data[i];
272
+ inserted.push(this.add(idOrNode, val));
273
+ }
274
+ return inserted;
275
+ }
276
+ /**
277
+ * The `fill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
278
+ * @param {(BinaryTreeNodeId | N)[]} idsOrNodes - The `idsOrNodes` parameter is an array that can contain either
279
+ * `BinaryTreeNodeId` or `N` values.
280
+ * @param {N[] | Array<N['val']>} [data] - The `data` parameter is an optional array of values that will be assigned to
281
+ * the nodes being added. If provided, the length of the `data` array should be equal to the length of the `idsOrNodes`
282
+ * array. Each value in the `data` array will be assigned to the
283
+ * @returns The method is returning a boolean value.
284
+ */
285
+ fill(idsOrNodes, data) {
286
+ this.clear();
287
+ return idsOrNodes.length === this.addMany(idsOrNodes, data).length;
288
+ }
289
+ /**
290
+ * The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent node
291
+ * that needs to be balanced.
292
+ * @param {N | BinaryTreeNodeId} nodeOrId - The `nodeOrId` parameter can be either a node object (`N`) or a binary tree
293
+ * node ID (`BinaryTreeNodeId`).
294
+ * @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean parameter that
295
+ * determines whether to update the left sum of all nodes in the binary tree after removing a node. If
296
+ * `isUpdateAllLeftSum` is set to `true`, the left sum of all nodes will be updated. If it
297
+ * @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
298
+ */
299
+ remove(nodeOrId, isUpdateAllLeftSum) {
300
+ isUpdateAllLeftSum = isUpdateAllLeftSum === undefined ? true : isUpdateAllLeftSum;
301
+ // TODO may implement update all left sum
302
+ if (isUpdateAllLeftSum) {
303
+ }
304
+ const bstDeletedResult = [];
305
+ if (!this.root)
306
+ return bstDeletedResult;
307
+ const curr = typeof nodeOrId === 'number' ? this.get(nodeOrId) : nodeOrId;
308
+ if (!curr)
309
+ return bstDeletedResult;
310
+ const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
311
+ let needBalanced = null, orgCurrent = curr;
312
+ if (!curr.left) {
313
+ if (!parent) {
314
+ if (curr.right !== undefined)
315
+ this._setRoot(curr.right);
316
+ }
317
+ else {
318
+ const { familyPosition: fp } = curr;
319
+ if (fp === FamilyPosition.LEFT || fp === FamilyPosition.ROOT_LEFT) {
320
+ parent.left = curr.right;
321
+ }
322
+ else if (fp === FamilyPosition.RIGHT || fp === FamilyPosition.ROOT_RIGHT) {
323
+ parent.right = curr.right;
324
+ }
325
+ needBalanced = parent;
326
+ }
327
+ }
328
+ else {
329
+ const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
330
+ if (leftSubTreeRightMost) {
331
+ const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
332
+ orgCurrent = this.swapLocation(curr, leftSubTreeRightMost);
333
+ if (parentOfLeftSubTreeMax) {
334
+ if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
335
+ parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
336
+ else
337
+ parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
338
+ needBalanced = parentOfLeftSubTreeMax;
339
+ }
340
+ }
341
+ }
342
+ this._setSize(this.size - 1);
343
+ bstDeletedResult.push({ deleted: orgCurrent, needBalanced });
344
+ return bstDeletedResult;
345
+ }
346
+ /**
347
+ * The function calculates the depth of a node in a binary tree.
348
+ * @param {N | BinaryTreeNodeId | null} beginRoot - The `beginRoot` parameter can be one of the following:
349
+ * @returns the depth of the given node or binary tree.
350
+ */
351
+ getDepth(beginRoot) {
352
+ if (typeof beginRoot === 'number')
353
+ beginRoot = this.get(beginRoot, 'id');
354
+ let depth = 0;
355
+ while (beginRoot === null || beginRoot === void 0 ? void 0 : beginRoot.parent) {
356
+ depth++;
357
+ beginRoot = beginRoot.parent;
358
+ }
359
+ return depth;
360
+ }
361
+ /**
362
+ * The `getHeight` function calculates the maximum height of a binary tree, either recursively or iteratively.
363
+ * @param {N | BinaryTreeNodeId | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
364
+ * generic type representing a node in a binary tree), `BinaryTreeNodeId` (a type representing the ID of a binary tree
365
+ * node), or `null`.
366
+ * @returns the height of the binary tree.
367
+ */
368
+ getHeight(beginRoot) {
369
+ beginRoot = beginRoot !== null && beginRoot !== void 0 ? beginRoot : this.root;
370
+ if (typeof beginRoot === 'number')
371
+ beginRoot = this.get(beginRoot, 'id');
372
+ if (!beginRoot)
373
+ return -1;
374
+ if (this._loopType === LoopType.RECURSIVE) {
375
+ const _getMaxHeight = (cur) => {
376
+ if (!cur)
377
+ return -1;
378
+ const leftHeight = _getMaxHeight(cur.left);
379
+ const rightHeight = _getMaxHeight(cur.right);
380
+ return Math.max(leftHeight, rightHeight) + 1;
381
+ };
382
+ return _getMaxHeight(beginRoot);
383
+ }
384
+ else {
385
+ if (!beginRoot) {
386
+ return -1;
387
+ }
388
+ const stack = [{ node: beginRoot, depth: 0 }];
389
+ let maxHeight = 0;
390
+ while (stack.length > 0) {
391
+ const { node, depth } = stack.pop();
392
+ if (node.left) {
393
+ stack.push({ node: node.left, depth: depth + 1 });
394
+ }
395
+ if (node.right) {
396
+ stack.push({ node: node.right, depth: depth + 1 });
397
+ }
398
+ maxHeight = Math.max(maxHeight, depth);
399
+ }
400
+ return maxHeight;
401
+ }
402
+ }
403
+ /**
404
+ * The `getMinHeight` function calculates the minimum height of a binary tree using either a recursive or iterative
405
+ * approach.
406
+ * @param {N | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It
407
+ * represents the starting node from which to calculate the minimum height of a binary tree. If no value is provided
408
+ * for `beginRoot`, the `this.root` property is used as the default value.
409
+ * @returns The function `getMinHeight` returns the minimum height of the binary tree.
410
+ */
411
+ getMinHeight(beginRoot) {
412
+ var _a, _b, _c;
413
+ beginRoot = beginRoot || this.root;
414
+ if (!beginRoot)
415
+ return -1;
416
+ if (this._loopType === LoopType.RECURSIVE) {
417
+ const _getMinHeight = (cur) => {
418
+ if (!cur)
419
+ return 0;
420
+ if (!cur.left && !cur.right)
421
+ return 0;
422
+ const leftMinHeight = _getMinHeight(cur.left);
423
+ const rightMinHeight = _getMinHeight(cur.right);
424
+ return Math.min(leftMinHeight, rightMinHeight) + 1;
425
+ };
426
+ return _getMinHeight(beginRoot);
427
+ }
428
+ else {
429
+ const stack = [];
430
+ let node = beginRoot, last = null;
431
+ const depths = new Map();
432
+ while (stack.length > 0 || node) {
433
+ if (node) {
434
+ stack.push(node);
435
+ node = node.left;
436
+ }
437
+ else {
438
+ node = stack[stack.length - 1];
439
+ if (!node.right || last === node.right) {
440
+ node = stack.pop();
441
+ if (node) {
442
+ const leftMinHeight = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
443
+ const rightMinHeight = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
444
+ depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
445
+ last = node;
446
+ node = null;
447
+ }
448
+ }
449
+ else
450
+ node = node.right;
451
+ }
452
+ }
453
+ return (_c = depths.get(beginRoot)) !== null && _c !== void 0 ? _c : -1;
454
+ }
455
+ }
456
+ /**
457
+ * The function checks if a binary tree is perfectly balanced by comparing the minimum height and the height of the
458
+ * tree.
459
+ * @param {N | null} [beginRoot] - The parameter `beginRoot` is of type `N` or `null`. It represents the root node of a
460
+ * tree or null if the tree is empty.
461
+ * @returns The method is returning a boolean value.
462
+ */
463
+ isPerfectlyBalanced(beginRoot) {
464
+ return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
465
+ }
466
+ /**
467
+ * The function `getNodes` returns an array of nodes that match a given property name and value in a binary tree.
468
+ * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
469
+ * generic type `N`. It represents the property of the binary tree node that you want to search for.
470
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
471
+ * specifies the property name to use when searching for nodes. If not provided, it defaults to 'id'.
472
+ * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
473
+ * return only one node that matches the given `nodeProperty` or `propertyName`. If `onlyOne` is set to `true`, the
474
+ * function will stop traversing the tree and return the first matching node. If `only
475
+ * @returns an array of nodes (type N).
476
+ */
477
+ getNodes(nodeProperty, propertyName, onlyOne) {
478
+ if (!this.root)
479
+ return [];
480
+ propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
481
+ const result = [];
482
+ if (this.loopType === LoopType.RECURSIVE) {
483
+ const _traverse = (cur) => {
484
+ if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
485
+ return;
486
+ if (!cur.left && !cur.right)
487
+ return;
488
+ cur.left && _traverse(cur.left);
489
+ cur.right && _traverse(cur.right);
490
+ };
491
+ _traverse(this.root);
492
+ }
493
+ else {
494
+ const queue = [this.root];
495
+ while (queue.length > 0) {
496
+ const cur = queue.shift();
497
+ if (cur) {
498
+ if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
499
+ return result;
500
+ cur.left && queue.push(cur.left);
501
+ cur.right && queue.push(cur.right);
502
+ }
503
+ }
504
+ }
505
+ return result;
506
+ }
507
+ /**
508
+ * The function checks if a binary tree node has a specific property.
509
+ * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or `N`.
510
+ * It represents the property of the binary tree node that you want to check.
511
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
512
+ * specifies the name of the property to be checked in the nodes. If not provided, it defaults to 'id'.
513
+ * @returns a boolean value.
514
+ */
515
+ has(nodeProperty, propertyName) {
516
+ propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
517
+ // TODO may support finding node by value equal
518
+ return this.getNodes(nodeProperty, propertyName).length > 0;
519
+ }
520
+ /**
521
+ * The function returns the first node that matches the given property name and value, or null if no matching node is
522
+ * found.
523
+ * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or `N`.
524
+ * It represents the property of the binary tree node that you want to search for.
525
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
526
+ * specifies the property name to be used for searching the binary tree nodes. If this parameter is not provided, the
527
+ * default value is set to `'id'`.
528
+ * @returns either the value of the specified property of the node, or the node itself if no property name is provided.
529
+ * If no matching node is found, it returns null.
530
+ */
531
+ get(nodeProperty, propertyName) {
532
+ var _a;
533
+ propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
534
+ // TODO may support finding node by value equal
535
+ return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
536
+ }
537
+ /**
538
+ * The function `getPathToRoot` returns an array of nodes representing the path from a given node to the root node, with
539
+ * an option to reverse the order of the nodes.
540
+ * @param {N} node - The `node` parameter represents a node in a tree structure. It is of type `N`, which could be any
541
+ * type that represents a node in your specific implementation.
542
+ * @param {boolean} [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the resulting
543
+ * path should be reversed or not. If `isReverse` is set to `true`, the path will be reversed before returning it. If
544
+ * `isReverse` is set to `false` or not provided, the path will
545
+ * @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
546
+ */
547
+ getPathToRoot(node, isReverse = true) {
548
+ // TODO to support get path through passing id
549
+ const result = [];
550
+ while (node.parent) {
551
+ // Array.push + Array.reverse is more efficient than Array.unshift
552
+ // TODO may consider using Deque, so far this is not the performance bottleneck
553
+ result.push(node);
554
+ node = node.parent;
555
+ }
556
+ result.push(node);
557
+ return isReverse ? result.reverse() : result;
558
+ }
559
+ /**
560
+ * The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if
561
+ * no node is specified.
562
+ * @param {N | BinaryTreeNodeId | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
563
+ * generic type representing a node in a binary tree), `BinaryTreeNodeId` (a type representing the ID of a binary tree
564
+ * node), or `null`.
565
+ * @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
566
+ * provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal
567
+ * from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost
568
+ * node is found (
569
+ */
570
+ getLeftMost(beginRoot) {
571
+ if (typeof beginRoot === 'number')
572
+ beginRoot = this.get(beginRoot, 'id');
573
+ beginRoot = beginRoot !== null && beginRoot !== void 0 ? beginRoot : this.root;
574
+ if (!beginRoot)
575
+ return beginRoot;
576
+ if (this._loopType === LoopType.RECURSIVE) {
577
+ const _traverse = (cur) => {
578
+ if (!cur.left)
579
+ return cur;
580
+ return _traverse(cur.left);
581
+ };
582
+ return _traverse(beginRoot);
583
+ }
584
+ else {
585
+ // Indirect implementation of iteration using tail recursion optimization
586
+ const _traverse = trampoline((cur) => {
587
+ if (!cur.left)
588
+ return cur;
589
+ return _traverse.cont(cur.left);
590
+ });
591
+ return _traverse(beginRoot);
592
+ }
593
+ }
594
+ /**
595
+ * The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using tail
596
+ * recursion optimization.
597
+ * @param {N | null} [node] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
598
+ * starting node from which we want to find the rightmost node. If no node is provided, the function will default to
599
+ * using the root node of the data structure.
600
+ * @returns The `getRightMost` function returns the rightmost node in a binary tree. If the `node` parameter is provided,
601
+ * it returns the rightmost node starting from that node. If the `node` parameter is not provided, it returns the
602
+ * rightmost node starting from the root of the binary tree.
603
+ */
604
+ getRightMost(node) {
605
+ // TODO support get right most by passing id in
606
+ node = node !== null && node !== void 0 ? node : this.root;
607
+ if (!node)
608
+ return node;
609
+ if (this._loopType === LoopType.RECURSIVE) {
610
+ const _traverse = (cur) => {
611
+ if (!cur.right)
612
+ return cur;
613
+ return _traverse(cur.right);
614
+ };
615
+ return _traverse(node);
616
+ }
617
+ else {
618
+ // Indirect implementation of iteration using tail recursion optimization
619
+ const _traverse = trampoline((cur) => {
620
+ if (!cur.right)
621
+ return cur;
622
+ return _traverse.cont(cur.right);
623
+ });
624
+ return _traverse(node);
625
+ }
626
+ }
627
+ /**
628
+ * The function checks if a binary search tree is valid by traversing it either recursively or iteratively.
629
+ * @param {N | null} node - The `node` parameter represents the root node of a binary search tree (BST).
630
+ * @returns a boolean value.
631
+ */
632
+ isSubtreeBST(node) {
633
+ // TODO there is a bug
634
+ if (!node)
635
+ return true;
636
+ if (this._loopType === LoopType.RECURSIVE) {
637
+ const dfs = (cur, min, max) => {
638
+ if (!cur)
639
+ return true;
640
+ if (cur.id <= min || cur.id >= max)
641
+ return false;
642
+ return dfs(cur.left, min, cur.id) && dfs(cur.right, cur.id, max);
643
+ };
644
+ return dfs(node, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
645
+ }
646
+ else {
647
+ const stack = [];
648
+ let prev = Number.MIN_SAFE_INTEGER, curr = node;
649
+ while (curr || stack.length > 0) {
650
+ while (curr) {
651
+ stack.push(curr);
652
+ curr = curr.left;
653
+ }
654
+ curr = stack.pop();
655
+ if (!curr || prev >= curr.id)
656
+ return false;
657
+ prev = curr.id;
658
+ curr = curr.right;
659
+ }
660
+ return true;
661
+ }
662
+ }
663
+ /**
664
+ * The function isBST checks if the binary tree is valid binary search tree.
665
+ * @returns The `isBST()` function is returning a boolean value.
666
+ */
667
+ isBST() {
668
+ return this.isSubtreeBST(this.root);
669
+ }
670
+ /**
671
+ * The function calculates the size of a subtree by traversing it either recursively or iteratively.
672
+ * @param {N | null | undefined} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree in a
673
+ * binary tree.
674
+ * @returns the size of the subtree rooted at `subTreeRoot`.
675
+ */
676
+ getSubTreeSize(subTreeRoot) {
677
+ // TODO support id passed in
678
+ let size = 0;
679
+ if (!subTreeRoot)
680
+ return size;
681
+ if (this._loopType === LoopType.RECURSIVE) {
682
+ const _traverse = (cur) => {
683
+ size++;
684
+ cur.left && _traverse(cur.left);
685
+ cur.right && _traverse(cur.right);
686
+ };
687
+ _traverse(subTreeRoot);
688
+ return size;
689
+ }
690
+ else {
691
+ const stack = [subTreeRoot];
692
+ while (stack.length > 0) {
693
+ const cur = stack.pop();
694
+ size++;
695
+ cur.right && stack.push(cur.right);
696
+ cur.left && stack.push(cur.left);
697
+ }
698
+ return size;
699
+ }
700
+ }
701
+ /**
702
+ * The function `subTreeSum` calculates the sum of a specified property in a binary tree or subtree.
703
+ * @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
704
+ * tree or the ID of a binary tree node. It can also be `null` if there is no subtree.
705
+ * @param {BinaryTreeNodePropertyName} [propertyName] - propertyName is an optional parameter that specifies the
706
+ * property of the binary tree node to use for calculating the sum. It can be either 'id' or 'val'. If propertyName is
707
+ * not provided, it defaults to 'id'.
708
+ * @returns a number, which is the sum of the values of the specified property in the subtree rooted at `subTreeRoot`.
709
+ */
710
+ subTreeSum(subTreeRoot, propertyName) {
711
+ propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
712
+ if (typeof subTreeRoot === 'number')
713
+ subTreeRoot = this.get(subTreeRoot, 'id');
714
+ if (!subTreeRoot)
715
+ return 0;
716
+ let sum = 0;
717
+ const _sumByProperty = (cur) => {
718
+ let needSum;
719
+ switch (propertyName) {
720
+ case 'id':
721
+ needSum = cur.id;
722
+ break;
723
+ case 'val':
724
+ needSum = typeof cur.val === 'number' ? cur.val : 0;
725
+ break;
726
+ default:
727
+ needSum = cur.id;
728
+ break;
729
+ }
730
+ return needSum;
731
+ };
732
+ if (this._loopType === LoopType.RECURSIVE) {
733
+ const _traverse = (cur) => {
734
+ sum += _sumByProperty(cur);
735
+ cur.left && _traverse(cur.left);
736
+ cur.right && _traverse(cur.right);
737
+ };
738
+ _traverse(subTreeRoot);
739
+ }
740
+ else {
741
+ const stack = [subTreeRoot];
742
+ while (stack.length > 0) {
743
+ const cur = stack.pop();
744
+ sum += _sumByProperty(cur);
745
+ cur.right && stack.push(cur.right);
746
+ cur.left && stack.push(cur.left);
747
+ }
748
+ }
749
+ return sum;
750
+ }
751
+ /**
752
+ * The function `subTreeAdd` adds a delta value to a specified property of each node in a subtree.
753
+ * @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
754
+ * tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
755
+ * @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
756
+ * each node in the subtree should be incremented.
757
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
758
+ * specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'id'.
759
+ * @returns a boolean value.
760
+ */
761
+ subTreeAdd(subTreeRoot, delta, propertyName) {
762
+ propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
763
+ if (typeof subTreeRoot === 'number')
764
+ subTreeRoot = this.get(subTreeRoot, 'id');
765
+ if (!subTreeRoot)
766
+ return false;
767
+ const _addByProperty = (cur) => {
768
+ switch (propertyName) {
769
+ case 'id':
770
+ cur.id += delta;
771
+ break;
772
+ default:
773
+ cur.id += delta;
774
+ break;
775
+ }
776
+ };
777
+ if (this._loopType === LoopType.RECURSIVE) {
778
+ const _traverse = (cur) => {
779
+ _addByProperty(cur);
780
+ cur.left && _traverse(cur.left);
781
+ cur.right && _traverse(cur.right);
782
+ };
783
+ _traverse(subTreeRoot);
784
+ }
785
+ else {
786
+ const stack = [subTreeRoot];
787
+ while (stack.length > 0) {
788
+ const cur = stack.pop();
789
+ _addByProperty(cur);
790
+ cur.right && stack.push(cur.right);
791
+ cur.left && stack.push(cur.left);
792
+ }
793
+ }
794
+ return true;
795
+ }
796
+ /**
797
+ * The BFS function performs a breadth-first search on a binary tree, accumulating properties of each node based on a specified property name.
798
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - An optional parameter that represents either a node or a property name.
799
+ * If a node is provided, the BFS algorithm will be performed starting from that node.
800
+ * If a property name is provided, the BFS algorithm will be performed starting from the root node, accumulating the specified property.
801
+ * @returns An instance of the `AbstractBinaryTreeNodeProperties` class with generic type `N`.
802
+ */
803
+ BFS(nodeOrPropertyName) {
804
+ nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
805
+ this._clearResults();
806
+ const queue = [this.root];
807
+ while (queue.length !== 0) {
808
+ const cur = queue.shift();
809
+ if (cur) {
810
+ this._accumulatedByPropertyName(cur, nodeOrPropertyName);
811
+ if ((cur === null || cur === void 0 ? void 0 : cur.left) !== null)
812
+ queue.push(cur.left);
813
+ if ((cur === null || cur === void 0 ? void 0 : cur.right) !== null)
814
+ queue.push(cur.right);
815
+ }
816
+ }
817
+ return this._getResultByPropertyName(nodeOrPropertyName);
818
+ }
819
+ /**
820
+ * The DFS function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
821
+ * each node based on the specified pattern and property name.
822
+ * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
823
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. If no `nodeOrPropertyName` is provided, the default value is `'id'`.
824
+ * @returns an instance of the AbstractBinaryTreeNodeProperties class, which contains the accumulated properties of the binary tree nodes based on the specified pattern and node or property name.
825
+ */
826
+ DFS(pattern, nodeOrPropertyName) {
827
+ pattern = pattern !== null && pattern !== void 0 ? pattern : 'in';
828
+ nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
829
+ this._clearResults();
830
+ const _traverse = (node) => {
831
+ switch (pattern) {
832
+ case 'in':
833
+ if (node.left)
834
+ _traverse(node.left);
835
+ this._accumulatedByPropertyName(node, nodeOrPropertyName);
836
+ if (node.right)
837
+ _traverse(node.right);
838
+ break;
839
+ case 'pre':
840
+ this._accumulatedByPropertyName(node, nodeOrPropertyName);
841
+ if (node.left)
842
+ _traverse(node.left);
843
+ if (node.right)
844
+ _traverse(node.right);
845
+ break;
846
+ case 'post':
847
+ if (node.left)
848
+ _traverse(node.left);
849
+ if (node.right)
850
+ _traverse(node.right);
851
+ this._accumulatedByPropertyName(node, nodeOrPropertyName);
852
+ break;
853
+ }
854
+ };
855
+ this.root && _traverse(this.root);
856
+ return this._getResultByPropertyName(nodeOrPropertyName);
857
+ }
858
+ /**
859
+ * The DFSIterative function performs an iterative depth-first search traversal on a binary tree, with the option to
860
+ * specify the traversal pattern and the property name to accumulate results by.
861
+ * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
862
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. By default, it is set to `'id'`.
863
+ * @returns An object of type AbstractBinaryTreeNodeProperties<N>.
864
+ */
865
+ DFSIterative(pattern, nodeOrPropertyName) {
866
+ pattern = pattern || 'in';
867
+ nodeOrPropertyName = nodeOrPropertyName || 'id';
868
+ this._clearResults();
869
+ if (!this.root)
870
+ return this._getResultByPropertyName(nodeOrPropertyName);
871
+ // 0: visit, 1: print
872
+ const stack = [{ opt: 0, node: this.root }];
873
+ while (stack.length > 0) {
874
+ const cur = stack.pop();
875
+ if (!cur || !cur.node)
876
+ continue;
877
+ if (cur.opt === 1) {
878
+ this._accumulatedByPropertyName(cur.node, nodeOrPropertyName);
879
+ }
880
+ else {
881
+ switch (pattern) {
882
+ case 'in':
883
+ stack.push({ opt: 0, node: cur.node.right });
884
+ stack.push({ opt: 1, node: cur.node });
885
+ stack.push({ opt: 0, node: cur.node.left });
886
+ break;
887
+ case 'pre':
888
+ stack.push({ opt: 0, node: cur.node.right });
889
+ stack.push({ opt: 0, node: cur.node.left });
890
+ stack.push({ opt: 1, node: cur.node });
891
+ break;
892
+ case 'post':
893
+ stack.push({ opt: 1, node: cur.node });
894
+ stack.push({ opt: 0, node: cur.node.right });
895
+ stack.push({ opt: 0, node: cur.node.left });
896
+ break;
897
+ default:
898
+ stack.push({ opt: 0, node: cur.node.right });
899
+ stack.push({ opt: 1, node: cur.node });
900
+ stack.push({ opt: 0, node: cur.node.left });
901
+ break;
902
+ }
903
+ }
904
+ }
905
+ return this._getResultByPropertyName(nodeOrPropertyName);
906
+ }
907
+ /**
908
+ * The `levelIterative` function performs a level-order traversal on a binary tree and returns the values of the nodes
909
+ * in an array, based on a specified property name.
910
+ * @param {N | null} node - The `node` parameter is a BinaryTreeNode object representing the starting
911
+ * node for the level order traversal. It can be null if no specific node is provided, in which case the root node of
912
+ * the tree is used as the starting node.
913
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
914
+ * can be either a `BinaryTreeNode` property name or the string `'id'`. If a property name is provided, the function
915
+ * will accumulate results based on that property. If no property name is provided, the function will default to
916
+ * accumulating results based on the 'id' property.
917
+ * @returns An object of type `AbstractBinaryTreeNodeProperties<N>`.
918
+ */
919
+ levelIterative(node, nodeOrPropertyName) {
920
+ nodeOrPropertyName = nodeOrPropertyName || 'id';
921
+ node = node || this.root;
922
+ if (!node)
923
+ return [];
924
+ this._clearResults();
925
+ const queue = [node];
926
+ while (queue.length > 0) {
927
+ const cur = queue.shift();
928
+ if (cur) {
929
+ this._accumulatedByPropertyName(cur, nodeOrPropertyName);
930
+ if (cur.left) {
931
+ queue.push(cur.left);
932
+ }
933
+ if (cur.right) {
934
+ queue.push(cur.right);
935
+ }
936
+ }
937
+ }
938
+ return this._getResultByPropertyName(nodeOrPropertyName);
939
+ }
940
+ /**
941
+ * The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
942
+ * @param {N | null} node - The `node` parameter is a BinaryTreeNode object or null. It represents the root node of a binary tree. If it is null, the function will use the root node of the current binary tree instance.
943
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that specifies the property of the `BinaryTreeNode` object to collect at each level. It can be one of the following values: 'id', 'val', or 'node'. If not provided, it defaults to 'id'.
944
+ * @returns A 2D array of `AbstractBinaryTreeNodeProperty<N>` objects.
945
+ */
946
+ listLevels(node, nodeOrPropertyName) {
947
+ nodeOrPropertyName = nodeOrPropertyName || 'id';
948
+ node = node || this.root;
949
+ if (!node)
950
+ return [];
951
+ const levelsNodes = [];
952
+ const collectByProperty = (node, level) => {
953
+ switch (nodeOrPropertyName) {
954
+ case 'id':
955
+ levelsNodes[level].push(node.id);
956
+ break;
957
+ case 'val':
958
+ levelsNodes[level].push(node.val);
959
+ break;
960
+ case 'node':
961
+ levelsNodes[level].push(node);
962
+ break;
963
+ default:
964
+ levelsNodes[level].push(node.id);
965
+ break;
966
+ }
967
+ };
968
+ if (this.loopType === LoopType.RECURSIVE) {
969
+ const _recursive = (node, level) => {
970
+ if (!levelsNodes[level])
971
+ levelsNodes[level] = [];
972
+ collectByProperty(node, level);
973
+ if (node.left)
974
+ _recursive(node.left, level + 1);
975
+ if (node.right)
976
+ _recursive(node.right, level + 1);
977
+ };
978
+ _recursive(node, 0);
979
+ }
980
+ else {
981
+ const stack = [[node, 0]];
982
+ while (stack.length > 0) {
983
+ const head = stack.pop();
984
+ const [node, level] = head;
985
+ if (!levelsNodes[level])
986
+ levelsNodes[level] = [];
987
+ collectByProperty(node, level);
988
+ if (node.right)
989
+ stack.push([node.right, level + 1]);
990
+ if (node.left)
991
+ stack.push([node.left, level + 1]);
992
+ }
993
+ }
994
+ return levelsNodes;
995
+ }
996
+ /**
997
+ * The function returns the predecessor of a given node in a binary tree.
998
+ * @param node - The parameter `node` is a BinaryTreeNode object, representing a node in a binary tree.
999
+ * @returns the predecessor of the given node in a binary tree.
1000
+ */
1001
+ getPredecessor(node) {
1002
+ if (node.left) {
1003
+ let predecessor = node.left;
1004
+ while (!predecessor || (predecessor.right && predecessor.right !== node)) {
1005
+ if (predecessor) {
1006
+ predecessor = predecessor.right;
1007
+ }
1008
+ }
1009
+ return predecessor;
1010
+ }
1011
+ else {
1012
+ return node;
1013
+ }
1014
+ }
1015
+ /**
1016
+ * The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm.
1017
+ * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
1018
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The property name of the nodes to retrieve or perform operations on during the traversal. It can be any valid property name of the nodes in the binary tree. If not provided, it defaults to 'id'.
1019
+ * @returns An array of AbstractBinaryTreeNodeProperties<N> objects.
1020
+ */
1021
+ morris(pattern, nodeOrPropertyName) {
1022
+ if (this.root === null)
1023
+ return [];
1024
+ pattern = pattern || 'in';
1025
+ nodeOrPropertyName = nodeOrPropertyName || 'id';
1026
+ this._clearResults();
1027
+ let cur = this.root;
1028
+ const _reverseEdge = (node) => {
1029
+ let pre = null;
1030
+ let next = null;
1031
+ while (node) {
1032
+ next = node.right;
1033
+ node.right = pre;
1034
+ pre = node;
1035
+ node = next;
1036
+ }
1037
+ return pre;
1038
+ };
1039
+ const _printEdge = (node) => {
1040
+ const tail = _reverseEdge(node);
1041
+ let cur = tail;
1042
+ while (cur) {
1043
+ this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1044
+ cur = cur.right;
1045
+ }
1046
+ _reverseEdge(tail);
1047
+ };
1048
+ switch (pattern) {
1049
+ case 'in':
1050
+ while (cur) {
1051
+ if (cur.left) {
1052
+ const predecessor = this.getPredecessor(cur);
1053
+ if (!predecessor.right) {
1054
+ predecessor.right = cur;
1055
+ cur = cur.left;
1056
+ continue;
1057
+ }
1058
+ else {
1059
+ predecessor.right = null;
1060
+ }
1061
+ }
1062
+ this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1063
+ cur = cur.right;
1064
+ }
1065
+ break;
1066
+ case 'pre':
1067
+ while (cur) {
1068
+ if (cur.left) {
1069
+ const predecessor = this.getPredecessor(cur);
1070
+ if (!predecessor.right) {
1071
+ predecessor.right = cur;
1072
+ this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1073
+ cur = cur.left;
1074
+ continue;
1075
+ }
1076
+ else {
1077
+ predecessor.right = null;
1078
+ }
1079
+ }
1080
+ else {
1081
+ this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1082
+ }
1083
+ cur = cur.right;
1084
+ }
1085
+ break;
1086
+ case 'post':
1087
+ while (cur) {
1088
+ if (cur.left) {
1089
+ const predecessor = this.getPredecessor(cur);
1090
+ if (predecessor.right === null) {
1091
+ predecessor.right = cur;
1092
+ cur = cur.left;
1093
+ continue;
1094
+ }
1095
+ else {
1096
+ predecessor.right = null;
1097
+ _printEdge(cur.left);
1098
+ }
1099
+ }
1100
+ cur = cur.right;
1101
+ }
1102
+ _printEdge(this.root);
1103
+ break;
1104
+ }
1105
+ return this._getResultByPropertyName(nodeOrPropertyName);
1106
+ }
1107
+ /**
1108
+ * The function adds a new node to a binary tree if there is an available position.
1109
+ * @param {N | null} newNode - The `newNode` parameter is of type `N | null`, which means it can either be a node of
1110
+ * type `N` or `null`. It represents the node that you want to add to the binary tree.
1111
+ * @param {N} parent - The parent parameter is of type N, which represents a node in a binary tree.
1112
+ * @returns either the left or right child node of the parent node, depending on which child is available for adding
1113
+ * the new node. If a new node is added, the function also updates the size of the binary tree. If neither the left nor
1114
+ * right child is available, the function returns undefined. If the parent node is null, the function also returns
1115
+ * undefined.
1116
+ */
1117
+ _addTo(newNode, parent) {
1118
+ if (parent) {
1119
+ // When all leaf nodes are null, it will no longer be possible to add new entity nodes to this binary tree.
1120
+ // In this scenario, null nodes serve as "sentinel nodes," "virtual nodes," or "placeholder nodes."
1121
+ if (parent.left === undefined) {
1122
+ parent.left = newNode;
1123
+ if (newNode) {
1124
+ this._setSize(this.size + 1);
1125
+ }
1126
+ return parent.left;
1127
+ }
1128
+ else if (parent.right === undefined) {
1129
+ parent.right = newNode;
1130
+ if (newNode) {
1131
+ this._setSize(this.size + 1);
1132
+ }
1133
+ return parent.right;
1134
+ }
1135
+ else {
1136
+ return;
1137
+ }
1138
+ }
1139
+ else {
1140
+ return;
1141
+ }
1142
+ }
1143
+ /**
1144
+ * The function sets the loop type for a protected variable.
1145
+ * @param {LoopType} value - The value parameter is of type LoopType.
1146
+ */
1147
+ _setLoopType(value) {
1148
+ this._loopType = value;
1149
+ }
1150
+ /**
1151
+ * The function sets the value of the `_visitedId` property in a protected manner.
1152
+ * @param {BinaryTreeNodeId[]} value - value is an array of BinaryTreeNodeId values.
1153
+ */
1154
+ _setVisitedId(value) {
1155
+ this._visitedId = value;
1156
+ }
1157
+ /**
1158
+ * The function sets the value of the "_visitedVal" property to the given array.
1159
+ * @param value - An array of type N.
1160
+ */
1161
+ _setVisitedVal(value) {
1162
+ this._visitedVal = value;
1163
+ }
1164
+ /**
1165
+ * The function sets the value of the _visitedNode property.
1166
+ * @param {N[]} value - N[] is an array of elements of type N.
1167
+ */
1168
+ _setVisitedNode(value) {
1169
+ this._visitedNode = value;
1170
+ }
1171
+ /**
1172
+ * The function sets the value of the `_visitedLeftSum` property to the provided array.
1173
+ * @param {number[]} value - An array of numbers that represents the visited left sum.
1174
+ */
1175
+ _setVisitedLeftSum(value) {
1176
+ this._visitedLeftSum = value;
1177
+ }
1178
+ /**
1179
+ * The function sets the root property of an object to a given value, and if the value is not null, it also sets the
1180
+ * parent property of the value to undefined.
1181
+ * @param {N | null} v - The parameter `v` is of type `N | null`, which means it can either be of type `N` or `null`.
1182
+ */
1183
+ _setRoot(v) {
1184
+ if (v) {
1185
+ v.parent = undefined;
1186
+ }
1187
+ this._root = v;
1188
+ }
1189
+ /**
1190
+ * The function sets the size of a protected variable.
1191
+ * @param {number} v - number
1192
+ */
1193
+ _setSize(v) {
1194
+ this._size = v;
1195
+ }
1196
+ /**
1197
+ * The function `_clearResults` resets the values of several arrays used for tracking visited nodes and their
1198
+ * properties.
1199
+ */
1200
+ _clearResults() {
1201
+ this._visitedId = [];
1202
+ this._visitedVal = [];
1203
+ this._visitedNode = [];
1204
+ this._visitedLeftSum = [];
1205
+ }
1206
+ /**
1207
+ * The function checks if a given property of a binary tree node matches a specified value, and if so, adds the node to
1208
+ * a result array.
1209
+ * @param {N} cur - The current node being processed.
1210
+ * @param {(N | null | undefined)[]} result - An array that stores the matching nodes.
1211
+ * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter is either a `BinaryTreeNodeId` or a `N`
1212
+ * type. It represents the property value that we are comparing against in the switch statement.
1213
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
1214
+ * specifies the property name to compare against when pushing nodes into the `result` array. It can be either `'id'`
1215
+ * or `'val'`. If it is not provided or is not equal to `'id'` or `'val'`, the
1216
+ * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
1217
+ * stop after finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set to
1218
+ * `true`, the function will stop after finding the first matching node and return `true`. If `onlyOne
1219
+ * @returns a boolean value indicating whether only one matching node should be pushed into the result array.
1220
+ */
1221
+ _pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne) {
1222
+ switch (propertyName) {
1223
+ case 'id':
1224
+ if (cur.id === nodeProperty) {
1225
+ result.push(cur);
1226
+ return !!onlyOne;
1227
+ }
1228
+ break;
1229
+ case 'val':
1230
+ if (cur.val === nodeProperty) {
1231
+ result.push(cur);
1232
+ return !!onlyOne;
1233
+ }
1234
+ break;
1235
+ default:
1236
+ if (cur.id === nodeProperty) {
1237
+ result.push(cur);
1238
+ return !!onlyOne;
1239
+ }
1240
+ break;
1241
+ }
1242
+ }
1243
+ /**
1244
+ * The function `_accumulatedByPropertyName` accumulates values from a given node based on the specified property name.
1245
+ * @param {N} node - The `node` parameter is of type `N`, which represents a node in a data structure.
1246
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
1247
+ * can be either a string representing a property name or a reference to a `Node` object. If it is a string, it
1248
+ * specifies the property name to be used for accumulating values. If it is a `Node` object, it specifies
1249
+ */
1250
+ _accumulatedByPropertyName(node, nodeOrPropertyName) {
1251
+ nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
1252
+ switch (nodeOrPropertyName) {
1253
+ case 'id':
1254
+ this._visitedId.push(node.id);
1255
+ break;
1256
+ case 'val':
1257
+ this._visitedVal.push(node.val);
1258
+ break;
1259
+ case 'node':
1260
+ this._visitedNode.push(node);
1261
+ break;
1262
+ default:
1263
+ this._visitedId.push(node.id);
1264
+ break;
1265
+ }
1266
+ }
1267
+ /**
1268
+ * The time complexity of Morris traversal is O(n), it's may slower than others
1269
+ * The space complexity Morris traversal is O(1) because no using stack
1270
+ */
1271
+ /**
1272
+ * The function `_getResultByPropertyName` returns the corresponding property value based on the given node or property
1273
+ * name.
1274
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
1275
+ * can accept either a `NodeOrPropertyName` type or be undefined.
1276
+ * @returns The method `_getResultByPropertyName` returns an instance of `AbstractBinaryTreeNodeProperties<N>`.
1277
+ */
1278
+ _getResultByPropertyName(nodeOrPropertyName) {
1279
+ nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
1280
+ switch (nodeOrPropertyName) {
1281
+ case 'id':
1282
+ return this._visitedId;
1283
+ case 'val':
1284
+ return this._visitedVal;
1285
+ case 'node':
1286
+ return this._visitedNode;
1287
+ default:
1288
+ return this._visitedId;
1289
+ }
1290
+ }
1291
+ }