red-black-tree-typed 1.47.3

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 (341) hide show
  1. package/.eslintrc.js +61 -0
  2. package/.prettierignore +6 -0
  3. package/.prettierrc.js +16 -0
  4. package/LICENSE +21 -0
  5. package/README.md +713 -0
  6. package/coverage/clover.xml +13 -0
  7. package/coverage/coverage-final.json +96 -0
  8. package/coverage/coverage-summary.json +60 -0
  9. package/coverage/lcov-report/base.css +403 -0
  10. package/coverage/lcov-report/block-navigation.js +87 -0
  11. package/coverage/lcov-report/favicon.png +0 -0
  12. package/coverage/lcov-report/index.html +119 -0
  13. package/coverage/lcov-report/index.ts.html +109 -0
  14. package/coverage/lcov-report/prettify.css +1 -0
  15. package/coverage/lcov-report/prettify.js +2 -0
  16. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  17. package/coverage/lcov-report/sorter.js +206 -0
  18. package/coverage/lcov.info +14 -0
  19. package/dist/data-structures/binary-tree/avl-tree.d.ts +173 -0
  20. package/dist/data-structures/binary-tree/avl-tree.js +429 -0
  21. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +149 -0
  22. package/dist/data-structures/binary-tree/binary-indexed-tree.js +269 -0
  23. package/dist/data-structures/binary-tree/binary-tree.d.ts +515 -0
  24. package/dist/data-structures/binary-tree/binary-tree.js +1665 -0
  25. package/dist/data-structures/binary-tree/bst.d.ts +253 -0
  26. package/dist/data-structures/binary-tree/bst.js +651 -0
  27. package/dist/data-structures/binary-tree/index.d.ts +7 -0
  28. package/dist/data-structures/binary-tree/index.js +23 -0
  29. package/dist/data-structures/binary-tree/rb-tree.d.ts +169 -0
  30. package/dist/data-structures/binary-tree/rb-tree.js +524 -0
  31. package/dist/data-structures/binary-tree/segment-tree.d.ts +67 -0
  32. package/dist/data-structures/binary-tree/segment-tree.js +180 -0
  33. package/dist/data-structures/binary-tree/tree-multimap.d.ts +162 -0
  34. package/dist/data-structures/binary-tree/tree-multimap.js +407 -0
  35. package/dist/data-structures/graph/abstract-graph.d.ts +450 -0
  36. package/dist/data-structures/graph/abstract-graph.js +1047 -0
  37. package/dist/data-structures/graph/directed-graph.d.ts +320 -0
  38. package/dist/data-structures/graph/directed-graph.js +530 -0
  39. package/dist/data-structures/graph/index.d.ts +4 -0
  40. package/dist/data-structures/graph/index.js +20 -0
  41. package/dist/data-structures/graph/map-graph.d.ts +73 -0
  42. package/dist/data-structures/graph/map-graph.js +93 -0
  43. package/dist/data-structures/graph/undirected-graph.d.ts +183 -0
  44. package/dist/data-structures/graph/undirected-graph.js +302 -0
  45. package/dist/data-structures/hash/hash-map.d.ts +186 -0
  46. package/dist/data-structures/hash/hash-map.js +367 -0
  47. package/dist/data-structures/hash/hash-table.d.ts +103 -0
  48. package/dist/data-structures/hash/hash-table.js +236 -0
  49. package/dist/data-structures/hash/index.d.ts +2 -0
  50. package/dist/data-structures/hash/index.js +18 -0
  51. package/dist/data-structures/heap/heap.d.ts +410 -0
  52. package/dist/data-structures/heap/heap.js +697 -0
  53. package/dist/data-structures/heap/index.d.ts +3 -0
  54. package/dist/data-structures/heap/index.js +19 -0
  55. package/dist/data-structures/heap/max-heap.d.ts +15 -0
  56. package/dist/data-structures/heap/max-heap.js +26 -0
  57. package/dist/data-structures/heap/min-heap.d.ts +15 -0
  58. package/dist/data-structures/heap/min-heap.js +26 -0
  59. package/dist/data-structures/index.d.ts +11 -0
  60. package/dist/data-structures/index.js +27 -0
  61. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +456 -0
  62. package/dist/data-structures/linked-list/doubly-linked-list.js +772 -0
  63. package/dist/data-structures/linked-list/index.d.ts +3 -0
  64. package/dist/data-structures/linked-list/index.js +19 -0
  65. package/dist/data-structures/linked-list/singly-linked-list.d.ts +414 -0
  66. package/dist/data-structures/linked-list/singly-linked-list.js +715 -0
  67. package/dist/data-structures/linked-list/skip-linked-list.d.ts +144 -0
  68. package/dist/data-structures/linked-list/skip-linked-list.js +251 -0
  69. package/dist/data-structures/matrix/index.d.ts +4 -0
  70. package/dist/data-structures/matrix/index.js +20 -0
  71. package/dist/data-structures/matrix/matrix.d.ts +21 -0
  72. package/dist/data-structures/matrix/matrix.js +28 -0
  73. package/dist/data-structures/matrix/matrix2d.d.ts +107 -0
  74. package/dist/data-structures/matrix/matrix2d.js +199 -0
  75. package/dist/data-structures/matrix/navigator.d.ts +52 -0
  76. package/dist/data-structures/matrix/navigator.js +106 -0
  77. package/dist/data-structures/matrix/vector2d.d.ts +200 -0
  78. package/dist/data-structures/matrix/vector2d.js +290 -0
  79. package/dist/data-structures/priority-queue/index.d.ts +3 -0
  80. package/dist/data-structures/priority-queue/index.js +19 -0
  81. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +15 -0
  82. package/dist/data-structures/priority-queue/max-priority-queue.js +26 -0
  83. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +15 -0
  84. package/dist/data-structures/priority-queue/min-priority-queue.js +26 -0
  85. package/dist/data-structures/priority-queue/priority-queue.d.ts +15 -0
  86. package/dist/data-structures/priority-queue/priority-queue.js +17 -0
  87. package/dist/data-structures/queue/deque.d.ts +572 -0
  88. package/dist/data-structures/queue/deque.js +990 -0
  89. package/dist/data-structures/queue/index.d.ts +2 -0
  90. package/dist/data-structures/queue/index.js +18 -0
  91. package/dist/data-structures/queue/queue.d.ts +209 -0
  92. package/dist/data-structures/queue/queue.js +274 -0
  93. package/dist/data-structures/stack/index.d.ts +1 -0
  94. package/dist/data-structures/stack/index.js +17 -0
  95. package/dist/data-structures/stack/stack.d.ts +106 -0
  96. package/dist/data-structures/stack/stack.js +136 -0
  97. package/dist/data-structures/tree/index.d.ts +1 -0
  98. package/dist/data-structures/tree/index.js +17 -0
  99. package/dist/data-structures/tree/tree.d.ts +8 -0
  100. package/dist/data-structures/tree/tree.js +40 -0
  101. package/dist/data-structures/trie/index.d.ts +1 -0
  102. package/dist/data-structures/trie/index.js +17 -0
  103. package/dist/data-structures/trie/trie.d.ts +155 -0
  104. package/dist/data-structures/trie/trie.js +326 -0
  105. package/dist/index.d.ts +10 -0
  106. package/dist/index.js +27 -0
  107. package/dist/interfaces/binary-tree.d.ts +7 -0
  108. package/dist/interfaces/binary-tree.js +2 -0
  109. package/dist/interfaces/doubly-linked-list.d.ts +1 -0
  110. package/dist/interfaces/doubly-linked-list.js +2 -0
  111. package/dist/interfaces/graph.d.ts +5 -0
  112. package/dist/interfaces/graph.js +2 -0
  113. package/dist/interfaces/heap.d.ts +1 -0
  114. package/dist/interfaces/heap.js +2 -0
  115. package/dist/interfaces/index.d.ts +8 -0
  116. package/dist/interfaces/index.js +24 -0
  117. package/dist/interfaces/navigator.d.ts +1 -0
  118. package/dist/interfaces/navigator.js +2 -0
  119. package/dist/interfaces/priority-queue.d.ts +1 -0
  120. package/dist/interfaces/priority-queue.js +2 -0
  121. package/dist/interfaces/segment-tree.d.ts +1 -0
  122. package/dist/interfaces/segment-tree.js +2 -0
  123. package/dist/interfaces/singly-linked-list.d.ts +1 -0
  124. package/dist/interfaces/singly-linked-list.js +2 -0
  125. package/dist/types/common.d.ts +20 -0
  126. package/dist/types/common.js +9 -0
  127. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +5 -0
  128. package/dist/types/data-structures/binary-tree/avl-tree.js +2 -0
  129. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
  130. package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +2 -0
  131. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +31 -0
  132. package/dist/types/data-structures/binary-tree/binary-tree.js +24 -0
  133. package/dist/types/data-structures/binary-tree/bst.d.ts +8 -0
  134. package/dist/types/data-structures/binary-tree/bst.js +2 -0
  135. package/dist/types/data-structures/binary-tree/index.d.ts +6 -0
  136. package/dist/types/data-structures/binary-tree/index.js +22 -0
  137. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +9 -0
  138. package/dist/types/data-structures/binary-tree/rb-tree.js +8 -0
  139. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
  140. package/dist/types/data-structures/binary-tree/segment-tree.js +2 -0
  141. package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +5 -0
  142. package/dist/types/data-structures/binary-tree/tree-multimap.js +2 -0
  143. package/dist/types/data-structures/graph/abstract-graph.d.ts +10 -0
  144. package/dist/types/data-structures/graph/abstract-graph.js +2 -0
  145. package/dist/types/data-structures/graph/directed-graph.d.ts +1 -0
  146. package/dist/types/data-structures/graph/directed-graph.js +2 -0
  147. package/dist/types/data-structures/graph/index.d.ts +3 -0
  148. package/dist/types/data-structures/graph/index.js +19 -0
  149. package/dist/types/data-structures/graph/map-graph.d.ts +1 -0
  150. package/dist/types/data-structures/graph/map-graph.js +2 -0
  151. package/dist/types/data-structures/graph/undirected-graph.d.ts +1 -0
  152. package/dist/types/data-structures/graph/undirected-graph.js +2 -0
  153. package/dist/types/data-structures/hash/hash-map.d.ts +11 -0
  154. package/dist/types/data-structures/hash/hash-map.js +2 -0
  155. package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
  156. package/dist/types/data-structures/hash/hash-table.js +2 -0
  157. package/dist/types/data-structures/hash/index.d.ts +3 -0
  158. package/dist/types/data-structures/hash/index.js +18 -0
  159. package/dist/types/data-structures/heap/heap.d.ts +1 -0
  160. package/dist/types/data-structures/heap/heap.js +2 -0
  161. package/dist/types/data-structures/heap/index.d.ts +1 -0
  162. package/dist/types/data-structures/heap/index.js +17 -0
  163. package/dist/types/data-structures/heap/max-heap.d.ts +1 -0
  164. package/dist/types/data-structures/heap/max-heap.js +2 -0
  165. package/dist/types/data-structures/heap/min-heap.d.ts +1 -0
  166. package/dist/types/data-structures/heap/min-heap.js +2 -0
  167. package/dist/types/data-structures/index.d.ts +11 -0
  168. package/dist/types/data-structures/index.js +27 -0
  169. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -0
  170. package/dist/types/data-structures/linked-list/doubly-linked-list.js +2 -0
  171. package/dist/types/data-structures/linked-list/index.d.ts +2 -0
  172. package/dist/types/data-structures/linked-list/index.js +18 -0
  173. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +1 -0
  174. package/dist/types/data-structures/linked-list/singly-linked-list.js +2 -0
  175. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
  176. package/dist/types/data-structures/linked-list/skip-linked-list.js +2 -0
  177. package/dist/types/data-structures/matrix/index.d.ts +1 -0
  178. package/dist/types/data-structures/matrix/index.js +17 -0
  179. package/dist/types/data-structures/matrix/matrix.d.ts +1 -0
  180. package/dist/types/data-structures/matrix/matrix.js +2 -0
  181. package/dist/types/data-structures/matrix/matrix2d.d.ts +1 -0
  182. package/dist/types/data-structures/matrix/matrix2d.js +2 -0
  183. package/dist/types/data-structures/matrix/navigator.d.ts +14 -0
  184. package/dist/types/data-structures/matrix/navigator.js +2 -0
  185. package/dist/types/data-structures/matrix/vector2d.d.ts +1 -0
  186. package/dist/types/data-structures/matrix/vector2d.js +2 -0
  187. package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
  188. package/dist/types/data-structures/priority-queue/index.js +19 -0
  189. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
  190. package/dist/types/data-structures/priority-queue/max-priority-queue.js +2 -0
  191. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
  192. package/dist/types/data-structures/priority-queue/min-priority-queue.js +2 -0
  193. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  194. package/dist/types/data-structures/priority-queue/priority-queue.js +2 -0
  195. package/dist/types/data-structures/queue/deque.d.ts +1 -0
  196. package/dist/types/data-structures/queue/deque.js +2 -0
  197. package/dist/types/data-structures/queue/index.d.ts +2 -0
  198. package/dist/types/data-structures/queue/index.js +18 -0
  199. package/dist/types/data-structures/queue/queue.d.ts +1 -0
  200. package/dist/types/data-structures/queue/queue.js +2 -0
  201. package/dist/types/data-structures/stack/index.d.ts +1 -0
  202. package/dist/types/data-structures/stack/index.js +17 -0
  203. package/dist/types/data-structures/stack/stack.d.ts +1 -0
  204. package/dist/types/data-structures/stack/stack.js +2 -0
  205. package/dist/types/data-structures/tree/index.d.ts +1 -0
  206. package/dist/types/data-structures/tree/index.js +17 -0
  207. package/dist/types/data-structures/tree/tree.d.ts +1 -0
  208. package/dist/types/data-structures/tree/tree.js +2 -0
  209. package/dist/types/data-structures/trie/index.d.ts +1 -0
  210. package/dist/types/data-structures/trie/index.js +17 -0
  211. package/dist/types/data-structures/trie/trie.d.ts +1 -0
  212. package/dist/types/data-structures/trie/trie.js +2 -0
  213. package/dist/types/index.d.ts +3 -0
  214. package/dist/types/index.js +19 -0
  215. package/dist/types/utils/index.d.ts +2 -0
  216. package/dist/types/utils/index.js +18 -0
  217. package/dist/types/utils/utils.d.ts +7 -0
  218. package/dist/types/utils/utils.js +2 -0
  219. package/dist/types/utils/validate-type.d.ts +19 -0
  220. package/dist/types/utils/validate-type.js +2 -0
  221. package/dist/utils/index.d.ts +1 -0
  222. package/dist/utils/index.js +17 -0
  223. package/dist/utils/utils.d.ts +24 -0
  224. package/dist/utils/utils.js +89 -0
  225. package/docs/.nojekyll +1 -0
  226. package/docs/assets/highlight.css +92 -0
  227. package/docs/assets/main.js +59 -0
  228. package/docs/assets/navigation.js +1 -0
  229. package/docs/assets/search.js +1 -0
  230. package/docs/assets/style.css +1383 -0
  231. package/docs/classes/AVLTree.html +2046 -0
  232. package/docs/classes/AVLTreeNode.html +263 -0
  233. package/docs/index.html +523 -0
  234. package/docs/modules.html +45 -0
  235. package/jest.config.js +8 -0
  236. package/package.json +147 -0
  237. package/src/data-structures/binary-tree/avl-tree.ts +443 -0
  238. package/src/data-structures/binary-tree/binary-indexed-tree.ts +306 -0
  239. package/src/data-structures/binary-tree/binary-tree.ts +1974 -0
  240. package/src/data-structures/binary-tree/bst.ts +676 -0
  241. package/src/data-structures/binary-tree/index.ts +7 -0
  242. package/src/data-structures/binary-tree/rb-tree.ts +585 -0
  243. package/src/data-structures/binary-tree/segment-tree.ts +190 -0
  244. package/src/data-structures/binary-tree/tree-multimap.ts +435 -0
  245. package/src/data-structures/graph/abstract-graph.ts +1181 -0
  246. package/src/data-structures/graph/directed-graph.ts +593 -0
  247. package/src/data-structures/graph/index.ts +4 -0
  248. package/src/data-structures/graph/map-graph.ts +106 -0
  249. package/src/data-structures/graph/undirected-graph.ts +331 -0
  250. package/src/data-structures/hash/hash-map.ts +401 -0
  251. package/src/data-structures/hash/hash-table.ts +268 -0
  252. package/src/data-structures/hash/index.ts +2 -0
  253. package/src/data-structures/heap/heap.ts +790 -0
  254. package/src/data-structures/heap/index.ts +3 -0
  255. package/src/data-structures/heap/max-heap.ts +26 -0
  256. package/src/data-structures/heap/min-heap.ts +26 -0
  257. package/src/data-structures/index.ts +11 -0
  258. package/src/data-structures/linked-list/doubly-linked-list.ts +837 -0
  259. package/src/data-structures/linked-list/index.ts +3 -0
  260. package/src/data-structures/linked-list/singly-linked-list.ts +784 -0
  261. package/src/data-structures/linked-list/skip-linked-list.ts +295 -0
  262. package/src/data-structures/matrix/index.ts +4 -0
  263. package/src/data-structures/matrix/matrix.ts +27 -0
  264. package/src/data-structures/matrix/matrix2d.ts +211 -0
  265. package/src/data-structures/matrix/navigator.ts +121 -0
  266. package/src/data-structures/matrix/vector2d.ts +315 -0
  267. package/src/data-structures/priority-queue/index.ts +3 -0
  268. package/src/data-structures/priority-queue/max-priority-queue.ts +25 -0
  269. package/src/data-structures/priority-queue/min-priority-queue.ts +25 -0
  270. package/src/data-structures/priority-queue/priority-queue.ts +16 -0
  271. package/src/data-structures/queue/deque.ts +1073 -0
  272. package/src/data-structures/queue/index.ts +2 -0
  273. package/src/data-structures/queue/queue.ts +308 -0
  274. package/src/data-structures/stack/index.ts +1 -0
  275. package/src/data-structures/stack/stack.ts +150 -0
  276. package/src/data-structures/tree/index.ts +1 -0
  277. package/src/data-structures/tree/tree.ts +41 -0
  278. package/src/data-structures/trie/index.ts +1 -0
  279. package/src/data-structures/trie/trie.ts +345 -0
  280. package/src/index.ts +11 -0
  281. package/src/interfaces/binary-tree.ts +10 -0
  282. package/src/interfaces/doubly-linked-list.ts +1 -0
  283. package/src/interfaces/graph.ts +7 -0
  284. package/src/interfaces/heap.ts +1 -0
  285. package/src/interfaces/index.ts +8 -0
  286. package/src/interfaces/navigator.ts +1 -0
  287. package/src/interfaces/priority-queue.ts +1 -0
  288. package/src/interfaces/segment-tree.ts +1 -0
  289. package/src/interfaces/singly-linked-list.ts +1 -0
  290. package/src/types/common.ts +23 -0
  291. package/src/types/data-structures/binary-tree/avl-tree.ts +9 -0
  292. package/src/types/data-structures/binary-tree/binary-indexed-tree.ts +1 -0
  293. package/src/types/data-structures/binary-tree/binary-tree.ts +35 -0
  294. package/src/types/data-structures/binary-tree/bst.ts +13 -0
  295. package/src/types/data-structures/binary-tree/index.ts +6 -0
  296. package/src/types/data-structures/binary-tree/rb-tree.ts +10 -0
  297. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -0
  298. package/src/types/data-structures/binary-tree/tree-multimap.ts +8 -0
  299. package/src/types/data-structures/graph/abstract-graph.ts +11 -0
  300. package/src/types/data-structures/graph/directed-graph.ts +2 -0
  301. package/src/types/data-structures/graph/index.ts +3 -0
  302. package/src/types/data-structures/graph/map-graph.ts +1 -0
  303. package/src/types/data-structures/graph/undirected-graph.ts +1 -0
  304. package/src/types/data-structures/hash/hash-map.ts +12 -0
  305. package/src/types/data-structures/hash/hash-table.ts +1 -0
  306. package/src/types/data-structures/hash/index.ts +4 -0
  307. package/src/types/data-structures/heap/heap.ts +1 -0
  308. package/src/types/data-structures/heap/index.ts +1 -0
  309. package/src/types/data-structures/heap/max-heap.ts +1 -0
  310. package/src/types/data-structures/heap/min-heap.ts +1 -0
  311. package/src/types/data-structures/index.ts +11 -0
  312. package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -0
  313. package/src/types/data-structures/linked-list/index.ts +2 -0
  314. package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -0
  315. package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -0
  316. package/src/types/data-structures/matrix/index.ts +1 -0
  317. package/src/types/data-structures/matrix/matrix.ts +1 -0
  318. package/src/types/data-structures/matrix/matrix2d.ts +1 -0
  319. package/src/types/data-structures/matrix/navigator.ts +14 -0
  320. package/src/types/data-structures/matrix/vector2d.ts +1 -0
  321. package/src/types/data-structures/priority-queue/index.ts +3 -0
  322. package/src/types/data-structures/priority-queue/max-priority-queue.ts +1 -0
  323. package/src/types/data-structures/priority-queue/min-priority-queue.ts +1 -0
  324. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  325. package/src/types/data-structures/queue/deque.ts +1 -0
  326. package/src/types/data-structures/queue/index.ts +2 -0
  327. package/src/types/data-structures/queue/queue.ts +1 -0
  328. package/src/types/data-structures/stack/index.ts +1 -0
  329. package/src/types/data-structures/stack/stack.ts +1 -0
  330. package/src/types/data-structures/tree/index.ts +1 -0
  331. package/src/types/data-structures/tree/tree.ts +1 -0
  332. package/src/types/data-structures/trie/index.ts +1 -0
  333. package/src/types/data-structures/trie/trie.ts +1 -0
  334. package/src/types/index.ts +3 -0
  335. package/src/types/utils/index.ts +2 -0
  336. package/src/types/utils/utils.ts +6 -0
  337. package/src/types/utils/validate-type.ts +35 -0
  338. package/src/utils/index.ts +1 -0
  339. package/src/utils/utils.ts +101 -0
  340. package/test/index.test.ts +111 -0
  341. package/tsconfig.json +38 -0
