avl-tree-typed 2.1.1 → 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 +3859 -0
  2. package/dist/cjs/index.cjs.map +1 -0
  3. package/dist/esm/index.mjs +3852 -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 +15 -3
  44. package/dist/{interfaces → types/interfaces}/binary-tree.d.ts +1 -1
  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/avl-tree-typed.js +3852 -0
  87. package/dist/umd/avl-tree-typed.js.map +1 -0
  88. package/dist/umd/avl-tree-typed.min.js +13 -0
  89. package/dist/umd/avl-tree-typed.min.js.map +1 -0
  90. package/package.json +25 -5
  91. package/src/data-structures/binary-tree/avl-tree-counter.ts +4 -4
  92. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
  93. package/src/data-structures/binary-tree/avl-tree.ts +2 -2
  94. package/src/data-structures/binary-tree/binary-tree.ts +4 -4
  95. package/src/data-structures/binary-tree/bst.ts +1 -1
  96. package/src/data-structures/binary-tree/red-black-tree.ts +2 -2
  97. package/src/data-structures/binary-tree/tree-counter.ts +4 -4
  98. package/src/data-structures/binary-tree/tree-multi-map.ts +1 -1
  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 +1 -1
  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 -15
  192. package/dist/index.js +0 -31
  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 -23
  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,503 +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 type { Comparator, DFSOrderPattern, ElementCallback, HeapOptions } from '../../types';
