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/cjs/heap.cjs
CHANGED
|
@@ -317,61 +317,19 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
317
317
|
return this._elements;
|
|
318
318
|
}
|
|
319
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
|
-
|
|
364
|
-
|
|
365
|
-
* @example
|
|
366
|
-
* // Track heap capacity
|
|
367
|
-
* const heap = new Heap<number>();
|
|
368
|
-
* console.log(heap.size); // 0;
|
|
369
|
-
* heap.add(10);
|
|
370
|
-
* heap.add(20);
|
|
371
|
-
* console.log(heap.size); // 2;
|
|
372
|
-
* heap.poll();
|
|
373
|
-
* console.log(heap.size); // 1;
|
|
374
|
-
*/
|
|
320
|
+
* Get the number of elements.
|
|
321
|
+
* @remarks Time O(1), Space O(1)
|
|
322
|
+
* @returns Heap size.
|
|
323
|
+
* @example
|
|
324
|
+
* // Track heap capacity
|
|
325
|
+
* const heap = new Heap<number>();
|
|
326
|
+
* console.log(heap.size); // 0;
|
|
327
|
+
* heap.add(10);
|
|
328
|
+
* heap.add(20);
|
|
329
|
+
* console.log(heap.size); // 2;
|
|
330
|
+
* heap.poll();
|
|
331
|
+
* console.log(heap.size); // 1;
|
|
332
|
+
*/
|
|
375
333
|
get size() {
|
|
376
334
|
return this.elements.length;
|
|
377
335
|
}
|
|
@@ -383,6 +341,14 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
383
341
|
get leaf() {
|
|
384
342
|
return this.elements[this.size - 1] ?? void 0;
|
|
385
343
|
}
|
|
344
|
+
/**
|
|
345
|
+
* Get the comparator used to order elements.
|
|
346
|
+
* @remarks Time O(1), Space O(1)
|
|
347
|
+
* @returns Comparator function.
|
|
348
|
+
*/
|
|
349
|
+
get comparator() {
|
|
350
|
+
return this._comparator;
|
|
351
|
+
}
|
|
386
352
|
/**
|
|
387
353
|
* Create a heap of the same class from an iterable.
|
|
388
354
|
* @remarks Time O(N), Space O(N)
|
|
@@ -409,123 +375,42 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
409
375
|
return new _Heap(elements, options);
|
|
410
376
|
}
|
|
411
377
|
/**
|
|
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
|
-
|
|
457
|
-
|
|
458
|
-
* @example
|
|
459
|
-
* // basic Heap creation and add operation
|
|
460
|
-
* // Create a min heap (default)
|
|
461
|
-
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
462
|
-
*
|
|
463
|
-
* // Verify size
|
|
464
|
-
* console.log(minHeap.size); // 6;
|
|
465
|
-
*
|
|
466
|
-
* // Add new element
|
|
467
|
-
* minHeap.add(4);
|
|
468
|
-
* console.log(minHeap.size); // 7;
|
|
469
|
-
*
|
|
470
|
-
* // Min heap property: smallest element at root
|
|
471
|
-
* const min = minHeap.peek();
|
|
472
|
-
* console.log(min); // 1;
|
|
473
|
-
*/
|
|
378
|
+
* Insert an element.
|
|
379
|
+
* @remarks Time O(log N) amortized, Space O(1)
|
|
380
|
+
* @param element - Element to insert.
|
|
381
|
+
* @returns True.
|
|
382
|
+
* @example
|
|
383
|
+
* // basic Heap creation and add operation
|
|
384
|
+
* // Create a min heap (default)
|
|
385
|
+
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
386
|
+
*
|
|
387
|
+
* // Verify size
|
|
388
|
+
* console.log(minHeap.size); // 6;
|
|
389
|
+
*
|
|
390
|
+
* // Add new element
|
|
391
|
+
* minHeap.add(4);
|
|
392
|
+
* console.log(minHeap.size); // 7;
|
|
393
|
+
*
|
|
394
|
+
* // Min heap property: smallest element at root
|
|
395
|
+
* const min = minHeap.peek();
|
|
396
|
+
* console.log(min); // 1;
|
|
397
|
+
*/
|
|
474
398
|
add(element) {
|
|
475
399
|
this._elements.push(element);
|
|
476
400
|
return this._bubbleUp(this.elements.length - 1);
|
|
477
401
|
}
|
|
478
402
|
/**
|
|
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
|
-
|
|
521
|
-
|
|
522
|
-
* @example
|
|
523
|
-
* // Add multiple elements
|
|
524
|
-
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
525
|
-
* heap.addMany([5, 3, 7, 1]);
|
|
526
|
-
* console.log(heap.peek()); // 1;
|
|
527
|
-
* console.log(heap.size); // 4;
|
|
528
|
-
*/
|
|
403
|
+
* Insert many elements from an iterable.
|
|
404
|
+
* @remarks Time O(N log N), Space O(1)
|
|
405
|
+
* @param elements - Iterable of elements or raw values.
|
|
406
|
+
* @returns Array of per-element success flags.
|
|
407
|
+
* @example
|
|
408
|
+
* // Add multiple elements
|
|
409
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
410
|
+
* heap.addMany([5, 3, 7, 1]);
|
|
411
|
+
* console.log(heap.peek()); // 1;
|
|
412
|
+
* console.log(heap.size); // 4;
|
|
413
|
+
*/
|
|
529
414
|
addMany(elements) {
|
|
530
415
|
const flags = [];
|
|
531
416
|
for (const el of elements) {
|
|
@@ -540,104 +425,63 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
540
425
|
return flags;
|
|
541
426
|
}
|
|
542
427
|
/**
|
|
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
|
-
|
|
583
|
-
|
|
584
|
-
* @example
|
|
585
|
-
* // Heap with custom comparator (MaxHeap behavior)
|
|
586
|
-
* interface Task {
|
|
587
|
-
* id: number;
|
|
588
|
-
* priority: number;
|
|
589
|
-
* name: string;
|
|
590
|
-
* }
|
|
591
|
-
*
|
|
592
|
-
* // Custom comparator for max heap behavior (higher priority first)
|
|
593
|
-
* const tasks: Task[] = [
|
|
594
|
-
* { id: 1, priority: 5, name: 'Email' },
|
|
595
|
-
* { id: 2, priority: 3, name: 'Chat' },
|
|
596
|
-
* { id: 3, priority: 8, name: 'Alert' }
|
|
597
|
-
* ];
|
|
598
|
-
*
|
|
599
|
-
* const maxHeap = new Heap(tasks, {
|
|
600
|
-
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
601
|
-
* });
|
|
602
|
-
*
|
|
603
|
-
* console.log(maxHeap.size); // 3;
|
|
604
|
-
*
|
|
605
|
-
* // Peek returns highest priority task
|
|
606
|
-
* const topTask = maxHeap.peek();
|
|
607
|
-
* console.log(topTask?.priority); // 8;
|
|
608
|
-
* console.log(topTask?.name); // 'Alert';
|
|
609
|
-
*/
|
|
428
|
+
* Remove and return the top element.
|
|
429
|
+
* @remarks Time O(log N), Space O(1)
|
|
430
|
+
* @returns Top element or undefined.
|
|
431
|
+
* @example
|
|
432
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
433
|
+
* interface Task {
|
|
434
|
+
* id: number;
|
|
435
|
+
* priority: number;
|
|
436
|
+
* name: string;
|
|
437
|
+
* }
|
|
438
|
+
*
|
|
439
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
440
|
+
* const tasks: Task[] = [
|
|
441
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
442
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
443
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
444
|
+
* ];
|
|
445
|
+
*
|
|
446
|
+
* const maxHeap = new Heap(tasks, {
|
|
447
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
448
|
+
* });
|
|
449
|
+
*
|
|
450
|
+
* console.log(maxHeap.size); // 3;
|
|
451
|
+
*
|
|
452
|
+
* // Peek returns highest priority task
|
|
453
|
+
* const topTask = maxHeap.peek();
|
|
454
|
+
* console.log(topTask?.priority); // 8;
|
|
455
|
+
* console.log(topTask?.name); // 'Alert';
|
|
456
|
+
*/
|
|
610
457
|
/**
|
|
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
|
-
|
|
637
|
-
|
|
638
|
-
* console.log(topTask?.priority); // 8;
|
|
639
|
-
* console.log(topTask?.name); // 'Alert';
|
|
640
|
-
*/
|
|
458
|
+
* @deprecated Use `pop` instead. Will be removed in a future major version.
|
|
459
|
+
* @example
|
|
460
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
461
|
+
* interface Task {
|
|
462
|
+
* id: number;
|
|
463
|
+
* priority: number;
|
|
464
|
+
* name: string;
|
|
465
|
+
* }
|
|
466
|
+
*
|
|
467
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
468
|
+
* const tasks: Task[] = [
|
|
469
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
470
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
471
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
472
|
+
* ];
|
|
473
|
+
*
|
|
474
|
+
* const maxHeap = new Heap(tasks, {
|
|
475
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
476
|
+
* });
|
|
477
|
+
*
|
|
478
|
+
* console.log(maxHeap.size); // 3;
|
|
479
|
+
*
|
|
480
|
+
* // Peek returns highest priority task
|
|
481
|
+
* const topTask = maxHeap.peek();
|
|
482
|
+
* console.log(topTask?.priority); // 8;
|
|
483
|
+
* console.log(topTask?.name); // 'Alert';
|
|
484
|
+
*/
|
|
641
485
|
poll() {
|
|
642
486
|
return this.pop();
|
|
643
487
|
}
|
|
@@ -657,319 +501,125 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
657
501
|
return value;
|
|
658
502
|
}
|
|
659
503
|
/**
|
|
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
|
-
* });
|
|
724
|
-
*
|
|
725
|
-
* // Add events in random order
|
|
726
|
-
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
727
|
-
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
728
|
-
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
729
|
-
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
730
|
-
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
731
|
-
*
|
|
732
|
-
* console.log(eventHeap.size); // 5;
|
|
733
|
-
*
|
|
734
|
-
* // Process events by priority (critical first)
|
|
735
|
-
* const processedOrder: Event[] = [];
|
|
736
|
-
* while (eventHeap.size > 0) {
|
|
737
|
-
* const event = eventHeap.poll();
|
|
738
|
-
* if (event) {
|
|
739
|
-
* processedOrder.push(event);
|
|
740
|
-
* }
|
|
741
|
-
* }
|
|
742
|
-
*
|
|
743
|
-
* // Verify critical events came first
|
|
744
|
-
* console.log(processedOrder[0].type); // 'critical';
|
|
745
|
-
* console.log(processedOrder[1].type); // 'critical';
|
|
746
|
-
* console.log(processedOrder[2].type); // 'warning';
|
|
747
|
-
* console.log(processedOrder[3].type); // 'info';
|
|
748
|
-
* console.log(processedOrder[4].type); // 'info';
|
|
749
|
-
*
|
|
750
|
-
* // Verify O(log n) operations
|
|
751
|
-
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
752
|
-
*
|
|
753
|
-
* // Add - O(log n)
|
|
754
|
-
* newHeap.add(2);
|
|
755
|
-
* console.log(newHeap.size); // 5;
|
|
756
|
-
*
|
|
757
|
-
* // Poll - O(log n)
|
|
758
|
-
* const removed = newHeap.poll();
|
|
759
|
-
* console.log(removed); // 1;
|
|
760
|
-
*
|
|
761
|
-
* // Peek - O(1)
|
|
762
|
-
* const top = newHeap.peek();
|
|
763
|
-
* console.log(top); // 2;
|
|
764
|
-
*/
|
|
504
|
+
* Get the current top element without removing it.
|
|
505
|
+
* @remarks Time O(1), Space O(1)
|
|
506
|
+
* @returns Top element or undefined.
|
|
507
|
+
* @example
|
|
508
|
+
* // Heap for event processing with priority
|
|
509
|
+
* interface Event {
|
|
510
|
+
* id: number;
|
|
511
|
+
* type: 'critical' | 'warning' | 'info';
|
|
512
|
+
* timestamp: number;
|
|
513
|
+
* message: string;
|
|
514
|
+
* }
|
|
515
|
+
*
|
|
516
|
+
* // Custom priority: critical > warning > info
|
|
517
|
+
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
518
|
+
*
|
|
519
|
+
* const eventHeap = new Heap<Event>([], {
|
|
520
|
+
* comparator: (a: Event, b: Event) => {
|
|
521
|
+
* const priorityA = priorityMap[a.type];
|
|
522
|
+
* const priorityB = priorityMap[b.type];
|
|
523
|
+
* return priorityB - priorityA; // Higher priority first
|
|
524
|
+
* }
|
|
525
|
+
* });
|
|
526
|
+
*
|
|
527
|
+
* // Add events in random order
|
|
528
|
+
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
529
|
+
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
530
|
+
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
531
|
+
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
532
|
+
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
533
|
+
*
|
|
534
|
+
* console.log(eventHeap.size); // 5;
|
|
535
|
+
*
|
|
536
|
+
* // Process events by priority (critical first)
|
|
537
|
+
* const processedOrder: Event[] = [];
|
|
538
|
+
* while (eventHeap.size > 0) {
|
|
539
|
+
* const event = eventHeap.poll();
|
|
540
|
+
* if (event) {
|
|
541
|
+
* processedOrder.push(event);
|
|
542
|
+
* }
|
|
543
|
+
* }
|
|
544
|
+
*
|
|
545
|
+
* // Verify critical events came first
|
|
546
|
+
* console.log(processedOrder[0].type); // 'critical';
|
|
547
|
+
* console.log(processedOrder[1].type); // 'critical';
|
|
548
|
+
* console.log(processedOrder[2].type); // 'warning';
|
|
549
|
+
* console.log(processedOrder[3].type); // 'info';
|
|
550
|
+
* console.log(processedOrder[4].type); // 'info';
|
|
551
|
+
*
|
|
552
|
+
* // Verify O(log n) operations
|
|
553
|
+
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
554
|
+
*
|
|
555
|
+
* // Add - O(log n)
|
|
556
|
+
* newHeap.add(2);
|
|
557
|
+
* console.log(newHeap.size); // 5;
|
|
558
|
+
*
|
|
559
|
+
* // Poll - O(log n)
|
|
560
|
+
* const removed = newHeap.poll();
|
|
561
|
+
* console.log(removed); // 1;
|
|
562
|
+
*
|
|
563
|
+
* // Peek - O(1)
|
|
564
|
+
* const top = newHeap.peek();
|
|
565
|
+
* console.log(top); // 2;
|
|
566
|
+
*/
|
|
765
567
|
peek() {
|
|
766
568
|
return this.elements[0];
|
|
767
569
|
}
|
|
768
570
|
/**
|
|
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
|
-
|
|
811
|
-
|
|
812
|
-
* @example
|
|
813
|
-
* // Check if heap is empty
|
|
814
|
-
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
815
|
-
* console.log(heap.isEmpty()); // true;
|
|
816
|
-
* heap.add(1);
|
|
817
|
-
* console.log(heap.isEmpty()); // false;
|
|
818
|
-
*/
|
|
571
|
+
* Check whether the heap is empty.
|
|
572
|
+
* @remarks Time O(1), Space O(1)
|
|
573
|
+
* @returns True if size is 0.
|
|
574
|
+
* @example
|
|
575
|
+
* // Check if heap is empty
|
|
576
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
577
|
+
* console.log(heap.isEmpty()); // true;
|
|
578
|
+
* heap.add(1);
|
|
579
|
+
* console.log(heap.isEmpty()); // false;
|
|
580
|
+
*/
|
|
819
581
|
isEmpty() {
|
|
820
582
|
return this.size === 0;
|
|
821
583
|
}
|
|
822
584
|
/**
|
|
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
|
-
|
|
865
|
-
|
|
866
|
-
* @example
|
|
867
|
-
* // Remove all elements
|
|
868
|
-
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
869
|
-
* heap.clear();
|
|
870
|
-
* console.log(heap.isEmpty()); // true;
|
|
871
|
-
*/
|
|
585
|
+
* Remove all elements.
|
|
586
|
+
* @remarks Time O(1), Space O(1)
|
|
587
|
+
* @returns void
|
|
588
|
+
* @example
|
|
589
|
+
* // Remove all elements
|
|
590
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
591
|
+
* heap.clear();
|
|
592
|
+
* console.log(heap.isEmpty()); // true;
|
|
593
|
+
*/
|
|
872
594
|
clear() {
|
|
873
595
|
this._elements = [];
|
|
874
596
|
}
|
|
875
597
|
/**
|
|
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
|
-
|
|
912
|
-
|
|
913
|
-
* @example
|
|
914
|
-
* // Check element existence
|
|
915
|
-
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
916
|
-
* console.log(heap.has(1)); // true;
|
|
917
|
-
* console.log(heap.has(99)); // false;
|
|
918
|
-
*/
|
|
598
|
+
* Check if an equal element exists in the heap.
|
|
599
|
+
* @remarks Time O(N), Space O(1)
|
|
600
|
+
* @param element - Element to search for.
|
|
601
|
+
* @returns True if found.
|
|
602
|
+
* @example
|
|
603
|
+
* // Check element existence
|
|
604
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
605
|
+
* console.log(heap.has(1)); // true;
|
|
606
|
+
* console.log(heap.has(99)); // false;
|
|
607
|
+
*/
|
|
919
608
|
has(element) {
|
|
920
609
|
for (const el of this.elements) if (this._equals(el, element)) return true;
|
|
921
610
|
return false;
|
|
922
611
|
}
|
|
923
612
|
/**
|
|
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
|
-
|
|
966
|
-
|
|
967
|
-
* @example
|
|
968
|
-
* // Remove specific element
|
|
969
|
-
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
970
|
-
* heap.delete(4);
|
|
971
|
-
* console.log(heap.toArray().includes(4)); // false;
|
|
972
|
-
*/
|
|
613
|
+
* Delete one occurrence of an element.
|
|
614
|
+
* @remarks Time O(N), Space O(1)
|
|
615
|
+
* @param element - Element to delete.
|
|
616
|
+
* @returns True if an element was removed.
|
|
617
|
+
* @example
|
|
618
|
+
* // Remove specific element
|
|
619
|
+
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
620
|
+
* heap.delete(4);
|
|
621
|
+
* console.log(heap.toArray().includes(4)); // false;
|
|
622
|
+
*/
|
|
973
623
|
delete(element) {
|
|
974
624
|
let index = -1;
|
|
975
625
|
for (let i = 0; i < this.elements.length; i++) {
|
|
@@ -1033,49 +683,16 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1033
683
|
return this;
|
|
1034
684
|
}
|
|
1035
685
|
/**
|
|
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
|
-
|
|
1072
|
-
|
|
1073
|
-
* @example
|
|
1074
|
-
* // Depth-first traversal
|
|
1075
|
-
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
1076
|
-
* const result = heap.dfs('IN');
|
|
1077
|
-
* console.log(result.length); // 3;
|
|
1078
|
-
*/
|
|
686
|
+
* Traverse the binary heap as a complete binary tree and collect elements.
|
|
687
|
+
* @remarks Time O(N), Space O(H)
|
|
688
|
+
* @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
689
|
+
* @returns Array of visited elements.
|
|
690
|
+
* @example
|
|
691
|
+
* // Depth-first traversal
|
|
692
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
693
|
+
* const result = heap.dfs('IN');
|
|
694
|
+
* console.log(result.length); // 3;
|
|
695
|
+
*/
|
|
1079
696
|
dfs(order = "PRE") {
|
|
1080
697
|
const result = [];
|
|
1081
698
|
const _dfs = /* @__PURE__ */ __name((index) => {
|
|
@@ -1112,57 +729,15 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1112
729
|
return results;
|
|
1113
730
|
}
|
|
1114
731
|
/**
|
|
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
|
-
|
|
1159
|
-
|
|
1160
|
-
* @example
|
|
1161
|
-
* // Sort elements using heap
|
|
1162
|
-
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
1163
|
-
* const sorted = heap.sort();
|
|
1164
|
-
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
1165
|
-
*/
|
|
732
|
+
* Return all elements in ascending order by repeatedly polling.
|
|
733
|
+
* @remarks Time O(N log N), Space O(N)
|
|
734
|
+
* @returns Sorted array of elements.
|
|
735
|
+
* @example
|
|
736
|
+
* // Sort elements using heap
|
|
737
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
738
|
+
* const sorted = heap.sort();
|
|
739
|
+
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
740
|
+
*/
|
|
1166
741
|
sort() {
|
|
1167
742
|
const visited = [];
|
|
1168
743
|
const cloned = this._createInstance();
|
|
@@ -1174,114 +749,34 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1174
749
|
return visited;
|
|
1175
750
|
}
|
|
1176
751
|
/**
|
|
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
|
-
|
|
1219
|
-
|
|
1220
|
-
* @example
|
|
1221
|
-
* // Create independent copy
|
|
1222
|
-
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
1223
|
-
* const copy = heap.clone();
|
|
1224
|
-
* copy.poll();
|
|
1225
|
-
* console.log(heap.size); // 3;
|
|
1226
|
-
* console.log(copy.size); // 2;
|
|
1227
|
-
*/
|
|
752
|
+
* Deep clone this heap.
|
|
753
|
+
* @remarks Time O(N), Space O(N)
|
|
754
|
+
* @returns A new heap with the same elements.
|
|
755
|
+
* @example
|
|
756
|
+
* // Create independent copy
|
|
757
|
+
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
758
|
+
* const copy = heap.clone();
|
|
759
|
+
* copy.poll();
|
|
760
|
+
* console.log(heap.size); // 3;
|
|
761
|
+
* console.log(copy.size); // 2;
|
|
762
|
+
*/
|
|
1228
763
|
clone() {
|
|
1229
764
|
const next = this._createInstance();
|
|
1230
765
|
for (const x of this.elements) next.add(x);
|
|
1231
766
|
return next;
|
|
1232
767
|
}
|
|
1233
768
|
/**
|
|
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
|
-
|
|
1278
|
-
|
|
1279
|
-
* @example
|
|
1280
|
-
* // Filter elements
|
|
1281
|
-
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
1282
|
-
* const evens = heap.filter(x => x % 2 === 0);
|
|
1283
|
-
* console.log(evens.size); // 2;
|
|
1284
|
-
*/
|
|
769
|
+
* Filter elements into a new heap of the same class.
|
|
770
|
+
* @remarks Time O(N log N), Space O(N)
|
|
771
|
+
* @param callback - Predicate (element, index, heap) → boolean to keep element.
|
|
772
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
773
|
+
* @returns A new heap with the kept elements.
|
|
774
|
+
* @example
|
|
775
|
+
* // Filter elements
|
|
776
|
+
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
777
|
+
* const evens = heap.filter(x => x % 2 === 0);
|
|
778
|
+
* console.log(evens.size); // 2;
|
|
779
|
+
*/
|
|
1285
780
|
filter(callback, thisArg) {
|
|
1286
781
|
const out = this._createInstance();
|
|
1287
782
|
let i = 0;
|
|
@@ -1295,59 +790,20 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1295
790
|
return out;
|
|
1296
791
|
}
|
|
1297
792
|
/**
|
|
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
|
-
|
|
1344
|
-
|
|
1345
|
-
* @example
|
|
1346
|
-
* // Transform elements
|
|
1347
|
-
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
1348
|
-
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
1349
|
-
* console.log(doubled.peek()); // 2;
|
|
1350
|
-
*/
|
|
793
|
+
* Map elements into a new heap of possibly different element type.
|
|
794
|
+
* @remarks Time O(N log N), Space O(N)
|
|
795
|
+
* @template EM
|
|
796
|
+
* @template RM
|
|
797
|
+
* @param callback - Mapping function (element, index, heap) → newElement.
|
|
798
|
+
* @param options - Options for the output heap, including comparator for EM.
|
|
799
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
800
|
+
* @returns A new heap with mapped elements.
|
|
801
|
+
* @example
|
|
802
|
+
* // Transform elements
|
|
803
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
804
|
+
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
805
|
+
* console.log(doubled.peek()); // 2;
|
|
806
|
+
*/
|
|
1351
807
|
map(callback, options, thisArg) {
|
|
1352
808
|
const { comparator, toElementFn, ...rest } = options ?? {};
|
|
1353
809
|
if (!comparator) raise(TypeError, ERR.comparatorRequired("Heap.map"));
|
|
@@ -1384,14 +840,6 @@ var Heap = class _Heap extends IterableElementBase {
|
|
|
1384
840
|
return 0;
|
|
1385
841
|
}, "_DEFAULT_COMPARATOR");
|
|
1386
842
|
_comparator = this._DEFAULT_COMPARATOR;
|
|
1387
|
-
/**
|
|
1388
|
-
* Get the comparator used to order elements.
|
|
1389
|
-
* @remarks Time O(1), Space O(1)
|
|
1390
|
-
* @returns Comparator function.
|
|
1391
|
-
*/
|
|
1392
|
-
get comparator() {
|
|
1393
|
-
return this._comparator;
|
|
1394
|
-
}
|
|
1395
843
|
*_getIterator() {
|
|
1396
844
|
for (const element of this.elements) yield element;
|
|
1397
845
|
}
|
|
@@ -1480,6 +928,7 @@ var FibonacciHeap = class {
|
|
|
1480
928
|
static {
|
|
1481
929
|
__name(this, "FibonacciHeap");
|
|
1482
930
|
}
|
|
931
|
+
_comparator;
|
|
1483
932
|
/**
|
|
1484
933
|
* Create a FibonacciHeap.
|
|
1485
934
|
* @remarks Time O(1), Space O(1)
|
|
@@ -1513,7 +962,6 @@ var FibonacciHeap = class {
|
|
|
1513
962
|
get min() {
|
|
1514
963
|
return this._min;
|
|
1515
964
|
}
|
|
1516
|
-
_comparator;
|
|
1517
965
|
get comparator() {
|
|
1518
966
|
return this._comparator;
|
|
1519
967
|
}
|