min-heap-typed 1.51.8 → 1.51.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +104 -66
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
- package/dist/data-structures/binary-tree/avl-tree.d.ts +80 -60
- package/dist/data-structures/binary-tree/avl-tree.js +78 -59
- package/dist/data-structures/binary-tree/binary-tree.d.ts +316 -224
- package/dist/data-structures/binary-tree/binary-tree.js +471 -361
- package/dist/data-structures/binary-tree/bst.d.ts +198 -200
- package/dist/data-structures/binary-tree/bst.js +215 -249
- package/dist/data-structures/binary-tree/rb-tree.d.ts +71 -72
- package/dist/data-structures/binary-tree/rb-tree.js +107 -98
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +90 -73
- package/dist/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/data-structures/graph/abstract-graph.js +10 -15
- package/dist/data-structures/hash/hash-map.d.ts +31 -38
- package/dist/data-structures/hash/hash-map.js +40 -55
- package/dist/data-structures/queue/deque.d.ts +2 -3
- package/dist/data-structures/queue/deque.js +2 -3
- package/dist/data-structures/trie/trie.d.ts +1 -1
- package/dist/data-structures/trie/trie.js +1 -1
- package/dist/interfaces/binary-tree.d.ts +6 -6
- package/dist/types/common.d.ts +1 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +5 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -3
- package/dist/utils/utils.js +3 -5
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +142 -92
- package/src/data-structures/binary-tree/avl-tree.ts +94 -66
- package/src/data-structures/binary-tree/binary-tree.ts +530 -398
- package/src/data-structures/binary-tree/bst.ts +251 -270
- package/src/data-structures/binary-tree/rb-tree.ts +121 -100
- package/src/data-structures/binary-tree/tree-multi-map.ts +125 -99
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/hash/hash-map.ts +42 -49
- package/src/data-structures/queue/deque.ts +2 -2
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/trie/trie.ts +2 -2
- package/src/interfaces/binary-tree.ts +8 -7
- package/src/types/common.ts +1 -2
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -2
- package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +5 -4
- package/src/types/data-structures/binary-tree/bst.ts +4 -4
- package/src/types/data-structures/binary-tree/rb-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -3
- package/src/utils/utils.ts +3 -3
|
@@ -95,27 +95,29 @@ exports.BinaryTreeNode = BinaryTreeNode;
|
|
|
95
95
|
*/
|
|
96
96
|
class BinaryTree extends base_1.IterableEntryBase {
|
|
97
97
|
/**
|
|
98
|
-
* The constructor function initializes a binary tree object with optional
|
|
99
|
-
* @param [
|
|
98
|
+
* The constructor function initializes a binary tree object with optional keysOrNodesOrEntriesOrRawElements and options.
|
|
99
|
+
* @param [keysOrNodesOrEntriesOrRawElements] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the
|
|
100
100
|
* nodes to be added to the binary tree.
|
|
101
101
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
102
102
|
* configuration options for the binary tree. In this case, it is of type
|
|
103
103
|
* `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
|
|
104
104
|
* required.
|
|
105
105
|
*/
|
|
106
|
-
constructor(
|
|
106
|
+
constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
|
|
107
107
|
super();
|
|
108
108
|
this.iterationType = 'ITERATIVE';
|
|
109
|
+
this._size = 0;
|
|
109
110
|
this._NIL = new BinaryTreeNode(NaN);
|
|
110
111
|
this._DEFAULT_CALLBACK = (node) => (node ? node.key : undefined);
|
|
111
112
|
if (options) {
|
|
112
|
-
const { iterationType } = options;
|
|
113
|
+
const { iterationType, toEntryFn } = options;
|
|
113
114
|
if (iterationType)
|
|
114
115
|
this.iterationType = iterationType;
|
|
116
|
+
if (typeof toEntryFn === 'function')
|
|
117
|
+
this._toEntryFn = toEntryFn;
|
|
115
118
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
this.addMany(keysOrNodesOrEntries);
|
|
119
|
+
if (keysOrNodesOrEntriesOrRawElements)
|
|
120
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
119
121
|
}
|
|
120
122
|
/**
|
|
121
123
|
* The function returns the root node, which can be of type NODE, null, or undefined.
|
|
@@ -139,6 +141,13 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
139
141
|
get NIL() {
|
|
140
142
|
return this._NIL;
|
|
141
143
|
}
|
|
144
|
+
/**
|
|
145
|
+
* The function returns the value of the _toEntryFn property.
|
|
146
|
+
* @returns The function being returned is `this._toEntryFn`.
|
|
147
|
+
*/
|
|
148
|
+
get toEntryFn() {
|
|
149
|
+
return this._toEntryFn;
|
|
150
|
+
}
|
|
142
151
|
/**
|
|
143
152
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
144
153
|
* @param {K} key - The key for the new node.
|
|
@@ -159,42 +168,42 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
159
168
|
return new BinaryTree([], Object.assign({ iterationType: this.iterationType }, options));
|
|
160
169
|
}
|
|
161
170
|
/**
|
|
162
|
-
* The function `
|
|
163
|
-
*
|
|
171
|
+
* The function `keyValueOrEntryOrRawElementToNode` converts a key-value pair, entry, or raw element
|
|
172
|
+
* into a node object.
|
|
173
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
174
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
164
175
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
165
|
-
* `
|
|
166
|
-
*
|
|
167
|
-
* @returns
|
|
176
|
+
* `keyValueOrEntryOrRawElementToNode` function. It represents the value associated with a key in a
|
|
177
|
+
* key-value pair. If provided, it will be used to create a node with the specified key and value.
|
|
178
|
+
* @returns The function `keyValueOrEntryOrRawElementToNode` returns either a `NODE` object, `null`,
|
|
179
|
+
* or `undefined`.
|
|
168
180
|
*/
|
|
169
|
-
|
|
170
|
-
if (
|
|
181
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) {
|
|
182
|
+
if (keyOrNodeOrEntryOrRawElement === undefined)
|
|
171
183
|
return;
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
const [key,
|
|
178
|
-
if (key
|
|
184
|
+
if (keyOrNodeOrEntryOrRawElement === null)
|
|
185
|
+
return null;
|
|
186
|
+
if (this.isNode(keyOrNodeOrEntryOrRawElement))
|
|
187
|
+
return keyOrNodeOrEntryOrRawElement;
|
|
188
|
+
if (this.toEntryFn) {
|
|
189
|
+
const [key, entryValue] = this.toEntryFn(keyOrNodeOrEntryOrRawElement);
|
|
190
|
+
if (key)
|
|
191
|
+
return this.createNode(key, entryValue !== null && entryValue !== void 0 ? entryValue : value);
|
|
192
|
+
else
|
|
179
193
|
return;
|
|
180
|
-
}
|
|
181
|
-
else if (key === null) {
|
|
182
|
-
node = null;
|
|
183
|
-
}
|
|
184
|
-
else {
|
|
185
|
-
node = this.createNode(key, value);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
else if (this.isNode(keyOrNodeOrEntry)) {
|
|
189
|
-
node = keyOrNodeOrEntry;
|
|
190
194
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
195
|
+
if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
196
|
+
const [key, value] = keyOrNodeOrEntryOrRawElement;
|
|
197
|
+
if (key === undefined)
|
|
198
|
+
return;
|
|
199
|
+
else if (key === null)
|
|
200
|
+
return null;
|
|
201
|
+
else
|
|
202
|
+
return this.createNode(key, value);
|
|
196
203
|
}
|
|
197
|
-
|
|
204
|
+
if (this.isKey(keyOrNodeOrEntryOrRawElement))
|
|
205
|
+
return this.createNode(keyOrNodeOrEntryOrRawElement, value);
|
|
206
|
+
return;
|
|
198
207
|
}
|
|
199
208
|
/**
|
|
200
209
|
* Time Complexity: O(n)
|
|
@@ -204,56 +213,56 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
204
213
|
* Time Complexity: O(n)
|
|
205
214
|
* Space Complexity: O(log n)
|
|
206
215
|
*
|
|
207
|
-
* The
|
|
208
|
-
*
|
|
209
|
-
* @param {
|
|
210
|
-
* `
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
*/
|
|
217
|
-
ensureNode(
|
|
218
|
-
if (
|
|
216
|
+
* The `ensureNode` function checks if the input is a valid node and returns it, or converts it to a
|
|
217
|
+
* node if it is a key or entry.
|
|
218
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
219
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, `KeyOrNodeOrEntry<K, V, NODE>`, or
|
|
220
|
+
* a raw element.
|
|
221
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
222
|
+
* parameter that specifies the type of iteration to be used when searching for a node. It has a
|
|
223
|
+
* default value of `'ITERATIVE'`.
|
|
224
|
+
* @returns The function `ensureNode` returns either a `NODE` object, `null`, or `undefined`.
|
|
225
|
+
*/
|
|
226
|
+
ensureNode(keyOrNodeOrEntryOrRawElement, iterationType = 'ITERATIVE') {
|
|
227
|
+
if (keyOrNodeOrEntryOrRawElement === null)
|
|
228
|
+
return null;
|
|
229
|
+
if (keyOrNodeOrEntryOrRawElement === undefined)
|
|
230
|
+
return;
|
|
231
|
+
if (keyOrNodeOrEntryOrRawElement === this.NIL)
|
|
219
232
|
return;
|
|
220
|
-
if (this.
|
|
221
|
-
return
|
|
233
|
+
if (this.isNode(keyOrNodeOrEntryOrRawElement))
|
|
234
|
+
return keyOrNodeOrEntryOrRawElement;
|
|
235
|
+
if (this.toEntryFn) {
|
|
236
|
+
const [key] = this.toEntryFn(keyOrNodeOrEntryOrRawElement);
|
|
237
|
+
if (key)
|
|
238
|
+
return this.getNodeByKey(key);
|
|
222
239
|
}
|
|
223
|
-
if (this.isEntry(
|
|
224
|
-
const key =
|
|
240
|
+
if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
241
|
+
const key = keyOrNodeOrEntryOrRawElement[0];
|
|
225
242
|
if (key === null)
|
|
226
243
|
return null;
|
|
227
244
|
if (key === undefined)
|
|
228
245
|
return;
|
|
229
246
|
return this.getNodeByKey(key, iterationType);
|
|
230
247
|
}
|
|
231
|
-
if (
|
|
232
|
-
return
|
|
233
|
-
|
|
234
|
-
return;
|
|
235
|
-
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
248
|
+
if (this.isKey(keyOrNodeOrEntryOrRawElement))
|
|
249
|
+
return this.getNodeByKey(keyOrNodeOrEntryOrRawElement, iterationType);
|
|
250
|
+
return;
|
|
236
251
|
}
|
|
237
252
|
/**
|
|
238
|
-
* The function checks if
|
|
239
|
-
* @param {
|
|
240
|
-
*
|
|
253
|
+
* The function checks if the input is an instance of the BinaryTreeNode class.
|
|
254
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
255
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
256
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
257
|
+
* an instance of the `BinaryTreeNode` class.
|
|
241
258
|
*/
|
|
242
|
-
|
|
243
|
-
return
|
|
259
|
+
isNode(keyOrNodeOrEntryOrRawElement) {
|
|
260
|
+
return keyOrNodeOrEntryOrRawElement instanceof BinaryTreeNode;
|
|
244
261
|
}
|
|
245
262
|
/**
|
|
246
|
-
* The function
|
|
247
|
-
* @param
|
|
248
|
-
*
|
|
249
|
-
*/
|
|
250
|
-
isNode(keyOrNodeOrEntry) {
|
|
251
|
-
return keyOrNodeOrEntry instanceof BinaryTreeNode;
|
|
252
|
-
}
|
|
253
|
-
/**
|
|
254
|
-
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
255
|
-
* BinaryTreeNode and its key is not NaN.
|
|
256
|
-
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
263
|
+
* The function checks if a given node is a valid node in a binary search tree.
|
|
264
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
|
|
265
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
257
266
|
* @returns a boolean value.
|
|
258
267
|
*/
|
|
259
268
|
isRealNode(node) {
|
|
@@ -262,21 +271,64 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
262
271
|
return this.isNode(node);
|
|
263
272
|
}
|
|
264
273
|
/**
|
|
265
|
-
* The function checks if a given node is a
|
|
266
|
-
* @param {
|
|
274
|
+
* The function checks if a given node is a real node or null.
|
|
275
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
|
|
276
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
277
|
+
* @returns a boolean value.
|
|
278
|
+
*/
|
|
279
|
+
isNodeOrNull(node) {
|
|
280
|
+
return this.isRealNode(node) || node === null;
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* The function checks if a given node is equal to the NIL value.
|
|
284
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
|
|
285
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
267
286
|
* @returns a boolean value.
|
|
268
287
|
*/
|
|
269
288
|
isNIL(node) {
|
|
270
289
|
return node === this.NIL;
|
|
271
290
|
}
|
|
272
291
|
/**
|
|
273
|
-
* The function checks if
|
|
274
|
-
*
|
|
275
|
-
*
|
|
292
|
+
* The function checks if the input is an array with two elements, indicating it is a binary tree
|
|
293
|
+
* node entry.
|
|
294
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
295
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
276
296
|
* @returns a boolean value.
|
|
277
297
|
*/
|
|
278
|
-
isEntry(
|
|
279
|
-
return Array.isArray(
|
|
298
|
+
isEntry(keyOrNodeOrEntryOrRawElement) {
|
|
299
|
+
return Array.isArray(keyOrNodeOrEntryOrRawElement) && keyOrNodeOrEntryOrRawElement.length === 2;
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* The function checks if a given value is a valid key by evaluating its type and value.
|
|
303
|
+
* @param {any} key - The `key` parameter can be of any type. It is the value that we want to check
|
|
304
|
+
* if it is a valid key.
|
|
305
|
+
* @param [isCheckValueOf=true] - The `isCheckValueOf` parameter is a boolean flag that determines
|
|
306
|
+
* whether the function should check the valueOf() method of an object when the key is of type
|
|
307
|
+
* 'object'. If `isCheckValueOf` is true, the function will recursively call itself with the value
|
|
308
|
+
* returned by key.valueOf().
|
|
309
|
+
* @returns a boolean value.
|
|
310
|
+
*/
|
|
311
|
+
isKey(key, isCheckValueOf = true) {
|
|
312
|
+
if (key === null)
|
|
313
|
+
return true;
|
|
314
|
+
const keyType = typeof key;
|
|
315
|
+
if (keyType === 'string' || keyType === 'bigint' || keyType === 'boolean')
|
|
316
|
+
return true;
|
|
317
|
+
if (keyType === 'number')
|
|
318
|
+
return !isNaN(key);
|
|
319
|
+
if (keyType === 'symbol' || keyType === 'undefined')
|
|
320
|
+
return false;
|
|
321
|
+
if (keyType === 'function')
|
|
322
|
+
return this.isKey(key());
|
|
323
|
+
if (keyType === 'object') {
|
|
324
|
+
if (typeof key.toString === 'function')
|
|
325
|
+
return true;
|
|
326
|
+
if (isCheckValueOf && typeof key.valueOf === 'function') {
|
|
327
|
+
this.isKey(key.valueOf(), false);
|
|
328
|
+
}
|
|
329
|
+
return false;
|
|
330
|
+
}
|
|
331
|
+
return false;
|
|
280
332
|
}
|
|
281
333
|
/**
|
|
282
334
|
* Time Complexity O(n)
|
|
@@ -286,14 +338,20 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
286
338
|
* Time Complexity O(n)
|
|
287
339
|
* Space Complexity O(1)
|
|
288
340
|
*
|
|
289
|
-
* The `add` function
|
|
290
|
-
*
|
|
291
|
-
* @param
|
|
292
|
-
*
|
|
293
|
-
*
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
341
|
+
* The `add` function is used to insert a new node into a binary tree, checking for duplicate keys
|
|
342
|
+
* and finding the appropriate insertion position.
|
|
343
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
|
|
344
|
+
* `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which represents the key,
|
|
345
|
+
* node, entry, or raw element to be added to the tree. It can also accept a value of type
|
|
346
|
+
* `KeyOrNodeOrEntry<K, V, NODE>
|
|
347
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
348
|
+
* key being added to the tree. It represents the value that will be stored in the tree for the given
|
|
349
|
+
* key.
|
|
350
|
+
* @returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
351
|
+
* insertion position cannot be found or if there are duplicate keys.
|
|
352
|
+
*/
|
|
353
|
+
add(keyOrNodeOrEntryOrRawElement, value) {
|
|
354
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
|
|
297
355
|
if (newNode === undefined)
|
|
298
356
|
return false;
|
|
299
357
|
// If the tree is empty, directly set the new node as the root node
|
|
@@ -347,20 +405,24 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
347
405
|
* Time Complexity: O(k * n)
|
|
348
406
|
* Space Complexity: O(1)
|
|
349
407
|
*
|
|
350
|
-
* The `addMany` function takes in
|
|
351
|
-
* adds each node with its corresponding value to
|
|
352
|
-
*
|
|
353
|
-
* @param
|
|
354
|
-
*
|
|
355
|
-
|
|
356
|
-
|
|
408
|
+
* The `addMany` function takes in an iterable of keys or nodes or entries or raw elements, and an
|
|
409
|
+
* optional iterable of values, and adds each key or node or entry with its corresponding value to a
|
|
410
|
+
* data structure, returning an array of booleans indicating whether each insertion was successful.
|
|
411
|
+
* @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
|
|
412
|
+
* elements. These elements will be added to the data structure.
|
|
413
|
+
* @param [values] - An optional iterable of values that correspond to the keys or nodes or entries
|
|
414
|
+
* in the `keysOrNodesOrEntriesOrRawElements` parameter.
|
|
415
|
+
* @returns The function `addMany` returns an array of booleans indicating whether each element was
|
|
416
|
+
* successfully added to the data structure.
|
|
417
|
+
*/
|
|
418
|
+
addMany(keysOrNodesOrEntriesOrRawElements, values) {
|
|
357
419
|
// TODO not sure addMany not be run multi times
|
|
358
420
|
const inserted = [];
|
|
359
421
|
let valuesIterator;
|
|
360
422
|
if (values) {
|
|
361
423
|
valuesIterator = values[Symbol.iterator]();
|
|
362
424
|
}
|
|
363
|
-
for (const
|
|
425
|
+
for (const keyOrNodeOrEntryOrRawElement of keysOrNodesOrEntriesOrRawElements) {
|
|
364
426
|
let value = undefined;
|
|
365
427
|
if (valuesIterator) {
|
|
366
428
|
const valueResult = valuesIterator.next();
|
|
@@ -368,7 +430,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
368
430
|
value = valueResult.value;
|
|
369
431
|
}
|
|
370
432
|
}
|
|
371
|
-
inserted.push(this.add(
|
|
433
|
+
inserted.push(this.add(keyOrNodeOrEntryOrRawElement, value));
|
|
372
434
|
}
|
|
373
435
|
return inserted;
|
|
374
436
|
}
|
|
@@ -381,36 +443,34 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
381
443
|
* Time Complexity: O(k * n)
|
|
382
444
|
* Space Complexity: O(1)
|
|
383
445
|
*
|
|
384
|
-
* The `refill` function clears the current data and adds new
|
|
385
|
-
* @param
|
|
386
|
-
* KeyOrNodeOrEntry<K, V, NODE
|
|
387
|
-
* @param [values] - The `values` parameter is an optional iterable
|
|
388
|
-
*
|
|
389
|
-
*
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
refill(keysOrNodesOrEntries, values) {
|
|
446
|
+
* The `refill` function clears the current data and adds new data to the collection.
|
|
447
|
+
* @param keysOrNodesOrEntriesOrRawElements - An iterable collection of keys, nodes, entries, or raw
|
|
448
|
+
* elements. These can be of any type (R) or a specific type (KeyOrNodeOrEntry<K, V, NODE>).
|
|
449
|
+
* @param [values] - The `values` parameter is an optional iterable of values that will be associated
|
|
450
|
+
* with the keys or nodes being added. If provided, the values will be assigned to the corresponding
|
|
451
|
+
* keys or nodes. If not provided, the values will be set to `undefined`.
|
|
452
|
+
*/
|
|
453
|
+
refill(keysOrNodesOrEntriesOrRawElements, values) {
|
|
393
454
|
this.clear();
|
|
394
|
-
this.addMany(
|
|
455
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements, values);
|
|
395
456
|
}
|
|
396
457
|
/**
|
|
397
458
|
* Time Complexity: O(n)
|
|
398
459
|
* Space Complexity: O(1)
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
/**
|
|
460
|
+
*/
|
|
461
|
+
/**
|
|
402
462
|
* Time Complexity: O(n)
|
|
403
463
|
* Space Complexity: O(1)
|
|
404
464
|
*
|
|
405
|
-
* The function
|
|
406
|
-
*
|
|
407
|
-
* @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value
|
|
408
|
-
*
|
|
409
|
-
* the callback function
|
|
410
|
-
* specific node based on its value or object.
|
|
465
|
+
* The above function is a TypeScript implementation of deleting a node from a binary tree, returning
|
|
466
|
+
* the deleted node and the node that needs to be balanced.
|
|
467
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
468
|
+
* used to identify the node that needs to be deleted from the binary tree. It can be of any type
|
|
469
|
+
* that is returned by the callback function.
|
|
411
470
|
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
412
|
-
* identifier of the node to be deleted. It is
|
|
413
|
-
*
|
|
471
|
+
* identifier of the node to be deleted. It is of type `C`, which extends the `BTNCallback<NODE>`
|
|
472
|
+
* interface. The `BTNCallback<NODE>` interface represents a callback function that takes a node of
|
|
473
|
+
* type `NODE
|
|
414
474
|
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
415
475
|
*/
|
|
416
476
|
delete(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
@@ -465,28 +525,27 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
465
525
|
*/
|
|
466
526
|
/**
|
|
467
527
|
* Time Complexity: O(n)
|
|
468
|
-
* Space Complexity: O(k + log n)
|
|
528
|
+
* Space Complexity: O(k + log n)
|
|
469
529
|
*
|
|
470
|
-
* The function `getNodes`
|
|
471
|
-
*
|
|
530
|
+
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
531
|
+
* recursive or iterative approach.
|
|
472
532
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
473
|
-
* that
|
|
474
|
-
* callback function
|
|
475
|
-
*
|
|
476
|
-
*
|
|
477
|
-
*
|
|
478
|
-
*
|
|
479
|
-
*
|
|
480
|
-
*
|
|
481
|
-
*
|
|
482
|
-
*
|
|
483
|
-
*
|
|
484
|
-
*
|
|
485
|
-
*
|
|
486
|
-
*
|
|
487
|
-
*
|
|
488
|
-
*
|
|
489
|
-
* @returns an array of nodes of type `NODE`.
|
|
533
|
+
* that is used to identify the nodes. It can be of any type and is used to match against the result
|
|
534
|
+
* of the callback function for each node.
|
|
535
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node as input and
|
|
536
|
+
* returns a value. This value is used to identify the nodes that match the given identifier. The
|
|
537
|
+
* `callback` function is optional and defaults to a default callback function
|
|
538
|
+
* (`this._DEFAULT_CALLBACK`) if not provided.
|
|
539
|
+
* @param [onlyOne=false] - A boolean value indicating whether to return only one node that matches
|
|
540
|
+
* the identifier or all nodes that match the identifier. If set to true, only the first matching
|
|
541
|
+
* node will be returned. If set to false, all matching nodes will be returned. The default value is
|
|
542
|
+
* false.
|
|
543
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
544
|
+
* point for the search. It can be either a node object, a key-value pair, or a key. If it is not
|
|
545
|
+
* provided, the `root` of the data structure is used as the starting point.
|
|
546
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
547
|
+
* iteration to be performed on the nodes of a binary tree. It can have two possible values:
|
|
548
|
+
* @returns an array of NODE objects.
|
|
490
549
|
*/
|
|
491
550
|
getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
492
551
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -531,24 +590,21 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
531
590
|
*/
|
|
532
591
|
/**
|
|
533
592
|
* Time Complexity: O(n)
|
|
534
|
-
* Space Complexity: O(log n)
|
|
593
|
+
* Space Complexity: O(log n).
|
|
535
594
|
*
|
|
536
|
-
* The function `getNode` returns the first node that matches the given identifier and callback
|
|
537
|
-
*
|
|
595
|
+
* The function `getNode` returns the first node that matches the given identifier and callback,
|
|
596
|
+
* starting from the specified root node and using the specified iteration type.
|
|
538
597
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
539
|
-
* used to identify the node you want to retrieve. It can be of any type that is
|
|
540
|
-
* callback function
|
|
541
|
-
*
|
|
542
|
-
*
|
|
543
|
-
*
|
|
544
|
-
*
|
|
545
|
-
*
|
|
546
|
-
*
|
|
547
|
-
*
|
|
548
|
-
* @
|
|
549
|
-
* be performed when searching for nodes in the binary tree. It determines the order in which the
|
|
550
|
-
* nodes are visited during the search.
|
|
551
|
-
* @returns a value of type `NODE | null | undefined`.
|
|
598
|
+
* used to identify the node you want to retrieve. It can be of any type that is the return type of
|
|
599
|
+
* the `C` callback function, or it can be `null` or `undefined`.
|
|
600
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
601
|
+
* node matches the desired criteria. It should return a value that can be used to identify the node.
|
|
602
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
603
|
+
* point for searching nodes in a tree structure. It can be either a root node, a key-value pair, or
|
|
604
|
+
* a node entry. If not provided, the search will start from the root of the tree.
|
|
605
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
606
|
+
* of iteration to be performed when searching for nodes. It can have one of the following values:
|
|
607
|
+
* @returns The method is returning a NODE object, or null, or undefined.
|
|
552
608
|
*/
|
|
553
609
|
getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
554
610
|
var _a;
|
|
@@ -562,15 +618,13 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
562
618
|
* Time Complexity: O(n)
|
|
563
619
|
* Space Complexity: O(log n)
|
|
564
620
|
*
|
|
565
|
-
* The function `getNodeByKey`
|
|
566
|
-
*
|
|
567
|
-
*
|
|
568
|
-
*
|
|
569
|
-
*
|
|
570
|
-
*
|
|
571
|
-
*
|
|
572
|
-
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
573
|
-
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
621
|
+
* The function `getNodeByKey` returns a node with a specific key value from a tree structure.
|
|
622
|
+
* @param {K} key - The key parameter is the value that you want to search for in the tree. It is
|
|
623
|
+
* used to find the node with the matching key value.
|
|
624
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
625
|
+
* parameter that specifies the type of iteration to be used when searching for a node in the tree.
|
|
626
|
+
* It has a default value of `'ITERATIVE'`.
|
|
627
|
+
* @returns a value of type NODE, null, or undefined.
|
|
574
628
|
*/
|
|
575
629
|
getNodeByKey(key, iterationType = 'ITERATIVE') {
|
|
576
630
|
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
@@ -583,23 +637,22 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
583
637
|
* Time Complexity: O(n)
|
|
584
638
|
* Space Complexity: O(log n)
|
|
585
639
|
*
|
|
586
|
-
* The function `get`
|
|
587
|
-
*
|
|
640
|
+
* The function `get` in TypeScript overrides the base class method and returns the value associated
|
|
641
|
+
* with the given identifier.
|
|
588
642
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
589
|
-
* used to identify the node in the binary tree. It can be of any type that is
|
|
643
|
+
* used to identify the node in the binary tree. It can be of any type that is returned by the
|
|
590
644
|
* callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
|
|
591
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
592
|
-
* the
|
|
593
|
-
*
|
|
594
|
-
*
|
|
595
|
-
*
|
|
596
|
-
*
|
|
597
|
-
*
|
|
598
|
-
*
|
|
599
|
-
*
|
|
600
|
-
*
|
|
601
|
-
*
|
|
602
|
-
* found, `undefined` is returned.
|
|
645
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
646
|
+
* node matches the given identifier. It is optional and defaults to `this._DEFAULT_CALLBACK`.
|
|
647
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
648
|
+
* point for the search in the binary tree. It can be either a root node of the tree or a key, node,
|
|
649
|
+
* or entry object that exists in the tree. If no specific starting point is provided, the search
|
|
650
|
+
* will begin from the root of the
|
|
651
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
652
|
+
* of iteration to be performed when searching for a node in the tree. It can have one of the
|
|
653
|
+
* following values:
|
|
654
|
+
* @returns The method is returning the value associated with the specified identifier in the binary
|
|
655
|
+
* tree.
|
|
603
656
|
*/
|
|
604
657
|
get(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
605
658
|
var _a;
|
|
@@ -607,28 +660,27 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
607
660
|
}
|
|
608
661
|
/**
|
|
609
662
|
* Time Complexity: O(n)
|
|
610
|
-
* Space Complexity: O(log n)
|
|
663
|
+
* Space Complexity: O(log n)
|
|
611
664
|
*/
|
|
612
665
|
/**
|
|
613
666
|
* Time Complexity: O(n)
|
|
614
|
-
* Space Complexity: O(log n)
|
|
667
|
+
* Space Complexity: O(log n)
|
|
615
668
|
*
|
|
616
|
-
* The function checks if a
|
|
669
|
+
* The `has` function checks if a given identifier exists in the data structure and returns a boolean
|
|
670
|
+
* value.
|
|
617
671
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
618
|
-
*
|
|
619
|
-
* callback function `C`. It can also be `null` or `undefined` if
|
|
620
|
-
*
|
|
621
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
622
|
-
*
|
|
623
|
-
*
|
|
624
|
-
*
|
|
625
|
-
*
|
|
626
|
-
*
|
|
627
|
-
*
|
|
628
|
-
*
|
|
629
|
-
*
|
|
630
|
-
* be performed in a pre-order, in-order, or post-order manner.
|
|
631
|
-
* @returns a boolean value.
|
|
672
|
+
* used to identify a specific node or entry in the data structure. It can be of any type that is
|
|
673
|
+
* returned by the callback function `C`. It can also be `null` or `undefined` if no specific
|
|
674
|
+
* identifier is provided.
|
|
675
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine
|
|
676
|
+
* whether a node should be included in the result or not. It is of type `C`, which extends the
|
|
677
|
+
* `BTNCallback<NODE>` type.
|
|
678
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
679
|
+
* point for the iteration in the data structure. It can be either a root node, a key-value pair, or
|
|
680
|
+
* a node entry. If not specified, it defaults to the root of the data structure.
|
|
681
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
682
|
+
* of iteration to be performed. It is an optional parameter with a default value of `IterationType`.
|
|
683
|
+
* @returns The method is returning a boolean value.
|
|
632
684
|
*/
|
|
633
685
|
has(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
634
686
|
callback = this._ensureCallback(identifier, callback);
|
|
@@ -672,9 +724,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
672
724
|
*
|
|
673
725
|
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
674
726
|
* height of the tree.
|
|
675
|
-
* @param {
|
|
676
|
-
*
|
|
677
|
-
*
|
|
727
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The parameter `beginRoot` is optional and
|
|
728
|
+
* has a default value of `this.root`. It represents the starting point for checking if the tree is
|
|
729
|
+
* perfectly balanced. It can be either a root node (`R`), a key or node or entry
|
|
730
|
+
* (`KeyOrNodeOrEntry<K, V, NODE
|
|
678
731
|
* @returns a boolean value.
|
|
679
732
|
*/
|
|
680
733
|
isPerfectlyBalanced(beginRoot = this.root) {
|
|
@@ -688,12 +741,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
688
741
|
* Time Complexity: O(n)
|
|
689
742
|
* Space Complexity: O(1)
|
|
690
743
|
*
|
|
691
|
-
* The function `
|
|
692
|
-
* @param {
|
|
693
|
-
*
|
|
694
|
-
*
|
|
695
|
-
*
|
|
696
|
-
*
|
|
744
|
+
* The function `isBST` checks if a binary search tree is valid, either recursively or iteratively.
|
|
745
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
746
|
+
* starting point for checking if a binary search tree (BST) is valid. It can be either a root node
|
|
747
|
+
* of the BST, a key value of a node in the BST, or an entry object containing both the key and value
|
|
748
|
+
* of a node in the BST
|
|
749
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to determine the type
|
|
750
|
+
* of iteration to be performed while checking if the binary search tree (BST) is valid. It can have
|
|
751
|
+
* two possible values:
|
|
697
752
|
* @returns a boolean value.
|
|
698
753
|
*/
|
|
699
754
|
isBST(beginRoot = this.root, iterationType = this.iterationType) {
|
|
@@ -746,14 +801,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
746
801
|
* Time Complexity: O(n)
|
|
747
802
|
* Space Complexity: O(1)
|
|
748
803
|
*
|
|
749
|
-
* The function calculates the depth of a given node in a
|
|
750
|
-
* @param {
|
|
751
|
-
*
|
|
752
|
-
*
|
|
753
|
-
* @param {
|
|
754
|
-
* from which
|
|
755
|
-
*
|
|
756
|
-
*
|
|
804
|
+
* The function calculates the depth of a given node or key in a tree-like data structure.
|
|
805
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} dist - The `dist` parameter can be either a `R`
|
|
806
|
+
* (representing a root node), or a `KeyOrNodeOrEntry<K, V, NODE>` (representing a key, node, or
|
|
807
|
+
* entry).
|
|
808
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is optional and
|
|
809
|
+
* represents the starting point from which to calculate the depth. It can be either a reference to a
|
|
810
|
+
* node in the tree or a key-value pair or an entry object. If not provided, the default value is
|
|
811
|
+
* `this.root`, which refers to the root node
|
|
812
|
+
* @returns the depth of a node in a tree structure.
|
|
757
813
|
*/
|
|
758
814
|
getDepth(dist, beginRoot = this.root) {
|
|
759
815
|
let distEnsured = this.ensureNode(dist);
|
|
@@ -774,17 +830,16 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
774
830
|
*/
|
|
775
831
|
/**
|
|
776
832
|
* Time Complexity: O(n)
|
|
777
|
-
* Space Complexity: O(
|
|
833
|
+
* Space Complexity: O(1)
|
|
778
834
|
*
|
|
779
|
-
* The
|
|
780
|
-
* iterative
|
|
781
|
-
* @param {
|
|
782
|
-
* starting
|
|
783
|
-
*
|
|
784
|
-
* @param iterationType - The `iterationType` parameter
|
|
785
|
-
*
|
|
786
|
-
*
|
|
787
|
-
* @returns the height of the binary tree.
|
|
835
|
+
* The `getHeight` function calculates the maximum height of a binary tree using either a recursive
|
|
836
|
+
* or iterative approach.
|
|
837
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
838
|
+
* starting point for calculating the height of a tree. It can be either a root node (`R`), a key or
|
|
839
|
+
* node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current tree.
|
|
840
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
841
|
+
* iteration used to calculate the height of the tree. It can have two possible values:
|
|
842
|
+
* @returns the maximum height of the binary tree.
|
|
788
843
|
*/
|
|
789
844
|
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
790
845
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -824,12 +879,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
824
879
|
*
|
|
825
880
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
826
881
|
* recursive or iterative approach.
|
|
827
|
-
* @param {
|
|
828
|
-
* starting
|
|
829
|
-
*
|
|
830
|
-
*
|
|
831
|
-
*
|
|
832
|
-
*
|
|
882
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
883
|
+
* starting point for calculating the minimum height of a tree. It can be either a root node (`R`), a
|
|
884
|
+
* key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current
|
|
885
|
+
* tree.
|
|
886
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
887
|
+
* iteration to be used when calculating the minimum height of the tree. It can have two possible
|
|
888
|
+
* values:
|
|
889
|
+
* @returns The function `getMinHeight` returns a number, which represents the minimum height of the
|
|
890
|
+
* binary tree.
|
|
833
891
|
*/
|
|
834
892
|
getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
835
893
|
var _a, _b, _c;
|
|
@@ -879,31 +937,27 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
879
937
|
/**
|
|
880
938
|
* Time Complexity: O(log n)
|
|
881
939
|
* Space Complexity: O(log n)
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
/**
|
|
940
|
+
*/
|
|
941
|
+
/**
|
|
885
942
|
* Time Complexity: O(log n)
|
|
886
943
|
* Space Complexity: O(log n)
|
|
887
944
|
*
|
|
888
|
-
* The function `getPathToRoot` returns an array of nodes from a given node
|
|
889
|
-
*
|
|
890
|
-
* @param {
|
|
891
|
-
*
|
|
892
|
-
* `null`, or `undefined`.
|
|
945
|
+
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
|
|
946
|
+
* up to the root node, with an option to reverse the order of the nodes.
|
|
947
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginNode - The `beginNode` parameter can be either of
|
|
948
|
+
* type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
893
949
|
* @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
|
|
894
950
|
* resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
|
|
895
|
-
* reversed before returning it. If `isReverse` is set to `false
|
|
896
|
-
* @returns The function `getPathToRoot` returns an array of
|
|
951
|
+
* reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
|
|
952
|
+
* @returns The function `getPathToRoot` returns an array of `NODE` objects.
|
|
897
953
|
*/
|
|
898
954
|
getPathToRoot(beginNode, isReverse = true) {
|
|
899
|
-
// TODO to support get path through passing key
|
|
900
955
|
const result = [];
|
|
901
956
|
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
902
957
|
if (!beginNodeEnsured)
|
|
903
958
|
return result;
|
|
904
959
|
while (beginNodeEnsured.parent) {
|
|
905
960
|
// Array.push + Array.reverse is more efficient than Array.unshift
|
|
906
|
-
// TODO may consider using Deque, so far this is not the performance bottleneck
|
|
907
961
|
result.push(beginNodeEnsured);
|
|
908
962
|
beginNodeEnsured = beginNodeEnsured.parent;
|
|
909
963
|
}
|
|
@@ -918,15 +972,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
918
972
|
* Time Complexity: O(log n)
|
|
919
973
|
* Space Complexity: O(1)
|
|
920
974
|
*
|
|
921
|
-
* The
|
|
922
|
-
*
|
|
923
|
-
* @param {
|
|
924
|
-
* for finding the leftmost node in a binary tree. It can be either a `
|
|
925
|
-
*
|
|
926
|
-
* @param iterationType - The `iterationType` parameter is used to
|
|
927
|
-
*
|
|
928
|
-
* @returns The function `getLeftMost` returns the leftmost node
|
|
929
|
-
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
975
|
+
* The `getLeftMost` function returns the leftmost node in a binary tree, either using recursive or
|
|
976
|
+
* iterative traversal.
|
|
977
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
978
|
+
* starting point for finding the leftmost node in a binary tree. It can be either a root node (`R`),
|
|
979
|
+
* a key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
|
|
980
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
981
|
+
* of iteration to be performed. It can have two possible values:
|
|
982
|
+
* @returns The function `getLeftMost` returns the leftmost node in a binary tree.
|
|
930
983
|
*/
|
|
931
984
|
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
932
985
|
if (this.isNIL(beginRoot))
|
|
@@ -960,16 +1013,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
960
1013
|
* Time Complexity: O(log n)
|
|
961
1014
|
* Space Complexity: O(1)
|
|
962
1015
|
*
|
|
963
|
-
* The
|
|
1016
|
+
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or
|
|
964
1017
|
* iteratively.
|
|
965
|
-
* @param {
|
|
966
|
-
* starting
|
|
967
|
-
* `
|
|
968
|
-
*
|
|
969
|
-
*
|
|
970
|
-
*
|
|
971
|
-
* @returns The function `getRightMost` returns
|
|
972
|
-
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
1018
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1019
|
+
* starting point for finding the rightmost node in a binary tree. It can be either a root node
|
|
1020
|
+
* (`R`), a key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
|
|
1021
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
1022
|
+
* of iteration to be performed when finding the rightmost node in a binary tree. It can have two
|
|
1023
|
+
* possible values:
|
|
1024
|
+
* @returns The function `getRightMost` returns a NODE object, `null`, or `undefined`.
|
|
973
1025
|
*/
|
|
974
1026
|
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
975
1027
|
if (this.isNIL(beginRoot))
|
|
@@ -1004,10 +1056,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1004
1056
|
* Time Complexity: O(log n)
|
|
1005
1057
|
* Space Complexity: O(1)
|
|
1006
1058
|
*
|
|
1007
|
-
* The function returns the predecessor of a given node in a tree.
|
|
1008
|
-
* @param {NODE} node - The parameter
|
|
1059
|
+
* The function returns the predecessor node of a given node in a binary tree.
|
|
1060
|
+
* @param {NODE} node - The parameter "node" is of type "NODE", which represents a node in a binary
|
|
1009
1061
|
* tree.
|
|
1010
|
-
* @returns the predecessor of the given
|
|
1062
|
+
* @returns the predecessor node of the given node.
|
|
1011
1063
|
*/
|
|
1012
1064
|
getPredecessor(node) {
|
|
1013
1065
|
if (this.isRealNode(node.left)) {
|
|
@@ -1033,8 +1085,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1033
1085
|
*
|
|
1034
1086
|
* The function `getSuccessor` returns the next node in a binary tree given a current node.
|
|
1035
1087
|
* @param {K | NODE | null} [x] - The parameter `x` can be of type `K`, `NODE`, or `null`.
|
|
1036
|
-
* @returns
|
|
1037
|
-
*
|
|
1088
|
+
* @returns The function `getSuccessor` returns a `NODE` object if a successor exists, `null` if
|
|
1089
|
+
* there is no successor, and `undefined` if the input `x` is not a valid node.
|
|
1038
1090
|
*/
|
|
1039
1091
|
getSuccessor(x) {
|
|
1040
1092
|
x = this.ensureNode(x);
|
|
@@ -1053,30 +1105,29 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1053
1105
|
/**
|
|
1054
1106
|
* Time complexity: O(n)
|
|
1055
1107
|
* Space complexity: O(n)
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
/**
|
|
1108
|
+
*/
|
|
1109
|
+
/**
|
|
1059
1110
|
* Time complexity: O(n)
|
|
1060
1111
|
* Space complexity: O(n)
|
|
1061
1112
|
*
|
|
1062
|
-
* The `dfs` function performs a depth-first search traversal on a binary tree
|
|
1063
|
-
*
|
|
1064
|
-
* callback function
|
|
1065
|
-
*
|
|
1066
|
-
*
|
|
1067
|
-
*
|
|
1068
|
-
*
|
|
1069
|
-
*
|
|
1070
|
-
*
|
|
1071
|
-
*
|
|
1072
|
-
*
|
|
1073
|
-
* @param {IterationType} iterationType - The `iterationType` parameter determines the
|
|
1074
|
-
* iteration to use
|
|
1113
|
+
* The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
|
|
1114
|
+
* function on each node according to a specified pattern and iteration type.
|
|
1115
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
1116
|
+
* visited during the depth-first search. It takes a node as an argument and returns a value. The
|
|
1117
|
+
* return type of the callback function is determined by the generic type `C`.
|
|
1118
|
+
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter determines the order in which the
|
|
1119
|
+
* nodes are visited during the depth-first search. It can have one of the following values:
|
|
1120
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
1121
|
+
* point of the depth-first search. It can be either a node object, a key-value pair, or a key. If it
|
|
1122
|
+
* is a key or key-value pair, the method will find the corresponding node in the tree and start the
|
|
1123
|
+
* search from there.
|
|
1124
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter determines the
|
|
1125
|
+
* type of iteration to use during the depth-first search. It can have two possible values:
|
|
1075
1126
|
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
1076
|
-
* whether
|
|
1077
|
-
* `true`, null
|
|
1078
|
-
*
|
|
1079
|
-
* @returns an array of
|
|
1127
|
+
* whether or not to include null values in the depth-first search traversal. If `includeNull` is set
|
|
1128
|
+
* to `true`, null values will be included in the traversal. If `includeNull` is set to `false`, null
|
|
1129
|
+
* values will
|
|
1130
|
+
* @returns an array of the return types of the callback function.
|
|
1080
1131
|
*/
|
|
1081
1132
|
dfs(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root, iterationType = 'ITERATIVE', includeNull = false) {
|
|
1082
1133
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1192,22 +1243,23 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1192
1243
|
* Time complexity: O(n)
|
|
1193
1244
|
* Space complexity: O(n)
|
|
1194
1245
|
*
|
|
1195
|
-
* The `bfs` function performs a breadth-first search
|
|
1196
|
-
*
|
|
1246
|
+
* The `bfs` function performs a breadth-first search on a binary tree, calling a callback function
|
|
1247
|
+
* on each node and returning an array of the results.
|
|
1197
1248
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1198
|
-
* the breadth-first search traversal. It takes a single
|
|
1249
|
+
* the breadth-first search traversal. It takes a single argument, which is the current node being
|
|
1199
1250
|
* visited, and returns a value of any type.
|
|
1200
|
-
* @param {
|
|
1201
|
-
* starting
|
|
1202
|
-
* or
|
|
1203
|
-
*
|
|
1204
|
-
* @param iterationType - The `iterationType` parameter determines the type of
|
|
1205
|
-
*
|
|
1206
|
-
* @param [includeNull=false] - The `includeNull` parameter is a boolean
|
|
1207
|
-
* to include null values in the breadth-first search traversal. If
|
|
1208
|
-
* `true`, null values will be included in the traversal
|
|
1209
|
-
*
|
|
1210
|
-
*
|
|
1251
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1252
|
+
* starting point of the breadth-first search. It can be either a root node of a tree or a key, node,
|
|
1253
|
+
* or entry object. If no value is provided, the `root` property of the class is used as the default
|
|
1254
|
+
* starting point.
|
|
1255
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
1256
|
+
* iteration to be performed. It can have two possible values:
|
|
1257
|
+
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
1258
|
+
* whether or not to include null values in the breadth-first search (BFS) traversal. If
|
|
1259
|
+
* `includeNull` is set to `true`, null values will be included in the traversal. If `includeNull` is
|
|
1260
|
+
* set to `false
|
|
1261
|
+
* @returns The function `bfs` returns an array of values that are the result of invoking the
|
|
1262
|
+
* `callback` function on each node in the breadth-first order traversal of the binary tree.
|
|
1211
1263
|
*/
|
|
1212
1264
|
bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
1213
1265
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1270,18 +1322,18 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1270
1322
|
* Space complexity: O(n)
|
|
1271
1323
|
*
|
|
1272
1324
|
* The `listLevels` function returns an array of arrays, where each inner array represents a level in
|
|
1273
|
-
* a binary tree and contains the
|
|
1274
|
-
* level.
|
|
1325
|
+
* a binary tree and contains the results of applying a callback function to the nodes at that level.
|
|
1275
1326
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1276
|
-
* the tree. It takes a
|
|
1277
|
-
*
|
|
1278
|
-
* @param {
|
|
1279
|
-
* starting
|
|
1280
|
-
*
|
|
1281
|
-
*
|
|
1282
|
-
*
|
|
1327
|
+
* the tree. It takes a node as an argument and returns a value. The return type of the callback
|
|
1328
|
+
* function is determined by the generic type `C` which extends `BTNCallback<NODE | null>`.
|
|
1329
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1330
|
+
* starting point for traversing the tree. It can be either a root node, a key-value pair, or a node
|
|
1331
|
+
* entry. If no value is provided, the `root` property of the class is used as the default starting
|
|
1332
|
+
* point.
|
|
1333
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
1334
|
+
* iteration to be performed on the binary tree. It can have two possible values:
|
|
1283
1335
|
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
1284
|
-
* whether to include null values in the resulting levels. If `includeNull` is set to `true`,
|
|
1336
|
+
* whether or not to include null values in the resulting levels. If `includeNull` is set to `true`,
|
|
1285
1337
|
* null values will be included in the levels. If `includeNull` is set to `false`, null values will
|
|
1286
1338
|
* be excluded
|
|
1287
1339
|
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
@@ -1346,17 +1398,17 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1346
1398
|
* The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
|
|
1347
1399
|
* algorithm.
|
|
1348
1400
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1349
|
-
* the tree. It takes a single
|
|
1350
|
-
*
|
|
1351
|
-
*
|
|
1352
|
-
*
|
|
1401
|
+
* the tree. It takes a single argument, which is the current node, and can return any value. The
|
|
1402
|
+
* return type of the `callback` function is determined by the `ReturnType<C>` type, which represents
|
|
1403
|
+
* the return
|
|
1404
|
+
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function is used
|
|
1405
|
+
* to specify the order in which the nodes of a binary tree are traversed. It can take one of the
|
|
1353
1406
|
* following values:
|
|
1354
|
-
* @param {
|
|
1355
|
-
* for the traversal. It can be
|
|
1356
|
-
* the root of the tree
|
|
1357
|
-
* @returns The function `morris` returns an array of values that are the
|
|
1358
|
-
*
|
|
1359
|
-
* by the return type of the `callback` function.
|
|
1407
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
1408
|
+
* point for the traversal. It can be either a node object, a key, or an entry object. If no value is
|
|
1409
|
+
* provided, the `root` of the tree is used as the starting point.
|
|
1410
|
+
* @returns The function `morris` returns an array of values that are the return values of the
|
|
1411
|
+
* callback function `callback`.
|
|
1360
1412
|
*/
|
|
1361
1413
|
morris(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root) {
|
|
1362
1414
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1451,8 +1503,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1451
1503
|
* Time complexity: O(n)
|
|
1452
1504
|
* Space complexity: O(n)
|
|
1453
1505
|
*
|
|
1454
|
-
* The `clone` function creates a
|
|
1455
|
-
* the new tree.
|
|
1506
|
+
* The `clone` function creates a deep copy of a tree object.
|
|
1456
1507
|
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
1457
1508
|
*/
|
|
1458
1509
|
clone() {
|
|
@@ -1473,16 +1524,16 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1473
1524
|
* Time Complexity: O(n)
|
|
1474
1525
|
* Space Complexity: O(n)
|
|
1475
1526
|
*
|
|
1476
|
-
* The `filter` function creates a new tree
|
|
1477
|
-
*
|
|
1478
|
-
*
|
|
1479
|
-
*
|
|
1480
|
-
*
|
|
1481
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that
|
|
1482
|
-
*
|
|
1483
|
-
*
|
|
1484
|
-
* @returns The `filter` method is returning a new tree object that contains the
|
|
1485
|
-
*
|
|
1527
|
+
* The `filter` function creates a new tree with entries that pass a given predicate function.
|
|
1528
|
+
* @param predicate - The `predicate` parameter is a callback function that is used to test each
|
|
1529
|
+
* element in the tree. It takes three arguments: `value`, `key`, and `index`. The `value` argument
|
|
1530
|
+
* represents the value of the current element being processed, the `key` argument represents the key
|
|
1531
|
+
* of the
|
|
1532
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
1533
|
+
* specify the value of `this` within the `predicate` function. When the `predicate` function is
|
|
1534
|
+
* called, `thisArg` will be used as the value of `this` within the function. If `thisArg`
|
|
1535
|
+
* @returns The `filter` method is returning a new tree object that contains the entries that pass
|
|
1536
|
+
* the given predicate function.
|
|
1486
1537
|
*/
|
|
1487
1538
|
filter(predicate, thisArg) {
|
|
1488
1539
|
const newTree = this.createTree();
|
|
@@ -1502,15 +1553,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1502
1553
|
* Time Complexity: O(n)
|
|
1503
1554
|
* Space Complexity: O(n)
|
|
1504
1555
|
*
|
|
1505
|
-
* The `map` function creates a new tree by applying a callback function to each
|
|
1506
|
-
*
|
|
1507
|
-
* @param callback - The callback parameter is a function that will be called for each
|
|
1508
|
-
*
|
|
1509
|
-
* the
|
|
1510
|
-
*
|
|
1511
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that
|
|
1512
|
-
*
|
|
1513
|
-
*
|
|
1556
|
+
* The `map` function creates a new tree by applying a callback function to each entry in the current
|
|
1557
|
+
* tree.
|
|
1558
|
+
* @param callback - The callback parameter is a function that will be called for each entry in the
|
|
1559
|
+
* tree. It takes three arguments: value, key, and index. The value argument represents the value of
|
|
1560
|
+
* the current entry, the key argument represents the key of the current entry, and the index
|
|
1561
|
+
* argument represents the index of the
|
|
1562
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
1563
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
1564
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
1514
1565
|
* @returns The `map` method is returning a new tree object.
|
|
1515
1566
|
*/
|
|
1516
1567
|
map(callback, thisArg) {
|
|
@@ -1538,11 +1589,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1538
1589
|
* Time Complexity: O(n)
|
|
1539
1590
|
* Space Complexity: O(n)
|
|
1540
1591
|
*
|
|
1541
|
-
* The `print` function
|
|
1542
|
-
* @param {
|
|
1543
|
-
*
|
|
1544
|
-
*
|
|
1545
|
-
*
|
|
1592
|
+
* The `print` function in TypeScript prints the binary tree structure with customizable options.
|
|
1593
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
1594
|
+
* point for printing the binary tree. It can be either a node of the binary tree or a key or entry
|
|
1595
|
+
* that exists in the binary tree. If no value is provided, the root of the binary tree will be used
|
|
1596
|
+
* as the starting point.
|
|
1597
|
+
* @param {BinaryTreePrintOptions} [options] - The `options` parameter is an optional object that
|
|
1598
|
+
* allows you to customize the printing behavior. It has the following properties:
|
|
1599
|
+
* @returns Nothing is being returned. The function has a return type of `void`, which means it does
|
|
1600
|
+
* not return any value.
|
|
1546
1601
|
*/
|
|
1547
1602
|
print(beginRoot = this.root, options) {
|
|
1548
1603
|
const opts = Object.assign({ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false }, options);
|
|
@@ -1567,13 +1622,18 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1567
1622
|
display(beginRoot);
|
|
1568
1623
|
}
|
|
1569
1624
|
/**
|
|
1570
|
-
*
|
|
1571
|
-
*
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
*
|
|
1575
|
-
*
|
|
1576
|
-
*
|
|
1625
|
+
* Time Complexity: O(1)
|
|
1626
|
+
* Space Complexity: O(1)
|
|
1627
|
+
*/
|
|
1628
|
+
/**
|
|
1629
|
+
* Time Complexity: O(1)
|
|
1630
|
+
* Space Complexity: O(1)
|
|
1631
|
+
*
|
|
1632
|
+
* The function `_getIterator` is a generator function that returns an iterator for the key-value
|
|
1633
|
+
* pairs in a binary search tree.
|
|
1634
|
+
* @param node - The `node` parameter represents the current node in the binary search tree. It is
|
|
1635
|
+
* initially set to the root node of the tree.
|
|
1636
|
+
* @returns an IterableIterator<[K, V | undefined]>.
|
|
1577
1637
|
*/
|
|
1578
1638
|
*_getIterator(node = this.root) {
|
|
1579
1639
|
if (!node)
|
|
@@ -1604,6 +1664,13 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1604
1664
|
}
|
|
1605
1665
|
}
|
|
1606
1666
|
/**
|
|
1667
|
+
* Time Complexity: O(n)
|
|
1668
|
+
* Space Complexity: O(n)
|
|
1669
|
+
*/
|
|
1670
|
+
/**
|
|
1671
|
+
* Time Complexity: O(n)
|
|
1672
|
+
* Space Complexity: O(n)
|
|
1673
|
+
*
|
|
1607
1674
|
* The `_displayAux` function is responsible for generating the display layout of a binary tree node,
|
|
1608
1675
|
* taking into account various options such as whether to show null, undefined, or NaN nodes.
|
|
1609
1676
|
* @param {NODE | null | undefined} node - The `node` parameter represents a node in a binary tree.
|
|
@@ -1670,10 +1737,21 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1670
1737
|
}
|
|
1671
1738
|
}
|
|
1672
1739
|
/**
|
|
1673
|
-
*
|
|
1674
|
-
*
|
|
1675
|
-
|
|
1676
|
-
|
|
1740
|
+
* Time Complexity: O(1)
|
|
1741
|
+
* Space Complexity: O(1)
|
|
1742
|
+
*/
|
|
1743
|
+
/**
|
|
1744
|
+
* Time Complexity: O(1)
|
|
1745
|
+
* Space Complexity: O(1)
|
|
1746
|
+
*
|
|
1747
|
+
* The function `_swapProperties` swaps the key-value properties between two nodes.
|
|
1748
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} srcNode - The source node that will be swapped with the
|
|
1749
|
+
* destination node. It can be either an instance of the class `R`, or an object of type
|
|
1750
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
1751
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} destNode - The `destNode` parameter is the node where
|
|
1752
|
+
* the properties will be swapped with the `srcNode`.
|
|
1753
|
+
* @returns either the `destNode` object with its properties swapped with the `srcNode` object's
|
|
1754
|
+
* properties, or `undefined` if either `srcNode` or `destNode` is falsy.
|
|
1677
1755
|
*/
|
|
1678
1756
|
_swapProperties(srcNode, destNode) {
|
|
1679
1757
|
srcNode = this.ensureNode(srcNode);
|
|
@@ -1692,12 +1770,20 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1692
1770
|
return undefined;
|
|
1693
1771
|
}
|
|
1694
1772
|
/**
|
|
1695
|
-
*
|
|
1773
|
+
* Time Complexity: O(1)
|
|
1774
|
+
* Space Complexity: O(1)
|
|
1775
|
+
*/
|
|
1776
|
+
/**
|
|
1777
|
+
* Time Complexity: O(1)
|
|
1778
|
+
* Space Complexity: O(1)
|
|
1779
|
+
*
|
|
1780
|
+
* The function replaces a node in a binary tree with a new node, updating the parent, left child,
|
|
1781
|
+
* right child, and root if necessary.
|
|
1696
1782
|
* @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
|
|
1697
1783
|
* tree.
|
|
1698
1784
|
* @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
1699
1785
|
* tree.
|
|
1700
|
-
* @returns
|
|
1786
|
+
* @returns the newNode.
|
|
1701
1787
|
*/
|
|
1702
1788
|
_replaceNode(oldNode, newNode) {
|
|
1703
1789
|
if (oldNode.parent) {
|
|
@@ -1717,10 +1803,17 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1717
1803
|
return newNode;
|
|
1718
1804
|
}
|
|
1719
1805
|
/**
|
|
1720
|
-
*
|
|
1721
|
-
*
|
|
1722
|
-
|
|
1723
|
-
|
|
1806
|
+
* Time Complexity: O(1)
|
|
1807
|
+
* Space Complexity: O(1)
|
|
1808
|
+
*/
|
|
1809
|
+
/**
|
|
1810
|
+
* Time Complexity: O(1)
|
|
1811
|
+
* Space Complexity: O(1)
|
|
1812
|
+
*
|
|
1813
|
+
* The function sets the root property of an object to the provided value, and also updates the
|
|
1814
|
+
* parent property of the new root.
|
|
1815
|
+
* @param {NODE | null | undefined} v - The parameter `v` is of type `NODE | null | undefined`. This
|
|
1816
|
+
* means that it can accept a value of type `NODE`, `null`, or `undefined`.
|
|
1724
1817
|
*/
|
|
1725
1818
|
_setRoot(v) {
|
|
1726
1819
|
if (v) {
|
|
@@ -1728,6 +1821,23 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1728
1821
|
}
|
|
1729
1822
|
this._root = v;
|
|
1730
1823
|
}
|
|
1824
|
+
/**
|
|
1825
|
+
* Time Complexity: O(1)
|
|
1826
|
+
* Space Complexity: O(1)
|
|
1827
|
+
*/
|
|
1828
|
+
/**
|
|
1829
|
+
* Time Complexity: O(1)
|
|
1830
|
+
* Space Complexity: O(1)
|
|
1831
|
+
*
|
|
1832
|
+
* The function `_ensureCallback` ensures that a callback function is provided and returns it.
|
|
1833
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is of type
|
|
1834
|
+
* `ReturnType<C> | null | undefined`. This means it can accept a value that is the return type of
|
|
1835
|
+
* the generic type `C`, or it can be `null` or `undefined`.
|
|
1836
|
+
* @param {C} callback - The `callback` parameter is a function that takes a `node` as an argument
|
|
1837
|
+
* and returns a value. It is of type `C`, which is a generic type that extends the
|
|
1838
|
+
* `BTNCallback<NODE>` type.
|
|
1839
|
+
* @returns the callback parameter.
|
|
1840
|
+
*/
|
|
1731
1841
|
_ensureCallback(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
1732
1842
|
if ((!callback || callback === this._DEFAULT_CALLBACK) && this.isNode(identifier)) {
|
|
1733
1843
|
callback = (node => node);
|