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