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