data-structure-typed 1.53.9 → 1.54.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 (488) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +1 -0
  3. package/benchmark/report.html +1 -28
  4. package/benchmark/report.json +17 -326
  5. package/dist/{mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts → cjs/data-structures/binary-tree/avl-tree-counter.d.ts} +54 -59
  6. package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js +408 -0
  7. package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js.map +1 -0
  8. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -189
  9. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +133 -357
  10. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +108 -78
  12. package/dist/cjs/data-structures/binary-tree/avl-tree.js +126 -79
  13. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
  15. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +3 -0
  16. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
  17. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +243 -190
  18. package/dist/cjs/data-structures/binary-tree/binary-tree.js +273 -229
  19. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  20. package/dist/cjs/data-structures/binary-tree/bst.d.ts +141 -122
  21. package/dist/cjs/data-structures/binary-tree/bst.js +170 -134
  22. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  23. package/dist/cjs/data-structures/binary-tree/index.d.ts +2 -0
  24. package/dist/cjs/data-structures/binary-tree/index.js +2 -0
  25. package/dist/cjs/data-structures/binary-tree/index.js.map +1 -1
  26. package/dist/cjs/data-structures/binary-tree/red-black-tree.d.ts +84 -80
  27. package/dist/cjs/data-structures/binary-tree/red-black-tree.js +101 -79
  28. package/dist/cjs/data-structures/binary-tree/red-black-tree.js.map +1 -1
  29. package/dist/{mjs/data-structures/binary-tree/tree-multi-map.d.ts → cjs/data-structures/binary-tree/tree-counter.d.ts} +54 -56
  30. package/dist/cjs/data-structures/binary-tree/tree-counter.js +445 -0
  31. package/dist/cjs/data-structures/binary-tree/tree-counter.js.map +1 -0
  32. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +78 -186
  33. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +140 -388
  34. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  35. package/dist/cjs/data-structures/graph/directed-graph.d.ts +3 -0
  36. package/dist/cjs/data-structures/graph/directed-graph.js +3 -0
  37. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  38. package/dist/cjs/data-structures/graph/map-graph.d.ts +3 -0
  39. package/dist/cjs/data-structures/graph/map-graph.js +3 -0
  40. package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
  41. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +3 -0
  42. package/dist/cjs/data-structures/graph/undirected-graph.js +3 -0
  43. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  44. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +3 -0
  45. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +3 -0
  46. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  47. package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +3 -0
  48. package/dist/cjs/data-structures/linked-list/skip-linked-list.js +3 -0
  49. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
  50. package/dist/cjs/data-structures/matrix/matrix.d.ts +3 -0
  51. package/dist/cjs/data-structures/matrix/matrix.js +3 -0
  52. package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
  53. package/dist/cjs/data-structures/matrix/navigator.d.ts +3 -0
  54. package/dist/cjs/data-structures/matrix/navigator.js +3 -0
  55. package/dist/cjs/data-structures/matrix/navigator.js.map +1 -1
  56. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
  57. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +3 -0
  58. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  59. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
  60. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +3 -0
  61. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  62. package/dist/cjs/data-structures/trie/trie.d.ts +0 -4
  63. package/dist/cjs/data-structures/trie/trie.js +0 -4
  64. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  65. package/dist/cjs/interfaces/binary-tree.d.ts +7 -6
  66. package/dist/cjs/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
  67. package/dist/cjs/types/data-structures/binary-tree/avl-tree-counter.js +3 -0
  68. package/dist/cjs/types/data-structures/binary-tree/avl-tree-counter.js.map +1 -0
  69. package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -3
  70. package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +0 -2
  71. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
  72. package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +3 -2
  73. package/dist/cjs/types/data-structures/binary-tree/index.d.ts +2 -0
  74. package/dist/cjs/types/data-structures/binary-tree/index.js +2 -0
  75. package/dist/cjs/types/data-structures/binary-tree/index.js.map +1 -1
  76. package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -3
  77. package/dist/cjs/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
  78. package/dist/cjs/types/data-structures/binary-tree/tree-counter.js +3 -0
  79. package/dist/cjs/types/data-structures/binary-tree/tree-counter.js.map +1 -0
  80. package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -3
  81. package/dist/esm/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
  82. package/dist/{mjs/data-structures/binary-tree/avl-tree-multi-map.js → esm/data-structures/binary-tree/avl-tree-counter.js} +72 -78
  83. package/dist/esm/data-structures/binary-tree/avl-tree-counter.js.map +1 -0
  84. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -0
  85. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js +191 -0
  86. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -0
  87. package/dist/esm/data-structures/binary-tree/avl-tree.d.ts +234 -0
  88. package/dist/{mjs → esm}/data-structures/binary-tree/avl-tree.js +126 -80
  89. package/dist/esm/data-structures/binary-tree/avl-tree.js.map +1 -0
  90. package/dist/{mjs → esm}/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
  91. package/dist/{mjs → esm}/data-structures/binary-tree/binary-indexed-tree.js +3 -0
  92. package/dist/esm/data-structures/binary-tree/binary-indexed-tree.js.map +1 -0
  93. package/dist/{mjs → esm}/data-structures/binary-tree/binary-tree.d.ts +243 -190
  94. package/dist/{mjs → esm}/data-structures/binary-tree/binary-tree.js +276 -234
  95. package/dist/esm/data-structures/binary-tree/binary-tree.js.map +1 -0
  96. package/dist/{mjs → esm}/data-structures/binary-tree/bst.d.ts +141 -122
  97. package/dist/{mjs → esm}/data-structures/binary-tree/bst.js +171 -140
  98. package/dist/esm/data-structures/binary-tree/bst.js.map +1 -0
  99. package/dist/{mjs → esm}/data-structures/binary-tree/index.d.ts +2 -0
  100. package/dist/{mjs → esm}/data-structures/binary-tree/index.js +2 -0
  101. package/dist/{mjs → esm}/data-structures/binary-tree/index.js.map +1 -1
  102. package/dist/{mjs → esm}/data-structures/binary-tree/red-black-tree.d.ts +84 -80
  103. package/dist/{mjs → esm}/data-structures/binary-tree/red-black-tree.js +100 -79
  104. package/dist/esm/data-structures/binary-tree/red-black-tree.js.map +1 -0
  105. package/dist/esm/data-structures/binary-tree/tree-counter.d.ts +212 -0
  106. package/dist/{mjs/data-structures/binary-tree/tree-multi-map.js → esm/data-structures/binary-tree/tree-counter.js} +88 -86
  107. package/dist/esm/data-structures/binary-tree/tree-counter.js.map +1 -0
  108. package/dist/esm/data-structures/binary-tree/tree-multi-map.d.ts +106 -0
  109. package/dist/esm/data-structures/binary-tree/tree-multi-map.js +196 -0
  110. package/dist/esm/data-structures/binary-tree/tree-multi-map.js.map +1 -0
  111. package/dist/{mjs → esm}/data-structures/graph/directed-graph.d.ts +3 -0
  112. package/dist/{mjs → esm}/data-structures/graph/directed-graph.js +3 -0
  113. package/dist/esm/data-structures/graph/directed-graph.js.map +1 -0
  114. package/dist/{mjs → esm}/data-structures/graph/map-graph.d.ts +3 -0
  115. package/dist/{mjs → esm}/data-structures/graph/map-graph.js +3 -0
  116. package/dist/esm/data-structures/graph/map-graph.js.map +1 -0
  117. package/dist/{mjs → esm}/data-structures/graph/undirected-graph.d.ts +3 -0
  118. package/dist/{mjs → esm}/data-structures/graph/undirected-graph.js +3 -0
  119. package/dist/esm/data-structures/graph/undirected-graph.js.map +1 -0
  120. package/dist/{mjs → esm}/data-structures/linked-list/singly-linked-list.d.ts +3 -0
  121. package/dist/{mjs → esm}/data-structures/linked-list/singly-linked-list.js +3 -0
  122. package/dist/esm/data-structures/linked-list/singly-linked-list.js.map +1 -0
  123. package/dist/{mjs → esm}/data-structures/linked-list/skip-linked-list.d.ts +3 -0
  124. package/dist/{mjs → esm}/data-structures/linked-list/skip-linked-list.js +3 -0
  125. package/dist/esm/data-structures/linked-list/skip-linked-list.js.map +1 -0
  126. package/dist/{mjs → esm}/data-structures/matrix/matrix.d.ts +3 -0
  127. package/dist/{mjs → esm}/data-structures/matrix/matrix.js +3 -0
  128. package/dist/esm/data-structures/matrix/matrix.js.map +1 -0
  129. package/dist/{mjs → esm}/data-structures/matrix/navigator.d.ts +3 -0
  130. package/dist/{mjs → esm}/data-structures/matrix/navigator.js +3 -0
  131. package/dist/esm/data-structures/matrix/navigator.js.map +1 -0
  132. package/dist/{mjs → esm}/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
  133. package/dist/{mjs → esm}/data-structures/priority-queue/max-priority-queue.js +3 -0
  134. package/dist/esm/data-structures/priority-queue/max-priority-queue.js.map +1 -0
  135. package/dist/{mjs → esm}/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
  136. package/dist/{mjs → esm}/data-structures/priority-queue/min-priority-queue.js +3 -0
  137. package/dist/esm/data-structures/priority-queue/min-priority-queue.js.map +1 -0
  138. package/dist/{mjs → esm}/data-structures/trie/trie.d.ts +0 -4
  139. package/dist/{mjs → esm}/data-structures/trie/trie.js +0 -4
  140. package/dist/esm/data-structures/trie/trie.js.map +1 -0
  141. package/dist/esm/interfaces/binary-tree.d.ts +9 -0
  142. package/dist/esm/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
  143. package/dist/esm/types/data-structures/binary-tree/avl-tree-counter.js +2 -0
  144. package/dist/esm/types/data-structures/binary-tree/avl-tree-counter.js.map +1 -0
  145. package/dist/esm/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
  146. package/dist/esm/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
  147. package/dist/{mjs → esm}/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
  148. package/dist/{mjs → esm}/types/data-structures/binary-tree/bst.d.ts +3 -2
  149. package/dist/{mjs → esm}/types/data-structures/binary-tree/index.d.ts +2 -0
  150. package/dist/{mjs → esm}/types/data-structures/binary-tree/index.js +2 -0
  151. package/dist/{mjs → esm}/types/data-structures/binary-tree/index.js.map +1 -1
  152. package/dist/esm/types/data-structures/binary-tree/rb-tree.d.ts +3 -0
  153. package/dist/esm/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
  154. package/dist/esm/types/data-structures/binary-tree/tree-counter.js +2 -0
  155. package/dist/esm/types/data-structures/binary-tree/tree-counter.js.map +1 -0
  156. package/dist/esm/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
  157. package/dist/umd/data-structure-typed.js +1407 -913
  158. package/dist/umd/data-structure-typed.min.js +5 -5
  159. package/dist/umd/data-structure-typed.min.js.map +1 -1
  160. package/package.json +14 -14
  161. package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
  162. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +148 -394
  163. package/src/data-structures/binary-tree/avl-tree.ts +152 -112
  164. package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
  165. package/src/data-structures/binary-tree/binary-tree.ts +446 -379
  166. package/src/data-structures/binary-tree/bst.ts +224 -201
  167. package/src/data-structures/binary-tree/index.ts +2 -0
  168. package/src/data-structures/binary-tree/red-black-tree.ts +138 -114
  169. package/src/data-structures/binary-tree/tree-counter.ts +504 -0
  170. package/src/data-structures/binary-tree/tree-multi-map.ts +156 -428
  171. package/src/data-structures/graph/directed-graph.ts +3 -0
  172. package/src/data-structures/graph/map-graph.ts +3 -0
  173. package/src/data-structures/graph/undirected-graph.ts +3 -0
  174. package/src/data-structures/linked-list/singly-linked-list.ts +3 -0
  175. package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
  176. package/src/data-structures/matrix/matrix.ts +3 -0
  177. package/src/data-structures/matrix/navigator.ts +3 -0
  178. package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
  179. package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
  180. package/src/data-structures/trie/trie.ts +0 -4
  181. package/src/interfaces/binary-tree.ts +10 -11
  182. package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
  183. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -4
  184. package/src/types/data-structures/binary-tree/avl-tree.ts +0 -3
  185. package/src/types/data-structures/binary-tree/binary-tree.ts +0 -5
  186. package/src/types/data-structures/binary-tree/bst.ts +5 -3
  187. package/src/types/data-structures/binary-tree/index.ts +2 -0
  188. package/src/types/data-structures/binary-tree/rb-tree.ts +1 -4
  189. package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
  190. package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -4
  191. package/test/integration/all-in-one.test.ts +1 -1
  192. package/test/integration/avl-tree.test.ts +1 -1
  193. package/test/integration/bst.test.ts +2 -2
  194. package/test/integration/compile.js +144 -0
  195. package/test/integration/compile.ts +171 -0
  196. package/test/integration/index.html +48 -48
  197. package/test/performance/data-structures/binary-tree/avl-tree.test.ts +4 -4
  198. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +5 -4
  199. package/test/performance/data-structures/comparison/comparison.test.ts +2 -2
  200. package/test/performance/reportor.ts +5 -3
  201. package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +877 -0
  202. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +459 -761
  203. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +223 -223
  204. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +474 -492
  205. package/test/unit/data-structures/binary-tree/bst.test.ts +1 -1
  206. package/test/unit/data-structures/binary-tree/overall.test.ts +0 -3
  207. package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +87 -90
  208. package/test/unit/data-structures/binary-tree/tree-counter.test.ts +975 -0
  209. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +729 -857
  210. package/test/unit/data-structures/hash/hash-map.test.ts +1 -1
  211. package/{tsconfig-mjs.json → tsconfig-esm.json} +1 -1
  212. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js.map +0 -1
  213. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +0 -204
  214. package/dist/mjs/data-structures/binary-tree/avl-tree.js.map +0 -1
  215. package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js.map +0 -1
  216. package/dist/mjs/data-structures/binary-tree/binary-tree.js.map +0 -1
  217. package/dist/mjs/data-structures/binary-tree/bst.js.map +0 -1
  218. package/dist/mjs/data-structures/binary-tree/red-black-tree.js.map +0 -1
  219. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js.map +0 -1
  220. package/dist/mjs/data-structures/graph/directed-graph.js.map +0 -1
  221. package/dist/mjs/data-structures/graph/map-graph.js.map +0 -1
  222. package/dist/mjs/data-structures/graph/undirected-graph.js.map +0 -1
  223. package/dist/mjs/data-structures/linked-list/singly-linked-list.js.map +0 -1
  224. package/dist/mjs/data-structures/linked-list/skip-linked-list.js.map +0 -1
  225. package/dist/mjs/data-structures/matrix/matrix.js.map +0 -1
  226. package/dist/mjs/data-structures/matrix/navigator.js.map +0 -1
  227. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js.map +0 -1
  228. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js.map +0 -1
  229. package/dist/mjs/data-structures/trie/trie.js.map +0 -1
  230. package/dist/mjs/interfaces/binary-tree.d.ts +0 -8
  231. package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -4
  232. package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +0 -4
  233. package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +0 -5
  234. package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +0 -4
  235. /package/dist/{mjs → esm}/common/index.d.ts +0 -0
  236. /package/dist/{mjs → esm}/common/index.js +0 -0
  237. /package/dist/{mjs → esm}/common/index.js.map +0 -0
  238. /package/dist/{mjs → esm}/data-structures/base/index.d.ts +0 -0
  239. /package/dist/{mjs → esm}/data-structures/base/index.js +0 -0
  240. /package/dist/{mjs → esm}/data-structures/base/index.js.map +0 -0
  241. /package/dist/{mjs → esm}/data-structures/base/iterable-element-base.d.ts +0 -0
  242. /package/dist/{mjs → esm}/data-structures/base/iterable-element-base.js +0 -0
  243. /package/dist/{mjs → esm}/data-structures/base/iterable-element-base.js.map +0 -0
  244. /package/dist/{mjs → esm}/data-structures/base/iterable-entry-base.d.ts +0 -0
  245. /package/dist/{mjs → esm}/data-structures/base/iterable-entry-base.js +0 -0
  246. /package/dist/{mjs → esm}/data-structures/base/iterable-entry-base.js.map +0 -0
  247. /package/dist/{mjs → esm}/data-structures/binary-tree/segment-tree.d.ts +0 -0
  248. /package/dist/{mjs → esm}/data-structures/binary-tree/segment-tree.js +0 -0
  249. /package/dist/{mjs → esm}/data-structures/binary-tree/segment-tree.js.map +0 -0
  250. /package/dist/{mjs → esm}/data-structures/graph/abstract-graph.d.ts +0 -0
  251. /package/dist/{mjs → esm}/data-structures/graph/abstract-graph.js +0 -0
  252. /package/dist/{mjs → esm}/data-structures/graph/abstract-graph.js.map +0 -0
  253. /package/dist/{mjs → esm}/data-structures/graph/index.d.ts +0 -0
  254. /package/dist/{mjs → esm}/data-structures/graph/index.js +0 -0
  255. /package/dist/{mjs → esm}/data-structures/graph/index.js.map +0 -0
  256. /package/dist/{mjs → esm}/data-structures/hash/hash-map.d.ts +0 -0
  257. /package/dist/{mjs → esm}/data-structures/hash/hash-map.js +0 -0
  258. /package/dist/{mjs → esm}/data-structures/hash/hash-map.js.map +0 -0
  259. /package/dist/{mjs → esm}/data-structures/hash/index.d.ts +0 -0
  260. /package/dist/{mjs → esm}/data-structures/hash/index.js +0 -0
  261. /package/dist/{mjs → esm}/data-structures/hash/index.js.map +0 -0
  262. /package/dist/{mjs → esm}/data-structures/heap/heap.d.ts +0 -0
  263. /package/dist/{mjs → esm}/data-structures/heap/heap.js +0 -0
  264. /package/dist/{mjs → esm}/data-structures/heap/heap.js.map +0 -0
  265. /package/dist/{mjs → esm}/data-structures/heap/index.d.ts +0 -0
  266. /package/dist/{mjs → esm}/data-structures/heap/index.js +0 -0
  267. /package/dist/{mjs → esm}/data-structures/heap/index.js.map +0 -0
  268. /package/dist/{mjs → esm}/data-structures/heap/max-heap.d.ts +0 -0
  269. /package/dist/{mjs → esm}/data-structures/heap/max-heap.js +0 -0
  270. /package/dist/{mjs → esm}/data-structures/heap/max-heap.js.map +0 -0
  271. /package/dist/{mjs → esm}/data-structures/heap/min-heap.d.ts +0 -0
  272. /package/dist/{mjs → esm}/data-structures/heap/min-heap.js +0 -0
  273. /package/dist/{mjs → esm}/data-structures/heap/min-heap.js.map +0 -0
  274. /package/dist/{mjs → esm}/data-structures/index.d.ts +0 -0
  275. /package/dist/{mjs → esm}/data-structures/index.js +0 -0
  276. /package/dist/{mjs → esm}/data-structures/index.js.map +0 -0
  277. /package/dist/{mjs → esm}/data-structures/linked-list/doubly-linked-list.d.ts +0 -0
  278. /package/dist/{mjs → esm}/data-structures/linked-list/doubly-linked-list.js +0 -0
  279. /package/dist/{mjs → esm}/data-structures/linked-list/doubly-linked-list.js.map +0 -0
  280. /package/dist/{mjs → esm}/data-structures/linked-list/index.d.ts +0 -0
  281. /package/dist/{mjs → esm}/data-structures/linked-list/index.js +0 -0
  282. /package/dist/{mjs → esm}/data-structures/linked-list/index.js.map +0 -0
  283. /package/dist/{mjs → esm}/data-structures/matrix/index.d.ts +0 -0
  284. /package/dist/{mjs → esm}/data-structures/matrix/index.js +0 -0
  285. /package/dist/{mjs → esm}/data-structures/matrix/index.js.map +0 -0
  286. /package/dist/{mjs → esm}/data-structures/priority-queue/index.d.ts +0 -0
  287. /package/dist/{mjs → esm}/data-structures/priority-queue/index.js +0 -0
  288. /package/dist/{mjs → esm}/data-structures/priority-queue/index.js.map +0 -0
  289. /package/dist/{mjs → esm}/data-structures/priority-queue/priority-queue.d.ts +0 -0
  290. /package/dist/{mjs → esm}/data-structures/priority-queue/priority-queue.js +0 -0
  291. /package/dist/{mjs → esm}/data-structures/priority-queue/priority-queue.js.map +0 -0
  292. /package/dist/{mjs → esm}/data-structures/queue/deque.d.ts +0 -0
  293. /package/dist/{mjs → esm}/data-structures/queue/deque.js +0 -0
  294. /package/dist/{mjs → esm}/data-structures/queue/deque.js.map +0 -0
  295. /package/dist/{mjs → esm}/data-structures/queue/index.d.ts +0 -0
  296. /package/dist/{mjs → esm}/data-structures/queue/index.js +0 -0
  297. /package/dist/{mjs → esm}/data-structures/queue/index.js.map +0 -0
  298. /package/dist/{mjs → esm}/data-structures/queue/queue.d.ts +0 -0
  299. /package/dist/{mjs → esm}/data-structures/queue/queue.js +0 -0
  300. /package/dist/{mjs → esm}/data-structures/queue/queue.js.map +0 -0
  301. /package/dist/{mjs → esm}/data-structures/stack/index.d.ts +0 -0
  302. /package/dist/{mjs → esm}/data-structures/stack/index.js +0 -0
  303. /package/dist/{mjs → esm}/data-structures/stack/index.js.map +0 -0
  304. /package/dist/{mjs → esm}/data-structures/stack/stack.d.ts +0 -0
  305. /package/dist/{mjs → esm}/data-structures/stack/stack.js +0 -0
  306. /package/dist/{mjs → esm}/data-structures/stack/stack.js.map +0 -0
  307. /package/dist/{mjs → esm}/data-structures/tree/index.d.ts +0 -0
  308. /package/dist/{mjs → esm}/data-structures/tree/index.js +0 -0
  309. /package/dist/{mjs → esm}/data-structures/tree/index.js.map +0 -0
  310. /package/dist/{mjs → esm}/data-structures/tree/tree.d.ts +0 -0
  311. /package/dist/{mjs → esm}/data-structures/tree/tree.js +0 -0
  312. /package/dist/{mjs → esm}/data-structures/tree/tree.js.map +0 -0
  313. /package/dist/{mjs → esm}/data-structures/trie/index.d.ts +0 -0
  314. /package/dist/{mjs → esm}/data-structures/trie/index.js +0 -0
  315. /package/dist/{mjs → esm}/data-structures/trie/index.js.map +0 -0
  316. /package/dist/{mjs → esm}/index.d.ts +0 -0
  317. /package/dist/{mjs → esm}/index.js +0 -0
  318. /package/dist/{mjs → esm}/index.js.map +0 -0
  319. /package/dist/{mjs → esm}/interfaces/binary-tree.js +0 -0
  320. /package/dist/{mjs → esm}/interfaces/binary-tree.js.map +0 -0
  321. /package/dist/{mjs → esm}/interfaces/doubly-linked-list.d.ts +0 -0
  322. /package/dist/{mjs → esm}/interfaces/doubly-linked-list.js +0 -0
  323. /package/dist/{mjs → esm}/interfaces/doubly-linked-list.js.map +0 -0
  324. /package/dist/{mjs → esm}/interfaces/graph.d.ts +0 -0
  325. /package/dist/{mjs → esm}/interfaces/graph.js +0 -0
  326. /package/dist/{mjs → esm}/interfaces/graph.js.map +0 -0
  327. /package/dist/{mjs → esm}/interfaces/heap.d.ts +0 -0
  328. /package/dist/{mjs → esm}/interfaces/heap.js +0 -0
  329. /package/dist/{mjs → esm}/interfaces/heap.js.map +0 -0
  330. /package/dist/{mjs → esm}/interfaces/index.d.ts +0 -0
  331. /package/dist/{mjs → esm}/interfaces/index.js +0 -0
  332. /package/dist/{mjs → esm}/interfaces/index.js.map +0 -0
  333. /package/dist/{mjs → esm}/interfaces/navigator.d.ts +0 -0
  334. /package/dist/{mjs → esm}/interfaces/navigator.js +0 -0
  335. /package/dist/{mjs → esm}/interfaces/navigator.js.map +0 -0
  336. /package/dist/{mjs → esm}/interfaces/priority-queue.d.ts +0 -0
  337. /package/dist/{mjs → esm}/interfaces/priority-queue.js +0 -0
  338. /package/dist/{mjs → esm}/interfaces/priority-queue.js.map +0 -0
  339. /package/dist/{mjs → esm}/interfaces/segment-tree.d.ts +0 -0
  340. /package/dist/{mjs → esm}/interfaces/segment-tree.js +0 -0
  341. /package/dist/{mjs → esm}/interfaces/segment-tree.js.map +0 -0
  342. /package/dist/{mjs → esm}/interfaces/singly-linked-list.d.ts +0 -0
  343. /package/dist/{mjs → esm}/interfaces/singly-linked-list.js +0 -0
  344. /package/dist/{mjs → esm}/interfaces/singly-linked-list.js.map +0 -0
  345. /package/dist/{mjs → esm}/types/common.d.ts +0 -0
  346. /package/dist/{mjs → esm}/types/common.js +0 -0
  347. /package/dist/{mjs → esm}/types/common.js.map +0 -0
  348. /package/dist/{mjs → esm}/types/data-structures/base/base.d.ts +0 -0
  349. /package/dist/{mjs → esm}/types/data-structures/base/base.js +0 -0
  350. /package/dist/{mjs → esm}/types/data-structures/base/base.js.map +0 -0
  351. /package/dist/{mjs → esm}/types/data-structures/base/index.d.ts +0 -0
  352. /package/dist/{mjs → esm}/types/data-structures/base/index.js +0 -0
  353. /package/dist/{mjs → esm}/types/data-structures/base/index.js.map +0 -0
  354. /package/dist/{mjs → esm}/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -0
  355. /package/dist/{mjs → esm}/types/data-structures/binary-tree/avl-tree-multi-map.js.map +0 -0
  356. /package/dist/{mjs → esm}/types/data-structures/binary-tree/avl-tree.js +0 -0
  357. /package/dist/{mjs → esm}/types/data-structures/binary-tree/avl-tree.js.map +0 -0
  358. /package/dist/{mjs → esm}/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -0
  359. /package/dist/{mjs → esm}/types/data-structures/binary-tree/binary-indexed-tree.js +0 -0
  360. /package/dist/{mjs → esm}/types/data-structures/binary-tree/binary-indexed-tree.js.map +0 -0
  361. /package/dist/{mjs → esm}/types/data-structures/binary-tree/binary-tree.js +0 -0
  362. /package/dist/{mjs → esm}/types/data-structures/binary-tree/binary-tree.js.map +0 -0
  363. /package/dist/{mjs → esm}/types/data-structures/binary-tree/bst.js +0 -0
  364. /package/dist/{mjs → esm}/types/data-structures/binary-tree/bst.js.map +0 -0
  365. /package/dist/{mjs → esm}/types/data-structures/binary-tree/rb-tree.js +0 -0
  366. /package/dist/{mjs → esm}/types/data-structures/binary-tree/rb-tree.js.map +0 -0
  367. /package/dist/{mjs → esm}/types/data-structures/binary-tree/segment-tree.d.ts +0 -0
  368. /package/dist/{mjs → esm}/types/data-structures/binary-tree/segment-tree.js +0 -0
  369. /package/dist/{mjs → esm}/types/data-structures/binary-tree/segment-tree.js.map +0 -0
  370. /package/dist/{mjs → esm}/types/data-structures/binary-tree/tree-multi-map.js +0 -0
  371. /package/dist/{mjs → esm}/types/data-structures/binary-tree/tree-multi-map.js.map +0 -0
  372. /package/dist/{mjs → esm}/types/data-structures/graph/abstract-graph.d.ts +0 -0
  373. /package/dist/{mjs → esm}/types/data-structures/graph/abstract-graph.js +0 -0
  374. /package/dist/{mjs → esm}/types/data-structures/graph/abstract-graph.js.map +0 -0
  375. /package/dist/{mjs → esm}/types/data-structures/graph/directed-graph.d.ts +0 -0
  376. /package/dist/{mjs → esm}/types/data-structures/graph/directed-graph.js +0 -0
  377. /package/dist/{mjs → esm}/types/data-structures/graph/directed-graph.js.map +0 -0
  378. /package/dist/{mjs → esm}/types/data-structures/graph/index.d.ts +0 -0
  379. /package/dist/{mjs → esm}/types/data-structures/graph/index.js +0 -0
  380. /package/dist/{mjs → esm}/types/data-structures/graph/index.js.map +0 -0
  381. /package/dist/{mjs → esm}/types/data-structures/graph/map-graph.d.ts +0 -0
  382. /package/dist/{mjs → esm}/types/data-structures/graph/map-graph.js +0 -0
  383. /package/dist/{mjs → esm}/types/data-structures/graph/map-graph.js.map +0 -0
  384. /package/dist/{mjs → esm}/types/data-structures/graph/undirected-graph.d.ts +0 -0
  385. /package/dist/{mjs → esm}/types/data-structures/graph/undirected-graph.js +0 -0
  386. /package/dist/{mjs → esm}/types/data-structures/graph/undirected-graph.js.map +0 -0
  387. /package/dist/{mjs → esm}/types/data-structures/hash/hash-map.d.ts +0 -0
  388. /package/dist/{mjs → esm}/types/data-structures/hash/hash-map.js +0 -0
  389. /package/dist/{mjs → esm}/types/data-structures/hash/hash-map.js.map +0 -0
  390. /package/dist/{mjs → esm}/types/data-structures/hash/index.d.ts +0 -0
  391. /package/dist/{mjs → esm}/types/data-structures/hash/index.js +0 -0
  392. /package/dist/{mjs → esm}/types/data-structures/hash/index.js.map +0 -0
  393. /package/dist/{mjs → esm}/types/data-structures/heap/heap.d.ts +0 -0
  394. /package/dist/{mjs → esm}/types/data-structures/heap/heap.js +0 -0
  395. /package/dist/{mjs → esm}/types/data-structures/heap/heap.js.map +0 -0
  396. /package/dist/{mjs → esm}/types/data-structures/heap/index.d.ts +0 -0
  397. /package/dist/{mjs → esm}/types/data-structures/heap/index.js +0 -0
  398. /package/dist/{mjs → esm}/types/data-structures/heap/index.js.map +0 -0
  399. /package/dist/{mjs → esm}/types/data-structures/heap/max-heap.d.ts +0 -0
  400. /package/dist/{mjs → esm}/types/data-structures/heap/max-heap.js +0 -0
  401. /package/dist/{mjs → esm}/types/data-structures/heap/max-heap.js.map +0 -0
  402. /package/dist/{mjs → esm}/types/data-structures/heap/min-heap.d.ts +0 -0
  403. /package/dist/{mjs → esm}/types/data-structures/heap/min-heap.js +0 -0
  404. /package/dist/{mjs → esm}/types/data-structures/heap/min-heap.js.map +0 -0
  405. /package/dist/{mjs → esm}/types/data-structures/index.d.ts +0 -0
  406. /package/dist/{mjs → esm}/types/data-structures/index.js +0 -0
  407. /package/dist/{mjs → esm}/types/data-structures/index.js.map +0 -0
  408. /package/dist/{mjs → esm}/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -0
  409. /package/dist/{mjs → esm}/types/data-structures/linked-list/doubly-linked-list.js +0 -0
  410. /package/dist/{mjs → esm}/types/data-structures/linked-list/doubly-linked-list.js.map +0 -0
  411. /package/dist/{mjs → esm}/types/data-structures/linked-list/index.d.ts +0 -0
  412. /package/dist/{mjs → esm}/types/data-structures/linked-list/index.js +0 -0
  413. /package/dist/{mjs → esm}/types/data-structures/linked-list/index.js.map +0 -0
  414. /package/dist/{mjs → esm}/types/data-structures/linked-list/singly-linked-list.d.ts +0 -0
  415. /package/dist/{mjs → esm}/types/data-structures/linked-list/singly-linked-list.js +0 -0
  416. /package/dist/{mjs → esm}/types/data-structures/linked-list/singly-linked-list.js.map +0 -0
  417. /package/dist/{mjs → esm}/types/data-structures/linked-list/skip-linked-list.d.ts +0 -0
  418. /package/dist/{mjs → esm}/types/data-structures/linked-list/skip-linked-list.js +0 -0
  419. /package/dist/{mjs → esm}/types/data-structures/linked-list/skip-linked-list.js.map +0 -0
  420. /package/dist/{mjs → esm}/types/data-structures/matrix/index.d.ts +0 -0
  421. /package/dist/{mjs → esm}/types/data-structures/matrix/index.js +0 -0
  422. /package/dist/{mjs → esm}/types/data-structures/matrix/index.js.map +0 -0
  423. /package/dist/{mjs → esm}/types/data-structures/matrix/matrix.d.ts +0 -0
  424. /package/dist/{mjs → esm}/types/data-structures/matrix/matrix.js +0 -0
  425. /package/dist/{mjs → esm}/types/data-structures/matrix/matrix.js.map +0 -0
  426. /package/dist/{mjs → esm}/types/data-structures/matrix/navigator.d.ts +0 -0
  427. /package/dist/{mjs → esm}/types/data-structures/matrix/navigator.js +0 -0
  428. /package/dist/{mjs → esm}/types/data-structures/matrix/navigator.js.map +0 -0
  429. /package/dist/{mjs → esm}/types/data-structures/priority-queue/index.d.ts +0 -0
  430. /package/dist/{mjs → esm}/types/data-structures/priority-queue/index.js +0 -0
  431. /package/dist/{mjs → esm}/types/data-structures/priority-queue/index.js.map +0 -0
  432. /package/dist/{mjs → esm}/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -0
  433. /package/dist/{mjs → esm}/types/data-structures/priority-queue/max-priority-queue.js +0 -0
  434. /package/dist/{mjs → esm}/types/data-structures/priority-queue/max-priority-queue.js.map +0 -0
  435. /package/dist/{mjs → esm}/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -0
  436. /package/dist/{mjs → esm}/types/data-structures/priority-queue/min-priority-queue.js +0 -0
  437. /package/dist/{mjs → esm}/types/data-structures/priority-queue/min-priority-queue.js.map +0 -0
  438. /package/dist/{mjs → esm}/types/data-structures/priority-queue/priority-queue.d.ts +0 -0
  439. /package/dist/{mjs → esm}/types/data-structures/priority-queue/priority-queue.js +0 -0
  440. /package/dist/{mjs → esm}/types/data-structures/priority-queue/priority-queue.js.map +0 -0
  441. /package/dist/{mjs → esm}/types/data-structures/queue/deque.d.ts +0 -0
  442. /package/dist/{mjs → esm}/types/data-structures/queue/deque.js +0 -0
  443. /package/dist/{mjs → esm}/types/data-structures/queue/deque.js.map +0 -0
  444. /package/dist/{mjs → esm}/types/data-structures/queue/index.d.ts +0 -0
  445. /package/dist/{mjs → esm}/types/data-structures/queue/index.js +0 -0
  446. /package/dist/{mjs → esm}/types/data-structures/queue/index.js.map +0 -0
  447. /package/dist/{mjs → esm}/types/data-structures/queue/queue.d.ts +0 -0
  448. /package/dist/{mjs → esm}/types/data-structures/queue/queue.js +0 -0
  449. /package/dist/{mjs → esm}/types/data-structures/queue/queue.js.map +0 -0
  450. /package/dist/{mjs → esm}/types/data-structures/stack/index.d.ts +0 -0
  451. /package/dist/{mjs → esm}/types/data-structures/stack/index.js +0 -0
  452. /package/dist/{mjs → esm}/types/data-structures/stack/index.js.map +0 -0
  453. /package/dist/{mjs → esm}/types/data-structures/stack/stack.d.ts +0 -0
  454. /package/dist/{mjs → esm}/types/data-structures/stack/stack.js +0 -0
  455. /package/dist/{mjs → esm}/types/data-structures/stack/stack.js.map +0 -0
  456. /package/dist/{mjs → esm}/types/data-structures/tree/index.d.ts +0 -0
  457. /package/dist/{mjs → esm}/types/data-structures/tree/index.js +0 -0
  458. /package/dist/{mjs → esm}/types/data-structures/tree/index.js.map +0 -0
  459. /package/dist/{mjs → esm}/types/data-structures/tree/tree.d.ts +0 -0
  460. /package/dist/{mjs → esm}/types/data-structures/tree/tree.js +0 -0
  461. /package/dist/{mjs → esm}/types/data-structures/tree/tree.js.map +0 -0
  462. /package/dist/{mjs → esm}/types/data-structures/trie/index.d.ts +0 -0
  463. /package/dist/{mjs → esm}/types/data-structures/trie/index.js +0 -0
  464. /package/dist/{mjs → esm}/types/data-structures/trie/index.js.map +0 -0
  465. /package/dist/{mjs → esm}/types/data-structures/trie/trie.d.ts +0 -0
  466. /package/dist/{mjs → esm}/types/data-structures/trie/trie.js +0 -0
  467. /package/dist/{mjs → esm}/types/data-structures/trie/trie.js.map +0 -0
  468. /package/dist/{mjs → esm}/types/index.d.ts +0 -0
  469. /package/dist/{mjs → esm}/types/index.js +0 -0
  470. /package/dist/{mjs → esm}/types/index.js.map +0 -0
  471. /package/dist/{mjs → esm}/types/utils/index.d.ts +0 -0
  472. /package/dist/{mjs → esm}/types/utils/index.js +0 -0
  473. /package/dist/{mjs → esm}/types/utils/index.js.map +0 -0
  474. /package/dist/{mjs → esm}/types/utils/utils.d.ts +0 -0
  475. /package/dist/{mjs → esm}/types/utils/utils.js +0 -0
  476. /package/dist/{mjs → esm}/types/utils/utils.js.map +0 -0
  477. /package/dist/{mjs → esm}/types/utils/validate-type.d.ts +0 -0
  478. /package/dist/{mjs → esm}/types/utils/validate-type.js +0 -0
  479. /package/dist/{mjs → esm}/types/utils/validate-type.js.map +0 -0
  480. /package/dist/{mjs → esm}/utils/index.d.ts +0 -0
  481. /package/dist/{mjs → esm}/utils/index.js +0 -0
  482. /package/dist/{mjs → esm}/utils/index.js.map +0 -0
  483. /package/dist/{mjs → esm}/utils/number.d.ts +0 -0
  484. /package/dist/{mjs → esm}/utils/number.js +0 -0
  485. /package/dist/{mjs → esm}/utils/number.js.map +0 -0
  486. /package/dist/{mjs → esm}/utils/utils.d.ts +0 -0
  487. /package/dist/{mjs → esm}/utils/utils.js +0 -0
  488. /package/dist/{mjs → esm}/utils/utils.js.map +0 -0
