deque-typed 1.54.0 → 1.54.1

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 (84) hide show
  1. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
  2. package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
  3. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -177
  4. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -340
  5. package/dist/data-structures/binary-tree/avl-tree.d.ts +102 -57
  6. package/dist/data-structures/binary-tree/avl-tree.js +110 -47
  7. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
  8. package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
  9. package/dist/data-structures/binary-tree/binary-tree.d.ts +240 -190
  10. package/dist/data-structures/binary-tree/binary-tree.js +269 -240
  11. package/dist/data-structures/binary-tree/bst.d.ts +145 -112
  12. package/dist/data-structures/binary-tree/bst.js +180 -129
  13. package/dist/data-structures/binary-tree/index.d.ts +2 -0
  14. package/dist/data-structures/binary-tree/index.js +2 -0
  15. package/dist/data-structures/binary-tree/red-black-tree.d.ts +100 -82
  16. package/dist/data-structures/binary-tree/red-black-tree.js +115 -79
  17. package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
  18. package/dist/data-structures/binary-tree/tree-counter.js +444 -0
  19. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -174
  20. package/dist/data-structures/binary-tree/tree-multi-map.js +142 -377
  21. package/dist/data-structures/graph/directed-graph.d.ts +3 -0
  22. package/dist/data-structures/graph/directed-graph.js +3 -0
  23. package/dist/data-structures/graph/map-graph.d.ts +3 -0
  24. package/dist/data-structures/graph/map-graph.js +3 -0
  25. package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
  26. package/dist/data-structures/graph/undirected-graph.js +3 -0
  27. package/dist/data-structures/linked-list/singly-linked-list.d.ts +3 -0
  28. package/dist/data-structures/linked-list/singly-linked-list.js +3 -0
  29. package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
  30. package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
  31. package/dist/data-structures/matrix/matrix.d.ts +3 -0
  32. package/dist/data-structures/matrix/matrix.js +3 -0
  33. package/dist/data-structures/matrix/navigator.d.ts +3 -0
  34. package/dist/data-structures/matrix/navigator.js +3 -0
  35. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
  36. package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
  37. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
  38. package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
  39. package/dist/data-structures/trie/trie.d.ts +0 -4
  40. package/dist/data-structures/trie/trie.js +0 -4
  41. package/dist/interfaces/binary-tree.d.ts +8 -8
  42. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
  43. package/dist/types/data-structures/binary-tree/avl-tree-counter.js +2 -0
  44. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -4
  45. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -3
  46. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -3
  47. package/dist/types/data-structures/binary-tree/bst.d.ts +3 -3
  48. package/dist/types/data-structures/binary-tree/index.d.ts +2 -0
  49. package/dist/types/data-structures/binary-tree/index.js +2 -0
  50. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -4
  51. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
  52. package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
  53. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -4
  54. package/package.json +2 -2
  55. package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
  56. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +155 -393
  57. package/src/data-structures/binary-tree/avl-tree.ts +144 -93
  58. package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
  59. package/src/data-structures/binary-tree/binary-tree.ts +433 -405
  60. package/src/data-structures/binary-tree/bst.ts +261 -239
  61. package/src/data-structures/binary-tree/index.ts +2 -0
  62. package/src/data-structures/binary-tree/red-black-tree.ts +163 -134
  63. package/src/data-structures/binary-tree/tree-counter.ts +504 -0
  64. package/src/data-structures/binary-tree/tree-multi-map.ts +161 -429
  65. package/src/data-structures/graph/directed-graph.ts +3 -0
  66. package/src/data-structures/graph/map-graph.ts +3 -0
  67. package/src/data-structures/graph/undirected-graph.ts +3 -0
  68. package/src/data-structures/linked-list/singly-linked-list.ts +3 -0
  69. package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
  70. package/src/data-structures/matrix/matrix.ts +3 -0
  71. package/src/data-structures/matrix/navigator.ts +3 -0
  72. package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
  73. package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
  74. package/src/data-structures/trie/trie.ts +0 -4
  75. package/src/interfaces/binary-tree.ts +10 -24
  76. package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
  77. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -6
  78. package/src/types/data-structures/binary-tree/avl-tree.ts +0 -5
  79. package/src/types/data-structures/binary-tree/binary-tree.ts +0 -5
  80. package/src/types/data-structures/binary-tree/bst.ts +5 -5
  81. package/src/types/data-structures/binary-tree/index.ts +2 -0
  82. package/src/types/data-structures/binary-tree/rb-tree.ts +1 -6
  83. package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
  84. package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -6
