data-structure-typed 2.0.5 → 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 (785) hide show
  1. package/CHANGELOG.md +5 -1
  2. package/COMMANDS.md +27 -0
  3. package/README.md +5 -34
  4. package/benchmark/report.html +13 -77
  5. package/benchmark/report.json +152 -184
  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/types/data-structures/base/iterable-element-base.d.ts +219 -0
  15. package/dist/types/data-structures/base/iterable-entry-base.d.ts +144 -0
  16. package/dist/types/data-structures/base/linear-base.d.ts +335 -0
  17. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +182 -0
  18. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +135 -0
  19. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +291 -0
  20. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +754 -0
  21. package/dist/types/data-structures/binary-tree/bst.d.ts +413 -0
  22. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +208 -0
  23. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +190 -0
  24. package/dist/{esm → types}/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  25. package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -0
  26. package/dist/types/data-structures/graph/directed-graph.d.ts +207 -0
  27. package/dist/types/data-structures/graph/map-graph.d.ts +78 -0
  28. package/dist/types/data-structures/graph/undirected-graph.d.ts +188 -0
  29. package/dist/types/data-structures/hash/hash-map.d.ts +345 -0
  30. package/dist/types/data-structures/heap/heap.d.ts +503 -0
  31. package/dist/types/data-structures/heap/max-heap.d.ts +32 -0
  32. package/dist/types/data-structures/heap/min-heap.d.ts +33 -0
  33. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +769 -0
  34. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +451 -0
  35. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -0
  36. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -0
  37. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -0
  38. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -0
  39. package/dist/types/data-structures/queue/deque.d.ts +431 -0
  40. package/dist/types/data-structures/queue/queue.d.ts +308 -0
  41. package/dist/{cjs → types}/data-structures/stack/stack.d.ts +124 -102
  42. package/dist/types/data-structures/trie/trie.d.ts +350 -0
  43. package/dist/types/interfaces/binary-tree.d.ts +59 -0
  44. package/dist/types/interfaces/graph.d.ts +21 -0
  45. package/dist/{cjs → types}/types/data-structures/base/base.d.ts +1 -1
  46. package/dist/{esm → types}/types/data-structures/graph/abstract-graph.d.ts +4 -0
  47. package/dist/{cjs → types}/types/utils/utils.d.ts +1 -0
  48. package/dist/{esm → types}/utils/utils.d.ts +1 -1
  49. package/dist/umd/data-structure-typed.js +4693 -6484
  50. package/dist/umd/data-structure-typed.js.map +1 -0
  51. package/dist/umd/data-structure-typed.min.js +8 -6
  52. package/dist/umd/data-structure-typed.min.js.map +1 -1
  53. package/jest.integration.config.js +3 -0
  54. package/package.json +13 -12
  55. package/src/data-structures/base/iterable-element-base.ts +238 -115
  56. package/src/data-structures/base/iterable-entry-base.ts +96 -120
  57. package/src/data-structures/base/linear-base.ts +271 -277
  58. package/src/data-structures/binary-tree/avl-tree-counter.ts +196 -217
  59. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +188 -102
  60. package/src/data-structures/binary-tree/avl-tree.ts +237 -206
  61. package/src/data-structures/binary-tree/binary-tree.ts +665 -896
  62. package/src/data-structures/binary-tree/bst.ts +565 -572
  63. package/src/data-structures/binary-tree/red-black-tree.ts +157 -223
  64. package/src/data-structures/binary-tree/tree-counter.ts +195 -219
  65. package/src/data-structures/binary-tree/tree-multi-map.ts +127 -98
  66. package/src/data-structures/graph/abstract-graph.ts +339 -264
  67. package/src/data-structures/graph/directed-graph.ts +146 -236
  68. package/src/data-structures/graph/map-graph.ts +63 -60
  69. package/src/data-structures/graph/undirected-graph.ts +129 -152
  70. package/src/data-structures/hash/hash-map.ts +274 -496
  71. package/src/data-structures/heap/heap.ts +389 -402
  72. package/src/data-structures/heap/max-heap.ts +12 -76
  73. package/src/data-structures/heap/min-heap.ts +13 -76
  74. package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
  75. package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
  76. package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
  77. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
  78. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
  79. package/src/data-structures/priority-queue/priority-queue.ts +3 -92
  80. package/src/data-structures/queue/deque.ts +381 -357
  81. package/src/data-structures/queue/queue.ts +310 -264
  82. package/src/data-structures/stack/stack.ts +217 -131
  83. package/src/data-structures/trie/trie.ts +240 -175
  84. package/src/interfaces/binary-tree.ts +240 -6
  85. package/src/interfaces/graph.ts +37 -0
  86. package/src/types/data-structures/base/base.ts +5 -5
  87. package/src/types/data-structures/graph/abstract-graph.ts +5 -0
  88. package/src/types/utils/utils.ts +2 -0
  89. package/src/utils/utils.ts +9 -14
  90. package/test/integration/all-in-one.test.ts +1 -1
  91. package/test/integration/index.html +1 -1
  92. package/test/performance/benchmark-runner.ts +528 -0
  93. package/test/performance/data-structures/comparison/comparison.test.ts +27 -57
  94. package/test/performance/reportor.mjs +43 -43
  95. package/test/performance/runner-config.json +39 -0
  96. package/test/performance/single-suite-runner.ts +69 -0
  97. package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +3 -3
  98. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +5 -5
  99. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +4 -4
  100. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +350 -90
  101. package/test/unit/data-structures/binary-tree/bst.test.ts +12 -9
  102. package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +2 -2
  103. package/test/unit/data-structures/binary-tree/tree-counter.test.ts +25 -24
  104. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +112 -4
  105. package/test/unit/data-structures/graph/abstract-graph.test.ts +0 -4
  106. package/test/unit/data-structures/graph/directed-graph.test.ts +1 -1
  107. package/test/unit/data-structures/heap/heap.test.ts +14 -21
  108. package/test/unit/data-structures/heap/max-heap.test.ts +5 -9
  109. package/test/unit/data-structures/heap/min-heap.test.ts +1 -4
  110. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +14 -14
  111. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +0 -7
  112. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +8 -11
  113. package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +1 -4
  114. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +1 -4
  115. package/test/unit/data-structures/queue/queue.test.ts +4 -5
  116. package/test/unit/utils/utils.test.ts +0 -1
  117. package/tsconfig-base.json +20 -20
  118. package/tsconfig-types.json +17 -0
  119. package/tsconfig.test.json +8 -0
  120. package/tsup.config.js +11 -22
  121. package/tsup.node.config.ts +37 -0
  122. package/dist/cjs/common/index.js +0 -29
  123. package/dist/cjs/common/index.js.map +0 -1
  124. package/dist/cjs/data-structures/base/index.js +0 -19
  125. package/dist/cjs/data-structures/base/index.js.map +0 -1
  126. package/dist/cjs/data-structures/base/iterable-element-base.d.ts +0 -116
  127. package/dist/cjs/data-structures/base/iterable-element-base.js +0 -202
  128. package/dist/cjs/data-structures/base/iterable-element-base.js.map +0 -1
  129. package/dist/cjs/data-structures/base/iterable-entry-base.d.ts +0 -168
  130. package/dist/cjs/data-structures/base/iterable-entry-base.js +0 -241
  131. package/dist/cjs/data-structures/base/iterable-entry-base.js.map +0 -1
  132. package/dist/cjs/data-structures/base/linear-base.d.ts +0 -277
  133. package/dist/cjs/data-structures/base/linear-base.js +0 -553
  134. package/dist/cjs/data-structures/base/linear-base.js.map +0 -1
  135. package/dist/cjs/data-structures/binary-tree/avl-tree-counter.d.ts +0 -214
  136. package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js +0 -409
  137. package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js.map +0 -1
  138. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -104
  139. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +0 -203
  140. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +0 -1
  141. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +0 -302
  142. package/dist/cjs/data-structures/binary-tree/avl-tree.js +0 -599
  143. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +0 -1
  144. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +0 -295
  145. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +0 -1
  146. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +0 -910
  147. package/dist/cjs/data-structures/binary-tree/binary-tree.js +0 -2197
  148. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +0 -1
  149. package/dist/cjs/data-structures/binary-tree/bst.d.ts +0 -461
  150. package/dist/cjs/data-structures/binary-tree/bst.js +0 -880
  151. package/dist/cjs/data-structures/binary-tree/bst.js.map +0 -1
  152. package/dist/cjs/data-structures/binary-tree/index.js +0 -27
  153. package/dist/cjs/data-structures/binary-tree/index.js.map +0 -1
  154. package/dist/cjs/data-structures/binary-tree/red-black-tree.d.ts +0 -280
  155. package/dist/cjs/data-structures/binary-tree/red-black-tree.js +0 -642
  156. package/dist/cjs/data-structures/binary-tree/red-black-tree.js.map +0 -1
  157. package/dist/cjs/data-structures/binary-tree/segment-tree.js +0 -298
  158. package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +0 -1
  159. package/dist/cjs/data-structures/binary-tree/tree-counter.d.ts +0 -212
  160. package/dist/cjs/data-structures/binary-tree/tree-counter.js +0 -445
  161. package/dist/cjs/data-structures/binary-tree/tree-counter.js.map +0 -1
  162. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +0 -267
  163. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +0 -365
  164. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +0 -1
  165. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +0 -335
  166. package/dist/cjs/data-structures/graph/abstract-graph.js +0 -867
  167. package/dist/cjs/data-structures/graph/abstract-graph.js.map +0 -1
  168. package/dist/cjs/data-structures/graph/directed-graph.d.ts +0 -323
  169. package/dist/cjs/data-structures/graph/directed-graph.js +0 -613
  170. package/dist/cjs/data-structures/graph/directed-graph.js.map +0 -1
  171. package/dist/cjs/data-structures/graph/index.js +0 -21
  172. package/dist/cjs/data-structures/graph/index.js.map +0 -1
  173. package/dist/cjs/data-structures/graph/map-graph.d.ts +0 -84
  174. package/dist/cjs/data-structures/graph/map-graph.js +0 -111
  175. package/dist/cjs/data-structures/graph/map-graph.js.map +0 -1
  176. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +0 -231
  177. package/dist/cjs/data-structures/graph/undirected-graph.js +0 -445
  178. package/dist/cjs/data-structures/graph/undirected-graph.js.map +0 -1
  179. package/dist/cjs/data-structures/hash/hash-map.d.ts +0 -519
  180. package/dist/cjs/data-structures/hash/hash-map.js +0 -880
  181. package/dist/cjs/data-structures/hash/hash-map.js.map +0 -1
  182. package/dist/cjs/data-structures/hash/index.js +0 -18
  183. package/dist/cjs/data-structures/hash/index.js.map +0 -1
  184. package/dist/cjs/data-structures/heap/heap.d.ts +0 -578
  185. package/dist/cjs/data-structures/heap/heap.js +0 -911
  186. package/dist/cjs/data-structures/heap/heap.js.map +0 -1
  187. package/dist/cjs/data-structures/heap/index.js +0 -20
  188. package/dist/cjs/data-structures/heap/index.js.map +0 -1
  189. package/dist/cjs/data-structures/heap/max-heap.d.ts +0 -68
  190. package/dist/cjs/data-structures/heap/max-heap.js +0 -96
  191. package/dist/cjs/data-structures/heap/max-heap.js.map +0 -1
  192. package/dist/cjs/data-structures/heap/min-heap.d.ts +0 -68
  193. package/dist/cjs/data-structures/heap/min-heap.js +0 -87
  194. package/dist/cjs/data-structures/heap/min-heap.js.map +0 -1
  195. package/dist/cjs/data-structures/index.js +0 -29
  196. package/dist/cjs/data-structures/index.js.map +0 -1
  197. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +0 -885
  198. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +0 -1238
  199. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +0 -1
  200. package/dist/cjs/data-structures/linked-list/index.js +0 -20
  201. package/dist/cjs/data-structures/linked-list/index.js.map +0 -1
  202. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +0 -500
  203. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +0 -870
  204. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +0 -1
  205. package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +0 -134
  206. package/dist/cjs/data-structures/linked-list/skip-linked-list.js +0 -245
  207. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +0 -1
  208. package/dist/cjs/data-structures/matrix/index.js +0 -19
  209. package/dist/cjs/data-structures/matrix/index.js.map +0 -1
  210. package/dist/cjs/data-structures/matrix/matrix.js +0 -449
  211. package/dist/cjs/data-structures/matrix/matrix.js.map +0 -1
  212. package/dist/cjs/data-structures/matrix/navigator.js +0 -112
  213. package/dist/cjs/data-structures/matrix/navigator.js.map +0 -1
  214. package/dist/cjs/data-structures/priority-queue/index.js +0 -20
  215. package/dist/cjs/data-structures/priority-queue/index.js.map +0 -1
  216. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +0 -71
  217. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +0 -102
  218. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +0 -1
  219. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +0 -72
  220. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +0 -94
  221. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +0 -1
  222. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +0 -74
  223. package/dist/cjs/data-structures/priority-queue/priority-queue.js +0 -96
  224. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +0 -1
  225. package/dist/cjs/data-structures/queue/deque.d.ts +0 -458
  226. package/dist/cjs/data-structures/queue/deque.js +0 -919
  227. package/dist/cjs/data-structures/queue/deque.js.map +0 -1
  228. package/dist/cjs/data-structures/queue/index.js +0 -19
  229. package/dist/cjs/data-structures/queue/index.js.map +0 -1
  230. package/dist/cjs/data-structures/queue/queue.d.ts +0 -329
  231. package/dist/cjs/data-structures/queue/queue.js +0 -457
  232. package/dist/cjs/data-structures/queue/queue.js.map +0 -1
  233. package/dist/cjs/data-structures/stack/index.js +0 -18
  234. package/dist/cjs/data-structures/stack/index.js.map +0 -1
  235. package/dist/cjs/data-structures/stack/stack.js +0 -346
  236. package/dist/cjs/data-structures/stack/stack.js.map +0 -1
  237. package/dist/cjs/data-structures/tree/index.js +0 -18
  238. package/dist/cjs/data-structures/tree/index.js.map +0 -1
  239. package/dist/cjs/data-structures/tree/tree.js +0 -108
  240. package/dist/cjs/data-structures/tree/tree.js.map +0 -1
  241. package/dist/cjs/data-structures/trie/index.js +0 -18
  242. package/dist/cjs/data-structures/trie/index.js.map +0 -1
  243. package/dist/cjs/data-structures/trie/trie.d.ts +0 -351
  244. package/dist/cjs/data-structures/trie/trie.js +0 -594
  245. package/dist/cjs/data-structures/trie/trie.js.map +0 -1
  246. package/dist/cjs/index.js +0 -22
  247. package/dist/cjs/index.js.map +0 -1
  248. package/dist/cjs/interfaces/binary-tree.d.ts +0 -9
  249. package/dist/cjs/interfaces/binary-tree.js +0 -3
  250. package/dist/cjs/interfaces/binary-tree.js.map +0 -1
  251. package/dist/cjs/interfaces/doubly-linked-list.js +0 -3
  252. package/dist/cjs/interfaces/doubly-linked-list.js.map +0 -1
  253. package/dist/cjs/interfaces/graph.d.ts +0 -5
  254. package/dist/cjs/interfaces/graph.js +0 -3
  255. package/dist/cjs/interfaces/graph.js.map +0 -1
  256. package/dist/cjs/interfaces/heap.js +0 -3
  257. package/dist/cjs/interfaces/heap.js.map +0 -1
  258. package/dist/cjs/interfaces/index.js +0 -25
  259. package/dist/cjs/interfaces/index.js.map +0 -1
  260. package/dist/cjs/interfaces/navigator.js +0 -3
  261. package/dist/cjs/interfaces/navigator.js.map +0 -1
  262. package/dist/cjs/interfaces/priority-queue.js +0 -3
  263. package/dist/cjs/interfaces/priority-queue.js.map +0 -1
  264. package/dist/cjs/interfaces/segment-tree.js +0 -3
  265. package/dist/cjs/interfaces/segment-tree.js.map +0 -1
  266. package/dist/cjs/interfaces/singly-linked-list.js +0 -3
  267. package/dist/cjs/interfaces/singly-linked-list.js.map +0 -1
  268. package/dist/cjs/types/common.js +0 -3
  269. package/dist/cjs/types/common.js.map +0 -1
  270. package/dist/cjs/types/data-structures/base/base.js +0 -3
  271. package/dist/cjs/types/data-structures/base/base.js.map +0 -1
  272. package/dist/cjs/types/data-structures/base/index.js +0 -18
  273. package/dist/cjs/types/data-structures/base/index.js.map +0 -1
  274. package/dist/cjs/types/data-structures/binary-tree/avl-tree-counter.js +0 -3
  275. package/dist/cjs/types/data-structures/binary-tree/avl-tree-counter.js.map +0 -1
  276. package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -3
  277. package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.js.map +0 -1
  278. package/dist/cjs/types/data-structures/binary-tree/avl-tree.js +0 -3
  279. package/dist/cjs/types/data-structures/binary-tree/avl-tree.js.map +0 -1
  280. package/dist/cjs/types/data-structures/binary-tree/binary-indexed-tree.js +0 -3
  281. package/dist/cjs/types/data-structures/binary-tree/binary-indexed-tree.js.map +0 -1
  282. package/dist/cjs/types/data-structures/binary-tree/binary-tree.js +0 -3
  283. package/dist/cjs/types/data-structures/binary-tree/binary-tree.js.map +0 -1
  284. package/dist/cjs/types/data-structures/binary-tree/bst.js +0 -3
  285. package/dist/cjs/types/data-structures/binary-tree/bst.js.map +0 -1
  286. package/dist/cjs/types/data-structures/binary-tree/index.js +0 -26
  287. package/dist/cjs/types/data-structures/binary-tree/index.js.map +0 -1
  288. package/dist/cjs/types/data-structures/binary-tree/red-black-tree.js +0 -3
  289. package/dist/cjs/types/data-structures/binary-tree/red-black-tree.js.map +0 -1
  290. package/dist/cjs/types/data-structures/binary-tree/segment-tree.js +0 -3
  291. package/dist/cjs/types/data-structures/binary-tree/segment-tree.js.map +0 -1
  292. package/dist/cjs/types/data-structures/binary-tree/tree-counter.js +0 -3
  293. package/dist/cjs/types/data-structures/binary-tree/tree-counter.js.map +0 -1
  294. package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.js +0 -3
  295. package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.js.map +0 -1
  296. package/dist/cjs/types/data-structures/graph/abstract-graph.d.ts +0 -10
  297. package/dist/cjs/types/data-structures/graph/abstract-graph.js +0 -3
  298. package/dist/cjs/types/data-structures/graph/abstract-graph.js.map +0 -1
  299. package/dist/cjs/types/data-structures/graph/directed-graph.js +0 -3
  300. package/dist/cjs/types/data-structures/graph/directed-graph.js.map +0 -1
  301. package/dist/cjs/types/data-structures/graph/index.js +0 -20
  302. package/dist/cjs/types/data-structures/graph/index.js.map +0 -1
  303. package/dist/cjs/types/data-structures/graph/map-graph.js +0 -3
  304. package/dist/cjs/types/data-structures/graph/map-graph.js.map +0 -1
  305. package/dist/cjs/types/data-structures/graph/undirected-graph.js +0 -3
  306. package/dist/cjs/types/data-structures/graph/undirected-graph.js.map +0 -1
  307. package/dist/cjs/types/data-structures/hash/hash-map.js +0 -3
  308. package/dist/cjs/types/data-structures/hash/hash-map.js.map +0 -1
  309. package/dist/cjs/types/data-structures/hash/index.js +0 -18
  310. package/dist/cjs/types/data-structures/hash/index.js.map +0 -1
  311. package/dist/cjs/types/data-structures/heap/heap.js +0 -3
  312. package/dist/cjs/types/data-structures/heap/heap.js.map +0 -1
  313. package/dist/cjs/types/data-structures/heap/index.js +0 -18
  314. package/dist/cjs/types/data-structures/heap/index.js.map +0 -1
  315. package/dist/cjs/types/data-structures/heap/max-heap.js +0 -3
  316. package/dist/cjs/types/data-structures/heap/max-heap.js.map +0 -1
  317. package/dist/cjs/types/data-structures/heap/min-heap.js +0 -3
  318. package/dist/cjs/types/data-structures/heap/min-heap.js.map +0 -1
  319. package/dist/cjs/types/data-structures/index.js +0 -29
  320. package/dist/cjs/types/data-structures/index.js.map +0 -1
  321. package/dist/cjs/types/data-structures/linked-list/doubly-linked-list.js +0 -3
  322. package/dist/cjs/types/data-structures/linked-list/doubly-linked-list.js.map +0 -1
  323. package/dist/cjs/types/data-structures/linked-list/index.js +0 -20
  324. package/dist/cjs/types/data-structures/linked-list/index.js.map +0 -1
  325. package/dist/cjs/types/data-structures/linked-list/singly-linked-list.js +0 -3
  326. package/dist/cjs/types/data-structures/linked-list/singly-linked-list.js.map +0 -1
  327. package/dist/cjs/types/data-structures/linked-list/skip-linked-list.js +0 -3
  328. package/dist/cjs/types/data-structures/linked-list/skip-linked-list.js.map +0 -1
  329. package/dist/cjs/types/data-structures/matrix/index.js +0 -19
  330. package/dist/cjs/types/data-structures/matrix/index.js.map +0 -1
  331. package/dist/cjs/types/data-structures/matrix/matrix.js +0 -3
  332. package/dist/cjs/types/data-structures/matrix/matrix.js.map +0 -1
  333. package/dist/cjs/types/data-structures/matrix/navigator.js +0 -3
  334. package/dist/cjs/types/data-structures/matrix/navigator.js.map +0 -1
  335. package/dist/cjs/types/data-structures/priority-queue/index.js +0 -20
  336. package/dist/cjs/types/data-structures/priority-queue/index.js.map +0 -1
  337. package/dist/cjs/types/data-structures/priority-queue/max-priority-queue.js +0 -3
  338. package/dist/cjs/types/data-structures/priority-queue/max-priority-queue.js.map +0 -1
  339. package/dist/cjs/types/data-structures/priority-queue/min-priority-queue.js +0 -3
  340. package/dist/cjs/types/data-structures/priority-queue/min-priority-queue.js.map +0 -1
  341. package/dist/cjs/types/data-structures/priority-queue/priority-queue.js +0 -3
  342. package/dist/cjs/types/data-structures/priority-queue/priority-queue.js.map +0 -1
  343. package/dist/cjs/types/data-structures/queue/deque.js +0 -3
  344. package/dist/cjs/types/data-structures/queue/deque.js.map +0 -1
  345. package/dist/cjs/types/data-structures/queue/index.js +0 -19
  346. package/dist/cjs/types/data-structures/queue/index.js.map +0 -1
  347. package/dist/cjs/types/data-structures/queue/queue.js +0 -3
  348. package/dist/cjs/types/data-structures/queue/queue.js.map +0 -1
  349. package/dist/cjs/types/data-structures/stack/index.js +0 -18
  350. package/dist/cjs/types/data-structures/stack/index.js.map +0 -1
  351. package/dist/cjs/types/data-structures/stack/stack.js +0 -3
  352. package/dist/cjs/types/data-structures/stack/stack.js.map +0 -1
  353. package/dist/cjs/types/data-structures/tree/index.js +0 -18
  354. package/dist/cjs/types/data-structures/tree/index.js.map +0 -1
  355. package/dist/cjs/types/data-structures/tree/tree.js +0 -3
  356. package/dist/cjs/types/data-structures/tree/tree.js.map +0 -1
  357. package/dist/cjs/types/data-structures/trie/index.js +0 -18
  358. package/dist/cjs/types/data-structures/trie/index.js.map +0 -1
  359. package/dist/cjs/types/data-structures/trie/trie.js +0 -3
  360. package/dist/cjs/types/data-structures/trie/trie.js.map +0 -1
  361. package/dist/cjs/types/index.js +0 -20
  362. package/dist/cjs/types/index.js.map +0 -1
  363. package/dist/cjs/types/utils/index.js +0 -19
  364. package/dist/cjs/types/utils/index.js.map +0 -1
  365. package/dist/cjs/types/utils/utils.js +0 -3
  366. package/dist/cjs/types/utils/utils.js.map +0 -1
  367. package/dist/cjs/types/utils/validate-type.js +0 -3
  368. package/dist/cjs/types/utils/validate-type.js.map +0 -1
  369. package/dist/cjs/utils/index.js +0 -19
  370. package/dist/cjs/utils/index.js.map +0 -1
  371. package/dist/cjs/utils/number.js +0 -24
  372. package/dist/cjs/utils/number.js.map +0 -1
  373. package/dist/cjs/utils/utils.d.ts +0 -209
  374. package/dist/cjs/utils/utils.js +0 -353
  375. package/dist/cjs/utils/utils.js.map +0 -1
  376. package/dist/esm/common/index.d.ts +0 -12
  377. package/dist/esm/common/index.js +0 -29
  378. package/dist/esm/common/index.js.map +0 -1
  379. package/dist/esm/data-structures/base/index.d.ts +0 -2
  380. package/dist/esm/data-structures/base/index.js +0 -3
  381. package/dist/esm/data-structures/base/index.js.map +0 -1
  382. package/dist/esm/data-structures/base/iterable-element-base.d.ts +0 -116
  383. package/dist/esm/data-structures/base/iterable-element-base.js +0 -199
  384. package/dist/esm/data-structures/base/iterable-element-base.js.map +0 -1
  385. package/dist/esm/data-structures/base/iterable-entry-base.d.ts +0 -168
  386. package/dist/esm/data-structures/base/iterable-entry-base.js +0 -237
  387. package/dist/esm/data-structures/base/iterable-entry-base.js.map +0 -1
  388. package/dist/esm/data-structures/base/linear-base.d.ts +0 -277
  389. package/dist/esm/data-structures/base/linear-base.js +0 -549
  390. package/dist/esm/data-structures/base/linear-base.js.map +0 -1
  391. package/dist/esm/data-structures/binary-tree/avl-tree-counter.d.ts +0 -214
  392. package/dist/esm/data-structures/binary-tree/avl-tree-counter.js +0 -410
  393. package/dist/esm/data-structures/binary-tree/avl-tree-counter.js.map +0 -1
  394. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -104
  395. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js +0 -205
  396. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js.map +0 -1
  397. package/dist/esm/data-structures/binary-tree/avl-tree.d.ts +0 -302
  398. package/dist/esm/data-structures/binary-tree/avl-tree.js +0 -601
  399. package/dist/esm/data-structures/binary-tree/avl-tree.js.map +0 -1
  400. package/dist/esm/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -174
  401. package/dist/esm/data-structures/binary-tree/binary-indexed-tree.js +0 -296
  402. package/dist/esm/data-structures/binary-tree/binary-indexed-tree.js.map +0 -1
  403. package/dist/esm/data-structures/binary-tree/binary-tree.d.ts +0 -910
  404. package/dist/esm/data-structures/binary-tree/binary-tree.js +0 -2198
  405. package/dist/esm/data-structures/binary-tree/binary-tree.js.map +0 -1
  406. package/dist/esm/data-structures/binary-tree/bst.d.ts +0 -461
  407. package/dist/esm/data-structures/binary-tree/bst.js +0 -881
  408. package/dist/esm/data-structures/binary-tree/bst.js.map +0 -1
  409. package/dist/esm/data-structures/binary-tree/index.d.ts +0 -10
  410. package/dist/esm/data-structures/binary-tree/index.js +0 -11
  411. package/dist/esm/data-structures/binary-tree/index.js.map +0 -1
  412. package/dist/esm/data-structures/binary-tree/red-black-tree.d.ts +0 -280
  413. package/dist/esm/data-structures/binary-tree/red-black-tree.js +0 -641
  414. package/dist/esm/data-structures/binary-tree/red-black-tree.js.map +0 -1
  415. package/dist/esm/data-structures/binary-tree/segment-tree.d.ts +0 -160
  416. package/dist/esm/data-structures/binary-tree/segment-tree.js +0 -295
  417. package/dist/esm/data-structures/binary-tree/segment-tree.js.map +0 -1
  418. package/dist/esm/data-structures/binary-tree/tree-counter.d.ts +0 -212
  419. package/dist/esm/data-structures/binary-tree/tree-counter.js +0 -446
  420. package/dist/esm/data-structures/binary-tree/tree-counter.js.map +0 -1
  421. package/dist/esm/data-structures/binary-tree/tree-multi-map.js +0 -367
  422. package/dist/esm/data-structures/binary-tree/tree-multi-map.js.map +0 -1
  423. package/dist/esm/data-structures/graph/abstract-graph.d.ts +0 -335
  424. package/dist/esm/data-structures/graph/abstract-graph.js +0 -862
  425. package/dist/esm/data-structures/graph/abstract-graph.js.map +0 -1
  426. package/dist/esm/data-structures/graph/directed-graph.d.ts +0 -323
  427. package/dist/esm/data-structures/graph/directed-graph.js +0 -609
  428. package/dist/esm/data-structures/graph/directed-graph.js.map +0 -1
  429. package/dist/esm/data-structures/graph/index.d.ts +0 -4
  430. package/dist/esm/data-structures/graph/index.js +0 -5
  431. package/dist/esm/data-structures/graph/index.js.map +0 -1
  432. package/dist/esm/data-structures/graph/map-graph.d.ts +0 -84
  433. package/dist/esm/data-structures/graph/map-graph.js +0 -108
  434. package/dist/esm/data-structures/graph/map-graph.js.map +0 -1
  435. package/dist/esm/data-structures/graph/undirected-graph.d.ts +0 -231
  436. package/dist/esm/data-structures/graph/undirected-graph.js +0 -439
  437. package/dist/esm/data-structures/graph/undirected-graph.js.map +0 -1
  438. package/dist/esm/data-structures/hash/hash-map.d.ts +0 -519
  439. package/dist/esm/data-structures/hash/hash-map.js +0 -878
  440. package/dist/esm/data-structures/hash/hash-map.js.map +0 -1
  441. package/dist/esm/data-structures/hash/index.d.ts +0 -1
  442. package/dist/esm/data-structures/hash/index.js +0 -2
  443. package/dist/esm/data-structures/hash/index.js.map +0 -1
  444. package/dist/esm/data-structures/heap/heap.d.ts +0 -578
  445. package/dist/esm/data-structures/heap/heap.js +0 -914
  446. package/dist/esm/data-structures/heap/heap.js.map +0 -1
  447. package/dist/esm/data-structures/heap/index.d.ts +0 -3
  448. package/dist/esm/data-structures/heap/index.js +0 -4
  449. package/dist/esm/data-structures/heap/index.js.map +0 -1
  450. package/dist/esm/data-structures/heap/max-heap.d.ts +0 -68
  451. package/dist/esm/data-structures/heap/max-heap.js +0 -95
  452. package/dist/esm/data-structures/heap/max-heap.js.map +0 -1
  453. package/dist/esm/data-structures/heap/min-heap.d.ts +0 -68
  454. package/dist/esm/data-structures/heap/min-heap.js +0 -83
  455. package/dist/esm/data-structures/heap/min-heap.js.map +0 -1
  456. package/dist/esm/data-structures/index.d.ts +0 -12
  457. package/dist/esm/data-structures/index.js +0 -13
  458. package/dist/esm/data-structures/index.js.map +0 -1
  459. package/dist/esm/data-structures/linked-list/doubly-linked-list.d.ts +0 -885
  460. package/dist/esm/data-structures/linked-list/doubly-linked-list.js +0 -1236
  461. package/dist/esm/data-structures/linked-list/doubly-linked-list.js.map +0 -1
  462. package/dist/esm/data-structures/linked-list/index.d.ts +0 -3
  463. package/dist/esm/data-structures/linked-list/index.js +0 -4
  464. package/dist/esm/data-structures/linked-list/index.js.map +0 -1
  465. package/dist/esm/data-structures/linked-list/singly-linked-list.d.ts +0 -500
  466. package/dist/esm/data-structures/linked-list/singly-linked-list.js +0 -866
  467. package/dist/esm/data-structures/linked-list/singly-linked-list.js.map +0 -1
  468. package/dist/esm/data-structures/linked-list/skip-linked-list.d.ts +0 -134
  469. package/dist/esm/data-structures/linked-list/skip-linked-list.js +0 -243
  470. package/dist/esm/data-structures/linked-list/skip-linked-list.js.map +0 -1
  471. package/dist/esm/data-structures/matrix/index.d.ts +0 -2
  472. package/dist/esm/data-structures/matrix/index.js +0 -3
  473. package/dist/esm/data-structures/matrix/index.js.map +0 -1
  474. package/dist/esm/data-structures/matrix/matrix.d.ts +0 -168
  475. package/dist/esm/data-structures/matrix/matrix.js +0 -444
  476. package/dist/esm/data-structures/matrix/matrix.js.map +0 -1
  477. package/dist/esm/data-structures/matrix/navigator.d.ts +0 -55
  478. package/dist/esm/data-structures/matrix/navigator.js +0 -114
  479. package/dist/esm/data-structures/matrix/navigator.js.map +0 -1
  480. package/dist/esm/data-structures/priority-queue/index.d.ts +0 -3
  481. package/dist/esm/data-structures/priority-queue/index.js +0 -4
  482. package/dist/esm/data-structures/priority-queue/index.js.map +0 -1
  483. package/dist/esm/data-structures/priority-queue/max-priority-queue.d.ts +0 -71
  484. package/dist/esm/data-structures/priority-queue/max-priority-queue.js +0 -101
  485. package/dist/esm/data-structures/priority-queue/max-priority-queue.js.map +0 -1
  486. package/dist/esm/data-structures/priority-queue/min-priority-queue.d.ts +0 -72
  487. package/dist/esm/data-structures/priority-queue/min-priority-queue.js +0 -90
  488. package/dist/esm/data-structures/priority-queue/min-priority-queue.js.map +0 -1
  489. package/dist/esm/data-structures/priority-queue/priority-queue.d.ts +0 -74
  490. package/dist/esm/data-structures/priority-queue/priority-queue.js +0 -92
  491. package/dist/esm/data-structures/priority-queue/priority-queue.js.map +0 -1
  492. package/dist/esm/data-structures/queue/deque.d.ts +0 -458
  493. package/dist/esm/data-structures/queue/deque.js +0 -915
  494. package/dist/esm/data-structures/queue/deque.js.map +0 -1
  495. package/dist/esm/data-structures/queue/index.d.ts +0 -2
  496. package/dist/esm/data-structures/queue/index.js +0 -3
  497. package/dist/esm/data-structures/queue/index.js.map +0 -1
  498. package/dist/esm/data-structures/queue/queue.d.ts +0 -329
  499. package/dist/esm/data-structures/queue/queue.js +0 -452
  500. package/dist/esm/data-structures/queue/queue.js.map +0 -1
  501. package/dist/esm/data-structures/stack/index.d.ts +0 -1
  502. package/dist/esm/data-structures/stack/index.js +0 -2
  503. package/dist/esm/data-structures/stack/index.js.map +0 -1
  504. package/dist/esm/data-structures/stack/stack.d.ts +0 -284
  505. package/dist/esm/data-structures/stack/stack.js +0 -342
  506. package/dist/esm/data-structures/stack/stack.js.map +0 -1
  507. package/dist/esm/data-structures/tree/index.d.ts +0 -1
  508. package/dist/esm/data-structures/tree/index.js +0 -2
  509. package/dist/esm/data-structures/tree/index.js.map +0 -1
  510. package/dist/esm/data-structures/tree/tree.d.ts +0 -62
  511. package/dist/esm/data-structures/tree/tree.js +0 -107
  512. package/dist/esm/data-structures/tree/tree.js.map +0 -1
  513. package/dist/esm/data-structures/trie/index.d.ts +0 -1
  514. package/dist/esm/data-structures/trie/index.js +0 -2
  515. package/dist/esm/data-structures/trie/index.js.map +0 -1
  516. package/dist/esm/data-structures/trie/trie.d.ts +0 -351
  517. package/dist/esm/data-structures/trie/trie.js +0 -592
  518. package/dist/esm/data-structures/trie/trie.js.map +0 -1
  519. package/dist/esm/index.d.ts +0 -5
  520. package/dist/esm/index.js +0 -6
  521. package/dist/esm/index.js.map +0 -1
  522. package/dist/esm/interfaces/binary-tree.d.ts +0 -9
  523. package/dist/esm/interfaces/binary-tree.js +0 -2
  524. package/dist/esm/interfaces/binary-tree.js.map +0 -1
  525. package/dist/esm/interfaces/doubly-linked-list.d.ts +0 -1
  526. package/dist/esm/interfaces/doubly-linked-list.js +0 -2
  527. package/dist/esm/interfaces/doubly-linked-list.js.map +0 -1
  528. package/dist/esm/interfaces/graph.d.ts +0 -5
  529. package/dist/esm/interfaces/graph.js +0 -2
  530. package/dist/esm/interfaces/graph.js.map +0 -1
  531. package/dist/esm/interfaces/heap.d.ts +0 -1
  532. package/dist/esm/interfaces/heap.js +0 -2
  533. package/dist/esm/interfaces/heap.js.map +0 -1
  534. package/dist/esm/interfaces/index.d.ts +0 -8
  535. package/dist/esm/interfaces/index.js +0 -9
  536. package/dist/esm/interfaces/index.js.map +0 -1
  537. package/dist/esm/interfaces/navigator.d.ts +0 -1
  538. package/dist/esm/interfaces/navigator.js +0 -2
  539. package/dist/esm/interfaces/navigator.js.map +0 -1
  540. package/dist/esm/interfaces/priority-queue.d.ts +0 -1
  541. package/dist/esm/interfaces/priority-queue.js +0 -2
  542. package/dist/esm/interfaces/priority-queue.js.map +0 -1
  543. package/dist/esm/interfaces/segment-tree.d.ts +0 -1
  544. package/dist/esm/interfaces/segment-tree.js +0 -2
  545. package/dist/esm/interfaces/segment-tree.js.map +0 -1
  546. package/dist/esm/interfaces/singly-linked-list.d.ts +0 -1
  547. package/dist/esm/interfaces/singly-linked-list.js +0 -2
  548. package/dist/esm/interfaces/singly-linked-list.js.map +0 -1
  549. package/dist/esm/types/common.d.ts +0 -15
  550. package/dist/esm/types/common.js +0 -2
  551. package/dist/esm/types/common.js.map +0 -1
  552. package/dist/esm/types/data-structures/base/base.d.ts +0 -13
  553. package/dist/esm/types/data-structures/base/base.js +0 -2
  554. package/dist/esm/types/data-structures/base/base.js.map +0 -1
  555. package/dist/esm/types/data-structures/base/index.d.ts +0 -1
  556. package/dist/esm/types/data-structures/base/index.js +0 -2
  557. package/dist/esm/types/data-structures/base/index.js.map +0 -1
  558. package/dist/esm/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -2
  559. package/dist/esm/types/data-structures/binary-tree/avl-tree-counter.js +0 -2
  560. package/dist/esm/types/data-structures/binary-tree/avl-tree-counter.js.map +0 -1
  561. package/dist/esm/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -2
  562. package/dist/esm/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -2
  563. package/dist/esm/types/data-structures/binary-tree/avl-tree-multi-map.js.map +0 -1
  564. package/dist/esm/types/data-structures/binary-tree/avl-tree.d.ts +0 -2
  565. package/dist/esm/types/data-structures/binary-tree/avl-tree.js +0 -2
  566. package/dist/esm/types/data-structures/binary-tree/avl-tree.js.map +0 -1
  567. package/dist/esm/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -1
  568. package/dist/esm/types/data-structures/binary-tree/binary-indexed-tree.js +0 -2
  569. package/dist/esm/types/data-structures/binary-tree/binary-indexed-tree.js.map +0 -1
  570. package/dist/esm/types/data-structures/binary-tree/binary-tree.d.ts +0 -29
  571. package/dist/esm/types/data-structures/binary-tree/binary-tree.js +0 -2
  572. package/dist/esm/types/data-structures/binary-tree/binary-tree.js.map +0 -1
  573. package/dist/esm/types/data-structures/binary-tree/bst.d.ts +0 -12
  574. package/dist/esm/types/data-structures/binary-tree/bst.js +0 -2
  575. package/dist/esm/types/data-structures/binary-tree/bst.js.map +0 -1
  576. package/dist/esm/types/data-structures/binary-tree/index.d.ts +0 -9
  577. package/dist/esm/types/data-structures/binary-tree/index.js +0 -10
  578. package/dist/esm/types/data-structures/binary-tree/index.js.map +0 -1
  579. package/dist/esm/types/data-structures/binary-tree/red-black-tree.d.ts +0 -3
  580. package/dist/esm/types/data-structures/binary-tree/red-black-tree.js +0 -2
  581. package/dist/esm/types/data-structures/binary-tree/red-black-tree.js.map +0 -1
  582. package/dist/esm/types/data-structures/binary-tree/segment-tree.d.ts +0 -1
  583. package/dist/esm/types/data-structures/binary-tree/segment-tree.js +0 -2
  584. package/dist/esm/types/data-structures/binary-tree/segment-tree.js.map +0 -1
  585. package/dist/esm/types/data-structures/binary-tree/tree-counter.d.ts +0 -2
  586. package/dist/esm/types/data-structures/binary-tree/tree-counter.js +0 -2
  587. package/dist/esm/types/data-structures/binary-tree/tree-counter.js.map +0 -1
  588. package/dist/esm/types/data-structures/binary-tree/tree-multi-map.d.ts +0 -2
  589. package/dist/esm/types/data-structures/binary-tree/tree-multi-map.js +0 -2
  590. package/dist/esm/types/data-structures/binary-tree/tree-multi-map.js.map +0 -1
  591. package/dist/esm/types/data-structures/graph/abstract-graph.js +0 -2
  592. package/dist/esm/types/data-structures/graph/abstract-graph.js.map +0 -1
  593. package/dist/esm/types/data-structures/graph/directed-graph.d.ts +0 -1
  594. package/dist/esm/types/data-structures/graph/directed-graph.js +0 -2
  595. package/dist/esm/types/data-structures/graph/directed-graph.js.map +0 -1
  596. package/dist/esm/types/data-structures/graph/index.d.ts +0 -3
  597. package/dist/esm/types/data-structures/graph/index.js +0 -4
  598. package/dist/esm/types/data-structures/graph/index.js.map +0 -1
  599. package/dist/esm/types/data-structures/graph/map-graph.d.ts +0 -1
  600. package/dist/esm/types/data-structures/graph/map-graph.js +0 -2
  601. package/dist/esm/types/data-structures/graph/map-graph.js.map +0 -1
  602. package/dist/esm/types/data-structures/graph/undirected-graph.d.ts +0 -1
  603. package/dist/esm/types/data-structures/graph/undirected-graph.js +0 -2
  604. package/dist/esm/types/data-structures/graph/undirected-graph.js.map +0 -1
  605. package/dist/esm/types/data-structures/hash/hash-map.d.ts +0 -19
  606. package/dist/esm/types/data-structures/hash/hash-map.js +0 -2
  607. package/dist/esm/types/data-structures/hash/hash-map.js.map +0 -1
  608. package/dist/esm/types/data-structures/hash/index.d.ts +0 -2
  609. package/dist/esm/types/data-structures/hash/index.js +0 -2
  610. package/dist/esm/types/data-structures/hash/index.js.map +0 -1
  611. package/dist/esm/types/data-structures/heap/heap.d.ts +0 -5
  612. package/dist/esm/types/data-structures/heap/heap.js +0 -2
  613. package/dist/esm/types/data-structures/heap/heap.js.map +0 -1
  614. package/dist/esm/types/data-structures/heap/index.d.ts +0 -1
  615. package/dist/esm/types/data-structures/heap/index.js +0 -2
  616. package/dist/esm/types/data-structures/heap/index.js.map +0 -1
  617. package/dist/esm/types/data-structures/heap/max-heap.d.ts +0 -1
  618. package/dist/esm/types/data-structures/heap/max-heap.js +0 -2
  619. package/dist/esm/types/data-structures/heap/max-heap.js.map +0 -1
  620. package/dist/esm/types/data-structures/heap/min-heap.d.ts +0 -1
  621. package/dist/esm/types/data-structures/heap/min-heap.js +0 -2
  622. package/dist/esm/types/data-structures/heap/min-heap.js.map +0 -1
  623. package/dist/esm/types/data-structures/index.d.ts +0 -12
  624. package/dist/esm/types/data-structures/index.js +0 -13
  625. package/dist/esm/types/data-structures/index.js.map +0 -1
  626. package/dist/esm/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -2
  627. package/dist/esm/types/data-structures/linked-list/doubly-linked-list.js +0 -2
  628. package/dist/esm/types/data-structures/linked-list/doubly-linked-list.js.map +0 -1
  629. package/dist/esm/types/data-structures/linked-list/index.d.ts +0 -3
  630. package/dist/esm/types/data-structures/linked-list/index.js +0 -4
  631. package/dist/esm/types/data-structures/linked-list/index.js.map +0 -1
  632. package/dist/esm/types/data-structures/linked-list/singly-linked-list.d.ts +0 -2
  633. package/dist/esm/types/data-structures/linked-list/singly-linked-list.js +0 -2
  634. package/dist/esm/types/data-structures/linked-list/singly-linked-list.js.map +0 -1
  635. package/dist/esm/types/data-structures/linked-list/skip-linked-list.d.ts +0 -4
  636. package/dist/esm/types/data-structures/linked-list/skip-linked-list.js +0 -2
  637. package/dist/esm/types/data-structures/linked-list/skip-linked-list.js.map +0 -1
  638. package/dist/esm/types/data-structures/matrix/index.d.ts +0 -2
  639. package/dist/esm/types/data-structures/matrix/index.js +0 -3
  640. package/dist/esm/types/data-structures/matrix/index.js.map +0 -1
  641. package/dist/esm/types/data-structures/matrix/matrix.d.ts +0 -7
  642. package/dist/esm/types/data-structures/matrix/matrix.js +0 -2
  643. package/dist/esm/types/data-structures/matrix/matrix.js.map +0 -1
  644. package/dist/esm/types/data-structures/matrix/navigator.d.ts +0 -14
  645. package/dist/esm/types/data-structures/matrix/navigator.js +0 -2
  646. package/dist/esm/types/data-structures/matrix/navigator.js.map +0 -1
  647. package/dist/esm/types/data-structures/priority-queue/index.d.ts +0 -3
  648. package/dist/esm/types/data-structures/priority-queue/index.js +0 -4
  649. package/dist/esm/types/data-structures/priority-queue/index.js.map +0 -1
  650. package/dist/esm/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -1
  651. package/dist/esm/types/data-structures/priority-queue/max-priority-queue.js +0 -2
  652. package/dist/esm/types/data-structures/priority-queue/max-priority-queue.js.map +0 -1
  653. package/dist/esm/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -1
  654. package/dist/esm/types/data-structures/priority-queue/min-priority-queue.js +0 -2
  655. package/dist/esm/types/data-structures/priority-queue/min-priority-queue.js.map +0 -1
  656. package/dist/esm/types/data-structures/priority-queue/priority-queue.d.ts +0 -2
  657. package/dist/esm/types/data-structures/priority-queue/priority-queue.js +0 -2
  658. package/dist/esm/types/data-structures/priority-queue/priority-queue.js.map +0 -1
  659. package/dist/esm/types/data-structures/queue/deque.d.ts +0 -4
  660. package/dist/esm/types/data-structures/queue/deque.js +0 -2
  661. package/dist/esm/types/data-structures/queue/deque.js.map +0 -1
  662. package/dist/esm/types/data-structures/queue/index.d.ts +0 -2
  663. package/dist/esm/types/data-structures/queue/index.js +0 -3
  664. package/dist/esm/types/data-structures/queue/index.js.map +0 -1
  665. package/dist/esm/types/data-structures/queue/queue.d.ts +0 -4
  666. package/dist/esm/types/data-structures/queue/queue.js +0 -2
  667. package/dist/esm/types/data-structures/queue/queue.js.map +0 -1
  668. package/dist/esm/types/data-structures/stack/index.d.ts +0 -1
  669. package/dist/esm/types/data-structures/stack/index.js +0 -2
  670. package/dist/esm/types/data-structures/stack/index.js.map +0 -1
  671. package/dist/esm/types/data-structures/stack/stack.d.ts +0 -2
  672. package/dist/esm/types/data-structures/stack/stack.js +0 -2
  673. package/dist/esm/types/data-structures/stack/stack.js.map +0 -1
  674. package/dist/esm/types/data-structures/tree/index.d.ts +0 -1
  675. package/dist/esm/types/data-structures/tree/index.js +0 -2
  676. package/dist/esm/types/data-structures/tree/index.js.map +0 -1
  677. package/dist/esm/types/data-structures/tree/tree.d.ts +0 -1
  678. package/dist/esm/types/data-structures/tree/tree.js +0 -2
  679. package/dist/esm/types/data-structures/tree/tree.js.map +0 -1
  680. package/dist/esm/types/data-structures/trie/index.d.ts +0 -1
  681. package/dist/esm/types/data-structures/trie/index.js +0 -2
  682. package/dist/esm/types/data-structures/trie/index.js.map +0 -1
  683. package/dist/esm/types/data-structures/trie/trie.d.ts +0 -4
  684. package/dist/esm/types/data-structures/trie/trie.js +0 -2
  685. package/dist/esm/types/data-structures/trie/trie.js.map +0 -1
  686. package/dist/esm/types/index.d.ts +0 -3
  687. package/dist/esm/types/index.js +0 -4
  688. package/dist/esm/types/index.js.map +0 -1
  689. package/dist/esm/types/utils/index.d.ts +0 -2
  690. package/dist/esm/types/utils/index.js +0 -3
  691. package/dist/esm/types/utils/index.js.map +0 -1
  692. package/dist/esm/types/utils/utils.d.ts +0 -21
  693. package/dist/esm/types/utils/utils.js +0 -2
  694. package/dist/esm/types/utils/utils.js.map +0 -1
  695. package/dist/esm/types/utils/validate-type.d.ts +0 -19
  696. package/dist/esm/types/utils/validate-type.js +0 -2
  697. package/dist/esm/types/utils/validate-type.js.map +0 -1
  698. package/dist/esm/utils/index.d.ts +0 -2
  699. package/dist/esm/utils/index.js +0 -3
  700. package/dist/esm/utils/index.js.map +0 -1
  701. package/dist/esm/utils/number.d.ts +0 -14
  702. package/dist/esm/utils/number.js +0 -21
  703. package/dist/esm/utils/number.js.map +0 -1
  704. package/dist/esm/utils/utils.js +0 -324
  705. package/dist/esm/utils/utils.js.map +0 -1
  706. package/test/performance/data-structures/binary-tree/avl-tree.test.mjs +0 -71
  707. package/test/performance/data-structures/binary-tree/red-black-tree.test.mjs +0 -81
  708. package/tsconfig-cjs.json +0 -14
  709. package/tsconfig-esm.json +0 -14
  710. /package/dist/{cjs → types}/common/index.d.ts +0 -0
  711. /package/dist/{cjs → types}/data-structures/base/index.d.ts +0 -0
  712. /package/dist/{cjs → types}/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -0
  713. /package/dist/{cjs → types}/data-structures/binary-tree/index.d.ts +0 -0
  714. /package/dist/{cjs → types}/data-structures/binary-tree/segment-tree.d.ts +0 -0
  715. /package/dist/{cjs → types}/data-structures/graph/index.d.ts +0 -0
  716. /package/dist/{cjs → types}/data-structures/hash/index.d.ts +0 -0
  717. /package/dist/{cjs → types}/data-structures/heap/index.d.ts +0 -0
  718. /package/dist/{cjs → types}/data-structures/index.d.ts +0 -0
  719. /package/dist/{cjs → types}/data-structures/linked-list/index.d.ts +0 -0
  720. /package/dist/{cjs → types}/data-structures/matrix/index.d.ts +0 -0
  721. /package/dist/{cjs → types}/data-structures/matrix/matrix.d.ts +0 -0
  722. /package/dist/{cjs → types}/data-structures/matrix/navigator.d.ts +0 -0
  723. /package/dist/{cjs → types}/data-structures/priority-queue/index.d.ts +0 -0
  724. /package/dist/{cjs → types}/data-structures/queue/index.d.ts +0 -0
  725. /package/dist/{cjs → types}/data-structures/stack/index.d.ts +0 -0
  726. /package/dist/{cjs → types}/data-structures/tree/index.d.ts +0 -0
  727. /package/dist/{cjs → types}/data-structures/tree/tree.d.ts +0 -0
  728. /package/dist/{cjs → types}/data-structures/trie/index.d.ts +0 -0
  729. /package/dist/{cjs → types}/index.d.ts +0 -0
  730. /package/dist/{cjs → types}/interfaces/doubly-linked-list.d.ts +0 -0
  731. /package/dist/{cjs → types}/interfaces/heap.d.ts +0 -0
  732. /package/dist/{cjs → types}/interfaces/index.d.ts +0 -0
  733. /package/dist/{cjs → types}/interfaces/navigator.d.ts +0 -0
  734. /package/dist/{cjs → types}/interfaces/priority-queue.d.ts +0 -0
  735. /package/dist/{cjs → types}/interfaces/segment-tree.d.ts +0 -0
  736. /package/dist/{cjs → types}/interfaces/singly-linked-list.d.ts +0 -0
  737. /package/dist/{cjs → types}/types/common.d.ts +0 -0
  738. /package/dist/{cjs → types}/types/data-structures/base/index.d.ts +0 -0
  739. /package/dist/{cjs → types}/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -0
  740. /package/dist/{cjs → types}/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -0
  741. /package/dist/{cjs → types}/types/data-structures/binary-tree/avl-tree.d.ts +0 -0
  742. /package/dist/{cjs → types}/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -0
  743. /package/dist/{cjs → types}/types/data-structures/binary-tree/binary-tree.d.ts +0 -0
  744. /package/dist/{cjs → types}/types/data-structures/binary-tree/bst.d.ts +0 -0
  745. /package/dist/{cjs → types}/types/data-structures/binary-tree/index.d.ts +0 -0
  746. /package/dist/{cjs → types}/types/data-structures/binary-tree/red-black-tree.d.ts +0 -0
  747. /package/dist/{cjs → types}/types/data-structures/binary-tree/segment-tree.d.ts +0 -0
  748. /package/dist/{cjs → types}/types/data-structures/binary-tree/tree-counter.d.ts +0 -0
  749. /package/dist/{cjs → types}/types/data-structures/binary-tree/tree-multi-map.d.ts +0 -0
  750. /package/dist/{cjs → types}/types/data-structures/graph/directed-graph.d.ts +0 -0
  751. /package/dist/{cjs → types}/types/data-structures/graph/index.d.ts +0 -0
  752. /package/dist/{cjs → types}/types/data-structures/graph/map-graph.d.ts +0 -0
  753. /package/dist/{cjs → types}/types/data-structures/graph/undirected-graph.d.ts +0 -0
  754. /package/dist/{cjs → types}/types/data-structures/hash/hash-map.d.ts +0 -0
  755. /package/dist/{cjs → types}/types/data-structures/hash/index.d.ts +0 -0
  756. /package/dist/{cjs → types}/types/data-structures/heap/heap.d.ts +0 -0
  757. /package/dist/{cjs → types}/types/data-structures/heap/index.d.ts +0 -0
  758. /package/dist/{cjs → types}/types/data-structures/heap/max-heap.d.ts +0 -0
  759. /package/dist/{cjs → types}/types/data-structures/heap/min-heap.d.ts +0 -0
  760. /package/dist/{cjs → types}/types/data-structures/index.d.ts +0 -0
  761. /package/dist/{cjs → types}/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -0
  762. /package/dist/{cjs → types}/types/data-structures/linked-list/index.d.ts +0 -0
  763. /package/dist/{cjs → types}/types/data-structures/linked-list/singly-linked-list.d.ts +0 -0
  764. /package/dist/{cjs → types}/types/data-structures/linked-list/skip-linked-list.d.ts +0 -0
  765. /package/dist/{cjs → types}/types/data-structures/matrix/index.d.ts +0 -0
  766. /package/dist/{cjs → types}/types/data-structures/matrix/matrix.d.ts +0 -0
  767. /package/dist/{cjs → types}/types/data-structures/matrix/navigator.d.ts +0 -0
  768. /package/dist/{cjs → types}/types/data-structures/priority-queue/index.d.ts +0 -0
  769. /package/dist/{cjs → types}/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -0
  770. /package/dist/{cjs → types}/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -0
  771. /package/dist/{cjs → types}/types/data-structures/priority-queue/priority-queue.d.ts +0 -0
  772. /package/dist/{cjs → types}/types/data-structures/queue/deque.d.ts +0 -0
  773. /package/dist/{cjs → types}/types/data-structures/queue/index.d.ts +0 -0
  774. /package/dist/{cjs → types}/types/data-structures/queue/queue.d.ts +0 -0
  775. /package/dist/{cjs → types}/types/data-structures/stack/index.d.ts +0 -0
  776. /package/dist/{cjs → types}/types/data-structures/stack/stack.d.ts +0 -0
  777. /package/dist/{cjs → types}/types/data-structures/tree/index.d.ts +0 -0
  778. /package/dist/{cjs → types}/types/data-structures/tree/tree.d.ts +0 -0
  779. /package/dist/{cjs → types}/types/data-structures/trie/index.d.ts +0 -0
  780. /package/dist/{cjs → types}/types/data-structures/trie/trie.d.ts +0 -0
  781. /package/dist/{cjs → types}/types/index.d.ts +0 -0
  782. /package/dist/{cjs → types}/types/utils/index.d.ts +0 -0
  783. /package/dist/{cjs → types}/types/utils/validate-type.d.ts +0 -0
  784. /package/dist/{cjs → types}/utils/index.d.ts +0 -0
  785. /package/dist/{cjs → types}/utils/number.d.ts +0 -0
