data-structure-typed 2.5.3 → 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/.husky/pre-commit +3 -0
- package/CHANGELOG.md +1 -1
- package/MIGRATION.md +48 -0
- package/README.md +20 -2
- package/README_CN.md +20 -2
- package/SPECIFICATION.md +24 -0
- package/SPECIFICATION.zh-CN.md +24 -0
- package/dist/cjs/binary-tree.cjs +1897 -19
- package/dist/cjs/graph.cjs +174 -0
- package/dist/cjs/hash.cjs +33 -0
- package/dist/cjs/heap.cjs +71 -0
- package/dist/cjs/index.cjs +2383 -3
- package/dist/cjs/linked-list.cjs +224 -2
- package/dist/cjs/matrix.cjs +24 -0
- package/dist/cjs/priority-queue.cjs +71 -0
- package/dist/cjs/queue.cjs +221 -1
- package/dist/cjs/stack.cjs +59 -0
- package/dist/cjs/trie.cjs +62 -0
- package/dist/cjs-legacy/binary-tree.cjs +1897 -19
- package/dist/cjs-legacy/graph.cjs +174 -0
- package/dist/cjs-legacy/hash.cjs +33 -0
- package/dist/cjs-legacy/heap.cjs +71 -0
- package/dist/cjs-legacy/index.cjs +2383 -3
- package/dist/cjs-legacy/linked-list.cjs +224 -2
- package/dist/cjs-legacy/matrix.cjs +24 -0
- package/dist/cjs-legacy/priority-queue.cjs +71 -0
- package/dist/cjs-legacy/queue.cjs +221 -1
- package/dist/cjs-legacy/stack.cjs +59 -0
- package/dist/cjs-legacy/trie.cjs +62 -0
- package/dist/esm/binary-tree.mjs +1897 -19
- package/dist/esm/graph.mjs +174 -0
- package/dist/esm/hash.mjs +33 -0
- package/dist/esm/heap.mjs +71 -0
- package/dist/esm/index.mjs +2383 -3
- package/dist/esm/linked-list.mjs +224 -2
- package/dist/esm/matrix.mjs +24 -0
- package/dist/esm/priority-queue.mjs +71 -0
- package/dist/esm/queue.mjs +221 -1
- package/dist/esm/stack.mjs +59 -0
- package/dist/esm/trie.mjs +62 -0
- package/dist/esm-legacy/binary-tree.mjs +1897 -19
- package/dist/esm-legacy/graph.mjs +174 -0
- package/dist/esm-legacy/hash.mjs +33 -0
- package/dist/esm-legacy/heap.mjs +71 -0
- package/dist/esm-legacy/index.mjs +2383 -3
- package/dist/esm-legacy/linked-list.mjs +224 -2
- package/dist/esm-legacy/matrix.mjs +24 -0
- package/dist/esm-legacy/priority-queue.mjs +71 -0
- package/dist/esm-legacy/queue.mjs +221 -1
- package/dist/esm-legacy/stack.mjs +59 -0
- package/dist/esm-legacy/trie.mjs +62 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +17 -0
- package/dist/types/data-structures/base/linear-base.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +36 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +42 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +75 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +72 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +57 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +18 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +375 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +389 -0
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +330 -0
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +438 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +30 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +27 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +33 -0
- package/dist/types/data-structures/heap/heap.d.ts +42 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +75 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +45 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +54 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +24 -0
- package/dist/types/data-structures/queue/deque.d.ts +90 -1
- package/dist/types/data-structures/queue/queue.d.ts +36 -0
- package/dist/types/data-structures/stack/stack.d.ts +30 -0
- package/dist/types/data-structures/trie/trie.d.ts +36 -0
- package/dist/umd/data-structure-typed.js +2383 -3
- package/dist/umd/data-structure-typed.min.js +3 -3
- 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 +30 -26
- 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/jest.integration.config.js +1 -2
- package/package.json +51 -50
- package/src/common/error.ts +15 -32
- package/src/common/index.ts +0 -3
- package/src/data-structures/base/iterable-element-base.ts +32 -3
- package/src/data-structures/base/linear-base.ts +13 -36
- package/src/data-structures/binary-tree/avl-tree.ts +31 -493
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +47 -530
- package/src/data-structures/binary-tree/binary-tree.ts +326 -1236
- package/src/data-structures/binary-tree/bst.ts +158 -1010
- package/src/data-structures/binary-tree/red-black-tree.ts +451 -1233
- package/src/data-structures/binary-tree/segment-tree.ts +73 -333
- package/src/data-structures/binary-tree/tree-map.ts +462 -4749
- package/src/data-structures/binary-tree/tree-multi-map.ts +310 -4530
- package/src/data-structures/binary-tree/tree-multi-set.ts +300 -3652
- package/src/data-structures/binary-tree/tree-set.ts +437 -4443
- package/src/data-structures/graph/abstract-graph.ts +98 -167
- package/src/data-structures/graph/directed-graph.ts +137 -532
- package/src/data-structures/graph/map-graph.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +132 -484
- package/src/data-structures/hash/hash-map.ts +154 -549
- package/src/data-structures/heap/heap.ts +200 -753
- package/src/data-structures/linked-list/doubly-linked-list.ts +153 -809
- package/src/data-structures/linked-list/singly-linked-list.ts +122 -749
- package/src/data-structures/linked-list/skip-linked-list.ts +211 -864
- package/src/data-structures/matrix/matrix.ts +179 -494
- 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 +241 -807
- package/src/data-structures/queue/queue.ts +102 -589
- package/src/data-structures/stack/stack.ts +76 -475
- package/src/data-structures/trie/trie.ts +98 -592
- 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,91 +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
|
-
* @example
|
|
520
|
-
* // Depth-first traversal
|
|
521
|
-
* const bst = new BST<number>([5, 3, 7, 1, 4]);
|
|
522
|
-
* const inOrder = bst.dfs(node => node.key, 'IN');
|
|
523
|
-
* 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];
|
|
524
452
|
*/
|
|
525
453
|
override dfs(): (K | undefined)[];
|
|
526
454
|
|
|
@@ -554,83 +482,13 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
554
482
|
return super.dfs(callback, pattern, onlyOne, startNode, iterationType);
|
|
555
483
|
}
|
|
556
484
|
|
|
557
|
-
|
|
485
|
+
/**
|
|
558
486
|
* BinaryTree level-order traversal
|
|
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
|
-
* @example
|
|
630
|
-
* // Breadth-first traversal
|
|
631
|
-
* const bst = new BST<number>([5, 3, 7]);
|
|
632
|
-
* const result = bst.bfs(node => node.key);
|
|
633
|
-
* 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;
|
|
634
492
|
*/
|
|
635
493
|
override bfs(): (K | undefined)[];
|
|
636
494
|
override bfs<C extends NodeCallback<BSTNode<K, V>>>(
|
|
@@ -657,89 +515,16 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
657
515
|
return super.bfs(callback, startNode, iterationType, false);
|
|
658
516
|
}
|
|
659
517
|
|
|
660
|
-
|
|
518
|
+
/**
|
|
661
519
|
* Level-order grouping
|
|
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
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
* @example
|
|
736
|
-
* // Level-order grouping
|
|
737
|
-
* const bst = new BST<number>([5, 3, 7, 1, 4]);
|
|
738
|
-
* const levels = bst.listLevels(node => node.key);
|
|
739
|
-
* console.log(levels); // toBeInstanceOf;
|
|
740
|
-
* console.log(levels[0].length); // 1; // root level has 1 node
|
|
741
|
-
* const allKeys = levels.flat().sort((a, b) => a - b);
|
|
742
|
-
* 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];
|
|
743
528
|
*/
|
|
744
529
|
override listLevels(): (K | undefined)[][];
|
|
745
530
|
|
|
@@ -775,87 +560,16 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
775
560
|
* @param [startNode=this._root] - The node to start the search from.
|
|
776
561
|
* @param [iterationType=this.iterationType] - The traversal method.
|
|
777
562
|
* @returns The first matching node, or undefined if not found.
|
|
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
|
-
|
|
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
|
-
* @example
|
|
854
|
-
* // Get node object by key
|
|
855
|
-
* const bst = new BST<number, string>([[5, 'root'], [3, 'left'], [7, 'right']]);
|
|
856
|
-
* const node = bst.getNode(3);
|
|
857
|
-
* console.log(node?.key); // 3;
|
|
858
|
-
* 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';
|
|
859
573
|
*/
|
|
860
574
|
override getNode(
|
|
861
575
|
keyNodeEntryOrPredicate:
|
|
@@ -916,82 +630,18 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
916
630
|
return undefined;
|
|
917
631
|
}
|
|
918
632
|
|
|
919
|
-
|
|
920
|
-
/**
|
|
633
|
+
/**
|
|
921
634
|
* Search nodes by predicate
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
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
|
-
* @example
|
|
991
|
-
* // Search nodes by predicate
|
|
992
|
-
* const bst = new BST<number, string>([[1, 'a'], [2, 'b'], [3, 'c'], [4, 'd']]);
|
|
993
|
-
* const found = bst.search(node => node.key > 2, true);
|
|
994
|
-
* 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;
|
|
995
645
|
*/
|
|
996
646
|
override search(
|
|
997
647
|
keyNodeEntryOrPredicate:
|
|
@@ -1088,7 +738,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1088
738
|
predicate = node => {
|
|
1089
739
|
/* istanbul ignore next -- node is always defined in iteration callbacks */
|
|
1090
740
|
if (!node) return false;
|
|
1091
|
-
return
|
|
741
|
+
return keyNodeEntryOrPredicate.isInRange(node.key, this._comparator);
|
|
1092
742
|
};
|
|
1093
743
|
} else {
|
|
1094
744
|
predicate = this._ensurePredicate(keyNodeEntryOrPredicate);
|
|
@@ -1096,7 +746,8 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1096
746
|
|
|
1097
747
|
// Optimization: Pruning logic
|
|
1098
748
|
const shouldVisitLeft = (cur: BSTNode<K, V> | null | undefined) => {
|
|
1099
|
-
/* 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;
|
|
1100
751
|
if (!this.isRealNode(cur.left)) return false;
|
|
1101
752
|
if (isRange) {
|
|
1102
753
|
// Range search: Only go left if the current key is >= the lower bound
|
|
@@ -1114,7 +765,8 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1114
765
|
};
|
|
1115
766
|
|
|
1116
767
|
const shouldVisitRight = (cur: BSTNode<K, V> | null | undefined) => {
|
|
1117
|
-
/* istanbul ignore next -- defensive */
|
|
768
|
+
/* istanbul ignore next -- defensive */
|
|
769
|
+
if (!cur) return false;
|
|
1118
770
|
if (!this.isRealNode(cur.right)) return false;
|
|
1119
771
|
if (isRange) {
|
|
1120
772
|
// Range search: Only go right if current key <= upper bound
|
|
@@ -1145,51 +797,12 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1145
797
|
);
|
|
1146
798
|
}
|
|
1147
799
|
|
|
1148
|
-
|
|
800
|
+
/**
|
|
1149
801
|
* Find all keys in a range
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
* @example
|
|
1190
|
-
* // Find all keys in a range
|
|
1191
|
-
* const bst = new BST<number>([10, 20, 30, 40, 50]);
|
|
1192
|
-
* 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];
|
|
1193
806
|
*/
|
|
1194
807
|
rangeSearch(range: Range<K> | [K, K]): (K | undefined)[];
|
|
1195
808
|
|
|
@@ -1230,12 +843,12 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1230
843
|
*
|
|
1231
844
|
* @param k - The 0-based position in tree order (0 = first element).
|
|
1232
845
|
* @returns The key at position k, or `undefined` if out of bounds.
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
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;
|
|
1239
852
|
*/
|
|
1240
853
|
getByRank(k: number): K | undefined;
|
|
1241
854
|
|
|
@@ -1276,9 +889,10 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1276
889
|
}
|
|
1277
890
|
}
|
|
1278
891
|
|
|
1279
|
-
const node =
|
|
1280
|
-
|
|
1281
|
-
|
|
892
|
+
const node =
|
|
893
|
+
actualIterationType === 'RECURSIVE'
|
|
894
|
+
? this._getByRankRecursive(this._root, k)
|
|
895
|
+
: this._getByRankIterative(this._root, k);
|
|
1282
896
|
|
|
1283
897
|
if (!node) return undefined;
|
|
1284
898
|
return actualCallback ? actualCallback(node) : node.key;
|
|
@@ -1423,15 +1037,16 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1423
1037
|
const count = hi - lo + 1;
|
|
1424
1038
|
|
|
1425
1039
|
// Find the lo-th node, then in-order traverse count nodes
|
|
1426
|
-
const startNode =
|
|
1427
|
-
|
|
1428
|
-
|
|
1040
|
+
const startNode =
|
|
1041
|
+
actualIterationType === 'RECURSIVE'
|
|
1042
|
+
? this._getByRankRecursive(this._root, lo)
|
|
1043
|
+
: this._getByRankIterative(this._root, lo);
|
|
1429
1044
|
|
|
1430
1045
|
if (!startNode) return [];
|
|
1431
1046
|
|
|
1432
1047
|
// In-order traversal from startNode collecting count elements
|
|
1433
1048
|
let collected = 0;
|
|
1434
|
-
const cb = actualCallback ?? this._DEFAULT_NODE_CALLBACK as C;
|
|
1049
|
+
const cb = actualCallback ?? (this._DEFAULT_NODE_CALLBACK as C);
|
|
1435
1050
|
|
|
1436
1051
|
// Use higher() to iterate — it's already O(log n) amortized per step
|
|
1437
1052
|
let current: BSTNode<K, V> | undefined = startNode;
|
|
@@ -1454,121 +1069,12 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1454
1069
|
* @param keyNodeOrEntry - The key, node, or entry to set.
|
|
1455
1070
|
* @param [value] - The value, if providing just a key.
|
|
1456
1071
|
* @returns True if the addition was successful, false otherwise.
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
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
|
-
* @example
|
|
1567
|
-
* // Set a key-value pair
|
|
1568
|
-
* const bst = new BST<number, string>();
|
|
1569
|
-
* bst.set(1, 'one');
|
|
1570
|
-
* bst.set(2, 'two');
|
|
1571
|
-
* 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';
|
|
1572
1078
|
*/
|
|
1573
1079
|
override set(
|
|
1574
1080
|
keyNodeOrEntry: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
@@ -1629,79 +1135,16 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1629
1135
|
* @param [isBalanceAdd=true] - If true, builds a balanced tree from the items.
|
|
1630
1136
|
* @param [iterationType=this.iterationType] - The traversal method for balanced set (recursive or iterative).
|
|
1631
1137
|
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
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
|
-
* @example
|
|
1700
|
-
* // Set multiple key-value pairs
|
|
1701
|
-
* const bst = new BST<number, string>();
|
|
1702
|
-
* bst.setMany([[1, 'a'], [2, 'b'], [3, 'c']]);
|
|
1703
|
-
* console.log(bst.size); // 3;
|
|
1704
|
-
* 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';
|
|
1705
1148
|
*/
|
|
1706
1149
|
override setMany(
|
|
1707
1150
|
keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, BSTNode<K, V>>>,
|
|
@@ -1800,51 +1243,12 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1800
1243
|
* Equivalent to Java TreeMap.ceiling.
|
|
1801
1244
|
* Time Complexity: O(log n) average, O(h) worst case.
|
|
1802
1245
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
* @example
|
|
1843
|
-
* // Find the least key ≥ target
|
|
1844
|
-
* const bst = new BST<number>([10, 20, 30, 40, 50]);
|
|
1845
|
-
* console.log(bst.ceiling(25)); // 30;
|
|
1846
|
-
* console.log(bst.ceiling(30)); // 30;
|
|
1847
|
-
* 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;
|
|
1848
1252
|
*/
|
|
1849
1253
|
ceiling(
|
|
1850
1254
|
keyNodeEntryOrPredicate:
|
|
@@ -1910,50 +1314,11 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1910
1314
|
* Equivalent to Java TreeMap.higher.
|
|
1911
1315
|
* Time Complexity: O(log n) average, O(h) worst case.
|
|
1912
1316
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
* @example
|
|
1953
|
-
* // Find the least key strictly > target
|
|
1954
|
-
* const bst = new BST<number>([10, 20, 30, 40]);
|
|
1955
|
-
* console.log(bst.higher(20)); // 30;
|
|
1956
|
-
* 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;
|
|
1957
1322
|
*/
|
|
1958
1323
|
higher(
|
|
1959
1324
|
keyNodeEntryOrPredicate:
|
|
@@ -2019,51 +1384,12 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
2019
1384
|
* Equivalent to Java TreeMap.floor.
|
|
2020
1385
|
* Time Complexity: O(log n) average, O(h) worst case.
|
|
2021
1386
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
* @example
|
|
2062
|
-
* // Find the greatest key ≤ target
|
|
2063
|
-
* const bst = new BST<number>([10, 20, 30, 40, 50]);
|
|
2064
|
-
* console.log(bst.floor(25)); // 20;
|
|
2065
|
-
* console.log(bst.floor(10)); // 10;
|
|
2066
|
-
* 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;
|
|
2067
1393
|
*/
|
|
2068
1394
|
floor(
|
|
2069
1395
|
keyNodeEntryOrPredicate:
|
|
@@ -2104,7 +1430,8 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
2104
1430
|
iterationType?: IterationType
|
|
2105
1431
|
): K | undefined | ReturnType<C> {
|
|
2106
1432
|
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === undefined) {
|
|
2107
|
-
/* istanbul ignore next */
|
|
1433
|
+
/* istanbul ignore next */
|
|
1434
|
+
if (typeof callback === 'string' || !callback) {
|
|
2108
1435
|
return undefined;
|
|
2109
1436
|
}
|
|
2110
1437
|
return undefined;
|
|
@@ -2138,7 +1465,8 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
2138
1465
|
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
2139
1466
|
const key = keyNodeEntryOrPredicate[0];
|
|
2140
1467
|
if (key === null || key === undefined) {
|
|
2141
|
-
/* istanbul ignore next */
|
|
1468
|
+
/* istanbul ignore next */
|
|
1469
|
+
if (typeof callback === 'string' || !callback) {
|
|
2142
1470
|
return undefined;
|
|
2143
1471
|
}
|
|
2144
1472
|
return undefined;
|
|
@@ -2172,50 +1500,11 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
2172
1500
|
* Equivalent to Java TreeMap.lower.
|
|
2173
1501
|
* Time Complexity: O(log n) average, O(h) worst case.
|
|
2174
1502
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
* @example
|
|
2215
|
-
* // Find the greatest key strictly < target
|
|
2216
|
-
* const bst = new BST<number>([10, 20, 30, 40]);
|
|
2217
|
-
* console.log(bst.lower(30)); // 20;
|
|
2218
|
-
* 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;
|
|
2219
1508
|
*/
|
|
2220
1509
|
lower(
|
|
2221
1510
|
keyNodeEntryOrPredicate:
|
|
@@ -2256,7 +1545,8 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
2256
1545
|
iterationType?: IterationType
|
|
2257
1546
|
): K | undefined | ReturnType<C> {
|
|
2258
1547
|
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === undefined) {
|
|
2259
|
-
/* istanbul ignore next */
|
|
1548
|
+
/* istanbul ignore next */
|
|
1549
|
+
if (typeof callback === 'string' || !callback) {
|
|
2260
1550
|
return undefined;
|
|
2261
1551
|
}
|
|
2262
1552
|
/* istanbul ignore next */
|
|
@@ -2291,10 +1581,11 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
2291
1581
|
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
2292
1582
|
const key = keyNodeEntryOrPredicate[0];
|
|
2293
1583
|
if (key === null || key === undefined) {
|
|
2294
|
-
/* istanbul ignore next */
|
|
1584
|
+
/* istanbul ignore next */
|
|
1585
|
+
if (typeof callback === 'string' || !callback) {
|
|
2295
1586
|
return undefined;
|
|
2296
1587
|
}
|
|
2297
|
-
|
|
1588
|
+
/* istanbul ignore next */
|
|
2298
1589
|
return undefined;
|
|
2299
1590
|
}
|
|
2300
1591
|
targetKey = key;
|
|
@@ -2383,51 +1674,14 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
2383
1674
|
*
|
|
2384
1675
|
* @param [iterationType=this.iterationType] - The traversal method for the initial node export.
|
|
2385
1676
|
* @returns True if successful, false if the tree was empty.
|
|
2386
|
-
|
|
2387
|
-
|
|
2388
|
-
|
|
2389
|
-
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
|
|
2399
|
-
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
* @example
|
|
2424
|
-
* // Rebalance the tree
|
|
2425
|
-
* const bst = new BST<number>();
|
|
2426
|
-
* // Insert in sorted order (worst case for BST)
|
|
2427
|
-
* for (let i = 1; i <= 7; i++) bst.add(i);
|
|
2428
|
-
* console.log(bst.isAVLBalanced()); // false;
|
|
2429
|
-
* bst.perfectlyBalance();
|
|
2430
|
-
* 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;
|
|
2431
1685
|
*/
|
|
2432
1686
|
perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
|
|
2433
1687
|
const nodes = this.dfs(node => node, 'IN', false, this._root, iterationType);
|
|
@@ -2460,47 +1714,10 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
2460
1714
|
*
|
|
2461
1715
|
* @param [iterationType=this.iterationType] - The traversal method.
|
|
2462
1716
|
* @returns True if the tree is AVL balanced, false otherwise.
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
* @example
|
|
2501
|
-
* // Check if tree is height-balanced
|
|
2502
|
-
* const bst = new BST<number>([3, 1, 5, 2, 4]);
|
|
2503
|
-
* 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;
|
|
2504
1721
|
*/
|
|
2505
1722
|
isAVLBalanced(iterationType: IterationType = this.iterationType): boolean {
|
|
2506
1723
|
if (!this._root) return true;
|
|
@@ -2558,86 +1775,15 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
2558
1775
|
* @param [options] - Options for the new BST.
|
|
2559
1776
|
* @param [thisArg] - `this` context for the callback.
|
|
2560
1777
|
* @returns A new, mapped BST.
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
|
|
2573
|
-
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
|
|
2579
|
-
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
|
|
2598
|
-
|
|
2599
|
-
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
|
|
2610
|
-
|
|
2611
|
-
|
|
2612
|
-
|
|
2613
|
-
|
|
2614
|
-
|
|
2615
|
-
|
|
2616
|
-
|
|
2617
|
-
|
|
2618
|
-
|
|
2619
|
-
|
|
2620
|
-
|
|
2621
|
-
|
|
2622
|
-
|
|
2623
|
-
|
|
2624
|
-
|
|
2625
|
-
|
|
2626
|
-
|
|
2627
|
-
|
|
2628
|
-
|
|
2629
|
-
|
|
2630
|
-
|
|
2631
|
-
|
|
2632
|
-
|
|
2633
|
-
|
|
2634
|
-
|
|
2635
|
-
|
|
2636
|
-
* @example
|
|
2637
|
-
* // Transform to new tree
|
|
2638
|
-
* const bst = new BST<number, number>([[1, 10], [2, 20], [3, 30]]);
|
|
2639
|
-
* const doubled = bst.map((value, key) => [key, (value ?? 0) * 2] as [number, number]);
|
|
2640
|
-
* 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];
|
|
2641
1787
|
*/
|
|
2642
1788
|
override map<MK = K, MV = V, MR = any>(
|
|
2643
1789
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
@@ -3236,9 +2382,8 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
3236
2382
|
*/
|
|
3237
2383
|
protected _updateCount(node: BSTNode<K, V>): void {
|
|
3238
2384
|
if (!this._enableOrderStatistic) return;
|
|
3239
|
-
node._count =
|
|
3240
|
-
+ (this.isRealNode(node.left) ? node.left._count : 0)
|
|
3241
|
-
+ (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);
|
|
3242
2387
|
}
|
|
3243
2388
|
|
|
3244
2389
|
/**
|
|
@@ -3323,8 +2468,11 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
3323
2468
|
if (cmp > 0) {
|
|
3324
2469
|
return this._getRankRecursive(node.left as BSTNode<K, V> | undefined, key);
|
|
3325
2470
|
} else if (cmp < 0) {
|
|
3326
|
-
return (
|
|
3327
|
-
|
|
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
|
+
);
|
|
3328
2476
|
} else {
|
|
3329
2477
|
return this.isRealNode(node.left) ? node.left._count : 0;
|
|
3330
2478
|
}
|