@@ -1,711 +1,165 @@
1
- import { BinaryTreeNode, BSTNode, RedBlackTreeNode, TreeMultiMap, TreeMultiMapNode } from '../../../../src';
2
- import { isDebugTest } from '../../../config';
1
+ import { BinaryTreeNode, BSTNode, Range, TreeMultiMap, TreeMultiMapNode } from '../../../../src';
3
2
  import { getRandomInt } from '../../../utils';
4
3
 
4
+ import { isDebugTest } from '../../../config';
5
+ import { costOfLiving } from './data/cost-of-living-by-country';
6
+
5
7
  const isDebug = isDebugTest;
6
8
  // const isDebug = true;
7
9
 
8
- describe('TreeMultiMap count', () => {
10
+ describe('TreeMultiMap 1', () => {
9
11
  let tmm: TreeMultiMap<number>;
12
+
10
13
  beforeEach(() => {
11
14
  tmm = new TreeMultiMap<number>();
12
15
  });
13
16
 
14
- it('Should added node count ', () => {
15
- tmm.addMany([
16
- [1, 1],
17
- [2, 2],
18
- [3, 3],
19
- [4, 4],
20
- [5, 5]
21
- ]);
22
- const newNode = new TreeMultiMapNode(3, 33, 10);
23
- tmm.add(newNode);
24
- expect(tmm.count).toBe(15);
25
- expect(tmm.getComputedCount()).toBe(15);
26
- expect(tmm.getNode(3)?.count).toBe(11);
27
- });
28
-
29
- it('Should count', () => {
30
- tmm.addMany([
31
- [1, 1],
32
- [2, 2],
33
- [3, 3]
34
- ]);
35
- tmm.lesserOrGreaterTraverse(node => (node.count += 2), 1, 1);
36
- expect(tmm.getComputedCount()).toBe(7);
37
- expect(tmm.count).toBe(3);
38
- });
39
- });
40
-
41
- describe('TreeMultiMap operations test1', () => {
42
- it('should height ', () => {
43
- const tmm = new TreeMultiMap();
44
- expect(tmm.getHeight()).toBe(-1);
45
- expect(tmm.getMinHeight()).toBe(-1);
46
-
47
- tmm.addMany([1, 6, 7, 2, 3, 4, 9, 11, 8, 5, 10, 12, 16, 14, 13, 15]);
48
- // tmm.print()
49
- expect(tmm.getHeight()).toBe(5);
50
- expect(tmm.getMinHeight()).toBe(2);
51
- });
52
-
53
- it('should size and count', () => {
54
- const tmm = new TreeMultiMap();
55
-
56
- expect(tmm instanceof TreeMultiMap);
57
-
58
- tmm.add([11, 11]);
59
- tmm.add([3, 3]);
60
- expect(tmm.count).toBe(2);
61
- expect(tmm.getComputedCount()).toBe(2);
62
- expect(tmm.size).toBe(2);
63
-
64
- const keyValuePairs: [number, number][] = [
65
- [11, 11],
66
- [3, 3],
67
- [15, 15],
68
- [1, 1],
69
- [8, 8],
70
- [13, 13],
71
- [16, 16],
72
- [2, 2],
73
- [6, 6],
74
- [9, 9],
75
- [12, 12],
76
- [14, 14],
77
- [4, 4],
78
- [7, 7],
79
- [10, 10],
80
- [5, 5]
81
- ];
82
-
83
- tmm.addMany(keyValuePairs);
84
- expect(tmm.size).toBe(16);
85
- expect(tmm.count).toBe(18);
86
- expect(tmm.getComputedCount()).toBe(18);
87
- tmm.delete(11);
88
- expect(tmm.count).toBe(17);
89
- expect(tmm.getComputedCount()).toBe(17);
90
- tmm.delete(3, true);
91
- expect(tmm.count).toBe(15);
92
- expect(tmm.getComputedCount()).toBe(15);
93
- });
94
-
95
- it('should perform various operations on a Binary Search Tree with numeric values1', () => {
96
- const tmm = new TreeMultiMap<number, number>();
97
-
98
- expect(tmm instanceof TreeMultiMap);
99
-
100
- tmm.add([11, 11]);
101
- tmm.add([3, 3]);
102
- const idAndValues: [number, number][] = [
103
- [11, 11],
104
- [3, 3],
105
- [15, 15],
106
- [1, 1],
107
- [8, 8],
108
- [13, 13],
109
- [16, 16],
110
- [2, 2],
111
- [6, 6],
112
- [9, 9],
113
- [12, 12],
114
- [14, 14],
115
- [4, 4],
116
- [7, 7],
117
- [10, 10],
118
- [5, 5]
119
- ];
120
- tmm.addMany(idAndValues);
121
- expect(tmm.root instanceof TreeMultiMapNode);
122
-
123
- if (tmm.root) expect(tmm.root.key == 11);
124
-
125
- expect(tmm.size).toBe(16);
126
- expect(tmm.count).toBe(18);
127
- expect(tmm.getComputedCount()).toBe(18);
128
-
129
- expect(tmm.has(6));
130
- if (isDebug) tmm.print();
131
- expect(tmm.getHeight(6)).toBe(1);
132
- expect(tmm.getDepth(6)).toBe(3);
133
- const nodeId10 = tmm.getNode(10);
134
- expect(nodeId10?.key).toBe(10);
135
-
136
- const nodeVal9 = tmm.getNode(node => node.key === 9);
137
- expect(nodeVal9?.key).toBe(9);
138
-
139
- const nodesByCount1 = tmm.getNodes(node => node.count === 1);
140
- expect(nodesByCount1.length).toBe(14);
141
-
142
- const nodesByCount2 = tmm.getNodes(node => node.count === 2);
143
- expect(nodesByCount2.length).toBe(2);
144
- const leftMost = tmm.getLeftMost();
145
- expect(leftMost).toBe(1);
146
-
147
- const node15 = tmm.getNode(15);
148
- const minNodeBySpecificNode = node15 && tmm.getLeftMost(node => node, node15);
149
- expect(minNodeBySpecificNode?.key).toBe(14);
150
-
151
- let subTreeSum = 0;
152
- if (node15) tmm.dfs(node => (subTreeSum += node.key), 'PRE', 15);
153
- expect(subTreeSum).toBe(45);
154
- let lesserSum = 0;
155
- tmm.lesserOrGreaterTraverse(node => (lesserSum += node.key), -1, 10);
156
- expect(lesserSum).toBe(45);
157
-
158
- expect(node15 instanceof TreeMultiMapNode);
159
- if (node15 instanceof TreeMultiMapNode) {
160
- const subTreeAdd = tmm.dfs(node => (node.count += 1), 'PRE', 15);
161
- expect(subTreeAdd);
162
- }
163
- const node11 = tmm.getNode(11);
164
- expect(node11 instanceof TreeMultiMapNode);
165
- if (node11 instanceof TreeMultiMapNode) {
166
- const allGreaterNodesAdded = tmm.lesserOrGreaterTraverse(node => (node.count += 2), 1, 11);
167
- expect(allGreaterNodesAdded);
168
- }
169
-
170
- const dfsInorderNodes = tmm.dfs(node => node, 'IN');
171
- expect(dfsInorderNodes[0].key).toBe(1);
172
- expect(dfsInorderNodes[dfsInorderNodes.length - 1].key).toBe(16);
173
- expect(tmm.isPerfectlyBalanced()).toBe(false);
174
- tmm.perfectlyBalance();
175
- expect(tmm.isPerfectlyBalanced()).toBe(false);
176
-
177
- expect(tmm.isAVLBalanced()).toBe(false);
178
-
179
- const bfsNodesAfterBalanced = tmm.bfs(node => node);
180
- expect(bfsNodesAfterBalanced[0].key).toBe(6);
181
- expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16);
182
-
183
- const removed11 = tmm.delete(11, true);
184
- expect(removed11 instanceof Array);
185
- expect(removed11[0]);
186
- expect(removed11[0].deleted);
187
-
188
- if (removed11[0].deleted) expect(removed11[0].deleted.key).toBe(11);
189
-
190
- expect(tmm.isAVLBalanced()).toBe(false);
191
-
192
- expect(tmm.getHeight(15)).toBe(1);
193
-
194
- const removed1 = tmm.delete(1, true);
195
- expect(removed1 instanceof Array);
196
- expect(removed1[0]);
197
- expect(removed1[0].deleted);
198
- if (removed1[0].deleted) expect(removed1[0].deleted.key).toBe(1);
199
-
200
- expect(tmm.isAVLBalanced()).toBe(false);
201
-
202
- expect(tmm.getHeight()).toBe(5);
203
-
204
- const removed4 = tmm.delete(4, true);
205
- expect(removed4 instanceof Array);
206
- expect(removed4[0]);
207
- expect(removed4[0].deleted);
208
- if (removed4[0].deleted) expect(removed4[0].deleted.key).toBe(4);
209
-
210
- expect(tmm.isAVLBalanced()).toBe(false);
211
- expect(tmm.getHeight()).toBe(5);
212
-
213
- const removed10 = tmm.delete(10, true);
214
- expect(removed10 instanceof Array);
215
- expect(removed10[0]);
216
- expect(removed10[0].deleted);
217
- if (removed10[0].deleted) expect(removed10[0].deleted.key).toBe(10);
218
- expect(tmm.isAVLBalanced()).toBe(false);
219
-
220
- expect(tmm.getHeight()).toBe(4);
221
-
222
- const removed15 = tmm.delete(15, true);
223
- expect(removed15 instanceof Array);
224
- expect(removed15[0]);
225
- expect(removed15[0].deleted);
226
- if (removed15[0].deleted) expect(removed15[0].deleted.key).toBe(15);
227
-
228
- expect(tmm.isAVLBalanced()).toBe(false);
229
- expect(tmm.getHeight()).toBe(3);
230
-
231
- const removed5 = tmm.delete(5, true);
232
- expect(removed5 instanceof Array);
233
- expect(removed5[0]);
234
- expect(removed5[0].deleted);
235
- if (removed5[0].deleted) expect(removed5[0].deleted.key).toBe(5);
236
-
237
- expect(tmm.isAVLBalanced()).toBe(true);
238
- expect(tmm.getHeight()).toBe(3);
239
-
240
- const removed13 = tmm.delete(13, true);
241
- expect(removed13 instanceof Array);
242
- expect(removed13[0]);
243
- expect(removed13[0].deleted);
244
- if (removed13[0].deleted) expect(removed13[0].deleted.key).toBe(13);
245
- expect(tmm.isAVLBalanced()).toBe(true);
246
- expect(tmm.getHeight()).toBe(3);
247
-
248
- const removed3 = tmm.delete(3, true);
249
- expect(removed3 instanceof Array);
250
- expect(removed3[0]);
251
- expect(removed3[0].deleted);
252
- if (removed3[0].deleted) expect(removed3[0].deleted.key).toBe(3);
253
- expect(tmm.isAVLBalanced()).toBe(false);
254
- expect(tmm.getHeight()).toBe(3);
255
-
256
- const removed8 = tmm.delete(8, true);
257
- expect(removed8 instanceof Array);
258
- expect(removed8[0]);
259
- expect(removed8[0].deleted);
260
- if (removed8[0].deleted) expect(removed8[0].deleted.key).toBe(8);
261
- expect(tmm.isAVLBalanced()).toBe(false);
262
- expect(tmm.getHeight()).toBe(3);
263
-
264
- const removed6 = tmm.delete(6, true);
265
- expect(removed6 instanceof Array);
266
- expect(removed6[0]);
267
- expect(removed6[0].deleted);
268
- if (removed6[0].deleted) expect(removed6[0].deleted.key).toBe(6);
269
- expect(tmm.delete(6, true).length).toBe(0);
270
- expect(tmm.isAVLBalanced()).toBe(false);
271
-
272
- expect(tmm.getHeight()).toBe(3);
273
-
274
- const removed7 = tmm.delete(7, true);
275
- expect(removed7 instanceof Array);
276
- expect(removed7[0]);
277
- expect(removed7[0].deleted);
278
- if (removed7[0].deleted) expect(removed7[0].deleted.key).toBe(7);
279
- expect(tmm.isAVLBalanced()).toBe(false);
280
- expect(tmm.getHeight()).toBe(3);
281
-
282
- const removed9 = tmm.delete(9, true);
283
- expect(removed9 instanceof Array);
284
- expect(removed9[0]);
285
- expect(removed9[0].deleted);
286
- if (removed9[0].deleted) expect(removed9[0].deleted.key).toBe(9);
287
- expect(tmm.isAVLBalanced()).toBe(true);
288
- expect(tmm.getHeight()).toBe(2);
289
-
290
- const removed14 = tmm.delete(14, true);
291
- expect(removed14 instanceof Array);
292
- expect(removed14[0]);
293
- expect(removed14[0].deleted);
294
- if (removed14[0].deleted) expect(removed14[0].deleted.key).toBe(14);
295
- expect(tmm.isAVLBalanced()).toBe(true);
296
- expect(tmm.getHeight()).toBe(1);
297
-
298
- expect(tmm.isAVLBalanced()).toBe(true);
299
-
300
- const bfsIDs = tmm.bfs(node => node.key);
301
-
302
- expect(bfsIDs[0]).toBe(12);
303
- expect(bfsIDs[1]).toBe(2);
304
- expect(bfsIDs[2]).toBe(16);
305
-
306
- const bfsNodes = tmm.bfs(node => node);
307
-
308
- expect(bfsNodes[0].key).toBe(12);
309
- expect(bfsNodes[1].key).toBe(2);
310
- expect(bfsNodes[2].key).toBe(16);
311
-
312
- expect(tmm.count).toBe(6);
313
- expect(tmm.getComputedCount()).toBe(8);
314
- });
315
-
316
- it('should perform various operations on a Binary Search Tree with object values', () => {
317
- const objTreeMultiMap = new TreeMultiMap<number, { key: number; keyA: number }>();
318
- expect(objTreeMultiMap).toBeInstanceOf(TreeMultiMap);
319
- objTreeMultiMap.add([11, { key: 11, keyA: 11 }]);
320
- objTreeMultiMap.add([3, { key: 3, keyA: 3 }]);
321
- const values: [number, { key: number; keyA: number }][] = [
322
- [15, { key: 15, keyA: 15 }],
323
- [1, { key: 1, keyA: 1 }],
324
- [8, { key: 8, keyA: 8 }],
325
- [13, { key: 13, keyA: 13 }],
326
- [16, { key: 16, keyA: 16 }],
327
- [2, { key: 2, keyA: 2 }],
328
- [6, { key: 6, keyA: 6 }],
329
- [9, { key: 9, keyA: 9 }],
330
- [12, { key: 12, keyA: 12 }],
331
- [14, { key: 14, keyA: 14 }],
332
- [4, { key: 4, keyA: 4 }],
333
- [7, { key: 7, keyA: 7 }],
334
- [10, { key: 10, keyA: 10 }],
335
- [5, { key: 5, keyA: 5 }]
336
- ];
337
-
338
- objTreeMultiMap.addMany(values);
339
-
340
- expect(objTreeMultiMap.root).toBeInstanceOf(TreeMultiMapNode);
341
-
342
- if (objTreeMultiMap.root) expect(objTreeMultiMap.root.key).toBe(5);
343
-
344
- expect(objTreeMultiMap.count).toBe(16);
345
- expect(objTreeMultiMap.getComputedCount()).toBe(16);
346
-
347
- expect(objTreeMultiMap.has(6)).toBe(true);
348
- });
349
- });
350
-
351
- describe('TreeMultiMap operations test recursively1', () => {
352
- it('should perform various operations on a Binary Search Tree with numeric values1', () => {
353
- const tmm = new TreeMultiMap<number>([], {
354
- iterationType: 'RECURSIVE'
17
+ it('Should add and delete values', () => {
18
+ tmm.add(3, 3);
19
+ tmm.add(3, 33);
20
+ tmm.add(3, 333);
21
+ expect(tmm.get(3)).toEqual([3, 33, 333]);
22
+ tmm.deleteValue(3, 33);
23
+ expect(tmm.get(3)).toEqual([3, 333]);
24
+ tmm.deleteValue(3, 3);
25
+ expect(tmm.get(3)).toEqual([333]);
26
+ tmm.deleteValue(3, 333);
27
+ expect(tmm.get(3)).toBe(undefined);
28
+ tmm.add(3, 3);
29
+ tmm.add([3, [3333, 33333]]);
30
+ expect(tmm.get(3)).toEqual([3, 3333, 33333]);
31
+ });
32
+
33
+ describe('add and getNode', () => {
34
+ it('should add and find a node in the tmm', () => {
35
+ tmm.add(10);
36
+ tmm.add(20);
37
+ tmm.add(5);
38
+
39
+ expect(tmm.getNode(10)).toBeInstanceOf(TreeMultiMapNode);
40
+ expect(tmm.getNode(20)).toBeInstanceOf(TreeMultiMapNode);
41
+ expect(tmm.getNode(5)).toBeInstanceOf(TreeMultiMapNode);
42
+ expect(tmm.getNode(15)).toBe(undefined);
355
43
  });
356
44
 
357
- expect(tmm instanceof TreeMultiMap);
358
- tmm.add([11, 11]);
359
- tmm.add([3, 3]);
360
- const idAndValues: [number, number][] = [
361
- [11, 11],
362
- [3, 3],
363
- [15, 15],
364
- [1, 1],
365
- [8, 8],
366
- [13, 13],
367
- [16, 16],
368
- [2, 2],
369
- [6, 6],
370
- [9, 9],
371
- [12, 12],
372
- [14, 14],
373
- [4, 4],
374
- [7, 7],
375
- [10, 10],
376
- [5, 5]
377
- ];
378
- tmm.addMany(idAndValues);
379
- expect(tmm.root).toBeInstanceOf(TreeMultiMapNode);
380
-
381
- if (tmm.root) expect(tmm.root.key).toBe(5);
382
-
383
- expect(tmm.size).toBe(16);
384
- expect(tmm.count).toBe(18);
385
- expect(tmm.getComputedCount()).toBe(18);
386
-
387
- expect(tmm.has(6));
388
-
389
- expect(tmm.getHeight(6)).toBe(1);
390
- expect(tmm.getDepth(6)).toBe(3);
391
- const nodeId10 = tmm.getNode(10);
392
- expect(nodeId10?.key).toBe(10);
393
-
394
- const nodeVal9 = tmm.getNode(node => node.key === 9);
395
- expect(nodeVal9?.key).toBe(9);
396
-
397
- const nodesByCount1 = tmm.getNodes(node => node.count === 1);
398
- expect(nodesByCount1.length).toBe(14);
399
-
400
- const nodesByCount2 = tmm.getNodes(node => node.count === 2);
401
- expect(nodesByCount2.length).toBe(2);
402
- const leftMost = tmm.getLeftMost();
403
- expect(leftMost).toBe(1);
404
-
405
- const node15 = tmm.getNode(15);
406
- const minNodeBySpecificNode = node15 && tmm.getLeftMost(node => node, node15);
407
- expect(minNodeBySpecificNode?.key).toBe(14);
408
-
409
- let subTreeSum = 0;
410
- if (node15) tmm.dfs(node => (subTreeSum += node.key), 'PRE', 15);
411
- expect(subTreeSum).toBe(45);
412
- let lesserSum = 0;
413
- expect(tmm.has(9)).toBe(true);
414
- tmm.lesserOrGreaterTraverse(
415
- node => {
416
- lesserSum += node.key;
417
- return node.key;
418
- },
419
- -1,
420
- 10
421
- );
422
- expect(lesserSum).toBe(45);
423
-
424
- expect(node15 instanceof TreeMultiMapNode);
425
- if (node15 instanceof TreeMultiMapNode) {
426
- const subTreeAdd = tmm.dfs(node => (node.count += 1), 'PRE', 15);
427
- expect(subTreeAdd);
428
- }
429
- const node11 = tmm.getNode(11);
430
- expect(node11 instanceof TreeMultiMapNode);
431
- if (node11 instanceof TreeMultiMapNode) {
432
- const allGreaterNodesAdded = tmm.lesserOrGreaterTraverse(node => (node.count += 2), 1, 11);
433
- expect(allGreaterNodesAdded);
434
- }
435
-
436
- const dfsInorderNodes = tmm.dfs(node => node, 'IN');
437
- expect(dfsInorderNodes[0].key).toBe(1);
438
- expect(dfsInorderNodes[dfsInorderNodes.length - 1].key).toBe(16);
439
- expect(tmm.isPerfectlyBalanced()).toBe(false);
440
-
441
- tmm.perfectlyBalance();
442
-
443
- expect(tmm.isPerfectlyBalanced()).toBe(false);
444
- expect(tmm.isAVLBalanced()).toBe(false);
445
-
446
- const bfsNodesAfterBalanced = tmm.bfs(node => node);
447
- expect(bfsNodesAfterBalanced[0].key).toBe(6);
448
- expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16);
449
-
450
- const removed11 = tmm.delete(11, true);
451
- expect(removed11 instanceof Array);
452
- expect(removed11[0]);
453
- expect(removed11[0].deleted);
454
-
455
- if (removed11[0].deleted) expect(removed11[0].deleted.key).toBe(11);
456
-
457
- expect(tmm.isAVLBalanced()).toBe(false);
458
-
459
- expect(tmm.getHeight(15)).toBe(1);
460
-
461
- const removed1 = tmm.delete(1, true);
462
- expect(removed1 instanceof Array);
463
- expect(removed1[0]);
464
- expect(removed1[0].deleted);
465
- if (removed1[0].deleted) expect(removed1[0].deleted.key).toBe(1);
466
-
467
- expect(tmm.isAVLBalanced()).toBe(false);
468
-
469
- expect(tmm.getHeight()).toBe(5);
470
-
471
- const removed4 = tmm.delete(4, true);
472
- expect(removed4 instanceof Array);
473
- expect(removed4[0]);
474
- expect(removed4[0].deleted);
475
- if (removed4[0].deleted) expect(removed4[0].deleted.key).toBe(4);
476
-
477
- expect(tmm.isAVLBalanced()).toBe(false);
478
- expect(tmm.getHeight()).toBe(5);
479
-
480
- const removed10 = tmm.delete(10, true);
481
- expect(removed10 instanceof Array);
482
- expect(removed10[0]);
483
- expect(removed10[0].deleted);
484
- if (removed10[0].deleted) expect(removed10[0].deleted.key).toBe(10);
485
- expect(tmm.isAVLBalanced()).toBe(false);
486
-
487
- expect(tmm.getHeight()).toBe(4);
488
-
489
- const removed15 = tmm.delete(15, true);
490
- expect(removed15 instanceof Array);
491
- expect(removed15[0]);
492
- expect(removed15[0].deleted);
493
- if (removed15[0].deleted) expect(removed15[0].deleted.key).toBe(15);
494
-
495
- expect(tmm.isAVLBalanced()).toBe(false);
496
- expect(tmm.getHeight()).toBe(3);
497
-
498
- const removed5 = tmm.delete(5, true);
499
- expect(removed5 instanceof Array);
500
- expect(removed5[0]);
501
- expect(removed5[0].deleted);
502
- if (removed5[0].deleted) expect(removed5[0].deleted.key).toBe(5);
503
-
504
- expect(tmm.isAVLBalanced()).toBe(true);
505
- expect(tmm.getHeight()).toBe(3);
506
-
507
- const removed13 = tmm.delete(13, true);
508
- expect(removed13 instanceof Array);
509
- expect(removed13[0]);
510
- expect(removed13[0].deleted);
511
- if (removed13[0].deleted) expect(removed13[0].deleted.key).toBe(13);
512
- expect(tmm.isAVLBalanced()).toBe(true);
513
- expect(tmm.getHeight()).toBe(3);
514
-
515
- const removed3 = tmm.delete(3, true);
516
- expect(removed3 instanceof Array);
517
- expect(removed3[0]);
518
- expect(removed3[0].deleted);
519
- if (removed3[0].deleted) expect(removed3[0].deleted.key).toBe(3);
520
- expect(tmm.isAVLBalanced()).toBe(false);
521
- expect(tmm.getHeight()).toBe(3);
522
-
523
- const removed8 = tmm.delete(8, true);
524
- expect(removed8 instanceof Array);
525
- expect(removed8[0]);
526
- expect(removed8[0].deleted);
527
- if (removed8[0].deleted) expect(removed8[0].deleted.key).toBe(8);
528
- expect(tmm.isAVLBalanced()).toBe(false);
529
- expect(tmm.getHeight()).toBe(3);
530
-
531
- const removed6 = tmm.delete(6, true);
532
- expect(removed6 instanceof Array);
533
- expect(removed6[0]);
534
- expect(removed6[0].deleted);
535
- if (removed6[0].deleted) expect(removed6[0].deleted.key).toBe(6);
536
- expect(tmm.delete(6, true).length).toBe(0);
537
- expect(tmm.isAVLBalanced()).toBe(false);
538
-
539
- expect(tmm.getHeight()).toBe(3);
540
-
541
- const removed7 = tmm.delete(7, true);
542
- expect(removed7 instanceof Array);
543
- expect(removed7[0]);
544
- expect(removed7[0].deleted);
545
- if (removed7[0].deleted) expect(removed7[0].deleted.key).toBe(7);
546
- expect(tmm.isAVLBalanced()).toBe(false);
547
- expect(tmm.getHeight()).toBe(3);
548
-
549
- const removed9 = tmm.delete(9, true);
550
- expect(removed9 instanceof Array);
551
- expect(removed9[0]);
552
- expect(removed9[0].deleted);
553
- if (removed9[0].deleted) expect(removed9[0].deleted.key).toBe(9);
554
- expect(tmm.isAVLBalanced()).toBe(true);
555
- expect(tmm.getHeight()).toBe(2);
556
-
557
- const removed14 = tmm.delete(14, true);
558
- expect(removed14 instanceof Array);
559
- expect(removed14[0]);
560
- expect(removed14[0].deleted);
561
- if (removed14[0].deleted) expect(removed14[0].deleted.key).toBe(14);
562
- expect(tmm.isAVLBalanced()).toBe(true);
563
- expect(tmm.getHeight()).toBe(1);
564
-
565
- expect(tmm.isAVLBalanced()).toBe(true);
566
-
567
- const bfsIDs = tmm.bfs(node => node.key);
568
-
569
- expect(bfsIDs[0]).toBe(12);
570
- expect(bfsIDs[1]).toBe(2);
571
- expect(bfsIDs[2]).toBe(16);
572
-
573
- const bfsNodes = tmm.bfs(node => node);
574
-
575
- expect(bfsNodes[0].key).toBe(12);
576
- expect(bfsNodes[1].key).toBe(2);
577
- expect(bfsNodes[2].key).toBe(16);
45
+ it('should add and find nodes with negative keys', () => {
46
+ tmm.add(-10);
47
+ tmm.add(-20);
578
48
 
579
- expect(tmm.count).toBe(6);
580
- expect(tmm.getComputedCount()).toBe(8);
49
+ expect(tmm.getNode(-10)).toBeInstanceOf(TreeMultiMapNode);
50
+ expect(tmm.getNode(-20)).toBeInstanceOf(TreeMultiMapNode);
51
+ });
581
52
  });
582
53
 
583
- it('should perform various operations on a Binary Search Tree with object values', () => {
584
- const objTreeMultiMap = new TreeMultiMap<number, { key: number; keyA: number }>();
585
- expect(objTreeMultiMap).toBeInstanceOf(TreeMultiMap);
586
- objTreeMultiMap.add([11, { key: 11, keyA: 11 }]);
587
- objTreeMultiMap.add([3, { key: 3, keyA: 3 }]);
588
- const values: [number, { key: number; keyA: number }][] = [
589
- [15, { key: 15, keyA: 15 }],
590
- [1, { key: 1, keyA: 1 }],
591
- [8, { key: 8, keyA: 8 }],
592
- [13, { key: 13, keyA: 13 }],
593
- [16, { key: 16, keyA: 16 }],
594
- [2, { key: 2, keyA: 2 }],
595
- [6, { key: 6, keyA: 6 }],
596
- [9, { key: 9, keyA: 9 }],
597
- [12, { key: 12, keyA: 12 }],
598
- [14, { key: 14, keyA: 14 }],
599
- [4, { key: 4, keyA: 4 }],
600
- [7, { key: 7, keyA: 7 }],
601
- [10, { key: 10, keyA: 10 }],
602
- [5, { key: 5, keyA: 5 }]
603
- ];
604
-
605
- objTreeMultiMap.addMany(values);
54
+ describe('deleteNode', () => {
55
+ it('should delete a node from the tmm', () => {
56
+ tmm.add(10);
57
+ tmm.add(20);
58
+ tmm.add(5);
59
+ tmm.delete(20);
606
60
 
607
- expect(objTreeMultiMap.root).toBeInstanceOf(TreeMultiMapNode);
61
+ expect(tmm.getNode(20)).toBe(undefined);
62
+ });
608
63
 
609
- if (objTreeMultiMap.root) expect(objTreeMultiMap.root.key).toBe(5);
64
+ it('should handle deleting a non-existent node', () => {
65
+ tmm.add(10);
66
+ tmm.add(20);
67
+ tmm.add(5);
68
+ tmm.delete(15);
610
69
 
611
- expect(objTreeMultiMap.count).toBe(16);
612
- expect(objTreeMultiMap.getComputedCount()).toBe(16);
70
+ expect(tmm.getNode(15)).toBe(undefined);
71
+ });
613
72
 
614
- expect(objTreeMultiMap.has(6)).toBe(true);
73
+ it('should getNode performance O(log n)', () => {
74
+ for (let i = 0; i < 10; i++) tmm.add(i);
75
+ tmm.getNode(6);
76
+ });
615
77
  });
616
- });
617
78
 
618
- describe('TreeMultiMap delete test', function () {
619
- const tmm = new TreeMultiMap<number, number>();
620
- const inputSize = 1000; // Adjust input sizes as needed
79
+ describe('minimum', () => {
80
+ it('should find the minimum node in the tmm', () => {
81
+ tmm.add(10);
82
+ tmm.add(20);
83
+ tmm.add(5);
84
+ tmm.add(15);
85
+ tmm.add(3);
621
86
 
622
- beforeEach(() => {
623
- tmm.clear();
624
- });
87
+ const minNode = tmm.getLeftMost(node => node, tmm.root);
88
+ expect(minNode?.key).toBe(3);
89
+ });
625
90
 
626
- it(`Observe the time consumption of TreeMultiMap.dfs be good`, function () {
627
- const startDFS = performance.now();
628
- const dfs = tmm.dfs(node => node);
629
- if (isDebug) console.log('---bfs', performance.now() - startDFS, dfs.length);
91
+ it('should handle an empty tmm', () => {
92
+ const minNode = tmm.getLeftMost(node => node, tmm.root);
93
+ expect(minNode).toBe(undefined);
94
+ });
630
95
  });
631
96
 
632
- it('The structure remains normal after random deletion', function () {
633
- for (let i = 0; i < inputSize; i++) {
634
- tmm.add(i);
635
- }
636
-
637
- expect(tmm.size).toBe(inputSize);
638
-
639
- for (let i = 0; i < inputSize; i++) {
640
- const num = getRandomInt(0, inputSize - 1);
641
- tmm.delete(num);
642
- }
643
-
644
- let nilCount = 0;
645
- const dfs = (cur: TreeMultiMapNode<number>) => {
646
- if (isNaN(cur.key)) nilCount++;
647
- if (cur.left) dfs(cur.left);
648
- if (cur.right) dfs(cur.right);
649
- };
650
- if (tmm.root) dfs(tmm.root);
97
+ describe('getRightMost', () => {
98
+ it('should find the getRightMost node in the tmm', () => {
99
+ tmm.add(10);
100
+ tmm.add(20);
101
+ tmm.add(5);
102
+ tmm.add(15);
103
+ tmm.add(25);
651
104
 
652
- expect(tmm.size).toBeLessThanOrEqual(inputSize);
653
- expect(tmm.getHeight()).toBeGreaterThan(Math.log2(inputSize) - 1);
654
- expect(tmm.getHeight()).toBeLessThan(Math.log2(inputSize) * 2);
105
+ const maxNode = tmm.getRightMost(node => node, tmm.root);
106
+ expect(maxNode?.key).toBe(25);
107
+ });
655
108
 
656
- expect(nilCount).toBe(tmm.size + 1);
109
+ it('should handle an empty tmm', () => {
110
+ const maxNode = tmm.getRightMost(node => node, tmm.root);
111
+ expect(maxNode).toBe(undefined);
112
+ });
657
113
  });
658
114
 
659
- it(`Random additions, complete deletions of structures are normal`, function () {
660
- for (let i = 0; i < inputSize; i++) {
661
- const num = getRandomInt(0, inputSize - 1);
662
- if (i === 0 && isDebug) console.log(`first:`, num);
663
- tmm.add(num);
664
- }
115
+ describe('getSuccessor', () => {
116
+ it('should find the getSuccessor of a node', () => {
117
+ tmm.add(10);
118
+ tmm.add(20);
119
+ tmm.add(5);
120
+ tmm.add(15);
121
+ tmm.add(25);
665
122
 
666
- for (let i = 0; i < inputSize; i++) {
667
- tmm.delete(i, true);
668
- }
123
+ const node = tmm.getNode(15);
124
+ const successorNode = tmm.getSuccessor(node!);
669
125
 
670
- let nilCount = 0;
671
- const dfs = (cur: TreeMultiMapNode<number>) => {
672
- if (isNaN(cur.key)) nilCount++;
673
- if (cur.left) dfs(cur.left);
674
- if (cur.right) dfs(cur.right);
675
- };
676
- if (tmm.root) dfs(tmm.root);
126
+ expect(successorNode?.key).toBe(20);
127
+ });
677
128
 
678
- expect(tmm.size).toBe(0);
679
- expect(tmm.getHeight()).toBe(-1);
680
- expect(nilCount).toBe(tmm.size + 1);
129
+ it('should handle a node with no getSuccessor', () => {
130
+ tmm.add(10);
131
+ tmm.add(5);
681
132
 
682
- if (isDebug) tmm.print();
133
+ const node = tmm.getNode(10);
134
+ const successorNode = tmm.getSuccessor(node!);
135
+ // TODO not sure if it should be undefined or tmm.NIL
136
+ expect(successorNode).toBe(undefined);
137
+ });
683
138
  });
684
139
 
685
- it(`Random additions, count deletions of structures are normal`, function () {
686
- for (let i = 0; i < inputSize; i++) {
687
- const num = getRandomInt(0, inputSize - 1);
688
- if (i === 0 && isDebug) console.log(`first:`, num);
689
- tmm.add(num);
690
- }
140
+ describe('getPredecessor', () => {
141
+ it('should find the getPredecessor of a node', () => {
142
+ tmm.add(10);
143
+ tmm.add(20);
144
+ tmm.add(5);
145
+ tmm.add(15);
146
+ tmm.add(25);
691
147
 
692
- for (let i = 0; i < inputSize; i++) {
693
- tmm.delete(i);
694
- }
148
+ const node = tmm.getNode(20);
149
+ const predecessorNode = tmm.getPredecessor(node!);
695
150
 
696
- let nanCount = 0;
697
- const dfs = (cur: TreeMultiMapNode<number>) => {
698
- if (isNaN(cur.key)) nanCount++;
699
- if (cur.left) dfs(cur.left);
700
- if (cur.right) dfs(cur.right);
701
- };
702
- if (tmm.root) dfs(tmm.root);
151
+ expect(predecessorNode?.key).toBe(15);
152
+ });
703
153
 
704
- expect(tmm.size).toBeGreaterThanOrEqual(0);
705
- expect(tmm.getHeight()).toBeGreaterThanOrEqual(0);
706
- expect(nanCount).toBeLessThanOrEqual(inputSize);
154
+ it('should handle a node with no getPredecessor', () => {
155
+ tmm.add(10);
156
+ tmm.add(20);
707
157
 
708
- if (isDebug) tmm.print();
158
+ const node = tmm.getNode(20);
159
+ const predecessorNode = tmm.getPredecessor(node!);
160
+ // TODO not sure if it should be tmm.NIL or something else.
161
+ expect(predecessorNode).toBe(tmm.getNode(20));
162
+ });
709
163
  });
710
164
 
711
165
  it('should the clone method', () => {
@@ -734,7 +188,7 @@ describe('TreeMultiMap delete test', function () {
734
188
  expect(tmm.root?.left?.left?.key).toBe(NaN);
735
189
  expect(tmm.root?.left?.right?.key).toBe(NaN);
736
190
  expect(tmm.root?.right?.key).toBe('4');
737
- expect(tmm.root?.right?.left?.key).toBe(`3`);
191
+ expect(tmm.root?.right?.left?.key).toBe('3');
738
192
  expect(tmm.root?.right?.right?.key).toBe('5');
739
193
  tmm.delete('3');
740
194
  checkTreeStructure(tmm);
@@ -744,232 +198,650 @@ describe('TreeMultiMap delete test', function () {
744
198
  expect(tmm.size).toBe(4);
745
199
  expect(cloned.size).toBe(3);
746
200
  });
201
+
202
+ it('should add value', () => {
203
+ const tmm = new TreeMultiMap<number, string>([4, 5, [1, ['1']], 2, 3]);
204
+ expect(tmm.get(1)).toEqual(['1']);
205
+ expect(tmm.getNode(1)?.value).toEqual([]);
206
+ tmm.add(1, 'a');
207
+ expect(tmm.get(1)).toEqual(['1', 'a']);
208
+ tmm.add([1, ['b']]);
209
+ expect(tmm.getNode(1)?.value).toEqual([]);
210
+ expect(tmm.get(1)).toEqual(['1', 'a', 'b']);
211
+ const tmmMapped = new TreeMultiMap<number>([4, 5, [1, ['1']], 2, 3]);
212
+ expect(tmmMapped.get(1)).toEqual(['1']);
213
+ expect(tmmMapped.getNode(1)?.value).toEqual([]);
214
+ tmmMapped.add(1, 'a');
215
+ expect(tmmMapped.get(1)).toEqual(['1', 'a']);
216
+ tmmMapped.add([1, ['b']]);
217
+ expect(tmmMapped.getNode(1)?.value).toEqual([]);
218
+ expect(tmmMapped.get(1)).toEqual(['1', 'a', 'b']);
219
+ });
747
220
  });
748
221
 
749
- describe('TreeMultiMap iterative methods test', () => {
750
- let treeMM: TreeMultiMap<number, string>;
222
+ describe('TreeMultiMap 2', () => {
223
+ let tmm: TreeMultiMap<number>;
224
+
751
225
  beforeEach(() => {
752
- treeMM = new TreeMultiMap<number, string>();
753
- treeMM.add(1, 'a', 10);
754
- treeMM.add([2, 'b'], undefined, 10);
755
- treeMM.add([3, 'c'], undefined, 1);
226
+ tmm = new TreeMultiMap<number>();
756
227
  });
757
228
 
758
- it('The node obtained by get Node should match the node type', () => {
759
- const node3 = treeMM.getNode(3);
760
- expect(node3).toBeInstanceOf(BinaryTreeNode);
761
- expect(node3).toBeInstanceOf(BSTNode);
762
- expect(node3).toBeInstanceOf(RedBlackTreeNode);
763
- });
229
+ it('should add nodes into the tmm', () => {
230
+ tmm.add(10);
231
+ expect(tmm.getNode(10)).toBeDefined();
232
+ tmm.add(20);
233
+ expect(tmm.getNode(20)).toBeDefined();
234
+ tmm.add(5);
235
+ expect(tmm.getNode(5)).toBeDefined();
236
+ });
237
+
238
+ it('should delete nodes from the tmm', () => {
239
+ tmm.add(10);
240
+ tmm.add(20);
241
+ tmm.add(5);
242
+ tmm.delete(20);
243
+ expect(tmm.getNode(20)).toBe(undefined);
244
+ });
245
+
246
+ it('should get the successor of a node', () => {
247
+ tmm.add(10);
248
+ tmm.add(20);
249
+ const node = tmm.getNode(10);
250
+ const successor = tmm.getSuccessor(node!);
251
+ expect(successor?.key).toBe(20);
252
+ });
253
+
254
+ it('should get the predecessor of a node', () => {
255
+ tmm.add(10);
256
+ tmm.add(20);
257
+ const node = tmm.getNode(20);
258
+ const predecessor = tmm.getPredecessor(node!);
259
+ expect(predecessor?.key).toBe(20);
260
+ });
261
+
262
+ it('should rotate nodes to the left', () => {
263
+ tmm.add(10);
264
+ tmm.add(20);
265
+ tmm.add(5);
266
+ const node = tmm.getNode(10);
267
+ tmm.add(15);
268
+ // Verify that rotation has occurred
269
+ expect(node?.left?.key).toBe(5);
270
+ expect(node?.right?.key).toBe(20);
271
+ });
272
+
273
+ it('should rotate nodes to the right', () => {
274
+ tmm.add(10);
275
+ tmm.add(20);
276
+ tmm.add(5);
277
+ const node = tmm.getNode(20);
278
+ tmm.add(25);
279
+ // Verify that rotation has occurred
280
+ expect(node?.left?.key).toBeNaN();
281
+ expect(node?.right?.key).toBe(25);
282
+ });
283
+
284
+ it('should all node attributes fully conform to the red-black tmm standards.', () => {
285
+ tmm.add(10);
286
+ tmm.add(20);
287
+ tmm.add(5);
288
+ tmm.add(15);
289
+ tmm.add(21);
290
+ tmm.add(6);
291
+ tmm.add(2);
292
+
293
+ let node10F = tmm.getNode(10);
294
+ let node20F = tmm.getNode(20);
295
+ let node5F = tmm.getNode(5);
296
+ let node15F = tmm.getNode(15);
297
+ let node21F = tmm.getNode(21);
298
+ let node6F = tmm.getNode(6);
299
+ let node2F = tmm.getNode(2);
300
+ expect(node10F?.key).toBe(10);
301
+ expect(node10F?.color).toBe('BLACK');
302
+ expect(node10F?.left).toBe(node5F);
303
+ expect(node10F?.right).toBe(node20F);
304
+ expect(node10F?.parent).toBe(undefined);
305
+ expect(node20F?.key).toBe(20);
306
+ expect(node20F?.color).toBe('BLACK');
307
+ expect(node20F?.left).toBe(node15F);
308
+ expect(node20F?.right).toBe(node21F);
309
+ expect(node20F?.parent).toBe(node10F);
310
+ expect(node5F?.key).toBe(5);
311
+ expect(node5F?.color).toBe('BLACK');
312
+ expect(node5F?.left).toBe(node2F);
313
+ expect(node5F?.right).toBe(node6F);
314
+ expect(node5F?.parent).toBe(node10F);
315
+ expect(node15F?.key).toBe(15);
316
+ expect(node15F?.color).toBe('RED');
317
+ expect(node15F?.left).toBe(tmm.NIL);
318
+ expect(node15F?.right).toBe(tmm.NIL);
319
+ expect(node15F?.parent).toBe(node20F);
320
+ expect(node21F?.key).toBe(21);
321
+ expect(node21F?.color).toBe('RED');
322
+ expect(node21F?.left).toBe(tmm.NIL);
323
+ expect(node21F?.right).toBe(tmm.NIL);
324
+ expect(node21F?.parent).toBe(node20F);
325
+ expect(node6F?.key).toBe(6);
326
+ expect(node6F?.color).toBe('RED');
327
+ expect(node6F?.left).toBe(tmm.NIL);
328
+ expect(node6F?.right).toBe(tmm.NIL);
329
+ expect(node6F?.parent).toBe(node5F);
330
+ expect(node2F?.key).toBe(2);
331
+ expect(node2F?.color).toBe('RED');
332
+ expect(node2F?.left).toBe(tmm.NIL);
333
+ expect(node2F?.right).toBe(tmm.NIL);
334
+ expect(node2F?.parent).toBe(node5F);
335
+ expect(node15F?.key).toBe(15);
336
+ expect(node15F?.color).toBe('RED');
337
+ expect(node15F?.left).toBe(tmm.NIL);
338
+ expect(node15F?.right).toBe(tmm.NIL);
339
+ expect(node15F?.parent).toBe(node20F);
340
+ tmm.delete(5);
341
+ node10F = tmm.getNode(10);
342
+ node20F = tmm.getNode(20);
343
+ node5F = tmm.getNode(5);
344
+ node15F = tmm.getNode(15);
345
+ node21F = tmm.getNode(21);
346
+ node6F = tmm.getNode(6);
347
+ node2F = tmm.getNode(2);
348
+ expect(node10F?.key).toBe(10);
349
+ expect(node10F?.color).toBe('BLACK');
350
+ expect(node10F?.left).toBe(node6F);
351
+ expect(node10F?.right).toBe(node20F);
352
+ expect(node10F?.parent).toBe(undefined);
353
+ expect(node20F?.key).toBe(20);
354
+ expect(node20F?.color).toBe('BLACK');
355
+ expect(node20F?.left).toBe(node15F);
356
+ expect(node20F?.right).toBe(node21F);
357
+ expect(node20F?.parent).toBe(node10F);
358
+ expect(node5F).toBe(undefined);
359
+ expect(node15F?.key).toBe(15);
360
+ expect(node15F?.color).toBe('RED');
361
+ expect(node15F?.left).toBe(tmm.NIL);
362
+ expect(node15F?.right).toBe(tmm.NIL);
363
+ expect(node15F?.parent).toBe(node20F);
364
+ expect(node21F?.key).toBe(21);
365
+ expect(node21F?.color).toBe('RED');
366
+ expect(node21F?.left).toBe(tmm.NIL);
367
+ expect(node21F?.right).toBe(tmm.NIL);
368
+ expect(node21F?.parent).toBe(node20F);
369
+ expect(node6F?.key).toBe(6);
370
+ expect(node6F?.color).toBe('BLACK');
371
+ expect(node6F?.left).toBe(node2F);
372
+ expect(node6F?.right).toBe(tmm.NIL);
373
+ expect(node6F?.parent).toBe(node10F);
374
+ expect(node2F?.key).toBe(2);
375
+ expect(node2F?.color).toBe('RED');
376
+ expect(node2F?.left).toBe(tmm.NIL);
377
+ expect(node2F?.right).toBe(tmm.NIL);
378
+ expect(node2F?.parent).toBe(node6F);
379
+ expect(node15F?.key).toBe(15);
380
+ expect(node15F?.color).toBe('RED');
381
+ expect(node15F?.left).toBe(tmm.NIL);
382
+ expect(node15F?.right).toBe(tmm.NIL);
383
+ expect(node15F?.parent).toBe(node20F);
384
+ tmm.delete(20);
385
+ node10F = tmm.getNode(10);
386
+ node20F = tmm.getNode(20);
387
+ node5F = tmm.getNode(5);
388
+ node15F = tmm.getNode(15);
389
+ node21F = tmm.getNode(21);
390
+ node6F = tmm.getNode(6);
391
+ node2F = tmm.getNode(2);
392
+ expect(node10F?.key).toBe(10);
393
+ expect(node10F?.color).toBe('BLACK');
394
+ expect(node10F?.left).toBe(node6F);
395
+ expect(node10F?.right).toBe(node21F);
396
+ expect(node10F?.parent).toBe(undefined);
397
+ expect(node20F).toBe(undefined);
398
+ expect(node5F).toBe(undefined);
399
+ expect(node15F?.key).toBe(15);
400
+ expect(node15F?.color).toBe('RED');
401
+ expect(node15F?.left).toBe(tmm.NIL);
402
+ expect(node15F?.right).toBe(tmm.NIL);
403
+ expect(node15F?.parent).toBe(node21F);
404
+ expect(node21F?.key).toBe(21);
405
+ expect(node21F?.color).toBe('BLACK');
406
+ expect(node21F?.left).toBe(node15F);
407
+ expect(node21F?.right).toBe(tmm.NIL);
408
+ expect(node21F?.parent).toBe(node10F);
409
+ expect(node6F?.key).toBe(6);
410
+ expect(node6F?.color).toBe('BLACK');
411
+ expect(node6F?.left).toBe(node2F);
412
+ expect(node6F?.right).toBe(tmm.NIL);
413
+ expect(node6F?.parent).toBe(node10F);
414
+ expect(node2F?.key).toBe(2);
415
+ expect(node2F?.color).toBe('RED');
416
+ expect(node2F?.left).toBe(tmm.NIL);
417
+ expect(node2F?.right).toBe(tmm.NIL);
418
+ expect(node2F?.parent).toBe(node6F);
419
+ expect(node15F?.key).toBe(15);
420
+ expect(node15F?.color).toBe('RED');
421
+ expect(node15F?.left).toBe(tmm.NIL);
422
+ expect(node15F?.right).toBe(tmm.NIL);
423
+ expect(node15F?.parent).toBe(node21F);
424
+ });
425
+
426
+ it('should fix the tmm after insertion', () => {
427
+ tmm.add(1);
428
+ tmm.add(2);
429
+ tmm.add(5);
430
+ tmm.add(15);
431
+ const node15F = tmm.getNode(15);
432
+ expect(node15F?.left).toBe(tmm.NIL);
433
+ expect(node15F?.right).toBe(tmm.NIL);
434
+ expect(node15F?.parent).toBe(tmm.getNode(5));
435
+
436
+ tmm.add(25);
437
+ tmm.add(10);
438
+ tmm.add(8);
439
+ tmm.add(28);
440
+ tmm.add(111);
441
+ tmm.add(12);
442
+ tmm.delete(2);
443
+ tmm.add(22);
444
+ tmm.add(50);
445
+ tmm.add(155);
446
+ tmm.add(225);
447
+ const node225F = tmm.getNode(225);
448
+ expect(node225F?.left).toBe(tmm.NIL);
449
+ expect(node225F?.right).toBe(tmm.NIL);
450
+ expect(node225F?.parent?.key).toBe(155);
451
+ tmm.add(7);
452
+ if (isDebug) tmm.print();
764
453
 
765
- it('forEach should iterate over all elements', () => {
766
- const mockCallback = jest.fn();
767
- treeMM.forEach((key, value) => {
768
- mockCallback(key, value);
769
- });
454
+ const node15S = tmm.getNode(15);
455
+ expect(node15S?.left?.key).toBe(10);
456
+ expect(node15S?.right?.key).toBe(25);
457
+ expect(tmm.root).toBe(tmm.getNode(8));
458
+ expect(node15S?.parent?.key).toBe(28);
459
+ tmm.delete(15);
460
+ expect(tmm.root?.key).toBe(8);
461
+ expect(tmm.root?.parent).toBe(undefined);
462
+
463
+ const node15T = tmm.getNode(15);
464
+ expect(node15T).toBe(undefined);
465
+
466
+ tmm.add(23);
467
+ tmm.add(33);
468
+ tmm.add(15);
469
+
470
+ const nodeLM = tmm.getLeftMost();
471
+ expect(nodeLM).toBe(1);
472
+
473
+ const node50 = tmm.getNode(50);
474
+ expect(node50?.key).toBe(50);
475
+ expect(node50?.left?.key).toBe(33);
476
+ expect(node50?.right).toBe(tmm.NIL);
477
+ const node15Fo = tmm.getNode(15);
478
+
479
+ expect(node15Fo?.key).toBe(15);
480
+ expect(node15Fo?.left).toBe(tmm.NIL);
481
+ const node225S = tmm.getNode(225);
482
+ expect(node225S?.left).toBe(tmm.NIL);
483
+ expect(node225S?.right).toBe(tmm.NIL);
484
+ expect(node225S?.parent?.key).toBe(155);
485
+ // TODO
486
+ // expect(tmm.getNode(0)).toBe(undefined);
487
+ tmm.add(2);
488
+ tmm.add(3);
489
+ tmm.add(4);
490
+ tmm.add(6);
491
+ tmm.add(9);
492
+ tmm.add(11);
493
+ tmm.add(13);
494
+ tmm.add(14);
495
+ tmm.add(16);
496
+ tmm.add(17);
497
+ tmm.add(18);
498
+ tmm.add(19);
499
+ tmm.add(110);
770
500
 
771
- expect(mockCallback.mock.calls.length).toBe(3);
772
- expect(mockCallback.mock.calls[0]).toEqual([1, 'a']);
773
- expect(mockCallback.mock.calls[1]).toEqual([2, 'b']);
774
- expect(mockCallback.mock.calls[2]).toEqual([3, 'c']);
775
- });
501
+ if (isDebug) tmm.print();
776
502
 
777
- it('filter should return a new tree with filtered elements', () => {
778
- const filteredTree = treeMM.filter(key => key > 1);
779
- expect(filteredTree.size).toBe(2);
780
- expect([...filteredTree]).toEqual([
781
- [2, 'b'],
782
- [3, 'c']
503
+ expect(tmm.dfs()).toEqual([
504
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 22, 23, 25, 28, 33, 50, 110, 111, 155, 225
783
505
  ]);
506
+
507
+ expect(tmm.isBST()).toBe(true);
784
508
  });
785
509
 
786
- it('map should return a new tree with modified elements', () => {
787
- const mappedTree = treeMM.map((key, value) => [(key * 2).toString(), value]);
788
- expect(mappedTree.size).toBe(3);
789
- expect([...mappedTree]).toEqual([
790
- ['2', 'a'],
791
- ['4', 'b'],
792
- ['6', 'c']
510
+ it('should fix the tmm after insertion and deletion', () => {
511
+ for (let i = 0; i < 100; i++) {
512
+ tmm.add(i);
513
+ }
514
+ for (let i = 0; i < 49; i++) {
515
+ tmm.delete(i);
516
+ }
517
+
518
+ expect(tmm.size).toBe(51);
519
+ expect(tmm.isBST()).toBe(true);
520
+ expect(tmm.isBST(tmm.root, 'RECURSIVE')).toBe(true);
521
+
522
+ expect(tmm.dfs(n => n.key, 'IN', tmm.root, 'ITERATIVE')).toEqual([
523
+ 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
524
+ 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
525
+ ]);
526
+ expect(tmm.dfs(n => n.key, 'IN', tmm.root, 'RECURSIVE')).toEqual([
527
+ 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
528
+ 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
793
529
  ]);
794
530
  });
795
531
 
796
- it('reduce should accumulate values', () => {
797
- const sum = treeMM.reduce((acc, value, key) => acc + key, 0);
798
- expect(sum).toBe(6);
799
- });
532
+ it('should fix the tmm after large scale insertion and deletion', () => {
533
+ for (let i = 0; i < 10000; i++) {
534
+ tmm.add(i);
535
+ }
536
+ for (let i = 0; i < 10000; i++) {
537
+ tmm.delete(i);
538
+ }
539
+
540
+ expect(tmm.size).toBe(0);
541
+ expect(tmm.isBST()).toBe(true);
542
+ expect(tmm.dfs(n => n.key, 'IN', tmm.root, 'ITERATIVE')).toEqual([]);
800
543
 
801
- it('[Symbol.iterator] should provide an iterator', () => {
802
- const entries = [];
803
- for (const entry of treeMM) {
804
- entries.push(entry);
544
+ tmm.clear();
545
+ for (let i = 0; i < 1000; i++) {
546
+ tmm.add(getRandomInt(-100, 1000));
547
+ tmm.delete(getRandomInt(-100, 1000));
805
548
  }
806
549
 
807
- expect(entries.length).toBe(3);
808
- expect(entries).toEqual([
809
- [1, 'a'],
810
- [2, 'b'],
811
- [3, 'c']
812
- ]);
550
+ // TODO there is a bug when dfs the tmm with NIL node
551
+ // expect(tmm.isBST()).toBe(true);
813
552
  });
814
553
 
815
- it('should clone work well', () => {
816
- expect(treeMM.count).toBe(21);
817
- expect(treeMM.getComputedCount()).toBe(21);
818
- const cloned = treeMM.clone();
819
- expect(cloned.root?.left?.key).toBe(1);
820
- if (cloned.isMapMode) expect(cloned.get(cloned.root?.right)).toBe('c');
821
- else expect(cloned.root?.right?.value).toBe(undefined);
822
- });
554
+ it('duplicates', () => {
555
+ tmm.addMany([9, 8, 7, 8, 8, 8, 2, 3, 6, 5, 5, 4]);
556
+ if (isDebug) tmm.print();
557
+
558
+ expect(tmm.size).toBe(8);
559
+ expect(tmm.isBST()).toBe(true);
560
+ expect(tmm.isAVLBalanced()).toBe(true);
561
+ tmm.addMany([10, 5, 2, 11]);
562
+ expect(tmm.size).toBe(10);
563
+ expect(tmm.isBST()).toBe(true);
564
+ expect(tmm.isAVLBalanced()).toBe(true);
823
565
 
824
- it('should keys', () => {
825
- const keys = treeMM.keys();
826
- expect([...keys]).toEqual([1, 2, 3]);
566
+ tmm.clear();
567
+ tmm.addMany([10, 20, 30, 40, 50, 60]);
568
+ expect(tmm.isAVLBalanced()).toBe(false);
827
569
  });
828
570
 
829
- it('should values', () => {
830
- const values = treeMM.values();
831
- expect([...values]).toEqual(['a', 'b', 'c']);
571
+ describe('TreeMultiMap delete test', function () {
572
+ const tmm = new TreeMultiMap<number, number>();
573
+ const inputSize = 100; // Adjust input sizes as needed
574
+
575
+ beforeEach(() => {
576
+ tmm.clear();
577
+ });
578
+ it('The structure remains normal after random deletion', function () {
579
+ for (let i = 0; i < inputSize; i++) {
580
+ tmm.add(i);
581
+ }
582
+
583
+ for (let i = 0; i < inputSize; i++) {
584
+ const num = getRandomInt(0, inputSize - 1);
585
+ tmm.delete(num);
586
+ }
587
+
588
+ let nanCount = 0;
589
+ const dfs = (cur: TreeMultiMapNode<number>) => {
590
+ if (isNaN(cur.key)) nanCount++;
591
+ if (cur.left) dfs(cur.left);
592
+ if (cur.right) dfs(cur.right);
593
+ };
594
+ if (tmm.root) dfs(tmm.root);
595
+
596
+ expect(tmm.size).toBeLessThanOrEqual(inputSize);
597
+ expect(tmm.getHeight()).toBeLessThan(Math.log2(inputSize) * 2);
598
+
599
+ expect(nanCount).toBeLessThanOrEqual(inputSize);
600
+ });
601
+
602
+ it(`Random additions, complete deletions of structures are normal`, function () {
603
+ for (let i = 0; i < inputSize; i++) {
604
+ const num = getRandomInt(0, inputSize - 1);
605
+ if (i === 0 && isDebug) console.log(`first:`, num);
606
+ tmm.add(num);
607
+ }
608
+
609
+ for (let i = 0; i < inputSize; i++) {
610
+ tmm.delete(i);
611
+ }
612
+
613
+ let nanCount = 0;
614
+ const dfs = (cur: TreeMultiMapNode<number>) => {
615
+ if (isNaN(cur.key)) nanCount++;
616
+ if (cur.left) dfs(cur.left);
617
+ if (cur.right) dfs(cur.right);
618
+ };
619
+ if (tmm.root) dfs(tmm.root);
620
+
621
+ expect(tmm.size).toBe(0);
622
+ expect(tmm.getHeight()).toBe(-1);
623
+ expect(nanCount).toBeLessThanOrEqual(inputSize);
624
+
625
+ if (isDebug) tmm.print();
626
+ });
832
627
  });
833
628
 
834
- it('should leaves', () => {
835
- const leaves = treeMM.leaves();
836
- expect(leaves).toEqual([1, 3]);
629
+ describe('TreeMultiMap iterative methods test', () => {
630
+ let tmm: TreeMultiMap<number, string, object, string, string, object>;
631
+ beforeEach(() => {
632
+ tmm = new TreeMultiMap();
633
+ tmm.add([1, ['a']]);
634
+ tmm.add(2, 'b');
635
+ tmm.add([3, ['c']]);
636
+ });
637
+
638
+ it('The node obtained by get Node should match the node type', () => {
639
+ const node3 = tmm.getNode(3);
640
+ expect(node3).toBeInstanceOf(BinaryTreeNode);
641
+ expect(node3).toBeInstanceOf(BSTNode);
642
+ expect(node3).toBeInstanceOf(TreeMultiMapNode);
643
+ });
644
+
645
+ it('forEach should iterate over all elements', () => {
646
+ const mockCallback = jest.fn();
647
+ tmm.forEach((key, value) => {
648
+ mockCallback(key, value);
649
+ });
650
+
651
+ expect(mockCallback.mock.calls.length).toBe(3);
652
+ expect(mockCallback.mock.calls[0]).toEqual([1, ['a']]);
653
+ expect(mockCallback.mock.calls[1]).toEqual([2, ['b']]);
654
+ expect(mockCallback.mock.calls[2]).toEqual([3, ['c']]);
655
+ });
656
+
657
+ it('filter should return a new tmm with filtered elements', () => {
658
+ const filteredTree = tmm.filter(key => key > 1);
659
+ expect(filteredTree.size).toBe(2);
660
+ expect([...filteredTree]).toEqual([
661
+ [2, ['b']],
662
+ [3, ['c']]
663
+ ]);
664
+ });
665
+
666
+ it('map should return a new tmm with modified elements', () => {
667
+ const tmmMapped = tmm.map((key, value) => [(key * 2).toString(), value ? value :[]]);
668
+ expect(tmmMapped.size).toBe(3);
669
+ expect([...tmmMapped]).toEqual([
670
+ ['2', ['a']],
671
+ ['4', ['b']],
672
+ ['6', ['c']]
673
+ ]);
674
+ });
675
+
676
+ it('reduce should accumulate values', () => {
677
+ const sum = tmm.reduce((acc, value, key) => acc + key, 0);
678
+ expect(sum).toBe(6);
679
+ });
680
+
681
+ it('[Symbol.iterator] should provide an iterator', () => {
682
+ const entries = [];
683
+ for (const entry of tmm) {
684
+ entries.push(entry);
685
+ }
686
+
687
+ expect(entries.length).toBe(3);
688
+ expect(entries).toEqual([
689
+ [1, ['a']],
690
+ [2, ['b']],
691
+ [3, ['c']]
692
+ ]);
693
+ });
837
694
  });
838
695
  });
839
696
 
840
- describe('TreeMultiMap count not map mode', () => {
841
- let tmm: TreeMultiMap<number>;
697
+ describe('TreeMultiMap - _deleteFixup', () => {
698
+ let tmm: TreeMultiMap<number, number>;
699
+
842
700
  beforeEach(() => {
843
- tmm = new TreeMultiMap<number>([], { isMapMode: false });
701
+ tmm = new TreeMultiMap();
844
702
  });
845
703
 
846
- it('Should added node count ', () => {
847
- tmm.addMany([
848
- [1, 1],
849
- [2, 2],
850
- [3, 3],
851
- [4, 4],
852
- [5, 5]
853
- ]);
854
- const newNode = new TreeMultiMapNode(3, undefined, 10);
855
- tmm.add(newNode, 33, 20);
856
- // TODO expect(tmm.count).toBe(25);
857
- expect(tmm.count).toBe(15);
858
- expect(tmm.getComputedCount()).toBe(15);
859
- expect(tmm.getNode(3)?.count).toBe(11);
704
+ it('should handle deleting a red leaf node', () => {
705
+ tmm.add(10, 10);
706
+ tmm.add(5, 5); // Red leaf
707
+ tmm.add(20, 20);
708
+
709
+ expect(tmm.delete(5)).toHaveLength(1); // Delete red leaf
710
+ expect(tmm.root?.left).toBe(tmm.NIL); // Left child should be NIL
860
711
  });
861
- });
862
712
 
863
- describe('TreeMultiMap operations test1 not map mode', () => {
864
- it('should perform various operations on a Binary Search Tree with numeric values1', () => {
865
- const tmm = new TreeMultiMap<number, number>([], { isMapMode: false });
866
-
867
- expect(tmm instanceof TreeMultiMap);
868
-
869
- tmm.add([11, 11]);
870
- tmm.add([3, 3]);
871
- const idAndValues: [number, number][] = [
872
- [11, 11],
873
- [3, 3],
874
- [15, 15],
875
- [1, 1],
876
- [8, 8],
877
- [13, 13],
878
- [16, 16],
879
- [2, 2],
880
- [6, 6],
881
- [9, 9],
882
- [12, 12],
883
- [14, 14],
884
- [4, 4],
885
- [7, 7],
886
- [10, 10],
887
- [5, 5]
888
- ];
889
- tmm.addMany(idAndValues);
890
- expect(tmm.root instanceof TreeMultiMapNode);
891
-
892
- if (tmm.root) expect(tmm.root.key == 11);
893
-
894
- expect(tmm.size).toBe(16);
895
- expect(tmm.count).toBe(18);
896
- expect(tmm.getComputedCount()).toBe(18);
897
-
898
- expect(tmm.has(6));
899
- if (isDebug) tmm.print();
900
- expect(tmm.getHeight(6)).toBe(1);
901
- expect(tmm.getDepth(6)).toBe(3);
902
- const nodeId10 = tmm.getNode(10);
903
- expect(nodeId10?.key).toBe(10);
713
+ it('should handle deleting a black leaf node', () => {
714
+ tmm.add(10, 10);
715
+ tmm.add(5, 5); // Black node
716
+ tmm.add(20, 20);
717
+ tmm.add(1, 1); // Black leaf node
904
718
 
905
- const nodeVal9 = tmm.getNode(node => node.key === 9);
906
- expect(nodeVal9?.key).toBe(9);
719
+ expect(tmm.delete(1)).toHaveLength(1); // Delete black leaf
720
+ expect(tmm.root?.left?.left).toBe(tmm.NIL);
721
+ });
722
+
723
+ it('should handle deleting black node with red sibling', () => {
724
+ tmm.add(10, 10);
725
+ tmm.add(5, 5); // Black node
726
+ tmm.add(20, 20); // Red sibling
727
+ tmm.add(25, 25); // Force the sibling to be red
728
+
729
+ expect(tmm.delete(5)).toHaveLength(1); // Delete black node
730
+ expect(tmm.root?.right?.color).toBe('BLACK'); // Ensure sibling color is black after fixup
731
+ });
732
+
733
+ it('should handle deleting black node with black sibling', () => {
734
+ tmm.add(10, 10);
735
+ tmm.add(5, 5); // Black node
736
+ tmm.add(20, 20); // Black sibling
737
+
738
+ expect(tmm.delete(5)).toHaveLength(1); // Delete black node
739
+ expect(tmm.root?.left).toBe(tmm.NIL);
740
+ });
741
+
742
+ it('should handle deleting the root node', () => {
743
+ tmm.add(10, 10); // Root node
744
+ tmm.add(5, 5);
745
+ tmm.add(20, 20);
746
+
747
+ expect(tmm.delete(10)).toHaveLength(1); // Delete root node
748
+ expect(tmm.root?.key).toBe(20); // New root should be 20
749
+ });
750
+
751
+ it('should handle complex case with multiple rotations', () => {
752
+ tmm.add(10, 10);
753
+ tmm.add(5, 5);
754
+ tmm.add(15, 15);
755
+ tmm.add(12, 12);
756
+ tmm.add(18, 18);
757
+ tmm.add(16, 16);
758
+
759
+ // Delete a node that will cause rotations and color changes
760
+ expect(tmm.delete(5)).toHaveLength(1);
761
+
762
+ // Verify the color and structure after fixup
763
+ expect(tmm.root?.color).toBe('BLACK');
764
+ expect(tmm.root?.left).toBe(tmm.NIL);
765
+ expect(tmm.root?.right?.left?.color).toBe('BLACK');
766
+ });
767
+
768
+ it('should handle complex delete fixup scenarios', () => {
769
+ const tmm = new TreeMultiMap<number, number>();
770
+
771
+ // Build a tmm that will require complex fixup
772
+ tmm.add(20, 20);
773
+ tmm.add(10, 10);
774
+ tmm.add(30, 30);
775
+ tmm.add(5, 5);
776
+ tmm.add(15, 15);
777
+ tmm.add(25, 25);
778
+ tmm.add(35, 35);
779
+ tmm.add(2, 2);
780
+ tmm.add(8, 8);
781
+
782
+ // This deletion should trigger a complex fixup
783
+ tmm.delete(2);
784
+ // tmm.print(tmm.root, { isShowNull: true, isShowRedBlackNIL: true, isShowUndefined: false });
785
+
786
+ expect(tmm.isLeaf(2)).toBe(false);
787
+ expect(tmm.isLeaf(8)).toBe(true);
788
+ expect(tmm.isLeaf(15)).toBe(true);
789
+ expect(tmm.isLeaf(25)).toBe(true);
790
+ expect(tmm.isLeaf(35)).toBe(true);
791
+ expect(tmm.isLeaf(20)).toBe(false);
792
+ expect(tmm.isLeaf(30)).toBe(false);
793
+ // Verify tmm structure and colors after fixup
794
+ expect(tmm.root?.color).toBe('BLACK');
795
+ expect(tmm.root?.key).toBe(20);
796
+ expect(tmm.root?.left?.color).toBe('RED');
797
+ expect(tmm.root?.left?.key).toBe(10);
798
+ expect(tmm.root?.right?.color).toBe('BLACK');
799
+ expect(tmm.root?.right?.key).toBe(30);
800
+ expect(tmm.root?.left?.left?.color).toBe('BLACK');
801
+ expect(tmm.root?.left?.left?.key).toBe(5);
802
+ expect(tmm.root?.left?.right?.color).toBe('BLACK');
803
+ expect(tmm.root?.left?.right?.key).toBe(15);
804
+ expect(tmm.leaves(node => (node === null ? '' : `${node.key} ${node.color}`), tmm.root, 'RECURSIVE')).toEqual([
805
+ '8 RED',
806
+ '15 BLACK',
807
+ '25 RED',
808
+ '35 RED'
809
+ ]);
810
+ expect(tmm.listLevels(node => (node === tmm.NIL ? 'NIL' : `${node.key} ${node.color}`))).toEqual([
811
+ ['20 BLACK'],
812
+ ['10 RED', '30 BLACK'],
813
+ ['5 BLACK', '15 BLACK', '25 RED', '35 RED'],
814
+ ['NIL', '8 RED', 'NIL', 'NIL', 'NIL', 'NIL', 'NIL', 'NIL'],
815
+ ['NIL', 'NIL']
816
+ ]);
907
817
  });
908
818
  });
909
819
 
910
- describe('TreeMultiMap operations test recursively1 not map mode', () => {
911
- it('should perform various operations on a Binary Search Tree with numeric values1', () => {
912
- const tmm = new TreeMultiMap<number>([], {
913
- iterationType: 'RECURSIVE',
914
- isMapMode: false
820
+ describe('real world data', () => {
821
+ it('cost of living', () => {
822
+ const indexedByRank = new TreeMultiMap(costOfLiving, {
823
+ specifyComparable: node => node.rank,
824
+ toEntryFn: raw => [raw, undefined]
915
825
  });
916
-
917
- expect(tmm instanceof TreeMultiMap);
918
- tmm.add([11, 11]);
919
- tmm.add([3, 3]);
920
- const idAndValues: [number, number][] = [
921
- [11, 11],
922
- [3, 3],
923
- [15, 15],
924
- [1, 1],
925
- [8, 8],
926
- [13, 13],
927
- [16, 16],
928
- [2, 2],
929
- [6, 6],
930
- [9, 9],
931
- [12, 12],
932
- [14, 14],
933
- [4, 4],
934
- [7, 7],
935
- [10, 10],
936
- [5, 5]
937
- ];
938
- tmm.addMany(idAndValues);
939
- expect(tmm.root).toBeInstanceOf(TreeMultiMapNode);
940
-
941
- if (tmm.root) expect(tmm.root.key).toBe(5);
942
-
943
- expect(tmm.size).toBe(16);
944
- expect(tmm.count).toBe(18);
945
- expect(tmm.getComputedCount()).toBe(18);
946
-
947
- expect(tmm.has(6));
948
-
949
- expect(tmm.getHeight(6)).toBe(1);
950
- expect(tmm.getDepth(6)).toBe(3);
951
- const nodeId10 = tmm.getNode(10);
952
- expect(nodeId10?.key).toBe(10);
953
-
954
- const nodeVal9 = tmm.getNode(node => node.key === 9);
955
- expect(nodeVal9?.key).toBe(9);
826
+ expect(indexedByRank.size).toBe(7);
827
+ expect(indexedByRank.dfs(node => node?.key?.country)).toEqual([
828
+ 'Switzerland',
829
+ 'New Zealand',
830
+ 'Mexico',
831
+ 'South Africa',
832
+ 'Japan',
833
+ 'Brazil',
834
+ 'Taiwan'
835
+ ]);
956
836
  });
957
837
  });
958
838
 
959
- describe('TreeMultiMap iterative methods test not map mode', () => {
960
- let treeMM: TreeMultiMap<number, string>;
961
- beforeEach(() => {
962
- treeMM = new TreeMultiMap<number, string>([], { isMapMode: false });
963
- treeMM.add(1, 'a', 10);
964
- treeMM.add([2, 'b'], undefined, 10);
965
- treeMM.add([3, 'c'], undefined, 1);
966
- });
967
-
968
- it('should clone work well', () => {
969
- expect(treeMM.count).toBe(21);
970
- expect(treeMM.getComputedCount()).toBe(21);
971
- const cloned = treeMM.clone();
972
- expect(cloned.root?.left?.key).toBe(1);
973
- expect(cloned.get(cloned.root?.right)).toBe(undefined);
839
+ describe('classic use', () => {
840
+ // Test case for finding elements in a given range
841
+ it('@example Find elements in a range', () => {
842
+ const tmm = new TreeMultiMap<number>([10, 5, 15, 3, 7, 12, 18]);
843
+ expect(tmm.search(new Range(5, 10))).toEqual([5, 10, 7]);
844
+ expect(tmm.search(new Range(4, 12))).toEqual([5, 10, 12, 7]);
845
+ expect(tmm.search(new Range(15, 20))).toEqual([15, 18]);
974
846
  });
975
847
  });