avl-tree-typed 1.53.8 → 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 (48) hide show
  1. package/dist/data-structures/base/iterable-entry-base.js +4 -4
  2. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +34 -27
  3. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +62 -52
  4. package/dist/data-structures/binary-tree/avl-tree.d.ts +3 -14
  5. package/dist/data-structures/binary-tree/avl-tree.js +22 -25
  6. package/dist/data-structures/binary-tree/binary-tree.d.ts +32 -16
  7. package/dist/data-structures/binary-tree/binary-tree.js +43 -24
  8. package/dist/data-structures/binary-tree/bst.d.ts +14 -23
  9. package/dist/data-structures/binary-tree/bst.js +28 -29
  10. package/dist/data-structures/binary-tree/red-black-tree.d.ts +26 -16
  11. package/dist/data-structures/binary-tree/red-black-tree.js +43 -59
  12. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +18 -14
  13. package/dist/data-structures/binary-tree/tree-multi-map.js +36 -23
  14. package/dist/data-structures/graph/abstract-graph.js +2 -2
  15. package/dist/data-structures/hash/hash-map.d.ts +1 -1
  16. package/dist/data-structures/hash/hash-map.js +5 -5
  17. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +10 -10
  18. package/dist/data-structures/linked-list/doubly-linked-list.js +12 -12
  19. package/dist/data-structures/linked-list/singly-linked-list.d.ts +10 -10
  20. package/dist/data-structures/linked-list/singly-linked-list.js +16 -16
  21. package/dist/interfaces/binary-tree.d.ts +1 -1
  22. package/dist/types/data-structures/base/base.d.ts +1 -1
  23. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
  24. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
  25. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
  26. package/dist/types/data-structures/binary-tree/bst.d.ts +3 -3
  27. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
  28. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -4
  29. package/package.json +2 -2
  30. package/src/data-structures/base/iterable-entry-base.ts +4 -4
  31. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +83 -64
  32. package/src/data-structures/binary-tree/avl-tree.ts +40 -34
  33. package/src/data-structures/binary-tree/binary-tree.ts +74 -30
  34. package/src/data-structures/binary-tree/bst.ts +57 -43
  35. package/src/data-structures/binary-tree/red-black-tree.ts +66 -70
  36. package/src/data-structures/binary-tree/tree-multi-map.ts +56 -30
  37. package/src/data-structures/graph/abstract-graph.ts +2 -2
  38. package/src/data-structures/hash/hash-map.ts +7 -7
  39. package/src/data-structures/linked-list/doubly-linked-list.ts +13 -13
  40. package/src/data-structures/linked-list/singly-linked-list.ts +17 -17
  41. package/src/interfaces/binary-tree.ts +4 -1
  42. package/src/types/data-structures/base/base.ts +1 -1
  43. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -2
  44. package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
  45. package/src/types/data-structures/binary-tree/binary-tree.ts +2 -2
  46. package/src/types/data-structures/binary-tree/bst.ts +3 -3
  47. package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
  48. package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -4
@@ -12,7 +12,8 @@ import type {
12
12
  AVLTreeOptions,
13
13
  BinaryTreeDeleteResult,
14
14
  BSTNOptKeyOrNode,
15
- BTNRep
15
+ BTNRep,
16
+ EntryCallback
16
17
  } from '../../types';
17
18
  import { IBinaryTree } from '../../interfaces';
18
19
 
@@ -31,26 +32,6 @@ export class AVLTreeNode<
31
32
  */
32
33
  constructor(key: K, value?: V) {
33
34
  super(key, value);
34
- this._height = 0;
35
- }
36
-
37
- protected _height: number;
38
-
39
- /**
40
- * The function returns the value of the height property.
41
- * @returns The height of the object.
42
- */
43
- get height(): number {
44
- return this._height;
45
- }
46
-
47
- /**
48
- * The above function sets the value of the height property.
49
- * @param {number} value - The value parameter is a number that represents the new height value to be
50
- * set.
51
- */
52
- set height(value: number) {
53
- this._height = value;
54
35
  }
55
36
  }
56
37
 
@@ -67,11 +48,23 @@ export class AVLTree<
67
48
  K = any,
68
49
  V = any,
69
50
  R = object,
51
+ MK = any,
52
+ MV = any,
53
+ MR = object,
70
54
  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>>
