min-heap-typed 1.40.0 → 1.41.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/binary-tree.d.ts +8 -0
- package/dist/data-structures/binary-tree/binary-tree.js +22 -2
- package/dist/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/data-structures/binary-tree/bst.js +1 -1
- package/dist/data-structures/binary-tree/rb-tree.d.ts +95 -9
- package/dist/data-structures/binary-tree/rb-tree.js +379 -12
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -7
- package/dist/types/data-structures/binary-tree/rb-tree.js +11 -6
- package/package.json +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +23 -1
- package/src/data-structures/binary-tree/bst.ts +4 -4
- package/src/data-structures/binary-tree/rb-tree.ts +418 -350
- package/src/types/data-structures/binary-tree/rb-tree.ts +6 -6
|
@@ -301,6 +301,14 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
301
301
|
* @returns The function `getPredecessor` returns the predecessor node of the given node `node`.
|
|
302
302
|
*/
|
|
303
303
|
getPredecessor(node: N): N;
|
|
304
|
+
/**
|
|
305
|
+
* The function `getSuccessor` returns the next node in a binary tree given a node `x`, or `null` if
|
|
306
|
+
* `x` is the last node.
|
|
307
|
+
* @param {N} x - N - a node in a binary tree
|
|
308
|
+
* @returns The function `getSuccessor` returns a value of type `N` (the successor node), or `null`
|
|
309
|
+
* if there is no successor, or `undefined` if the input `x` is `undefined`.
|
|
310
|
+
*/
|
|
311
|
+
getSuccessor(x: N): N | null | undefined;
|
|
304
312
|
/**
|
|
305
313
|
* The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
|
|
306
314
|
* algorithm and returns an array of values obtained by applying a callback function to each node.
|
|
@@ -813,7 +813,7 @@ class BinaryTree {
|
|
|
813
813
|
const ans = [];
|
|
814
814
|
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
815
815
|
const queue = new queue_1.Queue([beginRoot]);
|
|
816
|
-
|
|
816
|
+
const traverse = (level) => {
|
|
817
817
|
if (queue.size === 0)
|
|
818
818
|
return;
|
|
819
819
|
const current = queue.shift();
|
|
@@ -823,7 +823,7 @@ class BinaryTree {
|
|
|
823
823
|
if (current.right)
|
|
824
824
|
queue.push(current.right);
|
|
825
825
|
traverse(level + 1);
|
|
826
|
-
}
|
|
826
|
+
};
|
|
827
827
|
traverse(0);
|
|
828
828
|
}
|
|
829
829
|
else {
|
|
@@ -908,6 +908,24 @@ class BinaryTree {
|
|
|
908
908
|
return node;
|
|
909
909
|
}
|
|
910
910
|
}
|
|
911
|
+
/**
|
|
912
|
+
* The function `getSuccessor` returns the next node in a binary tree given a node `x`, or `null` if
|
|
913
|
+
* `x` is the last node.
|
|
914
|
+
* @param {N} x - N - a node in a binary tree
|
|
915
|
+
* @returns The function `getSuccessor` returns a value of type `N` (the successor node), or `null`
|
|
916
|
+
* if there is no successor, or `undefined` if the input `x` is `undefined`.
|
|
917
|
+
*/
|
|
918
|
+
getSuccessor(x) {
|
|
919
|
+
if (x.right) {
|
|
920
|
+
return this.getLeftMost(x.right);
|
|
921
|
+
}
|
|
922
|
+
let y = x.parent;
|
|
923
|
+
while (y && y && x === y.right) {
|
|
924
|
+
x = y;
|
|
925
|
+
y = y.parent;
|
|
926
|
+
}
|
|
927
|
+
return y;
|
|
928
|
+
}
|
|
911
929
|
// --- start additional methods ---
|
|
912
930
|
/**
|
|
913
931
|
* The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
|
|
@@ -1037,10 +1055,12 @@ class BinaryTree {
|
|
|
1037
1055
|
}
|
|
1038
1056
|
else {
|
|
1039
1057
|
if (node.left) {
|
|
1058
|
+
// @ts-ignore
|
|
1040
1059
|
yield* this[Symbol.iterator](node.left);
|
|
1041
1060
|
}
|
|
1042
1061
|
yield node.key;
|
|
1043
1062
|
if (node.right) {
|
|
1063
|
+
// @ts-ignore
|
|
1044
1064
|
yield* this[Symbol.iterator](node.right);
|
|
1045
1065
|
}
|
|
1046
1066
|
}
|
|
@@ -43,7 +43,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
43
43
|
/**
|
|
44
44
|
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
45
45
|
* maintaining balance.
|
|
46
|
-
* @param {[BTNKey | N,
|
|
46
|
+
* @param {[BTNKey | N, V][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
|
47
47
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
48
48
|
* array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
|
|
49
49
|
* `null
|
|
@@ -126,7 +126,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
126
126
|
/**
|
|
127
127
|
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
128
128
|
* maintaining balance.
|
|
129
|
-
* @param {[BTNKey | N,
|
|
129
|
+
* @param {[BTNKey | N, V][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
|
130
130
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
131
131
|
* array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
|
|
132
132
|
* `null
|
|
@@ -1,11 +1,97 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
color:
|
|
1
|
+
export declare class RBTreeNode {
|
|
2
|
+
key: number;
|
|
3
|
+
parent: RBTreeNode;
|
|
4
|
+
left: RBTreeNode;
|
|
5
|
+
right: RBTreeNode;
|
|
6
|
+
color: number;
|
|
7
|
+
constructor();
|
|
7
8
|
}
|
|
8
|
-
export declare class
|
|
9
|
-
constructor(
|
|
10
|
-
|
|
9
|
+
export declare class RedBlackTree {
|
|
10
|
+
constructor();
|
|
11
|
+
protected _root: RBTreeNode;
|
|
12
|
+
get root(): RBTreeNode;
|
|
13
|
+
protected _NIL: RBTreeNode;
|
|
14
|
+
get NIL(): RBTreeNode;
|
|
15
|
+
/**
|
|
16
|
+
* The `insert` function inserts a new node with a given key into a red-black tree and fixes any
|
|
17
|
+
* violations of the red-black tree properties.
|
|
18
|
+
* @param {number} key - The key parameter is a number that represents the value to be inserted into
|
|
19
|
+
* the RBTree.
|
|
20
|
+
* @returns The function does not explicitly return anything.
|
|
21
|
+
*/
|
|
22
|
+
insert(key: number): void;
|
|
23
|
+
/**
|
|
24
|
+
* The `delete` function in TypeScript is used to remove a node with a specific key from a red-black
|
|
25
|
+
* tree.
|
|
26
|
+
* @param {RBTreeNode} node - The `node` parameter is of type `RBTreeNode` and represents the current
|
|
27
|
+
* node being processed in the delete operation.
|
|
28
|
+
* @returns The `delete` function does not return anything. It has a return type of `void`.
|
|
29
|
+
*/
|
|
30
|
+
delete(key: number): void;
|
|
31
|
+
/**
|
|
32
|
+
* The function `getNode` is a recursive depth-first search algorithm that searches for a node with a
|
|
33
|
+
* given key in a red-black tree.
|
|
34
|
+
* @param {number} key - The key parameter is a number that represents the value we are searching for
|
|
35
|
+
* in the RBTree.
|
|
36
|
+
* @param beginRoot - The `beginRoot` parameter is an optional parameter that represents the starting
|
|
37
|
+
* point for the search in the binary search tree. If no value is provided for `beginRoot`, it
|
|
38
|
+
* defaults to the root of the binary search tree (`this.root`).
|
|
39
|
+
* @returns a RBTreeNode.
|
|
40
|
+
*/
|
|
41
|
+
getNode(key: number, beginRoot?: RBTreeNode): RBTreeNode;
|
|
42
|
+
/**
|
|
43
|
+
* The function returns the leftmost node in a red-black tree.
|
|
44
|
+
* @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode, which represents a node in
|
|
45
|
+
* a Red-Black Tree.
|
|
46
|
+
* @returns The leftmost node in the given RBTreeNode.
|
|
47
|
+
*/
|
|
48
|
+
getLeftMost(node: RBTreeNode): RBTreeNode;
|
|
49
|
+
/**
|
|
50
|
+
* The function returns the rightmost node in a red-black tree.
|
|
51
|
+
* @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode.
|
|
52
|
+
* @returns the rightmost node in a red-black tree.
|
|
53
|
+
*/
|
|
54
|
+
getRightMost(node: RBTreeNode): RBTreeNode;
|
|
55
|
+
/**
|
|
56
|
+
* The function returns the successor of a given node in a red-black tree.
|
|
57
|
+
* @param {RBTreeNode} x - RBTreeNode - The node for which we want to find the successor.
|
|
58
|
+
* @returns the successor of the given RBTreeNode.
|
|
59
|
+
*/
|
|
60
|
+
getSuccessor(x: RBTreeNode): RBTreeNode;
|
|
61
|
+
/**
|
|
62
|
+
* The function returns the predecessor of a given node in a red-black tree.
|
|
63
|
+
* @param {RBTreeNode} x - The parameter `x` is of type `RBTreeNode`, which represents a node in a
|
|
64
|
+
* Red-Black Tree.
|
|
65
|
+
* @returns the predecessor of the given RBTreeNode 'x'.
|
|
66
|
+
*/
|
|
67
|
+
getPredecessor(x: RBTreeNode): RBTreeNode;
|
|
68
|
+
/**
|
|
69
|
+
* The function performs a left rotation on a red-black tree node.
|
|
70
|
+
* @param {RBTreeNode} x - The parameter `x` is a RBTreeNode object.
|
|
71
|
+
*/
|
|
72
|
+
protected _leftRotate(x: RBTreeNode): void;
|
|
73
|
+
/**
|
|
74
|
+
* The function performs a right rotation on a red-black tree node.
|
|
75
|
+
* @param {RBTreeNode} x - x is a RBTreeNode, which represents the node that needs to be right
|
|
76
|
+
* rotated.
|
|
77
|
+
*/
|
|
78
|
+
protected _rightRotate(x: RBTreeNode): void;
|
|
79
|
+
/**
|
|
80
|
+
* The _fixDelete function is used to rebalance the Red-Black Tree after a node deletion.
|
|
81
|
+
* @param {RBTreeNode} x - The parameter `x` is of type `RBTreeNode`, which represents a node in a
|
|
82
|
+
* red-black tree.
|
|
83
|
+
*/
|
|
84
|
+
protected _fixDelete(x: RBTreeNode): void;
|
|
85
|
+
/**
|
|
86
|
+
* The function `_rbTransplant` replaces one node in a red-black tree with another node.
|
|
87
|
+
* @param {RBTreeNode} u - The parameter "u" represents a RBTreeNode object.
|
|
88
|
+
* @param {RBTreeNode} v - The parameter "v" is a RBTreeNode object.
|
|
89
|
+
*/
|
|
90
|
+
protected _rbTransplant(u: RBTreeNode, v: RBTreeNode): void;
|
|
91
|
+
/**
|
|
92
|
+
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
93
|
+
* @param {RBTreeNode} k - The parameter `k` is a RBTreeNode object, which represents a node in a
|
|
94
|
+
* red-black tree.
|
|
95
|
+
*/
|
|
96
|
+
protected _fixInsert(k: RBTreeNode): void;
|
|
11
97
|
}
|
|
@@ -1,21 +1,388 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.RedBlackTree = exports.RBTreeNode = void 0;
|
|
4
4
|
const types_1 = require("../../types");
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
this.
|
|
5
|
+
class RBTreeNode {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.key = 0;
|
|
8
|
+
this.color = types_1.RBTNColor.BLACK;
|
|
9
|
+
this.parent = null;
|
|
10
|
+
this.left = null;
|
|
11
|
+
this.right = null;
|
|
10
12
|
}
|
|
11
13
|
}
|
|
12
14
|
exports.RBTreeNode = RBTreeNode;
|
|
13
|
-
class
|
|
14
|
-
constructor(
|
|
15
|
-
|
|
15
|
+
class RedBlackTree {
|
|
16
|
+
constructor() {
|
|
17
|
+
this._NIL = new RBTreeNode();
|
|
18
|
+
this.NIL.color = types_1.RBTNColor.BLACK;
|
|
19
|
+
this.NIL.left = null;
|
|
20
|
+
this.NIL.right = null;
|
|
21
|
+
this._root = this.NIL;
|
|
16
22
|
}
|
|
17
|
-
|
|
18
|
-
return
|
|
23
|
+
get root() {
|
|
24
|
+
return this._root;
|
|
25
|
+
}
|
|
26
|
+
get NIL() {
|
|
27
|
+
return this._NIL;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The `insert` function inserts a new node with a given key into a red-black tree and fixes any
|
|
31
|
+
* violations of the red-black tree properties.
|
|
32
|
+
* @param {number} key - The key parameter is a number that represents the value to be inserted into
|
|
33
|
+
* the RBTree.
|
|
34
|
+
* @returns The function does not explicitly return anything.
|
|
35
|
+
*/
|
|
36
|
+
insert(key) {
|
|
37
|
+
const node = new RBTreeNode();
|
|
38
|
+
node.parent = null;
|
|
39
|
+
node.key = key;
|
|
40
|
+
node.left = this.NIL;
|
|
41
|
+
node.right = this.NIL;
|
|
42
|
+
node.color = types_1.RBTNColor.RED;
|
|
43
|
+
let y = null;
|
|
44
|
+
let x = this.root;
|
|
45
|
+
while (x !== this.NIL) {
|
|
46
|
+
y = x;
|
|
47
|
+
if (node.key < x.key) {
|
|
48
|
+
x = x.left;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
x = x.right;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
node.parent = y;
|
|
55
|
+
if (y === null) {
|
|
56
|
+
this._root = node;
|
|
57
|
+
}
|
|
58
|
+
else if (node.key < y.key) {
|
|
59
|
+
y.left = node;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
y.right = node;
|
|
63
|
+
}
|
|
64
|
+
if (node.parent === null) {
|
|
65
|
+
node.color = types_1.RBTNColor.BLACK;
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (node.parent.parent === null) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
this._fixInsert(node);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The `delete` function in TypeScript is used to remove a node with a specific key from a red-black
|
|
75
|
+
* tree.
|
|
76
|
+
* @param {RBTreeNode} node - The `node` parameter is of type `RBTreeNode` and represents the current
|
|
77
|
+
* node being processed in the delete operation.
|
|
78
|
+
* @returns The `delete` function does not return anything. It has a return type of `void`.
|
|
79
|
+
*/
|
|
80
|
+
delete(key) {
|
|
81
|
+
const helper = (node) => {
|
|
82
|
+
let z = this.NIL;
|
|
83
|
+
let x, y;
|
|
84
|
+
while (node !== this.NIL) {
|
|
85
|
+
if (node.key === key) {
|
|
86
|
+
z = node;
|
|
87
|
+
}
|
|
88
|
+
if (node.key <= key) {
|
|
89
|
+
node = node.right;
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
node = node.left;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (z === this.NIL) {
|
|
96
|
+
console.log("Couldn't find key in the tree");
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
y = z;
|
|
100
|
+
let yOriginalColor = y.color;
|
|
101
|
+
if (z.left === this.NIL) {
|
|
102
|
+
x = z.right;
|
|
103
|
+
this._rbTransplant(z, z.right);
|
|
104
|
+
}
|
|
105
|
+
else if (z.right === this.NIL) {
|
|
106
|
+
x = z.left;
|
|
107
|
+
this._rbTransplant(z, z.left);
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
y = this.getLeftMost(z.right);
|
|
111
|
+
yOriginalColor = y.color;
|
|
112
|
+
x = y.right;
|
|
113
|
+
if (y.parent === z) {
|
|
114
|
+
x.parent = y;
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
this._rbTransplant(y, y.right);
|
|
118
|
+
y.right = z.right;
|
|
119
|
+
y.right.parent = y;
|
|
120
|
+
}
|
|
121
|
+
this._rbTransplant(z, y);
|
|
122
|
+
y.left = z.left;
|
|
123
|
+
y.left.parent = y;
|
|
124
|
+
y.color = z.color;
|
|
125
|
+
}
|
|
126
|
+
if (yOriginalColor === 0) {
|
|
127
|
+
this._fixDelete(x);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
helper(this.root);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* The function `getNode` is a recursive depth-first search algorithm that searches for a node with a
|
|
134
|
+
* given key in a red-black tree.
|
|
135
|
+
* @param {number} key - The key parameter is a number that represents the value we are searching for
|
|
136
|
+
* in the RBTree.
|
|
137
|
+
* @param beginRoot - The `beginRoot` parameter is an optional parameter that represents the starting
|
|
138
|
+
* point for the search in the binary search tree. If no value is provided for `beginRoot`, it
|
|
139
|
+
* defaults to the root of the binary search tree (`this.root`).
|
|
140
|
+
* @returns a RBTreeNode.
|
|
141
|
+
*/
|
|
142
|
+
getNode(key, beginRoot = this.root) {
|
|
143
|
+
const dfs = (node) => {
|
|
144
|
+
if (node === this.NIL || key === node.key) {
|
|
145
|
+
return node;
|
|
146
|
+
}
|
|
147
|
+
if (key < node.key) {
|
|
148
|
+
return dfs(node.left);
|
|
149
|
+
}
|
|
150
|
+
return dfs(node.right);
|
|
151
|
+
};
|
|
152
|
+
return dfs(beginRoot);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* The function returns the leftmost node in a red-black tree.
|
|
156
|
+
* @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode, which represents a node in
|
|
157
|
+
* a Red-Black Tree.
|
|
158
|
+
* @returns The leftmost node in the given RBTreeNode.
|
|
159
|
+
*/
|
|
160
|
+
getLeftMost(node) {
|
|
161
|
+
while (node.left !== null && node.left !== this.NIL) {
|
|
162
|
+
node = node.left;
|
|
163
|
+
}
|
|
164
|
+
return node;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* The function returns the rightmost node in a red-black tree.
|
|
168
|
+
* @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode.
|
|
169
|
+
* @returns the rightmost node in a red-black tree.
|
|
170
|
+
*/
|
|
171
|
+
getRightMost(node) {
|
|
172
|
+
while (node.right !== null && node.right !== this.NIL) {
|
|
173
|
+
node = node.right;
|
|
174
|
+
}
|
|
175
|
+
return node;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* The function returns the successor of a given node in a red-black tree.
|
|
179
|
+
* @param {RBTreeNode} x - RBTreeNode - The node for which we want to find the successor.
|
|
180
|
+
* @returns the successor of the given RBTreeNode.
|
|
181
|
+
*/
|
|
182
|
+
getSuccessor(x) {
|
|
183
|
+
if (x.right !== this.NIL) {
|
|
184
|
+
return this.getLeftMost(x.right);
|
|
185
|
+
}
|
|
186
|
+
let y = x.parent;
|
|
187
|
+
while (y !== this.NIL && y !== null && x === y.right) {
|
|
188
|
+
x = y;
|
|
189
|
+
y = y.parent;
|
|
190
|
+
}
|
|
191
|
+
return y;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* The function returns the predecessor of a given node in a red-black tree.
|
|
195
|
+
* @param {RBTreeNode} x - The parameter `x` is of type `RBTreeNode`, which represents a node in a
|
|
196
|
+
* Red-Black Tree.
|
|
197
|
+
* @returns the predecessor of the given RBTreeNode 'x'.
|
|
198
|
+
*/
|
|
199
|
+
getPredecessor(x) {
|
|
200
|
+
if (x.left !== this.NIL) {
|
|
201
|
+
return this.getRightMost(x.left);
|
|
202
|
+
}
|
|
203
|
+
let y = x.parent;
|
|
204
|
+
while (y !== this.NIL && x === y.left) {
|
|
205
|
+
x = y;
|
|
206
|
+
y = y.parent;
|
|
207
|
+
}
|
|
208
|
+
return y;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* The function performs a left rotation on a red-black tree node.
|
|
212
|
+
* @param {RBTreeNode} x - The parameter `x` is a RBTreeNode object.
|
|
213
|
+
*/
|
|
214
|
+
_leftRotate(x) {
|
|
215
|
+
const y = x.right;
|
|
216
|
+
x.right = y.left;
|
|
217
|
+
if (y.left !== this.NIL) {
|
|
218
|
+
y.left.parent = x;
|
|
219
|
+
}
|
|
220
|
+
y.parent = x.parent;
|
|
221
|
+
if (x.parent === null) {
|
|
222
|
+
this._root = y;
|
|
223
|
+
}
|
|
224
|
+
else if (x === x.parent.left) {
|
|
225
|
+
x.parent.left = y;
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
x.parent.right = y;
|
|
229
|
+
}
|
|
230
|
+
y.left = x;
|
|
231
|
+
x.parent = y;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* The function performs a right rotation on a red-black tree node.
|
|
235
|
+
* @param {RBTreeNode} x - x is a RBTreeNode, which represents the node that needs to be right
|
|
236
|
+
* rotated.
|
|
237
|
+
*/
|
|
238
|
+
_rightRotate(x) {
|
|
239
|
+
const y = x.left;
|
|
240
|
+
x.left = y.right;
|
|
241
|
+
if (y.right !== this.NIL) {
|
|
242
|
+
y.right.parent = x;
|
|
243
|
+
}
|
|
244
|
+
y.parent = x.parent;
|
|
245
|
+
if (x.parent === null) {
|
|
246
|
+
this._root = y;
|
|
247
|
+
}
|
|
248
|
+
else if (x === x.parent.right) {
|
|
249
|
+
x.parent.right = y;
|
|
250
|
+
}
|
|
251
|
+
else {
|
|
252
|
+
x.parent.left = y;
|
|
253
|
+
}
|
|
254
|
+
y.right = x;
|
|
255
|
+
x.parent = y;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* The _fixDelete function is used to rebalance the Red-Black Tree after a node deletion.
|
|
259
|
+
* @param {RBTreeNode} x - The parameter `x` is of type `RBTreeNode`, which represents a node in a
|
|
260
|
+
* red-black tree.
|
|
261
|
+
*/
|
|
262
|
+
_fixDelete(x) {
|
|
263
|
+
let s;
|
|
264
|
+
while (x !== this.root && x.color === 0) {
|
|
265
|
+
if (x === x.parent.left) {
|
|
266
|
+
s = x.parent.right;
|
|
267
|
+
if (s.color === 1) {
|
|
268
|
+
s.color = types_1.RBTNColor.BLACK;
|
|
269
|
+
x.parent.color = types_1.RBTNColor.RED;
|
|
270
|
+
this._leftRotate(x.parent);
|
|
271
|
+
s = x.parent.right;
|
|
272
|
+
}
|
|
273
|
+
if (s.left.color === 0 && s.right.color === 0) {
|
|
274
|
+
s.color = types_1.RBTNColor.RED;
|
|
275
|
+
x = x.parent;
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
if (s.right.color === 0) {
|
|
279
|
+
s.left.color = types_1.RBTNColor.BLACK;
|
|
280
|
+
s.color = types_1.RBTNColor.RED;
|
|
281
|
+
this._rightRotate(s);
|
|
282
|
+
s = x.parent.right;
|
|
283
|
+
}
|
|
284
|
+
s.color = x.parent.color;
|
|
285
|
+
x.parent.color = types_1.RBTNColor.BLACK;
|
|
286
|
+
s.right.color = types_1.RBTNColor.BLACK;
|
|
287
|
+
this._leftRotate(x.parent);
|
|
288
|
+
x = this.root;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
s = x.parent.left;
|
|
293
|
+
if (s.color === 1) {
|
|
294
|
+
s.color = types_1.RBTNColor.BLACK;
|
|
295
|
+
x.parent.color = types_1.RBTNColor.RED;
|
|
296
|
+
this._rightRotate(x.parent);
|
|
297
|
+
s = x.parent.left;
|
|
298
|
+
}
|
|
299
|
+
if (s.right.color === 0 && s.right.color === 0) {
|
|
300
|
+
s.color = types_1.RBTNColor.RED;
|
|
301
|
+
x = x.parent;
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
if (s.left.color === 0) {
|
|
305
|
+
s.right.color = types_1.RBTNColor.BLACK;
|
|
306
|
+
s.color = types_1.RBTNColor.RED;
|
|
307
|
+
this._leftRotate(s);
|
|
308
|
+
s = x.parent.left;
|
|
309
|
+
}
|
|
310
|
+
s.color = x.parent.color;
|
|
311
|
+
x.parent.color = types_1.RBTNColor.BLACK;
|
|
312
|
+
s.left.color = types_1.RBTNColor.BLACK;
|
|
313
|
+
this._rightRotate(x.parent);
|
|
314
|
+
x = this.root;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
x.color = types_1.RBTNColor.BLACK;
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* The function `_rbTransplant` replaces one node in a red-black tree with another node.
|
|
322
|
+
* @param {RBTreeNode} u - The parameter "u" represents a RBTreeNode object.
|
|
323
|
+
* @param {RBTreeNode} v - The parameter "v" is a RBTreeNode object.
|
|
324
|
+
*/
|
|
325
|
+
_rbTransplant(u, v) {
|
|
326
|
+
if (u.parent === null) {
|
|
327
|
+
this._root = v;
|
|
328
|
+
}
|
|
329
|
+
else if (u === u.parent.left) {
|
|
330
|
+
u.parent.left = v;
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
333
|
+
u.parent.right = v;
|
|
334
|
+
}
|
|
335
|
+
v.parent = u.parent;
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
339
|
+
* @param {RBTreeNode} k - The parameter `k` is a RBTreeNode object, which represents a node in a
|
|
340
|
+
* red-black tree.
|
|
341
|
+
*/
|
|
342
|
+
_fixInsert(k) {
|
|
343
|
+
let u;
|
|
344
|
+
while (k.parent.color === 1) {
|
|
345
|
+
if (k.parent === k.parent.parent.right) {
|
|
346
|
+
u = k.parent.parent.left;
|
|
347
|
+
if (u.color === 1) {
|
|
348
|
+
u.color = types_1.RBTNColor.BLACK;
|
|
349
|
+
k.parent.color = types_1.RBTNColor.BLACK;
|
|
350
|
+
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
351
|
+
k = k.parent.parent;
|
|
352
|
+
}
|
|
353
|
+
else {
|
|
354
|
+
if (k === k.parent.left) {
|
|
355
|
+
k = k.parent;
|
|
356
|
+
this._rightRotate(k);
|
|
357
|
+
}
|
|
358
|
+
k.parent.color = types_1.RBTNColor.BLACK;
|
|
359
|
+
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
360
|
+
this._leftRotate(k.parent.parent);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
else {
|
|
364
|
+
u = k.parent.parent.right;
|
|
365
|
+
if (u.color === 1) {
|
|
366
|
+
u.color = types_1.RBTNColor.BLACK;
|
|
367
|
+
k.parent.color = types_1.RBTNColor.BLACK;
|
|
368
|
+
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
369
|
+
k = k.parent.parent;
|
|
370
|
+
}
|
|
371
|
+
else {
|
|
372
|
+
if (k === k.parent.right) {
|
|
373
|
+
k = k.parent;
|
|
374
|
+
this._leftRotate(k);
|
|
375
|
+
}
|
|
376
|
+
k.parent.color = types_1.RBTNColor.BLACK;
|
|
377
|
+
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
378
|
+
this._rightRotate(k.parent.parent);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
if (k === this.root) {
|
|
382
|
+
break;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
this.root.color = types_1.RBTNColor.BLACK;
|
|
19
386
|
}
|
|
20
387
|
}
|
|
21
|
-
exports.
|
|
388
|
+
exports.RedBlackTree = RedBlackTree;
|
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
RED = "RED",
|
|
5
|
-
BLACK = "BLACK"
|
|
1
|
+
export declare enum RBTNColor {
|
|
2
|
+
RED = 1,
|
|
3
|
+
BLACK = 0
|
|
6
4
|
}
|
|
7
|
-
export type RBTreeNodeNested<T> = RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
8
|
-
export type RBTreeOptions = BinaryTreeOptions & {};
|
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
// import {BinaryTreeOptions} from './binary-tree';
|
|
3
|
+
// import {RBTreeNode} from '../../../data-structures';
|
|
2
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
var
|
|
5
|
-
(function (
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
})(
|
|
5
|
+
exports.RBTNColor = void 0;
|
|
6
|
+
var RBTNColor;
|
|
7
|
+
(function (RBTNColor) {
|
|
8
|
+
RBTNColor[RBTNColor["RED"] = 1] = "RED";
|
|
9
|
+
RBTNColor[RBTNColor["BLACK"] = 0] = "BLACK";
|
|
10
|
+
})(RBTNColor = exports.RBTNColor || (exports.RBTNColor = {}));
|
|
11
|
+
// export type RBTreeNodeNested<T> = RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
12
|
+
//
|
|
13
|
+
// export type RBTreeOptions = BinaryTreeOptions & {}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "min-heap-typed",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.41.0",
|
|
4
4
|
"description": "Min Heap. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -131,6 +131,6 @@
|
|
|
131
131
|
"typescript": "^4.9.5"
|
|
132
132
|
},
|
|
133
133
|
"dependencies": {
|
|
134
|
-
"data-structure-typed": "^1.
|
|
134
|
+
"data-structure-typed": "^1.41.0"
|
|
135
135
|
}
|
|
136
136
|
}
|