graph-typed 1.53.7 → 1.53.9

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 (68) hide show
  1. package/dist/common/index.js +5 -0
  2. package/dist/data-structures/base/iterable-entry-base.js +4 -4
  3. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +36 -17
  4. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +65 -36
  5. package/dist/data-structures/binary-tree/avl-tree.d.ts +12 -8
  6. package/dist/data-structures/binary-tree/avl-tree.js +19 -6
  7. package/dist/data-structures/binary-tree/binary-tree.d.ts +53 -40
  8. package/dist/data-structures/binary-tree/binary-tree.js +76 -72
  9. package/dist/data-structures/binary-tree/bst.d.ts +87 -52
  10. package/dist/data-structures/binary-tree/bst.js +111 -63
  11. package/dist/data-structures/binary-tree/index.d.ts +1 -1
  12. package/dist/data-structures/binary-tree/index.js +1 -1
  13. package/dist/data-structures/binary-tree/{rb-tree.d.ts → red-black-tree.d.ts} +83 -10
  14. package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +91 -44
  15. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +34 -18
  16. package/dist/data-structures/binary-tree/tree-multi-map.js +66 -40
  17. package/dist/data-structures/graph/abstract-graph.js +2 -2
  18. package/dist/data-structures/hash/hash-map.d.ts +31 -1
  19. package/dist/data-structures/hash/hash-map.js +35 -5
  20. package/dist/data-structures/heap/heap.d.ts +20 -3
  21. package/dist/data-structures/heap/heap.js +31 -11
  22. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +46 -11
  23. package/dist/data-structures/linked-list/doubly-linked-list.js +68 -21
  24. package/dist/data-structures/linked-list/singly-linked-list.d.ts +44 -11
  25. package/dist/data-structures/linked-list/singly-linked-list.js +70 -26
  26. package/dist/data-structures/queue/deque.d.ts +37 -8
  27. package/dist/data-structures/queue/deque.js +73 -29
  28. package/dist/data-structures/queue/queue.d.ts +41 -1
  29. package/dist/data-structures/queue/queue.js +51 -9
  30. package/dist/data-structures/stack/stack.d.ts +27 -10
  31. package/dist/data-structures/stack/stack.js +39 -20
  32. package/dist/data-structures/trie/trie.d.ts +8 -3
  33. package/dist/data-structures/trie/trie.js +8 -3
  34. package/dist/interfaces/binary-tree.d.ts +3 -4
  35. package/dist/types/data-structures/base/base.d.ts +1 -1
  36. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -3
  37. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
  38. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -3
  39. package/dist/types/data-structures/binary-tree/bst.d.ts +3 -4
  40. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +4 -5
  41. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -5
  42. package/package.json +2 -2
  43. package/src/common/index.ts +7 -1
  44. package/src/data-structures/base/iterable-entry-base.ts +4 -4
  45. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +82 -55
  46. package/src/data-structures/binary-tree/avl-tree.ts +32 -15
  47. package/src/data-structures/binary-tree/binary-tree.ts +89 -84
  48. package/src/data-structures/binary-tree/bst.ts +149 -97
  49. package/src/data-structures/binary-tree/index.ts +1 -1
  50. package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +105 -55
  51. package/src/data-structures/binary-tree/tree-multi-map.ts +81 -51
  52. package/src/data-structures/graph/abstract-graph.ts +2 -2
  53. package/src/data-structures/hash/hash-map.ts +37 -7
  54. package/src/data-structures/heap/heap.ts +33 -10
  55. package/src/data-structures/linked-list/doubly-linked-list.ts +75 -21
  56. package/src/data-structures/linked-list/singly-linked-list.ts +77 -27
  57. package/src/data-structures/queue/deque.ts +72 -28
  58. package/src/data-structures/queue/queue.ts +50 -7
  59. package/src/data-structures/stack/stack.ts +39 -20
  60. package/src/data-structures/trie/trie.ts +8 -3
  61. package/src/interfaces/binary-tree.ts +3 -13
  62. package/src/types/data-structures/base/base.ts +1 -1
  63. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -4
  64. package/src/types/data-structures/binary-tree/avl-tree.ts +2 -4
  65. package/src/types/data-structures/binary-tree/binary-tree.ts +3 -3
  66. package/src/types/data-structures/binary-tree/bst.ts +3 -5
  67. package/src/types/data-structures/binary-tree/rb-tree.ts +4 -6
  68. package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -6