@@ -6,3 +6,5 @@ export * from './avl-tree';
6
6
  export * from './red-black-tree';
7
7
  export * from './avl-tree-multi-map';
8
8
  export * from './tree-multi-map';
9
+ export * from './tree-counter';
10
+ export * from './avl-tree-counter';
@@ -4,34 +4,56 @@ import type {
4
4
  CRUD,
5
5
  EntryCallback,
6
6
  OptNode,
7
+ OptNodeOrNull,
7
8
  RBTNColor,
8
- RedBlackTreeOptions,
9
- RedBlackTreeNested,
10
- RedBlackTreeNodeNested
9
+ RedBlackTreeOptions
11
10
  } from '../../types';
12
11
  import { BST, BSTNode } from './bst';
13
12
  import { IBinaryTree } from '../../interfaces';
14
13
 
15
- export class RedBlackTreeNode<
16
- K = any,
17
- V = any,
18
- NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>
19
- > extends BSTNode<K, V, NODE> {
14
+ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
20
15
  /**
21
- * The constructor function initializes a Red-Black Tree Node with a key, an optional value, and a
22
- * color.
23
- * @param {K} key - The key parameter is of type K and represents the key of the node in the
24
- * Red-Black Tree.
25
- * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
26
- * associated with the key in the Red-Black Tree Node. It is not required and can be omitted when
27
- * creating a new instance of the Red-Black Tree Node.
28
- * @param {RBTNColor} color - The `color` parameter is used to specify the color of the Red-Black
29
- * Tree Node. It is an optional parameter with a default value of `'BLACK'`.
16
+ * The constructor initializes a node with a key, value, and color for a Red-Black Tree.
17
+ * @param {K} key - The `key` parameter is a key of type `K` that is used to identify the node in a
18
+ * Red-Black Tree data structure.
19
+ * @param {V} [value] - The `value` parameter in the constructor is an optional parameter of type
20
+ * `V`. It represents the value associated with the key in the data structure being constructed.
21
+ * @param {RBTNColor} [color=BLACK] - The `color` parameter in the constructor is used to specify the
22
+ * color of the node in a Red-Black Tree. It has a default value of 'BLACK' if not provided
23
+ * explicitly.
30
24
  */
31
25
  constructor(key: K, value?: V, color: RBTNColor = 'BLACK') {
32
26
  super(key, value);
33
27
  this._color = color;
34
28
  }
29
+
30
+ override parent?: RedBlackTreeNode<K, V> = undefined;
31
+
32
+ override _left?: OptNodeOrNull<RedBlackTreeNode<K, V>> = undefined;
33
+
34
+ override get left(): OptNodeOrNull<RedBlackTreeNode<K, V>> {
35
+ return this._left;
36
+ }
37
+
38
+ override set left(v: OptNodeOrNull<RedBlackTreeNode<K, V>>) {
39
+ if (v) {
40
+ v.parent = this;
41
+ }
42
+ this._left = v;
43
+ }
44
+
45
+ override _right?: OptNodeOrNull<RedBlackTreeNode<K, V>> = undefined;
46
+
47
+ override get right(): OptNodeOrNull<RedBlackTreeNode<K, V>> {
48
+ return this._right;
49
+ }
50
+
51
+ override set right(v: OptNodeOrNull<RedBlackTreeNode<K, V>>) {
52
+ if (v) {
53
+ v.parent = this;
54
+ }
55
+ this._right = v;
56
+ }
35
57
  }
36
58
 
37
59
  /**
@@ -87,39 +109,25 @@ export class RedBlackTreeNode<
87
109
  * );
88
110
  * console.log(stocksInRange); // ['GOOGL', 'MSFT', 'META']
89
111
  */
