data-structure-typed 2.0.5 → 2.1.0

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 (260) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/COMMANDS.md +17 -0
  3. package/benchmark/report.html +13 -77
  4. package/benchmark/report.json +145 -177
  5. package/dist/cjs/data-structures/base/iterable-element-base.d.ts +186 -83
  6. package/dist/cjs/data-structures/base/iterable-element-base.js +149 -107
  7. package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -1
  8. package/dist/cjs/data-structures/base/iterable-entry-base.d.ts +95 -119
  9. package/dist/cjs/data-structures/base/iterable-entry-base.js +59 -116
  10. package/dist/cjs/data-structures/base/iterable-entry-base.js.map +1 -1
  11. package/dist/cjs/data-structures/base/linear-base.d.ts +250 -192
  12. package/dist/cjs/data-structures/base/linear-base.js +137 -274
  13. package/dist/cjs/data-structures/base/linear-base.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  15. package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js +171 -205
  16. package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js.map +1 -1
  17. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  18. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
  19. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  20. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +138 -149
  21. package/dist/cjs/data-structures/binary-tree/avl-tree.js +208 -195
  22. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  23. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +476 -632
  24. package/dist/cjs/data-structures/binary-tree/binary-tree.js +594 -865
  25. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  26. package/dist/cjs/data-structures/binary-tree/bst.d.ts +258 -306
  27. package/dist/cjs/data-structures/binary-tree/bst.js +505 -481
  28. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  29. package/dist/cjs/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  30. package/dist/cjs/data-structures/binary-tree/red-black-tree.js +114 -209
  31. package/dist/cjs/data-structures/binary-tree/red-black-tree.js.map +1 -1
  32. package/dist/cjs/data-structures/binary-tree/tree-counter.d.ts +132 -154
  33. package/dist/cjs/data-structures/binary-tree/tree-counter.js +172 -203
  34. package/dist/cjs/data-structures/binary-tree/tree-counter.js.map +1 -1
  35. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  36. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +105 -85
  37. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  38. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +238 -233
  39. package/dist/cjs/data-structures/graph/abstract-graph.js +267 -237
  40. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  41. package/dist/cjs/data-structures/graph/directed-graph.d.ts +108 -224
  42. package/dist/cjs/data-structures/graph/directed-graph.js +146 -233
  43. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  44. package/dist/cjs/data-structures/graph/map-graph.d.ts +49 -55
  45. package/dist/cjs/data-structures/graph/map-graph.js +56 -59
  46. package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
  47. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +103 -146
  48. package/dist/cjs/data-structures/graph/undirected-graph.js +129 -149
  49. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  50. package/dist/cjs/data-structures/hash/hash-map.d.ts +164 -338
  51. package/dist/cjs/data-structures/hash/hash-map.js +270 -457
  52. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  53. package/dist/cjs/data-structures/heap/heap.d.ts +214 -289
  54. package/dist/cjs/data-structures/heap/heap.js +340 -349
  55. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  56. package/dist/cjs/data-structures/heap/max-heap.d.ts +11 -47
  57. package/dist/cjs/data-structures/heap/max-heap.js +11 -66
  58. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  59. package/dist/cjs/data-structures/heap/min-heap.d.ts +12 -47
  60. package/dist/cjs/data-structures/heap/min-heap.js +11 -66
  61. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  62. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  63. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +368 -494
  64. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  65. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  66. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +447 -466
  67. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  68. package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  69. package/dist/cjs/data-structures/linked-list/skip-linked-list.js +0 -100
  70. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
  71. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  72. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +11 -78
  73. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  74. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  75. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +10 -79
  76. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  77. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -61
  78. package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -83
  79. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  80. package/dist/cjs/data-structures/queue/deque.d.ts +227 -254
  81. package/dist/cjs/data-structures/queue/deque.js +309 -348
  82. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  83. package/dist/cjs/data-structures/queue/queue.d.ts +180 -201
  84. package/dist/cjs/data-structures/queue/queue.js +265 -248
  85. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  86. package/dist/cjs/data-structures/stack/stack.d.ts +124 -102
  87. package/dist/cjs/data-structures/stack/stack.js +181 -125
  88. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  89. package/dist/cjs/data-structures/trie/trie.d.ts +164 -165
  90. package/dist/cjs/data-structures/trie/trie.js +189 -172
  91. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  92. package/dist/cjs/interfaces/binary-tree.d.ts +56 -6
  93. package/dist/cjs/interfaces/graph.d.ts +16 -0
  94. package/dist/cjs/types/data-structures/base/base.d.ts +1 -1
  95. package/dist/cjs/types/data-structures/graph/abstract-graph.d.ts +4 -0
  96. package/dist/cjs/types/utils/utils.d.ts +1 -0
  97. package/dist/cjs/utils/utils.d.ts +1 -1
  98. package/dist/cjs/utils/utils.js +2 -1
  99. package/dist/cjs/utils/utils.js.map +1 -1
  100. package/dist/esm/data-structures/base/iterable-element-base.d.ts +186 -83
  101. package/dist/esm/data-structures/base/iterable-element-base.js +155 -107
  102. package/dist/esm/data-structures/base/iterable-element-base.js.map +1 -1
  103. package/dist/esm/data-structures/base/iterable-entry-base.d.ts +95 -119
  104. package/dist/esm/data-structures/base/iterable-entry-base.js +59 -116
  105. package/dist/esm/data-structures/base/iterable-entry-base.js.map +1 -1
  106. package/dist/esm/data-structures/base/linear-base.d.ts +250 -192
  107. package/dist/esm/data-structures/base/linear-base.js +137 -274
  108. package/dist/esm/data-structures/base/linear-base.js.map +1 -1
  109. package/dist/esm/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  110. package/dist/esm/data-structures/binary-tree/avl-tree-counter.js +171 -212
  111. package/dist/esm/data-structures/binary-tree/avl-tree-counter.js.map +1 -1
  112. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  113. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js +133 -94
  114. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  115. package/dist/esm/data-structures/binary-tree/avl-tree.d.ts +138 -149
  116. package/dist/esm/data-structures/binary-tree/avl-tree.js +206 -200
  117. package/dist/esm/data-structures/binary-tree/avl-tree.js.map +1 -1
  118. package/dist/esm/data-structures/binary-tree/binary-tree.d.ts +476 -632
  119. package/dist/esm/data-structures/binary-tree/binary-tree.js +598 -874
  120. package/dist/esm/data-structures/binary-tree/binary-tree.js.map +1 -1
  121. package/dist/esm/data-structures/binary-tree/bst.d.ts +258 -306
  122. package/dist/esm/data-structures/binary-tree/bst.js +507 -487
  123. package/dist/esm/data-structures/binary-tree/bst.js.map +1 -1
  124. package/dist/esm/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  125. package/dist/esm/data-structures/binary-tree/red-black-tree.js +114 -215
  126. package/dist/esm/data-structures/binary-tree/red-black-tree.js.map +1 -1
  127. package/dist/esm/data-structures/binary-tree/tree-counter.d.ts +132 -154
  128. package/dist/esm/data-structures/binary-tree/tree-counter.js +175 -209
  129. package/dist/esm/data-structures/binary-tree/tree-counter.js.map +1 -1
  130. package/dist/esm/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  131. package/dist/esm/data-structures/binary-tree/tree-multi-map.js +103 -92
  132. package/dist/esm/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  133. package/dist/esm/data-structures/graph/abstract-graph.d.ts +238 -233
  134. package/dist/esm/data-structures/graph/abstract-graph.js +267 -237
  135. package/dist/esm/data-structures/graph/abstract-graph.js.map +1 -1
  136. package/dist/esm/data-structures/graph/directed-graph.d.ts +108 -224
  137. package/dist/esm/data-structures/graph/directed-graph.js +145 -233
  138. package/dist/esm/data-structures/graph/directed-graph.js.map +1 -1
  139. package/dist/esm/data-structures/graph/map-graph.d.ts +49 -55
  140. package/dist/esm/data-structures/graph/map-graph.js +56 -59
  141. package/dist/esm/data-structures/graph/map-graph.js.map +1 -1
  142. package/dist/esm/data-structures/graph/undirected-graph.d.ts +103 -146
  143. package/dist/esm/data-structures/graph/undirected-graph.js +128 -149
  144. package/dist/esm/data-structures/graph/undirected-graph.js.map +1 -1
  145. package/dist/esm/data-structures/hash/hash-map.d.ts +164 -338
  146. package/dist/esm/data-structures/hash/hash-map.js +270 -457
  147. package/dist/esm/data-structures/hash/hash-map.js.map +1 -1
  148. package/dist/esm/data-structures/heap/heap.d.ts +214 -289
  149. package/dist/esm/data-structures/heap/heap.js +329 -349
  150. package/dist/esm/data-structures/heap/heap.js.map +1 -1
  151. package/dist/esm/data-structures/heap/max-heap.d.ts +11 -47
  152. package/dist/esm/data-structures/heap/max-heap.js +11 -66
  153. package/dist/esm/data-structures/heap/max-heap.js.map +1 -1
  154. package/dist/esm/data-structures/heap/min-heap.d.ts +12 -47
  155. package/dist/esm/data-structures/heap/min-heap.js +11 -66
  156. package/dist/esm/data-structures/heap/min-heap.js.map +1 -1
  157. package/dist/esm/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  158. package/dist/esm/data-structures/linked-list/doubly-linked-list.js +368 -495
  159. package/dist/esm/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  160. package/dist/esm/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  161. package/dist/esm/data-structures/linked-list/singly-linked-list.js +448 -467
  162. package/dist/esm/data-structures/linked-list/singly-linked-list.js.map +1 -1
  163. package/dist/esm/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  164. package/dist/esm/data-structures/linked-list/skip-linked-list.js +0 -100
  165. package/dist/esm/data-structures/linked-list/skip-linked-list.js.map +1 -1
  166. package/dist/esm/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  167. package/dist/esm/data-structures/priority-queue/max-priority-queue.js +11 -78
  168. package/dist/esm/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  169. package/dist/esm/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  170. package/dist/esm/data-structures/priority-queue/min-priority-queue.js +10 -79
  171. package/dist/esm/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  172. package/dist/esm/data-structures/priority-queue/priority-queue.d.ts +2 -61
  173. package/dist/esm/data-structures/priority-queue/priority-queue.js +8 -83
  174. package/dist/esm/data-structures/priority-queue/priority-queue.js.map +1 -1
  175. package/dist/esm/data-structures/queue/deque.d.ts +227 -254
  176. package/dist/esm/data-structures/queue/deque.js +313 -348
  177. package/dist/esm/data-structures/queue/deque.js.map +1 -1
  178. package/dist/esm/data-structures/queue/queue.d.ts +180 -201
  179. package/dist/esm/data-structures/queue/queue.js +263 -248
  180. package/dist/esm/data-structures/queue/queue.js.map +1 -1
  181. package/dist/esm/data-structures/stack/stack.d.ts +124 -102
  182. package/dist/esm/data-structures/stack/stack.js +181 -125
  183. package/dist/esm/data-structures/stack/stack.js.map +1 -1
  184. package/dist/esm/data-structures/trie/trie.d.ts +164 -165
  185. package/dist/esm/data-structures/trie/trie.js +193 -172
  186. package/dist/esm/data-structures/trie/trie.js.map +1 -1
  187. package/dist/esm/interfaces/binary-tree.d.ts +56 -6
  188. package/dist/esm/interfaces/graph.d.ts +16 -0
  189. package/dist/esm/types/data-structures/base/base.d.ts +1 -1
  190. package/dist/esm/types/data-structures/graph/abstract-graph.d.ts +4 -0
  191. package/dist/esm/types/utils/utils.d.ts +1 -0
  192. package/dist/esm/utils/utils.d.ts +1 -1
  193. package/dist/esm/utils/utils.js +2 -1
  194. package/dist/esm/utils/utils.js.map +1 -1
  195. package/dist/umd/data-structure-typed.js +4685 -6477
  196. package/dist/umd/data-structure-typed.min.js +8 -6
  197. package/dist/umd/data-structure-typed.min.js.map +1 -1
  198. package/package.json +3 -4
  199. package/src/data-structures/base/iterable-element-base.ts +238 -115
  200. package/src/data-structures/base/iterable-entry-base.ts +96 -120
  201. package/src/data-structures/base/linear-base.ts +271 -277
  202. package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
  203. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
  204. package/src/data-structures/binary-tree/avl-tree.ts +239 -206
  205. package/src/data-structures/binary-tree/binary-tree.ts +660 -889
  206. package/src/data-structures/binary-tree/bst.ts +568 -570
  207. package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
  208. package/src/data-structures/binary-tree/tree-counter.ts +199 -218
  209. package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
  210. package/src/data-structures/graph/abstract-graph.ts +339 -264
  211. package/src/data-structures/graph/directed-graph.ts +146 -236
  212. package/src/data-structures/graph/map-graph.ts +63 -60
  213. package/src/data-structures/graph/undirected-graph.ts +129 -152
  214. package/src/data-structures/hash/hash-map.ts +274 -496
  215. package/src/data-structures/heap/heap.ts +389 -402
  216. package/src/data-structures/heap/max-heap.ts +12 -76
  217. package/src/data-structures/heap/min-heap.ts +13 -76
  218. package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
  219. package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
  220. package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
  221. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
  222. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
  223. package/src/data-structures/priority-queue/priority-queue.ts +3 -92
  224. package/src/data-structures/queue/deque.ts +381 -357
  225. package/src/data-structures/queue/queue.ts +310 -264
  226. package/src/data-structures/stack/stack.ts +217 -131
  227. package/src/data-structures/trie/trie.ts +240 -175
  228. package/src/interfaces/binary-tree.ts +240 -6
  229. package/src/interfaces/graph.ts +37 -0
  230. package/src/types/data-structures/base/base.ts +5 -5
  231. package/src/types/data-structures/graph/abstract-graph.ts +5 -0
  232. package/src/types/utils/utils.ts +2 -0
  233. package/src/utils/utils.ts +9 -14
  234. package/test/integration/index.html +1 -1
  235. package/test/performance/benchmark-runner.ts +528 -0
  236. package/test/performance/reportor.mjs +43 -43
  237. package/test/performance/runner-config.json +39 -0
  238. package/test/performance/single-suite-runner.ts +69 -0
  239. package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +3 -3
  240. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +5 -5
  241. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +4 -4
  242. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +350 -90
  243. package/test/unit/data-structures/binary-tree/bst.test.ts +12 -9
  244. package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +2 -2
  245. package/test/unit/data-structures/binary-tree/tree-counter.test.ts +25 -24
  246. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +3 -3
  247. package/test/unit/data-structures/graph/abstract-graph.test.ts +0 -4
  248. package/test/unit/data-structures/graph/directed-graph.test.ts +1 -1
  249. package/test/unit/data-structures/heap/heap.test.ts +14 -21
  250. package/test/unit/data-structures/heap/max-heap.test.ts +5 -9
  251. package/test/unit/data-structures/heap/min-heap.test.ts +1 -4
  252. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +14 -14
  253. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +0 -7
  254. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +8 -11
  255. package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +1 -4
  256. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +1 -4
  257. package/test/unit/data-structures/queue/queue.test.ts +4 -5
  258. package/test/unit/utils/utils.test.ts +0 -1
  259. package/test/performance/data-structures/binary-tree/avl-tree.test.mjs +0 -71
  260. package/test/performance/data-structures/binary-tree/red-black-tree.test.mjs +0 -81
