min-heap-typed 1.40.0-rc → 1.41.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (347) hide show
  1. package/.dependency-cruiser.js +422 -422
  2. package/.eslintrc.js +59 -59
  3. package/.prettierrc.js +14 -14
  4. package/README.md +20 -3
  5. package/coverage/clover.xml +11 -7
  6. package/coverage/coverage-final.json +95 -1
  7. package/coverage/coverage-summary.json +59 -2
  8. package/coverage/lcov-report/base.css +278 -99
  9. package/coverage/lcov-report/index.html +69 -65
  10. package/coverage/lcov-report/index.ts.html +36 -35
  11. package/coverage/lcov-report/sorter.js +15 -5
  12. package/dist/data-structures/binary-tree/avl-tree.d.ts +106 -0
  13. package/dist/data-structures/binary-tree/avl-tree.js +347 -0
  14. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +149 -0
  15. package/dist/data-structures/binary-tree/binary-indexed-tree.js +269 -0
  16. package/dist/data-structures/binary-tree/binary-tree.d.ts +363 -0
  17. package/dist/data-structures/binary-tree/binary-tree.js +1135 -0
  18. package/dist/data-structures/binary-tree/bst.d.ts +167 -0
  19. package/dist/data-structures/binary-tree/bst.js +512 -0
  20. package/dist/data-structures/binary-tree/index.d.ts +7 -0
  21. package/dist/data-structures/binary-tree/index.js +23 -0
  22. package/dist/data-structures/binary-tree/rb-tree.d.ts +97 -0
  23. package/dist/data-structures/binary-tree/rb-tree.js +388 -0
  24. package/dist/data-structures/binary-tree/segment-tree.d.ts +67 -0
  25. package/dist/data-structures/binary-tree/segment-tree.js +180 -0
  26. package/dist/data-structures/binary-tree/tree-multiset.d.ts +126 -0
  27. package/dist/data-structures/binary-tree/tree-multiset.js +355 -0
  28. package/dist/data-structures/graph/abstract-graph.d.ts +313 -0
  29. package/dist/data-structures/graph/abstract-graph.js +884 -0
  30. package/dist/data-structures/graph/directed-graph.d.ts +194 -0
  31. package/dist/data-structures/graph/directed-graph.js +404 -0
  32. package/dist/data-structures/graph/index.d.ts +4 -0
  33. package/dist/data-structures/graph/index.js +20 -0
  34. package/dist/data-structures/graph/map-graph.d.ts +73 -0
  35. package/dist/data-structures/graph/map-graph.js +93 -0
  36. package/dist/data-structures/graph/undirected-graph.d.ts +120 -0
  37. package/dist/data-structures/graph/undirected-graph.js +239 -0
  38. package/dist/data-structures/hash/coordinate-map.d.ts +44 -0
  39. package/dist/data-structures/hash/coordinate-map.js +62 -0
  40. package/dist/data-structures/hash/coordinate-set.d.ts +36 -0
  41. package/dist/data-structures/hash/coordinate-set.js +52 -0
  42. package/dist/data-structures/hash/hash-map.d.ts +50 -0
  43. package/dist/data-structures/hash/hash-map.js +153 -0
  44. package/dist/data-structures/hash/hash-table.d.ts +103 -0
  45. package/dist/data-structures/hash/hash-table.js +236 -0
  46. package/dist/data-structures/hash/index.d.ts +6 -0
  47. package/dist/data-structures/hash/index.js +22 -0
  48. package/dist/data-structures/hash/tree-map.d.ts +2 -0
  49. package/dist/data-structures/hash/tree-map.js +6 -0
  50. package/dist/data-structures/hash/tree-set.d.ts +2 -0
  51. package/dist/data-structures/hash/tree-set.js +6 -0
  52. package/dist/data-structures/heap/heap.d.ts +235 -0
  53. package/dist/data-structures/heap/heap.js +515 -0
  54. package/dist/data-structures/heap/index.d.ts +3 -0
  55. package/dist/data-structures/heap/index.js +19 -0
  56. package/dist/data-structures/heap/max-heap.d.ts +15 -0
  57. package/dist/data-structures/heap/max-heap.js +26 -0
  58. package/dist/data-structures/heap/min-heap.d.ts +15 -0
  59. package/dist/data-structures/heap/min-heap.js +26 -0
  60. package/dist/data-structures/index.d.ts +11 -0
  61. package/dist/data-structures/index.js +27 -0
  62. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +253 -0
  63. package/dist/data-structures/linked-list/doubly-linked-list.js +569 -0
  64. package/dist/data-structures/linked-list/index.d.ts +3 -0
  65. package/dist/data-structures/linked-list/index.js +19 -0
  66. package/dist/data-structures/linked-list/singly-linked-list.d.ts +232 -0
  67. package/dist/data-structures/linked-list/singly-linked-list.js +533 -0
  68. package/dist/data-structures/linked-list/skip-linked-list.d.ts +80 -0
  69. package/dist/data-structures/linked-list/skip-linked-list.js +187 -0
  70. package/dist/data-structures/matrix/index.d.ts +4 -0
  71. package/dist/data-structures/matrix/index.js +20 -0
  72. package/dist/data-structures/matrix/matrix.d.ts +21 -0
  73. package/dist/data-structures/matrix/matrix.js +28 -0
  74. package/dist/data-structures/matrix/matrix2d.d.ts +107 -0
  75. package/dist/data-structures/matrix/matrix2d.js +199 -0
  76. package/dist/data-structures/matrix/navigator.d.ts +52 -0
  77. package/dist/data-structures/matrix/navigator.js +106 -0
  78. package/dist/data-structures/matrix/vector2d.d.ts +200 -0
  79. package/dist/data-structures/matrix/vector2d.js +290 -0
  80. package/dist/data-structures/priority-queue/index.d.ts +3 -0
  81. package/dist/data-structures/priority-queue/index.js +19 -0
  82. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +15 -0
  83. package/dist/data-structures/priority-queue/max-priority-queue.js +26 -0
  84. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +15 -0
  85. package/dist/data-structures/priority-queue/min-priority-queue.js +26 -0
  86. package/dist/data-structures/priority-queue/priority-queue.d.ts +15 -0
  87. package/dist/data-structures/priority-queue/priority-queue.js +17 -0
  88. package/dist/data-structures/queue/deque.d.ts +161 -0
  89. package/dist/data-structures/queue/deque.js +264 -0
  90. package/dist/data-structures/queue/index.d.ts +2 -0
  91. package/dist/data-structures/queue/index.js +18 -0
  92. package/dist/data-structures/queue/queue.d.ts +122 -0
  93. package/dist/data-structures/queue/queue.js +187 -0
  94. package/dist/data-structures/stack/index.d.ts +1 -0
  95. package/dist/data-structures/stack/index.js +17 -0
  96. package/dist/data-structures/stack/stack.d.ts +64 -0
  97. package/dist/data-structures/stack/stack.js +94 -0
  98. package/dist/data-structures/tree/index.d.ts +1 -0
  99. package/dist/data-structures/tree/index.js +17 -0
  100. package/dist/data-structures/tree/tree.d.ts +8 -0
  101. package/dist/data-structures/tree/tree.js +40 -0
  102. package/dist/data-structures/trie/index.d.ts +1 -0
  103. package/dist/data-structures/trie/index.js +17 -0
  104. package/dist/data-structures/trie/trie.d.ts +79 -0
  105. package/dist/data-structures/trie/trie.js +251 -0
  106. package/dist/index.d.ts +3 -1
  107. package/dist/index.js +18 -4
  108. package/dist/interfaces/binary-tree.d.ts +7 -0
  109. package/dist/interfaces/binary-tree.js +2 -0
  110. package/dist/interfaces/doubly-linked-list.d.ts +1 -0
  111. package/dist/interfaces/doubly-linked-list.js +2 -0
  112. package/dist/interfaces/graph.d.ts +5 -0
  113. package/dist/interfaces/graph.js +2 -0
  114. package/dist/interfaces/heap.d.ts +1 -0
  115. package/dist/interfaces/heap.js +2 -0
  116. package/dist/interfaces/index.d.ts +8 -0
  117. package/dist/interfaces/index.js +24 -0
  118. package/dist/interfaces/navigator.d.ts +1 -0
  119. package/dist/interfaces/navigator.js +2 -0
  120. package/dist/interfaces/priority-queue.d.ts +1 -0
  121. package/dist/interfaces/priority-queue.js +2 -0
  122. package/dist/interfaces/segment-tree.d.ts +1 -0
  123. package/dist/interfaces/segment-tree.js +2 -0
  124. package/dist/interfaces/singly-linked-list.d.ts +1 -0
  125. package/dist/interfaces/singly-linked-list.js +2 -0
  126. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +4 -0
  127. package/dist/types/data-structures/binary-tree/avl-tree.js +2 -0
  128. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
  129. package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +2 -0
  130. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
  131. package/dist/types/data-structures/binary-tree/binary-tree.js +24 -0
  132. package/dist/types/data-structures/binary-tree/bst.d.ts +7 -0
  133. package/dist/types/data-structures/binary-tree/bst.js +2 -0
  134. package/dist/types/data-structures/binary-tree/index.d.ts +6 -0
  135. package/dist/types/data-structures/binary-tree/index.js +22 -0
  136. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +4 -0
  137. package/dist/types/data-structures/binary-tree/rb-tree.js +13 -0
  138. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
  139. package/dist/types/data-structures/binary-tree/segment-tree.js +2 -0
  140. package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +4 -0
  141. package/dist/types/data-structures/binary-tree/tree-multiset.js +2 -0
  142. package/dist/types/data-structures/graph/abstract-graph.d.ts +10 -0
  143. package/dist/types/data-structures/graph/abstract-graph.js +2 -0
  144. package/dist/types/data-structures/graph/directed-graph.d.ts +6 -0
  145. package/dist/types/data-structures/graph/directed-graph.js +9 -0
  146. package/dist/types/data-structures/graph/index.d.ts +3 -0
  147. package/dist/types/data-structures/graph/index.js +19 -0
  148. package/dist/types/data-structures/graph/map-graph.d.ts +1 -0
  149. package/dist/types/data-structures/graph/map-graph.js +2 -0
  150. package/dist/types/data-structures/graph/undirected-graph.d.ts +1 -0
  151. package/dist/types/data-structures/graph/undirected-graph.js +2 -0
  152. package/dist/types/data-structures/hash/coordinate-map.d.ts +1 -0
  153. package/dist/types/data-structures/hash/coordinate-map.js +2 -0
  154. package/dist/types/data-structures/hash/coordinate-set.d.ts +1 -0
  155. package/dist/types/data-structures/hash/coordinate-set.js +2 -0
  156. package/dist/types/data-structures/hash/hash-map.d.ts +1 -0
  157. package/dist/types/data-structures/hash/hash-map.js +2 -0
  158. package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
  159. package/dist/types/data-structures/hash/hash-table.js +2 -0
  160. package/dist/types/data-structures/hash/index.d.ts +1 -0
  161. package/dist/types/data-structures/hash/index.js +2 -0
  162. package/dist/types/data-structures/hash/tree-map.d.ts +1 -0
  163. package/dist/types/data-structures/hash/tree-map.js +2 -0
  164. package/dist/types/data-structures/hash/tree-set.d.ts +1 -0
  165. package/dist/types/data-structures/hash/tree-set.js +2 -0
  166. package/dist/types/data-structures/heap/heap.d.ts +1 -0
  167. package/dist/types/data-structures/heap/heap.js +2 -0
  168. package/dist/types/data-structures/heap/index.d.ts +1 -0
  169. package/dist/types/data-structures/heap/index.js +17 -0
  170. package/dist/types/data-structures/heap/max-heap.d.ts +1 -0
  171. package/dist/types/data-structures/heap/max-heap.js +2 -0
  172. package/dist/types/data-structures/heap/min-heap.d.ts +1 -0
  173. package/dist/types/data-structures/heap/min-heap.js +2 -0
  174. package/dist/types/data-structures/index.d.ts +11 -0
  175. package/dist/types/data-structures/index.js +27 -0
  176. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -0
  177. package/dist/types/data-structures/linked-list/doubly-linked-list.js +2 -0
  178. package/dist/types/data-structures/linked-list/index.d.ts +2 -0
  179. package/dist/types/data-structures/linked-list/index.js +18 -0
  180. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +1 -0
  181. package/dist/types/data-structures/linked-list/singly-linked-list.js +2 -0
  182. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
  183. package/dist/types/data-structures/linked-list/skip-linked-list.js +2 -0
  184. package/dist/types/data-structures/matrix/index.d.ts +1 -0
  185. package/dist/types/data-structures/matrix/index.js +17 -0
  186. package/dist/types/data-structures/matrix/matrix.d.ts +1 -0
  187. package/dist/types/data-structures/matrix/matrix.js +2 -0
  188. package/dist/types/data-structures/matrix/matrix2d.d.ts +1 -0
  189. package/dist/types/data-structures/matrix/matrix2d.js +2 -0
  190. package/dist/types/data-structures/matrix/navigator.d.ts +14 -0
  191. package/dist/types/data-structures/matrix/navigator.js +2 -0
  192. package/dist/types/data-structures/matrix/vector2d.d.ts +1 -0
  193. package/dist/types/data-structures/matrix/vector2d.js +2 -0
  194. package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
  195. package/dist/types/data-structures/priority-queue/index.js +19 -0
  196. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
  197. package/dist/types/data-structures/priority-queue/max-priority-queue.js +2 -0
  198. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
  199. package/dist/types/data-structures/priority-queue/min-priority-queue.js +2 -0
  200. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  201. package/dist/types/data-structures/priority-queue/priority-queue.js +2 -0
  202. package/dist/types/data-structures/queue/deque.d.ts +1 -0
  203. package/dist/types/data-structures/queue/deque.js +2 -0
  204. package/dist/types/data-structures/queue/index.d.ts +2 -0
  205. package/dist/types/data-structures/queue/index.js +18 -0
  206. package/dist/types/data-structures/queue/queue.d.ts +1 -0
  207. package/dist/types/data-structures/queue/queue.js +2 -0
  208. package/dist/types/data-structures/stack/index.d.ts +1 -0
  209. package/dist/types/data-structures/stack/index.js +17 -0
  210. package/dist/types/data-structures/stack/stack.d.ts +1 -0
  211. package/dist/types/data-structures/stack/stack.js +2 -0
  212. package/dist/types/data-structures/tree/index.d.ts +1 -0
  213. package/dist/types/data-structures/tree/index.js +17 -0
  214. package/dist/types/data-structures/tree/tree.d.ts +1 -0
  215. package/dist/types/data-structures/tree/tree.js +2 -0
  216. package/dist/types/data-structures/trie/index.d.ts +1 -0
  217. package/dist/types/data-structures/trie/index.js +17 -0
  218. package/dist/types/data-structures/trie/trie.d.ts +1 -0
  219. package/dist/types/data-structures/trie/trie.js +2 -0
  220. package/dist/types/helpers.d.ts +8 -0
  221. package/dist/types/helpers.js +9 -0
  222. package/dist/types/index.d.ts +3 -0
  223. package/dist/types/index.js +19 -0
  224. package/dist/types/utils/index.d.ts +2 -0
  225. package/dist/types/utils/index.js +18 -0
  226. package/dist/types/utils/utils.d.ts +7 -0
  227. package/dist/types/utils/utils.js +2 -0
  228. package/dist/types/utils/validate-type.d.ts +19 -0
  229. package/dist/types/utils/validate-type.js +2 -0
  230. package/dist/utils/index.d.ts +1 -0
  231. package/dist/utils/index.js +17 -0
  232. package/dist/utils/utils.d.ts +20 -0
  233. package/dist/utils/utils.js +73 -0
  234. package/jest.config.js +6 -6
  235. package/package.json +2 -2
  236. package/src/data-structures/binary-tree/avl-tree.ts +350 -0
  237. package/src/data-structures/binary-tree/binary-indexed-tree.ts +306 -0
  238. package/src/data-structures/binary-tree/binary-tree.ts +1284 -0
  239. package/src/data-structures/binary-tree/bst.ts +522 -0
  240. package/src/data-structures/binary-tree/index.ts +7 -0
  241. package/src/data-structures/binary-tree/rb-tree.ts +426 -0
  242. package/src/data-structures/binary-tree/segment-tree.ts +190 -0
  243. package/src/data-structures/binary-tree/tree-multiset.ts +379 -0
  244. package/src/data-structures/graph/abstract-graph.ts +1000 -0
  245. package/src/data-structures/graph/directed-graph.ts +449 -0
  246. package/src/data-structures/graph/index.ts +4 -0
  247. package/src/data-structures/graph/map-graph.ts +106 -0
  248. package/src/data-structures/graph/undirected-graph.ts +259 -0
  249. package/src/data-structures/hash/coordinate-map.ts +63 -0
  250. package/src/data-structures/hash/coordinate-set.ts +52 -0
  251. package/src/data-structures/hash/hash-map.ts +185 -0
  252. package/src/data-structures/hash/hash-table.ts +268 -0
  253. package/src/data-structures/hash/index.ts +6 -0
  254. package/src/data-structures/hash/tree-map.ts +2 -0
  255. package/src/data-structures/hash/tree-set.ts +2 -0
  256. package/src/data-structures/heap/heap.ts +589 -0
  257. package/src/data-structures/heap/index.ts +3 -0
  258. package/src/data-structures/heap/max-heap.ts +26 -0
  259. package/src/data-structures/heap/min-heap.ts +26 -0
  260. package/src/data-structures/index.ts +11 -0
  261. package/src/data-structures/linked-list/doubly-linked-list.ts +605 -0
  262. package/src/data-structures/linked-list/index.ts +3 -0
  263. package/src/data-structures/linked-list/singly-linked-list.ts +576 -0
  264. package/src/data-structures/linked-list/skip-linked-list.ts +221 -0
  265. package/src/data-structures/matrix/index.ts +4 -0
  266. package/src/data-structures/matrix/matrix.ts +27 -0
  267. package/src/data-structures/matrix/matrix2d.ts +211 -0
  268. package/src/data-structures/matrix/navigator.ts +121 -0
  269. package/src/data-structures/matrix/vector2d.ts +315 -0
  270. package/src/data-structures/priority-queue/index.ts +3 -0
  271. package/src/data-structures/priority-queue/max-priority-queue.ts +25 -0
  272. package/src/data-structures/priority-queue/min-priority-queue.ts +25 -0
  273. package/src/data-structures/priority-queue/priority-queue.ts +16 -0
  274. package/src/data-structures/queue/deque.ts +282 -0
  275. package/src/data-structures/queue/index.ts +2 -0
  276. package/src/data-structures/queue/queue.ts +209 -0
  277. package/src/data-structures/stack/index.ts +1 -0
  278. package/src/data-structures/stack/stack.ts +102 -0
  279. package/src/data-structures/tree/index.ts +1 -0
  280. package/src/data-structures/tree/tree.ts +41 -0
  281. package/src/data-structures/trie/index.ts +1 -0
  282. package/src/data-structures/trie/trie.ts +262 -0
  283. package/src/index.ts +4 -1
  284. package/src/interfaces/binary-tree.ts +10 -0
  285. package/src/interfaces/doubly-linked-list.ts +1 -0
  286. package/src/interfaces/graph.ts +7 -0
  287. package/src/interfaces/heap.ts +1 -0
  288. package/src/interfaces/index.ts +8 -0
  289. package/src/interfaces/navigator.ts +1 -0
  290. package/src/interfaces/priority-queue.ts +1 -0
  291. package/src/interfaces/segment-tree.ts +1 -0
  292. package/src/interfaces/singly-linked-list.ts +1 -0
  293. package/src/types/data-structures/binary-tree/avl-tree.ts +5 -0
  294. package/src/types/data-structures/binary-tree/binary-indexed-tree.ts +1 -0
  295. package/src/types/data-structures/binary-tree/binary-tree.ts +31 -0
  296. package/src/types/data-structures/binary-tree/bst.ts +11 -0
  297. package/src/types/data-structures/binary-tree/index.ts +6 -0
  298. package/src/types/data-structures/binary-tree/rb-tree.ts +8 -0
  299. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -0
  300. package/src/types/data-structures/binary-tree/tree-multiset.ts +6 -0
  301. package/src/types/data-structures/graph/abstract-graph.ts +11 -0
  302. package/src/types/data-structures/graph/directed-graph.ts +8 -0
  303. package/src/types/data-structures/graph/index.ts +3 -0
  304. package/src/types/data-structures/graph/map-graph.ts +1 -0
  305. package/src/types/data-structures/graph/undirected-graph.ts +1 -0
  306. package/src/types/data-structures/hash/coordinate-map.ts +1 -0
  307. package/src/types/data-structures/hash/coordinate-set.ts +1 -0
  308. package/src/types/data-structures/hash/hash-map.ts +1 -0
  309. package/src/types/data-structures/hash/hash-table.ts +1 -0
  310. package/src/types/data-structures/hash/index.ts +1 -0
  311. package/src/types/data-structures/hash/tree-map.ts +1 -0
  312. package/src/types/data-structures/hash/tree-set.ts +1 -0
  313. package/src/types/data-structures/heap/heap.ts +1 -0
  314. package/src/types/data-structures/heap/index.ts +1 -0
  315. package/src/types/data-structures/heap/max-heap.ts +1 -0
  316. package/src/types/data-structures/heap/min-heap.ts +1 -0
  317. package/src/types/data-structures/index.ts +11 -0
  318. package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -0
  319. package/src/types/data-structures/linked-list/index.ts +2 -0
  320. package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -0
  321. package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -0
  322. package/src/types/data-structures/matrix/index.ts +1 -0
  323. package/src/types/data-structures/matrix/matrix.ts +1 -0
  324. package/src/types/data-structures/matrix/matrix2d.ts +1 -0
  325. package/src/types/data-structures/matrix/navigator.ts +14 -0
  326. package/src/types/data-structures/matrix/vector2d.ts +1 -0
  327. package/src/types/data-structures/priority-queue/index.ts +3 -0
  328. package/src/types/data-structures/priority-queue/max-priority-queue.ts +1 -0
  329. package/src/types/data-structures/priority-queue/min-priority-queue.ts +1 -0
  330. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  331. package/src/types/data-structures/queue/deque.ts +1 -0
  332. package/src/types/data-structures/queue/index.ts +2 -0
  333. package/src/types/data-structures/queue/queue.ts +1 -0
  334. package/src/types/data-structures/stack/index.ts +1 -0
  335. package/src/types/data-structures/stack/stack.ts +1 -0
  336. package/src/types/data-structures/tree/index.ts +1 -0
  337. package/src/types/data-structures/tree/tree.ts +1 -0
  338. package/src/types/data-structures/trie/index.ts +1 -0
  339. package/src/types/data-structures/trie/trie.ts +1 -0
  340. package/src/types/helpers.ts +11 -0
  341. package/src/types/index.ts +3 -0
  342. package/src/types/utils/index.ts +2 -0
  343. package/src/types/utils/utils.ts +6 -0
  344. package/src/types/utils/validate-type.ts +35 -0
  345. package/src/utils/index.ts +1 -0
  346. package/src/utils/utils.ts +86 -0
  347. package/tsconfig.json +1 -2
