max-priority-queue-typed 2.4.5 → 2.5.1
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 +694 -119
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +693 -118
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +694 -119
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +693 -118
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +380 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +487 -147
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +956 -80
- package/dist/types/data-structures/binary-tree/bst.d.ts +816 -29
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +610 -31
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +326 -135
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3781 -6
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3607 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2874 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3528 -6
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +429 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +393 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +473 -89
- package/dist/types/data-structures/heap/heap.d.ts +581 -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 +646 -47
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +596 -68
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +793 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +499 -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 +593 -71
- package/dist/types/data-structures/queue/queue.d.ts +463 -42
- package/dist/types/data-structures/stack/stack.d.ts +384 -32
- package/dist/types/data-structures/trie/trie.d.ts +470 -48
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/max-priority-queue-typed.js +691 -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/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +386 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
- package/src/data-structures/binary-tree/binary-tree.ts +956 -81
- package/src/data-structures/binary-tree/bst.ts +840 -35
- package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
- package/src/data-structures/binary-tree/segment-tree.ts +498 -249
- package/src/data-structures/binary-tree/tree-map.ts +3784 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
- package/src/data-structures/binary-tree/tree-set.ts +3531 -10
- package/src/data-structures/graph/abstract-graph.ts +4 -4
- package/src/data-structures/graph/directed-graph.ts +429 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +393 -59
- package/src/data-structures/hash/hash-map.ts +476 -92
- package/src/data-structures/heap/heap.ts +581 -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 +646 -47
- package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
- package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
- package/src/data-structures/matrix/matrix.ts +584 -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 +592 -70
- package/src/data-structures/queue/queue.ts +463 -42
- package/src/data-structures/stack/stack.ts +384 -32
- package/src/data-structures/trie/trie.ts +470 -48
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
|
@@ -23,105 +23,6 @@ import { IterableElementBase } from '../base';
|
|
|
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 Prime's minimum-spanning tree algorithm, which use heaps to improve performance.
|
|
25
25
|
* @example
|
|
26
|
-
* // basic Heap creation and add operation
|
|
27
|
-
* // Create a min heap (default)
|
|
28
|
-
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
29
|
-
*
|
|
30
|
-
* // Verify size
|
|
31
|
-
* console.log(minHeap.size); // 6;
|
|
32
|
-
*
|
|
33
|
-
* // Add new element
|
|
34
|
-
* minHeap.add(4);
|
|
35
|
-
* console.log(minHeap.size); // 7;
|
|
36
|
-
*
|
|
37
|
-
* // Min heap property: smallest element at root
|
|
38
|
-
* const min = minHeap.peek();
|
|
39
|
-
* console.log(min); // 1;
|
|
40
|
-
* @example
|
|
41
|
-
* // Heap with custom comparator (MaxHeap behavior)
|
|
42
|
-
* interface Task {
|
|
43
|
-
* id: number;
|
|
44
|
-
* priority: number;
|
|
45
|
-
* name: string;
|
|
46
|
-
* }
|
|
47
|
-
*
|
|
48
|
-
* // Custom comparator for max heap behavior (higher priority first)
|
|
49
|
-
* const tasks: Task[] = [
|
|
50
|
-
* { id: 1, priority: 5, name: 'Email' },
|
|
51
|
-
* { id: 2, priority: 3, name: 'Chat' },
|
|
52
|
-
* { id: 3, priority: 8, name: 'Alert' }
|
|
53
|
-
* ];
|
|
54
|
-
*
|
|
55
|
-
* const maxHeap = new Heap(tasks, {
|
|
56
|
-
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
57
|
-
* });
|
|
58
|
-
*
|
|
59
|
-
* console.log(maxHeap.size); // 3;
|
|
60
|
-
*
|
|
61
|
-
* // Peek returns highest priority task
|
|
62
|
-
* const topTask = maxHeap.peek();
|
|
63
|
-
* console.log(topTask?.priority); // 8;
|
|
64
|
-
* console.log(topTask?.name); // 'Alert';
|
|
65
|
-
* @example
|
|
66
|
-
* // Heap for event processing with priority
|
|
67
|
-
* interface Event {
|
|
68
|
-
* id: number;
|
|
69
|
-
* type: 'critical' | 'warning' | 'info';
|
|
70
|
-
* timestamp: number;
|
|
71
|
-
* message: string;
|
|
72
|
-
* }
|
|
73
|
-
*
|
|
74
|
-
* // Custom priority: critical > warning > info
|
|
75
|
-
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
76
|
-
*
|
|
77
|
-
* const eventHeap = new Heap<Event>([], {
|
|
78
|
-
* comparator: (a: Event, b: Event) => {
|
|
79
|
-
* const priorityA = priorityMap[a.type];
|
|
80
|
-
* const priorityB = priorityMap[b.type];
|
|
81
|
-
* return priorityB - priorityA; // Higher priority first
|
|
82
|
-
* }
|
|
83
|
-
* });
|
|
84
|
-
*
|
|
85
|
-
* // Add events in random order
|
|
86
|
-
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
87
|
-
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
88
|
-
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
89
|
-
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
90
|
-
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
91
|
-
*
|
|
92
|
-
* console.log(eventHeap.size); // 5;
|
|
93
|
-
*
|
|
94
|
-
* // Process events by priority (critical first)
|
|
95
|
-
* const processedOrder: Event[] = [];
|
|
96
|
-
* while (eventHeap.size > 0) {
|
|
97
|
-
* const event = eventHeap.poll();
|
|
98
|
-
* if (event) {
|
|
99
|
-
* processedOrder.push(event);
|
|
100
|
-
* }
|
|
101
|
-
* }
|
|
102
|
-
*
|
|
103
|
-
* // Verify critical events came first
|
|
104
|
-
* console.log(processedOrder[0].type); // 'critical';
|
|
105
|
-
* console.log(processedOrder[1].type); // 'critical';
|
|
106
|
-
* console.log(processedOrder[2].type); // 'warning';
|
|
107
|
-
* console.log(processedOrder[3].type); // 'info';
|
|
108
|
-
* console.log(processedOrder[4].type); // 'info';
|
|
109
|
-
*
|
|
110
|
-
* // Verify O(log n) operations
|
|
111
|
-
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
112
|
-
*
|
|
113
|
-
* // Add - O(log n)
|
|
114
|
-
* newHeap.add(2);
|
|
115
|
-
* console.log(newHeap.size); // 5;
|
|
116
|
-
*
|
|
117
|
-
* // Poll - O(log n)
|
|
118
|
-
* const removed = newHeap.poll();
|
|
119
|
-
* console.log(removed); // 1;
|
|
120
|
-
*
|
|
121
|
-
* // Peek - O(1)
|
|
122
|
-
* const top = newHeap.peek();
|
|
123
|
-
* console.log(top); // 2;
|
|
124
|
-
* @example
|
|
125
26
|
* // Use Heap to solve top k problems
|
|
126
27
|
* function topKElements(arr: number[], k: number): number[] {
|
|
127
28
|
* const heap = new Heap<number>([], { comparator: (a, b) => b - a }); // Max heap
|
|
@@ -236,6 +137,12 @@ import { IterableElementBase } from '../base';
|
|
|
236
137
|
* ['Task5', 4]
|
|
237
138
|
* ]);
|
|
238
139
|
* console.log(scheduleTasks(tasks, 2)); // expectedMap;
|
|
140
|
+
* @example
|
|
141
|
+
* // Get all elements as array
|
|
142
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
143
|
+
* const arr = heap.toArray();
|
|
144
|
+
* console.log(arr.length); // 5;
|
|
145
|
+
* console.log(arr.sort()); // [1, 2, 3, 4, 5];
|
|
239
146
|
*/
|
|
240
147
|
export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
241
148
|
protected _equals: (a: E, b: E) => boolean;
|
|
@@ -258,6 +165,47 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
258
165
|
* Get the number of elements.
|
|
259
166
|
* @remarks Time O(1), Space O(1)
|
|
260
167
|
* @returns Heap size.
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
* @example
|
|
201
|
+
* // Track heap capacity
|
|
202
|
+
* const heap = new Heap<number>();
|
|
203
|
+
* console.log(heap.size); // 0;
|
|
204
|
+
* heap.add(10);
|
|
205
|
+
* heap.add(20);
|
|
206
|
+
* console.log(heap.size); // 2;
|
|
207
|
+
* heap.poll();
|
|
208
|
+
* console.log(heap.size); // 1;
|
|
261
209
|
*/
|
|
262
210
|
get size(): number;
|
|
263
211
|
/**
|
|
@@ -292,6 +240,53 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
292
240
|
* @remarks Time O(1) amortized, Space O(1)
|
|
293
241
|
* @param element - Element to insert.
|
|
294
242
|
* @returns True.
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
* @example
|
|
276
|
+
* // basic Heap creation and add operation
|
|
277
|
+
* // Create a min heap (default)
|
|
278
|
+
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
279
|
+
*
|
|
280
|
+
* // Verify size
|
|
281
|
+
* console.log(minHeap.size); // 6;
|
|
282
|
+
*
|
|
283
|
+
* // Add new element
|
|
284
|
+
* minHeap.add(4);
|
|
285
|
+
* console.log(minHeap.size); // 7;
|
|
286
|
+
*
|
|
287
|
+
* // Min heap property: smallest element at root
|
|
288
|
+
* const min = minHeap.peek();
|
|
289
|
+
* console.log(min); // 1;
|
|
295
290
|
*/
|
|
296
291
|
add(element: E): boolean;
|
|
297
292
|
/**
|
|
@@ -299,30 +294,284 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
299
294
|
* @remarks Time O(N log N), Space O(1)
|
|
300
295
|
* @param elements - Iterable of elements or raw values.
|
|
301
296
|
* @returns Array of per-element success flags.
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
* @example
|
|
327
|
+
* // Add multiple elements
|
|
328
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
329
|
+
* heap.addMany([5, 3, 7, 1]);
|
|
330
|
+
* console.log(heap.peek()); // 1;
|
|
331
|
+
* console.log(heap.size); // 4;
|
|
302
332
|
*/
|
|
303
333
|
addMany(elements: Iterable<E | R>): boolean[];
|
|
304
334
|
/**
|
|
305
335
|
* Remove and return the top element.
|
|
306
336
|
* @remarks Time O(log N), Space O(1)
|
|
307
337
|
* @returns Top element or undefined.
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
* @example
|
|
371
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
372
|
+
* interface Task {
|
|
373
|
+
* id: number;
|
|
374
|
+
* priority: number;
|
|
375
|
+
* name: string;
|
|
376
|
+
* }
|
|
377
|
+
*
|
|
378
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
379
|
+
* const tasks: Task[] = [
|
|
380
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
381
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
382
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
383
|
+
* ];
|
|
384
|
+
*
|
|
385
|
+
* const maxHeap = new Heap(tasks, {
|
|
386
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
387
|
+
* });
|
|
388
|
+
*
|
|
389
|
+
* console.log(maxHeap.size); // 3;
|
|
390
|
+
*
|
|
391
|
+
* // Peek returns highest priority task
|
|
392
|
+
* const topTask = maxHeap.peek();
|
|
393
|
+
* console.log(topTask?.priority); // 8;
|
|
394
|
+
* console.log(topTask?.name); // 'Alert';
|
|
308
395
|
*/
|
|
309
396
|
poll(): E | undefined;
|
|
310
397
|
/**
|
|
311
398
|
* Get the current top element without removing it.
|
|
312
399
|
* @remarks Time O(1), Space O(1)
|
|
313
400
|
* @returns Top element or undefined.
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
* @example
|
|
434
|
+
* // Heap for event processing with priority
|
|
435
|
+
* interface Event {
|
|
436
|
+
* id: number;
|
|
437
|
+
* type: 'critical' | 'warning' | 'info';
|
|
438
|
+
* timestamp: number;
|
|
439
|
+
* message: string;
|
|
440
|
+
* }
|
|
441
|
+
*
|
|
442
|
+
* // Custom priority: critical > warning > info
|
|
443
|
+
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
444
|
+
*
|
|
445
|
+
* const eventHeap = new Heap<Event>([], {
|
|
446
|
+
* comparator: (a: Event, b: Event) => {
|
|
447
|
+
* const priorityA = priorityMap[a.type];
|
|
448
|
+
* const priorityB = priorityMap[b.type];
|
|
449
|
+
* return priorityB - priorityA; // Higher priority first
|
|
450
|
+
* }
|
|
451
|
+
* });
|
|
452
|
+
*
|
|
453
|
+
* // Add events in random order
|
|
454
|
+
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
455
|
+
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
456
|
+
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
457
|
+
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
458
|
+
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
459
|
+
*
|
|
460
|
+
* console.log(eventHeap.size); // 5;
|
|
461
|
+
*
|
|
462
|
+
* // Process events by priority (critical first)
|
|
463
|
+
* const processedOrder: Event[] = [];
|
|
464
|
+
* while (eventHeap.size > 0) {
|
|
465
|
+
* const event = eventHeap.poll();
|
|
466
|
+
* if (event) {
|
|
467
|
+
* processedOrder.push(event);
|
|
468
|
+
* }
|
|
469
|
+
* }
|
|
470
|
+
*
|
|
471
|
+
* // Verify critical events came first
|
|
472
|
+
* console.log(processedOrder[0].type); // 'critical';
|
|
473
|
+
* console.log(processedOrder[1].type); // 'critical';
|
|
474
|
+
* console.log(processedOrder[2].type); // 'warning';
|
|
475
|
+
* console.log(processedOrder[3].type); // 'info';
|
|
476
|
+
* console.log(processedOrder[4].type); // 'info';
|
|
477
|
+
*
|
|
478
|
+
* // Verify O(log n) operations
|
|
479
|
+
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
480
|
+
*
|
|
481
|
+
* // Add - O(log n)
|
|
482
|
+
* newHeap.add(2);
|
|
483
|
+
* console.log(newHeap.size); // 5;
|
|
484
|
+
*
|
|
485
|
+
* // Poll - O(log n)
|
|
486
|
+
* const removed = newHeap.poll();
|
|
487
|
+
* console.log(removed); // 1;
|
|
488
|
+
*
|
|
489
|
+
* // Peek - O(1)
|
|
490
|
+
* const top = newHeap.peek();
|
|
491
|
+
* console.log(top); // 2;
|
|
314
492
|
*/
|
|
315
493
|
peek(): E | undefined;
|
|
316
494
|
/**
|
|
317
495
|
* Check whether the heap is empty.
|
|
318
496
|
* @remarks Time O(1), Space O(1)
|
|
319
497
|
* @returns True if size is 0.
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
* @example
|
|
529
|
+
* // Check if heap is empty
|
|
530
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
531
|
+
* console.log(heap.isEmpty()); // true;
|
|
532
|
+
* heap.add(1);
|
|
533
|
+
* console.log(heap.isEmpty()); // false;
|
|
320
534
|
*/
|
|
321
535
|
isEmpty(): boolean;
|
|
322
536
|
/**
|
|
323
537
|
* Remove all elements.
|
|
324
538
|
* @remarks Time O(1), Space O(1)
|
|
325
539
|
* @returns void
|
|
540
|
+
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
* @example
|
|
571
|
+
* // Remove all elements
|
|
572
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
573
|
+
* heap.clear();
|
|
574
|
+
* console.log(heap.isEmpty()); // true;
|
|
326
575
|
*/
|
|
327
576
|
clear(): void;
|
|
328
577
|
/**
|
|
@@ -337,6 +586,34 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
337
586
|
* @remarks Time O(N), Space O(1)
|
|
338
587
|
* @param element - Element to search for.
|
|
339
588
|
* @returns True if found.
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
|
|
612
|
+
* @example
|
|
613
|
+
* // Check element existence
|
|
614
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
615
|
+
* console.log(heap.has(1)); // true;
|
|
616
|
+
* console.log(heap.has(99)); // false;
|
|
340
617
|
*/
|
|
341
618
|
has(element: E): boolean;
|
|
342
619
|
/**
|
|
@@ -344,6 +621,40 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
344
621
|
* @remarks Time O(N), Space O(1)
|
|
345
622
|
* @param element - Element to delete.
|
|
346
623
|
* @returns True if an element was removed.
|
|
624
|
+
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
|
|
647
|
+
|
|
648
|
+
|
|
649
|
+
|
|
650
|
+
|
|
651
|
+
|
|
652
|
+
|
|
653
|
+
* @example
|
|
654
|
+
* // Remove specific element
|
|
655
|
+
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
656
|
+
* heap.delete(4);
|
|
657
|
+
* console.log(heap.toArray().includes(4)); // false;
|
|
347
658
|
*/
|
|
348
659
|
delete(element: E): boolean;
|
|
349
660
|
/**
|
|
@@ -365,6 +676,34 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
365
676
|
* @remarks Time O(N), Space O(H)
|
|
366
677
|
* @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
367
678
|
* @returns Array of visited elements.
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
|
|
695
|
+
|
|
696
|
+
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
* @example
|
|
703
|
+
* // Depth-first traversal
|
|
704
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
705
|
+
* const result = heap.dfs('IN');
|
|
706
|
+
* console.log(result.length); // 3;
|
|
368
707
|
*/
|
|
369
708
|
dfs(order?: DFSOrderPattern): E[];
|
|
370
709
|
/**
|
|
@@ -377,12 +716,86 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
377
716
|
* Return all elements in ascending order by repeatedly polling.
|
|
378
717
|
* @remarks Time O(N log N), Space O(N)
|
|
379
718
|
* @returns Sorted array of elements.
|
|
719
|
+
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
|
|
729
|
+
|
|
730
|
+
|
|
731
|
+
|
|
732
|
+
|
|
733
|
+
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
|
|
746
|
+
|
|
747
|
+
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
* @example
|
|
752
|
+
* // Sort elements using heap
|
|
753
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
754
|
+
* const sorted = heap.sort();
|
|
755
|
+
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
380
756
|
*/
|
|
381
757
|
sort(): E[];
|
|
382
758
|
/**
|
|
383
759
|
* Deep clone this heap.
|
|
384
760
|
* @remarks Time O(N), Space O(N)
|
|
385
761
|
* @returns A new heap with the same elements.
|
|
762
|
+
|
|
763
|
+
|
|
764
|
+
|
|
765
|
+
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
|
|
769
|
+
|
|
770
|
+
|
|
771
|
+
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
|
|
775
|
+
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
|
|
781
|
+
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
|
|
785
|
+
|
|
786
|
+
|
|
787
|
+
|
|
788
|
+
|
|
789
|
+
|
|
790
|
+
|
|
791
|
+
|
|
792
|
+
* @example
|
|
793
|
+
* // Create independent copy
|
|
794
|
+
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
795
|
+
* const copy = heap.clone();
|
|
796
|
+
* copy.poll();
|
|
797
|
+
* console.log(heap.size); // 3;
|
|
798
|
+
* console.log(copy.size); // 2;
|
|
386
799
|
*/
|
|
387
800
|
clone(): this;
|
|
388
801
|
/**
|
|
@@ -391,6 +804,41 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
391
804
|
* @param callback - Predicate (element, index, heap) → boolean to keep element.
|
|
392
805
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
393
806
|
* @returns A new heap with the kept elements.
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
|
|
814
|
+
|
|
815
|
+
|
|
816
|
+
|
|
817
|
+
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
|
|
829
|
+
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
* @example
|
|
838
|
+
* // Filter elements
|
|
839
|
+
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
840
|
+
* const evens = heap.filter(x => x % 2 === 0);
|
|
841
|
+
* console.log(evens.size); // 2;
|
|
394
842
|
*/
|
|
395
843
|
filter(callback: ElementCallback<E, R, boolean>, thisArg?: unknown): this;
|
|
396
844
|
/**
|
|
@@ -402,6 +850,40 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
402
850
|
* @param options - Options for the output heap, including comparator for EM.
|
|
403
851
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
404
852
|
* @returns A new heap with mapped elements.
|
|
853
|
+
|
|
854
|
+
|
|
855
|
+
|
|
856
|
+
|
|
857
|
+
|
|
858
|
+
|
|
859
|
+
|
|
860
|
+
|
|
861
|
+
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
|
|
872
|
+
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
|
|
878
|
+
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
|
|
882
|
+
* @example
|
|
883
|
+
* // Transform elements
|
|
884
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
885
|
+
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
886
|
+
* console.log(doubled.peek()); // 2;
|
|
405
887
|
*/
|
|
406
888
|
map<EM, RM>(callback: ElementCallback<E, R, EM>, options: HeapOptions<EM, RM> & {
|
|
407
889
|
comparator: Comparator<EM>;
|