graph-typed 1.53.9 → 1.54.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -17
  2. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +1 -20
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +9 -24
  4. package/dist/data-structures/binary-tree/avl-tree.js +18 -34
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +40 -37
  6. package/dist/data-structures/binary-tree/binary-tree.js +68 -53
  7. package/dist/data-structures/binary-tree/bst.d.ts +36 -50
  8. package/dist/data-structures/binary-tree/bst.js +45 -60
  9. package/dist/data-structures/binary-tree/red-black-tree.d.ts +29 -43
  10. package/dist/data-structures/binary-tree/red-black-tree.js +45 -59
  11. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +33 -45
  12. package/dist/data-structures/binary-tree/tree-multi-map.js +70 -83
  13. package/dist/interfaces/binary-tree.d.ts +4 -3
  14. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -2
  15. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -2
  16. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +3 -2
  17. package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
  18. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +5 -4
  19. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -2
  20. package/package.json +2 -2
  21. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +23 -31
  22. package/src/data-structures/binary-tree/avl-tree.ts +35 -46
  23. package/src/data-structures/binary-tree/binary-tree.ts +105 -66
  24. package/src/data-structures/binary-tree/bst.ts +105 -104
  25. package/src/data-structures/binary-tree/red-black-tree.ts +68 -73
  26. package/src/data-structures/binary-tree/tree-multi-map.ts +98 -102
  27. package/src/interfaces/binary-tree.ts +16 -3
  28. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +4 -2
  29. package/src/types/data-structures/binary-tree/avl-tree.ts +4 -2
  30. package/src/types/data-structures/binary-tree/binary-tree.ts +3 -3
  31. package/src/types/data-structures/binary-tree/bst.ts +4 -2
  32. package/src/types/data-structures/binary-tree/rb-tree.ts +6 -4
  33. package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -2
@@ -6,6 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import {
9
+ BSTNested,
9
10
  BSTNodeNested,
10
11
  BSTNOptKeyOrNode,
11
12
  BSTOptions,
@@ -18,7 +19,8 @@ import {
18
19
  IterationType,
19
20
  NodeCallback,
20
21
  NodePredicate,
21
- OptNode
22
+ OptNode,
23
+ OptNodeOrNull
22
24
  } from '../../types';
23
25
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
24
26
  import { IBinaryTree } from '../../interfaces';
@@ -40,13 +42,13 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
40
42
  this._right = undefined;
41
43
  }
42
44
 
43
- protected override _left?: NODE;
45
+ override _left?: OptNodeOrNull<NODE>;
44
46
 
45
47
  /**
46
48
  * The function returns the value of the `_left` property.
47
49
  * @returns The `_left` property of the current object is being returned.
48
50
  */
