data-structure-typed 1.47.5 → 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 (169) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +10 -7
  2. package/.github/workflows/ci.yml +1 -1
  3. package/.github/workflows/release-package.yml +1 -1
  4. package/CHANGELOG.md +1 -1
  5. package/CODE_OF_CONDUCT.md +32 -10
  6. package/COMMANDS.md +3 -1
  7. package/CONTRIBUTING.md +4 -3
  8. package/README.md +103 -28
  9. package/SECURITY.md +1 -1
  10. package/benchmark/report.html +46 -1
  11. package/benchmark/report.json +563 -8
  12. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +36 -18
  13. package/dist/cjs/data-structures/binary-tree/avl-tree.js +46 -29
  14. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  15. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +158 -129
  16. package/dist/cjs/data-structures/binary-tree/binary-tree.js +182 -184
  17. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/bst.d.ts +73 -63
  19. package/dist/cjs/data-structures/binary-tree/bst.js +168 -169
  20. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  21. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +54 -17
  22. package/dist/cjs/data-structures/binary-tree/rb-tree.js +77 -31
  23. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  24. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +29 -40
  25. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +66 -136
  26. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  27. package/dist/cjs/data-structures/graph/abstract-graph.js +1 -1
  28. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  29. package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -6
  30. package/dist/cjs/data-structures/hash/hash-map.js +5 -8
  31. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  32. package/dist/cjs/data-structures/heap/heap.d.ts +19 -21
  33. package/dist/cjs/data-structures/heap/heap.js +52 -34
  34. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  35. package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -5
  36. package/dist/cjs/data-structures/heap/max-heap.js +2 -2
  37. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  38. package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -5
  39. package/dist/cjs/data-structures/heap/min-heap.js +2 -2
  40. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  41. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  42. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +9 -1
  43. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  44. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  45. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +8 -1
  46. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  47. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  48. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +2 -2
  49. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  50. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  51. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +2 -2
  52. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  53. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
  54. package/dist/cjs/data-structures/priority-queue/priority-queue.js +2 -2
  55. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  56. package/dist/cjs/data-structures/queue/deque.d.ts +1 -0
  57. package/dist/cjs/data-structures/queue/deque.js +3 -0
  58. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  59. package/dist/cjs/data-structures/queue/queue.d.ts +1 -0
  60. package/dist/cjs/data-structures/queue/queue.js +3 -0
  61. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  62. package/dist/cjs/data-structures/stack/stack.d.ts +2 -1
  63. package/dist/cjs/data-structures/stack/stack.js +10 -2
  64. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  65. package/dist/cjs/data-structures/trie/trie.d.ts +3 -0
  66. package/dist/cjs/data-structures/trie/trie.js +19 -4
  67. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  68. package/dist/cjs/interfaces/binary-tree.d.ts +4 -2
  69. package/dist/cjs/types/common.d.ts +7 -0
  70. package/dist/cjs/types/common.js.map +1 -1
  71. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  72. package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -2
  73. package/dist/cjs/types/data-structures/hash/hash-map.d.ts +1 -2
  74. package/dist/cjs/types/data-structures/heap/heap.d.ts +4 -1
  75. package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  76. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +36 -18
  77. package/dist/mjs/data-structures/binary-tree/avl-tree.js +49 -30
  78. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +158 -129
  79. package/dist/mjs/data-structures/binary-tree/binary-tree.js +182 -194
  80. package/dist/mjs/data-structures/binary-tree/bst.d.ts +73 -63
  81. package/dist/mjs/data-structures/binary-tree/bst.js +171 -170
  82. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +54 -17
  83. package/dist/mjs/data-structures/binary-tree/rb-tree.js +81 -33
  84. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +29 -40
  85. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +70 -138
  86. package/dist/mjs/data-structures/graph/abstract-graph.js +1 -1
  87. package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -6
  88. package/dist/mjs/data-structures/hash/hash-map.js +5 -8
  89. package/dist/mjs/data-structures/heap/heap.d.ts +19 -21
  90. package/dist/mjs/data-structures/heap/heap.js +53 -35
  91. package/dist/mjs/data-structures/heap/max-heap.d.ts +2 -5
  92. package/dist/mjs/data-structures/heap/max-heap.js +2 -2
  93. package/dist/mjs/data-structures/heap/min-heap.d.ts +2 -5
  94. package/dist/mjs/data-structures/heap/min-heap.js +2 -2
  95. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  96. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +9 -1
  97. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  98. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +8 -1
  99. package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  100. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +2 -2
  101. package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  102. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +2 -2
  103. package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
  104. package/dist/mjs/data-structures/priority-queue/priority-queue.js +2 -2
  105. package/dist/mjs/data-structures/queue/deque.d.ts +1 -0
  106. package/dist/mjs/data-structures/queue/deque.js +3 -0
  107. package/dist/mjs/data-structures/queue/queue.d.ts +1 -0
  108. package/dist/mjs/data-structures/queue/queue.js +3 -0
  109. package/dist/mjs/data-structures/stack/stack.d.ts +2 -1
  110. package/dist/mjs/data-structures/stack/stack.js +10 -2
  111. package/dist/mjs/data-structures/trie/trie.d.ts +3 -0
  112. package/dist/mjs/data-structures/trie/trie.js +20 -4
  113. package/dist/mjs/interfaces/binary-tree.d.ts +4 -2
  114. package/dist/mjs/types/common.d.ts +7 -0
  115. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  116. package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -2
  117. package/dist/mjs/types/data-structures/hash/hash-map.d.ts +1 -2
  118. package/dist/mjs/types/data-structures/heap/heap.d.ts +4 -1
  119. package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  120. package/dist/umd/data-structure-typed.js +629 -595
  121. package/dist/umd/data-structure-typed.min.js +2 -2
  122. package/dist/umd/data-structure-typed.min.js.map +1 -1
  123. package/package.json +1 -1
  124. package/src/data-structures/binary-tree/avl-tree.ts +61 -31
  125. package/src/data-structures/binary-tree/binary-tree.ts +283 -254
  126. package/src/data-structures/binary-tree/bst.ts +193 -170
  127. package/src/data-structures/binary-tree/rb-tree.ts +87 -32
  128. package/src/data-structures/binary-tree/tree-multimap.ts +76 -136
  129. package/src/data-structures/graph/abstract-graph.ts +1 -1
  130. package/src/data-structures/hash/hash-map.ts +8 -8
  131. package/src/data-structures/heap/heap.ts +57 -39
  132. package/src/data-structures/heap/max-heap.ts +5 -5
  133. package/src/data-structures/heap/min-heap.ts +5 -5
  134. package/src/data-structures/linked-list/doubly-linked-list.ts +10 -1
  135. package/src/data-structures/linked-list/singly-linked-list.ts +9 -1
  136. package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
  137. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
  138. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  139. package/src/data-structures/queue/deque.ts +4 -0
  140. package/src/data-structures/queue/queue.ts +4 -0
  141. package/src/data-structures/stack/stack.ts +12 -3
  142. package/src/data-structures/trie/trie.ts +23 -4
  143. package/src/interfaces/binary-tree.ts +14 -2
  144. package/src/types/common.ts +15 -1
  145. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  146. package/src/types/data-structures/binary-tree/bst.ts +2 -3
  147. package/src/types/data-structures/hash/hash-map.ts +1 -2
  148. package/src/types/data-structures/heap/heap.ts +3 -1
  149. package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
  150. package/test/integration/{all-in-one.ts → all-in-one.test.ts} +1 -1
  151. package/test/integration/index.html +87 -0
  152. package/test/performance/data-structures/comparison/comparison.test.ts +7 -6
  153. package/test/performance/data-structures/heap/heap.test.ts +2 -2
  154. package/test/performance/data-structures/priority-queue/priority-queue.test.ts +1 -1
  155. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +20 -20
  156. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +56 -57
  157. package/test/unit/data-structures/binary-tree/bst.test.ts +49 -54
  158. package/test/unit/data-structures/binary-tree/overall.test.ts +17 -18
  159. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +3 -3
  160. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +118 -66
  161. package/test/unit/data-structures/heap/heap.test.ts +2 -2
  162. package/test/unit/data-structures/heap/max-heap.test.ts +1 -1
  163. package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
  164. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +4 -3
  165. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +9 -10
  166. package/test/unit/data-structures/stack/stack.test.ts +2 -2
  167. package/test/unit/unrestricted-interconversion.test.ts +156 -0
  168. package/tsconfig-cjs.json +1 -1
  169. package/test/integration/conversion.test.ts +0 -0