90
- export class RedBlackTree<
91
- K = any,
92
- V = any,
93
- R = object,
94
- MK = any,
95
- MV = any,
96
- MR = object,
97
- NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>,
98
- TREE extends RedBlackTree<K, V, R, MK, MV, MR, NODE, TREE> = RedBlackTree<
99
- K,
100
- V,
101
- R,
102
- MK,
103
- MV,
104
- MR,
105
- NODE,
106
- RedBlackTreeNested<K, V, R, MK, MV, MR, NODE>
107
- >
108
- >
109
- extends BST<K, V, R, MK, MV, MR, NODE, TREE>
110
- implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE>
112
+ export class RedBlackTree<K = any, V = any, R = object, MK = any, MV = any, MR = object>
113
+ extends BST<K, V, R, MK, MV, MR>
114
+ implements IBinaryTree<K, V, R, MK, MV, MR>
111
115
  {
112
116
  /**
113
- * This is the constructor function for a Red-Black Tree data structure in TypeScript.
114
- * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
115
- * iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
116
- * initialize the RBTree with the provided elements.
117
- * @param [options] - The `options` parameter is an optional object that can be passed to the
118
- * constructor. It is of type `RedBlackTreeOptions<K, V, R>`. This object can contain various options for
119
- * configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
120
- * depend on the implementation
117
+ * This TypeScript constructor initializes a Red-Black Tree with optional keys, nodes, entries, or
118
+ * raw data.
119
+ * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
120
+ * iterable that can contain either `BTNRep<K, V, RedBlackTreeNode<K, V>>` objects or `R` objects. It
121
+ * is used to initialize the Red-Black Tree with keys, nodes, entries, or
122
+ * @param [options] - The `options` parameter in the constructor is of type `RedBlackTreeOptions<K,
123
+ * V, R>`. It is an optional parameter that allows you to specify additional options for the
124
+ * RedBlackTree class. These options could include configuration settings, behavior customization, or
125
+ * any other parameters that are specific to
121
126
  */
122
- constructor(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>> = [], options?: RedBlackTreeOptions<K, V, R>) {
127
+ constructor(
128
+ keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, RedBlackTreeNode<K, V>> | R> = [],
129
+ options?: RedBlackTreeOptions<K, V, R>
130
+ ) {
123
131
  super([], options);
124
132
 
125
133
  this._root = this.NIL;
@@ -129,17 +137,16 @@ export class RedBlackTree<
129
137
  }
130
138
  }
131
139
 
132
- protected override _root: NODE | undefined;
140
+ protected override _root: RedBlackTreeNode<K, V> | undefined;
133
141
 
134
- /**
135
- * The function returns the root node of a tree or undefined if there is no root.
136
- * @returns The root node of the tree structure, or undefined if there is no root node.
137
- */
138
- override get root(): NODE | undefined {
142
+ override get root(): RedBlackTreeNode<K, V> | undefined {
139
143
  return this._root;
140
144
  }
141
145
 
142
146
  /**
147
+ * Time Complexity: O(1)
148
+ * Space Complexity: O(1)
149
+ *
143
150
  * The function creates a new Red-Black Tree node with the specified key, value, and color.
144
151
  * @param {K} key - The key parameter represents the key value of the node being created. It is of
145
152
  * type K, which is a generic type that can be replaced with any specific type when using the
@@ -153,24 +160,27 @@ export class RedBlackTree<
153
160
  * @returns A new instance of a RedBlackTreeNode with the specified key, value, and color is being
154
161
  * returned.
155
162
  */
156
- override createNode(key: K, value?: V, color: RBTNColor = 'BLACK'): NODE {
157
- return new RedBlackTreeNode<K, V, NODE>(key, this._isMapMode ? undefined : value, color) as NODE;
163
+ override createNode(key: K, value?: V, color: RBTNColor = 'BLACK'): RedBlackTreeNode<K, V> {
164
+ return new RedBlackTreeNode<K, V>(key, this._isMapMode ? undefined : value, color);
158
165
  }
159
166
 
160
167
  /**
168
+ * Time Complexity: O(1)
169
+ * Space Complexity: O(1)
170
+ *
161
171
  * The function creates a new Red-Black Tree with the specified options.
162
172
  * @param [options] - The `options` parameter is an optional object that contains additional
163
173
  * configuration options for creating the Red-Black Tree. It has the following properties:
164
174
  * @returns a new instance of a RedBlackTree object.
165
175
  */
166
- override createTree(options?: RedBlackTreeOptions<K, V, R>): TREE {
167
- return new RedBlackTree<K, V, R, MK, MV, MR, NODE, TREE>([], {
176
+ override createTree(options?: RedBlackTreeOptions<K, V, R>) {
177
+ return new RedBlackTree<K, V, R, MK, MV, MR>([], {
168
178
  iterationType: this.iterationType,
169
179
  isMapMode: this._isMapMode,
170
180
  specifyComparable: this._specifyComparable,
171
181
  toEntryFn: this._toEntryFn,
172
182
  ...options
173
- }) as TREE;
183
+ });
174
184
  }
175
185
 
176
186
  /**
@@ -178,13 +188,13 @@ export class RedBlackTree<
178
188
  * Space Complexity: O(1)
179
189
  *
180
190
  * The function checks if the input is an instance of the RedBlackTreeNode class.
181
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
182
- * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
183
- * @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
191
+ * @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The parameter
192
+ * `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, RedBlackTreeNode<K, V>>`.
193
+ * @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
184
194
  * an instance of the `RedBlackTreeNode` class.
185
195
  */
186
- override isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE {
187
- return keyNodeEntryOrRaw instanceof RedBlackTreeNode;
196
+ override isNode(keyNodeOrEntry: BTNRep<K, V, RedBlackTreeNode<K, V>>): keyNodeOrEntry is RedBlackTreeNode<K, V> {
197
+ return keyNodeOrEntry instanceof RedBlackTreeNode;
188
198
  }
189
199
 
190
200
  /**
@@ -201,12 +211,12 @@ export class RedBlackTree<
201
211
 
202
212
  /**
203
213
  * Time Complexity: O(log n)
204
- * Space Complexity: O(1)
214
+ * Space Complexity: O(log n)
205
215
  *
206
216
  * The function adds a new node to a binary search tree and returns true if the node was successfully
207
217
  * added.
208
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
209
- * `keyNodeEntryOrRaw` can accept a value of type `R` or `BTNRep<K, V, NODE>`.
218
+ * @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The parameter
219
+ * `keyNodeOrEntry` can accept a value of type `R` or `BTNRep<K, V, RedBlackTreeNode<K, V>>`.
210
220
  * @param {V} [value] - The `value` parameter is an optional value that you want to associate with
211
221
  * the key in the data structure. It represents the value that you want to add or update in the data
212
222
  * structure.
@@ -214,8 +224,8 @@ export class RedBlackTree<
214
224
  * the method returns true. If the node already exists and its value is updated, the method also
215
225
  * returns true. If the node cannot be added or updated, the method returns false.
216
226
  */
217
- override add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): boolean {
218
- const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
227
+ override add(keyNodeOrEntry: BTNRep<K, V, RedBlackTreeNode<K, V>>, value?: V): boolean {
228
+ const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
219
229
  if (!this.isRealNode(newNode)) return false;
220
230
 
221
231
  const insertStatus = this._insert(newNode);
@@ -240,32 +250,34 @@ export class RedBlackTree<
240
250
 
241
251
  /**
242
252
  * Time Complexity: O(log n)
243
- * Space Complexity: O(1)
253
+ * Space Complexity: O(log n)
244
254
  *
245
255
  * The function overrides the delete method in a binary tree data structure to remove a node based on
246
256
  * a given predicate and maintain the binary search tree properties.
247
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw`
257
+ * @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
248
258
  * parameter in the `override delete` method is used to specify the condition or key based on which a
249
259
  * node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
250
260
  * function that determines which node(s) should be deleted.
251
- * @returns The `override delete` method is returning an array of `BinaryTreeDeleteResult<NODE>`
261
+ * @returns The `override delete` method is returning an array of `BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>`
252
262
  * objects. Each object in the array contains information about the deleted node and whether
253
263
  * balancing is needed.
254
264
  */
255
- override delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[] {
256
- if (keyNodeEntryOrRaw === null) return [];
265
+ override delete(
266
+ keyNodeOrEntry: BTNRep<K, V, RedBlackTreeNode<K, V>>
267
+ ): BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>[] {
268
+ if (keyNodeOrEntry === null) return [];
257
269
 
258
- const results: BinaryTreeDeleteResult<NODE>[] = [];
259
- let nodeToDelete: OptNode<NODE>;
260
- if (this._isPredicate(keyNodeEntryOrRaw)) nodeToDelete = this.getNode(keyNodeEntryOrRaw);
261
- else nodeToDelete = this.isRealNode(keyNodeEntryOrRaw) ? keyNodeEntryOrRaw : this.getNode(keyNodeEntryOrRaw);
270
+ const results: BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>[] = [];
271
+ let nodeToDelete: OptNode<RedBlackTreeNode<K, V>>;
272
+ if (this._isPredicate(keyNodeOrEntry)) nodeToDelete = this.getNode(keyNodeOrEntry);
273
+ else nodeToDelete = this.isRealNode(keyNodeOrEntry) ? keyNodeOrEntry : this.getNode(keyNodeOrEntry);
262
274
 
263
275
  if (!nodeToDelete) {
264
276
  return results;
265
277
  }
266
278
 
267
279
  let originalColor = nodeToDelete.color;
268
- let replacementNode: NODE | undefined;
280
+ let replacementNode: RedBlackTreeNode<K, V> | undefined;
269
281
 
270
282
  if (!this.isRealNode(nodeToDelete.left)) {
271
283
  if (nodeToDelete.right !== null) {
@@ -316,15 +328,62 @@ export class RedBlackTree<
316
328
  return results;
317
329
  }
318
330
 
331
+ /**
332
+ * Time Complexity: O(n)
333
+ * Space Complexity: O(n)
334
+ *
335
+ * The `map` function in TypeScript overrides the default behavior to create a new Red-Black Tree by
336
+ * applying a callback to each entry in the original tree.
337
+ * @param callback - A function that will be called for each entry in the tree, with parameters
338
+ * representing the key, value, index, and the tree itself. It should return an entry for the new
339
+ * tree.
340
+ * @param [options] - The `options` parameter in the `map` method is of type `RedBlackTreeOptions<MK, MV,
341
+ * MR>`. This parameter allows you to specify additional options or configurations for the Red-Black
342
+ * Tree that will be created during the mapping process. These options could include things like
343
+ * custom comparators
344
+ * @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
345
+ * the value of `this` when executing the `callback` function. It allows you to set the context
346
+ * (value of `this`) for the callback function. This can be useful when you want to access properties
347
+ * or
348
+ * @returns A new Red-Black Tree is being returned, where each entry has been transformed using the
349
+ * provided callback function.
350
+ */
351
+ override map(
352
+ callback: EntryCallback<K, V | undefined, [MK, MV]>,
353
+ options?: RedBlackTreeOptions<MK, MV, MR>,
354
+ thisArg?: any
355
+ ): RedBlackTree<MK, MV, MR> {
356
+ const newTree = new RedBlackTree<MK, MV, MR>([], options);
357
+ let index = 0;
358
+ for (const [key, value] of this) {
359
+ newTree.add(callback.call(thisArg, key, value, index++, this));
360
+ }
361
+ return newTree;
362
+ }
363
+
364
+ /**
365
+ * Time Complexity: O(n)
366
+ * Space Complexity: O(n)
367
+ *
368
+ * The function `clone` overrides the default cloning behavior to create a deep copy of a tree
369
+ * structure.
370
+ * @returns The `cloned` object is being returned.
371
+ */
372
+ override clone() {
373
+ const cloned = this.createTree();
374
+ this._clone(cloned);
375
+ return cloned;
376
+ }
377
+
319
378
  /**
320
379
  * Time Complexity: O(1)
321
380
  * Space Complexity: O(1)
322
381
  *
323
382
  * The function sets the root of a tree-like structure and updates the parent property of the new
324
383
  * root.
325
- * @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
384
+ * @param {RedBlackTreeNode<K, V> | undefined} v - v is a parameter of type RedBlackTreeNode<K, V> or undefined.
326
385
  */
327
- protected override _setRoot(v: NODE | undefined) {
386
+ protected override _setRoot(v: RedBlackTreeNode<K, V> | undefined) {
328
387
  if (v) {
329
388
  v.parent = undefined;
330
389
  }
@@ -336,14 +395,17 @@ export class RedBlackTree<
336
395
  * Space Complexity: O(1)
337
396
  *
338
397
  * The function replaces an old node with a new node while preserving the color of the old node.
339
- * @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
398
+ * @param {RedBlackTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
340
399
  * the data structure.
341
- * @param {NODE} newNode - The `newNode` parameter is of type `NODE`, which represents a node in a
400
+ * @param {RedBlackTreeNode<K, V>} newNode - The `newNode` parameter is of type `RedBlackTreeNode<K, V>`, which represents a node in a
342
401
  * data structure.
343
402
  * @returns The method is returning the result of calling the `_replaceNode` method from the
344
403
  * superclass, with the `oldNode` and `newNode` parameters.
345
404
  */
346
- protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE {
405
+ protected override _replaceNode(
406
+ oldNode: RedBlackTreeNode<K, V>,
407
+ newNode: RedBlackTreeNode<K, V>
408
+ ): RedBlackTreeNode<K, V> {
347
409
  newNode.color = oldNode.color;
348
410
 
349
411
  return super._replaceNode(oldNode, newNode);
@@ -351,19 +413,19 @@ export class RedBlackTree<
351
413
 
352
414
  /**
353
415
  * Time Complexity: O(log n)
354
- * Space Complexity: O(1)
416
+ * Space Complexity: O(log n)
355
417
  *
356
418
  * The `_insert` function inserts a node into a binary search tree and performs necessary fix-ups to
357
419
  * maintain the red-black tree properties.
358
- * @param {NODE} node - The `node` parameter represents the node that needs to be inserted into the
420
+ * @param {RedBlackTreeNode<K, V>} node - The `node` parameter represents the node that needs to be inserted into the
359
421
  * binary search tree.
360
422
  * @returns a string value indicating the result of the insertion operation. It can return either
361
423
  * 'UPDATED' if the node with the same key already exists and was updated, or 'CREATED' if a new node
362
424
  * was created and inserted into the tree.
363
425
  */
364
- protected _insert(node: NODE): CRUD {
426
+ protected _insert(node: RedBlackTreeNode<K, V>): CRUD {
365
427
  let current = this.root;
366
- let parent: NODE | undefined = undefined;
428
+ let parent: RedBlackTreeNode<K, V> | undefined = undefined;
367
429
 
368
430
  while (this.isRealNode(current)) {
369
431
  parent = current;
@@ -401,11 +463,11 @@ export class RedBlackTree<
401
463
  * Space Complexity: O(1)
402
464
  *
403
465
  * The function `_transplant` is used to replace a node `u` with another node `v` in a binary tree.
404
- * @param {NODE} u - The parameter "u" represents a node in a binary tree.
405
- * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`, which means it can
406
- * either be a `NODE` object or `undefined`.
466
+ * @param {RedBlackTreeNode<K, V>} u - The parameter "u" represents a node in a binary tree.
467
+ * @param {RedBlackTreeNode<K, V> | undefined} v - The parameter `v` is of type `RedBlackTreeNode<K, V> | undefined`, which means it can
468
+ * either be a `RedBlackTreeNode<K, V>` object or `undefined`.
407
469
  */
408
- protected _transplant(u: NODE, v: NODE | undefined): void {
470
+ protected _transplant(u: RedBlackTreeNode<K, V>, v: RedBlackTreeNode<K, V> | undefined): void {
409
471
  if (!u.parent) {
410
472
  this._setRoot(v);
411
473
  } else if (u === u.parent.left) {
@@ -424,10 +486,10 @@ export class RedBlackTree<
424
486
  * Space Complexity: O(1)
425
487
  *
426
488
  * The `_insertFixup` function is used to fix the Red-Black Tree after inserting a new node.
427
- * @param {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree data
489
+ * @param {RedBlackTreeNode<K, V> | undefined} z - The parameter `z` represents a node in the Red-Black Tree data
428
490
  * structure. It can either be a valid node or `undefined`.
429
491
  */
430
- protected _insertFixup(z: NODE | undefined): void {
492
+ protected _insertFixup(z: RedBlackTreeNode<K, V> | undefined): void {
431
493
  // Continue fixing the tree as long as the parent of z is red
432
494
  while (z?.parent?.color === 'RED') {
433
495
  // Check if the parent of z is the left child of its parent
@@ -460,7 +522,7 @@ export class RedBlackTree<
460
522
  } else {
461
523
  // Symmetric case for the right child (left and right exchanged)
462
524
  // Follow the same logic as above with left and right exchanged
463
- const y: NODE | undefined = z?.parent?.parent?.left ?? undefined;
525
+ const y: RedBlackTreeNode<K, V> | undefined = z?.parent?.parent?.left ?? undefined;
464
526
  if (y?.color === 'RED') {
465
527
  z.parent.color = 'BLACK';
466
528
  y.color = 'BLACK';
@@ -491,12 +553,12 @@ export class RedBlackTree<
491
553
  *
492
554
  * The `_deleteFixup` function is used to fix the red-black tree after a node deletion by adjusting
493
555
  * the colors and performing rotations.
494
- * @param {NODE | undefined} node - The `node` parameter represents a node in a binary tree. It can
556
+ * @param {RedBlackTreeNode<K, V> | undefined} node - The `node` parameter represents a node in a binary tree. It can
495
557
  * be either a valid node object or `undefined`.
496
558
  * @returns The function does not return any value. It has a return type of `void`, which means it
497
559
  * does not return anything.
498
560
  */
499
- protected _deleteFixup(node: NODE | undefined): void {
561
+ protected _deleteFixup(node: RedBlackTreeNode<K, V> | undefined): void {
500
562
  // Early exit condition
501
563
  if (!node || node === this.root || node.color === 'BLACK') {
502
564
  if (node) {
@@ -506,7 +568,7 @@ export class RedBlackTree<
506
568
  }
507
569
 
508
570
  while (node && node !== this.root && node.color === 'BLACK') {
509
- const parent: NODE | undefined = node.parent;
571
+ const parent: RedBlackTreeNode<K, V> | undefined = node.parent;
510
572
 
511
573
  if (!parent) {
512
574
  break; // Ensure the loop terminates if there's an issue with the tree structure
@@ -573,11 +635,11 @@ export class RedBlackTree<
573
635
  * Space Complexity: O(1)
574
636
  *
575
637
  * The `_leftRotate` function performs a left rotation on a given node in a binary tree.
576
- * @param {NODE | undefined} x - The parameter `x` is of type `NODE | undefined`. It represents a
638
+ * @param {RedBlackTreeNode<K, V> | undefined} x - The parameter `x` is of type `RedBlackTreeNode<K, V> | undefined`. It represents a
577
639
  * node in a binary tree or `undefined` if there is no node.
578
640
  * @returns void, which means it does not return any value.
579
641
  */
580
- protected _leftRotate(x: NODE | undefined): void {
642
+ protected _leftRotate(x: RedBlackTreeNode<K, V> | undefined): void {
581
643
  if (!x || !x.right) {
582
644
  return;
583
645
  }
@@ -608,11 +670,11 @@ export class RedBlackTree<
608
670
  * Space Complexity: O(1)
609
671
  *
610
672
  * The `_rightRotate` function performs a right rotation on a given node in a binary tree.
611
- * @param {NODE | undefined} y - The parameter `y` is of type `NODE | undefined`. It represents a
673
+ * @param {RedBlackTreeNode<K, V> | undefined} y - The parameter `y` is of type `RedBlackTreeNode<K, V> | undefined`. It represents a
612
674
  * node in a binary tree or `undefined` if there is no node.
613
675
  * @returns void, which means it does not return any value.
614
676
  */
615
- protected _rightRotate(y: NODE | undefined): void {
677
+ protected _rightRotate(y: RedBlackTreeNode<K, V> | undefined): void {
616
678
  if (!y || !y.left) {
617
679
  return;
618
680
  }
@@ -637,37 +699,4 @@ export class RedBlackTree<
637
699
  x.right = y;
638
700
  y.parent = x;
639
701
  }
640
-
641
- /**
642
- * Time Complexity: O(n)
643
- * Space Complexity: O(n)
644
- *
645
- * The `map` function in TypeScript overrides the default behavior to create a new Red-Black Tree by
646
- * applying a callback to each entry in the original tree.
647
- * @param callback - A function that will be called for each entry in the tree, with parameters
648
- * representing the key, value, index, and the tree itself. It should return an entry for the new
649
- * tree.
650
- * @param [options] - The `options` parameter in the `map` method is of type `RedBlackTreeOptions<MK, MV,
651
- * MR>`. This parameter allows you to specify additional options or configurations for the Red-Black
652
- * Tree that will be created during the mapping process. These options could include things like
653
- * custom comparators
654
- * @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
655
- * the value of `this` when executing the `callback` function. It allows you to set the context
656
- * (value of `this`) for the callback function. This can be useful when you want to access properties
657
- * or
658
- * @returns A new Red-Black Tree is being returned, where each entry has been transformed using the
659
- * provided callback function.
660
- */
661
- override map(
662
- callback: EntryCallback<K, V | undefined, [MK, MV]>,
663
- options?: RedBlackTreeOptions<MK, MV, MR>,
664
- thisArg?: any
665
- ): RedBlackTree<MK, MV, MR> {
666
- const newTree = new RedBlackTree<MK, MV, MR>([], options);
667
- let index = 0;
668
- for (const [key, value] of this) {
669
- newTree.add(callback.call(thisArg, key, value, index++, this));
670
- }
671
- return newTree;
672
- }
673
702
  }