@@ -0,0 +1,426 @@
1
+ import {RBTNColor} from "../../types";
2
+
3
+ export class RBTreeNode {
4
+ key: number = 0;
5
+ parent: RBTreeNode;
6
+ left: RBTreeNode;
7
+ right: RBTreeNode;
8
+ color: number = RBTNColor.BLACK;
9
+
10
+ constructor() {
11
+ this.parent = null as unknown as RBTreeNode;
12
+ this.left = null as unknown as RBTreeNode;
13
+ this.right = null as unknown as RBTreeNode;
14
+ }
15
+ }
16
+
17
+ export class RedBlackTree {
18
+
19
+ constructor() {
20
+ this._NIL = new RBTreeNode();
21
+ this.NIL.color = RBTNColor.BLACK;
22
+ this.NIL.left = null as unknown as RBTreeNode;
23
+ this.NIL.right = null as unknown as RBTreeNode;
24
+ this._root = this.NIL;
25
+ }
26
+
27
+ protected _root: RBTreeNode;
28
+
29
+ get root(): RBTreeNode {
30
+ return this._root;
31
+ }
32
+
33
+ protected _NIL: RBTreeNode;
34
+
35
+ get NIL(): RBTreeNode {
36
+ return this._NIL;
37
+ }
38
+
39
+ /**
40
+ * The `insert` function inserts a new node with a given key into a red-black tree and fixes any
41
+ * violations of the red-black tree properties.
42
+ * @param {number} key - The key parameter is a number that represents the value to be inserted into
43
+ * the RBTree.
44
+ * @returns The function does not explicitly return anything.
45
+ */
46
+ insert(key: number): void {
47
+
48
+ const node: RBTreeNode = new RBTreeNode();
49
+ node.parent = null as unknown as RBTreeNode;
50
+ node.key = key;
51
+ node.left = this.NIL;
52
+ node.right = this.NIL;
53
+ node.color = RBTNColor.RED;
54
+
55
+ let y: RBTreeNode = null as unknown as RBTreeNode;
56
+ let x: RBTreeNode = this.root;
57
+
58
+ while (x !== this.NIL) {
59
+ y = x;
60
+ if (node.key < x.key) {
61
+ x = x.left;
62
+ } else {
63
+ x = x.right;
64
+ }
65
+ }
66
+
67
+ node.parent = y;
68
+ if (y === null) {
69
+ this._root = node;
70
+ } else if (node.key < y.key) {
71
+ y.left = node;
72
+ } else {
73
+ y.right = node;
74
+ }
75
+
76
+ if (node.parent === null) {
77
+ node.color = RBTNColor.BLACK;
78
+ return;
79
+ }
80
+
81
+ if (node.parent.parent === null) {
82
+ return;
83
+ }
84
+
85
+ this._fixInsert(node);
86
+ }
87
+
88
+ /**
89
+ * The `delete` function in TypeScript is used to remove a node with a specific key from a red-black
90
+ * tree.
91
+ * @param {RBTreeNode} node - The `node` parameter is of type `RBTreeNode` and represents the current
92
+ * node being processed in the delete operation.
93
+ * @returns The `delete` function does not return anything. It has a return type of `void`.
94
+ */
95
+ delete(key: number): void {
96
+ const helper = (node: RBTreeNode): void => {
97
+
98
+ let z: RBTreeNode = this.NIL;
99
+ let x: RBTreeNode, y: RBTreeNode;
100
+ while (node !== this.NIL) {
101
+ if (node.key === key) {
102
+ z = node;
103
+ }
104
+
105
+ if (node.key <= key) {
106
+ node = node.right;
107
+ } else {
108
+ node = node.left;
109
+ }
110
+ }
111
+
112
+ if (z === this.NIL) {
113
+ console.log("Couldn't find key in the tree");
114
+ return;
115
+ }
116
+
117
+ y = z;
118
+ let yOriginalColor: number = y.color;
119
+ if (z.left === this.NIL) {
120
+ x = z.right;
121
+ this._rbTransplant(z, z.right);
122
+ } else if (z.right === this.NIL) {
123
+ x = z.left;
124
+ this._rbTransplant(z, z.left);
125
+ } else {
126
+ y = this.getLeftMost(z.right);
127
+ yOriginalColor = y.color;
128
+ x = y.right;
129
+ if (y.parent === z) {
130
+ x.parent = y;
131
+ } else {
132
+ this._rbTransplant(y, y.right);
133
+ y.right = z.right;
134
+ y.right.parent = y;
135
+ }
136
+
137
+ this._rbTransplant(z, y);
138
+ y.left = z.left;
139
+ y.left.parent = y;
140
+ y.color = z.color;
141
+ }
142
+ if (yOriginalColor === 0) {
143
+ this._fixDelete(x);
144
+ }
145
+ }
146
+ helper(this.root);
147
+ }
148
+
149
+ /**
150
+ * The function `getNode` is a recursive depth-first search algorithm that searches for a node with a
151
+ * given key in a red-black tree.
152
+ * @param {number} key - The key parameter is a number that represents the value we are searching for
153
+ * in the RBTree.
154
+ * @param beginRoot - The `beginRoot` parameter is an optional parameter that represents the starting
155
+ * point for the search in the binary search tree. If no value is provided for `beginRoot`, it
156
+ * defaults to the root of the binary search tree (`this.root`).
157
+ * @returns a RBTreeNode.
158
+ */
159
+ getNode(key: number, beginRoot = this.root): RBTreeNode {
160
+ const dfs = (node: RBTreeNode): RBTreeNode => {
161
+ if (node === this.NIL || key === node.key) {
162
+ return node;
163
+ }
164
+
165
+ if (key < node.key) {
166
+ return dfs(node.left);
167
+ }
168
+ return dfs(node.right);
169
+ }
170
+ return dfs(beginRoot);
171
+ }
172
+
173
+
174
+ /**
175
+ * The function returns the leftmost node in a red-black tree.
176
+ * @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode, which represents a node in
177
+ * a Red-Black Tree.
178
+ * @returns The leftmost node in the given RBTreeNode.
179
+ */
180
+ getLeftMost(node: RBTreeNode): RBTreeNode {
181
+ while (node.left !== null && node.left !== this.NIL) {
182
+ node = node.left;
183
+ }
184
+ return node;
185
+ }
186
+
187
+
188
+ /**
189
+ * The function returns the rightmost node in a red-black tree.
190
+ * @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode.
191
+ * @returns the rightmost node in a red-black tree.
192
+ */
193
+ getRightMost(node: RBTreeNode): RBTreeNode {
194
+ while (node.right !== null && node.right !== this.NIL) {
195
+ node = node.right;
196
+ }
197
+ return node;
198
+ }
199
+
200
+ /**
201
+ * The function returns the successor of a given node in a red-black tree.
202
+ * @param {RBTreeNode} x - RBTreeNode - The node for which we want to find the successor.
203
+ * @returns the successor of the given RBTreeNode.
204
+ */
205
+ getSuccessor(x: RBTreeNode): RBTreeNode {
206
+
207
+ if (x.right !== this.NIL) {
208
+ return this.getLeftMost(x.right);
209
+ }
210
+
211
+ let y: RBTreeNode = x.parent;
212
+ while (y !== this.NIL && y !== null && x === y.right) {
213
+ x = y;
214
+ y = y.parent;
215
+ }
216
+ return y;
217
+ }
218
+
219
+ /**
220
+ * The function returns the predecessor of a given node in a red-black tree.
221
+ * @param {RBTreeNode} x - The parameter `x` is of type `RBTreeNode`, which represents a node in a
222
+ * Red-Black Tree.
223
+ * @returns the predecessor of the given RBTreeNode 'x'.
224
+ */
225
+ getPredecessor(x: RBTreeNode): RBTreeNode {
226
+
227
+ if (x.left !== this.NIL) {
228
+ return this.getRightMost(x.left);
229
+ }
230
+
231
+ let y: RBTreeNode = x.parent;
232
+ while (y !== this.NIL && x === y.left) {
233
+ x = y;
234
+ y = y.parent;
235
+ }
236
+
237
+ return y;
238
+ }
239
+
240
+ /**
241
+ * The function performs a left rotation on a red-black tree node.
242
+ * @param {RBTreeNode} x - The parameter `x` is a RBTreeNode object.
243
+ */
244
+ protected _leftRotate(x: RBTreeNode): void {
245
+ const y: RBTreeNode = x.right;
246
+ x.right = y.left;
247
+ if (y.left !== this.NIL) {
248
+ y.left.parent = x;
249
+ }
250
+ y.parent = x.parent;
251
+ if (x.parent === null) {
252
+ this._root = y;
253
+ } else if (x === x.parent.left) {
254
+ x.parent.left = y;
255
+ } else {
256
+ x.parent.right = y;
257
+ }
258
+ y.left = x;
259
+ x.parent = y;
260
+ }
261
+
262
+ /**
263
+ * The function performs a right rotation on a red-black tree node.
264
+ * @param {RBTreeNode} x - x is a RBTreeNode, which represents the node that needs to be right
265
+ * rotated.
266
+ */
267
+ protected _rightRotate(x: RBTreeNode): void {
268
+ const y: RBTreeNode = x.left;
269
+ x.left = y.right;
270
+ if (y.right !== this.NIL) {
271
+ y.right.parent = x;
272
+ }
273
+ y.parent = x.parent;
274
+ if (x.parent === null) {
275
+ this._root = y;
276
+ } else if (x === x.parent.right) {
277
+ x.parent.right = y;
278
+ } else {
279
+ x.parent.left = y;
280
+ }
281
+ y.right = x;
282
+ x.parent = y;
283
+ }
284
+
285
+ /**
286
+ * The _fixDelete function is used to rebalance the Red-Black Tree after a node deletion.
287
+ * @param {RBTreeNode} x - The parameter `x` is of type `RBTreeNode`, which represents a node in a
288
+ * red-black tree.
289
+ */
290
+ protected _fixDelete(x: RBTreeNode): void {
291
+ let s: RBTreeNode;
292
+ while (x !== this.root && x.color === 0) {
293
+ if (x === x.parent.left) {
294
+ s = x.parent.right;
295
+ if (s.color === 1) {
296
+
297
+ s.color = RBTNColor.BLACK;
298
+ x.parent.color = RBTNColor.RED;
299
+ this._leftRotate(x.parent);
300
+ s = x.parent.right;
301
+ }
302
+
303
+ if (s.left.color === 0 && s.right.color === 0) {
304
+
305
+ s.color = RBTNColor.RED;
306
+ x = x.parent;
307
+ } else {
308
+ if (s.right.color === 0) {
309
+
310
+ s.left.color = RBTNColor.BLACK;
311
+ s.color = RBTNColor.RED;
312
+ this._rightRotate(s);
313
+ s = x.parent.right;
314
+ }
315
+
316
+ s.color = x.parent.color;
317
+ x.parent.color = RBTNColor.BLACK;
318
+ s.right.color = RBTNColor.BLACK;
319
+ this._leftRotate(x.parent);
320
+ x = this.root;
321
+ }
322
+ } else {
323
+ s = x.parent.left;
324
+ if (s.color === 1) {
325
+
326
+ s.color = RBTNColor.BLACK;
327
+ x.parent.color = RBTNColor.RED;
328
+ this._rightRotate(x.parent);
329
+ s = x.parent.left;
330
+ }
331
+
332
+ if (s.right.color === 0 && s.right.color === 0) {
333
+
334
+ s.color = RBTNColor.RED;
335
+ x = x.parent;
336
+ } else {
337
+ if (s.left.color === 0) {
338
+
339
+ s.right.color = RBTNColor.BLACK;
340
+ s.color = RBTNColor.RED;
341
+ this._leftRotate(s);
342
+ s = x.parent.left;
343
+ }
344
+
345
+ s.color = x.parent.color;
346
+ x.parent.color = RBTNColor.BLACK;
347
+ s.left.color = RBTNColor.BLACK;
348
+ this._rightRotate(x.parent);
349
+ x = this.root;
350
+ }
351
+ }
352
+ }
353
+ x.color = RBTNColor.BLACK;
354
+ }
355
+
356
+ /**
357
+ * The function `_rbTransplant` replaces one node in a red-black tree with another node.
358
+ * @param {RBTreeNode} u - The parameter "u" represents a RBTreeNode object.
359
+ * @param {RBTreeNode} v - The parameter "v" is a RBTreeNode object.
360
+ */
361
+ protected _rbTransplant(u: RBTreeNode, v: RBTreeNode): void {
362
+ if (u.parent === null) {
363
+ this._root = v;
364
+ } else if (u === u.parent.left) {
365
+ u.parent.left = v;
366
+ } else {
367
+ u.parent.right = v;
368
+ }
369
+ v.parent = u.parent;
370
+ }
371
+
372
+ /**
373
+ * The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
374
+ * @param {RBTreeNode} k - The parameter `k` is a RBTreeNode object, which represents a node in a
375
+ * red-black tree.
376
+ */
377
+ protected _fixInsert(k: RBTreeNode): void {
378
+ let u: RBTreeNode;
379
+ while (k.parent.color === 1) {
380
+ if (k.parent === k.parent.parent.right) {
381
+ u = k.parent.parent.left;
382
+ if (u.color === 1) {
383
+
384
+ u.color = RBTNColor.BLACK;
385
+ k.parent.color = RBTNColor.BLACK;
386
+ k.parent.parent.color = RBTNColor.RED;
387
+ k = k.parent.parent;
388
+ } else {
389
+ if (k === k.parent.left) {
390
+
391
+ k = k.parent;
392
+ this._rightRotate(k);
393
+ }
394
+
395
+ k.parent.color = RBTNColor.BLACK;
396
+ k.parent.parent.color = RBTNColor.RED;
397
+ this._leftRotate(k.parent.parent);
398
+ }
399
+ } else {
400
+ u = k.parent.parent.right;
401
+
402
+ if (u.color === 1) {
403
+
404
+ u.color = RBTNColor.BLACK;
405
+ k.parent.color = RBTNColor.BLACK;
406
+ k.parent.parent.color = RBTNColor.RED;
407
+ k = k.parent.parent;
408
+ } else {
409
+ if (k === k.parent.right) {
410
+
411
+ k = k.parent;
412
+ this._leftRotate(k);
413
+ }
414
+
415
+ k.parent.color = RBTNColor.BLACK;
416
+ k.parent.parent.color = RBTNColor.RED;
417
+ this._rightRotate(k.parent.parent);
418
+ }
419
+ }
420
+ if (k === this.root) {
421
+ break;
422
+ }
423
+ }
424
+ this.root.color = RBTNColor.BLACK;
425
+ }
426
+ }
@@ -0,0 +1,190 @@
1
+ /**
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
7
+ */
8
+
9
+ import type {SegmentTreeNodeVal} from '../../types';
10
+
11
+ export class SegmentTreeNode {
12
+ start = 0;
13
+ end = 0;
14
+ value: SegmentTreeNodeVal | null = null;
15
+ sum = 0;
16
+ left: SegmentTreeNode | null = null;
17
+ right: SegmentTreeNode | null = null;
18
+
19
+ constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | null) {
20
+ this.start = start;
21
+ this.end = end;
22
+ this.sum = sum;
23
+ this.value = value || null;
24
+ }
25
+ }
26
+
27
+ export class SegmentTree {
28
+ /**
29
+ * The constructor initializes the values, start, end, and root properties of an object.
30
+ * @param {number[]} values - An array of numbers that will be used to build a binary search tree.
31
+ * @param {number} [start] - The `start` parameter is the index of the first element in the `values` array that should
32
+ * be included in the range. If no value is provided for `start`, it defaults to 0, which means the range starts from
33
+ * the beginning of the array.
34
+ * @param {number} [end] - The "end" parameter is the index of the last element in the "values" array that should be
35
+ * included in the range. If not provided, it defaults to the index of the last element in the "values" array.
36
+ */
37
+ constructor(values: number[], start?: number, end?: number) {
38
+ start = start || 0;
39
+ end = end || values.length - 1;
40
+ this._values = values;
41
+ this._start = start;
42
+ this._end = end;
43
+
44
+ if (values.length > 0) {
45
+ this._root = this.build(start, end);
46
+ } else {
47
+ this._root = null;
48
+ this._values = [];
49
+ }
50
+ }
51
+
52
+ protected _values: number[] = [];
53
+
54
+ get values(): number[] {
55
+ return this._values;
56
+ }
57
+
58
+ protected _start = 0;
59
+
60
+ get start(): number {
61
+ return this._start;
62
+ }
63
+
64
+ protected _end: number;
65
+
66
+ get end(): number {
67
+ return this._end;
68
+ }
69
+
70
+ protected _root: SegmentTreeNode | null;
71
+
72
+ get root(): SegmentTreeNode | null {
73
+ return this._root;
74
+ }
75
+
76
+ /**
77
+ * The build function creates a segment tree by recursively dividing the given range into smaller segments and assigning
78
+ * the sum of values to each segment.
79
+ * @param {number} start - The `start` parameter represents the starting index of the segment or range for which we are
80
+ * building the segment tree.
81
+ * @param {number} end - The "end" parameter represents the ending index of the segment or range for which we want to
82
+ * build a segment tree.
83
+ * @returns a SegmentTreeNode object.
84
+ */
85
+ build(start: number, end: number): SegmentTreeNode {
86
+ if (start > end) {
87
+ return new SegmentTreeNode(start, end, 0);
88
+ }
89
+ if (start === end) return new SegmentTreeNode(start, end, this._values[start]);
90
+
91
+ const mid = start + Math.floor((end - start) / 2);
92
+ const left = this.build(start, mid);
93
+ const right = this.build(mid + 1, end);
94
+ const cur = new SegmentTreeNode(start, end, left.sum + right.sum);
95
+ cur.left = left;
96
+ cur.right = right;
97
+ return cur;
98
+ }
99
+
100
+ /**
101
+ * The function updates the value of a node in a segment tree and recalculates the sum of its children if they exist.
102
+ * @param {number} index - The index parameter represents the index of the node in the segment tree that needs to be
103
+ * updated.
104
+ * @param {number} sum - The `sum` parameter represents the new value that should be assigned to the `sum` property of
105
+ * the `SegmentTreeNode` at the specified `index`.
106
+ * @param {SegmentTreeNodeVal} [value] - The `value` parameter is an optional value that can be assigned to the `value`
107
+ * property of the `SegmentTreeNode` object. It is not currently used in the code, but you can uncomment the line `//
108
+ * cur.value = value;` and pass a value for `value` in the
109
+ * @returns The function does not return anything.
110
+ */
111
+ updateNode(index: number, sum: number, value?: SegmentTreeNodeVal) {
112
+ const root = this.root || null;
113
+ if (!root) {
114
+ return;
115
+ }
116
+ const dfs = (cur: SegmentTreeNode, index: number, sum: number, value?: SegmentTreeNodeVal) => {
117
+ if (cur.start === cur.end && cur.start === index) {
118
+ cur.sum = sum;
119
+ if (value !== undefined) cur.value = value;
120
+ return;
121
+ }
122
+ const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
123
+ if (index <= mid) {
124
+ if (cur.left) {
125
+ dfs(cur.left, index, sum, value);
126
+ }
127
+ } else {
128
+ if (cur.right) {
129
+ dfs(cur.right, index, sum, value);
130
+ }
131
+ }
132
+ if (cur.left && cur.right) {
133
+ cur.sum = cur.left.sum + cur.right.sum;
134
+ }
135
+ };
136
+
137
+ dfs(root, index, sum, value);
138
+ }
139
+
140
+ /**
141
+ * The function `querySumByRange` calculates the sum of values within a given range in a segment tree.
142
+ * @param {number} indexA - The starting index of the range for which you want to calculate the sum.
143
+ * @param {number} indexB - The parameter `indexB` represents the ending index of the range for which you want to
144
+ * calculate the sum.
145
+ * @returns The function `querySumByRange` returns a number.
146
+ */
147
+ querySumByRange(indexA: number, indexB: number): number {
148
+ const root = this.root || null;
149
+ if (!root) {
150
+ return 0;
151
+ }
152
+
153
+ if (indexA < 0 || indexB >= this.values.length || indexA > indexB) {
154
+ return NaN;
155
+ }
156
+
157
+ const dfs = (cur: SegmentTreeNode, i: number, j: number): number => {
158
+ if (i <= cur.start && j >= cur.end) {
159
+ // The range [i, j] completely covers the current node's range [cur.start, cur.end]
160
+ return cur.sum;
161
+ }
162
+ const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
163
+ if (j <= mid) {
164
+ if (cur.left) {
165
+ return dfs(cur.left, i, j);
166
+ } else {
167
+ return NaN;
168
+ }
169
+ } else if (i > mid) {
170
+ if (cur.right) {
171
+ return dfs(cur.right, i, j);
172
+ } else {
173
+ return NaN;
174
+ }
175
+ } else {
176
+ // Query both left and right subtrees
177
+ let leftSum = 0;
178
+ let rightSum = 0;
179
+ if (cur.left) {
180
+ leftSum = dfs(cur.left, i, mid);
181
+ }
182
+ if (cur.right) {
183
+ rightSum = dfs(cur.right, mid + 1, j);
184
+ }
185
+ return leftSum + rightSum;
186
+ }
187
+ };
188
+ return dfs(root, indexA, indexB);
189
+ }
190
+ }