49
- override get left(): OptNode<NODE> {
51
+ override get left(): OptNodeOrNull<NODE> {
50
52
  return this._left;
51
53
  }
52
54
 
@@ -55,21 +57,21 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
55
57
  * @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be an
56
58
  * instance of the `NODE` class or `undefined`.
57
59
  */
58
- override set left(v: OptNode<NODE>) {
60
+ override set left(v: OptNodeOrNull<NODE>) {
59
61
  if (v) {
60
62
  v.parent = this as unknown as NODE;
61
63
  }
62
64
  this._left = v;
63
65
  }
64
66
 
65
- protected override _right?: NODE;
67
+ override _right?: OptNodeOrNull<NODE>;
66
68
 
67
69
  /**
68
70
  * The function returns the right node of a binary tree or undefined if there is no right node.
69
71
  * @returns The method is returning the value of the `_right` property, which is of type `NODE` or
70
72
  * `undefined`.
71
73
  */
72
- override get right(): OptNode<NODE> {
74
+ override get right(): OptNodeOrNull<NODE> {
73
75
  return this._right;
74
76
  }
75
77
 
@@ -78,7 +80,7 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
78
80
  * @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be a
79
81
  * `NODE` object or `undefined`.
80
82
  */
81
- override set right(v: OptNode<NODE>) {
83
+ override set right(v: OptNodeOrNull<NODE>) {
82
84
  if (v) {
83
85
  v.parent = this as unknown as NODE;
84
86
  }
@@ -151,9 +153,27 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
151
153
  * console.log(findLCA(5, 35)); // 15
152
154
  * console.log(findLCA(20, 30)); // 25
153
155
  */
154
- export class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>>
155
- extends BinaryTree<K, V, R, NODE>
156
- implements IBinaryTree<K, V, R, NODE>
156
+ export class BST<
157
+ K = any,
158
+ V = any,
159
+ R = object,
160
+ MK = any,
161
+ MV = any,
162
+ MR = object,
163
+ NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>,
164
+ TREE extends BST<K, V, R, MK, MV, MR, NODE, TREE> = BST<
165
+ K,
166
+ V,
167
+ R,
168
+ MK,
169
+ MV,
170
+ MR,
171
+ NODE,
172
+ BSTNested<K, V, R, MK, MV, MR, NODE>
173
+ >
174
+ >
175
+ extends BinaryTree<K, V, R, MK, MV, MR, NODE, TREE>
176
+ implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE>
157
177
  {
158
178
  /**
159
179
  * This is the constructor function for a Binary Search Tree class in TypeScript.
@@ -196,45 +216,6 @@ export class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V, NODE>
196
216
  return this._isReverse;
197
217
  }
198
218
 
199
- protected _comparator: Comparator<K> = (a: K, b: K): number => {
200
- if (isComparable(a) && isComparable(b)) {
201
- if (a > b) return 1;
202
- if (a < b) return -1;
203
- return 0;
204
- }
205
- if (this._specifyComparable) {
206
- if (this._specifyComparable(a) > this._specifyComparable(b)) return 1;
207
- if (this._specifyComparable(a) < this._specifyComparable(b)) return -1;
208
- return 0;
209
- }
210
- if (typeof a === 'object' || typeof b === 'object') {
211
- throw TypeError(
212
- `When comparing object types, a custom specifyComparable must be defined in the constructor's options parameter.`
213
- );
214
- }
215
-
216
- return 0;
217
- };
218
-
219
- /**
220
- * The function returns the value of the _comparator property.
221
- * @returns The `_comparator` property is being returned.
222
- */
223
- get comparator() {
224
- return this._comparator;
225
- }
226
-
227
- protected _specifyComparable?: (key: K) => Comparable;
228
-
229
- /**
230
- * This function returns the value of the `_specifyComparable` property.
231
- * @returns The method `specifyComparable()` is being returned, which is a getter method for the
232
- * `_specifyComparable` property.
233
- */
234
- get specifyComparable() {
235
- return this._specifyComparable;
236
- }
237
-
238
219
  /**
239
220
  * The function creates a new BSTNode with the given key and value and returns it.
240
221
  * @param {K} key - The key parameter is of type K, which represents the type of the key for the node
@@ -248,26 +229,39 @@ export class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V, NODE>
248
229
  }
249
230
 
250
231
  /**
251
- * Time Complexity: O(1)
252
- * Space Complexity: O(1)
253
- *
254
- * The `createTree` function in TypeScript overrides the default options with the provided options to
255
- * create a new Binary Search Tree.
256
- * @param [options] - The `options` parameter in the `createTree` method is an optional object that
257
- * can contain the following properties:
258
- * @returns A new instance of a Binary Search Tree (BST) is being returned with the specified options
259
- * and properties inherited from the current instance.
232
+ * The function creates a new binary search tree with the specified options.
233
+ * @param [options] - The `options` parameter is an optional object that allows you to customize the
234
+ * behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which has the
235
+ * following properties:
236
+ * @returns a new instance of the BST class with the provided options.
260
237
  */
261
- // @ts-ignore
262
- override createTree(options?: BSTOptions<K, V, R>) {
263
- return new BST<K, V, R>([], {
238
+ override createTree(options?: BSTOptions<K, V, R>): TREE {
239
+ return new BST<K, V, R, MK, MV, MR, NODE, TREE>([], {
264
240
  iterationType: this.iterationType,
265
241
  isMapMode: this._isMapMode,
266
242
  specifyComparable: this._specifyComparable,
267
243
  toEntryFn: this._toEntryFn,
268
244
  isReverse: this._isReverse,
269
245
  ...options
270
- });
246
+ }) as TREE;
247
+ }
248
+
249
+ /**
250
+ * The function overrides a method and converts a key, value pair or entry or raw element to a node.
251
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of
252
+ * type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw
253
+ * element.
254
+ * @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
255
+ * value associated with a key in a key-value pair.
256
+ * @returns either a NODE object or undefined.
257
+ */
258
+ protected override _keyValueNodeEntryRawToNodeAndValue(
259
+ keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
260
+ value?: V
261
+ ): [OptNode<NODE>, V | undefined] {
262
+ const [node, entryValue] = super._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
263
+ if (node === null) return [undefined, undefined];
264
+ return [node, value ?? entryValue];
271
265
  }
272
266
 
273
267
  /**
@@ -350,7 +344,7 @@ export class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V, NODE>
350
344
  this._size++;
351
345
  return true;
352
346
  }
353
- current = current.left;
347
+ if (current.left !== null) current = current.left;
354
348
  } else {
355
349
  if (current.right === undefined) {
356
350
  current.right = newNode;
@@ -358,7 +352,7 @@ export class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V, NODE>
358
352
  this._size++;
359
353
  return true;
360
354
  }
361
- current = current.right;
355
+ if (current.right !== null) current = current.right;
362
356
  }
363
357
  }
364
358
 
@@ -483,19 +477,6 @@ export class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V, NODE>
483
477
  return inserted;
484
478
  }
485
479
 
486
- /**
487
- * Time Complexity: O(n)
488
- * Space Complexity: O(1)
489
- *
490
- * The `merge` function overrides the base class method by adding elements from another
491
- * binary search tree.
492
- * @param anotherTree - `anotherTree` is an instance of a Binary Search Tree (BST) with key type `K`,
493
- * value type `V`, return type `R`, node type `NODE`, and tree type `TREE`.
494
- */
495
- override merge(anotherTree: this) {
496
- this.addMany(anotherTree, [], false);
497
- }
498
-
499
480
  /**
500
481
  * Time Complexity: O(log n)
501
482
  * Space Complexity: O(k + log n)
@@ -906,7 +887,7 @@ export class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V, NODE>
906
887
  let balanced = true;
907
888
 
908
889
  if (iterationType === 'RECURSIVE') {
909
- const _height = (cur: OptNode<NODE>): number => {
890
+ const _height = (cur: OptNodeOrNull<NODE>): number => {
910
891
  if (!cur) return 0;
911
892
  const leftHeight = _height(cur.left),
912
893
  rightHeight = _height(cur.right);
@@ -923,7 +904,7 @@ export class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V, NODE>
923
904
  while (stack.length > 0 || node) {
924
905
  if (node) {
925
906
  stack.push(node);
926
- node = node.left;
907
+ if (node.left !== null) node = node.left;
927
908
  } else {
928
909
  node = stack[stack.length - 1];
929
910
  if (!node.right || last === node.right) {
@@ -944,36 +925,43 @@ export class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V, NODE>
944
925
  return balanced;
945
926
  }
946
927
 
947
- // @ts-ignore
948
- override map<MK, MV, MR>(
949
- callback: EntryCallback<K, V | undefined, [MK, MV]>,
950
- options?: BSTOptions<MK, MV, MR>,
951
- thisArg?: any
952
- ) {
953
- const newTree = new BST<MK, MV, MR>([], options);
954
- let index = 0;
955
- for (const [key, value] of this) {
956
- newTree.add(callback.call(thisArg, key, value, index++, this));
928
+ protected _comparator: Comparator<K> = (a: K, b: K): number => {
929
+ if (isComparable(a) && isComparable(b)) {
930
+ if (a > b) return 1;
931
+ if (a < b) return -1;
932
+ return 0;
957
933
  }
958
- return newTree;
934
+ if (this._specifyComparable) {
935
+ if (this._specifyComparable(a) > this._specifyComparable(b)) return 1;
936
+ if (this._specifyComparable(a) < this._specifyComparable(b)) return -1;
937
+ return 0;
938
+ }
939
+ if (typeof a === 'object' || typeof b === 'object') {
940
+ throw TypeError(
941
+ `When comparing object types, a custom specifyComparable must be defined in the constructor's options parameter.`
942
+ );
943
+ }
944
+
945
+ return 0;
946
+ };
947
+
948
+ /**
949
+ * The function returns the value of the _comparator property.
950
+ * @returns The `_comparator` property is being returned.
951
+ */
952
+ get comparator() {
953
+ return this._comparator;
959
954
  }
960
955
 
956
+ protected _specifyComparable?: (key: K) => Comparable;
957
+
961
958
  /**
962
- * The function overrides a method and converts a key, value pair or entry or raw element to a node.
963
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of
964
- * type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw
965
- * element.
966
- * @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
967
- * value associated with a key in a key-value pair.
968
- * @returns either a NODE object or undefined.
959
+ * This function returns the value of the `_specifyComparable` property.
960
+ * @returns The method `specifyComparable()` is being returned, which is a getter method for the
961
+ * `_specifyComparable` property.
969
962
  */
970
- protected override _keyValueNodeEntryRawToNodeAndValue(
971
- keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
972
- value?: V
973
- ): [OptNode<NODE>, V | undefined] {
974
- const [node, entryValue] = super._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
975
- if (node === null) return [undefined, undefined];
976
- return [node, value ?? entryValue];
963
+ get specifyComparable() {
964
+ return this._specifyComparable;
977
965
  }
978
966
 
979
967
  /**
@@ -991,4 +979,17 @@ export class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V, NODE>
991
979
  protected _compare(a: K, b: K) {
992
980
  return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
993
981
  }
982
+
983
+ override map(
984
+ callback: EntryCallback<K, V | undefined, [MK, MV]>,
985
+ options?: BSTOptions<MK, MV, MR>,
986
+ thisArg?: any
987
+ ): BST<MK, MV, MR> {
988
+ const newTree = new BST<MK, MV, MR>([], options);
989
+ let index = 0;
990
+ for (const [key, value] of this) {
991
+ newTree.add(callback.call(thisArg, key, value, index++, this));
992
+ }
993
+ return newTree;
994
+ }
994
995
  }
@@ -6,6 +6,7 @@ import type {
6
6
  OptNode,
7
7
  RBTNColor,
8
8
  RedBlackTreeOptions,
9
+ RedBlackTreeNested,
9
10
  RedBlackTreeNodeNested
10
11
  } from '../../types';
11
12
  import { BST, BSTNode } from './bst';
@@ -31,24 +32,6 @@ export class RedBlackTreeNode<
31
32
  super(key, value);
32
33
  this._color = color;
33
34
  }
34
-
35
- protected _color: RBTNColor;
36
-
37
- /**
38
- * The function returns the color value of a variable.
39
- * @returns The color value stored in the private variable `_color`.
40
- */
41
- get color(): RBTNColor {
42
- return this._color;
43
- }
44
-
45
- /**
46
- * The function sets the color property to the specified value.
47
- * @param {RBTNColor} value - The value parameter is of type RBTNColor.
48
- */
49
- set color(value: RBTNColor) {
50
- this._color = value;
51
- }
52
35
  }
53
36
 
54
37
  /**
@@ -108,16 +91,29 @@ export class RedBlackTree<
108
91
  K = any,
109
92
  V = any,
110
93
  R = object,
111
- NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>
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
+ >
112
108
  >
113
- extends BST<K, V, R, NODE>
114
- implements IBinaryTree<K, V, R, NODE>
109
+ extends BST<K, V, R, MK, MV, MR, NODE, TREE>
110
+ implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE>
115
111
  {
116
112
  /**
117
113
  * This is the constructor function for a Red-Black Tree data structure in TypeScript.
118
114
  * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
119
115
  * iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
120
- * initialize the RedBlackTree with the provided elements.
116
+ * initialize the RBTree with the provided elements.
121
117
  * @param [options] - The `options` parameter is an optional object that can be passed to the
122
118
  * constructor. It is of type `RedBlackTreeOptions<K, V, R>`. This object can contain various options for
123
119
  * configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
@@ -162,23 +158,19 @@ export class RedBlackTree<
162
158
  }
163
159
 
164
160
  /**
165
- * The function `createTree` overrides the default implementation to create a Red-Black Tree with
166
- * specified options in TypeScript.
167
- * @param [options] - The `options` parameter in the `createTree` method is of type `RedBlackTreeOptions<K,
168
- * V, R>`, which is a generic type with three type parameters `K`, `V`, and `R`. This parameter
169
- * allows you to pass additional configuration options when creating a new Red-
170
- * @returns A new instance of a RedBlackTree with the specified options and properties from the
171
- * current object is being returned.
161
+ * The function creates a new Red-Black Tree with the specified options.
162
+ * @param [options] - The `options` parameter is an optional object that contains additional
163
+ * configuration options for creating the Red-Black Tree. It has the following properties:
164
+ * @returns a new instance of a RedBlackTree object.
172
165
  */
173
- // @ts-ignore
174
- override createTree(options?: RedBlackTreeOptions<K, V, R>) {
175
- return new RedBlackTree<K, V, R>([], {
166
+ override createTree(options?: RedBlackTreeOptions<K, V, R>): TREE {
167
+ return new RedBlackTree<K, V, R, MK, MV, MR, NODE, TREE>([], {
176
168
  iterationType: this.iterationType,
177
169
  isMapMode: this._isMapMode,
178
170
  specifyComparable: this._specifyComparable,
179
171
  toEntryFn: this._toEntryFn,
180
172
  ...options
181
- });
173
+ }) as TREE;
182
174
  }
183
175
 
184
176
  /**
@@ -276,8 +268,10 @@ export class RedBlackTree<
276
268
  let replacementNode: NODE | undefined;
277
269
 
278
270
  if (!this.isRealNode(nodeToDelete.left)) {
279
- replacementNode = nodeToDelete.right;
280
- this._transplant(nodeToDelete, nodeToDelete.right);
271
+ if (nodeToDelete.right !== null) {
272
+ replacementNode = nodeToDelete.right;
273
+ this._transplant(nodeToDelete, nodeToDelete.right);
274
+ }
281
275
  } else if (!this.isRealNode(nodeToDelete.right)) {
282
276
  replacementNode = nodeToDelete.left;
283
277
  this._transplant(nodeToDelete, nodeToDelete.left);
@@ -285,15 +279,17 @@ export class RedBlackTree<
285
279
  const successor = this.getLeftMost(node => node, nodeToDelete.right);
286
280
  if (successor) {
287
281
  originalColor = successor.color;
288
- replacementNode = successor.right;
282
+ if (successor.right !== null) replacementNode = successor.right;
289
283
 
290
284
  if (successor.parent === nodeToDelete) {
291
285
  if (this.isRealNode(replacementNode)) {
292
286
  replacementNode.parent = successor;
293
287
  }
294
288
  } else {
295
- this._transplant(successor, successor.right);
296
- successor.right = nodeToDelete.right;
289
+ if (successor.right !== null) {
290
+ this._transplant(successor, successor.right);
291
+ successor.right = nodeToDelete.right;
292
+ }
297
293
  if (this.isRealNode(successor.right)) {
298
294
  successor.right.parent = successor;
299
295
  }
@@ -320,40 +316,6 @@ export class RedBlackTree<
320
316
  return results;
321
317
  }
322
318
 
323
- /**
324
- * Time Complexity: O(n)
325
- * Space Complexity: O(n)
326
- *
327
- * The `map` function in TypeScript overrides the default behavior to create a new Red-Black Tree by
328
- * applying a callback to each entry in the original tree.
329
- * @param callback - A function that will be called for each entry in the tree, with parameters
330
- * representing the key, value, index, and the tree itself. It should return an entry for the new
331
- * tree.
332
- * @param [options] - The `options` parameter in the `map` method is of type `RedBlackTreeOptions<MK, MV,
333
- * MR>`. This parameter allows you to specify additional options or configurations for the Red-Black
334
- * Tree that will be created during the mapping process. These options could include things like
335
- * custom comparators
336
- * @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
337
- * the value of `this` when executing the `callback` function. It allows you to set the context
338
- * (value of `this`) for the callback function. This can be useful when you want to access properties
339
- * or
340
- * @returns A new Red-Black Tree is being returned, where each entry has been transformed using the
341
- * provided callback function.
342
- */
343
- // @ts-ignore
344
- override map<MK, MV, MR>(
345
- callback: EntryCallback<K, V | undefined, [MK, MV]>,
346
- options?: RedBlackTreeOptions<MK, MV, MR>,
347
- thisArg?: any
348
- ) {
349
- const newTree = new RedBlackTree<MK, MV, MR>([], options);
350
- let index = 0;
351
- for (const [key, value] of this) {
352
- newTree.add(callback.call(thisArg, key, value, index++, this));
353
- }
354
- return newTree;
355
- }
356
-
357
319
  /**
358
320
  * Time Complexity: O(1)
359
321
  * Space Complexity: O(1)
@@ -498,7 +460,7 @@ export class RedBlackTree<
498
460
  } else {
499
461
  // Symmetric case for the right child (left and right exchanged)
500
462
  // Follow the same logic as above with left and right exchanged
501
- const y: NODE | undefined = z?.parent?.parent?.left;
463
+ const y: NODE | undefined = z?.parent?.parent?.left ?? undefined;
502
464
  if (y?.color === 'RED') {
503
465
  z.parent.color = 'BLACK';
504
466
  y.color = 'BLACK';
@@ -675,4 +637,37 @@ export class RedBlackTree<
675
637
  x.right = y;
676
638
  y.parent = x;
677
639
  }
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
+ }
678
673
  }