@@ -6,12 +6,12 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import type {
9
- AVLTreeMultiMapNested,
10
9
  AVLTreeMultiMapNodeNested,
11
10
  AVLTreeMultiMapOptions,
12
11
  BinaryTreeDeleteResult,
13
12
  BSTNOptKeyOrNode,
14
13
  BTNRep,
14
+ EntryCallback,
15
15
  IterationType
16
16
  } from '../../types';
17
17
  import { IBinaryTree } from '../../interfaces';
@@ -64,17 +64,10 @@ export class AVLTreeMultiMap<
64
64
  K = any,
65
65
  V = any,
66
66
  R = object,
67
- NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>,
68
- TREE extends AVLTreeMultiMap<K, V, R, NODE, TREE> = AVLTreeMultiMap<
69
- K,
70
- V,
71
- R,
72
- NODE,
73
- AVLTreeMultiMapNested<K, V, R, NODE>
74
- >
67
+ NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>
75
68
  >
76
- extends AVLTree<K, V, R, NODE, TREE>
77
- implements IBinaryTree<K, V, R, NODE, TREE>
69
+ extends AVLTree<K, V, R, NODE>
70
+ implements IBinaryTree<K, V, R, NODE>
78
71
  {
79
72
  /**
80
73
  * The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
@@ -139,15 +132,16 @@ export class AVLTreeMultiMap<
139
132
  * @returns a new instance of the AVLTreeMultiMap class, with the specified options, as a TREE
140
133
  * object.
141
134
  */
142
- override createTree(options?: AVLTreeMultiMapOptions<K, V, R>): TREE {
143
- return new AVLTreeMultiMap<K, V, R, NODE, TREE>([], {
135
+ // @ts-ignore
136
+ override createTree(options?: AVLTreeMultiMapOptions<K, V, R>) {
137
+ return new AVLTreeMultiMap<K, V, R, NODE>([], {
144
138
  iterationType: this.iterationType,
145
139
  isMapMode: this._isMapMode,
146
- extractComparable: this._extractComparable,
140
+ specifyComparable: this._specifyComparable,
147
141
  toEntryFn: this._toEntryFn,
148
142
  isReverse: this._isReverse,
149
143
  ...options
150
- }) as TREE;
144
+ });
151
145
  }
152
146
 
153
147
  /**
@@ -161,44 +155,6 @@ export class AVLTreeMultiMap<
161
155
  return keyNodeEntryOrRaw instanceof AVLTreeMultiMapNode;
162
156
  }
163
157
 
164
- /**
165
- * The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
166
- * a node object.
167
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
168
- * `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`.
169
- * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
170
- * `override` function. It represents the value associated with the key in the data structure. If no
171
- * value is provided, it will default to `undefined`.
172
- * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
173
- * times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
174
- * @returns either a NODE object or undefined.
175
- */
176
- override keyValueNodeEntryRawToNodeAndValue(
177
- keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
178
- value?: V,
179
- count = 1
180
- ): [NODE | undefined, V | undefined] {
181
- if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) return [undefined, undefined];
182
- if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
183
-
184
- if (this.isEntry(keyNodeEntryOrRaw)) {
185
- const [key, entryValue] = keyNodeEntryOrRaw;
186
- if (key === undefined || key === null) return [undefined, undefined];
187
- const finalValue = value ?? entryValue;
188
- return [this.createNode(key, finalValue, count), finalValue];
189
- }
190
-
191
- if (this.isRaw(keyNodeEntryOrRaw)) {
192
- const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw);
193
- const finalValue = value ?? entryValue;
194
- if (this.isKey(key)) return [this.createNode(key, finalValue, count), finalValue];
195
- }
196
-
197
- if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, count), value];
198
-
199
- return [undefined, undefined];
200
- }
201
-
202
158
  /**
203
159
  * Time Complexity: O(log n)
204
160
  * Space Complexity: O(1)
@@ -217,7 +173,7 @@ export class AVLTreeMultiMap<
217
173
  * @returns a boolean value.
218
174
  */
