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,589 @@
1
+ /**
2
+ * data-structure-typed
3
+ * @author Kirk Qi
4
+ * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
5
+ * @license MIT License
6
+ */
7
+
8
+ import type {Comparator, DFSOrderPattern} from '../../types';
9
+
10
+ export class Heap<E = any> {
11
+ constructor(options: { comparator: Comparator<E>; nodes?: E[] }) {
12
+ this._comparator = options.comparator;
13
+ if (options.nodes && options.nodes.length > 0) {
14
+ this._nodes = options.nodes;
15
+ this.fix();
16
+ }
17
+ }
18
+
19
+ protected _nodes: E[] = [];
20
+
21
+ get nodes(): E[] {
22
+ return this._nodes;
23
+ }
24
+
25
+ protected _comparator: Comparator<E>;
26
+
27
+ get comparator(): Comparator<E> {
28
+ return this._comparator;
29
+ }
30
+
31
+ /**
32
+ * Get the size (number of elements) of the heap.
33
+ */
34
+ get size(): number {
35
+ return this.nodes.length;
36
+ }
37
+
38
+ /**
39
+ * Get the last element in the heap, which is not necessarily a leaf node.
40
+ * @returns The last element or undefined if the heap is empty.
41
+ */
42
+ get leaf(): E | undefined {
43
+ return this.nodes[this.size - 1] ?? undefined;
44
+ }
45
+
46
+ /**
47
+ * Static method that creates a binary heap from an array of nodes and a comparison function.
48
+ * @returns A new Heap instance.
49
+ * @param options
50
+ */
51
+ static heapify<E>(options: { nodes: E[]; comparator: Comparator<E> }): Heap<E> {
52
+ return new Heap<E>(options);
53
+ }
54
+
55
+ /**
56
+ * Insert an element into the heap and maintain the heap properties.
57
+ * @param element - The element to be inserted.
58
+ */
59
+ add(element: E): Heap<E> {
60
+ return this.push(element);
61
+ }
62
+
63
+ /**
64
+ * Insert an element into the heap and maintain the heap properties.
65
+ * @param element - The element to be inserted.
66
+ */
67
+ push(element: E): Heap<E> {
68
+ this.nodes.push(element);
69
+ this.bubbleUp(this.nodes.length - 1);
70
+ return this;
71
+ }
72
+
73
+ /**
74
+ * Remove and return the top element (smallest or largest element) from the heap.
75
+ * @returns The top element or undefined if the heap is empty.
76
+ */
77
+ poll(): E | undefined {
78
+ if (this.nodes.length === 0) {
79
+ return undefined;
80
+ }
81
+ if (this.nodes.length === 1) {
82
+ return this.nodes.pop() as E;
83
+ }
84
+
85
+ const topValue = this.nodes[0];
86
+ this.nodes[0] = this.nodes.pop() as E;
87
+ this.sinkDown(0);
88
+ return topValue;
89
+ }
90
+
91
+ /**
92
+ * Remove and return the top element (smallest or largest element) from the heap.
93
+ * @returns The top element or undefined if the heap is empty.
94
+ */
95
+ pop(): E | undefined {
96
+ return this.poll();
97
+ }
98
+
99
+ /**
100
+ * Peek at the top element of the heap without removing it.
101
+ * @returns The top element or undefined if the heap is empty.
102
+ */
103
+ peek(): E | undefined {
104
+ if (this.nodes.length === 0) {
105
+ return undefined;
106
+ }
107
+ return this.nodes[0];
108
+ }
109
+
110
+ /**
111
+ * Check if the heap is empty.
112
+ * @returns True if the heap is empty, otherwise false.
113
+ */
114
+ isEmpty() {
115
+ return this.size === 0;
116
+ }
117
+
118
+ /**
119
+ * Reset the nodes of the heap. Make the nodes empty.
120
+ */
121
+ clear() {
122
+ this._nodes = [];
123
+ }
124
+
125
+ /**
126
+ * Clear and add nodes of the heap
127
+ * @param nodes
128
+ */
129
+ refill(nodes: E[]) {
130
+ this._nodes = nodes;
131
+ this.fix();
132
+ }
133
+
134
+ /**
135
+ * Use a comparison function to check whether a binary heap contains a specific element.
136
+ * @param element - the element to check.
137
+ * @returns Returns true if the specified element is contained; otherwise, returns false.
138
+ */
139
+ has(element: E): boolean {
140
+ return this.nodes.includes(element);
141
+ }
142
+
143
+ /**
144
+ * Depth-first search (DFS) method, different traversal orders can be selected。
145
+ * @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
146
+ * @returns An array containing elements traversed in the specified order.
147
+ */
148
+ dfs(order: DFSOrderPattern): E[] {
149
+ const result: E[] = [];
150
+
151
+ // Auxiliary recursive function, traverses the binary heap according to the traversal order
152
+ const dfsHelper = (index: number) => {
153
+ if (index < this.size) {
154
+ if (order === 'in') {
155
+ dfsHelper(2 * index + 1);
156
+ result.push(this.nodes[index]);
157
+ dfsHelper(2 * index + 2);
158
+ } else if (order === 'pre') {
159
+ result.push(this.nodes[index]);
160
+ dfsHelper(2 * index + 1);
161
+ dfsHelper(2 * index + 2);
162
+ } else if (order === 'post') {
163
+ dfsHelper(2 * index + 1);
164
+ dfsHelper(2 * index + 2);
165
+ result.push(this.nodes[index]);
166
+ }
167
+ }
168
+ };
169
+
170
+ dfsHelper(0); // Traverse starting from the root node
171
+
172
+ return result;
173
+ }
174
+
175
+ /**
176
+ * Convert the heap to an array.
177
+ * @returns An array containing the elements of the heap.
178
+ */
179
+ toArray(): E[] {
180
+ return [...this.nodes];
181
+ }
182
+
183
+ getNodes(): E[] {
184
+ return this.nodes;
185
+ }
186
+
187
+ /**
188
+ * Clone the heap, creating a new heap with the same elements.
189
+ * @returns A new Heap instance containing the same elements.
190
+ */
191
+ clone(): Heap<E> {
192
+ const clonedHeap = new Heap<E>({comparator: this.comparator});
193
+ clonedHeap._nodes = [...this.nodes];
194
+ return clonedHeap;
195
+ }
196
+
197
+ /**
198
+ * Sort the elements in the heap and return them as an array.
199
+ * @returns An array containing the elements sorted in ascending order.
200
+ */
201
+ sort(): E[] {
202
+ const visitedNode: E[] = [];
203
+ const cloned = this.clone();
204
+ while (cloned.size !== 0) {
205
+ const top = cloned.poll();
206
+ if (top) visitedNode.push(top);
207
+ }
208
+ return visitedNode;
209
+ }
210
+
211
+ /**
212
+ * Float operation to maintain heap properties after adding an element.
213
+ * @param index - The index of the newly added element.
214
+ */
215
+ protected bubbleUp(index: number): void {
216
+ const element = this.nodes[index];
217
+ while (index > 0) {
218
+ const parentIndex = Math.floor((index - 1) / 2);
219
+ const parent = this.nodes[parentIndex];
220
+ if (this.comparator(element, parent) < 0) {
221
+ this.nodes[index] = parent;
222
+ this.nodes[parentIndex] = element;
223
+ index = parentIndex;
224
+ } else {
225
+ break;
226
+ }
227
+ }
228
+ }
229
+
230
+ /**
231
+ * Sinking operation to maintain heap properties after removing the top element.
232
+ * @param index - The index from which to start sinking.
233
+ */
234
+ protected sinkDown(index: number): void {
235
+ const leftChildIndex = 2 * index + 1;
236
+ const rightChildIndex = 2 * index + 2;
237
+ const length = this.nodes.length;
238
+ let targetIndex = index;
239
+
240
+ if (leftChildIndex < length && this.comparator(this.nodes[leftChildIndex], this.nodes[targetIndex]) < 0) {
241
+ targetIndex = leftChildIndex;
242
+ }
243
+ if (rightChildIndex < length && this.comparator(this.nodes[rightChildIndex], this.nodes[targetIndex]) < 0) {
244
+ targetIndex = rightChildIndex;
245
+ }
246
+
247
+ if (targetIndex !== index) {
248
+ const temp = this.nodes[index];
249
+ this.nodes[index] = this.nodes[targetIndex];
250
+ this.nodes[targetIndex] = temp;
251
+ this.sinkDown(targetIndex);
252
+ }
253
+ }
254
+
255
+ /**
256
+ * Fix the entire heap to maintain heap properties.
257
+ */
258
+ protected fix() {
259
+ for (let i = Math.floor(this.size / 2); i >= 0; i--) this.sinkDown(i);
260
+ }
261
+ }
262
+
263
+ export class FibonacciHeapNode<E> {
264
+ element: E;
265
+ degree: number;
266
+ left?: FibonacciHeapNode<E>;
267
+ right?: FibonacciHeapNode<E>;
268
+ child?: FibonacciHeapNode<E>;
269
+ parent?: FibonacciHeapNode<E>;
270
+ marked: boolean;
271
+
272
+ constructor(element: E, degree = 0) {
273
+ this.element = element;
274
+ this.degree = degree;
275
+ this.marked = false;
276
+ }
277
+ }
278
+
279
+ export class FibonacciHeap<E> {
280
+ constructor(comparator?: Comparator<E>) {
281
+ this.clear();
282
+ this._comparator = comparator || this.defaultComparator;
283
+
284
+ if (typeof this.comparator !== 'function') {
285
+ throw new Error('FibonacciHeap constructor: given comparator should be a function.');
286
+ }
287
+ }
288
+
289
+ protected _root?: FibonacciHeapNode<E>;
290
+
291
+ get root(): FibonacciHeapNode<E> | undefined {
292
+ return this._root;
293
+ }
294
+
295
+ protected _size = 0;
296
+
297
+ get size(): number {
298
+ return this._size;
299
+ }
300
+
301
+ protected _min?: FibonacciHeapNode<E>;
302
+
303
+ get min(): FibonacciHeapNode<E> | undefined {
304
+ return this._min;
305
+ }
306
+
307
+ protected _comparator: Comparator<E>;
308
+
309
+ get comparator(): Comparator<E> {
310
+ return this._comparator;
311
+ }
312
+
313
+ /**
314
+ * Get the size (number of elements) of the heap.
315
+ * @returns {number} The size of the heap. Returns 0 if the heap is empty. Returns -1 if the heap is invalid.
316
+ */
317
+ clear(): void {
318
+ this._root = undefined;
319
+ this._min = undefined;
320
+ this._size = 0;
321
+ }
322
+
323
+ /**
324
+ * O(1) time operation.
325
+ * Insert an element into the heap and maintain the heap properties.
326
+ * @param element
327
+ * @returns {FibonacciHeap<E>} FibonacciHeap<E> - The heap itself.
328
+ */
329
+ add(element: E): FibonacciHeap<E> {
330
+ return this.push(element);
331
+ }
332
+
333
+ /**
334
+ * O(1) time operation.
335
+ * Insert an element into the heap and maintain the heap properties.
336
+ * @param element
337
+ * @returns {FibonacciHeap<E>} FibonacciHeap<E> - The heap itself.
338
+ */
339
+ push(element: E): FibonacciHeap<E> {
340
+ const node = this.createNode(element);
341
+ node.left = node;
342
+ node.right = node;
343
+ this.mergeWithRoot(node);
344
+
345
+ if (!this.min || this.comparator(node.element, this.min.element) <= 0) {
346
+ this._min = node;
347
+ }
348
+
349
+ this._size++;
350
+ return this;
351
+ }
352
+
353
+ /**
354
+ * O(1) time operation.
355
+ * Peek at the top element of the heap without removing it.
356
+ * @returns The top element or undefined if the heap is empty.
357
+ * @protected
358
+ */
359
+ peek(): E | undefined {
360
+ return this.min ? this.min.element : undefined;
361
+ }
362
+
363
+ /**
364
+ * O(1) time operation.
365
+ * Get the size (number of elements) of the heap.
366
+ * @param {FibonacciHeapNode<E>} head - The head of the linked list.
367
+ * @protected
368
+ * @returns FibonacciHeapNode<E>[] - An array containing the nodes of the linked list.
369
+ */
370
+ consumeLinkedList(head?: FibonacciHeapNode<E>): FibonacciHeapNode<E>[] {
371
+ const nodes: FibonacciHeapNode<E>[] = [];
372
+ if (!head) return nodes;
373
+
374
+ let node: FibonacciHeapNode<E> | undefined = head;
375
+ let flag = false;
376
+
377
+ while (true) {
378
+ if (node === head && flag) break;
379
+ else if (node === head) flag = true;
380
+
381
+ if (node) {
382
+ nodes.push(node);
383
+ node = node.right;
384
+ }
385
+ }
386
+
387
+ return nodes;
388
+ }
389
+
390
+ /**
391
+ * O(log n) time operation.
392
+ * Remove and return the top element (smallest or largest element) from the heap.
393
+ * @param parent
394
+ * @param node
395
+ */
396
+ mergeWithChild(parent: FibonacciHeapNode<E>, node: FibonacciHeapNode<E>): void {
397
+ if (!parent.child) {
398
+ parent.child = node;
399
+ } else {
400
+ node.right = parent.child.right;
401
+ node.left = parent.child;
402
+ parent.child.right!.left = node;
403
+ parent.child.right = node;
404
+ }
405
+ }
406
+
407
+ /**
408
+ * O(log n) time operation.
409
+ * Remove and return the top element (smallest or largest element) from the heap.
410
+ * @returns The top element or undefined if the heap is empty.
411
+ */
412
+ poll(): E | undefined {
413
+ return this.pop();
414
+ }
415
+
416
+ /**
417
+ * O(log n) time operation.
418
+ * Remove and return the top element (smallest or largest element) from the heap.
419
+ * @returns The top element or undefined if the heap is empty.
420
+ */
421
+ pop(): E | undefined {
422
+ if (this.size === 0) return undefined;
423
+
424
+ const z = this.min!;
425
+ if (z.child) {
426
+ const nodes = this.consumeLinkedList(z.child);
427
+ for (const node of nodes) {
428
+ this.mergeWithRoot(node);
429
+ node.parent = undefined;
430
+ }
431
+ }
432
+
433
+ this.removeFromRoot(z);
434
+
435
+ if (z === z.right) {
436
+ this._min = undefined;
437
+ this._root = undefined;
438
+ } else {
439
+ this._min = z.right;
440
+ this.consolidate();
441
+ }
442
+
443
+ this._size--;
444
+
445
+ return z.element;
446
+ }
447
+
448
+ /**
449
+ * O(log n) time operation.
450
+ * merge two heaps. The heap that is merged will be cleared. The heap that is merged into will remain.
451
+ * @param heapToMerge
452
+ */
453
+ merge(heapToMerge: FibonacciHeap<E>): void {
454
+ if (heapToMerge.size === 0) {
455
+ return; // Nothing to merge
456
+ }
457
+
458
+ // Merge the root lists of the two heaps
459
+ if (this.root && heapToMerge.root) {
460
+ const thisRoot = this.root;
461
+ const otherRoot = heapToMerge.root;
462
+
463
+ const thisRootRight = thisRoot.right!;
464
+ const otherRootLeft = otherRoot.left!;
465
+
466
+ thisRoot.right = otherRoot;
467
+ otherRoot.left = thisRoot;
468
+
469
+ thisRootRight.left = otherRootLeft;
470
+ otherRootLeft.right = thisRootRight;
471
+ }
472
+
473
+ // Update the minimum node
474
+ if (!this.min || (heapToMerge.min && this.comparator(heapToMerge.min.element, this.min.element) < 0)) {
475
+ this._min = heapToMerge.min;
476
+ }
477
+
478
+ // Update the size
479
+ this._size += heapToMerge.size;
480
+
481
+ // Clear the heap that was merged
482
+ heapToMerge.clear();
483
+ }
484
+
485
+ /**
486
+ * Default comparator function used by the heap.
487
+ * @param {E} a
488
+ * @param {E} b
489
+ * @protected
490
+ */
491
+ protected defaultComparator(a: E, b: E): number {
492
+ if (a < b) return -1;
493
+ if (a > b) return 1;
494
+ return 0;
495
+ }
496
+
497
+ /**
498
+ * Create a new node.
499
+ * @param element
500
+ * @protected
501
+ */
502
+ protected createNode(element: E): FibonacciHeapNode<E> {
503
+ return new FibonacciHeapNode<E>(element);
504
+ }
505
+
506
+ /**
507
+ * Merge the given node with the root list.
508
+ * @param node - The node to be merged.
509
+ */
510
+ protected mergeWithRoot(node: FibonacciHeapNode<E>): void {
511
+ if (!this.root) {
512
+ this._root = node;
513
+ } else {
514
+ node.right = this.root.right;
515
+ node.left = this.root;
516
+ this.root.right!.left = node;
517
+ this.root.right = node;
518
+ }
519
+ }
520
+
521
+ /**
522
+ * O(log n) time operation.
523
+ * Remove and return the top element (smallest or largest element) from the heap.
524
+ * @param node - The node to be removed.
525
+ * @protected
526
+ */
527
+ protected removeFromRoot(node: FibonacciHeapNode<E>): void {
528
+ if (this.root === node) this._root = node.right;
529
+ if (node.left) node.left.right = node.right;
530
+ if (node.right) node.right.left = node.left;
531
+ }
532
+
533
+ /**
534
+ * O(log n) time operation.
535
+ * Remove and return the top element (smallest or largest element) from the heap.
536
+ * @param y
537
+ * @param x
538
+ * @protected
539
+ */
540
+ protected link(y: FibonacciHeapNode<E>, x: FibonacciHeapNode<E>): void {
541
+ this.removeFromRoot(y);
542
+ y.left = y;
543
+ y.right = y;
544
+ this.mergeWithChild(x, y);
545
+ x.degree++;
546
+ y.parent = x;
547
+ }
548
+
549
+ /**
550
+ * O(log n) time operation.
551
+ * Remove and return the top element (smallest or largest element) from the heap.
552
+ * @protected
553
+ */
554
+ protected consolidate(): void {
555
+ const A: (FibonacciHeapNode<E> | undefined)[] = new Array(this.size);
556
+ const nodes = this.consumeLinkedList(this.root);
557
+ let x: FibonacciHeapNode<E> | undefined,
558
+ y: FibonacciHeapNode<E> | undefined,
559
+ d: number,
560
+ t: FibonacciHeapNode<E> | undefined;
561
+
562
+ for (const node of nodes) {
563
+ x = node;
564
+ d = x.degree;
565
+
566
+ while (A[d]) {
567
+ y = A[d] as FibonacciHeapNode<E>;
568
+
569
+ if (this.comparator(x.element, y.element) > 0) {
570
+ t = x;
571
+ x = y;
572
+ y = t;
573
+ }
574
+
575
+ this.link(y, x);
576
+ A[d] = undefined;
577
+ d++;
578
+ }
579
+
580
+ A[d] = x;
581
+ }
582
+
583
+ for (let i = 0; i < this.size; i++) {
584
+ if (A[i] && this.comparator(A[i]!.element, this.min!.element) <= 0) {
585
+ this._min = A[i]!;
586
+ }
587
+ }
588
+ }
589
+ }
@@ -0,0 +1,3 @@
1
+ export * from './max-heap';
2
+ export * from './min-heap';
3
+ export * from './heap';
@@ -0,0 +1,26 @@
1
+ /**
2
+ * data-structure-typed
3
+ *
4
+ * @author Kirk Qi
5
+ * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
+ * @license MIT License
7
+ */
8
+
9
+ import {Heap} from './heap';
10
+ import type {Comparator} from '../../types';
11
+
12
+ export class MaxHeap<E = any> extends Heap<E> {
13
+ constructor(
14
+ options: { comparator: Comparator<E>; nodes?: E[] } = {
15
+ comparator: (a: E, b: E) => {
16
+ if (!(typeof a === 'number' && typeof b === 'number')) {
17
+ throw new Error('The a, b params of compare function must be number');
18
+ } else {
19
+ return b - a;
20
+ }
21
+ }
22
+ }
23
+ ) {
24
+ super(options);
25
+ }
26
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * data-structure-typed
3
+ *
4
+ * @author Kirk Qi
5
+ * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
+ * @license MIT License
7
+ */
8
+
9
+ import {Heap} from './heap';
10
+ import type {Comparator} from '../../types';
11
+
12
+ export class MinHeap<E = any> extends Heap<E> {
13
+ constructor(
14
+ options: { comparator: Comparator<E>; nodes?: E[] } = {
15
+ comparator: (a: E, b: E) => {
16
+ if (!(typeof a === 'number' && typeof b === 'number')) {
17
+ throw new Error('The a, b params of compare function must be number');
18
+ } else {
19
+ return a - b;
20
+ }
21
+ }
22
+ }
23
+ ) {
24
+ super(options);
25
+ }
26
+ }
@@ -0,0 +1,11 @@
1
+ export * from './hash';
2
+ export * from './linked-list';
3
+ export * from './stack';
4
+ export * from './queue';
5
+ export * from './graph';
6
+ export * from './binary-tree';
7
+ export * from './tree';
8
+ export * from './heap';
9
+ export * from './priority-queue';
10
+ export * from './matrix';
11
+ export * from './trie';