@@ -1,2198 +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
- * Represents a node in a binary tree.
14
- * @template V - The type of data stored in the node.
15
- * @template BinaryTreeNode<K, V> - The type of the family relationship in the binary tree.
16
- */
17
- export class BinaryTreeNode {
18
- key;
19
- value;
20
- parent = undefined;
21
- /**
22
- * The constructor function initializes an object with a key and an optional value in TypeScript.
23
- * @param {K} key - The `key` parameter in the constructor function is used to store the key value
24
- * for the key-value pair.
25
- * @param {V} [value] - The `value` parameter in the constructor is optional, meaning it does not
26
- * have to be provided when creating an instance of the class. If a `value` is not provided, it will
27
- * default to `undefined`.
28
- */
29
- constructor(key, value) {
30
- this.key = key;
31
- this.value = value;
32
- }
33
- _left = undefined;
34
- get left() {
35
- return this._left;
36
- }
37
- set left(v) {
38
- if (v) {
39
- v.parent = this;
40
- }
41
- this._left = v;
42
- }
43
- _right = undefined;
44
- get right() {
45
- return this._right;
46
- }
47
- set right(v) {
48
- if (v) {
49
- v.parent = this;
50
- }
51
- this._right = v;
52
- }
53
- _height = 0;
54
- get height() {
55
- return this._height;
56
- }
57
- set height(value) {
58
- this._height = value;
59
- }
60
- _color = 'BLACK';
61
- get color() {
62
- return this._color;
63
- }
64
- set color(value) {
65
- this._color = value;
66
- }
67
- _count = 1;
68
- get count() {
69
- return this._count;
70
- }
71
- set count(value) {
72
- this._count = value;
73
- }
74
- get familyPosition() {
75
- if (!this.parent) {
76
- return this.left || this.right ? 'ROOT' : 'ISOLATED';
77
- }
78
- if (this.parent.left === this) {
79
- return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
80
- }
81
- else if (this.parent.right === this) {
82
- return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
83
- }
84
- return 'MAL_NODE';
85
- }
86
- }
87
- /**
88
- * 1. Two Children Maximum: Each node has at most two children.
89
- * 2. Left and Right Children: Nodes have distinct left and right children.
90
- * 3. Depth and Height: Depth is the number of edges from the root to a node; height is the maximum depth in the tree.
91
- * 4. Subtrees: Each child of a node forms the root of a subtree.
92
- * 5. Leaf Nodes: Nodes without children are leaves.
93
- * @example
94
- * // determine loan approval using a decision tree
95
- * // Decision tree structure
96
- * const loanDecisionTree = new BinaryTree<string>(
97
- * ['stableIncome', 'goodCredit', 'Rejected', 'Approved', 'Rejected'],
98
- * { isDuplicate: true }
99
- * );
100
- *
101
- * function determineLoanApproval(
102
- * node?: BinaryTreeNode<string> | null,
103
- * conditions?: { [key: string]: boolean }
104
- * ): string {
105
- * if (!node) throw new Error('Invalid node');
106
- *
107
- * // If it's a leaf node, return the decision result
108
- * if (!node.left && !node.right) return node.key;
109
- *
110
- * // Check if a valid condition exists for the current node's key
111
- * return conditions?.[node.key]
112
- * ? determineLoanApproval(node.left, conditions)
113
- * : determineLoanApproval(node.right, conditions);
114
- * }
115
- *
116
- * // Test case 1: Stable income and good credit score
117
- * console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: true })); // 'Approved'
118
- *
119
- * // Test case 2: Stable income but poor credit score
120
- * console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: false })); // 'Rejected'
121
- *
122
- * // Test case 3: No stable income
123
- * console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: true })); // 'Rejected'
124
- *
125
- * // Test case 4: No stable income and poor credit score
126
- * console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: false })); // 'Rejected'
127
- * @example
128
- * // evaluate the arithmetic expression represented by the binary tree
129
- * const expressionTree = new BinaryTree<number | string>(['+', 3, '*', null, null, 5, '-', null, null, 2, 8]);
130
- *
131
- * function evaluate(node?: BinaryTreeNode<number | string> | null): number {
132
- * if (!node) return 0;
133
- *
134
- * if (typeof node.key === 'number') return node.key;
135
- *
136
- * const leftValue = evaluate(node.left); // Evaluate the left subtree
137
- * const rightValue = evaluate(node.right); // Evaluate the right subtree
138
- *
139
- * // Perform the operation based on the current node's operator
140
- * switch (node.key) {
141
- * case '+':
142
- * return leftValue + rightValue;
143
- * case '-':
144
- * return leftValue - rightValue;
145
- * case '*':
146
- * return leftValue * rightValue;
147
- * case '/':
148
- * return rightValue !== 0 ? leftValue / rightValue : 0; // Handle division by zero
149
- * default:
150
- * throw new Error(`Unsupported operator: ${node.key}`);
151
- * }
152
- * }
153
- *
154
- * console.log(evaluate(expressionTree.root)); // -27
155
- */
156
- export class BinaryTree extends IterableEntryBase {
157
- iterationType = 'ITERATIVE';
158
- /**
159
- * This TypeScript constructor function initializes a binary tree with optional options and adds
160
- * elements based on the provided input.
161
- * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
162
- * iterable that can contain either objects of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It
163
- * is used to initialize the binary tree with keys, nodes, entries, or raw data.
164
- * @param [options] - The `options` parameter in the constructor is an optional object that can
165
- * contain the following properties:
166
- */
167
- constructor(keysNodesEntriesOrRaws = [], options) {
168
- super();
169
- if (options) {
170
- const { iterationType, toEntryFn, isMapMode, isDuplicate } = options;
171
- if (iterationType)
172
- this.iterationType = iterationType;
173
- if (isMapMode !== undefined)
174
- this._isMapMode = isMapMode;
175
- if (isDuplicate !== undefined)
176
- this._isDuplicate = isDuplicate;
177
- if (typeof toEntryFn === 'function')
178
- this._toEntryFn = toEntryFn;
179
- else if (toEntryFn)
180
- throw TypeError('toEntryFn must be a function type');
181
- }
182
- if (keysNodesEntriesOrRaws)
183
- this.addMany(keysNodesEntriesOrRaws);
184
- }
185
- _isMapMode = true;
186
- get isMapMode() {
187
- return this._isMapMode;
188
- }
189
- _isDuplicate = false;
190
- get isDuplicate() {
191
- return this._isDuplicate;
192
- }
193
- _store = new Map();
194
- get store() {
195
- return this._store;
196
- }
197
- _root;
198
- get root() {
199
- return this._root;
200
- }
201
- _size = 0;
202
- get size() {
203
- return this._size;
204
- }
205
- _NIL = new BinaryTreeNode(NaN);
206
- get NIL() {
207
- return this._NIL;
208
- }
209
- _toEntryFn;
210
- get toEntryFn() {
211
- return this._toEntryFn;
212
- }
213
- /**
214
- * Time Complexity: O(1)
215
- * Space Complexity: O(1)
216
- *
217
- * The function creates a new binary tree node with a specified key and optional value.
218
- * @param {K} key - The `key` parameter is the key of the node being created in the binary tree.
219
- * @param {V} [value] - The `value` parameter in the `createNode` function is optional, meaning it is
220
- * not required to be provided when calling the function. If a `value` is provided, it should be of
221
- * type `V`, which is the type of the value associated with the node.
222
- * @returns A new BinaryTreeNode instance with the provided key and value is being returned, casted
223
- * as BinaryTreeNode<K, V>.
224
- */
225
- createNode(key, value) {
226
- return new BinaryTreeNode(key, this._isMapMode ? undefined : value);
227
- }
228
- /**
229
- * Time Complexity: O(1)
230
- * Space Complexity: O(1)
231
- *
232
- * The function creates a binary tree with the specified options.
233
- * @param [options] - The `options` parameter in the `createTree` function is an optional parameter
234
- * that allows you to provide partial configuration options for creating a binary tree. It is of type
235
- * `Partial<BinaryTreeOptions<K, V, R>>`, which means you can pass in an object containing a subset
236
- * of properties
237
- * @returns A new instance of a binary tree with the specified options is being returned.
238
- */
239
- createTree(options) {
240
- return new BinaryTree([], {
241
- iterationType: this.iterationType,
242
- isMapMode: this._isMapMode,
243
- toEntryFn: this._toEntryFn,
244
- ...options
245
- });
246
- }
247
- /**
248
- * Time Complexity: O(n)
249
- * Space Complexity: O(log n)
250
- *
251
- * The function `ensureNode` in TypeScript checks if a given input is a node, entry, key, or raw
252
- * value and returns the corresponding node or null.
253
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
254
- * parameter in the `ensureNode` function can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It
255
- * is used to determine whether the input is a key, node, entry, or raw data. The
256
- * @param {IterationType} iterationType - The `iterationType` parameter in the `ensureNode` function
257
- * is used to specify the type of iteration to be performed. It has a default value of
258
- * `this.iterationType` if not explicitly provided.
259
- * @returns The `ensureNode` function returns either a node, `null`, or `undefined` based on the
260
- * conditions specified in the code snippet.
261
- */
262
- ensureNode(keyNodeOrEntry, iterationType = this.iterationType) {
263
- if (keyNodeOrEntry === null)
264
- return null;
265
- if (keyNodeOrEntry === undefined)
266
- return;
267
- if (keyNodeOrEntry === this._NIL)
268
- return;
269
- if (this.isNode(keyNodeOrEntry))
270
- return keyNodeOrEntry;
271
- if (this.isEntry(keyNodeOrEntry)) {
272
- const key = keyNodeOrEntry[0];
273
- if (key === null)
274
- return null;
275
- if (key === undefined)
276
- return;
277
- return this.getNode(key, this._root, iterationType);
278
- }
279
- return this.getNode(keyNodeOrEntry, this._root, iterationType);
280
- }
281
- /**
282
- * Time Complexity: O(1)
283
- * Space Complexity: O(1)
284
- *
285
- * The function isNode checks if the input is an instance of BinaryTreeNode.
286
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
287
- * `keyNodeOrEntry` can be either a key, a node, an entry, or raw data. The function is
288
- * checking if the input is an instance of a `BinaryTreeNode` and returning a boolean value
289
- * accordingly.
290
- * @returns The function `isNode` is checking if the input `keyNodeOrEntry` is an instance of
291
- * `BinaryTreeNode`. If it is, the function returns `true`, indicating that the input is a node. If
292
- * it is not an instance of `BinaryTreeNode`, the function returns `false`, indicating that the input
293
- * is not a node.
294
- */
295
- isNode(keyNodeOrEntry) {
296
- return keyNodeOrEntry instanceof BinaryTreeNode;
297
- }
298
- /**
299
- * Time Complexity: O(1)
300
- * Space Complexity: O(1)
301
- *
302
- * The function `isRaw` checks if the input parameter is of type `R` by verifying if it is an object.
303
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R} keyNodeEntryOrRaw - K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
304
- * @returns The function `isRaw` is checking if the `keyNodeEntryOrRaw` parameter is of type `R` by
305
- * checking if it is an object. If the parameter is an object, the function will return `true`,
306
- * indicating that it is of type `R`.
307
- */
308
- isRaw(keyNodeEntryOrRaw) {
309
- return this._toEntryFn !== undefined && typeof keyNodeEntryOrRaw === 'object';
310
- }
311
- /**
312
- * Time Complexity: O(1)
313
- * Space Complexity: O(1)
314
- *
315
- * The function `isRealNode` checks if a given input is a valid node in a binary tree.
316
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
317
- * parameter in the `isRealNode` function can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`.
318
- * The function checks if the input parameter is a `BinaryTreeNode<K, V>` type by verifying if it is not equal
319
- * @returns The function `isRealNode` is checking if the input `keyNodeOrEntry` is a valid
320
- * node by comparing it to `this._NIL`, `null`, and `undefined`. If the input is not one of these
321
- * values, it then calls the `isNode` method to further determine if the input is a node. The
322
- * function will return a boolean value indicating whether the
323
- */
324
- isRealNode(keyNodeOrEntry) {
325
- if (keyNodeOrEntry === this._NIL || keyNodeOrEntry === null || keyNodeOrEntry === undefined)
326
- return false;
327
- return this.isNode(keyNodeOrEntry);
328
- }
329
- /**
330
- * Time Complexity: O(1)
331
- * Space Complexity: O(1)
332
- *
333
- * The function checks if a given input is a valid node or null.
334
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
335
- * `keyNodeOrEntry` in the `isRealNodeOrNull` function can be of type `BTNRep<K,
336
- * V, BinaryTreeNode<K, V>>` or `R`. It is a union type that can either be a key, a node, an entry, or
337
- * @returns The function `isRealNodeOrNull` is returning a boolean value. It checks if the input
338
- * `keyNodeOrEntry` is either `null` or a real node, and returns `true` if it is a node or
339
- * `null`, and `false` otherwise.
340
- */
341
- isRealNodeOrNull(keyNodeOrEntry) {
342
- return keyNodeOrEntry === null || this.isRealNode(keyNodeOrEntry);
343
- }
344
- /**
345
- * Time Complexity: O(1)
346
- * Space Complexity: O(1)
347
- *
348
- * The function isNIL checks if a given key, node, entry, or raw value is equal to the _NIL value.
349
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - BTNRep<K, V,
350
- * BinaryTreeNode<K, V>>
351
- * @returns The function is checking if the `keyNodeOrEntry` parameter is equal to the `_NIL`
352
- * property of the current object and returning a boolean value based on that comparison.
353
- */
354
- isNIL(keyNodeOrEntry) {
355
- return keyNodeOrEntry === this._NIL;
356
- }
357
- /**
358
- * Time Complexity: O(1)
359
- * Space Complexity: O(1)
360
- *
361
- * The function `isRange` checks if the input parameter is an instance of the `Range` class.
362
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>> | Range<K>} keyNodeEntryOrPredicate
363
- * - The `keyNodeEntryOrPredicate` parameter in the `isRange` function can be
364
- * of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `, `NodePredicate<BinaryTreeNode<K, V>>`, or
365
- * `Range<K>`. The function checks if the `keyNodeEntry
366
- * @returns The `isRange` function is checking if the `keyNodeEntryOrPredicate` parameter is an
367
- * instance of the `Range` class. If it is an instance of `Range`, the function will return `true`,
368
- * indicating that the parameter is a `Range<K>`. If it is not an instance of `Range`, the function
369
- * will return `false`.
370
- */
371
- isRange(keyNodeEntryOrPredicate) {
372
- return keyNodeEntryOrPredicate instanceof Range;
373
- }
374
- /**
375
- * Time Complexity: O(1)
376
- * Space Complexity: O(1)
377
- *
378
- * The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
379
- * tree.
380
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
381
- * `keyNodeOrEntry` can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It represents a
382
- * key, node, entry, or raw data in a binary tree structure. The function `isLeaf` checks whether the
383
- * provided
384
- * @returns The function `isLeaf` returns a boolean value indicating whether the input
385
- * `keyNodeOrEntry` is a leaf node in a binary tree.
386
- */
387
- isLeaf(keyNodeOrEntry) {
388
- keyNodeOrEntry = this.ensureNode(keyNodeOrEntry);
389
- if (keyNodeOrEntry === undefined)
390
- return false;
391
- if (keyNodeOrEntry === null)
392
- return true;
393
- return !this.isRealNode(keyNodeOrEntry.left) && !this.isRealNode(keyNodeOrEntry.right);
394
- }
395
- /**
396
- * Time Complexity: O(1)
397
- * Space Complexity: O(1)
398
- *
399
- * The function `isEntry` checks if the input is a BTNEntry object by verifying if it is an array
400
- * with a length of 2.
401
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
402
- * parameter in the `isEntry` function can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or type `R`.
403
- * The function checks if the provided `keyNodeOrEntry` is of type `BTN
404
- * @returns The `isEntry` function is checking if the `keyNodeOrEntry` parameter is an array
405
- * with a length of 2. If it is, then it returns `true`, indicating that the parameter is of type
406
- * `BTNEntry<K, V>`. If the condition is not met, it returns `false`.
407
- */
408
- isEntry(keyNodeOrEntry) {
409
- return Array.isArray(keyNodeOrEntry) && keyNodeOrEntry.length === 2;
410
- }
411
- /**
412
- * Time Complexity O(1)
413
- * Space Complexity O(1)
414
- *
415
- * The function `isValidKey` checks if a given key is comparable.
416
- * @param {any} key - The `key` parameter is of type `any`, which means it can be any data type in
417
- * TypeScript.
418
- * @returns The function `isValidKey` is checking if the `key` parameter is `null` or if it is comparable.
419
- * If the `key` is `null`, the function returns `true`. Otherwise, it returns the result of the
420
- * `isComparable` function, which is not provided in the code snippet.
421
- */
422
- isValidKey(key) {
423
- if (key === null)
424
- return true;
425
- return isComparable(key);
426
- }
427
- /**
428
- * Time Complexity O(n)
429
- * Space Complexity O(1)
430
- *
431
- * The `add` function in TypeScript adds a new node to a binary tree while handling duplicate keys
432
- * and finding the correct insertion position.
433
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `add` method you provided
434
- * seems to be for adding a new node to a binary tree structure. The `keyNodeOrEntry`
435
- * parameter in the method can accept different types of values:
436
- * @param {V} [value] - The `value` parameter in the `add` method represents the value associated
437
- * with the key that you want to add to the binary tree. When adding a key-value pair to the binary
438
- * tree, you provide the key and its corresponding value. The `add` method then creates a new node
439
- * with this
440
- * @returns The `add` method returns a boolean value. It returns `true` if the insertion of the new
441
- * node was successful, and `false` if the insertion position could not be found or if a duplicate
442
- * key was found and the node was replaced instead of inserted.
443
- */
444
- add(keyNodeOrEntry, value) {
445
- const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
446
- if (newNode === undefined)
447
- return false;
448
- // If the tree is empty, directly set the new node as the root node
449
- if (!this._root) {
450
- this._setRoot(newNode);
451
- if (this._isMapMode)
452
- this._setValue(newNode?.key, newValue);
453
- this._size = 1;
454
- return true;
455
- }
456
- const queue = new Queue([this._root]);
457
- let potentialParent; // Record the parent node of the potential insertion location
458
- while (queue.length > 0) {
459
- const cur = queue.shift();
460
- if (!cur)
461
- continue;
462
- if (!this._isDuplicate) {
463
- // Check for duplicate keys when newNode is not null
464
- if (newNode !== null && cur.key === newNode.key) {
465
- this._replaceNode(cur, newNode);
466
- if (this._isMapMode)
467
- this._setValue(cur.key, newValue);
468
- return true; // If duplicate keys are found, no insertion is performed
469
- }
470
- }
471
- // Record the first possible insertion location found
472
- if (potentialParent === undefined && (cur.left === undefined || cur.right === undefined)) {
473
- potentialParent = cur;
474
- }
475
- // Continue traversing the left and right subtrees
476
- if (cur.left !== null) {
477
- if (cur.left)
478
- queue.push(cur.left);
479
- }
480
- if (cur.right !== null) {
481
- if (cur.right)
482
- queue.push(cur.right);
483
- }
484
- }
485
- // At the end of the traversal, if the insertion position is found, insert
486
- if (potentialParent) {
487
- if (potentialParent.left === undefined) {
488
- potentialParent.left = newNode;
489
- }
490
- else if (potentialParent.right === undefined) {
491
- potentialParent.right = newNode;
492
- }
493
- if (this._isMapMode)
494
- this._setValue(newNode?.key, newValue);
495
- this._size++;
496
- return true;
497
- }
498
- return false; // If the insertion position cannot be found, return undefined
499
- }
500
- /**
501
- * Time Complexity: O(k * n)
502
- * Space Complexity: O(k)
503
- *
504
- * The `addMany` function takes in multiple keys or nodes or entries or raw values along with
505
- * optional values, and adds them to a data structure while returning an array indicating whether
506
- * each insertion was successful.
507
- * @param keysNodesEntriesOrRaws - `keysNodesEntriesOrRaws` is an iterable that can contain a
508
- * mix of keys, nodes, entries, or raw values. Each element in this iterable can be of type
509
- * `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`.
510
- * @param [values] - The `values` parameter in the `addMany` function is an optional parameter that
511
- * accepts an iterable of values. These values correspond to the keys or nodes being added in the
512
- * `keysNodesEntriesOrRaws` parameter. If provided, the function will iterate over the values and
513
- * assign them
514
- * @returns The `addMany` method returns an array of boolean values indicating whether each key,
515
- * node, entry, or raw value was successfully added to the data structure. Each boolean value
516
- * corresponds to the success of adding the corresponding key or value in the input iterable.
517
- */
518
- addMany(keysNodesEntriesOrRaws, values) {
519
- // TODO not sure addMany not be run multi times
520
- const inserted = [];
521
- let valuesIterator;
522
- if (values) {
523
- valuesIterator = values[Symbol.iterator]();
524
- }
525
- for (let keyNodeEntryOrRaw of keysNodesEntriesOrRaws) {
526
- let value = undefined;
527
- if (valuesIterator) {
528
- const valueResult = valuesIterator.next();
529
- if (!valueResult.done) {
530
- value = valueResult.value;
531
- }
532
- }
533
- if (this.isRaw(keyNodeEntryOrRaw))
534
- keyNodeEntryOrRaw = this._toEntryFn(keyNodeEntryOrRaw);
535
- inserted.push(this.add(keyNodeEntryOrRaw, value));
536
- }
537
- return inserted;
538
- }
539
- /**
540
- * Time Complexity: O(k * n)
541
- * Space Complexity: O(1)
542
- *
543
- * The `merge` function in TypeScript merges another binary tree into the current tree by adding all
544
- * elements from the other tree.
545
- * @param anotherTree - BinaryTree<K, V, R, MK, MV, MR>
546
- */
547
- merge(anotherTree) {
548
- this.addMany(anotherTree, []);
549
- }
550
- /**
551
- * Time Complexity: O(k * n)
552
- * Space Complexity: O(1)
553
- *
554
- * The `refill` function clears the existing data structure and then adds new key-value pairs based
555
- * on the provided input.
556
- * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the `refill`
557
- * method can accept an iterable containing a mix of `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` objects or `R`
558
- * objects.
559
- * @param [values] - The `values` parameter in the `refill` method is an optional parameter that
560
- * accepts an iterable of values of type `V` or `undefined`.
561
- */
562
- refill(keysNodesEntriesOrRaws, values) {
563
- this.clear();
564
- this.addMany(keysNodesEntriesOrRaws, values);
565
- }
566
- /**
567
- * Time Complexity: O(n)
568
- * Space Complexity: O(1)
569
- *
570
- * The function `delete` in TypeScript implements the deletion of a node in a binary tree and returns
571
- * the deleted node along with information for tree balancing.
572
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry
573
- * - The `delete` method you provided is used to delete a node from a binary tree based on the key,
574
- * node, entry or raw data. The method returns an array of
575
- * `BinaryTreeDeleteResult` objects containing information about the deleted node and whether
576
- * balancing is needed.
577
- * @returns The `delete` method returns an array of `BinaryTreeDeleteResult` objects. Each object in
578
- * the array contains information about the node that was deleted (`deleted`) and the node that may
579
- * need to be balanced (`needBalanced`).
580
- */
581
- delete(keyNodeOrEntry) {
582
- const deletedResult = [];
583
- if (!this._root)
584
- return deletedResult;
585
- const curr = this.getNode(keyNodeOrEntry);
586
- if (!curr)
587
- return deletedResult;
588
- const parent = curr?.parent;
589
- let needBalanced;
590
- let orgCurrent = curr;
591
- if (!curr.left && !curr.right && !parent) {
592
- this._setRoot(undefined);
593
- }
594
- else if (curr.left) {
595
- const leftSubTreeRightMost = this.getRightMost(node => node, curr.left);
596
- if (leftSubTreeRightMost) {
597
- const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
598
- orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
599
- if (parentOfLeftSubTreeMax) {
600
- if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
601
- parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
602
- else
603
- parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
604
- needBalanced = parentOfLeftSubTreeMax;
605
- }
606
- }
607
- }
608
- else if (parent) {
609
- const { familyPosition: fp } = curr;
610
- if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
611
- parent.left = curr.right;
612
- }
613
- else if (fp === 'RIGHT' || fp === 'ROOT_RIGHT') {
614
- parent.right = curr.right;
615
- }
616
- needBalanced = parent;
617
- }
618
- else {
619
- this._setRoot(curr.right);
620
- curr.right = undefined;
621
- }
622
- this._size = this._size - 1;
623
- deletedResult.push({ deleted: orgCurrent, needBalanced });
624
- if (this._isMapMode && orgCurrent)
625
- this._store.delete(orgCurrent.key);
626
- return deletedResult;
627
- }
628
- /**
629
- * Time Complexity: O(n)
630
- * Space Complexity: O(k + log n)
631
- *
632
- * The `search` function in TypeScript performs a depth-first or breadth-first search on a tree
633
- * structure based on a given predicate or key, with options to return multiple results or just one.
634
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The
635
- * `keyNodeEntryOrPredicate` parameter in the `search` function can accept three types of values:
636
- * @param [onlyOne=false] - The `onlyOne` parameter in the `search` function is a boolean flag that
637
- * determines whether the search should stop after finding the first matching node. If `onlyOne` is
638
- * set to `true`, the search will return as soon as a matching node is found. If `onlyOne` is
639
- * @param {C} callback - The `callback` parameter in the `search` function is a callback function
640
- * that will be called on each node that matches the search criteria. It is of type `C`, which
641
- * extends `NodeCallback<BinaryTreeNode<K, V> | null>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if
642
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `search` function is
643
- * used to specify the node from which the search operation should begin. It represents the starting
644
- * point in the binary tree where the search will be performed. If no specific `startNode` is
645
- * provided, the search operation will start from the root
646
- * @param {IterationType} iterationType - The `iterationType` parameter in the `search` function
647
- * specifies the type of iteration to be used when searching for nodes in a binary tree. It can have
648
- * two possible values:
649
- * @returns The `search` function returns an array of values that match the provided criteria based
650
- * on the search algorithm implemented within the function.
651
- */
652
- search(keyNodeEntryOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
653
- if (keyNodeEntryOrPredicate === undefined)
654
- return [];
655
- if (keyNodeEntryOrPredicate === null)
656
- return [];
657
- startNode = this.ensureNode(startNode);
658
- if (!startNode)
659
- return [];
660
- const predicate = this._ensurePredicate(keyNodeEntryOrPredicate);
661
- const ans = [];
662
- if (iterationType === 'RECURSIVE') {
663
- const dfs = (cur) => {
664
- if (predicate(cur)) {
665
- ans.push(callback(cur));
666
- if (onlyOne)
667
- return;
668
- }
669
- if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
670
- return;
671
- if (this.isRealNode(cur.left))
672
- dfs(cur.left);
673
- if (this.isRealNode(cur.right))
674
- dfs(cur.right);
675
- };
676
- dfs(startNode);
677
- }
678
- else {
679
- const stack = [startNode];
680
- while (stack.length > 0) {
681
- const cur = stack.pop();
682
- if (this.isRealNode(cur)) {
683
- if (predicate(cur)) {
684
- ans.push(callback(cur));
685
- if (onlyOne)
686
- return ans;
687
- }
688
- if (this.isRealNode(cur.left))
689
- stack.push(cur.left);
690
- if (this.isRealNode(cur.right))
691
- stack.push(cur.right);
692
- }
693
- }
694
- }
695
- return ans;
696
- }
697
- /**
698
- * Time Complexity: O(n)
699
- * Space Complexity: O(k + log n)
700
- *
701
- * The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
702
- * or predicate, with options for recursive or iterative traversal.
703
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
704
- * - The `getNodes` function you provided takes several parameters:
705
- * @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
706
- * determines whether to return only the first node that matches the criteria specified by the
707
- * `keyNodeEntryOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
708
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
709
- * `getNodes` function is used to specify the starting point for traversing the binary tree. It
710
- * represents the root node of the binary tree or the node from which the traversal should begin. If
711
- * not provided, the default value is set to `this._root
712
- * @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` function
713
- * determines the type of iteration to be performed when traversing the nodes of a binary tree. It
714
- * can have two possible values:
715
- * @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
716
- * based on the input parameters and the iteration type specified.
717
- */
718
- getNodes(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
719
- return this.search(keyNodeEntryOrPredicate, onlyOne, node => node, startNode, iterationType);
720
- }
721
- /**
722
- * Time Complexity: O(n)
723
- * Space Complexity: O(log n)
724
- *
725
- * The `getNode` function retrieves a node based on the provided key, node, entry, raw data, or
726
- * predicate.
727
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
728
- * - The `keyNodeEntryOrPredicate` parameter in the `getNode` function can accept a key,
729
- * node, entry, raw data, or a predicate function.
730
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
731
- * `getNode` function is used to specify the starting point for searching for a node in a binary
732
- * tree. If no specific starting point is provided, the default value is set to `this._root`, which
733
- * is typically the root node of the binary tree.
734
- * @param {IterationType} iterationType - The `iterationType` parameter in the `getNode` method is
735
- * used to specify the type of iteration to be performed when searching for a node. It has a default
736
- * value of `this.iterationType`, which means it will use the iteration type defined in the current
737
- * context if no specific value is provided
738
- * @returns The `getNode` function is returning the first node that matches the specified criteria,
739
- * or `null` if no matching node is found.
740
- */
741
- getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
742
- return this.search(keyNodeEntryOrPredicate, true, node => node, startNode, iterationType)[0];
743
- }
744
- /**
745
- * Time Complexity: O(n)
746
- * Space Complexity: O(log n)
747
- *
748
- * This function overrides the `get` method to retrieve the value associated with a specified key,
749
- * node, entry, raw data, or predicate in a data structure.
750
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
751
- * - The `keyNodeEntryOrPredicate` parameter in the `get` method can accept one of the
752
- * following types:
753
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `get`
754
- * method is used to specify the starting point for searching for a key or node in the binary tree.
755
- * If no specific starting point is provided, the default starting point is the root of the binary
756
- * tree (`this._root`).
757
- * @param {IterationType} iterationType - The `iterationType` parameter in the `get` method is used
758
- * to specify the type of iteration to be performed when searching for a key in the binary tree. It
759
- * is an optional parameter with a default value of `this.iterationType`, which means it will use the
760
- * iteration type defined in the
761
- * @returns The `get` method is returning the value associated with the specified key, node, entry,
762
- * raw data, or predicate in the binary tree map. If the specified key or node is found in the tree,
763
- * the method returns the corresponding value. If the key or node is not found, it returns
764
- * `undefined`.
765
- */
766
- get(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
767
- if (this._isMapMode) {
768
- const key = this._extractKey(keyNodeEntryOrPredicate);
769
- if (key === null || key === undefined)
770
- return;
771
- return this._store.get(key);
772
- }
773
- return this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)?.value;
774
- }
775
- /**
776
- * Time Complexity: O(n)
777
- * Space Complexity: O(log n)
778
- *
779
- * The `has` function in TypeScript checks if a specified key, node, entry, raw data, or predicate
780
- * exists in the data structure.
781
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
782
- * - The `keyNodeEntryOrPredicate` parameter in the `override has` method can accept one of
783
- * the following types:
784
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
785
- * `override` method is used to specify the starting point for the search operation within the data
786
- * structure. It defaults to `this._root` if not provided explicitly.
787
- * @param {IterationType} iterationType - The `iterationType` parameter in the `override has` method
788
- * is used to specify the type of iteration to be performed. It has a default value of
789
- * `this.iterationType`, which means it will use the iteration type defined in the current context if
790
- * no value is provided when calling the method.
791
- * @returns The `override has` method is returning a boolean value. It checks if there are any nodes
792
- * that match the provided key, node, entry, raw data, or predicate in the tree structure. If there
793
- * are matching nodes, it returns `true`, indicating that the tree contains the specified element.
794
- * Otherwise, it returns `false`.
795
- */
796
- has(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
797
- return this.search(keyNodeEntryOrPredicate, true, node => node, startNode, iterationType).length > 0;
798
- }
799
- /**
800
- * Time Complexity: O(1)
801
- * Space Complexity: O(1)
802
- *
803
- * The clear function removes nodes and values in map mode.
804
- */
805
- clear() {
806
- this._clearNodes();
807
- if (this._isMapMode)
808
- this._clearValues();
809
- }
810
- /**
811
- * Time Complexity: O(1)
812
- * Space Complexity: O(1)
813
- *
814
- * The `isEmpty` function in TypeScript checks if a data structure has no elements and returns a
815
- * boolean value.
816
- * @returns The `isEmpty()` method is returning a boolean value, specifically `true` if the `_size`
817
- * property is equal to 0, indicating that the data structure is empty, and `false` otherwise.
818
- */
819
- isEmpty() {
820
- return this._size === 0;
821
- }
822
- /**
823
- * Time Complexity: O(n)
824
- * Space Complexity: O(log n)
825
- *
826
- * The function checks if a binary tree is perfectly balanced by comparing its minimum height with
827
- * its height.
828
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter is the starting
829
- * point for checking if the binary tree is perfectly balanced. It represents the root node of the
830
- * binary tree or a specific node from which the balance check should begin.
831
- * @returns The method `isPerfectlyBalanced` is returning a boolean value, which indicates whether
832
- * the tree starting from the `startNode` node is perfectly balanced or not. The return value is
833
- * determined by comparing the minimum height of the tree with the height of the tree. If the minimum
834
- * height plus 1 is greater than or equal to the height of the tree, then it is considered perfectly
835
- * balanced and
836
- */
837
- isPerfectlyBalanced(startNode = this._root) {
838
- return this.getMinHeight(startNode) + 1 >= this.getHeight(startNode);
839
- }
840
- /**
841
- * Time Complexity: O(n)
842
- * Space Complexity: O(log n)
843
- *
844
- * The function `isBST` in TypeScript checks if a binary search tree is valid using either recursive
845
- * or iterative methods.
846
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `isBST`
847
- * function represents the starting point for checking whether a binary search tree (BST) is valid.
848
- * It can be a node in the BST or a reference to the root of the BST. If no specific node is
849
- * provided, the function will default to
850
- * @param {IterationType} iterationType - The `iterationType` parameter in the `isBST` function
851
- * determines whether the function should use a recursive approach or an iterative approach to check
852
- * if the binary search tree (BST) is valid.
853
- * @returns The `isBST` method is returning a boolean value, which indicates whether the binary
854
- * search tree (BST) represented by the given root node is a valid BST or not. The method checks if
855
- * the tree satisfies the BST property, where for every node, all nodes in its left subtree have keys
856
- * less than the node's key, and all nodes in its right subtree have keys greater than the node's
857
- */
858
- isBST(startNode = this._root, iterationType = this.iterationType) {
859
- // TODO there is a bug
860
- startNode = this.ensureNode(startNode);
861
- if (!startNode)
862
- return true;
863
- if (iterationType === 'RECURSIVE') {
864
- const dfs = (cur, min, max) => {
865
- if (!this.isRealNode(cur))
866
- return true;
867
- const numKey = Number(cur.key);
868
- if (numKey <= min || numKey >= max)
869
- return false;
870
- return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
871
- };
872
- const isStandardBST = dfs(startNode, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
873
- const isInverseBST = dfs(startNode, Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
874
- return isStandardBST || isInverseBST;
875
- }
876
- else {
877
- const checkBST = (checkMax = false) => {
878
- const stack = [];
879
- let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
880
- // @ts-ignore
881
- let curr = startNode;
882
- while (this.isRealNode(curr) || stack.length > 0) {
883
- while (this.isRealNode(curr)) {
884
- stack.push(curr);
885
- curr = curr.left;
886
- }
887
- curr = stack.pop();
888
- const numKey = Number(curr.key);
889
- if (!this.isRealNode(curr) || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey))
890
- return false;
891
- prev = numKey;
892
- curr = curr.right;
893
- }
894
- return true;
895
- };
896
- const isStandardBST = checkBST(false), isInverseBST = checkBST(true);
897
- return isStandardBST || isInverseBST;
898
- }
899
- }
900
- /**
901
- * Time Complexity: O(n)
902
- * Space Complexity: O(log n)
903
- *
904
- * The `getDepth` function calculates the depth between two nodes in a binary tree.
905
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } dist - The `dist` parameter in the `getDepth`
906
- * function represents the node or entry in a binary tree map, or a reference to a node in the tree.
907
- * It is the target node for which you want to calculate the depth from the `startNode` node.
908
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
909
- * `getDepth` function represents the starting point from which you want to calculate the depth of a
910
- * given node or entry in a binary tree. If no specific starting point is provided, the default value
911
- * for `startNode` is set to the root of the binary
912
- * @returns The `getDepth` method returns the depth of a given node `dist` relative to the
913
- * `startNode` node in a binary tree. If the `dist` node is not found in the path to the `startNode`
914
- * node, it returns the depth of the `dist` node from the root of the tree.
915
- */
916
- getDepth(dist, startNode = this._root) {
917
- let distEnsured = this.ensureNode(dist);
918
- const beginRootEnsured = this.ensureNode(startNode);
919
- let depth = 0;
920
- while (distEnsured?.parent) {
921
- if (distEnsured === beginRootEnsured) {
922
- return depth;
923
- }
924
- depth++;
925
- distEnsured = distEnsured.parent;
926
- }
927
- return depth;
928
- }
929
- /**
930
- * Time Complexity: O(n)
931
- * Space Complexity: O(log n)
932
- *
933
- * The `getHeight` function calculates the maximum height of a binary tree using either a recursive
934
- * or iterative approach in TypeScript.
935
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter is the starting
936
- * point from which the height of the binary tree will be calculated. It can be a node in the binary
937
- * tree or a reference to the root of the tree. If not provided, it defaults to the root of the
938
- * binary tree data structure.
939
- * @param {IterationType} iterationType - The `iterationType` parameter is used to determine the type
940
- * of iteration to be performed while calculating the height of the binary tree. It can have two
941
- * possible values:
942
- * @returns The `getHeight` method returns the height of the binary tree starting from the specified
943
- * root node. The height is calculated based on the maximum depth of the tree, considering either a
944
- * recursive approach or an iterative approach depending on the `iterationType` parameter.
945
- */
946
- getHeight(startNode = this._root, iterationType = this.iterationType) {
947
- startNode = this.ensureNode(startNode);
948
- if (!this.isRealNode(startNode))
949
- return -1;
950
- if (iterationType === 'RECURSIVE') {
951
- const _getMaxHeight = (cur) => {
952
- if (!this.isRealNode(cur))
953
- return -1;
954
- const leftHeight = _getMaxHeight(cur.left);
955
- const rightHeight = _getMaxHeight(cur.right);
956
- return Math.max(leftHeight, rightHeight) + 1;
957
- };
958
- return _getMaxHeight(startNode);
959
- }
960
- else {
961
- const stack = [{ node: startNode, depth: 0 }];
962
- let maxHeight = 0;
963
- while (stack.length > 0) {
964
- const { node, depth } = stack.pop();
965
- if (this.isRealNode(node.left))
966
- stack.push({ node: node.left, depth: depth + 1 });
967
- if (this.isRealNode(node.right))
968
- stack.push({ node: node.right, depth: depth + 1 });
969
- maxHeight = Math.max(maxHeight, depth);
970
- }
971
- return maxHeight;
972
- }
973
- }
974
- /**
975
- * Time Complexity: O(n)
976
- * Space Complexity: O(log n)
977
- *
978
- * The `getMinHeight` function calculates the minimum height of a binary tree using either a
979
- * recursive or iterative approach in TypeScript.
980
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
981
- * `getMinHeight` function represents the starting node from which the minimum height of the binary
982
- * tree will be calculated. It is either a node in the binary tree or a reference to the root of the
983
- * tree. If not provided, the default value is the root
984
- * @param {IterationType} iterationType - The `iterationType` parameter in the `getMinHeight` method
985
- * specifies the type of iteration to use when calculating the minimum height of a binary tree. It
986
- * can have two possible values:
987
- * @returns The `getMinHeight` method returns the minimum height of the binary tree starting from the
988
- * specified root node. The height is calculated based on the shortest path from the root node to a
989
- * leaf node in the tree. The method uses either a recursive approach or an iterative approach (using
990
- * a stack) based on the `iterationType` parameter.
991
- */
992
- getMinHeight(startNode = this._root, iterationType = this.iterationType) {
993
- startNode = this.ensureNode(startNode);
994
- if (!startNode)
995
- return -1;
996
- if (iterationType === 'RECURSIVE') {
997
- const _getMinHeight = (cur) => {
998
- if (!this.isRealNode(cur))
999
- return 0;
1000
- if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
1001
- return 0;
1002
- const leftMinHeight = _getMinHeight(cur.left);
1003
- const rightMinHeight = _getMinHeight(cur.right);
1004
- return Math.min(leftMinHeight, rightMinHeight) + 1;
1005
- };
1006
- return _getMinHeight(startNode);
1007
- }
1008
- else {
1009
- const stack = [];
1010
- let node = startNode, last = null;
1011
- const depths = new Map();
1012
- while (stack.length > 0 || node) {
1013
- if (this.isRealNode(node)) {
1014
- stack.push(node);
1015
- node = node.left;
1016
- }
1017
- else {
1018
- node = stack[stack.length - 1];
1019
- if (!this.isRealNode(node.right) || last === node.right) {
1020
- node = stack.pop();
1021
- if (this.isRealNode(node)) {
1022
- const leftMinHeight = this.isRealNode(node.left) ? depths.get(node.left) : -1;
1023
- const rightMinHeight = this.isRealNode(node.right) ? depths.get(node.right) : -1;
1024
- depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
1025
- last = node;
1026
- node = null;
1027
- }
1028
- }
1029
- else
1030
- node = node.right;
1031
- }
1032
- }
1033
- return depths.get(startNode);
1034
- }
1035
- }
1036
- /**
1037
- * Time Complexity: O(log n)
1038
- * Space Complexity: O(log n)
1039
- *
1040
- * The function `getPathToRoot` in TypeScript retrieves the path from a given node to the root of a
1041
- * tree structure, applying a specified callback function along the way.
1042
- * @param {C} callback - The `callback` parameter is a function that is used to process each node in
1043
- * the path to the root. It is expected to be a function that takes a node as an argument and returns
1044
- * a value based on that node. The return type of the callback function is determined by the generic
1045
- * type `C
1046
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } beginNode - The `beginNode` parameter in the
1047
- * `getPathToRoot` function can be either a key, a node, an entry, or any other value of type `R`.
1048
- * @param [isReverse=true] - The `isReverse` parameter in the `getPathToRoot` function determines
1049
- * whether the resulting path from the given `beginNode` to the root should be in reverse order or
1050
- * not. If `isReverse` is set to `true`, the path will be reversed before being returned. If `is
1051
- * @returns The function `getPathToRoot` returns an array of the return values of the callback
1052
- * function `callback` applied to each node in the path from the `beginNode` to the root node. The
1053
- * array is either in reverse order or in the original order based on the value of the `isReverse`
1054
- * parameter.
1055
- */
1056
- getPathToRoot(beginNode, callback = this._DEFAULT_NODE_CALLBACK, isReverse = false) {
1057
- const result = [];
1058
- let beginNodeEnsured = this.ensureNode(beginNode);
1059
- if (!beginNodeEnsured)
1060
- return result;
1061
- while (beginNodeEnsured.parent) {
1062
- // Array.push + Array.reverse is more efficient than Array.unshift
1063
- result.push(callback(beginNodeEnsured));
1064
- beginNodeEnsured = beginNodeEnsured.parent;
1065
- }
1066
- result.push(callback(beginNodeEnsured));
1067
- return isReverse ? result.reverse() : result;
1068
- }
1069
- /**
1070
- * Time Complexity: O(log n)
1071
- * Space Complexity: O(log n)
1072
- *
1073
- * The function `getLeftMost` retrieves the leftmost node in a binary tree using either recursive or
1074
- * tail-recursive iteration.
1075
- * @param {C} callback - The `callback` parameter is a function that will be called with the leftmost
1076
- * node of a binary tree or with `undefined` if the tree is empty. It is provided with a default
1077
- * value of `_DEFAULT_NODE_CALLBACK` if not specified.
1078
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
1079
- * `getLeftMost` function represents the starting point for finding the leftmost node in a binary
1080
- * tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
1081
- * starting point is provided, the function will default
1082
- * @param {IterationType} iterationType - The `iterationType` parameter in the `getLeftMost` function
1083
- * specifies the type of iteration to be used when traversing the binary tree nodes. It can have two
1084
- * possible values:
1085
- * @returns The `getLeftMost` function returns the result of the callback function `C` applied to the
1086
- * leftmost node in the binary tree starting from the `startNode` node. If the `startNode` node is
1087
- * `NIL`, it returns the result of the callback function applied to `undefined`. If the `startNode`
1088
- * node is not a real node, it returns the result of the callback
1089
- */
1090
- getLeftMost(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
1091
- if (this.isNIL(startNode))
1092
- return callback(undefined);
1093
- startNode = this.ensureNode(startNode);
1094
- if (!this.isRealNode(startNode))
1095
- return callback(startNode);
1096
- if (iterationType === 'RECURSIVE') {
1097
- const dfs = (cur) => {
1098
- const { left } = cur;
1099
- if (!this.isRealNode(left))
1100
- return cur;
1101
- return dfs(left);
1102
- };
1103
- return callback(dfs(startNode));
1104
- }
1105
- else {
1106
- // Indirect implementation of iteration using tail recursion optimization
1107
- const dfs = makeTrampoline((cur) => {
1108
- const { left } = cur;
1109
- if (!this.isRealNode(left))
1110
- return cur;
1111
- return makeTrampolineThunk(() => dfs(left));
1112
- });
1113
- return callback(dfs(startNode));
1114
- }
1115
- }
1116
- /**
1117
- * Time Complexity: O(log n)
1118
- * Space Complexity: O(log n)
1119
- *
1120
- * The function `getRightMost` retrieves the rightmost node in a binary tree using either recursive
1121
- * or iterative traversal methods.
1122
- * @param {C} callback - The `callback` parameter is a function that will be called with the result
1123
- * of finding the rightmost node in a binary tree. It is of type `NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>`,
1124
- * which means it is a callback function that can accept either an optional binary tree node or null
1125
- * as
1126
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
1127
- * `getRightMost` function represents the starting point for finding the rightmost node in a binary
1128
- * tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
1129
- * starting point is provided, the function will default
1130
- * @param {IterationType} iterationType - The `iterationType` parameter in the `getRightMost`
1131
- * function specifies the type of iteration to be used when traversing the binary tree nodes. It can
1132
- * have two possible values:
1133
- * @returns The `getRightMost` function returns the result of the callback function `C`, which is
1134
- * passed as a parameter to the function. The callback function is called with the rightmost node in
1135
- * the binary tree structure, determined based on the specified iteration type ('RECURSIVE' or
1136
- * other).
1137
- */
1138
- getRightMost(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
1139
- if (this.isNIL(startNode))
1140
- return callback(undefined);
1141
- startNode = this.ensureNode(startNode);
1142
- if (!startNode)
1143
- return callback(startNode);
1144
- if (iterationType === 'RECURSIVE') {
1145
- const dfs = (cur) => {
1146
- const { right } = cur;
1147
- if (!this.isRealNode(right))
1148
- return cur;
1149
- return dfs(right);
1150
- };
1151
- return callback(dfs(startNode));
1152
- }
1153
- else {
1154
- // Indirect implementation of iteration using tail recursion optimization
1155
- const dfs = makeTrampoline((cur) => {
1156
- const { right } = cur;
1157
- if (!this.isRealNode(right))
1158
- return cur;
1159
- return makeTrampolineThunk(() => dfs(right));
1160
- });
1161
- return callback(dfs(startNode));
1162
- }
1163
- }
1164
- /**
1165
- * Time Complexity: O(log n)
1166
- * Space Complexity: O(log n)
1167
- *
1168
- * The function `getPredecessor` in TypeScript returns the predecessor node of a given node in a
1169
- * binary tree.
1170
- * @param {BinaryTreeNode<K, V>} node - The `getPredecessor` function you provided seems to be attempting to find the
1171
- * predecessor of a given node in a binary tree. However, there seems to be a logical issue in the
1172
- * while loop condition that might cause an infinite loop.
1173
- * @returns The `getPredecessor` function returns the predecessor node of the input `BinaryTreeNode<K, V>` parameter.
1174
- * If the left child of the input node exists, it traverses to the rightmost node of the left subtree
1175
- * to find the predecessor. If the left child does not exist, it returns the input node itself.
1176
- */
1177
- getPredecessor(node) {
1178
- if (this.isRealNode(node.left)) {
1179
- let predecessor = node.left;
1180
- while (!this.isRealNode(predecessor) || (this.isRealNode(predecessor.right) && predecessor.right !== node)) {
1181
- if (this.isRealNode(predecessor)) {
1182
- predecessor = predecessor.right;
1183
- }
1184
- }
1185
- return predecessor;
1186
- }
1187
- else {
1188
- return node;
1189
- }
1190
- }
1191
- /**
1192
- * Time Complexity: O(log n)
1193
- * Space Complexity: O(log n)
1194
- *
1195
- * The function `getSuccessor` in TypeScript returns the next node in an in-order traversal of a
1196
- * binary tree.
1197
- * @param {K | BinaryTreeNode<K, V> | null} [x] - The `getSuccessor` function takes a parameter `x`, which can be of
1198
- * type `K`, `BinaryTreeNode<K, V>`, or `null`.
1199
- * @returns The `getSuccessor` function returns the successor node of the input node `x`. If `x` has
1200
- * a right child, the function returns the leftmost node in the right subtree of `x`. If `x` does not
1201
- * have a right child, the function traverses up the parent nodes until it finds a node that is not
1202
- * the right child of its parent, and returns that node
1203
- */
1204
- getSuccessor(x) {
1205
- x = this.ensureNode(x);
1206
- if (!this.isRealNode(x))
1207
- return undefined;
1208
- if (this.isRealNode(x.right)) {
1209
- return this.getLeftMost(node => node, x.right);
1210
- }
1211
- let y = x.parent;
1212
- while (this.isRealNode(y) && x === y.right) {
1213
- x = y;
1214
- y = y.parent;
1215
- }
1216
- return y;
1217
- }
1218
- /**
1219
- * Time complexity: O(n)
1220
- * Space complexity: O(n)
1221
- *
1222
- * The function performs a depth-first search on a binary tree structure based on the specified
1223
- * parameters.
1224
- * @param {C} callback - The `callback` parameter is a function that will be called for each node
1225
- * visited during the depth-first search. It should accept a `BinaryTreeNode` as an argument and
1226
- * return an optional node or null. The default value for this parameter is `_DEFAULT_NODE_CALLBACK`.
1227
- * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `dfs` function specifies
1228
- * the order in which the nodes are visited during a depth-first search traversal. The possible
1229
- * values for the `pattern` parameter are:
1230
- * @param {boolean} [onlyOne=false] - The `onlyOne` parameter in the `dfs` function is a boolean flag
1231
- * that determines whether the depth-first search should stop after finding the first matching node
1232
- * or continue searching for all matching nodes. If `onlyOne` is set to `true`, the search will stop
1233
- * after finding the first matching node
1234
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined}
1235
- * startNode - The `startNode` parameter in the `dfs` function can be one of the following types:
1236
- * @param {IterationType} iterationType - The `iterationType` parameter in the `dfs` function
1237
- * specifies the type of iteration to be performed during the Depth-First Search traversal. It is
1238
- * used to determine the order in which nodes are visited during the traversal. The possible values
1239
- * for `iterationType` are typically defined as an enum or a
1240
- * @param [includeNull=false] - The `includeNull` parameter in the `dfs` function determines whether
1241
- * null nodes should be included in the depth-first search traversal. If `includeNull` is set to
1242
- * `true`, null nodes will be included in the traversal process. If it is set to `false`, null nodes
1243
- * will be skipped
1244
- * @returns The `dfs` method is returning an array of the return type of the callback function `C`.
1245
- */
1246
- dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', onlyOne = false, startNode = this._root, iterationType = this.iterationType, includeNull = false) {
1247
- startNode = this.ensureNode(startNode);
1248
- if (!startNode)
1249
- return [];
1250
- return this._dfs(callback, pattern, onlyOne, startNode, iterationType, includeNull);
1251
- }
1252
- /**
1253
- * Time complexity: O(n)
1254
- * Space complexity: O(n)
1255
- *
1256
- * The `bfs` function performs a breadth-first search traversal on a binary tree or binary search
1257
- * tree, executing a specified callback function on each node visited.
1258
- * @param {C} callback - The `callback` parameter in the `bfs` function is a function that will be
1259
- * called on each node visited during the breadth-first search traversal. It is a generic type `C`
1260
- * that extends the `NodeCallback` type, which takes a parameter of type `BinaryTreeNode<K, V>` or `null`.
1261
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `bfs`
1262
- * function represents the starting point for the breadth-first search traversal in a binary tree. It
1263
- * can be specified as a key, node, or entry in the binary tree structure. If not provided, the
1264
- * default value is the root node of the binary
1265
- * @param {IterationType} iterationType - The `iterationType` parameter in the `bfs` function
1266
- * determines the type of iteration to be performed on the binary tree nodes. It can have two
1267
- * possible values:
1268
- * @param [includeNull=false] - The `includeNull` parameter in the `bfs` function determines whether
1269
- * to include `null` values in the breadth-first search traversal of a binary tree. If `includeNull`
1270
- * is set to `true`, the traversal will include `null` values for nodes that do not have children
1271
- * (left
1272
- * @returns The `bfs` function returns an array of values that are the result of applying the
1273
- * provided callback function to each node in the binary tree in a breadth-first search manner.
1274
- */
1275
- bfs(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType, includeNull = false) {
1276
- startNode = this.ensureNode(startNode);
1277
- if (!startNode)
1278
- return [];
1279
- const ans = [];
1280
- if (iterationType === 'RECURSIVE') {
1281
- const queue = new Queue([
1282
- startNode
1283
- ]);
1284
- const dfs = (level) => {
1285
- if (queue.length === 0)
1286
- return;
1287
- const current = queue.shift();
1288
- ans.push(callback(current));
1289
- if (includeNull) {
1290
- if (current && this.isRealNodeOrNull(current.left))
1291
- queue.push(current.left);
1292
- if (current && this.isRealNodeOrNull(current.right))
1293
- queue.push(current.right);
1294
- }
1295
- else {
1296
- if (this.isRealNode(current.left))
1297
- queue.push(current.left);
1298
- if (this.isRealNode(current.right))
1299
- queue.push(current.right);
1300
- }
1301
- dfs(level + 1);
1302
- };
1303
- dfs(0);
1304
- }
1305
- else {
1306
- const queue = new Queue([startNode]);
1307
- while (queue.length > 0) {
1308
- const levelSize = queue.length;
1309
- for (let i = 0; i < levelSize; i++) {
1310
- const current = queue.shift();
1311
- ans.push(callback(current));
1312
- if (includeNull) {
1313
- if (current && this.isRealNodeOrNull(current.left))
1314
- queue.push(current.left);
1315
- if (current && this.isRealNodeOrNull(current.right))
1316
- queue.push(current.right);
1317
- }
1318
- else {
1319
- if (this.isRealNode(current.left))
1320
- queue.push(current.left);
1321
- if (this.isRealNode(current.right))
1322
- queue.push(current.right);
1323
- }
1324
- }
1325
- }
1326
- }
1327
- return ans;
1328
- }
1329
- /**
1330
- * Time complexity: O(n)
1331
- * Space complexity: O(n)
1332
- *
1333
- * The `leaves` function in TypeScript returns an array of values from leaf nodes in a binary tree
1334
- * structure based on a specified callback and iteration type.
1335
- * @param {C} callback - The `callback` parameter is a function that will be called on each leaf node
1336
- * in the binary tree. It is optional and defaults to a default callback function if not provided.
1337
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `leaves`
1338
- * method is used to specify the starting point for finding and processing the leaves of a binary
1339
- * tree. It can be provided as either a key, a node, or an entry in the binary tree structure. If not
1340
- * explicitly provided, the default value
1341
- * @param {IterationType} iterationType - The `iterationType` parameter in the `leaves` method
1342
- * specifies the type of iteration to be performed when collecting the leaves of a binary tree. It
1343
- * can have two possible values:
1344
- * @returns The `leaves` method returns an array of values that are the result of applying the
1345
- * provided callback function to each leaf node in the binary tree.
1346
- */
1347
- leaves(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
1348
- startNode = this.ensureNode(startNode);
1349
- const leaves = [];
1350
- if (!this.isRealNode(startNode))
1351
- return [];
1352
- if (iterationType === 'RECURSIVE') {
1353
- const dfs = (cur) => {
1354
- if (this.isLeaf(cur)) {
1355
- leaves.push(callback(cur));
1356
- }
1357
- if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
1358
- return;
1359
- if (this.isRealNode(cur.left))
1360
- dfs(cur.left);
1361
- if (this.isRealNode(cur.right))
1362
- dfs(cur.right);
1363
- };
1364
- dfs(startNode);
1365
- }
1366
- else {
1367
- const queue = new Queue([startNode]);
1368
- while (queue.length > 0) {
1369
- const cur = queue.shift();
1370
- if (this.isRealNode(cur)) {
1371
- if (this.isLeaf(cur)) {
1372
- leaves.push(callback(cur));
1373
- }
1374
- if (this.isRealNode(cur.left))
1375
- queue.push(cur.left);
1376
- if (this.isRealNode(cur.right))
1377
- queue.push(cur.right);
1378
- }
1379
- }
1380
- }
1381
- return leaves;
1382
- }
1383
- /**
1384
- * Time complexity: O(n)
1385
- * Space complexity: O(n)
1386
- *
1387
- * The `listLevels` function in TypeScript generates a list of nodes at each level of a binary tree,
1388
- * using either recursive or iterative traversal based on the specified iteration type.
1389
- * @param {C} callback - The `callback` parameter is a function that will be applied to each node in
1390
- * the binary tree during the traversal. It is used to process each node and determine what
1391
- * information to include in the output for each level of the tree.
1392
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
1393
- * `listLevels` function represents the starting point for traversing the binary tree. It can be
1394
- * either a key, a node, or an entry in the binary tree. If not provided, the default value is the
1395
- * root of the binary tree.
1396
- * @param {IterationType} iterationType - The `iterationType` parameter in the `listLevels` function
1397
- * determines the type of iteration to be performed on the binary tree nodes. It can have two
1398
- * possible values:
1399
- * @param [includeNull=false] - The `includeNull` parameter in the `listLevels` method determines
1400
- * whether or not to include null nodes in the traversal of the binary tree. If `includeNull` is set
1401
- * to `true`, the traversal will include null nodes in the levels of the tree. If set to `false`,
1402
- * null
1403
- * @returns The `listLevels` method returns an array of arrays, where each inner array represents a
1404
- * level in a binary tree. Each inner array contains the return value of the provided callback
1405
- * function applied to the nodes at that level.
1406
- */
1407
- listLevels(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType, includeNull = false) {
1408
- startNode = this.ensureNode(startNode);
1409
- const levelsNodes = [];
1410
- if (!startNode)
1411
- return levelsNodes;
1412
- if (iterationType === 'RECURSIVE') {
1413
- const _recursive = (node, level) => {
1414
- if (!levelsNodes[level])
1415
- levelsNodes[level] = [];
1416
- levelsNodes[level].push(callback(node));
1417
- if (includeNull) {
1418
- if (node && this.isRealNodeOrNull(node.left))
1419
- _recursive(node.left, level + 1);
1420
- if (node && this.isRealNodeOrNull(node.right))
1421
- _recursive(node.right, level + 1);
1422
- }
1423
- else {
1424
- if (node && node.left)
1425
- _recursive(node.left, level + 1);
1426
- if (node && node.right)
1427
- _recursive(node.right, level + 1);
1428
- }
1429
- };
1430
- _recursive(startNode, 0);
1431
- }
1432
- else {
1433
- const stack = [[startNode, 0]];
1434
- while (stack.length > 0) {
1435
- const head = stack.pop();
1436
- const [node, level] = head;
1437
- if (!levelsNodes[level])
1438
- levelsNodes[level] = [];
1439
- levelsNodes[level].push(callback(node));
1440
- if (includeNull) {
1441
- if (node && this.isRealNodeOrNull(node.right))
1442
- stack.push([node.right, level + 1]);
1443
- if (node && this.isRealNodeOrNull(node.left))
1444
- stack.push([node.left, level + 1]);
1445
- }
1446
- else {
1447
- if (node && node.right)
1448
- stack.push([node.right, level + 1]);
1449
- if (node && node.left)
1450
- stack.push([node.left, level + 1]);
1451
- }
1452
- }
1453
- }
1454
- return levelsNodes;
1455
- }
1456
- /**
1457
- * Time complexity: O(n)
1458
- * Space complexity: O(n)
1459
- *
1460
- * The `morris` function in TypeScript performs a Depth-First Search traversal on a binary tree using
1461
- * Morris Traversal algorithm with different order patterns.
1462
- * @param {C} callback - The `callback` parameter in the `morris` function is a function that will be
1463
- * called on each node in the binary tree during the traversal. It is of type `C`, which extends the
1464
- * `NodeCallback<BinaryTreeNode<K, V> | null>` type. The default value for `callback` is `this._DEFAULT
1465
- * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function specifies
1466
- * the type of Depth-First Search (DFS) order pattern to traverse the binary tree. The possible
1467
- * values for the `pattern` parameter are:
1468
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `morris`
1469
- * function is the starting point for the Morris traversal algorithm. It represents the root node of
1470
- * the binary tree or the node from which the traversal should begin. It can be provided as either a
1471
- * key, a node, an entry, or a reference
1472
- * @returns The `morris` function is returning an array of values that are the result of applying the
1473
- * provided callback function to each node in the binary tree in the specified order pattern (IN,
1474
- * PRE, or POST).
1475
- */
1476
- morris(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', startNode = this._root) {
1477
- startNode = this.ensureNode(startNode);
1478
- if (!startNode)
1479
- return [];
1480
- const ans = [];
1481
- let cur = startNode;
1482
- const _reverseEdge = (node) => {
1483
- let pre = null;
1484
- let next = null;
1485
- while (node) {
1486
- next = node.right;
1487
- node.right = pre;
1488
- pre = node;
1489
- node = next;
1490
- }
1491
- return pre;
1492
- };
1493
- const _printEdge = (node) => {
1494
- const tail = _reverseEdge(node);
1495
- let cur = tail;
1496
- while (cur) {
1497
- ans.push(callback(cur));
1498
- cur = cur.right;
1499
- }
1500
- _reverseEdge(tail);
1501
- };
1502
- switch (pattern) {
1503
- case 'IN':
1504
- while (cur) {
1505
- if (cur.left) {
1506
- const predecessor = this.getPredecessor(cur);
1507
- if (!predecessor.right) {
1508
- predecessor.right = cur;
1509
- cur = cur.left;
1510
- continue;
1511
- }
1512
- else {
1513
- predecessor.right = null;
1514
- }
1515
- }
1516
- ans.push(callback(cur));
1517
- cur = cur.right;
1518
- }
1519
- break;
1520
- case 'PRE':
1521
- while (cur) {
1522
- if (cur.left) {
1523
- const predecessor = this.getPredecessor(cur);
1524
- if (!predecessor.right) {
1525
- predecessor.right = cur;
1526
- ans.push(callback(cur));
1527
- cur = cur.left;
1528
- continue;
1529
- }
1530
- else {
1531
- predecessor.right = null;
1532
- }
1533
- }
1534
- else {
1535
- ans.push(callback(cur));
1536
- }
1537
- cur = cur.right;
1538
- }
1539
- break;
1540
- case 'POST':
1541
- while (cur) {
1542
- if (cur.left) {
1543
- const predecessor = this.getPredecessor(cur);
1544
- if (predecessor.right === null) {
1545
- predecessor.right = cur;
1546
- cur = cur.left;
1547
- continue;
1548
- }
1549
- else {
1550
- predecessor.right = null;
1551
- _printEdge(cur.left);
1552
- }
1553
- }
1554
- cur = cur.right;
1555
- }
1556
- _printEdge(startNode);
1557
- break;
1558
- }
1559
- return ans;
1560
- }
1561
- /**
1562
- * Time complexity: O(n)
1563
- * Space complexity: O(n)
1564
- *
1565
- * The `clone` function creates a deep copy of a tree structure by traversing it using breadth-first
1566
- * search.
1567
- * @returns The `clone()` method is returning a cloned copy of the tree with the same structure and
1568
- * values as the original tree. The method creates a new tree, iterates over the nodes of the
1569
- * original tree using breadth-first search (bfs), and adds the nodes to the new tree. If a node in
1570
- * the original tree is null, a null node is added to the cloned tree. If a node
1571
- */
1572
- clone() {
1573
- const cloned = this.createTree();
1574
- this._clone(cloned);
1575
- return cloned;
1576
- }
1577
- /**
1578
- * Time Complexity: O(n)
1579
- * Space Complexity: O(n)
1580
- *
1581
- * The `filter` function iterates over key-value pairs in a tree data structure and creates a new
1582
- * tree with elements that satisfy a given predicate.
1583
- * @param predicate - The `predicate` parameter in the `filter` method is a function that will be
1584
- * called with four arguments: the `value` of the current entry, the `key` of the current entry, the
1585
- * `index` of the current entry in the iteration, and the reference to the tree itself (`
1586
- * @param {any} [thisArg] - The `thisArg` parameter in the `filter` method allows you to specify the
1587
- * value of `this` that should be used when executing the `predicate` function. This is useful when
1588
- * the `predicate` function relies on the context of a specific object or value. By providing a
1589
- * `thisArg
1590
- * @returns The `filter` method is returning a new tree that contains entries that pass the provided
1591
- * predicate function.
1592
- */
1593
- filter(predicate, thisArg) {
1594
- const newTree = this.createTree();
1595
- let index = 0;
1596
- for (const [key, value] of this) {
1597
- if (predicate.call(thisArg, key, value, index++, this)) {
1598
- newTree.add([key, value]);
1599
- }
1600
- }
1601
- return newTree;
1602
- }
1603
- /**
1604
- * Time Complexity: O(n)
1605
- * Space Complexity: O(n)
1606
- *
1607
- * The `map` function in TypeScript creates a new BinaryTree by applying a callback function to each
1608
- * entry in the original BinaryTree.
1609
- * @param callback - A function that will be called for each entry in the current binary tree. It
1610
- * takes the key, value (which can be undefined), and an array containing the mapped key and value as
1611
- * arguments.
1612
- * @param [options] - The `options` parameter in the `map` method is of type `BinaryTreeOptions<MK,
1613
- * MV, MR>`. It is an optional parameter that allows you to specify additional options for the binary
1614
- * tree being created during the mapping process. These options could include things like custom
1615
- * comparators, initial
1616
- * @param {any} [thisArg] - The `thisArg` parameter in the `map` method is used to specify the value
1617
- * of `this` when executing the `callback` function. It allows you to set the context (value of
1618
- * `this`) within the callback function. If `thisArg` is provided, it will be passed
1619
- * @returns The `map` function is returning a new `BinaryTree` instance filled with entries that are
1620
- * the result of applying the provided `callback` function to each entry in the original tree.
1621
- */
1622
- map(callback, options, thisArg) {
1623
- const newTree = new BinaryTree([], options);
1624
- let index = 0;
1625
- for (const [key, value] of this) {
1626
- newTree.add(callback.call(thisArg, key, value, index++, this));
1627
- }
1628
- return newTree;
1629
- }
1630
- /**
1631
- * Time Complexity: O(n)
1632
- * Space Complexity: O(n)
1633
- *
1634
- * The function `toVisual` in TypeScript overrides the visual representation of a binary tree with
1635
- * customizable options for displaying undefined, null, and sentinel nodes.
1636
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
1637
- * `toVisual` method is used to specify the starting point for visualizing the binary tree structure.
1638
- * It can be a node, key, entry, or the root of the tree. If no specific starting point is provided,
1639
- * the default is set to the root
1640
- * @param {BinaryTreePrintOptions} [options] - The `options` parameter in the `toVisual` method is an
1641
- * object that contains the following properties:
1642
- * @returns The `override toVisual` method returns a string that represents the visual display of the
1643
- * binary tree based on the provided options for showing undefined, null, and Red-Black NIL nodes.
1644
- * The method constructs the visual representation by calling the `_displayAux` method and appending
1645
- * the lines to the output string. The final output string contains the visual representation of the
1646
- * binary tree with the specified options.
1647
- */
1648
- toVisual(startNode = this._root, options) {
1649
- const opts = { isShowUndefined: false, isShowNull: true, isShowRedBlackNIL: false, ...options };
1650
- startNode = this.ensureNode(startNode);
1651
- let output = '';
1652
- if (!startNode)
1653
- return output;
1654
- if (opts.isShowUndefined)
1655
- output += `U for undefined\n`;
1656
- if (opts.isShowNull)
1657
- output += `N for null\n`;
1658
- if (opts.isShowRedBlackNIL)
1659
- output += `S for Sentinel Node(NIL)\n`;
1660
- const display = (root) => {
1661
- const [lines] = this._displayAux(root, opts);
1662
- let paragraph = '';
1663
- for (const line of lines) {
1664
- paragraph += line + '\n';
1665
- }
1666
- output += paragraph;
1667
- };
1668
- display(startNode);
1669
- return output;
1670
- }
1671
- /**
1672
- * Time Complexity: O(n)
1673
- * Space Complexity: O(n)
1674
- *
1675
- * The function `print` in TypeScript overrides the default print behavior to log a visual
1676
- * representation of the binary tree to the console.
1677
- * @param {BinaryTreePrintOptions} [options] - The `options` parameter is used to specify the
1678
- * printing options for the binary tree. It is an optional parameter that allows you to customize how
1679
- * the binary tree is printed, such as choosing between different traversal orders or formatting
1680
- * options.
1681
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
1682
- * `override print` method is used to specify the starting point for printing the binary tree. It can
1683
- * be either a key, a node, an entry, or the root of the tree. If no specific starting point is
1684
- * provided, the default value is set to
1685
- */
1686
- print(options, startNode = this._root) {
1687
- console.log(this.toVisual(startNode, options));
1688
- }
1689
- _clone(cloned) {
1690
- this.bfs(node => {
1691
- if (node === null)
1692
- cloned.add(null);
1693
- else {
1694
- if (this._isMapMode)
1695
- cloned.add([node.key, this._store.get(node.key)]);
1696
- else
1697
- cloned.add([node.key, node.value]);
1698
- }
1699
- }, this._root, this.iterationType, true);
1700
- if (this._isMapMode)
1701
- cloned._store = this._store;
1702
- }
1703
- /**
1704
- * Time Complexity: O(1)
1705
- * Space Complexity: O(1)
1706
- *
1707
- * The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
1708
- * or returns null.
1709
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The
1710
- * `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeOrEntry`, which
1711
- * can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. This parameter represents either a key, a
1712
- * node, an entry
1713
- * @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
1714
- * an optional parameter of type `V`. It represents the value associated with the key in the node
1715
- * being created. If a `value` is provided, it will be used when creating the node. If
1716
- * @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
1717
- * (`BinaryTreeNode<K, V> | null | undefined`) based on the input parameters provided. The function checks the type of the
1718
- * input parameter (`keyNodeOrEntry`) and processes it accordingly to return a node or null
1719
- * value.
1720
- */
1721
- _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value) {
1722
- if (keyNodeOrEntry === undefined)
1723
- return [undefined, undefined];
1724
- if (keyNodeOrEntry === null)
1725
- return [null, undefined];
1726
- if (this.isNode(keyNodeOrEntry))
1727
- return [keyNodeOrEntry, value];
1728
- if (this.isEntry(keyNodeOrEntry)) {
1729
- const [key, entryValue] = keyNodeOrEntry;
1730
- if (key === undefined)
1731
- return [undefined, undefined];
1732
- else if (key === null)
1733
- return [null, undefined];
1734
- const finalValue = value ?? entryValue;
1735
- return [this.createNode(key, finalValue), finalValue];
1736
- }
1737
- return [this.createNode(keyNodeOrEntry, value), value];
1738
- }
1739
- /**
1740
- * Time complexity: O(n)
1741
- * Space complexity: O(n)
1742
- *
1743
- * The `_dfs` function performs a depth-first search traversal on a binary tree, with customizable
1744
- * options for traversal order and node processing.
1745
- * @param {C} callback - The `callback` parameter in the `_dfs` method is a function that will be
1746
- * called on each node visited during the depth-first search traversal. It is a generic type `C` that
1747
- * extends `NodeCallback<BinaryTreeNode<K, V> | null>`. The default value for `callback`
1748
- * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `_dfs` method specifies the
1749
- * order in which the nodes are visited during a depth-first search traversal. It can have one of the
1750
- * following values:
1751
- * @param {boolean} [onlyOne=false] - The `onlyOne` parameter in the `_dfs` method is a boolean flag
1752
- * that determines whether the traversal should stop after processing a single node. If `onlyOne` is
1753
- * set to `true`, the traversal will return as soon as a single node is processed. If it is set to
1754
- * `false
1755
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined}
1756
- * startNode - The `startNode` parameter in the `_dfs` method is used to specify the starting node
1757
- * for the depth-first search traversal. It can be provided in different forms:
1758
- * @param {IterationType} iterationType - The `iterationType` parameter in the `_dfs` method
1759
- * specifies whether the traversal should be done recursively or iteratively. It can have two
1760
- * possible values:
1761
- * @param [includeNull=false] - The `includeNull` parameter in the `_dfs` method determines whether
1762
- * null nodes should be included in the traversal process. If `includeNull` is set to `true`, the
1763
- * method will consider null nodes as valid nodes to visit or process. If `includeNull` is set to
1764
- * `false`,
1765
- * @param shouldVisitLeft - The `shouldVisitLeft` parameter in the `_dfs` method is a function that
1766
- * determines whether the left child of a node should be visited during the Depth-First Search
1767
- * traversal. By default, it checks if the node is not null or undefined before visiting the left
1768
- * child. You can customize this behavior
1769
- * @param shouldVisitRight - The `shouldVisitRight` parameter in the `_dfs` method is a function that
1770
- * determines whether to visit the right child node of the current node during a depth-first search
1771
- * traversal. The default implementation of this function checks if the node is not null or undefined
1772
- * before deciding to visit it.
1773
- * @param shouldVisitRoot - The `shouldVisitRoot` parameter in the `_dfs` method is a function that
1774
- * determines whether a given node should be visited during the depth-first search traversal. The
1775
- * function takes a node as an argument and returns a boolean value indicating whether the node
1776
- * should be visited.
1777
- * @param shouldProcessRoot - The `shouldProcessRoot` parameter in the `_dfs` method is a function
1778
- * that determines whether the root node should be processed during the Depth-First Search traversal.
1779
- * It takes a node (BinaryTreeNode<K, V> | null | undefined) as input and returns a boolean value. If
1780
- * the function
1781
- * @returns The `_dfs` method returns an array of the return type of the provided callback function
1782
- * `C`.
1783
- */
1784
- _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 => {
1785
- if (includeNull)
1786
- return this.isRealNodeOrNull(node);
1787
- return this.isRealNode(node);
1788
- }, shouldProcessRoot = node => this.isRealNodeOrNull(node)) {
1789
- startNode = this.ensureNode(startNode);
1790
- if (!startNode)
1791
- return [];
1792
- const ans = [];
1793
- if (iterationType === 'RECURSIVE') {
1794
- const dfs = (node) => {
1795
- if (!shouldVisitRoot(node))
1796
- return;
1797
- const visitLeft = () => {
1798
- if (shouldVisitLeft(node) && node?.left !== undefined)
1799
- dfs(node?.left);
1800
- };
1801
- const visitRight = () => {
1802
- if (shouldVisitRight(node) && node?.right !== undefined)
1803
- dfs(node?.right);
1804
- };
1805
- switch (pattern) {
1806
- case 'IN':
1807
- visitLeft();
1808
- if (shouldProcessRoot(node)) {
1809
- ans.push(callback(node));
1810
- if (onlyOne)
1811
- return;
1812
- }
1813
- visitRight();
1814
- break;
1815
- case 'PRE':
1816
- if (shouldProcessRoot(node)) {
1817
- ans.push(callback(node));
1818
- if (onlyOne)
1819
- return;
1820
- }
1821
- visitLeft();
1822
- visitRight();
1823
- break;
1824
- case 'POST':
1825
- visitLeft();
1826
- visitRight();
1827
- if (shouldProcessRoot(node)) {
1828
- ans.push(callback(node));
1829
- if (onlyOne)
1830
- return;
1831
- }
1832
- break;
1833
- }
1834
- };
1835
- dfs(startNode);
1836
- }
1837
- else {
1838
- const stack = [{ opt: DFSOperation.VISIT, node: startNode }];
1839
- const pushLeft = (cur) => {
1840
- if (shouldVisitLeft(cur.node))
1841
- stack.push({ opt: DFSOperation.VISIT, node: cur.node?.left });
1842
- };
1843
- const pushRight = (cur) => {
1844
- if (shouldVisitRight(cur.node))
1845
- stack.push({ opt: DFSOperation.VISIT, node: cur.node?.right });
1846
- };
1847
- const pushRoot = (cur) => {
1848
- if (shouldVisitRoot(cur.node))
1849
- stack.push({ opt: DFSOperation.PROCESS, node: cur.node });
1850
- };
1851
- while (stack.length > 0) {
1852
- const cur = stack.pop();
1853
- if (cur === undefined)
1854
- continue;
1855
- if (!shouldVisitRoot(cur.node))
1856
- continue;
1857
- if (cur.opt === DFSOperation.PROCESS) {
1858
- if (shouldProcessRoot(cur.node) && cur.node !== undefined) {
1859
- ans.push(callback(cur.node));
1860
- if (onlyOne)
1861
- return ans;
1862
- }
1863
- }
1864
- else {
1865
- switch (pattern) {
1866
- case 'IN':
1867
- pushRight(cur);
1868
- pushRoot(cur);
1869
- pushLeft(cur);
1870
- break;
1871
- case 'PRE':
1872
- pushRight(cur);
1873
- pushLeft(cur);
1874
- pushRoot(cur);
1875
- break;
1876
- case 'POST':
1877
- pushRoot(cur);
1878
- pushRight(cur);
1879
- pushLeft(cur);
1880
- break;
1881
- }
1882
- }
1883
- }
1884
- }
1885
- return ans;
1886
- }
1887
- /**
1888
- * Time Complexity: O(1)
1889
- * Space Complexity: O(1)
1890
- *
1891
- * The function `_getIterator` returns an iterable iterator for a binary tree data structure, either
1892
- * using an iterative approach or a recursive approach based on the specified iteration type.
1893
- * @param node - The `node` parameter in the `_getIterator` method represents the current node being
1894
- * processed during iteration. It is initially set to the root node of the data structure (or the
1895
- * node passed as an argument), and then it is traversed through the data structure based on the
1896
- * iteration type specified (`ITER
1897
- * @returns The `_getIterator` method returns an IterableIterator containing key-value pairs of nodes
1898
- * in a binary tree structure. The method uses an iterative approach to traverse the tree based on
1899
- * the `iterationType` property. If the `iterationType` is set to 'ITERATIVE', the method uses a
1900
- * stack to perform an in-order traversal of the tree. If the `iterationType` is not 'ITERATIVE
1901
- */
1902
- *_getIterator(node = this._root) {
1903
- if (!node)
1904
- return;
1905
- if (this.iterationType === 'ITERATIVE') {
1906
- const stack = [];
1907
- let current = node;
1908
- while (current || stack.length > 0) {
1909
- while (this.isRealNode(current)) {
1910
- stack.push(current);
1911
- current = current.left;
1912
- }
1913
- current = stack.pop();
1914
- if (this.isRealNode(current)) {
1915
- if (this._isMapMode)
1916
- yield [current.key, this._store.get(current.key)];
1917
- else
1918
- yield [current.key, current.value];
1919
- current = current.right;
1920
- }
1921
- }
1922
- }
1923
- else {
1924
- if (node.left && this.isRealNode(node)) {
1925
- yield* this[Symbol.iterator](node.left);
1926
- }
1927
- if (this._isMapMode)
1928
- yield [node.key, this._store.get(node.key)];
1929
- else
1930
- yield [node.key, node.value];
1931
- if (node.right && this.isRealNode(node)) {
1932
- yield* this[Symbol.iterator](node.right);
1933
- }
1934
- }
1935
- }
1936
- /**
1937
- * Time Complexity: O(n)
1938
- * Space Complexity: O(n)
1939
- *
1940
- * The function `_displayAux` in TypeScript is responsible for generating the display layout of nodes
1941
- * in a binary tree based on specified options.
1942
- * @param node - The `node` parameter in the `_displayAux` function represents a node in a binary
1943
- * tree. It can be either a valid node containing a key or a special type of node like null,
1944
- * undefined, or a Red-Black tree NIL node. The function checks the type of the node and its
1945
- * @param {BinaryTreePrintOptions} options - The `options` parameter in the `_displayAux` function
1946
- * contains the following properties:
1947
- * @returns The `_displayAux` function returns a `NodeDisplayLayout`, which is an array containing
1948
- * information about how to display a node in a binary tree. The `NodeDisplayLayout` consists of four
1949
- * elements:
1950
- */
1951
- _displayAux(node, options) {
1952
- const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
1953
- const emptyDisplayLayout = [['─'], 1, 0, 0];
1954
- // Check if node is null or undefined or key is NaN
1955
- if (node === null && !isShowNull) {
1956
- return emptyDisplayLayout;
1957
- }
1958
- else if (node === undefined && !isShowUndefined) {
1959
- return emptyDisplayLayout;
1960
- }
1961
- else if (this.isNIL(node) && !isShowRedBlackNIL) {
1962
- return emptyDisplayLayout;
1963
- }
1964
- else if (node !== null && node !== undefined) {
1965
- // Display logic of normal nodes
1966
- const key = node.key, line = this.isNIL(node) ? 'S' : String(key), width = line.length;
1967
- return _buildNodeDisplay(line, width, this._displayAux(node.left, options), this._displayAux(node.right, options));
1968
- }
1969
- else {
1970
- // For cases where none of the conditions are met, null, undefined, and NaN nodes are not displayed
1971
- const line = node === undefined ? 'U' : 'N', width = line.length;
1972
- return _buildNodeDisplay(line, width, [[''], 1, 0, 0], [[''], 1, 0, 0]);
1973
- }
1974
- function _buildNodeDisplay(line, width, left, right) {
1975
- const [leftLines, leftWidth, leftHeight, leftMiddle] = left;
1976
- const [rightLines, rightWidth, rightHeight, rightMiddle] = right;
1977
- const firstLine = ' '.repeat(Math.max(0, leftMiddle + 1)) +
1978
- '_'.repeat(Math.max(0, leftWidth - leftMiddle - 1)) +
1979
- line +
1980
- '_'.repeat(Math.max(0, rightMiddle)) +
1981
- ' '.repeat(Math.max(0, rightWidth - rightMiddle));
1982
- const secondLine = (leftHeight > 0
1983
- ? ' '.repeat(leftMiddle) + '/' + ' '.repeat(leftWidth - leftMiddle - 1)
1984
- : ' '.repeat(leftWidth)) +
1985
- ' '.repeat(width) +
1986
- (rightHeight > 0
1987
- ? ' '.repeat(rightMiddle) + '\\' + ' '.repeat(rightWidth - rightMiddle - 1)
1988
- : ' '.repeat(rightWidth));
1989
- const mergedLines = [firstLine, secondLine];
1990
- for (let i = 0; i < Math.max(leftHeight, rightHeight); i++) {
1991
- const leftLine = i < leftHeight ? leftLines[i] : ' '.repeat(leftWidth);
1992
- const rightLine = i < rightHeight ? rightLines[i] : ' '.repeat(rightWidth);
1993
- mergedLines.push(leftLine + ' '.repeat(width) + rightLine);
1994
- }
1995
- return [
1996
- mergedLines,
1997
- leftWidth + width + rightWidth,
1998
- Math.max(leftHeight, rightHeight) + 2,
1999
- leftWidth + Math.floor(width / 2)
2000
- ];
2001
- }
2002
- }
2003
- _DEFAULT_NODE_CALLBACK = (node) => (node ? node.key : undefined);
2004
- /**
2005
- * Time Complexity: O(1)
2006
- * Space Complexity: O(1)
2007
- *
2008
- * The _swapProperties function swaps key and value properties between two nodes in a binary tree.
2009
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } srcNode - The `srcNode` parameter in the
2010
- * `_swapProperties` method can be either a BTNRep object containing key and value
2011
- * properties, or it can be of type R.
2012
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } destNode - The `destNode` parameter in the
2013
- * `_swapProperties` method represents the node or entry where the properties will be swapped with
2014
- * the `srcNode`. It can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. The method ensures that
2015
- * both `srcNode
2016
- * @returns The `_swapProperties` method returns either the `destNode` with its key and value swapped
2017
- * with the `srcNode`, or `undefined` if either `srcNode` or `destNode` is falsy.
2018
- */
2019
- _swapProperties(srcNode, destNode) {
2020
- srcNode = this.ensureNode(srcNode);
2021
- destNode = this.ensureNode(destNode);
2022
- if (srcNode && destNode) {
2023
- const { key, value } = destNode;
2024
- const tempNode = this.createNode(key, value);
2025
- if (tempNode) {
2026
- destNode.key = srcNode.key;
2027
- if (!this._isMapMode)
2028
- destNode.value = srcNode.value;
2029
- srcNode.key = tempNode.key;
2030
- if (!this._isMapMode)
2031
- srcNode.value = tempNode.value;
2032
- }
2033
- return destNode;
2034
- }
2035
- return undefined;
2036
- }
2037
- /**
2038
- * Time Complexity: O(1)
2039
- * Space Complexity: O(1)
2040
- *
2041
- * The _replaceNode function replaces an old node with a new node in a binary tree structure.
2042
- * @param {BinaryTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that you want to replace in a
2043
- * tree data structure.
2044
- * @param {BinaryTreeNode<K, V>} newNode - The `newNode` parameter in the `_replaceNode` function represents the node
2045
- * that will replace the `oldNode` in a tree data structure. This function is responsible for
2046
- * updating the parent, left child, right child, and root (if necessary) references when replacing a
2047
- * node in the tree.
2048
- * @returns The method `_replaceNode` is returning the `newNode` that was passed as a parameter after
2049
- * replacing the `oldNode` with it in the binary tree structure.
2050
- */
2051
- _replaceNode(oldNode, newNode) {
2052
- if (oldNode.parent) {
2053
- if (oldNode.parent.left === oldNode) {
2054
- oldNode.parent.left = newNode;
2055
- }
2056
- else if (oldNode.parent.right === oldNode) {
2057
- oldNode.parent.right = newNode;
2058
- }
2059
- }
2060
- newNode.left = oldNode.left;
2061
- newNode.right = oldNode.right;
2062
- newNode.parent = oldNode.parent;
2063
- if (this._root === oldNode) {
2064
- this._setRoot(newNode);
2065
- }
2066
- return newNode;
2067
- }
2068
- /**
2069
- * Time Complexity: O(1)
2070
- * Space Complexity: O(1)
2071
- *
2072
- * The function _setRoot sets the root node of a data structure while updating the parent reference
2073
- * of the previous root node.
2074
- * @param v - The parameter `v` in the `_setRoot` method is of type `BinaryTreeNode<K, V> | null | undefined`, which means
2075
- * it can either be an optional `BinaryTreeNode<K, V>` type or `null`.
2076
- */
2077
- _setRoot(v) {
2078
- if (v) {
2079
- v.parent = undefined;
2080
- }
2081
- this._root = v;
2082
- }
2083
- /**
2084
- * Time Complexity: O(1)
2085
- * Space Complexity: O(1)
2086
- *
2087
- * The function `_ensurePredicate` in TypeScript ensures that the input is converted into a valid
2088
- * predicate function for a binary tree node.
2089
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The
2090
- * `_ensurePredicate` method in the provided code snippet is responsible for ensuring that the input
2091
- * parameter `keyNodeEntryOrPredicate` is transformed into a valid predicate function that can be
2092
- * used for filtering nodes in a binary tree.
2093
- * @returns A NodePredicate<BinaryTreeNode<K, V>> function is being returned.
2094
- */
2095
- _ensurePredicate(keyNodeEntryOrPredicate) {
2096
- if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === undefined)
2097
- return (node) => (node ? false : false);
2098
- if (this._isPredicate(keyNodeEntryOrPredicate))
2099
- return keyNodeEntryOrPredicate;
2100
- if (this.isRealNode(keyNodeEntryOrPredicate))
2101
- return (node) => node === keyNodeEntryOrPredicate;
2102
- if (this.isEntry(keyNodeEntryOrPredicate)) {
2103
- const [key] = keyNodeEntryOrPredicate;
2104
- return (node) => {
2105
- if (!node)
2106
- return false;
2107
- return node.key === key;
2108
- };
2109
- }
2110
- return (node) => {
2111
- if (!node)
2112
- return false;
2113
- return node.key === keyNodeEntryOrPredicate;
2114
- };
2115
- }
2116
- /**
2117
- * Time Complexity: O(1)
2118
- * Space Complexity: O(1)
2119
- *
2120
- * The function `_isPredicate` checks if a given parameter is a function.
2121
- * @param {any} p - The parameter `p` is a variable of type `any`, which means it can hold any type
2122
- * of value. In this context, the function `_isPredicate` is checking if `p` is a function that
2123
- * satisfies the type `NodePredicate<BinaryTreeNode<K, V>>`.
2124
- * @returns The function is checking if the input `p` is a function and returning a boolean value
2125
- * based on that check. If `p` is a function, it will return `true`, indicating that `p` is a
2126
- * predicate function for a binary tree node. If `p` is not a function, it will return `false`.
2127
- */
2128
- _isPredicate(p) {
2129
- return typeof p === 'function';
2130
- }
2131
- /**
2132
- * Time Complexity: O(1)
2133
- * Space Complexity: O(1)
2134
- *
2135
- * The function `_extractKey` in TypeScript returns the key from a given input, which can be a node,
2136
- * entry, raw data, or null/undefined.
2137
- * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `_extractKey` method you provided is a
2138
- * TypeScript method that takes in a parameter `keyNodeOrEntry` of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `,
2139
- * where `BTNRep` is a generic type with keys `K`, `V`, and `BinaryTreeNode<K, V>`, and `
2140
- * @returns The `_extractKey` method returns the key value extracted from the `keyNodeOrEntry`
2141
- * parameter. The return value can be a key value of type `K`, `null`, or `undefined`, depending on
2142
- * the conditions checked in the method.
2143
- */
2144
- _extractKey(keyNodeOrEntry) {
2145
- if (keyNodeOrEntry === null)
2146
- return null;
2147
- if (keyNodeOrEntry === undefined)
2148
- return;
2149
- if (keyNodeOrEntry === this._NIL)
2150
- return;
2151
- if (this.isNode(keyNodeOrEntry))
2152
- return keyNodeOrEntry.key;
2153
- if (this.isEntry(keyNodeOrEntry))
2154
- return keyNodeOrEntry[0];
2155
- return keyNodeOrEntry;
2156
- }
2157
- /**
2158
- * Time Complexity: O(1)
2159
- * Space Complexity: O(1)
2160
- *
2161
- * The function `_setValue` sets a value in a store based on a key, handling cases where the key or
2162
- * value is null or undefined.
2163
- * @param {K | null | undefined} key - The `key` parameter can be of type `K`, `null`, or
2164
- * `undefined`.
2165
- * @param {V | undefined} value - The `value` parameter in the `_setValue` method can be of type `V`
2166
- * or `undefined`.
2167
- * @returns The method `_setValue` returns `false` if either the `key` is `null` or `undefined`, or
2168
- * if the `value` is `undefined`. Otherwise, it returns the result of calling the `set` method on the
2169
- * `_store` object with the `key` and `value` arguments.
2170
- */
2171
- _setValue(key, value) {
2172
- if (key === null || key === undefined)
2173
- return false;
2174
- if (value === undefined)
2175
- return false;
2176
- return this._store.set(key, value);
2177
- }
2178
- /**
2179
- * Time Complexity: O(1)
2180
- * Space Complexity: O(1)
2181
- *
2182
- * The _clearNodes function sets the root node to undefined and resets the size to 0.
2183
- */
2184
- _clearNodes() {
2185
- this._setRoot(undefined);
2186
- this._size = 0;
2187
- }
2188
- /**
2189
- * Time Complexity: O(1)
2190
- * Space Complexity: O(1)
2191
- *
2192
- * The _clearValues function clears all values stored in the _store object.
2193
- */
2194
- _clearValues() {
2195
- this._store.clear();
2196
- }
2197
- }
2198
- //# sourceMappingURL=binary-tree.js.map