219
175
  override add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count = 1): boolean {
220
- const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
176
+ const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
221
177
  if (newNode === undefined) return false;
222
178
 
223
179
  const orgNodeCount = newNode?.count || 0;
@@ -374,7 +330,8 @@ export class AVLTreeMultiMap<
374
330
  * The function overrides the clone method to create a deep copy of a tree object.
375
331
  * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
376
332
  */
377
- override clone(): TREE {
333
+ // @ts-ignore
334
+ override clone() {
378
335
  const cloned = this.createTree();
379
336
  if (this._isMapMode) this.bfs(node => cloned.add(node.key, undefined, node.count));
380
337
  else this.bfs(node => cloned.add(node.key, node.value, node.count));
@@ -382,6 +339,76 @@ export class AVLTreeMultiMap<
382
339
  return cloned;
383
340
  }
384
341
 
342
+ /**
343
+ * The `map` function in TypeScript overrides the default behavior to create a new AVLTreeMultiMap
344
+ * with modified entries based on a provided callback.
345
+ * @param callback - The `callback` parameter is a function that will be called for each entry in the
346
+ * AVLTreeMultiMap. It takes four arguments:
347
+ * @param [options] - The `options` parameter in the `override map` function is of type
348
+ * `AVLTreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional
349
+ * configuration options when creating a new `AVLTreeMultiMap` instance within the `map` function.
350
+ * These options
351
+ * @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
352
+ * the value of `this` when executing the `callback` function. It allows you to set the context
353
+ * (value of `this`) for the callback function. This can be useful when you want to access properties
354
+ * or
355
+ * @returns The `map` method is returning a new `AVLTreeMultiMap` instance with the entries
356
+ * transformed by the provided `callback` function. Each entry in the original tree is passed to the
357
+ * `callback` function along with the index and the original tree itself. The transformed entries are
358
+ * then added to the new `AVLTreeMultiMap` instance, which is returned at the end.
359
+ */
360
+ // @ts-ignore
361
+ override map<MK, MV, MR>(
362
+ callback: EntryCallback<K, V | undefined, [MK, MV]>,
363
+ options?: AVLTreeMultiMapOptions<MK, MV, MR>,
364
+ thisArg?: any
365
+ ) {
366
+ const newTree = new AVLTreeMultiMap<MK, MV, MR>([], options);
367
+ let index = 0;
368
+ for (const [key, value] of this) {
369
+ newTree.add(callback.call(thisArg, key, value, index++, this));
370
+ }
371
+ return newTree;
372
+ }
373
+
374
+ /**
375
+ * The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
376
+ * a node object.
377
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
378
+ * `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`.
379
+ * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
380
+ * `override` function. It represents the value associated with the key in the data structure. If no
381
+ * value is provided, it will default to `undefined`.
382
+ * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
383
+ * times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
384
+ * @returns either a NODE object or undefined.
385
+ */
386
+ protected override _keyValueNodeEntryRawToNodeAndValue(
387
+ keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
388
+ value?: V,
389
+ count = 1
390
+ ): [NODE | undefined, V | undefined] {
391
+ if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) return [undefined, undefined];
392
+ if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
393
+
394
+ if (this.isEntry(keyNodeEntryOrRaw)) {
395
+ const [key, entryValue] = keyNodeEntryOrRaw;
396
+ if (key === undefined || key === null) return [undefined, undefined];
397
+ const finalValue = value ?? entryValue;
398
+ return [this.createNode(key, finalValue, count), finalValue];
399
+ }
400
+
401
+ if (this.isRaw(keyNodeEntryOrRaw)) {
402
+ const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw);
403
+ const finalValue = value ?? entryValue;
404
+ if (this.isKey(key)) return [this.createNode(key, finalValue, count), finalValue];
405
+ }
406
+
407
+ if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, count), value];
408
+
409
+ return [undefined, undefined];
410
+ }
411
+
385
412
  /**
386
413
  * Time Complexity: O(1)
387
414
  * Space Complexity: O(1)
@@ -7,12 +7,12 @@
7
7
  */
