data-structure-typed 1.50.5 → 1.50.7
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/CHANGELOG.md +1 -1
- package/README.md +13 -18
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +167 -143
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +3 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +32 -28
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.js +17 -17
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +158 -141
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +416 -396
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +1 -0
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +84 -76
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/types/common.d.ts +6 -0
- package/dist/cjs/types/common.js +8 -1
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -0
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +3 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +32 -28
- package/dist/mjs/data-structures/binary-tree/bst.js +17 -17
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +158 -141
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +413 -396
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +1 -0
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +84 -76
- package/dist/mjs/types/common.d.ts +6 -0
- package/dist/mjs/types/common.js +7 -0
- package/dist/umd/data-structure-typed.js +519 -467
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +5 -1
- package/src/data-structures/binary-tree/avl-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +31 -29
- package/src/data-structures/binary-tree/bst.ts +18 -18
- package/src/data-structures/binary-tree/rb-tree.ts +436 -405
- package/src/data-structures/binary-tree/tree-multi-map.ts +85 -82
- package/src/types/common.ts +7 -0
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +26 -16
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +4 -1
- package/test/unit/data-structures/binary-tree/overall.test.ts +23 -21
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +387 -324
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +335 -204
|
@@ -1,11 +1,4 @@
|
|
|
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
|
-
import { RBTNColor } from '../../types';
|
|
1
|
+
import { CRUD, RBTNColor } from '../../types';
|
|
9
2
|
import { BST, BSTNode } from './bst';
|
|
10
3
|
export class RedBlackTreeNode extends BSTNode {
|
|
11
4
|
/**
|
|
@@ -26,7 +19,7 @@ export class RedBlackTreeNode extends BSTNode {
|
|
|
26
19
|
_color;
|
|
27
20
|
/**
|
|
28
21
|
* The function returns the color value of a variable.
|
|
29
|
-
* @returns The color value stored in the
|
|
22
|
+
* @returns The color value stored in the private variable `_color`.
|
|
30
23
|
*/
|
|
31
24
|
get color() {
|
|
32
25
|
return this._color;
|
|
@@ -39,64 +32,49 @@ export class RedBlackTreeNode extends BSTNode {
|
|
|
39
32
|
this._color = value;
|
|
40
33
|
}
|
|
41
34
|
}
|
|
42
|
-
/**
|
|
43
|
-
* 1. Each node is either red or black.
|
|
44
|
-
* 2. The root node is always black.
|
|
45
|
-
* 3. Leaf nodes are typically Sentinel nodes and are considered black.
|
|
46
|
-
* 4. Red nodes must have black children.
|
|
47
|
-
* 5. Black balance: Every path from any node to each of its leaf nodes contains the same number of black nodes.
|
|
48
|
-
*/
|
|
49
35
|
export class RedBlackTree extends BST {
|
|
50
36
|
/**
|
|
51
|
-
* This is the constructor function for a Red-Black Tree data structure in TypeScript
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
37
|
+
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
|
|
38
|
+
* @param keysOrNodesOrEntries - The `keysOrNodesOrEntries` parameter is an iterable object that can
|
|
39
|
+
* contain keys, nodes, or entries. It is used to initialize the RBTree with the provided keys,
|
|
40
|
+
* nodes, or entries.
|
|
41
|
+
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
42
|
+
* constructor. It allows you to customize the behavior of the RBTree. It can include properties such
|
|
43
|
+
* as `compareKeys`, `compareValues`, `allowDuplicates`, etc. These properties define how the RBTree
|
|
44
|
+
* should compare keys and
|
|
60
45
|
*/
|
|
61
46
|
constructor(keysOrNodesOrEntries = [], options) {
|
|
62
47
|
super([], options);
|
|
63
|
-
this._root = this.
|
|
64
|
-
if (keysOrNodesOrEntries)
|
|
65
|
-
|
|
48
|
+
this._root = this.SENTINEL;
|
|
49
|
+
if (keysOrNodesOrEntries) {
|
|
50
|
+
this.addMany(keysOrNodesOrEntries);
|
|
51
|
+
}
|
|
66
52
|
}
|
|
67
|
-
|
|
53
|
+
_SENTINEL = new RedBlackTreeNode(NaN);
|
|
68
54
|
/**
|
|
69
|
-
* The function returns the value of the
|
|
70
|
-
* @returns The method is returning the value of the `
|
|
55
|
+
* The function returns the value of the _SENTINEL property.
|
|
56
|
+
* @returns The method is returning the value of the `_SENTINEL` property.
|
|
71
57
|
*/
|
|
72
|
-
get
|
|
73
|
-
return this.
|
|
58
|
+
get SENTINEL() {
|
|
59
|
+
return this._SENTINEL;
|
|
74
60
|
}
|
|
75
61
|
_root;
|
|
76
62
|
/**
|
|
77
|
-
* The function returns the root node.
|
|
78
|
-
* @returns The root node of the
|
|
63
|
+
* The function returns the root node of a tree or undefined if there is no root.
|
|
64
|
+
* @returns The root node of the tree structure, or undefined if there is no root node.
|
|
79
65
|
*/
|
|
80
66
|
get root() {
|
|
81
67
|
return this._root;
|
|
82
68
|
}
|
|
83
|
-
_size = 0;
|
|
84
|
-
/**
|
|
85
|
-
* The function returns the size of an object.
|
|
86
|
-
* @returns The size of the object, which is a number.
|
|
87
|
-
*/
|
|
88
|
-
get size() {
|
|
89
|
-
return this._size;
|
|
90
|
-
}
|
|
91
69
|
/**
|
|
92
70
|
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
93
|
-
* @param {K} key - The key parameter
|
|
94
|
-
*
|
|
71
|
+
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
|
|
72
|
+
* which is a generic type representing the key's data type.
|
|
95
73
|
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
96
|
-
* associated with the node. It is
|
|
97
|
-
* specific type when using the `createNode` method.
|
|
74
|
+
* associated with the key in the node. It is not required and can be omitted if not needed.
|
|
98
75
|
* @param {RBTNColor} color - The "color" parameter is used to specify the color of the node in a
|
|
99
|
-
* Red-Black Tree. It
|
|
76
|
+
* Red-Black Tree. It is an optional parameter with a default value of "RBTNColor.BLACK". The color
|
|
77
|
+
* can be either "RBTNColor.RED" or "RBTNColor.BLACK".
|
|
100
78
|
* @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
|
|
101
79
|
* value, and color.
|
|
102
80
|
*/
|
|
@@ -104,10 +82,10 @@ export class RedBlackTree extends BST {
|
|
|
104
82
|
return new RedBlackTreeNode(key, value, color);
|
|
105
83
|
}
|
|
106
84
|
/**
|
|
107
|
-
* The function creates a Red-Black Tree with the
|
|
108
|
-
* @param
|
|
109
|
-
*
|
|
110
|
-
*
|
|
85
|
+
* The function creates a Red-Black Tree with the given options and returns it.
|
|
86
|
+
* @param [options] - The `options` parameter is an optional object that contains configuration
|
|
87
|
+
* options for creating the Red-Black Tree. It is of type `RBTreeOptions<K>`, where `K` represents
|
|
88
|
+
* the type of keys in the tree.
|
|
111
89
|
* @returns a new instance of a RedBlackTree object.
|
|
112
90
|
*/
|
|
113
91
|
createTree(options) {
|
|
@@ -117,12 +95,18 @@ export class RedBlackTree extends BST {
|
|
|
117
95
|
});
|
|
118
96
|
}
|
|
119
97
|
/**
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
*
|
|
125
|
-
*
|
|
98
|
+
* Time Complexity: O(1)
|
|
99
|
+
* Space Complexity: O(1)
|
|
100
|
+
*/
|
|
101
|
+
/**
|
|
102
|
+
* Time Complexity: O(1)
|
|
103
|
+
* Space Complexity: O(1)
|
|
104
|
+
*
|
|
105
|
+
* The function `keyValueOrEntryToNode` takes a key, value, or entry and returns a node if it is
|
|
106
|
+
* valid, otherwise it returns undefined.
|
|
107
|
+
* @param {KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntry - The key, value, or entry to convert.
|
|
108
|
+
* @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntry` is a key).
|
|
109
|
+
* @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
|
|
126
110
|
*/
|
|
127
111
|
keyValueOrEntryToNode(keyOrNodeOrEntry, value) {
|
|
128
112
|
let node;
|
|
@@ -150,166 +134,83 @@ export class RedBlackTree extends BST {
|
|
|
150
134
|
return node;
|
|
151
135
|
}
|
|
152
136
|
/**
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
|
|
137
|
+
* Time Complexity: O(1)
|
|
138
|
+
* Space Complexity: O(1)
|
|
139
|
+
* /
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Time Complexity: O(1)
|
|
143
|
+
* Space Complexity: O(1)
|
|
144
|
+
*
|
|
145
|
+
* The function checks if the input is an instance of the RedBlackTreeNode class.
|
|
146
|
+
* @param {KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntry - The object to check.
|
|
147
|
+
* @returns {boolean} - `true` if the object is a Red-Black Tree node, `false` otherwise.
|
|
157
148
|
*/
|
|
158
149
|
isNode(keyOrNodeOrEntry) {
|
|
159
150
|
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
|
|
160
151
|
}
|
|
161
152
|
/**
|
|
153
|
+
* Time Complexity: O(1)
|
|
154
|
+
* Space Complexity: O(1)
|
|
155
|
+
*/
|
|
156
|
+
/**
|
|
157
|
+
* Time Complexity: O(1)
|
|
158
|
+
* Space Complexity: O(1)
|
|
159
|
+
*
|
|
162
160
|
* The function checks if a given node is a real node in a Red-Black Tree.
|
|
163
161
|
* @param {NODE | undefined} node - The `node` parameter is of type `NODE | undefined`, which means
|
|
164
162
|
* it can either be of type `NODE` or `undefined`.
|
|
165
163
|
* @returns a boolean value.
|
|
166
164
|
*/
|
|
167
165
|
isRealNode(node) {
|
|
168
|
-
if (node === this.
|
|
166
|
+
if (node === this.SENTINEL || node === undefined)
|
|
169
167
|
return false;
|
|
170
168
|
return node instanceof RedBlackTreeNode;
|
|
171
169
|
}
|
|
172
170
|
/**
|
|
173
171
|
* Time Complexity: O(log n)
|
|
174
172
|
* Space Complexity: O(1)
|
|
175
|
-
* On average (where n is the number of nodes in the tree)
|
|
176
173
|
*/
|
|
177
174
|
/**
|
|
178
175
|
* Time Complexity: O(log n)
|
|
179
176
|
* Space Complexity: O(1)
|
|
180
177
|
*
|
|
181
|
-
* The `
|
|
182
|
-
*
|
|
183
|
-
* @param
|
|
184
|
-
*
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
*
|
|
178
|
+
* The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
|
|
179
|
+
* callback function.
|
|
180
|
+
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
|
|
181
|
+
* that you want to search for in the binary search tree. It can be of any type that is compatible
|
|
182
|
+
* with the type of nodes in the tree.
|
|
183
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
184
|
+
* the tree. It is used to determine whether a node matches the given identifier. The `callback`
|
|
185
|
+
* function should take a node as its parameter and return a value that can be compared to the
|
|
186
|
+
* `identifier` parameter.
|
|
187
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
|
|
188
|
+
* search tree. It can be either a key or a node. If it is a key, it will be converted to a node
|
|
189
|
+
* using the `ensureNode` method. If it is not provided, the `root`
|
|
190
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
191
|
+
* be performed when searching for nodes in the binary search tree. It is an optional parameter and
|
|
192
|
+
* its default value is taken from the `iterationType` property of the class.
|
|
193
|
+
* @returns The method is returning a value of type `NODE | null | undefined`.
|
|
188
194
|
*/
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
newNode.left = this._Sentinel;
|
|
194
|
-
newNode.right = this._Sentinel;
|
|
195
|
-
let y = undefined;
|
|
196
|
-
let x = this.root;
|
|
197
|
-
while (x !== this._Sentinel) {
|
|
198
|
-
y = x;
|
|
199
|
-
if (x) {
|
|
200
|
-
if (newNode.key < x.key) {
|
|
201
|
-
x = x.left;
|
|
202
|
-
}
|
|
203
|
-
else if (newNode.key > x.key) {
|
|
204
|
-
x = x?.right;
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
207
|
-
if (newNode !== x) {
|
|
208
|
-
this._replaceNode(x, newNode);
|
|
209
|
-
}
|
|
210
|
-
return false;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
newNode.parent = y;
|
|
215
|
-
if (y === undefined) {
|
|
216
|
-
this._setRoot(newNode);
|
|
217
|
-
}
|
|
218
|
-
else if (newNode.key < y.key) {
|
|
219
|
-
y.left = newNode;
|
|
220
|
-
}
|
|
221
|
-
else {
|
|
222
|
-
y.right = newNode;
|
|
223
|
-
}
|
|
224
|
-
if (newNode.parent === undefined) {
|
|
225
|
-
newNode.color = RBTNColor.BLACK;
|
|
226
|
-
this._size++;
|
|
227
|
-
return false;
|
|
228
|
-
}
|
|
229
|
-
if (newNode.parent.parent === undefined) {
|
|
230
|
-
this._size++;
|
|
231
|
-
return false;
|
|
232
|
-
}
|
|
233
|
-
this._fixInsert(newNode);
|
|
234
|
-
this._size++;
|
|
235
|
-
return true;
|
|
195
|
+
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
196
|
+
if (identifier instanceof RedBlackTreeNode)
|
|
197
|
+
callback = (node => node);
|
|
198
|
+
return super.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
236
199
|
}
|
|
237
200
|
/**
|
|
238
|
-
* Time Complexity: O(
|
|
201
|
+
* Time Complexity: O(1)
|
|
239
202
|
* Space Complexity: O(1)
|
|
240
203
|
*/
|
|
241
204
|
/**
|
|
242
|
-
* Time Complexity: O(
|
|
205
|
+
* Time Complexity: O(1)
|
|
243
206
|
* Space Complexity: O(1)
|
|
244
207
|
*
|
|
245
|
-
* The
|
|
246
|
-
*
|
|
247
|
-
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
248
|
-
* that you want to use to identify the node that you want to delete from the binary tree. It can be
|
|
249
|
-
* of any type that is returned by the callback function `C`. It can also be `null` or `undefined` if
|
|
250
|
-
* you don't want to
|
|
251
|
-
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` and
|
|
252
|
-
* returns a value of type `ReturnType<C>`. It is used to determine if a node should be deleted based
|
|
253
|
-
* on its identifier. The `callback` function is optional and defaults to `this._defaultOneParam
|
|
254
|
-
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
208
|
+
* The "clear" function sets the root node of a data structure to a sentinel value and resets the
|
|
209
|
+
* size counter to zero.
|
|
255
210
|
*/
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
return ans;
|
|
260
|
-
const helper = (node) => {
|
|
261
|
-
let z = this._Sentinel;
|
|
262
|
-
let x, y;
|
|
263
|
-
while (node !== this._Sentinel) {
|
|
264
|
-
if (node && callback(node) === identifier) {
|
|
265
|
-
z = node;
|
|
266
|
-
}
|
|
267
|
-
if (node && identifier && callback(node) <= identifier) {
|
|
268
|
-
node = node.right;
|
|
269
|
-
}
|
|
270
|
-
else {
|
|
271
|
-
node = node?.left;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
if (z === this._Sentinel) {
|
|
275
|
-
this._size--;
|
|
276
|
-
return;
|
|
277
|
-
}
|
|
278
|
-
y = z;
|
|
279
|
-
let yOriginalColor = y.color;
|
|
280
|
-
if (z.left === this._Sentinel) {
|
|
281
|
-
x = z.right;
|
|
282
|
-
this._rbTransplant(z, z.right);
|
|
283
|
-
}
|
|
284
|
-
else if (z.right === this._Sentinel) {
|
|
285
|
-
x = z.left;
|
|
286
|
-
this._rbTransplant(z, z.left);
|
|
287
|
-
}
|
|
288
|
-
else {
|
|
289
|
-
y = this.getLeftMost(z.right);
|
|
290
|
-
yOriginalColor = y.color;
|
|
291
|
-
x = y.right;
|
|
292
|
-
if (y.parent === z) {
|
|
293
|
-
x.parent = y;
|
|
294
|
-
}
|
|
295
|
-
else {
|
|
296
|
-
this._rbTransplant(y, y.right);
|
|
297
|
-
y.right = z.right;
|
|
298
|
-
y.right.parent = y;
|
|
299
|
-
}
|
|
300
|
-
this._rbTransplant(z, y);
|
|
301
|
-
y.left = z.left;
|
|
302
|
-
y.left.parent = y;
|
|
303
|
-
y.color = z.color;
|
|
304
|
-
}
|
|
305
|
-
if (yOriginalColor === RBTNColor.BLACK) {
|
|
306
|
-
this._fixDelete(x);
|
|
307
|
-
}
|
|
308
|
-
this._size--;
|
|
309
|
-
ans.push({ deleted: z, needBalanced: undefined });
|
|
310
|
-
};
|
|
311
|
-
helper(this.root);
|
|
312
|
-
return ans;
|
|
211
|
+
clear() {
|
|
212
|
+
super.clear();
|
|
213
|
+
this._root = this.SENTINEL;
|
|
313
214
|
}
|
|
314
215
|
/**
|
|
315
216
|
* Time Complexity: O(log n)
|
|
@@ -319,42 +220,33 @@ export class RedBlackTree extends BST {
|
|
|
319
220
|
* Time Complexity: O(log n)
|
|
320
221
|
* Space Complexity: O(1)
|
|
321
222
|
*
|
|
322
|
-
* The function
|
|
323
|
-
*
|
|
324
|
-
* @param
|
|
325
|
-
*
|
|
326
|
-
*
|
|
327
|
-
*
|
|
328
|
-
* @
|
|
329
|
-
*
|
|
330
|
-
* function should take a single parameter of type `NODE` (the type of the nodes in the binary tree) and
|
|
331
|
-
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is the starting point for
|
|
332
|
-
* searching for a node in a binary tree. It can be either a key value or a node object. If it is not
|
|
333
|
-
* provided, the search will start from the root of the binary tree.
|
|
334
|
-
* @param iterationType - The `iterationType` parameter is a variable that determines the type of
|
|
335
|
-
* iteration to be performed when searching for nodes in the binary tree. It is used in the
|
|
336
|
-
* `getNodes` method, which is called within the `getNode` method.
|
|
337
|
-
* @returns a value of type `NODE`, `null`, or `undefined`.
|
|
338
|
-
*/
|
|
339
|
-
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
340
|
-
if (identifier instanceof RedBlackTreeNode)
|
|
341
|
-
callback = (node => node);
|
|
342
|
-
beginRoot = this.ensureNode(beginRoot);
|
|
343
|
-
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
344
|
-
}
|
|
345
|
-
/**
|
|
346
|
-
* Time Complexity: O(1)
|
|
347
|
-
* Space Complexity: O(1)
|
|
348
|
-
*/
|
|
349
|
-
/**
|
|
350
|
-
* Time Complexity: O(1)
|
|
351
|
-
* Space Complexity: O(1)
|
|
352
|
-
*
|
|
353
|
-
* The "clear" function sets the root node to the sentinel node and resets the size to 0.
|
|
223
|
+
* The function adds a new node to a Red-Black Tree data structure and returns a boolean indicating
|
|
224
|
+
* whether the operation was successful.
|
|
225
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
226
|
+
* entry.
|
|
227
|
+
* @param {V} [value] - The `value` parameter is the value associated with the key that is being
|
|
228
|
+
* added to the tree.
|
|
229
|
+
* @returns The method is returning a boolean value. It returns true if the node was successfully
|
|
230
|
+
* added or updated, and false otherwise.
|
|
354
231
|
*/
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
this.
|
|
232
|
+
add(keyOrNodeOrEntry, value) {
|
|
233
|
+
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
|
|
234
|
+
if (!this.isRealNode(newNode))
|
|
235
|
+
return false;
|
|
236
|
+
const insertStatus = this._insert(newNode);
|
|
237
|
+
if (insertStatus === CRUD.CREATED) {
|
|
238
|
+
// Ensure the root is black
|
|
239
|
+
if (this.isRealNode(this._root)) {
|
|
240
|
+
this._root.color = RBTNColor.BLACK;
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
this._size++;
|
|
246
|
+
return true;
|
|
247
|
+
}
|
|
248
|
+
else
|
|
249
|
+
return insertStatus === CRUD.UPDATED;
|
|
358
250
|
}
|
|
359
251
|
/**
|
|
360
252
|
* Time Complexity: O(log n)
|
|
@@ -364,27 +256,73 @@ export class RedBlackTree extends BST {
|
|
|
364
256
|
* Time Complexity: O(log n)
|
|
365
257
|
* Space Complexity: O(1)
|
|
366
258
|
*
|
|
367
|
-
* The function
|
|
368
|
-
*
|
|
369
|
-
*
|
|
370
|
-
*
|
|
259
|
+
* The function `delete` in a binary tree class deletes a node from the tree and fixes the tree if
|
|
260
|
+
* necessary.
|
|
261
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the
|
|
262
|
+
* identifier of the node that needs to be deleted from the binary tree. It can be of any type that
|
|
263
|
+
* is returned by the callback function `C`. It can also be `null` or `undefined` if the node to be
|
|
264
|
+
* deleted is not found.
|
|
265
|
+
* @param {C} callback - The `callback` parameter is a function that is used to retrieve a node from
|
|
266
|
+
* the binary tree based on its identifier. It is an optional parameter and if not provided, the
|
|
267
|
+
* `_defaultOneParamCallback` function is used as the default callback. The callback function should
|
|
268
|
+
* return the identifier of the node to
|
|
269
|
+
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
371
270
|
*/
|
|
372
|
-
|
|
373
|
-
if (
|
|
374
|
-
return
|
|
271
|
+
delete(identifier, callback = this._defaultOneParamCallback) {
|
|
272
|
+
if (identifier === null)
|
|
273
|
+
return [];
|
|
274
|
+
const results = [];
|
|
275
|
+
const nodeToDelete = this.isRealNode(identifier) ? identifier : this.getNode(identifier, callback);
|
|
276
|
+
if (!nodeToDelete) {
|
|
277
|
+
return results;
|
|
278
|
+
}
|
|
279
|
+
let originalColor = nodeToDelete.color;
|
|
280
|
+
let replacementNode;
|
|
281
|
+
if (!this.isRealNode(nodeToDelete.left)) {
|
|
282
|
+
replacementNode = nodeToDelete.right;
|
|
283
|
+
this._transplant(nodeToDelete, nodeToDelete.right);
|
|
375
284
|
}
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
y = y.parent;
|
|
285
|
+
else if (!this.isRealNode(nodeToDelete.right)) {
|
|
286
|
+
replacementNode = nodeToDelete.left;
|
|
287
|
+
this._transplant(nodeToDelete, nodeToDelete.left);
|
|
380
288
|
}
|
|
381
|
-
|
|
289
|
+
else {
|
|
290
|
+
const successor = this.getLeftMost(nodeToDelete.right);
|
|
291
|
+
if (successor) {
|
|
292
|
+
originalColor = successor.color;
|
|
293
|
+
replacementNode = successor.right;
|
|
294
|
+
if (successor.parent === nodeToDelete) {
|
|
295
|
+
if (this.isRealNode(replacementNode)) {
|
|
296
|
+
replacementNode.parent = successor;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
this._transplant(successor, successor.right);
|
|
301
|
+
successor.right = nodeToDelete.right;
|
|
302
|
+
if (this.isRealNode(successor.right)) {
|
|
303
|
+
successor.right.parent = successor;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
this._transplant(nodeToDelete, successor);
|
|
307
|
+
successor.left = nodeToDelete.left;
|
|
308
|
+
if (this.isRealNode(successor.left)) {
|
|
309
|
+
successor.left.parent = successor;
|
|
310
|
+
}
|
|
311
|
+
successor.color = nodeToDelete.color;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
this._size--;
|
|
315
|
+
// If the original color was black, fix the tree
|
|
316
|
+
if (originalColor === RBTNColor.BLACK) {
|
|
317
|
+
this._deleteFixup(replacementNode);
|
|
318
|
+
}
|
|
319
|
+
results.push({ deleted: nodeToDelete, needBalanced: undefined });
|
|
320
|
+
return results;
|
|
382
321
|
}
|
|
383
322
|
/**
|
|
384
|
-
* The function sets the root
|
|
385
|
-
* root
|
|
386
|
-
* @param {NODE} v -
|
|
387
|
-
* structure.
|
|
323
|
+
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
324
|
+
* root.
|
|
325
|
+
* @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
|
|
388
326
|
*/
|
|
389
327
|
_setRoot(v) {
|
|
390
328
|
if (v) {
|
|
@@ -400,30 +338,64 @@ export class RedBlackTree extends BST {
|
|
|
400
338
|
* Time Complexity: O(1)
|
|
401
339
|
* Space Complexity: O(1)
|
|
402
340
|
*
|
|
403
|
-
* The function
|
|
404
|
-
* @param {
|
|
341
|
+
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
342
|
+
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
|
|
343
|
+
* the data structure.
|
|
344
|
+
* @param {NODE} newNode - The `newNode` parameter is the new node that will replace the old node in
|
|
345
|
+
* the data structure.
|
|
346
|
+
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
347
|
+
* superclass, with the `oldNode` and `newNode` parameters.
|
|
405
348
|
*/
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
349
|
+
_replaceNode(oldNode, newNode) {
|
|
350
|
+
newNode.color = oldNode.color;
|
|
351
|
+
return super._replaceNode(oldNode, newNode);
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Time Complexity: O(log n)
|
|
355
|
+
* Space Complexity: O(1)
|
|
356
|
+
*/
|
|
357
|
+
/**
|
|
358
|
+
* Time Complexity: O(log n)
|
|
359
|
+
* Space Complexity: O(1)
|
|
360
|
+
*
|
|
361
|
+
* The `_insert` function inserts or updates a node in a binary search tree and performs necessary
|
|
362
|
+
* fix-ups to maintain the red-black tree properties.
|
|
363
|
+
* @param {NODE} node - The `node` parameter represents the node that needs to be inserted into a
|
|
364
|
+
* binary search tree. It contains a `key` property that is used to determine the position of the
|
|
365
|
+
* node in the tree.
|
|
366
|
+
* @returns {'inserted' | 'updated'} - The result of the insertion.
|
|
367
|
+
*/
|
|
368
|
+
_insert(node) {
|
|
369
|
+
let current = this.root;
|
|
370
|
+
let parent = undefined;
|
|
371
|
+
while (this.isRealNode(current)) {
|
|
372
|
+
parent = current;
|
|
373
|
+
if (node.key < current.key) {
|
|
374
|
+
current = current.left ?? this.SENTINEL;
|
|
417
375
|
}
|
|
418
|
-
else if (
|
|
419
|
-
|
|
376
|
+
else if (node.key > current.key) {
|
|
377
|
+
current = current.right ?? this.SENTINEL;
|
|
420
378
|
}
|
|
421
379
|
else {
|
|
422
|
-
|
|
380
|
+
this._replaceNode(current, node);
|
|
381
|
+
return CRUD.UPDATED;
|
|
423
382
|
}
|
|
424
|
-
y.left = x;
|
|
425
|
-
x.parent = y;
|
|
426
383
|
}
|
|
384
|
+
node.parent = parent;
|
|
385
|
+
if (!parent) {
|
|
386
|
+
this._setRoot(node);
|
|
387
|
+
}
|
|
388
|
+
else if (node.key < parent.key) {
|
|
389
|
+
parent.left = node;
|
|
390
|
+
}
|
|
391
|
+
else {
|
|
392
|
+
parent.right = node;
|
|
393
|
+
}
|
|
394
|
+
node.left = this.SENTINEL;
|
|
395
|
+
node.right = this.SENTINEL;
|
|
396
|
+
node.color = RBTNColor.RED;
|
|
397
|
+
this._insertFixup(node);
|
|
398
|
+
return CRUD.CREATED;
|
|
427
399
|
}
|
|
428
400
|
/**
|
|
429
401
|
* Time Complexity: O(1)
|
|
@@ -433,30 +405,23 @@ export class RedBlackTree extends BST {
|
|
|
433
405
|
* Time Complexity: O(1)
|
|
434
406
|
* Space Complexity: O(1)
|
|
435
407
|
*
|
|
436
|
-
* The function
|
|
437
|
-
* @param {
|
|
438
|
-
*
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
x.parent.right = y;
|
|
454
|
-
}
|
|
455
|
-
else {
|
|
456
|
-
x.parent.left = y;
|
|
457
|
-
}
|
|
458
|
-
y.right = x;
|
|
459
|
-
x.parent = y;
|
|
408
|
+
* The function `_transplant` is used to replace a node `u` with another node `v` in a binary tree.
|
|
409
|
+
* @param {NODE} u - The parameter "u" represents a node in a binary tree.
|
|
410
|
+
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`, which means it can
|
|
411
|
+
* either be a `NODE` object or `undefined`.
|
|
412
|
+
*/
|
|
413
|
+
_transplant(u, v) {
|
|
414
|
+
if (!u.parent) {
|
|
415
|
+
this._setRoot(v);
|
|
416
|
+
}
|
|
417
|
+
else if (u === u.parent.left) {
|
|
418
|
+
u.parent.left = v;
|
|
419
|
+
}
|
|
420
|
+
else {
|
|
421
|
+
u.parent.right = v;
|
|
422
|
+
}
|
|
423
|
+
if (v) {
|
|
424
|
+
v.parent = u.parent;
|
|
460
425
|
}
|
|
461
426
|
}
|
|
462
427
|
/**
|
|
@@ -467,62 +432,67 @@ export class RedBlackTree extends BST {
|
|
|
467
432
|
* Time Complexity: O(log n)
|
|
468
433
|
* Space Complexity: O(1)
|
|
469
434
|
*
|
|
470
|
-
* The `
|
|
471
|
-
* @param {
|
|
472
|
-
*
|
|
473
|
-
*/
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
while (
|
|
477
|
-
if
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
435
|
+
* The `_insertFixup` function is used to fix the Red-Black Tree after inserting a new node.
|
|
436
|
+
* @param {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree. It can
|
|
437
|
+
* either be a valid node object or `undefined`.
|
|
438
|
+
*/
|
|
439
|
+
_insertFixup(z) {
|
|
440
|
+
// Continue fixing the tree as long as the parent of z is red
|
|
441
|
+
while (z?.parent?.color === RBTNColor.RED) {
|
|
442
|
+
// Check if the parent of z is the left child of its parent
|
|
443
|
+
if (z.parent === z.parent.parent?.left) {
|
|
444
|
+
// Case 1: The uncle (y) of z is red
|
|
445
|
+
const y = z.parent.parent.right;
|
|
446
|
+
if (y?.color === RBTNColor.RED) {
|
|
447
|
+
// Set colors to restore properties of Red-Black Tree
|
|
448
|
+
z.parent.color = RBTNColor.BLACK;
|
|
449
|
+
y.color = RBTNColor.BLACK;
|
|
450
|
+
z.parent.parent.color = RBTNColor.RED;
|
|
451
|
+
// Move up the tree to continue fixing
|
|
452
|
+
z = z.parent.parent;
|
|
485
453
|
}
|
|
486
454
|
else {
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
455
|
+
// Case 2: The uncle (y) of z is black, and z is a right child
|
|
456
|
+
if (z === z.parent.right) {
|
|
457
|
+
// Perform a left rotation to transform the case into Case 3
|
|
458
|
+
z = z.parent;
|
|
459
|
+
this._leftRotate(z);
|
|
490
460
|
}
|
|
491
|
-
//
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
461
|
+
// Case 3: The uncle (y) of z is black, and z is a left child
|
|
462
|
+
// Adjust colors and perform a right rotation
|
|
463
|
+
if (z && this.isRealNode(z.parent) && this.isRealNode(z.parent.parent)) {
|
|
464
|
+
z.parent.color = RBTNColor.BLACK;
|
|
465
|
+
z.parent.parent.color = RBTNColor.RED;
|
|
466
|
+
this._rightRotate(z.parent.parent);
|
|
495
467
|
}
|
|
496
|
-
this._leftRotate(k.parent.parent);
|
|
497
468
|
}
|
|
498
469
|
}
|
|
499
470
|
else {
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
471
|
+
// Symmetric case for the right child (left and right exchanged)
|
|
472
|
+
// Follow the same logic as above with left and right exchanged
|
|
473
|
+
const y = z?.parent?.parent?.left;
|
|
474
|
+
if (y?.color === RBTNColor.RED) {
|
|
475
|
+
z.parent.color = RBTNColor.BLACK;
|
|
476
|
+
y.color = RBTNColor.BLACK;
|
|
477
|
+
z.parent.parent.color = RBTNColor.RED;
|
|
478
|
+
z = z.parent.parent;
|
|
507
479
|
}
|
|
508
480
|
else {
|
|
509
|
-
if (
|
|
510
|
-
|
|
511
|
-
this.
|
|
481
|
+
if (z === z.parent.left) {
|
|
482
|
+
z = z.parent;
|
|
483
|
+
this._rightRotate(z);
|
|
512
484
|
}
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
485
|
+
if (z && this.isRealNode(z.parent) && this.isRealNode(z.parent.parent)) {
|
|
486
|
+
z.parent.color = RBTNColor.BLACK;
|
|
487
|
+
z.parent.parent.color = RBTNColor.RED;
|
|
488
|
+
this._leftRotate(z.parent.parent);
|
|
517
489
|
}
|
|
518
|
-
this._rightRotate(k.parent.parent);
|
|
519
490
|
}
|
|
520
491
|
}
|
|
521
|
-
if (k === this.root) {
|
|
522
|
-
break;
|
|
523
|
-
}
|
|
524
492
|
}
|
|
525
|
-
|
|
493
|
+
// Ensure that the root is black after fixing
|
|
494
|
+
if (this.isRealNode(this._root))
|
|
495
|
+
this._root.color = RBTNColor.BLACK;
|
|
526
496
|
}
|
|
527
497
|
/**
|
|
528
498
|
* Time Complexity: O(log n)
|
|
@@ -532,72 +502,86 @@ export class RedBlackTree extends BST {
|
|
|
532
502
|
* Time Complexity: O(log n)
|
|
533
503
|
* Space Complexity: O(1)
|
|
534
504
|
*
|
|
535
|
-
* The
|
|
536
|
-
*
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
505
|
+
* The `_deleteFixup` function is used to fix the red-black tree after a node deletion by adjusting
|
|
506
|
+
* the colors and performing rotations.
|
|
507
|
+
* @param {NODE | undefined} node - The `node` parameter represents a node in a Red-Black Tree data
|
|
508
|
+
* structure. It can be either a valid node object or `undefined`.
|
|
509
|
+
* @returns The function does not return any value. It has a return type of `void`.
|
|
510
|
+
*/
|
|
511
|
+
_deleteFixup(node) {
|
|
512
|
+
// Early exit condition
|
|
513
|
+
if (!node || node === this.root || node.color === RBTNColor.BLACK) {
|
|
514
|
+
if (node) {
|
|
515
|
+
node.color = RBTNColor.BLACK; // Ensure the final node is black
|
|
516
|
+
}
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
while (node && node !== this.root && node.color === RBTNColor.BLACK) {
|
|
520
|
+
const parent = node.parent;
|
|
521
|
+
if (!parent) {
|
|
522
|
+
break; // Ensure the loop terminates if there's an issue with the tree structure
|
|
523
|
+
}
|
|
524
|
+
if (node === parent.left) {
|
|
525
|
+
let sibling = parent.right;
|
|
526
|
+
// Cases 1 and 2: Sibling is red or both children of sibling are black
|
|
527
|
+
if (sibling?.color === RBTNColor.RED) {
|
|
528
|
+
sibling.color = RBTNColor.BLACK;
|
|
529
|
+
parent.color = RBTNColor.RED;
|
|
530
|
+
this._leftRotate(parent);
|
|
531
|
+
sibling = parent.right;
|
|
548
532
|
}
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
533
|
+
// Case 3: Sibling's left child is black
|
|
534
|
+
if ((sibling?.left?.color ?? RBTNColor.BLACK) === RBTNColor.BLACK) {
|
|
535
|
+
if (sibling)
|
|
536
|
+
sibling.color = RBTNColor.RED;
|
|
537
|
+
node = parent;
|
|
552
538
|
}
|
|
553
539
|
else {
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
s.color = x.parent.color;
|
|
563
|
-
x.parent.color = RBTNColor.BLACK;
|
|
564
|
-
if (s && s.right)
|
|
565
|
-
s.right.color = RBTNColor.BLACK;
|
|
566
|
-
this._leftRotate(x.parent);
|
|
567
|
-
x = this.root;
|
|
540
|
+
// Case 4: Adjust colors and perform a right rotation
|
|
541
|
+
if (sibling?.left)
|
|
542
|
+
sibling.left.color = RBTNColor.BLACK;
|
|
543
|
+
if (sibling)
|
|
544
|
+
sibling.color = parent.color;
|
|
545
|
+
parent.color = RBTNColor.BLACK;
|
|
546
|
+
this._rightRotate(parent);
|
|
547
|
+
node = this.root;
|
|
568
548
|
}
|
|
569
549
|
}
|
|
570
550
|
else {
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
551
|
+
// Symmetric case for the right child (left and right exchanged)
|
|
552
|
+
let sibling = parent.left;
|
|
553
|
+
// Cases 1 and 2: Sibling is red or both children of sibling are black
|
|
554
|
+
if (sibling?.color === RBTNColor.RED) {
|
|
555
|
+
sibling.color = RBTNColor.BLACK;
|
|
556
|
+
if (parent)
|
|
557
|
+
parent.color = RBTNColor.RED;
|
|
558
|
+
this._rightRotate(parent);
|
|
559
|
+
if (parent)
|
|
560
|
+
sibling = parent.left;
|
|
577
561
|
}
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
562
|
+
// Case 3: Sibling's left child is black
|
|
563
|
+
if ((sibling?.right?.color ?? RBTNColor.BLACK) === RBTNColor.BLACK) {
|
|
564
|
+
if (sibling)
|
|
565
|
+
sibling.color = RBTNColor.RED;
|
|
566
|
+
node = parent;
|
|
581
567
|
}
|
|
582
568
|
else {
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
x.parent.color = RBTNColor.BLACK;
|
|
593
|
-
if (s && s.left)
|
|
594
|
-
s.left.color = RBTNColor.BLACK;
|
|
595
|
-
this._rightRotate(x.parent);
|
|
596
|
-
x = this.root;
|
|
569
|
+
// Case 4: Adjust colors and perform a left rotation
|
|
570
|
+
if (sibling?.right)
|
|
571
|
+
sibling.right.color = RBTNColor.BLACK;
|
|
572
|
+
if (sibling)
|
|
573
|
+
sibling.color = parent.color;
|
|
574
|
+
if (parent)
|
|
575
|
+
parent.color = RBTNColor.BLACK;
|
|
576
|
+
this._leftRotate(parent);
|
|
577
|
+
node = this.root;
|
|
597
578
|
}
|
|
598
579
|
}
|
|
599
580
|
}
|
|
600
|
-
|
|
581
|
+
// Ensure that the final node (possibly the root) is black
|
|
582
|
+
if (node) {
|
|
583
|
+
node.color = RBTNColor.BLACK;
|
|
584
|
+
}
|
|
601
585
|
}
|
|
602
586
|
/**
|
|
603
587
|
* Time Complexity: O(1)
|
|
@@ -607,33 +591,66 @@ export class RedBlackTree extends BST {
|
|
|
607
591
|
* Time Complexity: O(1)
|
|
608
592
|
* Space Complexity: O(1)
|
|
609
593
|
*
|
|
610
|
-
* The
|
|
611
|
-
* @param {
|
|
612
|
-
*
|
|
594
|
+
* The `_leftRotate` function performs a left rotation on a given node in a binary tree.
|
|
595
|
+
* @param {NODE | undefined} x - The parameter `x` is of type `NODE | undefined`. It represents a
|
|
596
|
+
* node in a binary tree or `undefined` if there is no node.
|
|
597
|
+
* @returns void, which means it does not return any value.
|
|
613
598
|
*/
|
|
614
|
-
|
|
615
|
-
if (
|
|
616
|
-
|
|
599
|
+
_leftRotate(x) {
|
|
600
|
+
if (!x || !x.right) {
|
|
601
|
+
return;
|
|
617
602
|
}
|
|
618
|
-
|
|
619
|
-
|
|
603
|
+
const y = x.right;
|
|
604
|
+
x.right = y.left;
|
|
605
|
+
if (this.isRealNode(y.left)) {
|
|
606
|
+
y.left.parent = x;
|
|
607
|
+
}
|
|
608
|
+
y.parent = x.parent;
|
|
609
|
+
if (!x.parent) {
|
|
610
|
+
this._setRoot(y);
|
|
611
|
+
}
|
|
612
|
+
else if (x === x.parent.left) {
|
|
613
|
+
x.parent.left = y;
|
|
620
614
|
}
|
|
621
615
|
else {
|
|
622
|
-
|
|
616
|
+
x.parent.right = y;
|
|
623
617
|
}
|
|
624
|
-
|
|
618
|
+
y.left = x;
|
|
619
|
+
x.parent = y;
|
|
625
620
|
}
|
|
626
621
|
/**
|
|
627
|
-
*
|
|
628
|
-
*
|
|
629
|
-
* data structure. It is of type `NODE`, which is the type of the nodes in the data structure.
|
|
630
|
-
* @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
631
|
-
* data structure.
|
|
632
|
-
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
633
|
-
* superclass, passing in the `oldNode` and `newNode` as arguments.
|
|
622
|
+
* Time Complexity: O(1)
|
|
623
|
+
* Space Complexity: O(1)
|
|
634
624
|
*/
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
625
|
+
/**
|
|
626
|
+
* Time Complexity: O(1)
|
|
627
|
+
* Space Complexity: O(1)
|
|
628
|
+
*
|
|
629
|
+
* The `_rightRotate` function performs a right rotation on a given node in a binary tree.
|
|
630
|
+
* @param {NODE | undefined} y - The parameter `y` is of type `NODE | undefined`. It represents a
|
|
631
|
+
* node in a binary tree or `undefined` if there is no node.
|
|
632
|
+
* @returns void, which means it does not return any value.
|
|
633
|
+
*/
|
|
634
|
+
_rightRotate(y) {
|
|
635
|
+
if (!y || !y.left) {
|
|
636
|
+
return;
|
|
637
|
+
}
|
|
638
|
+
const x = y.left;
|
|
639
|
+
y.left = x.right;
|
|
640
|
+
if (this.isRealNode(x.right)) {
|
|
641
|
+
x.right.parent = y;
|
|
642
|
+
}
|
|
643
|
+
x.parent = y.parent;
|
|
644
|
+
if (!y.parent) {
|
|
645
|
+
this._setRoot(x);
|
|
646
|
+
}
|
|
647
|
+
else if (y === y.parent.left) {
|
|
648
|
+
y.parent.left = x;
|
|
649
|
+
}
|
|
650
|
+
else {
|
|
651
|
+
y.parent.right = x;
|
|
652
|
+
}
|
|
653
|
+
x.right = y;
|
|
654
|
+
y.parent = x;
|
|
638
655
|
}
|
|
639
656
|
}
|