min-heap-typed 1.38.0 → 1.38.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (334) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +100 -0
  2. package/dist/data-structures/binary-tree/avl-tree.js +341 -0
  3. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +144 -0
  4. package/dist/data-structures/binary-tree/binary-indexed-tree.js +261 -0
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +409 -0
  6. package/dist/data-structures/binary-tree/binary-tree.js +1065 -0
  7. package/dist/data-structures/binary-tree/bst.d.ts +167 -0
  8. package/dist/data-structures/binary-tree/bst.js +512 -0
  9. package/dist/data-structures/binary-tree/index.d.ts +7 -0
  10. package/dist/data-structures/binary-tree/index.js +23 -0
  11. package/dist/data-structures/binary-tree/rb-tree.d.ts +13 -0
  12. package/dist/data-structures/binary-tree/rb-tree.js +27 -0
  13. package/dist/data-structures/binary-tree/segment-tree.d.ts +83 -0
  14. package/dist/data-structures/binary-tree/segment-tree.js +228 -0
  15. package/dist/data-structures/binary-tree/tree-multiset.d.ts +122 -0
  16. package/dist/data-structures/binary-tree/tree-multiset.js +351 -0
  17. package/dist/data-structures/graph/abstract-graph.d.ts +332 -0
  18. package/dist/data-structures/graph/abstract-graph.js +923 -0
  19. package/dist/data-structures/graph/directed-graph.d.ts +200 -0
  20. package/dist/data-structures/graph/directed-graph.js +422 -0
  21. package/dist/data-structures/graph/index.d.ts +4 -0
  22. package/dist/data-structures/graph/index.js +20 -0
  23. package/dist/data-structures/graph/map-graph.d.ts +79 -0
  24. package/dist/data-structures/graph/map-graph.js +111 -0
  25. package/dist/data-structures/graph/undirected-graph.d.ts +127 -0
  26. package/dist/data-structures/graph/undirected-graph.js +252 -0
  27. package/dist/data-structures/hash/coordinate-map.d.ts +45 -0
  28. package/dist/data-structures/hash/coordinate-map.js +65 -0
  29. package/dist/data-structures/hash/coordinate-set.d.ts +37 -0
  30. package/dist/data-structures/hash/coordinate-set.js +55 -0
  31. package/dist/data-structures/hash/hash-map.d.ts +56 -0
  32. package/dist/data-structures/hash/hash-map.js +171 -0
  33. package/dist/data-structures/hash/hash-table.d.ts +106 -0
  34. package/dist/data-structures/hash/hash-table.js +245 -0
  35. package/dist/data-structures/hash/index.d.ts +6 -0
  36. package/dist/data-structures/hash/index.js +22 -0
  37. package/dist/data-structures/hash/tree-map.d.ts +2 -0
  38. package/dist/data-structures/hash/tree-map.js +6 -0
  39. package/dist/data-structures/hash/tree-set.d.ts +2 -0
  40. package/dist/data-structures/hash/tree-set.js +6 -0
  41. package/dist/data-structures/heap/heap.d.ts +224 -0
  42. package/dist/data-structures/heap/heap.js +497 -0
  43. package/dist/data-structures/heap/index.d.ts +3 -0
  44. package/dist/data-structures/heap/index.js +19 -0
  45. package/dist/data-structures/heap/max-heap.d.ts +12 -0
  46. package/dist/data-structures/heap/max-heap.js +24 -0
  47. package/dist/data-structures/heap/min-heap.d.ts +12 -0
  48. package/dist/data-structures/heap/min-heap.js +24 -0
  49. package/dist/data-structures/index.d.ts +11 -0
  50. package/dist/data-structures/index.js +27 -0
  51. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +234 -0
  52. package/dist/data-structures/linked-list/doubly-linked-list.js +583 -0
  53. package/dist/data-structures/linked-list/index.d.ts +3 -0
  54. package/dist/data-structures/linked-list/index.js +19 -0
  55. package/dist/data-structures/linked-list/singly-linked-list.d.ts +157 -0
  56. package/dist/data-structures/linked-list/singly-linked-list.js +448 -0
  57. package/dist/data-structures/linked-list/skip-linked-list.d.ts +61 -0
  58. package/dist/data-structures/linked-list/skip-linked-list.js +142 -0
  59. package/dist/data-structures/matrix/index.d.ts +4 -0
  60. package/dist/data-structures/matrix/index.js +20 -0
  61. package/dist/data-structures/matrix/matrix.d.ts +21 -0
  62. package/dist/data-structures/matrix/matrix.js +28 -0
  63. package/dist/data-structures/matrix/matrix2d.d.ts +108 -0
  64. package/dist/data-structures/matrix/matrix2d.js +203 -0
  65. package/dist/data-structures/matrix/navigator.d.ts +52 -0
  66. package/dist/data-structures/matrix/navigator.js +106 -0
  67. package/dist/data-structures/matrix/vector2d.d.ts +201 -0
  68. package/dist/data-structures/matrix/vector2d.js +291 -0
  69. package/dist/data-structures/priority-queue/index.d.ts +3 -0
  70. package/dist/data-structures/priority-queue/index.js +19 -0
  71. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -0
  72. package/dist/data-structures/priority-queue/max-priority-queue.js +24 -0
  73. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -0
  74. package/dist/data-structures/priority-queue/min-priority-queue.js +24 -0
  75. package/dist/data-structures/priority-queue/priority-queue.d.ts +12 -0
  76. package/dist/data-structures/priority-queue/priority-queue.js +17 -0
  77. package/dist/data-structures/queue/deque.d.ts +165 -0
  78. package/dist/data-structures/queue/deque.js +276 -0
  79. package/dist/data-structures/queue/index.d.ts +2 -0
  80. package/dist/data-structures/queue/index.js +18 -0
  81. package/dist/data-structures/queue/queue.d.ts +107 -0
  82. package/dist/data-structures/queue/queue.js +170 -0
  83. package/dist/data-structures/stack/index.d.ts +1 -0
  84. package/dist/data-structures/stack/index.js +17 -0
  85. package/dist/data-structures/stack/stack.d.ts +63 -0
  86. package/dist/data-structures/stack/stack.js +91 -0
  87. package/dist/data-structures/tree/index.d.ts +1 -0
  88. package/dist/data-structures/tree/index.js +17 -0
  89. package/dist/data-structures/tree/tree.d.ts +14 -0
  90. package/dist/data-structures/tree/tree.js +58 -0
  91. package/dist/data-structures/trie/index.d.ts +1 -0
  92. package/dist/data-structures/trie/index.js +17 -0
  93. package/dist/data-structures/trie/trie.d.ts +84 -0
  94. package/dist/data-structures/trie/trie.js +268 -0
  95. package/dist/index.d.ts +3 -3
  96. package/dist/index.js +3 -3
  97. package/dist/interfaces/binary-tree.d.ts +7 -0
  98. package/dist/interfaces/binary-tree.js +2 -0
  99. package/dist/interfaces/doubly-linked-list.d.ts +1 -0
  100. package/dist/interfaces/doubly-linked-list.js +2 -0
  101. package/dist/interfaces/graph.d.ts +5 -0
  102. package/dist/interfaces/graph.js +2 -0
  103. package/dist/interfaces/heap.d.ts +1 -0
  104. package/dist/interfaces/heap.js +2 -0
  105. package/dist/interfaces/index.d.ts +8 -0
  106. package/dist/interfaces/index.js +24 -0
  107. package/dist/interfaces/navigator.d.ts +1 -0
  108. package/dist/interfaces/navigator.js +2 -0
  109. package/dist/interfaces/priority-queue.d.ts +1 -0
  110. package/dist/interfaces/priority-queue.js +2 -0
  111. package/dist/interfaces/segment-tree.d.ts +1 -0
  112. package/dist/interfaces/segment-tree.js +2 -0
  113. package/dist/interfaces/singly-linked-list.d.ts +1 -0
  114. package/dist/interfaces/singly-linked-list.js +2 -0
  115. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +4 -0
  116. package/dist/types/data-structures/binary-tree/avl-tree.js +2 -0
  117. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
  118. package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +2 -0
  119. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +31 -0
  120. package/dist/types/data-structures/binary-tree/binary-tree.js +24 -0
  121. package/dist/types/data-structures/binary-tree/bst.d.ts +7 -0
  122. package/dist/types/data-structures/binary-tree/bst.js +2 -0
  123. package/dist/types/data-structures/binary-tree/index.d.ts +6 -0
  124. package/dist/types/data-structures/binary-tree/index.js +22 -0
  125. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +8 -0
  126. package/dist/types/data-structures/binary-tree/rb-tree.js +8 -0
  127. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
  128. package/dist/types/data-structures/binary-tree/segment-tree.js +2 -0
  129. package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +4 -0
  130. package/dist/types/data-structures/binary-tree/tree-multiset.js +2 -0
  131. package/dist/types/data-structures/graph/abstract-graph.d.ts +10 -0
  132. package/dist/types/data-structures/graph/abstract-graph.js +2 -0
  133. package/dist/types/data-structures/graph/directed-graph.d.ts +6 -0
  134. package/dist/types/data-structures/graph/directed-graph.js +9 -0
  135. package/dist/types/data-structures/graph/index.d.ts +3 -0
  136. package/dist/types/data-structures/graph/index.js +19 -0
  137. package/dist/types/data-structures/graph/map-graph.d.ts +1 -0
  138. package/dist/types/data-structures/graph/map-graph.js +2 -0
  139. package/dist/types/data-structures/graph/undirected-graph.d.ts +1 -0
  140. package/dist/types/data-structures/graph/undirected-graph.js +2 -0
  141. package/dist/types/data-structures/hash/coordinate-map.d.ts +1 -0
  142. package/dist/types/data-structures/hash/coordinate-map.js +2 -0
  143. package/dist/types/data-structures/hash/coordinate-set.d.ts +1 -0
  144. package/dist/types/data-structures/hash/coordinate-set.js +2 -0
  145. package/dist/types/data-structures/hash/hash-map.d.ts +1 -0
  146. package/dist/types/data-structures/hash/hash-map.js +2 -0
  147. package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
  148. package/dist/types/data-structures/hash/hash-table.js +2 -0
  149. package/dist/types/data-structures/hash/index.d.ts +1 -0
  150. package/dist/types/data-structures/hash/index.js +2 -0
  151. package/dist/types/data-structures/hash/tree-map.d.ts +1 -0
  152. package/dist/types/data-structures/hash/tree-map.js +2 -0
  153. package/dist/types/data-structures/hash/tree-set.d.ts +1 -0
  154. package/dist/types/data-structures/hash/tree-set.js +2 -0
  155. package/dist/types/data-structures/heap/heap.d.ts +1 -0
  156. package/dist/types/data-structures/heap/heap.js +2 -0
  157. package/dist/types/data-structures/heap/index.d.ts +1 -0
  158. package/dist/types/data-structures/heap/index.js +17 -0
  159. package/dist/types/data-structures/heap/max-heap.d.ts +1 -0
  160. package/dist/types/data-structures/heap/max-heap.js +2 -0
  161. package/dist/types/data-structures/heap/min-heap.d.ts +1 -0
  162. package/dist/types/data-structures/heap/min-heap.js +2 -0
  163. package/dist/types/data-structures/index.d.ts +11 -0
  164. package/dist/types/data-structures/index.js +27 -0
  165. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -0
  166. package/dist/types/data-structures/linked-list/doubly-linked-list.js +2 -0
  167. package/dist/types/data-structures/linked-list/index.d.ts +2 -0
  168. package/dist/types/data-structures/linked-list/index.js +18 -0
  169. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +1 -0
  170. package/dist/types/data-structures/linked-list/singly-linked-list.js +2 -0
  171. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
  172. package/dist/types/data-structures/linked-list/skip-linked-list.js +2 -0
  173. package/dist/types/data-structures/matrix/index.d.ts +1 -0
  174. package/dist/types/data-structures/matrix/index.js +17 -0
  175. package/dist/types/data-structures/matrix/matrix.d.ts +1 -0
  176. package/dist/types/data-structures/matrix/matrix.js +2 -0
  177. package/dist/types/data-structures/matrix/matrix2d.d.ts +1 -0
  178. package/dist/types/data-structures/matrix/matrix2d.js +2 -0
  179. package/dist/types/data-structures/matrix/navigator.d.ts +14 -0
  180. package/dist/types/data-structures/matrix/navigator.js +2 -0
  181. package/dist/types/data-structures/matrix/vector2d.d.ts +1 -0
  182. package/dist/types/data-structures/matrix/vector2d.js +2 -0
  183. package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
  184. package/dist/types/data-structures/priority-queue/index.js +19 -0
  185. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
  186. package/dist/types/data-structures/priority-queue/max-priority-queue.js +2 -0
  187. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
  188. package/dist/types/data-structures/priority-queue/min-priority-queue.js +2 -0
  189. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  190. package/dist/types/data-structures/priority-queue/priority-queue.js +2 -0
  191. package/dist/types/data-structures/queue/deque.d.ts +1 -0
  192. package/dist/types/data-structures/queue/deque.js +2 -0
  193. package/dist/types/data-structures/queue/index.d.ts +2 -0
  194. package/dist/types/data-structures/queue/index.js +18 -0
  195. package/dist/types/data-structures/queue/queue.d.ts +1 -0
  196. package/dist/types/data-structures/queue/queue.js +2 -0
  197. package/dist/types/data-structures/stack/index.d.ts +1 -0
  198. package/dist/types/data-structures/stack/index.js +17 -0
  199. package/dist/types/data-structures/stack/stack.d.ts +1 -0
  200. package/dist/types/data-structures/stack/stack.js +2 -0
  201. package/dist/types/data-structures/tree/index.d.ts +1 -0
  202. package/dist/types/data-structures/tree/index.js +17 -0
  203. package/dist/types/data-structures/tree/tree.d.ts +1 -0
  204. package/dist/types/data-structures/tree/tree.js +2 -0
  205. package/dist/types/data-structures/trie/index.d.ts +1 -0
  206. package/dist/types/data-structures/trie/index.js +17 -0
  207. package/dist/types/data-structures/trie/trie.d.ts +1 -0
  208. package/dist/types/data-structures/trie/trie.js +2 -0
  209. package/dist/types/helpers.d.ts +9 -0
  210. package/dist/types/helpers.js +9 -0
  211. package/dist/types/index.d.ts +3 -0
  212. package/dist/types/index.js +19 -0
  213. package/dist/types/utils/index.d.ts +2 -0
  214. package/dist/types/utils/index.js +18 -0
  215. package/dist/types/utils/utils.d.ts +7 -0
  216. package/dist/types/utils/utils.js +2 -0
  217. package/dist/types/utils/validate-type.d.ts +19 -0
  218. package/dist/types/utils/validate-type.js +2 -0
  219. package/dist/utils/index.d.ts +1 -0
  220. package/dist/utils/index.js +17 -0
  221. package/dist/utils/utils.d.ts +20 -0
  222. package/dist/utils/utils.js +73 -0
  223. package/package.json +1 -4
  224. package/src/data-structures/binary-tree/avl-tree.ts +342 -0
  225. package/src/data-structures/binary-tree/binary-indexed-tree.ts +298 -0
  226. package/src/data-structures/binary-tree/binary-tree.ts +1141 -0
  227. package/src/data-structures/binary-tree/bst.ts +529 -0
  228. package/src/data-structures/binary-tree/index.ts +7 -0
  229. package/src/data-structures/binary-tree/rb-tree.ts +366 -0
  230. package/src/data-structures/binary-tree/segment-tree.ts +257 -0
  231. package/src/data-structures/binary-tree/tree-multiset.ts +375 -0
  232. package/src/data-structures/graph/abstract-graph.ts +1044 -0
  233. package/src/data-structures/graph/directed-graph.ts +470 -0
  234. package/src/data-structures/graph/index.ts +4 -0
  235. package/src/data-structures/graph/map-graph.ts +134 -0
  236. package/src/data-structures/graph/undirected-graph.ts +274 -0
  237. package/src/data-structures/hash/coordinate-map.ts +67 -0
  238. package/src/data-structures/hash/coordinate-set.ts +56 -0
  239. package/src/data-structures/hash/hash-map.ts +209 -0
  240. package/src/data-structures/hash/hash-table.ts +280 -0
  241. package/src/data-structures/hash/index.ts +6 -0
  242. package/src/data-structures/hash/tree-map.ts +1 -0
  243. package/src/data-structures/hash/tree-set.ts +1 -0
  244. package/src/data-structures/heap/heap.ts +561 -0
  245. package/src/data-structures/heap/index.ts +3 -0
  246. package/src/data-structures/heap/max-heap.ts +24 -0
  247. package/src/data-structures/heap/min-heap.ts +24 -0
  248. package/src/data-structures/index.ts +11 -0
  249. package/src/data-structures/linked-list/doubly-linked-list.ts +636 -0
  250. package/src/data-structures/linked-list/index.ts +3 -0
  251. package/src/data-structures/linked-list/singly-linked-list.ts +501 -0
  252. package/src/data-structures/linked-list/skip-linked-list.ts +173 -0
  253. package/src/data-structures/matrix/index.ts +4 -0
  254. package/src/data-structures/matrix/matrix.ts +27 -0
  255. package/src/data-structures/matrix/matrix2d.ts +213 -0
  256. package/src/data-structures/matrix/navigator.ts +121 -0
  257. package/src/data-structures/matrix/vector2d.ts +316 -0
  258. package/src/data-structures/priority-queue/index.ts +3 -0
  259. package/src/data-structures/priority-queue/max-priority-queue.ts +23 -0
  260. package/src/data-structures/priority-queue/min-priority-queue.ts +23 -0
  261. package/src/data-structures/priority-queue/priority-queue.ts +16 -0
  262. package/src/data-structures/queue/deque.ts +297 -0
  263. package/src/data-structures/queue/index.ts +2 -0
  264. package/src/data-structures/queue/queue.ts +191 -0
  265. package/src/data-structures/stack/index.ts +1 -0
  266. package/src/data-structures/stack/stack.ts +98 -0
  267. package/src/data-structures/tree/index.ts +1 -0
  268. package/src/data-structures/tree/tree.ts +67 -0
  269. package/src/data-structures/trie/index.ts +1 -0
  270. package/src/data-structures/trie/trie.ts +286 -0
  271. package/src/index.ts +3 -3
  272. package/src/interfaces/binary-tree.ts +10 -0
  273. package/src/interfaces/doubly-linked-list.ts +1 -0
  274. package/src/interfaces/graph.ts +7 -0
  275. package/src/interfaces/heap.ts +1 -0
  276. package/src/interfaces/index.ts +8 -0
  277. package/src/interfaces/navigator.ts +1 -0
  278. package/src/interfaces/priority-queue.ts +1 -0
  279. package/src/interfaces/segment-tree.ts +1 -0
  280. package/src/interfaces/singly-linked-list.ts +1 -0
  281. package/src/types/data-structures/binary-tree/avl-tree.ts +5 -0
  282. package/src/types/data-structures/binary-tree/binary-indexed-tree.ts +1 -0
  283. package/src/types/data-structures/binary-tree/binary-tree.ts +35 -0
  284. package/src/types/data-structures/binary-tree/bst.ts +11 -0
  285. package/src/types/data-structures/binary-tree/index.ts +6 -0
  286. package/src/types/data-structures/binary-tree/rb-tree.ts +8 -0
  287. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -0
  288. package/src/types/data-structures/binary-tree/tree-multiset.ts +6 -0
  289. package/src/types/data-structures/graph/abstract-graph.ts +11 -0
  290. package/src/types/data-structures/graph/directed-graph.ts +8 -0
  291. package/src/types/data-structures/graph/index.ts +3 -0
  292. package/src/types/data-structures/graph/map-graph.ts +1 -0
  293. package/src/types/data-structures/graph/undirected-graph.ts +1 -0
  294. package/src/types/data-structures/hash/coordinate-map.ts +1 -0
  295. package/src/types/data-structures/hash/coordinate-set.ts +1 -0
  296. package/src/types/data-structures/hash/hash-map.ts +1 -0
  297. package/src/types/data-structures/hash/hash-table.ts +1 -0
  298. package/src/types/data-structures/hash/index.ts +1 -0
  299. package/src/types/data-structures/hash/tree-map.ts +1 -0
  300. package/src/types/data-structures/hash/tree-set.ts +1 -0
  301. package/src/types/data-structures/heap/heap.ts +1 -0
  302. package/src/types/data-structures/heap/index.ts +1 -0
  303. package/src/types/data-structures/heap/max-heap.ts +1 -0
  304. package/src/types/data-structures/heap/min-heap.ts +1 -0
  305. package/src/types/data-structures/index.ts +11 -0
  306. package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -0
  307. package/src/types/data-structures/linked-list/index.ts +2 -0
  308. package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -0
  309. package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -0
  310. package/src/types/data-structures/matrix/index.ts +1 -0
  311. package/src/types/data-structures/matrix/matrix.ts +1 -0
  312. package/src/types/data-structures/matrix/matrix2d.ts +1 -0
  313. package/src/types/data-structures/matrix/navigator.ts +14 -0
  314. package/src/types/data-structures/matrix/vector2d.ts +1 -0
  315. package/src/types/data-structures/priority-queue/index.ts +3 -0
  316. package/src/types/data-structures/priority-queue/max-priority-queue.ts +1 -0
  317. package/src/types/data-structures/priority-queue/min-priority-queue.ts +1 -0
  318. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  319. package/src/types/data-structures/queue/deque.ts +1 -0
  320. package/src/types/data-structures/queue/index.ts +2 -0
  321. package/src/types/data-structures/queue/queue.ts +1 -0
  322. package/src/types/data-structures/stack/index.ts +1 -0
  323. package/src/types/data-structures/stack/stack.ts +1 -0
  324. package/src/types/data-structures/tree/index.ts +1 -0
  325. package/src/types/data-structures/tree/tree.ts +1 -0
  326. package/src/types/data-structures/trie/index.ts +1 -0
  327. package/src/types/data-structures/trie/trie.ts +1 -0
  328. package/src/types/helpers.ts +13 -0
  329. package/src/types/index.ts +3 -0
  330. package/src/types/utils/index.ts +2 -0
  331. package/src/types/utils/utils.ts +6 -0
  332. package/src/types/utils/validate-type.ts +35 -0
  333. package/src/utils/index.ts +1 -0
  334. package/src/utils/utils.ts +86 -0