8
8
  import { BST, BSTNode } from './bst';
9
9
  import type {
10
- AVLTreeNested,
11
10
  AVLTreeNodeNested,
12
11
  AVLTreeOptions,
13
12
  BinaryTreeDeleteResult,
14
13
  BSTNOptKeyOrNode,
15
- BTNRep
14
+ BTNRep,
15
+ EntryCallback
16
16
  } from '../../types';
17
17
  import { IBinaryTree } from '../../interfaces';
18
18
 
@@ -67,11 +67,10 @@ export class AVLTree<
67
67
  K = any,
68
68
  V = any,
69
69
  R = object,
70
- NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>,
71
- TREE extends AVLTree<K, V, R, NODE, TREE> = AVLTree<K, V, R, NODE, AVLTreeNested<K, V, R, NODE>>
70
+ NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>
72
71
  >
73
- extends BST<K, V, R, NODE, TREE>
74
- implements IBinaryTree<K, V, R, NODE, TREE>
72
+ extends BST<K, V, R, NODE>
73
+ implements IBinaryTree<K, V, R, NODE>
75
74
  {
76
75
  /**
77
76
  * This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
@@ -103,21 +102,25 @@ export class AVLTree<
103
102
  }
104
103
 
105
104
  /**
106
- * The function creates a new AVL tree with the specified options and returns it.
107
- * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
108
- * passed to the `createTree` function. It is used to customize the behavior of the AVL tree that is
109
- * being created.
110
- * @returns a new AVLTree object.
105
+ * The function `createTree` in TypeScript overrides the default AVLTree creation with the provided
106
+ * options.
107
+ * @param [options] - The `options` parameter in the `createTree` function is an object that contains
108
+ * configuration options for creating an AVL tree. These options can include properties such as
109
+ * `iterationType`, `isMapMode`, `specifyComparable`, `toEntryFn`, and `isReverse`. The function
110
+ * creates a
111
+ * @returns An AVLTree object is being returned with the specified options and properties inherited
112
+ * from the current object.
111
113
  */
112
- override createTree(options?: AVLTreeOptions<K, V, R>): TREE {
113
- return new AVLTree<K, V, R, NODE, TREE>([], {
114
+ // @ts-ignore
115
+ override createTree(options?: AVLTreeOptions<K, V, R>) {
116
+ return new AVLTree<K, V, R, NODE>([], {
114
117
  iterationType: this.iterationType,
115
118
  isMapMode: this._isMapMode,
116
- extractComparable: this._extractComparable,
119
+ specifyComparable: this._specifyComparable,
117
120
  toEntryFn: this._toEntryFn,
118
121
  isReverse: this._isReverse,
119
122
  ...options
120
- }) as TREE;
123
+ });
121
124
  }
122
125
 
123
126
  /**
@@ -174,6 +177,20 @@ export class AVLTree<
174
177
  return deletedResults;
175
178
  }
176
179
 
180
+ // @ts-ignore
181
+ override map<MK, MV, MR>(
182
+ callback: EntryCallback<K, V | undefined, [MK, MV]>,
183
+ options?: AVLTreeOptions<MK, MV, MR>,
184
+ thisArg?: any
185
+ ) {
186
+ const newTree = new AVLTree<MK, MV, MR>([], options);
187
+ let index = 0;
188
+ for (const [key, value] of this) {
189
+ newTree.add(callback.call(thisArg, key, value, index++, this));
190
+ }
191
+ return newTree;
192
+ }
193
+
177
194
  /**
178
195
  * Time Complexity: O(1)
179
196
  * Space Complexity: O(1)
@@ -8,7 +8,6 @@
8
8
 
9
9
  import {
10
10
  BinaryTreeDeleteResult,
11
- BinaryTreeNested,
12
11
  BinaryTreeNodeNested,
13
12
  BinaryTreeOptions,
14
13
  BinaryTreePrintOptions,
@@ -105,11 +104,10 @@ export class BinaryTree<
105
104
  K = any,
106
105
  V = any,
107
106
  R = object,
108
- NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>,
109
- TREE extends BinaryTree<K, V, R, NODE, TREE> = BinaryTree<K, V, R, NODE, BinaryTreeNested<K, V, R, NODE>>
107
+ NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>
110
108
  >
111
109
  extends IterableEntryBase<K, V | undefined>
112
- implements IBinaryTree<K, V, R, NODE, TREE>
110
+ implements IBinaryTree<K, V, R, NODE>
113
111
  {
114
112
  iterationType: IterationType = 'ITERATIVE';
115
113
 
@@ -172,6 +170,9 @@ export class BinaryTree<
172
170
  }
173
171
 
174
172
  /**
173
+ * Time Complexity: O(1)
174
+ * Space Complexity: O(1)
175
+ *
175
176
  * The function creates a new binary tree node with a specified key and optional value.
176
177
  * @param {K} key - The `key` parameter is the key of the node being created in the binary tree.
177
178
  * @param {V} [value] - The `value` parameter in the `createNode` function is optional, meaning it is
@@ -185,63 +186,26 @@ export class BinaryTree<
185
186
  }
186
187
 
187
188
  /**
188
- * The function creates a binary tree with the specified options.
189
- * @param [options] - The `options` parameter in the `createTree` function is an optional parameter
190
- * that allows you to provide partial configuration options for creating a binary tree. It is of type
191
- * `Partial<BinaryTreeOptions<K, V, R>>`, which means you can pass in an object containing a subset
192
- * of properties
193
- * @returns A new instance of a binary tree with the specified options is being returned.
189
+ * Time Complexity: O(1)
190
+ * Space Complexity: O(1)
191
+ *
192
+ * The `createTree` function creates a new binary tree based on the provided options.
193
+ * @param [options] - The `options` parameter in the `createTree` method is of type
194
+ * `BinaryTreeOptions<K, V, R>`. This type likely contains configuration options for creating a
195
+ * binary tree, such as the iteration type, whether the tree is in map mode, and functions for
196
+ * converting entries.
197
+ * @returns The `createTree` method is returning an instance of the `BinaryTree` class with the
198
+ * provided options. The method is creating a new `BinaryTree` object with an empty array as the
199
+ * initial data, and then setting various options such as `iterationType`, `isMapMode`, and
200
+ * `toEntryFn` based on the current object's properties and the provided `options`. Finally, it
194
201
  */
195
- createTree(options?: BinaryTreeOptions<K, V, R>): TREE {
196
- return new BinaryTree<K, V, R, NODE, TREE>([], {
202
+ createTree(options?: BinaryTreeOptions<K, V, R>): typeof this {
203
+ return new BinaryTree<K, V, R>([], {
197
204
  iterationType: this.iterationType,
198
205
  isMapMode: this._isMapMode,
199
206
  toEntryFn: this._toEntryFn,
200
207
  ...options
201
- }) as TREE;
202
- }
203
-
204
- /**
205
- * The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
206
- * or returns null.
207
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
208
- * `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeEntryOrRaw`, which
209
- * can be of type `BTNRep<K, V, NODE>` or `R`. This parameter represents either a key, a
210
- * node, an entry
211
- * @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
212
- * an optional parameter of type `V`. It represents the value associated with the key in the node
213
- * being created. If a `value` is provided, it will be used when creating the node. If
214
- * @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
215
- * (`OptNodeOrNull<NODE>`) based on the input parameters provided. The function checks the type of the
216
- * input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null
217
- * value.
218
- */
219
- keyValueNodeEntryRawToNodeAndValue(
220
- keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
221
- value?: V
222
- ): [OptNodeOrNull<NODE>, V | undefined] {
223
- if (keyNodeEntryOrRaw === undefined) return [undefined, undefined];
224
- if (keyNodeEntryOrRaw === null) return [null, undefined];
225
-
226
- if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
227
-
228
- if (this.isEntry(keyNodeEntryOrRaw)) {
229
- const [key, entryValue] = keyNodeEntryOrRaw;
230
- if (key === undefined) return [undefined, undefined];
231
- else if (key === null) return [null, undefined];
232
- const finalValue = value ?? entryValue;
233
- return [this.createNode(key, finalValue), finalValue];
234
- }
235
-
236
- if (this.isRaw(keyNodeEntryOrRaw)) {
237
- const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw);
238
- const finalValue = value ?? entryValue;
239
- if (this.isKey(key)) return [this.createNode(key, finalValue), finalValue];
240
- }
241
-
242
- if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value), value];
243
-
244
- return [undefined, undefined];
208
+ }) as unknown as typeof this;
245
209
  }
