data-structure-typed 1.51.7 → 1.51.9
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 +13 -13
- package/benchmark/report.html +1 -37
- package/benchmark/report.json +15 -387
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +103 -74
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +116 -93
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +82 -62
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +90 -71
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +318 -233
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +492 -392
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +204 -251
- package/dist/cjs/data-structures/binary-tree/bst.js +256 -358
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +74 -85
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +111 -119
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +92 -76
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/cjs/data-structures/graph/abstract-graph.js +10 -15
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +31 -38
- package/dist/cjs/data-structures/hash/hash-map.js +40 -55
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +1 -3
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +2 -3
- package/dist/cjs/data-structures/queue/deque.js +2 -3
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +1 -1
- package/dist/cjs/data-structures/trie/trie.js +1 -1
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +7 -7
- package/dist/cjs/types/common.d.ts +2 -3
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +4 -3
- package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +4 -3
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +6 -5
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +6 -5
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +4 -3
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -3
- package/dist/cjs/types/utils/utils.d.ts +10 -1
- package/dist/cjs/utils/utils.d.ts +2 -1
- package/dist/cjs/utils/utils.js +27 -1
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +103 -74
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +116 -93
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +82 -62
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +90 -71
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +318 -233
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +493 -393
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +204 -251
- package/dist/mjs/data-structures/binary-tree/bst.js +252 -357
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +74 -85
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +111 -119
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +92 -76
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/mjs/data-structures/graph/abstract-graph.js +10 -15
- package/dist/mjs/data-structures/hash/hash-map.d.ts +31 -38
- package/dist/mjs/data-structures/hash/hash-map.js +41 -55
- package/dist/mjs/data-structures/heap/heap.d.ts +1 -3
- package/dist/mjs/data-structures/queue/deque.d.ts +2 -3
- package/dist/mjs/data-structures/queue/deque.js +2 -3
- package/dist/mjs/data-structures/trie/trie.d.ts +1 -1
- package/dist/mjs/data-structures/trie/trie.js +1 -1
- package/dist/mjs/interfaces/binary-tree.d.ts +7 -7
- package/dist/mjs/types/common.d.ts +2 -3
- package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +4 -3
- package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +4 -3
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +6 -5
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +6 -5
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +4 -3
- package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -3
- package/dist/mjs/types/utils/utils.d.ts +10 -1
- package/dist/mjs/utils/utils.d.ts +2 -1
- package/dist/mjs/utils/utils.js +25 -0
- package/dist/umd/data-structure-typed.js +1427 -1343
- package/dist/umd/data-structure-typed.min.js +4 -4
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +6 -6
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +142 -100
- package/src/data-structures/binary-tree/avl-tree.ts +109 -80
- package/src/data-structures/binary-tree/binary-tree.ts +556 -433
- package/src/data-structures/binary-tree/bst.ts +286 -375
- package/src/data-structures/binary-tree/rb-tree.ts +132 -125
- package/src/data-structures/binary-tree/tree-multi-map.ts +129 -102
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/hash/hash-map.ts +42 -49
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/queue/deque.ts +2 -2
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/trie/trie.ts +2 -2
- package/src/interfaces/binary-tree.ts +11 -9
- package/src/types/common.ts +2 -3
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +4 -3
- package/src/types/data-structures/binary-tree/avl-tree.ts +4 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +7 -6
- package/src/types/data-structures/binary-tree/bst.ts +6 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +4 -3
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -3
- package/src/types/utils/utils.ts +14 -1
- package/src/utils/utils.ts +20 -1
- package/test/integration/all-in-one.test.ts +2 -2
- package/test/integration/avl-tree.test.ts +2 -2
- package/test/integration/bst.test.ts +3 -3
- package/test/integration/index.html +95 -11
- package/test/performance/reportor.ts +2 -3
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +75 -7
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +56 -0
- package/test/unit/data-structures/binary-tree/bst.test.ts +6 -6
- package/test/unit/data-structures/binary-tree/overall.test.ts +24 -20
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +6 -6
- package/test/unit/data-structures/graph/directed-graph.test.ts +2 -2
- package/test/unit/utils/utils.test.ts +5 -0
- package/test/utils/big-o.ts +12 -6
- package/test/utils/console.ts +1 -1
- package/tsconfig-base.json +1 -0
|
@@ -97,34 +97,25 @@ export class BinaryTreeNode {
|
|
|
97
97
|
export class BinaryTree extends IterableEntryBase {
|
|
98
98
|
iterationType = 'ITERATIVE';
|
|
99
99
|
/**
|
|
100
|
-
* The constructor function initializes a binary tree object with optional
|
|
101
|
-
* @param [
|
|
100
|
+
* The constructor function initializes a binary tree object with optional keysOrNodesOrEntriesOrRawElements and options.
|
|
101
|
+
* @param [keysOrNodesOrEntriesOrRawElements] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the
|
|
102
102
|
* nodes to be added to the binary tree.
|
|
103
103
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
104
104
|
* configuration options for the binary tree. In this case, it is of type
|
|
105
105
|
* `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
|
|
106
106
|
* required.
|
|
107
107
|
*/
|
|
108
|
-
constructor(
|
|
108
|
+
constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
|
|
109
109
|
super();
|
|
110
110
|
if (options) {
|
|
111
|
-
const { iterationType,
|
|
111
|
+
const { iterationType, toEntryFn } = options;
|
|
112
112
|
if (iterationType)
|
|
113
113
|
this.iterationType = iterationType;
|
|
114
|
-
if (
|
|
115
|
-
this.
|
|
114
|
+
if (typeof toEntryFn === 'function')
|
|
115
|
+
this._toEntryFn = toEntryFn;
|
|
116
116
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
this.addMany(keysOrNodesOrEntries);
|
|
120
|
-
}
|
|
121
|
-
_extractor = (key) => (typeof key === 'number' ? key : Number(key));
|
|
122
|
-
/**
|
|
123
|
-
* The function returns the value of the `_extractor` property.
|
|
124
|
-
* @returns The `_extractor` property is being returned.
|
|
125
|
-
*/
|
|
126
|
-
get extractor() {
|
|
127
|
-
return this._extractor;
|
|
117
|
+
if (keysOrNodesOrEntriesOrRawElements)
|
|
118
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
128
119
|
}
|
|
129
120
|
_root;
|
|
130
121
|
/**
|
|
@@ -135,7 +126,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
135
126
|
get root() {
|
|
136
127
|
return this._root;
|
|
137
128
|
}
|
|
138
|
-
_size;
|
|
129
|
+
_size = 0;
|
|
139
130
|
/**
|
|
140
131
|
* The function returns the size of an object.
|
|
141
132
|
* @returns The size of the object, which is a number.
|
|
@@ -151,6 +142,14 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
151
142
|
get NIL() {
|
|
152
143
|
return this._NIL;
|
|
153
144
|
}
|
|
145
|
+
_toEntryFn;
|
|
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
|
+
}
|
|
154
153
|
/**
|
|
155
154
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
156
155
|
* @param {K} key - The key for the new node.
|
|
@@ -171,42 +170,42 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
171
170
|
return new BinaryTree([], { iterationType: this.iterationType, ...options });
|
|
172
171
|
}
|
|
173
172
|
/**
|
|
174
|
-
* The function `
|
|
175
|
-
*
|
|
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>`.
|
|
176
177
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
177
|
-
* `
|
|
178
|
-
*
|
|
179
|
-
* @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`.
|
|
180
182
|
*/
|
|
181
|
-
|
|
182
|
-
if (
|
|
183
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) {
|
|
184
|
+
if (keyOrNodeOrEntryOrRawElement === undefined)
|
|
183
185
|
return;
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
const [key,
|
|
190
|
-
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 ?? value);
|
|
194
|
+
else
|
|
191
195
|
return;
|
|
192
|
-
}
|
|
193
|
-
else if (key === null) {
|
|
194
|
-
node = null;
|
|
195
|
-
}
|
|
196
|
-
else {
|
|
197
|
-
node = this.createNode(key, value);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
else if (this.isNode(keyOrNodeOrEntry)) {
|
|
201
|
-
node = keyOrNodeOrEntry;
|
|
202
196
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
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);
|
|
208
205
|
}
|
|
209
|
-
|
|
206
|
+
if (this.isKey(keyOrNodeOrEntryOrRawElement))
|
|
207
|
+
return this.createNode(keyOrNodeOrEntryOrRawElement, value);
|
|
208
|
+
return;
|
|
210
209
|
}
|
|
211
210
|
/**
|
|
212
211
|
* Time Complexity: O(n)
|
|
@@ -216,56 +215,56 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
216
215
|
* Time Complexity: O(n)
|
|
217
216
|
* Space Complexity: O(log n)
|
|
218
217
|
*
|
|
219
|
-
* The
|
|
220
|
-
*
|
|
221
|
-
* @param {
|
|
222
|
-
* `
|
|
223
|
-
*
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
*/
|
|
229
|
-
ensureNode(
|
|
230
|
-
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)
|
|
231
234
|
return;
|
|
232
|
-
if (this.
|
|
233
|
-
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);
|
|
234
241
|
}
|
|
235
|
-
if (this.isEntry(
|
|
236
|
-
const key =
|
|
242
|
+
if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
243
|
+
const key = keyOrNodeOrEntryOrRawElement[0];
|
|
237
244
|
if (key === null)
|
|
238
245
|
return null;
|
|
239
246
|
if (key === undefined)
|
|
240
247
|
return;
|
|
241
248
|
return this.getNodeByKey(key, iterationType);
|
|
242
249
|
}
|
|
243
|
-
if (
|
|
244
|
-
return
|
|
245
|
-
|
|
246
|
-
return;
|
|
247
|
-
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
248
|
-
}
|
|
249
|
-
/**
|
|
250
|
-
* The function checks if a given node is a real node or null.
|
|
251
|
-
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
252
|
-
* @returns a boolean value.
|
|
253
|
-
*/
|
|
254
|
-
isNodeOrNull(node) {
|
|
255
|
-
return this.isRealNode(node) || node === null;
|
|
250
|
+
if (this.isKey(keyOrNodeOrEntryOrRawElement))
|
|
251
|
+
return this.getNodeByKey(keyOrNodeOrEntryOrRawElement, iterationType);
|
|
252
|
+
return;
|
|
256
253
|
}
|
|
257
254
|
/**
|
|
258
|
-
* The function
|
|
259
|
-
* @param
|
|
260
|
-
*
|
|
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.
|
|
261
260
|
*/
|
|
262
|
-
isNode(
|
|
263
|
-
return
|
|
261
|
+
isNode(keyOrNodeOrEntryOrRawElement) {
|
|
262
|
+
return keyOrNodeOrEntryOrRawElement instanceof BinaryTreeNode;
|
|
264
263
|
}
|
|
265
264
|
/**
|
|
266
|
-
* The function checks if a given node is a
|
|
267
|
-
*
|
|
268
|
-
*
|
|
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>`.
|
|
269
268
|
* @returns a boolean value.
|
|
270
269
|
*/
|
|
271
270
|
isRealNode(node) {
|
|
@@ -274,21 +273,64 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
274
273
|
return this.isNode(node);
|
|
275
274
|
}
|
|
276
275
|
/**
|
|
277
|
-
* The function checks if a given node is a
|
|
278
|
-
* @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>`.
|
|
279
288
|
* @returns a boolean value.
|
|
280
289
|
*/
|
|
281
290
|
isNIL(node) {
|
|
282
291
|
return node === this.NIL;
|
|
283
292
|
}
|
|
284
293
|
/**
|
|
285
|
-
* The function checks if
|
|
286
|
-
*
|
|
287
|
-
*
|
|
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>`.
|
|
288
298
|
* @returns a boolean value.
|
|
289
299
|
*/
|
|
290
|
-
isEntry(
|
|
291
|
-
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;
|
|
292
334
|
}
|
|
293
335
|
/**
|
|
294
336
|
* Time Complexity O(n)
|
|
@@ -298,14 +340,20 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
298
340
|
* Time Complexity O(n)
|
|
299
341
|
* Space Complexity O(1)
|
|
300
342
|
*
|
|
301
|
-
* The `add` function
|
|
302
|
-
*
|
|
303
|
-
* @param
|
|
304
|
-
*
|
|
305
|
-
*
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
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);
|
|
309
357
|
if (newNode === undefined)
|
|
310
358
|
return false;
|
|
311
359
|
// If the tree is empty, directly set the new node as the root node
|
|
@@ -359,20 +407,24 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
359
407
|
* Time Complexity: O(k * n)
|
|
360
408
|
* Space Complexity: O(1)
|
|
361
409
|
*
|
|
362
|
-
* The `addMany` function takes in
|
|
363
|
-
* adds each node with its corresponding value to
|
|
364
|
-
*
|
|
365
|
-
* @param
|
|
366
|
-
*
|
|
367
|
-
|
|
368
|
-
|
|
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) {
|
|
369
421
|
// TODO not sure addMany not be run multi times
|
|
370
422
|
const inserted = [];
|
|
371
423
|
let valuesIterator;
|
|
372
424
|
if (values) {
|
|
373
425
|
valuesIterator = values[Symbol.iterator]();
|
|
374
426
|
}
|
|
375
|
-
for (const
|
|
427
|
+
for (const keyOrNodeOrEntryOrRawElement of keysOrNodesOrEntriesOrRawElements) {
|
|
376
428
|
let value = undefined;
|
|
377
429
|
if (valuesIterator) {
|
|
378
430
|
const valueResult = valuesIterator.next();
|
|
@@ -380,7 +432,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
380
432
|
value = valueResult.value;
|
|
381
433
|
}
|
|
382
434
|
}
|
|
383
|
-
inserted.push(this.add(
|
|
435
|
+
inserted.push(this.add(keyOrNodeOrEntryOrRawElement, value));
|
|
384
436
|
}
|
|
385
437
|
return inserted;
|
|
386
438
|
}
|
|
@@ -393,36 +445,34 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
393
445
|
* Time Complexity: O(k * n)
|
|
394
446
|
* Space Complexity: O(1)
|
|
395
447
|
*
|
|
396
|
-
* The `refill` function clears the current data and adds new
|
|
397
|
-
* @param
|
|
398
|
-
* KeyOrNodeOrEntry<K, V, NODE
|
|
399
|
-
* @param [values] - The `values` parameter is an optional iterable
|
|
400
|
-
*
|
|
401
|
-
*
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
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) {
|
|
405
456
|
this.clear();
|
|
406
|
-
this.addMany(
|
|
457
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements, values);
|
|
407
458
|
}
|
|
408
459
|
/**
|
|
409
460
|
* Time Complexity: O(n)
|
|
410
461
|
* Space Complexity: O(1)
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
/**
|
|
462
|
+
*/
|
|
463
|
+
/**
|
|
414
464
|
* Time Complexity: O(n)
|
|
415
465
|
* Space Complexity: O(1)
|
|
416
466
|
*
|
|
417
|
-
* The function
|
|
418
|
-
*
|
|
419
|
-
* @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value
|
|
420
|
-
*
|
|
421
|
-
* the callback function
|
|
422
|
-
* 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.
|
|
423
472
|
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
424
|
-
* identifier of the node to be deleted. It is
|
|
425
|
-
*
|
|
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
|
|
426
476
|
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
427
477
|
*/
|
|
428
478
|
delete(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
@@ -477,28 +527,27 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
477
527
|
*/
|
|
478
528
|
/**
|
|
479
529
|
* Time Complexity: O(n)
|
|
480
|
-
* Space Complexity: O(k + log n)
|
|
530
|
+
* Space Complexity: O(k + log n)
|
|
481
531
|
*
|
|
482
|
-
* The function `getNodes`
|
|
483
|
-
*
|
|
532
|
+
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
533
|
+
* recursive or iterative approach.
|
|
484
534
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
485
|
-
* that
|
|
486
|
-
* callback function
|
|
487
|
-
*
|
|
488
|
-
*
|
|
489
|
-
*
|
|
490
|
-
*
|
|
491
|
-
*
|
|
492
|
-
*
|
|
493
|
-
*
|
|
494
|
-
*
|
|
495
|
-
*
|
|
496
|
-
*
|
|
497
|
-
*
|
|
498
|
-
*
|
|
499
|
-
*
|
|
500
|
-
*
|
|
501
|
-
* @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.
|
|
502
551
|
*/
|
|
503
552
|
getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
504
553
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -543,24 +592,21 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
543
592
|
*/
|
|
544
593
|
/**
|
|
545
594
|
* Time Complexity: O(n)
|
|
546
|
-
* Space Complexity: O(log n)
|
|
595
|
+
* Space Complexity: O(log n).
|
|
547
596
|
*
|
|
548
|
-
* The function `getNode` returns the first node that matches the given identifier and callback
|
|
549
|
-
*
|
|
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.
|
|
550
599
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
551
|
-
* used to identify the node you want to retrieve. It can be of any type that is
|
|
552
|
-
* callback function
|
|
553
|
-
*
|
|
554
|
-
*
|
|
555
|
-
*
|
|
556
|
-
*
|
|
557
|
-
*
|
|
558
|
-
*
|
|
559
|
-
*
|
|
560
|
-
* @
|
|
561
|
-
* be performed when searching for nodes in the binary tree. It determines the order in which the
|
|
562
|
-
* nodes are visited during the search.
|
|
563
|
-
* @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.
|
|
564
610
|
*/
|
|
565
611
|
getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
566
612
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
@@ -573,15 +619,13 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
573
619
|
* Time Complexity: O(n)
|
|
574
620
|
* Space Complexity: O(log n)
|
|
575
621
|
*
|
|
576
|
-
* The function `getNodeByKey`
|
|
577
|
-
*
|
|
578
|
-
*
|
|
579
|
-
*
|
|
580
|
-
*
|
|
581
|
-
*
|
|
582
|
-
*
|
|
583
|
-
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
584
|
-
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
622
|
+
* The function `getNodeByKey` returns a node with a specific key value from a tree structure.
|
|
623
|
+
* @param {K} key - The key parameter is the value that you want to search for in the tree. It is
|
|
624
|
+
* used to find the node with the matching key value.
|
|
625
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
626
|
+
* parameter that specifies the type of iteration to be used when searching for a node in the tree.
|
|
627
|
+
* It has a default value of `'ITERATIVE'`.
|
|
628
|
+
* @returns a value of type NODE, null, or undefined.
|
|
585
629
|
*/
|
|
586
630
|
getNodeByKey(key, iterationType = 'ITERATIVE') {
|
|
587
631
|
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
@@ -594,51 +638,49 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
594
638
|
* Time Complexity: O(n)
|
|
595
639
|
* Space Complexity: O(log n)
|
|
596
640
|
*
|
|
597
|
-
* The function `get`
|
|
598
|
-
*
|
|
641
|
+
* The function `get` in TypeScript overrides the base class method and returns the value associated
|
|
642
|
+
* with the given identifier.
|
|
599
643
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
600
|
-
* used to identify the node in the binary tree. It can be of any type that is
|
|
644
|
+
* used to identify the node in the binary tree. It can be of any type that is returned by the
|
|
601
645
|
* callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
|
|
602
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
603
|
-
* the
|
|
604
|
-
*
|
|
605
|
-
*
|
|
606
|
-
*
|
|
607
|
-
*
|
|
608
|
-
*
|
|
609
|
-
*
|
|
610
|
-
*
|
|
611
|
-
*
|
|
612
|
-
*
|
|
613
|
-
* found, `undefined` is returned.
|
|
646
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
647
|
+
* node matches the given identifier. It is optional and defaults to `this._DEFAULT_CALLBACK`.
|
|
648
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
649
|
+
* point for the search in the binary tree. It can be either a root node of the tree or a key, node,
|
|
650
|
+
* or entry object that exists in the tree. If no specific starting point is provided, the search
|
|
651
|
+
* will begin from the root of the
|
|
652
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
653
|
+
* of iteration to be performed when searching for a node in the tree. It can have one of the
|
|
654
|
+
* following values:
|
|
655
|
+
* @returns The method is returning the value associated with the specified identifier in the binary
|
|
656
|
+
* tree.
|
|
614
657
|
*/
|
|
615
658
|
get(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
616
659
|
return this.getNode(identifier, callback, beginRoot, iterationType)?.value;
|
|
617
660
|
}
|
|
618
661
|
/**
|
|
619
662
|
* Time Complexity: O(n)
|
|
620
|
-
* Space Complexity: O(log n)
|
|
663
|
+
* Space Complexity: O(log n)
|
|
621
664
|
*/
|
|
622
665
|
/**
|
|
623
666
|
* Time Complexity: O(n)
|
|
624
|
-
* Space Complexity: O(log n)
|
|
667
|
+
* Space Complexity: O(log n)
|
|
625
668
|
*
|
|
626
|
-
* The function checks if a
|
|
669
|
+
* The `has` function checks if a given identifier exists in the data structure and returns a boolean
|
|
670
|
+
* value.
|
|
627
671
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
628
|
-
*
|
|
629
|
-
* callback function `C`. It can also be `null` or `undefined` if
|
|
630
|
-
*
|
|
631
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
632
|
-
*
|
|
633
|
-
*
|
|
634
|
-
*
|
|
635
|
-
*
|
|
636
|
-
*
|
|
637
|
-
*
|
|
638
|
-
*
|
|
639
|
-
*
|
|
640
|
-
* be performed in a pre-order, in-order, or post-order manner.
|
|
641
|
-
* @returns a boolean value.
|
|
672
|
+
* used to identify a specific node or entry in the data structure. It can be of any type that is
|
|
673
|
+
* returned by the callback function `C`. It can also be `null` or `undefined` if no specific
|
|
674
|
+
* identifier is provided.
|
|
675
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine
|
|
676
|
+
* whether a node should be included in the result or not. It is of type `C`, which extends the
|
|
677
|
+
* `BTNCallback<NODE>` type.
|
|
678
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
679
|
+
* point for the iteration in the data structure. It can be either a root node, a key-value pair, or
|
|
680
|
+
* a node entry. If not specified, it defaults to the root of the data structure.
|
|
681
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
682
|
+
* of iteration to be performed. It is an optional parameter with a default value of `IterationType`.
|
|
683
|
+
* @returns The method is returning a boolean value.
|
|
642
684
|
*/
|
|
643
685
|
has(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
644
686
|
callback = this._ensureCallback(identifier, callback);
|
|
@@ -682,9 +724,10 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
682
724
|
*
|
|
683
725
|
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
684
726
|
* height of the tree.
|
|
685
|
-
* @param {
|
|
686
|
-
*
|
|
687
|
-
*
|
|
727
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The parameter `beginRoot` is optional and
|
|
728
|
+
* has a default value of `this.root`. It represents the starting point for checking if the tree is
|
|
729
|
+
* perfectly balanced. It can be either a root node (`R`), a key or node or entry
|
|
730
|
+
* (`KeyOrNodeOrEntry<K, V, NODE
|
|
688
731
|
* @returns a boolean value.
|
|
689
732
|
*/
|
|
690
733
|
isPerfectlyBalanced(beginRoot = this.root) {
|
|
@@ -698,12 +741,14 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
698
741
|
* Time Complexity: O(n)
|
|
699
742
|
* Space Complexity: O(1)
|
|
700
743
|
*
|
|
701
|
-
* The function `
|
|
702
|
-
* @param {
|
|
703
|
-
*
|
|
704
|
-
*
|
|
705
|
-
*
|
|
706
|
-
*
|
|
744
|
+
* The function `isBST` checks if a binary search tree is valid, either recursively or iteratively.
|
|
745
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
746
|
+
* starting point for checking if a binary search tree (BST) is valid. It can be either a root node
|
|
747
|
+
* of the BST, a key value of a node in the BST, or an entry object containing both the key and value
|
|
748
|
+
* of a node in the BST
|
|
749
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to determine the type
|
|
750
|
+
* of iteration to be performed while checking if the binary search tree (BST) is valid. It can have
|
|
751
|
+
* two possible values:
|
|
707
752
|
* @returns a boolean value.
|
|
708
753
|
*/
|
|
709
754
|
isBST(beginRoot = this.root, iterationType = this.iterationType) {
|
|
@@ -715,7 +760,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
715
760
|
const dfs = (cur, min, max) => {
|
|
716
761
|
if (!this.isRealNode(cur))
|
|
717
762
|
return true;
|
|
718
|
-
const numKey =
|
|
763
|
+
const numKey = Number(cur.key);
|
|
719
764
|
if (numKey <= min || numKey >= max)
|
|
720
765
|
return false;
|
|
721
766
|
return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
|
|
@@ -736,7 +781,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
736
781
|
curr = curr.left;
|
|
737
782
|
}
|
|
738
783
|
curr = stack.pop();
|
|
739
|
-
const numKey =
|
|
784
|
+
const numKey = Number(curr.key);
|
|
740
785
|
if (!this.isRealNode(curr) || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey))
|
|
741
786
|
return false;
|
|
742
787
|
prev = numKey;
|
|
@@ -756,25 +801,26 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
756
801
|
* Time Complexity: O(n)
|
|
757
802
|
* Space Complexity: O(1)
|
|
758
803
|
*
|
|
759
|
-
* The function calculates the depth of a given node in a
|
|
760
|
-
* @param {
|
|
761
|
-
*
|
|
762
|
-
*
|
|
763
|
-
* @param {
|
|
764
|
-
* from which
|
|
765
|
-
*
|
|
766
|
-
*
|
|
804
|
+
* The function calculates the depth of a given node or key in a tree-like data structure.
|
|
805
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} dist - The `dist` parameter can be either a `R`
|
|
806
|
+
* (representing a root node), or a `KeyOrNodeOrEntry<K, V, NODE>` (representing a key, node, or
|
|
807
|
+
* entry).
|
|
808
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is optional and
|
|
809
|
+
* represents the starting point from which to calculate the depth. It can be either a reference to a
|
|
810
|
+
* node in the tree or a key-value pair or an entry object. If not provided, the default value is
|
|
811
|
+
* `this.root`, which refers to the root node
|
|
812
|
+
* @returns the depth of a node in a tree structure.
|
|
767
813
|
*/
|
|
768
814
|
getDepth(dist, beginRoot = this.root) {
|
|
769
|
-
|
|
770
|
-
|
|
815
|
+
let distEnsured = this.ensureNode(dist);
|
|
816
|
+
const beginRootEnsured = this.ensureNode(beginRoot);
|
|
771
817
|
let depth = 0;
|
|
772
|
-
while (
|
|
773
|
-
if (
|
|
818
|
+
while (distEnsured?.parent) {
|
|
819
|
+
if (distEnsured === beginRootEnsured) {
|
|
774
820
|
return depth;
|
|
775
821
|
}
|
|
776
822
|
depth++;
|
|
777
|
-
|
|
823
|
+
distEnsured = distEnsured.parent;
|
|
778
824
|
}
|
|
779
825
|
return depth;
|
|
780
826
|
}
|
|
@@ -784,17 +830,16 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
784
830
|
*/
|
|
785
831
|
/**
|
|
786
832
|
* Time Complexity: O(n)
|
|
787
|
-
* Space Complexity: O(
|
|
833
|
+
* Space Complexity: O(1)
|
|
788
834
|
*
|
|
789
|
-
* The
|
|
790
|
-
* iterative
|
|
791
|
-
* @param {
|
|
792
|
-
* starting
|
|
793
|
-
*
|
|
794
|
-
* @param iterationType - The `iterationType` parameter
|
|
795
|
-
*
|
|
796
|
-
*
|
|
797
|
-
* @returns the height of the binary tree.
|
|
835
|
+
* The `getHeight` function calculates the maximum height of a binary tree using either a recursive
|
|
836
|
+
* or iterative approach.
|
|
837
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
838
|
+
* starting point for calculating the height of a tree. It can be either a root node (`R`), a key or
|
|
839
|
+
* node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current tree.
|
|
840
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
841
|
+
* iteration used to calculate the height of the tree. It can have two possible values:
|
|
842
|
+
* @returns the maximum height of the binary tree.
|
|
798
843
|
*/
|
|
799
844
|
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
800
845
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -834,12 +879,15 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
834
879
|
*
|
|
835
880
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
836
881
|
* recursive or iterative approach.
|
|
837
|
-
* @param {
|
|
838
|
-
* starting
|
|
839
|
-
*
|
|
840
|
-
*
|
|
841
|
-
*
|
|
842
|
-
*
|
|
882
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
883
|
+
* starting point for calculating the minimum height of a tree. It can be either a root node (`R`), a
|
|
884
|
+
* key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current
|
|
885
|
+
* tree.
|
|
886
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
887
|
+
* iteration to be used when calculating the minimum height of the tree. It can have two possible
|
|
888
|
+
* values:
|
|
889
|
+
* @returns The function `getMinHeight` returns a number, which represents the minimum height of the
|
|
890
|
+
* binary tree.
|
|
843
891
|
*/
|
|
844
892
|
getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
845
893
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -888,35 +936,31 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
888
936
|
/**
|
|
889
937
|
* Time Complexity: O(log n)
|
|
890
938
|
* Space Complexity: O(log n)
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
/**
|
|
939
|
+
*/
|
|
940
|
+
/**
|
|
894
941
|
* Time Complexity: O(log n)
|
|
895
942
|
* Space Complexity: O(log n)
|
|
896
943
|
*
|
|
897
|
-
* The function `getPathToRoot` returns an array of nodes from a given node
|
|
898
|
-
*
|
|
899
|
-
* @param {
|
|
900
|
-
*
|
|
901
|
-
* `null`, or `undefined`.
|
|
944
|
+
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
|
|
945
|
+
* up to the root node, with an option to reverse the order of the nodes.
|
|
946
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginNode - The `beginNode` parameter can be either of
|
|
947
|
+
* type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
902
948
|
* @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
|
|
903
949
|
* resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
|
|
904
|
-
* reversed before returning it. If `isReverse` is set to `false
|
|
905
|
-
* @returns The function `getPathToRoot` returns an array of
|
|
950
|
+
* reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
|
|
951
|
+
* @returns The function `getPathToRoot` returns an array of `NODE` objects.
|
|
906
952
|
*/
|
|
907
953
|
getPathToRoot(beginNode, isReverse = true) {
|
|
908
|
-
// TODO to support get path through passing key
|
|
909
954
|
const result = [];
|
|
910
|
-
|
|
911
|
-
if (!
|
|
955
|
+
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
956
|
+
if (!beginNodeEnsured)
|
|
912
957
|
return result;
|
|
913
|
-
while (
|
|
958
|
+
while (beginNodeEnsured.parent) {
|
|
914
959
|
// Array.push + Array.reverse is more efficient than Array.unshift
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
beginNode = beginNode.parent;
|
|
960
|
+
result.push(beginNodeEnsured);
|
|
961
|
+
beginNodeEnsured = beginNodeEnsured.parent;
|
|
918
962
|
}
|
|
919
|
-
result.push(
|
|
963
|
+
result.push(beginNodeEnsured);
|
|
920
964
|
return isReverse ? result.reverse() : result;
|
|
921
965
|
}
|
|
922
966
|
/**
|
|
@@ -927,15 +971,14 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
927
971
|
* Time Complexity: O(log n)
|
|
928
972
|
* Space Complexity: O(1)
|
|
929
973
|
*
|
|
930
|
-
* The
|
|
931
|
-
*
|
|
932
|
-
* @param {
|
|
933
|
-
* for finding the leftmost node in a binary tree. It can be either a `
|
|
934
|
-
*
|
|
935
|
-
* @param iterationType - The `iterationType` parameter is used to
|
|
936
|
-
*
|
|
937
|
-
* @returns The function `getLeftMost` returns the leftmost node
|
|
938
|
-
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
974
|
+
* The `getLeftMost` function returns the leftmost node in a binary tree, either using recursive or
|
|
975
|
+
* iterative traversal.
|
|
976
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
977
|
+
* starting point for finding the leftmost node in a binary tree. It can be either a root node (`R`),
|
|
978
|
+
* a key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
|
|
979
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
980
|
+
* of iteration to be performed. It can have two possible values:
|
|
981
|
+
* @returns The function `getLeftMost` returns the leftmost node in a binary tree.
|
|
939
982
|
*/
|
|
940
983
|
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
941
984
|
if (this.isNIL(beginRoot))
|
|
@@ -969,16 +1012,15 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
969
1012
|
* Time Complexity: O(log n)
|
|
970
1013
|
* Space Complexity: O(1)
|
|
971
1014
|
*
|
|
972
|
-
* The
|
|
1015
|
+
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or
|
|
973
1016
|
* iteratively.
|
|
974
|
-
* @param {
|
|
975
|
-
* starting
|
|
976
|
-
* `
|
|
977
|
-
*
|
|
978
|
-
*
|
|
979
|
-
*
|
|
980
|
-
* @returns The function `getRightMost` returns
|
|
981
|
-
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
1017
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1018
|
+
* starting point for finding the rightmost node in a binary tree. It can be either a root node
|
|
1019
|
+
* (`R`), a key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
|
|
1020
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
1021
|
+
* of iteration to be performed when finding the rightmost node in a binary tree. It can have two
|
|
1022
|
+
* possible values:
|
|
1023
|
+
* @returns The function `getRightMost` returns a NODE object, `null`, or `undefined`.
|
|
982
1024
|
*/
|
|
983
1025
|
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
984
1026
|
if (this.isNIL(beginRoot))
|
|
@@ -1013,10 +1055,10 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1013
1055
|
* Time Complexity: O(log n)
|
|
1014
1056
|
* Space Complexity: O(1)
|
|
1015
1057
|
*
|
|
1016
|
-
* The function returns the predecessor of a given node in a tree.
|
|
1017
|
-
* @param {NODE} node - The parameter
|
|
1058
|
+
* The function returns the predecessor node of a given node in a binary tree.
|
|
1059
|
+
* @param {NODE} node - The parameter "node" is of type "NODE", which represents a node in a binary
|
|
1018
1060
|
* tree.
|
|
1019
|
-
* @returns the predecessor of the given
|
|
1061
|
+
* @returns the predecessor node of the given node.
|
|
1020
1062
|
*/
|
|
1021
1063
|
getPredecessor(node) {
|
|
1022
1064
|
if (this.isRealNode(node.left)) {
|
|
@@ -1042,8 +1084,8 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1042
1084
|
*
|
|
1043
1085
|
* The function `getSuccessor` returns the next node in a binary tree given a current node.
|
|
1044
1086
|
* @param {K | NODE | null} [x] - The parameter `x` can be of type `K`, `NODE`, or `null`.
|
|
1045
|
-
* @returns
|
|
1046
|
-
*
|
|
1087
|
+
* @returns The function `getSuccessor` returns a `NODE` object if a successor exists, `null` if
|
|
1088
|
+
* there is no successor, and `undefined` if the input `x` is not a valid node.
|
|
1047
1089
|
*/
|
|
1048
1090
|
getSuccessor(x) {
|
|
1049
1091
|
x = this.ensureNode(x);
|
|
@@ -1062,30 +1104,29 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1062
1104
|
/**
|
|
1063
1105
|
* Time complexity: O(n)
|
|
1064
1106
|
* Space complexity: O(n)
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
/**
|
|
1107
|
+
*/
|
|
1108
|
+
/**
|
|
1068
1109
|
* Time complexity: O(n)
|
|
1069
1110
|
* Space complexity: O(n)
|
|
1070
1111
|
*
|
|
1071
|
-
* The `dfs` function performs a depth-first search traversal on a binary tree
|
|
1072
|
-
*
|
|
1073
|
-
* callback function
|
|
1074
|
-
*
|
|
1075
|
-
*
|
|
1076
|
-
*
|
|
1077
|
-
*
|
|
1078
|
-
*
|
|
1079
|
-
*
|
|
1080
|
-
*
|
|
1081
|
-
*
|
|
1082
|
-
* @param {IterationType} iterationType - The `iterationType` parameter determines the
|
|
1083
|
-
* iteration to use
|
|
1112
|
+
* The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
|
|
1113
|
+
* function on each node according to a specified pattern and iteration type.
|
|
1114
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
1115
|
+
* visited during the depth-first search. It takes a node as an argument and returns a value. The
|
|
1116
|
+
* return type of the callback function is determined by the generic type `C`.
|
|
1117
|
+
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter determines the order in which the
|
|
1118
|
+
* nodes are visited during the depth-first search. It can have one of the following values:
|
|
1119
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
1120
|
+
* point of the depth-first search. It can be either a node object, a key-value pair, or a key. If it
|
|
1121
|
+
* is a key or key-value pair, the method will find the corresponding node in the tree and start the
|
|
1122
|
+
* search from there.
|
|
1123
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter determines the
|
|
1124
|
+
* type of iteration to use during the depth-first search. It can have two possible values:
|
|
1084
1125
|
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
1085
|
-
* whether
|
|
1086
|
-
* `true`, null
|
|
1087
|
-
*
|
|
1088
|
-
* @returns an array of
|
|
1126
|
+
* whether or not to include null values in the depth-first search traversal. If `includeNull` is set
|
|
1127
|
+
* to `true`, null values will be included in the traversal. If `includeNull` is set to `false`, null
|
|
1128
|
+
* values will
|
|
1129
|
+
* @returns an array of the return types of the callback function.
|
|
1089
1130
|
*/
|
|
1090
1131
|
dfs(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root, iterationType = 'ITERATIVE', includeNull = false) {
|
|
1091
1132
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1201,22 +1242,23 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1201
1242
|
* Time complexity: O(n)
|
|
1202
1243
|
* Space complexity: O(n)
|
|
1203
1244
|
*
|
|
1204
|
-
* The `bfs` function performs a breadth-first search
|
|
1205
|
-
*
|
|
1245
|
+
* The `bfs` function performs a breadth-first search on a binary tree, calling a callback function
|
|
1246
|
+
* on each node and returning an array of the results.
|
|
1206
1247
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1207
|
-
* the breadth-first search traversal. It takes a single
|
|
1248
|
+
* the breadth-first search traversal. It takes a single argument, which is the current node being
|
|
1208
1249
|
* visited, and returns a value of any type.
|
|
1209
|
-
* @param {
|
|
1210
|
-
* starting
|
|
1211
|
-
* or
|
|
1212
|
-
*
|
|
1213
|
-
* @param iterationType - The `iterationType` parameter determines the type of
|
|
1214
|
-
*
|
|
1215
|
-
* @param [includeNull=false] - The `includeNull` parameter is a boolean
|
|
1216
|
-
* to include null values in the breadth-first search traversal. If
|
|
1217
|
-
* `true`, null values will be included in the traversal
|
|
1218
|
-
*
|
|
1219
|
-
*
|
|
1250
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1251
|
+
* starting point of the breadth-first search. It can be either a root node of a tree or a key, node,
|
|
1252
|
+
* or entry object. If no value is provided, the `root` property of the class is used as the default
|
|
1253
|
+
* starting point.
|
|
1254
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
1255
|
+
* iteration to be performed. It can have two possible values:
|
|
1256
|
+
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
1257
|
+
* whether or not to include null values in the breadth-first search (BFS) traversal. If
|
|
1258
|
+
* `includeNull` is set to `true`, null values will be included in the traversal. If `includeNull` is
|
|
1259
|
+
* set to `false
|
|
1260
|
+
* @returns The function `bfs` returns an array of values that are the result of invoking the
|
|
1261
|
+
* `callback` function on each node in the breadth-first order traversal of the binary tree.
|
|
1220
1262
|
*/
|
|
1221
1263
|
bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
1222
1264
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1279,18 +1321,18 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1279
1321
|
* Space complexity: O(n)
|
|
1280
1322
|
*
|
|
1281
1323
|
* The `listLevels` function returns an array of arrays, where each inner array represents a level in
|
|
1282
|
-
* a binary tree and contains the
|
|
1283
|
-
* level.
|
|
1324
|
+
* a binary tree and contains the results of applying a callback function to the nodes at that level.
|
|
1284
1325
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1285
|
-
* the tree. It takes a
|
|
1286
|
-
*
|
|
1287
|
-
* @param {
|
|
1288
|
-
* starting
|
|
1289
|
-
*
|
|
1290
|
-
*
|
|
1291
|
-
*
|
|
1326
|
+
* the tree. It takes a node as an argument and returns a value. The return type of the callback
|
|
1327
|
+
* function is determined by the generic type `C` which extends `BTNCallback<NODE | null>`.
|
|
1328
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1329
|
+
* starting point for traversing the tree. It can be either a root node, a key-value pair, or a node
|
|
1330
|
+
* entry. If no value is provided, the `root` property of the class is used as the default starting
|
|
1331
|
+
* point.
|
|
1332
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
1333
|
+
* iteration to be performed on the binary tree. It can have two possible values:
|
|
1292
1334
|
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
1293
|
-
* whether to include null values in the resulting levels. If `includeNull` is set to `true`,
|
|
1335
|
+
* whether or not to include null values in the resulting levels. If `includeNull` is set to `true`,
|
|
1294
1336
|
* null values will be included in the levels. If `includeNull` is set to `false`, null values will
|
|
1295
1337
|
* be excluded
|
|
1296
1338
|
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
@@ -1355,17 +1397,17 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1355
1397
|
* The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
|
|
1356
1398
|
* algorithm.
|
|
1357
1399
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1358
|
-
* the tree. It takes a single
|
|
1359
|
-
*
|
|
1360
|
-
*
|
|
1361
|
-
*
|
|
1400
|
+
* the tree. It takes a single argument, which is the current node, and can return any value. The
|
|
1401
|
+
* return type of the `callback` function is determined by the `ReturnType<C>` type, which represents
|
|
1402
|
+
* the return
|
|
1403
|
+
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function is used
|
|
1404
|
+
* to specify the order in which the nodes of a binary tree are traversed. It can take one of the
|
|
1362
1405
|
* following values:
|
|
1363
|
-
* @param {
|
|
1364
|
-
* for the traversal. It can be
|
|
1365
|
-
* the root of the tree
|
|
1366
|
-
* @returns The function `morris` returns an array of values that are the
|
|
1367
|
-
*
|
|
1368
|
-
* by the return type of the `callback` function.
|
|
1406
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
1407
|
+
* point for the traversal. It can be either a node object, a key, or an entry object. If no value is
|
|
1408
|
+
* provided, the `root` of the tree is used as the starting point.
|
|
1409
|
+
* @returns The function `morris` returns an array of values that are the return values of the
|
|
1410
|
+
* callback function `callback`.
|
|
1369
1411
|
*/
|
|
1370
1412
|
morris(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root) {
|
|
1371
1413
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1460,8 +1502,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1460
1502
|
* Time complexity: O(n)
|
|
1461
1503
|
* Space complexity: O(n)
|
|
1462
1504
|
*
|
|
1463
|
-
* The `clone` function creates a
|
|
1464
|
-
* the new tree.
|
|
1505
|
+
* The `clone` function creates a deep copy of a tree object.
|
|
1465
1506
|
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
1466
1507
|
*/
|
|
1467
1508
|
clone() {
|
|
@@ -1482,16 +1523,16 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1482
1523
|
* Time Complexity: O(n)
|
|
1483
1524
|
* Space Complexity: O(n)
|
|
1484
1525
|
*
|
|
1485
|
-
* The `filter` function creates a new tree
|
|
1486
|
-
*
|
|
1487
|
-
*
|
|
1488
|
-
*
|
|
1489
|
-
*
|
|
1490
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that
|
|
1491
|
-
*
|
|
1492
|
-
*
|
|
1493
|
-
* @returns The `filter` method is returning a new tree object that contains the
|
|
1494
|
-
*
|
|
1526
|
+
* The `filter` function creates a new tree with entries that pass a given predicate function.
|
|
1527
|
+
* @param predicate - The `predicate` parameter is a callback function that is used to test each
|
|
1528
|
+
* element in the tree. It takes three arguments: `value`, `key`, and `index`. The `value` argument
|
|
1529
|
+
* represents the value of the current element being processed, the `key` argument represents the key
|
|
1530
|
+
* of the
|
|
1531
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
1532
|
+
* specify the value of `this` within the `predicate` function. When the `predicate` function is
|
|
1533
|
+
* called, `thisArg` will be used as the value of `this` within the function. If `thisArg`
|
|
1534
|
+
* @returns The `filter` method is returning a new tree object that contains the entries that pass
|
|
1535
|
+
* the given predicate function.
|
|
1495
1536
|
*/
|
|
1496
1537
|
filter(predicate, thisArg) {
|
|
1497
1538
|
const newTree = this.createTree();
|
|
@@ -1511,15 +1552,15 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1511
1552
|
* Time Complexity: O(n)
|
|
1512
1553
|
* Space Complexity: O(n)
|
|
1513
1554
|
*
|
|
1514
|
-
* The `map` function creates a new tree by applying a callback function to each
|
|
1515
|
-
*
|
|
1516
|
-
* @param callback - The callback parameter is a function that will be called for each
|
|
1517
|
-
*
|
|
1518
|
-
* the
|
|
1519
|
-
*
|
|
1520
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that
|
|
1521
|
-
*
|
|
1522
|
-
*
|
|
1555
|
+
* The `map` function creates a new tree by applying a callback function to each entry in the current
|
|
1556
|
+
* tree.
|
|
1557
|
+
* @param callback - The callback parameter is a function that will be called for each entry in the
|
|
1558
|
+
* tree. It takes three arguments: value, key, and index. The value argument represents the value of
|
|
1559
|
+
* the current entry, the key argument represents the key of the current entry, and the index
|
|
1560
|
+
* argument represents the index of the
|
|
1561
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
1562
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
1563
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
1523
1564
|
* @returns The `map` method is returning a new tree object.
|
|
1524
1565
|
*/
|
|
1525
1566
|
map(callback, thisArg) {
|
|
@@ -1547,11 +1588,15 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1547
1588
|
* Time Complexity: O(n)
|
|
1548
1589
|
* Space Complexity: O(n)
|
|
1549
1590
|
*
|
|
1550
|
-
* The `print` function
|
|
1551
|
-
* @param {
|
|
1552
|
-
*
|
|
1553
|
-
*
|
|
1554
|
-
*
|
|
1591
|
+
* The `print` function in TypeScript prints the binary tree structure with customizable options.
|
|
1592
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
1593
|
+
* point for printing the binary tree. It can be either a node of the binary tree or a key or entry
|
|
1594
|
+
* that exists in the binary tree. If no value is provided, the root of the binary tree will be used
|
|
1595
|
+
* as the starting point.
|
|
1596
|
+
* @param {BinaryTreePrintOptions} [options] - The `options` parameter is an optional object that
|
|
1597
|
+
* allows you to customize the printing behavior. It has the following properties:
|
|
1598
|
+
* @returns Nothing is being returned. The function has a return type of `void`, which means it does
|
|
1599
|
+
* not return any value.
|
|
1555
1600
|
*/
|
|
1556
1601
|
print(beginRoot = this.root, options) {
|
|
1557
1602
|
const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
|
|
@@ -1562,10 +1607,10 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1562
1607
|
console.log(`U for undefined
|
|
1563
1608
|
`);
|
|
1564
1609
|
if (opts.isShowNull)
|
|
1565
|
-
console.log(`
|
|
1610
|
+
console.log(`N for null
|
|
1566
1611
|
`);
|
|
1567
1612
|
if (opts.isShowRedBlackNIL)
|
|
1568
|
-
console.log(`S for Sentinel Node
|
|
1613
|
+
console.log(`S for Sentinel Node(NIL)
|
|
1569
1614
|
`);
|
|
1570
1615
|
const display = (root) => {
|
|
1571
1616
|
const [lines, , ,] = this._displayAux(root, opts);
|
|
@@ -1576,13 +1621,18 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1576
1621
|
display(beginRoot);
|
|
1577
1622
|
}
|
|
1578
1623
|
/**
|
|
1579
|
-
*
|
|
1580
|
-
*
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
*
|
|
1584
|
-
*
|
|
1585
|
-
*
|
|
1624
|
+
* Time Complexity: O(1)
|
|
1625
|
+
* Space Complexity: O(1)
|
|
1626
|
+
*/
|
|
1627
|
+
/**
|
|
1628
|
+
* Time Complexity: O(1)
|
|
1629
|
+
* Space Complexity: O(1)
|
|
1630
|
+
*
|
|
1631
|
+
* The function `_getIterator` is a generator function that returns an iterator for the key-value
|
|
1632
|
+
* pairs in a binary search tree.
|
|
1633
|
+
* @param node - The `node` parameter represents the current node in the binary search tree. It is
|
|
1634
|
+
* initially set to the root node of the tree.
|
|
1635
|
+
* @returns an IterableIterator<[K, V | undefined]>.
|
|
1586
1636
|
*/
|
|
1587
1637
|
*_getIterator(node = this.root) {
|
|
1588
1638
|
if (!node)
|
|
@@ -1591,28 +1641,35 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1591
1641
|
const stack = [];
|
|
1592
1642
|
let current = node;
|
|
1593
1643
|
while (current || stack.length > 0) {
|
|
1594
|
-
while (
|
|
1644
|
+
while (this.isRealNode(current)) {
|
|
1595
1645
|
stack.push(current);
|
|
1596
1646
|
current = current.left;
|
|
1597
1647
|
}
|
|
1598
1648
|
current = stack.pop();
|
|
1599
|
-
if (
|
|
1649
|
+
if (this.isRealNode(current)) {
|
|
1600
1650
|
yield [current.key, current.value];
|
|
1601
1651
|
current = current.right;
|
|
1602
1652
|
}
|
|
1603
1653
|
}
|
|
1604
1654
|
}
|
|
1605
1655
|
else {
|
|
1606
|
-
if (node.left &&
|
|
1656
|
+
if (node.left && this.isRealNode(node)) {
|
|
1607
1657
|
yield* this[Symbol.iterator](node.left);
|
|
1608
1658
|
}
|
|
1609
1659
|
yield [node.key, node.value];
|
|
1610
|
-
if (node.right &&
|
|
1660
|
+
if (node.right && this.isRealNode(node)) {
|
|
1611
1661
|
yield* this[Symbol.iterator](node.right);
|
|
1612
1662
|
}
|
|
1613
1663
|
}
|
|
1614
1664
|
}
|
|
1615
1665
|
/**
|
|
1666
|
+
* Time Complexity: O(n)
|
|
1667
|
+
* Space Complexity: O(n)
|
|
1668
|
+
*/
|
|
1669
|
+
/**
|
|
1670
|
+
* Time Complexity: O(n)
|
|
1671
|
+
* Space Complexity: O(n)
|
|
1672
|
+
*
|
|
1616
1673
|
* The `_displayAux` function is responsible for generating the display layout of a binary tree node,
|
|
1617
1674
|
* taking into account various options such as whether to show null, undefined, or NaN nodes.
|
|
1618
1675
|
* @param {NODE | null | undefined} node - The `node` parameter represents a node in a binary tree.
|
|
@@ -1636,12 +1693,12 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1636
1693
|
else if (node === undefined && !isShowUndefined) {
|
|
1637
1694
|
return emptyDisplayLayout;
|
|
1638
1695
|
}
|
|
1639
|
-
else if (
|
|
1696
|
+
else if (this.isNIL(node) && !isShowRedBlackNIL) {
|
|
1640
1697
|
return emptyDisplayLayout;
|
|
1641
1698
|
}
|
|
1642
1699
|
else if (node !== null && node !== undefined) {
|
|
1643
1700
|
// Display logic of normal nodes
|
|
1644
|
-
const key = node.key, line =
|
|
1701
|
+
const key = node.key, line = this.isNIL(node) ? 'S' : key.toString(), width = line.length;
|
|
1645
1702
|
return _buildNodeDisplay(line, width, this._displayAux(node.left, options), this._displayAux(node.right, options));
|
|
1646
1703
|
}
|
|
1647
1704
|
else {
|
|
@@ -1680,10 +1737,21 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1680
1737
|
}
|
|
1681
1738
|
_DEFAULT_CALLBACK = (node) => (node ? node.key : undefined);
|
|
1682
1739
|
/**
|
|
1683
|
-
*
|
|
1684
|
-
*
|
|
1685
|
-
|
|
1686
|
-
|
|
1740
|
+
* Time Complexity: O(1)
|
|
1741
|
+
* Space Complexity: O(1)
|
|
1742
|
+
*/
|
|
1743
|
+
/**
|
|
1744
|
+
* Time Complexity: O(1)
|
|
1745
|
+
* Space Complexity: O(1)
|
|
1746
|
+
*
|
|
1747
|
+
* The function `_swapProperties` swaps the key-value properties between two nodes.
|
|
1748
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} srcNode - The source node that will be swapped with the
|
|
1749
|
+
* destination node. It can be either an instance of the class `R`, or an object of type
|
|
1750
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
1751
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} destNode - The `destNode` parameter is the node where
|
|
1752
|
+
* the properties will be swapped with the `srcNode`.
|
|
1753
|
+
* @returns either the `destNode` object with its properties swapped with the `srcNode` object's
|
|
1754
|
+
* properties, or `undefined` if either `srcNode` or `destNode` is falsy.
|
|
1687
1755
|
*/
|
|
1688
1756
|
_swapProperties(srcNode, destNode) {
|
|
1689
1757
|
srcNode = this.ensureNode(srcNode);
|
|
@@ -1702,12 +1770,20 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1702
1770
|
return undefined;
|
|
1703
1771
|
}
|
|
1704
1772
|
/**
|
|
1705
|
-
*
|
|
1773
|
+
* Time Complexity: O(1)
|
|
1774
|
+
* Space Complexity: O(1)
|
|
1775
|
+
*/
|
|
1776
|
+
/**
|
|
1777
|
+
* Time Complexity: O(1)
|
|
1778
|
+
* Space Complexity: O(1)
|
|
1779
|
+
*
|
|
1780
|
+
* The function replaces a node in a binary tree with a new node, updating the parent, left child,
|
|
1781
|
+
* right child, and root if necessary.
|
|
1706
1782
|
* @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
|
|
1707
1783
|
* tree.
|
|
1708
1784
|
* @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
1709
1785
|
* tree.
|
|
1710
|
-
* @returns
|
|
1786
|
+
* @returns the newNode.
|
|
1711
1787
|
*/
|
|
1712
1788
|
_replaceNode(oldNode, newNode) {
|
|
1713
1789
|
if (oldNode.parent) {
|
|
@@ -1727,10 +1803,17 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1727
1803
|
return newNode;
|
|
1728
1804
|
}
|
|
1729
1805
|
/**
|
|
1730
|
-
*
|
|
1731
|
-
*
|
|
1732
|
-
|
|
1733
|
-
|
|
1806
|
+
* Time Complexity: O(1)
|
|
1807
|
+
* Space Complexity: O(1)
|
|
1808
|
+
*/
|
|
1809
|
+
/**
|
|
1810
|
+
* Time Complexity: O(1)
|
|
1811
|
+
* Space Complexity: O(1)
|
|
1812
|
+
*
|
|
1813
|
+
* The function sets the root property of an object to the provided value, and also updates the
|
|
1814
|
+
* parent property of the new root.
|
|
1815
|
+
* @param {NODE | null | undefined} v - The parameter `v` is of type `NODE | null | undefined`. This
|
|
1816
|
+
* means that it can accept a value of type `NODE`, `null`, or `undefined`.
|
|
1734
1817
|
*/
|
|
1735
1818
|
_setRoot(v) {
|
|
1736
1819
|
if (v) {
|
|
@@ -1738,6 +1821,23 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1738
1821
|
}
|
|
1739
1822
|
this._root = v;
|
|
1740
1823
|
}
|
|
1824
|
+
/**
|
|
1825
|
+
* Time Complexity: O(1)
|
|
1826
|
+
* Space Complexity: O(1)
|
|
1827
|
+
*/
|
|
1828
|
+
/**
|
|
1829
|
+
* Time Complexity: O(1)
|
|
1830
|
+
* Space Complexity: O(1)
|
|
1831
|
+
*
|
|
1832
|
+
* The function `_ensureCallback` ensures that a callback function is provided and returns it.
|
|
1833
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is of type
|
|
1834
|
+
* `ReturnType<C> | null | undefined`. This means it can accept a value that is the return type of
|
|
1835
|
+
* the generic type `C`, or it can be `null` or `undefined`.
|
|
1836
|
+
* @param {C} callback - The `callback` parameter is a function that takes a `node` as an argument
|
|
1837
|
+
* and returns a value. It is of type `C`, which is a generic type that extends the
|
|
1838
|
+
* `BTNCallback<NODE>` type.
|
|
1839
|
+
* @returns the callback parameter.
|
|
1840
|
+
*/
|
|
1741
1841
|
_ensureCallback(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
1742
1842
|
if ((!callback || callback === this._DEFAULT_CALLBACK) && this.isNode(identifier)) {
|
|
1743
1843
|
callback = (node => node);
|