data-structure-typed 2.6.0 → 2.6.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/.github/workflows/ci.yml +7 -2
- package/.github/workflows/release-package.yml +9 -2
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +108 -108
- package/docs-site-docusaurus/docs/api/classes/BST.md +101 -101
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +66 -66
- package/docs-site-docusaurus/docs/api/classes/Deque.md +235 -51
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +21 -21
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +231 -67
- 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 +14 -14
- package/docs-site-docusaurus/docs/api/classes/Heap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +83 -13
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +124 -20
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +140 -32
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +23 -23
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +159 -51
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +20 -20
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +23 -23
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/Queue.md +142 -34
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +117 -117
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +158 -50
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +21 -21
- package/docs-site-docusaurus/docs/api/classes/Stack.md +108 -26
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +33 -33
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +75 -39
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +301 -39
- package/docs-site-docusaurus/docs/api/classes/Trie.md +110 -28
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +20 -20
- package/package.json +45 -46
- package/src/common/error.ts +15 -32
- package/src/common/index.ts +0 -3
- package/src/data-structures/base/iterable-element-base.ts +0 -3
- package/src/data-structures/base/linear-base.ts +2 -36
- package/src/data-structures/binary-tree/avl-tree.ts +31 -529
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +47 -572
- package/src/data-structures/binary-tree/binary-tree.ts +326 -1311
- package/src/data-structures/binary-tree/bst.ts +158 -1082
- package/src/data-structures/binary-tree/red-black-tree.ts +451 -1290
- package/src/data-structures/binary-tree/segment-tree.ts +73 -351
- package/src/data-structures/binary-tree/tree-map.ts +462 -5124
- package/src/data-structures/binary-tree/tree-multi-map.ts +302 -4914
- package/src/data-structures/binary-tree/tree-multi-set.ts +284 -3972
- package/src/data-structures/binary-tree/tree-set.ts +338 -4836
- package/src/data-structures/graph/abstract-graph.ts +98 -167
- package/src/data-structures/graph/directed-graph.ts +137 -562
- package/src/data-structures/graph/map-graph.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +132 -511
- package/src/data-structures/hash/hash-map.ts +154 -582
- package/src/data-structures/heap/heap.ts +200 -795
- package/src/data-structures/linked-list/doubly-linked-list.ts +121 -865
- package/src/data-structures/linked-list/singly-linked-list.ts +122 -794
- package/src/data-structures/linked-list/skip-linked-list.ts +211 -918
- package/src/data-structures/matrix/matrix.ts +179 -518
- package/src/data-structures/matrix/navigator.ts +0 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -6
- package/src/data-structures/priority-queue/min-priority-queue.ts +6 -11
- package/src/data-structures/priority-queue/priority-queue.ts +1 -2
- package/src/data-structures/queue/deque.ts +214 -882
- package/src/data-structures/queue/queue.ts +102 -625
- package/src/data-structures/stack/stack.ts +76 -505
- package/src/data-structures/trie/trie.ts +98 -628
- package/src/types/common.ts +0 -10
- package/src/types/data-structures/binary-tree/bst.ts +0 -7
- package/src/types/data-structures/binary-tree/red-black-tree.ts +0 -1
- package/src/types/data-structures/graph/abstract-graph.ts +0 -2
- package/src/types/data-structures/hash/hash-map.ts +0 -3
- package/src/types/data-structures/hash/index.ts +0 -1
- package/src/types/data-structures/matrix/navigator.ts +0 -2
- package/src/types/utils/utils.ts +0 -7
- package/src/types/utils/validate-type.ts +0 -7
- package/src/utils/number.ts +0 -2
- package/src/utils/utils.ts +0 -5
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
|
|
9
8
|
import { BST } from './bst';
|
|
10
9
|
import type {
|
|
11
10
|
AVLTreeOptions,
|
|
@@ -122,7 +121,7 @@ export class AVLTreeNode<K = any, V = any> {
|
|
|
122
121
|
*
|
|
123
122
|
* @returns The node's color.
|
|
124
123
|
*/
|
|
125
|
-
/* istanbul ignore next -- inherited field, used by RedBlackTree subclass */
|
|
124
|
+
/* istanbul ignore next -- inherited field, used by RedBlackTree subclass */ /* istanbul ignore next -- inherited field, not used by AVLTree */
|
|
126
125
|
get color(): RBTNColor {
|
|
127
126
|
return this._color;
|
|
128
127
|
}
|
|
@@ -140,6 +139,7 @@ export class AVLTreeNode<K = any, V = any> {
|
|
|
140
139
|
*
|
|
141
140
|
* @returns The subtree node count.
|
|
142
141
|
*/
|
|
142
|
+
|
|
143
143
|
/* istanbul ignore next -- inherited field, not used by AVLTree */
|
|
144
144
|
get count(): number {
|
|
145
145
|
return this._count;
|
|
@@ -160,13 +160,11 @@ export class AVLTreeNode<K = any, V = any> {
|
|
|
160
160
|
if (!this.parent) {
|
|
161
161
|
return this.left || this.right ? 'ROOT' : 'ISOLATED';
|
|
162
162
|
}
|
|
163
|
-
|
|
164
163
|
if (this.parent.left === this) {
|
|
165
164
|
return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
|
|
166
165
|
} else if (this.parent.right === this) {
|
|
167
166
|
return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
|
|
168
167
|
}
|
|
169
|
-
|
|
170
168
|
/* istanbul ignore next -- defensive: unreachable if tree structure is correct */
|
|
171
169
|
return 'MAL_NODE';
|
|
172
170
|
}
|
|
@@ -359,172 +357,12 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
359
357
|
* @param keyNodeOrEntry - The key, node, or entry to set.
|
|
360
358
|
* @param [value] - The value, if providing just a key.
|
|
361
359
|
* @returns True if the addition was successful, false otherwise.
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
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
|
-
|
|
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
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
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
|
-
|
|
521
|
-
|
|
522
|
-
* @example
|
|
523
|
-
* // Set a key-value pair
|
|
524
|
-
* const avl = new AVLTree<number, string>();
|
|
525
|
-
* avl.set(1, 'one');
|
|
526
|
-
* avl.set(2, 'two');
|
|
527
|
-
* console.log(avl.get(1)); // 'one';
|
|
360
|
+
* @example
|
|
361
|
+
* // Set a key-value pair
|
|
362
|
+
* const avl = new AVLTree<number, string>();
|
|
363
|
+
* avl.set(1, 'one');
|
|
364
|
+
* avl.set(2, 'two');
|
|
365
|
+
* console.log(avl.get(1)); // 'one';
|
|
528
366
|
*/
|
|
529
367
|
override set(
|
|
530
368
|
keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
@@ -543,138 +381,12 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
543
381
|
*
|
|
544
382
|
* @param keyNodeOrEntry - The node to delete.
|
|
545
383
|
* @returns An array containing deletion results.
|
|
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
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
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
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
* @example
|
|
673
|
-
* // Remove nodes and verify structure
|
|
674
|
-
* const avl = new AVLTree<number>([5, 3, 7, 1, 4, 6, 8]);
|
|
675
|
-
* avl.delete(3);
|
|
676
|
-
* console.log(avl.has(3)); // false;
|
|
677
|
-
* console.log(avl.size); // 6;
|
|
384
|
+
* @example
|
|
385
|
+
* // Remove nodes and verify structure
|
|
386
|
+
* const avl = new AVLTree<number>([5, 3, 7, 1, 4, 6, 8]);
|
|
387
|
+
* avl.delete(3);
|
|
388
|
+
* console.log(avl.has(3)); // false;
|
|
389
|
+
* console.log(avl.size); // 6;
|
|
678
390
|
*/
|
|
679
391
|
override delete(
|
|
680
392
|
keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
|
|
@@ -696,96 +408,20 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
696
408
|
*
|
|
697
409
|
* @param [iterationType=this.iterationType] - The traversal method for the initial node export.
|
|
698
410
|
* @returns True if successful, false if the tree was empty.
|
|
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
|
-
|
|
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
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
* @example
|
|
774
|
-
* // Rebalance the tree
|
|
775
|
-
* const avl = new AVLTree<number>();
|
|
776
|
-
* // Insert in sorted order (worst case for BST)
|
|
777
|
-
* for (let i = 1; i <= 7; i++) avl.add(i);
|
|
778
|
-
* console.log(avl.isAVLBalanced()); // false;
|
|
779
|
-
* avl.perfectlyBalance();
|
|
780
|
-
* console.log(avl.isAVLBalanced()); // true;
|
|
411
|
+
* @example
|
|
412
|
+
* // Rebalance the tree
|
|
413
|
+
* const avl = new AVLTree<number>();
|
|
414
|
+
* // Insert in sorted order (worst case for BST)
|
|
415
|
+
* for (let i = 1; i <= 7; i++) avl.add(i);
|
|
416
|
+
* console.log(avl.isAVLBalanced()); // false;
|
|
417
|
+
* avl.perfectlyBalance();
|
|
418
|
+
* console.log(avl.isAVLBalanced()); // true;
|
|
781
419
|
*/
|
|
782
420
|
override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
|
|
783
421
|
const nodes = this.dfs(node => node, 'IN', false, this._root, iterationType);
|
|
784
422
|
const n = nodes.length;
|
|
785
423
|
if (n === 0) return false;
|
|
786
|
-
|
|
787
424
|
this._clearNodes();
|
|
788
|
-
|
|
789
425
|
// Build balanced tree from sorted array
|
|
790
426
|
const build = (l: number, r: number, parent?: AVLTreeNode<K, V>): AVLTreeNode<K, V> | undefined => {
|
|
791
427
|
if (l > r) return undefined;
|
|
@@ -794,14 +430,12 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
794
430
|
root.left = build(l, m - 1, root);
|
|
795
431
|
root.right = build(m + 1, r, root);
|
|
796
432
|
root.parent = parent;
|
|
797
|
-
|
|
798
433
|
// Update height during the build
|
|
799
434
|
const lh = root.left ? (root.left as AVLTreeNode<K, V>).height : -1;
|
|
800
435
|
const rh = root.right ? (root.right as AVLTreeNode<K, V>).height : -1;
|
|
801
436
|
root.height = Math.max(lh, rh) + 1;
|
|
802
437
|
return root;
|
|
803
438
|
};
|
|
804
|
-
|
|
805
439
|
const newRoot = build(0, n - 1, undefined);
|
|
806
440
|
this._setRoot(newRoot);
|
|
807
441
|
this._size = n;
|
|
@@ -819,126 +453,15 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
819
453
|
* @param [options] - Options for the new AVLTree.
|
|
820
454
|
* @param [thisArg] - `this` context for the callback.
|
|
821
455
|
* @returns A new, mapped AVLTree.
|
|
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
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
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
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
* @example
|
|
938
|
-
* // Transform to new tree
|
|
939
|
-
* const avl = new AVLTree<number, number>([[1, 10], [2, 20], [3, 30]]);
|
|
940
|
-
* const doubled = avl.map((value, key) => [key, (value ?? 0) * 2] as [number, number]);
|
|
941
|
-
* console.log([...doubled.values()]); // [20, 40, 60];
|
|
456
|
+
* @example
|
|
457
|
+
* // Transform to new tree
|
|
458
|
+
* const avl = new AVLTree<number, number>([
|
|
459
|
+
* [1, 10],
|
|
460
|
+
* [2, 20],
|
|
461
|
+
* [3, 30]
|
|
462
|
+
* ]);
|
|
463
|
+
* const doubled = avl.map((value, key) => [key, (value ?? 0) * 2] as [number, number]);
|
|
464
|
+
* console.log([...doubled.values()]); // [20, 40, 60];
|
|
942
465
|
*/
|
|
943
466
|
override map<MK = K, MV = V, MR = any>(
|
|
944
467
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
@@ -946,7 +469,6 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
946
469
|
thisArg?: unknown
|
|
947
470
|
): AVLTree<MK, MV, MR> {
|
|
948
471
|
const out = this._createLike<MK, MV, MR>([], options);
|
|
949
|
-
|
|
950
472
|
let index = 0;
|
|
951
473
|
// Iterates in-order
|
|
952
474
|
for (const [key, value] of this) {
|
|
@@ -1006,25 +528,20 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1006
528
|
): AVLTreeNode<K, V> | undefined {
|
|
1007
529
|
const srcNodeEnsured = this.ensureNode(srcNode);
|
|
1008
530
|
const destNodeEnsured = this.ensureNode(destNode);
|
|
1009
|
-
|
|
1010
531
|
if (srcNodeEnsured && destNodeEnsured) {
|
|
1011
532
|
const { key, value, height } = destNodeEnsured;
|
|
1012
533
|
const tempNode = this.createNode(key, value);
|
|
1013
|
-
|
|
1014
534
|
if (tempNode) {
|
|
1015
535
|
tempNode.height = height;
|
|
1016
|
-
|
|
1017
536
|
// Copy src to dest
|
|
1018
537
|
destNodeEnsured.key = srcNodeEnsured.key;
|
|
1019
538
|
if (!this._isMapMode) destNodeEnsured.value = srcNodeEnsured.value;
|
|
1020
539
|
destNodeEnsured.height = srcNodeEnsured.height;
|
|
1021
|
-
|
|
1022
540
|
// Copy temp (original dest) to src
|
|
1023
541
|
srcNodeEnsured.key = tempNode.key;
|
|
1024
542
|
if (!this._isMapMode) srcNodeEnsured.value = tempNode.value;
|
|
1025
543
|
srcNodeEnsured.height = tempNode.height;
|
|
1026
544
|
}
|
|
1027
|
-
|
|
1028
545
|
return destNodeEnsured;
|
|
1029
546
|
}
|
|
1030
547
|
/* istanbul ignore next -- defensive: srcNode/destNode are always valid when called internally */
|
|
@@ -1070,7 +587,6 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1070
587
|
B.right.parent = A;
|
|
1071
588
|
}
|
|
1072
589
|
if (B) B.parent = parentOfA;
|
|
1073
|
-
|
|
1074
590
|
// Update parent's child pointer
|
|
1075
591
|
if (A === this.root) {
|
|
1076
592
|
if (B) this._setRoot(B);
|
|
@@ -1081,7 +597,6 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1081
597
|
if (parentOfA) parentOfA.right = B;
|
|
1082
598
|
}
|
|
1083
599
|
}
|
|
1084
|
-
|
|
1085
600
|
// Perform rotation
|
|
1086
601
|
if (B) {
|
|
1087
602
|
A.left = B.right;
|
|
@@ -1108,7 +623,6 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1108
623
|
}
|
|
1109
624
|
if (A && C !== null) A.parent = C;
|
|
1110
625
|
if (B && C !== null) B.parent = C;
|
|
1111
|
-
|
|
1112
626
|
if (C) {
|
|
1113
627
|
if (C.left) {
|
|
1114
628
|
if (B !== null) C.left.parent = B;
|
|
@@ -1118,7 +632,6 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1118
632
|
}
|
|
1119
633
|
C.parent = parentOfA;
|
|
1120
634
|
}
|
|
1121
|
-
|
|
1122
635
|
// Update parent's child pointer
|
|
1123
636
|
if (A === this.root) {
|
|
1124
637
|
if (C) this._setRoot(C);
|
|
@@ -1131,7 +644,6 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1131
644
|
}
|
|
1132
645
|
}
|
|
1133
646
|
}
|
|
1134
|
-
|
|
1135
647
|
// Perform rotation
|
|
1136
648
|
if (C) {
|
|
1137
649
|
A.left = C.right;
|
|
@@ -1139,7 +651,6 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1139
651
|
C.left = B;
|
|
1140
652
|
C.right = A;
|
|
1141
653
|
}
|
|
1142
|
-
|
|
1143
654
|
this._updateHeight(A);
|
|
1144
655
|
if (B) this._updateHeight(B);
|
|
1145
656
|
if (C) this._updateHeight(C);
|
|
@@ -1164,7 +675,6 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1164
675
|
}
|
|
1165
676
|
B.parent = parentOfA;
|
|
1166
677
|
}
|
|
1167
|
-
|
|
1168
678
|
// Update parent's child pointer
|
|
1169
679
|
if (A === this.root) {
|
|
1170
680
|
if (B) this._setRoot(B);
|
|
@@ -1177,7 +687,6 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1177
687
|
}
|
|
1178
688
|
}
|
|
1179
689
|
}
|
|
1180
|
-
|
|
1181
690
|
// Perform rotation
|
|
1182
691
|
if (B) {
|
|
1183
692
|
A.right = B.left;
|
|
@@ -1202,10 +711,8 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1202
711
|
if (B) {
|
|
1203
712
|
C = B.left; // The "middle" node
|
|
1204
713
|
}
|
|
1205
|
-
|
|
1206
714
|
if (C !== null) A.parent = C;
|
|
1207
715
|
if (B && C !== null) B.parent = C;
|
|
1208
|
-
|
|
1209
716
|
if (C) {
|
|
1210
717
|
if (C.left) {
|
|
1211
718
|
C.left.parent = A;
|
|
@@ -1215,7 +722,6 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1215
722
|
}
|
|
1216
723
|
C.parent = parentOfA;
|
|
1217
724
|
}
|
|
1218
|
-
|
|
1219
725
|
// Update parent's child pointer
|
|
1220
726
|
if (A === this.root) {
|
|
1221
727
|
if (C) this._setRoot(C);
|
|
@@ -1228,13 +734,11 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1228
734
|
}
|
|
1229
735
|
}
|
|
1230
736
|
}
|
|
1231
|
-
|
|
1232
737
|
// Perform rotation
|
|
1233
738
|
if (C) A.right = C.left;
|
|
1234
739
|
if (B && C) B.left = C.right;
|
|
1235
740
|
if (C) C.left = A;
|
|
1236
741
|
if (C) C.right = B;
|
|
1237
|
-
|
|
1238
742
|
this._updateHeight(A);
|
|
1239
743
|
if (B) this._updateHeight(B);
|
|
1240
744
|
if (C) this._updateHeight(C);
|
|
@@ -1253,14 +757,12 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
1253
757
|
// Get the path from the node to the root.
|
|
1254
758
|
node = this.ensureNode(node);
|
|
1255
759
|
const path = this.getPathToRoot(node, node => node, false);
|
|
1256
|
-
|
|
1257
760
|
// Iterate up the path (from node to root)
|
|
1258
761
|
for (let i = 0; i < path.length; i++) {
|
|
1259
762
|
const A = path[i];
|
|
1260
763
|
if (A) {
|
|
1261
764
|
this._updateHeight(A);
|
|
1262
765
|
this._updateCount(A);
|
|
1263
|
-
|
|
1264
766
|
// Check the balance factor
|
|
1265
767
|
switch (this._balanceFactor(A)) {
|
|
1266
768
|
case -2: // Left-heavy
|