heap-typed 2.1.0 → 2.1.2

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 (281) hide show
  1. package/dist/cjs/index.cjs +1020 -0
  2. package/dist/cjs/index.cjs.map +1 -0
  3. package/dist/esm/index.mjs +1012 -0
  4. package/dist/esm/index.mjs.map +1 -0
  5. package/dist/types/data-structures/base/index.d.ts +2 -1
  6. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +182 -2
  7. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +135 -2
  8. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +291 -2
  9. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -1
  10. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +754 -29
  11. package/dist/types/data-structures/binary-tree/bst.d.ts +413 -12
  12. package/dist/types/data-structures/binary-tree/index.d.ts +3 -2
  13. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +208 -3
  14. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -1
  15. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +190 -2
  16. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +270 -2
  17. package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -14
  18. package/dist/types/data-structures/graph/directed-graph.d.ts +207 -1
  19. package/dist/types/data-structures/graph/index.d.ts +2 -1
  20. package/dist/types/data-structures/graph/map-graph.d.ts +78 -1
  21. package/dist/types/data-structures/graph/undirected-graph.d.ts +188 -1
  22. package/dist/types/data-structures/hash/hash-map.d.ts +345 -19
  23. package/dist/types/data-structures/hash/index.d.ts +0 -1
  24. package/dist/types/data-structures/heap/heap.d.ts +503 -5
  25. package/dist/types/data-structures/heap/index.d.ts +2 -0
  26. package/dist/types/data-structures/heap/max-heap.d.ts +32 -1
  27. package/dist/types/data-structures/heap/min-heap.d.ts +33 -1
  28. package/dist/types/data-structures/index.d.ts +7 -7
  29. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +769 -2
  30. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +451 -2
  31. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -4
  32. package/dist/types/data-structures/matrix/index.d.ts +1 -1
  33. package/dist/types/data-structures/matrix/matrix.d.ts +168 -7
  34. package/dist/types/data-structures/matrix/navigator.d.ts +54 -13
  35. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -1
  36. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -1
  37. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -2
  38. package/dist/types/data-structures/queue/deque.d.ts +431 -4
  39. package/dist/types/data-structures/queue/queue.d.ts +308 -4
  40. package/dist/types/data-structures/stack/stack.d.ts +306 -2
  41. package/dist/types/data-structures/tree/tree.d.ts +62 -1
  42. package/dist/types/data-structures/trie/trie.d.ts +350 -4
  43. package/dist/types/index.d.ts +11 -2
  44. package/dist/{interfaces → types/interfaces}/binary-tree.d.ts +3 -3
  45. package/dist/types/types/data-structures/base/index.d.ts +1 -0
  46. package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
  47. package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
  48. package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
  49. package/dist/types/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
  50. package/dist/types/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
  51. package/dist/types/types/data-structures/binary-tree/bst.d.ts +12 -0
  52. package/dist/{data-structures → types/types/data-structures}/binary-tree/index.d.ts +2 -3
  53. package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
  54. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
  55. package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
  56. package/dist/types/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
  57. package/dist/types/types/data-structures/graph/abstract-graph.d.ts +14 -0
  58. package/dist/types/types/data-structures/graph/directed-graph.d.ts +1 -0
  59. package/dist/{data-structures → types/types/data-structures}/graph/index.d.ts +1 -2
  60. package/dist/types/types/data-structures/graph/map-graph.d.ts +1 -0
  61. package/dist/types/types/data-structures/graph/undirected-graph.d.ts +1 -0
  62. package/dist/types/types/data-structures/hash/hash-map.d.ts +19 -0
  63. package/dist/types/types/data-structures/hash/index.d.ts +2 -0
  64. package/dist/types/types/data-structures/heap/heap.d.ts +5 -0
  65. package/dist/types/types/data-structures/heap/index.d.ts +1 -0
  66. package/dist/types/types/data-structures/heap/max-heap.d.ts +1 -0
  67. package/dist/types/types/data-structures/heap/min-heap.d.ts +1 -0
  68. package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -0
  69. package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +2 -0
  70. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +4 -0
  71. package/dist/types/types/data-structures/matrix/matrix.d.ts +7 -0
  72. package/dist/types/types/data-structures/matrix/navigator.d.ts +14 -0
  73. package/dist/types/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
  74. package/dist/types/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
  75. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +2 -0
  76. package/dist/types/types/data-structures/queue/deque.d.ts +4 -0
  77. package/dist/types/types/data-structures/queue/queue.d.ts +4 -0
  78. package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
  79. package/dist/types/types/data-structures/tree/tree.d.ts +1 -0
  80. package/dist/types/types/data-structures/trie/trie.d.ts +4 -0
  81. package/dist/types/types/index.d.ts +3 -0
  82. package/dist/types/types/utils/index.d.ts +2 -0
  83. package/dist/types/types/utils/utils.d.ts +22 -0
  84. package/dist/types/utils/index.d.ts +1 -1
  85. package/dist/types/utils/utils.d.ts +209 -22
  86. package/dist/umd/heap-typed.js +1023 -0
  87. package/dist/umd/heap-typed.js.map +1 -0
  88. package/dist/umd/heap-typed.min.js +22 -0
  89. package/dist/umd/heap-typed.min.js.map +1 -0
  90. package/package.json +25 -5
  91. package/src/data-structures/binary-tree/avl-tree-counter.ts +8 -11
  92. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +6 -11
  93. package/src/data-structures/binary-tree/avl-tree.ts +6 -8
  94. package/src/data-structures/binary-tree/binary-tree.ts +13 -15
  95. package/src/data-structures/binary-tree/bst.ts +6 -11
  96. package/src/data-structures/binary-tree/red-black-tree.ts +6 -11
  97. package/src/data-structures/binary-tree/tree-counter.ts +8 -13
  98. package/src/data-structures/binary-tree/tree-multi-map.ts +6 -11
  99. package/src/data-structures/heap/heap.ts +5 -5
  100. package/src/data-structures/linked-list/singly-linked-list.ts +2 -2
  101. package/src/interfaces/binary-tree.ts +3 -3
  102. package/tsconfig.base.json +23 -0
  103. package/tsconfig.json +8 -34
  104. package/tsconfig.test.json +8 -0
  105. package/tsconfig.types.json +15 -0
  106. package/tsup.config.js +28 -0
  107. package/tsup.node.config.js +37 -0
  108. package/dist/common/index.js +0 -28
  109. package/dist/constants/index.js +0 -8
  110. package/dist/data-structures/base/index.d.ts +0 -2
  111. package/dist/data-structures/base/index.js +0 -18
  112. package/dist/data-structures/base/iterable-element-base.js +0 -243
  113. package/dist/data-structures/base/iterable-entry-base.js +0 -183
  114. package/dist/data-structures/base/linear-base.js +0 -415
  115. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +0 -182
  116. package/dist/data-structures/binary-tree/avl-tree-counter.js +0 -374
  117. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -135
  118. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +0 -250
  119. package/dist/data-structures/binary-tree/avl-tree.d.ts +0 -291
  120. package/dist/data-structures/binary-tree/avl-tree.js +0 -611
  121. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -174
  122. package/dist/data-structures/binary-tree/binary-indexed-tree.js +0 -294
  123. package/dist/data-structures/binary-tree/binary-tree.d.ts +0 -754
  124. package/dist/data-structures/binary-tree/binary-tree.js +0 -1925
  125. package/dist/data-structures/binary-tree/bst.d.ts +0 -413
  126. package/dist/data-structures/binary-tree/bst.js +0 -903
  127. package/dist/data-structures/binary-tree/index.js +0 -26
  128. package/dist/data-structures/binary-tree/red-black-tree.d.ts +0 -208
  129. package/dist/data-structures/binary-tree/red-black-tree.js +0 -546
  130. package/dist/data-structures/binary-tree/segment-tree.d.ts +0 -160
  131. package/dist/data-structures/binary-tree/segment-tree.js +0 -297
  132. package/dist/data-structures/binary-tree/tree-counter.d.ts +0 -190
  133. package/dist/data-structures/binary-tree/tree-counter.js +0 -413
  134. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +0 -270
  135. package/dist/data-structures/binary-tree/tree-multi-map.js +0 -384
  136. package/dist/data-structures/graph/abstract-graph.d.ts +0 -340
  137. package/dist/data-structures/graph/abstract-graph.js +0 -896
  138. package/dist/data-structures/graph/directed-graph.d.ts +0 -207
  139. package/dist/data-structures/graph/directed-graph.js +0 -525
  140. package/dist/data-structures/graph/index.js +0 -20
  141. package/dist/data-structures/graph/map-graph.d.ts +0 -78
  142. package/dist/data-structures/graph/map-graph.js +0 -107
  143. package/dist/data-structures/graph/undirected-graph.d.ts +0 -188
  144. package/dist/data-structures/graph/undirected-graph.js +0 -424
  145. package/dist/data-structures/hash/hash-map.d.ts +0 -345
  146. package/dist/data-structures/hash/hash-map.js +0 -692
  147. package/dist/data-structures/hash/index.d.ts +0 -1
  148. package/dist/data-structures/hash/index.js +0 -17
  149. package/dist/data-structures/heap/heap.d.ts +0 -503
  150. package/dist/data-structures/heap/heap.js +0 -901
  151. package/dist/data-structures/heap/index.d.ts +0 -3
  152. package/dist/data-structures/heap/index.js +0 -19
  153. package/dist/data-structures/heap/max-heap.d.ts +0 -32
  154. package/dist/data-structures/heap/max-heap.js +0 -40
  155. package/dist/data-structures/heap/min-heap.d.ts +0 -33
  156. package/dist/data-structures/heap/min-heap.js +0 -31
  157. package/dist/data-structures/index.js +0 -28
  158. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -769
  159. package/dist/data-structures/linked-list/doubly-linked-list.js +0 -1111
  160. package/dist/data-structures/linked-list/index.js +0 -19
  161. package/dist/data-structures/linked-list/singly-linked-list.d.ts +0 -451
  162. package/dist/data-structures/linked-list/singly-linked-list.js +0 -850
  163. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -27
  164. package/dist/data-structures/linked-list/skip-linked-list.js +0 -144
  165. package/dist/data-structures/matrix/index.js +0 -18
  166. package/dist/data-structures/matrix/matrix.d.ts +0 -168
  167. package/dist/data-structures/matrix/matrix.js +0 -448
  168. package/dist/data-structures/matrix/navigator.d.ts +0 -55
  169. package/dist/data-structures/matrix/navigator.js +0 -111
  170. package/dist/data-structures/priority-queue/index.js +0 -19
  171. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +0 -27
  172. package/dist/data-structures/priority-queue/max-priority-queue.js +0 -34
  173. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +0 -26
  174. package/dist/data-structures/priority-queue/min-priority-queue.js +0 -24
  175. package/dist/data-structures/priority-queue/priority-queue.d.ts +0 -15
  176. package/dist/data-structures/priority-queue/priority-queue.js +0 -20
  177. package/dist/data-structures/queue/deque.d.ts +0 -431
  178. package/dist/data-structures/queue/deque.js +0 -879
  179. package/dist/data-structures/queue/index.js +0 -18
  180. package/dist/data-structures/queue/queue.d.ts +0 -308
  181. package/dist/data-structures/queue/queue.js +0 -473
  182. package/dist/data-structures/stack/index.js +0 -17
  183. package/dist/data-structures/stack/stack.d.ts +0 -306
  184. package/dist/data-structures/stack/stack.js +0 -401
  185. package/dist/data-structures/tree/index.js +0 -17
  186. package/dist/data-structures/tree/tree.d.ts +0 -62
  187. package/dist/data-structures/tree/tree.js +0 -107
  188. package/dist/data-structures/trie/index.js +0 -17
  189. package/dist/data-structures/trie/trie.d.ts +0 -350
  190. package/dist/data-structures/trie/trie.js +0 -610
  191. package/dist/index.d.ts +0 -12
  192. package/dist/index.js +0 -28
  193. package/dist/interfaces/binary-tree.js +0 -2
  194. package/dist/interfaces/doubly-linked-list.js +0 -2
  195. package/dist/interfaces/graph.js +0 -2
  196. package/dist/interfaces/heap.js +0 -2
  197. package/dist/interfaces/index.js +0 -24
  198. package/dist/interfaces/navigator.js +0 -2
  199. package/dist/interfaces/priority-queue.js +0 -2
  200. package/dist/interfaces/segment-tree.js +0 -2
  201. package/dist/interfaces/singly-linked-list.js +0 -2
  202. package/dist/types/common.js +0 -2
  203. package/dist/types/data-structures/base/base.js +0 -2
  204. package/dist/types/data-structures/base/index.js +0 -17
  205. package/dist/types/data-structures/binary-tree/avl-tree-counter.js +0 -2
  206. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -2
  207. package/dist/types/data-structures/binary-tree/avl-tree.js +0 -2
  208. package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +0 -2
  209. package/dist/types/data-structures/binary-tree/binary-tree.js +0 -2
  210. package/dist/types/data-structures/binary-tree/bst.js +0 -2
  211. package/dist/types/data-structures/binary-tree/index.js +0 -25
  212. package/dist/types/data-structures/binary-tree/red-black-tree.js +0 -2
  213. package/dist/types/data-structures/binary-tree/segment-tree.js +0 -2
  214. package/dist/types/data-structures/binary-tree/tree-counter.js +0 -2
  215. package/dist/types/data-structures/binary-tree/tree-multi-map.js +0 -2
  216. package/dist/types/data-structures/graph/abstract-graph.js +0 -2
  217. package/dist/types/data-structures/graph/directed-graph.js +0 -2
  218. package/dist/types/data-structures/graph/index.js +0 -19
  219. package/dist/types/data-structures/graph/map-graph.js +0 -2
  220. package/dist/types/data-structures/graph/undirected-graph.js +0 -2
  221. package/dist/types/data-structures/hash/hash-map.js +0 -2
  222. package/dist/types/data-structures/hash/index.js +0 -17
  223. package/dist/types/data-structures/heap/heap.js +0 -2
  224. package/dist/types/data-structures/heap/index.js +0 -17
  225. package/dist/types/data-structures/heap/max-heap.js +0 -2
  226. package/dist/types/data-structures/heap/min-heap.js +0 -2
  227. package/dist/types/data-structures/index.js +0 -28
  228. package/dist/types/data-structures/linked-list/doubly-linked-list.js +0 -2
  229. package/dist/types/data-structures/linked-list/index.js +0 -19
  230. package/dist/types/data-structures/linked-list/singly-linked-list.js +0 -2
  231. package/dist/types/data-structures/linked-list/skip-linked-list.js +0 -2
  232. package/dist/types/data-structures/matrix/index.js +0 -18
  233. package/dist/types/data-structures/matrix/matrix.js +0 -2
  234. package/dist/types/data-structures/matrix/navigator.js +0 -2
  235. package/dist/types/data-structures/priority-queue/index.js +0 -19
  236. package/dist/types/data-structures/priority-queue/max-priority-queue.js +0 -2
  237. package/dist/types/data-structures/priority-queue/min-priority-queue.js +0 -2
  238. package/dist/types/data-structures/priority-queue/priority-queue.js +0 -2
  239. package/dist/types/data-structures/queue/deque.js +0 -2
  240. package/dist/types/data-structures/queue/index.js +0 -18
  241. package/dist/types/data-structures/queue/queue.js +0 -2
  242. package/dist/types/data-structures/stack/index.js +0 -17
  243. package/dist/types/data-structures/stack/stack.js +0 -2
  244. package/dist/types/data-structures/tree/index.js +0 -17
  245. package/dist/types/data-structures/tree/tree.js +0 -2
  246. package/dist/types/data-structures/trie/index.js +0 -17
  247. package/dist/types/data-structures/trie/trie.js +0 -2
  248. package/dist/types/index.js +0 -19
  249. package/dist/types/utils/index.js +0 -18
  250. package/dist/types/utils/utils.js +0 -2
  251. package/dist/types/utils/validate-type.js +0 -2
  252. package/dist/utils/index.d.ts +0 -2
  253. package/dist/utils/index.js +0 -18
  254. package/dist/utils/number.js +0 -24
  255. package/dist/utils/utils.d.ts +0 -209
  256. package/dist/utils/utils.js +0 -353
  257. package/dist/{common → types/common}/index.d.ts +0 -0
  258. package/dist/{constants → types/constants}/index.d.ts +0 -0
  259. package/dist/{data-structures → types/data-structures}/base/iterable-element-base.d.ts +0 -0
  260. package/dist/{data-structures → types/data-structures}/base/iterable-entry-base.d.ts +0 -0
  261. package/dist/{data-structures → types/data-structures}/base/linear-base.d.ts +0 -0
  262. package/dist/{interfaces → types/interfaces}/doubly-linked-list.d.ts +0 -0
  263. package/dist/{interfaces → types/interfaces}/graph.d.ts +0 -0
  264. package/dist/{interfaces → types/interfaces}/heap.d.ts +0 -0
  265. package/dist/{interfaces → types/interfaces}/index.d.ts +0 -0
  266. package/dist/{interfaces → types/interfaces}/navigator.d.ts +0 -0
  267. package/dist/{interfaces → types/interfaces}/priority-queue.d.ts +0 -0
  268. package/dist/{interfaces → types/interfaces}/segment-tree.d.ts +0 -0
  269. package/dist/{interfaces → types/interfaces}/singly-linked-list.d.ts +0 -0
  270. package/dist/types/{common.d.ts → types/common.d.ts} +0 -0
  271. package/dist/types/{data-structures → types/data-structures}/base/base.d.ts +0 -0
  272. package/dist/{data-structures → types/types/data-structures}/index.d.ts +7 -7
  273. package/dist/{data-structures → types/types/data-structures}/linked-list/index.d.ts +0 -0
  274. package/dist/{data-structures → types/types/data-structures}/matrix/index.d.ts +1 -1
  275. /package/dist/{data-structures → types/types/data-structures}/priority-queue/index.d.ts +0 -0
  276. /package/dist/{data-structures → types/types/data-structures}/queue/index.d.ts +0 -0
  277. /package/dist/{data-structures → types/types/data-structures}/stack/index.d.ts +0 -0
  278. /package/dist/{data-structures → types/types/data-structures}/tree/index.d.ts +0 -0
  279. /package/dist/{data-structures → types/types/data-structures}/trie/index.d.ts +0 -0
  280. /package/dist/types/{utils → types/utils}/validate-type.d.ts +0 -0
  281. /package/dist/{utils → types/utils}/number.d.ts +0 -0
