heap-typed 1.47.6 → 1.47.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +40 -22
  2. package/dist/data-structures/binary-tree/avl-tree.js +45 -36
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +105 -113
  4. package/dist/data-structures/binary-tree/binary-tree.js +133 -119
  5. package/dist/data-structures/binary-tree/bst.d.ts +53 -44
  6. package/dist/data-structures/binary-tree/bst.js +137 -154
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +48 -15
  8. package/dist/data-structures/binary-tree/rb-tree.js +70 -33
  9. package/dist/data-structures/binary-tree/tree-multimap.d.ts +26 -37
  10. package/dist/data-structures/binary-tree/tree-multimap.js +58 -137
  11. package/dist/data-structures/hash/hash-map.d.ts +2 -6
  12. package/dist/data-structures/hash/hash-map.js +5 -8
  13. package/dist/data-structures/trie/trie.d.ts +3 -0
  14. package/dist/data-structures/trie/trie.js +19 -4
  15. package/dist/interfaces/binary-tree.d.ts +3 -3
  16. package/dist/types/common.d.ts +6 -1
  17. package/dist/types/data-structures/hash/hash-map.d.ts +1 -2
  18. package/package.json +2 -2
  19. package/src/data-structures/binary-tree/avl-tree.ts +59 -39
  20. package/src/data-structures/binary-tree/binary-tree.ts +192 -180
  21. package/src/data-structures/binary-tree/bst.ts +157 -154
  22. package/src/data-structures/binary-tree/rb-tree.ts +78 -37
  23. package/src/data-structures/binary-tree/tree-multimap.ts +67 -145
  24. package/src/data-structures/hash/hash-map.ts +8 -8
  25. package/src/data-structures/trie/trie.ts +23 -4
  26. package/src/interfaces/binary-tree.ts +3 -3
  27. package/src/types/common.ts +11 -1
  28. package/src/types/data-structures/hash/hash-map.ts +1 -2
@@ -6,14 +6,21 @@
6
6
  * @license MIT License
7
7
  */
8
8
 
9
- import type { BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, BTNKey } from '../../types';
9
+ import type {
10
+ BinaryTreeNodeNested,
11
+ BinaryTreeOptions,
12
+ BTNCallback,
13
+ BTNKey,
14
+ BTNodeEntry,
15
+ BTNodeExemplar,
16
+ BTNodeKeyOrNode
17
+ } from '../../types';
10
18
  import {
11
19
  BinaryTreeNested,
12
20
  BinaryTreePrintOptions,
13
21
  BiTreeDeleteResult,
14
22
  DFSOrderPattern,
15
23
  FamilyPosition,
16
- IterableEntriesOrKeys,
17
24
  IterationType,
18
25
  NodeDisplayLayout
19
26
  } from '../../types';
@@ -27,26 +34,12 @@ import { Queue } from '../queue';
27
34
  * @template N - The type of the family relationship in the binary tree.
28
35
  */
