data-structure-typed 2.1.0 → 2.1.1

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 (704) hide show
  1. package/CHANGELOG.md +5 -1
  2. package/COMMANDS.md +13 -3
  3. package/README.md +5 -34
  4. package/benchmark/report.html +1 -1
  5. package/benchmark/report.json +101 -101
  6. package/dist/cjs/index.cjs +13062 -0
  7. package/dist/cjs/index.cjs.map +1 -0
  8. package/dist/esm/index.mjs +12984 -0
  9. package/dist/esm/index.mjs.map +1 -0
  10. package/dist/index.cjs +13091 -0
  11. package/dist/index.cjs.map +1 -0
  12. package/dist/index.js +13013 -0
  13. package/dist/index.js.map +1 -0
  14. package/dist/{cjs → types}/data-structures/binary-tree/avl-tree-counter.d.ts +4 -4
  15. package/dist/{esm → types}/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
  16. package/dist/{esm → types}/data-structures/binary-tree/avl-tree.d.ts +4 -4
  17. package/dist/{esm → types}/data-structures/binary-tree/binary-tree.d.ts +5 -5
  18. package/dist/{esm → types}/data-structures/binary-tree/bst.d.ts +5 -5
  19. package/dist/{esm → types}/data-structures/binary-tree/red-black-tree.d.ts +5 -5
  20. package/dist/{cjs → types}/data-structures/binary-tree/tree-counter.d.ts +4 -4
  21. package/dist/{cjs → types}/data-structures/binary-tree/tree-multi-map.d.ts +5 -5
  22. package/dist/{esm → types}/interfaces/binary-tree.d.ts +2 -2
  23. package/dist/umd/data-structure-typed.js +12 -11
  24. package/dist/umd/data-structure-typed.js.map +1 -0
  25. package/dist/umd/data-structure-typed.min.js +2 -2
  26. package/dist/umd/data-structure-typed.min.js.map +1 -1
  27. package/jest.integration.config.js +3 -0
  28. package/package.json +13 -11
  29. package/src/data-structures/binary-tree/avl-tree-counter.ts +4 -7
  30. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +5 -10
  31. package/src/data-structures/binary-tree/avl-tree.ts +4 -6
  32. package/src/data-structures/binary-tree/binary-tree.ts +14 -16
  33. package/src/data-structures/binary-tree/bst.ts +5 -10
  34. package/src/data-structures/binary-tree/red-black-tree.ts +5 -10
  35. package/src/data-structures/binary-tree/tree-counter.ts +4 -9
  36. package/src/data-structures/binary-tree/tree-multi-map.ts +5 -10
  37. package/src/interfaces/binary-tree.ts +2 -2
  38. package/test/integration/all-in-one.test.ts +1 -1
  39. package/test/performance/data-structures/comparison/comparison.test.ts +27 -57
  40. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +109 -1
  41. package/tsconfig-base.json +20 -20
  42. package/tsconfig-types.json +17 -0
  43. package/tsconfig.test.json +8 -0
  44. package/tsup.config.js +11 -22
  45. package/tsup.node.config.ts +37 -0
  46. package/dist/cjs/common/index.js +0 -29
  47. package/dist/cjs/common/index.js.map +0 -1
  48. package/dist/cjs/data-structures/base/index.js +0 -19
  49. package/dist/cjs/data-structures/base/index.js.map +0 -1
  50. package/dist/cjs/data-structures/base/iterable-element-base.js +0 -244
  51. package/dist/cjs/data-structures/base/iterable-element-base.js.map +0 -1
  52. package/dist/cjs/data-structures/base/iterable-entry-base.js +0 -184
  53. package/dist/cjs/data-structures/base/iterable-entry-base.js.map +0 -1
  54. package/dist/cjs/data-structures/base/linear-base.js +0 -416
  55. package/dist/cjs/data-structures/base/linear-base.js.map +0 -1
  56. package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js +0 -375
  57. package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js.map +0 -1
  58. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -135
  59. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +0 -251
  60. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +0 -1
  61. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +0 -291
  62. package/dist/cjs/data-structures/binary-tree/avl-tree.js +0 -612
  63. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +0 -1
  64. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +0 -295
  65. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +0 -1
  66. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +0 -754
  67. package/dist/cjs/data-structures/binary-tree/binary-tree.js +0 -1926
  68. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +0 -1
  69. package/dist/cjs/data-structures/binary-tree/bst.d.ts +0 -413
  70. package/dist/cjs/data-structures/binary-tree/bst.js +0 -904
  71. package/dist/cjs/data-structures/binary-tree/bst.js.map +0 -1
  72. package/dist/cjs/data-structures/binary-tree/index.js +0 -27
  73. package/dist/cjs/data-structures/binary-tree/index.js.map +0 -1
  74. package/dist/cjs/data-structures/binary-tree/red-black-tree.d.ts +0 -208
  75. package/dist/cjs/data-structures/binary-tree/red-black-tree.js +0 -547
  76. package/dist/cjs/data-structures/binary-tree/red-black-tree.js.map +0 -1
  77. package/dist/cjs/data-structures/binary-tree/segment-tree.js +0 -298
  78. package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +0 -1
  79. package/dist/cjs/data-structures/binary-tree/tree-counter.js +0 -414
  80. package/dist/cjs/data-structures/binary-tree/tree-counter.js.map +0 -1
  81. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +0 -385
  82. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +0 -1
  83. package/dist/cjs/data-structures/graph/abstract-graph.js +0 -897
  84. package/dist/cjs/data-structures/graph/abstract-graph.js.map +0 -1
  85. package/dist/cjs/data-structures/graph/directed-graph.js +0 -526
  86. package/dist/cjs/data-structures/graph/directed-graph.js.map +0 -1
  87. package/dist/cjs/data-structures/graph/index.js +0 -21
  88. package/dist/cjs/data-structures/graph/index.js.map +0 -1
  89. package/dist/cjs/data-structures/graph/map-graph.js +0 -108
  90. package/dist/cjs/data-structures/graph/map-graph.js.map +0 -1
  91. package/dist/cjs/data-structures/graph/undirected-graph.js +0 -425
  92. package/dist/cjs/data-structures/graph/undirected-graph.js.map +0 -1
  93. package/dist/cjs/data-structures/hash/hash-map.js +0 -693
  94. package/dist/cjs/data-structures/hash/hash-map.js.map +0 -1
  95. package/dist/cjs/data-structures/hash/index.js +0 -18
  96. package/dist/cjs/data-structures/hash/index.js.map +0 -1
  97. package/dist/cjs/data-structures/heap/heap.js +0 -902
  98. package/dist/cjs/data-structures/heap/heap.js.map +0 -1
  99. package/dist/cjs/data-structures/heap/index.js +0 -20
  100. package/dist/cjs/data-structures/heap/index.js.map +0 -1
  101. package/dist/cjs/data-structures/heap/max-heap.js +0 -41
  102. package/dist/cjs/data-structures/heap/max-heap.js.map +0 -1
  103. package/dist/cjs/data-structures/heap/min-heap.js +0 -32
  104. package/dist/cjs/data-structures/heap/min-heap.js.map +0 -1
  105. package/dist/cjs/data-structures/index.js +0 -29
  106. package/dist/cjs/data-structures/index.js.map +0 -1
  107. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +0 -1112
  108. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +0 -1
  109. package/dist/cjs/data-structures/linked-list/index.js +0 -20
  110. package/dist/cjs/data-structures/linked-list/index.js.map +0 -1
  111. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +0 -851
  112. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +0 -1
  113. package/dist/cjs/data-structures/linked-list/skip-linked-list.js +0 -145
  114. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +0 -1
  115. package/dist/cjs/data-structures/matrix/index.js +0 -19
  116. package/dist/cjs/data-structures/matrix/index.js.map +0 -1
  117. package/dist/cjs/data-structures/matrix/matrix.js +0 -449
  118. package/dist/cjs/data-structures/matrix/matrix.js.map +0 -1
  119. package/dist/cjs/data-structures/matrix/navigator.js +0 -112
  120. package/dist/cjs/data-structures/matrix/navigator.js.map +0 -1
  121. package/dist/cjs/data-structures/priority-queue/index.js +0 -20
  122. package/dist/cjs/data-structures/priority-queue/index.js.map +0 -1
  123. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +0 -35
  124. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +0 -1
  125. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +0 -25
  126. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +0 -1
  127. package/dist/cjs/data-structures/priority-queue/priority-queue.js +0 -21
  128. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +0 -1
  129. package/dist/cjs/data-structures/queue/deque.js +0 -880
  130. package/dist/cjs/data-structures/queue/deque.js.map +0 -1
  131. package/dist/cjs/data-structures/queue/index.js +0 -19
  132. package/dist/cjs/data-structures/queue/index.js.map +0 -1
  133. package/dist/cjs/data-structures/queue/queue.js +0 -474
  134. package/dist/cjs/data-structures/queue/queue.js.map +0 -1
  135. package/dist/cjs/data-structures/stack/index.js +0 -18
  136. package/dist/cjs/data-structures/stack/index.js.map +0 -1
  137. package/dist/cjs/data-structures/stack/stack.js +0 -402
  138. package/dist/cjs/data-structures/stack/stack.js.map +0 -1
  139. package/dist/cjs/data-structures/tree/index.js +0 -18
  140. package/dist/cjs/data-structures/tree/index.js.map +0 -1
  141. package/dist/cjs/data-structures/tree/tree.js +0 -108
  142. package/dist/cjs/data-structures/tree/tree.js.map +0 -1
  143. package/dist/cjs/data-structures/trie/index.js +0 -18
  144. package/dist/cjs/data-structures/trie/index.js.map +0 -1
  145. package/dist/cjs/data-structures/trie/trie.js +0 -611
  146. package/dist/cjs/data-structures/trie/trie.js.map +0 -1
  147. package/dist/cjs/index.js +0 -22
  148. package/dist/cjs/index.js.map +0 -1
  149. package/dist/cjs/interfaces/binary-tree.d.ts +0 -59
  150. package/dist/cjs/interfaces/binary-tree.js +0 -3
  151. package/dist/cjs/interfaces/binary-tree.js.map +0 -1
  152. package/dist/cjs/interfaces/doubly-linked-list.js +0 -3
  153. package/dist/cjs/interfaces/doubly-linked-list.js.map +0 -1
  154. package/dist/cjs/interfaces/graph.js +0 -3
  155. package/dist/cjs/interfaces/graph.js.map +0 -1
  156. package/dist/cjs/interfaces/heap.js +0 -3
  157. package/dist/cjs/interfaces/heap.js.map +0 -1
  158. package/dist/cjs/interfaces/index.js +0 -25
  159. package/dist/cjs/interfaces/index.js.map +0 -1
  160. package/dist/cjs/interfaces/navigator.js +0 -3
  161. package/dist/cjs/interfaces/navigator.js.map +0 -1
  162. package/dist/cjs/interfaces/priority-queue.js +0 -3
  163. package/dist/cjs/interfaces/priority-queue.js.map +0 -1
  164. package/dist/cjs/interfaces/segment-tree.js +0 -3
  165. package/dist/cjs/interfaces/segment-tree.js.map +0 -1
  166. package/dist/cjs/interfaces/singly-linked-list.js +0 -3
  167. package/dist/cjs/interfaces/singly-linked-list.js.map +0 -1
  168. package/dist/cjs/types/common.js +0 -3
  169. package/dist/cjs/types/common.js.map +0 -1
  170. package/dist/cjs/types/data-structures/base/base.js +0 -3
  171. package/dist/cjs/types/data-structures/base/base.js.map +0 -1
  172. package/dist/cjs/types/data-structures/base/index.js +0 -18
  173. package/dist/cjs/types/data-structures/base/index.js.map +0 -1
  174. package/dist/cjs/types/data-structures/binary-tree/avl-tree-counter.js +0 -3
  175. package/dist/cjs/types/data-structures/binary-tree/avl-tree-counter.js.map +0 -1
  176. package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -3
  177. package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.js.map +0 -1
  178. package/dist/cjs/types/data-structures/binary-tree/avl-tree.js +0 -3
  179. package/dist/cjs/types/data-structures/binary-tree/avl-tree.js.map +0 -1
  180. package/dist/cjs/types/data-structures/binary-tree/binary-indexed-tree.js +0 -3
  181. package/dist/cjs/types/data-structures/binary-tree/binary-indexed-tree.js.map +0 -1
  182. package/dist/cjs/types/data-structures/binary-tree/binary-tree.js +0 -3
  183. package/dist/cjs/types/data-structures/binary-tree/binary-tree.js.map +0 -1
  184. package/dist/cjs/types/data-structures/binary-tree/bst.js +0 -3
  185. package/dist/cjs/types/data-structures/binary-tree/bst.js.map +0 -1
  186. package/dist/cjs/types/data-structures/binary-tree/index.js +0 -26
  187. package/dist/cjs/types/data-structures/binary-tree/index.js.map +0 -1
  188. package/dist/cjs/types/data-structures/binary-tree/red-black-tree.js +0 -3
  189. package/dist/cjs/types/data-structures/binary-tree/red-black-tree.js.map +0 -1
  190. package/dist/cjs/types/data-structures/binary-tree/segment-tree.js +0 -3
  191. package/dist/cjs/types/data-structures/binary-tree/segment-tree.js.map +0 -1
  192. package/dist/cjs/types/data-structures/binary-tree/tree-counter.js +0 -3
  193. package/dist/cjs/types/data-structures/binary-tree/tree-counter.js.map +0 -1
  194. package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.js +0 -3
  195. package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.js.map +0 -1
  196. package/dist/cjs/types/data-structures/graph/abstract-graph.js +0 -3
  197. package/dist/cjs/types/data-structures/graph/abstract-graph.js.map +0 -1
  198. package/dist/cjs/types/data-structures/graph/directed-graph.js +0 -3
  199. package/dist/cjs/types/data-structures/graph/directed-graph.js.map +0 -1
  200. package/dist/cjs/types/data-structures/graph/index.js +0 -20
  201. package/dist/cjs/types/data-structures/graph/index.js.map +0 -1
  202. package/dist/cjs/types/data-structures/graph/map-graph.js +0 -3
  203. package/dist/cjs/types/data-structures/graph/map-graph.js.map +0 -1
  204. package/dist/cjs/types/data-structures/graph/undirected-graph.js +0 -3
  205. package/dist/cjs/types/data-structures/graph/undirected-graph.js.map +0 -1
  206. package/dist/cjs/types/data-structures/hash/hash-map.js +0 -3
  207. package/dist/cjs/types/data-structures/hash/hash-map.js.map +0 -1
  208. package/dist/cjs/types/data-structures/hash/index.js +0 -18
  209. package/dist/cjs/types/data-structures/hash/index.js.map +0 -1
  210. package/dist/cjs/types/data-structures/heap/heap.js +0 -3
  211. package/dist/cjs/types/data-structures/heap/heap.js.map +0 -1
  212. package/dist/cjs/types/data-structures/heap/index.js +0 -18
  213. package/dist/cjs/types/data-structures/heap/index.js.map +0 -1
  214. package/dist/cjs/types/data-structures/heap/max-heap.js +0 -3
  215. package/dist/cjs/types/data-structures/heap/max-heap.js.map +0 -1
  216. package/dist/cjs/types/data-structures/heap/min-heap.js +0 -3
  217. package/dist/cjs/types/data-structures/heap/min-heap.js.map +0 -1
  218. package/dist/cjs/types/data-structures/index.js +0 -29
  219. package/dist/cjs/types/data-structures/index.js.map +0 -1
  220. package/dist/cjs/types/data-structures/linked-list/doubly-linked-list.js +0 -3
  221. package/dist/cjs/types/data-structures/linked-list/doubly-linked-list.js.map +0 -1
  222. package/dist/cjs/types/data-structures/linked-list/index.js +0 -20
  223. package/dist/cjs/types/data-structures/linked-list/index.js.map +0 -1
  224. package/dist/cjs/types/data-structures/linked-list/singly-linked-list.js +0 -3
  225. package/dist/cjs/types/data-structures/linked-list/singly-linked-list.js.map +0 -1
  226. package/dist/cjs/types/data-structures/linked-list/skip-linked-list.js +0 -3
  227. package/dist/cjs/types/data-structures/linked-list/skip-linked-list.js.map +0 -1
  228. package/dist/cjs/types/data-structures/matrix/index.js +0 -19
  229. package/dist/cjs/types/data-structures/matrix/index.js.map +0 -1
  230. package/dist/cjs/types/data-structures/matrix/matrix.js +0 -3
  231. package/dist/cjs/types/data-structures/matrix/matrix.js.map +0 -1
  232. package/dist/cjs/types/data-structures/matrix/navigator.js +0 -3
  233. package/dist/cjs/types/data-structures/matrix/navigator.js.map +0 -1
  234. package/dist/cjs/types/data-structures/priority-queue/index.js +0 -20
  235. package/dist/cjs/types/data-structures/priority-queue/index.js.map +0 -1
  236. package/dist/cjs/types/data-structures/priority-queue/max-priority-queue.js +0 -3
  237. package/dist/cjs/types/data-structures/priority-queue/max-priority-queue.js.map +0 -1
  238. package/dist/cjs/types/data-structures/priority-queue/min-priority-queue.js +0 -3
  239. package/dist/cjs/types/data-structures/priority-queue/min-priority-queue.js.map +0 -1
  240. package/dist/cjs/types/data-structures/priority-queue/priority-queue.js +0 -3
  241. package/dist/cjs/types/data-structures/priority-queue/priority-queue.js.map +0 -1
  242. package/dist/cjs/types/data-structures/queue/deque.js +0 -3
  243. package/dist/cjs/types/data-structures/queue/deque.js.map +0 -1
  244. package/dist/cjs/types/data-structures/queue/index.js +0 -19
  245. package/dist/cjs/types/data-structures/queue/index.js.map +0 -1
  246. package/dist/cjs/types/data-structures/queue/queue.js +0 -3
  247. package/dist/cjs/types/data-structures/queue/queue.js.map +0 -1
  248. package/dist/cjs/types/data-structures/stack/index.js +0 -18
  249. package/dist/cjs/types/data-structures/stack/index.js.map +0 -1
  250. package/dist/cjs/types/data-structures/stack/stack.js +0 -3
  251. package/dist/cjs/types/data-structures/stack/stack.js.map +0 -1
  252. package/dist/cjs/types/data-structures/tree/index.js +0 -18
  253. package/dist/cjs/types/data-structures/tree/index.js.map +0 -1
  254. package/dist/cjs/types/data-structures/tree/tree.js +0 -3
  255. package/dist/cjs/types/data-structures/tree/tree.js.map +0 -1
  256. package/dist/cjs/types/data-structures/trie/index.js +0 -18
  257. package/dist/cjs/types/data-structures/trie/index.js.map +0 -1
  258. package/dist/cjs/types/data-structures/trie/trie.js +0 -3
  259. package/dist/cjs/types/data-structures/trie/trie.js.map +0 -1
  260. package/dist/cjs/types/index.js +0 -20
  261. package/dist/cjs/types/index.js.map +0 -1
  262. package/dist/cjs/types/utils/index.js +0 -19
  263. package/dist/cjs/types/utils/index.js.map +0 -1
  264. package/dist/cjs/types/utils/utils.js +0 -3
  265. package/dist/cjs/types/utils/utils.js.map +0 -1
  266. package/dist/cjs/types/utils/validate-type.js +0 -3
  267. package/dist/cjs/types/utils/validate-type.js.map +0 -1
  268. package/dist/cjs/utils/index.js +0 -19
  269. package/dist/cjs/utils/index.js.map +0 -1
  270. package/dist/cjs/utils/number.js +0 -24
  271. package/dist/cjs/utils/number.js.map +0 -1
  272. package/dist/cjs/utils/utils.js +0 -354
  273. package/dist/cjs/utils/utils.js.map +0 -1
  274. package/dist/esm/common/index.d.ts +0 -12
  275. package/dist/esm/common/index.js +0 -29
  276. package/dist/esm/common/index.js.map +0 -1
  277. package/dist/esm/data-structures/base/index.d.ts +0 -2
  278. package/dist/esm/data-structures/base/index.js +0 -3
  279. package/dist/esm/data-structures/base/index.js.map +0 -1
  280. package/dist/esm/data-structures/base/iterable-element-base.d.ts +0 -219
  281. package/dist/esm/data-structures/base/iterable-element-base.js +0 -247
  282. package/dist/esm/data-structures/base/iterable-element-base.js.map +0 -1
  283. package/dist/esm/data-structures/base/iterable-entry-base.d.ts +0 -144
  284. package/dist/esm/data-structures/base/iterable-entry-base.js +0 -180
  285. package/dist/esm/data-structures/base/iterable-entry-base.js.map +0 -1
  286. package/dist/esm/data-structures/base/linear-base.d.ts +0 -335
  287. package/dist/esm/data-structures/base/linear-base.js +0 -412
  288. package/dist/esm/data-structures/base/linear-base.js.map +0 -1
  289. package/dist/esm/data-structures/binary-tree/avl-tree-counter.d.ts +0 -182
  290. package/dist/esm/data-structures/binary-tree/avl-tree-counter.js +0 -369
  291. package/dist/esm/data-structures/binary-tree/avl-tree-counter.js.map +0 -1
  292. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js +0 -244
  293. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js.map +0 -1
  294. package/dist/esm/data-structures/binary-tree/avl-tree.js +0 -607
  295. package/dist/esm/data-structures/binary-tree/avl-tree.js.map +0 -1
  296. package/dist/esm/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -174
  297. package/dist/esm/data-structures/binary-tree/binary-indexed-tree.js +0 -296
  298. package/dist/esm/data-structures/binary-tree/binary-indexed-tree.js.map +0 -1
  299. package/dist/esm/data-structures/binary-tree/binary-tree.js +0 -1922
  300. package/dist/esm/data-structures/binary-tree/binary-tree.js.map +0 -1
  301. package/dist/esm/data-structures/binary-tree/bst.js +0 -901
  302. package/dist/esm/data-structures/binary-tree/bst.js.map +0 -1
  303. package/dist/esm/data-structures/binary-tree/index.d.ts +0 -10
  304. package/dist/esm/data-structures/binary-tree/index.js +0 -11
  305. package/dist/esm/data-structures/binary-tree/index.js.map +0 -1
  306. package/dist/esm/data-structures/binary-tree/red-black-tree.js +0 -540
  307. package/dist/esm/data-structures/binary-tree/red-black-tree.js.map +0 -1
  308. package/dist/esm/data-structures/binary-tree/segment-tree.d.ts +0 -160
  309. package/dist/esm/data-structures/binary-tree/segment-tree.js +0 -295
  310. package/dist/esm/data-structures/binary-tree/segment-tree.js.map +0 -1
  311. package/dist/esm/data-structures/binary-tree/tree-counter.d.ts +0 -190
  312. package/dist/esm/data-structures/binary-tree/tree-counter.js +0 -412
  313. package/dist/esm/data-structures/binary-tree/tree-counter.js.map +0 -1
  314. package/dist/esm/data-structures/binary-tree/tree-multi-map.d.ts +0 -270
  315. package/dist/esm/data-structures/binary-tree/tree-multi-map.js +0 -378
  316. package/dist/esm/data-structures/binary-tree/tree-multi-map.js.map +0 -1
  317. package/dist/esm/data-structures/graph/abstract-graph.d.ts +0 -340
  318. package/dist/esm/data-structures/graph/abstract-graph.js +0 -892
  319. package/dist/esm/data-structures/graph/abstract-graph.js.map +0 -1
  320. package/dist/esm/data-structures/graph/directed-graph.d.ts +0 -207
  321. package/dist/esm/data-structures/graph/directed-graph.js +0 -521
  322. package/dist/esm/data-structures/graph/directed-graph.js.map +0 -1
  323. package/dist/esm/data-structures/graph/index.d.ts +0 -4
  324. package/dist/esm/data-structures/graph/index.js +0 -5
  325. package/dist/esm/data-structures/graph/index.js.map +0 -1
  326. package/dist/esm/data-structures/graph/map-graph.d.ts +0 -78
  327. package/dist/esm/data-structures/graph/map-graph.js +0 -105
  328. package/dist/esm/data-structures/graph/map-graph.js.map +0 -1
  329. package/dist/esm/data-structures/graph/undirected-graph.d.ts +0 -188
  330. package/dist/esm/data-structures/graph/undirected-graph.js +0 -418
  331. package/dist/esm/data-structures/graph/undirected-graph.js.map +0 -1
  332. package/dist/esm/data-structures/hash/hash-map.d.ts +0 -345
  333. package/dist/esm/data-structures/hash/hash-map.js +0 -691
  334. package/dist/esm/data-structures/hash/hash-map.js.map +0 -1
  335. package/dist/esm/data-structures/hash/index.d.ts +0 -1
  336. package/dist/esm/data-structures/hash/index.js +0 -2
  337. package/dist/esm/data-structures/hash/index.js.map +0 -1
  338. package/dist/esm/data-structures/heap/heap.d.ts +0 -503
  339. package/dist/esm/data-structures/heap/heap.js +0 -894
  340. package/dist/esm/data-structures/heap/heap.js.map +0 -1
  341. package/dist/esm/data-structures/heap/index.d.ts +0 -3
  342. package/dist/esm/data-structures/heap/index.js +0 -4
  343. package/dist/esm/data-structures/heap/index.js.map +0 -1
  344. package/dist/esm/data-structures/heap/max-heap.d.ts +0 -32
  345. package/dist/esm/data-structures/heap/max-heap.js +0 -40
  346. package/dist/esm/data-structures/heap/max-heap.js.map +0 -1
  347. package/dist/esm/data-structures/heap/min-heap.d.ts +0 -33
  348. package/dist/esm/data-structures/heap/min-heap.js +0 -28
  349. package/dist/esm/data-structures/heap/min-heap.js.map +0 -1
  350. package/dist/esm/data-structures/index.d.ts +0 -12
  351. package/dist/esm/data-structures/index.js +0 -13
  352. package/dist/esm/data-structures/index.js.map +0 -1
  353. package/dist/esm/data-structures/linked-list/doubly-linked-list.d.ts +0 -769
  354. package/dist/esm/data-structures/linked-list/doubly-linked-list.js +0 -1109
  355. package/dist/esm/data-structures/linked-list/doubly-linked-list.js.map +0 -1
  356. package/dist/esm/data-structures/linked-list/index.d.ts +0 -3
  357. package/dist/esm/data-structures/linked-list/index.js +0 -4
  358. package/dist/esm/data-structures/linked-list/index.js.map +0 -1
  359. package/dist/esm/data-structures/linked-list/singly-linked-list.d.ts +0 -451
  360. package/dist/esm/data-structures/linked-list/singly-linked-list.js +0 -847
  361. package/dist/esm/data-structures/linked-list/singly-linked-list.js.map +0 -1
  362. package/dist/esm/data-structures/linked-list/skip-linked-list.d.ts +0 -27
  363. package/dist/esm/data-structures/linked-list/skip-linked-list.js +0 -143
  364. package/dist/esm/data-structures/linked-list/skip-linked-list.js.map +0 -1
  365. package/dist/esm/data-structures/matrix/index.d.ts +0 -2
  366. package/dist/esm/data-structures/matrix/index.js +0 -3
  367. package/dist/esm/data-structures/matrix/index.js.map +0 -1
  368. package/dist/esm/data-structures/matrix/matrix.d.ts +0 -168
  369. package/dist/esm/data-structures/matrix/matrix.js +0 -444
  370. package/dist/esm/data-structures/matrix/matrix.js.map +0 -1
  371. package/dist/esm/data-structures/matrix/navigator.d.ts +0 -55
  372. package/dist/esm/data-structures/matrix/navigator.js +0 -114
  373. package/dist/esm/data-structures/matrix/navigator.js.map +0 -1
  374. package/dist/esm/data-structures/priority-queue/index.d.ts +0 -3
  375. package/dist/esm/data-structures/priority-queue/index.js +0 -4
  376. package/dist/esm/data-structures/priority-queue/index.js.map +0 -1
  377. package/dist/esm/data-structures/priority-queue/max-priority-queue.d.ts +0 -27
  378. package/dist/esm/data-structures/priority-queue/max-priority-queue.js +0 -34
  379. package/dist/esm/data-structures/priority-queue/max-priority-queue.js.map +0 -1
  380. package/dist/esm/data-structures/priority-queue/min-priority-queue.d.ts +0 -26
  381. package/dist/esm/data-structures/priority-queue/min-priority-queue.js +0 -21
  382. package/dist/esm/data-structures/priority-queue/min-priority-queue.js.map +0 -1
  383. package/dist/esm/data-structures/priority-queue/priority-queue.d.ts +0 -15
  384. package/dist/esm/data-structures/priority-queue/priority-queue.js +0 -17
  385. package/dist/esm/data-structures/priority-queue/priority-queue.js.map +0 -1
  386. package/dist/esm/data-structures/queue/deque.d.ts +0 -431
  387. package/dist/esm/data-structures/queue/deque.js +0 -880
  388. package/dist/esm/data-structures/queue/deque.js.map +0 -1
  389. package/dist/esm/data-structures/queue/index.d.ts +0 -2
  390. package/dist/esm/data-structures/queue/index.js +0 -3
  391. package/dist/esm/data-structures/queue/index.js.map +0 -1
  392. package/dist/esm/data-structures/queue/queue.d.ts +0 -308
  393. package/dist/esm/data-structures/queue/queue.js +0 -467
  394. package/dist/esm/data-structures/queue/queue.js.map +0 -1
  395. package/dist/esm/data-structures/stack/index.d.ts +0 -1
  396. package/dist/esm/data-structures/stack/index.js +0 -2
  397. package/dist/esm/data-structures/stack/index.js.map +0 -1
  398. package/dist/esm/data-structures/stack/stack.d.ts +0 -306
  399. package/dist/esm/data-structures/stack/stack.js +0 -398
  400. package/dist/esm/data-structures/stack/stack.js.map +0 -1
  401. package/dist/esm/data-structures/tree/index.d.ts +0 -1
  402. package/dist/esm/data-structures/tree/index.js +0 -2
  403. package/dist/esm/data-structures/tree/index.js.map +0 -1
  404. package/dist/esm/data-structures/tree/tree.d.ts +0 -62
  405. package/dist/esm/data-structures/tree/tree.js +0 -107
  406. package/dist/esm/data-structures/tree/tree.js.map +0 -1
  407. package/dist/esm/data-structures/trie/index.d.ts +0 -1
  408. package/dist/esm/data-structures/trie/index.js +0 -2
  409. package/dist/esm/data-structures/trie/index.js.map +0 -1
  410. package/dist/esm/data-structures/trie/trie.d.ts +0 -350
  411. package/dist/esm/data-structures/trie/trie.js +0 -613
  412. package/dist/esm/data-structures/trie/trie.js.map +0 -1
  413. package/dist/esm/index.d.ts +0 -5
  414. package/dist/esm/index.js +0 -6
  415. package/dist/esm/index.js.map +0 -1
  416. package/dist/esm/interfaces/binary-tree.js +0 -2
  417. package/dist/esm/interfaces/binary-tree.js.map +0 -1
  418. package/dist/esm/interfaces/doubly-linked-list.d.ts +0 -1
  419. package/dist/esm/interfaces/doubly-linked-list.js +0 -2
  420. package/dist/esm/interfaces/doubly-linked-list.js.map +0 -1
  421. package/dist/esm/interfaces/graph.d.ts +0 -21
  422. package/dist/esm/interfaces/graph.js +0 -2
  423. package/dist/esm/interfaces/graph.js.map +0 -1
  424. package/dist/esm/interfaces/heap.d.ts +0 -1
  425. package/dist/esm/interfaces/heap.js +0 -2
  426. package/dist/esm/interfaces/heap.js.map +0 -1
  427. package/dist/esm/interfaces/index.d.ts +0 -8
  428. package/dist/esm/interfaces/index.js +0 -9
  429. package/dist/esm/interfaces/index.js.map +0 -1
  430. package/dist/esm/interfaces/navigator.d.ts +0 -1
  431. package/dist/esm/interfaces/navigator.js +0 -2
  432. package/dist/esm/interfaces/navigator.js.map +0 -1
  433. package/dist/esm/interfaces/priority-queue.d.ts +0 -1
  434. package/dist/esm/interfaces/priority-queue.js +0 -2
  435. package/dist/esm/interfaces/priority-queue.js.map +0 -1
  436. package/dist/esm/interfaces/segment-tree.d.ts +0 -1
  437. package/dist/esm/interfaces/segment-tree.js +0 -2
  438. package/dist/esm/interfaces/segment-tree.js.map +0 -1
  439. package/dist/esm/interfaces/singly-linked-list.d.ts +0 -1
  440. package/dist/esm/interfaces/singly-linked-list.js +0 -2
  441. package/dist/esm/interfaces/singly-linked-list.js.map +0 -1
  442. package/dist/esm/types/common.d.ts +0 -15
  443. package/dist/esm/types/common.js +0 -2
  444. package/dist/esm/types/common.js.map +0 -1
  445. package/dist/esm/types/data-structures/base/base.d.ts +0 -13
  446. package/dist/esm/types/data-structures/base/base.js +0 -2
  447. package/dist/esm/types/data-structures/base/base.js.map +0 -1
  448. package/dist/esm/types/data-structures/base/index.d.ts +0 -1
  449. package/dist/esm/types/data-structures/base/index.js +0 -2
  450. package/dist/esm/types/data-structures/base/index.js.map +0 -1
  451. package/dist/esm/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -2
  452. package/dist/esm/types/data-structures/binary-tree/avl-tree-counter.js +0 -2
  453. package/dist/esm/types/data-structures/binary-tree/avl-tree-counter.js.map +0 -1
  454. package/dist/esm/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -2
  455. package/dist/esm/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -2
  456. package/dist/esm/types/data-structures/binary-tree/avl-tree-multi-map.js.map +0 -1
  457. package/dist/esm/types/data-structures/binary-tree/avl-tree.d.ts +0 -2
  458. package/dist/esm/types/data-structures/binary-tree/avl-tree.js +0 -2
  459. package/dist/esm/types/data-structures/binary-tree/avl-tree.js.map +0 -1
  460. package/dist/esm/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -1
  461. package/dist/esm/types/data-structures/binary-tree/binary-indexed-tree.js +0 -2
  462. package/dist/esm/types/data-structures/binary-tree/binary-indexed-tree.js.map +0 -1
  463. package/dist/esm/types/data-structures/binary-tree/binary-tree.d.ts +0 -29
  464. package/dist/esm/types/data-structures/binary-tree/binary-tree.js +0 -2
  465. package/dist/esm/types/data-structures/binary-tree/binary-tree.js.map +0 -1
  466. package/dist/esm/types/data-structures/binary-tree/bst.d.ts +0 -12
  467. package/dist/esm/types/data-structures/binary-tree/bst.js +0 -2
  468. package/dist/esm/types/data-structures/binary-tree/bst.js.map +0 -1
  469. package/dist/esm/types/data-structures/binary-tree/index.d.ts +0 -9
  470. package/dist/esm/types/data-structures/binary-tree/index.js +0 -10
  471. package/dist/esm/types/data-structures/binary-tree/index.js.map +0 -1
  472. package/dist/esm/types/data-structures/binary-tree/red-black-tree.d.ts +0 -3
  473. package/dist/esm/types/data-structures/binary-tree/red-black-tree.js +0 -2
  474. package/dist/esm/types/data-structures/binary-tree/red-black-tree.js.map +0 -1
  475. package/dist/esm/types/data-structures/binary-tree/segment-tree.d.ts +0 -1
  476. package/dist/esm/types/data-structures/binary-tree/segment-tree.js +0 -2
  477. package/dist/esm/types/data-structures/binary-tree/segment-tree.js.map +0 -1
  478. package/dist/esm/types/data-structures/binary-tree/tree-counter.d.ts +0 -2
  479. package/dist/esm/types/data-structures/binary-tree/tree-counter.js +0 -2
  480. package/dist/esm/types/data-structures/binary-tree/tree-counter.js.map +0 -1
  481. package/dist/esm/types/data-structures/binary-tree/tree-multi-map.d.ts +0 -2
  482. package/dist/esm/types/data-structures/binary-tree/tree-multi-map.js +0 -2
  483. package/dist/esm/types/data-structures/binary-tree/tree-multi-map.js.map +0 -1
  484. package/dist/esm/types/data-structures/graph/abstract-graph.d.ts +0 -14
  485. package/dist/esm/types/data-structures/graph/abstract-graph.js +0 -2
  486. package/dist/esm/types/data-structures/graph/abstract-graph.js.map +0 -1
  487. package/dist/esm/types/data-structures/graph/directed-graph.d.ts +0 -1
  488. package/dist/esm/types/data-structures/graph/directed-graph.js +0 -2
  489. package/dist/esm/types/data-structures/graph/directed-graph.js.map +0 -1
  490. package/dist/esm/types/data-structures/graph/index.d.ts +0 -3
  491. package/dist/esm/types/data-structures/graph/index.js +0 -4
  492. package/dist/esm/types/data-structures/graph/index.js.map +0 -1
  493. package/dist/esm/types/data-structures/graph/map-graph.d.ts +0 -1
  494. package/dist/esm/types/data-structures/graph/map-graph.js +0 -2
  495. package/dist/esm/types/data-structures/graph/map-graph.js.map +0 -1
  496. package/dist/esm/types/data-structures/graph/undirected-graph.d.ts +0 -1
  497. package/dist/esm/types/data-structures/graph/undirected-graph.js +0 -2
  498. package/dist/esm/types/data-structures/graph/undirected-graph.js.map +0 -1
  499. package/dist/esm/types/data-structures/hash/hash-map.d.ts +0 -19
  500. package/dist/esm/types/data-structures/hash/hash-map.js +0 -2
  501. package/dist/esm/types/data-structures/hash/hash-map.js.map +0 -1
  502. package/dist/esm/types/data-structures/hash/index.d.ts +0 -2
  503. package/dist/esm/types/data-structures/hash/index.js +0 -2
  504. package/dist/esm/types/data-structures/hash/index.js.map +0 -1
  505. package/dist/esm/types/data-structures/heap/heap.d.ts +0 -5
  506. package/dist/esm/types/data-structures/heap/heap.js +0 -2
  507. package/dist/esm/types/data-structures/heap/heap.js.map +0 -1
  508. package/dist/esm/types/data-structures/heap/index.d.ts +0 -1
  509. package/dist/esm/types/data-structures/heap/index.js +0 -2
  510. package/dist/esm/types/data-structures/heap/index.js.map +0 -1
  511. package/dist/esm/types/data-structures/heap/max-heap.d.ts +0 -1
  512. package/dist/esm/types/data-structures/heap/max-heap.js +0 -2
  513. package/dist/esm/types/data-structures/heap/max-heap.js.map +0 -1
  514. package/dist/esm/types/data-structures/heap/min-heap.d.ts +0 -1
  515. package/dist/esm/types/data-structures/heap/min-heap.js +0 -2
  516. package/dist/esm/types/data-structures/heap/min-heap.js.map +0 -1
  517. package/dist/esm/types/data-structures/index.d.ts +0 -12
  518. package/dist/esm/types/data-structures/index.js +0 -13
  519. package/dist/esm/types/data-structures/index.js.map +0 -1
  520. package/dist/esm/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -2
  521. package/dist/esm/types/data-structures/linked-list/doubly-linked-list.js +0 -2
  522. package/dist/esm/types/data-structures/linked-list/doubly-linked-list.js.map +0 -1
  523. package/dist/esm/types/data-structures/linked-list/index.d.ts +0 -3
  524. package/dist/esm/types/data-structures/linked-list/index.js +0 -4
  525. package/dist/esm/types/data-structures/linked-list/index.js.map +0 -1
  526. package/dist/esm/types/data-structures/linked-list/singly-linked-list.d.ts +0 -2
  527. package/dist/esm/types/data-structures/linked-list/singly-linked-list.js +0 -2
  528. package/dist/esm/types/data-structures/linked-list/singly-linked-list.js.map +0 -1
  529. package/dist/esm/types/data-structures/linked-list/skip-linked-list.d.ts +0 -4
  530. package/dist/esm/types/data-structures/linked-list/skip-linked-list.js +0 -2
  531. package/dist/esm/types/data-structures/linked-list/skip-linked-list.js.map +0 -1
  532. package/dist/esm/types/data-structures/matrix/index.d.ts +0 -2
  533. package/dist/esm/types/data-structures/matrix/index.js +0 -3
  534. package/dist/esm/types/data-structures/matrix/index.js.map +0 -1
  535. package/dist/esm/types/data-structures/matrix/matrix.d.ts +0 -7
  536. package/dist/esm/types/data-structures/matrix/matrix.js +0 -2
  537. package/dist/esm/types/data-structures/matrix/matrix.js.map +0 -1
  538. package/dist/esm/types/data-structures/matrix/navigator.d.ts +0 -14
  539. package/dist/esm/types/data-structures/matrix/navigator.js +0 -2
  540. package/dist/esm/types/data-structures/matrix/navigator.js.map +0 -1
  541. package/dist/esm/types/data-structures/priority-queue/index.d.ts +0 -3
  542. package/dist/esm/types/data-structures/priority-queue/index.js +0 -4
  543. package/dist/esm/types/data-structures/priority-queue/index.js.map +0 -1
  544. package/dist/esm/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -1
  545. package/dist/esm/types/data-structures/priority-queue/max-priority-queue.js +0 -2
  546. package/dist/esm/types/data-structures/priority-queue/max-priority-queue.js.map +0 -1
  547. package/dist/esm/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -1
  548. package/dist/esm/types/data-structures/priority-queue/min-priority-queue.js +0 -2
  549. package/dist/esm/types/data-structures/priority-queue/min-priority-queue.js.map +0 -1
  550. package/dist/esm/types/data-structures/priority-queue/priority-queue.d.ts +0 -2
  551. package/dist/esm/types/data-structures/priority-queue/priority-queue.js +0 -2
  552. package/dist/esm/types/data-structures/priority-queue/priority-queue.js.map +0 -1
  553. package/dist/esm/types/data-structures/queue/deque.d.ts +0 -4
  554. package/dist/esm/types/data-structures/queue/deque.js +0 -2
  555. package/dist/esm/types/data-structures/queue/deque.js.map +0 -1
  556. package/dist/esm/types/data-structures/queue/index.d.ts +0 -2
  557. package/dist/esm/types/data-structures/queue/index.js +0 -3
  558. package/dist/esm/types/data-structures/queue/index.js.map +0 -1
  559. package/dist/esm/types/data-structures/queue/queue.d.ts +0 -4
  560. package/dist/esm/types/data-structures/queue/queue.js +0 -2
  561. package/dist/esm/types/data-structures/queue/queue.js.map +0 -1
  562. package/dist/esm/types/data-structures/stack/index.d.ts +0 -1
  563. package/dist/esm/types/data-structures/stack/index.js +0 -2
  564. package/dist/esm/types/data-structures/stack/index.js.map +0 -1
  565. package/dist/esm/types/data-structures/stack/stack.d.ts +0 -2
  566. package/dist/esm/types/data-structures/stack/stack.js +0 -2
  567. package/dist/esm/types/data-structures/stack/stack.js.map +0 -1
  568. package/dist/esm/types/data-structures/tree/index.d.ts +0 -1
  569. package/dist/esm/types/data-structures/tree/index.js +0 -2
  570. package/dist/esm/types/data-structures/tree/index.js.map +0 -1
  571. package/dist/esm/types/data-structures/tree/tree.d.ts +0 -1
  572. package/dist/esm/types/data-structures/tree/tree.js +0 -2
  573. package/dist/esm/types/data-structures/tree/tree.js.map +0 -1
  574. package/dist/esm/types/data-structures/trie/index.d.ts +0 -1
  575. package/dist/esm/types/data-structures/trie/index.js +0 -2
  576. package/dist/esm/types/data-structures/trie/index.js.map +0 -1
  577. package/dist/esm/types/data-structures/trie/trie.d.ts +0 -4
  578. package/dist/esm/types/data-structures/trie/trie.js +0 -2
  579. package/dist/esm/types/data-structures/trie/trie.js.map +0 -1
  580. package/dist/esm/types/index.d.ts +0 -3
  581. package/dist/esm/types/index.js +0 -4
  582. package/dist/esm/types/index.js.map +0 -1
  583. package/dist/esm/types/utils/index.d.ts +0 -2
  584. package/dist/esm/types/utils/index.js +0 -3
  585. package/dist/esm/types/utils/index.js.map +0 -1
  586. package/dist/esm/types/utils/utils.d.ts +0 -22
  587. package/dist/esm/types/utils/utils.js +0 -2
  588. package/dist/esm/types/utils/utils.js.map +0 -1
  589. package/dist/esm/types/utils/validate-type.d.ts +0 -19
  590. package/dist/esm/types/utils/validate-type.js +0 -2
  591. package/dist/esm/types/utils/validate-type.js.map +0 -1
  592. package/dist/esm/utils/index.d.ts +0 -2
  593. package/dist/esm/utils/index.js +0 -3
  594. package/dist/esm/utils/index.js.map +0 -1
  595. package/dist/esm/utils/number.d.ts +0 -14
  596. package/dist/esm/utils/number.js +0 -21
  597. package/dist/esm/utils/number.js.map +0 -1
  598. package/dist/esm/utils/utils.d.ts +0 -209
  599. package/dist/esm/utils/utils.js +0 -325
  600. package/dist/esm/utils/utils.js.map +0 -1
  601. package/tsconfig-cjs.json +0 -14
  602. package/tsconfig-esm.json +0 -14
  603. /package/dist/{cjs → types}/common/index.d.ts +0 -0
  604. /package/dist/{cjs → types}/data-structures/base/index.d.ts +0 -0
  605. /package/dist/{cjs → types}/data-structures/base/iterable-element-base.d.ts +0 -0
  606. /package/dist/{cjs → types}/data-structures/base/iterable-entry-base.d.ts +0 -0
  607. /package/dist/{cjs → types}/data-structures/base/linear-base.d.ts +0 -0
  608. /package/dist/{cjs → types}/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -0
  609. /package/dist/{cjs → types}/data-structures/binary-tree/index.d.ts +0 -0
  610. /package/dist/{cjs → types}/data-structures/binary-tree/segment-tree.d.ts +0 -0
  611. /package/dist/{cjs → types}/data-structures/graph/abstract-graph.d.ts +0 -0
  612. /package/dist/{cjs → types}/data-structures/graph/directed-graph.d.ts +0 -0
  613. /package/dist/{cjs → types}/data-structures/graph/index.d.ts +0 -0
  614. /package/dist/{cjs → types}/data-structures/graph/map-graph.d.ts +0 -0
  615. /package/dist/{cjs → types}/data-structures/graph/undirected-graph.d.ts +0 -0
  616. /package/dist/{cjs → types}/data-structures/hash/hash-map.d.ts +0 -0
  617. /package/dist/{cjs → types}/data-structures/hash/index.d.ts +0 -0
  618. /package/dist/{cjs → types}/data-structures/heap/heap.d.ts +0 -0
  619. /package/dist/{cjs → types}/data-structures/heap/index.d.ts +0 -0
  620. /package/dist/{cjs → types}/data-structures/heap/max-heap.d.ts +0 -0
  621. /package/dist/{cjs → types}/data-structures/heap/min-heap.d.ts +0 -0
  622. /package/dist/{cjs → types}/data-structures/index.d.ts +0 -0
  623. /package/dist/{cjs → types}/data-structures/linked-list/doubly-linked-list.d.ts +0 -0
  624. /package/dist/{cjs → types}/data-structures/linked-list/index.d.ts +0 -0
  625. /package/dist/{cjs → types}/data-structures/linked-list/singly-linked-list.d.ts +0 -0
  626. /package/dist/{cjs → types}/data-structures/linked-list/skip-linked-list.d.ts +0 -0
  627. /package/dist/{cjs → types}/data-structures/matrix/index.d.ts +0 -0
  628. /package/dist/{cjs → types}/data-structures/matrix/matrix.d.ts +0 -0
  629. /package/dist/{cjs → types}/data-structures/matrix/navigator.d.ts +0 -0
  630. /package/dist/{cjs → types}/data-structures/priority-queue/index.d.ts +0 -0
  631. /package/dist/{cjs → types}/data-structures/priority-queue/max-priority-queue.d.ts +0 -0
  632. /package/dist/{cjs → types}/data-structures/priority-queue/min-priority-queue.d.ts +0 -0
  633. /package/dist/{cjs → types}/data-structures/priority-queue/priority-queue.d.ts +0 -0
  634. /package/dist/{cjs → types}/data-structures/queue/deque.d.ts +0 -0
  635. /package/dist/{cjs → types}/data-structures/queue/index.d.ts +0 -0
  636. /package/dist/{cjs → types}/data-structures/queue/queue.d.ts +0 -0
  637. /package/dist/{cjs → types}/data-structures/stack/index.d.ts +0 -0
  638. /package/dist/{cjs → types}/data-structures/stack/stack.d.ts +0 -0
  639. /package/dist/{cjs → types}/data-structures/tree/index.d.ts +0 -0
  640. /package/dist/{cjs → types}/data-structures/tree/tree.d.ts +0 -0
  641. /package/dist/{cjs → types}/data-structures/trie/index.d.ts +0 -0
  642. /package/dist/{cjs → types}/data-structures/trie/trie.d.ts +0 -0
  643. /package/dist/{cjs → types}/index.d.ts +0 -0
  644. /package/dist/{cjs → types}/interfaces/doubly-linked-list.d.ts +0 -0
  645. /package/dist/{cjs → types}/interfaces/graph.d.ts +0 -0
  646. /package/dist/{cjs → types}/interfaces/heap.d.ts +0 -0
  647. /package/dist/{cjs → types}/interfaces/index.d.ts +0 -0
  648. /package/dist/{cjs → types}/interfaces/navigator.d.ts +0 -0
  649. /package/dist/{cjs → types}/interfaces/priority-queue.d.ts +0 -0
  650. /package/dist/{cjs → types}/interfaces/segment-tree.d.ts +0 -0
  651. /package/dist/{cjs → types}/interfaces/singly-linked-list.d.ts +0 -0
  652. /package/dist/{cjs → types}/types/common.d.ts +0 -0
  653. /package/dist/{cjs → types}/types/data-structures/base/base.d.ts +0 -0
  654. /package/dist/{cjs → types}/types/data-structures/base/index.d.ts +0 -0
  655. /package/dist/{cjs → types}/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -0
  656. /package/dist/{cjs → types}/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -0
  657. /package/dist/{cjs → types}/types/data-structures/binary-tree/avl-tree.d.ts +0 -0
  658. /package/dist/{cjs → types}/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -0
  659. /package/dist/{cjs → types}/types/data-structures/binary-tree/binary-tree.d.ts +0 -0
  660. /package/dist/{cjs → types}/types/data-structures/binary-tree/bst.d.ts +0 -0
  661. /package/dist/{cjs → types}/types/data-structures/binary-tree/index.d.ts +0 -0
  662. /package/dist/{cjs → types}/types/data-structures/binary-tree/red-black-tree.d.ts +0 -0
  663. /package/dist/{cjs → types}/types/data-structures/binary-tree/segment-tree.d.ts +0 -0
  664. /package/dist/{cjs → types}/types/data-structures/binary-tree/tree-counter.d.ts +0 -0
  665. /package/dist/{cjs → types}/types/data-structures/binary-tree/tree-multi-map.d.ts +0 -0
  666. /package/dist/{cjs → types}/types/data-structures/graph/abstract-graph.d.ts +0 -0
  667. /package/dist/{cjs → types}/types/data-structures/graph/directed-graph.d.ts +0 -0
  668. /package/dist/{cjs → types}/types/data-structures/graph/index.d.ts +0 -0
  669. /package/dist/{cjs → types}/types/data-structures/graph/map-graph.d.ts +0 -0
  670. /package/dist/{cjs → types}/types/data-structures/graph/undirected-graph.d.ts +0 -0
  671. /package/dist/{cjs → types}/types/data-structures/hash/hash-map.d.ts +0 -0
  672. /package/dist/{cjs → types}/types/data-structures/hash/index.d.ts +0 -0
  673. /package/dist/{cjs → types}/types/data-structures/heap/heap.d.ts +0 -0
  674. /package/dist/{cjs → types}/types/data-structures/heap/index.d.ts +0 -0
  675. /package/dist/{cjs → types}/types/data-structures/heap/max-heap.d.ts +0 -0
  676. /package/dist/{cjs → types}/types/data-structures/heap/min-heap.d.ts +0 -0
  677. /package/dist/{cjs → types}/types/data-structures/index.d.ts +0 -0
  678. /package/dist/{cjs → types}/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -0
  679. /package/dist/{cjs → types}/types/data-structures/linked-list/index.d.ts +0 -0
  680. /package/dist/{cjs → types}/types/data-structures/linked-list/singly-linked-list.d.ts +0 -0
  681. /package/dist/{cjs → types}/types/data-structures/linked-list/skip-linked-list.d.ts +0 -0
  682. /package/dist/{cjs → types}/types/data-structures/matrix/index.d.ts +0 -0
  683. /package/dist/{cjs → types}/types/data-structures/matrix/matrix.d.ts +0 -0
  684. /package/dist/{cjs → types}/types/data-structures/matrix/navigator.d.ts +0 -0
  685. /package/dist/{cjs → types}/types/data-structures/priority-queue/index.d.ts +0 -0
  686. /package/dist/{cjs → types}/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -0
  687. /package/dist/{cjs → types}/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -0
  688. /package/dist/{cjs → types}/types/data-structures/priority-queue/priority-queue.d.ts +0 -0
  689. /package/dist/{cjs → types}/types/data-structures/queue/deque.d.ts +0 -0
  690. /package/dist/{cjs → types}/types/data-structures/queue/index.d.ts +0 -0
  691. /package/dist/{cjs → types}/types/data-structures/queue/queue.d.ts +0 -0
  692. /package/dist/{cjs → types}/types/data-structures/stack/index.d.ts +0 -0
  693. /package/dist/{cjs → types}/types/data-structures/stack/stack.d.ts +0 -0
  694. /package/dist/{cjs → types}/types/data-structures/tree/index.d.ts +0 -0
  695. /package/dist/{cjs → types}/types/data-structures/tree/tree.d.ts +0 -0
  696. /package/dist/{cjs → types}/types/data-structures/trie/index.d.ts +0 -0
  697. /package/dist/{cjs → types}/types/data-structures/trie/trie.d.ts +0 -0
  698. /package/dist/{cjs → types}/types/index.d.ts +0 -0
  699. /package/dist/{cjs → types}/types/utils/index.d.ts +0 -0
  700. /package/dist/{cjs → types}/types/utils/utils.d.ts +0 -0
  701. /package/dist/{cjs → types}/types/utils/validate-type.d.ts +0 -0
  702. /package/dist/{cjs → types}/utils/index.d.ts +0 -0
  703. /package/dist/{cjs → types}/utils/number.d.ts +0 -0
  704. /package/dist/{cjs → types}/utils/utils.d.ts +0 -0