246
210
 
247
211
  /**
@@ -420,7 +384,7 @@ export class BinaryTree<
420
384
  * key was found and the node was replaced instead of inserted.
421
385
  */
422
386
  add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): boolean {
423
- const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
387
+ const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
424
388
  if (newNode === undefined) return false;
425
389
 
426
390
  // If the tree is empty, directly set the new node as the root node
@@ -526,7 +490,7 @@ export class BinaryTree<
526
490
  * elements from the other tree.
527
491
  * @param anotherTree - `BinaryTree<K, V, R, NODE, TREE>`
528
492
  */
529
- merge(anotherTree: BinaryTree<K, V, R, NODE, TREE>) {
493
+ merge(anotherTree: this) {
530
494
  this.addMany(anotherTree, []);
531
495
  }
532
496
 
@@ -1635,7 +1599,7 @@ export class BinaryTree<
1635
1599
  * original tree using breadth-first search (bfs), and adds the nodes to the new tree. If a node in
1636
1600
  * the original tree is null, a null node is added to the cloned tree. If a node
1637
1601
  */
1638
- clone(): TREE {
1602
+ clone() {
1639
1603
  const cloned = this.createTree();
1640
1604
  this.bfs(
1641
1605
  node => {
@@ -1673,7 +1637,7 @@ export class BinaryTree<
1673
1637
  const newTree = this.createTree();
1674
1638
  let index = 0;
1675
1639
  for (const [key, value] of this) {
1676
- if (predicate.call(thisArg, value, key, index++, this)) {
1640
+ if (predicate.call(thisArg, key, value, index++, this)) {
1677
1641
  newTree.add([key, value]);
1678
1642
  }
1679
1643
  }
@@ -1684,36 +1648,34 @@ export class BinaryTree<
1684
1648
  * Time Complexity: O(n)
1685
1649
  * Space Complexity: O(n)
1686
1650
  *
1687
- * The `map` function iterates over key-value pairs in a tree data structure, applies a callback
1688
- * function to each value, and returns a new tree with the updated values.
1689
- * @param callback - The `callback` parameter in the `map` method is a function that will be called
1690
- * on each entry in the tree. It takes four arguments:
1691
- * @param {any} [thisArg] - The `thisArg` parameter in the `map` function is an optional parameter
1692
- * that specifies the value to be passed as `this` when executing the callback function. If provided,
1693
- * the `thisArg` value will be used as the `this` value within the callback function. If `thisArg
1694
- * @returns The `map` method is returning a new tree with the entries modified by the provided
1695
- * callback function. Each entry in the original tree is passed to the callback function, and the
1696
- * result of the callback function is added to the new tree.
1651
+ * The `map` function in TypeScript creates a new BinaryTree by applying a callback function to each
1652
+ * entry in the original BinaryTree.
1653
+ * @param callback - A function that will be called for each entry in the current binary tree. It
1654
+ * takes the key, value (which can be undefined), and an array containing the mapped key and value as
1655
+ * arguments.
1656
+ * @param [options] - The `options` parameter in the `map` method is of type `BinaryTreeOptions<MK,
1657
+ * MV, MR>`. It is an optional parameter that allows you to specify additional options for the binary
1658
+ * tree being created during the mapping process. These options could include things like custom
1659
+ * comparators, initial
1660
+ * @param {any} [thisArg] - The `thisArg` parameter in the `map` method is used to specify the value
1661
+ * of `this` when executing the `callback` function. It allows you to set the context (value of
1662
+ * `this`) within the callback function. If `thisArg` is provided, it will be passed
1663
+ * @returns The `map` function is returning a new `BinaryTree` instance filled with entries that are
1664
+ * the result of applying the provided `callback` function to each entry in the original tree.
1697
1665
  */
1698
- map(callback: EntryCallback<K, V | undefined, V>, thisArg?: any) {
1699
- const newTree = this.createTree();
1666
+ map<MK, MV, MR>(
1667
+ callback: EntryCallback<K, V | undefined, [MK, MV]>,
1668
+ options?: BinaryTreeOptions<MK, MV, MR>,
1669
+ thisArg?: any
1670
+ ) {
1671
+ const newTree = new BinaryTree<MK, MV, MR>([], options);
1700
1672
  let index = 0;
1701
1673
  for (const [key, value] of this) {
1702
- newTree.add([key, callback.call(thisArg, value, key, index++, this)]);
1674
+ newTree.add(callback.call(thisArg, key, value, index++, this));
1703
1675
  }
1704
1676
  return newTree;
1705
1677
  }
1706
1678
 
1707
- // // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
1708
- // // map<NV>(callback: (entry: [K, V | undefined], tree: this) => NV) {
1709
- // // const newTree = this.createTree();
1710
- // // for (const [key, value] of this) {
1711
- // // newTree.add(key, callback([key, value], this));
1712
- // // }
1713
- // // return newTree;
1714
- // // }
1715
- //
1716
-
1717
1679
  /**
1718
1680
  * Time Complexity: O(n)
1719
1681
  * Space Complexity: O(n)
@@ -1743,7 +1705,7 @@ export class BinaryTree<
1743
1705
  if (opts.isShowRedBlackNIL) output += `S for Sentinel Node(NIL)\n`;
1744
1706
 
1745
1707
  const display = (root: OptNodeOrNull<NODE>): void => {
1746
- const [lines, , ,] = this._displayAux(root, opts);
1708
+ const [lines, ,] = this._displayAux(root, opts);
1747
1709
  let paragraph = '';
1748
1710
  for (const line of lines) {
1749
1711
  paragraph += line + '\n';
@@ -1774,6 +1736,49 @@ export class BinaryTree<
1774
1736
  console.log(this.toVisual(startNode, options));
1775
1737
  }
1776
1738
 
1739
+ /**
1740
+ * The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
1741
+ * or returns null.
1742
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
1743
+ * `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeEntryOrRaw`, which
1744
+ * can be of type `BTNRep<K, V, NODE>` or `R`. This parameter represents either a key, a
1745
+ * node, an entry
1746
+ * @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
1747
+ * an optional parameter of type `V`. It represents the value associated with the key in the node
1748
+ * being created. If a `value` is provided, it will be used when creating the node. If
1749
+ * @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
1750
+ * (`OptNodeOrNull<NODE>`) based on the input parameters provided. The function checks the type of the
1751
+ * input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null
1752
+ * value.
1753
+ */
1754
+ protected _keyValueNodeEntryRawToNodeAndValue(
1755
+ keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
1756
+ value?: V
1757
+ ): [OptNodeOrNull<NODE>, V | undefined] {
1758
+ if (keyNodeEntryOrRaw === undefined) return [undefined, undefined];
1759
+ if (keyNodeEntryOrRaw === null) return [null, undefined];
1760
+
1761
+ if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
1762
+
1763
+ if (this.isEntry(keyNodeEntryOrRaw)) {
1764
+ const [key, entryValue] = keyNodeEntryOrRaw;
1765
+ if (key === undefined) return [undefined, undefined];
1766
+ else if (key === null) return [null, undefined];
1767
+ const finalValue = value ?? entryValue;
1768
+ return [this.createNode(key, finalValue), finalValue];
1769
+ }
1770
+
1771
+ if (this.isRaw(keyNodeEntryOrRaw)) {
1772
+ const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw);
1773
+ const finalValue = value ?? entryValue;
1774
+ if (this.isKey(key)) return [this.createNode(key, finalValue), finalValue];
1775
+ }
1776
+
1777
+ if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value), value];
1778
+
1779
+ return [undefined, undefined];
1780
+ }
1781
+
1777
1782
  /**
1778
1783
  * Time complexity: O(n)
1779
1784
  * Space complexity: O(n)