@@ -14,38 +14,17 @@ import { Queue } from '../queue';
14
14
  * @template N - The type of the family relationship in the binary tree.
15
15
  */
16
16
  export class BinaryTreeNode {
17
- /**
18
- * The key associated with the node.
19
- */
20
17
  key;
21
- /**
22
- * The value stored in the node.
23
- */
24
18
  value;
25
- /**
26
- * The parent node of the current node.
27
- */
28
19
  parent;
29
- /**
30
- * Creates a new instance of BinaryTreeNode.
31
- * @param {BTNKey} key - The key associated with the node.
32
- * @param {V} value - The value stored in the node.
33
- */
34
20
  constructor(key, value) {
35
21
  this.key = key;
36
22
  this.value = value;
37
23
  }
38
24
  _left;
39
- /**
40
- * Get the left child node.
41
- */
42
25
  get left() {
43
26
  return this._left;
44
27
  }
45
- /**
46
- * Set the left child node.
47
- * @param {N | null | undefined} v - The left child node.
48
- */
49
28
  set left(v) {
50
29
  if (v) {
51
30
  v.parent = this;
@@ -53,16 +32,9 @@ export class BinaryTreeNode {
53
32
  this._left = v;
54
33
  }
55
34
  _right;
56
- /**
57
- * Get the right child node.
58
- */
59
35
  get right() {
60
36
  return this._right;
61
37
  }
62
- /**
63
- * Set the right child node.
64
- * @param {N | null | undefined} v - The right child node.
65
- */
66
38
  set right(v) {
67
39
  if (v) {
68
40
  v.parent = this;
@@ -88,35 +60,43 @@ export class BinaryTreeNode {
88
60
  }
89
61
  }
90
62
  /**
91
- * Represents a binary tree data structure.
92
- * @template N - The type of the binary tree's nodes.
63
+ * 1. Two Children Maximum: Each node has at most two children.
64
+ * 2. Left and Right Children: Nodes have distinct left and right children.
65
+ * 3. Depth and Height: Depth is the number of edges from the root to a node; height is the maximum depth in the tree.
66
+ * 4. Subtrees: Each child of a node forms the root of a subtree.
67
+ * 5. Leaf Nodes: Nodes without children are leaves.
68
+ * 6. Internal Nodes: Nodes with at least one child are internal.
69
+ * 7. Balanced Trees: The heights of the left and right subtrees of any node differ by no more than one.
70
+ * 8. Full Trees: Every node has either 0 or 2 children.
71
+ * 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
93
72
  */
94
73
  export class BinaryTree {
95
- options;
74
+ iterationType = IterationType.ITERATIVE;
96
75
  /**
97
- * Creates a new instance of BinaryTree.
98
- * @param {BinaryTreeOptions} [options] - The options for the binary tree.
76
+ * The constructor function initializes a binary tree object with optional elements and options.
77
+ * @param [elements] - An optional iterable of BTNodeExemplar objects. These objects represent the
78
+ * elements to be added to the binary tree.
79
+ * @param [options] - The `options` parameter is an optional object that can contain additional
80
+ * configuration options for the binary tree. In this case, it is of type
81
+ * `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
82
+ * required.
99
83
  */
100
- constructor(options) {
84
+ constructor(elements, options) {
101
85
  if (options) {
102
- this.options = { iterationType: IterationType.ITERATIVE, ...options };
103
- }
104
- else {
105
- this.options = { iterationType: IterationType.ITERATIVE };
86
+ const { iterationType } = options;
87
+ if (iterationType) {
88
+ this.iterationType = iterationType;
89
+ }
106
90
  }
107
91
  this._size = 0;
92
+ if (elements)
93
+ this.addMany(elements);
108
94
  }
109
95
  _root;
110
- /**
111
- * Get the root node of the binary tree.
112
- */
113
96
  get root() {
114
97
  return this._root;
115
98
  }
116
99
  _size;
117
- /**
118
- * Get the number of nodes in the binary tree.
119
- */
120
100
  get size() {
121
101
  return this._size;
122
102
  }
@@ -129,35 +109,46 @@ export class BinaryTree {
129
109
  createNode(key, value) {
130
110
  return new BinaryTreeNode(key, value);
131
111
  }
112
+ /**
113
+ * The function creates a binary tree with the given options.
114
+ * @param [options] - The `options` parameter is an optional object that allows you to customize the
115
+ * behavior of the `BinaryTree` class. It is of type `Partial<BinaryTreeOptions>`, which means that
116
+ * you can provide only a subset of the properties defined in the `BinaryTreeOptions` interface.
117
+ * @returns a new instance of a binary tree.
118
+ */
132
119
  createTree(options) {
133
- return new BinaryTree({ ...this.options, ...options });
120
+ return new BinaryTree([], { iterationType: this.iterationType, ...options });
134
121
  }
135
122
  /**
136
- * Time Complexity: O(n)
137
- * Space Complexity: O(1)
138
- * 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.
123
+ * The function checks if a given value is an entry in a binary tree node.
124
+ * @param kne - BTNodeExemplar<V, N> - A generic type representing a node in a binary tree. It has
125
+ * two type parameters V and N, representing the value and node type respectively.
126
+ * @returns a boolean value.
139
127
  */
128
+ isEntry(kne) {
129
+ return Array.isArray(kne) && kne.length === 2;
130
+ }
140
131
  /**
141
- * Time Complexity: O(n)
142
- * Space Complexity: O(1)
132
+ * Time Complexity O(log n) - O(n)
133
+ * Space Complexity O(1)
134
+ */
135
+ /**
136
+ * Time Complexity O(log n) - O(n)
137
+ * Space Complexity O(1)
143
138
  *
144
- * The `add` function adds a new node with a key and value to a binary tree, or updates the value of
145
- * an existing node with the same key.
146
- * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
147
- * following types:
148
- * @param {V} [value] - The value to be associated with the key or node being added to the binary
149
- * tree.
150
- * @returns The function `add` returns a node (`N`) if it was successfully inserted into the binary
151
- * tree, or `null` or `undefined` if the insertion was not successful.
139
+ * The `add` function adds a new node to a binary tree, either by key or by providing a node object.
140
+ * @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be one of the following:
141
+ * @returns The function `add` returns the inserted node (`N`), `null`, or `undefined`.
152
142
  */
153
- add(keyOrNode, value) {
143
+ add(keyOrNodeOrEntry) {
144
+ let inserted, needInsert;
154
145
  const _bfs = (root, newNode) => {
155
146
  const queue = new Queue([root]);
156
147
  while (queue.size > 0) {
157
148
  const cur = queue.shift();
158
149
  if (newNode && cur.key === newNode.key) {
159
- cur.value = newNode.value;
160
- return;
150
+ this._replaceNode(cur, newNode);
151
+ return newNode;
161
152
  }
162
153
  const inserted = this._addTo(newNode, cur);
163
154
  if (inserted !== undefined)
@@ -168,15 +159,26 @@ export class BinaryTree {
168
159
  queue.push(cur.right);
169
160
  }
170
161
  };
171
- let inserted, needInsert;
172
- if (keyOrNode === null) {
162
+ if (keyOrNodeOrEntry === null) {
173
163
  needInsert = null;
174
164
  }
175
- else if (this.isNodeKey(keyOrNode)) {
176
- needInsert = this.createNode(keyOrNode, value);
165
+ else if (this.isNodeKey(keyOrNodeOrEntry)) {
166
+ needInsert = this.createNode(keyOrNodeOrEntry);
177
167
  }
178
- else if (keyOrNode instanceof BinaryTreeNode) {
179
- needInsert = keyOrNode;
168
+ else if (keyOrNodeOrEntry instanceof BinaryTreeNode) {
169
+ needInsert = keyOrNodeOrEntry;
170
+ }
171
+ else if (this.isEntry(keyOrNodeOrEntry)) {
172
+ const [key, value] = keyOrNodeOrEntry;
173
+ if (key === undefined) {
174
+ return;
175
+ }
176
+ else if (key === null) {
177
+ needInsert = null;
178
+ }
179
+ else {
180
+ needInsert = this.createNode(key, value);
181
+ }
180
182
  }
181
183
  else {
182
184
  return;
@@ -197,35 +199,28 @@ export class BinaryTree {
197
199
  return inserted;
198
200
  }
199
201
  /**
200
- * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
202
+ * Time Complexity: O(k log n) - O(k * n)
201
203
  * Space Complexity: O(1)
204
+ * 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.
202
205
  */
203
206
  /**
204
- * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
207
+ * Time Complexity: O(k log n) - O(k * n)
205
208
  * Space Complexity: O(1)
206
209
  *
207
- * The `addMany` function takes an array of keys or nodes and an optional array of values, and adds
208
- * each key-value pair to a data structure.
209
- * @param {(BTNKey | N |null | undefined)[]} keysOrNodes - An array of keys or nodes to be added to
210
- * the binary search tree. Each element can be of type `BTNKey` (a key value), `N` (a node), `null`,
211
- * or `undefined`.
212
- * @param {(V | undefined)[]} [values] - The `values` parameter is an optional array of values that
213
- * correspond to the keys or nodes being added. If provided, the values will be associated with the
214
- * keys or nodes during the add operation.
215
- * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
216
- */
217
- addMany(keysOrNodes, values) {
210
+ * The function `addMany` takes in an iterable of `BTNodeExemplar` objects, adds each object to the
211
+ * current instance, and returns an array of the inserted nodes.
212
+ * @param nodes - The `nodes` parameter is an iterable (such as an array or a set) of
213
+ * `BTNodeExemplar<V, N>` objects.
214
+ * @returns The function `addMany` returns an array of values, where each value is either of type
215
+ * `N`, `null`, or `undefined`.
216
+ */
217
+ addMany(nodes) {
218
218
  // TODO not sure addMany not be run multi times
219
- return keysOrNodes.map((keyOrNode, i) => {
220
- if (keyOrNode instanceof BinaryTreeNode) {
221
- return this.add(keyOrNode.key, keyOrNode.value);
222
- }
223
- if (keyOrNode === null) {
224
- return this.add(null);
225
- }
226
- const value = values?.[i];
227
- return this.add(keyOrNode, value);
228
- });
219
+ const inserted = [];
220
+ for (const kne of nodes) {
221
+ inserted.push(this.add(kne));
222
+ }
223
+ return inserted;
229
224
  }
230
225
  /**
231
226
  * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
@@ -235,22 +230,14 @@ export class BinaryTree {
235
230
  * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
236
231
  * Space Complexity: O(1)
237
232
  *
238
- * The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
239
- * @param {(BTNKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
240
- * `BTNKey` or `N` values.
241
- * @param {N[] | Array<V>} [values] - The `data` parameter is an optional array of values that will be assigned to
242
- * the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
243
- * array. Each value in the `data` array will be assigned to the
244
- * @returns The method is returning a boolean value.
233
+ * The `refill` function clears the current collection and adds new nodes, keys, or entries to it.
234
+ * @param nodesOrKeysOrEntries - The parameter `nodesOrKeysOrEntries` is an iterable object that can
235
+ * contain either `BTNodeExemplar` objects, keys, or entries.
245
236
  */
246
- refill(keysOrNodes, values) {
237
+ refill(nodesOrKeysOrEntries) {
247
238
  this.clear();
248
- return keysOrNodes.length === this.addMany(keysOrNodes, values).length;
239
+ this.addMany(nodesOrKeysOrEntries);
249
240
  }
250
- /**
251
- * Time Complexity: O(n)
252
- * Space Complexity: O(1)
253
- */
254
241
  /**
255
242
  * Time Complexity: O(n)
256
243
  * Space Complexity: O(1)
@@ -299,7 +286,7 @@ export class BinaryTree {
299
286
  const leftSubTreeRightMost = this.getRightMost(curr.left);
300
287
  if (leftSubTreeRightMost) {
301
288
  const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
302
- orgCurrent = this._swap(curr, leftSubTreeRightMost);
289
+ orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
303
290
  if (parentOfLeftSubTreeMax) {
304
291
  if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
305
292
  parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
@@ -332,8 +319,8 @@ export class BinaryTree {
332
319
  * @returns the depth of the `distNode` relative to the `beginRoot`.
333
320
  */
334
321
  getDepth(distNode, beginRoot = this.root) {
335
- distNode = this.ensureNotKey(distNode);
336
- beginRoot = this.ensureNotKey(beginRoot);
322
+ distNode = this.ensureNode(distNode);
323
+ beginRoot = this.ensureNode(beginRoot);
337
324
  let depth = 0;
338
325
  while (distNode?.parent) {
339
326
  if (distNode === beginRoot) {
@@ -346,8 +333,7 @@ export class BinaryTree {
346
333
  }
347
334
  /**
348
335
  * Time Complexity: O(n)
349
- * Space Complexity: O(log n)
350
- * Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
336
+ * Space Complexity: O(1)
351
337
  */
352
338
  /**
353
339
  * Time Complexity: O(n)
@@ -363,8 +349,8 @@ export class BinaryTree {
363
349
  * values:
364
350
  * @returns the height of the binary tree.
365
351
  */
366
- getHeight(beginRoot = this.root, iterationType = this.options.iterationType) {
367
- beginRoot = this.ensureNotKey(beginRoot);
352
+ getHeight(beginRoot = this.root, iterationType = this.iterationType) {
353
+ beginRoot = this.ensureNode(beginRoot);
368
354
  if (!beginRoot)
369
355
  return -1;
370
356
  if (iterationType === IterationType.RECURSIVE) {
@@ -409,8 +395,8 @@ export class BinaryTree {
409
395
  * to calculate the minimum height of a binary tree. It can have two possible values:
410
396
  * @returns The function `getMinHeight` returns the minimum height of a binary tree.
411
397
  */
412
- getMinHeight(beginRoot = this.root, iterationType = this.options.iterationType) {
413
- beginRoot = this.ensureNotKey(beginRoot);
398
+ getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
399
+ beginRoot = this.ensureNode(beginRoot);
414
400
  if (!beginRoot)
415
401
  return -1;
416
402
  if (iterationType === IterationType.RECURSIVE) {
@@ -456,6 +442,7 @@ export class BinaryTree {
456
442
  /**
457
443
  * Time Complexity: O(n)
458
444
  * Space Complexity: O(log n)
445
+ * Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
459
446
  */
460
447
  /**
461
448
  * Time Complexity: O(n)
@@ -471,10 +458,6 @@ export class BinaryTree {
471
458
  isPerfectlyBalanced(beginRoot = this.root) {
472
459
  return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
473
460
  }
474
- /**
475
- * Time Complexity: O(n)
476
- * Space Complexity: O(log n).
477
- */
478
461
  /**
479
462
  * Time Complexity: O(n)
480
463
  * Space Complexity: O(log n).
@@ -500,10 +483,10 @@ export class BinaryTree {
500
483
  * traverse the binary tree. It can have two possible values:
501
484
  * @returns an array of nodes of type `N`.
502
485
  */
503
- getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.options.iterationType) {
486
+ getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
504
487
  if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
505
488
  callback = (node => node);
506
- beginRoot = this.ensureNotKey(beginRoot);
489
+ beginRoot = this.ensureNode(beginRoot);
507
490
  if (!beginRoot)
508
491
  return [];
509
492
  const ans = [];
@@ -538,10 +521,6 @@ export class BinaryTree {
538
521
  }
539
522
  return ans;
540
523
  }
541
- /**
542
- * Time Complexity: O(n)
543
- * Space Complexity: O(log n).
544
- */
545
524
  /**
546
525
  * Time Complexity: O(n)
547
526
  *
@@ -562,15 +541,11 @@ export class BinaryTree {
562
541
  * be performed in a pre-order, in-order, or post-order manner.
563
542
  * @returns a boolean value.
564
543
  */
565
- has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType) {
544
+ has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
566
545
  if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
567
546
  callback = (node => node);
568
547
  return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
569
548
  }
570
- /**
571
- * Time Complexity: O(n)
572
- * Space Complexity: O(log n)
573
- */
574
549
  /**
575
550
  * Time Complexity: O(n)
576
551
  * Space Complexity: O(log n)
@@ -592,7 +567,7 @@ export class BinaryTree {
592
567
  * nodes are visited during the search.
593
568
  * @returns a value of type `N | null | undefined`.
594
569
  */
595
- getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType) {
570
+ getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
596
571
  if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
597
572
  callback = (node => node);
598
573
  return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
@@ -645,7 +620,11 @@ export class BinaryTree {
645
620
  }
646
621
  }
647
622
  /**
648
- * The function `ensureNotKey` returns the node corresponding to the given key if it is a valid node
623
+ * Time Complexity: O(n)
624
+ * Space Complexity: O(log n)
625
+ */
626
+ /**
627
+ * The function `ensureNode` returns the node corresponding to the given key if it is a valid node
649
628
  * key, otherwise it returns the key itself.
650
629
  * @param {BTNKey | N | null | undefined} key - The `key` parameter can be of type `BTNKey`, `N`,
651
630
  * `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
@@ -655,13 +634,9 @@ export class BinaryTree {
655
634
  * @returns either the node corresponding to the given key if it is a valid node key, or the key
656
635
  * itself if it is not a valid node key.
657
636
  */
658
- ensureNotKey(key, iterationType = IterationType.ITERATIVE) {
637
+ ensureNode(key, iterationType = IterationType.ITERATIVE) {
659
638
  return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
660
639
  }
661
- /**
662
- * Time Complexity: O(n)
663
- * Space Complexity: O(log n)
664
- */
665
640
  /**
666
641
  * Time Complexity: O(n)
667
642
  * Space Complexity: O(log n)
@@ -684,11 +659,15 @@ export class BinaryTree {
684
659
  * @returns The value of the node with the given identifier is being returned. If the node is not
685
660
  * found, `undefined` is returned.
686
661
  */
687
- get(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType) {
662
+ get(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
688
663
  if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
689
664
  callback = (node => node);
690
665
  return this.getNode(identifier, callback, beginRoot, iterationType)?.value ?? undefined;
691
666
  }
667
+ /**
668
+ * Time Complexity: O(n)
669
+ * Space Complexity: O(log n)
670
+ */
692
671
  /**
693
672
  * Clear the binary tree, removing all nodes.
694
673
  */
@@ -703,10 +682,6 @@ export class BinaryTree {
703
682
  isEmpty() {
704
683
  return this.size === 0;
705
684
  }
706
- /**
707
- * Time Complexity: O(log n)
708
- * Space Complexity: O(log n)
709
- */
710
685
  /**
711
686
  * Time Complexity: O(log n)
712
687
  * Space Complexity: O(log n)
@@ -724,7 +699,7 @@ export class BinaryTree {
724
699
  getPathToRoot(beginRoot, isReverse = true) {
725
700
  // TODO to support get path through passing key
726
701
  const result = [];
727
- beginRoot = this.ensureNotKey(beginRoot);
702
+ beginRoot = this.ensureNode(beginRoot);
728
703
  if (!beginRoot)
729
704
  return result;
730
705
  while (beginRoot.parent) {
@@ -738,7 +713,7 @@ export class BinaryTree {
738
713
  }
739
714
  /**
740
715
  * Time Complexity: O(log n)
741
- * Space Complexity: O(1)
716
+ * Space Complexity: O(log n)
742
717
  */
743
718
  /**
744
719
  * Time Complexity: O(log n)
@@ -754,8 +729,8 @@ export class BinaryTree {
754
729
  * @returns The function `getLeftMost` returns the leftmost node (`N`) in the binary tree. If there
755
730
  * is no leftmost node, it returns `null` or `undefined` depending on the input.
756
731
  */
757
- getLeftMost(beginRoot = this.root, iterationType = this.options.iterationType) {
758
- beginRoot = this.ensureNotKey(beginRoot);
732
+ getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
733
+ beginRoot = this.ensureNode(beginRoot);
759
734
  if (!beginRoot)
760
735
  return beginRoot;
761
736
  if (iterationType === IterationType.RECURSIVE) {
@@ -795,9 +770,9 @@ export class BinaryTree {
795
770
  * @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If there
796
771
  * is no rightmost node, it returns `null` or `undefined`, depending on the input.
797
772
  */
798
- getRightMost(beginRoot = this.root, iterationType = this.options.iterationType) {
773
+ getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
799
774
  // TODO support get right most by passing key in
800
- beginRoot = this.ensureNotKey(beginRoot);
775
+ beginRoot = this.ensureNode(beginRoot);
801
776
  if (!beginRoot)
802
777
  return beginRoot;
803
778
  if (iterationType === IterationType.RECURSIVE) {
@@ -819,7 +794,7 @@ export class BinaryTree {
819
794
  }
820
795
  }
821
796
  /**
822
- * Time Complexity: O(n)
797
+ * Time Complexity: O(log n)
823
798
  * Space Complexity: O(1)
824
799
  */
825
800
  /**
@@ -834,9 +809,9 @@ export class BinaryTree {
834
809
  * possible values:
835
810
  * @returns a boolean value.
836
811
  */
837
- isSubtreeBST(beginRoot, iterationType = this.options.iterationType) {
812
+ isSubtreeBST(beginRoot, iterationType = this.iterationType) {
838
813
  // TODO there is a bug
839
- beginRoot = this.ensureNotKey(beginRoot);
814
+ beginRoot = this.ensureNode(beginRoot);
840
815
  if (!beginRoot)
841
816
  return true;
842
817
  if (iterationType === IterationType.RECURSIVE) {
@@ -881,15 +856,11 @@ export class BinaryTree {
881
856
  * expected to be
882
857
  * @returns a boolean value.
883
858
  */
884
- isBST(iterationType = this.options.iterationType) {
859
+ isBST(iterationType = this.iterationType) {
885
860
  if (this.root === null)
886
861
  return true;
887
862
  return this.isSubtreeBST(this.root, iterationType);
888
863
  }
889
- /**
890
- * Time complexity: O(n)
891
- * Space complexity: O(log n)
892
- */
893
864
  /**
894
865
  * Time complexity: O(n)
895
866
  * Space complexity: O(log n)
@@ -912,8 +883,8 @@ export class BinaryTree {
912
883
  * the `callback` function on each node in the subtree. The type of the array elements is determined
913
884
  * by the return type of the `callback` function.
914
885
  */
915
- subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType, includeNull = false) {
916
- beginRoot = this.ensureNotKey(beginRoot);
886
+ subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
887
+ beginRoot = this.ensureNode(beginRoot);
917
888
  const ans = [];
918
889
  if (!beginRoot)
919
890
  return ans;
@@ -952,6 +923,10 @@ export class BinaryTree {
952
923
  }
953
924
  return ans;
954
925
  }
926
+ /**
927
+ * Time complexity: O(n)
928
+ * Space complexity: O(log n)
929
+ */
955
930
  /**
956
931
  * The function checks if a given node is a real node by verifying if it is an instance of
957
932
  * BinaryTreeNode and its key is not NaN.
@@ -986,10 +961,6 @@ export class BinaryTree {
986
961
  isNodeKey(potentialKey) {
987
962
  return typeof potentialKey === 'number';
988
963
  }
989
- /**
990
- * Time complexity: O(n)
991
- * Space complexity: O(n)
992
- */
993
964
  /**
994
965
  * Time complexity: O(n)
995
966
  * Space complexity: O(n)
@@ -1014,7 +985,7 @@ export class BinaryTree {
1014
985
  * @returns an array of values that are the return values of the callback function.
1015
986
  */
1016
987
  dfs(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root, iterationType = IterationType.ITERATIVE, includeNull = false) {
1017
- beginRoot = this.ensureNotKey(beginRoot);
988
+ beginRoot = this.ensureNode(beginRoot);
1018
989
  if (!beginRoot)
1019
990
  return [];
1020
991
  const ans = [];
@@ -1119,10 +1090,6 @@ export class BinaryTree {
1119
1090
  }
1120
1091
  return ans;
1121
1092
  }
1122
- /**
1123
- * Time complexity: O(n)
1124
- * Space complexity: O(n)
1125
- */
1126
1093
  /**
1127
1094
  * Time complexity: O(n)
1128
1095
  * Space complexity: O(n)
@@ -1144,8 +1111,8 @@ export class BinaryTree {
1144
1111
  * @returns an array of values that are the result of invoking the callback function on each node in
1145
1112
  * the breadth-first traversal of a binary tree.
1146
1113
  */
1147
- bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType, includeNull = false) {
1148
- beginRoot = this.ensureNotKey(beginRoot);
1114
+ bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
1115
+ beginRoot = this.ensureNode(beginRoot);
1149
1116
  if (!beginRoot)
1150
1117
  return [];
1151
1118
  const ans = [];
@@ -1196,10 +1163,6 @@ export class BinaryTree {
1196
1163
  }
1197
1164
  return ans;
1198
1165
  }
1199
- /**
1200
- * Time complexity: O(n)
1201
- * Space complexity: O(n)
1202
- */
1203
1166
  /**
1204
1167
  * Time complexity: O(n)
1205
1168
  * Space complexity: O(n)
@@ -1221,8 +1184,8 @@ export class BinaryTree {
1221
1184
  * be excluded
1222
1185
  * @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
1223
1186
  */
1224
- listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType, includeNull = false) {
1225
- beginRoot = this.ensureNotKey(beginRoot);
1187
+ listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
1188
+ beginRoot = this.ensureNode(beginRoot);
1226
1189
  const levelsNodes = [];
1227
1190
  if (!beginRoot)
1228
1191
  return levelsNodes;
@@ -1277,7 +1240,7 @@ export class BinaryTree {
1277
1240
  * @returns The function `getPredecessor` returns a value of type `N | undefined`.
1278
1241
  */
1279
1242
  getPredecessor(node) {
1280
- node = this.ensureNotKey(node);
1243
+ node = this.ensureNode(node);
1281
1244
  if (!this.isRealNode(node))
1282
1245
  return undefined;
1283
1246
  if (node.left) {
@@ -1300,7 +1263,7 @@ export class BinaryTree {
1300
1263
  * after the given node in the inorder traversal of the binary tree.
1301
1264
  */
1302
1265
  getSuccessor(x) {
1303
- x = this.ensureNotKey(x);
1266
+ x = this.ensureNode(x);
1304
1267
  if (!x)
1305
1268
  return undefined;
1306
1269
  if (x.right) {
@@ -1313,10 +1276,6 @@ export class BinaryTree {
1313
1276
  }
1314
1277
  return y;
1315
1278
  }
1316
- /**
1317
- * Time complexity: O(n)
1318
- * Space complexity: O(1)
1319
- */
1320
1279
  /**
1321
1280
  * Time complexity: O(n)
1322
1281
  * Space complexity: O(1)
@@ -1336,7 +1295,7 @@ export class BinaryTree {
1336
1295
  * by the return type of the `callback` function.
1337
1296
  */
1338
1297
  morris(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root) {
1339
- beginRoot = this.ensureNotKey(beginRoot);
1298
+ beginRoot = this.ensureNode(beginRoot);
1340
1299
  if (beginRoot === null)
1341
1300
  return [];
1342
1301
  const ans = [];
@@ -1420,6 +1379,10 @@ export class BinaryTree {
1420
1379
  }
1421
1380
  return ans;
1422
1381
  }
1382
+ /**
1383
+ * Time complexity: O(n)
1384
+ * Space complexity: O(1)
1385
+ */
1423
1386
  /**
1424
1387
  * The `forEach` function iterates over each entry in a tree and calls a callback function with the
1425
1388
  * entry and the tree as arguments.
@@ -1443,19 +1406,11 @@ export class BinaryTree {
1443
1406
  const newTree = this.createTree();
1444
1407
  for (const [key, value] of this) {
1445
1408
  if (predicate([key, value], this)) {
1446
- newTree.add(key, value);
1409
+ newTree.add([key, value]);
1447
1410
  }
1448
1411
  }
1449
1412
  return newTree;
1450
1413
  }
1451
- // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
1452
- // map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
1453
- // const newTree = this.createTree();
1454
- // for (const [key, value] of this) {
1455
- // newTree.add(key, callback([key, value], this));
1456
- // }
1457
- // return newTree;
1458
- // }
1459
1414
  /**
1460
1415
  * The `map` function creates a new tree by applying a callback function to each entry in the current
1461
1416
  * tree.
@@ -1465,10 +1420,18 @@ export class BinaryTree {
1465
1420
  map(callback) {
1466
1421
  const newTree = this.createTree();
1467
1422
  for (const [key, value] of this) {
1468
- newTree.add(key, callback([key, value], this));
1423
+ newTree.add([key, callback([key, value], this)]);
1469
1424
  }
1470
1425
  return newTree;
1471
1426
  }
1427
+ // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
1428
+ // map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
1429
+ // const newTree = this.createTree();
1430
+ // for (const [key, value] of this) {
1431
+ // newTree.add(key, callback([key, value], this));
1432
+ // }
1433
+ // return newTree;
1434
+ // }
1472
1435
  /**
1473
1436
  * The `reduce` function iterates over the entries of a tree and applies a callback function to each
1474
1437
  * entry, accumulating a single value.
@@ -1500,7 +1463,7 @@ export class BinaryTree {
1500
1463
  *[Symbol.iterator](node = this.root) {
1501
1464
  if (!node)
1502
1465
  return;
1503
- if (this.options.iterationType === IterationType.ITERATIVE) {
1466
+ if (this.iterationType === IterationType.ITERATIVE) {
1504
1467
  const stack = [];
1505
1468
  let current = node;
1506
1469
  while (current || stack.length > 0) {
@@ -1534,7 +1497,7 @@ export class BinaryTree {
1534
1497
  */
1535
1498
  print(beginRoot = this.root, options) {
1536
1499
  const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
1537
- beginRoot = this.ensureNotKey(beginRoot);
1500
+ beginRoot = this.ensureNode(beginRoot);
1538
1501
  if (!beginRoot)
1539
1502
  return;
1540
1503
  if (opts.isShowUndefined)
@@ -1604,9 +1567,9 @@ export class BinaryTree {
1604
1567
  * @param {N} destNode - The destination node to swap.
1605
1568
  * @returns {N} - The destination node after the swap.
1606
1569
  */
1607
- _swap(srcNode, destNode) {
1608
- srcNode = this.ensureNotKey(srcNode);
1609
- destNode = this.ensureNotKey(destNode);
1570
+ _swapProperties(srcNode, destNode) {
1571
+ srcNode = this.ensureNode(srcNode);
1572
+ destNode = this.ensureNode(destNode);
1610
1573
  if (srcNode && destNode) {
1611
1574
  const { key, value } = destNode;
1612
1575
  const tempNode = this.createNode(key, value);
@@ -1620,6 +1583,31 @@ export class BinaryTree {
1620
1583
  }
1621
1584
  return undefined;
1622
1585
  }
1586
+ /**
1587
+ * The function replaces an old node with a new node in a binary tree.
1588
+ * @param {N} oldNode - The oldNode parameter represents the node that needs to be replaced in the
1589
+ * tree.
1590
+ * @param {N} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
1591
+ * tree.
1592
+ * @returns The method is returning the newNode.
1593
+ */
1594
+ _replaceNode(oldNode, newNode) {
1595
+ if (oldNode.parent) {
1596
+ if (oldNode.parent.left === oldNode) {
1597
+ oldNode.parent.left = newNode;
1598
+ }
1599
+ else if (oldNode.parent.right === oldNode) {
1600
+ oldNode.parent.right = newNode;
1601
+ }
1602
+ }
1603
+ newNode.left = oldNode.left;
1604
+ newNode.right = oldNode.right;
1605
+ newNode.parent = oldNode.parent;
1606
+ if (this.root === oldNode) {
1607
+ this._root = newNode;
1608
+ }
1609
+ return newNode;
1610
+ }
1623
1611
  /**
1624
1612
  * The function `_addTo` adds a new node to a binary tree if there is an available position.
1625
1613
  * @param {N | null | undefined} newNode - The `newNode` parameter represents the node that you want to add to