min-heap-typed 1.50.8 → 1.51.0
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/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +15 -3
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +14 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/dist/data-structures/binary-tree/binary-tree.js +11 -11
- package/dist/data-structures/binary-tree/bst.d.ts +19 -2
- package/dist/data-structures/binary-tree/bst.js +20 -2
- package/dist/data-structures/binary-tree/rb-tree.d.ts +5 -5
- package/dist/data-structures/binary-tree/rb-tree.js +42 -44
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +40 -22
- package/dist/data-structures/binary-tree/tree-multi-map.js +43 -27
- package/dist/data-structures/heap/heap.d.ts +1 -1
- package/dist/data-structures/heap/heap.js +5 -5
- package/dist/types/common.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -4
- package/dist/types/data-structures/binary-tree/rb-tree.js +0 -6
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +17 -3
- package/src/data-structures/binary-tree/binary-tree.ts +36 -24
- package/src/data-structures/binary-tree/bst.ts +32 -11
- package/src/data-structures/binary-tree/rb-tree.ts +44 -44
- package/src/data-structures/binary-tree/tree-multi-map.ts +46 -27
- package/src/data-structures/heap/heap.ts +5 -5
- package/src/types/common.ts +1 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
|
@@ -210,7 +210,10 @@ export class BST<
|
|
|
210
210
|
* type of iteration to be performed. It has a default value of `'ITERATIVE'`.
|
|
211
211
|
* @returns either a node object (NODE) or undefined.
|
|
212
212
|
*/
|
|
213
|
-
override ensureNode(
|
|
213
|
+
override ensureNode(
|
|
214
|
+
keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
|
|
215
|
+
iterationType: IterationType = 'ITERATIVE'
|
|
216
|
+
): NODE | undefined {
|
|
214
217
|
let res: NODE | undefined;
|
|
215
218
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
216
219
|
res = keyOrNodeOrEntry;
|
|
@@ -321,7 +324,7 @@ export class BST<
|
|
|
321
324
|
keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>>,
|
|
322
325
|
values?: Iterable<V | undefined>,
|
|
323
326
|
isBalanceAdd = true,
|
|
324
|
-
iterationType = this.iterationType
|
|
327
|
+
iterationType: IterationType = this.iterationType
|
|
325
328
|
): boolean[] {
|
|
326
329
|
const inserted: boolean[] = [];
|
|
327
330
|
|
|
@@ -422,7 +425,8 @@ export class BST<
|
|
|
422
425
|
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
423
426
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
424
427
|
*/
|
|
425
|
-
override getNodeByKey(key: K, iterationType = 'ITERATIVE'): NODE | undefined {
|
|
428
|
+
override getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | undefined {
|
|
429
|
+
// return this.getNodes(key, this._defaultOneParamCallback, true, this.root, iterationType)[0];
|
|
426
430
|
if (!this.isRealNode(this.root)) return undefined;
|
|
427
431
|
if (iterationType === 'RECURSIVE') {
|
|
428
432
|
const _dfs = (cur: NODE): NODE | undefined => {
|
|
@@ -480,7 +484,7 @@ export class BST<
|
|
|
480
484
|
callback: C = this._defaultOneParamCallback as C,
|
|
481
485
|
onlyOne = false,
|
|
482
486
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
483
|
-
iterationType = this.iterationType
|
|
487
|
+
iterationType: IterationType = this.iterationType
|
|
484
488
|
): NODE[] {
|
|
485
489
|
beginRoot = this.ensureNode(beginRoot);
|
|
486
490
|
if (!beginRoot) return [];
|
|
@@ -565,7 +569,7 @@ export class BST<
|
|
|
565
569
|
*/
|
|
566
570
|
override dfs<C extends BTNCallback<NODE>>(
|
|
567
571
|
callback: C = this._defaultOneParamCallback as C,
|
|
568
|
-
pattern: DFSOrderPattern = '
|
|
572
|
+
pattern: DFSOrderPattern = 'IN',
|
|
569
573
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
570
574
|
iterationType: IterationType = 'ITERATIVE'
|
|
571
575
|
): ReturnType<C>[] {
|
|
@@ -597,7 +601,7 @@ export class BST<
|
|
|
597
601
|
override bfs<C extends BTNCallback<NODE>>(
|
|
598
602
|
callback: C = this._defaultOneParamCallback as C,
|
|
599
603
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
600
|
-
iterationType = this.iterationType
|
|
604
|
+
iterationType: IterationType = this.iterationType
|
|
601
605
|
): ReturnType<C>[] {
|
|
602
606
|
return super.bfs(callback, beginRoot, iterationType, false);
|
|
603
607
|
}
|
|
@@ -628,7 +632,7 @@ export class BST<
|
|
|
628
632
|
override listLevels<C extends BTNCallback<NODE>>(
|
|
629
633
|
callback: C = this._defaultOneParamCallback as C,
|
|
630
634
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
631
|
-
iterationType = this.iterationType
|
|
635
|
+
iterationType: IterationType = this.iterationType
|
|
632
636
|
): ReturnType<C>[][] {
|
|
633
637
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
634
638
|
}
|
|
@@ -699,7 +703,7 @@ export class BST<
|
|
|
699
703
|
callback: C = this._defaultOneParamCallback as C,
|
|
700
704
|
lesserOrGreater: CP = 'LT',
|
|
701
705
|
targetNode: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
702
|
-
iterationType = this.iterationType
|
|
706
|
+
iterationType: IterationType = this.iterationType
|
|
703
707
|
): ReturnType<C>[] {
|
|
704
708
|
targetNode = this.ensureNode(targetNode);
|
|
705
709
|
const ans: ReturnType<BTNCallback<NODE>>[] = [];
|
|
@@ -751,8 +755,8 @@ export class BST<
|
|
|
751
755
|
* values:
|
|
752
756
|
* @returns The function `perfectlyBalance` returns a boolean value.
|
|
753
757
|
*/
|
|
754
|
-
perfectlyBalance(iterationType = this.iterationType): boolean {
|
|
755
|
-
const sorted = this.dfs(node => node, '
|
|
758
|
+
perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
|
|
759
|
+
const sorted = this.dfs(node => node, 'IN'),
|
|
756
760
|
n = sorted.length;
|
|
757
761
|
this.clear();
|
|
758
762
|
|
|
@@ -812,7 +816,7 @@ export class BST<
|
|
|
812
816
|
* to check if the AVL tree is balanced. It can have two possible values:
|
|
813
817
|
* @returns a boolean value.
|
|
814
818
|
*/
|
|
815
|
-
isAVLBalanced(iterationType = this.iterationType): boolean {
|
|
819
|
+
isAVLBalanced(iterationType: IterationType = this.iterationType): boolean {
|
|
816
820
|
if (!this.root) return true;
|
|
817
821
|
|
|
818
822
|
let balanced = true;
|
|
@@ -884,6 +888,15 @@ export class BST<
|
|
|
884
888
|
return compared > 0 ? 'GT' : compared < 0 ? 'LT' : 'EQ';
|
|
885
889
|
}
|
|
886
890
|
|
|
891
|
+
/**
|
|
892
|
+
* The function `_lt` compares two values `a` and `b` using an extractor function and returns true if
|
|
893
|
+
* `a` is less than `b` based on the specified variant.
|
|
894
|
+
* @param {K} a - The parameter "a" is of type "K", which means it can be any type. It represents the
|
|
895
|
+
* first value to be compared in the function.
|
|
896
|
+
* @param {K} b - The parameter `b` is of type `K`, which means it can be any type. It is used as one
|
|
897
|
+
* of the arguments for the comparison in the `_lt` function.
|
|
898
|
+
* @returns a boolean value.
|
|
899
|
+
*/
|
|
887
900
|
protected _lt(a: K, b: K): boolean {
|
|
888
901
|
const extractedA = this.extractor(a);
|
|
889
902
|
const extractedB = this.extractor(b);
|
|
@@ -893,6 +906,14 @@ export class BST<
|
|
|
893
906
|
// return a < b;
|
|
894
907
|
}
|
|
895
908
|
|
|
909
|
+
/**
|
|
910
|
+
* The function compares two values using a custom extractor function and returns true if the first
|
|
911
|
+
* value is greater than the second value.
|
|
912
|
+
* @param {K} a - The parameter "a" is of type K, which means it can be any type.
|
|
913
|
+
* @param {K} b - The parameter "b" is of type K, which means it can be any type. It is used as one
|
|
914
|
+
* of the arguments for the comparison in the function.
|
|
915
|
+
* @returns a boolean value.
|
|
916
|
+
*/
|
|
896
917
|
protected _gt(a: K, b: K): boolean {
|
|
897
918
|
const extractedA = this.extractor(a);
|
|
898
919
|
const extractedB = this.extractor(b);
|
|
@@ -2,6 +2,7 @@ import type {
|
|
|
2
2
|
BinaryTreeDeleteResult,
|
|
3
3
|
BSTNKeyOrNode,
|
|
4
4
|
BTNCallback,
|
|
5
|
+
IterationType,
|
|
5
6
|
KeyOrNodeOrEntry,
|
|
6
7
|
RBTreeOptions,
|
|
7
8
|
RedBlackTreeNested,
|
|
@@ -25,9 +26,9 @@ export class RedBlackTreeNode<
|
|
|
25
26
|
* associated with the key in the Red-Black Tree Node. It is not required and can be omitted when
|
|
26
27
|
* creating a new instance of the Red-Black Tree Node.
|
|
27
28
|
* @param {RBTNColor} color - The `color` parameter is used to specify the color of the Red-Black
|
|
28
|
-
* Tree Node. It is an optional parameter with a default value of `
|
|
29
|
+
* Tree Node. It is an optional parameter with a default value of `'BLACK'`.
|
|
29
30
|
*/
|
|
30
|
-
constructor(key: K, value?: V, color: RBTNColor =
|
|
31
|
+
constructor(key: K, value?: V, color: RBTNColor = 'BLACK') {
|
|
31
32
|
super(key, value);
|
|
32
33
|
this._color = color;
|
|
33
34
|
}
|
|
@@ -106,12 +107,12 @@ export class RedBlackTree<
|
|
|
106
107
|
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
107
108
|
* associated with the key in the node. It is not required and can be omitted if not needed.
|
|
108
109
|
* @param {RBTNColor} color - The "color" parameter is used to specify the color of the node in a
|
|
109
|
-
* Red-Black Tree. It is an optional parameter with a default value of "
|
|
110
|
-
* can be either "
|
|
110
|
+
* Red-Black Tree. It is an optional parameter with a default value of "'BLACK'". The color
|
|
111
|
+
* can be either "'RED'" or "'BLACK'".
|
|
111
112
|
* @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
|
|
112
113
|
* value, and color.
|
|
113
114
|
*/
|
|
114
|
-
override createNode(key: K, value?: V, color: RBTNColor =
|
|
115
|
+
override createNode(key: K, value?: V, color: RBTNColor = 'BLACK'): NODE {
|
|
115
116
|
return new RedBlackTreeNode<K, V, NODE>(key, value, color) as NODE;
|
|
116
117
|
}
|
|
117
118
|
|
|
@@ -156,10 +157,10 @@ export class RedBlackTree<
|
|
|
156
157
|
if (key === undefined || key === null) {
|
|
157
158
|
return;
|
|
158
159
|
} else {
|
|
159
|
-
node = this.createNode(key, value,
|
|
160
|
+
node = this.createNode(key, value, 'RED');
|
|
160
161
|
}
|
|
161
162
|
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
162
|
-
node = this.createNode(keyOrNodeOrEntry, value,
|
|
163
|
+
node = this.createNode(keyOrNodeOrEntry, value, 'RED');
|
|
163
164
|
} else {
|
|
164
165
|
return;
|
|
165
166
|
}
|
|
@@ -232,9 +233,8 @@ export class RedBlackTree<
|
|
|
232
233
|
identifier: ReturnType<C> | undefined,
|
|
233
234
|
callback: C = this._defaultOneParamCallback as C,
|
|
234
235
|
beginRoot: BSTNKeyOrNode<K, NODE> = this.root,
|
|
235
|
-
iterationType = this.iterationType
|
|
236
|
+
iterationType: IterationType = this.iterationType
|
|
236
237
|
): NODE | null | undefined {
|
|
237
|
-
// if ((identifier as any) instanceof RedBlackTreeNode) callback = (node => node) as C;
|
|
238
238
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
239
239
|
}
|
|
240
240
|
|
|
@@ -282,7 +282,7 @@ export class RedBlackTree<
|
|
|
282
282
|
if (insertStatus === 'CREATED') {
|
|
283
283
|
// Ensure the root is black
|
|
284
284
|
if (this.isRealNode(this._root)) {
|
|
285
|
-
this._root.color =
|
|
285
|
+
this._root.color = 'BLACK';
|
|
286
286
|
} else {
|
|
287
287
|
return false;
|
|
288
288
|
}
|
|
@@ -363,7 +363,7 @@ export class RedBlackTree<
|
|
|
363
363
|
this._size--;
|
|
364
364
|
|
|
365
365
|
// If the original color was black, fix the tree
|
|
366
|
-
if (originalColor ===
|
|
366
|
+
if (originalColor === 'BLACK') {
|
|
367
367
|
this._deleteFixup(replacementNode);
|
|
368
368
|
}
|
|
369
369
|
|
|
@@ -451,7 +451,7 @@ export class RedBlackTree<
|
|
|
451
451
|
|
|
452
452
|
node.left = this.SENTINEL;
|
|
453
453
|
node.right = this.SENTINEL;
|
|
454
|
-
node.color =
|
|
454
|
+
node.color = 'RED';
|
|
455
455
|
|
|
456
456
|
this._insertFixup(node);
|
|
457
457
|
return 'CREATED';
|
|
@@ -500,16 +500,16 @@ export class RedBlackTree<
|
|
|
500
500
|
*/
|
|
501
501
|
protected _insertFixup(z: NODE | undefined): void {
|
|
502
502
|
// Continue fixing the tree as long as the parent of z is red
|
|
503
|
-
while (z?.parent?.color ===
|
|
503
|
+
while (z?.parent?.color === 'RED') {
|
|
504
504
|
// Check if the parent of z is the left child of its parent
|
|
505
505
|
if (z.parent === z.parent.parent?.left) {
|
|
506
506
|
// Case 1: The uncle (y) of z is red
|
|
507
507
|
const y = z.parent.parent.right;
|
|
508
|
-
if (y?.color ===
|
|
508
|
+
if (y?.color === 'RED') {
|
|
509
509
|
// Set colors to restore properties of Red-Black Tree
|
|
510
|
-
z.parent.color =
|
|
511
|
-
y.color =
|
|
512
|
-
z.parent.parent.color =
|
|
510
|
+
z.parent.color = 'BLACK';
|
|
511
|
+
y.color = 'BLACK';
|
|
512
|
+
z.parent.parent.color = 'RED';
|
|
513
513
|
// Move up the tree to continue fixing
|
|
514
514
|
z = z.parent.parent;
|
|
515
515
|
} else {
|
|
@@ -523,8 +523,8 @@ export class RedBlackTree<
|
|
|
523
523
|
// Case 3: The uncle (y) of z is black, and z is a left child
|
|
524
524
|
// Adjust colors and perform a right rotation
|
|
525
525
|
if (z && this.isRealNode(z.parent) && this.isRealNode(z.parent.parent)) {
|
|
526
|
-
z.parent.color =
|
|
527
|
-
z.parent.parent.color =
|
|
526
|
+
z.parent.color = 'BLACK';
|
|
527
|
+
z.parent.parent.color = 'RED';
|
|
528
528
|
this._rightRotate(z.parent.parent);
|
|
529
529
|
}
|
|
530
530
|
}
|
|
@@ -532,10 +532,10 @@ export class RedBlackTree<
|
|
|
532
532
|
// Symmetric case for the right child (left and right exchanged)
|
|
533
533
|
// Follow the same logic as above with left and right exchanged
|
|
534
534
|
const y: NODE | undefined = z?.parent?.parent?.left;
|
|
535
|
-
if (y?.color ===
|
|
536
|
-
z.parent.color =
|
|
537
|
-
y.color =
|
|
538
|
-
z.parent.parent!.color =
|
|
535
|
+
if (y?.color === 'RED') {
|
|
536
|
+
z.parent.color = 'BLACK';
|
|
537
|
+
y.color = 'BLACK';
|
|
538
|
+
z.parent.parent!.color = 'RED';
|
|
539
539
|
z = z.parent.parent;
|
|
540
540
|
} else {
|
|
541
541
|
if (z === z.parent.left) {
|
|
@@ -544,8 +544,8 @@ export class RedBlackTree<
|
|
|
544
544
|
}
|
|
545
545
|
|
|
546
546
|
if (z && this.isRealNode(z.parent) && this.isRealNode(z.parent.parent)) {
|
|
547
|
-
z.parent.color =
|
|
548
|
-
z.parent.parent.color =
|
|
547
|
+
z.parent.color = 'BLACK';
|
|
548
|
+
z.parent.parent.color = 'RED';
|
|
549
549
|
this._leftRotate(z.parent.parent);
|
|
550
550
|
}
|
|
551
551
|
}
|
|
@@ -553,7 +553,7 @@ export class RedBlackTree<
|
|
|
553
553
|
}
|
|
554
554
|
|
|
555
555
|
// Ensure that the root is black after fixing
|
|
556
|
-
if (this.isRealNode(this._root)) this._root.color =
|
|
556
|
+
if (this.isRealNode(this._root)) this._root.color = 'BLACK';
|
|
557
557
|
}
|
|
558
558
|
|
|
559
559
|
/**
|
|
@@ -573,14 +573,14 @@ export class RedBlackTree<
|
|
|
573
573
|
*/
|
|
574
574
|
protected _deleteFixup(node: NODE | undefined): void {
|
|
575
575
|
// Early exit condition
|
|
576
|
-
if (!node || node === this.root || node.color ===
|
|
576
|
+
if (!node || node === this.root || node.color === 'BLACK') {
|
|
577
577
|
if (node) {
|
|
578
|
-
node.color =
|
|
578
|
+
node.color = 'BLACK'; // Ensure the final node is black
|
|
579
579
|
}
|
|
580
580
|
return;
|
|
581
581
|
}
|
|
582
582
|
|
|
583
|
-
while (node && node !== this.root && node.color ===
|
|
583
|
+
while (node && node !== this.root && node.color === 'BLACK') {
|
|
584
584
|
const parent: NODE | undefined = node.parent;
|
|
585
585
|
|
|
586
586
|
if (!parent) {
|
|
@@ -591,22 +591,22 @@ export class RedBlackTree<
|
|
|
591
591
|
let sibling = parent.right;
|
|
592
592
|
|
|
593
593
|
// Cases 1 and 2: Sibling is red or both children of sibling are black
|
|
594
|
-
if (sibling?.color ===
|
|
595
|
-
sibling.color =
|
|
596
|
-
parent.color =
|
|
594
|
+
if (sibling?.color === 'RED') {
|
|
595
|
+
sibling.color = 'BLACK';
|
|
596
|
+
parent.color = 'RED';
|
|
597
597
|
this._leftRotate(parent);
|
|
598
598
|
sibling = parent.right;
|
|
599
599
|
}
|
|
600
600
|
|
|
601
601
|
// Case 3: Sibling's left child is black
|
|
602
|
-
if ((sibling?.left?.color ??
|
|
603
|
-
if (sibling) sibling.color =
|
|
602
|
+
if ((sibling?.left?.color ?? 'BLACK') === 'BLACK') {
|
|
603
|
+
if (sibling) sibling.color = 'RED';
|
|
604
604
|
node = parent;
|
|
605
605
|
} else {
|
|
606
606
|
// Case 4: Adjust colors and perform a right rotation
|
|
607
|
-
if (sibling?.left) sibling.left.color =
|
|
607
|
+
if (sibling?.left) sibling.left.color = 'BLACK';
|
|
608
608
|
if (sibling) sibling.color = parent.color;
|
|
609
|
-
parent.color =
|
|
609
|
+
parent.color = 'BLACK';
|
|
610
610
|
this._rightRotate(parent);
|
|
611
611
|
node = this.root;
|
|
612
612
|
}
|
|
@@ -615,22 +615,22 @@ export class RedBlackTree<
|
|
|
615
615
|
let sibling = parent.left;
|
|
616
616
|
|
|
617
617
|
// Cases 1 and 2: Sibling is red or both children of sibling are black
|
|
618
|
-
if (sibling?.color ===
|
|
619
|
-
sibling.color =
|
|
620
|
-
if (parent) parent.color =
|
|
618
|
+
if (sibling?.color === 'RED') {
|
|
619
|
+
sibling.color = 'BLACK';
|
|
620
|
+
if (parent) parent.color = 'RED';
|
|
621
621
|
this._rightRotate(parent);
|
|
622
622
|
if (parent) sibling = parent.left;
|
|
623
623
|
}
|
|
624
624
|
|
|
625
625
|
// Case 3: Sibling's left child is black
|
|
626
|
-
if ((sibling?.right?.color ??
|
|
627
|
-
if (sibling) sibling.color =
|
|
626
|
+
if ((sibling?.right?.color ?? 'BLACK') === 'BLACK') {
|
|
627
|
+
if (sibling) sibling.color = 'RED';
|
|
628
628
|
node = parent;
|
|
629
629
|
} else {
|
|
630
630
|
// Case 4: Adjust colors and perform a left rotation
|
|
631
|
-
if (sibling?.right) sibling.right.color =
|
|
631
|
+
if (sibling?.right) sibling.right.color = 'BLACK';
|
|
632
632
|
if (sibling) sibling.color = parent.color;
|
|
633
|
-
if (parent) parent.color =
|
|
633
|
+
if (parent) parent.color = 'BLACK';
|
|
634
634
|
this._leftRotate(parent);
|
|
635
635
|
node = this.root;
|
|
636
636
|
}
|
|
@@ -639,7 +639,7 @@ export class RedBlackTree<
|
|
|
639
639
|
|
|
640
640
|
// Ensure that the final node (possibly the root) is black
|
|
641
641
|
if (node) {
|
|
642
|
-
node.color =
|
|
642
|
+
node.color = 'BLACK';
|
|
643
643
|
}
|
|
644
644
|
}
|
|
645
645
|
|
|
@@ -9,6 +9,7 @@ import type {
|
|
|
9
9
|
BinaryTreeDeleteResult,
|
|
10
10
|
BSTNKeyOrNode,
|
|
11
11
|
BTNCallback,
|
|
12
|
+
IterationType,
|
|
12
13
|
KeyOrNodeOrEntry,
|
|
13
14
|
TreeMultiMapNested,
|
|
14
15
|
TreeMultiMapNodeNested,
|
|
@@ -24,17 +25,19 @@ export class TreeMultiMapNode<
|
|
|
24
25
|
NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNodeNested<K, V>
|
|
25
26
|
> extends RedBlackTreeNode<K, V, NODE> {
|
|
26
27
|
/**
|
|
27
|
-
* The constructor function initializes
|
|
28
|
-
* @param {K} key - The key parameter
|
|
29
|
-
*
|
|
30
|
-
* @param {V} [value] - The `value` parameter is an optional parameter
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* 1.
|
|
28
|
+
* The constructor function initializes a Red-Black Tree node with a key, value, count, and color.
|
|
29
|
+
* @param {K} key - The key parameter represents the key of the node in the Red-Black Tree. It is
|
|
30
|
+
* used to identify and locate the node within the tree.
|
|
31
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
32
|
+
* associated with the key in the Red-Black Tree node. It is not required and can be omitted when
|
|
33
|
+
* creating a new node.
|
|
34
|
+
* @param [count=1] - The `count` parameter represents the number of occurrences of a particular key
|
|
35
|
+
* in the Red-Black Tree. It is an optional parameter with a default value of 1.
|
|
36
|
+
* @param {RBTNColor} [color=BLACK] - The `color` parameter is used to specify the color of the node
|
|
37
|
+
* in a Red-Black Tree. It is optional and has a default value of `'BLACK'`.
|
|
35
38
|
*/
|
|
36
|
-
constructor(key: K, value?: V, count = 1) {
|
|
37
|
-
super(key, value);
|
|
39
|
+
constructor(key: K, value?: V, count = 1, color: RBTNColor = 'BLACK') {
|
|
40
|
+
super(key, value, color);
|
|
38
41
|
this.count = count;
|
|
39
42
|
}
|
|
40
43
|
|
|
@@ -91,25 +94,41 @@ export class TreeMultiMap<
|
|
|
91
94
|
return this._count;
|
|
92
95
|
}
|
|
93
96
|
|
|
94
|
-
|
|
97
|
+
/**
|
|
98
|
+
* Time Complexity: O(n)
|
|
99
|
+
* Space Complexity: O(1)
|
|
100
|
+
*/
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Time Complexity: O(n)
|
|
104
|
+
* Space Complexity: O(1)
|
|
105
|
+
*
|
|
106
|
+
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
107
|
+
* search.
|
|
108
|
+
* @returns the sum of the count property of all nodes in the tree.
|
|
109
|
+
*/
|
|
110
|
+
getComputedCount(): number {
|
|
95
111
|
let sum = 0;
|
|
96
112
|
this.dfs(node => (sum += node.count));
|
|
97
113
|
return sum;
|
|
98
114
|
}
|
|
99
115
|
|
|
100
116
|
/**
|
|
101
|
-
* The function creates a new TreeMultiMapNode
|
|
117
|
+
* The function creates a new TreeMultiMapNode with the specified key, value, color, and count.
|
|
102
118
|
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
|
|
103
|
-
* which is a generic type
|
|
104
|
-
* @param {V} [value] - The `value` parameter
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
* default
|
|
109
|
-
* @
|
|
119
|
+
* which is a generic type representing the key type of the node.
|
|
120
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
121
|
+
* node. It is an optional parameter, which means it can be omitted when calling the `createNode`
|
|
122
|
+
* function. If provided, it should be of type `V`.
|
|
123
|
+
* @param {RBTNColor} [color=BLACK] - The color parameter is used to specify the color of the node in
|
|
124
|
+
* a Red-Black Tree. It can have two possible values: 'RED' or 'BLACK'. The default value is 'BLACK'.
|
|
125
|
+
* @param {number} [count] - The `count` parameter represents the number of occurrences of a key in
|
|
126
|
+
* the tree. It is an optional parameter and is used to keep track of the number of values associated
|
|
127
|
+
* with a key in the tree.
|
|
128
|
+
* @returns A new instance of the TreeMultiMapNode class is being returned.
|
|
110
129
|
*/
|
|
111
|
-
override createNode(key: K, value?: V, count?: number): NODE {
|
|
112
|
-
return new TreeMultiMapNode(key, value, count) as NODE;
|
|
130
|
+
override createNode(key: K, value?: V, color: RBTNColor = 'BLACK', count?: number): NODE {
|
|
131
|
+
return new TreeMultiMapNode(key, value, count, color) as NODE;
|
|
113
132
|
}
|
|
114
133
|
|
|
115
134
|
/**
|
|
@@ -153,10 +172,10 @@ export class TreeMultiMap<
|
|
|
153
172
|
if (key === undefined || key === null) {
|
|
154
173
|
return;
|
|
155
174
|
} else {
|
|
156
|
-
node = this.createNode(key, value, count);
|
|
175
|
+
node = this.createNode(key, value, 'BLACK', count);
|
|
157
176
|
}
|
|
158
177
|
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
159
|
-
node = this.createNode(keyOrNodeOrEntry, value, count);
|
|
178
|
+
node = this.createNode(keyOrNodeOrEntry, value, 'BLACK', count);
|
|
160
179
|
} else {
|
|
161
180
|
return;
|
|
162
181
|
}
|
|
@@ -314,7 +333,7 @@ export class TreeMultiMap<
|
|
|
314
333
|
this._size--;
|
|
315
334
|
|
|
316
335
|
// If the original color was black, fix the tree
|
|
317
|
-
if (originalColor ===
|
|
336
|
+
if (originalColor === 'BLACK') {
|
|
318
337
|
this._deleteFixup(replacementNode);
|
|
319
338
|
}
|
|
320
339
|
|
|
@@ -356,8 +375,8 @@ export class TreeMultiMap<
|
|
|
356
375
|
* values:
|
|
357
376
|
* @returns a boolean value.
|
|
358
377
|
*/
|
|
359
|
-
override perfectlyBalance(iterationType = this.iterationType): boolean {
|
|
360
|
-
const sorted = this.dfs(node => node, '
|
|
378
|
+
override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
|
|
379
|
+
const sorted = this.dfs(node => node, 'IN'),
|
|
361
380
|
n = sorted.length;
|
|
362
381
|
if (sorted.length < 1) return false;
|
|
363
382
|
|
|
@@ -431,7 +450,7 @@ export class TreeMultiMap<
|
|
|
431
450
|
destNode = this.ensureNode(destNode);
|
|
432
451
|
if (srcNode && destNode) {
|
|
433
452
|
const { key, value, count, color } = destNode;
|
|
434
|
-
const tempNode = this.createNode(key, value, count);
|
|
453
|
+
const tempNode = this.createNode(key, value, color, count);
|
|
435
454
|
if (tempNode) {
|
|
436
455
|
tempNode.color = color;
|
|
437
456
|
|
|
@@ -247,10 +247,10 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
247
247
|
* Space Complexity: O(log n)
|
|
248
248
|
*
|
|
249
249
|
* Depth-first search (DFS) method, different traversal orders can be selected。
|
|
250
|
-
* @param order - Traverse order parameter: '
|
|
250
|
+
* @param order - Traverse order parameter: 'IN' (in-order), 'PRE' (pre-order) or 'POST' (post-order).
|
|
251
251
|
* @returns An array containing elements traversed in the specified order.
|
|
252
252
|
*/
|
|
253
|
-
dfs(order: DFSOrderPattern = '
|
|
253
|
+
dfs(order: DFSOrderPattern = 'PRE'): E[] {
|
|
254
254
|
const result: E[] = [];
|
|
255
255
|
|
|
256
256
|
// Auxiliary recursive function, traverses the binary heap according to the traversal order
|
|
@@ -258,15 +258,15 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
258
258
|
const left = 2 * index + 1,
|
|
259
259
|
right = left + 1;
|
|
260
260
|
if (index < this.size) {
|
|
261
|
-
if (order === '
|
|
261
|
+
if (order === 'IN') {
|
|
262
262
|
_dfs(left);
|
|
263
263
|
result.push(this.elements[index]);
|
|
264
264
|
_dfs(right);
|
|
265
|
-
} else if (order === '
|
|
265
|
+
} else if (order === 'PRE') {
|
|
266
266
|
result.push(this.elements[index]);
|
|
267
267
|
_dfs(left);
|
|
268
268
|
_dfs(right);
|
|
269
|
-
} else if (order === '
|
|
269
|
+
} else if (order === 'POST') {
|
|
270
270
|
_dfs(left);
|
|
271
271
|
_dfs(right);
|
|
272
272
|
result.push(this.elements[index]);
|
package/src/types/common.ts
CHANGED
|
@@ -13,7 +13,7 @@ export type FamilyPosition = 'ROOT' | 'LEFT' | 'RIGHT' | 'ROOT_LEFT' | 'ROOT_RIG
|
|
|
13
13
|
|
|
14
14
|
export type Comparator<K> = (a: K, b: K) => number;
|
|
15
15
|
|
|
16
|
-
export type DFSOrderPattern = '
|
|
16
|
+
export type DFSOrderPattern = 'PRE' | 'IN' | 'POST';
|
|
17
17
|
|
|
18
18
|
export type NodeDisplayLayout = [string[], number, number, number];
|
|
19
19
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { RedBlackTree, RedBlackTreeNode } from '../../../data-structures';
|
|
2
2
|
import type { BSTOptions } from "./bst";
|
|
3
3
|
|
|
4
|
-
export
|
|
4
|
+
export type RBTNColor = 'RED' | 'BLACK';
|
|
5
5
|
|
|
6
6
|
export type RedBlackTreeNodeNested<K, V> = RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
7
7
|
|