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,2 @@
1
+ export * from './queue';
2
+ export * from './deque';
@@ -0,0 +1,308 @@
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 `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
27
+ * @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
28
+ */
29
+ getFirst(): E | undefined {
30
+ return this.head?.value;
31
+ }
32
+
33
+ /**
34
+ * The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
35
+ * @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
36
+ */
37
+ peek(): E | undefined {
38
+ return this.getFirst();
39
+ }
40
+ }
41
+
42
+ export class Queue<E = any> {
43
+ /**
44
+ * The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
45
+ * @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it
46
+ * will be used to initialize the `_nodes` property of the class. If not provided, the `_nodes` property will be
47
+ * initialized as an empty array.
48
+ */
49
+ constructor(elements?: E[]) {
50
+ this._nodes = elements || [];
51
+ this._offset = 0;
52
+ }
53
+
54
+ protected _nodes: E[];
55
+
56
+ get nodes(): E[] {
57
+ return this._nodes;
58
+ }
59
+
60
+ protected _offset: number;
61
+
62
+ get offset(): number {
63
+ return this._offset;
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
+ * Time Complexity: O(1) - constant time as it adds an element to the end of the array.
88
+ * Space Complexity: O(1) - no additional space is used.
89
+ */
90
+
91
+ /**
92
+ * Time Complexity: O(1) - constant time as it adds an element to the end of the array.
93
+ * Space Complexity: O(1) - no additional space is used.
94
+ *
95
+ * 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.
96
+ * @param {E} element - The `element` parameter represents the element that you want to add to the queue.
97
+ * @returns The `add` method is returning a `Queue<E>` object.
98
+ */
99
+ push(element: E): Queue<E> {
100
+ this.nodes.push(element);
101
+ return this;
102
+ }
103
+
104
+ /**
105
+ * Time Complexity: O(n) - where n is the number of elements in the queue. In the worst case, it may need to shift all elements to update the offset.
106
+ * Space Complexity: O(1) - no additional space is used.
107
+ */
108
+
109
+ /**
110
+ * Time Complexity: O(n) - where n is the number of elements in the queue. In the worst case, it may need to shift all elements to update the offset.
111
+ * Space Complexity: O(1) - no additional space is used.
112
+ *
113
+ * The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if
114
+ * necessary to optimize performance.
115
+ * @returns The function `shift()` returns either the first element in the queue or `null` if the queue is empty.
116
+ */
117
+ shift(): E | undefined {
118
+ if (this.size === 0) return undefined;
119
+
120
+ const first = this.getFirst();
121
+ this._offset += 1;
122
+
123
+ if (this.offset * 2 < this.nodes.length) return first;
124
+
125
+ // only delete dequeued elements when reaching half size
126
+ // to decrease latency of shifting elements.
127
+ this._nodes = this.nodes.slice(this.offset);
128
+ this._offset = 0;
129
+ return first;
130
+ }
131
+
132
+ /**
133
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
134
+ * Space Complexity: O(1) - no additional space is used.
135
+ */
136
+
137
+ /**
138
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
139
+ * Space Complexity: O(1) - no additional space is used.
140
+ *
141
+ * The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
142
+ * @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at
143
+ * the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
144
+ */
145
+ getFirst(): E | undefined {
146
+ return this.size > 0 ? this.nodes[this.offset] : undefined;
147
+ }
148
+
149
+ /**
150
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
151
+ * Space Complexity: O(1) - no additional space is used.
152
+ */
153
+
154
+ /**
155
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
156
+ * Space Complexity: O(1) - no additional space is used.
157
+ *
158
+ * The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
159
+ * @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
160
+ * the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
161
+ */
162
+ peek(): E | undefined {
163
+ return this.getFirst();
164
+ }
165
+
166
+ /**
167
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
168
+ * Space Complexity: O(1) - no additional space is used.
169
+ */
170
+
171
+ /**
172
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
173
+ * Space Complexity: O(1) - no additional space is used.
174
+ *
175
+ * The `getLast` function returns the last element in an array-like data structure, or null if the structure is empty.
176
+ * @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
177
+ * array is empty, it returns `null`.
178
+ */
179
+ getLast(): E | undefined {
180
+ return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
181
+ }
182
+
183
+ /**
184
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
185
+ * Space Complexity: O(1) - no additional space is used.
186
+ */
187
+
188
+ /**
189
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
190
+ * Space Complexity: O(1) - no additional space is used.
191
+ *
192
+ * The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty.
193
+ * @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
194
+ * array is empty, it returns `null`.
195
+ */
196
+ peekLast(): E | undefined {
197
+ return this.getLast();
198
+ }
199
+
200
+ /**
201
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
202
+ * Space Complexity: O(1) - no additional space is used.
203
+ */
204
+
205
+ /**
206
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
207
+ * Space Complexity: O(1) - no additional space is used.
208
+ *
209
+ * The enqueue function adds a value to the end of a queue.
210
+ * @param {E} value - The value parameter represents the value that you want to add to the queue.
211
+ */
212
+ enqueue(value: E) {
213
+ this.push(value);
214
+ }
215
+
216
+ /**
217
+ * Time Complexity: O(n) - same as shift().
218
+ * Space Complexity: O(1) - same as shift().
219
+ */
220
+
221
+ /**
222
+ * Time Complexity: O(n) - same as shift().
223
+ * Space Complexity: O(1) - same as shift().
224
+ *
225
+ * The `dequeue` function removes and returns the first element from a queue, or returns null if the queue is empty.
226
+ * @returns The method is returning a value of type E or null.
227
+ */
228
+ dequeue(): E | undefined {
229
+ return this.shift();
230
+ }
231
+
232
+ /**
233
+ * Time Complexity: O(1) - constant time as it retrieves the value at the specified index.
234
+ * Space Complexity: O(1) - no additional space is used.
235
+ */
236
+
237
+ /**
238
+ * Time Complexity: O(1) - constant time as it retrieves the value at the specified index.
239
+ * Space Complexity: O(1) - no additional space is used.
240
+ *
241
+ * @param index
242
+ */
243
+ getAt(index: number): E | undefined {
244
+ return this.nodes[index];
245
+ }
246
+
247
+ /**
248
+ * Time Complexity: O(1) - constant time as it retrieves the value at the specified index.
249
+ * Space Complexity: O(1) - no additional space is used.
250
+ */
251
+
252
+ /**
253
+ * Time Complexity: O(1) - constant time as it retrieves the value at the specified index.
254
+ * Space Complexity: O(1) - no additional space is used.
255
+ *
256
+ * The function checks if a data structure is empty by comparing its size to zero.
257
+ * @returns {boolean} A boolean value indicating whether the size of the object is 0 or not.
258
+ */
259
+ isEmpty(): boolean {
260
+ return this.size === 0;
261
+ }
262
+
263
+ /**
264
+ * Time Complexity: O(1) - constant time as it returns a shallow copy of the internal array.
265
+ * Space Complexity: O(n) - where n is the number of elements in the queue.
266
+ */
267
+
268
+ /**
269
+ * Time Complexity: O(1) - constant time as it returns a shallow copy of the internal array.
270
+ * Space Complexity: O(n) - where n is the number of elements in the queue.
271
+ *
272
+ * The toArray() function returns an array of elements from the current offset to the end of the _nodes array.
273
+ * @returns An array of type E is being returned.
274
+ */
275
+ toArray(): E[] {
276
+ return this.nodes.slice(this.offset);
277
+ }
278
+
279
+ /**
280
+ * The clear function resets the nodes array and offset to their initial values.
281
+ */
282
+ clear(): void {
283
+ this._nodes = [];
284
+ this._offset = 0;
285
+ }
286
+
287
+ /**
288
+ * Time Complexity: O(n) - where n is the number of elements in the queue. It creates a shallow copy of the internal array.
289
+ * Space Complexity: O(n) - the space required is proportional to the number of elements in the queue.
290
+ */
291
+
292
+ /**
293
+ * Time Complexity: O(n) - where n is the number of elements in the queue. It creates a shallow copy of the internal array.
294
+ * Space Complexity: O(n) - the space required is proportional to the number of elements in the queue.
295
+ *
296
+ * The `clone()` function returns a new Queue object with the same elements as the original Queue.
297
+ * @returns The `clone()` method is returning a new instance of the `Queue` class.
298
+ */
299
+ clone(): Queue<E> {
300
+ return new Queue(this.nodes.slice(this.offset));
301
+ }
302
+
303
+ * [Symbol.iterator]() {
304
+ for (const item of this.nodes) {
305
+ yield item;
306
+ }
307
+ }
308
+ }
@@ -0,0 +1 @@
1
+ export * from './stack';
@@ -0,0 +1,150 @@
1
+ /**
2
+ * @license MIT
3
+ * @copyright Tyler Zeng <zrwusa@gmail.com>
4
+ * @class
5
+ */
6
+ export class Stack<E = any> {
7
+ /**
8
+ * The constructor initializes an array of elements, which can be provided as an optional parameter.
9
+ * @param {E[]} [elements] - The `elements` parameter is an optional parameter of type `E[]`, which represents an array
10
+ * of elements of type `E`. It is used to initialize the `_elements` property of the class. If the `elements` parameter
11
+ * is provided and is an array, it is assigned to the `_elements
12
+ */
13
+ constructor(elements?: E[]) {
14
+ this._elements = Array.isArray(elements) ? elements : [];
15
+ }
16
+
17
+ protected _elements: E[];
18
+
19
+ get elements(): E[] {
20
+ return this._elements;
21
+ }
22
+
23
+ /**
24
+ * Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
25
+ * Space Complexity: O(n), as it creates a new stack with the elements from the input array.
26
+ */
27
+
28
+ /**
29
+ * Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
30
+ * Space Complexity: O(n), as it creates a new stack with the elements from the input array.
31
+ *
32
+ * The function "fromArray" creates a new Stack object from an array of elements.
33
+ * @param {E[]} elements - The `elements` parameter is an array of elements of type `E`.
34
+ * @returns {Stack} The method is returning a new instance of the Stack class, initialized with the elements from the input
35
+ * array.
36
+ */
37
+ static fromArray<E>(elements: E[]): Stack<E> {
38
+ return new Stack(elements);
39
+ }
40
+
41
+ /**
42
+ * The function checks if an array is empty and returns a boolean value.
43
+ * @returns A boolean value indicating whether the `_elements` array is empty or not.
44
+ */
45
+ isEmpty(): boolean {
46
+ return this.elements.length === 0;
47
+ }
48
+
49
+ /**
50
+ * The size() function returns the number of elements in an array.
51
+ * @returns The size of the elements array.
52
+ */
53
+ size(): number {
54
+ return this.elements.length;
55
+ }
56
+
57
+ /**
58
+ * Time Complexity: O(1), as it only involves accessing the last element of the array.
59
+ * Space Complexity: O(1), as it does not use any additional space.
60
+ */
61
+
62
+ /**
63
+ * Time Complexity: O(1), as it only involves accessing the last element of the array.
64
+ * Space Complexity: O(1), as it does not use any additional space.
65
+ *
66
+ * The `peek` function returns the last element of an array, or null if the array is empty.
67
+ * @returns The `peek()` function returns the last element of the `_elements` array, or `null` if the array is empty.
68
+ */
69
+ peek(): E | null {
70
+ if (this.isEmpty()) return null;
71
+
72
+ return this.elements[this.elements.length - 1];
73
+ }
74
+
75
+ /**
76
+ * Time Complexity: O(1), as it only involves accessing the last element of the array.
77
+ * Space Complexity: O(1), as it does not use any additional space.
78
+ */
79
+
80
+ /**
81
+ * Time Complexity: O(1), as it only involves accessing the last element of the array.
82
+ * Space Complexity: O(1), as it does not use any additional space.
83
+ *
84
+ * The push function adds an element to the stack and returns the updated stack.
85
+ * @param {E} element - The parameter "element" is of type E, which means it can be any data type.
86
+ * @returns The `push` method is returning the updated `Stack<E>` object.
87
+ */
88
+ push(element: E): Stack<E> {
89
+ this.elements.push(element);
90
+ return this;
91
+ }
92
+
93
+ /**
94
+ * Time Complexity: O(1), as it only involves accessing the last element of the array.
95
+ * Space Complexity: O(1), as it does not use any additional space.
96
+ */
97
+
98
+ /**
99
+ * Time Complexity: O(1), as it only involves accessing the last element of the array.
100
+ * Space Complexity: O(1), as it does not use any additional space.
101
+ *
102
+ * The `pop` function removes and returns the last element from an array, or returns null if the array is empty.
103
+ * @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
104
+ * array is empty, it returns `null`.
105
+ */
106
+ pop(): E | null {
107
+ if (this.isEmpty()) return null;
108
+
109
+ return this.elements.pop() || null;
110
+ }
111
+
112
+ /**
113
+ * Time Complexity: O(n)
114
+ * Space Complexity: O(n)
115
+ */
116
+
117
+ /**
118
+ * Time Complexity: O(n)
119
+ * Space Complexity: O(n)
120
+ *
121
+ * The toArray function returns a copy of the elements in an array.
122
+ * @returns An array of type E.
123
+ */
124
+ toArray(): E[] {
125
+ return this.elements.slice();
126
+ }
127
+
128
+ /**
129
+ * The clear function clears the elements array.
130
+ */
131
+ clear(): void {
132
+ this._elements = [];
133
+ }
134
+
135
+ /**
136
+ * Time Complexity: O(n), where n is the number of elements in the stack, as it creates a new stack and copies all elements into it.
137
+ * Space Complexity: O(n), as it creates a new stack.
138
+ */
139
+
140
+ /**
141
+ * Time Complexity: O(n), where n is the number of elements in the stack, as it creates a new stack and copies all elements into it.
142
+ * Space Complexity: O(n), as it creates a new stack.
143
+ *
144
+ * The `clone()` function returns a new `Stack` object with the same elements as the original stack.
145
+ * @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
146
+ */
147
+ clone(): Stack<E> {
148
+ return new Stack(this.elements.slice());
149
+ }
150
+ }
@@ -0,0 +1 @@
1
+ export * from './tree';
@@ -0,0 +1,41 @@
1
+ export class TreeNode<V = any> {
2
+ key: string;
3
+ value?: V | undefined;
4
+ children?: TreeNode<V>[] | undefined;
5
+
6
+ constructor(key: string, value?: V, children?: TreeNode<V>[]) {
7
+ this.key = key;
8
+ this.value = value || undefined;
9
+ this.children = children || [];
10
+ }
11
+
12
+ addChildren(children: TreeNode<V> | TreeNode<V>[]) {
13
+ if (!this.children) {
14
+ this.children = [];
15
+ }
16
+ if (children instanceof TreeNode) {
17
+ this.children.push(children);
18
+ } else {
19
+ this.children = this.children.concat(children);
20
+ }
21
+ }
22
+
23
+ getHeight() {
24
+ let maxDepth = 0;
25
+ if (this) {
26
+ const bfs = (node: TreeNode<V>, level: number) => {
27
+ if (level > maxDepth) {
28
+ maxDepth = level;
29
+ }
30
+ const { children } = node;
31
+ if (children) {
32
+ for (let i = 0, len = children.length; i < len; i++) {
33
+ bfs(children[i], level + 1);
34
+ }
35
+ }
36
+ };
37
+ bfs(this, 0);
38
+ }
39
+ return maxDepth;
40
+ }
41
+ }
@@ -0,0 +1 @@
1
+ export * from './trie';