min-heap-typed 1.41.0 → 1.41.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/binary-tree.d.ts +9 -6
- package/dist/data-structures/binary-tree/binary-tree.js +29 -10
- package/dist/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/data-structures/binary-tree/bst.js +2 -2
- package/dist/data-structures/binary-tree/rb-tree.d.ts +20 -4
- package/dist/data-structures/binary-tree/rb-tree.js +109 -44
- package/dist/data-structures/binary-tree/tree-multiset.js +2 -2
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +73 -25
- package/src/data-structures/binary-tree/bst.ts +7 -4
- package/src/data-structures/binary-tree/rb-tree.ts +121 -68
- package/src/data-structures/binary-tree/tree-multiset.ts +4 -3
- package/src/data-structures/graph/abstract-graph.ts +11 -12
- package/src/data-structures/graph/directed-graph.ts +7 -6
- package/src/data-structures/graph/undirected-graph.ts +7 -6
- package/src/data-structures/hash/hash-map.ts +1 -1
- package/src/data-structures/hash/tree-map.ts +1 -2
- package/src/data-structures/hash/tree-set.ts +1 -2
- package/src/data-structures/heap/heap.ts +2 -2
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/matrix/matrix.ts +1 -1
- package/src/data-structures/matrix/vector2d.ts +1 -2
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +3 -4
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
|
@@ -108,7 +108,8 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|
|
108
108
|
* @template N - The type of the binary tree's nodes.
|
|
109
109
|
*/
|
|
110
110
|
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
|
|
111
|
-
implements IBinaryTree<V, N>
|
|
111
|
+
implements IBinaryTree<V, N>
|
|
112
|
+
{
|
|
112
113
|
iterationType: IterationType = IterationType.ITERATIVE;
|
|
113
114
|
|
|
114
115
|
/**
|
|
@@ -201,7 +202,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
201
202
|
}
|
|
202
203
|
|
|
203
204
|
const key = typeof keyOrNode === 'number' ? keyOrNode : keyOrNode ? keyOrNode.key : undefined;
|
|
204
|
-
const existNode = key !== undefined ? this.
|
|
205
|
+
const existNode = key !== undefined ? this.getNode(key, (node: N) => node.key) : undefined;
|
|
205
206
|
|
|
206
207
|
if (this.root) {
|
|
207
208
|
if (existNode) {
|
|
@@ -290,7 +291,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
290
291
|
if (!this.root) return bstDeletedResult;
|
|
291
292
|
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
292
293
|
|
|
293
|
-
const curr = this.
|
|
294
|
+
const curr = this.getNode(identifier, callback);
|
|
294
295
|
if (!curr) return bstDeletedResult;
|
|
295
296
|
|
|
296
297
|
const parent: N | null = curr?.parent ? curr.parent : null;
|
|
@@ -342,8 +343,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
342
343
|
* @returns the depth of the `distNode` relative to the `beginRoot`.
|
|
343
344
|
*/
|
|
344
345
|
getDepth(distNode: BTNKey | N | null, beginRoot: BTNKey | N | null = this.root): number {
|
|
345
|
-
if (typeof distNode === 'number') distNode = this.
|
|
346
|
-
if (typeof beginRoot === 'number') beginRoot = this.
|
|
346
|
+
if (typeof distNode === 'number') distNode = this.getNode(distNode);
|
|
347
|
+
if (typeof beginRoot === 'number') beginRoot = this.getNode(beginRoot);
|
|
347
348
|
let depth = 0;
|
|
348
349
|
while (distNode?.parent) {
|
|
349
350
|
if (distNode === beginRoot) {
|
|
@@ -368,7 +369,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
368
369
|
* @returns the height of the binary tree.
|
|
369
370
|
*/
|
|
370
371
|
getHeight(beginRoot: BTNKey | N | null = this.root, iterationType = this.iterationType): number {
|
|
371
|
-
if (typeof beginRoot === 'number') beginRoot = this.
|
|
372
|
+
if (typeof beginRoot === 'number') beginRoot = this.getNode(beginRoot);
|
|
372
373
|
if (!beginRoot) return -1;
|
|
373
374
|
|
|
374
375
|
if (iterationType === IterationType.RECURSIVE) {
|
|
@@ -385,7 +386,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
385
386
|
return -1;
|
|
386
387
|
}
|
|
387
388
|
|
|
388
|
-
const stack: {
|
|
389
|
+
const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
|
|
389
390
|
let maxHeight = 0;
|
|
390
391
|
|
|
391
392
|
while (stack.length > 0) {
|
|
@@ -558,21 +559,21 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
558
559
|
has<C extends BTNCallback<N, BTNKey>>(
|
|
559
560
|
identifier: BTNKey,
|
|
560
561
|
callback?: C,
|
|
561
|
-
beginRoot?: N,
|
|
562
|
+
beginRoot?: N | null,
|
|
562
563
|
iterationType?: IterationType
|
|
563
564
|
): boolean;
|
|
564
565
|
|
|
565
566
|
has<C extends BTNCallback<N, N>>(
|
|
566
567
|
identifier: N | null,
|
|
567
568
|
callback?: C,
|
|
568
|
-
beginRoot?: N,
|
|
569
|
+
beginRoot?: N | null,
|
|
569
570
|
iterationType?: IterationType
|
|
570
571
|
): boolean;
|
|
571
572
|
|
|
572
573
|
has<C extends BTNCallback<N>>(
|
|
573
574
|
identifier: ReturnType<C> | null,
|
|
574
575
|
callback: C,
|
|
575
|
-
beginRoot?: N,
|
|
576
|
+
beginRoot?: N | null,
|
|
576
577
|
iterationType?: IterationType
|
|
577
578
|
): boolean;
|
|
578
579
|
|
|
@@ -600,28 +601,28 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
600
601
|
iterationType = this.iterationType
|
|
601
602
|
): boolean {
|
|
602
603
|
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
603
|
-
|
|
604
|
+
|
|
604
605
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
605
606
|
}
|
|
606
607
|
|
|
607
|
-
|
|
608
|
+
getNode<C extends BTNCallback<N, BTNKey>>(
|
|
608
609
|
identifier: BTNKey,
|
|
609
610
|
callback?: C,
|
|
610
|
-
beginRoot?: N,
|
|
611
|
+
beginRoot?: N | null,
|
|
611
612
|
iterationType?: IterationType
|
|
612
613
|
): N | null;
|
|
613
614
|
|
|
614
|
-
|
|
615
|
+
getNode<C extends BTNCallback<N, N>>(
|
|
615
616
|
identifier: N | null,
|
|
616
617
|
callback?: C,
|
|
617
|
-
beginRoot?: N,
|
|
618
|
+
beginRoot?: N | null,
|
|
618
619
|
iterationType?: IterationType
|
|
619
620
|
): N | null;
|
|
620
621
|
|
|
621
|
-
|
|
622
|
+
getNode<C extends BTNCallback<N>>(
|
|
622
623
|
identifier: ReturnType<C>,
|
|
623
624
|
callback: C,
|
|
624
|
-
beginRoot?: N,
|
|
625
|
+
beginRoot?: N | null,
|
|
625
626
|
iterationType?: IterationType
|
|
626
627
|
): N | null;
|
|
627
628
|
|
|
@@ -640,17 +641,64 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
640
641
|
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
641
642
|
* @returns either the found node (of type N) or null if no node is found.
|
|
642
643
|
*/
|
|
643
|
-
|
|
644
|
+
getNode<C extends BTNCallback<N>>(
|
|
644
645
|
identifier: ReturnType<C> | null,
|
|
645
646
|
callback: C = ((node: N) => node.key) as C,
|
|
646
647
|
beginRoot = this.root,
|
|
647
648
|
iterationType = this.iterationType
|
|
648
649
|
): N | null {
|
|
649
650
|
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
650
|
-
|
|
651
|
+
|
|
651
652
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
652
653
|
}
|
|
653
654
|
|
|
655
|
+
get<C extends BTNCallback<N, BTNKey>>(
|
|
656
|
+
identifier: BTNKey,
|
|
657
|
+
callback?: C,
|
|
658
|
+
beginRoot?: N | null,
|
|
659
|
+
iterationType?: IterationType
|
|
660
|
+
): V | undefined;
|
|
661
|
+
|
|
662
|
+
get<C extends BTNCallback<N, N>>(
|
|
663
|
+
identifier: N | null,
|
|
664
|
+
callback?: C,
|
|
665
|
+
beginRoot?: N | null,
|
|
666
|
+
iterationType?: IterationType
|
|
667
|
+
): V | undefined;
|
|
668
|
+
|
|
669
|
+
get<C extends BTNCallback<N>>(
|
|
670
|
+
identifier: ReturnType<C>,
|
|
671
|
+
callback: C,
|
|
672
|
+
beginRoot?: N | null,
|
|
673
|
+
iterationType?: IterationType
|
|
674
|
+
): V | undefined;
|
|
675
|
+
|
|
676
|
+
/**
|
|
677
|
+
* The function `get` returns the first node value in a binary tree that matches the given property or key.
|
|
678
|
+
* @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
|
|
679
|
+
* the node that you want to find in the binary tree. It can be either a `BTNKey` or `N`
|
|
680
|
+
* type.
|
|
681
|
+
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
682
|
+
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
683
|
+
* whether the node matches the criteria or not. The default callback function
|
|
684
|
+
* (`((node: N) => node.key)`) is used if no callback function is
|
|
685
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
686
|
+
* the root node from which the search should begin.
|
|
687
|
+
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be
|
|
688
|
+
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
689
|
+
* @returns either the found value (of type V) or undefined if no node value is found.
|
|
690
|
+
*/
|
|
691
|
+
get<C extends BTNCallback<N>>(
|
|
692
|
+
identifier: ReturnType<C> | null,
|
|
693
|
+
callback: C = ((node: N) => node.key) as C,
|
|
694
|
+
beginRoot = this.root,
|
|
695
|
+
iterationType = this.iterationType
|
|
696
|
+
): V | undefined {
|
|
697
|
+
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
698
|
+
|
|
699
|
+
return this.getNode(identifier, callback, beginRoot, iterationType)?.value ?? undefined;
|
|
700
|
+
}
|
|
701
|
+
|
|
654
702
|
/**
|
|
655
703
|
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
|
|
656
704
|
* up to the root node, with the option to reverse the order of the nodes.
|
|
@@ -686,7 +734,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
686
734
|
* no leftmost node, it returns `null`.
|
|
687
735
|
*/
|
|
688
736
|
getLeftMost(beginRoot: BTNKey | N | null = this.root, iterationType = this.iterationType): N | null {
|
|
689
|
-
if (typeof beginRoot === 'number') beginRoot = this.
|
|
737
|
+
if (typeof beginRoot === 'number') beginRoot = this.getNode(beginRoot);
|
|
690
738
|
|
|
691
739
|
if (!beginRoot) return beginRoot;
|
|
692
740
|
|
|
@@ -812,7 +860,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
812
860
|
beginRoot: BTNKey | N | null = this.root,
|
|
813
861
|
iterationType = this.iterationType
|
|
814
862
|
): ReturnType<C>[] {
|
|
815
|
-
if (typeof beginRoot === 'number') beginRoot = this.
|
|
863
|
+
if (typeof beginRoot === 'number') beginRoot = this.getNode(beginRoot);
|
|
816
864
|
|
|
817
865
|
const ans: ReturnType<BTNCallback<N>>[] = [];
|
|
818
866
|
if (!beginRoot) return ans;
|
|
@@ -888,7 +936,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
888
936
|
_traverse(beginRoot);
|
|
889
937
|
} else {
|
|
890
938
|
// 0: visit, 1: print
|
|
891
|
-
const stack: {
|
|
939
|
+
const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];
|
|
892
940
|
|
|
893
941
|
while (stack.length > 0) {
|
|
894
942
|
const cur = stack.pop();
|
|
@@ -960,7 +1008,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
960
1008
|
if (current.right) queue.push(current.right);
|
|
961
1009
|
|
|
962
1010
|
traverse(level + 1);
|
|
963
|
-
}
|
|
1011
|
+
};
|
|
964
1012
|
|
|
965
1013
|
traverse(0);
|
|
966
1014
|
} else {
|
|
@@ -1055,7 +1103,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1055
1103
|
* @returns The function `getSuccessor` returns a value of type `N` (the successor node), or `null`
|
|
1056
1104
|
* if there is no successor, or `undefined` if the input `x` is `undefined`.
|
|
1057
1105
|
*/
|
|
1058
|
-
getSuccessor(x: N): N | null | undefined{
|
|
1106
|
+
getSuccessor(x: N): N | null | undefined {
|
|
1059
1107
|
if (x.right) {
|
|
1060
1108
|
return this.getLeftMost(x.right);
|
|
1061
1109
|
}
|
|
@@ -1178,7 +1226,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1178
1226
|
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
1179
1227
|
* binary tree nodes in a specific order.
|
|
1180
1228
|
*/
|
|
1181
|
-
*
|
|
1229
|
+
*[Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
|
|
1182
1230
|
if (!node) {
|
|
1183
1231
|
return;
|
|
1184
1232
|
}
|
|
@@ -19,7 +19,8 @@ export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extend
|
|
|
19
19
|
|
|
20
20
|
export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>>
|
|
21
21
|
extends BinaryTree<V, N>
|
|
22
|
-
implements IBinaryTree<V, N>
|
|
22
|
+
implements IBinaryTree<V, N>
|
|
23
|
+
{
|
|
23
24
|
/**
|
|
24
25
|
* The constructor function initializes a binary search tree object with an optional comparator
|
|
25
26
|
* function.
|
|
@@ -153,7 +154,9 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
153
154
|
return super.addMany(keysOrNodes, data);
|
|
154
155
|
}
|
|
155
156
|
const inserted: (N | null | undefined)[] = [];
|
|
156
|
-
const combinedArr: [BTNKey | N, V][] = keysOrNodes.map(
|
|
157
|
+
const combinedArr: [BTNKey | N, V][] = keysOrNodes.map(
|
|
158
|
+
(value: BTNKey | N, index) => [value, data?.[index]] as [BTNKey | N, V]
|
|
159
|
+
);
|
|
157
160
|
let sorted = [];
|
|
158
161
|
|
|
159
162
|
function isNodeOrNullTuple(arr: [BTNKey | N, V][]): arr is [N, V][] {
|
|
@@ -231,7 +234,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
231
234
|
* @returns either the first node that matches the given nodeProperty and callback, or null if no
|
|
232
235
|
* matching node is found.
|
|
233
236
|
*/
|
|
234
|
-
override
|
|
237
|
+
override getNode<C extends BTNCallback<N>>(
|
|
235
238
|
identifier: ReturnType<C> | null,
|
|
236
239
|
callback: C = ((node: N) => node.key) as C,
|
|
237
240
|
beginRoot = this.root,
|
|
@@ -362,7 +365,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
362
365
|
targetNode: BTNKey | N | null = this.root,
|
|
363
366
|
iterationType = this.iterationType
|
|
364
367
|
): ReturnType<C>[] {
|
|
365
|
-
if (typeof targetNode === 'number') targetNode = this.
|
|
368
|
+
if (typeof targetNode === 'number') targetNode = this.getNode(targetNode);
|
|
366
369
|
const ans: ReturnType<BTNCallback<N>>[] = [];
|
|
367
370
|
if (!targetNode) return ans;
|
|
368
371
|
const targetKey = targetNode.key;
|
|
@@ -1,27 +1,41 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import {RBTNColor} from '../../types';
|
|
2
10
|
|
|
3
11
|
export class RBTreeNode {
|
|
4
|
-
key: number
|
|
12
|
+
key: number;
|
|
5
13
|
parent: RBTreeNode;
|
|
6
14
|
left: RBTreeNode;
|
|
7
15
|
right: RBTreeNode;
|
|
8
16
|
color: number = RBTNColor.BLACK;
|
|
9
17
|
|
|
10
|
-
constructor() {
|
|
18
|
+
constructor(key: number, color: RBTNColor = RBTNColor.BLACK) {
|
|
19
|
+
this.key = key;
|
|
20
|
+
this.color = color;
|
|
11
21
|
this.parent = null as unknown as RBTreeNode;
|
|
12
22
|
this.left = null as unknown as RBTreeNode;
|
|
13
23
|
this.right = null as unknown as RBTreeNode;
|
|
14
24
|
}
|
|
15
25
|
}
|
|
16
26
|
|
|
27
|
+
export const NIL = new RBTreeNode(0);
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 1. Each node is either red or black.
|
|
31
|
+
* 2. The root node is always black.
|
|
32
|
+
* 3. Leaf nodes are typically NIL nodes and are considered black.
|
|
33
|
+
* 4. Red nodes must have black children.
|
|
34
|
+
* 5. Black balance: Every path from any node to each of its leaf nodes contains the same number of black nodes.
|
|
35
|
+
*/
|
|
17
36
|
export class RedBlackTree {
|
|
18
|
-
|
|
19
37
|
constructor() {
|
|
20
|
-
this.
|
|
21
|
-
this.NIL.color = RBTNColor.BLACK;
|
|
22
|
-
this.NIL.left = null as unknown as RBTreeNode;
|
|
23
|
-
this.NIL.right = null as unknown as RBTreeNode;
|
|
24
|
-
this._root = this.NIL;
|
|
38
|
+
this._root = NIL;
|
|
25
39
|
}
|
|
26
40
|
|
|
27
41
|
protected _root: RBTreeNode;
|
|
@@ -30,12 +44,6 @@ export class RedBlackTree {
|
|
|
30
44
|
return this._root;
|
|
31
45
|
}
|
|
32
46
|
|
|
33
|
-
protected _NIL: RBTreeNode;
|
|
34
|
-
|
|
35
|
-
get NIL(): RBTreeNode {
|
|
36
|
-
return this._NIL;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
47
|
/**
|
|
40
48
|
* The `insert` function inserts a new node with a given key into a red-black tree and fixes any
|
|
41
49
|
* violations of the red-black tree properties.
|
|
@@ -44,18 +52,14 @@ export class RedBlackTree {
|
|
|
44
52
|
* @returns The function does not explicitly return anything.
|
|
45
53
|
*/
|
|
46
54
|
insert(key: number): void {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
node.
|
|
50
|
-
node.key = key;
|
|
51
|
-
node.left = this.NIL;
|
|
52
|
-
node.right = this.NIL;
|
|
53
|
-
node.color = RBTNColor.RED;
|
|
55
|
+
const node: RBTreeNode = new RBTreeNode(key, RBTNColor.RED);
|
|
56
|
+
node.left = NIL;
|
|
57
|
+
node.right = NIL;
|
|
54
58
|
|
|
55
59
|
let y: RBTreeNode = null as unknown as RBTreeNode;
|
|
56
60
|
let x: RBTreeNode = this.root;
|
|
57
61
|
|
|
58
|
-
while (x !==
|
|
62
|
+
while (x !== NIL) {
|
|
59
63
|
y = x;
|
|
60
64
|
if (node.key < x.key) {
|
|
61
65
|
x = x.left;
|
|
@@ -94,10 +98,9 @@ export class RedBlackTree {
|
|
|
94
98
|
*/
|
|
95
99
|
delete(key: number): void {
|
|
96
100
|
const helper = (node: RBTreeNode): void => {
|
|
97
|
-
|
|
98
|
-
let z: RBTreeNode = this.NIL;
|
|
101
|
+
let z: RBTreeNode = NIL;
|
|
99
102
|
let x: RBTreeNode, y: RBTreeNode;
|
|
100
|
-
while (node !==
|
|
103
|
+
while (node !== NIL) {
|
|
101
104
|
if (node.key === key) {
|
|
102
105
|
z = node;
|
|
103
106
|
}
|
|
@@ -109,17 +112,16 @@ export class RedBlackTree {
|
|
|
109
112
|
}
|
|
110
113
|
}
|
|
111
114
|
|
|
112
|
-
if (z ===
|
|
113
|
-
console.log("Couldn't find key in the tree");
|
|
115
|
+
if (z === NIL) {
|
|
114
116
|
return;
|
|
115
117
|
}
|
|
116
118
|
|
|
117
119
|
y = z;
|
|
118
120
|
let yOriginalColor: number = y.color;
|
|
119
|
-
if (z.left ===
|
|
121
|
+
if (z.left === NIL) {
|
|
120
122
|
x = z.right;
|
|
121
123
|
this._rbTransplant(z, z.right);
|
|
122
|
-
} else if (z.right ===
|
|
124
|
+
} else if (z.right === NIL) {
|
|
123
125
|
x = z.left;
|
|
124
126
|
this._rbTransplant(z, z.left);
|
|
125
127
|
} else {
|
|
@@ -139,13 +141,17 @@ export class RedBlackTree {
|
|
|
139
141
|
y.left.parent = y;
|
|
140
142
|
y.color = z.color;
|
|
141
143
|
}
|
|
142
|
-
if (yOriginalColor ===
|
|
144
|
+
if (yOriginalColor === RBTNColor.BLACK) {
|
|
143
145
|
this._fixDelete(x);
|
|
144
146
|
}
|
|
145
|
-
}
|
|
147
|
+
};
|
|
146
148
|
helper(this.root);
|
|
147
149
|
}
|
|
148
150
|
|
|
151
|
+
isRealNode(node: RBTreeNode | null | undefined): node is RBTreeNode {
|
|
152
|
+
return node !== NIL && node !== null;
|
|
153
|
+
}
|
|
154
|
+
|
|
149
155
|
/**
|
|
150
156
|
* The function `getNode` is a recursive depth-first search algorithm that searches for a node with a
|
|
151
157
|
* given key in a red-black tree.
|
|
@@ -158,40 +164,40 @@ export class RedBlackTree {
|
|
|
158
164
|
*/
|
|
159
165
|
getNode(key: number, beginRoot = this.root): RBTreeNode {
|
|
160
166
|
const dfs = (node: RBTreeNode): RBTreeNode => {
|
|
161
|
-
if (
|
|
162
|
-
|
|
163
|
-
|
|
167
|
+
if (this.isRealNode(node)) {
|
|
168
|
+
if (key === node.key) {
|
|
169
|
+
return node;
|
|
170
|
+
}
|
|
164
171
|
|
|
165
|
-
|
|
166
|
-
return dfs(node.
|
|
172
|
+
if (key < node.key) return dfs(node.left);
|
|
173
|
+
return dfs(node.right);
|
|
174
|
+
} else {
|
|
175
|
+
return null as unknown as RBTreeNode;
|
|
167
176
|
}
|
|
168
|
-
|
|
169
|
-
}
|
|
177
|
+
};
|
|
170
178
|
return dfs(beginRoot);
|
|
171
179
|
}
|
|
172
180
|
|
|
173
|
-
|
|
174
181
|
/**
|
|
175
182
|
* The function returns the leftmost node in a red-black tree.
|
|
176
183
|
* @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode, which represents a node in
|
|
177
184
|
* a Red-Black Tree.
|
|
178
185
|
* @returns The leftmost node in the given RBTreeNode.
|
|
179
186
|
*/
|
|
180
|
-
getLeftMost(node: RBTreeNode): RBTreeNode {
|
|
181
|
-
while (node.left !== null && node.left !==
|
|
187
|
+
getLeftMost(node: RBTreeNode = this.root): RBTreeNode {
|
|
188
|
+
while (node.left !== null && node.left !== NIL) {
|
|
182
189
|
node = node.left;
|
|
183
190
|
}
|
|
184
191
|
return node;
|
|
185
192
|
}
|
|
186
193
|
|
|
187
|
-
|
|
188
194
|
/**
|
|
189
195
|
* The function returns the rightmost node in a red-black tree.
|
|
190
196
|
* @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode.
|
|
191
197
|
* @returns the rightmost node in a red-black tree.
|
|
192
198
|
*/
|
|
193
199
|
getRightMost(node: RBTreeNode): RBTreeNode {
|
|
194
|
-
while (node.right !== null && node.right !==
|
|
200
|
+
while (node.right !== null && node.right !== NIL) {
|
|
195
201
|
node = node.right;
|
|
196
202
|
}
|
|
197
203
|
return node;
|
|
@@ -203,13 +209,12 @@ export class RedBlackTree {
|
|
|
203
209
|
* @returns the successor of the given RBTreeNode.
|
|
204
210
|
*/
|
|
205
211
|
getSuccessor(x: RBTreeNode): RBTreeNode {
|
|
206
|
-
|
|
207
|
-
if (x.right !== this.NIL) {
|
|
212
|
+
if (x.right !== NIL) {
|
|
208
213
|
return this.getLeftMost(x.right);
|
|
209
214
|
}
|
|
210
215
|
|
|
211
216
|
let y: RBTreeNode = x.parent;
|
|
212
|
-
while (y !==
|
|
217
|
+
while (y !== NIL && y !== null && x === y.right) {
|
|
213
218
|
x = y;
|
|
214
219
|
y = y.parent;
|
|
215
220
|
}
|
|
@@ -223,13 +228,12 @@ export class RedBlackTree {
|
|
|
223
228
|
* @returns the predecessor of the given RBTreeNode 'x'.
|
|
224
229
|
*/
|
|
225
230
|
getPredecessor(x: RBTreeNode): RBTreeNode {
|
|
226
|
-
|
|
227
|
-
if (x.left !== this.NIL) {
|
|
231
|
+
if (x.left !== NIL) {
|
|
228
232
|
return this.getRightMost(x.left);
|
|
229
233
|
}
|
|
230
234
|
|
|
231
235
|
let y: RBTreeNode = x.parent;
|
|
232
|
-
while (y !==
|
|
236
|
+
while (y !== NIL && x === y.left) {
|
|
233
237
|
x = y;
|
|
234
238
|
y = y.parent;
|
|
235
239
|
}
|
|
@@ -237,6 +241,65 @@ export class RedBlackTree {
|
|
|
237
241
|
return y;
|
|
238
242
|
}
|
|
239
243
|
|
|
244
|
+
print(beginRoot: RBTreeNode = this.root) {
|
|
245
|
+
const display = (root: RBTreeNode | null): void => {
|
|
246
|
+
const [lines, , ,] = _displayAux(root);
|
|
247
|
+
for (const line of lines) {
|
|
248
|
+
console.log(line);
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
const _displayAux = (node: RBTreeNode | null): [string[], number, number, number] => {
|
|
253
|
+
if (node === null) {
|
|
254
|
+
return [[], 0, 0, 0];
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (node.right === null && node.left === null) {
|
|
258
|
+
const line = `${node.key}`;
|
|
259
|
+
const width = line.length;
|
|
260
|
+
const height = 1;
|
|
261
|
+
const middle = Math.floor(width / 2);
|
|
262
|
+
return [[line], width, height, middle];
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (node.right === null) {
|
|
266
|
+
const [lines, n, p, x] = _displayAux(node.left);
|
|
267
|
+
const s = `${node.key}`;
|
|
268
|
+
const u = s.length;
|
|
269
|
+
const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s;
|
|
270
|
+
const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u);
|
|
271
|
+
const shifted_lines = lines.map(line => line + ' '.repeat(u));
|
|
272
|
+
return [[first_line, second_line, ...shifted_lines], n + u, p + 2, n + Math.floor(u / 2)];
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (node.left === null) {
|
|
276
|
+
const [lines, n, p, u] = _displayAux(node.right);
|
|
277
|
+
const s = `${node.key}`;
|
|
278
|
+
const x = s.length;
|
|
279
|
+
const first_line = s + '_'.repeat(x) + ' '.repeat(n - x);
|
|
280
|
+
const second_line = ' '.repeat(u + x) + '\\' + ' '.repeat(n - x - 1);
|
|
281
|
+
const shifted_lines = lines.map(line => ' '.repeat(u) + line);
|
|
282
|
+
return [[first_line, second_line, ...shifted_lines], n + x, p + 2, Math.floor(u / 2)];
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const [left, n, p, x] = _displayAux(node.left);
|
|
286
|
+
const [right, m, q, y] = _displayAux(node.right);
|
|
287
|
+
const s = `${node.key}`;
|
|
288
|
+
const u = s.length;
|
|
289
|
+
const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s + '_'.repeat(y) + ' '.repeat(m - y);
|
|
290
|
+
const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u + y) + '\\' + ' '.repeat(m - y - 1);
|
|
291
|
+
if (p < q) {
|
|
292
|
+
left.push(...new Array(q - p).fill(' '.repeat(n)));
|
|
293
|
+
} else if (q < p) {
|
|
294
|
+
right.push(...new Array(p - q).fill(' '.repeat(m)));
|
|
295
|
+
}
|
|
296
|
+
const zipped_lines = left.map((a, i) => a + ' '.repeat(u) + right[i]);
|
|
297
|
+
return [[first_line, second_line, ...zipped_lines], n + m + u, Math.max(p, q) + 2, n + Math.floor(u / 2)];
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
display(beginRoot);
|
|
301
|
+
}
|
|
302
|
+
|
|
240
303
|
/**
|
|
241
304
|
* The function performs a left rotation on a red-black tree node.
|
|
242
305
|
* @param {RBTreeNode} x - The parameter `x` is a RBTreeNode object.
|
|
@@ -244,7 +307,7 @@ export class RedBlackTree {
|
|
|
244
307
|
protected _leftRotate(x: RBTreeNode): void {
|
|
245
308
|
const y: RBTreeNode = x.right;
|
|
246
309
|
x.right = y.left;
|
|
247
|
-
if (y.left !==
|
|
310
|
+
if (y.left !== NIL) {
|
|
248
311
|
y.left.parent = x;
|
|
249
312
|
}
|
|
250
313
|
y.parent = x.parent;
|
|
@@ -267,7 +330,7 @@ export class RedBlackTree {
|
|
|
267
330
|
protected _rightRotate(x: RBTreeNode): void {
|
|
268
331
|
const y: RBTreeNode = x.left;
|
|
269
332
|
x.left = y.right;
|
|
270
|
-
if (y.right !==
|
|
333
|
+
if (y.right !== NIL) {
|
|
271
334
|
y.right.parent = x;
|
|
272
335
|
}
|
|
273
336
|
y.parent = x.parent;
|
|
@@ -289,24 +352,21 @@ export class RedBlackTree {
|
|
|
289
352
|
*/
|
|
290
353
|
protected _fixDelete(x: RBTreeNode): void {
|
|
291
354
|
let s: RBTreeNode;
|
|
292
|
-
while (x !== this.root && x.color ===
|
|
355
|
+
while (x !== this.root && x.color === RBTNColor.BLACK) {
|
|
293
356
|
if (x === x.parent.left) {
|
|
294
357
|
s = x.parent.right;
|
|
295
358
|
if (s.color === 1) {
|
|
296
|
-
|
|
297
359
|
s.color = RBTNColor.BLACK;
|
|
298
360
|
x.parent.color = RBTNColor.RED;
|
|
299
361
|
this._leftRotate(x.parent);
|
|
300
362
|
s = x.parent.right;
|
|
301
363
|
}
|
|
302
364
|
|
|
303
|
-
if (s.left.color ===
|
|
304
|
-
|
|
365
|
+
if (s.left !== null && s.left.color === RBTNColor.BLACK && s.right.color === RBTNColor.BLACK) {
|
|
305
366
|
s.color = RBTNColor.RED;
|
|
306
367
|
x = x.parent;
|
|
307
368
|
} else {
|
|
308
|
-
if (s.right.color ===
|
|
309
|
-
|
|
369
|
+
if (s.right.color === RBTNColor.BLACK) {
|
|
310
370
|
s.left.color = RBTNColor.BLACK;
|
|
311
371
|
s.color = RBTNColor.RED;
|
|
312
372
|
this._rightRotate(s);
|
|
@@ -322,20 +382,17 @@ export class RedBlackTree {
|
|
|
322
382
|
} else {
|
|
323
383
|
s = x.parent.left;
|
|
324
384
|
if (s.color === 1) {
|
|
325
|
-
|
|
326
385
|
s.color = RBTNColor.BLACK;
|
|
327
386
|
x.parent.color = RBTNColor.RED;
|
|
328
387
|
this._rightRotate(x.parent);
|
|
329
388
|
s = x.parent.left;
|
|
330
389
|
}
|
|
331
390
|
|
|
332
|
-
if (s.right.color ===
|
|
333
|
-
|
|
391
|
+
if (s.right.color === RBTNColor.BLACK && s.right.color === RBTNColor.BLACK) {
|
|
334
392
|
s.color = RBTNColor.RED;
|
|
335
393
|
x = x.parent;
|
|
336
394
|
} else {
|
|
337
|
-
if (s.left.color ===
|
|
338
|
-
|
|
395
|
+
if (s.left.color === RBTNColor.BLACK) {
|
|
339
396
|
s.right.color = RBTNColor.BLACK;
|
|
340
397
|
s.color = RBTNColor.RED;
|
|
341
398
|
this._leftRotate(s);
|
|
@@ -380,14 +437,12 @@ export class RedBlackTree {
|
|
|
380
437
|
if (k.parent === k.parent.parent.right) {
|
|
381
438
|
u = k.parent.parent.left;
|
|
382
439
|
if (u.color === 1) {
|
|
383
|
-
|
|
384
440
|
u.color = RBTNColor.BLACK;
|
|
385
441
|
k.parent.color = RBTNColor.BLACK;
|
|
386
442
|
k.parent.parent.color = RBTNColor.RED;
|
|
387
443
|
k = k.parent.parent;
|
|
388
444
|
} else {
|
|
389
445
|
if (k === k.parent.left) {
|
|
390
|
-
|
|
391
446
|
k = k.parent;
|
|
392
447
|
this._rightRotate(k);
|
|
393
448
|
}
|
|
@@ -400,14 +455,12 @@ export class RedBlackTree {
|
|
|
400
455
|
u = k.parent.parent.right;
|
|
401
456
|
|
|
402
457
|
if (u.color === 1) {
|
|
403
|
-
|
|
404
458
|
u.color = RBTNColor.BLACK;
|
|
405
459
|
k.parent.color = RBTNColor.BLACK;
|
|
406
460
|
k.parent.parent.color = RBTNColor.RED;
|
|
407
461
|
k = k.parent.parent;
|
|
408
462
|
} else {
|
|
409
463
|
if (k === k.parent.right) {
|
|
410
|
-
|
|
411
464
|
k = k.parent;
|
|
412
465
|
this._leftRotate(k);
|
|
413
466
|
}
|
|
@@ -423,4 +476,4 @@ export class RedBlackTree {
|
|
|
423
476
|
}
|
|
424
477
|
this.root.color = RBTNColor.BLACK;
|
|
425
478
|
}
|
|
426
|
-
}
|
|
479
|
+
}
|
|
@@ -37,7 +37,8 @@ export class TreeMultisetNode<
|
|
|
37
37
|
*/
|
|
38
38
|
export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNode<V, TreeMultisetNodeNested<V>>>
|
|
39
39
|
extends AVLTree<V, N>
|
|
40
|
-
implements IBinaryTree<V, N>
|
|
40
|
+
implements IBinaryTree<V, N>
|
|
41
|
+
{
|
|
41
42
|
/**
|
|
42
43
|
* The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
|
|
43
44
|
* merge duplicated values.
|
|
@@ -169,7 +170,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
169
170
|
} else if (parent.right === undefined) {
|
|
170
171
|
parent.right = newNode;
|
|
171
172
|
if (newNode !== null) {
|
|
172
|
-
this._size =
|
|
173
|
+
this._size = this.size + 1;
|
|
173
174
|
this._setCount(this.count + newNode.count);
|
|
174
175
|
}
|
|
175
176
|
return parent.right;
|
|
@@ -282,7 +283,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
282
283
|
const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
|
|
283
284
|
if (!this.root) return bstDeletedResult;
|
|
284
285
|
|
|
285
|
-
const curr: N | null = this.
|
|
286
|
+
const curr: N | null = this.getNode(identifier, callback);
|
|
286
287
|
if (!curr) return bstDeletedResult;
|
|
287
288
|
|
|
288
289
|
const parent: N | null = curr?.parent ? curr.parent : null;
|