linked-list-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,375 @@
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 type {BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions} from '../../types';
9
+ import {BinaryTreeDeletedResult, CP, FamilyPosition, IterationType} from '../../types';
10
+ import {IBinaryTree} from '../../interfaces';
11
+ import {AVLTree, AVLTreeNode} from './avl-tree';
12
+
13
+ export class TreeMultisetNode<
14
+ V = any,
15
+ FAMILY extends TreeMultisetNode<V, FAMILY> = TreeMultisetNodeNested<V>
16
+ > extends AVLTreeNode<V, FAMILY> {
17
+ count: number;
18
+
19
+ /**
20
+ * The constructor function initializes a BinaryTreeNode object with a key, value, and count.
21
+ * @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
22
+ * of the binary tree node.
23
+ * @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value of the binary
24
+ * tree node. If no value is provided, it will be `undefined`.
25
+ * @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value
26
+ * occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
27
+ * parameter when creating a new instance of the `BinaryTreeNode` class.
28
+ */
29
+ constructor(key: BinaryTreeNodeKey, val?: V, count = 1) {
30
+ super(key, val);
31
+ this.count = count;
32
+ }
33
+ }
34
+
35
+ /**
36
+ * The only distinction between a TreeMultiset and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
37
+ */
38
+ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultisetNode>
39
+ extends AVLTree<N>
40
+ implements IBinaryTree<N>
41
+ {
42
+ /**
43
+ * The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
44
+ * merge duplicated values.
45
+ * @param {TreeMultisetOptions} [options] - An optional object that contains additional configuration options for the
46
+ * TreeMultiset.
47
+ */
48
+ constructor(options?: TreeMultisetOptions) {
49
+ super(options);
50
+ }
51
+
52
+ private _count = 0;
53
+
54
+ get count(): number {
55
+ return this._count;
56
+ }
57
+
58
+ /**
59
+ * The function creates a new BSTNode with the given key, value, and count.
60
+ * @param {BinaryTreeNodeKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
61
+ * distinguish one node from another in the tree.
62
+ * @param {N} val - The `val` parameter represents the value that will be stored in the binary search tree node.
63
+ * @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
64
+ * occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
65
+ * @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
66
+ */
67
+ override createNode(key: BinaryTreeNodeKey, val?: N['val'], count?: number): N {
68
+ return new TreeMultisetNode(key, val, count) as N;
69
+ }
70
+
71
+ /**
72
+ * The `add` function adds a new node to a binary search tree, updating the count if the key already
73
+ * exists, and balancing the tree if necessary.
74
+ * @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
75
+ * `BinaryTreeNodeKey` (which represents the key of the node to be added), a `N` (which represents a
76
+ * node to be added), or `null` (which represents a null node).
77
+ * @param [val] - The `val` parameter represents the value associated with the key that is being
78
+ * added to the binary tree.
79
+ * @param [count=1] - The `count` parameter represents the number of occurrences of the key/value
80
+ * pair that will be added to the binary tree. It has a default value of 1, which means that if no
81
+ * count is specified, the default count will be 1.
82
+ * @returns The function `add` returns a value of type `N | null | undefined`.
83
+ */
84
+ override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val'], count = 1): N | null | undefined {
85
+ let inserted: N | null | undefined = undefined,
86
+ newNode: N | null;
87
+ if (keyOrNode instanceof TreeMultisetNode) {
88
+ newNode = this.createNode(keyOrNode.key, keyOrNode.val, keyOrNode.count);
89
+ } else if (keyOrNode === null) {
90
+ newNode = null;
91
+ } else {
92
+ newNode = this.createNode(keyOrNode, val, count);
93
+ }
94
+ if (!this.root) {
95
+ this._setRoot(newNode);
96
+ this._setSize(this.size + 1);
97
+ newNode && this._setCount(this.count + newNode.count);
98
+ inserted = this.root;
99
+ } else {
100
+ let cur = this.root;
101
+ let traversing = true;
102
+ while (traversing) {
103
+ if (cur) {
104
+ if (newNode) {
105
+ if (this._compare(cur.key, newNode.key) === CP.eq) {
106
+ cur.val = newNode.val;
107
+ cur.count += newNode.count;
108
+ this._setCount(this.count + newNode.count);
109
+ traversing = false;
110
+ inserted = cur;
111
+ } else if (this._compare(cur.key, newNode.key) === CP.gt) {
112
+ // Traverse left of the node
113
+ if (cur.left === undefined) {
114
+ //Add to the left of the current node
115
+ cur.left = newNode;
116
+ this._setSize(this.size + 1);
117
+ this._setCount(this.count + newNode.count);
118
+
119
+ traversing = false;
120
+ inserted = cur.left;
121
+ } else {
122
+ //Traverse the left of the current node
123
+ if (cur.left) cur = cur.left;
124
+ }
125
+ } else if (this._compare(cur.key, newNode.key) === CP.lt) {
126
+ // Traverse right of the node
127
+ if (cur.right === undefined) {
128
+ //Add to the right of the current node
129
+ cur.right = newNode;
130
+ this._setSize(this.size + 1);
131
+ this._setCount(this.count + newNode.count);
132
+
133
+ traversing = false;
134
+ inserted = cur.right;
135
+ } else {
136
+ //Traverse the left of the current node
137
+ if (cur.right) cur = cur.right;
138
+ }
139
+ }
140
+ } else {
141
+ // TODO may need to support null inserted
142
+ }
143
+ } else {
144
+ traversing = false;
145
+ }
146
+ }
147
+ }
148
+ if (inserted) this._balancePath(inserted);
149
+ return inserted;
150
+ }
151
+
152
+ /**
153
+ * The function adds a new node to a binary tree if there is an available slot in the parent node.
154
+ * @param {N | null} newNode - The `newNode` parameter represents the node that needs to be added to
155
+ * the tree. It can be either a node object (`N`) or `null`.
156
+ * @param {N} parent - The `parent` parameter represents the parent node to which the new node will
157
+ * be added as a child.
158
+ * @returns The method `_addTo` returns either the `parent.left`, `parent.right`, or `undefined`.
159
+ */
160
+ override _addTo(newNode: N | null, parent: N): N | null | undefined {
161
+ if (parent) {
162
+ if (parent.left === undefined) {
163
+ parent.left = newNode;
164
+ if (newNode !== null) {
165
+ this._setSize(this.size + 1);
166
+ this._setCount(this.count + newNode.count);
167
+ }
168
+
169
+ return parent.left;
170
+ } else if (parent.right === undefined) {
171
+ parent.right = newNode;
172
+ if (newNode !== null) {
173
+ this._setSize(this.size + 1);
174
+ this._setCount(this.count + newNode.count);
175
+ }
176
+ return parent.right;
177
+ } else {
178
+ return;
179
+ }
180
+ } else {
181
+ return;
182
+ }
183
+ }
184
+
185
+ /**
186
+ * The `addMany` function adds multiple keys or nodes to a TreeMultiset and returns an array of the
187
+ * inserted nodes.
188
+ * @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
189
+ * added to the multiset. Each element can be either a BinaryTreeNodeKey or a TreeMultisetNode.
190
+ * @param {N['val'][]} [data] - The `data` parameter is an optional array of values that correspond
191
+ * to the keys or nodes being added to the multiset. It is used to associate additional data with
192
+ * each key or node.
193
+ * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
194
+ */
195
+ override addMany(
196
+ keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[],
197
+ data?: N['val'][]
198
+ ): (N | null | undefined)[] {
199
+ const inserted: (N | null | undefined)[] = [];
200
+
201
+ for (let i = 0; i < keysOrNodes.length; i++) {
202
+ const keyOrNode = keysOrNodes[i];
203
+
204
+ if (keyOrNode instanceof TreeMultisetNode) {
205
+ inserted.push(this.add(keyOrNode.key, keyOrNode.val, keyOrNode.count));
206
+ continue;
207
+ }
208
+
209
+ if (keyOrNode === null) {
210
+ inserted.push(this.add(NaN, null, 0));
211
+ continue;
212
+ }
213
+
214
+ inserted.push(this.add(keyOrNode, data?.[i], 1));
215
+ }
216
+ return inserted;
217
+ }
218
+
219
+ /**
220
+ * The `perfectlyBalance` function in TypeScript takes a sorted array of nodes and builds a balanced
221
+ * binary search tree using either a recursive or iterative approach.
222
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
223
+ * type of iteration to use when building a balanced binary search tree. It can have two possible
224
+ * values:
225
+ * @returns a boolean value.
226
+ */
227
+ override perfectlyBalance(iterationType = this.iterationType): boolean {
228
+ const sorted = this.dfs(node => node, 'in'),
229
+ n = sorted.length;
230
+ if (sorted.length < 1) return false;
231
+
232
+ this.clear();
233
+
234
+ if (iterationType === IterationType.RECURSIVE) {
235
+ const buildBalanceBST = (l: number, r: number) => {
236
+ if (l > r) return;
237
+ const m = l + Math.floor((r - l) / 2);
238
+ const midNode = sorted[m];
239
+ this.add(midNode.key, midNode.val, midNode.count);
240
+ buildBalanceBST(l, m - 1);
241
+ buildBalanceBST(m + 1, r);
242
+ };
243
+
244
+ buildBalanceBST(0, n - 1);
245
+ return true;
246
+ } else {
247
+ const stack: [[number, number]] = [[0, n - 1]];
248
+ while (stack.length > 0) {
249
+ const popped = stack.pop();
250
+ if (popped) {
251
+ const [l, r] = popped;
252
+ if (l <= r) {
253
+ const m = l + Math.floor((r - l) / 2);
254
+ const midNode = sorted[m];
255
+ this.add(midNode.key, midNode.val, midNode.count);
256
+ stack.push([m + 1, r]);
257
+ stack.push([l, m - 1]);
258
+ }
259
+ }
260
+ }
261
+ return true;
262
+ }
263
+ }
264
+
265
+ /**
266
+ * The `delete` function in a binary search tree deletes a node from the tree and returns the deleted
267
+ * node along with the parent node that needs to be balanced.
268
+ * @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object
269
+ * (`N`) or a key value (`BinaryTreeNodeKey`). It represents the node or key that needs to be deleted
270
+ * from the binary tree.
271
+ * @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
272
+ * being deleted. If set to true, the count of the node will not be considered and the node will be
273
+ * deleted regardless of its count. If set to false (default), the count of the node will be
274
+ * decremented by 1 and
275
+ * @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
276
+ */
277
+ override delete(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount = false): BinaryTreeDeletedResult<N>[] {
278
+ const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
279
+ if (!this.root) return bstDeletedResult;
280
+
281
+ const curr: N | null = this.get(nodeOrKey);
282
+ if (!curr) return bstDeletedResult;
283
+
284
+ const parent: N | null = curr?.parent ? curr.parent : null;
285
+ let needBalanced: N | null = null,
286
+ orgCurrent = curr;
287
+
288
+ if (curr.count > 1 && !ignoreCount) {
289
+ curr.count--;
290
+ this._setCount(this.count - 1);
291
+ } else {
292
+ if (!curr.left) {
293
+ if (!parent) {
294
+ if (curr.right !== undefined) this._setRoot(curr.right);
295
+ } else {
296
+ const {familyPosition: fp} = curr;
297
+ if (fp === FamilyPosition.LEFT || fp === FamilyPosition.ROOT_LEFT) {
298
+ parent.left = curr.right;
299
+ } else if (fp === FamilyPosition.RIGHT || fp === FamilyPosition.ROOT_RIGHT) {
300
+ parent.right = curr.right;
301
+ }
302
+ needBalanced = parent;
303
+ }
304
+ } else {
305
+ const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
306
+ if (leftSubTreeRightMost) {
307
+ const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
308
+ orgCurrent = this._swap(curr, leftSubTreeRightMost);
309
+ if (parentOfLeftSubTreeMax) {
310
+ if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) {
311
+ parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
312
+ } else {
313
+ parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
314
+ }
315
+ needBalanced = parentOfLeftSubTreeMax;
316
+ }
317
+ }
318
+ }
319
+ this._setSize(this.size - 1);
320
+ // TODO How to handle when the count of target node is lesser than current node's count
321
+ this._setCount(this.count - orgCurrent.count);
322
+ }
323
+
324
+ bstDeletedResult.push({deleted: orgCurrent, needBalanced});
325
+
326
+ if (needBalanced) {
327
+ this._balancePath(needBalanced);
328
+ }
329
+
330
+ return bstDeletedResult;
331
+ }
332
+
333
+ /**
334
+ * The clear() function clears the contents of a data structure and sets the count to zero.
335
+ */
336
+ clear() {
337
+ super.clear();
338
+ this._setCount(0);
339
+ }
340
+
341
+ /**
342
+ * The function swaps the values of two nodes in a binary tree.
343
+ * @param {N} srcNode - The source node that needs to be swapped with the destination node.
344
+ * @param {N} destNode - The `destNode` parameter represents the destination node where the values
345
+ * from `srcNode` will be swapped into.
346
+ * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
347
+ */
348
+ protected override _swap(srcNode: N, destNode: N): N {
349
+ const {key, val, count, height} = destNode;
350
+ const tempNode = this.createNode(key, val, count);
351
+ if (tempNode) {
352
+ tempNode.height = height;
353
+
354
+ destNode.key = srcNode.key;
355
+ destNode.val = srcNode.val;
356
+ destNode.count = srcNode.count;
357
+ destNode.height = srcNode.height;
358
+
359
+ srcNode.key = tempNode.key;
360
+ srcNode.val = tempNode.val;
361
+ srcNode.count = tempNode.count;
362
+ srcNode.height = tempNode.height;
363
+ }
364
+
365
+ return destNode;
366
+ }
367
+
368
+ /**
369
+ * The function sets the value of the "_count" property.
370
+ * @param {number} v - number
371
+ */
372
+ protected _setCount(v: number) {
373
+ this._count = v;
374
+ }
375
+ }