@@ -5,21 +5,33 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { AVLTreeMultiMapOptions, BTNOptKeyOrNull } from '../../types';
8
+
9
+ import type {
10
+ AVLTreeMultiMapOptions,
11
+ AVLTreeOptions,
12
+ BTNOptKeyOrNull,
13
+ ElemOf,
14
+ EntryCallback,
15
+ IterationType
16
+ } from '../../types';
9
17
  import { AVLTree, AVLTreeNode } from './avl-tree';
10
18
  import { IBinaryTree } from '../../interfaces';
11
19
 
20
+ /**
21
+ * Node used by AVLTreeMultiMap; stores the key with a bucket of values (array).
22
+ * @remarks Time O(1), Space O(1)
23
+ * @template K
24
+ * @template V
25
+ */
12
26
  export class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> {
13
27
  override parent?: AVLTreeMultiMapNode<K, V> = undefined;
14
28
 
15
29
  /**
16
- * This TypeScript constructor initializes an object with a key of type K and an array of values of
17
- * type V.
18
- * @param {K} key - The `key` parameter is typically used to store a unique identifier or key for the
19
- * data being stored in the data structure. It helps in quickly accessing or retrieving the
20
- * associated value in the data structure.
21
- * @param {V[]} value - The `value` parameter in the constructor represents an array of values of
22
- * type `V`.
30
+ * Create an AVLTreeMultiMap node with a value bucket.
31
+ * @remarks Time O(1), Space O(1)
32
+ * @param key - Key of the node.
33
+ * @param value - Initial array of values.
34
+ * @returns New AVLTreeMultiMapNode instance.
23
35
  */
24
36
  constructor(key: K, value: V[]) {
25
37
  super(key, value);
@@ -27,10 +39,21 @@ export class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> {
27
39
 
28
40
  override _left?: AVLTreeMultiMapNode<K, V> | null | undefined = undefined;
29
41
 
42
+ /**
43
+ * Get the left child pointer.
44
+ * @remarks Time O(1), Space O(1)
45
+ * @returns Left child node, or null/undefined.
46
+ */
30
47
  override get left(): AVLTreeMultiMapNode<K, V> | null | undefined {
31
48
  return this._left;
32
49
  }
33
50
 
51
+ /**
52
+ * Set the left child and update its parent pointer.
53
+ * @remarks Time O(1), Space O(1)
54
+ * @param v - New left child node, or null/undefined.
55
+ * @returns void
56
+ */
34
57
  override set left(v: AVLTreeMultiMapNode<K, V> | null | undefined) {
35
58
  if (v) {
36
59
  v.parent = this;
@@ -40,10 +63,21 @@ export class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> {
40
63
 
41
64
  override _right?: AVLTreeMultiMapNode<K, V> | null | undefined = undefined;
42
65
 
66
+ /**
67
+ * Get the right child pointer.
68
+ * @remarks Time O(1), Space O(1)
69
+ * @returns Right child node, or null/undefined.
70
+ */
43
71
  override get right(): AVLTreeMultiMapNode<K, V> | null | undefined {
44
72
  return this._right;
45
73
  }
46
74
 
75
+ /**
76
+ * Set the right child and update its parent pointer.
77
+ * @remarks Time O(1), Space O(1)
78
+ * @param v - New right child node, or null/undefined.
79
+ * @returns void
80
+ */
47
81
  override set right(v: AVLTreeMultiMapNode<K, V> | null | undefined) {
48
82
  if (v) {
49
83
  v.parent = this;
@@ -53,22 +87,22 @@ export class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> {
53
87
  }
54
88
 
55
89
  /**
56
- *
90
+ * AVL-tree–based multimap (key → array of values). Preserves O(log N) updates and supports map-like mode.
91
+ * @remarks Time O(1), Space O(1)
92
+ * @template K
93
+ * @template V
94
+ * @template R
57
95
  */
58
- export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR = object>
59
- extends AVLTree<K, V[], R, MK, MV[], MR>
60
- implements IBinaryTree<K, V[], R, MK, MV, MR>
96
+ export class AVLTreeMultiMap<K = any, V = any, R extends object = object>
97
+ extends AVLTree<K, V[], R>
98
+ implements IBinaryTree<K, V[], R>
61
99
  {
62
100
  /**
63
- * The constructor initializes an AVLTreeMultiMap with the provided keys, nodes, entries, or raw data
64
- * and options.
65
- * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
66
- * iterable that can contain either key-value pairs represented as `BTNRep<K, V[],
67
- * AVLTreeMultiMapNode<K, V>>` or raw data represented as `R`. This parameter is used to initialize
68
- * the AVLTreeMulti
69
- * @param [options] - The `options` parameter in the constructor is of type
70
- * `AVLTreeMultiMapOptions<K, V[], R>`. It is an optional parameter that allows you to specify
71
- * additional options for configuring the AVLTreeMultiMap instance.
101
+ * Create an AVLTreeMultiMap and optionally bulk-insert items.
102
+ * @remarks Time O(N log N), Space O(N)
103
+ * @param [keysNodesEntriesOrRaws] - Iterable of keys/nodes/entries/raw items to insert.
104
+ * @param [options] - Options for AVLTreeMultiMap (comparator, reverse, map mode).
105
+ * @returns New AVLTreeMultiMap instance.
72
106
  */
73
107
  constructor(
74
108
  keysNodesEntriesOrRaws: Iterable<
@@ -82,45 +116,7 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
82
116
  }
83
117
  }
84
118
 
85
- /**
86
- * Time Complexity: O(1)
87
- * Space Complexity: O(1)
88
- *
89
- * The function `createTree` in TypeScript overrides the creation of an AVLTreeMultiMap with
90
- * specified options.
91
- * @param [options] - The `options` parameter in the `createTree` function is of type
92
- * `AVLTreeMultiMapOptions<K, V[], R>`. This means it is an object that can have properties of type
93
- * `K`, `V[]`, and `R`. The function creates a new `AVL
94
- * @returns The `createTree` method is returning a new instance of `AVLTreeMultiMap` with the
95
- * provided options.
96
- */
97
- override createTree(options?: AVLTreeMultiMapOptions<K, V[], R>) {
98
- return new AVLTreeMultiMap<K, V, R, MK, MV, MR>([], {
99
- iterationType: this.iterationType,
100
- specifyComparable: this._specifyComparable,
101
- toEntryFn: this._toEntryFn,
102
- isReverse: this._isReverse,
103
- isMapMode: this._isMapMode,
104
- ...options
105
- });
106
- }
107
-
108
- /**
109
- * Time Complexity: O(1)
110
- * Space Complexity: O(1)
111
- *
112
- * The `createNode` function in TypeScript overrides the default implementation to create a new
113
- * AVLTreeMultiMapNode with a specified key and value array.
114
- * @param {K} key - The `key` parameter represents the key of the node being created in the
115
- * AVLTreeMultiMap.
116
- * @param {V[]} value - The `value` parameter in the `createNode` method represents an array of
117
- * values associated with a specific key in the AVLTreeMultiMapNode. If no value is provided when
118
- * calling the method, an empty array `[]` is used as the default value.
119
- * @returns An AVLTreeMultiMapNode object is being returned, with the specified key and value. If the
120
- * AVLTreeMultiMap is in map mode, an empty array is used as the value, otherwise the provided value
121
- * array is used.
122
- */
123
- override createNode(key: K, value: V[] = []): AVLTreeMultiMapNode<K, V> {
119
+ override _createNode(key: K, value: V[] = []): AVLTreeMultiMapNode<K, V> {
124
120
  return new AVLTreeMultiMapNode<K, V>(key, this._isMapMode ? [] : value);
125
121
  }
126
122
 
@@ -131,22 +127,14 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
131
127
  override add(key: K, value: V): boolean;
132
128
 
133
129
  /**
134
- * Time Complexity: O(log n)
135
- * Space Complexity: O(log n)
136
- *
137
- * The function `add` in this TypeScript code overrides the superclass method to add key-value pairs
138
- * to an AVLTreeMultiMap, handling different input types and scenarios.
139
- * @param [key] - The `key` parameter in the `override add` method represents the key of the entry to
140
- * be added to the AVLTreeMultiMap. It can be of type `K`, which is the key type of the map. The key
141
- * can be a single key value, a node of the AVLTree
142
- * @param {V[]} [values] - The `values` parameter in the `add` method represents an array of values
143
- * that you want to add to the AVLTreeMultiMap. It can contain one or more values associated with a
144
- * specific key.
145
- * @returns The `add` method is returning a boolean value, which indicates whether the operation was
146
- * successful or not.
130
+ * Insert a value or a list of values into the multimap. If the key exists, values are appended.
131
+ * @remarks Time O(log N + M), Space O(1)
132
+ * @param keyNodeOrEntry - Key, node, or [key, values] entry.
133
+ * @param [value] - Single value to add when a bare key is provided.
134
+ * @returns True if inserted or appended; false if ignored.
147
135
  */
148
136
  override add(
149
- keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K,
137
+ keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined,
150
138
  value?: V
151
139
  ): boolean {
152
140
  if (this.isRealNode(keyNodeOrEntry)) return super.add(keyNodeOrEntry);
@@ -197,24 +185,14 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
197
185
  }
198
186
 
199
187
  /**
200
- * Time Complexity: O(log n)
201
- * Space Complexity: O(log n)
202
- *
203
- * The function `deleteValue` removes a specific value from a key in an AVLTreeMultiMap data
204
- * structure and deletes the entire node if no values are left for that key.
205
- * @param {K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K} keyNodeOrEntry - The `keyNodeOrEntry`
206
- * parameter in the `deleteValue` function can be either a `BTNRep` object representing a key-value
207
- * pair in the AVLTreeMultiMapNode, or just the key itself.
208
- * @param {V} value - The `value` parameter in the `deleteValue` function represents the specific
209
- * value that you want to delete from the multi-map data structure associated with a particular key.
210
- * The function checks if the value exists in the array of values associated with the key, and if
211
- * found, removes it from the array.
212
- * @returns The `deleteValue` function returns a boolean value. It returns `true` if the specified
213
- * `value` was successfully deleted from the array of values associated with the `keyNodeOrEntry`. If
214
- * the value was not found in the array, it returns `false`.
188
+ * Delete a single value from the bucket at a given key. Removes the key if the bucket becomes empty.
189
+ * @remarks Time O(log N), Space O(1)
190
+ * @param keyNodeOrEntry - Key, node, or [key, values] entry to locate the bucket.
191
+ * @param value - Value to remove from the bucket.
192
+ * @returns True if the value was removed; false if not found.
215
193
  */
216
194
  deleteValue(
217
- keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K,
195
+ keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined,
218
196
  value: V
219
197
  ): boolean {
220
198
  const values = this.get(keyNodeOrEntry);
@@ -223,7 +201,6 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
223
201
  if (index === -1) return false;
224
202
  values.splice(index, 1);
225
203
 
226
- // If no values left, remove the entire node
227
204
  if (values.length === 0) this.delete(keyNodeOrEntry);
228
205
 
229
206
  return true;
@@ -232,16 +209,130 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
232
209
  }
233
210
 
234
211
  /**
235
- * Time Complexity: O(n)
236
- * Space Complexity: O(n)
237
- *
238
- * The function `clone` overrides the default cloning behavior to create a deep copy of a tree
239
- * structure.
240
- * @returns A cloned tree object is being returned.
212
+ * Rebuild the tree into a perfectly balanced form using in-order nodes.
213
+ * @remarks Time O(N), Space O(N)
214
+ * @param [iterationType] - Traversal style to use when constructing the balanced tree.
215
+ * @returns True if rebalancing succeeded (tree not empty).
216
+ */
217
+ override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
218
+ const nodes = this.dfs(node => node, 'IN', false, this._root, iterationType);
219
+ const n = nodes.length;
220
+ if (n === 0) return false;
221
+
222
+ this._clearNodes();
223
+
224
+ const build = (l: number, r: number, parent?: any): any | undefined => {
225
+ if (l > r) return undefined;
226
+ const m = l + ((r - l) >> 1);
227
+ const root = nodes[m];
228
+ root.left = build(l, m - 1, root);
229
+ root.right = build(m + 1, r, root);
230
+ root.parent = parent;
231
+ const lh = root.left ? root.left.height : -1;
232
+ const rh = root.right ? root.right.height : -1;
233
+ root.height = Math.max(lh, rh) + 1;
234
+ return root;
235
+ };
236
+
237
+ const newRoot = build(0, n - 1, undefined);
238
+ this._setRoot(newRoot);
239
+ this._size = n;
240
+ return true;
241
+ }
242
+
243
+ /**
244
+ * Create a new tree by mapping each [key, values] bucket.
245
+ * @remarks Time O(N log N), Space O(N)
246
+ * @template MK
247
+ * @template MVArr
248
+ * @template MR
249
+ * @param callback - Function mapping (key, values, index, tree) → [newKey, newValue].
250
+ * @param [options] - Options for the output tree.
251
+ * @param [thisArg] - Value for `this` inside the callback.
252
+ * @returns A new AVLTreeMultiMap when mapping to array values; see overloads.
253
+ */
254
+ override map<MK = K, MVArr extends unknown[] = V[], MR extends object = object>(
255
+ callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>,
256
+ options?: Partial<AVLTreeOptions<MK, MVArr, MR>>,
257
+ thisArg?: unknown
258
+ ): AVLTreeMultiMap<MK, ElemOf<MVArr>, MR>;
259
+
260
+ /**
261
+ * Create a new tree by mapping each [key, values] bucket.
262
+ * @remarks Time O(N log N), Space O(N)
263
+ * @template MK
264
+ * @template MV
265
+ * @template MR
266
+ * @param callback - Function mapping (key, values, index, tree) → [newKey, newValue].
267
+ * @param [options] - Options for the output tree.
268
+ * @param [thisArg] - Value for `this` inside the callback.
269
+ * @returns A new AVLTree when mapping to non-array values; see overloads.
270
+ */
271
+ override map<MK = K, MV = V[], MR extends object = object>(
272
+ callback: EntryCallback<K, V[] | undefined, [MK, MV]>,
273
+ options?: Partial<AVLTreeOptions<MK, MV, MR>>,
274
+ thisArg?: unknown
275
+ ): AVLTree<MK, MV, MR>;
276
+
277
+ /**
278
+ * Create a new tree by mapping each [key, values] bucket.
279
+ * @remarks Time O(N log N), Space O(N)
280
+ * @template MK
281
+ * @template MV
282
+ * @template MR
283
+ * @param callback - Function mapping (key, values, index, tree) → [newKey, newValue].
284
+ * @param [options] - Options for the output tree.
285
+ * @param [thisArg] - Value for `this` inside the callback.
286
+ * @returns The mapped AVLTree or AVLTreeMultiMap depending on MV; see overloads.
287
+ */
288
+ override map<MK, MV, MR extends object>(
289
+ callback: EntryCallback<K, V[] | undefined, [MK, MV]>,
290
+ options?: Partial<AVLTreeOptions<MK, MV, MR>>,
291
+ thisArg?: unknown
292
+ ): AVLTree<MK, MV, MR> {
293
+ const out = this._createLike<MK, MV, MR>([], options);
294
+ let i = 0;
295
+ for (const [k, v] of this) out.add(callback.call(thisArg, k, v, i++, this));
296
+ return out;
297
+ }
298
+
299
+ /**
300
+ * (Protected) Create an empty instance of the same concrete class.
301
+ * @remarks Time O(1), Space O(1)
302
+ * @template TK
303
+ * @template TV
304
+ * @template TR
305
+ * @param [options] - Optional constructor options for the like-kind instance.
306
+ * @returns An empty like-kind instance.
307
+ */
308
+ protected override _createInstance<TK = K, TV = V, TR extends object = R>(
309
+ options?: Partial<AVLTreeOptions<TK, TV, TR>>
310
+ ): this {
311
+ const Ctor = this.constructor as unknown as new (
312
+ iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
313
+ opts?: AVLTreeOptions<TK, TV, TR>
314
+ ) => AVLTree<TK, TV, TR>;
315
+ return new Ctor([], { ...(this._snapshotOptions?.<TK, TV, TR>() ?? {}), ...(options ?? {}) }) as unknown as this;
316
+ }
317
+
318
+ /**
319
+ * (Protected) Create a like-kind instance and seed it from an iterable.
320
+ * @remarks Time O(N log N), Space O(N)
321
+ * @template TK
322
+ * @template TV
323
+ * @template TR
324
+ * @param iter - Iterable used to seed the new tree.
325
+ * @param [options] - Options merged with the current snapshot.
326
+ * @returns A like-kind AVLTree built from the iterable.
241
327
  */
242
- override clone() {
243
- const cloned = this.createTree();
244
- this._clone(cloned);
245
- return cloned;
328
+ protected override _createLike<TK = K, TV = V, TR extends object = R>(
329
+ iter: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
330
+ options?: Partial<AVLTreeOptions<TK, TV, TR>>
331
+ ): AVLTree<TK, TV, TR> {
332
+ const Ctor = this.constructor as unknown as new (
333
+ iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
334
+ opts?: AVLTreeOptions<TK, TV, TR>
335
+ ) => AVLTree<TK, TV, TR>;
336
+ return new Ctor(iter, { ...(this._snapshotOptions?.<TK, TV, TR>() ?? {}), ...(options ?? {}) });
246
337
  }
247
338
  }