29
36
  export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>> {
30
- /**
31
- * The key associated with the node.
32
- */
33
37
  key: BTNKey;
34
38
 
35
- /**
36
- * The value stored in the node.
37
- */
38
39
  value?: V;
39
40
 
40
- /**
41
- * The parent node of the current node.
42
- */
43
- parent?: N | null;
41
+ parent?: N;
44
42
 
45
- /**
46
- * Creates a new instance of BinaryTreeNode.
47
- * @param {BTNKey} key - The key associated with the node.
48
- * @param {V} value - The value stored in the node.
49
- */
50
43
  constructor(key: BTNKey, value?: V) {
51
44
  this.key = key;
52
45
  this.value = value;
@@ -54,17 +47,10 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
54
47
 
55
48
  protected _left?: N | null;
56
49
 
57
- /**
58
- * Get the left child node.
59
- */
60
50
  get left(): N | null | undefined {
61
51
  return this._left;
62
52
  }
63
53
 
64
- /**
65
- * Set the left child node.
66
- * @param {N | null | undefined} v - The left child node.
67
- */
68
54
  set left(v: N | null | undefined) {
69
55
  if (v) {
70
56
  v.parent = this as unknown as N;
@@ -74,17 +60,10 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
74
60
 
75
61
  protected _right?: N | null;
76
62
 
77
- /**
78
- * Get the right child node.
79
- */
80
63
  get right(): N | null | undefined {
81
64
  return this._right;
82
65
  }
83
66
 
84
- /**
85
- * Set the right child node.
86
- * @param {N | null | undefined} v - The right child node.
87
- */
88
67
  set right(v: N | null | undefined) {
89
68
  if (v) {
90
69
  v.parent = this as unknown as N;
@@ -113,8 +92,15 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
113
92
  }
114
93
 
115
94
  /**
116
- * Represents a binary tree data structure.
117
- * @template N - The type of the binary tree's nodes.
95
+ * 1. Two Children Maximum: Each node has at most two children.
96
+ * 2. Left and Right Children: Nodes have distinct left and right children.
97
+ * 3. Depth and Height: Depth is the number of edges from the root to a node; height is the maximum depth in the tree.
98
+ * 4. Subtrees: Each child of a node forms the root of a subtree.
99
+ * 5. Leaf Nodes: Nodes without children are leaves.
100
+ * 6. Internal Nodes: Nodes with at least one child are internal.
101
+ * 7. Balanced Trees: The heights of the left and right subtrees of any node differ by no more than one.
102
+ * 8. Full Trees: Every node has either 0 or 2 children.
103
+ * 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
118
104
  */
119
105
  export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>, TREE extends BinaryTree<V, N, TREE> = BinaryTree<V, N, BinaryTreeNested<V, N>>>
120
106
  implements IBinaryTree<V, N, TREE> {
@@ -122,10 +108,15 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
122
108
  iterationType = IterationType.ITERATIVE
123
109
 
124
110
  /**
125
- * Creates a new instance of BinaryTree.
126
- * @param {BinaryTreeOptions} [options] - The options for the binary tree.
111
+ * The constructor function initializes a binary tree object with optional elements and options.
112
+ * @param [elements] - An optional iterable of BTNodeExemplar objects. These objects represent the
113
+ * elements to be added to the binary tree.
114
+ * @param [options] - The `options` parameter is an optional object that can contain additional
115
+ * configuration options for the binary tree. In this case, it is of type
116
+ * `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
117
+ * required.
127
118
  */
128
- constructor(elements?: IterableEntriesOrKeys<V>, options?: Partial<BinaryTreeOptions>) {
119
+ constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<BinaryTreeOptions>) {
129
120
 
130
121
  if (options) {
131
122
  const { iterationType } = options;
@@ -136,23 +127,17 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
136
127
 
137
128
  this._size = 0;
138
129
 
139
- if (elements) this.init(elements);
130
+ if (elements) this.addMany(elements);
140
131
  }
141
132
 
142
133
  protected _root?: N | null;
143
134
 
144
- /**
145
- * Get the root node of the binary tree.
146
- */
147
135
  get root(): N | null | undefined {
148
136
  return this._root;
149
137
  }
150
138
 
151
139
  protected _size: number;
152
140
 
153
- /**
154
- * Get the number of nodes in the binary tree.
155
- */
156
141
  get size(): number {
157
142
  return this._size;
158
143
  }
@@ -167,31 +152,51 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
167
152
  return new BinaryTreeNode<V, N>(key, value) as N;
168
153
  }
169
154
 
155
+ /**
156
+ * The function creates a binary tree with the given options.
157
+ * @param [options] - The `options` parameter is an optional object that allows you to customize the
158
+ * behavior of the `BinaryTree` class. It is of type `Partial<BinaryTreeOptions>`, which means that
159
+ * you can provide only a subset of the properties defined in the `BinaryTreeOptions` interface.
160
+ * @returns a new instance of a binary tree.
161
+ */
170
162
  createTree(options?: Partial<BinaryTreeOptions>): TREE {
171
163
  return new BinaryTree<V, N, TREE>([], { iterationType: this.iterationType, ...options }) as TREE;
172
164
  }
173
165
 
174
166
  /**
175
- * Time Complexity: O(n)
176
- * Space Complexity: O(1)
167
+ * The function checks if a given value is an entry in a binary tree node.
168
+ * @param kne - BTNodeExemplar<V, N> - A generic type representing a node in a binary tree. It has
169
+ * two type parameters V and N, representing the value and node type respectively.
170
+ * @returns a boolean value.
171
+ */
172
+ isEntry(kne: BTNodeExemplar<V, N>): kne is BTNodeEntry<V> {
173
+ return Array.isArray(kne) && kne.length === 2;
174
+ }
175
+
176
+ /**
177
+ * Time Complexity O(log n) - O(n)
178
+ * Space Complexity O(1)
179
+ */
180
+
181
+ /**
182
+ * Time Complexity O(log n) - O(n)
183
+ * Space Complexity O(1)
177
184
  *
178
- * The `add` function adds a new node with a key and value to a binary tree, or updates the value of
179
- * an existing node with the same key.
180
- * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
181
- * following types:
182
- * @param {V} [value] - The value to be associated with the key or node being added to the binary
183
- * tree.
184
- * @returns The function `add` returns a node (`N`) if it was successfully inserted into the binary
185
- * tree, or `null` or `undefined` if the insertion was not successful.
185
+ * The `add` function adds a new node to a binary tree, either by key or by providing a node object.
186
+ * @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be one of the following:
187
+ * @returns The function `add` returns the inserted node (`N`), `null`, or `undefined`.
186
188
  */
187
- add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | null | undefined {
189
+ add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | null | undefined {
190
+
191
+ let inserted: N | null | undefined, needInsert: N | null | undefined;
192
+
188
193
  const _bfs = (root: N, newNode: N | null): N | undefined | null => {
189
194
  const queue = new Queue<N>([root]);
190
195
  while (queue.size > 0) {
191
196
  const cur = queue.shift()!;
192
197
  if (newNode && cur.key === newNode.key) {
193
- cur.value = newNode.value;
194
- return;
198
+ this._replaceNode(cur, newNode);
199
+ return newNode;
195
200
  }
196
201
  const inserted = this._addTo(newNode, cur);
197
202
  if (inserted !== undefined) return inserted;
@@ -200,14 +205,21 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
200
205
  }
201
206
  };
202
207
 
203
- let inserted: N | null | undefined, needInsert: N | null | undefined;
204
-
205
- if (keyOrNode === null) {
208
+ if (keyOrNodeOrEntry === null) {
206
209
  needInsert = null;
207
- } else if (this.isNodeKey(keyOrNode)) {
208
- needInsert = this.createNode(keyOrNode, value);
209
- } else if (keyOrNode instanceof BinaryTreeNode) {
210
- needInsert = keyOrNode;
210
+ } else if (this.isNodeKey(keyOrNodeOrEntry)) {
211
+ needInsert = this.createNode(keyOrNodeOrEntry);
212
+ } else if (keyOrNodeOrEntry instanceof BinaryTreeNode) {
213
+ needInsert = keyOrNodeOrEntry;
214
+ } else if (this.isEntry(keyOrNodeOrEntry)) {
215
+ const [key, value] = keyOrNodeOrEntry;
216
+ if (key === undefined) {
217
+ return;
218
+ } else if (key === null) {
219
+ needInsert = null;
220
+ } else {
221
+ needInsert = this.createNode(key, value);
222
+ }
211
223
  } else {
212
224
  return;
213
225
  }
@@ -227,39 +239,30 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
227
239
  }
228
240
 
229
241
  /**
230
- * Time Complexity: O(n)
242
+ * Time Complexity: O(k log n) - O(k * n)
231
243
  * Space Complexity: O(1)
232
244
  * Comments: The time complexity for adding a node depends on the depth of the tree. In the best case (when the tree is empty), it's O(1). In the worst case (when the tree is a degenerate tree), it's O(n). The space complexity is constant.
233
245
  */
234
246
 
247
+
235
248
  /**
236
- * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
249
+ * Time Complexity: O(k log n) - O(k * n)
237
250
  * Space Complexity: O(1)
238
251
  *
239
- * The `addMany` function takes an array of keys or nodes and an optional array of values, and adds
240
- * each key-value pair to a data structure.
241
- * @param {(BTNKey | N |null | undefined)[]} keysOrNodes - An array of keys or nodes to be added to
242
- * the binary search tree. Each element can be of type `BTNKey` (a key value), `N` (a node), `null`,
243
- * or `undefined`.
244
- * @param {(V | undefined)[]} [values] - The `values` parameter is an optional array of values that
245
- * correspond to the keys or nodes being added. If provided, the values will be associated with the
246
- * keys or nodes during the add operation.
247
- * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
248
- */
249
- addMany(keysOrNodes: (BTNKey | N | null | undefined)[], values?: (V | undefined)[]): (N | null | undefined)[] {
252
+ * The function `addMany` takes in an iterable of `BTNodeExemplar` objects, adds each object to the
253
+ * current instance, and returns an array of the inserted nodes.
254
+ * @param nodes - The `nodes` parameter is an iterable (such as an array or a set) of
255
+ * `BTNodeExemplar<V, N>` objects.
256
+ * @returns The function `addMany` returns an array of values, where each value is either of type
257
+ * `N`, `null`, or `undefined`.
258
+ */
259
+ addMany(nodes: Iterable<BTNodeExemplar<V, N>>): (N | null | undefined)[] {
250
260
  // TODO not sure addMany not be run multi times
251
- return keysOrNodes.map((keyOrNode, i) => {
252
- if (keyOrNode instanceof BinaryTreeNode) {
253
- return this.add(keyOrNode.key, keyOrNode.value);
254
- }
255
-
256
- if (keyOrNode === null) {
257
- return this.add(null);
258
- }
259
-
260
- const value = values?.[i];
261
- return this.add(keyOrNode, value);
262
- });
261
+ const inserted: (N | null | undefined)[] = [];
262
+ for (const kne of nodes) {
263
+ inserted.push(this.add(kne));
264
+ }
265
+ return inserted;
263
266
  }
264
267
 
265
268
  /**
@@ -271,17 +274,13 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
271
274
  * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
272
275
  * Space Complexity: O(1)
273
276
  *
274
- * The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
275
- * @param {(BTNKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
276
- * `BTNKey` or `N` values.
277
- * @param {N[] | Array<V>} [values] - The `data` parameter is an optional array of values that will be assigned to
278
- * the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
279
- * array. Each value in the `data` array will be assigned to the
280
- * @returns The method is returning a boolean value.
281
- */
282
- refill(keysOrNodes: (BTNKey | N | null | undefined)[], values?: (V | undefined)[]): boolean {
277
+ * The `refill` function clears the current collection and adds new nodes, keys, or entries to it.
278
+ * @param nodesOrKeysOrEntries - The parameter `nodesOrKeysOrEntries` is an iterable object that can
279
+ * contain either `BTNodeExemplar` objects, keys, or entries.
280
+ */
281
+ refill(nodesOrKeysOrEntries: Iterable<BTNodeExemplar<V, N>>): void {
283
282
  this.clear();
284
- return keysOrNodes.length === this.addMany(keysOrNodes, values).length;
283
+ this.addMany(nodesOrKeysOrEntries);
285
284
  }
286
285
 
287
286
  /**
@@ -344,7 +343,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
344
343
  const leftSubTreeRightMost = this.getRightMost(curr.left);
345
344
  if (leftSubTreeRightMost) {
346
345
  const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
347
- orgCurrent = this._swap(curr, leftSubTreeRightMost);
346
+ orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
348
347
  if (parentOfLeftSubTreeMax) {
349
348
  if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
350
349
  parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
@@ -378,9 +377,9 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
378
377
  * `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
379
378
  * @returns the depth of the `distNode` relative to the `beginRoot`.
380
379
  */
381
- getDepth(distNode: BTNKey | N | null | undefined, beginRoot: BTNKey | N | null | undefined = this.root): number {
382
- distNode = this.ensureNotKey(distNode);
383
- beginRoot = this.ensureNotKey(beginRoot);
380
+ getDepth(distNode: BTNodeKeyOrNode<N>, beginRoot: BTNodeKeyOrNode<N> = this.root): number {
381
+ distNode = this.ensureNode(distNode);
382
+ beginRoot = this.ensureNode(beginRoot);
384
383
  let depth = 0;
385
384
  while (distNode?.parent) {
386
385
  if (distNode === beginRoot) {
@@ -411,8 +410,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
411
410
  * values:
412
411
  * @returns the height of the binary tree.
413
412
  */
414
- getHeight(beginRoot: BTNKey | N | null | undefined = this.root, iterationType = this.iterationType): number {
415
- beginRoot = this.ensureNotKey(beginRoot);
413
+ getHeight(beginRoot: BTNodeKeyOrNode<N> = this.root, iterationType = this.iterationType): number {
414
+ beginRoot = this.ensureNode(beginRoot);
416
415
  if (!beginRoot) return -1;
417
416
 
418
417
  if (iterationType === IterationType.RECURSIVE) {
@@ -460,8 +459,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
460
459
  * to calculate the minimum height of a binary tree. It can have two possible values:
461
460
  * @returns The function `getMinHeight` returns the minimum height of a binary tree.
462
461
  */
463
- getMinHeight(beginRoot: BTNKey | N | null | undefined = this.root, iterationType = this.iterationType): number {
464
- beginRoot = this.ensureNotKey(beginRoot);
462
+ getMinHeight(beginRoot: BTNodeKeyOrNode<N> = this.root, iterationType = this.iterationType): number {
463
+ beginRoot = this.ensureNode(beginRoot);
465
464
  if (!beginRoot) return -1;
466
465
 
467
466
  if (iterationType === IterationType.RECURSIVE) {
@@ -520,7 +519,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
520
519
  * value of a binary tree node), `N` (a node of a binary tree), `null`, or `undefined`. If
521
520
  * @returns a boolean value.
522
521
  */
523
- isPerfectlyBalanced(beginRoot: BTNKey | N | null | undefined = this.root): boolean {
522
+ isPerfectlyBalanced(beginRoot: BTNodeKeyOrNode<N> = this.root): boolean {
524
523
  return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
525
524
  }
526
525
 
@@ -533,7 +532,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
533
532
  identifier: BTNKey,
534
533
  callback?: C,
535
534
  onlyOne?: boolean,
536
- beginRoot?: BTNKey | N | null | undefined,
535
+ beginRoot?: BTNodeKeyOrNode<N>,
537
536
  iterationType?: IterationType
538
537
  ): N[];
539
538
 
@@ -541,7 +540,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
541
540
  identifier: N | null | undefined,
542
541
  callback?: C,
543
542
  onlyOne?: boolean,
544
- beginRoot?: BTNKey | N | null | undefined,
543
+ beginRoot?: BTNodeKeyOrNode<N>,
545
544
  iterationType?: IterationType
546
545
  ): N[];
547
546
 
@@ -549,7 +548,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
549
548
  identifier: ReturnType<C>,
550
549
  callback: C,
551
550
  onlyOne?: boolean,
552
- beginRoot?: BTNKey | N | null | undefined,
551
+ beginRoot?: BTNodeKeyOrNode<N>,
553
552
  iterationType?: IterationType
554
553
  ): N[];
555
554
 
@@ -582,12 +581,12 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
582
581
  identifier: ReturnType<C> | null | undefined,
583
582
  callback: C = this._defaultOneParamCallback as C,
584
583
  onlyOne = false,
585
- beginRoot: BTNKey | N | null | undefined = this.root,
584
+ beginRoot: BTNodeKeyOrNode<N> = this.root,
586
585
  iterationType = this.iterationType
587
586
  ): N[] {
588
587
  if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
589
588
  callback = (node => node) as C;
590
- beginRoot = this.ensureNotKey(beginRoot);
589
+ beginRoot = this.ensureNode(beginRoot);
591
590
  if (!beginRoot) return [];
592
591
 
593
592
  const ans: N[] = [];
@@ -630,21 +629,21 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
630
629
  has<C extends BTNCallback<N, BTNKey>>(
631
630
  identifier: BTNKey,
632
631
  callback?: C,
633
- beginRoot?: BTNKey | N | null | undefined,
632
+ beginRoot?: BTNodeKeyOrNode<N>,
634
633
  iterationType?: IterationType
635
634
  ): boolean;
636
635
 
637
636
  has<C extends BTNCallback<N, N>>(
638
637
  identifier: N | null | undefined,
639
638
  callback?: C,
640
- beginRoot?: BTNKey | N | null | undefined,
639
+ beginRoot?: BTNodeKeyOrNode<N>,
641
640
  iterationType?: IterationType
642
641
  ): boolean;
643
642
 
644
643
  has<C extends BTNCallback<N>>(
645
644
  identifier: ReturnType<C> | null | undefined,
646
645
  callback: C,
647
- beginRoot?: BTNKey | N | null | undefined,
646
+ beginRoot?: BTNodeKeyOrNode<N>,
648
647
  iterationType?: IterationType
649
648
  ): boolean;
650
649
 
@@ -671,7 +670,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
671
670
  has<C extends BTNCallback<N>>(
672
671
  identifier: ReturnType<C> | null | undefined,
673
672
  callback: C = this._defaultOneParamCallback as C,
674
- beginRoot: BTNKey | N | null | undefined = this.root,
673
+ beginRoot: BTNodeKeyOrNode<N> = this.root,
675
674
  iterationType = this.iterationType
676
675
  ): boolean {
677
676
  if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
@@ -688,21 +687,21 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
688
687
  getNode<C extends BTNCallback<N, BTNKey>>(
689
688
  identifier: BTNKey,
690
689
  callback?: C,
691
- beginRoot?: BTNKey | N | null | undefined,
690
+ beginRoot?: BTNodeKeyOrNode<N>,
692
691
  iterationType?: IterationType
693
692
  ): N | null | undefined;
694
693
 
695
694
  getNode<C extends BTNCallback<N, N>>(
696
695
  identifier: N | null | undefined,
697
696
  callback?: C,
698
- beginRoot?: BTNKey | N | null | undefined,
697
+ beginRoot?: BTNodeKeyOrNode<N>,
699
698
  iterationType?: IterationType
700
699
  ): N | null | undefined;
701
700
 
702
701
  getNode<C extends BTNCallback<N>>(
703
702
  identifier: ReturnType<C>,
704
703
  callback: C,
705
- beginRoot?: BTNKey | N | null | undefined,
704
+ beginRoot?: BTNodeKeyOrNode<N>,
706
705
  iterationType?: IterationType
707
706
  ): N | null | undefined;
708
707
 
@@ -730,7 +729,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
730
729
  getNode<C extends BTNCallback<N>>(
731
730
  identifier: ReturnType<C> | null | undefined,
732
731
  callback: C = this._defaultOneParamCallback as C,
733
- beginRoot: BTNKey | N | null | undefined = this.root,
732
+ beginRoot: BTNodeKeyOrNode<N> = this.root,
734
733
  iterationType = this.iterationType
735
734
  ): N | null | undefined {
736
735
  if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
@@ -789,7 +788,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
789
788
  */
790
789
 
791
790
  /**
792
- * The function `ensureNotKey` returns the node corresponding to the given key if it is a valid node
791
+ * The function `ensureNode` returns the node corresponding to the given key if it is a valid node
793
792
  * key, otherwise it returns the key itself.
794
793
  * @param {BTNKey | N | null | undefined} key - The `key` parameter can be of type `BTNKey`, `N`,
795
794
  * `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
@@ -799,28 +798,28 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
799
798
  * @returns either the node corresponding to the given key if it is a valid node key, or the key
800
799
  * itself if it is not a valid node key.
801
800
  */
802
- ensureNotKey(key: BTNKey | N | null | undefined, iterationType = IterationType.ITERATIVE): N | null | undefined {
801
+ ensureNode(key: BTNodeKeyOrNode<N>, iterationType = IterationType.ITERATIVE): N | null | undefined {
803
802
  return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
804
803
  }
805
804
 
806
805
  get<C extends BTNCallback<N, BTNKey>>(
807
806
  identifier: BTNKey,
808
807
  callback?: C,
809
- beginRoot?: BTNKey | N | null | undefined,
808
+ beginRoot?: BTNodeKeyOrNode<N>,
810
809
  iterationType?: IterationType
811
810
  ): V | undefined;
812
811
 
813
812
  get<C extends BTNCallback<N, N>>(
814
813
  identifier: N | null | undefined,
815
814
  callback?: C,
816
- beginRoot?: BTNKey | N | null | undefined,
815
+ beginRoot?: BTNodeKeyOrNode<N>,
817
816
  iterationType?: IterationType
818
817
  ): V | undefined;
819
818
 
820
819
  get<C extends BTNCallback<N>>(
821
820
  identifier: ReturnType<C>,
822
821
  callback: C,
823
- beginRoot?: BTNKey | N | null | undefined,
822
+ beginRoot?: BTNodeKeyOrNode<N>,
824
823
  iterationType?: IterationType
825
824
  ): V | undefined;
826
825
 
@@ -849,7 +848,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
849
848
  get<C extends BTNCallback<N>>(
850
849
  identifier: ReturnType<C> | null | undefined,
851
850
  callback: C = this._defaultOneParamCallback as C,
852
- beginRoot: BTNKey | N | null | undefined = this.root,
851
+ beginRoot: BTNodeKeyOrNode<N> = this.root,
853
852
  iterationType = this.iterationType
854
853
  ): V | undefined {
855
854
  if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
@@ -893,10 +892,10 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
893
892
  * reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
894
893
  * @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
895
894
  */
896
- getPathToRoot(beginRoot: BTNKey | N | null | undefined, isReverse = true): N[] {
895
+ getPathToRoot(beginRoot: BTNodeKeyOrNode<N>, isReverse = true): N[] {
897
896
  // TODO to support get path through passing key
898
897
  const result: N[] = [];
899
- beginRoot = this.ensureNotKey(beginRoot);
898
+ beginRoot = this.ensureNode(beginRoot);
900
899
 
901
900
  if (!beginRoot) return result;
902
901
 
@@ -930,10 +929,10 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
930
929
  * is no leftmost node, it returns `null` or `undefined` depending on the input.
931
930
  */
932
931
  getLeftMost(
933
- beginRoot: BTNKey | N | null | undefined = this.root,
932
+ beginRoot: BTNodeKeyOrNode<N> = this.root,
934
933
  iterationType = this.iterationType
935
934
  ): N | null | undefined {
936
- beginRoot = this.ensureNotKey(beginRoot);
935
+ beginRoot = this.ensureNode(beginRoot);
937
936
 
938
937
  if (!beginRoot) return beginRoot;
939
938
 
@@ -976,11 +975,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
976
975
  * is no rightmost node, it returns `null` or `undefined`, depending on the input.
977
976
  */
978
977
  getRightMost(
979
- beginRoot: BTNKey | N | null | undefined = this.root,
978
+ beginRoot: BTNodeKeyOrNode<N> = this.root,
980
979
  iterationType = this.iterationType
981
980
  ): N | null | undefined {
982
981
  // TODO support get right most by passing key in
983
- beginRoot = this.ensureNotKey(beginRoot);
982
+ beginRoot = this.ensureNode(beginRoot);
984
983
  if (!beginRoot) return beginRoot;
985
984
 
986
985
  if (iterationType === IterationType.RECURSIVE) {
@@ -1018,9 +1017,9 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1018
1017
  * possible values:
1019
1018
  * @returns a boolean value.
1020
1019
  */
1021
- isSubtreeBST(beginRoot: BTNKey | N | null | undefined, iterationType = this.iterationType): boolean {
1020
+ isSubtreeBST(beginRoot: BTNodeKeyOrNode<N>, iterationType = this.iterationType): boolean {
1022
1021
  // TODO there is a bug
1023
- beginRoot = this.ensureNotKey(beginRoot);
1022
+ beginRoot = this.ensureNode(beginRoot);
1024
1023
  if (!beginRoot) return true;
1025
1024
 
1026
1025
  if (iterationType === IterationType.RECURSIVE) {
@@ -1077,21 +1076,21 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1077
1076
 
1078
1077
  subTreeTraverse<C extends BTNCallback<N>>(
1079
1078
  callback?: C,
1080
- beginRoot?: BTNKey | N | null | undefined,
1079
+ beginRoot?: BTNodeKeyOrNode<N>,
1081
1080
  iterationType?: IterationType,
1082
1081
  includeNull?: false
1083
1082
  ): ReturnType<C>[];
1084
1083
 
1085
1084
  subTreeTraverse<C extends BTNCallback<N>>(
1086
1085
  callback?: C,
1087
- beginRoot?: BTNKey | N | null | undefined,
1086
+ beginRoot?: BTNodeKeyOrNode<N>,
1088
1087
  iterationType?: IterationType,
1089
1088
  includeNull?: undefined
1090
1089
  ): ReturnType<C>[];
1091
1090
 
1092
1091
  subTreeTraverse<C extends BTNCallback<N | null | undefined>>(
1093
1092
  callback?: C,
1094
- beginRoot?: BTNKey | N | null | undefined,
1093
+ beginRoot?: BTNodeKeyOrNode<N>,
1095
1094
  iterationType?: IterationType,
1096
1095
  includeNull?: true
1097
1096
  ): ReturnType<C>[];
@@ -1120,11 +1119,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1120
1119
  */
1121
1120
  subTreeTraverse<C extends BTNCallback<N | null | undefined>>(
1122
1121
  callback: C = this._defaultOneParamCallback as C,
1123
- beginRoot: BTNKey | N | null | undefined = this.root,
1122
+ beginRoot: BTNodeKeyOrNode<N> = this.root,
1124
1123
  iterationType = this.iterationType,
1125
1124
  includeNull = false
1126
1125
  ): ReturnType<C>[] {
1127
- beginRoot = this.ensureNotKey(beginRoot);
1126
+ beginRoot = this.ensureNode(beginRoot);
1128
1127
 
1129
1128
  const ans: (ReturnType<BTNCallback<N>> | null | undefined)[] = [];
1130
1129
  if (!beginRoot) return ans;
@@ -1210,7 +1209,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1210
1209
  dfs<C extends BTNCallback<N>>(
1211
1210
  callback?: C,
1212
1211
  pattern?: DFSOrderPattern,
1213
- beginRoot?: BTNKey | N | null | undefined,
1212
+ beginRoot?: BTNodeKeyOrNode<N>,
1214
1213
  iterationType?: IterationType,
1215
1214
  includeNull?: false
1216
1215
  ): ReturnType<C>[];
@@ -1218,7 +1217,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1218
1217
  dfs<C extends BTNCallback<N>>(
1219
1218
  callback?: C,
1220
1219
  pattern?: DFSOrderPattern,
1221
- beginRoot?: BTNKey | N | null | undefined,
1220
+ beginRoot?: BTNodeKeyOrNode<N>,
1222
1221
  iterationType?: IterationType,
1223
1222
  includeNull?: undefined
1224
1223
  ): ReturnType<C>[];
@@ -1226,7 +1225,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1226
1225
  dfs<C extends BTNCallback<N | null | undefined>>(
1227
1226
  callback?: C,
1228
1227
  pattern?: DFSOrderPattern,
1229
- beginRoot?: BTNKey | N | null | undefined,
1228
+ beginRoot?: BTNodeKeyOrNode<N>,
1230
1229
  iterationType?: IterationType,
1231
1230
  includeNull?: true
1232
1231
  ): ReturnType<C>[];
@@ -1257,11 +1256,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1257
1256
  dfs<C extends BTNCallback<N | null | undefined>>(
1258
1257
  callback: C = this._defaultOneParamCallback as C,
1259
1258
  pattern: DFSOrderPattern = 'in',
1260
- beginRoot: BTNKey | N | null | undefined = this.root,
1259
+ beginRoot: BTNodeKeyOrNode<N> = this.root,
1261
1260
  iterationType: IterationType = IterationType.ITERATIVE,
1262
1261
  includeNull = false
1263
1262
  ): ReturnType<C>[] {
1264
- beginRoot = this.ensureNotKey(beginRoot);
1263
+ beginRoot = this.ensureNode(beginRoot);
1265
1264
  if (!beginRoot) return [];
1266
1265
  const ans: ReturnType<C>[] = [];
1267
1266
  if (iterationType === IterationType.RECURSIVE) {
@@ -1356,21 +1355,21 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1356
1355
 
1357
1356
  bfs<C extends BTNCallback<N>>(
1358
1357
  callback?: C,
1359
- beginRoot?: BTNKey | N | null | undefined,
1358
+ beginRoot?: BTNodeKeyOrNode<N>,
1360
1359
  iterationType?: IterationType,
1361
1360
  includeNull?: false
1362
1361
  ): ReturnType<C>[];
1363
1362
 
1364
1363
  bfs<C extends BTNCallback<N>>(
1365
1364
  callback?: C,
1366
- beginRoot?: BTNKey | N | null | undefined,
1365
+ beginRoot?: BTNodeKeyOrNode<N>,
1367
1366
  iterationType?: IterationType,
1368
1367
  includeNull?: undefined
1369
1368
  ): ReturnType<C>[];
1370
1369
 
1371
1370
  bfs<C extends BTNCallback<N | null | undefined>>(
1372
1371
  callback?: C,
1373
- beginRoot?: BTNKey | N | null | undefined,
1372
+ beginRoot?: BTNodeKeyOrNode<N>,
1374
1373
  iterationType?: IterationType,
1375
1374
  includeNull?: true
1376
1375
  ): ReturnType<C>[];
@@ -1398,11 +1397,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1398
1397
  */
1399
1398
  bfs<C extends BTNCallback<N | null | undefined>>(
1400
1399
  callback: C = this._defaultOneParamCallback as C,
1401
- beginRoot: BTNKey | N | null | undefined = this.root,
1400
+ beginRoot: BTNodeKeyOrNode<N> = this.root,
1402
1401
  iterationType = this.iterationType,
1403
1402
  includeNull = false
1404
1403
  ): ReturnType<C>[] {
1405
- beginRoot = this.ensureNotKey(beginRoot);
1404
+ beginRoot = this.ensureNode(beginRoot);
1406
1405
  if (!beginRoot) return [];
1407
1406
 
1408
1407
  const ans: ReturnType<BTNCallback<N>>[] = [];
@@ -1457,21 +1456,21 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1457
1456
 
1458
1457
  listLevels<C extends BTNCallback<N>>(
1459
1458
  callback?: C,
1460
- beginRoot?: BTNKey | N | null | undefined,
1459
+ beginRoot?: BTNodeKeyOrNode<N>,
1461
1460
  iterationType?: IterationType,
1462
1461
  includeNull?: false
1463
1462
  ): ReturnType<C>[][];
1464
1463
 
1465
1464
  listLevels<C extends BTNCallback<N>>(
1466
1465
  callback?: C,
1467
- beginRoot?: BTNKey | N | null | undefined,
1466
+ beginRoot?: BTNodeKeyOrNode<N>,
1468
1467
  iterationType?: IterationType,
1469
1468
  includeNull?: undefined
1470
1469
  ): ReturnType<C>[][];
1471
1470
 
1472
1471
  listLevels<C extends BTNCallback<N | null | undefined>>(
1473
1472
  callback?: C,
1474
- beginRoot?: BTNKey | N | null | undefined,
1473
+ beginRoot?: BTNodeKeyOrNode<N>,
1475
1474
  iterationType?: IterationType,
1476
1475
  includeNull?: true
1477
1476
  ): ReturnType<C>[][];
@@ -1499,11 +1498,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1499
1498
  */
1500
1499
  listLevels<C extends BTNCallback<N | null | undefined>>(
1501
1500
  callback: C = this._defaultOneParamCallback as C,
1502
- beginRoot: BTNKey | N | null | undefined = this.root,
1501
+ beginRoot: BTNodeKeyOrNode<N> = this.root,
1503
1502
  iterationType = this.iterationType,
1504
1503
  includeNull = false
1505
1504
  ): ReturnType<C>[][] {
1506
- beginRoot = this.ensureNotKey(beginRoot);
1505
+ beginRoot = this.ensureNode(beginRoot);
1507
1506
  const levelsNodes: ReturnType<C>[][] = [];
1508
1507
  if (!beginRoot) return levelsNodes;
1509
1508
 
@@ -1557,8 +1556,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1557
1556
  * `null`, or `undefined`.
1558
1557
  * @returns The function `getPredecessor` returns a value of type `N | undefined`.
1559
1558
  */
1560
- getPredecessor(node: BTNKey | N | null | undefined): N | undefined {
1561
- node = this.ensureNotKey(node);
1559
+ getPredecessor(node: BTNodeKeyOrNode<N>): N | undefined {
1560
+ node = this.ensureNode(node);
1562
1561
  if (!this.isRealNode(node)) return undefined;
1563
1562
 
1564
1563
  if (node.left) {
@@ -1581,7 +1580,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1581
1580
  * after the given node in the inorder traversal of the binary tree.
1582
1581
  */
1583
1582
  getSuccessor(x?: BTNKey | N | null): N | null | undefined {
1584
- x = this.ensureNotKey(x);
1583
+ x = this.ensureNode(x);
1585
1584
  if (!x) return undefined;
1586
1585
 
1587
1586
  if (x.right) {
@@ -1617,9 +1616,9 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1617
1616
  morris<C extends BTNCallback<N>>(
1618
1617
  callback: C = this._defaultOneParamCallback as C,
1619
1618
  pattern: DFSOrderPattern = 'in',
1620
- beginRoot: BTNKey | N | null | undefined = this.root
1619
+ beginRoot: BTNodeKeyOrNode<N> = this.root
1621
1620
  ): ReturnType<C>[] {
1622
- beginRoot = this.ensureNotKey(beginRoot);
1621
+ beginRoot = this.ensureNode(beginRoot);
1623
1622
  if (beginRoot === null) return [];
1624
1623
  const ans: ReturnType<BTNCallback<N>>[] = [];
1625
1624
 
@@ -1729,7 +1728,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1729
1728
  const newTree = this.createTree();
1730
1729
  for (const [key, value] of this) {
1731
1730
  if (predicate([key, value], this)) {
1732
- newTree.add(key, value);
1731
+ newTree.add([key, value]);
1733
1732
  }
1734
1733
  }
1735
1734
  return newTree;
@@ -1744,7 +1743,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1744
1743
  map(callback: (entry: [BTNKey, V | undefined], tree: this) => V) {
1745
1744
  const newTree = this.createTree();
1746
1745
  for (const [key, value] of this) {
1747
- newTree.add(key, callback([key, value], this));
1746
+ newTree.add([key, callback([key, value], this)]);
1748
1747
  }
1749
1748
  return newTree;
1750
1749
  }
@@ -1825,9 +1824,9 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1825
1824
  * following types:
1826
1825
  * @param {BinaryTreePrintOptions} [options={ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false}] - Options object that controls printing behavior. You can specify whether to display undefined, null, or sentinel nodes.
1827
1826
  */
1828
- print(beginRoot: BTNKey | N | null | undefined = this.root, options?: BinaryTreePrintOptions): void {
1827
+ print(beginRoot: BTNodeKeyOrNode<N> = this.root, options?: BinaryTreePrintOptions): void {
1829
1828
  const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
1830
- beginRoot = this.ensureNotKey(beginRoot);
1829
+ beginRoot = this.ensureNode(beginRoot);
1831
1830
  if (!beginRoot) return;
1832
1831
 
1833
1832
  if (opts.isShowUndefined) console.log(`U for undefined
@@ -1847,19 +1846,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1847
1846
  display(beginRoot);
1848
1847
  }
1849
1848
 
1850
- init(elements: IterableEntriesOrKeys<V>): void {
1851
- if (elements) {
1852
- for (const entryOrKey of elements) {
1853
- if (Array.isArray(entryOrKey)) {
1854
- const [key, value] = entryOrKey;
1855
- this.add(key, value);
1856
- } else {
1857
- this.add(entryOrKey);
1858
- }
1859
- }
1860
- }
1861
- }
1862
-
1863
1849
  protected _displayAux(node: N | null | undefined, options: BinaryTreePrintOptions): NodeDisplayLayout {
1864
1850
  const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
1865
1851
  const emptyDisplayLayout = <NodeDisplayLayout>[['─'], 1, 0, 0];
@@ -1918,9 +1904,9 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1918
1904
  * @param {N} destNode - The destination node to swap.
1919
1905
  * @returns {N} - The destination node after the swap.
1920
1906
  */
1921
- protected _swap(srcNode: BTNKey | N | null | undefined, destNode: BTNKey | N | null | undefined): N | undefined {
1922
- srcNode = this.ensureNotKey(srcNode);
1923
- destNode = this.ensureNotKey(destNode);
1907
+ protected _swapProperties(srcNode: BTNodeKeyOrNode<N>, destNode: BTNodeKeyOrNode<N>): N | undefined {
1908
+ srcNode = this.ensureNode(srcNode);
1909
+ destNode = this.ensureNode(destNode);
1924
1910
 
1925
1911
  if (srcNode && destNode) {
1926
1912
  const { key, value } = destNode;
@@ -1939,6 +1925,32 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1939
1925
  return undefined;
1940
1926
  }
1941
1927
 
1928
+ /**
1929
+ * The function replaces an old node with a new node in a binary tree.
1930
+ * @param {N} oldNode - The oldNode parameter represents the node that needs to be replaced in the
1931
+ * tree.
1932
+ * @param {N} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
1933
+ * tree.
1934
+ * @returns The method is returning the newNode.
1935
+ */
1936
+ protected _replaceNode(oldNode: N, newNode: N): N {
1937
+ if (oldNode.parent) {
1938
+ if (oldNode.parent.left === oldNode) {
1939
+ oldNode.parent.left = newNode;
1940
+ } else if (oldNode.parent.right === oldNode) {
1941
+ oldNode.parent.right = newNode;
1942
+ }
1943
+ }
1944
+ newNode.left = oldNode.left;
1945
+ newNode.right = oldNode.right;
1946
+ newNode.parent = oldNode.parent;
1947
+ if (this.root === oldNode) {
1948
+ this._root = newNode;
1949
+ }
1950
+
1951
+ return newNode;
1952
+ }
1953
+
1942
1954
  /**
1943
1955
  * The function `_addTo` adds a new node to a binary tree if there is an available position.
1944
1956
  * @param {N | null | undefined} newNode - The `newNode` parameter represents the node that you want to add to
@@ -1950,7 +1962,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1950
1962
  * the binary tree. If neither the left nor right child is available, the function returns undefined.
1951
1963
  * If the parent node is null, the function also returns undefined.
1952
1964
  */
1953
- protected _addTo(newNode: N | null | undefined, parent: BTNKey | N | null | undefined): N | null | undefined {
1965
+ protected _addTo(newNode: N | null | undefined, parent: BTNodeKeyOrNode<N>): N | null | undefined {
1954
1966
  if (this.isNodeKey(parent)) parent = this.getNode(parent);
1955
1967
 
1956
1968
  if (parent) {