@@ -0,0 +1,697 @@
1
+ "use strict";
2
+ /**
3
+ * data-structure-typed
4
+ * @author Kirk Qi
5
+ * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
+ * @license MIT License
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.FibonacciHeap = exports.FibonacciHeapNode = exports.Heap = void 0;
10
+ class Heap {
11
+ constructor(options) {
12
+ this._elements = [];
13
+ this._comparator = options.comparator;
14
+ if (options.elements && options.elements.length > 0) {
15
+ this._elements = options.elements;
16
+ this.fix();
17
+ }
18
+ }
19
+ get elements() {
20
+ return this._elements;
21
+ }
22
+ get comparator() {
23
+ return this._comparator;
24
+ }
25
+ /**
26
+ * Get the size (number of elements) of the heap.
27
+ */
28
+ get size() {
29
+ return this.elements.length;
30
+ }
31
+ /**
32
+ * Get the last element in the heap, which is not necessarily a leaf node.
33
+ * @returns The last element or undefined if the heap is empty.
34
+ */
35
+ get leaf() {
36
+ var _a;
37
+ return (_a = this.elements[this.size - 1]) !== null && _a !== void 0 ? _a : undefined;
38
+ }
39
+ /**
40
+ * Static method that creates a binary heap from an array of elements and a comparison function.
41
+ * @returns A new Heap instance.
42
+ * @param options
43
+ */
44
+ static heapify(options) {
45
+ return new Heap(options);
46
+ }
47
+ /**
48
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
49
+ * Space Complexity: O(1)
50
+ */
51
+ /**
52
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
53
+ * Space Complexity: O(1)
54
+ *
55
+ * Insert an element into the heap and maintain the heap properties.
56
+ * @param element - The element to be inserted.
57
+ */
58
+ add(element) {
59
+ return this.push(element);
60
+ }
61
+ /**
62
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
63
+ * Space Complexity: O(1)
64
+ */
65
+ /**
66
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
67
+ * Space Complexity: O(1)
68
+ *
69
+ * Insert an element into the heap and maintain the heap properties.
70
+ * @param element - The element to be inserted.
71
+ */
72
+ push(element) {
73
+ this._elements.push(element);
74
+ this._bubbleUp(this.elements.length - 1);
75
+ return this;
76
+ }
77
+ /**
78
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
79
+ * Space Complexity: O(1)
80
+ */
81
+ /**
82
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
83
+ * Space Complexity: O(1)
84
+ *
85
+ * Remove and return the top element (smallest or largest element) from the heap.
86
+ * @returns The top element or undefined if the heap is empty.
87
+ */
88
+ poll() {
89
+ if (this.elements.length === 0)
90
+ return;
91
+ const value = this.elements[0];
92
+ const last = this.elements.pop();
93
+ if (this.elements.length) {
94
+ this.elements[0] = last;
95
+ this._sinkDown(0, this.elements.length >> 1);
96
+ }
97
+ return value;
98
+ }
99
+ /**
100
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
101
+ * Space Complexity: O(1)
102
+ */
103
+ /**
104
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
105
+ * Space Complexity: O(1)
106
+ *
107
+ * Remove and return the top element (smallest or largest element) from the heap.
108
+ * @returns The top element or undefined if the heap is empty.
109
+ */
110
+ pop() {
111
+ return this.poll();
112
+ }
113
+ /**
114
+ * Peek at the top element of the heap without removing it.
115
+ * @returns The top element or undefined if the heap is empty.
116
+ */
117
+ peek() {
118
+ return this.elements[0];
119
+ }
120
+ /**
121
+ * Check if the heap is empty.
122
+ * @returns True if the heap is empty, otherwise false.
123
+ */
124
+ isEmpty() {
125
+ return this.size === 0;
126
+ }
127
+ /**
128
+ * Reset the elements of the heap. Make the elements empty.
129
+ */
130
+ clear() {
131
+ this._elements = [];
132
+ }
133
+ /**
134
+ * Time Complexity: O(n), where n is the number of elements in the elements array.
135
+ * Space Complexity: O(n)
136
+ */
137
+ /**
138
+ * Time Complexity: O(n), where n is the number of elements in the elements array.
139
+ * Space Complexity: O(n)
140
+ *
141
+ * Clear and add elements of the heap
142
+ * @param elements
143
+ */
144
+ refill(elements) {
145
+ this._elements = elements;
146
+ this.fix();
147
+ }
148
+ /**
149
+ * Time Complexity: O(n), where n is the number of elements in the heap.
150
+ * Space Complexity: O(1)
151
+ */
152
+ /**
153
+ * Time Complexity: O(n), where n is the number of elements in the heap.
154
+ * Space Complexity: O(1)
155
+ *
156
+ * Use a comparison function to check whether a binary heap contains a specific element.
157
+ * @param element - the element to check.
158
+ * @returns Returns true if the specified element is contained; otherwise, returns false.
159
+ */
160
+ has(element) {
161
+ return this.elements.includes(element);
162
+ }
163
+ /**
164
+ * Time Complexity: O(n). The worst-case O(n), where n is the number of elements in the heap. This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation.
165
+ * Space Complexity: O(1)
166
+ */
167
+ /**
168
+ * Time Complexity: O(n). The worst-case O(n), where n is the number of elements in the heap. This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation.
169
+ * Space Complexity: O(1)
170
+ *
171
+ * The `delete` function removes an element from an array-like data structure, maintaining the order
172
+ * and structure of the remaining elements.
173
+ * @param {E} element - The `element` parameter represents the element that you want to delete from
174
+ * the array `this.elements`.
175
+ * @returns The `delete` function is returning a boolean value. It returns `true` if the element was
176
+ * successfully deleted from the array, and `false` if the element was not found in the array.
177
+ */
178
+ delete(element) {
179
+ const index = this.elements.indexOf(element);
180
+ if (index < 0)
181
+ return false;
182
+ if (index === 0) {
183
+ this.pop();
184
+ }
185
+ else if (index === this.elements.length - 1) {
186
+ this.elements.pop();
187
+ }
188
+ else {
189
+ this.elements.splice(index, 1, this.elements.pop());
190
+ this._bubbleUp(index);
191
+ this._sinkDown(index, this.elements.length >> 1);
192
+ }
193
+ return true;
194
+ }
195
+ /**
196
+ * Time Complexity: O(n), where n is the number of elements in the heap.
197
+ * Space Complexity: O(h), where h is the height of the heap.
198
+ */
199
+ /**
200
+ * Time Complexity: O(n), where n is the number of elements in the heap.
201
+ * Space Complexity: O(h), where h is the height of the heap.
202
+ *
203
+ * Depth-first search (DFS) method, different traversal orders can be selected。
204
+ * @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
205
+ * @returns An array containing elements traversed in the specified order.
206
+ */
207
+ dfs(order) {
208
+ const result = [];
209
+ // Auxiliary recursive function, traverses the binary heap according to the traversal order
210
+ const dfsHelper = (index) => {
211
+ if (index < this.size) {
212
+ if (order === 'in') {
213
+ dfsHelper(2 * index + 1);
214
+ result.push(this.elements[index]);
215
+ dfsHelper(2 * index + 2);
216
+ }
217
+ else if (order === 'pre') {
218
+ result.push(this.elements[index]);
219
+ dfsHelper(2 * index + 1);
220
+ dfsHelper(2 * index + 2);
221
+ }
222
+ else if (order === 'post') {
223
+ dfsHelper(2 * index + 1);
224
+ dfsHelper(2 * index + 2);
225
+ result.push(this.elements[index]);
226
+ }
227
+ }
228
+ };
229
+ dfsHelper(0); // Traverse starting from the root node
230
+ return result;
231
+ }
232
+ /**
233
+ * Time Complexity: O(n)
234
+ * Space Complexity: O(n)
235
+ */
236
+ /**
237
+ * Time Complexity: O(n)
238
+ * Space Complexity: O(n)
239
+ *
240
+ * Convert the heap to an array.
241
+ * @returns An array containing the elements of the heap.
242
+ */
243
+ toArray() {
244
+ return [...this.elements];
245
+ }
246
+ /**
247
+ * Time Complexity: O(n)
248
+ * Space Complexity: O(n)
249
+ */
250
+ /**
251
+ * Time Complexity: O(n)
252
+ * Space Complexity: O(n)
253
+ *
254
+ * Clone the heap, creating a new heap with the same elements.
255
+ * @returns A new Heap instance containing the same elements.
256
+ */
257
+ clone() {
258
+ const clonedHeap = new Heap({ comparator: this.comparator });
259
+ clonedHeap._elements = [...this.elements];
260
+ return clonedHeap;
261
+ }
262
+ /**
263
+ * Time Complexity: O(n log n)
264
+ * Space Complexity: O(n)
265
+ */
266
+ /**
267
+ * Time Complexity: O(n log n)
268
+ * Space Complexity: O(n)
269
+ *
270
+ * Sort the elements in the heap and return them as an array.
271
+ * @returns An array containing the elements sorted in ascending order.
272
+ */
273
+ sort() {
274
+ const visitedNode = [];
275
+ const cloned = this.clone();
276
+ while (cloned.size !== 0) {
277
+ const top = cloned.poll();
278
+ if (top)
279
+ visitedNode.push(top);
280
+ }
281
+ return visitedNode;
282
+ }
283
+ /**
284
+ * Time Complexity: O(log n)
285
+ * Space Complexity: O(1)
286
+ */
287
+ /**
288
+ * Time Complexity: O(n)
289
+ * Space Complexity: O(1)
290
+ *
291
+ * Fix the entire heap to maintain heap properties.
292
+ */
293
+ fix() {
294
+ for (let i = Math.floor(this.size / 2); i >= 0; i--)
295
+ this._sinkDown(i, this.elements.length >> 1);
296
+ }
297
+ /**
298
+ * Time Complexity: O(log n)
299
+ * Space Complexity: O(1)
300
+ */
301
+ /**
302
+ * Time Complexity: O(log n)
303
+ * Space Complexity: O(1)
304
+ *
305
+ * Float operation to maintain heap properties after adding an element.
306
+ * @param index - The index of the newly added element.
307
+ */
308
+ _bubbleUp(index) {
309
+ const element = this.elements[index];
310
+ while (index > 0) {
311
+ const parent = (index - 1) >> 1;
312
+ const parentItem = this.elements[parent];
313
+ if (this._comparator(parentItem, element) <= 0)
314
+ break;
315
+ this.elements[index] = parentItem;
316
+ index = parent;
317
+ }
318
+ this.elements[index] = element;
319
+ }
320
+ /**
321
+ * Time Complexity: O(n)
322
+ * Space Complexity: O(1)
323
+ */
324
+ /**
325
+ * Time Complexity: O(log n)
326
+ * Space Complexity: O(1)
327
+ *
328
+ * Sinking operation to maintain heap properties after removing the top element.
329
+ * @param index - The index from which to start sinking.
330
+ * @param halfLength
331
+ */
332
+ _sinkDown(index, halfLength) {
333
+ const element = this.elements[index];
334
+ while (index < halfLength) {
335
+ let left = index << 1 | 1;
336
+ const right = left + 1;
337
+ let minItem = this.elements[left];
338
+ if (right < this.elements.length &&
339
+ this._comparator(minItem, this.elements[right]) > 0) {
340
+ left = right;
341
+ minItem = this.elements[right];
342
+ }
343
+ if (this._comparator(minItem, element) >= 0)
344
+ break;
345
+ this.elements[index] = minItem;
346
+ index = left;
347
+ }
348
+ this.elements[index] = element;
349
+ }
350
+ }
351
+ exports.Heap = Heap;
352
+ class FibonacciHeapNode {
353
+ constructor(element, degree = 0) {
354
+ this.element = element;
355
+ this.degree = degree;
356
+ this.marked = false;
357
+ }
358
+ }
359
+ exports.FibonacciHeapNode = FibonacciHeapNode;
360
+ class FibonacciHeap {
361
+ constructor(comparator) {
362
+ this._size = 0;
363
+ this.clear();
364
+ this._comparator = comparator || this.defaultComparator;
365
+ if (typeof this.comparator !== 'function') {
366
+ throw new Error('FibonacciHeap constructor: given comparator should be a function.');
367
+ }
368
+ }
369
+ get root() {
370
+ return this._root;
371
+ }
372
+ get size() {
373
+ return this._size;
374
+ }
375
+ get min() {
376
+ return this._min;
377
+ }
378
+ get comparator() {
379
+ return this._comparator;
380
+ }
381
+ /**
382
+ * Get the size (number of elements) of the heap.
383
+ * @returns {number} The size of the heap. Returns 0 if the heap is empty. Returns -1 if the heap is invalid.
384
+ */
385
+ clear() {
386
+ this._root = undefined;
387
+ this._min = undefined;
388
+ this._size = 0;
389
+ }
390
+ /**
391
+ * Time Complexity: O(1)
392
+ * Space Complexity: O(1)
393
+ */
394
+ /**
395
+ * Time Complexity: O(1)
396
+ * Space Complexity: O(1)
397
+ *
398
+ * Insert an element into the heap and maintain the heap properties.
399
+ * @param element
400
+ * @returns {FibonacciHeap<E>} FibonacciHeap<E> - The heap itself.
401
+ */
402
+ add(element) {
403
+ return this.push(element);
404
+ }
405
+ /**
406
+ * Time Complexity: O(1)
407
+ * Space Complexity: O(1)
408
+ */
409
+ /**
410
+ * Time Complexity: O(1)
411
+ * Space Complexity: O(1)
412
+ *
413
+ * Insert an element into the heap and maintain the heap properties.
414
+ * @param element
415
+ * @returns {FibonacciHeap<E>} FibonacciHeap<E> - The heap itself.
416
+ */
417
+ push(element) {
418
+ const node = this.createNode(element);
419
+ node.left = node;
420
+ node.right = node;
421
+ this.mergeWithRoot(node);
422
+ if (!this.min || this.comparator(node.element, this.min.element) <= 0) {
423
+ this._min = node;
424
+ }
425
+ this._size++;
426
+ return this;
427
+ }
428
+ /**
429
+ * Time Complexity: O(1)
430
+ * Space Complexity: O(1)
431
+ */
432
+ /**
433
+ * Time Complexity: O(1)
434
+ * Space Complexity: O(1)
435
+ *
436
+ * Peek at the top element of the heap without removing it.
437
+ * @returns The top element or undefined if the heap is empty.
438
+ * @protected
439
+ */
440
+ peek() {
441
+ return this.min ? this.min.element : undefined;
442
+ }
443
+ /**
444
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
445
+ * Space Complexity: O(1)
446
+ */
447
+ /**
448
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
449
+ * Space Complexity: O(1)
450
+ *
451
+ * Get the size (number of elements) of the heap.
452
+ * @param {FibonacciHeapNode<E>} head - The head of the linked list.
453
+ * @protected
454
+ * @returns FibonacciHeapNode<E>[] - An array containing the elements of the linked list.
455
+ */
456
+ consumeLinkedList(head) {
457
+ const elements = [];
458
+ if (!head)
459
+ return elements;
460
+ let node = head;
461
+ let flag = false;
462
+ while (true) {
463
+ if (node === head && flag)
464
+ break;
465
+ else if (node === head)
466
+ flag = true;
467
+ if (node) {
468
+ elements.push(node);
469
+ node = node.right;
470
+ }
471
+ }
472
+ return elements;
473
+ }
474
+ /**
475
+ * Time Complexity: O(1)
476
+ * Space Complexity: O(1)
477
+ *
478
+ * @param parent
479
+ * @param node
480
+ */
481
+ mergeWithChild(parent, node) {
482
+ if (!parent.child) {
483
+ parent.child = node;
484
+ }
485
+ else {
486
+ node.right = parent.child.right;
487
+ node.left = parent.child;
488
+ parent.child.right.left = node;
489
+ parent.child.right = node;
490
+ }
491
+ }
492
+ /**
493
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
494
+ * Space Complexity: O(1)
495
+ */
496
+ /**
497
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
498
+ * Space Complexity: O(1)
499
+ *
500
+ * Remove and return the top element (smallest or largest element) from the heap.
501
+ * @returns The top element or undefined if the heap is empty.
502
+ */
503
+ poll() {
504
+ return this.pop();
505
+ }
506
+ /**
507
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
508
+ * Space Complexity: O(1)
509
+ */
510
+ /**
511
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
512
+ * Space Complexity: O(1)
513
+ *
514
+ * Remove and return the top element (smallest or largest element) from the heap.
515
+ * @returns The top element or undefined if the heap is empty.
516
+ */
517
+ pop() {
518
+ if (this.size === 0)
519
+ return undefined;
520
+ const z = this.min;
521
+ if (z.child) {
522
+ const elements = this.consumeLinkedList(z.child);
523
+ for (const node of elements) {
524
+ this.mergeWithRoot(node);
525
+ node.parent = undefined;
526
+ }
527
+ }
528
+ this.removeFromRoot(z);
529
+ if (z === z.right) {
530
+ this._min = undefined;
531
+ this._root = undefined;
532
+ }
533
+ else {
534
+ this._min = z.right;
535
+ this.consolidate();
536
+ }
537
+ this._size--;
538
+ return z.element;
539
+ }
540
+ /**
541
+ * Time Complexity: O(1)
542
+ * Space Complexity: O(1)
543
+ */
544
+ /**
545
+ * Time Complexity: O(1)
546
+ * Space Complexity: O(1)
547
+ *
548
+ * merge two heaps. The heap that is merged will be cleared. The heap that is merged into will remain.
549
+ * @param heapToMerge
550
+ */
551
+ merge(heapToMerge) {
552
+ if (heapToMerge.size === 0) {
553
+ return; // Nothing to merge
554
+ }
555
+ // Merge the root lists of the two heaps
556
+ if (this.root && heapToMerge.root) {
557
+ const thisRoot = this.root;
558
+ const otherRoot = heapToMerge.root;
559
+ const thisRootRight = thisRoot.right;
560
+ const otherRootLeft = otherRoot.left;
561
+ thisRoot.right = otherRoot;
562
+ otherRoot.left = thisRoot;
563
+ thisRootRight.left = otherRootLeft;
564
+ otherRootLeft.right = thisRootRight;
565
+ }
566
+ // Update the minimum node
567
+ if (!this.min || (heapToMerge.min && this.comparator(heapToMerge.min.element, this.min.element) < 0)) {
568
+ this._min = heapToMerge.min;
569
+ }
570
+ // Update the size
571
+ this._size += heapToMerge.size;
572
+ // Clear the heap that was merged
573
+ heapToMerge.clear();
574
+ }
575
+ /**
576
+ * Default comparator function used by the heap.
577
+ * @param {E} a
578
+ * @param {E} b
579
+ * @protected
580
+ */
581
+ defaultComparator(a, b) {
582
+ if (a < b)
583
+ return -1;
584
+ if (a > b)
585
+ return 1;
586
+ return 0;
587
+ }
588
+ /**
589
+ * Create a new node.
590
+ * @param element
591
+ * @protected
592
+ */
593
+ createNode(element) {
594
+ return new FibonacciHeapNode(element);
595
+ }
596
+ /**
597
+ * Time Complexity: O(1)
598
+ * Space Complexity: O(1)
599
+ */
600
+ /**
601
+ * Time Complexity: O(1)
602
+ * Space Complexity: O(1)
603
+ *
604
+ * Merge the given node with the root list.
605
+ * @param node - The node to be merged.
606
+ */
607
+ mergeWithRoot(node) {
608
+ if (!this.root) {
609
+ this._root = node;
610
+ }
611
+ else {
612
+ node.right = this.root.right;
613
+ node.left = this.root;
614
+ this.root.right.left = node;
615
+ this.root.right = node;
616
+ }
617
+ }
618
+ /**
619
+ * Time Complexity: O(1)
620
+ * Space Complexity: O(1)
621
+ */
622
+ /**
623
+ * Time Complexity: O(1)
624
+ * Space Complexity: O(1)
625
+ *.
626
+ * Remove and return the top element (smallest or largest element) from the heap.
627
+ * @param node - The node to be removed.
628
+ * @protected
629
+ */
630
+ removeFromRoot(node) {
631
+ if (this.root === node)
632
+ this._root = node.right;
633
+ if (node.left)
634
+ node.left.right = node.right;
635
+ if (node.right)
636
+ node.right.left = node.left;
637
+ }
638
+ /**
639
+ * Time Complexity: O(1)
640
+ * Space Complexity: O(1)
641
+ */
642
+ /**
643
+ * Time Complexity: O(1)
644
+ * Space Complexity: O(1)
645
+ *
646
+ * Remove and return the top element (smallest or largest element) from the heap.
647
+ * @param y
648
+ * @param x
649
+ * @protected
650
+ */
651
+ link(y, x) {
652
+ this.removeFromRoot(y);
653
+ y.left = y;
654
+ y.right = y;
655
+ this.mergeWithChild(x, y);
656
+ x.degree++;
657
+ y.parent = x;
658
+ }
659
+ /**
660
+ * Time Complexity: O(n log n), where n is the number of elements in the heap.
661
+ * Space Complexity: O(n)
662
+ */
663
+ /**
664
+ * Time Complexity: O(n log n), where n is the number of elements in the heap.
665
+ * Space Complexity: O(n)
666
+ *
667
+ * Remove and return the top element (smallest or largest element) from the heap.
668
+ * @protected
669
+ */
670
+ consolidate() {
671
+ const A = new Array(this.size);
672
+ const elements = this.consumeLinkedList(this.root);
673
+ let x, y, d, t;
674
+ for (const node of elements) {
675
+ x = node;
676
+ d = x.degree;
677
+ while (A[d]) {
678
+ y = A[d];
679
+ if (this.comparator(x.element, y.element) > 0) {
680
+ t = x;
681
+ x = y;
682
+ y = t;
683
+ }
684
+ this.link(y, x);
685
+ A[d] = undefined;
686
+ d++;
687
+ }
688
+ A[d] = x;
689
+ }
690
+ for (let i = 0; i < this.size; i++) {
691
+ if (A[i] && this.comparator(A[i].element, this.min.element) <= 0) {
692
+ this._min = A[i];
693
+ }
694
+ }
695
+ }
696
+ }
697
+ exports.FibonacciHeap = FibonacciHeap;
@@ -0,0 +1,3 @@
1
+ export * from './max-heap';
2
+ export * from './min-heap';
3
+ export * from './heap';
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./max-heap"), exports);
18
+ __exportStar(require("./min-heap"), exports);
19
+ __exportStar(require("./heap"), exports);