priority-queue-typed 1.49.5 → 1.49.6
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.d.ts +53 -48
- package/dist/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/data-structures/binary-tree/binary-tree.d.ts +153 -130
- package/dist/data-structures/binary-tree/binary-tree.js +192 -149
- package/dist/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/data-structures/binary-tree/bst.js +113 -89
- package/dist/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +46 -39
- package/dist/data-structures/binary-tree/tree-multimap.js +58 -51
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +3 -3
- package/dist/types/common.js +2 -2
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +58 -53
- package/src/data-structures/binary-tree/binary-tree.ts +253 -205
- package/src/data-structures/binary-tree/bst.ts +125 -104
- package/src/data-structures/binary-tree/rb-tree.ts +66 -64
- package/src/data-structures/binary-tree/tree-multimap.ts +62 -56
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +3 -3
|
@@ -14,10 +14,9 @@ import type {
|
|
|
14
14
|
BinaryTreePrintOptions,
|
|
15
15
|
BTNCallback,
|
|
16
16
|
BTNEntry,
|
|
17
|
-
BTNExemplar,
|
|
18
|
-
BTNKeyOrNode,
|
|
19
17
|
DFSOrderPattern,
|
|
20
18
|
EntryCallback,
|
|
19
|
+
KeyOrNodeOrEntry,
|
|
21
20
|
NodeDisplayLayout
|
|
22
21
|
} from '../../types';
|
|
23
22
|
import { FamilyPosition, IterationType } from '../../types';
|
|
@@ -112,15 +111,15 @@ export class BinaryTree<
|
|
|
112
111
|
iterationType = IterationType.ITERATIVE;
|
|
113
112
|
|
|
114
113
|
/**
|
|
115
|
-
* The constructor function initializes a binary tree object with optional
|
|
116
|
-
* @param [
|
|
117
|
-
*
|
|
114
|
+
* The constructor function initializes a binary tree object with optional nodes and options.
|
|
115
|
+
* @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the
|
|
116
|
+
* nodes to be added to the binary tree.
|
|
118
117
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
119
118
|
* configuration options for the binary tree. In this case, it is of type
|
|
120
119
|
* `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
|
|
121
120
|
* required.
|
|
122
121
|
*/
|
|
123
|
-
constructor(
|
|
122
|
+
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<BinaryTreeOptions<K>>) {
|
|
124
123
|
super();
|
|
125
124
|
if (options) {
|
|
126
125
|
const { iterationType, extractor } = options;
|
|
@@ -134,7 +133,7 @@ export class BinaryTree<
|
|
|
134
133
|
|
|
135
134
|
this._size = 0;
|
|
136
135
|
|
|
137
|
-
if (
|
|
136
|
+
if (nodes) this.addMany(nodes);
|
|
138
137
|
}
|
|
139
138
|
|
|
140
139
|
protected _extractor = (key: K) => Number(key);
|
|
@@ -177,30 +176,21 @@ export class BinaryTree<
|
|
|
177
176
|
}
|
|
178
177
|
|
|
179
178
|
/**
|
|
180
|
-
* The function
|
|
181
|
-
* @param
|
|
182
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the class N.
|
|
183
|
-
*/
|
|
184
|
-
isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N {
|
|
185
|
-
return exemplar instanceof BinaryTreeNode;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
190
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
|
|
179
|
+
* The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
|
|
180
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
191
181
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
192
|
-
* `exemplarToNode` function. It represents the value associated with the
|
|
182
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If no value
|
|
193
183
|
* is provided, it will be `undefined`.
|
|
194
184
|
* @returns a value of type N (node), or null, or undefined.
|
|
195
185
|
*/
|
|
196
|
-
exemplarToNode(
|
|
197
|
-
if (
|
|
186
|
+
exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | null | undefined {
|
|
187
|
+
if (keyOrNodeOrEntry === undefined) return;
|
|
198
188
|
|
|
199
189
|
let node: N | null | undefined;
|
|
200
|
-
if (
|
|
190
|
+
if (keyOrNodeOrEntry === null) {
|
|
201
191
|
node = null;
|
|
202
|
-
} else if (this.isEntry(
|
|
203
|
-
const [key, value] =
|
|
192
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
193
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
204
194
|
if (key === undefined) {
|
|
205
195
|
return;
|
|
206
196
|
} else if (key === null) {
|
|
@@ -208,24 +198,112 @@ export class BinaryTree<
|
|
|
208
198
|
} else {
|
|
209
199
|
node = this.createNode(key, value);
|
|
210
200
|
}
|
|
211
|
-
} else if (this.isNode(
|
|
212
|
-
node =
|
|
213
|
-
} else if (this.isNotNodeInstance(
|
|
214
|
-
node = this.createNode(
|
|
201
|
+
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
202
|
+
node = keyOrNodeOrEntry;
|
|
203
|
+
} else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
204
|
+
node = this.createNode(keyOrNodeOrEntry, value);
|
|
215
205
|
} else {
|
|
216
206
|
return;
|
|
217
207
|
}
|
|
218
208
|
return node;
|
|
219
209
|
}
|
|
220
210
|
|
|
211
|
+
/**
|
|
212
|
+
* Time Complexity: O(n)
|
|
213
|
+
* Space Complexity: O(log n)
|
|
214
|
+
*/
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Time Complexity: O(n)
|
|
218
|
+
* Space Complexity: O(log n)
|
|
219
|
+
*
|
|
220
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a valid node
|
|
221
|
+
* key, otherwise it returns the key itself.
|
|
222
|
+
* @param {K | N | null | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`,
|
|
223
|
+
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
|
|
224
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
225
|
+
* type of iteration to be used when searching for a node by key. It has a default value of
|
|
226
|
+
* `IterationType.ITERATIVE`.
|
|
227
|
+
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
|
228
|
+
* itself if it is not a valid node key.
|
|
229
|
+
*/
|
|
230
|
+
ensureNode(
|
|
231
|
+
keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>,
|
|
232
|
+
iterationType = IterationType.ITERATIVE
|
|
233
|
+
): N | null | undefined {
|
|
234
|
+
let res: N | null | undefined;
|
|
235
|
+
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
236
|
+
res = keyOrNodeOrEntry;
|
|
237
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
238
|
+
if (keyOrNodeOrEntry[0] === null) res = null;
|
|
239
|
+
else if (keyOrNodeOrEntry[0] !== undefined) res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
240
|
+
} else {
|
|
241
|
+
if (keyOrNodeOrEntry === null) res = null;
|
|
242
|
+
else if (keyOrNodeOrEntry !== undefined) res = this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
243
|
+
}
|
|
244
|
+
return res;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
|
|
249
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,N>`.
|
|
250
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the class N.
|
|
251
|
+
*/
|
|
252
|
+
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N {
|
|
253
|
+
return keyOrNodeOrEntry instanceof BinaryTreeNode;
|
|
254
|
+
}
|
|
255
|
+
|
|
221
256
|
/**
|
|
222
257
|
* The function checks if a given value is an entry in a binary tree node.
|
|
223
|
-
* @param
|
|
258
|
+
* @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,N> - A generic type representing a node in a binary tree. It has
|
|
224
259
|
* two type parameters V and N, representing the value and node type respectively.
|
|
225
260
|
* @returns a boolean value.
|
|
226
261
|
*/
|
|
227
|
-
isEntry(
|
|
228
|
-
return Array.isArray(
|
|
262
|
+
isEntry(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is BTNEntry<K, V> {
|
|
263
|
+
return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Time complexity: O(n)
|
|
268
|
+
* Space complexity: O(log n)
|
|
269
|
+
*/
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
273
|
+
* BinaryTreeNode and its key is not NaN.
|
|
274
|
+
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
275
|
+
* @returns a boolean value.
|
|
276
|
+
*/
|
|
277
|
+
isRealNode(node: KeyOrNodeOrEntry<K, V, N>): node is N {
|
|
278
|
+
return node instanceof BinaryTreeNode && String(node.key) !== 'NaN';
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
|
|
283
|
+
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
284
|
+
* @returns a boolean value.
|
|
285
|
+
*/
|
|
286
|
+
isNIL(node: KeyOrNodeOrEntry<K, V, N>) {
|
|
287
|
+
return node instanceof BinaryTreeNode && String(node.key) === 'NaN';
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* The function checks if a given node is a real node or null.
|
|
292
|
+
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
293
|
+
* @returns a boolean value.
|
|
294
|
+
*/
|
|
295
|
+
isNodeOrNull(node: KeyOrNodeOrEntry<K, V, N>): node is N | null {
|
|
296
|
+
return this.isRealNode(node) || node === null;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
301
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
302
|
+
* data type.
|
|
303
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
304
|
+
*/
|
|
305
|
+
isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K {
|
|
306
|
+
return !(potentialKey instanceof BinaryTreeNode);
|
|
229
307
|
}
|
|
230
308
|
|
|
231
309
|
/**
|
|
@@ -243,15 +321,15 @@ export class BinaryTree<
|
|
|
243
321
|
* @param {V} [value] - The value to be inserted into the binary tree.
|
|
244
322
|
* @returns The function `add` returns either a node (`N`), `null`, or `undefined`.
|
|
245
323
|
*/
|
|
246
|
-
add(keyOrNodeOrEntry:
|
|
324
|
+
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean {
|
|
247
325
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
248
|
-
if (newNode === undefined) return;
|
|
326
|
+
if (newNode === undefined) return false;
|
|
249
327
|
|
|
250
328
|
// If the tree is empty, directly set the new node as the root node
|
|
251
329
|
if (!this.root) {
|
|
252
330
|
this._root = newNode;
|
|
253
331
|
this._size = 1;
|
|
254
|
-
return
|
|
332
|
+
return true;
|
|
255
333
|
}
|
|
256
334
|
|
|
257
335
|
const queue = new Queue<N>([this.root]);
|
|
@@ -265,7 +343,7 @@ export class BinaryTree<
|
|
|
265
343
|
// Check for duplicate keys when newNode is not null
|
|
266
344
|
if (newNode !== null && cur.key === newNode.key) {
|
|
267
345
|
this._replaceNode(cur, newNode);
|
|
268
|
-
return
|
|
346
|
+
return true; // If duplicate keys are found, no insertion is performed
|
|
269
347
|
}
|
|
270
348
|
|
|
271
349
|
// Record the first possible insertion location found
|
|
@@ -290,10 +368,10 @@ export class BinaryTree<
|
|
|
290
368
|
potentialParent.right = newNode;
|
|
291
369
|
}
|
|
292
370
|
this._size++;
|
|
293
|
-
return
|
|
371
|
+
return true;
|
|
294
372
|
}
|
|
295
373
|
|
|
296
|
-
return
|
|
374
|
+
return false; // If the insertion position cannot be found, return undefined
|
|
297
375
|
}
|
|
298
376
|
|
|
299
377
|
/**
|
|
@@ -306,22 +384,22 @@ export class BinaryTree<
|
|
|
306
384
|
* Time Complexity: O(k log n) - O(k * n)
|
|
307
385
|
* Space Complexity: O(1)
|
|
308
386
|
*
|
|
309
|
-
* The `addMany` function takes in a collection of
|
|
387
|
+
* The `addMany` function takes in a collection of keysOrNodesOrEntries and an optional collection of values, and
|
|
310
388
|
* adds each node with its corresponding value to the data structure.
|
|
311
|
-
* @param
|
|
389
|
+
* @param keysOrNodesOrEntries - An iterable collection of KeyOrNodeOrEntry objects.
|
|
312
390
|
* @param [values] - An optional iterable of values that will be assigned to each node being added.
|
|
313
391
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
314
392
|
*/
|
|
315
|
-
addMany(
|
|
393
|
+
addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>, values?: Iterable<V | undefined>): boolean[] {
|
|
316
394
|
// TODO not sure addMany not be run multi times
|
|
317
|
-
const inserted:
|
|
395
|
+
const inserted: boolean[] = [];
|
|
318
396
|
|
|
319
397
|
let valuesIterator: Iterator<V | undefined> | undefined;
|
|
320
398
|
if (values) {
|
|
321
399
|
valuesIterator = values[Symbol.iterator]();
|
|
322
400
|
}
|
|
323
401
|
|
|
324
|
-
for (const
|
|
402
|
+
for (const keyOrNodeOrEntry of keysOrNodesOrEntries) {
|
|
325
403
|
let value: V | undefined | null = undefined;
|
|
326
404
|
|
|
327
405
|
if (valuesIterator) {
|
|
@@ -331,25 +409,39 @@ export class BinaryTree<
|
|
|
331
409
|
}
|
|
332
410
|
}
|
|
333
411
|
|
|
334
|
-
inserted.push(this.add(
|
|
412
|
+
inserted.push(this.add(keyOrNodeOrEntry, value));
|
|
335
413
|
}
|
|
336
414
|
|
|
337
415
|
return inserted;
|
|
338
416
|
}
|
|
339
417
|
|
|
340
418
|
/**
|
|
341
|
-
* Time Complexity: O(k * n)
|
|
419
|
+
* Time Complexity: O(k * n)
|
|
342
420
|
* Space Complexity: O(1)
|
|
421
|
+
* "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
343
422
|
*/
|
|
344
423
|
|
|
345
|
-
|
|
424
|
+
/**
|
|
425
|
+
* Time Complexity: O(k * n)
|
|
426
|
+
* Space Complexity: O(1)
|
|
427
|
+
*
|
|
428
|
+
* The `refill` function clears the current data and adds new key-value pairs to the data structure.
|
|
429
|
+
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries. These can be of type
|
|
430
|
+
* KeyOrNodeOrEntry<K, V, N>.
|
|
431
|
+
* @param [values] - The `values` parameter is an optional iterable that contains the values to be
|
|
432
|
+
* associated with the keys or nodes or entries in the `keysOrNodesOrEntries` parameter. If provided,
|
|
433
|
+
* the values will be associated with the corresponding keys or nodes or entries in the
|
|
434
|
+
* `keysOrNodesOrEntries` iterable
|
|
435
|
+
*/
|
|
436
|
+
refill(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>, values?: Iterable<V | undefined>): void {
|
|
346
437
|
this.clear();
|
|
347
|
-
this.addMany(
|
|
438
|
+
this.addMany(keysOrNodesOrEntries, values);
|
|
348
439
|
}
|
|
349
440
|
|
|
350
441
|
/**
|
|
351
|
-
* Time Complexity: O(k * n)
|
|
442
|
+
* Time Complexity: O(k * n)
|
|
352
443
|
* Space Complexity: O(1)
|
|
444
|
+
* "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
353
445
|
*/
|
|
354
446
|
|
|
355
447
|
delete<C extends BTNCallback<N, K>>(identifier: K, callback?: C): BinaryTreeDeleteResult<N>[];
|
|
@@ -433,24 +525,24 @@ export class BinaryTree<
|
|
|
433
525
|
* Space Complexity: O(1)
|
|
434
526
|
*
|
|
435
527
|
* The function calculates the depth of a given node in a binary tree.
|
|
436
|
-
* @param {K | N | null | undefined}
|
|
528
|
+
* @param {K | N | null | undefined} dist - The `dist` parameter represents the node in
|
|
437
529
|
* the binary tree whose depth we want to find. It can be of type `K`, `N`, `null`, or
|
|
438
530
|
* `undefined`.
|
|
439
531
|
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
440
532
|
* from which we want to calculate the depth. It can be either a `K` (binary tree node key) or
|
|
441
533
|
* `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
|
|
442
|
-
* @returns the depth of the `
|
|
534
|
+
* @returns the depth of the `dist` relative to the `beginRoot`.
|
|
443
535
|
*/
|
|
444
|
-
getDepth(
|
|
445
|
-
|
|
536
|
+
getDepth(dist: KeyOrNodeOrEntry<K, V, N>, beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root): number {
|
|
537
|
+
dist = this.ensureNode(dist);
|
|
446
538
|
beginRoot = this.ensureNode(beginRoot);
|
|
447
539
|
let depth = 0;
|
|
448
|
-
while (
|
|
449
|
-
if (
|
|
540
|
+
while (dist?.parent) {
|
|
541
|
+
if (dist === beginRoot) {
|
|
450
542
|
return depth;
|
|
451
543
|
}
|
|
452
544
|
depth++;
|
|
453
|
-
|
|
545
|
+
dist = dist.parent;
|
|
454
546
|
}
|
|
455
547
|
return depth;
|
|
456
548
|
}
|
|
@@ -474,7 +566,7 @@ export class BinaryTree<
|
|
|
474
566
|
* values:
|
|
475
567
|
* @returns the height of the binary tree.
|
|
476
568
|
*/
|
|
477
|
-
getHeight(beginRoot:
|
|
569
|
+
getHeight(beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root, iterationType = this.iterationType): number {
|
|
478
570
|
beginRoot = this.ensureNode(beginRoot);
|
|
479
571
|
if (!beginRoot) return -1;
|
|
480
572
|
|
|
@@ -523,7 +615,7 @@ export class BinaryTree<
|
|
|
523
615
|
* to calculate the minimum height of a binary tree. It can have two possible values:
|
|
524
616
|
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
|
|
525
617
|
*/
|
|
526
|
-
getMinHeight(beginRoot:
|
|
618
|
+
getMinHeight(beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root, iterationType = this.iterationType): number {
|
|
527
619
|
beginRoot = this.ensureNode(beginRoot);
|
|
528
620
|
if (!beginRoot) return -1;
|
|
529
621
|
|
|
@@ -583,7 +675,7 @@ export class BinaryTree<
|
|
|
583
675
|
* value of a binary tree node), `N` (a node of a binary tree), `null`, or `undefined`. If
|
|
584
676
|
* @returns a boolean value.
|
|
585
677
|
*/
|
|
586
|
-
isPerfectlyBalanced(beginRoot:
|
|
678
|
+
isPerfectlyBalanced(beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root): boolean {
|
|
587
679
|
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
588
680
|
}
|
|
589
681
|
|
|
@@ -596,7 +688,7 @@ export class BinaryTree<
|
|
|
596
688
|
identifier: K,
|
|
597
689
|
callback?: C,
|
|
598
690
|
onlyOne?: boolean,
|
|
599
|
-
beginRoot?:
|
|
691
|
+
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
600
692
|
iterationType?: IterationType
|
|
601
693
|
): N[];
|
|
602
694
|
|
|
@@ -604,7 +696,7 @@ export class BinaryTree<
|
|
|
604
696
|
identifier: N | null | undefined,
|
|
605
697
|
callback?: C,
|
|
606
698
|
onlyOne?: boolean,
|
|
607
|
-
beginRoot?:
|
|
699
|
+
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
608
700
|
iterationType?: IterationType
|
|
609
701
|
): N[];
|
|
610
702
|
|
|
@@ -612,7 +704,7 @@ export class BinaryTree<
|
|
|
612
704
|
identifier: ReturnType<C>,
|
|
613
705
|
callback: C,
|
|
614
706
|
onlyOne?: boolean,
|
|
615
|
-
beginRoot?:
|
|
707
|
+
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
616
708
|
iterationType?: IterationType
|
|
617
709
|
): N[];
|
|
618
710
|
|
|
@@ -645,7 +737,7 @@ export class BinaryTree<
|
|
|
645
737
|
identifier: ReturnType<C> | null | undefined,
|
|
646
738
|
callback: C = this._defaultOneParamCallback as C,
|
|
647
739
|
onlyOne = false,
|
|
648
|
-
beginRoot:
|
|
740
|
+
beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
649
741
|
iterationType = this.iterationType
|
|
650
742
|
): N[] {
|
|
651
743
|
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
@@ -693,26 +785,27 @@ export class BinaryTree<
|
|
|
693
785
|
has<C extends BTNCallback<N, K>>(
|
|
694
786
|
identifier: K,
|
|
695
787
|
callback?: C,
|
|
696
|
-
beginRoot?:
|
|
788
|
+
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
697
789
|
iterationType?: IterationType
|
|
698
790
|
): boolean;
|
|
699
791
|
|
|
700
792
|
has<C extends BTNCallback<N, N>>(
|
|
701
793
|
identifier: N | null | undefined,
|
|
702
794
|
callback?: C,
|
|
703
|
-
beginRoot?:
|
|
795
|
+
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
704
796
|
iterationType?: IterationType
|
|
705
797
|
): boolean;
|
|
706
798
|
|
|
707
799
|
has<C extends BTNCallback<N>>(
|
|
708
800
|
identifier: ReturnType<C> | null | undefined,
|
|
709
801
|
callback: C,
|
|
710
|
-
beginRoot?:
|
|
802
|
+
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
711
803
|
iterationType?: IterationType
|
|
712
804
|
): boolean;
|
|
713
805
|
|
|
714
806
|
/**
|
|
715
807
|
* Time Complexity: O(n)
|
|
808
|
+
* Space Complexity: O(log n).
|
|
716
809
|
*
|
|
717
810
|
* The function checks if a Binary Tree Node with a specific identifier exists in the tree.
|
|
718
811
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
@@ -734,7 +827,7 @@ export class BinaryTree<
|
|
|
734
827
|
has<C extends BTNCallback<N>>(
|
|
735
828
|
identifier: ReturnType<C> | null | undefined,
|
|
736
829
|
callback: C = this._defaultOneParamCallback as C,
|
|
737
|
-
beginRoot:
|
|
830
|
+
beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
738
831
|
iterationType = this.iterationType
|
|
739
832
|
): boolean {
|
|
740
833
|
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
@@ -751,21 +844,21 @@ export class BinaryTree<
|
|
|
751
844
|
getNode<C extends BTNCallback<N, K>>(
|
|
752
845
|
identifier: K,
|
|
753
846
|
callback?: C,
|
|
754
|
-
beginRoot?:
|
|
847
|
+
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
755
848
|
iterationType?: IterationType
|
|
756
849
|
): N | null | undefined;
|
|
757
850
|
|
|
758
851
|
getNode<C extends BTNCallback<N, N>>(
|
|
759
852
|
identifier: N | null | undefined,
|
|
760
853
|
callback?: C,
|
|
761
|
-
beginRoot?:
|
|
854
|
+
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
762
855
|
iterationType?: IterationType
|
|
763
856
|
): N | null | undefined;
|
|
764
857
|
|
|
765
858
|
getNode<C extends BTNCallback<N>>(
|
|
766
859
|
identifier: ReturnType<C>,
|
|
767
860
|
callback: C,
|
|
768
|
-
beginRoot?:
|
|
861
|
+
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
769
862
|
iterationType?: IterationType
|
|
770
863
|
): N | null | undefined;
|
|
771
864
|
|
|
@@ -793,7 +886,7 @@ export class BinaryTree<
|
|
|
793
886
|
getNode<C extends BTNCallback<N>>(
|
|
794
887
|
identifier: ReturnType<C> | null | undefined,
|
|
795
888
|
callback: C = this._defaultOneParamCallback as C,
|
|
796
|
-
beginRoot:
|
|
889
|
+
beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
797
890
|
iterationType = this.iterationType
|
|
798
891
|
): N | null | undefined {
|
|
799
892
|
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
@@ -846,44 +939,24 @@ export class BinaryTree<
|
|
|
846
939
|
}
|
|
847
940
|
}
|
|
848
941
|
|
|
849
|
-
/**
|
|
850
|
-
* Time Complexity: O(n)
|
|
851
|
-
* Space Complexity: O(log n)
|
|
852
|
-
*/
|
|
853
|
-
|
|
854
|
-
/**
|
|
855
|
-
* The function `ensureNode` returns the node corresponding to the given key if it is a valid node
|
|
856
|
-
* key, otherwise it returns the key itself.
|
|
857
|
-
* @param {K | N | null | undefined} key - The `key` parameter can be of type `K`, `N`,
|
|
858
|
-
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
|
|
859
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
860
|
-
* type of iteration to be used when searching for a node by key. It has a default value of
|
|
861
|
-
* `IterationType.ITERATIVE`.
|
|
862
|
-
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
|
863
|
-
* itself if it is not a valid node key.
|
|
864
|
-
*/
|
|
865
|
-
ensureNode(key: BTNKeyOrNode<K, N>, iterationType = IterationType.ITERATIVE): N | null | undefined {
|
|
866
|
-
return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
867
|
-
}
|
|
868
|
-
|
|
869
942
|
get<C extends BTNCallback<N, K>>(
|
|
870
943
|
identifier: K,
|
|
871
944
|
callback?: C,
|
|
872
|
-
beginRoot?:
|
|
945
|
+
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
873
946
|
iterationType?: IterationType
|
|
874
947
|
): V | undefined;
|
|
875
948
|
|
|
876
949
|
get<C extends BTNCallback<N, N>>(
|
|
877
950
|
identifier: N | null | undefined,
|
|
878
951
|
callback?: C,
|
|
879
|
-
beginRoot?:
|
|
952
|
+
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
880
953
|
iterationType?: IterationType
|
|
881
954
|
): V | undefined;
|
|
882
955
|
|
|
883
956
|
get<C extends BTNCallback<N>>(
|
|
884
957
|
identifier: ReturnType<C>,
|
|
885
958
|
callback: C,
|
|
886
|
-
beginRoot?:
|
|
959
|
+
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
887
960
|
iterationType?: IterationType
|
|
888
961
|
): V | undefined;
|
|
889
962
|
|
|
@@ -912,7 +985,7 @@ export class BinaryTree<
|
|
|
912
985
|
get<C extends BTNCallback<N>>(
|
|
913
986
|
identifier: ReturnType<C> | null | undefined,
|
|
914
987
|
callback: C = this._defaultOneParamCallback as C,
|
|
915
|
-
beginRoot:
|
|
988
|
+
beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
916
989
|
iterationType = this.iterationType
|
|
917
990
|
): V | undefined {
|
|
918
991
|
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
@@ -922,11 +995,14 @@ export class BinaryTree<
|
|
|
922
995
|
}
|
|
923
996
|
|
|
924
997
|
/**
|
|
925
|
-
* Time Complexity: O(
|
|
926
|
-
* Space Complexity: O(
|
|
998
|
+
* Time Complexity: O(1)
|
|
999
|
+
* Space Complexity: O(1)
|
|
927
1000
|
*/
|
|
928
1001
|
|
|
929
1002
|
/**
|
|
1003
|
+
* Time Complexity: O(1)
|
|
1004
|
+
* Space Complexity: O(1)
|
|
1005
|
+
*
|
|
930
1006
|
* Clear the binary tree, removing all nodes.
|
|
931
1007
|
*/
|
|
932
1008
|
clear() {
|
|
@@ -935,6 +1011,14 @@ export class BinaryTree<
|
|
|
935
1011
|
}
|
|
936
1012
|
|
|
937
1013
|
/**
|
|
1014
|
+
* Time Complexity: O(1)
|
|
1015
|
+
* Space Complexity: O(1)
|
|
1016
|
+
*/
|
|
1017
|
+
|
|
1018
|
+
/**
|
|
1019
|
+
* Time Complexity: O(1)
|
|
1020
|
+
* Space Complexity: O(1)
|
|
1021
|
+
*
|
|
938
1022
|
* Check if the binary tree is empty.
|
|
939
1023
|
* @returns {boolean} - True if the binary tree is empty, false otherwise.
|
|
940
1024
|
*/
|
|
@@ -956,7 +1040,7 @@ export class BinaryTree<
|
|
|
956
1040
|
* reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
|
|
957
1041
|
* @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
|
|
958
1042
|
*/
|
|
959
|
-
getPathToRoot(beginRoot:
|
|
1043
|
+
getPathToRoot(beginRoot: KeyOrNodeOrEntry<K, V, N>, isReverse = true): N[] {
|
|
960
1044
|
// TODO to support get path through passing key
|
|
961
1045
|
const result: N[] = [];
|
|
962
1046
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -975,7 +1059,7 @@ export class BinaryTree<
|
|
|
975
1059
|
|
|
976
1060
|
/**
|
|
977
1061
|
* Time Complexity: O(log n)
|
|
978
|
-
* Space Complexity: O(
|
|
1062
|
+
* Space Complexity: O(1)
|
|
979
1063
|
*/
|
|
980
1064
|
|
|
981
1065
|
/**
|
|
@@ -992,7 +1076,10 @@ export class BinaryTree<
|
|
|
992
1076
|
* @returns The function `getLeftMost` returns the leftmost node (`N`) in the binary tree. If there
|
|
993
1077
|
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
994
1078
|
*/
|
|
995
|
-
getLeftMost(
|
|
1079
|
+
getLeftMost(
|
|
1080
|
+
beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
1081
|
+
iterationType = this.iterationType
|
|
1082
|
+
): N | null | undefined {
|
|
996
1083
|
beginRoot = this.ensureNode(beginRoot);
|
|
997
1084
|
|
|
998
1085
|
if (!beginRoot) return beginRoot;
|
|
@@ -1035,7 +1122,10 @@ export class BinaryTree<
|
|
|
1035
1122
|
* @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If there
|
|
1036
1123
|
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
1037
1124
|
*/
|
|
1038
|
-
getRightMost(
|
|
1125
|
+
getRightMost(
|
|
1126
|
+
beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
1127
|
+
iterationType = this.iterationType
|
|
1128
|
+
): N | null | undefined {
|
|
1039
1129
|
// TODO support get right most by passing key in
|
|
1040
1130
|
beginRoot = this.ensureNode(beginRoot);
|
|
1041
1131
|
if (!beginRoot) return beginRoot;
|
|
@@ -1075,7 +1165,7 @@ export class BinaryTree<
|
|
|
1075
1165
|
* possible values:
|
|
1076
1166
|
* @returns a boolean value.
|
|
1077
1167
|
*/
|
|
1078
|
-
|
|
1168
|
+
isBST(beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root, iterationType = this.iterationType): boolean {
|
|
1079
1169
|
// TODO there is a bug
|
|
1080
1170
|
beginRoot = this.ensureNode(beginRoot);
|
|
1081
1171
|
if (!beginRoot) return true;
|
|
@@ -1088,69 +1178,56 @@ export class BinaryTree<
|
|
|
1088
1178
|
return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
|
|
1089
1179
|
};
|
|
1090
1180
|
|
|
1091
|
-
|
|
1181
|
+
const isStandardBST = dfs(beginRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
1182
|
+
const isInverseBST = dfs(beginRoot, Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
|
|
1183
|
+
return isStandardBST || isInverseBST;
|
|
1092
1184
|
} else {
|
|
1093
|
-
const
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
curr
|
|
1185
|
+
const checkBST = (checkMax = false) => {
|
|
1186
|
+
const stack = [];
|
|
1187
|
+
let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
|
|
1188
|
+
// @ts-ignore
|
|
1189
|
+
let curr: N | null | undefined = beginRoot;
|
|
1190
|
+
while (curr || stack.length > 0) {
|
|
1191
|
+
while (curr) {
|
|
1192
|
+
stack.push(curr);
|
|
1193
|
+
curr = curr.left;
|
|
1194
|
+
}
|
|
1195
|
+
curr = stack.pop()!;
|
|
1196
|
+
const numKey = this.extractor(curr.key);
|
|
1197
|
+
if (!curr || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey)) return false;
|
|
1198
|
+
prev = numKey;
|
|
1199
|
+
curr = curr.right;
|
|
1100
1200
|
}
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
}
|
|
1107
|
-
return true;
|
|
1201
|
+
return true;
|
|
1202
|
+
};
|
|
1203
|
+
const isStandardBST = checkBST(false),
|
|
1204
|
+
isInverseBST = checkBST(true);
|
|
1205
|
+
return isStandardBST || isInverseBST;
|
|
1108
1206
|
}
|
|
1109
1207
|
}
|
|
1110
1208
|
|
|
1111
1209
|
/**
|
|
1112
|
-
* Time
|
|
1113
|
-
* Space
|
|
1114
|
-
*/
|
|
1115
|
-
|
|
1116
|
-
/**
|
|
1117
|
-
* Time Complexity: O(n)
|
|
1118
|
-
* Space Complexity: O(1)
|
|
1119
|
-
*
|
|
1120
|
-
* The function checks if a binary tree is a binary search tree.
|
|
1121
|
-
* @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
|
|
1122
|
-
* be used when checking if the binary tree is a binary search tree (BST). It is an optional
|
|
1123
|
-
* parameter with a default value of "this.iterationType". The value of "this.iterationType" is
|
|
1124
|
-
* expected to be
|
|
1125
|
-
* @returns a boolean value.
|
|
1126
|
-
*/
|
|
1127
|
-
isBST(iterationType = this.iterationType): boolean {
|
|
1128
|
-
if (this.root === null) return true;
|
|
1129
|
-
return this.isSubtreeBST(this.root, iterationType);
|
|
1130
|
-
}
|
|
1131
|
-
|
|
1132
|
-
/**
|
|
1133
|
-
* Time Complexity: O(n)
|
|
1134
|
-
* Space Complexity: O(1)
|
|
1210
|
+
* Time complexity: O(n)
|
|
1211
|
+
* Space complexity: O(log n)
|
|
1135
1212
|
*/
|
|
1136
1213
|
|
|
1137
1214
|
subTreeTraverse<C extends BTNCallback<N>>(
|
|
1138
1215
|
callback?: C,
|
|
1139
|
-
beginRoot?:
|
|
1216
|
+
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
1140
1217
|
iterationType?: IterationType,
|
|
1141
1218
|
includeNull?: false
|
|
1142
1219
|
): ReturnType<C>[];
|
|
1143
1220
|
|
|
1144
1221
|
subTreeTraverse<C extends BTNCallback<N>>(
|
|
1145
1222
|
callback?: C,
|
|
1146
|
-
beginRoot?:
|
|
1223
|
+
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
1147
1224
|
iterationType?: IterationType,
|
|
1148
1225
|
includeNull?: undefined
|
|
1149
1226
|
): ReturnType<C>[];
|
|
1150
1227
|
|
|
1151
1228
|
subTreeTraverse<C extends BTNCallback<N | null | undefined>>(
|
|
1152
1229
|
callback?: C,
|
|
1153
|
-
beginRoot?:
|
|
1230
|
+
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
1154
1231
|
iterationType?: IterationType,
|
|
1155
1232
|
includeNull?: true
|
|
1156
1233
|
): ReturnType<C>[];
|
|
@@ -1174,12 +1251,12 @@ export class BinaryTree<
|
|
|
1174
1251
|
* whether to include null values in the traversal. If `includeNull` is set to `true`, the
|
|
1175
1252
|
* traversal will include null values, otherwise it will skip them.
|
|
1176
1253
|
* @returns The function `subTreeTraverse` returns an array of values that are the result of invoking
|
|
1177
|
-
* the `callback` function on each node in the subtree. The type of the array
|
|
1254
|
+
* the `callback` function on each node in the subtree. The type of the array nodes is determined
|
|
1178
1255
|
* by the return type of the `callback` function.
|
|
1179
1256
|
*/
|
|
1180
1257
|
subTreeTraverse<C extends BTNCallback<N | null | undefined>>(
|
|
1181
1258
|
callback: C = this._defaultOneParamCallback as C,
|
|
1182
|
-
beginRoot:
|
|
1259
|
+
beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
1183
1260
|
iterationType = this.iterationType,
|
|
1184
1261
|
includeNull = false
|
|
1185
1262
|
): ReturnType<C>[] {
|
|
@@ -1223,53 +1300,10 @@ export class BinaryTree<
|
|
|
1223
1300
|
return ans;
|
|
1224
1301
|
}
|
|
1225
1302
|
|
|
1226
|
-
/**
|
|
1227
|
-
* Time complexity: O(n)
|
|
1228
|
-
* Space complexity: O(log n)
|
|
1229
|
-
*/
|
|
1230
|
-
|
|
1231
|
-
/**
|
|
1232
|
-
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
1233
|
-
* BinaryTreeNode and its key is not NaN.
|
|
1234
|
-
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
1235
|
-
* @returns a boolean value.
|
|
1236
|
-
*/
|
|
1237
|
-
isRealNode(node: BTNExemplar<K, V, N>): node is N {
|
|
1238
|
-
return node instanceof BinaryTreeNode && String(node.key) !== 'NaN';
|
|
1239
|
-
}
|
|
1240
|
-
|
|
1241
|
-
/**
|
|
1242
|
-
* The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
|
|
1243
|
-
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
1244
|
-
* @returns a boolean value.
|
|
1245
|
-
*/
|
|
1246
|
-
isNIL(node: BTNExemplar<K, V, N>) {
|
|
1247
|
-
return node instanceof BinaryTreeNode && String(node.key) === 'NaN';
|
|
1248
|
-
}
|
|
1249
|
-
|
|
1250
|
-
/**
|
|
1251
|
-
* The function checks if a given node is a real node or null.
|
|
1252
|
-
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
1253
|
-
* @returns a boolean value.
|
|
1254
|
-
*/
|
|
1255
|
-
isNodeOrNull(node: BTNExemplar<K, V, N>): node is N | null {
|
|
1256
|
-
return this.isRealNode(node) || node === null;
|
|
1257
|
-
}
|
|
1258
|
-
|
|
1259
|
-
/**
|
|
1260
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
1261
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
1262
|
-
* data type.
|
|
1263
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
1264
|
-
*/
|
|
1265
|
-
isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
|
|
1266
|
-
return !(potentialKey instanceof BinaryTreeNode);
|
|
1267
|
-
}
|
|
1268
|
-
|
|
1269
1303
|
dfs<C extends BTNCallback<N>>(
|
|
1270
1304
|
callback?: C,
|
|
1271
1305
|
pattern?: DFSOrderPattern,
|
|
1272
|
-
beginRoot?:
|
|
1306
|
+
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
1273
1307
|
iterationType?: IterationType,
|
|
1274
1308
|
includeNull?: false
|
|
1275
1309
|
): ReturnType<C>[];
|
|
@@ -1277,7 +1311,7 @@ export class BinaryTree<
|
|
|
1277
1311
|
dfs<C extends BTNCallback<N>>(
|
|
1278
1312
|
callback?: C,
|
|
1279
1313
|
pattern?: DFSOrderPattern,
|
|
1280
|
-
beginRoot?:
|
|
1314
|
+
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
1281
1315
|
iterationType?: IterationType,
|
|
1282
1316
|
includeNull?: undefined
|
|
1283
1317
|
): ReturnType<C>[];
|
|
@@ -1285,7 +1319,7 @@ export class BinaryTree<
|
|
|
1285
1319
|
dfs<C extends BTNCallback<N | null | undefined>>(
|
|
1286
1320
|
callback?: C,
|
|
1287
1321
|
pattern?: DFSOrderPattern,
|
|
1288
|
-
beginRoot?:
|
|
1322
|
+
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
1289
1323
|
iterationType?: IterationType,
|
|
1290
1324
|
includeNull?: true
|
|
1291
1325
|
): ReturnType<C>[];
|
|
@@ -1316,7 +1350,7 @@ export class BinaryTree<
|
|
|
1316
1350
|
dfs<C extends BTNCallback<N | null | undefined>>(
|
|
1317
1351
|
callback: C = this._defaultOneParamCallback as C,
|
|
1318
1352
|
pattern: DFSOrderPattern = 'in',
|
|
1319
|
-
beginRoot:
|
|
1353
|
+
beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
1320
1354
|
iterationType: IterationType = IterationType.ITERATIVE,
|
|
1321
1355
|
includeNull = false
|
|
1322
1356
|
): ReturnType<C>[] {
|
|
@@ -1415,21 +1449,21 @@ export class BinaryTree<
|
|
|
1415
1449
|
|
|
1416
1450
|
bfs<C extends BTNCallback<N>>(
|
|
1417
1451
|
callback?: C,
|
|
1418
|
-
beginRoot?:
|
|
1452
|
+
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
1419
1453
|
iterationType?: IterationType,
|
|
1420
1454
|
includeNull?: false
|
|
1421
1455
|
): ReturnType<C>[];
|
|
1422
1456
|
|
|
1423
1457
|
bfs<C extends BTNCallback<N>>(
|
|
1424
1458
|
callback?: C,
|
|
1425
|
-
beginRoot?:
|
|
1459
|
+
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
1426
1460
|
iterationType?: IterationType,
|
|
1427
1461
|
includeNull?: undefined
|
|
1428
1462
|
): ReturnType<C>[];
|
|
1429
1463
|
|
|
1430
1464
|
bfs<C extends BTNCallback<N | null | undefined>>(
|
|
1431
1465
|
callback?: C,
|
|
1432
|
-
beginRoot?:
|
|
1466
|
+
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
1433
1467
|
iterationType?: IterationType,
|
|
1434
1468
|
includeNull?: true
|
|
1435
1469
|
): ReturnType<C>[];
|
|
@@ -1457,7 +1491,7 @@ export class BinaryTree<
|
|
|
1457
1491
|
*/
|
|
1458
1492
|
bfs<C extends BTNCallback<N | null | undefined>>(
|
|
1459
1493
|
callback: C = this._defaultOneParamCallback as C,
|
|
1460
|
-
beginRoot:
|
|
1494
|
+
beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
1461
1495
|
iterationType = this.iterationType,
|
|
1462
1496
|
includeNull = false
|
|
1463
1497
|
): ReturnType<C>[] {
|
|
@@ -1516,21 +1550,21 @@ export class BinaryTree<
|
|
|
1516
1550
|
|
|
1517
1551
|
listLevels<C extends BTNCallback<N>>(
|
|
1518
1552
|
callback?: C,
|
|
1519
|
-
beginRoot?:
|
|
1553
|
+
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
1520
1554
|
iterationType?: IterationType,
|
|
1521
1555
|
includeNull?: false
|
|
1522
1556
|
): ReturnType<C>[][];
|
|
1523
1557
|
|
|
1524
1558
|
listLevels<C extends BTNCallback<N>>(
|
|
1525
1559
|
callback?: C,
|
|
1526
|
-
beginRoot?:
|
|
1560
|
+
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
1527
1561
|
iterationType?: IterationType,
|
|
1528
1562
|
includeNull?: undefined
|
|
1529
1563
|
): ReturnType<C>[][];
|
|
1530
1564
|
|
|
1531
1565
|
listLevels<C extends BTNCallback<N | null | undefined>>(
|
|
1532
1566
|
callback?: C,
|
|
1533
|
-
beginRoot?:
|
|
1567
|
+
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
1534
1568
|
iterationType?: IterationType,
|
|
1535
1569
|
includeNull?: true
|
|
1536
1570
|
): ReturnType<C>[][];
|
|
@@ -1558,7 +1592,7 @@ export class BinaryTree<
|
|
|
1558
1592
|
*/
|
|
1559
1593
|
listLevels<C extends BTNCallback<N | null | undefined>>(
|
|
1560
1594
|
callback: C = this._defaultOneParamCallback as C,
|
|
1561
|
-
beginRoot:
|
|
1595
|
+
beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
1562
1596
|
iterationType = this.iterationType,
|
|
1563
1597
|
includeNull = false
|
|
1564
1598
|
): ReturnType<C>[][] {
|
|
@@ -1655,7 +1689,13 @@ export class BinaryTree<
|
|
|
1655
1689
|
|
|
1656
1690
|
/**
|
|
1657
1691
|
* Time complexity: O(n)
|
|
1658
|
-
* Space complexity: O(
|
|
1692
|
+
* Space complexity: O(n)
|
|
1693
|
+
*/
|
|
1694
|
+
|
|
1695
|
+
/**
|
|
1696
|
+
* Time complexity: O(n)
|
|
1697
|
+
* Space complexity: O(n)
|
|
1698
|
+
*
|
|
1659
1699
|
* The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
|
|
1660
1700
|
* algorithm.
|
|
1661
1701
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
@@ -1668,13 +1708,13 @@ export class BinaryTree<
|
|
|
1668
1708
|
* for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
|
|
1669
1709
|
* the root of the tree. If no value is provided, the default value is the root of the tree.
|
|
1670
1710
|
* @returns The function `morris` returns an array of values that are the result of invoking the
|
|
1671
|
-
* `callback` function on each node in the binary tree. The type of the array
|
|
1711
|
+
* `callback` function on each node in the binary tree. The type of the array nodes is determined
|
|
1672
1712
|
* by the return type of the `callback` function.
|
|
1673
1713
|
*/
|
|
1674
1714
|
morris<C extends BTNCallback<N>>(
|
|
1675
1715
|
callback: C = this._defaultOneParamCallback as C,
|
|
1676
1716
|
pattern: DFSOrderPattern = 'in',
|
|
1677
|
-
beginRoot:
|
|
1717
|
+
beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root
|
|
1678
1718
|
): ReturnType<C>[] {
|
|
1679
1719
|
beginRoot = this.ensureNode(beginRoot);
|
|
1680
1720
|
if (beginRoot === null) return [];
|
|
@@ -1785,8 +1825,8 @@ export class BinaryTree<
|
|
|
1785
1825
|
* Time Complexity: O(n)
|
|
1786
1826
|
* Space Complexity: O(n)
|
|
1787
1827
|
*
|
|
1788
|
-
* The `filter` function creates a new tree by iterating over the
|
|
1789
|
-
* adding only the
|
|
1828
|
+
* The `filter` function creates a new tree by iterating over the nodes of the current tree and
|
|
1829
|
+
* adding only the nodes that satisfy the given predicate function.
|
|
1790
1830
|
* @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
|
|
1791
1831
|
* `key`, and `index`. It should return a boolean value indicating whether the pair should be
|
|
1792
1832
|
* included in the filtered tree or not.
|
|
@@ -1847,13 +1887,21 @@ export class BinaryTree<
|
|
|
1847
1887
|
//
|
|
1848
1888
|
|
|
1849
1889
|
/**
|
|
1890
|
+
* Time Complexity: O(n)
|
|
1891
|
+
* Space Complexity: O(n)
|
|
1892
|
+
*/
|
|
1893
|
+
|
|
1894
|
+
/**
|
|
1895
|
+
* Time Complexity: O(n)
|
|
1896
|
+
* Space Complexity: O(n)
|
|
1897
|
+
*
|
|
1850
1898
|
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
|
1851
1899
|
* @param {K | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `K | N | null |
|
|
1852
1900
|
* undefined`. It represents the root node of a binary tree. The root node can have one of the
|
|
1853
1901
|
* following types:
|
|
1854
1902
|
* @param {BinaryTreePrintOptions} [options={ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false}] - Options object that controls printing behavior. You can specify whether to display undefined, null, or sentinel nodes.
|
|
1855
1903
|
*/
|
|
1856
|
-
print(beginRoot:
|
|
1904
|
+
print(beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root, options?: BinaryTreePrintOptions): void {
|
|
1857
1905
|
const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
|
|
1858
1906
|
beginRoot = this.ensureNode(beginRoot);
|
|
1859
1907
|
if (!beginRoot) return;
|
|
@@ -1985,7 +2033,7 @@ export class BinaryTree<
|
|
|
1985
2033
|
* @param {N} destNode - The destination node to swap.
|
|
1986
2034
|
* @returns {N} - The destination node after the swap.
|
|
1987
2035
|
*/
|
|
1988
|
-
protected _swapProperties(srcNode:
|
|
2036
|
+
protected _swapProperties(srcNode: KeyOrNodeOrEntry<K, V, N>, destNode: KeyOrNodeOrEntry<K, V, N>): N | undefined {
|
|
1989
2037
|
srcNode = this.ensureNode(srcNode);
|
|
1990
2038
|
destNode = this.ensureNode(destNode);
|
|
1991
2039
|
|