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.
Files changed (47) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +13 -18
  3. package/benchmark/report.html +13 -13
  4. package/benchmark/report.json +167 -143
  5. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -0
  6. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +3 -0
  7. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  9. package/dist/cjs/data-structures/binary-tree/binary-tree.js +32 -28
  10. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/bst.js +17 -17
  12. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  13. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +158 -141
  14. package/dist/cjs/data-structures/binary-tree/rb-tree.js +416 -396
  15. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  16. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +1 -0
  17. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +84 -76
  18. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  19. package/dist/cjs/types/common.d.ts +6 -0
  20. package/dist/cjs/types/common.js +8 -1
  21. package/dist/cjs/types/common.js.map +1 -1
  22. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -0
  23. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +3 -0
  24. package/dist/mjs/data-structures/binary-tree/binary-tree.js +32 -28
  25. package/dist/mjs/data-structures/binary-tree/bst.js +17 -17
  26. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +158 -141
  27. package/dist/mjs/data-structures/binary-tree/rb-tree.js +413 -396
  28. package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +1 -0
  29. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +84 -76
  30. package/dist/mjs/types/common.d.ts +6 -0
  31. package/dist/mjs/types/common.js +7 -0
  32. package/dist/umd/data-structure-typed.js +519 -467
  33. package/dist/umd/data-structure-typed.min.js +2 -2
  34. package/dist/umd/data-structure-typed.min.js.map +1 -1
  35. package/package.json +1 -1
  36. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +5 -1
  37. package/src/data-structures/binary-tree/avl-tree.ts +1 -1
  38. package/src/data-structures/binary-tree/binary-tree.ts +31 -29
  39. package/src/data-structures/binary-tree/bst.ts +18 -18
  40. package/src/data-structures/binary-tree/rb-tree.ts +436 -405
  41. package/src/data-structures/binary-tree/tree-multi-map.ts +85 -82
  42. package/src/types/common.ts +7 -0
  43. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +26 -16
  44. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +4 -1
  45. package/test/unit/data-structures/binary-tree/overall.test.ts +23 -21
  46. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +387 -324
  47. 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 protected variable `_color`.
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, which
52
- * initializes the tree with optional nodes and options.
53
- * @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, NODE>`
54
- * objects. It represents the initial nodes that will be added to the RBTree during its
55
- * construction. If this parameter is provided, the `addMany` method is called to add all the
56
- * nodes to the
57
- * @param [options] - The `options` parameter is an optional object that allows you to customize the
58
- * behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
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._Sentinel;
64
- if (keysOrNodesOrEntries)
65
- super.addMany(keysOrNodesOrEntries);
48
+ this._root = this.SENTINEL;
49
+ if (keysOrNodesOrEntries) {
50
+ this.addMany(keysOrNodesOrEntries);
51
+ }
66
52
  }
67
- _Sentinel = new RedBlackTreeNode(NaN);
53
+ _SENTINEL = new RedBlackTreeNode(NaN);
68
54
  /**
69
- * The function returns the value of the `_Sentinel` property.
70
- * @returns The method is returning the value of the `_Sentinel` property.
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 Sentinel() {
73
- return this._Sentinel;
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 data structure.
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 is the key value associated with the node. It is used to
94
- * identify and compare nodes in the Red-Black Tree.
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 of type `V`, which is a generic type that can be replaced with any
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 can be either "RED" or "BLACK". By default, the color is set to "BLACK".
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 specified options and returns it.
108
- * @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
109
- * passed to the `createTree` function. It is used to customize the behavior of the `RedBlackTree`
110
- * class.
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
- * The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible.
121
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, where:
122
- * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
123
- * `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If a value
124
- * is provided, it will be used when creating the new node. If no value is provided, the new node
125
- * @returns a node of type NODE or undefined.
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
- * The function checks if an keyOrNodeOrEntry is an instance of the RedBlackTreeNode class.
154
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
155
- * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the RedBlackTreeNode
156
- * class.
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._Sentinel || node === undefined)
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 `add` function adds a new node to a binary search tree and performs necessary rotations and
182
- * color changes to maintain the red-black tree properties.
183
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
184
- * entry.
185
- * @param {V} [value] - The `value` parameter represents the value associated with the key that is
186
- * being added to the binary search tree.
187
- * @returns The method `add` returns either the newly added node (`NODE`) or `undefined`.
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
- add(keyOrNodeOrEntry, value) {
190
- const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
191
- if (newNode === undefined)
192
- return false;
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(log n)
201
+ * Time Complexity: O(1)
239
202
  * Space Complexity: O(1)
240
203
  */
241
204
  /**
242
- * Time Complexity: O(log n)
205
+ * Time Complexity: O(1)
243
206
  * Space Complexity: O(1)
244
207
  *
245
- * The `delete` function removes a node from a binary tree based on a given identifier and updates
246
- * the tree accordingly.
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
- delete(identifier, callback = this._defaultOneParamCallback) {
257
- const ans = [];
258
- if (identifier === null)
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 `getNode` retrieves a single node from a binary tree based on a given identifier and
323
- * callback function.
324
- * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value used to
325
- * identify the node you want to retrieve. It can be of any type that is the return type of the `C`
326
- * callback function. If the `identifier` is `undefined`, it means you want to retrieve the first
327
- * node that matches the other criteria
328
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
329
- * the binary tree. It is used to determine if a node matches the given identifier. The `callback`
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
- clear() {
356
- this._root = this._Sentinel;
357
- this._size = 0;
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 returns the predecessor of a given node in a red-black tree.
368
- * @param {RedBlackTreeNode} x - The parameter `x` is of type `RedBlackTreeNode`, which represents a node in a
369
- * Red-Black Tree.
370
- * @returns the predecessor of the given RedBlackTreeNode 'x'.
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
- getPredecessor(x) {
373
- if (this.isRealNode(x.left)) {
374
- return this.getRightMost(x.left);
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
- let y = x.parent;
377
- while (this.isRealNode(y) && x === y.left) {
378
- x = y;
379
- y = y.parent;
285
+ else if (!this.isRealNode(nodeToDelete.right)) {
286
+ replacementNode = nodeToDelete.left;
287
+ this._transplant(nodeToDelete, nodeToDelete.left);
380
288
  }
381
- return y;
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 node of a tree structure and updates the parent property of the new
385
- * root node.
386
- * @param {NODE} v - The parameter "v" is of type "NODE", which represents a node in a data
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 performs a left rotation on a binary tree node.
404
- * @param {RedBlackTreeNode} x - The parameter `x` is of type `NODE`, which likely represents a node in a binary tree.
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
- _leftRotate(x) {
407
- if (x.right) {
408
- const y = x.right;
409
- x.right = y.left;
410
- if (y.left !== this._Sentinel) {
411
- if (y.left)
412
- y.left.parent = x;
413
- }
414
- y.parent = x.parent;
415
- if (x.parent === undefined) {
416
- this._setRoot(y);
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 (x === x.parent.left) {
419
- x.parent.left = y;
376
+ else if (node.key > current.key) {
377
+ current = current.right ?? this.SENTINEL;
420
378
  }
421
379
  else {
422
- x.parent.right = y;
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 performs a right rotation on a red-black tree node.
437
- * @param {RedBlackTreeNode} x - x is a RedBlackTreeNode, which represents the node that needs to be right
438
- * rotated.
439
- */
440
- _rightRotate(x) {
441
- if (x.left) {
442
- const y = x.left;
443
- x.left = y.right;
444
- if (y.right !== this._Sentinel) {
445
- if (y.right)
446
- y.right.parent = x;
447
- }
448
- y.parent = x.parent;
449
- if (x.parent === undefined) {
450
- this._setRoot(y);
451
- }
452
- else if (x === x.parent.right) {
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 `_fixInsert` function is used to fix the red-black tree after an insertion operation.
471
- * @param {RedBlackTreeNode} k - The parameter `k` is a RedBlackTreeNode object, which represents a node in a
472
- * red-black tree.
473
- */
474
- _fixInsert(k) {
475
- let u;
476
- while (k.parent && k.parent.color === RBTNColor.RED) {
477
- if (k.parent.parent && k.parent === k.parent.parent.right) {
478
- u = k.parent.parent.left;
479
- if (u && u.color === RBTNColor.RED) {
480
- // Delay color flip
481
- k.parent.color = RBTNColor.BLACK;
482
- u.color = RBTNColor.BLACK;
483
- k.parent.parent.color = RBTNColor.RED;
484
- k = k.parent.parent;
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
- if (k === k.parent.left) {
488
- k = k.parent;
489
- this._rightRotate(k);
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
- // Check color before rotation
492
- if (k.parent.color === RBTNColor.RED) {
493
- k.parent.color = RBTNColor.BLACK;
494
- k.parent.parent.color = RBTNColor.RED;
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
- u = k.parent.parent.right;
501
- if (u && u.color === RBTNColor.RED) {
502
- // Delay color flip
503
- k.parent.color = RBTNColor.BLACK;
504
- u.color = RBTNColor.BLACK;
505
- k.parent.parent.color = RBTNColor.RED;
506
- k = k.parent.parent;
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 (k === k.parent.right) {
510
- k = k.parent;
511
- this._leftRotate(k);
481
+ if (z === z.parent.left) {
482
+ z = z.parent;
483
+ this._rightRotate(z);
512
484
  }
513
- // Check color before rotation
514
- if (k.parent.color === RBTNColor.RED) {
515
- k.parent.color = RBTNColor.BLACK;
516
- k.parent.parent.color = RBTNColor.RED;
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
- this.root.color = RBTNColor.BLACK;
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 function `_fixDelete` is used to fix the red-black tree after a node deletion.
536
- * @param {RedBlackTreeNode} x - The parameter `x` represents a node in a Red-Black Tree (RBT).
537
- */
538
- _fixDelete(x) {
539
- let s;
540
- while (x !== this.root && x.color === RBTNColor.BLACK) {
541
- if (x.parent && x === x.parent.left) {
542
- s = x.parent.right;
543
- if (s.color === 1) {
544
- s.color = RBTNColor.BLACK;
545
- x.parent.color = RBTNColor.RED;
546
- this._leftRotate(x.parent);
547
- s = x.parent.right;
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
- if (s.left !== undefined && s.left.color === RBTNColor.BLACK && s.right && s.right.color === RBTNColor.BLACK) {
550
- s.color = RBTNColor.RED;
551
- x = x.parent;
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
- if (s.right && s.right.color === RBTNColor.BLACK) {
555
- if (s.left)
556
- s.left.color = RBTNColor.BLACK;
557
- s.color = RBTNColor.RED;
558
- this._rightRotate(s);
559
- s = x.parent.right;
560
- }
561
- if (s)
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
- s = x.parent.left;
572
- if (s.color === 1) {
573
- s.color = RBTNColor.BLACK;
574
- x.parent.color = RBTNColor.RED;
575
- this._rightRotate(x.parent);
576
- s = x.parent.left;
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
- if (s && s.right && s.right.color === RBTNColor.BLACK && s.right.color === RBTNColor.BLACK) {
579
- s.color = RBTNColor.RED;
580
- x = x.parent;
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
- if (s && s.left && s.left.color === RBTNColor.BLACK) {
584
- if (s.right)
585
- s.right.color = RBTNColor.BLACK;
586
- s.color = RBTNColor.RED;
587
- this._leftRotate(s);
588
- s = x.parent.left;
589
- }
590
- if (s)
591
- s.color = x.parent.color;
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
- x.color = RBTNColor.BLACK;
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 function `_rbTransplant` replaces one node in a red-black tree with another node.
611
- * @param {RedBlackTreeNode} u - The parameter "u" represents a RedBlackTreeNode object.
612
- * @param {RedBlackTreeNode} v - The parameter "v" is a RedBlackTreeNode object.
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
- _rbTransplant(u, v) {
615
- if (u.parent === undefined) {
616
- this._setRoot(v);
599
+ _leftRotate(x) {
600
+ if (!x || !x.right) {
601
+ return;
617
602
  }
618
- else if (u === u.parent.left) {
619
- u.parent.left = v;
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
- u.parent.right = v;
616
+ x.parent.right = y;
623
617
  }
624
- v.parent = u.parent;
618
+ y.left = x;
619
+ x.parent = y;
625
620
  }
626
621
  /**
627
- * The function replaces an old node with a new node while preserving the color of the old node.
628
- * @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in a
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
- _replaceNode(oldNode, newNode) {
636
- newNode.color = oldNode.color;
637
- return super._replaceNode(oldNode, newNode);
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
  }