@@ -1,1922 +0,0 @@
1
- /**
2
- * data-structure-typed
3
- *
4
- * @author Pablo Zeng
5
- * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
- * @license MIT License
7
- */
8
- import { isComparable, makeTrampoline, makeTrampolineThunk } from '../../utils';
9
- import { Queue } from '../queue';
10
- import { IterableEntryBase } from '../base';
11
- import { DFSOperation, Range } from '../../common';
12
- /**
13
- * @template K - The type of the key.
14
- * @template V - The type of the value.
15
- */
16
- export class BinaryTreeNode {
17
- key;
18
- value;
19
- parent = undefined;
20
- /**
21
- * Creates an instance of BinaryTreeNode.
22
- * @remarks Time O(1), Space O(1)
23
- *
24
- * @param key - The key of the node.
25
- * @param [value] - The value associated with the key.
26
- */
27
- constructor(key, value) {
28
- this.key = key;
29
- this.value = value;
30
- }
31
- _left = undefined;
32
- /**
33
- * Gets the left child of the node.
34
- * @remarks Time O(1), Space O(1)
35
- *
36
- * @returns The left child.
37
- */
38
- get left() {
39
- return this._left;
40
- }
41
- /**
42
- * Sets the left child of the node and updates its parent reference.
43
- * @remarks Time O(1), Space O(1)
44
- *
45
- * @param v - The node to set as the left child.
46
- */
47
- set left(v) {
48
- if (v) {
49
- v.parent = this;
50
- }
51
- this._left = v;
52
- }
53
- _right = undefined;
54
- /**
55
- * Gets the right child of the node.
56
- * @remarks Time O(1), Space O(1)
57
- *
58
- * @returns The right child.
59
- */
60
- get right() {
61
- return this._right;
62
- }
63
- /**
64
- * Sets the right child of the node and updates its parent reference.
65
- * @remarks Time O(1), Space O(1)
66
- *
67
- * @param v - The node to set as the right child.
68
- */
69
- set right(v) {
70
- if (v) {
71
- v.parent = this;
72
- }
73
- this._right = v;
74
- }
75
- _height = 0;
76
- /**
77
- * Gets the height of the node (used in self-balancing trees).
78
- * @remarks Time O(1), Space O(1)
79
- *
80
- * @returns The height.
81
- */
82
- get height() {
83
- return this._height;
84
- }
85
- /**
86
- * Sets the height of the node.
87
- * @remarks Time O(1), Space O(1)
88
- *
89
- * @param value - The new height.
90
- */
91
- set height(value) {
92
- this._height = value;
93
- }
94
- _color = 'BLACK';
95
- /**
96
- * Gets the color of the node (used in Red-Black trees).
97
- * @remarks Time O(1), Space O(1)
98
- *
99
- * @returns The node's color.
100
- */
101
- get color() {
102
- return this._color;
103
- }
104
- /**
105
- * Sets the color of the node.
106
- * @remarks Time O(1), Space O(1)
107
- *
108
- * @param value - The new color.
109
- */
110
- set color(value) {
111
- this._color = value;
112
- }
113
- _count = 1;
114
- /**
115
- * Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
116
- * @remarks Time O(1), Space O(1)
117
- *
118
- * @returns The subtree node count.
119
- */
120
- get count() {
121
- return this._count;
122
- }
123
- /**
124
- * Sets the count of nodes in the subtree.
125
- * @remarks Time O(1), Space O(1)
126
- *
127
- * @param value - The new count.
128
- */
129
- set count(value) {
130
- this._count = value;
131
- }
132
- /**
133
- * Gets the position of the node relative to its parent.
134
- * @remarks Time O(1), Space O(1)
135
- *
136
- * @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
137
- */
138
- get familyPosition() {
139
- if (!this.parent) {
140
- return this.left || this.right ? 'ROOT' : 'ISOLATED';
141
- }
142
- if (this.parent.left === this) {
143
- return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
144
- }
145
- else if (this.parent.right === this) {
146
- return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
147
- }
148
- return 'MAL_NODE';
149
- }
150
- }
151
- /**
152
- * A general Binary Tree implementation.
153
- *
154
- * @remarks
155
- * This class implements a basic Binary Tree, not a Binary Search Tree.
156
- * The `add` operation inserts nodes level-by-level (BFS) into the first available slot.
157
- *
158
- * @template K - The type of the key.
159
- * @template V - The type of the value.
160
- * @template R - The type of the raw data object (if using `toEntryFn`).
161
- * 1. Two Children Maximum: Each node has at most two children.
162
- * 2. Left and Right Children: Nodes have distinct left and right children.
163
- * 3. Depth and Height: Depth is the number of edges from the root to a node; height is the maximum depth in the tree.
164
- * 4. Subtrees: Each child of a node forms the root of a subtree.
165
- * 5. Leaf Nodes: Nodes without children are leaves.
166
- * @example
167
- * // determine loan approval using a decision tree
168
- * // Decision tree structure
169
- * const loanDecisionTree = new BinaryTree<string>(
170
- * ['stableIncome', 'goodCredit', 'Rejected', 'Approved', 'Rejected'],
171
- * { isDuplicate: true }
172
- * );
173
- *
174
- * function determineLoanApproval(
175
- * node?: BinaryTreeNode<string> | null,
176
- * conditions?: { [key: string]: boolean }
177
- * ): string {
178
- * if (!node) throw new Error('Invalid node');
179
- *
180
- * // If it's a leaf node, return the decision result
181
- * if (!node.left && !node.right) return node.key;
182
- *
183
- * // Check if a valid condition exists for the current node's key
184
- * return conditions?.[node.key]
185
- * ? determineLoanApproval(node.left, conditions)
186
- * : determineLoanApproval(node.right, conditions);
187
- * }
188
- *
189
- * // Test case 1: Stable income and good credit score
190
- * console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: true })); // 'Approved'
191
- *
192
- * // Test case 2: Stable income but poor credit score
193
- * console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: false })); // 'Rejected'
194
- *
195
- * // Test case 3: No stable income
196
- * console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: true })); // 'Rejected'
197
- *
198
- * // Test case 4: No stable income and poor credit score
199
- * console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: false })); // 'Rejected'
200
- * @example
201
- * // evaluate the arithmetic expression represented by the binary tree
202
- * const expressionTree = new BinaryTree<number | string>(['+', 3, '*', null, null, 5, '-', null, null, 2, 8]);
203
- *
204
- * function evaluate(node?: BinaryTreeNode<number | string> | null): number {
205
- * if (!node) return 0;
206
- *
207
- * if (typeof node.key === 'number') return node.key;
208
- *
209
- * const leftValue = evaluate(node.left); // Evaluate the left subtree
210
- * const rightValue = evaluate(node.right); // Evaluate the right subtree
211
- *
212
- * // Perform the operation based on the current node's operator
213
- * switch (node.key) {
214
- * case '+':
215
- * return leftValue + rightValue;
216
- * case '-':
217
- * return leftValue - rightValue;
218
- * case '*':
219
- * return leftValue * rightValue;
220
- * case '/':
221
- * return rightValue !== 0 ? leftValue / rightValue : 0; // Handle division by zero
222
- * default:
223
- * throw new Error(`Unsupported operator: ${node.key}`);
224
- * }
225
- * }
226
- *
227
- * console.log(evaluate(expressionTree.root)); // -27
228
- */
229
- export class BinaryTree extends IterableEntryBase {
230
- iterationType = 'ITERATIVE';
231
- /**
232
- * Creates an instance of BinaryTree.
233
- * @remarks Time O(N * M), where N is the number of items in `keysNodesEntriesOrRaws` and M is the tree size at insertion time (due to O(M) `add` operation). Space O(N) for storing the nodes.
234
- *
235
- * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
236
- * @param [options] - Configuration options for the tree.
237
- */
238
- constructor(keysNodesEntriesOrRaws = [], options) {
239
- super();
240
- if (options) {
241
- const { iterationType, toEntryFn, isMapMode, isDuplicate } = options;
242
- if (iterationType)
243
- this.iterationType = iterationType;
244
- if (isMapMode !== undefined)
245
- this._isMapMode = isMapMode;
246
- if (isDuplicate !== undefined)
247
- this._isDuplicate = isDuplicate;
248
- if (typeof toEntryFn === 'function')
249
- this._toEntryFn = toEntryFn;
250
- else if (toEntryFn)
251
- throw TypeError('toEntryFn must be a function type');
252
- }
253
- if (keysNodesEntriesOrRaws)
254
- this.addMany(keysNodesEntriesOrRaws);
255
- }
256
- _isMapMode = true;
257
- /**
258
- * Gets whether the tree is in Map mode.
259
- * @remarks In Map mode (default), values are stored in an external Map, and nodes only hold keys. If false, values are stored directly on the nodes. Time O(1)
260
- *
261
- * @returns True if in Map mode, false otherwise.
262
- */
263
- get isMapMode() {
264
- return this._isMapMode;
265
- }
266
- _isDuplicate = false;
267
- /**
268
- * Gets whether the tree allows duplicate keys.
269
- * @remarks Time O(1)
270
- *
271
- * @returns True if duplicates are allowed, false otherwise.
272
- */
273
- get isDuplicate() {
274
- return this._isDuplicate;
275
- }
276
- _store = new Map();
277
- /**
278
- * Gets the external value store (used in Map mode).
279
- * @remarks Time O(1)
280
- *
281
- * @returns The map storing key-value pairs.
282
- */
283
- get store() {
284
- return this._store;
285
- }
286
- _root;
287
- /**
288
- * Gets the root node of the tree.
289
- * @remarks Time O(1)
290
- *
291
- * @returns The root node.
292
- */
293
- get root() {
294
- return this._root;
295
- }
296
- _size = 0;
297
- /**
298
- * Gets the number of nodes in the tree.
299
- * @remarks Time O(1)
300
- *
301
- * @returns The size of the tree.
302
- */
303
- get size() {
304
- return this._size;
305
- }
306
- _NIL = new BinaryTreeNode(NaN);
307
- /**
308
- * Gets the sentinel NIL node (used in self-balancing trees like Red-Black Tree).
309
- * @remarks Time O(1)
310
- *
311
- * @returns The NIL node.
312
- */
313
- get NIL() {
314
- return this._NIL;
315
- }
316
- _toEntryFn;
317
- /**
318
- * Gets the function used to convert raw data objects (R) into [key, value] entries.
319
- * @remarks Time O(1)
320
- *
321
- * @returns The conversion function.
322
- */
323
- get toEntryFn() {
324
- return this._toEntryFn;
325
- }
326
- /**
327
- * (Protected) Creates a new node.
328
- * @remarks Time O(1), Space O(1)
329
- *
330
- * @param key - The key for the new node.
331
- * @param [value] - The value for the new node (used if not in Map mode).
332
- * @returns The newly created node.
333
- */
334
- _createNode(key, value) {
335
- return new BinaryTreeNode(key, this._isMapMode ? undefined : value);
336
- }
337
- /**
338
- * Creates a new, empty tree of the same type and configuration.
339
- * @remarks Time O(1) (excluding options cloning), Space O(1)
340
- *
341
- * @param [options] - Optional overrides for the new tree's options.
342
- * @returns A new, empty tree instance.
343
- */
344
- createTree(options) {
345
- return this._createInstance(options);
346
- }
347
- /**
348
- * Ensures the input is a node. If it's a key or entry, it searches for the node.
349
- * @remarks Time O(1) if a node is passed. O(N) if a key or entry is passed (due to `getNode` performing a full search). Space O(1) if iterative search, O(H) if recursive (where H is height, O(N) worst-case).
350
- *
351
- * @param keyNodeOrEntry - The item to resolve to a node.
352
- * @param [iterationType=this.iterationType] - The traversal method to use if searching.
353
- * @returns The resolved node, or null/undefined if not found or input is null/undefined.
354
- */
355
- ensureNode(keyNodeOrEntry, iterationType = this.iterationType) {
356
- if (keyNodeOrEntry === null)
357
- return null;
358
- if (keyNodeOrEntry === undefined)
359
- return;
360
- if (keyNodeOrEntry === this._NIL)
361
- return;
362
- if (this.isNode(keyNodeOrEntry))
363
- return keyNodeOrEntry;
364
- if (this.isEntry(keyNodeOrEntry)) {
365
- const key = keyNodeOrEntry[0];
366
- if (key === null)
367
- return null;
368
- if (key === undefined)
369
- return;
370
- return this.getNode(key, this._root, iterationType);
371
- }
372
- return this.getNode(keyNodeOrEntry, this._root, iterationType);
373
- }
374
- /**
375
- * Checks if the given item is a `BinaryTreeNode` instance.
376
- * @remarks Time O(1), Space O(1)
377
- *
378
- * @param keyNodeOrEntry - The item to check.
379
- * @returns True if it's a node, false otherwise.
380
- */
381
- isNode(keyNodeOrEntry) {
382
- return keyNodeOrEntry instanceof BinaryTreeNode;
383
- }
384
- /**
385
- * Checks if the given item is a raw data object (R) that needs conversion via `toEntryFn`.
386
- * @remarks Time O(1), Space O(1)
387
- *
388
- * @param keyNodeEntryOrRaw - The item to check.
389
- * @returns True if it's a raw object, false otherwise.
390
- */
391
- isRaw(keyNodeEntryOrRaw) {
392
- return this._toEntryFn !== undefined && typeof keyNodeEntryOrRaw === 'object';
393
- }
394
- /**
395
- * Checks if the given item is a "real" node (i.e., not null, undefined, or NIL).
396
- * @remarks Time O(1), Space O(1)
397
- *
398
- * @param keyNodeOrEntry - The item to check.
399
- * @returns True if it's a real node, false otherwise.
400
- */
401
- isRealNode(keyNodeOrEntry) {
402
- if (keyNodeOrEntry === this._NIL || keyNodeOrEntry === null || keyNodeOrEntry === undefined)
403
- return false;
404
- return this.isNode(keyNodeOrEntry);
405
- }
406
- /**
407
- * Checks if the given item is either a "real" node or null.
408
- * @remarks Time O(1), Space O(1)
409
- *
410
- * @param keyNodeOrEntry - The item to check.
411
- * @returns True if it's a real node or null, false otherwise.
412
- */
413
- isRealNodeOrNull(keyNodeOrEntry) {
414
- return keyNodeOrEntry === null || this.isRealNode(keyNodeOrEntry);
415
- }
416
- /**
417
- * Checks if the given item is the sentinel NIL node.
418
- * @remarks Time O(1), Space O(1)
419
- *
420
- * @param keyNodeOrEntry - The item to check.
421
- * @returns True if it's the NIL node, false otherwise.
422
- */
423
- isNIL(keyNodeOrEntry) {
424
- return keyNodeOrEntry === this._NIL;
425
- }
426
- /**
427
- * Checks if the given item is a `Range` object.
428
- * @remarks Time O(1), Space O(1)
429
- *
430
- * @param keyNodeEntryOrPredicate - The item to check.
431
- * @returns True if it's a Range, false otherwise.
432
- */
433
- isRange(keyNodeEntryOrPredicate) {
434
- return keyNodeEntryOrPredicate instanceof Range;
435
- }
436
- /**
437
- * Checks if a node is a leaf (has no real children).
438
- * @remarks Time O(N) if a key/entry is passed (due to `ensureNode`). O(1) if a node is passed. Space O(1) or O(H) (from `ensureNode`).
439
- *
440
- * @param keyNodeOrEntry - The node to check.
441
- * @returns True if the node is a leaf, false otherwise.
442
- */
443
- isLeaf(keyNodeOrEntry) {
444
- keyNodeOrEntry = this.ensureNode(keyNodeOrEntry);
445
- if (keyNodeOrEntry === undefined)
446
- return false;
447
- if (keyNodeOrEntry === null)
448
- return true; // A null spot is considered a leaf
449
- return !this.isRealNode(keyNodeOrEntry.left) && !this.isRealNode(keyNodeOrEntry.right);
450
- }
451
- /**
452
- * Checks if the given item is a [key, value] entry pair.
453
- * @remarks Time O(1), Space O(1)
454
- *
455
- * @param keyNodeOrEntry - The item to check.
456
- * @returns True if it's an entry, false otherwise.
457
- */
458
- isEntry(keyNodeOrEntry) {
459
- return Array.isArray(keyNodeOrEntry) && keyNodeOrEntry.length === 2;
460
- }
461
- /**
462
- * Checks if the given key is valid (comparable or null).
463
- * @remarks Time O(1), Space O(1)
464
- *
465
- * @param key - The key to validate.
466
- * @returns True if the key is valid, false otherwise.
467
- */
468
- isValidKey(key) {
469
- if (key === null)
470
- return true;
471
- return isComparable(key);
472
- }
473
- /**
474
- * Adds a new node to the tree.
475
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation adds the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
476
- *
477
- * @param keyNodeOrEntry - The key, node, or entry to add.
478
- * @param [value] - The value, if providing just a key.
479
- * @returns True if the addition was successful, false otherwise.
480
- */
481
- add(keyNodeOrEntry, value) {
482
- const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
483
- if (newNode === undefined)
484
- return false;
485
- if (!this._root) {
486
- this._setRoot(newNode);
487
- if (this._isMapMode)
488
- this._setValue(newNode?.key, newValue);
489
- this._size = 1;
490
- return true;
491
- }
492
- const queue = new Queue([this._root]);
493
- let potentialParent;
494
- while (queue.length > 0) {
495
- const cur = queue.shift();
496
- if (!cur)
497
- continue;
498
- if (!this._isDuplicate) {
499
- if (newNode !== null && cur.key === newNode.key) {
500
- this._replaceNode(cur, newNode);
501
- if (this._isMapMode)
502
- this._setValue(cur.key, newValue);
503
- return true; // Replaced existing node
504
- }
505
- }
506
- if (potentialParent === undefined && (cur.left === undefined || cur.right === undefined)) {
507
- potentialParent = cur;
508
- }
509
- if (cur.left !== null) {
510
- if (cur.left)
511
- queue.push(cur.left);
512
- }
513
- if (cur.right !== null) {
514
- if (cur.right)
515
- queue.push(cur.right);
516
- }
517
- }
518
- if (potentialParent) {
519
- if (potentialParent.left === undefined) {
520
- potentialParent.left = newNode;
521
- }
522
- else if (potentialParent.right === undefined) {
523
- potentialParent.right = newNode;
524
- }
525
- if (this._isMapMode)
526
- this._setValue(newNode?.key, newValue);
527
- this._size++;
528
- return true;
529
- }
530
- return false; // Should not happen if tree is not full?
531
- }
532
- /**
533
- * Adds multiple items to the tree.
534
- * @remarks Time O(N * M), where N is the number of items to add and M is the size of the tree at insertion (due to O(M) `add` operation). Space O(M) (from `add`) + O(N) (for the `inserted` array).
535
- *
536
- * @param keysNodesEntriesOrRaws - An iterable of items to add.
537
- * @param [values] - An optional parallel iterable of values.
538
- * @returns An array of booleans indicating the success of each individual `add` operation.
539
- */
540
- addMany(keysNodesEntriesOrRaws, values) {
541
- const inserted = [];
542
- let valuesIterator;
543
- if (values) {
544
- valuesIterator = values[Symbol.iterator]();
545
- }
546
- for (let keyNodeEntryOrRaw of keysNodesEntriesOrRaws) {
547
- let value = undefined;
548
- if (valuesIterator) {
549
- const valueResult = valuesIterator.next();
550
- if (!valueResult.done) {
551
- value = valueResult.value;
552
- }
553
- }
554
- if (this.isRaw(keyNodeEntryOrRaw))
555
- keyNodeEntryOrRaw = this._toEntryFn(keyNodeEntryOrRaw);
556
- inserted.push(this.add(keyNodeEntryOrRaw, value));
557
- }
558
- return inserted;
559
- }
560
- /**
561
- * Merges another tree into this one by adding all its nodes.
562
- * @remarks Time O(N * M), same as `addMany`, where N is the size of `anotherTree` and M is the size of this tree. Space O(M) (from `add`).
563
- *
564
- * @param anotherTree - The tree to merge.
565
- */
566
- merge(anotherTree) {
567
- this.addMany(anotherTree, []);
568
- }
569
- /**
570
- * Clears the tree and refills it with new items.
571
- * @remarks Time O(N) (for `clear`) + O(N * M) (for `addMany`) = O(N * M). Space O(M) (from `addMany`).
572
- *
573
- * @param keysNodesEntriesOrRaws - An iterable of items to add.
574
- * @param [values] - An optional parallel iterable of values.
575
- */
576
- refill(keysNodesEntriesOrRaws, values) {
577
- this.clear();
578
- this.addMany(keysNodesEntriesOrRaws, values);
579
- }
580
- /**
581
- * Deletes a node from the tree.
582
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation finds the node, and if it has two children, swaps it with the rightmost node of its left subtree (in-order predecessor) before deleting. Time O(N) in the worst case. O(N) to find the node (`getNode`) and O(H) (which is O(N) worst-case) to find the rightmost node. Space O(1) (if `getNode` is iterative, which it is).
583
- *
584
- * @param keyNodeOrEntry - The node to delete.
585
- * @returns An array containing deletion results (for compatibility with self-balancing trees).
586
- */
587
- delete(keyNodeOrEntry) {
588
- const deletedResult = [];
589
- if (!this._root)
590
- return deletedResult;
591
- const curr = this.getNode(keyNodeOrEntry);
592
- if (!curr)
593
- return deletedResult;
594
- const parent = curr?.parent;
595
- let needBalanced;
596
- let orgCurrent = curr;
597
- if (!curr.left && !curr.right && !parent) {
598
- // Deleting the root with no children
599
- this._setRoot(undefined);
600
- }
601
- else if (curr.left) {
602
- // Node has a left child (or two children)
603
- // Find the rightmost node in the left subtree
604
- const leftSubTreeRightMost = this.getRightMost(node => node, curr.left);
605
- if (leftSubTreeRightMost) {
606
- const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
607
- // Swap properties
608
- orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
609
- // `orgCurrent` is now the node to be physically deleted (which was the rightmost)
610
- if (parentOfLeftSubTreeMax) {
611
- // Unlink the rightmost node
612
- if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
613
- parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
614
- else
615
- parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
616
- needBalanced = parentOfLeftSubTreeMax;
617
- }
618
- }
619
- }
620
- else if (parent) {
621
- // Node has no left child, but has a parent
622
- // Promote the right child (which could be null)
623
- const { familyPosition: fp } = curr;
624
- if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
625
- parent.left = curr.right;
626
- }
627
- else if (fp === 'RIGHT' || fp === 'ROOT_RIGHT') {
628
- parent.right = curr.right;
629
- }
630
- needBalanced = parent;
631
- }
632
- else {
633
- // Deleting the root, which has no left child
634
- // Promote the right child as the new root
635
- this._setRoot(curr.right);
636
- curr.right = undefined;
637
- }
638
- this._size = this._size - 1;
639
- deletedResult.push({ deleted: orgCurrent, needBalanced });
640
- if (this._isMapMode && orgCurrent)
641
- this._store.delete(orgCurrent.key);
642
- return deletedResult;
643
- }
644
- /**
645
- * Searches the tree for nodes matching a predicate.
646
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Performs a full DFS (pre-order) scan of the tree. Time O(N), as it may visit every node. Space O(H) for the call stack (recursive) or explicit stack (iterative), where H is the tree height (O(N) worst-case).
647
- *
648
- * @template C - The type of the callback function.
649
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
650
- * @param [onlyOne=false] - If true, stops after finding the first match.
651
- * @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on matching nodes.
652
- * @param [startNode=this._root] - The node to start the search from.
653
- * @param [iterationType=this.iterationType] - Whether to use 'RECURSIVE' or 'ITERATIVE' search.
654
- * @returns An array of results from the callback function for each matching node.
655
- */
656
- search(keyNodeEntryOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
657
- if (keyNodeEntryOrPredicate === undefined)
658
- return [];
659
- if (keyNodeEntryOrPredicate === null)
660
- return [];
661
- startNode = this.ensureNode(startNode);
662
- if (!startNode)
663
- return [];
664
- const predicate = this._ensurePredicate(keyNodeEntryOrPredicate);
665
- const ans = [];
666
- if (iterationType === 'RECURSIVE') {
667
- const dfs = (cur) => {
668
- if (predicate(cur)) {
669
- ans.push(callback(cur));
670
- if (onlyOne)
671
- return;
672
- }
673
- if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
674
- return;
675
- if (this.isRealNode(cur.left))
676
- dfs(cur.left);
677
- if (this.isRealNode(cur.right))
678
- dfs(cur.right);
679
- };
680
- dfs(startNode);
681
- }
682
- else {
683
- const stack = [startNode];
684
- while (stack.length > 0) {
685
- const cur = stack.pop();
686
- if (this.isRealNode(cur)) {
687
- if (predicate(cur)) {
688
- ans.push(callback(cur));
689
- if (onlyOne)
690
- return ans;
691
- }
692
- if (this.isRealNode(cur.left))
693
- stack.push(cur.left);
694
- if (this.isRealNode(cur.right))
695
- stack.push(cur.right);
696
- }
697
- }
698
- }
699
- return ans;
700
- }
701
- getNodes(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
702
- return this.search(keyNodeEntryOrPredicate, onlyOne, node => node, startNode, iterationType);
703
- }
704
- /**
705
- * Gets the first node matching a predicate.
706
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Time O(N) in the worst case (via `search`). Space O(H) or O(N) (via `search`).
707
- *
708
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
709
- * @param [startNode=this._root] - The node to start the search from.
710
- * @param [iterationType=this.iterationType] - The traversal method.
711
- * @returns The first matching node, or undefined if not found.
712
- */
713
- getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
714
- return this.search(keyNodeEntryOrPredicate, true, node => node, startNode, iterationType)[0];
715
- }
716
- /**
717
- * Gets the value associated with a key.
718
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Time O(1) if in Map mode. O(N) if not in Map mode (uses `getNode`). Space O(1) if in Map mode. O(H) or O(N) otherwise.
719
- *
720
- * @param keyNodeEntryOrPredicate - The key, node, or entry to get the value for.
721
- * @param [startNode=this._root] - The node to start searching from (if not in Map mode).
722
- * @param [iterationType=this.iterationType] - The traversal method (if not in Map mode).
723
- * @returns The associated value, or undefined.
724
- */
725
- get(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
726
- if (this._isMapMode) {
727
- const key = this._extractKey(keyNodeEntryOrPredicate);
728
- if (key === null || key === undefined)
729
- return;
730
- return this._store.get(key);
731
- }
732
- return this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)?.value;
733
- }
734
- has(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
735
- return this.search(keyNodeEntryOrPredicate, true, node => node, startNode, iterationType).length > 0;
736
- }
737
- /**
738
- * Clears the tree of all nodes and values.
739
- * @remarks Time O(N) if in Map mode (due to `_store.clear()`), O(1) otherwise. Space O(1)
740
- */
741
- clear() {
742
- this._clearNodes();
743
- if (this._isMapMode)
744
- this._clearValues();
745
- }
746
- /**
747
- * Checks if the tree is empty.
748
- * @remarks Time O(1), Space O(1)
749
- *
750
- * @returns True if the tree has no nodes, false otherwise.
751
- */
752
- isEmpty() {
753
- return this._size === 0;
754
- }
755
- /**
756
- * Checks if the tree is perfectly balanced.
757
- * @remarks A tree is perfectly balanced if the difference between min and max height is at most 1. Time O(N), as it requires two full traversals (`getMinHeight` and `getHeight`). Space O(H) or O(N) (from height calculation).
758
- *
759
- * @param [startNode=this._root] - The node to start checking from.
760
- * @returns True if perfectly balanced, false otherwise.
761
- */
762
- isPerfectlyBalanced(startNode = this._root) {
763
- return this.getMinHeight(startNode) + 1 >= this.getHeight(startNode);
764
- }
765
- /**
766
- * Checks if the tree is a valid Binary Search Tree (BST).
767
- * @remarks Time O(N), as it must visit every node. Space O(H) for the call stack (recursive) or explicit stack (iterative), where H is the tree height (O(N) worst-case).
768
- *
769
- * @param [startNode=this._root] - The node to start checking from.
770
- * @param [iterationType=this.iterationType] - The traversal method.
771
- * @returns True if it's a valid BST, false otherwise.
772
- */
773
- isBST(startNode = this._root, iterationType = this.iterationType) {
774
- startNode = this.ensureNode(startNode);
775
- if (!startNode)
776
- return true;
777
- if (iterationType === 'RECURSIVE') {
778
- const dfs = (cur, min, max) => {
779
- if (!this.isRealNode(cur))
780
- return true;
781
- const numKey = Number(cur.key);
782
- if (numKey <= min || numKey >= max)
783
- return false;
784
- return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
785
- };
786
- const isStandardBST = dfs(startNode, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
787
- const isInverseBST = dfs(startNode, Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER); // Check for reverse BST
788
- return isStandardBST || isInverseBST;
789
- }
790
- else {
791
- // Iterative in-order traversal check
792
- const checkBST = (checkMax = false) => {
793
- const stack = [];
794
- let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
795
- let curr = startNode;
796
- while (this.isRealNode(curr) || stack.length > 0) {
797
- while (this.isRealNode(curr)) {
798
- stack.push(curr);
799
- curr = curr.left;
800
- }
801
- curr = stack.pop();
802
- const numKey = Number(curr.key);
803
- if (!this.isRealNode(curr) || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey))
804
- return false;
805
- prev = numKey;
806
- curr = curr.right;
807
- }
808
- return true;
809
- };
810
- const isStandardBST = checkBST(false);
811
- const isInverseBST = checkBST(true);
812
- return isStandardBST || isInverseBST;
813
- }
814
- }
815
- /**
816
- * Gets the depth of a node (distance from `startNode`).
817
- * @remarks Time O(H), where H is the depth of the `dist` node relative to `startNode`. O(N) worst-case. Space O(1).
818
- *
819
- * @param dist - The node to find the depth of.
820
- * @param [startNode=this._root] - The node to measure depth from (defaults to root).
821
- * @returns The depth (0 if `dist` is `startNode`).
822
- */
823
- getDepth(dist, startNode = this._root) {
824
- let distEnsured = this.ensureNode(dist);
825
- const beginRootEnsured = this.ensureNode(startNode);
826
- let depth = 0;
827
- while (distEnsured?.parent) {
828
- if (distEnsured === beginRootEnsured) {
829
- return depth;
830
- }
831
- depth++;
832
- distEnsured = distEnsured.parent;
833
- }
834
- return depth;
835
- }
836
- /**
837
- * Gets the maximum height of the tree (longest path from startNode to a leaf).
838
- * @remarks Time O(N), as it must visit every node. Space O(H) for recursive stack (O(N) worst-case) or O(N) for iterative stack (storing node + depth).
839
- *
840
- * @param [startNode=this._root] - The node to start measuring from.
841
- * @param [iterationType=this.iterationType] - The traversal method.
842
- * @returns The height ( -1 for an empty tree, 0 for a single-node tree).
843
- */
844
- getHeight(startNode = this._root, iterationType = this.iterationType) {
845
- startNode = this.ensureNode(startNode);
846
- if (!this.isRealNode(startNode))
847
- return -1;
848
- if (iterationType === 'RECURSIVE') {
849
- const _getMaxHeight = (cur) => {
850
- if (!this.isRealNode(cur))
851
- return -1;
852
- const leftHeight = _getMaxHeight(cur.left);
853
- const rightHeight = _getMaxHeight(cur.right);
854
- return Math.max(leftHeight, rightHeight) + 1;
855
- };
856
- return _getMaxHeight(startNode);
857
- }
858
- else {
859
- // Iterative (using DFS)
860
- const stack = [{ node: startNode, depth: 0 }];
861
- let maxHeight = 0;
862
- while (stack.length > 0) {
863
- const { node, depth } = stack.pop();
864
- if (this.isRealNode(node.left))
865
- stack.push({ node: node.left, depth: depth + 1 });
866
- if (this.isRealNode(node.right))
867
- stack.push({ node: node.right, depth: depth + 1 });
868
- maxHeight = Math.max(maxHeight, depth);
869
- }
870
- return maxHeight;
871
- }
872
- }
873
- /**
874
- * Gets the minimum height of the tree (shortest path from startNode to a leaf).
875
- * @remarks Time O(N), as it must visit every node. Space O(H) for recursive stack (O(N) worst-case) or O(N) for iterative (due to `depths` Map).
876
- *
877
- * @param [startNode=this._root] - The node to start measuring from.
878
- * @param [iterationType=this.iterationType] - The traversal method.
879
- * @returns The minimum height (-1 for empty, 0 for single node).
880
- */
881
- getMinHeight(startNode = this._root, iterationType = this.iterationType) {
882
- startNode = this.ensureNode(startNode);
883
- if (!startNode)
884
- return -1;
885
- if (iterationType === 'RECURSIVE') {
886
- const _getMinHeight = (cur) => {
887
- if (!this.isRealNode(cur))
888
- return 0;
889
- if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
890
- return 0; // Leaf node
891
- const leftMinHeight = _getMinHeight(cur.left);
892
- const rightMinHeight = _getMinHeight(cur.right);
893
- return Math.min(leftMinHeight, rightMinHeight) + 1;
894
- };
895
- return _getMinHeight(startNode);
896
- }
897
- else {
898
- // Iterative (using post-order DFS)
899
- const stack = [];
900
- let node = startNode, last = null;
901
- const depths = new Map();
902
- while (stack.length > 0 || node) {
903
- if (this.isRealNode(node)) {
904
- stack.push(node);
905
- node = node.left;
906
- }
907
- else {
908
- node = stack[stack.length - 1];
909
- if (!this.isRealNode(node.right) || last === node.right) {
910
- node = stack.pop();
911
- if (this.isRealNode(node)) {
912
- const leftMinHeight = this.isRealNode(node.left) ? depths.get(node.left) : -1;
913
- const rightMinHeight = this.isRealNode(node.right) ? depths.get(node.right) : -1;
914
- depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
915
- last = node;
916
- node = null;
917
- }
918
- }
919
- else
920
- node = node.right;
921
- }
922
- }
923
- return depths.get(startNode);
924
- }
925
- }
926
- /**
927
- * Gets the path from a given node up to the root.
928
- * @remarks Time O(H), where H is the depth of the `beginNode`. O(N) worst-case. Space O(H) for the result array.
929
- *
930
- * @template C - The type of the callback function.
931
- * @param beginNode - The node to start the path from.
932
- * @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on each node in the path.
933
- * @param [isReverse=false] - If true, returns the path from root-to-node.
934
- * @returns An array of callback results.
935
- */
936
- getPathToRoot(beginNode, callback = this._DEFAULT_NODE_CALLBACK, isReverse = false) {
937
- const result = [];
938
- let beginNodeEnsured = this.ensureNode(beginNode);
939
- if (!beginNodeEnsured)
940
- return result;
941
- while (beginNodeEnsured.parent) {
942
- result.push(callback(beginNodeEnsured));
943
- beginNodeEnsured = beginNodeEnsured.parent;
944
- }
945
- result.push(callback(beginNodeEnsured)); // Add the root
946
- return isReverse ? result.reverse() : result;
947
- }
948
- /**
949
- * Finds the leftmost node in a subtree (the node with the smallest key in a BST).
950
- * @remarks Time O(H), where H is the height of the left spine. O(N) worst-case. Space O(H) for recursive/trampoline stack.
951
- *
952
- * @template C - The type of the callback function.
953
- * @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on the leftmost node.
954
- * @param [startNode=this._root] - The subtree root to search from.
955
- * @param [iterationType=this.iterationType] - The traversal method.
956
- * @returns The callback result for the leftmost node.
957
- */
958
- getLeftMost(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
959
- if (this.isNIL(startNode))
960
- return callback(undefined);
961
- startNode = this.ensureNode(startNode);
962
- if (!this.isRealNode(startNode))
963
- return callback(undefined);
964
- if (iterationType === 'RECURSIVE') {
965
- const dfs = (cur) => {
966
- const { left } = cur;
967
- if (!this.isRealNode(left))
968
- return cur;
969
- return dfs(left);
970
- };
971
- return callback(dfs(startNode));
972
- }
973
- else {
974
- // Iterative (trampolined to prevent stack overflow, though 'ITERATIVE' usually means a loop)
975
- const dfs = makeTrampoline((cur) => {
976
- const { left } = cur;
977
- if (!this.isRealNode(left))
978
- return cur;
979
- return makeTrampolineThunk(() => dfs(left));
980
- });
981
- return callback(dfs(startNode));
982
- }
983
- }
984
- /**
985
- * Finds the rightmost node in a subtree (the node with the largest key in a BST).
986
- * @remarks Time O(H), where H is the height of the right spine. O(N) worst-case. Space O(H) for recursive/trampoline stack.
987
- *
988
- * @template C - The type of the callback function.
989
- * @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on the rightmost node.
990
- * @param [startNode=this._root] - The subtree root to search from.
991
- * @param [iterationType=this.iterationType] - The traversal method.
992
- * @returns The callback result for the rightmost node.
993
- */
994
- getRightMost(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
995
- if (this.isNIL(startNode))
996
- return callback(undefined);
997
- startNode = this.ensureNode(startNode);
998
- if (!startNode)
999
- return callback(undefined);
1000
- if (iterationType === 'RECURSIVE') {
1001
- const dfs = (cur) => {
1002
- const { right } = cur;
1003
- if (!this.isRealNode(right))
1004
- return cur;
1005
- return dfs(right);
1006
- };
1007
- return callback(dfs(startNode));
1008
- }
1009
- else {
1010
- const dfs = makeTrampoline((cur) => {
1011
- const { right } = cur;
1012
- if (!this.isRealNode(right))
1013
- return cur;
1014
- return makeTrampolineThunk(() => dfs(right));
1015
- });
1016
- return callback(dfs(startNode));
1017
- }
1018
- }
1019
- /**
1020
- * Gets the Morris traversal predecessor (rightmost node in the left subtree, or node itself).
1021
- * @remarks This is primarily a helper for Morris traversal. Time O(H), where H is the height of the left subtree. O(N) worst-case. Space O(1).
1022
- *
1023
- * @param node - The node to find the predecessor for.
1024
- * @returns The Morris predecessor.
1025
- */
1026
- getPredecessor(node) {
1027
- if (this.isRealNode(node.left)) {
1028
- let predecessor = node.left;
1029
- while (!this.isRealNode(predecessor) || (this.isRealNode(predecessor.right) && predecessor.right !== node)) {
1030
- if (this.isRealNode(predecessor)) {
1031
- predecessor = predecessor.right;
1032
- }
1033
- }
1034
- return predecessor;
1035
- }
1036
- else {
1037
- return node;
1038
- }
1039
- }
1040
- /**
1041
- * Gets the in-order successor of a node in a BST.
1042
- * @remarks Time O(H), where H is the tree height. O(N) worst-case. Space O(H) (due to `getLeftMost` stack).
1043
- *
1044
- * @param [x] - The node to find the successor of.
1045
- * @returns The successor node, or null/undefined if none exists.
1046
- */
1047
- getSuccessor(x) {
1048
- x = this.ensureNode(x);
1049
- if (!this.isRealNode(x))
1050
- return undefined;
1051
- if (this.isRealNode(x.right)) {
1052
- return this.getLeftMost(node => node, x.right);
1053
- }
1054
- let y = x.parent;
1055
- while (this.isRealNode(y) && x === y.right) {
1056
- x = y;
1057
- y = y.parent;
1058
- }
1059
- return y;
1060
- }
1061
- /**
1062
- * Performs a Depth-First Search (DFS) traversal.
1063
- * @remarks Time O(N), visits every node. Space O(H) for the call/explicit stack. O(N) worst-case.
1064
- *
1065
- * @template C - The type of the callback function.
1066
- * @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each node.
1067
- * @param [pattern='IN'] - The traversal order ('IN', 'PRE', 'POST').
1068
- * @param [onlyOne=false] - If true, stops after the first callback.
1069
- * @param [startNode=this._root] - The node to start from.
1070
- * @param [iterationType=this.iterationType] - The traversal method.
1071
- * @param [includeNull=false] - If true, includes null nodes in the traversal.
1072
- * @returns An array of callback results.
1073
- */
1074
- dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', onlyOne = false, startNode = this._root, iterationType = this.iterationType, includeNull = false) {
1075
- startNode = this.ensureNode(startNode);
1076
- if (!startNode)
1077
- return [];
1078
- return this._dfs(callback, pattern, onlyOne, startNode, iterationType, includeNull);
1079
- }
1080
- /**
1081
- * Performs a Breadth-First Search (BFS) or Level-Order traversal.
1082
- * @remarks Time O(N), visits every node. Space O(N) in the worst case for the queue (e.g., a full last level).
1083
- *
1084
- * @template C - The type of the callback function.
1085
- * @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each node.
1086
- * @param [startNode=this._root] - The node to start from.
1087
- * @param [iterationType=this.iterationType] - The traversal method ('RECURSIVE' BFS is less common but supported here).
1088
- * @param [includeNull=false] - If true, includes null nodes in the traversal.
1089
- * @returns An array of callback results.
1090
- */
1091
- bfs(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType, includeNull = false) {
1092
- startNode = this.ensureNode(startNode);
1093
- if (!startNode)
1094
- return [];
1095
- const ans = [];
1096
- if (iterationType === 'RECURSIVE') {
1097
- // This is a "recursive" BFS, which is atypical. It uses a queue but calls itself.
1098
- const queue = new Queue([
1099
- startNode
1100
- ]);
1101
- const dfs = (level) => {
1102
- if (queue.length === 0)
1103
- return;
1104
- const current = queue.shift();
1105
- ans.push(callback(current));
1106
- if (includeNull) {
1107
- if (current && this.isRealNodeOrNull(current.left))
1108
- queue.push(current.left);
1109
- if (current && this.isRealNodeOrNull(current.right))
1110
- queue.push(current.right);
1111
- }
1112
- else {
1113
- if (this.isRealNode(current.left))
1114
- queue.push(current.left);
1115
- if (this.isRealNode(current.right))
1116
- queue.push(current.right);
1117
- }
1118
- dfs(level + 1);
1119
- };
1120
- dfs(0);
1121
- }
1122
- else {
1123
- // Standard iterative BFS
1124
- const queue = new Queue([startNode]);
1125
- while (queue.length > 0) {
1126
- const levelSize = queue.length; // Not strictly needed here, but good for level-by-level
1127
- for (let i = 0; i < levelSize; i++) {
1128
- const current = queue.shift();
1129
- ans.push(callback(current));
1130
- if (includeNull) {
1131
- if (current && this.isRealNodeOrNull(current.left))
1132
- queue.push(current.left);
1133
- if (current && this.isRealNodeOrNull(current.right))
1134
- queue.push(current.right);
1135
- }
1136
- else {
1137
- if (this.isRealNode(current.left))
1138
- queue.push(current.left);
1139
- if (this.isRealNode(current.right))
1140
- queue.push(current.right);
1141
- }
1142
- }
1143
- }
1144
- }
1145
- return ans;
1146
- }
1147
- /**
1148
- * Finds all leaf nodes in the tree.
1149
- * @remarks Time O(N), visits every node. Space O(H) for recursive stack or O(N) for iterative queue.
1150
- *
1151
- * @template C - The type of the callback function.
1152
- * @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each leaf node.
1153
- * @param [startNode=this._root] - The node to start from.
1154
- * @param [iterationType=this.iterationType] - The traversal method.
1155
- * @returns An array of callback results.
1156
- */
1157
- leaves(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
1158
- startNode = this.ensureNode(startNode);
1159
- const leaves = [];
1160
- if (!this.isRealNode(startNode))
1161
- return [];
1162
- if (iterationType === 'RECURSIVE') {
1163
- // DFS-based
1164
- const dfs = (cur) => {
1165
- if (this.isLeaf(cur)) {
1166
- leaves.push(callback(cur));
1167
- }
1168
- if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
1169
- return;
1170
- if (this.isRealNode(cur.left))
1171
- dfs(cur.left);
1172
- if (this.isRealNode(cur.right))
1173
- dfs(cur.right);
1174
- };
1175
- dfs(startNode);
1176
- }
1177
- else {
1178
- // BFS-based
1179
- const queue = new Queue([startNode]);
1180
- while (queue.length > 0) {
1181
- const cur = queue.shift();
1182
- if (this.isRealNode(cur)) {
1183
- if (this.isLeaf(cur)) {
1184
- leaves.push(callback(cur));
1185
- }
1186
- if (this.isRealNode(cur.left))
1187
- queue.push(cur.left);
1188
- if (this.isRealNode(cur.right))
1189
- queue.push(cur.right);
1190
- }
1191
- }
1192
- }
1193
- return leaves;
1194
- }
1195
- /**
1196
- * Returns a 2D array of nodes, grouped by level.
1197
- * @remarks Time O(N), visits every node. Space O(N) for the result array and the queue/stack.
1198
- *
1199
- * @template C - The type of the callback function.
1200
- * @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each node.
1201
- * @param [startNode=this._root] - The node to start from.
1202
- * @param [iterationType=this.iterationType] - The traversal method.
1203
- * @param [includeNull=false] - If true, includes null nodes.
1204
- * @returns A 2D array of callback results.
1205
- */
1206
- listLevels(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType, includeNull = false) {
1207
- startNode = this.ensureNode(startNode);
1208
- const levelsNodes = [];
1209
- if (!startNode)
1210
- return levelsNodes;
1211
- if (iterationType === 'RECURSIVE') {
1212
- // Pre-order DFS based level listing
1213
- const _recursive = (node, level) => {
1214
- if (!levelsNodes[level])
1215
- levelsNodes[level] = [];
1216
- levelsNodes[level].push(callback(node));
1217
- if (includeNull) {
1218
- if (node && this.isRealNodeOrNull(node.left))
1219
- _recursive(node.left, level + 1);
1220
- if (node && this.isRealNodeOrNull(node.right))
1221
- _recursive(node.right, level + 1);
1222
- }
1223
- else {
1224
- if (node && node.left)
1225
- _recursive(node.left, level + 1);
1226
- if (node && node.right)
1227
- _recursive(node.right, level + 1);
1228
- }
1229
- };
1230
- _recursive(startNode, 0);
1231
- }
1232
- else {
1233
- // Iterative DFS based level listing
1234
- const stack = [[startNode, 0]];
1235
- while (stack.length > 0) {
1236
- const head = stack.pop();
1237
- const [node, level] = head;
1238
- if (!levelsNodes[level])
1239
- levelsNodes[level] = [];
1240
- levelsNodes[level].push(callback(node));
1241
- if (includeNull) {
1242
- if (node && this.isRealNodeOrNull(node.right))
1243
- stack.push([node.right, level + 1]);
1244
- if (node && this.isRealNodeOrNull(node.left))
1245
- stack.push([node.left, level + 1]);
1246
- }
1247
- else {
1248
- if (node && node.right)
1249
- stack.push([node.right, level + 1]);
1250
- if (node && node.left)
1251
- stack.push([node.left, level + 1]);
1252
- }
1253
- }
1254
- }
1255
- return levelsNodes;
1256
- }
1257
- /**
1258
- * Performs a Morris (threaded) traversal.
1259
- * @remarks This traversal uses O(1) extra space (excluding the result array) by temporarily modifying the tree's right child pointers. Time O(N), as each node is visited a constant number of times. Space O(1) (excluding the `ans` array).
1260
- *
1261
- * @template C - The type of the callback function.
1262
- * @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each node.
1263
- * @param [pattern='IN'] - The traversal order ('IN', 'PRE', 'POST').
1264
- * @param [startNode=this._root] - The node to start from.
1265
- * @returns An array of callback results.
1266
- */
1267
- morris(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', startNode = this._root) {
1268
- startNode = this.ensureNode(startNode);
1269
- if (!startNode)
1270
- return [];
1271
- const ans = [];
1272
- let cur = startNode;
1273
- // Helper to reverse a linked list (formed by right pointers)
1274
- const _reverseEdge = (node) => {
1275
- let pre = null;
1276
- let next = null;
1277
- while (node) {
1278
- next = node.right;
1279
- node.right = pre;
1280
- pre = node;
1281
- node = next;
1282
- }
1283
- return pre;
1284
- };
1285
- // Helper to print the reversed edge (for post-order)
1286
- const _printEdge = (node) => {
1287
- const tail = _reverseEdge(node);
1288
- let cur = tail;
1289
- while (cur) {
1290
- ans.push(callback(cur));
1291
- cur = cur.right;
1292
- }
1293
- _reverseEdge(tail); // Restore the edge
1294
- };
1295
- switch (pattern) {
1296
- case 'IN':
1297
- while (cur) {
1298
- if (cur.left) {
1299
- const predecessor = this.getPredecessor(cur);
1300
- if (!predecessor.right) {
1301
- // Create thread
1302
- predecessor.right = cur;
1303
- cur = cur.left;
1304
- continue;
1305
- }
1306
- else {
1307
- // Remove thread
1308
- predecessor.right = null;
1309
- }
1310
- }
1311
- ans.push(callback(cur));
1312
- cur = cur.right;
1313
- }
1314
- break;
1315
- case 'PRE':
1316
- while (cur) {
1317
- if (cur.left) {
1318
- const predecessor = this.getPredecessor(cur);
1319
- if (!predecessor.right) {
1320
- // Create thread and visit
1321
- predecessor.right = cur;
1322
- ans.push(callback(cur));
1323
- cur = cur.left;
1324
- continue;
1325
- }
1326
- else {
1327
- // Remove thread
1328
- predecessor.right = null;
1329
- }
1330
- }
1331
- else {
1332
- ans.push(callback(cur));
1333
- }
1334
- cur = cur.right;
1335
- }
1336
- break;
1337
- case 'POST':
1338
- while (cur) {
1339
- if (cur.left) {
1340
- const predecessor = this.getPredecessor(cur);
1341
- if (predecessor.right === null) {
1342
- // Create thread
1343
- predecessor.right = cur;
1344
- cur = cur.left;
1345
- continue;
1346
- }
1347
- else {
1348
- // Remove thread and print right spine of left child
1349
- predecessor.right = null;
1350
- _printEdge(cur.left);
1351
- }
1352
- }
1353
- cur = cur.right;
1354
- }
1355
- _printEdge(startNode); // Print the right spine of the root
1356
- break;
1357
- }
1358
- return ans;
1359
- }
1360
- /**
1361
- * Clones the tree.
1362
- * @remarks Time O(N * M), where N is the number of nodes and M is the tree size during insertion (due to `bfs` + `add`, and `add` is O(M)). Space O(N) for the new tree and the BFS queue.
1363
- *
1364
- * @returns A new, cloned instance of the tree.
1365
- */
1366
- clone() {
1367
- const out = this._createInstance();
1368
- this._clone(out);
1369
- return out;
1370
- }
1371
- /**
1372
- * Creates a new tree containing only the entries that satisfy the predicate.
1373
- * @remarks Time O(N * M), where N is nodes in this tree, and M is size of the new tree during insertion (O(N) iteration + O(M) `add` for each item). Space O(N) for the new tree.
1374
- *
1375
- * @param predicate - A function to test each [key, value] pair.
1376
- * @param [thisArg] - `this` context for the predicate.
1377
- * @returns A new, filtered tree.
1378
- */
1379
- filter(predicate, thisArg) {
1380
- const out = this._createInstance();
1381
- let i = 0;
1382
- for (const [k, v] of this)
1383
- if (predicate.call(thisArg, k, v, i++, this))
1384
- out.add([k, v]);
1385
- return out;
1386
- }
1387
- /**
1388
- * Creates a new tree by mapping each [key, value] pair to a new entry.
1389
- * @remarks Time O(N * M), where N is nodes in this tree, and M is size of the new tree during insertion. Space O(N) for the new tree.
1390
- *
1391
- * @template MK - New key type.
1392
- * @template MV - New value type.
1393
- * @template MR - New raw type.
1394
- * @param cb - A function to map each [key, value] pair.
1395
- * @param [options] - Options for the new tree.
1396
- * @param [thisArg] - `this` context for the callback.
1397
- * @returns A new, mapped tree.
1398
- */
1399
- map(cb, options, thisArg) {
1400
- const out = this._createLike([], options);
1401
- let i = 0;
1402
- for (const [k, v] of this)
1403
- out.add(cb.call(thisArg, k, v, i++, this));
1404
- return out;
1405
- }
1406
- /**
1407
- * Generates a string representation of the tree for visualization.
1408
- * @remarks Time O(N), visits every node. Space O(N*H) or O(N^2) in the worst case, as the string width can grow significantly.
1409
- *
1410
- * @param [startNode=this._root] - The node to start printing from.
1411
- * @param [options] - Options to control the output (e.g., show nulls).
1412
- * @returns The string representation of the tree.
1413
- */
1414
- toVisual(startNode = this._root, options) {
1415
- const opts = { isShowUndefined: false, isShowNull: true, isShowRedBlackNIL: false, ...options };
1416
- startNode = this.ensureNode(startNode);
1417
- let output = '';
1418
- if (!startNode)
1419
- return output;
1420
- if (opts.isShowUndefined)
1421
- output += `U for undefined\n`;
1422
- if (opts.isShowNull)
1423
- output += `N for null\n`;
1424
- if (opts.isShowRedBlackNIL)
1425
- output += `S for Sentinel Node(NIL)\n`;
1426
- const display = (root) => {
1427
- const [lines] = this._displayAux(root, opts);
1428
- let paragraph = '';
1429
- for (const line of lines) {
1430
- paragraph += line + '\n';
1431
- }
1432
- output += paragraph;
1433
- };
1434
- display(startNode);
1435
- return output;
1436
- }
1437
- /**
1438
- * Prints a visual representation of the tree to the console.
1439
- * @remarks Time O(N) (via `toVisual`). Space O(N*H) or O(N^2) (via `toVisual`).
1440
- *
1441
- * @param [options] - Options to control the output.
1442
- * @param [startNode=this._root] - The node to start printing from.
1443
- */
1444
- print(options, startNode = this._root) {
1445
- console.log(this.toVisual(startNode, options));
1446
- }
1447
- /**
1448
- * (Protected) Core DFS implementation.
1449
- * @remarks Time O(N), visits every node satisfying predicates. Space O(H) for call/explicit stack. O(N) worst-case.
1450
- *
1451
- * @template C - Callback type.
1452
- * @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on nodes.
1453
- * @param [pattern='IN'] - Traversal order.
1454
- * @param [onlyOne=false] - Stop after first match.
1455
- * @param [startNode=this._root] - Starting node.
1456
- * @param [iterationType=this.iterationType] - Traversal method.
1457
- * @param [includeNull=false] - Include nulls.
1458
- * @param [shouldVisitLeft] - Predicate to traverse left.
1459
- * @param [shouldVisitRight] - Predicate to traverse right.
1460
- * @param [shouldVisitRoot] - Predicate to visit root.
1461
- * @param [shouldProcessRoot] - Predicate to process root.
1462
- * @returns Array of callback results.
1463
- */
1464
- _dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', onlyOne = false, startNode = this._root, iterationType = this.iterationType, includeNull = false, shouldVisitLeft = node => !!node, shouldVisitRight = node => !!node, shouldVisitRoot = node => {
1465
- if (includeNull)
1466
- return this.isRealNodeOrNull(node);
1467
- return this.isRealNode(node);
1468
- }, shouldProcessRoot = node => this.isRealNodeOrNull(node)) {
1469
- startNode = this.ensureNode(startNode);
1470
- if (!startNode)
1471
- return [];
1472
- const ans = [];
1473
- if (iterationType === 'RECURSIVE') {
1474
- const dfs = (node) => {
1475
- if (!shouldVisitRoot(node))
1476
- return;
1477
- const visitLeft = () => {
1478
- if (shouldVisitLeft(node) && node?.left !== undefined)
1479
- dfs(node?.left);
1480
- };
1481
- const visitRight = () => {
1482
- if (shouldVisitRight(node) && node?.right !== undefined)
1483
- dfs(node?.right);
1484
- };
1485
- switch (pattern) {
1486
- case 'IN':
1487
- visitLeft();
1488
- if (shouldProcessRoot(node)) {
1489
- ans.push(callback(node));
1490
- if (onlyOne)
1491
- return;
1492
- }
1493
- visitRight();
1494
- break;
1495
- case 'PRE':
1496
- if (shouldProcessRoot(node)) {
1497
- ans.push(callback(node));
1498
- if (onlyOne)
1499
- return;
1500
- }
1501
- visitLeft();
1502
- visitRight();
1503
- break;
1504
- case 'POST':
1505
- visitLeft();
1506
- visitRight();
1507
- if (shouldProcessRoot(node)) {
1508
- ans.push(callback(node));
1509
- if (onlyOne)
1510
- return;
1511
- }
1512
- break;
1513
- }
1514
- };
1515
- dfs(startNode);
1516
- }
1517
- else {
1518
- // Iterative
1519
- const stack = [{ opt: DFSOperation.VISIT, node: startNode }];
1520
- const pushLeft = (cur) => {
1521
- if (shouldVisitLeft(cur.node))
1522
- stack.push({ opt: DFSOperation.VISIT, node: cur.node?.left });
1523
- };
1524
- const pushRight = (cur) => {
1525
- if (shouldVisitRight(cur.node))
1526
- stack.push({ opt: DFSOperation.VISIT, node: cur.node?.right });
1527
- };
1528
- const pushRoot = (cur) => {
1529
- if (shouldVisitRoot(cur.node))
1530
- stack.push({ opt: DFSOperation.PROCESS, node: cur.node });
1531
- };
1532
- while (stack.length > 0) {
1533
- const cur = stack.pop();
1534
- if (cur === undefined)
1535
- continue;
1536
- if (!shouldVisitRoot(cur.node))
1537
- continue;
1538
- if (cur.opt === DFSOperation.PROCESS) {
1539
- if (shouldProcessRoot(cur.node) && cur.node !== undefined) {
1540
- ans.push(callback(cur.node));
1541
- if (onlyOne)
1542
- return ans;
1543
- }
1544
- }
1545
- else {
1546
- // VISIT
1547
- switch (pattern) {
1548
- case 'IN':
1549
- pushRight(cur);
1550
- pushRoot(cur);
1551
- pushLeft(cur);
1552
- break;
1553
- case 'PRE':
1554
- pushRight(cur);
1555
- pushLeft(cur);
1556
- pushRoot(cur);
1557
- break;
1558
- case 'POST':
1559
- pushRoot(cur);
1560
- pushRight(cur);
1561
- pushLeft(cur);
1562
- break;
1563
- }
1564
- }
1565
- }
1566
- }
1567
- return ans;
1568
- }
1569
- /**
1570
- * (Protected) Gets the iterator for the tree (default in-order).
1571
- * @remarks Time O(N) for full iteration. O(H) to get the first element. Space O(H) for the iterative stack. O(H) for recursive stack.
1572
- *
1573
- * @param [node=this._root] - The node to start iteration from.
1574
- * @returns An iterator for [key, value] pairs.
1575
- */
1576
- *_getIterator(node = this._root) {
1577
- if (!node)
1578
- return;
1579
- if (this.iterationType === 'ITERATIVE') {
1580
- const stack = [];
1581
- let current = node;
1582
- while (current || stack.length > 0) {
1583
- // Go to the leftmost node
1584
- while (this.isRealNode(current)) {
1585
- stack.push(current);
1586
- current = current.left;
1587
- }
1588
- // Visit the node
1589
- current = stack.pop();
1590
- if (this.isRealNode(current)) {
1591
- if (this._isMapMode)
1592
- yield [current.key, this._store.get(current.key)];
1593
- else
1594
- yield [current.key, current.value];
1595
- // Move to the right subtree
1596
- current = current.right;
1597
- }
1598
- }
1599
- }
1600
- else {
1601
- // Recursive in-order traversal
1602
- if (node.left && this.isRealNode(node)) {
1603
- yield* this[Symbol.iterator](node.left);
1604
- }
1605
- if (this._isMapMode)
1606
- yield [node.key, this._store.get(node.key)];
1607
- else
1608
- yield [node.key, node.value];
1609
- if (node.right && this.isRealNode(node)) {
1610
- yield* this[Symbol.iterator](node.right);
1611
- }
1612
- }
1613
- }
1614
- /**
1615
- * (Protected) Default callback function, returns the node's key.
1616
- * @remarks Time O(1)
1617
- *
1618
- * @param node - The node.
1619
- * @returns The node's key or undefined.
1620
- */
1621
- _DEFAULT_NODE_CALLBACK = (node) => (node ? node.key : undefined);
1622
- /**
1623
- * (Protected) Snapshots the current tree's configuration options.
1624
- * @remarks Time O(1)
1625
- *
1626
- * @template TK, TV, TR - Generic types for the options.
1627
- * @returns The options object.
1628
- */
1629
- _snapshotOptions() {
1630
- return {
1631
- iterationType: this.iterationType,
1632
- toEntryFn: this.toEntryFn,
1633
- isMapMode: this.isMapMode,
1634
- isDuplicate: this.isDuplicate
1635
- };
1636
- }
1637
- /**
1638
- * (Protected) Creates a new, empty instance of the same tree constructor.
1639
- * @remarks Time O(1)
1640
- *
1641
- * @template TK, TV, TR - Generic types for the new instance.
1642
- * @param [options] - Options for the new tree.
1643
- * @returns A new, empty tree.
1644
- */
1645
- _createInstance(options) {
1646
- const Ctor = this.constructor;
1647
- return new Ctor([], { ...this._snapshotOptions(), ...(options ?? {}) });
1648
- }
1649
- /**
1650
- * (Protected) Creates a new instance of the same tree constructor, potentially with different generic types.
1651
- * @remarks Time O(N) (or as per constructor) due to processing the iterable.
1652
- *
1653
- * @template TK, TV, TR - Generic types for the new instance.
1654
- * @param [iter=[]] - An iterable to populate the new tree.
1655
- * @param [options] - Options for the new tree.
1656
- * @returns A new tree.
1657
- */
1658
- _createLike(iter = [], options) {
1659
- const Ctor = this.constructor;
1660
- return new Ctor(iter, { ...this._snapshotOptions(), ...(options ?? {}) });
1661
- }
1662
- /**
1663
- * (Protected) Converts a key, node, or entry into a standardized [node, value] tuple.
1664
- * @remarks Time O(1)
1665
- *
1666
- * @param keyNodeOrEntry - The input item.
1667
- * @param [value] - An optional value (used if input is just a key).
1668
- * @returns A tuple of [node, value].
1669
- */
1670
- _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value) {
1671
- if (keyNodeOrEntry === undefined)
1672
- return [undefined, undefined];
1673
- if (keyNodeOrEntry === null)
1674
- return [null, undefined];
1675
- if (this.isNode(keyNodeOrEntry))
1676
- return [keyNodeOrEntry, value];
1677
- if (this.isEntry(keyNodeOrEntry)) {
1678
- const [key, entryValue] = keyNodeOrEntry;
1679
- if (key === undefined)
1680
- return [undefined, undefined];
1681
- else if (key === null)
1682
- return [null, undefined];
1683
- const finalValue = value ?? entryValue;
1684
- return [this._createNode(key, finalValue), finalValue];
1685
- }
1686
- return [this._createNode(keyNodeOrEntry, value), value];
1687
- }
1688
- /**
1689
- * (Protected) Helper for cloning. Performs a BFS and adds all nodes to the new tree.
1690
- * @remarks Time O(N * M) (O(N) BFS + O(M) `add` for each node).
1691
- *
1692
- * @param cloned - The new, empty tree instance to populate.
1693
- */
1694
- _clone(cloned) {
1695
- // Use BFS with nulls to preserve the tree structure
1696
- this.bfs(node => {
1697
- if (node === null)
1698
- cloned.add(null);
1699
- else {
1700
- if (this._isMapMode)
1701
- cloned.add([node.key, this._store.get(node.key)]);
1702
- else
1703
- cloned.add([node.key, node.value]);
1704
- }
1705
- }, this._root, this.iterationType, true // Include nulls
1706
- );
1707
- if (this._isMapMode)
1708
- cloned._store = this._store;
1709
- }
1710
- /**
1711
- * (Protected) Recursive helper for `toVisual`.
1712
- * @remarks Time O(N), Space O(N*H) or O(N^2)
1713
- *
1714
- * @param node - The current node.
1715
- * @param options - Print options.
1716
- * @returns Layout information for this subtree.
1717
- */
1718
- _displayAux(node, options) {
1719
- const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
1720
- const emptyDisplayLayout = [['─'], 1, 0, 0]; // Represents an empty spot
1721
- if (node === null && !isShowNull) {
1722
- return emptyDisplayLayout;
1723
- }
1724
- else if (node === undefined && !isShowUndefined) {
1725
- return emptyDisplayLayout;
1726
- }
1727
- else if (this.isNIL(node) && !isShowRedBlackNIL) {
1728
- return emptyDisplayLayout;
1729
- }
1730
- else if (node !== null && node !== undefined) {
1731
- // Real node
1732
- const key = node.key, line = this.isNIL(node) ? 'S' : String(key), width = line.length;
1733
- return _buildNodeDisplay(line, width, this._displayAux(node.left, options), this._displayAux(node.right, options));
1734
- }
1735
- else {
1736
- // Null or Undefined
1737
- const line = node === undefined ? 'U' : 'N', width = line.length;
1738
- // Treat as a leaf
1739
- return _buildNodeDisplay(line, width, [[''], 1, 0, 0], [[''], 1, 0, 0]);
1740
- }
1741
- /**
1742
- * (Inner) Builds the display lines for a node.
1743
- * @remarks Time/Space: Proportional to the width and height of the subtrees.
1744
- */
1745
- function _buildNodeDisplay(line, width, left, right) {
1746
- const [leftLines, leftWidth, leftHeight, leftMiddle] = left;
1747
- const [rightLines, rightWidth, rightHeight, rightMiddle] = right;
1748
- const firstLine = ' '.repeat(Math.max(0, leftMiddle + 1)) +
1749
- '_'.repeat(Math.max(0, leftWidth - leftMiddle - 1)) +
1750
- line +
1751
- '_'.repeat(Math.max(0, rightMiddle)) +
1752
- ' '.repeat(Math.max(0, rightWidth - rightMiddle));
1753
- const secondLine = (leftHeight > 0
1754
- ? ' '.repeat(leftMiddle) + '/' + ' '.repeat(leftWidth - leftMiddle - 1)
1755
- : ' '.repeat(leftWidth)) +
1756
- ' '.repeat(width) +
1757
- (rightHeight > 0
1758
- ? ' '.repeat(rightMiddle) + '\\' + ' '.repeat(rightWidth - rightMiddle - 1)
1759
- : ' '.repeat(rightWidth));
1760
- const mergedLines = [firstLine, secondLine];
1761
- for (let i = 0; i < Math.max(leftHeight, rightHeight); i++) {
1762
- const leftLine = i < leftHeight ? leftLines[i] : ' '.repeat(leftWidth);
1763
- const rightLine = i < rightHeight ? rightLines[i] : ' '.repeat(rightWidth);
1764
- mergedLines.push(leftLine + ' '.repeat(width) + rightLine);
1765
- }
1766
- return [
1767
- mergedLines,
1768
- leftWidth + width + rightWidth,
1769
- Math.max(leftHeight, rightHeight) + 2,
1770
- leftWidth + Math.floor(width / 2)
1771
- ];
1772
- }
1773
- }
1774
- /**
1775
- * (Protected) Swaps the key/value properties of two nodes.
1776
- * @remarks Time O(1)
1777
- *
1778
- * @param srcNode - The source node.
1779
- * @param destNode - The destination node.
1780
- * @returns The `destNode` (now holding `srcNode`'s properties).
1781
- */
1782
- _swapProperties(srcNode, destNode) {
1783
- srcNode = this.ensureNode(srcNode);
1784
- destNode = this.ensureNode(destNode);
1785
- if (srcNode && destNode) {
1786
- const { key, value } = destNode;
1787
- const tempNode = this._createNode(key, value); // Use a temp node to hold dest properties
1788
- if (tempNode) {
1789
- // Copy src to dest
1790
- destNode.key = srcNode.key;
1791
- if (!this._isMapMode)
1792
- destNode.value = srcNode.value;
1793
- // Copy temp (original dest) to src
1794
- srcNode.key = tempNode.key;
1795
- if (!this._isMapMode)
1796
- srcNode.value = tempNode.value;
1797
- }
1798
- return destNode;
1799
- }
1800
- return undefined;
1801
- }
1802
- /**
1803
- * (Protected) Replaces a node in the tree with a new node, maintaining children and parent links.
1804
- * @remarks Time O(1)
1805
- *
1806
- * @param oldNode - The node to be replaced.
1807
- * @param newNode - The node to insert.
1808
- * @returns The `newNode`.
1809
- */
1810
- _replaceNode(oldNode, newNode) {
1811
- if (oldNode.parent) {
1812
- if (oldNode.parent.left === oldNode) {
1813
- oldNode.parent.left = newNode;
1814
- }
1815
- else if (oldNode.parent.right === oldNode) {
1816
- oldNode.parent.right = newNode;
1817
- }
1818
- }
1819
- newNode.left = oldNode.left;
1820
- newNode.right = oldNode.right;
1821
- newNode.parent = oldNode.parent;
1822
- if (this._root === oldNode) {
1823
- this._setRoot(newNode);
1824
- }
1825
- return newNode;
1826
- }
1827
- /**
1828
- * (Protected) Sets the root node and clears its parent reference.
1829
- * @remarks Time O(1)
1830
- *
1831
- * @param v - The node to set as root.
1832
- */
1833
- _setRoot(v) {
1834
- if (v) {
1835
- v.parent = undefined;
1836
- }
1837
- this._root = v;
1838
- }
1839
- _ensurePredicate(keyNodeEntryOrPredicate) {
1840
- if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === undefined)
1841
- return (node) => (node ? false : false);
1842
- if (this._isPredicate(keyNodeEntryOrPredicate))
1843
- return keyNodeEntryOrPredicate;
1844
- if (this.isRealNode(keyNodeEntryOrPredicate))
1845
- return (node) => node === keyNodeEntryOrPredicate;
1846
- if (this.isEntry(keyNodeEntryOrPredicate)) {
1847
- const [key] = keyNodeEntryOrPredicate;
1848
- return (node) => {
1849
- if (!node)
1850
- return false;
1851
- return node.key === key;
1852
- };
1853
- }
1854
- // Assume it's a key
1855
- return (node) => {
1856
- if (!node)
1857
- return false;
1858
- return node.key === keyNodeEntryOrPredicate;
1859
- };
1860
- }
1861
- /**
1862
- * (Protected) Checks if an item is a predicate function.
1863
- * @remarks Time O(1)
1864
- *
1865
- * @param p - The item to check.
1866
- * @returns True if it's a function.
1867
- */
1868
- _isPredicate(p) {
1869
- return typeof p === 'function';
1870
- }
1871
- /**
1872
- * (Protected) Extracts the key from a key, node, or entry.
1873
- * @remarks Time O(1)
1874
- *
1875
- * @param keyNodeOrEntry - The item.
1876
- * @returns The extracted key.
1877
- */
1878
- _extractKey(keyNodeOrEntry) {
1879
- if (keyNodeOrEntry === null)
1880
- return null;
1881
- if (keyNodeOrEntry === undefined)
1882
- return;
1883
- if (keyNodeOrEntry === this._NIL)
1884
- return;
1885
- if (this.isNode(keyNodeOrEntry))
1886
- return keyNodeOrEntry.key;
1887
- if (this.isEntry(keyNodeOrEntry))
1888
- return keyNodeOrEntry[0];
1889
- return keyNodeOrEntry;
1890
- }
1891
- /**
1892
- * (Protected) Sets a value in the external store (Map mode).
1893
- * @remarks Time O(1) (average for Map.set).
1894
- *
1895
- * @param key - The key.
1896
- * @param value - The value.
1897
- * @returns True if successful.
1898
- */
1899
- _setValue(key, value) {
1900
- if (key === null || key === undefined)
1901
- return false;
1902
- if (value === undefined)
1903
- return false; // Or allow setting undefined?
1904
- return this._store.set(key, value);
1905
- }
1906
- /**
1907
- * (Protected) Clears all nodes from the tree.
1908
- * @remarks Time O(1)
1909
- */
1910
- _clearNodes() {
1911
- this._setRoot(undefined);
1912
- this._size = 0;
1913
- }
1914
- /**
1915
- * (Protected) Clears all values from the external store.
1916
- * @remarks Time O(N)
1917
- */
1918
- _clearValues() {
1919
- this._store.clear();
1920
- }
1921
- }
1922
- //# sourceMappingURL=binary-tree.js.map