55
+ TREE extends AVLTree<K, V, R, MK, MV, MR, NODE, TREE> = AVLTree<
56
+ K,
57
+ V,
58
+ R,
59
+ MK,
60
+ MV,
61
+ MR,
62
+ NODE,
63
+ AVLTreeNested<K, V, R, MK, MV, MR, NODE>
64
+ >
72
65
  >
73
- extends BST<K, V, R, NODE, TREE>
74
- implements IBinaryTree<K, V, R, NODE, TREE>
66
+ extends BST<K, V, R, MK, MV, MR, NODE, TREE>
67
+ implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE>
75
68
  {
76
69
  /**
77
70
  * This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
@@ -110,10 +103,10 @@ export class AVLTree<
110
103
  * @returns a new AVLTree object.
111
104
  */
112
105
  override createTree(options?: AVLTreeOptions<K, V, R>): TREE {
113
- return new AVLTree<K, V, R, NODE, TREE>([], {
106
+ return new AVLTree<K, V, R, MK, MV, MR, NODE, TREE>([], {
114
107
  iterationType: this.iterationType,
115
108
  isMapMode: this._isMapMode,
116
- extractComparable: this._extractComparable,
109
+ specifyComparable: this._specifyComparable,
117
110
  toEntryFn: this._toEntryFn,
118
111
  isReverse: this._isReverse,
119
112
  ...options
@@ -174,6 +167,19 @@ export class AVLTree<
174
167
  return deletedResults;
175
168
  }
176
169
 
170
+ override map(
171
+ callback: EntryCallback<K, V | undefined, [MK, MV]>,
172
+ options?: AVLTreeOptions<MK, MV, MR>,
173
+ thisArg?: any
174
+ ): AVLTree<MK, MV, MR> {
175
+ const newTree = new AVLTree<MK, MV, MR>([], options);
176
+ let index = 0;
177
+ for (const [key, value] of this) {
178
+ newTree.add(callback.call(thisArg, key, value, index++, this));
179
+ }
180
+ return newTree;
181
+ }
182
+
177
183
  /**
178
184
  * Time Complexity: O(1)
179
185
  * Space Complexity: O(1)
@@ -262,7 +268,7 @@ export class AVLTree<
262
268
  protected _balanceLL(A: NODE): void {
263
269
  const parentOfA = A.parent;
264
270
  const B = A.left;
265
- A.parent = B;
271
+ if (B !== null) A.parent = B;
266
272
  if (B && B.right) {
267
273
  B.right.parent = A;
268
274
  }
@@ -299,12 +305,12 @@ export class AVLTree<
299
305
  if (B) {
300
306
  C = B.right;
301
307
  }
302
- if (A) A.parent = C;
303
- if (B) B.parent = C;
308
+ if (A && C !== null) A.parent = C;
309
+ if (B && C !== null) B.parent = C;
304
310
 
305
311
  if (C) {
306
312
  if (C.left) {
307
- C.left.parent = B;
313
+ if (B !== null) C.left.parent = B;
308
314
  }
309
315
  if (C.right) {
310
316
  C.right.parent = A;
@@ -346,7 +352,7 @@ export class AVLTree<
346
352
  protected _balanceRR(A: NODE): void {
347
353
  const parentOfA = A.parent;
348
354
  const B = A.right;
349
- A.parent = B;
355
+ if (B !== null) A.parent = B;
350
356
  if (B) {
351
357
  if (B.left) {
352
358
  B.left.parent = A;
@@ -389,15 +395,15 @@ export class AVLTree<
389
395
  C = B.left;
390
396
  }
391
397
 
392
- A.parent = C;
393
- if (B) B.parent = C;
398
+ if (C !== null) A.parent = C;
399
+ if (B && C !== null) B.parent = C;
394
400
 
395
401
  if (C) {
396
402
  if (C.left) {
397
403
  C.left.parent = A;
398
404
  }
399
405
  if (C.right) {
400
- C.right.parent = B;
406
+ if (B !== null) C.right.parent = B;
401
407
  }
402
408
  C.parent = parentOfA;
403
409
  }
@@ -23,6 +23,7 @@ import {
23
23
  NodeDisplayLayout,
24
24
  NodePredicate,
25
25
  OptNodeOrNull,
26
+ type RBTNColor,
26
27
  ToEntryFn
27
28
  } from '../../types';
28
29
  import { IBinaryTree } from '../../interfaces';
@@ -52,7 +53,7 @@ export class BinaryTreeNode<
52
53
  this.value = value;
53
54
  }
54
55
 
55
- protected _left?: OptNodeOrNull<NODE>;
56
+ _left?: OptNodeOrNull<NODE>;
56
57
 
57
58
  get left(): OptNodeOrNull<NODE> {
58
59
  return this._left;
@@ -65,7 +66,7 @@ export class BinaryTreeNode<
65
66
  this._left = v;
66
67
  }
67
68
 
68
- protected _right?: OptNodeOrNull<NODE>;
69
+ _right?: OptNodeOrNull<NODE>;
69
70
 
70
71
  get right(): OptNodeOrNull<NODE> {
71
72
  return this._right;
@@ -78,6 +79,36 @@ export class BinaryTreeNode<
78
79
  this._right = v;
79
80
  }
80
81
 
82
+ _height: number = 0;
83
+
84
+ get height(): number {
85
+ return this._height;
86
+ }
87
+
88
+ set height(value: number) {
89
+ this._height = value;
90
+ }
91
+
92
+ _color: RBTNColor = 'BLACK';
93
+
94
+ get color(): RBTNColor {
95
+ return this._color;
96
+ }
97
+
98
+ set color(value: RBTNColor) {
99
+ this._color = value;
100
+ }
101
+
102
+ _count: number = 1;
103
+
104
+ get count(): number {
105
+ return this._count;
106
+ }
107
+
108
+ set count(value: number) {
109
+ this._count = value;
110
+ }
111
+
81
112
  get familyPosition(): FamilyPosition {
82
113
  const that = this as unknown as NODE;
83
114
  if (!this.parent) {
@@ -105,11 +136,23 @@ export class BinaryTree<
105
136
  K = any,
106
137
  V = any,
107
138
  R = object,
139
+ MK = any,
140
+ MV = any,
141
+ MR = object,
108
142
  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>>
143
+ TREE extends BinaryTree<K, V, R, MK, MV, MR, NODE, TREE> = BinaryTree<
144
+ K,
145
+ V,
146
+ R,
147
+ MK,
148
+ MV,
149
+ MR,
150
+ NODE,
151
+ BinaryTreeNested<K, V, R, MK, MV, MR, NODE>
152
+ >
110
153
  >
111
154
  extends IterableEntryBase<K, V | undefined>
112
- implements IBinaryTree<K, V, R, NODE, TREE>
155
+ implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE>
113
156
  {
114
157
  iterationType: IterationType = 'ITERATIVE';
115
158
 
@@ -172,6 +215,9 @@ export class BinaryTree<
172
215
  }
173
216
 
174
217
  /**
218
+ * Time Complexity: O(1)
219
+ * Space Complexity: O(1)
220
+ *
175
221
  * The function creates a new binary tree node with a specified key and optional value.
176
222
  * @param {K} key - The `key` parameter is the key of the node being created in the binary tree.
177
223
  * @param {V} [value] - The `value` parameter in the `createNode` function is optional, meaning it is
@@ -193,7 +239,7 @@ export class BinaryTree<
193
239
  * @returns A new instance of a binary tree with the specified options is being returned.
194
240
  */
195
241
  createTree(options?: BinaryTreeOptions<K, V, R>): TREE {
196
- return new BinaryTree<K, V, R, NODE, TREE>([], {
242
+ return new BinaryTree<K, V, R, MK, MV, MR, NODE, TREE>([], {
197
243
  iterationType: this.iterationType,
198
244
  isMapMode: this._isMapMode,
199
245
  toEntryFn: this._toEntryFn,
@@ -1673,7 +1719,7 @@ export class BinaryTree<
1673
1719
  const newTree = this.createTree();
1674
1720
  let index = 0;
1675
1721
  for (const [key, value] of this) {
1676
- if (predicate.call(thisArg, value, key, index++, this)) {
1722
+ if (predicate.call(thisArg, key, value, index++, this)) {
1677
1723
  newTree.add([key, value]);
1678
1724
  }
1679
1725
  }
@@ -1684,36 +1730,34 @@ export class BinaryTree<
1684
1730
  * Time Complexity: O(n)
1685
1731
  * Space Complexity: O(n)
1686
1732
  *
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.
1733
+ * The `map` function in TypeScript creates a new BinaryTree by applying a callback function to each
1734
+ * entry in the original BinaryTree.
1735
+ * @param callback - A function that will be called for each entry in the current binary tree. It
1736
+ * takes the key, value (which can be undefined), and an array containing the mapped key and value as
1737
+ * arguments.
1738
+ * @param [options] - The `options` parameter in the `map` method is of type `BinaryTreeOptions<MK,
1739
+ * MV, MR>`. It is an optional parameter that allows you to specify additional options for the binary
1740
+ * tree being created during the mapping process. These options could include things like custom
1741
+ * comparators, initial
1742
+ * @param {any} [thisArg] - The `thisArg` parameter in the `map` method is used to specify the value
1743
+ * of `this` when executing the `callback` function. It allows you to set the context (value of
1744
+ * `this`) within the callback function. If `thisArg` is provided, it will be passed
1745
+ * @returns The `map` function is returning a new `BinaryTree` instance filled with entries that are
1746
+ * the result of applying the provided `callback` function to each entry in the original tree.
1697
1747
  */
1698
- map(callback: EntryCallback<K, V | undefined, V>, thisArg?: any) {
1699
- const newTree = this.createTree();
1748
+ map(
1749
+ callback: EntryCallback<K, V | undefined, [MK, MV]>,
1750
+ options?: BinaryTreeOptions<MK, MV, MR>,
1751
+ thisArg?: any
1752
+ ): BinaryTree<MK, MV, MR> {
1753
+ const newTree = new BinaryTree<MK, MV, MR>([], options);
1700
1754
  let index = 0;
1701
1755
  for (const [key, value] of this) {
1702
- newTree.add([key, callback.call(thisArg, value, key, index++, this)]);
1756
+ newTree.add(callback.call(thisArg, key, value, index++, this));
1703
1757
  }
1704
1758
  return newTree;
1705
1759
  }
1706
1760
 
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
1761
  /**
1718
1762
  * Time Complexity: O(n)
1719
1763
  * Space Complexity: O(n)
@@ -1743,7 +1787,7 @@ export class BinaryTree<
1743
1787
  if (opts.isShowRedBlackNIL) output += `S for Sentinel Node(NIL)\n`;
1744
1788
 
1745
1789
  const display = (root: OptNodeOrNull<NODE>): void => {
1746
- const [lines, , ,] = this._displayAux(root, opts);
1790
+ const [lines, ,] = this._displayAux(root, opts);
1747
1791
  let paragraph = '';
1748
1792
  for (const line of lines) {
1749
1793
  paragraph += line + '\n';
@@ -5,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type {
8
+ import {
9
9
  BSTNested,
10
10
  BSTNodeNested,
11
11
  BSTNOptKeyOrNode,
@@ -15,10 +15,12 @@ import type {
15
15
  Comparator,
16
16
  CP,
17
17
  DFSOrderPattern,
18
+ EntryCallback,
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
  }
@@ -155,11 +157,23 @@ export class BST<
155
157
  K = any,
156
158
  V = any,
157
159
  R = object,
160
+ MK = any,
161
+ MV = any,
162
+ MR = object,
158
163
  NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>,
159
- TREE extends BST<K, V, R, NODE, TREE> = BST<K, V, R, NODE, BSTNested<K, V, R, NODE>>
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
+ >
160
174
  >
161
- extends BinaryTree<K, V, R, NODE, TREE>
162
- implements IBinaryTree<K, V, R, NODE, TREE>
175
+ extends BinaryTree<K, V, R, MK, MV, MR, NODE, TREE>
176
+ implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE>
163
177
  {
164
178
  /**
165
179
  * This is the constructor function for a Binary Search Tree class in TypeScript.
@@ -173,8 +187,8 @@ export class BST<
173
187
  super([], options);
174
188
 
175
189
  if (options) {
176
- const { extractComparable, isReverse } = options;
177
- if (typeof extractComparable === 'function') this._extractComparable = extractComparable;
190
+ const { specifyComparable, isReverse } = options;
191
+ if (typeof specifyComparable === 'function') this._specifyComparable = specifyComparable;
178
192
  if (isReverse !== undefined) this._isReverse = isReverse;
179
193
  }
180
194
 
@@ -222,10 +236,10 @@ export class BST<
222
236
  * @returns a new instance of the BST class with the provided options.
223
237
  */
224
238
  override createTree(options?: BSTOptions<K, V, R>): TREE {
225
- return new BST<K, V, R, NODE, TREE>([], {
239
+ return new BST<K, V, R, MK, MV, MR, NODE, TREE>([], {
226
240
  iterationType: this.iterationType,
227
241
  isMapMode: this._isMapMode,
228
- extractComparable: this._extractComparable,
242
+ specifyComparable: this._specifyComparable,
229
243
  toEntryFn: this._toEntryFn,
230
244
  isReverse: this._isReverse,
231
245
  ...options
@@ -292,7 +306,7 @@ export class BST<
292
306
  * this._DEFAULT_COMPARATOR`.
293
307
  */
294
308
  override isKey(key: any): key is K {
295
- return isComparable(key, this._extractComparable !== undefined);
309
+ return isComparable(key, this._specifyComparable !== undefined);
296
310
  }
297
311
 
298
312
  /**
@@ -330,7 +344,7 @@ export class BST<
330
344
  this._size++;
331
345
  return true;
332
346
  }
333
- current = current.left;
347
+ if (current.left !== null) current = current.left;
334
348
  } else {
335
349
  if (current.right === undefined) {
336
350
  current.right = newNode;
@@ -338,7 +352,7 @@ export class BST<
338
352
  this._size++;
339
353
  return true;
340
354
  }
341
- current = current.right;
355
+ if (current.right !== null) current = current.right;
342
356
  }
343
357
  }
344
358
 
@@ -463,19 +477,6 @@ export class BST<
463
477
  return inserted;
464
478
  }
465
479
 
466
- /**
467
- * Time Complexity: O(n)
468
- * Space Complexity: O(1)
469
- *
470
- * The `merge` function overrides the base class method by adding elements from another
471
- * binary search tree.
472
- * @param anotherTree - `anotherTree` is an instance of a Binary Search Tree (BST) with key type `K`,
473
- * value type `V`, return type `R`, node type `NODE`, and tree type `TREE`.
474
- */
475
- override merge(anotherTree: BST<K, V, R, NODE, TREE>) {
476
- this.addMany(anotherTree, [], false);
477
- }
478
-
479
480
  /**
480
481
  * Time Complexity: O(log n)
481
482
  * Space Complexity: O(k + log n)
@@ -886,7 +887,7 @@ export class BST<
886
887
  let balanced = true;
887
888
 
888
889
  if (iterationType === 'RECURSIVE') {
889
- const _height = (cur: OptNode<NODE>): number => {
890
+ const _height = (cur: OptNodeOrNull<NODE>): number => {
890
891
  if (!cur) return 0;
891
892
  const leftHeight = _height(cur.left),
892
893
  rightHeight = _height(cur.right);
@@ -903,7 +904,7 @@ export class BST<
903
904
  while (stack.length > 0 || node) {
904
905
  if (node) {
905
906
  stack.push(node);
906
- node = node.left;
907
+ if (node.left !== null) node = node.left;
907
908
  } else {
908
909
  node = stack[stack.length - 1];
909
910
  if (!node.right || last === node.right) {
@@ -930,14 +931,14 @@ export class BST<
930
931
  if (a < b) return -1;
931
932
  return 0;
932
933
  }
933
- if (this._extractComparable) {
934
- if (this._extractComparable(a) > this._extractComparable(b)) return 1;
935
- if (this._extractComparable(a) < this._extractComparable(b)) return -1;
934
+ if (this._specifyComparable) {
935
+ if (this._specifyComparable(a) > this._specifyComparable(b)) return 1;
936
+ if (this._specifyComparable(a) < this._specifyComparable(b)) return -1;
936
937
  return 0;
937
938
  }
938
939
  if (typeof a === 'object' || typeof b === 'object') {
939
940
  throw TypeError(
940
- `When comparing object types, a custom extractComparable must be defined in the constructor's options parameter.`
941
+ `When comparing object types, a custom specifyComparable must be defined in the constructor's options parameter.`
941
942
  );
942
943
  }
943
944
 
@@ -952,15 +953,15 @@ export class BST<
952
953
  return this._comparator;
953
954
  }
954
955
 
955
- protected _extractComparable?: (key: K) => Comparable;
956
+ protected _specifyComparable?: (key: K) => Comparable;
956
957
 
957
958
  /**
958
- * This function returns the value of the `_extractComparable` property.
959
- * @returns The method `extractComparable()` is being returned, which is a getter method for the
960
- * `_extractComparable` property.
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.
961
962
  */
962
- get extractComparable() {
963
- return this._extractComparable;
963
+ get specifyComparable() {
964
+ return this._specifyComparable;
964
965
  }
965
966
 
966
967
  /**
@@ -978,4 +979,17 @@ export class BST<
978
979
  protected _compare(a: K, b: K) {
979
980
  return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
980
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
+ }
981
995
  }