max-priority-queue-typed 2.4.5 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -0
- package/dist/cjs/index.cjs +400 -119
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +399 -118
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +400 -119
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +399 -118
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +429 -78
- package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +212 -32
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
- package/dist/types/data-structures/graph/directed-graph.d.ts +219 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +204 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
- package/dist/types/data-structures/heap/heap.d.ts +287 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +272 -65
- package/dist/types/data-structures/queue/queue.d.ts +211 -42
- package/dist/types/data-structures/stack/stack.d.ts +174 -32
- package/dist/types/data-structures/trie/trie.d.ts +213 -43
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/umd/max-priority-queue-typed.js +397 -116
- package/dist/umd/max-priority-queue-typed.js.map +1 -1
- package/dist/umd/max-priority-queue-typed.min.js +1 -1
- package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/binary-tree/avl-tree.ts +134 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +302 -247
- package/src/data-structures/binary-tree/binary-tree.ts +429 -79
- package/src/data-structures/binary-tree/bst.ts +335 -34
- package/src/data-structures/binary-tree/red-black-tree.ts +290 -97
- package/src/data-structures/binary-tree/segment-tree.ts +372 -248
- package/src/data-structures/binary-tree/tree-map.ts +1284 -6
- package/src/data-structures/binary-tree/tree-multi-map.ts +1094 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +858 -65
- package/src/data-structures/binary-tree/tree-set.ts +1136 -9
- package/src/data-structures/graph/directed-graph.ts +219 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +204 -59
- package/src/data-structures/hash/hash-map.ts +230 -77
- package/src/data-structures/heap/heap.ts +287 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
- package/src/data-structures/matrix/matrix.ts +416 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +272 -65
- package/src/data-structures/queue/queue.ts +211 -42
- package/src/data-structures/stack/stack.ts +174 -32
- package/src/data-structures/trie/trie.ts +213 -43
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
|
@@ -26,105 +26,6 @@ import { ERR } from '../../common';
|
|
|
26
26
|
* 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
|
|
27
27
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prime's minimum-spanning tree algorithm, which use heaps to improve performance.
|
|
28
28
|
* @example
|
|
29
|
-
* // basic Heap creation and add operation
|
|
30
|
-
* // Create a min heap (default)
|
|
31
|
-
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
32
|
-
*
|
|
33
|
-
* // Verify size
|
|
34
|
-
* console.log(minHeap.size); // 6;
|
|
35
|
-
*
|
|
36
|
-
* // Add new element
|
|
37
|
-
* minHeap.add(4);
|
|
38
|
-
* console.log(minHeap.size); // 7;
|
|
39
|
-
*
|
|
40
|
-
* // Min heap property: smallest element at root
|
|
41
|
-
* const min = minHeap.peek();
|
|
42
|
-
* console.log(min); // 1;
|
|
43
|
-
* @example
|
|
44
|
-
* // Heap with custom comparator (MaxHeap behavior)
|
|
45
|
-
* interface Task {
|
|
46
|
-
* id: number;
|
|
47
|
-
* priority: number;
|
|
48
|
-
* name: string;
|
|
49
|
-
* }
|
|
50
|
-
*
|
|
51
|
-
* // Custom comparator for max heap behavior (higher priority first)
|
|
52
|
-
* const tasks: Task[] = [
|
|
53
|
-
* { id: 1, priority: 5, name: 'Email' },
|
|
54
|
-
* { id: 2, priority: 3, name: 'Chat' },
|
|
55
|
-
* { id: 3, priority: 8, name: 'Alert' }
|
|
56
|
-
* ];
|
|
57
|
-
*
|
|
58
|
-
* const maxHeap = new Heap(tasks, {
|
|
59
|
-
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
60
|
-
* });
|
|
61
|
-
*
|
|
62
|
-
* console.log(maxHeap.size); // 3;
|
|
63
|
-
*
|
|
64
|
-
* // Peek returns highest priority task
|
|
65
|
-
* const topTask = maxHeap.peek();
|
|
66
|
-
* console.log(topTask?.priority); // 8;
|
|
67
|
-
* console.log(topTask?.name); // 'Alert';
|
|
68
|
-
* @example
|
|
69
|
-
* // Heap for event processing with priority
|
|
70
|
-
* interface Event {
|
|
71
|
-
* id: number;
|
|
72
|
-
* type: 'critical' | 'warning' | 'info';
|
|
73
|
-
* timestamp: number;
|
|
74
|
-
* message: string;
|
|
75
|
-
* }
|
|
76
|
-
*
|
|
77
|
-
* // Custom priority: critical > warning > info
|
|
78
|
-
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
79
|
-
*
|
|
80
|
-
* const eventHeap = new Heap<Event>([], {
|
|
81
|
-
* comparator: (a: Event, b: Event) => {
|
|
82
|
-
* const priorityA = priorityMap[a.type];
|
|
83
|
-
* const priorityB = priorityMap[b.type];
|
|
84
|
-
* return priorityB - priorityA; // Higher priority first
|
|
85
|
-
* }
|
|
86
|
-
* });
|
|
87
|
-
*
|
|
88
|
-
* // Add events in random order
|
|
89
|
-
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
90
|
-
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
91
|
-
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
92
|
-
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
93
|
-
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
94
|
-
*
|
|
95
|
-
* console.log(eventHeap.size); // 5;
|
|
96
|
-
*
|
|
97
|
-
* // Process events by priority (critical first)
|
|
98
|
-
* const processedOrder: Event[] = [];
|
|
99
|
-
* while (eventHeap.size > 0) {
|
|
100
|
-
* const event = eventHeap.poll();
|
|
101
|
-
* if (event) {
|
|
102
|
-
* processedOrder.push(event);
|
|
103
|
-
* }
|
|
104
|
-
* }
|
|
105
|
-
*
|
|
106
|
-
* // Verify critical events came first
|
|
107
|
-
* console.log(processedOrder[0].type); // 'critical';
|
|
108
|
-
* console.log(processedOrder[1].type); // 'critical';
|
|
109
|
-
* console.log(processedOrder[2].type); // 'warning';
|
|
110
|
-
* console.log(processedOrder[3].type); // 'info';
|
|
111
|
-
* console.log(processedOrder[4].type); // 'info';
|
|
112
|
-
*
|
|
113
|
-
* // Verify O(log n) operations
|
|
114
|
-
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
115
|
-
*
|
|
116
|
-
* // Add - O(log n)
|
|
117
|
-
* newHeap.add(2);
|
|
118
|
-
* console.log(newHeap.size); // 5;
|
|
119
|
-
*
|
|
120
|
-
* // Poll - O(log n)
|
|
121
|
-
* const removed = newHeap.poll();
|
|
122
|
-
* console.log(removed); // 1;
|
|
123
|
-
*
|
|
124
|
-
* // Peek - O(1)
|
|
125
|
-
* const top = newHeap.peek();
|
|
126
|
-
* console.log(top); // 2;
|
|
127
|
-
* @example
|
|
128
29
|
* // Use Heap to solve top k problems
|
|
129
30
|
* function topKElements(arr: number[], k: number): number[] {
|
|
130
31
|
* const heap = new Heap<number>([], { comparator: (a, b) => b - a }); // Max heap
|
|
@@ -239,6 +140,12 @@ import { ERR } from '../../common';
|
|
|
239
140
|
* ['Task5', 4]
|
|
240
141
|
* ]);
|
|
241
142
|
* console.log(scheduleTasks(tasks, 2)); // expectedMap;
|
|
143
|
+
* @example
|
|
144
|
+
* // Get all elements as array
|
|
145
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
146
|
+
* const arr = heap.toArray();
|
|
147
|
+
* console.log(arr.length); // 5;
|
|
148
|
+
* console.log(arr.sort()); // [1, 2, 3, 4, 5];
|
|
242
149
|
*/
|
|
243
150
|
export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
244
151
|
protected _equals: (a: E, b: E) => boolean = Object.is;
|
|
@@ -278,6 +185,26 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
278
185
|
* Get the number of elements.
|
|
279
186
|
* @remarks Time O(1), Space O(1)
|
|
280
187
|
* @returns Heap size.
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
* @example
|
|
200
|
+
* // Track heap capacity
|
|
201
|
+
* const heap = new Heap<number>();
|
|
202
|
+
* console.log(heap.size); // 0;
|
|
203
|
+
* heap.add(10);
|
|
204
|
+
* heap.add(20);
|
|
205
|
+
* console.log(heap.size); // 2;
|
|
206
|
+
* heap.poll();
|
|
207
|
+
* console.log(heap.size); // 1;
|
|
281
208
|
*/
|
|
282
209
|
|
|
283
210
|
get size(): number {
|
|
@@ -332,6 +259,32 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
332
259
|
* @remarks Time O(1) amortized, Space O(1)
|
|
333
260
|
* @param element - Element to insert.
|
|
334
261
|
* @returns True.
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
* @example
|
|
274
|
+
* // basic Heap creation and add operation
|
|
275
|
+
* // Create a min heap (default)
|
|
276
|
+
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
277
|
+
*
|
|
278
|
+
* // Verify size
|
|
279
|
+
* console.log(minHeap.size); // 6;
|
|
280
|
+
*
|
|
281
|
+
* // Add new element
|
|
282
|
+
* minHeap.add(4);
|
|
283
|
+
* console.log(minHeap.size); // 7;
|
|
284
|
+
*
|
|
285
|
+
* // Min heap property: smallest element at root
|
|
286
|
+
* const min = minHeap.peek();
|
|
287
|
+
* console.log(min); // 1;
|
|
335
288
|
*/
|
|
336
289
|
|
|
337
290
|
add(element: E): boolean {
|
|
@@ -344,6 +297,20 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
344
297
|
* @remarks Time O(N log N), Space O(1)
|
|
345
298
|
* @param elements - Iterable of elements or raw values.
|
|
346
299
|
* @returns Array of per-element success flags.
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
* @example
|
|
309
|
+
* // Add multiple elements
|
|
310
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
311
|
+
* heap.addMany([5, 3, 7, 1]);
|
|
312
|
+
* console.log(heap.peek()); // 1;
|
|
313
|
+
* console.log(heap.size); // 4;
|
|
347
314
|
*/
|
|
348
315
|
|
|
349
316
|
addMany(elements: Iterable<E | R>): boolean[] {
|
|
@@ -364,6 +331,42 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
364
331
|
* Remove and return the top element.
|
|
365
332
|
* @remarks Time O(log N), Space O(1)
|
|
366
333
|
* @returns Top element or undefined.
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
* @example
|
|
346
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
347
|
+
* interface Task {
|
|
348
|
+
* id: number;
|
|
349
|
+
* priority: number;
|
|
350
|
+
* name: string;
|
|
351
|
+
* }
|
|
352
|
+
*
|
|
353
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
354
|
+
* const tasks: Task[] = [
|
|
355
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
356
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
357
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
358
|
+
* ];
|
|
359
|
+
*
|
|
360
|
+
* const maxHeap = new Heap(tasks, {
|
|
361
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
362
|
+
* });
|
|
363
|
+
*
|
|
364
|
+
* console.log(maxHeap.size); // 3;
|
|
365
|
+
*
|
|
366
|
+
* // Peek returns highest priority task
|
|
367
|
+
* const topTask = maxHeap.peek();
|
|
368
|
+
* console.log(topTask?.priority); // 8;
|
|
369
|
+
* console.log(topTask?.name); // 'Alert';
|
|
367
370
|
*/
|
|
368
371
|
|
|
369
372
|
poll(): E | undefined {
|
|
@@ -381,6 +384,76 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
381
384
|
* Get the current top element without removing it.
|
|
382
385
|
* @remarks Time O(1), Space O(1)
|
|
383
386
|
* @returns Top element or undefined.
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
* @example
|
|
399
|
+
* // Heap for event processing with priority
|
|
400
|
+
* interface Event {
|
|
401
|
+
* id: number;
|
|
402
|
+
* type: 'critical' | 'warning' | 'info';
|
|
403
|
+
* timestamp: number;
|
|
404
|
+
* message: string;
|
|
405
|
+
* }
|
|
406
|
+
*
|
|
407
|
+
* // Custom priority: critical > warning > info
|
|
408
|
+
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
409
|
+
*
|
|
410
|
+
* const eventHeap = new Heap<Event>([], {
|
|
411
|
+
* comparator: (a: Event, b: Event) => {
|
|
412
|
+
* const priorityA = priorityMap[a.type];
|
|
413
|
+
* const priorityB = priorityMap[b.type];
|
|
414
|
+
* return priorityB - priorityA; // Higher priority first
|
|
415
|
+
* }
|
|
416
|
+
* });
|
|
417
|
+
*
|
|
418
|
+
* // Add events in random order
|
|
419
|
+
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
420
|
+
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
421
|
+
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
422
|
+
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
423
|
+
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
424
|
+
*
|
|
425
|
+
* console.log(eventHeap.size); // 5;
|
|
426
|
+
*
|
|
427
|
+
* // Process events by priority (critical first)
|
|
428
|
+
* const processedOrder: Event[] = [];
|
|
429
|
+
* while (eventHeap.size > 0) {
|
|
430
|
+
* const event = eventHeap.poll();
|
|
431
|
+
* if (event) {
|
|
432
|
+
* processedOrder.push(event);
|
|
433
|
+
* }
|
|
434
|
+
* }
|
|
435
|
+
*
|
|
436
|
+
* // Verify critical events came first
|
|
437
|
+
* console.log(processedOrder[0].type); // 'critical';
|
|
438
|
+
* console.log(processedOrder[1].type); // 'critical';
|
|
439
|
+
* console.log(processedOrder[2].type); // 'warning';
|
|
440
|
+
* console.log(processedOrder[3].type); // 'info';
|
|
441
|
+
* console.log(processedOrder[4].type); // 'info';
|
|
442
|
+
*
|
|
443
|
+
* // Verify O(log n) operations
|
|
444
|
+
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
445
|
+
*
|
|
446
|
+
* // Add - O(log n)
|
|
447
|
+
* newHeap.add(2);
|
|
448
|
+
* console.log(newHeap.size); // 5;
|
|
449
|
+
*
|
|
450
|
+
* // Poll - O(log n)
|
|
451
|
+
* const removed = newHeap.poll();
|
|
452
|
+
* console.log(removed); // 1;
|
|
453
|
+
*
|
|
454
|
+
* // Peek - O(1)
|
|
455
|
+
* const top = newHeap.peek();
|
|
456
|
+
* console.log(top); // 2;
|
|
384
457
|
*/
|
|
385
458
|
|
|
386
459
|
peek(): E | undefined {
|
|
@@ -391,6 +464,21 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
391
464
|
* Check whether the heap is empty.
|
|
392
465
|
* @remarks Time O(1), Space O(1)
|
|
393
466
|
* @returns True if size is 0.
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
* @example
|
|
477
|
+
* // Check if heap is empty
|
|
478
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
479
|
+
* console.log(heap.isEmpty()); // true;
|
|
480
|
+
* heap.add(1);
|
|
481
|
+
* console.log(heap.isEmpty()); // false;
|
|
394
482
|
*/
|
|
395
483
|
|
|
396
484
|
isEmpty(): boolean {
|
|
@@ -401,6 +489,20 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
401
489
|
* Remove all elements.
|
|
402
490
|
* @remarks Time O(1), Space O(1)
|
|
403
491
|
* @returns void
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
* @example
|
|
502
|
+
* // Remove all elements
|
|
503
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
504
|
+
* heap.clear();
|
|
505
|
+
* console.log(heap.isEmpty()); // true;
|
|
404
506
|
*/
|
|
405
507
|
|
|
406
508
|
clear(): void {
|
|
@@ -424,6 +526,13 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
424
526
|
* @remarks Time O(N), Space O(1)
|
|
425
527
|
* @param element - Element to search for.
|
|
426
528
|
* @returns True if found.
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
* @example
|
|
532
|
+
* // Check element existence
|
|
533
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
534
|
+
* console.log(heap.has(1)); // true;
|
|
535
|
+
* console.log(heap.has(99)); // false;
|
|
427
536
|
*/
|
|
428
537
|
|
|
429
538
|
override has(element: E): boolean {
|
|
@@ -436,6 +545,19 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
436
545
|
* @remarks Time O(N), Space O(1)
|
|
437
546
|
* @param element - Element to delete.
|
|
438
547
|
* @returns True if an element was removed.
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
* @example
|
|
557
|
+
* // Remove specific element
|
|
558
|
+
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
559
|
+
* heap.delete(4);
|
|
560
|
+
* console.log(heap.toArray().includes(4)); // false;
|
|
439
561
|
*/
|
|
440
562
|
|
|
441
563
|
delete(element: E): boolean {
|
|
@@ -504,6 +626,13 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
504
626
|
* @remarks Time O(N), Space O(H)
|
|
505
627
|
* @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
506
628
|
* @returns Array of visited elements.
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
* @example
|
|
632
|
+
* // Depth-first traversal
|
|
633
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
634
|
+
* const result = heap.dfs('IN');
|
|
635
|
+
* console.log(result.length); // 3;
|
|
507
636
|
*/
|
|
508
637
|
|
|
509
638
|
dfs(order: DFSOrderPattern = 'PRE'): E[] {
|
|
@@ -549,6 +678,22 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
549
678
|
* Return all elements in ascending order by repeatedly polling.
|
|
550
679
|
* @remarks Time O(N log N), Space O(N)
|
|
551
680
|
* @returns Sorted array of elements.
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
* @example
|
|
693
|
+
* // Sort elements using heap
|
|
694
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
695
|
+
* const sorted = heap.sort();
|
|
696
|
+
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
552
697
|
*/
|
|
553
698
|
|
|
554
699
|
sort(): E[] {
|
|
@@ -566,6 +711,22 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
566
711
|
* Deep clone this heap.
|
|
567
712
|
* @remarks Time O(N), Space O(N)
|
|
568
713
|
* @returns A new heap with the same elements.
|
|
714
|
+
|
|
715
|
+
|
|
716
|
+
|
|
717
|
+
|
|
718
|
+
|
|
719
|
+
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
|
|
723
|
+
* @example
|
|
724
|
+
* // Create independent copy
|
|
725
|
+
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
726
|
+
* const copy = heap.clone();
|
|
727
|
+
* copy.poll();
|
|
728
|
+
* console.log(heap.size); // 3;
|
|
729
|
+
* console.log(copy.size); // 2;
|
|
569
730
|
*/
|
|
570
731
|
|
|
571
732
|
clone(): this {
|
|
@@ -580,6 +741,20 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
580
741
|
* @param callback - Predicate (element, index, heap) → boolean to keep element.
|
|
581
742
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
582
743
|
* @returns A new heap with the kept elements.
|
|
744
|
+
|
|
745
|
+
|
|
746
|
+
|
|
747
|
+
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
|
|
753
|
+
* @example
|
|
754
|
+
* // Filter elements
|
|
755
|
+
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
756
|
+
* const evens = heap.filter(x => x % 2 === 0);
|
|
757
|
+
* console.log(evens.size); // 2;
|
|
583
758
|
*/
|
|
584
759
|
|
|
585
760
|
filter(callback: ElementCallback<E, R, boolean>, thisArg?: unknown): this {
|
|
@@ -604,6 +779,19 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
604
779
|
* @param options - Options for the output heap, including comparator for EM.
|
|
605
780
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
606
781
|
* @returns A new heap with mapped elements.
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
|
|
785
|
+
|
|
786
|
+
|
|
787
|
+
|
|
788
|
+
|
|
789
|
+
|
|
790
|
+
* @example
|
|
791
|
+
* // Transform elements
|
|
792
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
793
|
+
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
794
|
+
* console.log(doubled.peek()); // 2;
|
|
607
795
|
*/
|
|
608
796
|
|
|
609
797
|
map<EM, RM>(
|
|
@@ -23,6 +23,52 @@ import { ERR } from '../../common';
|
|
|
23
23
|
* 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
|
|
24
24
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum-spanning tree algorithm, which use heaps to improve performance.
|
|
25
25
|
* @example
|
|
26
|
+
* // Find the K largest elements
|
|
27
|
+
* const data = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
|
|
28
|
+
* const heap = new MaxHeap(data);
|
|
29
|
+
*
|
|
30
|
+
* // Extract top 3 elements
|
|
31
|
+
* const top3 = [];
|
|
32
|
+
* for (let i = 0; i < 3; i++) {
|
|
33
|
+
* top3.push(heap.poll());
|
|
34
|
+
* }
|
|
35
|
+
* console.log(top3); // [9, 6, 5];
|
|
36
|
+
* @example
|
|
37
|
+
* // Priority-based task processing
|
|
38
|
+
* interface Task {
|
|
39
|
+
* name: string;
|
|
40
|
+
* priority: number;
|
|
41
|
+
* }
|
|
42
|
+
*
|
|
43
|
+
* const heap = new MaxHeap<Task>([], {
|
|
44
|
+
* comparator: (a, b) => b.priority - a.priority
|
|
45
|
+
* });
|
|
46
|
+
*
|
|
47
|
+
* heap.add({ name: 'Low priority', priority: 1 });
|
|
48
|
+
* heap.add({ name: 'Critical fix', priority: 10 });
|
|
49
|
+
* heap.add({ name: 'Medium task', priority: 5 });
|
|
50
|
+
*
|
|
51
|
+
* // Highest priority first
|
|
52
|
+
* console.log(heap.poll()?.name); // 'Critical fix';
|
|
53
|
+
* console.log(heap.poll()?.name); // 'Medium task';
|
|
54
|
+
* console.log(heap.poll()?.name); // 'Low priority';
|
|
55
|
+
* @example
|
|
56
|
+
* // Real-time top score tracking
|
|
57
|
+
* const scores = new MaxHeap<number>();
|
|
58
|
+
*
|
|
59
|
+
* // Stream of scores coming in
|
|
60
|
+
* for (const score of [72, 85, 91, 68, 95, 78, 88]) {
|
|
61
|
+
* scores.add(score);
|
|
62
|
+
* }
|
|
63
|
+
*
|
|
64
|
+
* // Current highest score without removing
|
|
65
|
+
* console.log(scores.peek()); // 95;
|
|
66
|
+
* console.log(scores.size); // 7;
|
|
67
|
+
*
|
|
68
|
+
* // Remove top 2 scores
|
|
69
|
+
* console.log(scores.poll()); // 95;
|
|
70
|
+
* console.log(scores.poll()); // 91;
|
|
71
|
+
* console.log(scores.peek()); // 88;
|
|
26
72
|
*/
|
|
27
73
|
export class MaxHeap<E = any, R = any> extends Heap<E, R> {
|
|
28
74
|
/**
|
|
@@ -23,6 +23,65 @@ import { Heap } from './heap';
|
|
|
23
23
|
* 7. Efficient Sorting Algorithms: For example, heap sort. MinHeap sort uses the properties of a heap to sort elements.
|
|
24
24
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
25
25
|
* @example
|
|
26
|
+
* // Merge K sorted arrays
|
|
27
|
+
* const arrays = [
|
|
28
|
+
* [1, 4, 7],
|
|
29
|
+
* [2, 5, 8],
|
|
30
|
+
* [3, 6, 9]
|
|
31
|
+
* ];
|
|
32
|
+
*
|
|
33
|
+
* // Use min heap to merge: track (value, arrayIndex, elementIndex)
|
|
34
|
+
* const heap = new MinHeap<[number, number, number]>([], {
|
|
35
|
+
* comparator: (a, b) => a[0] - b[0]
|
|
36
|
+
* });
|
|
37
|
+
*
|
|
38
|
+
* // Initialize with first element of each array
|
|
39
|
+
* arrays.forEach((arr, i) => heap.add([arr[0], i, 0]));
|
|
40
|
+
*
|
|
41
|
+
* const merged: number[] = [];
|
|
42
|
+
* while (heap.size > 0) {
|
|
43
|
+
* const [val, arrIdx, elemIdx] = heap.poll()!;
|
|
44
|
+
* merged.push(val);
|
|
45
|
+
* if (elemIdx + 1 < arrays[arrIdx].length) {
|
|
46
|
+
* heap.add([arrays[arrIdx][elemIdx + 1], arrIdx, elemIdx + 1]);
|
|
47
|
+
* }
|
|
48
|
+
* }
|
|
49
|
+
*
|
|
50
|
+
* console.log(merged); // [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|
51
|
+
* @example
|
|
52
|
+
* // Dijkstra-style shortest distance tracking
|
|
53
|
+
* // Simulating distance updates: (distance, nodeId)
|
|
54
|
+
* const heap = new MinHeap<[number, string]>([], {
|
|
55
|
+
* comparator: (a, b) => a[0] - b[0]
|
|
56
|
+
* });
|
|
57
|
+
*
|
|
58
|
+
* heap.add([0, 'start']);
|
|
59
|
+
* heap.add([10, 'A']);
|
|
60
|
+
* heap.add([5, 'B']);
|
|
61
|
+
* heap.add([3, 'C']);
|
|
62
|
+
*
|
|
63
|
+
* // Process nearest node first
|
|
64
|
+
* console.log(heap.poll()); // [0, 'start'];
|
|
65
|
+
* console.log(heap.poll()); // [3, 'C'];
|
|
66
|
+
* console.log(heap.poll()); // [5, 'B'];
|
|
67
|
+
* console.log(heap.poll()); // [10, 'A'];
|
|
68
|
+
* @example
|
|
69
|
+
* // Running median with min heap (upper half)
|
|
70
|
+
* const upperHalf = new MinHeap<number>();
|
|
71
|
+
*
|
|
72
|
+
* // Add larger numbers to min heap
|
|
73
|
+
* for (const n of [5, 8, 3, 9, 1]) {
|
|
74
|
+
* upperHalf.add(n);
|
|
75
|
+
* }
|
|
76
|
+
*
|
|
77
|
+
* // Smallest of the upper half is always accessible
|
|
78
|
+
* console.log(upperHalf.peek()); // 1;
|
|
79
|
+
* console.log(upperHalf.size); // 5;
|
|
80
|
+
*
|
|
81
|
+
* // Remove smallest repeatedly
|
|
82
|
+
* console.log(upperHalf.poll()); // 1;
|
|
83
|
+
* console.log(upperHalf.poll()); // 3;
|
|
84
|
+
* console.log(upperHalf.peek()); // 5;
|
|
26
85
|
*/
|
|
27
86
|
export class MinHeap<E = any, R = any> extends Heap<E, R> {
|
|
28
87
|
/**
|