data-structure-typed 2.6.1 → 2.6.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.
- package/CHANGELOG.md +9 -1
- package/dist/cjs/binary-tree.cjs +2927 -23688
- package/dist/cjs/graph.cjs +845 -2634
- package/dist/cjs/hash.cjs +185 -616
- package/dist/cjs/heap.cjs +266 -818
- package/dist/cjs/index.cjs +5116 -31358
- package/dist/cjs/linked-list.cjs +644 -2615
- package/dist/cjs/matrix.cjs +220 -535
- package/dist/cjs/priority-queue.cjs +266 -818
- package/dist/cjs/queue.cjs +637 -2343
- package/dist/cjs/stack.cjs +124 -530
- package/dist/cjs/trie.cjs +145 -592
- package/dist/cjs-legacy/binary-tree.cjs +4352 -25113
- package/dist/cjs-legacy/graph.cjs +847 -2636
- package/dist/cjs-legacy/hash.cjs +181 -612
- package/dist/cjs-legacy/heap.cjs +266 -818
- package/dist/cjs-legacy/index.cjs +6657 -32899
- package/dist/cjs-legacy/linked-list.cjs +640 -2611
- package/dist/cjs-legacy/matrix.cjs +220 -535
- package/dist/cjs-legacy/priority-queue.cjs +266 -818
- package/dist/cjs-legacy/queue.cjs +634 -2340
- package/dist/cjs-legacy/stack.cjs +124 -530
- package/dist/cjs-legacy/trie.cjs +145 -592
- package/dist/esm/binary-tree.mjs +2927 -23688
- package/dist/esm/graph.mjs +845 -2634
- package/dist/esm/hash.mjs +185 -616
- package/dist/esm/heap.mjs +266 -818
- package/dist/esm/index.mjs +5116 -31358
- package/dist/esm/linked-list.mjs +644 -2615
- package/dist/esm/matrix.mjs +220 -535
- package/dist/esm/priority-queue.mjs +266 -818
- package/dist/esm/queue.mjs +637 -2343
- package/dist/esm/stack.mjs +124 -530
- package/dist/esm/trie.mjs +145 -592
- package/dist/esm-legacy/binary-tree.mjs +4352 -25113
- package/dist/esm-legacy/graph.mjs +847 -2636
- package/dist/esm-legacy/hash.mjs +181 -612
- package/dist/esm-legacy/heap.mjs +266 -818
- package/dist/esm-legacy/index.mjs +6657 -32899
- package/dist/esm-legacy/linked-list.mjs +640 -2611
- package/dist/esm-legacy/matrix.mjs +220 -535
- package/dist/esm-legacy/priority-queue.mjs +266 -818
- package/dist/esm-legacy/queue.mjs +634 -2340
- package/dist/esm-legacy/stack.mjs +124 -530
- package/dist/esm-legacy/trie.mjs +145 -592
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +29 -500
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +38 -563
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +230 -1212
- package/dist/types/data-structures/binary-tree/bst.d.ts +124 -1063
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +99 -854
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +62 -314
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +366 -5057
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +277 -4885
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +253 -3929
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +261 -4782
- package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -44
- package/dist/types/data-structures/graph/directed-graph.d.ts +130 -527
- package/dist/types/data-structures/graph/undirected-graph.d.ts +125 -482
- package/dist/types/data-structures/hash/hash-map.d.ts +132 -562
- package/dist/types/data-structures/heap/heap.d.ts +194 -746
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +120 -809
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -733
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +193 -863
- package/dist/types/data-structures/matrix/matrix.d.ts +159 -474
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -6
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +6 -11
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/types/data-structures/queue/deque.d.ts +196 -804
- package/dist/types/data-structures/queue/queue.d.ts +99 -585
- package/dist/types/data-structures/stack/stack.d.ts +75 -481
- package/dist/types/data-structures/trie/trie.d.ts +98 -584
- package/dist/umd/data-structure-typed.js +6580 -32822
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +162 -215
- package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +9 -9
- package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +47 -47
- package/docs-site-docusaurus/docs/api/classes/BST.md +155 -208
- package/docs-site-docusaurus/docs/api/classes/BSTNode.md +7 -7
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +13 -29
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +104 -158
- package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/Deque.md +72 -104
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +60 -80
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +67 -101
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +6 -6
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +9 -9
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +52 -48
- package/docs-site-docusaurus/docs/api/classes/Heap.md +40 -68
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +17 -17
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +38 -38
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +45 -45
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +23 -23
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +68 -96
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +60 -80
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +57 -52
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +38 -66
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +41 -74
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +38 -66
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +46 -79
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +40 -68
- package/docs-site-docusaurus/docs/api/classes/Queue.md +54 -78
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +171 -225
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +10 -22
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +68 -98
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +4 -4
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +98 -75
- package/docs-site-docusaurus/docs/api/classes/Stack.md +33 -53
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +104 -129
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +57 -124
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +45 -116
- package/docs-site-docusaurus/docs/api/classes/Trie.md +37 -61
- package/docs-site-docusaurus/docs/api/classes/TrieNode.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +59 -77
- package/package.json +1 -1
- package/src/data-structures/binary-tree/tree-multi-set.ts +15 -11
package/dist/esm/heap.mjs
CHANGED
|
@@ -315,61 +315,19 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
315
315
|
return this._elements;
|
|
316
316
|
}
|
|
317
317
|
/**
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
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
|
-
* @example
|
|
364
|
-
* // Track heap capacity
|
|
365
|
-
* const heap = new Heap<number>();
|
|
366
|
-
* console.log(heap.size); // 0;
|
|
367
|
-
* heap.add(10);
|
|
368
|
-
* heap.add(20);
|
|
369
|
-
* console.log(heap.size); // 2;
|
|
370
|
-
* heap.poll();
|
|
371
|
-
* console.log(heap.size); // 1;
|
|
372
|
-
*/
|
|
318
|
+
* Get the number of elements.
|
|
319
|
+
* @remarks Time O(1), Space O(1)
|
|
320
|
+
* @returns Heap size.
|
|
321
|
+
* @example
|
|
322
|
+
* // Track heap capacity
|
|
323
|
+
* const heap = new Heap<number>();
|
|
324
|
+
* console.log(heap.size); // 0;
|
|
325
|
+
* heap.add(10);
|
|
326
|
+
* heap.add(20);
|
|
327
|
+
* console.log(heap.size); // 2;
|
|
328
|
+
* heap.poll();
|
|
329
|
+
* console.log(heap.size); // 1;
|
|
330
|
+
*/
|
|
373
331
|
get size() {
|
|
374
332
|
return this.elements.length;
|
|
375
333
|
}
|
|
@@ -381,6 +339,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
381
339
|
get leaf() {
|
|
382
340
|
return this.elements[this.size - 1] ?? void 0;
|
|
383
341
|
}
|
|
342
|
+
/**
|
|
343
|
+
* Get the comparator used to order elements.
|
|
344
|
+
* @remarks Time O(1), Space O(1)
|
|
345
|
+
* @returns Comparator function.
|
|
346
|
+
*/
|
|
347
|
+
get comparator() {
|
|
348
|
+
return this._comparator;
|
|
349
|
+
}
|
|
384
350
|
/**
|
|
385
351
|
* Create a heap of the same class from an iterable.
|
|
386
352
|
* @remarks Time O(N), Space O(N)
|
|
@@ -407,123 +373,42 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
407
373
|
return new _Heap(elements, options);
|
|
408
374
|
}
|
|
409
375
|
/**
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
* @example
|
|
457
|
-
* // basic Heap creation and add operation
|
|
458
|
-
* // Create a min heap (default)
|
|
459
|
-
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
460
|
-
*
|
|
461
|
-
* // Verify size
|
|
462
|
-
* console.log(minHeap.size); // 6;
|
|
463
|
-
*
|
|
464
|
-
* // Add new element
|
|
465
|
-
* minHeap.add(4);
|
|
466
|
-
* console.log(minHeap.size); // 7;
|
|
467
|
-
*
|
|
468
|
-
* // Min heap property: smallest element at root
|
|
469
|
-
* const min = minHeap.peek();
|
|
470
|
-
* console.log(min); // 1;
|
|
471
|
-
*/
|
|
376
|
+
* Insert an element.
|
|
377
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
378
|
+
* @param element - Element to insert.
|
|
379
|
+
* @returns True.
|
|
380
|
+
* @example
|
|
381
|
+
* // basic Heap creation and add operation
|
|
382
|
+
* // Create a min heap (default)
|
|
383
|
+
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
384
|
+
*
|
|
385
|
+
* // Verify size
|
|
386
|
+
* console.log(minHeap.size); // 6;
|
|
387
|
+
*
|
|
388
|
+
* // Add new element
|
|
389
|
+
* minHeap.add(4);
|
|
390
|
+
* console.log(minHeap.size); // 7;
|
|
391
|
+
*
|
|
392
|
+
* // Min heap property: smallest element at root
|
|
393
|
+
* const min = minHeap.peek();
|
|
394
|
+
* console.log(min); // 1;
|
|
395
|
+
*/
|
|
472
396
|
add(element) {
|
|
473
397
|
this._elements.push(element);
|
|
474
398
|
return this._bubbleUp(this.elements.length - 1);
|
|
475
399
|
}
|
|
476
400
|
/**
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
* @example
|
|
521
|
-
* // Add multiple elements
|
|
522
|
-
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
523
|
-
* heap.addMany([5, 3, 7, 1]);
|
|
524
|
-
* console.log(heap.peek()); // 1;
|
|
525
|
-
* console.log(heap.size); // 4;
|
|
526
|
-
*/
|
|
401
|
+
* Insert many elements from an iterable.
|
|
402
|
+
* @remarks Time O(N log N), Space O(1)
|
|
403
|
+
* @param elements - Iterable of elements or raw values.
|
|
404
|
+
* @returns Array of per-element success flags.
|
|
405
|
+
* @example
|
|
406
|
+
* // Add multiple elements
|
|
407
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
408
|
+
* heap.addMany([5, 3, 7, 1]);
|
|
409
|
+
* console.log(heap.peek()); // 1;
|
|
410
|
+
* console.log(heap.size); // 4;
|
|
411
|
+
*/
|
|
527
412
|
addMany(elements) {
|
|
528
413
|
const flags = [];
|
|
529
414
|
for (const el of elements) {
|
|
@@ -538,104 +423,63 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
538
423
|
return flags;
|
|
539
424
|
}
|
|
540
425
|
/**
|
|
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
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
* @example
|
|
583
|
-
* // Heap with custom comparator (MaxHeap behavior)
|
|
584
|
-
* interface Task {
|
|
585
|
-
* id: number;
|
|
586
|
-
* priority: number;
|
|
587
|
-
* name: string;
|
|
588
|
-
* }
|
|
589
|
-
*
|
|
590
|
-
* // Custom comparator for max heap behavior (higher priority first)
|
|
591
|
-
* const tasks: Task[] = [
|
|
592
|
-
* { id: 1, priority: 5, name: 'Email' },
|
|
593
|
-
* { id: 2, priority: 3, name: 'Chat' },
|
|
594
|
-
* { id: 3, priority: 8, name: 'Alert' }
|
|
595
|
-
* ];
|
|
596
|
-
*
|
|
597
|
-
* const maxHeap = new Heap(tasks, {
|
|
598
|
-
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
599
|
-
* });
|
|
600
|
-
*
|
|
601
|
-
* console.log(maxHeap.size); // 3;
|
|
602
|
-
*
|
|
603
|
-
* // Peek returns highest priority task
|
|
604
|
-
* const topTask = maxHeap.peek();
|
|
605
|
-
* console.log(topTask?.priority); // 8;
|
|
606
|
-
* console.log(topTask?.name); // 'Alert';
|
|
607
|
-
*/
|
|
426
|
+
* Remove and return the top element.
|
|
427
|
+
* @remarks Time O(log N), Space O(1)
|
|
428
|
+
* @returns Top element or undefined.
|
|
429
|
+
* @example
|
|
430
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
431
|
+
* interface Task {
|
|
432
|
+
* id: number;
|
|
433
|
+
* priority: number;
|
|
434
|
+
* name: string;
|
|
435
|
+
* }
|
|
436
|
+
*
|
|
437
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
438
|
+
* const tasks: Task[] = [
|
|
439
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
440
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
441
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
442
|
+
* ];
|
|
443
|
+
*
|
|
444
|
+
* const maxHeap = new Heap(tasks, {
|
|
445
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
446
|
+
* });
|
|
447
|
+
*
|
|
448
|
+
* console.log(maxHeap.size); // 3;
|
|
449
|
+
*
|
|
450
|
+
* // Peek returns highest priority task
|
|
451
|
+
* const topTask = maxHeap.peek();
|
|
452
|
+
* console.log(topTask?.priority); // 8;
|
|
453
|
+
* console.log(topTask?.name); // 'Alert';
|
|
454
|
+
*/
|
|
608
455
|
/**
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
* console.log(topTask?.priority); // 8;
|
|
637
|
-
* console.log(topTask?.name); // 'Alert';
|
|
638
|
-
*/
|
|
456
|
+
* @deprecated Use `pop` instead. Will be removed in a future major version.
|
|
457
|
+
* @example
|
|
458
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
459
|
+
* interface Task {
|
|
460
|
+
* id: number;
|
|
461
|
+
* priority: number;
|
|
462
|
+
* name: string;
|
|
463
|
+
* }
|
|
464
|
+
*
|
|
465
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
466
|
+
* const tasks: Task[] = [
|
|
467
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
468
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
469
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
470
|
+
* ];
|
|
471
|
+
*
|
|
472
|
+
* const maxHeap = new Heap(tasks, {
|
|
473
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
474
|
+
* });
|
|
475
|
+
*
|
|
476
|
+
* console.log(maxHeap.size); // 3;
|
|
477
|
+
*
|
|
478
|
+
* // Peek returns highest priority task
|
|
479
|
+
* const topTask = maxHeap.peek();
|
|
480
|
+
* console.log(topTask?.priority); // 8;
|
|
481
|
+
* console.log(topTask?.name); // 'Alert';
|
|
482
|
+
*/
|
|
639
483
|
poll() {
|
|
640
484
|
return this.pop();
|
|
641
485
|
}
|
|
@@ -655,319 +499,125 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
655
499
|
return value;
|
|
656
500
|
}
|
|
657
501
|
/**
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
* });
|
|
722
|
-
*
|
|
723
|
-
* // Add events in random order
|
|
724
|
-
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
725
|
-
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
726
|
-
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
727
|
-
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
728
|
-
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
729
|
-
*
|
|
730
|
-
* console.log(eventHeap.size); // 5;
|
|
731
|
-
*
|
|
732
|
-
* // Process events by priority (critical first)
|
|
733
|
-
* const processedOrder: Event[] = [];
|
|
734
|
-
* while (eventHeap.size > 0) {
|
|
735
|
-
* const event = eventHeap.poll();
|
|
736
|
-
* if (event) {
|
|
737
|
-
* processedOrder.push(event);
|
|
738
|
-
* }
|
|
739
|
-
* }
|
|
740
|
-
*
|
|
741
|
-
* // Verify critical events came first
|
|
742
|
-
* console.log(processedOrder[0].type); // 'critical';
|
|
743
|
-
* console.log(processedOrder[1].type); // 'critical';
|
|
744
|
-
* console.log(processedOrder[2].type); // 'warning';
|
|
745
|
-
* console.log(processedOrder[3].type); // 'info';
|
|
746
|
-
* console.log(processedOrder[4].type); // 'info';
|
|
747
|
-
*
|
|
748
|
-
* // Verify O(log n) operations
|
|
749
|
-
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
750
|
-
*
|
|
751
|
-
* // Add - O(log n)
|
|
752
|
-
* newHeap.add(2);
|
|
753
|
-
* console.log(newHeap.size); // 5;
|
|
754
|
-
*
|
|
755
|
-
* // Poll - O(log n)
|
|
756
|
-
* const removed = newHeap.poll();
|
|
757
|
-
* console.log(removed); // 1;
|
|
758
|
-
*
|
|
759
|
-
* // Peek - O(1)
|
|
760
|
-
* const top = newHeap.peek();
|
|
761
|
-
* console.log(top); // 2;
|
|
762
|
-
*/
|
|
502
|
+
* Get the current top element without removing it.
|
|
503
|
+
* @remarks Time O(1), Space O(1)
|
|
504
|
+
* @returns Top element or undefined.
|
|
505
|
+
* @example
|
|
506
|
+
* // Heap for event processing with priority
|
|
507
|
+
* interface Event {
|
|
508
|
+
* id: number;
|
|
509
|
+
* type: 'critical' | 'warning' | 'info';
|
|
510
|
+
* timestamp: number;
|
|
511
|
+
* message: string;
|
|
512
|
+
* }
|
|
513
|
+
*
|
|
514
|
+
* // Custom priority: critical > warning > info
|
|
515
|
+
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
516
|
+
*
|
|
517
|
+
* const eventHeap = new Heap<Event>([], {
|
|
518
|
+
* comparator: (a: Event, b: Event) => {
|
|
519
|
+
* const priorityA = priorityMap[a.type];
|
|
520
|
+
* const priorityB = priorityMap[b.type];
|
|
521
|
+
* return priorityB - priorityA; // Higher priority first
|
|
522
|
+
* }
|
|
523
|
+
* });
|
|
524
|
+
*
|
|
525
|
+
* // Add events in random order
|
|
526
|
+
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
527
|
+
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
528
|
+
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
529
|
+
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
530
|
+
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
531
|
+
*
|
|
532
|
+
* console.log(eventHeap.size); // 5;
|
|
533
|
+
*
|
|
534
|
+
* // Process events by priority (critical first)
|
|
535
|
+
* const processedOrder: Event[] = [];
|
|
536
|
+
* while (eventHeap.size > 0) {
|
|
537
|
+
* const event = eventHeap.poll();
|
|
538
|
+
* if (event) {
|
|
539
|
+
* processedOrder.push(event);
|
|
540
|
+
* }
|
|
541
|
+
* }
|
|
542
|
+
*
|
|
543
|
+
* // Verify critical events came first
|
|
544
|
+
* console.log(processedOrder[0].type); // 'critical';
|
|
545
|
+
* console.log(processedOrder[1].type); // 'critical';
|
|
546
|
+
* console.log(processedOrder[2].type); // 'warning';
|
|
547
|
+
* console.log(processedOrder[3].type); // 'info';
|
|
548
|
+
* console.log(processedOrder[4].type); // 'info';
|
|
549
|
+
*
|
|
550
|
+
* // Verify O(log n) operations
|
|
551
|
+
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
552
|
+
*
|
|
553
|
+
* // Add - O(log n)
|
|
554
|
+
* newHeap.add(2);
|
|
555
|
+
* console.log(newHeap.size); // 5;
|
|
556
|
+
*
|
|
557
|
+
* // Poll - O(log n)
|
|
558
|
+
* const removed = newHeap.poll();
|
|
559
|
+
* console.log(removed); // 1;
|
|
560
|
+
*
|
|
561
|
+
* // Peek - O(1)
|
|
562
|
+
* const top = newHeap.peek();
|
|
563
|
+
* console.log(top); // 2;
|
|
564
|
+
*/
|
|
763
565
|
peek() {
|
|
764
566
|
return this.elements[0];
|
|
765
567
|
}
|
|
766
568
|
/**
|
|
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
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
* @example
|
|
811
|
-
* // Check if heap is empty
|
|
812
|
-
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
813
|
-
* console.log(heap.isEmpty()); // true;
|
|
814
|
-
* heap.add(1);
|
|
815
|
-
* console.log(heap.isEmpty()); // false;
|
|
816
|
-
*/
|
|
569
|
+
* Check whether the heap is empty.
|
|
570
|
+
* @remarks Time O(1), Space O(1)
|
|
571
|
+
* @returns True if size is 0.
|
|
572
|
+
* @example
|
|
573
|
+
* // Check if heap is empty
|
|
574
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
575
|
+
* console.log(heap.isEmpty()); // true;
|
|
576
|
+
* heap.add(1);
|
|
577
|
+
* console.log(heap.isEmpty()); // false;
|
|
578
|
+
*/
|
|
817
579
|
isEmpty() {
|
|
818
580
|
return this.size === 0;
|
|
819
581
|
}
|
|
820
582
|
/**
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
* @example
|
|
865
|
-
* // Remove all elements
|
|
866
|
-
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
867
|
-
* heap.clear();
|
|
868
|
-
* console.log(heap.isEmpty()); // true;
|
|
869
|
-
*/
|
|
583
|
+
* Remove all elements.
|
|
584
|
+
* @remarks Time O(1), Space O(1)
|
|
585
|
+
* @returns void
|
|
586
|
+
* @example
|
|
587
|
+
* // Remove all elements
|
|
588
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
589
|
+
* heap.clear();
|
|
590
|
+
* console.log(heap.isEmpty()); // true;
|
|
591
|
+
*/
|
|
870
592
|
clear() {
|
|
871
593
|
this._elements = [];
|
|
872
594
|
}
|
|
873
595
|
/**
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
* @example
|
|
912
|
-
* // Check element existence
|
|
913
|
-
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
914
|
-
* console.log(heap.has(1)); // true;
|
|
915
|
-
* console.log(heap.has(99)); // false;
|
|
916
|
-
*/
|
|
596
|
+
* Check if an equal element exists in the heap.
|
|
597
|
+
* @remarks Time O(N), Space O(1)
|
|
598
|
+
* @param element - Element to search for.
|
|
599
|
+
* @returns True if found.
|
|
600
|
+
* @example
|
|
601
|
+
* // Check element existence
|
|
602
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
603
|
+
* console.log(heap.has(1)); // true;
|
|
604
|
+
* console.log(heap.has(99)); // false;
|
|
605
|
+
*/
|
|
917
606
|
has(element) {
|
|
918
607
|
for (const el of this.elements) if (this._equals(el, element)) return true;
|
|
919
608
|
return false;
|
|
920
609
|
}
|
|
921
610
|
/**
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
* @example
|
|
966
|
-
* // Remove specific element
|
|
967
|
-
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
968
|
-
* heap.delete(4);
|
|
969
|
-
* console.log(heap.toArray().includes(4)); // false;
|
|
970
|
-
*/
|
|
611
|
+
* Delete one occurrence of an element.
|
|
612
|
+
* @remarks Time O(N), Space O(1)
|
|
613
|
+
* @param element - Element to delete.
|
|
614
|
+
* @returns True if an element was removed.
|
|
615
|
+
* @example
|
|
616
|
+
* // Remove specific element
|
|
617
|
+
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
618
|
+
* heap.delete(4);
|
|
619
|
+
* console.log(heap.toArray().includes(4)); // false;
|
|
620
|
+
*/
|
|
971
621
|
delete(element) {
|
|
972
622
|
let index = -1;
|
|
973
623
|
for (let i = 0; i < this.elements.length; i++) {
|
|
@@ -1031,49 +681,16 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1031
681
|
return this;
|
|
1032
682
|
}
|
|
1033
683
|
/**
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
* @example
|
|
1072
|
-
* // Depth-first traversal
|
|
1073
|
-
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
1074
|
-
* const result = heap.dfs('IN');
|
|
1075
|
-
* console.log(result.length); // 3;
|
|
1076
|
-
*/
|
|
684
|
+
* Traverse the binary heap as a complete binary tree and collect elements.
|
|
685
|
+
* @remarks Time O(N), Space O(H)
|
|
686
|
+
* @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
687
|
+
* @returns Array of visited elements.
|
|
688
|
+
* @example
|
|
689
|
+
* // Depth-first traversal
|
|
690
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
691
|
+
* const result = heap.dfs('IN');
|
|
692
|
+
* console.log(result.length); // 3;
|
|
693
|
+
*/
|
|
1077
694
|
dfs(order = "PRE") {
|
|
1078
695
|
const result = [];
|
|
1079
696
|
const _dfs = /* @__PURE__ */ __name((index) => {
|
|
@@ -1110,57 +727,15 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1110
727
|
return results;
|
|
1111
728
|
}
|
|
1112
729
|
/**
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
* @example
|
|
1159
|
-
* // Sort elements using heap
|
|
1160
|
-
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
1161
|
-
* const sorted = heap.sort();
|
|
1162
|
-
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
1163
|
-
*/
|
|
730
|
+
* Return all elements in ascending order by repeatedly polling.
|
|
731
|
+
* @remarks Time O(N log N), Space O(N)
|
|
732
|
+
* @returns Sorted array of elements.
|
|
733
|
+
* @example
|
|
734
|
+
* // Sort elements using heap
|
|
735
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
736
|
+
* const sorted = heap.sort();
|
|
737
|
+
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
738
|
+
*/
|
|
1164
739
|
sort() {
|
|
1165
740
|
const visited = [];
|
|
1166
741
|
const cloned = this._createInstance();
|
|
@@ -1172,114 +747,34 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1172
747
|
return visited;
|
|
1173
748
|
}
|
|
1174
749
|
/**
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
* @example
|
|
1219
|
-
* // Create independent copy
|
|
1220
|
-
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
1221
|
-
* const copy = heap.clone();
|
|
1222
|
-
* copy.poll();
|
|
1223
|
-
* console.log(heap.size); // 3;
|
|
1224
|
-
* console.log(copy.size); // 2;
|
|
1225
|
-
*/
|
|
750
|
+
* Deep clone this heap.
|
|
751
|
+
* @remarks Time O(N), Space O(N)
|
|
752
|
+
* @returns A new heap with the same elements.
|
|
753
|
+
* @example
|
|
754
|
+
* // Create independent copy
|
|
755
|
+
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
756
|
+
* const copy = heap.clone();
|
|
757
|
+
* copy.poll();
|
|
758
|
+
* console.log(heap.size); // 3;
|
|
759
|
+
* console.log(copy.size); // 2;
|
|
760
|
+
*/
|
|
1226
761
|
clone() {
|
|
1227
762
|
const next = this._createInstance();
|
|
1228
763
|
for (const x of this.elements) next.add(x);
|
|
1229
764
|
return next;
|
|
1230
765
|
}
|
|
1231
766
|
/**
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
* @example
|
|
1278
|
-
* // Filter elements
|
|
1279
|
-
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
1280
|
-
* const evens = heap.filter(x => x % 2 === 0);
|
|
1281
|
-
* console.log(evens.size); // 2;
|
|
1282
|
-
*/
|
|
767
|
+
* Filter elements into a new heap of the same class.
|
|
768
|
+
* @remarks Time O(N log N), Space O(N)
|
|
769
|
+
* @param callback - Predicate (element, index, heap) → boolean to keep element.
|
|
770
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
771
|
+
* @returns A new heap with the kept elements.
|
|
772
|
+
* @example
|
|
773
|
+
* // Filter elements
|
|
774
|
+
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
775
|
+
* const evens = heap.filter(x => x % 2 === 0);
|
|
776
|
+
* console.log(evens.size); // 2;
|
|
777
|
+
*/
|
|
1283
778
|
filter(callback, thisArg) {
|
|
1284
779
|
const out = this._createInstance();
|
|
1285
780
|
let i = 0;
|
|
@@ -1293,59 +788,20 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1293
788
|
return out;
|
|
1294
789
|
}
|
|
1295
790
|
/**
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
* @example
|
|
1344
|
-
* // Transform elements
|
|
1345
|
-
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
1346
|
-
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
1347
|
-
* console.log(doubled.peek()); // 2;
|
|
1348
|
-
*/
|
|
791
|
+
* Map elements into a new heap of possibly different element type.
|
|
792
|
+
* @remarks Time O(N log N), Space O(N)
|
|
793
|
+
* @template EM
|
|
794
|
+
* @template RM
|
|
795
|
+
* @param callback - Mapping function (element, index, heap) → newElement.
|
|
796
|
+
* @param options - Options for the output heap, including comparator for EM.
|
|
797
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
798
|
+
* @returns A new heap with mapped elements.
|
|
799
|
+
* @example
|
|
800
|
+
* // Transform elements
|
|
801
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
802
|
+
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
803
|
+
* console.log(doubled.peek()); // 2;
|
|
804
|
+
*/
|
|
1349
805
|
map(callback, options, thisArg) {
|
|
1350
806
|
const { comparator, toElementFn, ...rest } = options ?? {};
|
|
1351
807
|
if (!comparator) raise(TypeError, ERR.comparatorRequired("Heap.map"));
|
|
@@ -1382,14 +838,6 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1382
838
|
return 0;
|
|
1383
839
|
}, "_DEFAULT_COMPARATOR");
|
|
1384
840
|
_comparator = this._DEFAULT_COMPARATOR;
|
|
1385
|
-
/**
|
|
1386
|
-
* Get the comparator used to order elements.
|
|
1387
|
-
* @remarks Time O(1), Space O(1)
|
|
1388
|
-
* @returns Comparator function.
|
|
1389
|
-
*/
|
|
1390
|
-
get comparator() {
|
|
1391
|
-
return this._comparator;
|
|
1392
|
-
}
|
|
1393
841
|
*_getIterator() {
|
|
1394
842
|
for (const element of this.elements) yield element;
|
|
1395
843
|
}
|
|
@@ -1478,6 +926,7 @@ var FibonacciHeap = class {
|
|
|
1478
926
|
static {
|
|
1479
927
|
__name(this, "FibonacciHeap");
|
|
1480
928
|
}
|
|
929
|
+
_comparator;
|
|
1481
930
|
/**
|
|
1482
931
|
* Create a FibonacciHeap.
|
|
1483
932
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1511,7 +960,6 @@ var FibonacciHeap = class {
|
|
|
1511
960
|
get min() {
|
|
1512
961
|
return this._min;
|
|
1513
962
|
}
|
|
1514
|
-
_comparator;
|
|
1515
963
|
get comparator() {
|
|
1516
964
|
return this._comparator;
|
|
1517
965
|
}
|