heap-typed 1.49.5 → 1.49.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree.d.ts +53 -48
- package/dist/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/data-structures/binary-tree/binary-tree.d.ts +153 -130
- package/dist/data-structures/binary-tree/binary-tree.js +192 -149
- package/dist/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/data-structures/binary-tree/bst.js +113 -89
- package/dist/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +46 -39
- package/dist/data-structures/binary-tree/tree-multimap.js +58 -51
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +3 -3
- package/dist/types/common.js +2 -2
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +58 -53
- package/src/data-structures/binary-tree/binary-tree.ts +253 -205
- package/src/data-structures/binary-tree/bst.ts +125 -104
- package/src/data-structures/binary-tree/rb-tree.ts +66 -64
- package/src/data-structures/binary-tree/tree-multimap.ts +62 -56
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +3 -3
|
@@ -68,15 +68,15 @@ exports.BinaryTreeNode = BinaryTreeNode;
|
|
|
68
68
|
*/
|
|
69
69
|
class BinaryTree extends base_1.IterableEntryBase {
|
|
70
70
|
/**
|
|
71
|
-
* The constructor function initializes a binary tree object with optional
|
|
72
|
-
* @param [
|
|
73
|
-
*
|
|
71
|
+
* The constructor function initializes a binary tree object with optional nodes and options.
|
|
72
|
+
* @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the
|
|
73
|
+
* nodes to be added to the binary tree.
|
|
74
74
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
75
75
|
* configuration options for the binary tree. In this case, it is of type
|
|
76
76
|
* `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
|
|
77
77
|
* required.
|
|
78
78
|
*/
|
|
79
|
-
constructor(
|
|
79
|
+
constructor(nodes, options) {
|
|
80
80
|
super();
|
|
81
81
|
this.iterationType = types_1.IterationType.ITERATIVE;
|
|
82
82
|
this._extractor = (key) => Number(key);
|
|
@@ -91,8 +91,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
93
|
this._size = 0;
|
|
94
|
-
if (
|
|
95
|
-
this.addMany(
|
|
94
|
+
if (nodes)
|
|
95
|
+
this.addMany(nodes);
|
|
96
96
|
}
|
|
97
97
|
get extractor() {
|
|
98
98
|
return this._extractor;
|
|
@@ -123,30 +123,22 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
123
123
|
return new BinaryTree([], Object.assign({ iterationType: this.iterationType }, options));
|
|
124
124
|
}
|
|
125
125
|
/**
|
|
126
|
-
* The function
|
|
127
|
-
* @param
|
|
128
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the class N.
|
|
129
|
-
*/
|
|
130
|
-
isNode(exemplar) {
|
|
131
|
-
return exemplar instanceof BinaryTreeNode;
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
135
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
|
|
126
|
+
* The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
|
|
127
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
136
128
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
137
|
-
* `exemplarToNode` function. It represents the value associated with the
|
|
129
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If no value
|
|
138
130
|
* is provided, it will be `undefined`.
|
|
139
131
|
* @returns a value of type N (node), or null, or undefined.
|
|
140
132
|
*/
|
|
141
|
-
exemplarToNode(
|
|
142
|
-
if (
|
|
133
|
+
exemplarToNode(keyOrNodeOrEntry, value) {
|
|
134
|
+
if (keyOrNodeOrEntry === undefined)
|
|
143
135
|
return;
|
|
144
136
|
let node;
|
|
145
|
-
if (
|
|
137
|
+
if (keyOrNodeOrEntry === null) {
|
|
146
138
|
node = null;
|
|
147
139
|
}
|
|
148
|
-
else if (this.isEntry(
|
|
149
|
-
const [key, value] =
|
|
140
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
141
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
150
142
|
if (key === undefined) {
|
|
151
143
|
return;
|
|
152
144
|
}
|
|
@@ -157,25 +149,108 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
157
149
|
node = this.createNode(key, value);
|
|
158
150
|
}
|
|
159
151
|
}
|
|
160
|
-
else if (this.isNode(
|
|
161
|
-
node =
|
|
152
|
+
else if (this.isNode(keyOrNodeOrEntry)) {
|
|
153
|
+
node = keyOrNodeOrEntry;
|
|
162
154
|
}
|
|
163
|
-
else if (this.isNotNodeInstance(
|
|
164
|
-
node = this.createNode(
|
|
155
|
+
else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
156
|
+
node = this.createNode(keyOrNodeOrEntry, value);
|
|
165
157
|
}
|
|
166
158
|
else {
|
|
167
159
|
return;
|
|
168
160
|
}
|
|
169
161
|
return node;
|
|
170
162
|
}
|
|
163
|
+
/**
|
|
164
|
+
* Time Complexity: O(n)
|
|
165
|
+
* Space Complexity: O(log n)
|
|
166
|
+
*/
|
|
167
|
+
/**
|
|
168
|
+
* Time Complexity: O(n)
|
|
169
|
+
* Space Complexity: O(log n)
|
|
170
|
+
*
|
|
171
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a valid node
|
|
172
|
+
* key, otherwise it returns the key itself.
|
|
173
|
+
* @param {K | N | null | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`,
|
|
174
|
+
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
|
|
175
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
176
|
+
* type of iteration to be used when searching for a node by key. It has a default value of
|
|
177
|
+
* `IterationType.ITERATIVE`.
|
|
178
|
+
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
|
179
|
+
* itself if it is not a valid node key.
|
|
180
|
+
*/
|
|
181
|
+
ensureNode(keyOrNodeOrEntry, iterationType = types_1.IterationType.ITERATIVE) {
|
|
182
|
+
let res;
|
|
183
|
+
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
184
|
+
res = keyOrNodeOrEntry;
|
|
185
|
+
}
|
|
186
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
187
|
+
if (keyOrNodeOrEntry[0] === null)
|
|
188
|
+
res = null;
|
|
189
|
+
else if (keyOrNodeOrEntry[0] !== undefined)
|
|
190
|
+
res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
if (keyOrNodeOrEntry === null)
|
|
194
|
+
res = null;
|
|
195
|
+
else if (keyOrNodeOrEntry !== undefined)
|
|
196
|
+
res = this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
197
|
+
}
|
|
198
|
+
return res;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
|
|
202
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,N>`.
|
|
203
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the class N.
|
|
204
|
+
*/
|
|
205
|
+
isNode(keyOrNodeOrEntry) {
|
|
206
|
+
return keyOrNodeOrEntry instanceof BinaryTreeNode;
|
|
207
|
+
}
|
|
171
208
|
/**
|
|
172
209
|
* The function checks if a given value is an entry in a binary tree node.
|
|
173
|
-
* @param
|
|
210
|
+
* @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,N> - A generic type representing a node in a binary tree. It has
|
|
174
211
|
* two type parameters V and N, representing the value and node type respectively.
|
|
175
212
|
* @returns a boolean value.
|
|
176
213
|
*/
|
|
177
|
-
isEntry(
|
|
178
|
-
return Array.isArray(
|
|
214
|
+
isEntry(keyOrNodeOrEntry) {
|
|
215
|
+
return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Time complexity: O(n)
|
|
219
|
+
* Space complexity: O(log n)
|
|
220
|
+
*/
|
|
221
|
+
/**
|
|
222
|
+
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
223
|
+
* BinaryTreeNode and its key is not NaN.
|
|
224
|
+
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
225
|
+
* @returns a boolean value.
|
|
226
|
+
*/
|
|
227
|
+
isRealNode(node) {
|
|
228
|
+
return node instanceof BinaryTreeNode && String(node.key) !== 'NaN';
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
|
|
232
|
+
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
233
|
+
* @returns a boolean value.
|
|
234
|
+
*/
|
|
235
|
+
isNIL(node) {
|
|
236
|
+
return node instanceof BinaryTreeNode && String(node.key) === 'NaN';
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* The function checks if a given node is a real node or null.
|
|
240
|
+
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
241
|
+
* @returns a boolean value.
|
|
242
|
+
*/
|
|
243
|
+
isNodeOrNull(node) {
|
|
244
|
+
return this.isRealNode(node) || node === null;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
248
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
249
|
+
* data type.
|
|
250
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
251
|
+
*/
|
|
252
|
+
isNotNodeInstance(potentialKey) {
|
|
253
|
+
return !(potentialKey instanceof BinaryTreeNode);
|
|
179
254
|
}
|
|
180
255
|
/**
|
|
181
256
|
* Time Complexity O(log n) - O(n)
|
|
@@ -194,12 +269,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
194
269
|
add(keyOrNodeOrEntry, value) {
|
|
195
270
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
196
271
|
if (newNode === undefined)
|
|
197
|
-
return;
|
|
272
|
+
return false;
|
|
198
273
|
// If the tree is empty, directly set the new node as the root node
|
|
199
274
|
if (!this.root) {
|
|
200
275
|
this._root = newNode;
|
|
201
276
|
this._size = 1;
|
|
202
|
-
return
|
|
277
|
+
return true;
|
|
203
278
|
}
|
|
204
279
|
const queue = new queue_1.Queue([this.root]);
|
|
205
280
|
let potentialParent; // Record the parent node of the potential insertion location
|
|
@@ -210,7 +285,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
210
285
|
// Check for duplicate keys when newNode is not null
|
|
211
286
|
if (newNode !== null && cur.key === newNode.key) {
|
|
212
287
|
this._replaceNode(cur, newNode);
|
|
213
|
-
return
|
|
288
|
+
return true; // If duplicate keys are found, no insertion is performed
|
|
214
289
|
}
|
|
215
290
|
// Record the first possible insertion location found
|
|
216
291
|
if (potentialParent === undefined && (cur.left === undefined || cur.right === undefined)) {
|
|
@@ -233,9 +308,9 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
233
308
|
potentialParent.right = newNode;
|
|
234
309
|
}
|
|
235
310
|
this._size++;
|
|
236
|
-
return
|
|
311
|
+
return true;
|
|
237
312
|
}
|
|
238
|
-
return
|
|
313
|
+
return false; // If the insertion position cannot be found, return undefined
|
|
239
314
|
}
|
|
240
315
|
/**
|
|
241
316
|
* Time Complexity: O(k log n) - O(k * n)
|
|
@@ -246,20 +321,20 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
246
321
|
* Time Complexity: O(k log n) - O(k * n)
|
|
247
322
|
* Space Complexity: O(1)
|
|
248
323
|
*
|
|
249
|
-
* The `addMany` function takes in a collection of
|
|
324
|
+
* The `addMany` function takes in a collection of keysOrNodesOrEntries and an optional collection of values, and
|
|
250
325
|
* adds each node with its corresponding value to the data structure.
|
|
251
|
-
* @param
|
|
326
|
+
* @param keysOrNodesOrEntries - An iterable collection of KeyOrNodeOrEntry objects.
|
|
252
327
|
* @param [values] - An optional iterable of values that will be assigned to each node being added.
|
|
253
328
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
254
329
|
*/
|
|
255
|
-
addMany(
|
|
330
|
+
addMany(keysOrNodesOrEntries, values) {
|
|
256
331
|
// TODO not sure addMany not be run multi times
|
|
257
332
|
const inserted = [];
|
|
258
333
|
let valuesIterator;
|
|
259
334
|
if (values) {
|
|
260
335
|
valuesIterator = values[Symbol.iterator]();
|
|
261
336
|
}
|
|
262
|
-
for (const
|
|
337
|
+
for (const keyOrNodeOrEntry of keysOrNodesOrEntries) {
|
|
263
338
|
let value = undefined;
|
|
264
339
|
if (valuesIterator) {
|
|
265
340
|
const valueResult = valuesIterator.next();
|
|
@@ -267,17 +342,30 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
267
342
|
value = valueResult.value;
|
|
268
343
|
}
|
|
269
344
|
}
|
|
270
|
-
inserted.push(this.add(
|
|
345
|
+
inserted.push(this.add(keyOrNodeOrEntry, value));
|
|
271
346
|
}
|
|
272
347
|
return inserted;
|
|
273
348
|
}
|
|
274
349
|
/**
|
|
275
|
-
* Time Complexity: O(k * n)
|
|
350
|
+
* Time Complexity: O(k * n)
|
|
276
351
|
* Space Complexity: O(1)
|
|
352
|
+
* "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
277
353
|
*/
|
|
278
|
-
|
|
354
|
+
/**
|
|
355
|
+
* Time Complexity: O(k * n)
|
|
356
|
+
* Space Complexity: O(1)
|
|
357
|
+
*
|
|
358
|
+
* The `refill` function clears the current data and adds new key-value pairs to the data structure.
|
|
359
|
+
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries. These can be of type
|
|
360
|
+
* KeyOrNodeOrEntry<K, V, N>.
|
|
361
|
+
* @param [values] - The `values` parameter is an optional iterable that contains the values to be
|
|
362
|
+
* associated with the keys or nodes or entries in the `keysOrNodesOrEntries` parameter. If provided,
|
|
363
|
+
* the values will be associated with the corresponding keys or nodes or entries in the
|
|
364
|
+
* `keysOrNodesOrEntries` iterable
|
|
365
|
+
*/
|
|
366
|
+
refill(keysOrNodesOrEntries, values) {
|
|
279
367
|
this.clear();
|
|
280
|
-
this.addMany(
|
|
368
|
+
this.addMany(keysOrNodesOrEntries, values);
|
|
281
369
|
}
|
|
282
370
|
/**
|
|
283
371
|
* Time Complexity: O(n)
|
|
@@ -351,24 +439,24 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
351
439
|
* Space Complexity: O(1)
|
|
352
440
|
*
|
|
353
441
|
* The function calculates the depth of a given node in a binary tree.
|
|
354
|
-
* @param {K | N | null | undefined}
|
|
442
|
+
* @param {K | N | null | undefined} dist - The `dist` parameter represents the node in
|
|
355
443
|
* the binary tree whose depth we want to find. It can be of type `K`, `N`, `null`, or
|
|
356
444
|
* `undefined`.
|
|
357
445
|
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
358
446
|
* from which we want to calculate the depth. It can be either a `K` (binary tree node key) or
|
|
359
447
|
* `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
|
|
360
|
-
* @returns the depth of the `
|
|
448
|
+
* @returns the depth of the `dist` relative to the `beginRoot`.
|
|
361
449
|
*/
|
|
362
|
-
getDepth(
|
|
363
|
-
|
|
450
|
+
getDepth(dist, beginRoot = this.root) {
|
|
451
|
+
dist = this.ensureNode(dist);
|
|
364
452
|
beginRoot = this.ensureNode(beginRoot);
|
|
365
453
|
let depth = 0;
|
|
366
|
-
while (
|
|
367
|
-
if (
|
|
454
|
+
while (dist === null || dist === void 0 ? void 0 : dist.parent) {
|
|
455
|
+
if (dist === beginRoot) {
|
|
368
456
|
return depth;
|
|
369
457
|
}
|
|
370
458
|
depth++;
|
|
371
|
-
|
|
459
|
+
dist = dist.parent;
|
|
372
460
|
}
|
|
373
461
|
return depth;
|
|
374
462
|
}
|
|
@@ -565,6 +653,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
565
653
|
}
|
|
566
654
|
/**
|
|
567
655
|
* Time Complexity: O(n)
|
|
656
|
+
* Space Complexity: O(log n).
|
|
568
657
|
*
|
|
569
658
|
* The function checks if a Binary Tree Node with a specific identifier exists in the tree.
|
|
570
659
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
@@ -662,24 +751,6 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
662
751
|
}
|
|
663
752
|
}
|
|
664
753
|
}
|
|
665
|
-
/**
|
|
666
|
-
* Time Complexity: O(n)
|
|
667
|
-
* Space Complexity: O(log n)
|
|
668
|
-
*/
|
|
669
|
-
/**
|
|
670
|
-
* The function `ensureNode` returns the node corresponding to the given key if it is a valid node
|
|
671
|
-
* key, otherwise it returns the key itself.
|
|
672
|
-
* @param {K | N | null | undefined} key - The `key` parameter can be of type `K`, `N`,
|
|
673
|
-
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
|
|
674
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
675
|
-
* type of iteration to be used when searching for a node by key. It has a default value of
|
|
676
|
-
* `IterationType.ITERATIVE`.
|
|
677
|
-
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
|
678
|
-
* itself if it is not a valid node key.
|
|
679
|
-
*/
|
|
680
|
-
ensureNode(key, iterationType = types_1.IterationType.ITERATIVE) {
|
|
681
|
-
return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
682
|
-
}
|
|
683
754
|
/**
|
|
684
755
|
* Time Complexity: O(n)
|
|
685
756
|
* Space Complexity: O(log n)
|
|
@@ -709,10 +780,13 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
709
780
|
return (_b = (_a = this.getNode(identifier, callback, beginRoot, iterationType)) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : undefined;
|
|
710
781
|
}
|
|
711
782
|
/**
|
|
712
|
-
* Time Complexity: O(
|
|
713
|
-
* Space Complexity: O(
|
|
783
|
+
* Time Complexity: O(1)
|
|
784
|
+
* Space Complexity: O(1)
|
|
714
785
|
*/
|
|
715
786
|
/**
|
|
787
|
+
* Time Complexity: O(1)
|
|
788
|
+
* Space Complexity: O(1)
|
|
789
|
+
*
|
|
716
790
|
* Clear the binary tree, removing all nodes.
|
|
717
791
|
*/
|
|
718
792
|
clear() {
|
|
@@ -720,6 +794,13 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
720
794
|
this._size = 0;
|
|
721
795
|
}
|
|
722
796
|
/**
|
|
797
|
+
* Time Complexity: O(1)
|
|
798
|
+
* Space Complexity: O(1)
|
|
799
|
+
*/
|
|
800
|
+
/**
|
|
801
|
+
* Time Complexity: O(1)
|
|
802
|
+
* Space Complexity: O(1)
|
|
803
|
+
*
|
|
723
804
|
* Check if the binary tree is empty.
|
|
724
805
|
* @returns {boolean} - True if the binary tree is empty, false otherwise.
|
|
725
806
|
*/
|
|
@@ -757,7 +838,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
757
838
|
}
|
|
758
839
|
/**
|
|
759
840
|
* Time Complexity: O(log n)
|
|
760
|
-
* Space Complexity: O(
|
|
841
|
+
* Space Complexity: O(1)
|
|
761
842
|
*/
|
|
762
843
|
/**
|
|
763
844
|
* Time Complexity: O(log n)
|
|
@@ -853,7 +934,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
853
934
|
* possible values:
|
|
854
935
|
* @returns a boolean value.
|
|
855
936
|
*/
|
|
856
|
-
|
|
937
|
+
isBST(beginRoot = this.root, iterationType = this.iterationType) {
|
|
857
938
|
// TODO there is a bug
|
|
858
939
|
beginRoot = this.ensureNode(beginRoot);
|
|
859
940
|
if (!beginRoot)
|
|
@@ -867,46 +948,34 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
867
948
|
return false;
|
|
868
949
|
return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
|
|
869
950
|
};
|
|
870
|
-
|
|
951
|
+
const isStandardBST = dfs(beginRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
952
|
+
const isInverseBST = dfs(beginRoot, Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
|
|
953
|
+
return isStandardBST || isInverseBST;
|
|
871
954
|
}
|
|
872
955
|
else {
|
|
873
|
-
const
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
956
|
+
const checkBST = (checkMax = false) => {
|
|
957
|
+
const stack = [];
|
|
958
|
+
let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
|
|
959
|
+
// @ts-ignore
|
|
960
|
+
let curr = beginRoot;
|
|
961
|
+
while (curr || stack.length > 0) {
|
|
962
|
+
while (curr) {
|
|
963
|
+
stack.push(curr);
|
|
964
|
+
curr = curr.left;
|
|
965
|
+
}
|
|
966
|
+
curr = stack.pop();
|
|
967
|
+
const numKey = this.extractor(curr.key);
|
|
968
|
+
if (!curr || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey))
|
|
969
|
+
return false;
|
|
970
|
+
prev = numKey;
|
|
971
|
+
curr = curr.right;
|
|
879
972
|
}
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
prev = numKey;
|
|
885
|
-
curr = curr.right;
|
|
886
|
-
}
|
|
887
|
-
return true;
|
|
973
|
+
return true;
|
|
974
|
+
};
|
|
975
|
+
const isStandardBST = checkBST(false), isInverseBST = checkBST(true);
|
|
976
|
+
return isStandardBST || isInverseBST;
|
|
888
977
|
}
|
|
889
978
|
}
|
|
890
|
-
/**
|
|
891
|
-
* Time Complexity: O(n)
|
|
892
|
-
* Space Complexity: O(1)
|
|
893
|
-
*/
|
|
894
|
-
/**
|
|
895
|
-
* Time Complexity: O(n)
|
|
896
|
-
* Space Complexity: O(1)
|
|
897
|
-
*
|
|
898
|
-
* The function checks if a binary tree is a binary search tree.
|
|
899
|
-
* @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
|
|
900
|
-
* be used when checking if the binary tree is a binary search tree (BST). It is an optional
|
|
901
|
-
* parameter with a default value of "this.iterationType". The value of "this.iterationType" is
|
|
902
|
-
* expected to be
|
|
903
|
-
* @returns a boolean value.
|
|
904
|
-
*/
|
|
905
|
-
isBST(iterationType = this.iterationType) {
|
|
906
|
-
if (this.root === null)
|
|
907
|
-
return true;
|
|
908
|
-
return this.isSubtreeBST(this.root, iterationType);
|
|
909
|
-
}
|
|
910
979
|
/**
|
|
911
980
|
* Time complexity: O(n)
|
|
912
981
|
* Space complexity: O(log n)
|
|
@@ -926,7 +995,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
926
995
|
* whether to include null values in the traversal. If `includeNull` is set to `true`, the
|
|
927
996
|
* traversal will include null values, otherwise it will skip them.
|
|
928
997
|
* @returns The function `subTreeTraverse` returns an array of values that are the result of invoking
|
|
929
|
-
* the `callback` function on each node in the subtree. The type of the array
|
|
998
|
+
* the `callback` function on each node in the subtree. The type of the array nodes is determined
|
|
930
999
|
* by the return type of the `callback` function.
|
|
931
1000
|
*/
|
|
932
1001
|
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
@@ -969,44 +1038,6 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
969
1038
|
}
|
|
970
1039
|
return ans;
|
|
971
1040
|
}
|
|
972
|
-
/**
|
|
973
|
-
* Time complexity: O(n)
|
|
974
|
-
* Space complexity: O(log n)
|
|
975
|
-
*/
|
|
976
|
-
/**
|
|
977
|
-
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
978
|
-
* BinaryTreeNode and its key is not NaN.
|
|
979
|
-
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
980
|
-
* @returns a boolean value.
|
|
981
|
-
*/
|
|
982
|
-
isRealNode(node) {
|
|
983
|
-
return node instanceof BinaryTreeNode && String(node.key) !== 'NaN';
|
|
984
|
-
}
|
|
985
|
-
/**
|
|
986
|
-
* The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
|
|
987
|
-
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
988
|
-
* @returns a boolean value.
|
|
989
|
-
*/
|
|
990
|
-
isNIL(node) {
|
|
991
|
-
return node instanceof BinaryTreeNode && String(node.key) === 'NaN';
|
|
992
|
-
}
|
|
993
|
-
/**
|
|
994
|
-
* The function checks if a given node is a real node or null.
|
|
995
|
-
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
996
|
-
* @returns a boolean value.
|
|
997
|
-
*/
|
|
998
|
-
isNodeOrNull(node) {
|
|
999
|
-
return this.isRealNode(node) || node === null;
|
|
1000
|
-
}
|
|
1001
|
-
/**
|
|
1002
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
1003
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
1004
|
-
* data type.
|
|
1005
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
1006
|
-
*/
|
|
1007
|
-
isNotNodeInstance(potentialKey) {
|
|
1008
|
-
return !(potentialKey instanceof BinaryTreeNode);
|
|
1009
|
-
}
|
|
1010
1041
|
/**
|
|
1011
1042
|
* Time complexity: O(n)
|
|
1012
1043
|
* Space complexity: O(n)
|
|
@@ -1328,7 +1359,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1328
1359
|
}
|
|
1329
1360
|
/**
|
|
1330
1361
|
* Time complexity: O(n)
|
|
1331
|
-
* Space complexity: O(
|
|
1362
|
+
* Space complexity: O(n)
|
|
1363
|
+
*/
|
|
1364
|
+
/**
|
|
1365
|
+
* Time complexity: O(n)
|
|
1366
|
+
* Space complexity: O(n)
|
|
1367
|
+
*
|
|
1332
1368
|
* The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
|
|
1333
1369
|
* algorithm.
|
|
1334
1370
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
@@ -1341,7 +1377,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1341
1377
|
* for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
|
|
1342
1378
|
* the root of the tree. If no value is provided, the default value is the root of the tree.
|
|
1343
1379
|
* @returns The function `morris` returns an array of values that are the result of invoking the
|
|
1344
|
-
* `callback` function on each node in the binary tree. The type of the array
|
|
1380
|
+
* `callback` function on each node in the binary tree. The type of the array nodes is determined
|
|
1345
1381
|
* by the return type of the `callback` function.
|
|
1346
1382
|
*/
|
|
1347
1383
|
morris(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root) {
|
|
@@ -1454,8 +1490,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1454
1490
|
* Time Complexity: O(n)
|
|
1455
1491
|
* Space Complexity: O(n)
|
|
1456
1492
|
*
|
|
1457
|
-
* The `filter` function creates a new tree by iterating over the
|
|
1458
|
-
* adding only the
|
|
1493
|
+
* The `filter` function creates a new tree by iterating over the nodes of the current tree and
|
|
1494
|
+
* adding only the nodes that satisfy the given predicate function.
|
|
1459
1495
|
* @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
|
|
1460
1496
|
* `key`, and `index`. It should return a boolean value indicating whether the pair should be
|
|
1461
1497
|
* included in the filtered tree or not.
|
|
@@ -1512,6 +1548,13 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1512
1548
|
// // }
|
|
1513
1549
|
//
|
|
1514
1550
|
/**
|
|
1551
|
+
* Time Complexity: O(n)
|
|
1552
|
+
* Space Complexity: O(n)
|
|
1553
|
+
*/
|
|
1554
|
+
/**
|
|
1555
|
+
* Time Complexity: O(n)
|
|
1556
|
+
* Space Complexity: O(n)
|
|
1557
|
+
*
|
|
1515
1558
|
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
|
1516
1559
|
* @param {K | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `K | N | null |
|
|
1517
1560
|
* undefined`. It represents the root node of a binary tree. The root node can have one of the
|