data-structure-typed 2.5.0 → 2.5.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 (246) hide show
  1. package/.vitepress/cache/deps_temp_51f5f1b0/chunk-7OIKW5WK.js +12984 -0
  2. package/.vitepress/cache/deps_temp_51f5f1b0/package.json +3 -0
  3. package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vue_devtools-api.js +4505 -0
  4. package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vueuse_core.js +9731 -0
  5. package/.vitepress/cache/deps_temp_51f5f1b0/vue.js +347 -0
  6. package/CHANGELOG.md +5 -1
  7. package/README.md +124 -29
  8. package/dist/cjs/binary-tree.cjs +26282 -0
  9. package/dist/cjs/graph.cjs +5422 -0
  10. package/dist/cjs/hash.cjs +1310 -0
  11. package/dist/cjs/heap.cjs +1602 -0
  12. package/dist/cjs/index.cjs +31257 -14673
  13. package/dist/cjs/linked-list.cjs +4576 -0
  14. package/dist/cjs/matrix.cjs +1080 -0
  15. package/dist/cjs/priority-queue.cjs +1376 -0
  16. package/dist/cjs/queue.cjs +4264 -0
  17. package/dist/cjs/stack.cjs +907 -0
  18. package/dist/cjs/trie.cjs +1223 -0
  19. package/dist/cjs-legacy/binary-tree.cjs +26319 -0
  20. package/dist/cjs-legacy/graph.cjs +5420 -0
  21. package/dist/cjs-legacy/hash.cjs +1310 -0
  22. package/dist/cjs-legacy/heap.cjs +1599 -0
  23. package/dist/cjs-legacy/index.cjs +31268 -14679
  24. package/dist/cjs-legacy/linked-list.cjs +4582 -0
  25. package/dist/cjs-legacy/matrix.cjs +1083 -0
  26. package/dist/cjs-legacy/priority-queue.cjs +1374 -0
  27. package/dist/cjs-legacy/queue.cjs +4262 -0
  28. package/dist/cjs-legacy/stack.cjs +907 -0
  29. package/dist/cjs-legacy/trie.cjs +1222 -0
  30. package/dist/esm/binary-tree.mjs +26267 -0
  31. package/dist/esm/graph.mjs +5409 -0
  32. package/dist/esm/hash.mjs +1307 -0
  33. package/dist/esm/heap.mjs +1596 -0
  34. package/dist/esm/index.mjs +31254 -14674
  35. package/dist/esm/linked-list.mjs +4569 -0
  36. package/dist/esm/matrix.mjs +1076 -0
  37. package/dist/esm/priority-queue.mjs +1372 -0
  38. package/dist/esm/queue.mjs +4260 -0
  39. package/dist/esm/stack.mjs +905 -0
  40. package/dist/esm/trie.mjs +1220 -0
  41. package/dist/esm-legacy/binary-tree.mjs +26304 -0
  42. package/dist/esm-legacy/graph.mjs +5407 -0
  43. package/dist/esm-legacy/hash.mjs +1307 -0
  44. package/dist/esm-legacy/heap.mjs +1593 -0
  45. package/dist/esm-legacy/index.mjs +31265 -14680
  46. package/dist/esm-legacy/linked-list.mjs +4575 -0
  47. package/dist/esm-legacy/matrix.mjs +1079 -0
  48. package/dist/esm-legacy/priority-queue.mjs +1370 -0
  49. package/dist/esm-legacy/queue.mjs +4258 -0
  50. package/dist/esm-legacy/stack.mjs +905 -0
  51. package/dist/esm-legacy/trie.mjs +1219 -0
  52. package/dist/types/common/error.d.ts +9 -0
  53. package/dist/types/common/index.d.ts +1 -1
  54. package/dist/types/data-structures/base/index.d.ts +1 -0
  55. package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
  56. package/dist/types/data-structures/base/linear-base.d.ts +3 -3
  57. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +288 -0
  58. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +336 -0
  59. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +618 -18
  60. package/dist/types/data-structures/binary-tree/bst.d.ts +676 -1
  61. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +456 -0
  62. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +144 -1
  63. package/dist/types/data-structures/binary-tree/tree-map.d.ts +3307 -399
  64. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3285 -360
  65. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2674 -325
  66. package/dist/types/data-structures/binary-tree/tree-set.d.ts +3072 -287
  67. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
  68. package/dist/types/data-structures/graph/directed-graph.d.ts +240 -0
  69. package/dist/types/data-structures/graph/undirected-graph.d.ts +216 -0
  70. package/dist/types/data-structures/hash/hash-map.d.ts +274 -10
  71. package/dist/types/data-structures/heap/heap.d.ts +336 -0
  72. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +411 -3
  73. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +363 -3
  74. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +434 -2
  75. package/dist/types/data-structures/matrix/matrix.d.ts +192 -0
  76. package/dist/types/data-structures/queue/deque.d.ts +364 -4
  77. package/dist/types/data-structures/queue/queue.d.ts +288 -0
  78. package/dist/types/data-structures/stack/stack.d.ts +240 -0
  79. package/dist/types/data-structures/trie/trie.d.ts +292 -4
  80. package/dist/types/interfaces/graph.d.ts +1 -1
  81. package/dist/types/types/common.d.ts +2 -2
  82. package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
  83. package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
  84. package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
  85. package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
  86. package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
  87. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  88. package/dist/types/types/utils/validate-type.d.ts +4 -4
  89. package/dist/umd/data-structure-typed.js +31196 -14608
  90. package/dist/umd/data-structure-typed.min.js +11 -5
  91. package/docs-site-docusaurus/README.md +41 -0
  92. package/docs-site-docusaurus/docs/api/README.md +52 -0
  93. package/docs-site-docusaurus/docs/api/classes/AVLTree.md +6644 -0
  94. package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +282 -0
  95. package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +2266 -0
  96. package/docs-site-docusaurus/docs/api/classes/BST.md +6293 -0
  97. package/docs-site-docusaurus/docs/api/classes/BSTNode.md +333 -0
  98. package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +455 -0
  99. package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +4647 -0
  100. package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +331 -0
  101. package/docs-site-docusaurus/docs/api/classes/Deque.md +2767 -0
  102. package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +2999 -0
  103. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +2685 -0
  104. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +221 -0
  105. package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +253 -0
  106. package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +21 -0
  107. package/docs-site-docusaurus/docs/api/classes/HashMap.md +1333 -0
  108. package/docs-site-docusaurus/docs/api/classes/Heap.md +1881 -0
  109. package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +800 -0
  110. package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +644 -0
  111. package/docs-site-docusaurus/docs/api/classes/LinearBase.md +1632 -0
  112. package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +1853 -0
  113. package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +1108 -0
  114. package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +156 -0
  115. package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +2824 -0
  116. package/docs-site-docusaurus/docs/api/classes/MapGraph.md +2929 -0
  117. package/docs-site-docusaurus/docs/api/classes/Matrix.md +1026 -0
  118. package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +1866 -0
  119. package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +1883 -0
  120. package/docs-site-docusaurus/docs/api/classes/MinHeap.md +1879 -0
  121. package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +1882 -0
  122. package/docs-site-docusaurus/docs/api/classes/Navigator.md +109 -0
  123. package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +1839 -0
  124. package/docs-site-docusaurus/docs/api/classes/Queue.md +2244 -0
  125. package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +6888 -0
  126. package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +372 -0
  127. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +2897 -0
  128. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +169 -0
  129. package/docs-site-docusaurus/docs/api/classes/SkipList.md +1229 -0
  130. package/docs-site-docusaurus/docs/api/classes/Stack.md +1573 -0
  131. package/docs-site-docusaurus/docs/api/classes/TreeMap.md +1389 -0
  132. package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +1591 -0
  133. package/docs-site-docusaurus/docs/api/classes/TreeSet.md +1246 -0
  134. package/docs-site-docusaurus/docs/api/classes/Trie.md +1708 -0
  135. package/docs-site-docusaurus/docs/api/classes/TrieNode.md +199 -0
  136. package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +2979 -0
  137. package/docs-site-docusaurus/docs/guide/_category_.json +6 -0
  138. package/docs-site-docusaurus/docs/guide/architecture.md +615 -0
  139. package/docs-site-docusaurus/docs/guide/concepts.md +451 -0
  140. package/docs-site-docusaurus/docs/guide/faq.md +180 -0
  141. package/docs-site-docusaurus/docs/guide/guides.md +597 -0
  142. package/docs-site-docusaurus/docs/guide/installation.md +62 -0
  143. package/docs-site-docusaurus/docs/guide/integrations.md +825 -0
  144. package/docs-site-docusaurus/docs/guide/overview.md +645 -0
  145. package/docs-site-docusaurus/docs/guide/performance.md +835 -0
  146. package/docs-site-docusaurus/docs/guide/quick-start.md +104 -0
  147. package/docs-site-docusaurus/docs/guide/use-cases/_category_.json +6 -0
  148. package/docs-site-docusaurus/docs/guide/use-cases/array-sort-alternative.md +158 -0
  149. package/docs-site-docusaurus/docs/guide/use-cases/heap-vs-sorting.md +92 -0
  150. package/docs-site-docusaurus/docs/guide/use-cases/map-vs-treemap.md +151 -0
  151. package/docs-site-docusaurus/docs/guide/use-cases/priority-queue-typescript.md +113 -0
  152. package/docs-site-docusaurus/docs/guide/use-cases/treemap-javascript.md +151 -0
  153. package/docs-site-docusaurus/docusaurus.config.ts +159 -0
  154. package/docs-site-docusaurus/fix-mdx-generics.mjs +75 -0
  155. package/docs-site-docusaurus/package-lock.json +18667 -0
  156. package/docs-site-docusaurus/package.json +50 -0
  157. package/docs-site-docusaurus/prefix-class-to-methods.mjs +48 -0
  158. package/docs-site-docusaurus/sidebars.ts +23 -0
  159. package/docs-site-docusaurus/sort-protected.mjs +87 -0
  160. package/docs-site-docusaurus/src/css/custom.css +96 -0
  161. package/docs-site-docusaurus/src/pages/index.module.css +13 -0
  162. package/docs-site-docusaurus/src/pages/index.tsx +120 -0
  163. package/docs-site-docusaurus/src/pages/markdown-page.md +7 -0
  164. package/docs-site-docusaurus/src/theme/TOCItems/index.tsx +34 -0
  165. package/docs-site-docusaurus/static/.nojekyll +0 -0
  166. package/docs-site-docusaurus/static/img/docusaurus-social-card.jpg +0 -0
  167. package/docs-site-docusaurus/static/img/docusaurus.png +0 -0
  168. package/docs-site-docusaurus/static/img/favicon.ico +0 -0
  169. package/docs-site-docusaurus/static/img/favicon.png +0 -0
  170. package/docs-site-docusaurus/static/img/logo-180.png +0 -0
  171. package/docs-site-docusaurus/static/img/logo.jpg +0 -0
  172. package/docs-site-docusaurus/static/img/logo.png +0 -0
  173. package/docs-site-docusaurus/static/img/logo.svg +1 -0
  174. package/docs-site-docusaurus/static/img/og-image.png +0 -0
  175. package/docs-site-docusaurus/static/img/undraw_docusaurus_mountain.svg +171 -0
  176. package/docs-site-docusaurus/static/img/undraw_docusaurus_react.svg +170 -0
  177. package/docs-site-docusaurus/static/img/undraw_docusaurus_tree.svg +40 -0
  178. package/docs-site-docusaurus/static/llms.txt +37 -0
  179. package/docs-site-docusaurus/static/robots.txt +4 -0
  180. package/docs-site-docusaurus/typedoc.json +23 -0
  181. package/llms.txt +37 -0
  182. package/package.json +159 -55
  183. package/src/common/error.ts +19 -1
  184. package/src/common/index.ts +1 -1
  185. package/src/data-structures/base/index.ts +1 -0
  186. package/src/data-structures/base/iterable-element-base.ts +3 -2
  187. package/src/data-structures/base/iterable-entry-base.ts +8 -8
  188. package/src/data-structures/base/linear-base.ts +3 -3
  189. package/src/data-structures/binary-tree/avl-tree.ts +287 -0
  190. package/src/data-structures/binary-tree/binary-indexed-tree.ts +327 -5
  191. package/src/data-structures/binary-tree/binary-tree.ts +581 -6
  192. package/src/data-structures/binary-tree/bst.ts +922 -7
  193. package/src/data-structures/binary-tree/red-black-tree.ts +453 -0
  194. package/src/data-structures/binary-tree/segment-tree.ts +139 -2
  195. package/src/data-structures/binary-tree/tree-map.ts +3300 -495
  196. package/src/data-structures/binary-tree/tree-multi-map.ts +3384 -563
  197. package/src/data-structures/binary-tree/tree-multi-set.ts +2757 -493
  198. package/src/data-structures/binary-tree/tree-set.ts +3122 -440
  199. package/src/data-structures/graph/abstract-graph.ts +6 -6
  200. package/src/data-structures/graph/directed-graph.ts +230 -0
  201. package/src/data-structures/graph/undirected-graph.ts +207 -0
  202. package/src/data-structures/hash/hash-map.ts +270 -19
  203. package/src/data-structures/heap/heap.ts +326 -4
  204. package/src/data-structures/heap/max-heap.ts +2 -2
  205. package/src/data-structures/linked-list/doubly-linked-list.ts +394 -3
  206. package/src/data-structures/linked-list/singly-linked-list.ts +348 -3
  207. package/src/data-structures/linked-list/skip-linked-list.ts +421 -7
  208. package/src/data-structures/matrix/matrix.ts +194 -10
  209. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
  210. package/src/data-structures/queue/deque.ts +350 -5
  211. package/src/data-structures/queue/queue.ts +276 -0
  212. package/src/data-structures/stack/stack.ts +230 -0
  213. package/src/data-structures/trie/trie.ts +283 -7
  214. package/src/interfaces/graph.ts +1 -1
  215. package/src/types/common.ts +2 -2
  216. package/src/types/data-structures/binary-tree/bst.ts +1 -0
  217. package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
  218. package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
  219. package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
  220. package/src/types/data-structures/heap/heap.ts +1 -0
  221. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  222. package/src/types/utils/validate-type.ts +4 -4
  223. package/vercel.json +6 -0
  224. package/dist/leetcode/avl-tree-counter.mjs +0 -2957
  225. package/dist/leetcode/avl-tree-multi-map.mjs +0 -2889
  226. package/dist/leetcode/avl-tree.mjs +0 -2720
  227. package/dist/leetcode/binary-tree.mjs +0 -1594
  228. package/dist/leetcode/bst.mjs +0 -2398
  229. package/dist/leetcode/deque.mjs +0 -683
  230. package/dist/leetcode/directed-graph.mjs +0 -1733
  231. package/dist/leetcode/doubly-linked-list.mjs +0 -709
  232. package/dist/leetcode/hash-map.mjs +0 -493
  233. package/dist/leetcode/heap.mjs +0 -542
  234. package/dist/leetcode/max-heap.mjs +0 -375
  235. package/dist/leetcode/max-priority-queue.mjs +0 -383
  236. package/dist/leetcode/min-heap.mjs +0 -363
  237. package/dist/leetcode/min-priority-queue.mjs +0 -371
  238. package/dist/leetcode/priority-queue.mjs +0 -363
  239. package/dist/leetcode/queue.mjs +0 -943
  240. package/dist/leetcode/red-black-tree.mjs +0 -2765
  241. package/dist/leetcode/singly-linked-list.mjs +0 -754
  242. package/dist/leetcode/stack.mjs +0 -217
  243. package/dist/leetcode/tree-counter.mjs +0 -3039
  244. package/dist/leetcode/tree-multi-map.mjs +0 -2913
  245. package/dist/leetcode/trie.mjs +0 -413
  246. package/dist/leetcode/undirected-graph.mjs +0 -1650
