min-heap-typed 1.51.8 → 1.52.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/base/index.d.ts +2 -1
- package/dist/data-structures/base/index.js +2 -1
- package/dist/data-structures/base/iterable-element-base.d.ts +171 -0
- package/dist/data-structures/base/iterable-element-base.js +225 -0
- package/dist/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
- package/dist/data-structures/base/{iterable-base.js → iterable-entry-base.js} +12 -189
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +106 -68
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
- package/dist/data-structures/binary-tree/avl-tree.d.ts +82 -62
- package/dist/data-structures/binary-tree/avl-tree.js +78 -59
- package/dist/data-structures/binary-tree/binary-tree.d.ts +318 -226
- package/dist/data-structures/binary-tree/binary-tree.js +475 -363
- package/dist/data-structures/binary-tree/bst.d.ts +192 -202
- package/dist/data-structures/binary-tree/bst.js +207 -249
- package/dist/data-structures/binary-tree/rb-tree.d.ts +73 -74
- package/dist/data-structures/binary-tree/rb-tree.js +107 -98
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +92 -75
- 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 +33 -40
- package/dist/data-structures/hash/hash-map.js +40 -55
- package/dist/data-structures/heap/heap.d.ts +43 -114
- package/dist/data-structures/heap/heap.js +59 -127
- package/dist/data-structures/heap/max-heap.d.ts +50 -4
- package/dist/data-structures/heap/max-heap.js +76 -10
- package/dist/data-structures/heap/min-heap.d.ts +51 -5
- package/dist/data-structures/heap/min-heap.js +68 -11
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
- package/dist/data-structures/linked-list/doubly-linked-list.js +26 -28
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +22 -25
- package/dist/data-structures/linked-list/singly-linked-list.js +29 -26
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
- package/dist/data-structures/priority-queue/max-priority-queue.js +79 -10
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
- package/dist/data-structures/priority-queue/min-priority-queue.js +71 -11
- package/dist/data-structures/priority-queue/priority-queue.d.ts +50 -4
- package/dist/data-structures/priority-queue/priority-queue.js +70 -1
- package/dist/data-structures/queue/deque.d.ts +21 -20
- package/dist/data-structures/queue/deque.js +29 -23
- package/dist/data-structures/queue/queue.d.ts +8 -28
- package/dist/data-structures/queue/queue.js +15 -31
- package/dist/data-structures/stack/stack.d.ts +17 -22
- package/dist/data-structures/stack/stack.js +25 -24
- package/dist/data-structures/trie/trie.d.ts +19 -14
- package/dist/data-structures/trie/trie.js +27 -16
- package/dist/interfaces/binary-tree.d.ts +7 -7
- package/dist/types/common.d.ts +1 -2
- package/dist/types/data-structures/base/base.d.ts +5 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +5 -5
- package/dist/types/data-structures/binary-tree/bst.d.ts +4 -5
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
- package/dist/types/data-structures/heap/heap.d.ts +3 -2
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/types/data-structures/queue/deque.d.ts +3 -2
- package/dist/types/data-structures/queue/queue.d.ts +2 -1
- package/dist/types/data-structures/stack/stack.d.ts +2 -1
- package/dist/types/data-structures/trie/trie.d.ts +3 -2
- package/dist/utils/utils.js +3 -5
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +2 -1
- package/src/data-structures/base/iterable-element-base.ts +250 -0
- package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +22 -213
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +144 -95
- package/src/data-structures/binary-tree/avl-tree.ts +96 -69
- package/src/data-structures/binary-tree/binary-tree.ts +535 -403
- package/src/data-structures/binary-tree/bst.ts +247 -277
- package/src/data-structures/binary-tree/rb-tree.ts +123 -103
- package/src/data-structures/binary-tree/tree-multi-map.ts +127 -102
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/hash/hash-map.ts +46 -53
- package/src/data-structures/heap/heap.ts +71 -152
- package/src/data-structures/heap/max-heap.ts +88 -13
- package/src/data-structures/heap/min-heap.ts +78 -15
- package/src/data-structures/linked-list/doubly-linked-list.ts +32 -32
- package/src/data-structures/linked-list/singly-linked-list.ts +37 -29
- package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
- package/src/data-structures/priority-queue/priority-queue.ts +81 -4
- package/src/data-structures/queue/deque.ts +37 -26
- package/src/data-structures/queue/queue.ts +23 -36
- package/src/data-structures/stack/stack.ts +31 -26
- package/src/data-structures/trie/trie.ts +35 -20
- package/src/interfaces/binary-tree.ts +9 -9
- package/src/types/common.ts +1 -2
- package/src/types/data-structures/base/base.ts +14 -6
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -4
- package/src/types/data-structures/binary-tree/avl-tree.ts +3 -4
- package/src/types/data-structures/binary-tree/binary-tree.ts +6 -6
- package/src/types/data-structures/binary-tree/bst.ts +4 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -4
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -4
- package/src/types/data-structures/heap/heap.ts +4 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
- package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/types/data-structures/queue/deque.ts +3 -1
- package/src/types/data-structures/queue/queue.ts +3 -1
- package/src/types/data-structures/stack/stack.ts +3 -1
- package/src/types/data-structures/trie/trie.ts +3 -1
- package/src/utils/utils.ts +3 -3
|
@@ -14,7 +14,6 @@ import type {
|
|
|
14
14
|
BinaryTreePrintOptions,
|
|
15
15
|
BTNCallback,
|
|
16
16
|
BTNEntry,
|
|
17
|
-
Comparable,
|
|
18
17
|
DFSOrderPattern,
|
|
19
18
|
EntryCallback,
|
|
20
19
|
FamilyPosition,
|
|
@@ -33,7 +32,7 @@ import { IterableEntryBase } from '../base';
|
|
|
33
32
|
* @template NODE - The type of the family relationship in the binary tree.
|
|
34
33
|
*/
|
|
35
34
|
export class BinaryTreeNode<
|
|
36
|
-
K
|
|
35
|
+
K = any,
|
|
37
36
|
V = any,
|
|
38
37
|
NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>
|
|
39
38
|
> {
|
|
@@ -131,34 +130,38 @@ export class BinaryTreeNode<
|
|
|
131
130
|
*/
|
|
132
131
|
|
|
133
132
|
export class BinaryTree<
|
|
134
|
-
K
|
|
133
|
+
K = any,
|
|
135
134
|
V = any,
|
|
135
|
+
R = BTNEntry<K, V>,
|
|
136
136
|
NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>,
|
|
137
|
-
TREE extends BinaryTree<K, V, NODE, TREE> = BinaryTree<K, V, NODE, BinaryTreeNested<K, V, NODE>>
|
|
137
|
+
TREE extends BinaryTree<K, V, R, NODE, TREE> = BinaryTree<K, V, R, NODE, BinaryTreeNested<K, V, R, NODE>>
|
|
138
138
|
>
|
|
139
139
|
extends IterableEntryBase<K, V | undefined>
|
|
140
|
-
implements IBinaryTree<K, V, NODE, TREE> {
|
|
140
|
+
implements IBinaryTree<K, V, R, NODE, TREE> {
|
|
141
141
|
iterationType: IterationType = 'ITERATIVE';
|
|
142
142
|
|
|
143
143
|
/**
|
|
144
|
-
* The constructor function initializes a binary tree object with optional
|
|
145
|
-
* @param [
|
|
144
|
+
* The constructor function initializes a binary tree object with optional keysOrNodesOrEntriesOrRawElements and options.
|
|
145
|
+
* @param [keysOrNodesOrEntriesOrRawElements] - Optional iterable of KeyOrNodeOrEntry objects. These objects represent the
|
|
146
146
|
* nodes to be added to the binary tree.
|
|
147
147
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
148
148
|
* configuration options for the binary tree. In this case, it is of type
|
|
149
149
|
* `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
|
|
150
150
|
* required.
|
|
151
151
|
*/
|
|
152
|
-
constructor(
|
|
152
|
+
constructor(
|
|
153
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
154
|
+
options?: BinaryTreeOptions<K, V, R>
|
|
155
|
+
) {
|
|
153
156
|
super();
|
|
154
157
|
if (options) {
|
|
155
|
-
const { iterationType } = options;
|
|
158
|
+
const { iterationType, toEntryFn } = options;
|
|
156
159
|
if (iterationType) this.iterationType = iterationType;
|
|
160
|
+
if (typeof toEntryFn === 'function') this._toEntryFn = toEntryFn;
|
|
161
|
+
else if (toEntryFn) throw TypeError('toEntryFn must be a function type');
|
|
157
162
|
}
|
|
158
163
|
|
|
159
|
-
this.
|
|
160
|
-
|
|
161
|
-
if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
|
|
164
|
+
if (keysOrNodesOrEntriesOrRawElements) this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
162
165
|
}
|
|
163
166
|
|
|
164
167
|
protected _root?: NODE | null;
|
|
@@ -172,7 +175,7 @@ export class BinaryTree<
|
|
|
172
175
|
return this._root;
|
|
173
176
|
}
|
|
174
177
|
|
|
175
|
-
protected _size: number;
|
|
178
|
+
protected _size: number = 0;
|
|
176
179
|
|
|
177
180
|
/**
|
|
178
181
|
* The function returns the size of an object.
|
|
@@ -192,6 +195,16 @@ export class BinaryTree<
|
|
|
192
195
|
return this._NIL;
|
|
193
196
|
}
|
|
194
197
|
|
|
198
|
+
protected _toEntryFn?: (rawElement: R) => BTNEntry<K, V>;
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* The function returns the value of the _toEntryFn property.
|
|
202
|
+
* @returns The function being returned is `this._toEntryFn`.
|
|
203
|
+
*/
|
|
204
|
+
get toEntryFn() {
|
|
205
|
+
return this._toEntryFn;
|
|
206
|
+
}
|
|
207
|
+
|
|
195
208
|
/**
|
|
196
209
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
197
210
|
* @param {K} key - The key for the new node.
|
|
@@ -209,41 +222,46 @@ export class BinaryTree<
|
|
|
209
222
|
* you can provide only a subset of the properties defined in the `BinaryTreeOptions` interface.
|
|
210
223
|
* @returns a new instance of a binary tree.
|
|
211
224
|
*/
|
|
212
|
-
createTree(options?: Partial<BinaryTreeOptions
|
|
213
|
-
return new BinaryTree<K, V, NODE, TREE>([], { iterationType: this.iterationType, ...options }) as TREE;
|
|
225
|
+
createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): TREE {
|
|
226
|
+
return new BinaryTree<K, V, R, NODE, TREE>([], { iterationType: this.iterationType, ...options }) as TREE;
|
|
214
227
|
}
|
|
215
228
|
|
|
216
229
|
/**
|
|
217
|
-
* The function `
|
|
218
|
-
*
|
|
230
|
+
* The function `keyValueOrEntryOrRawElementToNode` converts a key-value pair, entry, or raw element
|
|
231
|
+
* into a node object.
|
|
232
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
233
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
219
234
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
220
|
-
* `
|
|
221
|
-
*
|
|
222
|
-
* @returns
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
return;
|
|
235
|
+
* `keyValueOrEntryOrRawElementToNode` function. It represents the value associated with a key in a
|
|
236
|
+
* key-value pair. If provided, it will be used to create a node with the specified key and value.
|
|
237
|
+
* @returns The function `keyValueOrEntryOrRawElementToNode` returns either a `NODE` object, `null`,
|
|
238
|
+
* or `undefined`.
|
|
239
|
+
*/
|
|
240
|
+
keyValueOrEntryOrRawElementToNode(
|
|
241
|
+
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
242
|
+
value?: V
|
|
243
|
+
): NODE | null | undefined {
|
|
244
|
+
if (keyOrNodeOrEntryOrRawElement === undefined) return;
|
|
245
|
+
if (keyOrNodeOrEntryOrRawElement === null) return null;
|
|
246
|
+
|
|
247
|
+
if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
|
|
248
|
+
|
|
249
|
+
if (this.toEntryFn) {
|
|
250
|
+
const [key, entryValue] = this.toEntryFn(keyOrNodeOrEntryOrRawElement as R);
|
|
251
|
+
if (key) return this.createNode(key, entryValue ?? value);
|
|
252
|
+
else return;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
256
|
+
const [key, value] = keyOrNodeOrEntryOrRawElement;
|
|
257
|
+
if (key === undefined) return;
|
|
258
|
+
else if (key === null) return null;
|
|
259
|
+
else return this.createNode(key, value);
|
|
245
260
|
}
|
|
246
|
-
|
|
261
|
+
|
|
262
|
+
if (this.isKey(keyOrNodeOrEntryOrRawElement)) return this.createNode(keyOrNodeOrEntryOrRawElement, value);
|
|
263
|
+
|
|
264
|
+
return;
|
|
247
265
|
}
|
|
248
266
|
|
|
249
267
|
/**
|
|
@@ -255,81 +273,122 @@ export class BinaryTree<
|
|
|
255
273
|
* Time Complexity: O(n)
|
|
256
274
|
* Space Complexity: O(log n)
|
|
257
275
|
*
|
|
258
|
-
* The
|
|
259
|
-
*
|
|
260
|
-
* @param {
|
|
261
|
-
* `
|
|
262
|
-
*
|
|
263
|
-
*
|
|
264
|
-
*
|
|
265
|
-
*
|
|
266
|
-
*
|
|
276
|
+
* The `ensureNode` function checks if the input is a valid node and returns it, or converts it to a
|
|
277
|
+
* node if it is a key or entry.
|
|
278
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
279
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, `KeyOrNodeOrEntry<K, V, NODE>`, or
|
|
280
|
+
* a raw element.
|
|
281
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
282
|
+
* parameter that specifies the type of iteration to be used when searching for a node. It has a
|
|
283
|
+
* default value of `'ITERATIVE'`.
|
|
284
|
+
* @returns The function `ensureNode` returns either a `NODE` object, `null`, or `undefined`.
|
|
267
285
|
*/
|
|
268
286
|
ensureNode(
|
|
269
|
-
|
|
287
|
+
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
270
288
|
iterationType: IterationType = 'ITERATIVE'
|
|
271
289
|
): NODE | null | undefined {
|
|
272
|
-
if (
|
|
273
|
-
if (
|
|
274
|
-
|
|
290
|
+
if (keyOrNodeOrEntryOrRawElement === null) return null;
|
|
291
|
+
if (keyOrNodeOrEntryOrRawElement === undefined) return;
|
|
292
|
+
if (keyOrNodeOrEntryOrRawElement === this.NIL) return;
|
|
293
|
+
if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
|
|
294
|
+
|
|
295
|
+
if (this.toEntryFn) {
|
|
296
|
+
const [key] = this.toEntryFn(keyOrNodeOrEntryOrRawElement as R);
|
|
297
|
+
if (key) return this.getNodeByKey(key);
|
|
275
298
|
}
|
|
276
|
-
|
|
277
|
-
|
|
299
|
+
|
|
300
|
+
if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
301
|
+
const key = keyOrNodeOrEntryOrRawElement[0];
|
|
278
302
|
if (key === null) return null;
|
|
279
303
|
if (key === undefined) return;
|
|
280
304
|
return this.getNodeByKey(key, iterationType);
|
|
281
305
|
}
|
|
282
|
-
|
|
283
|
-
if (
|
|
284
|
-
return
|
|
306
|
+
|
|
307
|
+
if (this.isKey(keyOrNodeOrEntryOrRawElement)) return this.getNodeByKey(keyOrNodeOrEntryOrRawElement, iterationType);
|
|
308
|
+
return;
|
|
285
309
|
}
|
|
286
310
|
|
|
287
311
|
/**
|
|
288
|
-
* The function checks if
|
|
289
|
-
* @param {
|
|
290
|
-
*
|
|
312
|
+
* The function checks if the input is an instance of the BinaryTreeNode class.
|
|
313
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
314
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
315
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
316
|
+
* an instance of the `BinaryTreeNode` class.
|
|
291
317
|
*/
|
|
292
|
-
|
|
293
|
-
return
|
|
318
|
+
isNode(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntryOrRawElement is NODE {
|
|
319
|
+
return keyOrNodeOrEntryOrRawElement instanceof BinaryTreeNode;
|
|
294
320
|
}
|
|
295
321
|
|
|
296
322
|
/**
|
|
297
|
-
* The function
|
|
298
|
-
* @param
|
|
299
|
-
*
|
|
323
|
+
* The function checks if a given node is a valid node in a binary search tree.
|
|
324
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
|
|
325
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
326
|
+
* @returns a boolean value.
|
|
300
327
|
*/
|
|
301
|
-
|
|
302
|
-
|
|
328
|
+
isRealNode(node: R | KeyOrNodeOrEntry<K, V, NODE>): node is NODE {
|
|
329
|
+
if (node === this.NIL || node === null || node === undefined) return false;
|
|
330
|
+
return this.isNode(node);
|
|
303
331
|
}
|
|
304
332
|
|
|
305
333
|
/**
|
|
306
|
-
* The function checks if a given node is a real node
|
|
307
|
-
*
|
|
308
|
-
*
|
|
334
|
+
* The function checks if a given node is a real node or null.
|
|
335
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
|
|
336
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
309
337
|
* @returns a boolean value.
|
|
310
338
|
*/
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
return this.isNode(node);
|
|
339
|
+
isNodeOrNull(node: R | KeyOrNodeOrEntry<K, V, NODE>): node is NODE | null {
|
|
340
|
+
return this.isRealNode(node) || node === null;
|
|
314
341
|
}
|
|
315
342
|
|
|
316
343
|
/**
|
|
317
|
-
* The function checks if a given node is
|
|
318
|
-
* @param {
|
|
344
|
+
* The function checks if a given node is equal to the NIL value.
|
|
345
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
|
|
346
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
319
347
|
* @returns a boolean value.
|
|
320
348
|
*/
|
|
321
|
-
isNIL(node: KeyOrNodeOrEntry<K, V, NODE>) {
|
|
349
|
+
isNIL(node: R | KeyOrNodeOrEntry<K, V, NODE>) {
|
|
322
350
|
return node === this.NIL;
|
|
323
351
|
}
|
|
324
352
|
|
|
325
353
|
/**
|
|
326
|
-
* The function checks if
|
|
327
|
-
*
|
|
328
|
-
*
|
|
354
|
+
* The function checks if the input is an array with two elements, indicating it is a binary tree
|
|
355
|
+
* node entry.
|
|
356
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
357
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
358
|
+
* @returns a boolean value.
|
|
359
|
+
*/
|
|
360
|
+
isEntry(
|
|
361
|
+
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
|
|
362
|
+
): keyOrNodeOrEntryOrRawElement is BTNEntry<K, V> {
|
|
363
|
+
return Array.isArray(keyOrNodeOrEntryOrRawElement) && keyOrNodeOrEntryOrRawElement.length === 2;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* The function checks if a given value is a valid key by evaluating its type and value.
|
|
368
|
+
* @param {any} key - The `key` parameter can be of any type. It is the value that we want to check
|
|
369
|
+
* if it is a valid key.
|
|
370
|
+
* @param [isCheckValueOf=true] - The `isCheckValueOf` parameter is a boolean flag that determines
|
|
371
|
+
* whether the function should check the valueOf() method of an object when the key is of type
|
|
372
|
+
* 'object'. If `isCheckValueOf` is true, the function will recursively call itself with the value
|
|
373
|
+
* returned by key.valueOf().
|
|
329
374
|
* @returns a boolean value.
|
|
330
375
|
*/
|
|
331
|
-
|
|
332
|
-
|
|
376
|
+
isKey(key: any, isCheckValueOf = true): key is K {
|
|
377
|
+
if (key === null) return true;
|
|
378
|
+
const keyType = typeof key;
|
|
379
|
+
if (keyType === 'string' || keyType === 'bigint' || keyType === 'boolean') return true;
|
|
380
|
+
if (keyType === 'number') return !isNaN(key);
|
|
381
|
+
if (keyType === 'symbol' || keyType === 'undefined') return false;
|
|
382
|
+
if (keyType === 'function') return this.isKey(key());
|
|
383
|
+
if (keyType === 'object') {
|
|
384
|
+
if (typeof key.toString === 'function') return true;
|
|
385
|
+
if (isCheckValueOf && typeof key.valueOf === 'function') {
|
|
386
|
+
this.isKey(key.valueOf(), false);
|
|
387
|
+
}
|
|
388
|
+
return false;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
return false;
|
|
333
392
|
}
|
|
334
393
|
|
|
335
394
|
/**
|
|
@@ -341,14 +400,20 @@ export class BinaryTree<
|
|
|
341
400
|
* Time Complexity O(n)
|
|
342
401
|
* Space Complexity O(1)
|
|
343
402
|
*
|
|
344
|
-
* The `add` function
|
|
345
|
-
*
|
|
346
|
-
* @param
|
|
347
|
-
*
|
|
348
|
-
*
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
403
|
+
* The `add` function is used to insert a new node into a binary tree, checking for duplicate keys
|
|
404
|
+
* and finding the appropriate insertion position.
|
|
405
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
|
|
406
|
+
* `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which represents the key,
|
|
407
|
+
* node, entry, or raw element to be added to the tree. It can also accept a value of type
|
|
408
|
+
* `KeyOrNodeOrEntry<K, V, NODE>
|
|
409
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
410
|
+
* key being added to the tree. It represents the value that will be stored in the tree for the given
|
|
411
|
+
* key.
|
|
412
|
+
* @returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
413
|
+
* insertion position cannot be found or if there are duplicate keys.
|
|
414
|
+
*/
|
|
415
|
+
add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
|
|
416
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
|
|
352
417
|
if (newNode === undefined) return false;
|
|
353
418
|
|
|
354
419
|
// If the tree is empty, directly set the new node as the root node
|
|
@@ -410,13 +475,20 @@ export class BinaryTree<
|
|
|
410
475
|
* Time Complexity: O(k * n)
|
|
411
476
|
* Space Complexity: O(1)
|
|
412
477
|
*
|
|
413
|
-
* The `addMany` function takes in
|
|
414
|
-
* adds each node with its corresponding value to
|
|
415
|
-
*
|
|
416
|
-
* @param
|
|
417
|
-
*
|
|
418
|
-
|
|
419
|
-
|
|
478
|
+
* The `addMany` function takes in an iterable of keys or nodes or entries or raw elements, and an
|
|
479
|
+
* optional iterable of values, and adds each key or node or entry with its corresponding value to a
|
|
480
|
+
* data structure, returning an array of booleans indicating whether each insertion was successful.
|
|
481
|
+
* @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
|
|
482
|
+
* elements. These elements will be added to the data structure.
|
|
483
|
+
* @param [values] - An optional iterable of values that correspond to the keys or nodes or entries
|
|
484
|
+
* in the `keysOrNodesOrEntriesOrRawElements` parameter.
|
|
485
|
+
* @returns The function `addMany` returns an array of booleans indicating whether each element was
|
|
486
|
+
* successfully added to the data structure.
|
|
487
|
+
*/
|
|
488
|
+
addMany(
|
|
489
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>>,
|
|
490
|
+
values?: Iterable<V | undefined>
|
|
491
|
+
): boolean[] {
|
|
420
492
|
// TODO not sure addMany not be run multi times
|
|
421
493
|
const inserted: boolean[] = [];
|
|
422
494
|
|
|
@@ -425,7 +497,7 @@ export class BinaryTree<
|
|
|
425
497
|
valuesIterator = values[Symbol.iterator]();
|
|
426
498
|
}
|
|
427
499
|
|
|
428
|
-
for (const
|
|
500
|
+
for (const keyOrNodeOrEntryOrRawElement of keysOrNodesOrEntriesOrRawElements) {
|
|
429
501
|
let value: V | undefined | null = undefined;
|
|
430
502
|
|
|
431
503
|
if (valuesIterator) {
|
|
@@ -435,7 +507,7 @@ export class BinaryTree<
|
|
|
435
507
|
}
|
|
436
508
|
}
|
|
437
509
|
|
|
438
|
-
inserted.push(this.add(
|
|
510
|
+
inserted.push(this.add(keyOrNodeOrEntryOrRawElement, value));
|
|
439
511
|
}
|
|
440
512
|
|
|
441
513
|
return inserted;
|
|
@@ -451,17 +523,19 @@ export class BinaryTree<
|
|
|
451
523
|
* Time Complexity: O(k * n)
|
|
452
524
|
* Space Complexity: O(1)
|
|
453
525
|
*
|
|
454
|
-
* The `refill` function clears the current data and adds new
|
|
455
|
-
* @param
|
|
456
|
-
* KeyOrNodeOrEntry<K, V, NODE
|
|
457
|
-
* @param [values] - The `values` parameter is an optional iterable
|
|
458
|
-
*
|
|
459
|
-
*
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
526
|
+
* The `refill` function clears the current data and adds new data to the collection.
|
|
527
|
+
* @param keysOrNodesOrEntriesOrRawElements - An iterable collection of keys, nodes, entries, or raw
|
|
528
|
+
* elements. These can be of any type (R) or a specific type (KeyOrNodeOrEntry<K, V, NODE>).
|
|
529
|
+
* @param [values] - The `values` parameter is an optional iterable of values that will be associated
|
|
530
|
+
* with the keys or nodes being added. If provided, the values will be assigned to the corresponding
|
|
531
|
+
* keys or nodes. If not provided, the values will be set to `undefined`.
|
|
532
|
+
*/
|
|
533
|
+
refill(
|
|
534
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>>,
|
|
535
|
+
values?: Iterable<V | undefined>
|
|
536
|
+
): void {
|
|
463
537
|
this.clear();
|
|
464
|
-
this.addMany(
|
|
538
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements, values);
|
|
465
539
|
}
|
|
466
540
|
|
|
467
541
|
delete<C extends BTNCallback<NODE, K>>(identifier: K, callback?: C): BinaryTreeDeleteResult<NODE>[];
|
|
@@ -476,21 +550,21 @@ export class BinaryTree<
|
|
|
476
550
|
/**
|
|
477
551
|
* Time Complexity: O(n)
|
|
478
552
|
* Space Complexity: O(1)
|
|
479
|
-
|
|
553
|
+
*/
|
|
480
554
|
|
|
481
|
-
|
|
555
|
+
/**
|
|
482
556
|
* Time Complexity: O(n)
|
|
483
557
|
* Space Complexity: O(1)
|
|
484
558
|
*
|
|
485
|
-
* The function
|
|
486
|
-
*
|
|
487
|
-
* @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value
|
|
488
|
-
*
|
|
489
|
-
* the callback function
|
|
490
|
-
* specific node based on its value or object.
|
|
559
|
+
* The above function is a TypeScript implementation of deleting a node from a binary tree, returning
|
|
560
|
+
* the deleted node and the node that needs to be balanced.
|
|
561
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
562
|
+
* used to identify the node that needs to be deleted from the binary tree. It can be of any type
|
|
563
|
+
* that is returned by the callback function.
|
|
491
564
|
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
492
|
-
* identifier of the node to be deleted. It is
|
|
493
|
-
*
|
|
565
|
+
* identifier of the node to be deleted. It is of type `C`, which extends the `BTNCallback<NODE>`
|
|
566
|
+
* interface. The `BTNCallback<NODE>` interface represents a callback function that takes a node of
|
|
567
|
+
* type `NODE
|
|
494
568
|
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
495
569
|
*/
|
|
496
570
|
delete<C extends BTNCallback<NODE>>(
|
|
@@ -545,7 +619,7 @@ export class BinaryTree<
|
|
|
545
619
|
identifier: K,
|
|
546
620
|
callback?: C,
|
|
547
621
|
onlyOne?: boolean,
|
|
548
|
-
beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
|
|
622
|
+
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
549
623
|
iterationType?: IterationType
|
|
550
624
|
): NODE[];
|
|
551
625
|
|
|
@@ -553,7 +627,7 @@ export class BinaryTree<
|
|
|
553
627
|
identifier: NODE | null | undefined,
|
|
554
628
|
callback?: C,
|
|
555
629
|
onlyOne?: boolean,
|
|
556
|
-
beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
|
|
630
|
+
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
557
631
|
iterationType?: IterationType
|
|
558
632
|
): NODE[];
|
|
559
633
|
|
|
@@ -561,7 +635,7 @@ export class BinaryTree<
|
|
|
561
635
|
identifier: ReturnType<C>,
|
|
562
636
|
callback: C,
|
|
563
637
|
onlyOne?: boolean,
|
|
564
|
-
beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
|
|
638
|
+
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
565
639
|
iterationType?: IterationType
|
|
566
640
|
): NODE[];
|
|
567
641
|
|
|
@@ -572,34 +646,33 @@ export class BinaryTree<
|
|
|
572
646
|
|
|
573
647
|
/**
|
|
574
648
|
* Time Complexity: O(n)
|
|
575
|
-
* Space Complexity: O(k + log n)
|
|
649
|
+
* Space Complexity: O(k + log n)
|
|
576
650
|
*
|
|
577
|
-
* The function `getNodes`
|
|
578
|
-
*
|
|
651
|
+
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
652
|
+
* recursive or iterative approach.
|
|
579
653
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
580
|
-
* that
|
|
581
|
-
* callback function
|
|
582
|
-
*
|
|
583
|
-
*
|
|
584
|
-
*
|
|
585
|
-
*
|
|
586
|
-
*
|
|
587
|
-
*
|
|
588
|
-
*
|
|
589
|
-
*
|
|
590
|
-
*
|
|
591
|
-
*
|
|
592
|
-
*
|
|
593
|
-
*
|
|
594
|
-
*
|
|
595
|
-
*
|
|
596
|
-
* @returns an array of nodes of type `NODE`.
|
|
654
|
+
* that is used to identify the nodes. It can be of any type and is used to match against the result
|
|
655
|
+
* of the callback function for each node.
|
|
656
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node as input and
|
|
657
|
+
* returns a value. This value is used to identify the nodes that match the given identifier. The
|
|
658
|
+
* `callback` function is optional and defaults to a default callback function
|
|
659
|
+
* (`this._DEFAULT_CALLBACK`) if not provided.
|
|
660
|
+
* @param [onlyOne=false] - A boolean value indicating whether to return only one node that matches
|
|
661
|
+
* the identifier or all nodes that match the identifier. If set to true, only the first matching
|
|
662
|
+
* node will be returned. If set to false, all matching nodes will be returned. The default value is
|
|
663
|
+
* false.
|
|
664
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
665
|
+
* point for the search. It can be either a node object, a key-value pair, or a key. If it is not
|
|
666
|
+
* provided, the `root` of the data structure is used as the starting point.
|
|
667
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
668
|
+
* iteration to be performed on the nodes of a binary tree. It can have two possible values:
|
|
669
|
+
* @returns an array of NODE objects.
|
|
597
670
|
*/
|
|
598
671
|
getNodes<C extends BTNCallback<NODE>>(
|
|
599
672
|
identifier: ReturnType<C> | null | undefined,
|
|
600
673
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
601
674
|
onlyOne = false,
|
|
602
|
-
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
675
|
+
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
603
676
|
iterationType: IterationType = this.iterationType
|
|
604
677
|
): NODE[] {
|
|
605
678
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -641,21 +714,21 @@ export class BinaryTree<
|
|
|
641
714
|
getNode<C extends BTNCallback<NODE, K>>(
|
|
642
715
|
identifier: K,
|
|
643
716
|
callback?: C,
|
|
644
|
-
beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
|
|
717
|
+
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
645
718
|
iterationType?: IterationType
|
|
646
719
|
): NODE | null | undefined;
|
|
647
720
|
|
|
648
721
|
getNode<C extends BTNCallback<NODE, NODE>>(
|
|
649
722
|
identifier: NODE | null | undefined,
|
|
650
723
|
callback?: C,
|
|
651
|
-
beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
|
|
724
|
+
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
652
725
|
iterationType?: IterationType
|
|
653
726
|
): NODE | null | undefined;
|
|
654
727
|
|
|
655
728
|
getNode<C extends BTNCallback<NODE>>(
|
|
656
729
|
identifier: ReturnType<C>,
|
|
657
730
|
callback: C,
|
|
658
|
-
beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
|
|
731
|
+
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
659
732
|
iterationType?: IterationType
|
|
660
733
|
): NODE | null | undefined;
|
|
661
734
|
|
|
@@ -666,29 +739,26 @@ export class BinaryTree<
|
|
|
666
739
|
|
|
667
740
|
/**
|
|
668
741
|
* Time Complexity: O(n)
|
|
669
|
-
* Space Complexity: O(log n)
|
|
742
|
+
* Space Complexity: O(log n).
|
|
670
743
|
*
|
|
671
|
-
* The function `getNode` returns the first node that matches the given identifier and callback
|
|
672
|
-
*
|
|
744
|
+
* The function `getNode` returns the first node that matches the given identifier and callback,
|
|
745
|
+
* starting from the specified root node and using the specified iteration type.
|
|
673
746
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
674
|
-
* used to identify the node you want to retrieve. It can be of any type that is
|
|
675
|
-
* callback function
|
|
676
|
-
*
|
|
677
|
-
*
|
|
678
|
-
*
|
|
679
|
-
*
|
|
680
|
-
*
|
|
681
|
-
*
|
|
682
|
-
*
|
|
683
|
-
* @
|
|
684
|
-
* be performed when searching for nodes in the binary tree. It determines the order in which the
|
|
685
|
-
* nodes are visited during the search.
|
|
686
|
-
* @returns a value of type `NODE | null | undefined`.
|
|
747
|
+
* used to identify the node you want to retrieve. It can be of any type that is the return type of
|
|
748
|
+
* the `C` callback function, or it can be `null` or `undefined`.
|
|
749
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
750
|
+
* node matches the desired criteria. It should return a value that can be used to identify the node.
|
|
751
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
752
|
+
* point for searching nodes in a tree structure. It can be either a root node, a key-value pair, or
|
|
753
|
+
* a node entry. If not provided, the search will start from the root of the tree.
|
|
754
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
755
|
+
* of iteration to be performed when searching for nodes. It can have one of the following values:
|
|
756
|
+
* @returns The method is returning a NODE object, or null, or undefined.
|
|
687
757
|
*/
|
|
688
758
|
getNode<C extends BTNCallback<NODE>>(
|
|
689
759
|
identifier: ReturnType<C> | null | undefined,
|
|
690
760
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
691
|
-
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
761
|
+
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
692
762
|
iterationType: IterationType = this.iterationType
|
|
693
763
|
): NODE | null | undefined {
|
|
694
764
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
@@ -703,15 +773,13 @@ export class BinaryTree<
|
|
|
703
773
|
* Time Complexity: O(n)
|
|
704
774
|
* Space Complexity: O(log n)
|
|
705
775
|
*
|
|
706
|
-
* The function `getNodeByKey`
|
|
707
|
-
*
|
|
708
|
-
*
|
|
709
|
-
*
|
|
710
|
-
*
|
|
711
|
-
*
|
|
712
|
-
*
|
|
713
|
-
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
714
|
-
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
776
|
+
* The function `getNodeByKey` returns a node with a specific key value from a tree structure.
|
|
777
|
+
* @param {K} key - The key parameter is the value that you want to search for in the tree. It is
|
|
778
|
+
* used to find the node with the matching key value.
|
|
779
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
780
|
+
* parameter that specifies the type of iteration to be used when searching for a node in the tree.
|
|
781
|
+
* It has a default value of `'ITERATIVE'`.
|
|
782
|
+
* @returns a value of type NODE, null, or undefined.
|
|
715
783
|
*/
|
|
716
784
|
getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | null | undefined {
|
|
717
785
|
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
@@ -720,21 +788,21 @@ export class BinaryTree<
|
|
|
720
788
|
override get<C extends BTNCallback<NODE, K>>(
|
|
721
789
|
identifier: K,
|
|
722
790
|
callback?: C,
|
|
723
|
-
beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
|
|
791
|
+
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
724
792
|
iterationType?: IterationType
|
|
725
793
|
): V | undefined;
|
|
726
794
|
|
|
727
795
|
override get<C extends BTNCallback<NODE, NODE>>(
|
|
728
796
|
identifier: NODE | null | undefined,
|
|
729
797
|
callback?: C,
|
|
730
|
-
beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
|
|
798
|
+
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
731
799
|
iterationType?: IterationType
|
|
732
800
|
): V | undefined;
|
|
733
801
|
|
|
734
802
|
override get<C extends BTNCallback<NODE>>(
|
|
735
803
|
identifier: ReturnType<C>,
|
|
736
804
|
callback: C,
|
|
737
|
-
beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
|
|
805
|
+
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
738
806
|
iterationType?: IterationType
|
|
739
807
|
): V | undefined;
|
|
740
808
|
|
|
@@ -747,28 +815,27 @@ export class BinaryTree<
|
|
|
747
815
|
* Time Complexity: O(n)
|
|
748
816
|
* Space Complexity: O(log n)
|
|
749
817
|
*
|
|
750
|
-
* The function `get`
|
|
751
|
-
*
|
|
818
|
+
* The function `get` in TypeScript overrides the base class method and returns the value associated
|
|
819
|
+
* with the given identifier.
|
|
752
820
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
753
|
-
* used to identify the node in the binary tree. It can be of any type that is
|
|
821
|
+
* used to identify the node in the binary tree. It can be of any type that is returned by the
|
|
754
822
|
* callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
|
|
755
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
756
|
-
* the
|
|
757
|
-
*
|
|
758
|
-
*
|
|
759
|
-
*
|
|
760
|
-
*
|
|
761
|
-
*
|
|
762
|
-
*
|
|
763
|
-
*
|
|
764
|
-
*
|
|
765
|
-
*
|
|
766
|
-
* found, `undefined` is returned.
|
|
823
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
824
|
+
* node matches the given identifier. It is optional and defaults to `this._DEFAULT_CALLBACK`.
|
|
825
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
826
|
+
* point for the search in the binary tree. It can be either a root node of the tree or a key, node,
|
|
827
|
+
* or entry object that exists in the tree. If no specific starting point is provided, the search
|
|
828
|
+
* will begin from the root of the
|
|
829
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
830
|
+
* of iteration to be performed when searching for a node in the tree. It can have one of the
|
|
831
|
+
* following values:
|
|
832
|
+
* @returns The method is returning the value associated with the specified identifier in the binary
|
|
833
|
+
* tree.
|
|
767
834
|
*/
|
|
768
835
|
override get<C extends BTNCallback<NODE>>(
|
|
769
836
|
identifier: ReturnType<C> | null | undefined,
|
|
770
837
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
771
|
-
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
838
|
+
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
772
839
|
iterationType: IterationType = this.iterationType
|
|
773
840
|
): V | undefined {
|
|
774
841
|
return this.getNode(identifier, callback, beginRoot, iterationType)?.value;
|
|
@@ -777,54 +844,53 @@ export class BinaryTree<
|
|
|
777
844
|
override has<C extends BTNCallback<NODE, K>>(
|
|
778
845
|
identifier: K,
|
|
779
846
|
callback?: C,
|
|
780
|
-
beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
|
|
847
|
+
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
781
848
|
iterationType?: IterationType
|
|
782
849
|
): boolean;
|
|
783
850
|
|
|
784
851
|
override has<C extends BTNCallback<NODE, NODE>>(
|
|
785
852
|
identifier: NODE | null | undefined,
|
|
786
853
|
callback?: C,
|
|
787
|
-
beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
|
|
854
|
+
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
788
855
|
iterationType?: IterationType
|
|
789
856
|
): boolean;
|
|
790
857
|
|
|
791
858
|
override has<C extends BTNCallback<NODE>>(
|
|
792
859
|
identifier: ReturnType<C> | null | undefined,
|
|
793
860
|
callback: C,
|
|
794
|
-
beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
|
|
861
|
+
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
795
862
|
iterationType?: IterationType
|
|
796
863
|
): boolean;
|
|
797
864
|
|
|
798
865
|
/**
|
|
799
866
|
* Time Complexity: O(n)
|
|
800
|
-
* Space Complexity: O(log n)
|
|
867
|
+
* Space Complexity: O(log n)
|
|
801
868
|
*/
|
|
802
869
|
|
|
803
870
|
/**
|
|
804
871
|
* Time Complexity: O(n)
|
|
805
|
-
* Space Complexity: O(log n)
|
|
872
|
+
* Space Complexity: O(log n)
|
|
806
873
|
*
|
|
807
|
-
* The function checks if a
|
|
874
|
+
* The `has` function checks if a given identifier exists in the data structure and returns a boolean
|
|
875
|
+
* value.
|
|
808
876
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
809
|
-
*
|
|
810
|
-
* callback function `C`. It can also be `null` or `undefined` if
|
|
811
|
-
*
|
|
812
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
813
|
-
*
|
|
814
|
-
*
|
|
815
|
-
*
|
|
816
|
-
*
|
|
817
|
-
*
|
|
818
|
-
*
|
|
819
|
-
*
|
|
820
|
-
*
|
|
821
|
-
* be performed in a pre-order, in-order, or post-order manner.
|
|
822
|
-
* @returns a boolean value.
|
|
877
|
+
* used to identify a specific node or entry in the data structure. It can be of any type that is
|
|
878
|
+
* returned by the callback function `C`. It can also be `null` or `undefined` if no specific
|
|
879
|
+
* identifier is provided.
|
|
880
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine
|
|
881
|
+
* whether a node should be included in the result or not. It is of type `C`, which extends the
|
|
882
|
+
* `BTNCallback<NODE>` type.
|
|
883
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
884
|
+
* point for the iteration in the data structure. It can be either a root node, a key-value pair, or
|
|
885
|
+
* a node entry. If not specified, it defaults to the root of the data structure.
|
|
886
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
887
|
+
* of iteration to be performed. It is an optional parameter with a default value of `IterationType`.
|
|
888
|
+
* @returns The method is returning a boolean value.
|
|
823
889
|
*/
|
|
824
890
|
override has<C extends BTNCallback<NODE>>(
|
|
825
891
|
identifier: ReturnType<C> | null | undefined,
|
|
826
892
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
827
|
-
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
893
|
+
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
828
894
|
iterationType: IterationType = this.iterationType
|
|
829
895
|
): boolean {
|
|
830
896
|
callback = this._ensureCallback(identifier, callback);
|
|
@@ -875,12 +941,13 @@ export class BinaryTree<
|
|
|
875
941
|
*
|
|
876
942
|
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
877
943
|
* height of the tree.
|
|
878
|
-
* @param {
|
|
879
|
-
*
|
|
880
|
-
*
|
|
944
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The parameter `beginRoot` is optional and
|
|
945
|
+
* has a default value of `this.root`. It represents the starting point for checking if the tree is
|
|
946
|
+
* perfectly balanced. It can be either a root node (`R`), a key or node or entry
|
|
947
|
+
* (`KeyOrNodeOrEntry<K, V, NODE
|
|
881
948
|
* @returns a boolean value.
|
|
882
949
|
*/
|
|
883
|
-
isPerfectlyBalanced(beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root): boolean {
|
|
950
|
+
isPerfectlyBalanced(beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root): boolean {
|
|
884
951
|
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
885
952
|
}
|
|
886
953
|
|
|
@@ -893,16 +960,18 @@ export class BinaryTree<
|
|
|
893
960
|
* Time Complexity: O(n)
|
|
894
961
|
* Space Complexity: O(1)
|
|
895
962
|
*
|
|
896
|
-
* The function `
|
|
897
|
-
* @param {
|
|
898
|
-
*
|
|
899
|
-
*
|
|
900
|
-
*
|
|
901
|
-
*
|
|
963
|
+
* The function `isBST` checks if a binary search tree is valid, either recursively or iteratively.
|
|
964
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
965
|
+
* starting point for checking if a binary search tree (BST) is valid. It can be either a root node
|
|
966
|
+
* of the BST, a key value of a node in the BST, or an entry object containing both the key and value
|
|
967
|
+
* of a node in the BST
|
|
968
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to determine the type
|
|
969
|
+
* of iteration to be performed while checking if the binary search tree (BST) is valid. It can have
|
|
970
|
+
* two possible values:
|
|
902
971
|
* @returns a boolean value.
|
|
903
972
|
*/
|
|
904
973
|
isBST(
|
|
905
|
-
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
974
|
+
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
906
975
|
iterationType: IterationType = this.iterationType
|
|
907
976
|
): boolean {
|
|
908
977
|
// TODO there is a bug
|
|
@@ -954,16 +1023,17 @@ export class BinaryTree<
|
|
|
954
1023
|
* Time Complexity: O(n)
|
|
955
1024
|
* Space Complexity: O(1)
|
|
956
1025
|
*
|
|
957
|
-
* The function calculates the depth of a given node in a
|
|
958
|
-
* @param {
|
|
959
|
-
*
|
|
960
|
-
*
|
|
961
|
-
* @param {
|
|
962
|
-
* from which
|
|
963
|
-
*
|
|
964
|
-
*
|
|
965
|
-
|
|
966
|
-
|
|
1026
|
+
* The function calculates the depth of a given node or key in a tree-like data structure.
|
|
1027
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} dist - The `dist` parameter can be either a `R`
|
|
1028
|
+
* (representing a root node), or a `KeyOrNodeOrEntry<K, V, NODE>` (representing a key, node, or
|
|
1029
|
+
* entry).
|
|
1030
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is optional and
|
|
1031
|
+
* represents the starting point from which to calculate the depth. It can be either a reference to a
|
|
1032
|
+
* node in the tree or a key-value pair or an entry object. If not provided, the default value is
|
|
1033
|
+
* `this.root`, which refers to the root node
|
|
1034
|
+
* @returns the depth of a node in a tree structure.
|
|
1035
|
+
*/
|
|
1036
|
+
getDepth(dist: R | KeyOrNodeOrEntry<K, V, NODE>, beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root): number {
|
|
967
1037
|
let distEnsured = this.ensureNode(dist);
|
|
968
1038
|
const beginRootEnsured = this.ensureNode(beginRoot);
|
|
969
1039
|
let depth = 0;
|
|
@@ -984,20 +1054,19 @@ export class BinaryTree<
|
|
|
984
1054
|
|
|
985
1055
|
/**
|
|
986
1056
|
* Time Complexity: O(n)
|
|
987
|
-
* Space Complexity: O(
|
|
1057
|
+
* Space Complexity: O(1)
|
|
988
1058
|
*
|
|
989
|
-
* The
|
|
990
|
-
* iterative
|
|
991
|
-
* @param {
|
|
992
|
-
* starting
|
|
993
|
-
*
|
|
994
|
-
* @param iterationType - The `iterationType` parameter
|
|
995
|
-
*
|
|
996
|
-
*
|
|
997
|
-
* @returns the height of the binary tree.
|
|
1059
|
+
* The `getHeight` function calculates the maximum height of a binary tree using either a recursive
|
|
1060
|
+
* or iterative approach.
|
|
1061
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1062
|
+
* starting point for calculating the height of a tree. It can be either a root node (`R`), a key or
|
|
1063
|
+
* node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current tree.
|
|
1064
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
1065
|
+
* iteration used to calculate the height of the tree. It can have two possible values:
|
|
1066
|
+
* @returns the maximum height of the binary tree.
|
|
998
1067
|
*/
|
|
999
1068
|
getHeight(
|
|
1000
|
-
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1069
|
+
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1001
1070
|
iterationType: IterationType = this.iterationType
|
|
1002
1071
|
): number {
|
|
1003
1072
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1040,15 +1109,18 @@ export class BinaryTree<
|
|
|
1040
1109
|
*
|
|
1041
1110
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
1042
1111
|
* recursive or iterative approach.
|
|
1043
|
-
* @param {
|
|
1044
|
-
* starting
|
|
1045
|
-
*
|
|
1046
|
-
*
|
|
1047
|
-
*
|
|
1048
|
-
*
|
|
1112
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1113
|
+
* starting point for calculating the minimum height of a tree. It can be either a root node (`R`), a
|
|
1114
|
+
* key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current
|
|
1115
|
+
* tree.
|
|
1116
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
1117
|
+
* iteration to be used when calculating the minimum height of the tree. It can have two possible
|
|
1118
|
+
* values:
|
|
1119
|
+
* @returns The function `getMinHeight` returns a number, which represents the minimum height of the
|
|
1120
|
+
* binary tree.
|
|
1049
1121
|
*/
|
|
1050
1122
|
getMinHeight(
|
|
1051
|
-
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1123
|
+
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1052
1124
|
iterationType: IterationType = this.iterationType
|
|
1053
1125
|
): number {
|
|
1054
1126
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1096,24 +1168,22 @@ export class BinaryTree<
|
|
|
1096
1168
|
/**
|
|
1097
1169
|
* Time Complexity: O(log n)
|
|
1098
1170
|
* Space Complexity: O(log n)
|
|
1099
|
-
|
|
1171
|
+
*/
|
|
1100
1172
|
|
|
1101
|
-
|
|
1173
|
+
/**
|
|
1102
1174
|
* Time Complexity: O(log n)
|
|
1103
1175
|
* Space Complexity: O(log n)
|
|
1104
1176
|
*
|
|
1105
|
-
* The function `getPathToRoot` returns an array of nodes from a given node
|
|
1106
|
-
*
|
|
1107
|
-
* @param {
|
|
1108
|
-
*
|
|
1109
|
-
* `null`, or `undefined`.
|
|
1177
|
+
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
|
|
1178
|
+
* up to the root node, with an option to reverse the order of the nodes.
|
|
1179
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginNode - The `beginNode` parameter can be either of
|
|
1180
|
+
* type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
1110
1181
|
* @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
|
|
1111
1182
|
* resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
|
|
1112
|
-
* reversed before returning it. If `isReverse` is set to `false
|
|
1113
|
-
* @returns The function `getPathToRoot` returns an array of
|
|
1183
|
+
* reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
|
|
1184
|
+
* @returns The function `getPathToRoot` returns an array of `NODE` objects.
|
|
1114
1185
|
*/
|
|
1115
|
-
getPathToRoot(beginNode: KeyOrNodeOrEntry<K, V, NODE>, isReverse = true): NODE[] {
|
|
1116
|
-
// TODO to support get path through passing key
|
|
1186
|
+
getPathToRoot(beginNode: R | KeyOrNodeOrEntry<K, V, NODE>, isReverse = true): NODE[] {
|
|
1117
1187
|
const result: NODE[] = [];
|
|
1118
1188
|
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
1119
1189
|
|
|
@@ -1121,7 +1191,6 @@ export class BinaryTree<
|
|
|
1121
1191
|
|
|
1122
1192
|
while (beginNodeEnsured.parent) {
|
|
1123
1193
|
// Array.push + Array.reverse is more efficient than Array.unshift
|
|
1124
|
-
// TODO may consider using Deque, so far this is not the performance bottleneck
|
|
1125
1194
|
result.push(beginNodeEnsured);
|
|
1126
1195
|
beginNodeEnsured = beginNodeEnsured.parent;
|
|
1127
1196
|
}
|
|
@@ -1138,18 +1207,17 @@ export class BinaryTree<
|
|
|
1138
1207
|
* Time Complexity: O(log n)
|
|
1139
1208
|
* Space Complexity: O(1)
|
|
1140
1209
|
*
|
|
1141
|
-
* The
|
|
1142
|
-
*
|
|
1143
|
-
* @param {
|
|
1144
|
-
* for finding the leftmost node in a binary tree. It can be either a `
|
|
1145
|
-
*
|
|
1146
|
-
* @param iterationType - The `iterationType` parameter is used to
|
|
1147
|
-
*
|
|
1148
|
-
* @returns The function `getLeftMost` returns the leftmost node
|
|
1149
|
-
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
1210
|
+
* The `getLeftMost` function returns the leftmost node in a binary tree, either using recursive or
|
|
1211
|
+
* iterative traversal.
|
|
1212
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1213
|
+
* starting point for finding the leftmost node in a binary tree. It can be either a root node (`R`),
|
|
1214
|
+
* a key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
|
|
1215
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
1216
|
+
* of iteration to be performed. It can have two possible values:
|
|
1217
|
+
* @returns The function `getLeftMost` returns the leftmost node in a binary tree.
|
|
1150
1218
|
*/
|
|
1151
1219
|
getLeftMost(
|
|
1152
|
-
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1220
|
+
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1153
1221
|
iterationType: IterationType = this.iterationType
|
|
1154
1222
|
): NODE | null | undefined {
|
|
1155
1223
|
if (this.isNIL(beginRoot)) return beginRoot as NODE;
|
|
@@ -1184,19 +1252,18 @@ export class BinaryTree<
|
|
|
1184
1252
|
* Time Complexity: O(log n)
|
|
1185
1253
|
* Space Complexity: O(1)
|
|
1186
1254
|
*
|
|
1187
|
-
* The
|
|
1255
|
+
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or
|
|
1188
1256
|
* iteratively.
|
|
1189
|
-
* @param {
|
|
1190
|
-
* starting
|
|
1191
|
-
* `
|
|
1192
|
-
*
|
|
1193
|
-
*
|
|
1194
|
-
*
|
|
1195
|
-
* @returns The function `getRightMost` returns
|
|
1196
|
-
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
1257
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1258
|
+
* starting point for finding the rightmost node in a binary tree. It can be either a root node
|
|
1259
|
+
* (`R`), a key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
|
|
1260
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
1261
|
+
* of iteration to be performed when finding the rightmost node in a binary tree. It can have two
|
|
1262
|
+
* possible values:
|
|
1263
|
+
* @returns The function `getRightMost` returns a NODE object, `null`, or `undefined`.
|
|
1197
1264
|
*/
|
|
1198
1265
|
getRightMost(
|
|
1199
|
-
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1266
|
+
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1200
1267
|
iterationType: IterationType = this.iterationType
|
|
1201
1268
|
): NODE | null | undefined {
|
|
1202
1269
|
if (this.isNIL(beginRoot)) return beginRoot as NODE;
|
|
@@ -1231,10 +1298,10 @@ export class BinaryTree<
|
|
|
1231
1298
|
* Time Complexity: O(log n)
|
|
1232
1299
|
* Space Complexity: O(1)
|
|
1233
1300
|
*
|
|
1234
|
-
* The function returns the predecessor of a given node in a tree.
|
|
1235
|
-
* @param {NODE} node - The parameter
|
|
1301
|
+
* The function returns the predecessor node of a given node in a binary tree.
|
|
1302
|
+
* @param {NODE} node - The parameter "node" is of type "NODE", which represents a node in a binary
|
|
1236
1303
|
* tree.
|
|
1237
|
-
* @returns the predecessor of the given
|
|
1304
|
+
* @returns the predecessor node of the given node.
|
|
1238
1305
|
*/
|
|
1239
1306
|
getPredecessor(node: NODE): NODE {
|
|
1240
1307
|
if (this.isRealNode(node.left)) {
|
|
@@ -1261,8 +1328,8 @@ export class BinaryTree<
|
|
|
1261
1328
|
*
|
|
1262
1329
|
* The function `getSuccessor` returns the next node in a binary tree given a current node.
|
|
1263
1330
|
* @param {K | NODE | null} [x] - The parameter `x` can be of type `K`, `NODE`, or `null`.
|
|
1264
|
-
* @returns
|
|
1265
|
-
*
|
|
1331
|
+
* @returns The function `getSuccessor` returns a `NODE` object if a successor exists, `null` if
|
|
1332
|
+
* there is no successor, and `undefined` if the input `x` is not a valid node.
|
|
1266
1333
|
*/
|
|
1267
1334
|
getSuccessor(x?: K | NODE | null): NODE | null | undefined {
|
|
1268
1335
|
x = this.ensureNode(x);
|
|
@@ -1283,7 +1350,7 @@ export class BinaryTree<
|
|
|
1283
1350
|
dfs<C extends BTNCallback<NODE>>(
|
|
1284
1351
|
callback?: C,
|
|
1285
1352
|
pattern?: DFSOrderPattern,
|
|
1286
|
-
beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
|
|
1353
|
+
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
1287
1354
|
iterationType?: IterationType,
|
|
1288
1355
|
includeNull?: false
|
|
1289
1356
|
): ReturnType<C>[];
|
|
@@ -1291,7 +1358,7 @@ export class BinaryTree<
|
|
|
1291
1358
|
dfs<C extends BTNCallback<NODE | null>>(
|
|
1292
1359
|
callback?: C,
|
|
1293
1360
|
pattern?: DFSOrderPattern,
|
|
1294
|
-
beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
|
|
1361
|
+
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
1295
1362
|
iterationType?: IterationType,
|
|
1296
1363
|
includeNull?: true
|
|
1297
1364
|
): ReturnType<C>[];
|
|
@@ -1299,35 +1366,35 @@ export class BinaryTree<
|
|
|
1299
1366
|
/**
|
|
1300
1367
|
* Time complexity: O(n)
|
|
1301
1368
|
* Space complexity: O(n)
|
|
1302
|
-
|
|
1369
|
+
*/
|
|
1303
1370
|
|
|
1304
|
-
|
|
1371
|
+
/**
|
|
1305
1372
|
* Time complexity: O(n)
|
|
1306
1373
|
* Space complexity: O(n)
|
|
1307
1374
|
*
|
|
1308
|
-
* The `dfs` function performs a depth-first search traversal on a binary tree
|
|
1309
|
-
*
|
|
1310
|
-
* callback function
|
|
1311
|
-
*
|
|
1312
|
-
*
|
|
1313
|
-
*
|
|
1314
|
-
*
|
|
1315
|
-
*
|
|
1316
|
-
*
|
|
1317
|
-
*
|
|
1318
|
-
*
|
|
1319
|
-
* @param {IterationType} iterationType - The `iterationType` parameter determines the
|
|
1320
|
-
* iteration to use
|
|
1375
|
+
* The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
|
|
1376
|
+
* function on each node according to a specified pattern and iteration type.
|
|
1377
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
1378
|
+
* visited during the depth-first search. It takes a node as an argument and returns a value. The
|
|
1379
|
+
* return type of the callback function is determined by the generic type `C`.
|
|
1380
|
+
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter determines the order in which the
|
|
1381
|
+
* nodes are visited during the depth-first search. It can have one of the following values:
|
|
1382
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
1383
|
+
* point of the depth-first search. It can be either a node object, a key-value pair, or a key. If it
|
|
1384
|
+
* is a key or key-value pair, the method will find the corresponding node in the tree and start the
|
|
1385
|
+
* search from there.
|
|
1386
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter determines the
|
|
1387
|
+
* type of iteration to use during the depth-first search. It can have two possible values:
|
|
1321
1388
|
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
1322
|
-
* whether
|
|
1323
|
-
* `true`, null
|
|
1324
|
-
*
|
|
1325
|
-
* @returns an array of
|
|
1389
|
+
* whether or not to include null values in the depth-first search traversal. If `includeNull` is set
|
|
1390
|
+
* to `true`, null values will be included in the traversal. If `includeNull` is set to `false`, null
|
|
1391
|
+
* values will
|
|
1392
|
+
* @returns an array of the return types of the callback function.
|
|
1326
1393
|
*/
|
|
1327
1394
|
dfs<C extends BTNCallback<NODE | null | undefined>>(
|
|
1328
1395
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
1329
1396
|
pattern: DFSOrderPattern = 'IN',
|
|
1330
|
-
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1397
|
+
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1331
1398
|
iterationType: IterationType = 'ITERATIVE',
|
|
1332
1399
|
includeNull = false
|
|
1333
1400
|
): ReturnType<C>[] {
|
|
@@ -1421,14 +1488,14 @@ export class BinaryTree<
|
|
|
1421
1488
|
|
|
1422
1489
|
bfs<C extends BTNCallback<NODE>>(
|
|
1423
1490
|
callback?: C,
|
|
1424
|
-
beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
|
|
1491
|
+
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
1425
1492
|
iterationType?: IterationType,
|
|
1426
1493
|
includeNull?: false
|
|
1427
1494
|
): ReturnType<C>[];
|
|
1428
1495
|
|
|
1429
1496
|
bfs<C extends BTNCallback<NODE | null>>(
|
|
1430
1497
|
callback?: C,
|
|
1431
|
-
beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
|
|
1498
|
+
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
1432
1499
|
iterationType?: IterationType,
|
|
1433
1500
|
includeNull?: true
|
|
1434
1501
|
): ReturnType<C>[];
|
|
@@ -1442,26 +1509,27 @@ export class BinaryTree<
|
|
|
1442
1509
|
* Time complexity: O(n)
|
|
1443
1510
|
* Space complexity: O(n)
|
|
1444
1511
|
*
|
|
1445
|
-
* The `bfs` function performs a breadth-first search
|
|
1446
|
-
*
|
|
1512
|
+
* The `bfs` function performs a breadth-first search on a binary tree, calling a callback function
|
|
1513
|
+
* on each node and returning an array of the results.
|
|
1447
1514
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1448
|
-
* the breadth-first search traversal. It takes a single
|
|
1515
|
+
* the breadth-first search traversal. It takes a single argument, which is the current node being
|
|
1449
1516
|
* visited, and returns a value of any type.
|
|
1450
|
-
* @param {
|
|
1451
|
-
* starting
|
|
1452
|
-
* or
|
|
1453
|
-
*
|
|
1454
|
-
* @param iterationType - The `iterationType` parameter determines the type of
|
|
1455
|
-
*
|
|
1456
|
-
* @param [includeNull=false] - The `includeNull` parameter is a boolean
|
|
1457
|
-
* to include null values in the breadth-first search traversal. If
|
|
1458
|
-
* `true`, null values will be included in the traversal
|
|
1459
|
-
*
|
|
1460
|
-
*
|
|
1517
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1518
|
+
* starting point of the breadth-first search. It can be either a root node of a tree or a key, node,
|
|
1519
|
+
* or entry object. If no value is provided, the `root` property of the class is used as the default
|
|
1520
|
+
* starting point.
|
|
1521
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
1522
|
+
* iteration to be performed. It can have two possible values:
|
|
1523
|
+
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
1524
|
+
* whether or not to include null values in the breadth-first search (BFS) traversal. If
|
|
1525
|
+
* `includeNull` is set to `true`, null values will be included in the traversal. If `includeNull` is
|
|
1526
|
+
* set to `false
|
|
1527
|
+
* @returns The function `bfs` returns an array of values that are the result of invoking the
|
|
1528
|
+
* `callback` function on each node in the breadth-first order traversal of the binary tree.
|
|
1461
1529
|
*/
|
|
1462
1530
|
bfs<C extends BTNCallback<NODE | null>>(
|
|
1463
1531
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
1464
|
-
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1532
|
+
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1465
1533
|
iterationType: IterationType = this.iterationType,
|
|
1466
1534
|
includeNull = false
|
|
1467
1535
|
): ReturnType<C>[] {
|
|
@@ -1515,14 +1583,14 @@ export class BinaryTree<
|
|
|
1515
1583
|
|
|
1516
1584
|
listLevels<C extends BTNCallback<NODE>>(
|
|
1517
1585
|
callback?: C,
|
|
1518
|
-
beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
|
|
1586
|
+
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
1519
1587
|
iterationType?: IterationType,
|
|
1520
1588
|
includeNull?: false
|
|
1521
1589
|
): ReturnType<C>[][];
|
|
1522
1590
|
|
|
1523
1591
|
listLevels<C extends BTNCallback<NODE | null>>(
|
|
1524
1592
|
callback?: C,
|
|
1525
|
-
beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
|
|
1593
|
+
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
1526
1594
|
iterationType?: IterationType,
|
|
1527
1595
|
includeNull?: true
|
|
1528
1596
|
): ReturnType<C>[][];
|
|
@@ -1537,25 +1605,25 @@ export class BinaryTree<
|
|
|
1537
1605
|
* Space complexity: O(n)
|
|
1538
1606
|
*
|
|
1539
1607
|
* The `listLevels` function returns an array of arrays, where each inner array represents a level in
|
|
1540
|
-
* a binary tree and contains the
|
|
1541
|
-
* level.
|
|
1608
|
+
* a binary tree and contains the results of applying a callback function to the nodes at that level.
|
|
1542
1609
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1543
|
-
* the tree. It takes a
|
|
1544
|
-
*
|
|
1545
|
-
* @param {
|
|
1546
|
-
* starting
|
|
1547
|
-
*
|
|
1548
|
-
*
|
|
1549
|
-
*
|
|
1610
|
+
* the tree. It takes a node as an argument and returns a value. The return type of the callback
|
|
1611
|
+
* function is determined by the generic type `C` which extends `BTNCallback<NODE | null>`.
|
|
1612
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1613
|
+
* starting point for traversing the tree. It can be either a root node, a key-value pair, or a node
|
|
1614
|
+
* entry. If no value is provided, the `root` property of the class is used as the default starting
|
|
1615
|
+
* point.
|
|
1616
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
1617
|
+
* iteration to be performed on the binary tree. It can have two possible values:
|
|
1550
1618
|
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
1551
|
-
* whether to include null values in the resulting levels. If `includeNull` is set to `true`,
|
|
1619
|
+
* whether or not to include null values in the resulting levels. If `includeNull` is set to `true`,
|
|
1552
1620
|
* null values will be included in the levels. If `includeNull` is set to `false`, null values will
|
|
1553
1621
|
* be excluded
|
|
1554
1622
|
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
1555
1623
|
*/
|
|
1556
1624
|
listLevels<C extends BTNCallback<NODE | null>>(
|
|
1557
1625
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
1558
|
-
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1626
|
+
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1559
1627
|
iterationType: IterationType = this.iterationType,
|
|
1560
1628
|
includeNull = false
|
|
1561
1629
|
): ReturnType<C>[][] {
|
|
@@ -1612,22 +1680,22 @@ export class BinaryTree<
|
|
|
1612
1680
|
* The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
|
|
1613
1681
|
* algorithm.
|
|
1614
1682
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1615
|
-
* the tree. It takes a single
|
|
1616
|
-
*
|
|
1617
|
-
*
|
|
1618
|
-
*
|
|
1683
|
+
* the tree. It takes a single argument, which is the current node, and can return any value. The
|
|
1684
|
+
* return type of the `callback` function is determined by the `ReturnType<C>` type, which represents
|
|
1685
|
+
* the return
|
|
1686
|
+
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function is used
|
|
1687
|
+
* to specify the order in which the nodes of a binary tree are traversed. It can take one of the
|
|
1619
1688
|
* following values:
|
|
1620
|
-
* @param {
|
|
1621
|
-
* for the traversal. It can be
|
|
1622
|
-
* the root of the tree
|
|
1623
|
-
* @returns The function `morris` returns an array of values that are the
|
|
1624
|
-
*
|
|
1625
|
-
* by the return type of the `callback` function.
|
|
1689
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
1690
|
+
* point for the traversal. It can be either a node object, a key, or an entry object. If no value is
|
|
1691
|
+
* provided, the `root` of the tree is used as the starting point.
|
|
1692
|
+
* @returns The function `morris` returns an array of values that are the return values of the
|
|
1693
|
+
* callback function `callback`.
|
|
1626
1694
|
*/
|
|
1627
1695
|
morris<C extends BTNCallback<NODE>>(
|
|
1628
1696
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
1629
1697
|
pattern: DFSOrderPattern = 'IN',
|
|
1630
|
-
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root
|
|
1698
|
+
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root
|
|
1631
1699
|
): ReturnType<C>[] {
|
|
1632
1700
|
beginRoot = this.ensureNode(beginRoot);
|
|
1633
1701
|
if (beginRoot === null) return [];
|
|
@@ -1719,8 +1787,7 @@ export class BinaryTree<
|
|
|
1719
1787
|
* Time complexity: O(n)
|
|
1720
1788
|
* Space complexity: O(n)
|
|
1721
1789
|
*
|
|
1722
|
-
* The `clone` function creates a
|
|
1723
|
-
* the new tree.
|
|
1790
|
+
* The `clone` function creates a deep copy of a tree object.
|
|
1724
1791
|
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
1725
1792
|
*/
|
|
1726
1793
|
clone(): TREE {
|
|
@@ -1746,16 +1813,16 @@ export class BinaryTree<
|
|
|
1746
1813
|
* Time Complexity: O(n)
|
|
1747
1814
|
* Space Complexity: O(n)
|
|
1748
1815
|
*
|
|
1749
|
-
* The `filter` function creates a new tree
|
|
1750
|
-
*
|
|
1751
|
-
*
|
|
1752
|
-
*
|
|
1753
|
-
*
|
|
1754
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that
|
|
1755
|
-
*
|
|
1756
|
-
*
|
|
1757
|
-
* @returns The `filter` method is returning a new tree object that contains the
|
|
1758
|
-
*
|
|
1816
|
+
* The `filter` function creates a new tree with entries that pass a given predicate function.
|
|
1817
|
+
* @param predicate - The `predicate` parameter is a callback function that is used to test each
|
|
1818
|
+
* element in the tree. It takes three arguments: `value`, `key`, and `index`. The `value` argument
|
|
1819
|
+
* represents the value of the current element being processed, the `key` argument represents the key
|
|
1820
|
+
* of the
|
|
1821
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
1822
|
+
* specify the value of `this` within the `predicate` function. When the `predicate` function is
|
|
1823
|
+
* called, `thisArg` will be used as the value of `this` within the function. If `thisArg`
|
|
1824
|
+
* @returns The `filter` method is returning a new tree object that contains the entries that pass
|
|
1825
|
+
* the given predicate function.
|
|
1759
1826
|
*/
|
|
1760
1827
|
filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: any) {
|
|
1761
1828
|
const newTree = this.createTree();
|
|
@@ -1777,15 +1844,15 @@ export class BinaryTree<
|
|
|
1777
1844
|
* Time Complexity: O(n)
|
|
1778
1845
|
* Space Complexity: O(n)
|
|
1779
1846
|
*
|
|
1780
|
-
* The `map` function creates a new tree by applying a callback function to each
|
|
1781
|
-
*
|
|
1782
|
-
* @param callback - The callback parameter is a function that will be called for each
|
|
1783
|
-
*
|
|
1784
|
-
* the
|
|
1785
|
-
*
|
|
1786
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that
|
|
1787
|
-
*
|
|
1788
|
-
*
|
|
1847
|
+
* The `map` function creates a new tree by applying a callback function to each entry in the current
|
|
1848
|
+
* tree.
|
|
1849
|
+
* @param callback - The callback parameter is a function that will be called for each entry in the
|
|
1850
|
+
* tree. It takes three arguments: value, key, and index. The value argument represents the value of
|
|
1851
|
+
* the current entry, the key argument represents the key of the current entry, and the index
|
|
1852
|
+
* argument represents the index of the
|
|
1853
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
1854
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
1855
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
1789
1856
|
* @returns The `map` method is returning a new tree object.
|
|
1790
1857
|
*/
|
|
1791
1858
|
map(callback: EntryCallback<K, V | undefined, V>, thisArg?: any) {
|
|
@@ -1816,13 +1883,17 @@ export class BinaryTree<
|
|
|
1816
1883
|
* Time Complexity: O(n)
|
|
1817
1884
|
* Space Complexity: O(n)
|
|
1818
1885
|
*
|
|
1819
|
-
* The `print` function
|
|
1820
|
-
* @param {
|
|
1821
|
-
*
|
|
1822
|
-
*
|
|
1823
|
-
*
|
|
1824
|
-
|
|
1825
|
-
|
|
1886
|
+
* The `print` function in TypeScript prints the binary tree structure with customizable options.
|
|
1887
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
1888
|
+
* point for printing the binary tree. It can be either a node of the binary tree or a key or entry
|
|
1889
|
+
* that exists in the binary tree. If no value is provided, the root of the binary tree will be used
|
|
1890
|
+
* as the starting point.
|
|
1891
|
+
* @param {BinaryTreePrintOptions} [options] - The `options` parameter is an optional object that
|
|
1892
|
+
* allows you to customize the printing behavior. It has the following properties:
|
|
1893
|
+
* @returns Nothing is being returned. The function has a return type of `void`, which means it does
|
|
1894
|
+
* not return any value.
|
|
1895
|
+
*/
|
|
1896
|
+
override print(beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root, options?: BinaryTreePrintOptions): void {
|
|
1826
1897
|
const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
|
|
1827
1898
|
beginRoot = this.ensureNode(beginRoot);
|
|
1828
1899
|
if (!beginRoot) return;
|
|
@@ -1848,13 +1919,19 @@ export class BinaryTree<
|
|
|
1848
1919
|
}
|
|
1849
1920
|
|
|
1850
1921
|
/**
|
|
1851
|
-
*
|
|
1852
|
-
*
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
*
|
|
1857
|
-
*
|
|
1922
|
+
* Time Complexity: O(1)
|
|
1923
|
+
* Space Complexity: O(1)
|
|
1924
|
+
*/
|
|
1925
|
+
|
|
1926
|
+
/**
|
|
1927
|
+
* Time Complexity: O(1)
|
|
1928
|
+
* Space Complexity: O(1)
|
|
1929
|
+
*
|
|
1930
|
+
* The function `_getIterator` is a generator function that returns an iterator for the key-value
|
|
1931
|
+
* pairs in a binary search tree.
|
|
1932
|
+
* @param node - The `node` parameter represents the current node in the binary search tree. It is
|
|
1933
|
+
* initially set to the root node of the tree.
|
|
1934
|
+
* @returns an IterableIterator<[K, V | undefined]>.
|
|
1858
1935
|
*/
|
|
1859
1936
|
protected* _getIterator(node = this.root): IterableIterator<[K, V | undefined]> {
|
|
1860
1937
|
if (!node) return;
|
|
@@ -1888,6 +1965,14 @@ export class BinaryTree<
|
|
|
1888
1965
|
}
|
|
1889
1966
|
|
|
1890
1967
|
/**
|
|
1968
|
+
* Time Complexity: O(n)
|
|
1969
|
+
* Space Complexity: O(n)
|
|
1970
|
+
*/
|
|
1971
|
+
|
|
1972
|
+
/**
|
|
1973
|
+
* Time Complexity: O(n)
|
|
1974
|
+
* Space Complexity: O(n)
|
|
1975
|
+
*
|
|
1891
1976
|
* The `_displayAux` function is responsible for generating the display layout of a binary tree node,
|
|
1892
1977
|
* taking into account various options such as whether to show null, undefined, or NaN nodes.
|
|
1893
1978
|
* @param {NODE | null | undefined} node - The `node` parameter represents a node in a binary tree.
|
|
@@ -1916,7 +2001,7 @@ export class BinaryTree<
|
|
|
1916
2001
|
// Display logic of normal nodes
|
|
1917
2002
|
|
|
1918
2003
|
const key = node.key,
|
|
1919
|
-
line = this.isNIL(node) ? 'S' : key
|
|
2004
|
+
line = this.isNIL(node) ? 'S' : String(key),
|
|
1920
2005
|
width = line.length;
|
|
1921
2006
|
|
|
1922
2007
|
return _buildNodeDisplay(
|
|
@@ -1927,7 +2012,7 @@ export class BinaryTree<
|
|
|
1927
2012
|
);
|
|
1928
2013
|
} else {
|
|
1929
2014
|
// For cases where none of the conditions are met, null, undefined, and NaN nodes are not displayed
|
|
1930
|
-
const line = node === undefined ? 'U' : '
|
|
2015
|
+
const line = node === undefined ? 'U' : 'N',
|
|
1931
2016
|
width = line.length;
|
|
1932
2017
|
|
|
1933
2018
|
return _buildNodeDisplay(line, width, [[''], 1, 0, 0], [[''], 1, 0, 0]);
|
|
@@ -1972,14 +2057,26 @@ export class BinaryTree<
|
|
|
1972
2057
|
protected _DEFAULT_CALLBACK = (node: NODE | null | undefined) => (node ? node.key : undefined);
|
|
1973
2058
|
|
|
1974
2059
|
/**
|
|
1975
|
-
*
|
|
1976
|
-
*
|
|
1977
|
-
|
|
1978
|
-
|
|
2060
|
+
* Time Complexity: O(1)
|
|
2061
|
+
* Space Complexity: O(1)
|
|
2062
|
+
*/
|
|
2063
|
+
|
|
2064
|
+
/**
|
|
2065
|
+
* Time Complexity: O(1)
|
|
2066
|
+
* Space Complexity: O(1)
|
|
2067
|
+
*
|
|
2068
|
+
* The function `_swapProperties` swaps the key-value properties between two nodes.
|
|
2069
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} srcNode - The source node that will be swapped with the
|
|
2070
|
+
* destination node. It can be either an instance of the class `R`, or an object of type
|
|
2071
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
2072
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} destNode - The `destNode` parameter is the node where
|
|
2073
|
+
* the properties will be swapped with the `srcNode`.
|
|
2074
|
+
* @returns either the `destNode` object with its properties swapped with the `srcNode` object's
|
|
2075
|
+
* properties, or `undefined` if either `srcNode` or `destNode` is falsy.
|
|
1979
2076
|
*/
|
|
1980
2077
|
protected _swapProperties(
|
|
1981
|
-
srcNode: KeyOrNodeOrEntry<K, V, NODE>,
|
|
1982
|
-
destNode: KeyOrNodeOrEntry<K, V, NODE>
|
|
2078
|
+
srcNode: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
2079
|
+
destNode: R | KeyOrNodeOrEntry<K, V, NODE>
|
|
1983
2080
|
): NODE | undefined {
|
|
1984
2081
|
srcNode = this.ensureNode(srcNode);
|
|
1985
2082
|
destNode = this.ensureNode(destNode);
|
|
@@ -2002,12 +2099,21 @@ export class BinaryTree<
|
|
|
2002
2099
|
}
|
|
2003
2100
|
|
|
2004
2101
|
/**
|
|
2005
|
-
*
|
|
2102
|
+
* Time Complexity: O(1)
|
|
2103
|
+
* Space Complexity: O(1)
|
|
2104
|
+
*/
|
|
2105
|
+
|
|
2106
|
+
/**
|
|
2107
|
+
* Time Complexity: O(1)
|
|
2108
|
+
* Space Complexity: O(1)
|
|
2109
|
+
*
|
|
2110
|
+
* The function replaces a node in a binary tree with a new node, updating the parent, left child,
|
|
2111
|
+
* right child, and root if necessary.
|
|
2006
2112
|
* @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
|
|
2007
2113
|
* tree.
|
|
2008
2114
|
* @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
2009
2115
|
* tree.
|
|
2010
|
-
* @returns
|
|
2116
|
+
* @returns the newNode.
|
|
2011
2117
|
*/
|
|
2012
2118
|
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
|
|
2013
2119
|
if (oldNode.parent) {
|
|
@@ -2028,10 +2134,18 @@ export class BinaryTree<
|
|
|
2028
2134
|
}
|
|
2029
2135
|
|
|
2030
2136
|
/**
|
|
2031
|
-
*
|
|
2032
|
-
*
|
|
2033
|
-
|
|
2034
|
-
|
|
2137
|
+
* Time Complexity: O(1)
|
|
2138
|
+
* Space Complexity: O(1)
|
|
2139
|
+
*/
|
|
2140
|
+
|
|
2141
|
+
/**
|
|
2142
|
+
* Time Complexity: O(1)
|
|
2143
|
+
* Space Complexity: O(1)
|
|
2144
|
+
*
|
|
2145
|
+
* The function sets the root property of an object to the provided value, and also updates the
|
|
2146
|
+
* parent property of the new root.
|
|
2147
|
+
* @param {NODE | null | undefined} v - The parameter `v` is of type `NODE | null | undefined`. This
|
|
2148
|
+
* means that it can accept a value of type `NODE`, `null`, or `undefined`.
|
|
2035
2149
|
*/
|
|
2036
2150
|
protected _setRoot(v: NODE | null | undefined) {
|
|
2037
2151
|
if (v) {
|
|
@@ -2040,6 +2154,24 @@ export class BinaryTree<
|
|
|
2040
2154
|
this._root = v;
|
|
2041
2155
|
}
|
|
2042
2156
|
|
|
2157
|
+
/**
|
|
2158
|
+
* Time Complexity: O(1)
|
|
2159
|
+
* Space Complexity: O(1)
|
|
2160
|
+
*/
|
|
2161
|
+
|
|
2162
|
+
/**
|
|
2163
|
+
* Time Complexity: O(1)
|
|
2164
|
+
* Space Complexity: O(1)
|
|
2165
|
+
*
|
|
2166
|
+
* The function `_ensureCallback` ensures that a callback function is provided and returns it.
|
|
2167
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is of type
|
|
2168
|
+
* `ReturnType<C> | null | undefined`. This means it can accept a value that is the return type of
|
|
2169
|
+
* the generic type `C`, or it can be `null` or `undefined`.
|
|
2170
|
+
* @param {C} callback - The `callback` parameter is a function that takes a `node` as an argument
|
|
2171
|
+
* and returns a value. It is of type `C`, which is a generic type that extends the
|
|
2172
|
+
* `BTNCallback<NODE>` type.
|
|
2173
|
+
* @returns the callback parameter.
|
|
2174
|
+
*/
|
|
2043
2175
|
protected _ensureCallback<C extends BTNCallback<NODE>>(
|
|
2044
2176
|
identifier: ReturnType<C> | null | undefined,
|
|
2045
2177
|
callback: C = this._DEFAULT_CALLBACK as C
|