@@ -1,250 +0,0 @@
1
- "use strict";
2
- /**
3
- * data-structure-typed
4
- *
5
- * @author Pablo Zeng
6
- * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
7
- * @license MIT License
8
- */
9
- Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.AVLTreeMultiMap = exports.AVLTreeMultiMapNode = void 0;
11
- const avl_tree_1 = require("./avl-tree");
12
- /**
13
- * Node used by AVLTreeMultiMap; stores the key with a bucket of values (array).
14
- * @remarks Time O(1), Space O(1)
15
- * @template K
16
- * @template V
17
- */
18
- class AVLTreeMultiMapNode extends avl_tree_1.AVLTreeNode {
19
- /**
20
- * Create an AVLTreeMultiMap node with a value bucket.
21
- * @remarks Time O(1), Space O(1)
22
- * @param key - Key of the node.
23
- * @param value - Initial array of values.
24
- * @returns New AVLTreeMultiMapNode instance.
25
- */
26
- constructor(key, value) {
27
- super(key, value);
28
- this.parent = undefined;
29
- this._left = undefined;
30
- this._right = undefined;
31
- }
32
- /**
33
- * Get the left child pointer.
34
- * @remarks Time O(1), Space O(1)
35
- * @returns Left child node, or null/undefined.
36
- */
37
- get left() {
38
- return this._left;
39
- }
40
- /**
41
- * Set the left child and update its parent pointer.
42
- * @remarks Time O(1), Space O(1)
43
- * @param v - New left child node, or null/undefined.
44
- * @returns void
45
- */
46
- set left(v) {
47
- if (v) {
48
- v.parent = this;
49
- }
50
- this._left = v;
51
- }
52
- /**
53
- * Get the right child pointer.
54
- * @remarks Time O(1), Space O(1)
55
- * @returns Right child node, or null/undefined.
56
- */
57
- get right() {
58
- return this._right;
59
- }
60
- /**
61
- * Set the right child and update its parent pointer.
62
- * @remarks Time O(1), Space O(1)
63
- * @param v - New right child node, or null/undefined.
64
- * @returns void
65
- */
66
- set right(v) {
67
- if (v) {
68
- v.parent = this;
69
- }
70
- this._right = v;
71
- }
72
- }
73
- exports.AVLTreeMultiMapNode = AVLTreeMultiMapNode;
74
- /**
75
- * AVL-tree–based multimap (key → array of values). Preserves O(log N) updates and supports map-like mode.
76
- * @remarks Time O(1), Space O(1)
77
- * @template K
78
- * @template V
79
- * @template R
80
- */
81
- class AVLTreeMultiMap extends avl_tree_1.AVLTree {
82
- /**
83
- * Create an AVLTreeMultiMap and optionally bulk-insert items.
84
- * @remarks Time O(N log N), Space O(N)
85
- * @param [keysNodesEntriesOrRaws] - Iterable of keys/nodes/entries/raw items to insert.
86
- * @param [options] - Options for AVLTreeMultiMap (comparator, reverse, map mode).
87
- * @returns New AVLTreeMultiMap instance.
88
- */
89
- constructor(keysNodesEntriesOrRaws = [], options) {
90
- super([], Object.assign(Object.assign({}, options), { isMapMode: true }));
91
- if (keysNodesEntriesOrRaws) {
92
- this.addMany(keysNodesEntriesOrRaws);
93
- }
94
- }
95
- _createNode(key, value = []) {
96
- return new AVLTreeMultiMapNode(key, this._isMapMode ? [] : value);
97
- }
98
- /**
99
- * Insert a value or a list of values into the multimap. If the key exists, values are appended.
100
- * @remarks Time O(log N + M), Space O(1)
101
- * @param keyNodeOrEntry - Key, node, or [key, values] entry.
102
- * @param [value] - Single value to add when a bare key is provided.
103
- * @returns True if inserted or appended; false if ignored.
104
- */
105
- add(keyNodeOrEntry, value) {
106
- if (this.isRealNode(keyNodeOrEntry))
107
- return super.add(keyNodeOrEntry);
108
- const _commonAdd = (key, values) => {
109
- if (key === undefined || key === null)
110
- return false;
111
- const _addToValues = () => {
112
- const existingValues = this.get(key);
113
- if (existingValues !== undefined && values !== undefined) {
114
- for (const value of values)
115
- existingValues.push(value);
116
- return true;
117
- }
118
- return false;
119
- };
120
- const _addByNode = () => {
121
- const existingNode = this.getNode(key);
122
- if (this.isRealNode(existingNode)) {
123
- const existingValues = this.get(existingNode);
124
- if (existingValues === undefined) {
125
- super.add(key, values);
126
- return true;
127
- }
128
- if (values !== undefined) {
129
- for (const value of values)
130
- existingValues.push(value);
131
- return true;
132
- }
133
- else {
134
- return false;
135
- }
136
- }
137
- else {
138
- return super.add(key, values);
139
- }
140
- };
141
- if (this._isMapMode) {
142
- return _addByNode() || _addToValues();
143
- }
144
- return _addToValues() || _addByNode();
145
- };
146
- if (this.isEntry(keyNodeOrEntry)) {
147
- const [key, values] = keyNodeOrEntry;
148
- return _commonAdd(key, value !== undefined ? [value] : values);
149
- }
150
- return _commonAdd(keyNodeOrEntry, value !== undefined ? [value] : undefined);
151
- }
152
- /**
153
- * Delete a single value from the bucket at a given key. Removes the key if the bucket becomes empty.
154
- * @remarks Time O(log N), Space O(1)
155
- * @param keyNodeOrEntry - Key, node, or [key, values] entry to locate the bucket.
156
- * @param value - Value to remove from the bucket.
157
- * @returns True if the value was removed; false if not found.
158
- */
159
- deleteValue(keyNodeOrEntry, value) {
160
- const values = this.get(keyNodeOrEntry);
161
- if (Array.isArray(values)) {
162
- const index = values.indexOf(value);
163
- if (index === -1)
164
- return false;
165
- values.splice(index, 1);
166
- if (values.length === 0)
167
- this.delete(keyNodeOrEntry);
168
- return true;
169
- }
170
- return false;
171
- }
172
- /**
173
- * Rebuild the tree into a perfectly balanced form using in-order nodes.
174
- * @remarks Time O(N), Space O(N)
175
- * @param [iterationType] - Traversal style to use when constructing the balanced tree.
176
- * @returns True if rebalancing succeeded (tree not empty).
177
- */
178
- perfectlyBalance(iterationType = this.iterationType) {
179
- const nodes = this.dfs(node => node, 'IN', false, this._root, iterationType);
180
- const n = nodes.length;
181
- if (n === 0)
182
- return false;
183
- this._clearNodes();
184
- const build = (l, r, parent) => {
185
- if (l > r)
186
- return undefined;
187
- const m = l + ((r - l) >> 1);
188
- const root = nodes[m];
189
- root.left = build(l, m - 1, root);
190
- root.right = build(m + 1, r, root);
191
- root.parent = parent;
192
- const lh = root.left ? root.left.height : -1;
193
- const rh = root.right ? root.right.height : -1;
194
- root.height = Math.max(lh, rh) + 1;
195
- return root;
196
- };
197
- const newRoot = build(0, n - 1, undefined);
198
- this._setRoot(newRoot);
199
- this._size = n;
200
- return true;
201
- }
202
- /**
203
- * Create a new tree by mapping each [key, values] bucket.
204
- * @remarks Time O(N log N), Space O(N)
205
- * @template MK
206
- * @template MV
207
- * @template MR
208
- * @param callback - Function mapping (key, values, index, tree) → [newKey, newValue].
209
- * @param [options] - Options for the output tree.
210
- * @param [thisArg] - Value for `this` inside the callback.
211
- * @returns The mapped AVLTree or AVLTreeMultiMap depending on MV; see overloads.
212
- */
213
- map(callback, options, thisArg) {
214
- const out = this._createLike([], options);
215
- let i = 0;
216
- for (const [k, v] of this)
217
- out.add(callback.call(thisArg, k, v, i++, this));
218
- return out;
219
- }
220
- /**
221
- * (Protected) Create an empty instance of the same concrete class.
222
- * @remarks Time O(1), Space O(1)
223
- * @template TK
224
- * @template TV
225
- * @template TR
226
- * @param [options] - Optional constructor options for the like-kind instance.
227
- * @returns An empty like-kind instance.
228
- */
229
- _createInstance(options) {
230
- var _a, _b;
231
- const Ctor = this.constructor;
232
- return new Ctor([], Object.assign(Object.assign({}, ((_b = (_a = this._snapshotOptions) === null || _a === void 0 ? void 0 : _a.call(this)) !== null && _b !== void 0 ? _b : {})), (options !== null && options !== void 0 ? options : {})));
233
- }
234
- /**
235
- * (Protected) Create a like-kind instance and seed it from an iterable.
236
- * @remarks Time O(N log N), Space O(N)
237
- * @template TK
238
- * @template TV
239
- * @template TR
240
- * @param iter - Iterable used to seed the new tree.
241
- * @param [options] - Options merged with the current snapshot.
242
- * @returns A like-kind AVLTree built from the iterable.
243
- */
244
- _createLike(iter = [], options) {
245
- var _a, _b;
246
- const Ctor = this.constructor;
247
- return new Ctor(iter, Object.assign(Object.assign({}, ((_b = (_a = this._snapshotOptions) === null || _a === void 0 ? void 0 : _a.call(this)) !== null && _b !== void 0 ? _b : {})), (options !== null && options !== void 0 ? options : {})));
248
- }
249
- }
250
- exports.AVLTreeMultiMap = AVLTreeMultiMap;
@@ -1,291 +0,0 @@
1
- /**
2
- * data-structure-typed
3
- *
4
- * @author Pablo Zeng
5
- * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
- * @license MIT License
7
- */
8
- import { BST, BSTNode } from './bst';
9
- import type { AVLTreeOptions, BinaryTreeDeleteResult, BinaryTreeOptions, BSTNOptKeyOrNode, EntryCallback, IterationType } from '../../types';
10
- import { BSTOptions } from '../../types';
11
- import { IBinaryTree } from '../../interfaces';
12
- /**
13
- * Represents a Node in an AVL (Adelson-Velsky and Landis) Tree.
14
- * It extends a BSTNode and ensures the 'height' property is maintained.
15
- *
16
- * @template K - The type of the key.
17
- * @template V - The type of the value.
18
- */
19
- export declare class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
20
- parent?: AVLTreeNode<K, V>;
21
- /**
22
- * Creates an instance of AVLTreeNode.
23
- * @remarks Time O(1), Space O(1)
24
- *
25
- * @param key - The key of the node.
26
- * @param [value] - The value associated with the key.
27
- */
28
- constructor(key: K, value?: V);
29
- _left?: AVLTreeNode<K, V> | null | undefined;
30
- /**
31
- * Gets the left child of the node.
32
- * @remarks Time O(1), Space O(1)
33
- *
34
- * @returns The left child.
35
- */
36
- get left(): AVLTreeNode<K, V> | null | undefined;
37
- /**
38
- * Sets the left child of the node and updates its parent reference.
39
- * @remarks Time O(1), Space O(1)
40
- *
41
- * @param v - The node to set as the left child.
42
- */
43
- set left(v: AVLTreeNode<K, V> | null | undefined);
44
- _right?: AVLTreeNode<K, V> | null | undefined;
45
- /**
46
- * Gets the right child of the node.
47
- * @remarks Time O(1), Space O(1)
48
- *
49
- * @returns The right child.
50
- */
51
- get right(): AVLTreeNode<K, V> | null | undefined;
52
- /**
53
- * Sets the right child of the node and updates its parent reference.
54
- * @remarks Time O(1), Space O(1)
55
- *
56
- * @param v - The node to set as the right child.
57
- */
58
- set right(v: AVLTreeNode<K, V> | null | undefined);
59
- }
60
- /**
61
- * Represents a self-balancing AVL (Adelson-Velsky and Landis) Tree.
62
- * This tree extends BST and performs rotations on add/delete to maintain balance.
63
- *
64
- * @template K - The type of the key.
65
- * @template V - The type of the value.
66
- * @template R - The type of the raw data object (if using `toEntryFn`).
67
- *
68
- * 1. Height-Balanced: Each node's left and right subtrees differ in height by no more than one.
69
- * 2. Automatic Rebalancing: AVL trees rebalance themselves automatically during insertions and deletions.
70
- * 3. Rotations for Balancing: Utilizes rotations (single or double) to maintain balance after updates.
71
- * 4. Order Preservation: Maintains the binary search tree property where left child values are less than the parent, and right child values are greater.
72
- * 5. Efficient Lookups: Offers O(log n) search time, where 'n' is the number of nodes, due to its balanced nature.
73
- * 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
74
- * 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.@example
75
- * // Find elements in a range
76
- * // In interval queries, AVL trees, with their strictly balanced structure and lower height, offer better query efficiency, making them ideal for frequent and high-performance interval queries. In contrast, Red-Black trees, with lower update costs, are more suitable for scenarios involving frequent insertions and deletions where the requirements for interval queries are less demanding.
77
- * type Datum = { timestamp: Date; temperature: number };
78
- * // Fixed dataset of CPU temperature readings
79
- * const cpuData: Datum[] = [
80
- * { timestamp: new Date('2024-12-02T00:00:00'), temperature: 55.1 },
81
- * { timestamp: new Date('2024-12-02T00:01:00'), temperature: 56.3 },
82
- * { timestamp: new Date('2024-12-02T00:02:00'), temperature: 54.8 },
83
- * { timestamp: new Date('2024-12-02T00:03:00'), temperature: 57.2 },
84
- * { timestamp: new Date('2024-12-02T00:04:00'), temperature: 58.0 },
85
- * { timestamp: new Date('2024-12-02T00:05:00'), temperature: 59.4 },
86
- * { timestamp: new Date('2024-12-02T00:06:00'), temperature: 60.1 },
87
- * { timestamp: new Date('2024-12-02T00:07:00'), temperature: 61.3 },
88
- * { timestamp: new Date('2024-12-02T00:08:00'), temperature: 62.0 },
89
- * { timestamp: new Date('2024-12-02T00:09:00'), temperature: 63.5 },
90
- * { timestamp: new Date('2024-12-02T00:10:00'), temperature: 64.0 },
91
- * { timestamp: new Date('2024-12-02T00:11:00'), temperature: 62.8 },
92
- * { timestamp: new Date('2024-12-02T00:12:00'), temperature: 61.5 },
93
- * { timestamp: new Date('2024-12-02T00:13:00'), temperature: 60.2 },
94
- * { timestamp: new Date('2024-12-02T00:14:00'), temperature: 59.8 },
95
- * { timestamp: new Date('2024-12-02T00:15:00'), temperature: 58.6 },
96
- * { timestamp: new Date('2024-12-02T00:16:00'), temperature: 57.4 },
97
- * { timestamp: new Date('2024-12-02T00:17:00'), temperature: 56.2 },
98
- * { timestamp: new Date('2024-12-02T00:18:00'), temperature: 55.7 },
99
- * { timestamp: new Date('2024-12-02T00:19:00'), temperature: 54.5 },
100
- * { timestamp: new Date('2024-12-02T00:20:00'), temperature: 53.2 },
101
- * { timestamp: new Date('2024-12-02T00:21:00'), temperature: 52.8 },
102
- * { timestamp: new Date('2024-12-02T00:22:00'), temperature: 51.9 },
103
- * { timestamp: new Date('2024-12-02T00:23:00'), temperature: 50.5 },
104
- * { timestamp: new Date('2024-12-02T00:24:00'), temperature: 49.8 },
105
- * { timestamp: new Date('2024-12-02T00:25:00'), temperature: 48.7 },
106
- * { timestamp: new Date('2024-12-02T00:26:00'), temperature: 47.5 },
107
- * { timestamp: new Date('2024-12-02T00:27:00'), temperature: 46.3 },
108
- * { timestamp: new Date('2024-12-02T00:28:00'), temperature: 45.9 },
109
- * { timestamp: new Date('2024-12-02T00:29:00'), temperature: 45.0 }
110
- * ];
111
- *
112
- * // Create an AVL tree to store CPU temperature data
113
- * const cpuTemperatureTree = new AVLTree<Date, number, Datum>(cpuData, {
114
- * toEntryFn: ({ timestamp, temperature }) => [timestamp, temperature]
115
- * });
116
- *
117
- * // Query a specific time range (e.g., from 00:05 to 00:15)
118
- * const rangeStart = new Date('2024-12-02T00:05:00');
119
- * const rangeEnd = new Date('2024-12-02T00:15:00');
120
- * const rangeResults = cpuTemperatureTree.rangeSearch([rangeStart, rangeEnd], node => ({
121
- * minute: node ? node.key.getMinutes() : 0,
122
- * temperature: cpuTemperatureTree.get(node ? node.key : undefined)
123
- * }));
124
- *
125
- * console.log(rangeResults); // [
126
- * // { minute: 5, temperature: 59.4 },
127
- * // { minute: 6, temperature: 60.1 },
128
- * // { minute: 7, temperature: 61.3 },
129
- * // { minute: 8, temperature: 62 },
130
- * // { minute: 9, temperature: 63.5 },
131
- * // { minute: 10, temperature: 64 },
132
- * // { minute: 11, temperature: 62.8 },
133
- * // { minute: 12, temperature: 61.5 },
134
- * // { minute: 13, temperature: 60.2 },
135
- * // { minute: 14, temperature: 59.8 },
136
- * // { minute: 15, temperature: 58.6 }
137
- * // ]
138
- */
139
- export declare class AVLTree<K = any, V = any, R extends object = object> extends BST<K, V, R> implements IBinaryTree<K, V, R> {
140
- /**
141
- * Creates an instance of AVLTree.
142
- * @remarks Time O(N log N) (from `addMany` with balanced add). Space O(N).
143
- *
144
- * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
145
- * @param [options] - Configuration options for the AVL tree.
146
- */
147
- constructor(keysNodesEntriesOrRaws?: Iterable<K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: AVLTreeOptions<K, V, R>);
148
- /**
149
- * (Protected) Creates a new AVL tree node.
150
- * @remarks Time O(1), Space O(1)
151
- *
152
- * @param key - The key for the new node.
153
- * @param [value] - The value for the new node.
154
- * @returns The newly created AVLTreeNode.
155
- */
156
- _createNode(key: K, value?: V): AVLTreeNode<K, V>;
157
- /**
158
- * Checks if the given item is an `AVLTreeNode` instance.
159
- * @remarks Time O(1), Space O(1)
160
- *
161
- * @param keyNodeOrEntry - The item to check.
162
- * @returns True if it's an AVLTreeNode, false otherwise.
163
- */
164
- isNode(keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is AVLTreeNode<K, V>;
165
- /**
166
- * Adds a new node to the AVL tree and balances the tree path.
167
- * @remarks Time O(log N) (O(H) for BST add + O(H) for `_balancePath`). Space O(H) for path/recursion.
168
- *
169
- * @param keyNodeOrEntry - The key, node, or entry to add.
170
- * @param [value] - The value, if providing just a key.
171
- * @returns True if the addition was successful, false otherwise.
172
- */
173
- add(keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
174
- /**
175
- * Deletes a node from the AVL tree and re-balances the tree.
176
- * @remarks Time O(log N) (O(H) for BST delete + O(H) for `_balancePath`). Space O(H) for path/recursion.
177
- *
178
- * @param keyNodeOrEntry - The node to delete.
179
- * @returns An array containing deletion results.
180
- */
181
- delete(keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): BinaryTreeDeleteResult<AVLTreeNode<K, V>>[];
182
- /**
183
- * Rebuilds the tree to be perfectly balanced.
184
- * @remarks AVL trees are already height-balanced, but this makes them *perfectly* balanced (minimal height and all leaves at N or N-1).
185
- * Time O(N) (O(N) for DFS, O(N) for sorted build). Space O(N) for node array and recursion stack.
186
- *
187
- * @param [iterationType=this.iterationType] - The traversal method for the initial node export.
188
- * @returns True if successful, false if the tree was empty.
189
- */
190
- perfectlyBalance(iterationType?: IterationType): boolean;
191
- /**
192
- * Creates a new AVLTree by mapping each [key, value] pair.
193
- * @remarks Time O(N log N) (O(N) iteration + O(log M) `add` for each item into the new tree). Space O(N) for the new tree.
194
- *
195
- * @template MK - New key type.
196
- * @template MV - New value type.
197
- * @template MR - New raw type.
198
- * @param callback - A function to map each [key, value] pair.
199
- * @param [options] - Options for the new AVLTree.
200
- * @param [thisArg] - `this` context for the callback.
201
- * @returns A new, mapped AVLTree.
202
- */
203
- map<MK = K, MV = V, MR extends object = object>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<BinaryTreeOptions<MK, MV, MR>>, thisArg?: unknown): AVLTree<MK, MV, MR>;
204
- /**
205
- * (Protected) Creates a new, empty instance of the same AVLTree constructor.
206
- * @remarks Time O(1)
207
- *
208
- * @template TK, TV, TR - Generic types for the new instance.
209
- * @param [options] - Options for the new tree.
210
- * @returns A new, empty tree.
211
- */
212
- protected _createInstance<TK = K, TV = V, TR extends object = R>(options?: Partial<BSTOptions<TK, TV, TR>>): this;
213
- /**
214
- * (Protected) Creates a new instance of the same AVLTree constructor, potentially with different generic types.
215
- * @remarks Time O(N log N) (from constructor) due to processing the iterable.
216
- *
217
- * @template TK, TV, TR - Generic types for the new instance.
218
- * @param [iter=[]] - An iterable to populate the new tree.
219
- * @param [options] - Options for the new tree.
220
- * @returns A new AVLTree.
221
- */
222
- protected _createLike<TK = K, TV = V, TR extends object = R>(iter?: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<BSTOptions<TK, TV, TR>>): AVLTree<TK, TV, TR>;
223
- /**
224
- * (Protected) Swaps properties of two nodes, including height.
225
- * @remarks Time O(H) (due to `ensureNode`), but O(1) if nodes are passed directly.
226
- *
227
- * @param srcNode - The source node.
228
- * @param destNode - The destination node.
229
- * @returns The `destNode` (now holding `srcNode`'s properties).
230
- */
231
- protected _swapProperties(srcNode: BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>, destNode: BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>): AVLTreeNode<K, V> | undefined;
232
- /**
233
- * (Protected) Calculates the balance factor (height(right) - height(left)).
234
- * @remarks Time O(1) (assumes heights are stored).
235
- *
236
- * @param node - The node to check.
237
- * @returns The balance factor (positive if right-heavy, negative if left-heavy).
238
- */
239
- protected _balanceFactor(node: AVLTreeNode<K, V>): number;
240
- /**
241
- * (Protected) Recalculates and updates the height of a node based on its children's heights.
242
- * @remarks Time O(1) (assumes children's heights are correct).
243
- *
244
- * @param node - The node to update.
245
- */
246
- protected _updateHeight(node: AVLTreeNode<K, V>): void;
247
- /**
248
- * (Protected) Performs a Left-Left (LL) rotation (a single right rotation).
249
- * @remarks Time O(1), Space O(1)
250
- *
251
- * @param A - The unbalanced node (root of the unbalanced subtree).
252
- */
253
- protected _balanceLL(A: AVLTreeNode<K, V>): void;
254
- /**
255
- * (Protected) Performs a Left-Right (LR) double rotation.
256
- * @remarks Time O(1), Space O(1)
257
- *
258
- * @param A - The unbalanced node (root of the unbalanced subtree).
259
- */
260
- protected _balanceLR(A: AVLTreeNode<K, V>): void;
261
- /**
262
- * (Protected) Performs a Right-Right (RR) rotation (a single left rotation).
263
- * @remarks Time O(1), Space O(1)
264
- *
265
- * @param A - The unbalanced node (root of the unbalanced subtree).
266
- */
267
- protected _balanceRR(A: AVLTreeNode<K, V>): void;
268
- /**
269
- * (Protected) Performs a Right-Left (RL) double rotation.
270
- * @remarks Time O(1), Space O(1)
271
- *
272
- * @param A - The unbalanced node (root of the unbalanced subtree).
273
- */
274
- protected _balanceRL(A: AVLTreeNode<K, V>): void;
275
- /**
276
- * (Protected) Traverses up the tree from the specified node, updating heights and performing rotations as needed.
277
- * @remarks Time O(log N) (O(H)), as it traverses the path to root. Space O(H) for the path array.
278
- *
279
- * @param node - The node to start balancing from (e.g., the newly inserted node or parent of the deleted node).
280
- */
281
- protected _balancePath(node: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): void;
282
- /**
283
- * (Protected) Replaces a node, ensuring height is copied.
284
- * @remarks Time O(1)
285
- *
286
- * @param oldNode - The node to be replaced.
287
- * @param newNode - The node to insert.
288
- * @returns The `newNode`.
289
- */
290
- protected _replaceNode(oldNode: AVLTreeNode<K, V>, newNode: AVLTreeNode<K, V>): AVLTreeNode<K, V>;
291
- }