@@ -0,0 +1,1881 @@
1
+ [**data-structure-typed**](../README.md)
2
+
3
+ ***
4
+
5
+ [data-structure-typed](../README.md) / Heap
6
+
7
+ # Class: Heap\<E, R\>
8
+
9
+ Defined in: [data-structures/heap/heap.ts:150](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L150)
10
+
11
+ Binary heap with pluggable comparator; supports fast insertion and removal of the top element.
12
+
13
+ ## Remarks
14
+
15
+ Time O(1), Space O(1)
16
+
17
+ ## Examples
18
+
19
+ ```ts
20
+ // Use Heap to solve top k problems
21
+ function topKElements(arr: number[], k: number): number[] {
22
+ const heap = new Heap<number>([], { comparator: (a, b) => b - a }); // Max heap
23
+ arr.forEach(num => {
24
+ heap.add(num);
25
+ if (heap.size > k) heap.poll(); // Keep the heap size at K
26
+ });
27
+ return heap.toArray();
28
+ }
29
+
30
+ const numbers = [10, 30, 20, 5, 15, 25];
31
+ console.log(topKElements(numbers, 3)); // [15, 10, 5];
32
+ ```
33
+
34
+ ```ts
35
+ // Use Heap to dynamically maintain the median
36
+ class MedianFinder {
37
+ private low: MaxHeap<number>; // Max heap, stores the smaller half
38
+ private high: MinHeap<number>; // Min heap, stores the larger half
39
+
40
+ constructor() {
41
+ this.low = new MaxHeap<number>([]);
42
+ this.high = new MinHeap<number>([]);
43
+ }
44
+
45
+ addNum(num: number): void {
46
+ if (this.low.isEmpty() || num <= this.low.peek()!) this.low.add(num);
47
+ else this.high.add(num);
48
+
49
+ // Balance heaps
50
+ if (this.low.size > this.high.size + 1) this.high.add(this.low.poll()!);
51
+ else if (this.high.size > this.low.size) this.low.add(this.high.poll()!);
52
+ }
53
+
54
+ findMedian(): number {
55
+ if (this.low.size === this.high.size) return (this.low.peek()! + this.high.peek()!) / 2;
56
+ return this.low.peek()!;
57
+ }
58
+ }
59
+
60
+ const medianFinder = new MedianFinder();
61
+ medianFinder.addNum(10);
62
+ console.log(medianFinder.findMedian()); // 10;
63
+ medianFinder.addNum(20);
64
+ console.log(medianFinder.findMedian()); // 15;
65
+ medianFinder.addNum(30);
66
+ console.log(medianFinder.findMedian()); // 20;
67
+ medianFinder.addNum(40);
68
+ console.log(medianFinder.findMedian()); // 25;
69
+ medianFinder.addNum(50);
70
+ console.log(medianFinder.findMedian()); // 30;
71
+ ```
72
+
73
+ ```ts
74
+ // Use Heap for load balancing
75
+ function loadBalance(requests: number[], servers: number): number[] {
76
+ const serverHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // min heap
77
+ const serverLoads = new Array(servers).fill(0);
78
+
79
+ for (let i = 0; i < servers; i++) {
80
+ serverHeap.add({ id: i, load: 0 });
81
+ }
82
+
83
+ requests.forEach(req => {
84
+ const server = serverHeap.poll()!;
85
+ serverLoads[server.id] += req;
86
+ server.load += req;
87
+ serverHeap.add(server); // The server after updating the load is re-entered into the heap
88
+ });
89
+
90
+ return serverLoads;
91
+ }
92
+
93
+ const requests = [5, 2, 8, 3, 7];
94
+ console.log(loadBalance(requests, 3)); // [12, 8, 5];
95
+ ```
96
+
97
+ ```ts
98
+ // Use Heap to schedule tasks
99
+ type Task = [string, number];
100
+
101
+ function scheduleTasks(tasks: Task[], machines: number): Map<number, Task[]> {
102
+ const machineHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // Min heap
103
+ const allocation = new Map<number, Task[]>();
104
+
105
+ // Initialize the load on each machine
106
+ for (let i = 0; i < machines; i++) {
107
+ machineHeap.add({ id: i, load: 0 });
108
+ allocation.set(i, []);
109
+ }
110
+
111
+ // Assign tasks
112
+ tasks.forEach(([task, load]) => {
113
+ const machine = machineHeap.poll()!;
114
+ allocation.get(machine.id)!.push([task, load]);
115
+ machine.load += load;
116
+ machineHeap.add(machine); // The machine after updating the load is re-entered into the heap
117
+ });
118
+
119
+ return allocation;
120
+ }
121
+
122
+ const tasks: Task[] = [
123
+ ['Task1', 3],
124
+ ['Task2', 1],
125
+ ['Task3', 2],
126
+ ['Task4', 5],
127
+ ['Task5', 4]
128
+ ];
129
+ const expectedMap = new Map<number, Task[]>();
130
+ expectedMap.set(0, [
131
+ ['Task1', 3],
132
+ ['Task4', 5]
133
+ ]);
134
+ expectedMap.set(1, [
135
+ ['Task2', 1],
136
+ ['Task3', 2],
137
+ ['Task5', 4]
138
+ ]);
139
+ console.log(scheduleTasks(tasks, 2)); // expectedMap;
140
+ ```
141
+
142
+ ```ts
143
+ // Get all elements as array
144
+ const heap = new Heap<number>([5, 1, 3, 2, 4]);
145
+ const arr = heap.toArray();
146
+ console.log(arr.length); // 5;
147
+ console.log(arr.sort()); // [1, 2, 3, 4, 5];
148
+ ```
149
+
150
+ ## Extends
151
+
152
+ - [`IterableElementBase`](IterableElementBase.md)\<`E`, `R`\>
153
+
154
+ ## Extended by
155
+
156
+ - [`MaxHeap`](MaxHeap.md)
157
+ - [`MinHeap`](MinHeap.md)
158
+ - [`PriorityQueue`](PriorityQueue.md)
159
+
160
+ ## Type Parameters
161
+
162
+ ### E
163
+
164
+ `E` = `any`
165
+
166
+ ### R
167
+
168
+ `R` = `any`
169
+
170
+ 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.
171
+ 2. Heap Properties: Each node in a heap follows a specific order property, which varies depending on the type of heap:
172
+ Max Heap: The value of each parent node is greater than or equal to the value of its children.
173
+ Min Heap: The value of each parent node is less than or equal to the value of its children.
174
+ 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.
175
+ 4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
176
+ 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
177
+ 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.
178
+ 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
179
+ 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prime's minimum-spanning tree algorithm, which use heaps to improve performance.
180
+
181
+ ## Constructors
182
+
183
+ ### Constructor
184
+
185
+ ```ts
186
+ new Heap<E, R>(elements?, options?): Heap<E, R>;
187
+ ```
188
+
189
+ Defined in: [data-structures/heap/heap.ts:161](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L161)
190
+
191
+ Create a Heap and optionally bulk-insert elements.
192
+
193
+ #### Parameters
194
+
195
+ ##### elements?
196
+
197
+ `Iterable`\<`E` \| `R`\> = `[]`
198
+
199
+ Iterable of elements (or raw values if toElementFn is set).
200
+
201
+ ##### options?
202
+
203
+ `HeapOptions`\<`E`, `R`\>
204
+
205
+ Options such as comparator and toElementFn.
206
+
207
+ #### Returns
208
+
209
+ `Heap`\<`E`, `R`\>
210
+
211
+ New Heap instance.
212
+
213
+ #### Remarks
214
+
215
+ Time O(N), Space O(N)
216
+
217
+ #### Overrides
218
+
219
+ [`IterableElementBase`](IterableElementBase.md).[`constructor`](IterableElementBase.md#constructor)
220
+
221
+ ## Properties
222
+
223
+ ### comparator
224
+
225
+ #### Get Signature
226
+
227
+ ```ts
228
+ get comparator(): Comparator<E>;
229
+ ```
230
+
231
+ Defined in: [data-structures/heap/heap.ts:1184](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L1184)
232
+
233
+ Get the comparator used to order elements.
234
+
235
+ ##### Remarks
236
+
237
+ Time O(1), Space O(1)
238
+
239
+ ##### Returns
240
+
241
+ `Comparator`\<`E`\>
242
+
243
+ Comparator function.
244
+
245
+ ***
246
+
247
+ ### elements
248
+
249
+ #### Get Signature
250
+
251
+ ```ts
252
+ get elements(): E[];
253
+ ```
254
+
255
+ Defined in: [data-structures/heap/heap.ts:180](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L180)
256
+
257
+ Get the backing array of the heap.
258
+
259
+ ##### Remarks
260
+
261
+ Time O(1), Space O(1)
262
+
263
+ ##### Returns
264
+
265
+ `E`[]
266
+
267
+ Internal elements array.
268
+
269
+ ***
270
+
271
+ ### leaf
272
+
273
+ #### Get Signature
274
+
275
+ ```ts
276
+ get leaf(): E | undefined;
277
+ ```
278
+
279
+ Defined in: [data-structures/heap/heap.ts:244](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L244)
280
+
281
+ Get the last leaf element.
282
+
283
+ ##### Remarks
284
+
285
+ Time O(1), Space O(1)
286
+
287
+ ##### Returns
288
+
289
+ `E` \| `undefined`
290
+
291
+ Last element or undefined.
292
+
293
+ ***
294
+
295
+ ### size
296
+
297
+ #### Get Signature
298
+
299
+ ```ts
300
+ get size(): number;
301
+ ```
302
+
303
+ Defined in: [data-structures/heap/heap.ts:234](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L234)
304
+
305
+ Get the number of elements.
306
+
307
+ ##### Remarks
308
+
309
+ Time O(1), Space O(1)
310
+
311
+ ##### Example
312
+
313
+ ```ts
314
+ // Track heap capacity
315
+ const heap = new Heap<number>();
316
+ console.log(heap.size); // 0;
317
+ heap.add(10);
318
+ heap.add(20);
319
+ console.log(heap.size); // 2;
320
+ heap.poll();
321
+ console.log(heap.size); // 1;
322
+ ```
323
+
324
+ ##### Returns
325
+
326
+ `number`
327
+
328
+ Heap size.
329
+
330
+ *
331
+
332
+ ***
333
+
334
+ ### toElementFn
335
+
336
+ #### Get Signature
337
+
338
+ ```ts
339
+ get toElementFn(): ((rawElement) => E) | undefined;
340
+ ```
341
+
342
+ Defined in: [data-structures/base/iterable-element-base.ts:48](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L48)
343
+
344
+ Exposes the current `toElementFn`, if configured.
345
+
346
+ ##### Remarks
347
+
348
+ Time O(1), Space O(1).
349
+
350
+ ##### Returns
351
+
352
+ ((`rawElement`) => `E`) \| `undefined`
353
+
354
+ The converter function or `undefined` when not set.
355
+
356
+ #### Inherited from
357
+
358
+ [`IterableElementBase`](IterableElementBase.md).[`toElementFn`](IterableElementBase.md#toelementfn)
359
+
360
+ ## Methods
361
+
362
+ ### \[iterator\]()
363
+
364
+ ```ts
365
+ iterator: IterableIterator<E>;
366
+ ```
367
+
368
+ Defined in: [data-structures/base/iterable-element-base.ts:61](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L61)
369
+
370
+ Returns an iterator over the structure's elements.
371
+
372
+ #### Parameters
373
+
374
+ ##### args
375
+
376
+ ...`unknown`[]
377
+
378
+ Optional iterator arguments forwarded to the internal iterator.
379
+
380
+ #### Returns
381
+
382
+ `IterableIterator`\<`E`\>
383
+
384
+ An `IterableIterator<E>` that yields the elements in traversal order.
385
+
386
+ #### Remarks
387
+
388
+ Producing the iterator is O(1); consuming the entire iterator is Time O(n) with O(1) extra space.
389
+
390
+ #### Inherited from
391
+
392
+ [`IterableElementBase`](IterableElementBase.md).[`[iterator]`](IterableElementBase.md#iterator)
393
+
394
+ ***
395
+
396
+ ### add()
397
+
398
+ ```ts
399
+ add(element): boolean;
400
+ ```
401
+
402
+ Defined in: [data-structures/heap/heap.ts:338](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L338)
403
+
404
+ Insert an element.
405
+
406
+ #### Parameters
407
+
408
+ ##### element
409
+
410
+ `E`
411
+
412
+ Element to insert.
413
+
414
+ #### Returns
415
+
416
+ `boolean`
417
+
418
+ True.
419
+
420
+ *
421
+
422
+ #### Remarks
423
+
424
+ Time O(1) amortized, Space O(1)
425
+
426
+ #### Example
427
+
428
+ ```ts
429
+ // basic Heap creation and add operation
430
+ // Create a min heap (default)
431
+ const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
432
+
433
+ // Verify size
434
+ console.log(minHeap.size); // 6;
435
+
436
+ // Add new element
437
+ minHeap.add(4);
438
+ console.log(minHeap.size); // 7;
439
+
440
+ // Min heap property: smallest element at root
441
+ const min = minHeap.peek();
442
+ console.log(min); // 1;
443
+ ```
444
+
445
+ ***
446
+
447
+ ### addMany()
448
+
449
+ ```ts
450
+ addMany(elements): boolean[];
451
+ ```
452
+
453
+ Defined in: [data-structures/heap/heap.ts:388](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L388)
454
+
455
+ Insert many elements from an iterable.
456
+
457
+ #### Parameters
458
+
459
+ ##### elements
460
+
461
+ `Iterable`\<`E` \| `R`\>
462
+
463
+ Iterable of elements or raw values.
464
+
465
+ #### Returns
466
+
467
+ `boolean`[]
468
+
469
+ Array of per-element success flags.
470
+
471
+ *
472
+
473
+ #### Remarks
474
+
475
+ Time O(N log N), Space O(1)
476
+
477
+ #### Example
478
+
479
+ ```ts
480
+ // Add multiple elements
481
+ const heap = new Heap<number>([], { comparator: (a, b) => a - b });
482
+ heap.addMany([5, 3, 7, 1]);
483
+ console.log(heap.peek()); // 1;
484
+ console.log(heap.size); // 4;
485
+ ```
486
+
487
+ ***
488
+
489
+ ### clear()
490
+
491
+ ```ts
492
+ clear(): void;
493
+ ```
494
+
495
+ Defined in: [data-structures/heap/heap.ts:676](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L676)
496
+
497
+ Remove all elements.
498
+
499
+ #### Returns
500
+
501
+ `void`
502
+
503
+ void
504
+
505
+ *
506
+
507
+ #### Remarks
508
+
509
+ Time O(1), Space O(1)
510
+
511
+ #### Example
512
+
513
+ ```ts
514
+ // Remove all elements
515
+ const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
516
+ heap.clear();
517
+ console.log(heap.isEmpty()); // true;
518
+ ```
519
+
520
+ #### Overrides
521
+
522
+ [`IterableElementBase`](IterableElementBase.md).[`clear`](IterableElementBase.md#clear)
523
+
524
+ ***
525
+
526
+ ### clone()
527
+
528
+ ```ts
529
+ clone(): this;
530
+ ```
531
+
532
+ Defined in: [data-structures/heap/heap.ts:1020](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L1020)
533
+
534
+ Deep clone this heap.
535
+
536
+ #### Returns
537
+
538
+ `this`
539
+
540
+ A new heap with the same elements.
541
+
542
+ *
543
+
544
+ #### Remarks
545
+
546
+ Time O(N), Space O(N)
547
+
548
+ #### Example
549
+
550
+ ```ts
551
+ // Create independent copy
552
+ const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
553
+ const copy = heap.clone();
554
+ copy.poll();
555
+ console.log(heap.size); // 3;
556
+ console.log(copy.size); // 2;
557
+ ```
558
+
559
+ #### Overrides
560
+
561
+ [`IterableElementBase`](IterableElementBase.md).[`clone`](IterableElementBase.md#clone)
562
+
563
+ ***
564
+
565
+ ### delete()
566
+
567
+ ```ts
568
+ delete(element): boolean;
569
+ ```
570
+
571
+ Defined in: [data-structures/heap/heap.ts:779](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L779)
572
+
573
+ Delete one occurrence of an element.
574
+
575
+ #### Parameters
576
+
577
+ ##### element
578
+
579
+ `E`
580
+
581
+ Element to delete.
582
+
583
+ #### Returns
584
+
585
+ `boolean`
586
+
587
+ True if an element was removed.
588
+
589
+ *
590
+
591
+ #### Remarks
592
+
593
+ Time O(N), Space O(1)
594
+
595
+ #### Example
596
+
597
+ ```ts
598
+ // Remove specific element
599
+ const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
600
+ heap.delete(4);
601
+ console.log(heap.toArray().includes(4)); // false;
602
+ ```
603
+
604
+ ***
605
+
606
+ ### deleteBy()
607
+
608
+ ```ts
609
+ deleteBy(predicate): boolean;
610
+ ```
611
+
612
+ Defined in: [data-structures/heap/heap.ts:807](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L807)
613
+
614
+ Delete the first element that matches a predicate.
615
+
616
+ #### Parameters
617
+
618
+ ##### predicate
619
+
620
+ (`element`, `index`, `heap`) => `boolean`
621
+
622
+ Function (element, index, heap) → boolean.
623
+
624
+ #### Returns
625
+
626
+ `boolean`
627
+
628
+ True if an element was removed.
629
+
630
+ #### Remarks
631
+
632
+ Time O(N), Space O(1)
633
+
634
+ ***
635
+
636
+ ### dfs()
637
+
638
+ ```ts
639
+ dfs(order?): E[];
640
+ ```
641
+
642
+ Defined in: [data-structures/heap/heap.ts:878](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L878)
643
+
644
+ Traverse the binary heap as a complete binary tree and collect elements.
645
+
646
+ #### Parameters
647
+
648
+ ##### order?
649
+
650
+ `DFSOrderPattern` = `'PRE'`
651
+
652
+ Traversal order: 'PRE' | 'IN' | 'POST'.
653
+
654
+ #### Returns
655
+
656
+ `E`[]
657
+
658
+ Array of visited elements.
659
+
660
+ *
661
+
662
+ #### Remarks
663
+
664
+ Time O(N), Space O(H)
665
+
666
+ #### Example
667
+
668
+ ```ts
669
+ // Depth-first traversal
670
+ const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
671
+ const result = heap.dfs('IN');
672
+ console.log(result.length); // 3;
673
+ ```
674
+
675
+ ***
676
+
677
+ ### every()
678
+
679
+ ```ts
680
+ every(predicate, thisArg?): boolean;
681
+ ```
682
+
683
+ Defined in: [data-structures/base/iterable-element-base.ts:87](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L87)
684
+
685
+ Tests whether all elements satisfy the predicate.
686
+
687
+ #### Parameters
688
+
689
+ ##### predicate
690
+
691
+ `ElementCallback`\<`E`, `R`, `boolean`\>
692
+
693
+ Function invoked for each element with signature `(value, index, self)`.
694
+
695
+ ##### thisArg?
696
+
697
+ `unknown`
698
+
699
+ Optional `this` binding for the predicate.
700
+
701
+ #### Returns
702
+
703
+ `boolean`
704
+
705
+ `true` if every element passes; otherwise `false`.
706
+
707
+ #### Remarks
708
+
709
+ Time O(n) in the worst case; may exit early when the first failure is found. Space O(1).
710
+
711
+ #### Inherited from
712
+
713
+ [`IterableElementBase`](IterableElementBase.md).[`every`](IterableElementBase.md#every)
714
+
715
+ ***
716
+
717
+ ### filter()
718
+
719
+ ```ts
720
+ filter(callback, thisArg?): this;
721
+ ```
722
+
723
+ Defined in: [data-structures/heap/heap.ts:1072](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L1072)
724
+
725
+ Filter elements into a new heap of the same class.
726
+
727
+ #### Parameters
728
+
729
+ ##### callback
730
+
731
+ `ElementCallback`\<`E`, `R`, `boolean`\>
732
+
733
+ Predicate (element, index, heap) → boolean to keep element.
734
+
735
+ ##### thisArg?
736
+
737
+ `unknown`
738
+
739
+ Value for `this` inside the callback.
740
+
741
+ #### Returns
742
+
743
+ `this`
744
+
745
+ A new heap with the kept elements.
746
+
747
+ *
748
+
749
+ #### Remarks
750
+
751
+ Time O(N log N), Space O(N)
752
+
753
+ #### Example
754
+
755
+ ```ts
756
+ // Filter elements
757
+ const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
758
+ const evens = heap.filter(x => x % 2 === 0);
759
+ console.log(evens.size); // 2;
760
+ ```
761
+
762
+ #### Overrides
763
+
764
+ [`IterableElementBase`](IterableElementBase.md).[`filter`](IterableElementBase.md#filter)
765
+
766
+ ***
767
+
768
+ ### find()
769
+
770
+ #### Call Signature
771
+
772
+ ```ts
773
+ find<S>(predicate, thisArg?): S | undefined;
774
+ ```
775
+
776
+ Defined in: [data-structures/base/iterable-element-base.ts:163](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L163)
777
+
778
+ Finds the first element that satisfies the predicate and returns it.
779
+
780
+ Finds the first element of type `S` (a subtype of `E`) that satisfies the predicate and returns it.
781
+
782
+ ##### Type Parameters
783
+
784
+ ###### S
785
+
786
+ `S`
787
+
788
+ ##### Parameters
789
+
790
+ ###### predicate
791
+
792
+ `ElementCallback`\<`E`, `R`, `S`\>
793
+
794
+ Type-guard predicate: `(value, index, self) => value is S`.
795
+
796
+ ###### thisArg?
797
+
798
+ `unknown`
799
+
800
+ Optional `this` binding for the predicate.
801
+
802
+ ##### Returns
803
+
804
+ `S` \| `undefined`
805
+
806
+ The matched element typed as `S`, or `undefined` if not found.
807
+
808
+ ##### Remarks
809
+
810
+ Time O(n) in the worst case; may exit early on the first match. Space O(1).
811
+
812
+ ##### Inherited from
813
+
814
+ [`IterableElementBase`](IterableElementBase.md).[`find`](IterableElementBase.md#find)
815
+
816
+ #### Call Signature
817
+
818
+ ```ts
819
+ find(predicate, thisArg?): E | undefined;
820
+ ```
821
+
822
+ Defined in: [data-structures/base/iterable-element-base.ts:164](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L164)
823
+
824
+ Finds the first element that satisfies the predicate and returns it.
825
+
826
+ Finds the first element of type `S` (a subtype of `E`) that satisfies the predicate and returns it.
827
+
828
+ ##### Parameters
829
+
830
+ ###### predicate
831
+
832
+ `ElementCallback`\<`E`, `R`, `unknown`\>
833
+
834
+ Type-guard predicate: `(value, index, self) => value is S`.
835
+
836
+ ###### thisArg?
837
+
838
+ `unknown`
839
+
840
+ Optional `this` binding for the predicate.
841
+
842
+ ##### Returns
843
+
844
+ `E` \| `undefined`
845
+
846
+ The matched element typed as `S`, or `undefined` if not found.
847
+
848
+ ##### Remarks
849
+
850
+ Time O(n) in the worst case; may exit early on the first match. Space O(1).
851
+
852
+ ##### Inherited from
853
+
854
+ [`IterableElementBase`](IterableElementBase.md).[`find`](IterableElementBase.md#find)
855
+
856
+ ***
857
+
858
+ ### fix()
859
+
860
+ ```ts
861
+ fix(): boolean[];
862
+ ```
863
+
864
+ Defined in: [data-structures/heap/heap.ts:909](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L909)
865
+
866
+ Restore heap order bottom-up (heapify in-place).
867
+
868
+ #### Returns
869
+
870
+ `boolean`[]
871
+
872
+ Array of per-node results from fixing steps.
873
+
874
+ #### Remarks
875
+
876
+ Time O(N), Space O(1)
877
+
878
+ ***
879
+
880
+ ### forEach()
881
+
882
+ ```ts
883
+ forEach(callbackfn, thisArg?): void;
884
+ ```
885
+
886
+ Defined in: [data-structures/base/iterable-element-base.ts:133](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L133)
887
+
888
+ Invokes a callback for each element in iteration order.
889
+
890
+ #### Parameters
891
+
892
+ ##### callbackfn
893
+
894
+ `ElementCallback`\<`E`, `R`, `void`\>
895
+
896
+ Function invoked per element with signature `(value, index, self)`.
897
+
898
+ ##### thisArg?
899
+
900
+ `unknown`
901
+
902
+ Optional `this` binding for the callback.
903
+
904
+ #### Returns
905
+
906
+ `void`
907
+
908
+ `void`.
909
+
910
+ #### Remarks
911
+
912
+ Time O(n), Space O(1).
913
+
914
+ #### Inherited from
915
+
916
+ [`IterableElementBase`](IterableElementBase.md).[`forEach`](IterableElementBase.md#foreach)
917
+
918
+ ***
919
+
920
+ ### has()
921
+
922
+ ```ts
923
+ has(element): boolean;
924
+ ```
925
+
926
+ Defined in: [data-structures/heap/heap.ts:730](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L730)
927
+
928
+ Check if an equal element exists in the heap.
929
+
930
+ #### Parameters
931
+
932
+ ##### element
933
+
934
+ `E`
935
+
936
+ Element to search for.
937
+
938
+ #### Returns
939
+
940
+ `boolean`
941
+
942
+ True if found.
943
+
944
+ *
945
+
946
+ #### Remarks
947
+
948
+ Time O(N), Space O(1)
949
+
950
+ #### Example
951
+
952
+ ```ts
953
+ // Check element existence
954
+ const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
955
+ console.log(heap.has(1)); // true;
956
+ console.log(heap.has(99)); // false;
957
+ ```
958
+
959
+ #### Overrides
960
+
961
+ [`IterableElementBase`](IterableElementBase.md).[`has`](IterableElementBase.md#has)
962
+
963
+ ***
964
+
965
+ ### isEmpty()
966
+
967
+ ```ts
968
+ isEmpty(): boolean;
969
+ ```
970
+
971
+ Defined in: [data-structures/heap/heap.ts:628](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L628)
972
+
973
+ Check whether the heap is empty.
974
+
975
+ #### Returns
976
+
977
+ `boolean`
978
+
979
+ True if size is 0.
980
+
981
+ *
982
+
983
+ #### Remarks
984
+
985
+ Time O(1), Space O(1)
986
+
987
+ #### Example
988
+
989
+ ```ts
990
+ // Check if heap is empty
991
+ const heap = new Heap<number>([], { comparator: (a, b) => a - b });
992
+ console.log(heap.isEmpty()); // true;
993
+ heap.add(1);
994
+ console.log(heap.isEmpty()); // false;
995
+ ```
996
+
997
+ #### Overrides
998
+
999
+ [`IterableElementBase`](IterableElementBase.md).[`isEmpty`](IterableElementBase.md#isempty)
1000
+
1001
+ ***
1002
+
1003
+ ### map()
1004
+
1005
+ ```ts
1006
+ map<EM, RM>(
1007
+ callback,
1008
+ options,
1009
+ thisArg?): Heap<EM, RM>;
1010
+ ```
1011
+
1012
+ Defined in: [data-structures/heap/heap.ts:1133](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L1133)
1013
+
1014
+ Map elements into a new heap of possibly different element type.
1015
+
1016
+ #### Type Parameters
1017
+
1018
+ ##### EM
1019
+
1020
+ `EM`
1021
+
1022
+ ##### RM
1023
+
1024
+ `RM`
1025
+
1026
+ #### Parameters
1027
+
1028
+ ##### callback
1029
+
1030
+ `ElementCallback`\<`E`, `R`, `EM`\>
1031
+
1032
+ Mapping function (element, index, heap) → newElement.
1033
+
1034
+ ##### options
1035
+
1036
+ `IterableElementBaseOptions`\<`EM`, `RM`\> & `object` & `object`
1037
+
1038
+ Options for the output heap, including comparator for EM.
1039
+
1040
+ ##### thisArg?
1041
+
1042
+ `unknown`
1043
+
1044
+ Value for `this` inside the callback.
1045
+
1046
+ #### Returns
1047
+
1048
+ `Heap`\<`EM`, `RM`\>
1049
+
1050
+ A new heap with mapped elements.
1051
+
1052
+ *
1053
+
1054
+ #### Remarks
1055
+
1056
+ Time O(N log N), Space O(N)
1057
+
1058
+ #### Example
1059
+
1060
+ ```ts
1061
+ // Transform elements
1062
+ const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
1063
+ const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
1064
+ console.log(doubled.peek()); // 2;
1065
+ ```
1066
+
1067
+ #### Overrides
1068
+
1069
+ [`IterableElementBase`](IterableElementBase.md).[`map`](IterableElementBase.md#map)
1070
+
1071
+ ***
1072
+
1073
+ ### mapSame()
1074
+
1075
+ ```ts
1076
+ mapSame(callback, thisArg?): this;
1077
+ ```
1078
+
1079
+ Defined in: [data-structures/heap/heap.ts:1157](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L1157)
1080
+
1081
+ Map elements into a new heap of the same element type.
1082
+
1083
+ #### Parameters
1084
+
1085
+ ##### callback
1086
+
1087
+ `ElementCallback`\<`E`, `R`, `E`\>
1088
+
1089
+ Mapping function (element, index, heap) → element.
1090
+
1091
+ ##### thisArg?
1092
+
1093
+ `unknown`
1094
+
1095
+ Value for `this` inside the callback.
1096
+
1097
+ #### Returns
1098
+
1099
+ `this`
1100
+
1101
+ A new heap with mapped elements.
1102
+
1103
+ #### Remarks
1104
+
1105
+ Time O(N log N), Space O(N)
1106
+
1107
+ #### Overrides
1108
+
1109
+ [`IterableElementBase`](IterableElementBase.md).[`mapSame`](IterableElementBase.md#mapsame)
1110
+
1111
+ ***
1112
+
1113
+ ### peek()
1114
+
1115
+ ```ts
1116
+ peek(): E | undefined;
1117
+ ```
1118
+
1119
+ Defined in: [data-structures/heap/heap.ts:579](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L579)
1120
+
1121
+ Get the current top element without removing it.
1122
+
1123
+ #### Returns
1124
+
1125
+ `E` \| `undefined`
1126
+
1127
+ Top element or undefined.
1128
+
1129
+ *
1130
+
1131
+ #### Remarks
1132
+
1133
+ Time O(1), Space O(1)
1134
+
1135
+ #### Example
1136
+
1137
+ ```ts
1138
+ // Heap for event processing with priority
1139
+ interface Event {
1140
+ id: number;
1141
+ type: 'critical' | 'warning' | 'info';
1142
+ timestamp: number;
1143
+ message: string;
1144
+ }
1145
+
1146
+ // Custom priority: critical > warning > info
1147
+ const priorityMap = { critical: 3, warning: 2, info: 1 };
1148
+
1149
+ const eventHeap = new Heap<Event>([], {
1150
+ comparator: (a: Event, b: Event) => {
1151
+ const priorityA = priorityMap[a.type];
1152
+ const priorityB = priorityMap[b.type];
1153
+ return priorityB - priorityA; // Higher priority first
1154
+ }
1155
+ });
1156
+
1157
+ // Add events in random order
1158
+ eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
1159
+ eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
1160
+ eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
1161
+ eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
1162
+ eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
1163
+
1164
+ console.log(eventHeap.size); // 5;
1165
+
1166
+ // Process events by priority (critical first)
1167
+ const processedOrder: Event[] = [];
1168
+ while (eventHeap.size > 0) {
1169
+ const event = eventHeap.poll();
1170
+ if (event) {
1171
+ processedOrder.push(event);
1172
+ }
1173
+ }
1174
+
1175
+ // Verify critical events came first
1176
+ console.log(processedOrder[0].type); // 'critical';
1177
+ console.log(processedOrder[1].type); // 'critical';
1178
+ console.log(processedOrder[2].type); // 'warning';
1179
+ console.log(processedOrder[3].type); // 'info';
1180
+ console.log(processedOrder[4].type); // 'info';
1181
+
1182
+ // Verify O(log n) operations
1183
+ const newHeap = new Heap<number>([5, 3, 7, 1]);
1184
+
1185
+ // Add - O(log n)
1186
+ newHeap.add(2);
1187
+ console.log(newHeap.size); // 5;
1188
+
1189
+ // Poll - O(log n)
1190
+ const removed = newHeap.poll();
1191
+ console.log(removed); // 1;
1192
+
1193
+ // Peek - O(1)
1194
+ const top = newHeap.peek();
1195
+ console.log(top); // 2;
1196
+ ```
1197
+
1198
+ ***
1199
+
1200
+ ### poll()
1201
+
1202
+ ```ts
1203
+ poll(): E | undefined;
1204
+ ```
1205
+
1206
+ Defined in: [data-structures/heap/heap.ts:468](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L468)
1207
+
1208
+ Remove and return the top element.
1209
+
1210
+ #### Returns
1211
+
1212
+ `E` \| `undefined`
1213
+
1214
+ Top element or undefined.
1215
+
1216
+ *
1217
+
1218
+ #### Remarks
1219
+
1220
+ Time O(log N), Space O(1)
1221
+
1222
+ #### Example
1223
+
1224
+ ```ts
1225
+ // Heap with custom comparator (MaxHeap behavior)
1226
+ interface Task {
1227
+ id: number;
1228
+ priority: number;
1229
+ name: string;
1230
+ }
1231
+
1232
+ // Custom comparator for max heap behavior (higher priority first)
1233
+ const tasks: Task[] = [
1234
+ { id: 1, priority: 5, name: 'Email' },
1235
+ { id: 2, priority: 3, name: 'Chat' },
1236
+ { id: 3, priority: 8, name: 'Alert' }
1237
+ ];
1238
+
1239
+ const maxHeap = new Heap(tasks, {
1240
+ comparator: (a: Task, b: Task) => b.priority - a.priority
1241
+ });
1242
+
1243
+ console.log(maxHeap.size); // 3;
1244
+
1245
+ // Peek returns highest priority task
1246
+ const topTask = maxHeap.peek();
1247
+ console.log(topTask?.priority); // 8;
1248
+ console.log(topTask?.name); // 'Alert';
1249
+ ```
1250
+
1251
+ ***
1252
+
1253
+ ### print()
1254
+
1255
+ ```ts
1256
+ print(): void;
1257
+ ```
1258
+
1259
+ Defined in: [data-structures/base/iterable-element-base.ts:269](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L269)
1260
+
1261
+ Prints `toVisual()` to the console. Intended for quick debugging.
1262
+
1263
+ #### Returns
1264
+
1265
+ `void`
1266
+
1267
+ `void`.
1268
+
1269
+ #### Remarks
1270
+
1271
+ Time O(n) due to materialization, Space O(n) for the intermediate representation.
1272
+
1273
+ #### Inherited from
1274
+
1275
+ [`IterableElementBase`](IterableElementBase.md).[`print`](IterableElementBase.md#print)
1276
+
1277
+ ***
1278
+
1279
+ ### reduce()
1280
+
1281
+ Reduces all elements to a single accumulated value.
1282
+
1283
+ #### Param
1284
+
1285
+ Reducer of signature `(acc, value, index, self) => nextAcc`. The first element is used as the initial accumulator.
1286
+
1287
+ #### Param
1288
+
1289
+ Reducer of signature `(acc, value, index, self) => nextAcc`.
1290
+
1291
+ #### Param
1292
+
1293
+ The initial accumulator value of type `E`.
1294
+
1295
+ #### Template
1296
+
1297
+ The accumulator type when it differs from `E`.
1298
+
1299
+ #### Param
1300
+
1301
+ Reducer of signature `(acc: U, value, index, self) => U`.
1302
+
1303
+ #### Param
1304
+
1305
+ The initial accumulator value of type `U`.
1306
+
1307
+ #### Remarks
1308
+
1309
+ Time O(n), Space O(1). Throws if called on an empty structure without `initialValue`.
1310
+
1311
+ #### Call Signature
1312
+
1313
+ ```ts
1314
+ reduce(callbackfn): E;
1315
+ ```
1316
+
1317
+ Defined in: [data-structures/base/iterable-element-base.ts:194](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L194)
1318
+
1319
+ ##### Parameters
1320
+
1321
+ ###### callbackfn
1322
+
1323
+ `ReduceElementCallback`\<`E`, `R`\>
1324
+
1325
+ ##### Returns
1326
+
1327
+ `E`
1328
+
1329
+ ##### Inherited from
1330
+
1331
+ [`IterableElementBase`](IterableElementBase.md).[`reduce`](IterableElementBase.md#reduce)
1332
+
1333
+ #### Call Signature
1334
+
1335
+ ```ts
1336
+ reduce(callbackfn, initialValue): E;
1337
+ ```
1338
+
1339
+ Defined in: [data-structures/base/iterable-element-base.ts:195](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L195)
1340
+
1341
+ ##### Parameters
1342
+
1343
+ ###### callbackfn
1344
+
1345
+ `ReduceElementCallback`\<`E`, `R`\>
1346
+
1347
+ ###### initialValue
1348
+
1349
+ `E`
1350
+
1351
+ ##### Returns
1352
+
1353
+ `E`
1354
+
1355
+ ##### Inherited from
1356
+
1357
+ [`IterableElementBase`](IterableElementBase.md).[`reduce`](IterableElementBase.md#reduce)
1358
+
1359
+ #### Call Signature
1360
+
1361
+ ```ts
1362
+ reduce<U>(callbackfn, initialValue): U;
1363
+ ```
1364
+
1365
+ Defined in: [data-structures/base/iterable-element-base.ts:196](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L196)
1366
+
1367
+ ##### Type Parameters
1368
+
1369
+ ###### U
1370
+
1371
+ `U`
1372
+
1373
+ ##### Parameters
1374
+
1375
+ ###### callbackfn
1376
+
1377
+ `ReduceElementCallback`\<`E`, `R`, `U`\>
1378
+
1379
+ ###### initialValue
1380
+
1381
+ `U`
1382
+
1383
+ ##### Returns
1384
+
1385
+ `U`
1386
+
1387
+ ##### Inherited from
1388
+
1389
+ [`IterableElementBase`](IterableElementBase.md).[`reduce`](IterableElementBase.md#reduce)
1390
+
1391
+ ***
1392
+
1393
+ ### refill()
1394
+
1395
+ ```ts
1396
+ refill(elements): boolean[];
1397
+ ```
1398
+
1399
+ Defined in: [data-structures/heap/heap.ts:687](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L687)
1400
+
1401
+ Replace the backing array and rebuild the heap.
1402
+
1403
+ #### Parameters
1404
+
1405
+ ##### elements
1406
+
1407
+ `Iterable`\<`E`\>
1408
+
1409
+ Iterable used to refill the heap.
1410
+
1411
+ #### Returns
1412
+
1413
+ `boolean`[]
1414
+
1415
+ Array of per-node results from fixing steps.
1416
+
1417
+ #### Remarks
1418
+
1419
+ Time O(N), Space O(N)
1420
+
1421
+ ***
1422
+
1423
+ ### setEquality()
1424
+
1425
+ ```ts
1426
+ setEquality(equals): this;
1427
+ ```
1428
+
1429
+ Defined in: [data-structures/heap/heap.ts:835](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L835)
1430
+
1431
+ Set the equality comparator used by has/delete operations.
1432
+
1433
+ #### Parameters
1434
+
1435
+ ##### equals
1436
+
1437
+ (`a`, `b`) => `boolean`
1438
+
1439
+ Equality predicate (a, b) → boolean.
1440
+
1441
+ #### Returns
1442
+
1443
+ `this`
1444
+
1445
+ This heap.
1446
+
1447
+ #### Remarks
1448
+
1449
+ Time O(1), Space O(1)
1450
+
1451
+ ***
1452
+
1453
+ ### some()
1454
+
1455
+ ```ts
1456
+ some(predicate, thisArg?): boolean;
1457
+ ```
1458
+
1459
+ Defined in: [data-structures/base/iterable-element-base.ts:110](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L110)
1460
+
1461
+ Tests whether at least one element satisfies the predicate.
1462
+
1463
+ #### Parameters
1464
+
1465
+ ##### predicate
1466
+
1467
+ `ElementCallback`\<`E`, `R`, `boolean`\>
1468
+
1469
+ Function invoked for each element with signature `(value, index, self)`.
1470
+
1471
+ ##### thisArg?
1472
+
1473
+ `unknown`
1474
+
1475
+ Optional `this` binding for the predicate.
1476
+
1477
+ #### Returns
1478
+
1479
+ `boolean`
1480
+
1481
+ `true` if any element passes; otherwise `false`.
1482
+
1483
+ #### Remarks
1484
+
1485
+ Time O(n) in the worst case; may exit early on first success. Space O(1).
1486
+
1487
+ #### Inherited from
1488
+
1489
+ [`IterableElementBase`](IterableElementBase.md).[`some`](IterableElementBase.md#some)
1490
+
1491
+ ***
1492
+
1493
+ ### sort()
1494
+
1495
+ ```ts
1496
+ sort(): E[];
1497
+ ```
1498
+
1499
+ Defined in: [data-structures/heap/heap.ts:963](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L963)
1500
+
1501
+ Return all elements in ascending order by repeatedly polling.
1502
+
1503
+ #### Returns
1504
+
1505
+ `E`[]
1506
+
1507
+ Sorted array of elements.
1508
+
1509
+ *
1510
+
1511
+ #### Remarks
1512
+
1513
+ Time O(N log N), Space O(N)
1514
+
1515
+ #### Example
1516
+
1517
+ ```ts
1518
+ // Sort elements using heap
1519
+ const heap = new Heap<number>([5, 1, 3, 2, 4]);
1520
+ const sorted = heap.sort();
1521
+ console.log(sorted); // [1, 2, 3, 4, 5];
1522
+ ```
1523
+
1524
+ ***
1525
+
1526
+ ### toArray()
1527
+
1528
+ ```ts
1529
+ toArray(): E[];
1530
+ ```
1531
+
1532
+ Defined in: [data-structures/base/iterable-element-base.ts:246](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L246)
1533
+
1534
+ Materializes the elements into a new array.
1535
+
1536
+ #### Returns
1537
+
1538
+ `E`[]
1539
+
1540
+ A shallow array copy of the iteration order.
1541
+
1542
+ #### Remarks
1543
+
1544
+ Time O(n), Space O(n).
1545
+
1546
+ #### Inherited from
1547
+
1548
+ [`IterableElementBase`](IterableElementBase.md).[`toArray`](IterableElementBase.md#toarray)
1549
+
1550
+ ***
1551
+
1552
+ ### toVisual()
1553
+
1554
+ ```ts
1555
+ toVisual(): E[];
1556
+ ```
1557
+
1558
+ Defined in: [data-structures/base/iterable-element-base.ts:258](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L258)
1559
+
1560
+ Returns a representation of the structure suitable for quick visualization.
1561
+ Defaults to an array of elements; subclasses may override to provide richer visuals.
1562
+
1563
+ #### Returns
1564
+
1565
+ `E`[]
1566
+
1567
+ A visual representation (array by default).
1568
+
1569
+ #### Remarks
1570
+
1571
+ Time O(n), Space O(n).
1572
+
1573
+ #### Inherited from
1574
+
1575
+ [`IterableElementBase`](IterableElementBase.md).[`toVisual`](IterableElementBase.md#tovisual)
1576
+
1577
+ ***
1578
+
1579
+ ### values()
1580
+
1581
+ ```ts
1582
+ values(): IterableIterator<E>;
1583
+ ```
1584
+
1585
+ Defined in: [data-structures/base/iterable-element-base.ts:72](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L72)
1586
+
1587
+ Returns an iterator over the values (alias of the default iterator).
1588
+
1589
+ #### Returns
1590
+
1591
+ `IterableIterator`\<`E`\>
1592
+
1593
+ An `IterableIterator<E>` over all elements.
1594
+
1595
+ #### Remarks
1596
+
1597
+ Creating the iterator is O(1); full iteration is Time O(n), Space O(1).
1598
+
1599
+ #### Inherited from
1600
+
1601
+ [`IterableElementBase`](IterableElementBase.md).[`values`](IterableElementBase.md#values)
1602
+
1603
+ ***
1604
+
1605
+ ### from()
1606
+
1607
+ ```ts
1608
+ static from<T, R, S>(
1609
+ this,
1610
+ elements?,
1611
+ options?): S;
1612
+ ```
1613
+
1614
+ Defined in: [data-structures/heap/heap.ts:259](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L259)
1615
+
1616
+ Create a heap of the same class from an iterable.
1617
+
1618
+ #### Type Parameters
1619
+
1620
+ ##### T
1621
+
1622
+ `T`
1623
+
1624
+ ##### R
1625
+
1626
+ `R` = `any`
1627
+
1628
+ ##### S
1629
+
1630
+ `S` *extends* `Heap`\<`T`, `R`\> = `Heap`\<`T`, `R`\>
1631
+
1632
+ #### Parameters
1633
+
1634
+ ##### this
1635
+
1636
+ `Object`
1637
+
1638
+ ##### elements?
1639
+
1640
+ `Iterable`\<`T` \| `R`, `any`, `any`\>
1641
+
1642
+ Iterable of elements or raw records.
1643
+
1644
+ ##### options?
1645
+
1646
+ `HeapOptions`\<`T`, `R`\>
1647
+
1648
+ Heap options including comparator.
1649
+
1650
+ #### Returns
1651
+
1652
+ `S`
1653
+
1654
+ A new heap instance of this class.
1655
+
1656
+ #### Remarks
1657
+
1658
+ Time O(N), Space O(N)
1659
+
1660
+ ***
1661
+
1662
+ ### heapify()
1663
+
1664
+ ```ts
1665
+ static heapify<EE, RR>(elements, options): Heap<EE, RR>;
1666
+ ```
1667
+
1668
+ Defined in: [data-structures/heap/heap.ts:277](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L277)
1669
+
1670
+ Build a Heap from an iterable in linear time given a comparator.
1671
+
1672
+ #### Type Parameters
1673
+
1674
+ ##### EE
1675
+
1676
+ `EE` = `any`
1677
+
1678
+ ##### RR
1679
+
1680
+ `RR` = `any`
1681
+
1682
+ #### Parameters
1683
+
1684
+ ##### elements
1685
+
1686
+ `Iterable`\<`EE`\>
1687
+
1688
+ Iterable of elements.
1689
+
1690
+ ##### options
1691
+
1692
+ `HeapOptions`\<`EE`, `RR`\>
1693
+
1694
+ Heap options including comparator.
1695
+
1696
+ #### Returns
1697
+
1698
+ `Heap`\<`EE`, `RR`\>
1699
+
1700
+ A new Heap built from elements.
1701
+
1702
+ #### Remarks
1703
+
1704
+ Time O(N), Space O(N)
1705
+
1706
+
1707
+ ---
1708
+
1709
+ ## Protected Members
1710
+
1711
+ ### \_toElementFn?
1712
+
1713
+ ```ts
1714
+ protected optional _toElementFn?: (rawElement) => E;
1715
+ ```
1716
+
1717
+ Defined in: [data-structures/base/iterable-element-base.ts:39](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L39)
1718
+
1719
+ The converter used to transform a raw element (`R`) into a public element (`E`).
1720
+
1721
+ #### Parameters
1722
+
1723
+ ##### rawElement
1724
+
1725
+ `R`
1726
+
1727
+ #### Returns
1728
+
1729
+ `E`
1730
+
1731
+ #### Remarks
1732
+
1733
+ Time O(1), Space O(1).
1734
+
1735
+ #### Inherited from
1736
+
1737
+ [`IterableElementBase`](IterableElementBase.md).[`_toElementFn`](IterableElementBase.md#_toelementfn)
1738
+
1739
+ ## Accessors
1740
+
1741
+ ### \_createInstance()
1742
+
1743
+ ```ts
1744
+ protected _createInstance(options?): this;
1745
+ ```
1746
+
1747
+ Defined in: [data-structures/heap/heap.ts:1230](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L1230)
1748
+
1749
+ (Protected) Create an empty instance of the same concrete class.
1750
+
1751
+ #### Parameters
1752
+
1753
+ ##### options?
1754
+
1755
+ `HeapOptions`\<`E`, `R`\>
1756
+
1757
+ Options to override comparator or toElementFn.
1758
+
1759
+ #### Returns
1760
+
1761
+ `this`
1762
+
1763
+ A like-kind empty heap instance.
1764
+
1765
+ #### Remarks
1766
+
1767
+ Time O(1), Space O(1)
1768
+
1769
+ ***
1770
+
1771
+ ### \_createLike()
1772
+
1773
+ ```ts
1774
+ protected _createLike<EM, RM>(elements?, options?): Heap<EM, RM>;
1775
+ ```
1776
+
1777
+ Defined in: [data-structures/heap/heap.ts:1248](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L1248)
1778
+
1779
+ (Protected) Create a like-kind instance seeded by elements.
1780
+
1781
+ #### Type Parameters
1782
+
1783
+ ##### EM
1784
+
1785
+ `EM`
1786
+
1787
+ ##### RM
1788
+
1789
+ `RM`
1790
+
1791
+ #### Parameters
1792
+
1793
+ ##### elements?
1794
+
1795
+ `Iterable`\<`EM`, `any`, `any`\> \| `Iterable`\<`RM`, `any`, `any`\>
1796
+
1797
+ Iterable of elements or raw values to seed.
1798
+
1799
+ ##### options?
1800
+
1801
+ `HeapOptions`\<`EM`, `RM`\>
1802
+
1803
+ Options forwarded to the constructor.
1804
+
1805
+ #### Returns
1806
+
1807
+ `Heap`\<`EM`, `RM`\>
1808
+
1809
+ A like-kind heap instance.
1810
+
1811
+ #### Remarks
1812
+
1813
+ Time O(N log N), Space O(N)
1814
+
1815
+ ***
1816
+
1817
+ ### \_getIterator()
1818
+
1819
+ ```ts
1820
+ protected _getIterator(): IterableIterator<E>;
1821
+ ```
1822
+
1823
+ Defined in: [data-structures/heap/heap.ts:1188](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L1188)
1824
+
1825
+ Internal iterator factory used by the default iterator.
1826
+
1827
+ #### Returns
1828
+
1829
+ `IterableIterator`\<`E`\>
1830
+
1831
+ An iterator over elements.
1832
+
1833
+ #### Remarks
1834
+
1835
+ Implementations should yield in O(1) per element with O(1) extra space when possible.
1836
+
1837
+ #### Overrides
1838
+
1839
+ [`IterableElementBase`](IterableElementBase.md).[`_getIterator`](IterableElementBase.md#_getiterator)
1840
+
1841
+ ***
1842
+
1843
+ ### \_spawnLike()
1844
+
1845
+ ```ts
1846
+ protected _spawnLike<EM, RM>(options?): Heap<EM, RM>;
1847
+ ```
1848
+
1849
+ Defined in: [data-structures/heap/heap.ts:1268](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/heap/heap.ts#L1268)
1850
+
1851
+ (Protected) Spawn an empty like-kind heap instance.
1852
+
1853
+ #### Type Parameters
1854
+
1855
+ ##### EM
1856
+
1857
+ `EM`
1858
+
1859
+ ##### RM
1860
+
1861
+ `RM`
1862
+
1863
+ #### Parameters
1864
+
1865
+ ##### options?
1866
+
1867
+ `HeapOptions`\<`EM`, `RM`\>
1868
+
1869
+ Options forwarded to the constructor.
1870
+
1871
+ #### Returns
1872
+
1873
+ `Heap`\<`EM`, `RM`\>
1874
+
1875
+ An empty like-kind heap instance.
1876
+
1877
+ #### Remarks
1878
+
1879
+ Time O(1), Space O(1)
1880
+
1881
+ ***