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
|
@@ -25,7 +25,7 @@ import { BinaryTree } from './binary-tree';
|
|
|
25
25
|
import { IBinaryTree } from '../../interfaces';
|
|
26
26
|
import { Queue } from '../queue';
|
|
27
27
|
import { isComparable } from '../../utils';
|
|
28
|
-
import { ERR,
|
|
28
|
+
import { ERR, raise, Range } from '../../common';
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
31
|
* Represents a Node in a Binary Search Tree.
|
|
@@ -104,6 +104,7 @@ export class BSTNode<K = any, V = any> {
|
|
|
104
104
|
*
|
|
105
105
|
* @returns The height.
|
|
106
106
|
*/
|
|
107
|
+
|
|
107
108
|
/* istanbul ignore next -- covered by AVLTree/RedBlackTree tests (subclass uses height) */
|
|
108
109
|
get height(): number {
|
|
109
110
|
return this._height;
|
|
@@ -115,6 +116,7 @@ export class BSTNode<K = any, V = any> {
|
|
|
115
116
|
*
|
|
116
117
|
* @param value - The new height.
|
|
117
118
|
*/
|
|
119
|
+
|
|
118
120
|
/* istanbul ignore next -- covered by AVLTree/RedBlackTree tests (subclass uses height) */
|
|
119
121
|
set height(value: number) {
|
|
120
122
|
this._height = value;
|
|
@@ -128,6 +130,7 @@ export class BSTNode<K = any, V = any> {
|
|
|
128
130
|
*
|
|
129
131
|
* @returns The node's color.
|
|
130
132
|
*/
|
|
133
|
+
|
|
131
134
|
/* istanbul ignore next -- covered by RedBlackTree tests (subclass uses color) */
|
|
132
135
|
get color(): RBTNColor {
|
|
133
136
|
return this._color;
|
|
@@ -139,6 +142,7 @@ export class BSTNode<K = any, V = any> {
|
|
|
139
142
|
*
|
|
140
143
|
* @param value - The new color.
|
|
141
144
|
*/
|
|
145
|
+
|
|
142
146
|
/* istanbul ignore next -- covered by RedBlackTree tests (subclass uses color) */
|
|
143
147
|
set color(value: RBTNColor) {
|
|
144
148
|
this._color = value;
|
|
@@ -152,6 +156,7 @@ export class BSTNode<K = any, V = any> {
|
|
|
152
156
|
*
|
|
153
157
|
* @returns The subtree node count.
|
|
154
158
|
*/
|
|
159
|
+
|
|
155
160
|
/* istanbul ignore next -- internal field used by subclasses */
|
|
156
161
|
get count(): number {
|
|
157
162
|
return this._count;
|
|
@@ -163,6 +168,7 @@ export class BSTNode<K = any, V = any> {
|
|
|
163
168
|
*
|
|
164
169
|
* @param value - The new count.
|
|
165
170
|
*/
|
|
171
|
+
|
|
166
172
|
/* istanbul ignore next -- internal field used by subclasses */
|
|
167
173
|
set count(value: number) {
|
|
168
174
|
this._count = value;
|
|
@@ -324,6 +330,15 @@ export class BSTNode<K = any, V = any> {
|
|
|
324
330
|
* console.log(findLCA(20, 30)); // 25;
|
|
325
331
|
*/
|
|
326
332
|
export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implements IBinaryTree<K, V, R> {
|
|
333
|
+
protected _enableOrderStatistic: boolean = false;
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* The comparator function used to determine the order of keys in the tree.
|
|
337
|
+
|
|
338
|
+
* @remarks Time O(1) Space O(1)
|
|
339
|
+
*/
|
|
340
|
+
protected readonly _comparator: Comparator<K>;
|
|
341
|
+
|
|
327
342
|
/**
|
|
328
343
|
* Creates an instance of BST.
|
|
329
344
|
* @remarks Time O(N log N) or O(N^2) depending on `isBalanceAdd` in `addMany` and input order. Space O(N).
|
|
@@ -356,8 +371,6 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
356
371
|
|
|
357
372
|
protected override _root?: BSTNode<K, V> = undefined;
|
|
358
373
|
|
|
359
|
-
protected _enableOrderStatistic: boolean = false;
|
|
360
|
-
|
|
361
374
|
/**
|
|
362
375
|
* Gets the root node of the tree.
|
|
363
376
|
* @remarks Time O(1)
|
|
@@ -368,13 +381,6 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
368
381
|
return this._root;
|
|
369
382
|
}
|
|
370
383
|
|
|
371
|
-
/**
|
|
372
|
-
* The comparator function used to determine the order of keys in the tree.
|
|
373
|
-
|
|
374
|
-
* @remarks Time O(1) Space O(1)
|
|
375
|
-
*/
|
|
376
|
-
protected readonly _comparator: Comparator<K>;
|
|
377
|
-
|
|
378
384
|
/**
|
|
379
385
|
* Gets the comparator function used by the tree.
|
|
380
386
|
* @remarks Time O(1)
|
|
@@ -436,97 +442,13 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
436
442
|
return isComparable(key);
|
|
437
443
|
}
|
|
438
444
|
|
|
439
|
-
|
|
445
|
+
/**
|
|
440
446
|
* Depth-first search traversal
|
|
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
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
* @example
|
|
526
|
-
* // Depth-first traversal
|
|
527
|
-
* const bst = new BST<number>([5, 3, 7, 1, 4]);
|
|
528
|
-
* const inOrder = bst.dfs(node => node.key, 'IN');
|
|
529
|
-
* console.log(inOrder); // [1, 3, 4, 5, 7];
|
|
447
|
+
* @example
|
|
448
|
+
* // Depth-first traversal
|
|
449
|
+
* const bst = new BST<number>([5, 3, 7, 1, 4]);
|
|
450
|
+
* const inOrder = bst.dfs(node => node.key, 'IN');
|
|
451
|
+
* console.log(inOrder); // [1, 3, 4, 5, 7];
|
|
530
452
|
*/
|
|
531
453
|
override dfs(): (K | undefined)[];
|
|
532
454
|
|
|
@@ -560,89 +482,13 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
560
482
|
return super.dfs(callback, pattern, onlyOne, startNode, iterationType);
|
|
561
483
|
}
|
|
562
484
|
|
|
563
|
-
|
|
485
|
+
/**
|
|
564
486
|
* BinaryTree level-order traversal
|
|
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
|
-
* @example
|
|
642
|
-
* // Breadth-first traversal
|
|
643
|
-
* const bst = new BST<number>([5, 3, 7]);
|
|
644
|
-
* const result = bst.bfs(node => node.key);
|
|
645
|
-
* console.log(result.length); // 3;
|
|
487
|
+
* @example
|
|
488
|
+
* // Breadth-first traversal
|
|
489
|
+
* const bst = new BST<number>([5, 3, 7]);
|
|
490
|
+
* const result = bst.bfs(node => node.key);
|
|
491
|
+
* console.log(result.length); // 3;
|
|
646
492
|
*/
|
|
647
493
|
override bfs(): (K | undefined)[];
|
|
648
494
|
override bfs<C extends NodeCallback<BSTNode<K, V>>>(
|
|
@@ -669,95 +515,16 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
669
515
|
return super.bfs(callback, startNode, iterationType, false);
|
|
670
516
|
}
|
|
671
517
|
|
|
672
|
-
|
|
518
|
+
/**
|
|
673
519
|
* Level-order grouping
|
|
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
|
-
|
|
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
|
-
* @example
|
|
754
|
-
* // Level-order grouping
|
|
755
|
-
* const bst = new BST<number>([5, 3, 7, 1, 4]);
|
|
756
|
-
* const levels = bst.listLevels(node => node.key);
|
|
757
|
-
* console.log(levels); // toBeInstanceOf;
|
|
758
|
-
* console.log(levels[0].length); // 1; // root level has 1 node
|
|
759
|
-
* const allKeys = levels.flat().sort((a, b) => a - b);
|
|
760
|
-
* console.log(allKeys); // [1, 3, 4, 5, 7];
|
|
520
|
+
* @example
|
|
521
|
+
* // Level-order grouping
|
|
522
|
+
* const bst = new BST<number>([5, 3, 7, 1, 4]);
|
|
523
|
+
* const levels = bst.listLevels(node => node.key);
|
|
524
|
+
* console.log(levels); // toBeInstanceOf;
|
|
525
|
+
* console.log(levels[0].length); // 1; // root level has 1 node
|
|
526
|
+
* const allKeys = levels.flat().sort((a, b) => a - b);
|
|
527
|
+
* console.log(allKeys); // [1, 3, 4, 5, 7];
|
|
761
528
|
*/
|
|
762
529
|
override listLevels(): (K | undefined)[][];
|
|
763
530
|
|
|
@@ -793,93 +560,16 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
793
560
|
* @param [startNode=this._root] - The node to start the search from.
|
|
794
561
|
* @param [iterationType=this.iterationType] - The traversal method.
|
|
795
562
|
* @returns The first matching node, or undefined if not found.
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
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
|
-
* @example
|
|
878
|
-
* // Get node object by key
|
|
879
|
-
* const bst = new BST<number, string>([[5, 'root'], [3, 'left'], [7, 'right']]);
|
|
880
|
-
* const node = bst.getNode(3);
|
|
881
|
-
* console.log(node?.key); // 3;
|
|
882
|
-
* console.log(node?.value); // 'left';
|
|
563
|
+
* @example
|
|
564
|
+
* // Get node object by key
|
|
565
|
+
* const bst = new BST<number, string>([
|
|
566
|
+
* [5, 'root'],
|
|
567
|
+
* [3, 'left'],
|
|
568
|
+
* [7, 'right']
|
|
569
|
+
* ]);
|
|
570
|
+
* const node = bst.getNode(3);
|
|
571
|
+
* console.log(node?.key); // 3;
|
|
572
|
+
* console.log(node?.value); // 'left';
|
|
883
573
|
*/
|
|
884
574
|
override getNode(
|
|
885
575
|
keyNodeEntryOrPredicate:
|
|
@@ -940,88 +630,18 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
940
630
|
return undefined;
|
|
941
631
|
}
|
|
942
632
|
|
|
943
|
-
|
|
944
|
-
/**
|
|
633
|
+
/**
|
|
945
634
|
* Search nodes by predicate
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
* @example
|
|
1021
|
-
* // Search nodes by predicate
|
|
1022
|
-
* const bst = new BST<number, string>([[1, 'a'], [2, 'b'], [3, 'c'], [4, 'd']]);
|
|
1023
|
-
* const found = bst.search(node => node.key > 2, true);
|
|
1024
|
-
* console.log(found.length); // >= 1;
|
|
635
|
+
* @example
|
|
636
|
+
* // Search nodes by predicate
|
|
637
|
+
* const bst = new BST<number, string>([
|
|
638
|
+
* [1, 'a'],
|
|
639
|
+
* [2, 'b'],
|
|
640
|
+
* [3, 'c'],
|
|
641
|
+
* [4, 'd']
|
|
642
|
+
* ]);
|
|
643
|
+
* const found = bst.search(node => node.key > 2, true);
|
|
644
|
+
* console.log(found.length); // >= 1;
|
|
1025
645
|
*/
|
|
1026
646
|
override search(
|
|
1027
647
|
keyNodeEntryOrPredicate:
|
|
@@ -1118,7 +738,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1118
738
|
predicate = node => {
|
|
1119
739
|
/* istanbul ignore next -- node is always defined in iteration callbacks */
|
|
1120
740
|
if (!node) return false;
|
|
1121
|
-
return
|
|
741
|
+
return keyNodeEntryOrPredicate.isInRange(node.key, this._comparator);
|
|
1122
742
|
};
|
|
1123
743
|
} else {
|
|
1124
744
|
predicate = this._ensurePredicate(keyNodeEntryOrPredicate);
|
|
@@ -1126,7 +746,8 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1126
746
|
|
|
1127
747
|
// Optimization: Pruning logic
|
|
1128
748
|
const shouldVisitLeft = (cur: BSTNode<K, V> | null | undefined) => {
|
|
1129
|
-
/* istanbul ignore next -- defensive: cur is always defined when called from iteration */
|
|
749
|
+
/* istanbul ignore next -- defensive: cur is always defined when called from iteration */
|
|
750
|
+
if (!cur) return false;
|
|
1130
751
|
if (!this.isRealNode(cur.left)) return false;
|
|
1131
752
|
if (isRange) {
|
|
1132
753
|
// Range search: Only go left if the current key is >= the lower bound
|
|
@@ -1144,7 +765,8 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1144
765
|
};
|
|
1145
766
|
|
|
1146
767
|
const shouldVisitRight = (cur: BSTNode<K, V> | null | undefined) => {
|
|
1147
|
-
/* istanbul ignore next -- defensive */
|
|
768
|
+
/* istanbul ignore next -- defensive */
|
|
769
|
+
if (!cur) return false;
|
|
1148
770
|
if (!this.isRealNode(cur.right)) return false;
|
|
1149
771
|
if (isRange) {
|
|
1150
772
|
// Range search: Only go right if current key <= upper bound
|
|
@@ -1175,54 +797,12 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1175
797
|
);
|
|
1176
798
|
}
|
|
1177
799
|
|
|
1178
|
-
|
|
800
|
+
/**
|
|
1179
801
|
* Find all keys in a range
|
|
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
|
-
|
|
1221
|
-
|
|
1222
|
-
* @example
|
|
1223
|
-
* // Find all keys in a range
|
|
1224
|
-
* const bst = new BST<number>([10, 20, 30, 40, 50]);
|
|
1225
|
-
* console.log(bst.rangeSearch([15, 35])); // [20, 30];
|
|
802
|
+
* @example
|
|
803
|
+
* // Find all keys in a range
|
|
804
|
+
* const bst = new BST<number>([10, 20, 30, 40, 50]);
|
|
805
|
+
* console.log(bst.rangeSearch([15, 35])); // [20, 30];
|
|
1226
806
|
*/
|
|
1227
807
|
rangeSearch(range: Range<K> | [K, K]): (K | undefined)[];
|
|
1228
808
|
|
|
@@ -1263,12 +843,12 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1263
843
|
*
|
|
1264
844
|
* @param k - The 0-based position in tree order (0 = first element).
|
|
1265
845
|
* @returns The key at position k, or `undefined` if out of bounds.
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
846
|
+
* @example
|
|
847
|
+
* // Order-statistic on BST
|
|
848
|
+
* const tree = new BST<number>([30, 10, 50, 20, 40], { enableOrderStatistic: true });
|
|
849
|
+
* console.log(tree.getByRank(0)); // 10;
|
|
850
|
+
* console.log(tree.getByRank(4)); // 50;
|
|
851
|
+
* console.log(tree.getRank(30)); // 2;
|
|
1272
852
|
*/
|
|
1273
853
|
getByRank(k: number): K | undefined;
|
|
1274
854
|
|
|
@@ -1309,9 +889,10 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1309
889
|
}
|
|
1310
890
|
}
|
|
1311
891
|
|
|
1312
|
-
const node =
|
|
1313
|
-
|
|
1314
|
-
|
|
892
|
+
const node =
|
|
893
|
+
actualIterationType === 'RECURSIVE'
|
|
894
|
+
? this._getByRankRecursive(this._root, k)
|
|
895
|
+
: this._getByRankIterative(this._root, k);
|
|
1315
896
|
|
|
1316
897
|
if (!node) return undefined;
|
|
1317
898
|
return actualCallback ? actualCallback(node) : node.key;
|
|
@@ -1456,15 +1037,16 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1456
1037
|
const count = hi - lo + 1;
|
|
1457
1038
|
|
|
1458
1039
|
// Find the lo-th node, then in-order traverse count nodes
|
|
1459
|
-
const startNode =
|
|
1460
|
-
|
|
1461
|
-
|
|
1040
|
+
const startNode =
|
|
1041
|
+
actualIterationType === 'RECURSIVE'
|
|
1042
|
+
? this._getByRankRecursive(this._root, lo)
|
|
1043
|
+
: this._getByRankIterative(this._root, lo);
|
|
1462
1044
|
|
|
1463
1045
|
if (!startNode) return [];
|
|
1464
1046
|
|
|
1465
1047
|
// In-order traversal from startNode collecting count elements
|
|
1466
1048
|
let collected = 0;
|
|
1467
|
-
const cb = actualCallback ?? this._DEFAULT_NODE_CALLBACK as C;
|
|
1049
|
+
const cb = actualCallback ?? (this._DEFAULT_NODE_CALLBACK as C);
|
|
1468
1050
|
|
|
1469
1051
|
// Use higher() to iterate — it's already O(log n) amortized per step
|
|
1470
1052
|
let current: BSTNode<K, V> | undefined = startNode;
|
|
@@ -1487,130 +1069,12 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1487
1069
|
* @param keyNodeOrEntry - The key, node, or entry to set.
|
|
1488
1070
|
* @param [value] - The value, if providing just a key.
|
|
1489
1071
|
* @returns True if the addition was successful, false otherwise.
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
* @example
|
|
1609
|
-
* // Set a key-value pair
|
|
1610
|
-
* const bst = new BST<number, string>();
|
|
1611
|
-
* bst.set(1, 'one');
|
|
1612
|
-
* bst.set(2, 'two');
|
|
1613
|
-
* console.log(bst.get(1)); // 'one';
|
|
1072
|
+
* @example
|
|
1073
|
+
* // Set a key-value pair
|
|
1074
|
+
* const bst = new BST<number, string>();
|
|
1075
|
+
* bst.set(1, 'one');
|
|
1076
|
+
* bst.set(2, 'two');
|
|
1077
|
+
* console.log(bst.get(1)); // 'one';
|
|
1614
1078
|
*/
|
|
1615
1079
|
override set(
|
|
1616
1080
|
keyNodeOrEntry: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
@@ -1671,85 +1135,16 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1671
1135
|
* @param [isBalanceAdd=true] - If true, builds a balanced tree from the items.
|
|
1672
1136
|
* @param [iterationType=this.iterationType] - The traversal method for balanced set (recursive or iterative).
|
|
1673
1137
|
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
* @example
|
|
1748
|
-
* // Set multiple key-value pairs
|
|
1749
|
-
* const bst = new BST<number, string>();
|
|
1750
|
-
* bst.setMany([[1, 'a'], [2, 'b'], [3, 'c']]);
|
|
1751
|
-
* console.log(bst.size); // 3;
|
|
1752
|
-
* console.log(bst.get(2)); // 'b';
|
|
1138
|
+
* @example
|
|
1139
|
+
* // Set multiple key-value pairs
|
|
1140
|
+
* const bst = new BST<number, string>();
|
|
1141
|
+
* bst.setMany([
|
|
1142
|
+
* [1, 'a'],
|
|
1143
|
+
* [2, 'b'],
|
|
1144
|
+
* [3, 'c']
|
|
1145
|
+
* ]);
|
|
1146
|
+
* console.log(bst.size); // 3;
|
|
1147
|
+
* console.log(bst.get(2)); // 'b';
|
|
1753
1148
|
*/
|
|
1754
1149
|
override setMany(
|
|
1755
1150
|
keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, BSTNode<K, V>>>,
|
|
@@ -1848,54 +1243,12 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1848
1243
|
* Equivalent to Java TreeMap.ceiling.
|
|
1849
1244
|
* Time Complexity: O(log n) average, O(h) worst case.
|
|
1850
1245
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
* @example
|
|
1894
|
-
* // Find the least key ≥ target
|
|
1895
|
-
* const bst = new BST<number>([10, 20, 30, 40, 50]);
|
|
1896
|
-
* console.log(bst.ceiling(25)); // 30;
|
|
1897
|
-
* console.log(bst.ceiling(30)); // 30;
|
|
1898
|
-
* console.log(bst.ceiling(55)); // undefined;
|
|
1246
|
+
* @example
|
|
1247
|
+
* // Find the least key ≥ target
|
|
1248
|
+
* const bst = new BST<number>([10, 20, 30, 40, 50]);
|
|
1249
|
+
* console.log(bst.ceiling(25)); // 30;
|
|
1250
|
+
* console.log(bst.ceiling(30)); // 30;
|
|
1251
|
+
* console.log(bst.ceiling(55)); // undefined;
|
|
1899
1252
|
*/
|
|
1900
1253
|
ceiling(
|
|
1901
1254
|
keyNodeEntryOrPredicate:
|
|
@@ -1961,53 +1314,11 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1961
1314
|
* Equivalent to Java TreeMap.higher.
|
|
1962
1315
|
* Time Complexity: O(log n) average, O(h) worst case.
|
|
1963
1316
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
* @example
|
|
2007
|
-
* // Find the least key strictly > target
|
|
2008
|
-
* const bst = new BST<number>([10, 20, 30, 40]);
|
|
2009
|
-
* console.log(bst.higher(20)); // 30;
|
|
2010
|
-
* console.log(bst.higher(40)); // undefined;
|
|
1317
|
+
* @example
|
|
1318
|
+
* // Find the least key strictly > target
|
|
1319
|
+
* const bst = new BST<number>([10, 20, 30, 40]);
|
|
1320
|
+
* console.log(bst.higher(20)); // 30;
|
|
1321
|
+
* console.log(bst.higher(40)); // undefined;
|
|
2011
1322
|
*/
|
|
2012
1323
|
higher(
|
|
2013
1324
|
keyNodeEntryOrPredicate:
|
|
@@ -2073,54 +1384,12 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
2073
1384
|
* Equivalent to Java TreeMap.floor.
|
|
2074
1385
|
* Time Complexity: O(log n) average, O(h) worst case.
|
|
2075
1386
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
* @example
|
|
2119
|
-
* // Find the greatest key ≤ target
|
|
2120
|
-
* const bst = new BST<number>([10, 20, 30, 40, 50]);
|
|
2121
|
-
* console.log(bst.floor(25)); // 20;
|
|
2122
|
-
* console.log(bst.floor(10)); // 10;
|
|
2123
|
-
* console.log(bst.floor(5)); // undefined;
|
|
1387
|
+
* @example
|
|
1388
|
+
* // Find the greatest key ≤ target
|
|
1389
|
+
* const bst = new BST<number>([10, 20, 30, 40, 50]);
|
|
1390
|
+
* console.log(bst.floor(25)); // 20;
|
|
1391
|
+
* console.log(bst.floor(10)); // 10;
|
|
1392
|
+
* console.log(bst.floor(5)); // undefined;
|
|
2124
1393
|
*/
|
|
2125
1394
|
floor(
|
|
2126
1395
|
keyNodeEntryOrPredicate:
|
|
@@ -2161,7 +1430,8 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
2161
1430
|
iterationType?: IterationType
|
|
2162
1431
|
): K | undefined | ReturnType<C> {
|
|
2163
1432
|
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === undefined) {
|
|
2164
|
-
/* istanbul ignore next */
|
|
1433
|
+
/* istanbul ignore next */
|
|
1434
|
+
if (typeof callback === 'string' || !callback) {
|
|
2165
1435
|
return undefined;
|
|
2166
1436
|
}
|
|
2167
1437
|
return undefined;
|
|
@@ -2195,7 +1465,8 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
2195
1465
|
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
2196
1466
|
const key = keyNodeEntryOrPredicate[0];
|
|
2197
1467
|
if (key === null || key === undefined) {
|
|
2198
|
-
/* istanbul ignore next */
|
|
1468
|
+
/* istanbul ignore next */
|
|
1469
|
+
if (typeof callback === 'string' || !callback) {
|
|
2199
1470
|
return undefined;
|
|
2200
1471
|
}
|
|
2201
1472
|
return undefined;
|
|
@@ -2229,53 +1500,11 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
2229
1500
|
* Equivalent to Java TreeMap.lower.
|
|
2230
1501
|
* Time Complexity: O(log n) average, O(h) worst case.
|
|
2231
1502
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
* @example
|
|
2275
|
-
* // Find the greatest key strictly < target
|
|
2276
|
-
* const bst = new BST<number>([10, 20, 30, 40]);
|
|
2277
|
-
* console.log(bst.lower(30)); // 20;
|
|
2278
|
-
* console.log(bst.lower(10)); // undefined;
|
|
1503
|
+
* @example
|
|
1504
|
+
* // Find the greatest key strictly < target
|
|
1505
|
+
* const bst = new BST<number>([10, 20, 30, 40]);
|
|
1506
|
+
* console.log(bst.lower(30)); // 20;
|
|
1507
|
+
* console.log(bst.lower(10)); // undefined;
|
|
2279
1508
|
*/
|
|
2280
1509
|
lower(
|
|
2281
1510
|
keyNodeEntryOrPredicate:
|
|
@@ -2316,7 +1545,8 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
2316
1545
|
iterationType?: IterationType
|
|
2317
1546
|
): K | undefined | ReturnType<C> {
|
|
2318
1547
|
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === undefined) {
|
|
2319
|
-
/* istanbul ignore next */
|
|
1548
|
+
/* istanbul ignore next */
|
|
1549
|
+
if (typeof callback === 'string' || !callback) {
|
|
2320
1550
|
return undefined;
|
|
2321
1551
|
}
|
|
2322
1552
|
/* istanbul ignore next */
|
|
@@ -2351,10 +1581,11 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
2351
1581
|
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
2352
1582
|
const key = keyNodeEntryOrPredicate[0];
|
|
2353
1583
|
if (key === null || key === undefined) {
|
|
2354
|
-
/* istanbul ignore next */
|
|
1584
|
+
/* istanbul ignore next */
|
|
1585
|
+
if (typeof callback === 'string' || !callback) {
|
|
2355
1586
|
return undefined;
|
|
2356
1587
|
}
|
|
2357
|
-
|
|
1588
|
+
/* istanbul ignore next */
|
|
2358
1589
|
return undefined;
|
|
2359
1590
|
}
|
|
2360
1591
|
targetKey = key;
|
|
@@ -2443,54 +1674,14 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
2443
1674
|
*
|
|
2444
1675
|
* @param [iterationType=this.iterationType] - The traversal method for the initial node export.
|
|
2445
1676
|
* @returns True if successful, false if the tree was empty.
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
* @example
|
|
2487
|
-
* // Rebalance the tree
|
|
2488
|
-
* const bst = new BST<number>();
|
|
2489
|
-
* // Insert in sorted order (worst case for BST)
|
|
2490
|
-
* for (let i = 1; i <= 7; i++) bst.add(i);
|
|
2491
|
-
* console.log(bst.isAVLBalanced()); // false;
|
|
2492
|
-
* bst.perfectlyBalance();
|
|
2493
|
-
* console.log(bst.isAVLBalanced()); // true;
|
|
1677
|
+
* @example
|
|
1678
|
+
* // Rebalance the tree
|
|
1679
|
+
* const bst = new BST<number>();
|
|
1680
|
+
* // Insert in sorted order (worst case for BST)
|
|
1681
|
+
* for (let i = 1; i <= 7; i++) bst.add(i);
|
|
1682
|
+
* console.log(bst.isAVLBalanced()); // false;
|
|
1683
|
+
* bst.perfectlyBalance();
|
|
1684
|
+
* console.log(bst.isAVLBalanced()); // true;
|
|
2494
1685
|
*/
|
|
2495
1686
|
perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
|
|
2496
1687
|
const nodes = this.dfs(node => node, 'IN', false, this._root, iterationType);
|
|
@@ -2523,50 +1714,10 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
2523
1714
|
*
|
|
2524
1715
|
* @param [iterationType=this.iterationType] - The traversal method.
|
|
2525
1716
|
* @returns True if the tree is AVL balanced, false otherwise.
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
|
|
2537
|
-
|
|
2538
|
-
|
|
2539
|
-
|
|
2540
|
-
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2544
|
-
|
|
2545
|
-
|
|
2546
|
-
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
|
|
2566
|
-
* @example
|
|
2567
|
-
* // Check if tree is height-balanced
|
|
2568
|
-
* const bst = new BST<number>([3, 1, 5, 2, 4]);
|
|
2569
|
-
* console.log(bst.isAVLBalanced()); // true;
|
|
1717
|
+
* @example
|
|
1718
|
+
* // Check if tree is height-balanced
|
|
1719
|
+
* const bst = new BST<number>([3, 1, 5, 2, 4]);
|
|
1720
|
+
* console.log(bst.isAVLBalanced()); // true;
|
|
2570
1721
|
*/
|
|
2571
1722
|
isAVLBalanced(iterationType: IterationType = this.iterationType): boolean {
|
|
2572
1723
|
if (!this._root) return true;
|
|
@@ -2624,92 +1775,15 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
2624
1775
|
* @param [options] - Options for the new BST.
|
|
2625
1776
|
* @param [thisArg] - `this` context for the callback.
|
|
2626
1777
|
* @returns A new, mapped BST.
|
|
2627
|
-
|
|
2628
|
-
|
|
2629
|
-
|
|
2630
|
-
|
|
2631
|
-
|
|
2632
|
-
|
|
2633
|
-
|
|
2634
|
-
|
|
2635
|
-
|
|
2636
|
-
|
|
2637
|
-
|
|
2638
|
-
|
|
2639
|
-
|
|
2640
|
-
|
|
2641
|
-
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
|
|
2645
|
-
|
|
2646
|
-
|
|
2647
|
-
|
|
2648
|
-
|
|
2649
|
-
|
|
2650
|
-
|
|
2651
|
-
|
|
2652
|
-
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
|
|
2656
|
-
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
|
|
2698
|
-
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
* @example
|
|
2709
|
-
* // Transform to new tree
|
|
2710
|
-
* const bst = new BST<number, number>([[1, 10], [2, 20], [3, 30]]);
|
|
2711
|
-
* const doubled = bst.map((value, key) => [key, (value ?? 0) * 2] as [number, number]);
|
|
2712
|
-
* console.log([...doubled.values()]); // [20, 40, 60];
|
|
1778
|
+
* @example
|
|
1779
|
+
* // Transform to new tree
|
|
1780
|
+
* const bst = new BST<number, number>([
|
|
1781
|
+
* [1, 10],
|
|
1782
|
+
* [2, 20],
|
|
1783
|
+
* [3, 30]
|
|
1784
|
+
* ]);
|
|
1785
|
+
* const doubled = bst.map((value, key) => [key, (value ?? 0) * 2] as [number, number]);
|
|
1786
|
+
* console.log([...doubled.values()]); // [20, 40, 60];
|
|
2713
1787
|
*/
|
|
2714
1788
|
override map<MK = K, MV = V, MR = any>(
|
|
2715
1789
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
@@ -3308,9 +2382,8 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
3308
2382
|
*/
|
|
3309
2383
|
protected _updateCount(node: BSTNode<K, V>): void {
|
|
3310
2384
|
if (!this._enableOrderStatistic) return;
|
|
3311
|
-
node._count =
|
|
3312
|
-
+ (this.isRealNode(node.left) ? node.left._count : 0)
|
|
3313
|
-
+ (this.isRealNode(node.right) ? node.right._count : 0);
|
|
2385
|
+
node._count =
|
|
2386
|
+
1 + (this.isRealNode(node.left) ? node.left._count : 0) + (this.isRealNode(node.right) ? node.right._count : 0);
|
|
3314
2387
|
}
|
|
3315
2388
|
|
|
3316
2389
|
/**
|
|
@@ -3395,8 +2468,11 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
3395
2468
|
if (cmp > 0) {
|
|
3396
2469
|
return this._getRankRecursive(node.left as BSTNode<K, V> | undefined, key);
|
|
3397
2470
|
} else if (cmp < 0) {
|
|
3398
|
-
return (
|
|
3399
|
-
|
|
2471
|
+
return (
|
|
2472
|
+
(this.isRealNode(node.left) ? node.left._count : 0) +
|
|
2473
|
+
1 +
|
|
2474
|
+
this._getRankRecursive(node.right as BSTNode<K, V> | undefined, key)
|
|
2475
|
+
);
|
|
3400
2476
|
} else {
|
|
3401
2477
|
return this.isRealNode(node.left) ? node.left._count : 0;
|
|
3402
2478
|
}
|