9
- import { IterableElementBase } from '../base';
10
- /**
11
- * Binary heap with pluggable comparator; supports fast insertion and removal of the top element.
12
- * @remarks Time O(1), Space O(1)
13
- * @template E
14
- * @template R
15
- * 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
16
- * 2. Heap Properties: Each node in a heap follows a specific order property, which varies depending on the type of heap:
17
- * Max Heap: The value of each parent node is greater than or equal to the value of its children.
18
- * Min Heap: The value of each parent node is less than or equal to the value of its children.
19
- * 3. Root Node Access: In a heap, the largest element (in a max heap) or the smallest element (in a min heap) is always at the root of the tree.
20
- * 4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
21
- * 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
22
- * 6. Non-linear Search: While a heap allows rapid access to its largest or smallest element, it is less efficient for other operations, such as searching for a specific element, as it is not designed for these tasks.
23
- * 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
24
- * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prime's minimum-spanning tree algorithm, which use heaps to improve performance.
25
- * @example
26
- * // Use Heap to sort an array
27
- * function heapSort(arr: number[]): number[] {
28
- * const heap = new Heap<number>(arr, { comparator: (a, b) => a - b });
29
- * const sorted: number[] = [];
30
- * while (!heap.isEmpty()) {
31
- * sorted.push(heap.poll()!); // Poll minimum element
32
- * }
33
- * return sorted;
34
- * }
35
- *
36
- * const array = [5, 3, 8, 4, 1, 2];
37
- * console.log(heapSort(array)); // [1, 2, 3, 4, 5, 8]
38
- * @example
39
- * // Use Heap to solve top k problems
40
- * function topKElements(arr: number[], k: number): number[] {
41
- * const heap = new Heap<number>([], { comparator: (a, b) => b - a }); // Max heap
42
- * arr.forEach(num => {
43
- * heap.add(num);
44
- * if (heap.size > k) heap.poll(); // Keep the heap size at K
45
- * });
46
- * return heap.toArray();
47
- * }
48
- *
49
- * const numbers = [10, 30, 20, 5, 15, 25];
50
- * console.log(topKElements(numbers, 3)); // [15, 10, 5]
51
- * @example
52
- * // Use Heap to merge sorted sequences
53
- * function mergeSortedSequences(sequences: number[][]): number[] {
54
- * const heap = new Heap<{ value: number; seqIndex: number; itemIndex: number }>([], {
55
- * comparator: (a, b) => a.value - b.value // Min heap
56
- * });
57
- *
58
- * // Initialize heap
59
- * sequences.forEach((seq, seqIndex) => {
60
- * if (seq.length) {
61
- * heap.add({ value: seq[0], seqIndex, itemIndex: 0 });
62
- * }
63
- * });
64
- *
65
- * const merged: number[] = [];
66
- * while (!heap.isEmpty()) {
67
- * const { value, seqIndex, itemIndex } = heap.poll()!;
68
- * merged.push(value);
69
- *
70
- * if (itemIndex + 1 < sequences[seqIndex].length) {
71
- * heap.add({
72
- * value: sequences[seqIndex][itemIndex + 1],
73
- * seqIndex,
74
- * itemIndex: itemIndex + 1
75
- * });
76
- * }
77
- * }
78
- *
79
- * return merged;
80
- * }
81
- *
82
- * const sequences = [
83
- * [1, 4, 7],
84
- * [2, 5, 8],
85
- * [3, 6, 9]
86
- * ];
87
- * console.log(mergeSortedSequences(sequences)); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
88
- * @example
89
- * // Use Heap to dynamically maintain the median
90
- * class MedianFinder {
91
- * private low: MaxHeap<number>; // Max heap, stores the smaller half
92
- * private high: MinHeap<number>; // Min heap, stores the larger half
93
- *
94
- * constructor() {
95
- * this.low = new MaxHeap<number>([]);
96
- * this.high = new MinHeap<number>([]);
97
- * }
98
- *
99
- * addNum(num: number): void {
100
- * if (this.low.isEmpty() || num <= this.low.peek()!) this.low.add(num);
101
- * else this.high.add(num);
102
- *
103
- * // Balance heaps
104
- * if (this.low.size > this.high.size + 1) this.high.add(this.low.poll()!);
105
- * else if (this.high.size > this.low.size) this.low.add(this.high.poll()!);
106
- * }
107
- *
108
- * findMedian(): number {
109
- * if (this.low.size === this.high.size) return (this.low.peek()! + this.high.peek()!) / 2;
110
- * return this.low.peek()!;
111
- * }
112
- * }
113
- *
114
- * const medianFinder = new MedianFinder();
115
- * medianFinder.addNum(10);
116
- * console.log(medianFinder.findMedian()); // 10
117
- * medianFinder.addNum(20);
118
- * console.log(medianFinder.findMedian()); // 15
119
- * medianFinder.addNum(30);
120
- * console.log(medianFinder.findMedian()); // 20
121
- * medianFinder.addNum(40);
122
- * console.log(medianFinder.findMedian()); // 25
123
- * medianFinder.addNum(50);
124
- * console.log(medianFinder.findMedian()); // 30
125
- * @example
126
- * // Use Heap for load balancing
127
- * function loadBalance(requests: number[], servers: number): number[] {
128
- * const serverHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // min heap
129
- * const serverLoads = new Array(servers).fill(0);
130
- *
131
- * for (let i = 0; i < servers; i++) {
132
- * serverHeap.add({ id: i, load: 0 });
133
- * }
134
- *
135
- * requests.forEach(req => {
136
- * const server = serverHeap.poll()!;
137
- * serverLoads[server.id] += req;
138
- * server.load += req;
139
- * serverHeap.add(server); // The server after updating the load is re-entered into the heap
140
- * });
141
- *
142
- * return serverLoads;
143
- * }
144
- *
145
- * const requests = [5, 2, 8, 3, 7];
146
- * console.log(loadBalance(requests, 3)); // [12, 8, 5]
147
- * @example
148
- * // Use Heap to schedule tasks
149
- * type Task = [string, number];
150
- *
151
- * function scheduleTasks(tasks: Task[], machines: number): Map<number, Task[]> {
152
- * const machineHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // Min heap
153
- * const allocation = new Map<number, Task[]>();
154
- *
155
- * // Initialize the load on each machine
156
- * for (let i = 0; i < machines; i++) {
157
- * machineHeap.add({ id: i, load: 0 });
158
- * allocation.set(i, []);
159
- * }
160
- *
161
- * // Assign tasks
162
- * tasks.forEach(([task, load]) => {
163
- * const machine = machineHeap.poll()!;
164
- * allocation.get(machine.id)!.push([task, load]);
165
- * machine.load += load;
166
- * machineHeap.add(machine); // The machine after updating the load is re-entered into the heap
167
- * });
168
- *
169
- * return allocation;
170
- * }
171
- *
172
- * const tasks: Task[] = [
173
- * ['Task1', 3],
174
- * ['Task2', 1],
175
- * ['Task3', 2],
176
- * ['Task4', 5],
177
- * ['Task5', 4]
178
- * ];
179
- * const expectedMap = new Map<number, Task[]>();
180
- * expectedMap.set(0, [
181
- * ['Task1', 3],
182
- * ['Task4', 5]
183
- * ]);
184
- * expectedMap.set(1, [
185
- * ['Task2', 1],
186
- * ['Task3', 2],
187
- * ['Task5', 4]
188
- * ]);
189
- * console.log(scheduleTasks(tasks, 2)); // expectedMap
190
- */
191
- export declare class Heap<E = unknown, R = never> extends IterableElementBase<E, R> {
192
- protected _equals: (a: E, b: E) => boolean;
193
- /**
194
- * Create a Heap and optionally bulk-insert elements.
195
- * @remarks Time O(N), Space O(N)
196
- * @param [elements] - Iterable of elements (or raw values if toElementFn is set).
197
- * @param [options] - Options such as comparator and toElementFn.
198
- * @returns New Heap instance.
199
- */
200
- constructor(elements?: Iterable<E | R>, options?: HeapOptions<E, R>);
201
- protected _elements: E[];
202
- /**
203
- * Get the backing array of the heap.
204
- * @remarks Time O(1), Space O(1)
205
- * @returns Internal elements array.
206
- */
207
- get elements(): E[];
208
- /**
209
- * Get the number of elements.
210
- * @remarks Time O(1), Space O(1)
211
- * @returns Heap size.
212
- */
213
- get size(): number;
214
- /**
215
- * Get the last leaf element.
216
- * @remarks Time O(1), Space O(1)
217
- * @returns Last element or undefined.
218
- */
219
- get leaf(): E | undefined;
220
- /**
221
- * Create a heap of the same class from an iterable.
222
- * @remarks Time O(N), Space O(N)
223
- * @template T
224
- * @template R
225
- * @template S
226
- * @param [elements] - Iterable of elements or raw records.
227
- * @param [options] - Heap options including comparator.
228
- * @returns A new heap instance of this class.
229
- */
230
- static from<T, R = never, S extends Heap<T, R> = Heap<T, R>>(this: new (elements?: Iterable<T | R>, options?: HeapOptions<T, R>) => S, elements?: Iterable<T | R>, options?: HeapOptions<T, R>): S;
231
- /**
232
- * Build a Heap from an iterable in linear time given a comparator.
233
- * @remarks Time O(N), Space O(N)
234
- * @template EE
235
- * @template RR
236
- * @param elements - Iterable of elements.
237
- * @param options - Heap options including comparator.
238
- * @returns A new Heap built from elements.
239
- */
240
- static heapify<EE = unknown, RR = never>(elements: Iterable<EE>, options: HeapOptions<EE, RR>): Heap<EE, RR>;
241
- /**
242
- * Insert an element.
243
- * @remarks Time O(1) amortized, Space O(1)
244
- * @param element - Element to insert.
245
- * @returns True.
246
- */
247
- add(element: E): boolean;
248
- /**
249
- * Insert many elements from an iterable.
250
- * @remarks Time O(N log N), Space O(1)
251
- * @param elements - Iterable of elements or raw values.
252
- * @returns Array of per-element success flags.
253
- */
254
- addMany(elements: Iterable<E | R>): boolean[];
255
- /**
256
- * Remove and return the top element.
257
- * @remarks Time O(log N), Space O(1)
258
- * @returns Top element or undefined.
259
- */
260
- poll(): E | undefined;
261
- /**
262
- * Get the current top element without removing it.
263
- * @remarks Time O(1), Space O(1)
264
- * @returns Top element or undefined.
265
- */
266
- peek(): E | undefined;
267
- /**
268
- * Check whether the heap is empty.
269
- * @remarks Time O(1), Space O(1)
270
- * @returns True if size is 0.
271
- */
272
- isEmpty(): boolean;
273
- /**
274
- * Remove all elements.
275
- * @remarks Time O(1), Space O(1)
276
- * @returns void
277
- */
278
- clear(): void;
279
- /**
280
- * Replace the backing array and rebuild the heap.
281
- * @remarks Time O(N), Space O(N)
282
- * @param elements - Iterable used to refill the heap.
283
- * @returns Array of per-node results from fixing steps.
284
- */
285
- refill(elements: Iterable<E>): boolean[];
286
- /**
287
- * Check if an equal element exists in the heap.
288
- * @remarks Time O(N), Space O(1)
289
- * @param element - Element to search for.
290
- * @returns True if found.
291
- */
292
- has(element: E): boolean;
293
- /**
294
- * Delete one occurrence of an element.
295
- * @remarks Time O(N), Space O(1)
296
- * @param element - Element to delete.
297
- * @returns True if an element was removed.
298
- */
299
- delete(element: E): boolean;
300
- /**
301
- * Delete the first element that matches a predicate.
302
- * @remarks Time O(N), Space O(1)
303
- * @param predicate - Function (element, index, heap) → boolean.
304
- * @returns True if an element was removed.
305
- */
306
- deleteBy(predicate: (element: E, index: number, heap: this) => boolean): boolean;
307
- /**
308
- * Set the equality comparator used by has/delete operations.
309
- * @remarks Time O(1), Space O(1)
310
- * @param equals - Equality predicate (a, b) → boolean.
311
- * @returns This heap.
312
- */
313
- setEquality(equals: (a: E, b: E) => boolean): this;
314
- /**
315
- * Traverse the binary heap as a complete binary tree and collect elements.
316
- * @remarks Time O(N), Space O(H)
317
- * @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
318
- * @returns Array of visited elements.
319
- */
320
- dfs(order?: DFSOrderPattern): E[];
321
- /**
322
- * Restore heap order bottom-up (heapify in-place).
323
- * @remarks Time O(N), Space O(1)
324
- * @returns Array of per-node results from fixing steps.
325
- */
326
- fix(): boolean[];
327
- /**
328
- * Return all elements in ascending order by repeatedly polling.
329
- * @remarks Time O(N log N), Space O(N)
330
- * @returns Sorted array of elements.
331
- */
332
- sort(): E[];
333
- /**
334
- * Deep clone this heap.
335
- * @remarks Time O(N), Space O(N)
336
- * @returns A new heap with the same elements.
337
- */
338
- clone(): this;
339
- /**
340
- * Filter elements into a new heap of the same class.
341
- * @remarks Time O(N log N), Space O(N)
342
- * @param callback - Predicate (element, index, heap) → boolean to keep element.
343
- * @param [thisArg] - Value for `this` inside the callback.
344
- * @returns A new heap with the kept elements.
345
- */
346
- filter(callback: ElementCallback<E, R, boolean>, thisArg?: unknown): this;
347
- /**
348
- * Map elements into a new heap of possibly different element type.
349
- * @remarks Time O(N log N), Space O(N)
350
- * @template EM
351
- * @template RM
352
- * @param callback - Mapping function (element, index, heap) → newElement.
353
- * @param options - Options for the output heap, including comparator for EM.
354
- * @param [thisArg] - Value for `this` inside the callback.
355
- * @returns A new heap with mapped elements.
356
- */
357
- map<EM, RM>(callback: ElementCallback<E, R, EM>, options: HeapOptions<EM, RM> & {
358
- comparator: Comparator<EM>;
359
- }, thisArg?: unknown): Heap<EM, RM>;
360
- /**
361
- * Map elements into a new heap of the same element type.
362
- * @remarks Time O(N log N), Space O(N)
363
- * @param callback - Mapping function (element, index, heap) → element.
364
- * @param [thisArg] - Value for `this` inside the callback.
365
- * @returns A new heap with mapped elements.
366
- */
367
- mapSame(callback: ElementCallback<E, R, E>, thisArg?: unknown): this;
368
- protected _DEFAULT_COMPARATOR: (a: E, b: E) => number;
369
- protected _comparator: Comparator<E>; /**
370
- * Get the comparator used to order elements.
371
- * @remarks Time O(1), Space O(1)
372
- * @returns Comparator function.
373
- */
374
- /**
375
- * Get the comparator used to order elements.
376
- * @remarks Time O(1), Space O(1)
377
- * @returns Comparator function.
378
- */
379
- get comparator(): Comparator<E>;
380
- protected _getIterator(): IterableIterator<E>;
381
- protected _bubbleUp(index: number): boolean;
382
- protected _sinkDown(index: number, halfLength: number): boolean;
383
- /**
384
- * (Protected) Create an empty instance of the same concrete class.
385
- * @remarks Time O(1), Space O(1)
386
- * @param [options] - Options to override comparator or toElementFn.
387
- * @returns A like-kind empty heap instance.
388
- */
389
- protected _createInstance(options?: HeapOptions<E, R>): this;
390
- /**
391
- * (Protected) Create a like-kind instance seeded by elements.
392
- * @remarks Time O(N log N), Space O(N)
393
- * @template EM
394
- * @template RM
395
- * @param [elements] - Iterable of elements or raw values to seed.
396
- * @param [options] - Options forwarded to the constructor.
397
- * @returns A like-kind heap instance.
398
- */
399
- protected _createLike<EM, RM>(elements?: Iterable<EM> | Iterable<RM>, options?: HeapOptions<EM, RM>): Heap<EM, RM>;
400
- /**
401
- * (Protected) Spawn an empty like-kind heap instance.
402
- * @remarks Time O(1), Space O(1)
403
- * @template EM
404
- * @template RM
405
- * @param [options] - Options forwarded to the constructor.
406
- * @returns An empty like-kind heap instance.
407
- */
408
- protected _spawnLike<EM, RM>(options?: HeapOptions<EM, RM>): Heap<EM, RM>;
409
- }
410
- /**
411
- * Node container used by FibonacciHeap.
412
- * @remarks Time O(1), Space O(1)
413
- * @template E
414
- */
415
- export declare class FibonacciHeapNode<E> {
416
- element: E;
417
- degree: number;
418
- left?: FibonacciHeapNode<E>;
419
- right?: FibonacciHeapNode<E>;
420
- child?: FibonacciHeapNode<E>;
421
- parent?: FibonacciHeapNode<E>;
422
- marked: boolean;
423
- constructor(element: E, degree?: number);
424
- }
425
- /**
426
- * Fibonacci heap (min-heap) optimized for fast merges and amortized operations.
427
- * @remarks Time O(1), Space O(1)
428
- * @template E
429
- * @example examples will be generated by unit test
430
- */
431
- export declare class FibonacciHeap<E> {
432
- /**
433
- * Create a FibonacciHeap.
434
- * @remarks Time O(1), Space O(1)
435
- * @param [comparator] - Comparator to order elements (min-heap by default).
436
- * @returns New FibonacciHeap instance.
437
- */
438
- constructor(comparator?: Comparator<E>);
439
- protected _root?: FibonacciHeapNode<E>;
440
- /**
441
- * Get the circular root list head.
442
- * @remarks Time O(1), Space O(1)
443
- * @returns Root node or undefined.
444
- */
445
- get root(): FibonacciHeapNode<E> | undefined;
446
- protected _size: number;
447
- get size(): number;
448
- protected _min?: FibonacciHeapNode<E>;
449
- /**
450
- * Get the current minimum node.
451
- * @remarks Time O(1), Space O(1)
452
- * @returns Min node or undefined.
453
- */
454
- get min(): FibonacciHeapNode<E> | undefined;
455
- protected _comparator: Comparator<E>;
456
- get comparator(): Comparator<E>;
457
- clear(): void;
458
- add(element: E): boolean;
459
- /**
460
- * Push an element into the root list.
461
- * @remarks Time O(1) amortized, Space O(1)
462
- * @param element - Element to insert.
463
- * @returns This heap.
464
- */
465
- push(element: E): this;
466
- peek(): E | undefined;
467
- /**
468
- * Collect nodes from a circular doubly linked list starting at head.
469
- * @remarks Time O(K), Space O(K)
470
- * @param [head] - Start node of the circular list.
471
- * @returns Array of nodes from the list.
472
- */
473
- consumeLinkedList(head?: FibonacciHeapNode<E>): FibonacciHeapNode<E>[];
474
- /**
475
- * Insert a node into a parent's child list (circular).
476
- * @remarks Time O(1), Space O(1)
477
- * @param parent - Parent node.
478
- * @param node - Child node to insert.
479
- * @returns void
480
- */
481
- mergeWithChild(parent: FibonacciHeapNode<E>, node: FibonacciHeapNode<E>): void;
482
- poll(): E | undefined;
483
- /**
484
- * Remove and return the minimum element, consolidating the root list.
485
- * @remarks Time O(log N) amortized, Space O(1)
486
- * @returns Minimum element or undefined.
487
- */
488
- pop(): E | undefined;
489
- /**
490
- * Meld another heap into this heap.
491
- * @remarks Time O(1), Space O(1)
492
- * @param heapToMerge - Another FibonacciHeap to meld into this one.
493
- * @returns void
494
- */
495
- merge(heapToMerge: FibonacciHeap<E>): void;
496
- _createNode(element: E): FibonacciHeapNode<E>;
497
- isEmpty(): boolean;
498
- protected _defaultComparator(a: E, b: E): number;
499
- protected mergeWithRoot(node: FibonacciHeapNode<E>): void;
500
- protected removeFromRoot(node: FibonacciHeapNode<E>): void;
501
- protected _link(y: FibonacciHeapNode<E>, x: FibonacciHeapNode<E>): void;
502
- protected _consolidate(): void;
503
- }