@@ -0,0 +1,23 @@
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
+ import {PriorityQueue} from './priority-queue';
9
+ import type {Comparator} from '../../types';
10
+
11
+ export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
12
+ constructor(
13
+ compare: Comparator<E> = (a: E, b: E) => {
14
+ if (!(typeof a === 'number' && typeof b === 'number')) {
15
+ throw new Error('The a, b params of compare function must be number');
16
+ } else {
17
+ return a - b;
18
+ }
19
+ }
20
+ ) {
21
+ super(compare);
22
+ }
23
+ }
@@ -0,0 +1,16 @@
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 {Comparator} from '../../types';
11
+
12
+ export class PriorityQueue<E> extends Heap<E> {
13
+ constructor(comparator: Comparator<E>) {
14
+ super(comparator);
15
+ }
16
+ }
@@ -0,0 +1,297 @@
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
+ import {DoublyLinkedList} from '../linked-list';
9
+
10
+ // O(n) time complexity of obtaining the value
11
+ // O(1) time complexity of adding at the beginning and the end
12
+ export class Deque<E = any> extends DoublyLinkedList<E> {}
13
+
14
+ // O(1) time complexity of obtaining the value
15
+ // O(n) time complexity of adding at the beginning and the end
16
+ // todo tested slowest one
17
+ export class ObjectDeque<E = number> {
18
+ constructor(capacity?: number) {
19
+ if (capacity !== undefined) this._capacity = capacity;
20
+ }
21
+
22
+ private _nodes: {[key: number]: E} = {};
23
+
24
+ get nodes(): {[p: number]: E} {
25
+ return this._nodes;
26
+ }
27
+
28
+ private _capacity = Number.MAX_SAFE_INTEGER;
29
+
30
+ get capacity(): number {
31
+ return this._capacity;
32
+ }
33
+
34
+ set capacity(value: number) {
35
+ this._capacity = value;
36
+ }
37
+
38
+ private _first = -1;
39
+
40
+ get first(): number {
41
+ return this._first;
42
+ }
43
+
44
+ set first(value: number) {
45
+ this._first = value;
46
+ }
47
+
48
+ private _last = -1;
49
+
50
+ get last(): number {
51
+ return this._last;
52
+ }
53
+
54
+ set last(value: number) {
55
+ this._last = value;
56
+ }
57
+
58
+ private _size = 0;
59
+
60
+ get size(): number {
61
+ return this._size;
62
+ }
63
+
64
+ /**
65
+ * The "addFirst" function adds a value to the beginning of an array-like data structure.
66
+ * @param {E} value - The `value` parameter represents the value that you want to add to the beginning of the data
67
+ * structure.
68
+ */
69
+ addFirst(value: E) {
70
+ if (this._size === 0) {
71
+ const mid = Math.floor(this._capacity / 2);
72
+ this._first = mid;
73
+ this._last = mid;
74
+ } else {
75
+ this._first--;
76
+ }
77
+ this._nodes[this._first] = value;
78
+ this._size++;
79
+ }
80
+
81
+ /**
82
+ * The addLast function adds a value to the end of an array-like data structure.
83
+ * @param {E} value - The `value` parameter represents the value that you want to add to the end of the data structure.
84
+ */
85
+ addLast(value: E) {
86
+ if (this._size === 0) {
87
+ const mid = Math.floor(this._capacity / 2);
88
+ this._first = mid;
89
+ this._last = mid;
90
+ } else {
91
+ this._last++;
92
+ }
93
+ this._nodes[this._last] = value;
94
+ this._size++;
95
+ }
96
+
97
+ /**
98
+ * The function `pollFirst()` removes and returns the first element in a data structure.
99
+ * @returns The value of the first element in the data structure.
100
+ */
101
+ pollFirst() {
102
+ if (!this._size) return;
103
+ const value = this.peekFirst();
104
+ delete this._nodes[this._first];
105
+ this._first++;
106
+ this._size--;
107
+ return value;
108
+ }
109
+
110
+ /**
111
+ * The `peekFirst` function returns the first element in an array-like data structure if it exists.
112
+ * @returns The element at the first position of the `_nodes` array.
113
+ */
114
+ peekFirst() {
115
+ if (this._size) return this._nodes[this._first];
116
+ }
117
+
118
+ /**
119
+ * The `pollLast()` function removes and returns the last element in a data structure.
120
+ * @returns The value that was removed from the data structure.
121
+ */
122
+ pollLast() {
123
+ if (!this._size) return;
124
+ const value = this.peekLast();
125
+ delete this._nodes[this._last];
126
+ this._last--;
127
+ this._size--;
128
+
129
+ return value;
130
+ }
131
+
132
+ /**
133
+ * The `peekLast()` function returns the last element in an array-like data structure.
134
+ * @returns The last element in the array "_nodes" is being returned.
135
+ */
136
+ peekLast() {
137
+ if (this._size) return this._nodes[this._last];
138
+ }
139
+
140
+ /**
141
+ * The get function returns the element at the specified index in an array-like data structure.
142
+ * @param {number} index - The index parameter is a number that represents the position of the element you want to
143
+ * retrieve from the array.
144
+ * @returns The element at the specified index in the `_nodes` array is being returned. If there is no element at that
145
+ * index, `null` is returned.
146
+ */
147
+ get(index: number) {
148
+ return this._nodes[this._first + index] || null;
149
+ }
150
+
151
+ /**
152
+ * The function checks if the size of a data structure is less than or equal to zero.
153
+ * @returns The method is returning a boolean value indicating whether the size of the object is less than or equal to 0.
154
+ */
155
+ isEmpty() {
156
+ return this._size <= 0;
157
+ }
158
+
159
+ protected _seNodes(value: {[p: number]: E}) {
160
+ this._nodes = value;
161
+ }
162
+
163
+ protected _setSize(value: number) {
164
+ this._size = value;
165
+ }
166
+ }
167
+
168
+ // O(1) time complexity of obtaining the value
169
+ // O(n) time complexity of adding at the beginning and the end
170
+ export class ArrayDeque<E> {
171
+ protected _nodes: E[] = [];
172
+
173
+ get size() {
174
+ return this._nodes.length;
175
+ }
176
+
177
+ /**
178
+ * O(n) time complexity of adding at the beginning and the end
179
+ */
180
+
181
+ /**
182
+ * The function "addLast" adds a value to the end of an array.
183
+ * @param {E} value - The value parameter represents the value that you want to add to the end of the array.
184
+ * @returns The return value is the new length of the array after the value has been added.
185
+ */
186
+ addLast(value: E) {
187
+ return this._nodes.push(value);
188
+ }
189
+
190
+ /**
191
+ * The function "pollLast" returns and removes the last element from an array, or returns null if the array is empty.
192
+ * @returns The method `pollLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
193
+ */
194
+ pollLast(): E | null {
195
+ return this._nodes.pop() ?? null;
196
+ }
197
+
198
+ /**
199
+ * The `pollFirst` function removes and returns the first element from an array, or returns null if the array is empty.
200
+ * @returns The `pollFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
201
+ * empty.
202
+ */
203
+ pollFirst(): E | null {
204
+ return this._nodes.shift() ?? null;
205
+ }
206
+
207
+ /**
208
+ * O(n) time complexity of adding at the beginning and the end
209
+ */
210
+
211
+ /**
212
+ * The function "addFirst" adds a value to the beginning of an array.
213
+ * @param {E} value - The value parameter represents the value that you want to add to the beginning of the array.
214
+ * @returns The return value of the `addFirst` function is the new length of the array `_nodes` after adding the
215
+ * `value` at the beginning.
216
+ */
217
+ addFirst(value: E) {
218
+ return this._nodes.unshift(value);
219
+ }
220
+
221
+ /**
222
+ * The `peekFirst` function returns the first element of an array or null if the array is empty.
223
+ * @returns The function `peekFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is
224
+ * empty, it will return `null`.
225
+ */
226
+ peekFirst(): E | null {
227
+ return this._nodes[0] ?? null;
228
+ }
229
+
230
+ /**
231
+ * The `peekLast` function returns the last element of an array or null if the array is empty.
232
+ * @returns The method `peekLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
233
+ */
234
+ peekLast(): E | null {
235
+ return this._nodes[this._nodes.length - 1] ?? null;
236
+ }
237
+
238
+ /**
239
+ * O(1) time complexity of obtaining the value
240
+ */
241
+
242
+ /**
243
+ * The get function returns the element at the specified index in an array, or null if the index is out of bounds.
244
+ * @param {number} index - The index parameter is a number that represents the position of the element you want to
245
+ * retrieve from the array.
246
+ * @returns The method is returning the element at the specified index in the `_nodes` array. If the element exists, it
247
+ * will be returned. If the element does not exist (i.e., the index is out of bounds), `null` will be returned.
248
+ */
249
+ get(index: number): E | null {
250
+ return this._nodes[index] ?? null;
251
+ }
252
+
253
+ /**
254
+ * The set function assigns a value to a specific index in an array.
255
+ * @param {number} index - The index parameter is a number that represents the position of the element in the array
256
+ * that you want to set a new value for.
257
+ * @param {E} value - The value parameter represents the new value that you want to set at the specified index in the
258
+ * _nodes array.
259
+ * @returns The value that is being set at the specified index in the `_nodes` array.
260
+ */
261
+ set(index: number, value: E) {
262
+ return (this._nodes[index] = value);
263
+ }
264
+
265
+ /**
266
+ * The insert function adds a value at a specified index in an array.
267
+ * @param {number} index - The index parameter specifies the position at which the value should be inserted in the
268
+ * array. It is a number that represents the index of the array where the value should be inserted. The index starts
269
+ * from 0, so the first element of the array has an index of 0, the second element has
270
+ * @param {E} value - The value parameter represents the value that you want to insert into the array at the specified
271
+ * index.
272
+ * @returns The splice method returns an array containing the removed elements, if any. In this case, since no elements
273
+ * are being removed, an empty array will be returned.
274
+ */
275
+ insert(index: number, value: E) {
276
+ return this._nodes.splice(index, 0, value);
277
+ }
278
+
279
+ /**
280
+ * The delete function removes an element from an array at a specified index.
281
+ * @param {number} index - The index parameter specifies the position of the element to be removed from the array. It
282
+ * is a number that represents the index of the element to be removed.
283
+ * @returns The method is returning an array containing the removed element.
284
+ */
285
+ delete(index: number) {
286
+ return this._nodes.splice(index, 1);
287
+ }
288
+
289
+ /**
290
+ * The function checks if an array called "_nodes" is empty.
291
+ * @returns The method `isEmpty()` is returning a boolean value. It returns `true` if the length of the `_nodes` array
292
+ * is 0, indicating that the array is empty. Otherwise, it returns `false`.
293
+ */
294
+ isEmpty() {
295
+ return this._nodes.length === 0;
296
+ }
297
+ }
@@ -0,0 +1,2 @@
1
+ export * from './queue';
2
+ export * from './deque';
@@ -0,0 +1,191 @@
1
+ /**
2
+ * @license MIT
3
+ * @copyright Tyler Zeng <zrwusa@gmail.com>
4
+ * @class
5
+ */
6
+ import {SinglyLinkedList} from '../linked-list';
7
+
8
+ export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
9
+ /**
10
+ * The enqueue function adds a value to the end of an array.
11
+ * @param {E} value - The value parameter represents the value that you want to add to the queue.
12
+ */
13
+ enqueue(value: E) {
14
+ this.push(value);
15
+ }
16
+
17
+ /**
18
+ * The `dequeue` function removes and returns the first element from a queue, or returns null if the queue is empty.
19
+ * @returns The method is returning the element at the front of the queue, or null if the queue is empty.
20
+ */
21
+ dequeue(): E | undefined {
22
+ return this.shift();
23
+ }
24
+
25
+ /**
26
+ * The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
27
+ * @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
28
+ */
29
+ peek(): E | undefined {
30
+ return this.head?.val;
31
+ }
32
+ }
33
+
34
+ export class Queue<E = any> {
35
+ /**
36
+ * The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
37
+ * @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it
38
+ * will be used to initialize the `_nodes` property of the class. If not provided, the `_nodes` property will be
39
+ * initialized as an empty array.
40
+ */
41
+ constructor(elements?: E[]) {
42
+ this._nodes = elements || [];
43
+ this._offset = 0;
44
+ }
45
+
46
+ private _nodes: E[];
47
+
48
+ get nodes(): E[] {
49
+ return this._nodes;
50
+ }
51
+
52
+ set nodes(value: E[]) {
53
+ this._nodes = value;
54
+ }
55
+
56
+ private _offset: number;
57
+
58
+ get offset(): number {
59
+ return this._offset;
60
+ }
61
+
62
+ set offset(value: number) {
63
+ this._offset = value;
64
+ }
65
+
66
+ /**
67
+ * The size function returns the number of elements in an array.
68
+ * @returns {number} The size of the array, which is the difference between the length of the array and the offset.
69
+ */
70
+ get size(): number {
71
+ return this.nodes.length - this.offset;
72
+ }
73
+
74
+ /**
75
+ * The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
76
+ * @public
77
+ * @static
78
+ * @param {E[]} elements - The "elements" parameter is an array of elements of type E.
79
+ * @returns The method is returning a new instance of the Queue class, initialized with the elements from the input
80
+ * array.
81
+ */
82
+ static fromArray<E>(elements: E[]): Queue<E> {
83
+ return new Queue(elements);
84
+ }
85
+
86
+ /**
87
+ * The push function adds an element to the end of the queue and returns the updated queue.Adds an element at the back of the queue.
88
+ * @param {E} element - The `element` parameter represents the element that you want to add to the queue.
89
+ * @returns The `add` method is returning a `Queue<E>` object.
90
+ */
91
+ push(element: E): Queue<E> {
92
+ this.nodes.push(element);
93
+ return this;
94
+ }
95
+
96
+ /**
97
+ * The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if
98
+ * necessary to optimize performance.
99
+ * @returns The function `shift()` returns either the first element in the queue or `null` if the queue is empty.
100
+ */
101
+ shift(): E | undefined {
102
+ if (this.size === 0) return undefined;
103
+
104
+ const first = this.peek();
105
+ this.offset += 1;
106
+
107
+ if (this.offset * 2 < this.nodes.length) return first;
108
+
109
+ // only delete dequeued elements when reaching half size
110
+ // to decrease latency of shifting elements.
111
+ this.nodes = this.nodes.slice(this.offset);
112
+ this.offset = 0;
113
+ return first;
114
+ }
115
+
116
+ /**
117
+ * The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
118
+ * @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
119
+ * the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
120
+ */
121
+ peek(): E | undefined {
122
+ return this.size > 0 ? this.nodes[this.offset] : undefined;
123
+ }
124
+
125
+ /**
126
+ * The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty.
127
+ * @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
128
+ * array is empty, it returns `null`.
129
+ */
130
+ peekLast(): E | undefined {
131
+ return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
132
+ }
133
+
134
+ /**
135
+ * The enqueue function adds a value to the end of a queue.
136
+ * @param {E} value - The value parameter represents the value that you want to add to the queue.
137
+ */
138
+ enqueue(value: E) {
139
+ this.push(value);
140
+ }
141
+
142
+ /**
143
+ * The `dequeue` function removes and returns the first element from a queue, or returns null if the queue is empty.
144
+ * @returns The method is returning a value of type E or null.
145
+ */
146
+ dequeue(): E | undefined {
147
+ return this.shift();
148
+ }
149
+
150
+ getAt(index: number): E | undefined {
151
+ return this.nodes[index];
152
+ }
153
+
154
+ /**
155
+ * The function checks if a data structure is empty by comparing its size to zero.
156
+ * @returns {boolean} A boolean value indicating whether the size of the object is 0 or not.
157
+ */
158
+ isEmpty(): boolean {
159
+ return this.size === 0;
160
+ }
161
+
162
+ /**
163
+ * The toArray() function returns an array of elements from the current offset to the end of the _nodes array.
164
+ * @returns An array of type E is being returned.
165
+ */
166
+ toArray(): E[] {
167
+ return this.nodes.slice(this.offset);
168
+ }
169
+
170
+ /**
171
+ * The clear function resets the nodes array and offset to their initial values.
172
+ */
173
+ clear(): void {
174
+ this.nodes = [];
175
+ this.offset = 0;
176
+ }
177
+
178
+ /**
179
+ * The `clone()` function returns a new Queue object with the same elements as the original Queue.
180
+ * @returns The `clone()` method is returning a new instance of the `Queue` class.
181
+ */
182
+ clone(): Queue<E> {
183
+ return new Queue(this.nodes.slice(this.offset));
184
+ }
185
+
186
+ *[Symbol.iterator]() {
187
+ for (const item of this.nodes) {
188
+ yield item;
189
+ }
190
+ }
191
+ }
@@ -0,0 +1 @@
1
+ export * from './stack';
@@ -0,0 +1,98 @@
1
+ /**
2
+ * @license MIT
3
+ * @copyright Tyler Zeng <zrwusa@gmail.com>
4
+ * @class
5
+ */
6
+ export class Stack<E = any> {
7
+ protected _elements: E[];
8
+
9
+ /**
10
+ * The constructor initializes an array of elements, which can be provided as an optional parameter.
11
+ * @param {E[]} [elements] - The `elements` parameter is an optional parameter of type `E[]`, which represents an array
12
+ * of elements of type `E`. It is used to initialize the `_elements` property of the class. If the `elements` parameter
13
+ * is provided and is an array, it is assigned to the `_elements
14
+ */
15
+ constructor(elements?: E[]) {
16
+ this._elements = Array.isArray(elements) ? elements : [];
17
+ }
18
+
19
+ /**
20
+ * The function "fromArray" creates a new Stack object from an array of elements.
21
+ * @param {E[]} elements - The `elements` parameter is an array of elements of type `E`.
22
+ * @returns {Stack} The method is returning a new instance of the Stack class, initialized with the elements from the input
23
+ * array.
24
+ */
25
+ static fromArray<E>(elements: E[]): Stack<E> {
26
+ return new Stack(elements);
27
+ }
28
+
29
+ /**
30
+ * The function checks if an array is empty and returns a boolean value.
31
+ * @returns A boolean value indicating whether the `_elements` array is empty or not.
32
+ */
33
+ isEmpty(): boolean {
34
+ return this._elements.length === 0;
35
+ }
36
+
37
+ /**
38
+ * The size() function returns the number of elements in an array.
39
+ * @returns The size of the elements array.
40
+ */
41
+ size(): number {
42
+ return this._elements.length;
43
+ }
44
+
45
+ /**
46
+ * The `peek` function returns the last element of an array, or null if the array is empty.
47
+ * @returns The `peek()` function returns the last element of the `_elements` array, or `null` if the array is empty.
48
+ */
49
+ peek(): E | null {
50
+ if (this.isEmpty()) return null;
51
+
52
+ return this._elements[this._elements.length - 1];
53
+ }
54
+
55
+ /**
56
+ * The push function adds an element to the stack and returns the updated stack.
57
+ * @param {E} element - The parameter "element" is of type E, which means it can be any data type.
58
+ * @returns The `push` method is returning the updated `Stack<E>` object.
59
+ */
60
+ push(element: E): Stack<E> {
61
+ this._elements.push(element);
62
+ return this;
63
+ }
64
+
65
+ /**
66
+ * The `pop` function removes and returns the last element from an array, or returns null if the array is empty.
67
+ * @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
68
+ * array is empty, it returns `null`.
69
+ */
70
+ pop(): E | null {
71
+ if (this.isEmpty()) return null;
72
+
73
+ return this._elements.pop() || null;
74
+ }
75
+
76
+ /**
77
+ * The toArray function returns a copy of the elements in an array.
78
+ * @returns An array of type E.
79
+ */
80
+ toArray(): E[] {
81
+ return this._elements.slice();
82
+ }
83
+
84
+ /**
85
+ * The clear function clears the elements array.
86
+ */
87
+ clear(): void {
88
+ this._elements = [];
89
+ }
90
+
91
+ /**
92
+ * The `clone()` function returns a new `Stack` object with the same elements as the original stack.
93
+ * @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
94
+ */
95
+ clone(): Stack<E> {
96
+ return new Stack(this._elements.slice());
97
+ }
98
+ }
@@ -0,0 +1 @@
1
+ export * from './tree';
@@ -0,0 +1,67 @@
1
+ export class TreeNode<V = any> {
2
+ constructor(key: string, value?: V, children?: TreeNode<V>[]) {
3
+ this._key = key;
4
+ this._value = value || undefined;
5
+ this._children = children || [];
6
+ }
7
+
8
+ private _key: string;
9
+
10
+ get key(): string {
11
+ return this._key;
12
+ }
13
+
14
+ set key(value: string) {
15
+ this._key = value;
16
+ }
17
+
18
+ private _value?: V | undefined;
19
+
20
+ get value(): V | undefined {
21
+ return this._value;
22
+ }
23
+
24
+ set value(value: V | undefined) {
25
+ this._value = value;
26
+ }
27
+
28
+ private _children?: TreeNode<V>[] | undefined;
29
+
30
+ get children(): TreeNode<V>[] | undefined {
31
+ return this._children;
32
+ }
33
+
34
+ set children(value: TreeNode<V>[] | undefined) {
35
+ this._children = value;
36
+ }
37
+
38
+ addChildren(children: TreeNode<V> | TreeNode<V>[]) {
39
+ if (!this.children) {
40
+ this.children = [];
41
+ }
42
+ if (children instanceof TreeNode) {
43
+ this.children.push(children);
44
+ } else {
45
+ this.children = this.children.concat(children);
46
+ }
47
+ }
48
+
49
+ getHeight() {
50
+ let maxDepth = 0;
51
+ if (this) {
52
+ const bfs = (node: TreeNode<V>, level: number) => {
53
+ if (level > maxDepth) {
54
+ maxDepth = level;
55
+ }
56
+ const {children} = node;
57
+ if (children) {
58
+ for (let i = 0, len = children.length; i < len; i++) {
59
+ bfs(children[i], level + 1);
60
+ }
61
+ }
62
+ };
63
+ bfs(this, 0);
64
+ }
65
+ return maxDepth;
66
+ }
67
+ }
@@ -0,0 +1 @@
1
+ export * from './trie';