stack-typed 1.53.9 → 1.54.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
- package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -189
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +133 -357
- package/dist/data-structures/binary-tree/avl-tree.d.ts +108 -78
- package/dist/data-structures/binary-tree/avl-tree.js +126 -79
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +243 -190
- package/dist/data-structures/binary-tree/binary-tree.js +273 -229
- package/dist/data-structures/binary-tree/bst.d.ts +141 -122
- package/dist/data-structures/binary-tree/bst.js +170 -134
- package/dist/data-structures/binary-tree/index.d.ts +2 -0
- package/dist/data-structures/binary-tree/index.js +2 -0
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +84 -80
- package/dist/data-structures/binary-tree/red-black-tree.js +101 -79
- package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
- package/dist/data-structures/binary-tree/tree-counter.js +444 -0
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -186
- package/dist/data-structures/binary-tree/tree-multi-map.js +140 -388
- package/dist/data-structures/graph/directed-graph.d.ts +3 -0
- package/dist/data-structures/graph/directed-graph.js +3 -0
- package/dist/data-structures/graph/map-graph.d.ts +3 -0
- package/dist/data-structures/graph/map-graph.js +3 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
- package/dist/data-structures/graph/undirected-graph.js +3 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +3 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +3 -0
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
- package/dist/data-structures/matrix/matrix.d.ts +3 -0
- package/dist/data-structures/matrix/matrix.js +3 -0
- package/dist/data-structures/matrix/navigator.d.ts +3 -0
- package/dist/data-structures/matrix/navigator.js +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
- package/dist/data-structures/trie/trie.d.ts +0 -4
- package/dist/data-structures/trie/trie.js +0 -4
- package/dist/interfaces/binary-tree.d.ts +7 -6
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.js +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/index.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/index.js +2 -0
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -3
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -3
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +148 -394
- package/src/data-structures/binary-tree/avl-tree.ts +152 -112
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-tree.ts +446 -379
- package/src/data-structures/binary-tree/bst.ts +224 -201
- package/src/data-structures/binary-tree/index.ts +2 -0
- package/src/data-structures/binary-tree/red-black-tree.ts +138 -114
- package/src/data-structures/binary-tree/tree-counter.ts +504 -0
- package/src/data-structures/binary-tree/tree-multi-map.ts +156 -428
- package/src/data-structures/graph/directed-graph.ts +3 -0
- package/src/data-structures/graph/map-graph.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +3 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
- package/src/data-structures/matrix/matrix.ts +3 -0
- package/src/data-structures/matrix/navigator.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
- package/src/data-structures/trie/trie.ts +0 -4
- package/src/interfaces/binary-tree.ts +10 -11
- package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -4
- package/src/types/data-structures/binary-tree/avl-tree.ts +0 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +0 -5
- package/src/types/data-structures/binary-tree/bst.ts +5 -3
- package/src/types/data-structures/binary-tree/index.ts +2 -0
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -4
- package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -4
|
@@ -15,10 +15,24 @@ const common_1 = require("../../common");
|
|
|
15
15
|
/**
|
|
16
16
|
* Represents a node in a binary tree.
|
|
17
17
|
* @template V - The type of data stored in the node.
|
|
18
|
-
* @template
|
|
18
|
+
* @template BinaryTreeNode<K, V> - The type of the family relationship in the binary tree.
|
|
19
19
|
*/
|
|
20
20
|
class BinaryTreeNode {
|
|
21
|
+
/**
|
|
22
|
+
* The constructor function initializes an object with a key and an optional value in TypeScript.
|
|
23
|
+
* @param {K} key - The `key` parameter in the constructor function is used to store the key value
|
|
24
|
+
* for the key-value pair.
|
|
25
|
+
* @param {V} [value] - The `value` parameter in the constructor is optional, meaning it does not
|
|
26
|
+
* have to be provided when creating an instance of the class. If a `value` is not provided, it will
|
|
27
|
+
* default to `undefined`.
|
|
28
|
+
*/
|
|
21
29
|
constructor(key, value) {
|
|
30
|
+
this.parent = undefined;
|
|
31
|
+
this._left = undefined;
|
|
32
|
+
this._right = undefined;
|
|
33
|
+
this._height = 0;
|
|
34
|
+
this._color = 'BLACK';
|
|
35
|
+
this._count = 1;
|
|
22
36
|
this.key = key;
|
|
23
37
|
this.value = value;
|
|
24
38
|
}
|
|
@@ -40,15 +54,32 @@ class BinaryTreeNode {
|
|
|
40
54
|
}
|
|
41
55
|
this._right = v;
|
|
42
56
|
}
|
|
57
|
+
get height() {
|
|
58
|
+
return this._height;
|
|
59
|
+
}
|
|
60
|
+
set height(value) {
|
|
61
|
+
this._height = value;
|
|
62
|
+
}
|
|
63
|
+
get color() {
|
|
64
|
+
return this._color;
|
|
65
|
+
}
|
|
66
|
+
set color(value) {
|
|
67
|
+
this._color = value;
|
|
68
|
+
}
|
|
69
|
+
get count() {
|
|
70
|
+
return this._count;
|
|
71
|
+
}
|
|
72
|
+
set count(value) {
|
|
73
|
+
this._count = value;
|
|
74
|
+
}
|
|
43
75
|
get familyPosition() {
|
|
44
|
-
const that = this;
|
|
45
76
|
if (!this.parent) {
|
|
46
77
|
return this.left || this.right ? 'ROOT' : 'ISOLATED';
|
|
47
78
|
}
|
|
48
|
-
if (this.parent.left ===
|
|
79
|
+
if (this.parent.left === this) {
|
|
49
80
|
return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
|
|
50
81
|
}
|
|
51
|
-
else if (this.parent.right ===
|
|
82
|
+
else if (this.parent.right === this) {
|
|
52
83
|
return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
|
|
53
84
|
}
|
|
54
85
|
return 'MAL_NODE';
|
|
@@ -64,13 +95,13 @@ exports.BinaryTreeNode = BinaryTreeNode;
|
|
|
64
95
|
*/
|
|
65
96
|
class BinaryTree extends base_1.IterableEntryBase {
|
|
66
97
|
/**
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
* @param [options] - The `options` parameter in the constructor is an object that can
|
|
73
|
-
* following properties:
|
|
98
|
+
* This TypeScript constructor function initializes a binary tree with optional options and adds
|
|
99
|
+
* elements based on the provided input.
|
|
100
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
|
|
101
|
+
* iterable that can contain either objects of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. It
|
|
102
|
+
* is used to initialize the binary tree with keys, nodes, entries, or raw data.
|
|
103
|
+
* @param [options] - The `options` parameter in the constructor is an optional object that can
|
|
104
|
+
* contain the following properties:
|
|
74
105
|
*/
|
|
75
106
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
76
107
|
super();
|
|
@@ -122,7 +153,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
122
153
|
* not required to be provided when calling the function. If a `value` is provided, it should be of
|
|
123
154
|
* type `V`, which is the type of the value associated with the node.
|
|
124
155
|
* @returns A new BinaryTreeNode instance with the provided key and value is being returned, casted
|
|
125
|
-
* as
|
|
156
|
+
* as BinaryTreeNode<K, V>.
|
|
126
157
|
*/
|
|
127
158
|
createNode(key, value) {
|
|
128
159
|
return new BinaryTreeNode(key, this._isMapMode ? undefined : value);
|
|
@@ -131,15 +162,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
131
162
|
* Time Complexity: O(1)
|
|
132
163
|
* Space Complexity: O(1)
|
|
133
164
|
*
|
|
134
|
-
* The
|
|
135
|
-
* @param [options] - The `options` parameter in the `createTree`
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
* @returns
|
|
140
|
-
* provided options. The method is creating a new `BinaryTree` object with an empty array as the
|
|
141
|
-
* initial data, and then setting various options such as `iterationType`, `isMapMode`, and
|
|
142
|
-
* `toEntryFn` based on the current object's properties and the provided `options`. Finally, it
|
|
165
|
+
* The function creates a binary tree with the specified options.
|
|
166
|
+
* @param [options] - The `options` parameter in the `createTree` function is an optional parameter
|
|
167
|
+
* that allows you to provide partial configuration options for creating a binary tree. It is of type
|
|
168
|
+
* `Partial<BinaryTreeOptions<K, V, R>>`, which means you can pass in an object containing a subset
|
|
169
|
+
* of properties
|
|
170
|
+
* @returns A new instance of a binary tree with the specified options is being returned.
|
|
143
171
|
*/
|
|
144
172
|
createTree(options) {
|
|
145
173
|
return new BinaryTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, toEntryFn: this._toEntryFn }, options));
|
|
@@ -150,8 +178,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
150
178
|
*
|
|
151
179
|
* The function `ensureNode` in TypeScript checks if a given input is a node, entry, key, or raw
|
|
152
180
|
* value and returns the corresponding node or null.
|
|
153
|
-
* @param {BTNRep<K, V,
|
|
154
|
-
* parameter in the `ensureNode` function can be of type `BTNRep<K, V,
|
|
181
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
182
|
+
* parameter in the `ensureNode` function can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. It
|
|
155
183
|
* is used to determine whether the input is a key, node, entry, or raw data. The
|
|
156
184
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `ensureNode` function
|
|
157
185
|
* is used to specify the type of iteration to be performed. It has a default value of
|
|
@@ -159,49 +187,48 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
159
187
|
* @returns The `ensureNode` function returns either a node, `null`, or `undefined` based on the
|
|
160
188
|
* conditions specified in the code snippet.
|
|
161
189
|
*/
|
|
162
|
-
ensureNode(
|
|
163
|
-
if (
|
|
190
|
+
ensureNode(keyNodeOrEntry, iterationType = this.iterationType) {
|
|
191
|
+
if (keyNodeOrEntry === null)
|
|
164
192
|
return null;
|
|
165
|
-
if (
|
|
193
|
+
if (keyNodeOrEntry === undefined)
|
|
166
194
|
return;
|
|
167
|
-
if (
|
|
195
|
+
if (keyNodeOrEntry === this._NIL)
|
|
168
196
|
return;
|
|
169
|
-
if (this.isNode(
|
|
170
|
-
return
|
|
171
|
-
if (this.isEntry(
|
|
172
|
-
const key =
|
|
197
|
+
if (this.isNode(keyNodeOrEntry))
|
|
198
|
+
return keyNodeOrEntry;
|
|
199
|
+
if (this.isEntry(keyNodeOrEntry)) {
|
|
200
|
+
const key = keyNodeOrEntry[0];
|
|
173
201
|
if (key === null)
|
|
174
202
|
return null;
|
|
175
203
|
if (key === undefined)
|
|
176
204
|
return;
|
|
177
205
|
return this.getNode(key, this._root, iterationType);
|
|
178
206
|
}
|
|
179
|
-
|
|
180
|
-
const [key] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
181
|
-
if (this.isKey(key))
|
|
182
|
-
return this.getNode(key);
|
|
183
|
-
}
|
|
184
|
-
if (this.isKey(keyNodeEntryOrRaw))
|
|
185
|
-
return this.getNode(keyNodeEntryOrRaw, this._root, iterationType);
|
|
186
|
-
return;
|
|
207
|
+
return this.getNode(keyNodeOrEntry, this._root, iterationType);
|
|
187
208
|
}
|
|
188
209
|
/**
|
|
210
|
+
* Time Complexity: O(1)
|
|
211
|
+
* Space Complexity: O(1)
|
|
212
|
+
*
|
|
189
213
|
* The function isNode checks if the input is an instance of BinaryTreeNode.
|
|
190
|
-
* @param {BTNRep<K, V,
|
|
191
|
-
* `
|
|
214
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The parameter
|
|
215
|
+
* `keyNodeOrEntry` can be either a key, a node, an entry, or raw data. The function is
|
|
192
216
|
* checking if the input is an instance of a `BinaryTreeNode` and returning a boolean value
|
|
193
217
|
* accordingly.
|
|
194
|
-
* @returns The function `isNode` is checking if the input `
|
|
218
|
+
* @returns The function `isNode` is checking if the input `keyNodeOrEntry` is an instance of
|
|
195
219
|
* `BinaryTreeNode`. If it is, the function returns `true`, indicating that the input is a node. If
|
|
196
220
|
* it is not an instance of `BinaryTreeNode`, the function returns `false`, indicating that the input
|
|
197
221
|
* is not a node.
|
|
198
222
|
*/
|
|
199
|
-
isNode(
|
|
200
|
-
return
|
|
223
|
+
isNode(keyNodeOrEntry) {
|
|
224
|
+
return keyNodeOrEntry instanceof BinaryTreeNode;
|
|
201
225
|
}
|
|
202
226
|
/**
|
|
227
|
+
* Time Complexity: O(1)
|
|
228
|
+
* Space Complexity: O(1)
|
|
229
|
+
*
|
|
203
230
|
* The function `isRaw` checks if the input parameter is of type `R` by verifying if it is an object.
|
|
204
|
-
* @param {BTNRep<K, V,
|
|
231
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | R} keyNodeEntryOrRaw - BTNRep<K, V, BinaryTreeNode<K, V>>
|
|
205
232
|
* @returns The function `isRaw` is checking if the `keyNodeEntryOrRaw` parameter is of type `R` by
|
|
206
233
|
* checking if it is an object. If the parameter is an object, the function will return `true`,
|
|
207
234
|
* indicating that it is of type `R`.
|
|
@@ -210,88 +237,117 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
210
237
|
return this._toEntryFn !== undefined && typeof keyNodeEntryOrRaw === 'object';
|
|
211
238
|
}
|
|
212
239
|
/**
|
|
240
|
+
* Time Complexity: O(1)
|
|
241
|
+
* Space Complexity: O(1)
|
|
242
|
+
*
|
|
213
243
|
* The function `isRealNode` checks if a given input is a valid node in a binary tree.
|
|
214
|
-
* @param {BTNRep<K, V,
|
|
215
|
-
* parameter in the `isRealNode` function can be of type `BTNRep<K, V,
|
|
216
|
-
* The function checks if the input parameter is a `
|
|
217
|
-
* @returns The function `isRealNode` is checking if the input `
|
|
244
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
245
|
+
* parameter in the `isRealNode` function can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`.
|
|
246
|
+
* The function checks if the input parameter is a `BinaryTreeNode<K, V>` type by verifying if it is not equal
|
|
247
|
+
* @returns The function `isRealNode` is checking if the input `keyNodeOrEntry` is a valid
|
|
218
248
|
* node by comparing it to `this._NIL`, `null`, and `undefined`. If the input is not one of these
|
|
219
249
|
* values, it then calls the `isNode` method to further determine if the input is a node. The
|
|
220
250
|
* function will return a boolean value indicating whether the
|
|
221
251
|
*/
|
|
222
|
-
isRealNode(
|
|
223
|
-
if (
|
|
252
|
+
isRealNode(keyNodeOrEntry) {
|
|
253
|
+
if (keyNodeOrEntry === this._NIL || keyNodeOrEntry === null || keyNodeOrEntry === undefined)
|
|
224
254
|
return false;
|
|
225
|
-
return this.isNode(
|
|
255
|
+
return this.isNode(keyNodeOrEntry);
|
|
226
256
|
}
|
|
227
257
|
/**
|
|
258
|
+
* Time Complexity: O(1)
|
|
259
|
+
* Space Complexity: O(1)
|
|
260
|
+
*
|
|
228
261
|
* The function checks if a given input is a valid node or null.
|
|
229
|
-
* @param {BTNRep<K, V,
|
|
230
|
-
* `
|
|
231
|
-
* V,
|
|
262
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The parameter
|
|
263
|
+
* `keyNodeOrEntry` in the `isRealNodeOrNull` function can be of type `BTNRep<K,
|
|
264
|
+
* V, BinaryTreeNode<K, V>>` or `R`. It is a union type that can either be a key, a node, an entry, or
|
|
232
265
|
* @returns The function `isRealNodeOrNull` is returning a boolean value. It checks if the input
|
|
233
|
-
* `
|
|
266
|
+
* `keyNodeOrEntry` is either `null` or a real node, and returns `true` if it is a node or
|
|
234
267
|
* `null`, and `false` otherwise.
|
|
235
268
|
*/
|
|
236
|
-
isRealNodeOrNull(
|
|
237
|
-
return
|
|
269
|
+
isRealNodeOrNull(keyNodeOrEntry) {
|
|
270
|
+
return keyNodeOrEntry === null || this.isRealNode(keyNodeOrEntry);
|
|
238
271
|
}
|
|
239
272
|
/**
|
|
273
|
+
* Time Complexity: O(1)
|
|
274
|
+
* Space Complexity: O(1)
|
|
275
|
+
*
|
|
240
276
|
* The function isNIL checks if a given key, node, entry, or raw value is equal to the _NIL value.
|
|
241
|
-
* @param {BTNRep<K, V,
|
|
242
|
-
*
|
|
243
|
-
* @returns The function is checking if the `
|
|
277
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - BTNRep<K, V,
|
|
278
|
+
* BinaryTreeNode<K, V>>
|
|
279
|
+
* @returns The function is checking if the `keyNodeOrEntry` parameter is equal to the `_NIL`
|
|
244
280
|
* property of the current object and returning a boolean value based on that comparison.
|
|
245
281
|
*/
|
|
246
|
-
isNIL(
|
|
247
|
-
return
|
|
282
|
+
isNIL(keyNodeOrEntry) {
|
|
283
|
+
return keyNodeOrEntry === this._NIL;
|
|
248
284
|
}
|
|
249
|
-
|
|
250
|
-
|
|
285
|
+
/**
|
|
286
|
+
* Time Complexity: O(1)
|
|
287
|
+
* Space Complexity: O(1)
|
|
288
|
+
*
|
|
289
|
+
* The function `isRange` checks if the input parameter is an instance of the `Range` class.
|
|
290
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>> | Range<K>}
|
|
291
|
+
* keyNodeEntryOrPredicate - The `keyNodeEntryOrPredicate` parameter in the `isRange` function can be
|
|
292
|
+
* of type `BTNRep<K, V, BinaryTreeNode<K, V>>`, `NodePredicate<BinaryTreeNode<K, V>>`, or
|
|
293
|
+
* `Range<K>`. The function checks if the `keyNodeEntry
|
|
294
|
+
* @returns The `isRange` function is checking if the `keyNodeEntryOrPredicate` parameter is an
|
|
295
|
+
* instance of the `Range` class. If it is an instance of `Range`, the function will return `true`,
|
|
296
|
+
* indicating that the parameter is a `Range<K>`. If it is not an instance of `Range`, the function
|
|
297
|
+
* will return `false`.
|
|
298
|
+
*/
|
|
299
|
+
isRange(keyNodeEntryOrPredicate) {
|
|
300
|
+
return keyNodeEntryOrPredicate instanceof common_1.Range;
|
|
251
301
|
}
|
|
252
302
|
/**
|
|
303
|
+
* Time Complexity: O(1)
|
|
304
|
+
* Space Complexity: O(1)
|
|
305
|
+
*
|
|
253
306
|
* The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
|
|
254
307
|
* tree.
|
|
255
|
-
* @param {BTNRep<K, V,
|
|
256
|
-
* `
|
|
308
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The parameter
|
|
309
|
+
* `keyNodeOrEntry` can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. It represents a
|
|
257
310
|
* key, node, entry, or raw data in a binary tree structure. The function `isLeaf` checks whether the
|
|
258
311
|
* provided
|
|
259
312
|
* @returns The function `isLeaf` returns a boolean value indicating whether the input
|
|
260
|
-
* `
|
|
313
|
+
* `keyNodeOrEntry` is a leaf node in a binary tree.
|
|
261
314
|
*/
|
|
262
|
-
isLeaf(
|
|
263
|
-
|
|
264
|
-
if (
|
|
315
|
+
isLeaf(keyNodeOrEntry) {
|
|
316
|
+
keyNodeOrEntry = this.ensureNode(keyNodeOrEntry);
|
|
317
|
+
if (keyNodeOrEntry === undefined)
|
|
265
318
|
return false;
|
|
266
|
-
if (
|
|
319
|
+
if (keyNodeOrEntry === null)
|
|
267
320
|
return true;
|
|
268
|
-
return !this.isRealNode(
|
|
321
|
+
return !this.isRealNode(keyNodeOrEntry.left) && !this.isRealNode(keyNodeOrEntry.right);
|
|
269
322
|
}
|
|
270
323
|
/**
|
|
324
|
+
* Time Complexity: O(1)
|
|
325
|
+
* Space Complexity: O(1)
|
|
326
|
+
*
|
|
271
327
|
* The function `isEntry` checks if the input is a BTNEntry object by verifying if it is an array
|
|
272
328
|
* with a length of 2.
|
|
273
|
-
* @param {BTNRep<K, V,
|
|
274
|
-
* parameter in the `isEntry` function can be of type `BTNRep<K, V,
|
|
275
|
-
* The function checks if the provided `
|
|
276
|
-
* @returns The `isEntry` function is checking if the `
|
|
329
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
330
|
+
* parameter in the `isEntry` function can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or type `R`.
|
|
331
|
+
* The function checks if the provided `keyNodeOrEntry` is of type `BTN
|
|
332
|
+
* @returns The `isEntry` function is checking if the `keyNodeOrEntry` parameter is an array
|
|
277
333
|
* with a length of 2. If it is, then it returns `true`, indicating that the parameter is of type
|
|
278
334
|
* `BTNEntry<K, V>`. If the condition is not met, it returns `false`.
|
|
279
335
|
*/
|
|
280
|
-
isEntry(
|
|
281
|
-
return Array.isArray(
|
|
336
|
+
isEntry(keyNodeOrEntry) {
|
|
337
|
+
return Array.isArray(keyNodeOrEntry) && keyNodeOrEntry.length === 2;
|
|
282
338
|
}
|
|
283
339
|
/**
|
|
284
340
|
* Time Complexity O(1)
|
|
285
341
|
* Space Complexity O(1)
|
|
286
342
|
*
|
|
287
|
-
* The function `
|
|
343
|
+
* The function `isValidKey` checks if a given key is comparable.
|
|
288
344
|
* @param {any} key - The `key` parameter is of type `any`, which means it can be any data type in
|
|
289
345
|
* TypeScript.
|
|
290
|
-
* @returns The function `
|
|
346
|
+
* @returns The function `isValidKey` is checking if the `key` parameter is `null` or if it is comparable.
|
|
291
347
|
* If the `key` is `null`, the function returns `true`. Otherwise, it returns the result of the
|
|
292
348
|
* `isComparable` function, which is not provided in the code snippet.
|
|
293
349
|
*/
|
|
294
|
-
|
|
350
|
+
isValidKey(key) {
|
|
295
351
|
if (key === null)
|
|
296
352
|
return true;
|
|
297
353
|
return (0, utils_1.isComparable)(key);
|
|
@@ -302,8 +358,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
302
358
|
*
|
|
303
359
|
* The `add` function in TypeScript adds a new node to a binary tree while handling duplicate keys
|
|
304
360
|
* and finding the correct insertion position.
|
|
305
|
-
* @param {BTNRep<K, V,
|
|
306
|
-
* seems to be for adding a new node to a binary tree structure. The `
|
|
361
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `add` method you provided
|
|
362
|
+
* seems to be for adding a new node to a binary tree structure. The `keyNodeOrEntry`
|
|
307
363
|
* parameter in the method can accept different types of values:
|
|
308
364
|
* @param {V} [value] - The `value` parameter in the `add` method represents the value associated
|
|
309
365
|
* with the key that you want to add to the binary tree. When adding a key-value pair to the binary
|
|
@@ -313,8 +369,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
313
369
|
* node was successful, and `false` if the insertion position could not be found or if a duplicate
|
|
314
370
|
* key was found and the node was replaced instead of inserted.
|
|
315
371
|
*/
|
|
316
|
-
add(
|
|
317
|
-
const [newNode, newValue] = this.
|
|
372
|
+
add(keyNodeOrEntry, value) {
|
|
373
|
+
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
318
374
|
if (newNode === undefined)
|
|
319
375
|
return false;
|
|
320
376
|
// If the tree is empty, directly set the new node as the root node
|
|
@@ -369,14 +425,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
369
425
|
}
|
|
370
426
|
/**
|
|
371
427
|
* Time Complexity: O(k * n)
|
|
372
|
-
* Space Complexity: O(
|
|
428
|
+
* Space Complexity: O(k)
|
|
373
429
|
*
|
|
374
430
|
* The `addMany` function takes in multiple keys or nodes or entries or raw values along with
|
|
375
431
|
* optional values, and adds them to a data structure while returning an array indicating whether
|
|
376
432
|
* each insertion was successful.
|
|
377
433
|
* @param keysNodesEntriesOrRaws - `keysNodesEntriesOrRaws` is an iterable that can contain a
|
|
378
434
|
* mix of keys, nodes, entries, or raw values. Each element in this iterable can be of type
|
|
379
|
-
* `BTNRep<K, V,
|
|
435
|
+
* `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`.
|
|
380
436
|
* @param [values] - The `values` parameter in the `addMany` function is an optional parameter that
|
|
381
437
|
* accepts an iterable of values. These values correspond to the keys or nodes being added in the
|
|
382
438
|
* `keysNodesEntriesOrRaws` parameter. If provided, the function will iterate over the values and
|
|
@@ -392,7 +448,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
392
448
|
if (values) {
|
|
393
449
|
valuesIterator = values[Symbol.iterator]();
|
|
394
450
|
}
|
|
395
|
-
for (
|
|
451
|
+
for (let keyNodeEntryOrRaw of keysNodesEntriesOrRaws) {
|
|
396
452
|
let value = undefined;
|
|
397
453
|
if (valuesIterator) {
|
|
398
454
|
const valueResult = valuesIterator.next();
|
|
@@ -400,6 +456,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
400
456
|
value = valueResult.value;
|
|
401
457
|
}
|
|
402
458
|
}
|
|
459
|
+
if (this.isRaw(keyNodeEntryOrRaw))
|
|
460
|
+
keyNodeEntryOrRaw = this._toEntryFn(keyNodeEntryOrRaw);
|
|
403
461
|
inserted.push(this.add(keyNodeEntryOrRaw, value));
|
|
404
462
|
}
|
|
405
463
|
return inserted;
|
|
@@ -410,7 +468,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
410
468
|
*
|
|
411
469
|
* The `merge` function in TypeScript merges another binary tree into the current tree by adding all
|
|
412
470
|
* elements from the other tree.
|
|
413
|
-
* @param anotherTree -
|
|
471
|
+
* @param anotherTree - BinaryTree<K, V, R, MK, MV, MR>
|
|
414
472
|
*/
|
|
415
473
|
merge(anotherTree) {
|
|
416
474
|
this.addMany(anotherTree, []);
|
|
@@ -422,7 +480,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
422
480
|
* The `refill` function clears the existing data structure and then adds new key-value pairs based
|
|
423
481
|
* on the provided input.
|
|
424
482
|
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the `refill`
|
|
425
|
-
* method can accept an iterable containing a mix of `BTNRep<K, V,
|
|
483
|
+
* method can accept an iterable containing a mix of `BTNRep<K, V, BinaryTreeNode<K, V>>` objects or `R`
|
|
426
484
|
* objects.
|
|
427
485
|
* @param [values] - The `values` parameter in the `refill` method is an optional parameter that
|
|
428
486
|
* accepts an iterable of values of type `V` or `undefined`.
|
|
@@ -437,7 +495,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
437
495
|
*
|
|
438
496
|
* The function `delete` in TypeScript implements the deletion of a node in a binary tree and returns
|
|
439
497
|
* the deleted node along with information for tree balancing.
|
|
440
|
-
* @param {BTNRep<K, V,
|
|
498
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry
|
|
441
499
|
* - The `delete` method you provided is used to delete a node from a binary tree based on the key,
|
|
442
500
|
* node, entry or raw data. The method returns an array of
|
|
443
501
|
* `BinaryTreeDeleteResult` objects containing information about the deleted node and whether
|
|
@@ -446,11 +504,11 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
446
504
|
* the array contains information about the node that was deleted (`deleted`) and the node that may
|
|
447
505
|
* need to be balanced (`needBalanced`).
|
|
448
506
|
*/
|
|
449
|
-
delete(
|
|
507
|
+
delete(keyNodeOrEntry) {
|
|
450
508
|
const deletedResult = [];
|
|
451
509
|
if (!this._root)
|
|
452
510
|
return deletedResult;
|
|
453
|
-
const curr = this.getNode(
|
|
511
|
+
const curr = this.getNode(keyNodeOrEntry);
|
|
454
512
|
if (!curr)
|
|
455
513
|
return deletedResult;
|
|
456
514
|
const parent = curr === null || curr === void 0 ? void 0 : curr.parent;
|
|
@@ -499,15 +557,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
499
557
|
*
|
|
500
558
|
* The `search` function in TypeScript performs a depth-first or breadth-first search on a tree
|
|
501
559
|
* structure based on a given predicate or key, with options to return multiple results or just one.
|
|
502
|
-
* @param {BTNRep<K, V,
|
|
503
|
-
* `
|
|
560
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The
|
|
561
|
+
* `keyNodeEntryOrPredicate` parameter in the `search` function can accept three types of values:
|
|
504
562
|
* @param [onlyOne=false] - The `onlyOne` parameter in the `search` function is a boolean flag that
|
|
505
563
|
* determines whether the search should stop after finding the first matching node. If `onlyOne` is
|
|
506
564
|
* set to `true`, the search will return as soon as a matching node is found. If `onlyOne` is
|
|
507
565
|
* @param {C} callback - The `callback` parameter in the `search` function is a callback function
|
|
508
566
|
* that will be called on each node that matches the search criteria. It is of type `C`, which
|
|
509
|
-
* extends `NodeCallback<
|
|
510
|
-
* @param {BTNRep<K, V,
|
|
567
|
+
* extends `NodeCallback<BinaryTreeNode<K, V>>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if
|
|
568
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `search` function is
|
|
511
569
|
* used to specify the node from which the search operation should begin. It represents the starting
|
|
512
570
|
* point in the binary tree where the search will be performed. If no specific `startNode` is
|
|
513
571
|
* provided, the search operation will start from the root
|
|
@@ -517,15 +575,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
517
575
|
* @returns The `search` function returns an array of values that match the provided criteria based
|
|
518
576
|
* on the search algorithm implemented within the function.
|
|
519
577
|
*/
|
|
520
|
-
search(
|
|
521
|
-
if (
|
|
578
|
+
search(keyNodeEntryOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
579
|
+
if (keyNodeEntryOrPredicate === undefined)
|
|
522
580
|
return [];
|
|
523
|
-
if (
|
|
581
|
+
if (keyNodeEntryOrPredicate === null)
|
|
524
582
|
return [];
|
|
525
583
|
startNode = this.ensureNode(startNode);
|
|
526
584
|
if (!startNode)
|
|
527
585
|
return [];
|
|
528
|
-
const predicate = this._ensurePredicate(
|
|
586
|
+
const predicate = this._ensurePredicate(keyNodeEntryOrPredicate);
|
|
529
587
|
const ans = [];
|
|
530
588
|
if (iterationType === 'RECURSIVE') {
|
|
531
589
|
const dfs = (cur) => {
|
|
@@ -568,12 +626,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
568
626
|
*
|
|
569
627
|
* The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
|
|
570
628
|
* or predicate, with options for recursive or iterative traversal.
|
|
571
|
-
* @param {BTNRep<K, V,
|
|
629
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
|
|
572
630
|
* - The `getNodes` function you provided takes several parameters:
|
|
573
631
|
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
|
|
574
632
|
* determines whether to return only the first node that matches the criteria specified by the
|
|
575
|
-
* `
|
|
576
|
-
* @param {BTNRep<K, V,
|
|
633
|
+
* `keyNodeEntryOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
|
|
634
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
577
635
|
* `getNodes` function is used to specify the starting point for traversing the binary tree. It
|
|
578
636
|
* represents the root node of the binary tree or the node from which the traversal should begin. If
|
|
579
637
|
* not provided, the default value is set to `this._root
|
|
@@ -583,19 +641,19 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
583
641
|
* @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
|
|
584
642
|
* based on the input parameters and the iteration type specified.
|
|
585
643
|
*/
|
|
586
|
-
getNodes(
|
|
587
|
-
return this.search(
|
|
644
|
+
getNodes(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
645
|
+
return this.search(keyNodeEntryOrPredicate, onlyOne, node => node, startNode, iterationType);
|
|
588
646
|
}
|
|
589
647
|
/**
|
|
590
648
|
* Time Complexity: O(n)
|
|
591
|
-
* Space Complexity: O(log n)
|
|
649
|
+
* Space Complexity: O(log n)
|
|
592
650
|
*
|
|
593
651
|
* The `getNode` function retrieves a node based on the provided key, node, entry, raw data, or
|
|
594
652
|
* predicate.
|
|
595
|
-
* @param {BTNRep<K, V,
|
|
596
|
-
* - The `
|
|
653
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
|
|
654
|
+
* - The `keyNodeEntryOrPredicate` parameter in the `getNode` function can accept a key,
|
|
597
655
|
* node, entry, raw data, or a predicate function.
|
|
598
|
-
* @param {BTNRep<K, V,
|
|
656
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
599
657
|
* `getNode` function is used to specify the starting point for searching for a node in a binary
|
|
600
658
|
* tree. If no specific starting point is provided, the default value is set to `this._root`, which
|
|
601
659
|
* is typically the root node of the binary tree.
|
|
@@ -606,9 +664,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
606
664
|
* @returns The `getNode` function is returning the first node that matches the specified criteria,
|
|
607
665
|
* or `null` if no matching node is found.
|
|
608
666
|
*/
|
|
609
|
-
getNode(
|
|
610
|
-
|
|
611
|
-
return (_a = this.search(keyNodeEntryRawOrPredicate, true, node => node, startNode, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
|
|
667
|
+
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
668
|
+
return this.search(keyNodeEntryOrPredicate, true, node => node, startNode, iterationType)[0];
|
|
612
669
|
}
|
|
613
670
|
/**
|
|
614
671
|
* Time Complexity: O(n)
|
|
@@ -616,10 +673,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
616
673
|
*
|
|
617
674
|
* This function overrides the `get` method to retrieve the value associated with a specified key,
|
|
618
675
|
* node, entry, raw data, or predicate in a data structure.
|
|
619
|
-
* @param {BTNRep<K, V,
|
|
620
|
-
* - The `
|
|
676
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
|
|
677
|
+
* - The `keyNodeEntryOrPredicate` parameter in the `get` method can accept one of the
|
|
621
678
|
* following types:
|
|
622
|
-
* @param {BTNRep<K, V,
|
|
679
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `get`
|
|
623
680
|
* method is used to specify the starting point for searching for a key or node in the binary tree.
|
|
624
681
|
* If no specific starting point is provided, the default starting point is the root of the binary
|
|
625
682
|
* tree (`this._root`).
|
|
@@ -632,15 +689,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
632
689
|
* the method returns the corresponding value. If the key or node is not found, it returns
|
|
633
690
|
* `undefined`.
|
|
634
691
|
*/
|
|
635
|
-
get(
|
|
692
|
+
get(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
636
693
|
var _a;
|
|
637
694
|
if (this._isMapMode) {
|
|
638
|
-
const key = this._extractKey(
|
|
695
|
+
const key = this._extractKey(keyNodeEntryOrPredicate);
|
|
639
696
|
if (key === null || key === undefined)
|
|
640
697
|
return;
|
|
641
698
|
return this._store.get(key);
|
|
642
699
|
}
|
|
643
|
-
return (_a = this.getNode(
|
|
700
|
+
return (_a = this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)) === null || _a === void 0 ? void 0 : _a.value;
|
|
644
701
|
}
|
|
645
702
|
/**
|
|
646
703
|
* Time Complexity: O(n)
|
|
@@ -648,10 +705,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
648
705
|
*
|
|
649
706
|
* The `has` function in TypeScript checks if a specified key, node, entry, raw data, or predicate
|
|
650
707
|
* exists in the data structure.
|
|
651
|
-
* @param {BTNRep<K, V,
|
|
652
|
-
* - The `
|
|
708
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
|
|
709
|
+
* - The `keyNodeEntryOrPredicate` parameter in the `override has` method can accept one of
|
|
653
710
|
* the following types:
|
|
654
|
-
* @param {BTNRep<K, V,
|
|
711
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
655
712
|
* `override` method is used to specify the starting point for the search operation within the data
|
|
656
713
|
* structure. It defaults to `this._root` if not provided explicitly.
|
|
657
714
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `override has` method
|
|
@@ -663,14 +720,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
663
720
|
* are matching nodes, it returns `true`, indicating that the tree contains the specified element.
|
|
664
721
|
* Otherwise, it returns `false`.
|
|
665
722
|
*/
|
|
666
|
-
has(
|
|
667
|
-
return this.search(
|
|
723
|
+
has(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
724
|
+
return this.search(keyNodeEntryOrPredicate, true, node => node, startNode, iterationType).length > 0;
|
|
668
725
|
}
|
|
669
726
|
/**
|
|
670
727
|
* Time Complexity: O(1)
|
|
671
728
|
* Space Complexity: O(1)
|
|
672
729
|
*
|
|
673
|
-
* The
|
|
730
|
+
* The clear function removes nodes and values in map mode.
|
|
674
731
|
*/
|
|
675
732
|
clear() {
|
|
676
733
|
this._clearNodes();
|
|
@@ -695,7 +752,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
695
752
|
*
|
|
696
753
|
* The function checks if a binary tree is perfectly balanced by comparing its minimum height with
|
|
697
754
|
* its height.
|
|
698
|
-
* @param {BTNRep<K, V,
|
|
755
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter is the starting
|
|
699
756
|
* point for checking if the binary tree is perfectly balanced. It represents the root node of the
|
|
700
757
|
* binary tree or a specific node from which the balance check should begin.
|
|
701
758
|
* @returns The method `isPerfectlyBalanced` is returning a boolean value, which indicates whether
|
|
@@ -709,11 +766,11 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
709
766
|
}
|
|
710
767
|
/**
|
|
711
768
|
* Time Complexity: O(n)
|
|
712
|
-
* Space Complexity: O(
|
|
769
|
+
* Space Complexity: O(log n)
|
|
713
770
|
*
|
|
714
771
|
* The function `isBST` in TypeScript checks if a binary search tree is valid using either recursive
|
|
715
772
|
* or iterative methods.
|
|
716
|
-
* @param {BTNRep<K, V,
|
|
773
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `isBST`
|
|
717
774
|
* function represents the starting point for checking whether a binary search tree (BST) is valid.
|
|
718
775
|
* It can be a node in the BST or a reference to the root of the BST. If no specific node is
|
|
719
776
|
* provided, the function will default to
|
|
@@ -769,13 +826,13 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
769
826
|
}
|
|
770
827
|
/**
|
|
771
828
|
* Time Complexity: O(n)
|
|
772
|
-
* Space Complexity: O(
|
|
829
|
+
* Space Complexity: O(log n)
|
|
773
830
|
*
|
|
774
831
|
* The `getDepth` function calculates the depth between two nodes in a binary tree.
|
|
775
|
-
* @param {BTNRep<K, V,
|
|
832
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} dist - The `dist` parameter in the `getDepth`
|
|
776
833
|
* function represents the node or entry in a binary tree map, or a reference to a node in the tree.
|
|
777
834
|
* It is the target node for which you want to calculate the depth from the `startNode` node.
|
|
778
|
-
* @param {BTNRep<K, V,
|
|
835
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
779
836
|
* `getDepth` function represents the starting point from which you want to calculate the depth of a
|
|
780
837
|
* given node or entry in a binary tree. If no specific starting point is provided, the default value
|
|
781
838
|
* for `startNode` is set to the root of the binary
|
|
@@ -798,11 +855,11 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
798
855
|
}
|
|
799
856
|
/**
|
|
800
857
|
* Time Complexity: O(n)
|
|
801
|
-
* Space Complexity: O(
|
|
858
|
+
* Space Complexity: O(log n)
|
|
802
859
|
*
|
|
803
860
|
* The `getHeight` function calculates the maximum height of a binary tree using either a recursive
|
|
804
861
|
* or iterative approach in TypeScript.
|
|
805
|
-
* @param {BTNRep<K, V,
|
|
862
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter is the starting
|
|
806
863
|
* point from which the height of the binary tree will be calculated. It can be a node in the binary
|
|
807
864
|
* tree or a reference to the root of the tree. If not provided, it defaults to the root of the
|
|
808
865
|
* binary tree data structure.
|
|
@@ -847,7 +904,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
847
904
|
*
|
|
848
905
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
849
906
|
* recursive or iterative approach in TypeScript.
|
|
850
|
-
* @param {BTNRep<K, V,
|
|
907
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
851
908
|
* `getMinHeight` function represents the starting node from which the minimum height of the binary
|
|
852
909
|
* tree will be calculated. It is either a node in the binary tree or a reference to the root of the
|
|
853
910
|
* tree. If not provided, the default value is the root
|
|
@@ -913,7 +970,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
913
970
|
* the path to the root. It is expected to be a function that takes a node as an argument and returns
|
|
914
971
|
* a value based on that node. The return type of the callback function is determined by the generic
|
|
915
972
|
* type `C
|
|
916
|
-
* @param {BTNRep<K, V,
|
|
973
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} beginNode - The `beginNode` parameter in the
|
|
917
974
|
* `getPathToRoot` function can be either a key, a node, an entry, or any other value of type `R`.
|
|
918
975
|
* @param [isReverse=true] - The `isReverse` parameter in the `getPathToRoot` function determines
|
|
919
976
|
* whether the resulting path from the given `beginNode` to the root should be in reverse order or
|
|
@@ -938,14 +995,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
938
995
|
}
|
|
939
996
|
/**
|
|
940
997
|
* Time Complexity: O(log n)
|
|
941
|
-
* Space Complexity: O(
|
|
998
|
+
* Space Complexity: O(log n)
|
|
942
999
|
*
|
|
943
1000
|
* The function `getLeftMost` retrieves the leftmost node in a binary tree using either recursive or
|
|
944
1001
|
* tail-recursive iteration.
|
|
945
1002
|
* @param {C} callback - The `callback` parameter is a function that will be called with the leftmost
|
|
946
1003
|
* node of a binary tree or with `undefined` if the tree is empty. It is provided with a default
|
|
947
1004
|
* value of `_DEFAULT_NODE_CALLBACK` if not specified.
|
|
948
|
-
* @param {BTNRep<K, V,
|
|
1005
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
949
1006
|
* `getLeftMost` function represents the starting point for finding the leftmost node in a binary
|
|
950
1007
|
* tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
|
|
951
1008
|
* starting point is provided, the function will default
|
|
@@ -983,15 +1040,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
983
1040
|
}
|
|
984
1041
|
/**
|
|
985
1042
|
* Time Complexity: O(log n)
|
|
986
|
-
* Space Complexity: O(
|
|
1043
|
+
* Space Complexity: O(log n)
|
|
987
1044
|
*
|
|
988
1045
|
* The function `getRightMost` retrieves the rightmost node in a binary tree using either recursive
|
|
989
1046
|
* or iterative traversal methods.
|
|
990
1047
|
* @param {C} callback - The `callback` parameter is a function that will be called with the result
|
|
991
|
-
* of finding the rightmost node in a binary tree. It is of type `NodeCallback<OptNodeOrNull<
|
|
1048
|
+
* of finding the rightmost node in a binary tree. It is of type `NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>`,
|
|
992
1049
|
* which means it is a callback function that can accept either an optional binary tree node or null
|
|
993
1050
|
* as
|
|
994
|
-
* @param {BTNRep<K, V,
|
|
1051
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
995
1052
|
* `getRightMost` function represents the starting point for finding the rightmost node in a binary
|
|
996
1053
|
* tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
|
|
997
1054
|
* starting point is provided, the function will default
|
|
@@ -1029,14 +1086,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1029
1086
|
}
|
|
1030
1087
|
/**
|
|
1031
1088
|
* Time Complexity: O(log n)
|
|
1032
|
-
* Space Complexity: O(
|
|
1089
|
+
* Space Complexity: O(log n)
|
|
1033
1090
|
*
|
|
1034
1091
|
* The function `getPredecessor` in TypeScript returns the predecessor node of a given node in a
|
|
1035
1092
|
* binary tree.
|
|
1036
|
-
* @param {
|
|
1093
|
+
* @param {BinaryTreeNode<K, V>} node - The `getPredecessor` function you provided seems to be attempting to find the
|
|
1037
1094
|
* predecessor of a given node in a binary tree. However, there seems to be a logical issue in the
|
|
1038
1095
|
* while loop condition that might cause an infinite loop.
|
|
1039
|
-
* @returns The `getPredecessor` function returns the predecessor node of the input `
|
|
1096
|
+
* @returns The `getPredecessor` function returns the predecessor node of the input `BinaryTreeNode<K, V>` parameter.
|
|
1040
1097
|
* If the left child of the input node exists, it traverses to the rightmost node of the left subtree
|
|
1041
1098
|
* to find the predecessor. If the left child does not exist, it returns the input node itself.
|
|
1042
1099
|
*/
|
|
@@ -1056,12 +1113,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1056
1113
|
}
|
|
1057
1114
|
/**
|
|
1058
1115
|
* Time Complexity: O(log n)
|
|
1059
|
-
* Space Complexity: O(
|
|
1116
|
+
* Space Complexity: O(log n)
|
|
1060
1117
|
*
|
|
1061
1118
|
* The function `getSuccessor` in TypeScript returns the next node in an in-order traversal of a
|
|
1062
1119
|
* binary tree.
|
|
1063
|
-
* @param {K |
|
|
1064
|
-
* type `K`, `
|
|
1120
|
+
* @param {K | BinaryTreeNode<K, V> | null} [x] - The `getSuccessor` function takes a parameter `x`, which can be of
|
|
1121
|
+
* type `K`, `BinaryTreeNode<K, V>`, or `null`.
|
|
1065
1122
|
* @returns The `getSuccessor` function returns the successor node of the input node `x`. If `x` has
|
|
1066
1123
|
* a right child, the function returns the leftmost node in the right subtree of `x`. If `x` does not
|
|
1067
1124
|
* have a right child, the function traverses up the parent nodes until it finds a node that is not
|
|
@@ -1088,12 +1145,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1088
1145
|
* The function `dfs` performs a depth-first search traversal on a binary tree structure based on the
|
|
1089
1146
|
* specified parameters.
|
|
1090
1147
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends the
|
|
1091
|
-
* `NodeCallback` interface with a type parameter of `OptNodeOrNull<
|
|
1148
|
+
* `NodeCallback` interface with a type parameter of `OptNodeOrNull<BinaryTreeNode<K, V>>`. It has a default value of
|
|
1092
1149
|
* `this._DEFAULT_NODE_CALLBACK as C`.
|
|
1093
1150
|
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `dfs` method specifies the
|
|
1094
1151
|
* order in which the Depth-First Search (DFS) algorithm should traverse the nodes in the tree. The
|
|
1095
1152
|
* possible values for the `pattern` parameter are:
|
|
1096
|
-
* @param {BTNRep<K, V,
|
|
1153
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `dfs`
|
|
1097
1154
|
* method is used to specify the starting point for the Depth-First Search traversal. It can be
|
|
1098
1155
|
* either a `BTNRep` object representing a key, node, or entry in the binary tree map,
|
|
1099
1156
|
* or it can be a
|
|
@@ -1121,8 +1178,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1121
1178
|
* tree, executing a specified callback function on each node visited.
|
|
1122
1179
|
* @param {C} callback - The `callback` parameter in the `bfs` function is a function that will be
|
|
1123
1180
|
* called on each node visited during the breadth-first search traversal. It is a generic type `C`
|
|
1124
|
-
* that extends the `NodeCallback` type, which takes a parameter of type `
|
|
1125
|
-
* @param {BTNRep<K, V,
|
|
1181
|
+
* that extends the `NodeCallback` type, which takes a parameter of type `BinaryTreeNode<K, V>` or `null`.
|
|
1182
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `bfs`
|
|
1126
1183
|
* function represents the starting point for the breadth-first search traversal in a binary tree. It
|
|
1127
1184
|
* can be specified as a key, node, or entry in the binary tree structure. If not provided, the
|
|
1128
1185
|
* default value is the root node of the binary
|
|
@@ -1142,7 +1199,9 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1142
1199
|
return [];
|
|
1143
1200
|
const ans = [];
|
|
1144
1201
|
if (iterationType === 'RECURSIVE') {
|
|
1145
|
-
const queue = new queue_1.Queue([
|
|
1202
|
+
const queue = new queue_1.Queue([
|
|
1203
|
+
startNode
|
|
1204
|
+
]);
|
|
1146
1205
|
const dfs = (level) => {
|
|
1147
1206
|
if (queue.size === 0)
|
|
1148
1207
|
return;
|
|
@@ -1196,7 +1255,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1196
1255
|
* structure based on a specified callback and iteration type.
|
|
1197
1256
|
* @param {C} callback - The `callback` parameter is a function that will be called on each leaf node
|
|
1198
1257
|
* in the binary tree. It is optional and defaults to a default callback function if not provided.
|
|
1199
|
-
* @param {BTNRep<K, V,
|
|
1258
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `leaves`
|
|
1200
1259
|
* method is used to specify the starting point for finding and processing the leaves of a binary
|
|
1201
1260
|
* tree. It can be provided as either a key, a node, or an entry in the binary tree structure. If not
|
|
1202
1261
|
* explicitly provided, the default value
|
|
@@ -1251,7 +1310,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1251
1310
|
* @param {C} callback - The `callback` parameter is a function that will be applied to each node in
|
|
1252
1311
|
* the binary tree during the traversal. It is used to process each node and determine what
|
|
1253
1312
|
* information to include in the output for each level of the tree.
|
|
1254
|
-
* @param {BTNRep<K, V,
|
|
1313
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
1255
1314
|
* `listLevels` function represents the starting point for traversing the binary tree. It can be
|
|
1256
1315
|
* either a key, a node, or an entry in the binary tree. If not provided, the default value is the
|
|
1257
1316
|
* root of the binary tree.
|
|
@@ -1323,11 +1382,11 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1323
1382
|
* Morris Traversal algorithm with different order patterns.
|
|
1324
1383
|
* @param {C} callback - The `callback` parameter in the `morris` function is a function that will be
|
|
1325
1384
|
* called on each node in the binary tree during the traversal. It is of type `C`, which extends the
|
|
1326
|
-
* `NodeCallback<
|
|
1385
|
+
* `NodeCallback<BinaryTreeNode<K, V>>` type. The default value for `callback` is `this._DEFAULT
|
|
1327
1386
|
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function specifies
|
|
1328
1387
|
* the type of Depth-First Search (DFS) order pattern to traverse the binary tree. The possible
|
|
1329
1388
|
* values for the `pattern` parameter are:
|
|
1330
|
-
* @param {BTNRep<K, V,
|
|
1389
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `morris`
|
|
1331
1390
|
* function is the starting point for the Morris traversal algorithm. It represents the root node of
|
|
1332
1391
|
* the binary tree or the node from which the traversal should begin. It can be provided as either a
|
|
1333
1392
|
* key, a node, an entry, or a reference
|
|
@@ -1433,6 +1492,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1433
1492
|
*/
|
|
1434
1493
|
clone() {
|
|
1435
1494
|
const cloned = this.createTree();
|
|
1495
|
+
this._clone(cloned);
|
|
1496
|
+
return cloned;
|
|
1497
|
+
}
|
|
1498
|
+
_clone(cloned) {
|
|
1436
1499
|
this.bfs(node => {
|
|
1437
1500
|
if (node === null)
|
|
1438
1501
|
cloned.add(null);
|
|
@@ -1445,7 +1508,6 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1445
1508
|
}, this._root, this.iterationType, true);
|
|
1446
1509
|
if (this._isMapMode)
|
|
1447
1510
|
cloned._store = this._store;
|
|
1448
|
-
return cloned;
|
|
1449
1511
|
}
|
|
1450
1512
|
/**
|
|
1451
1513
|
* Time Complexity: O(n)
|
|
@@ -1506,7 +1568,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1506
1568
|
*
|
|
1507
1569
|
* The function `toVisual` in TypeScript overrides the visual representation of a binary tree with
|
|
1508
1570
|
* customizable options for displaying undefined, null, and sentinel nodes.
|
|
1509
|
-
* @param {BTNRep<K, V,
|
|
1571
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
1510
1572
|
* `toVisual` method is used to specify the starting point for visualizing the binary tree structure.
|
|
1511
1573
|
* It can be a node, key, entry, or the root of the tree. If no specific starting point is provided,
|
|
1512
1574
|
* the default is set to the root
|
|
@@ -1531,7 +1593,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1531
1593
|
if (opts.isShowRedBlackNIL)
|
|
1532
1594
|
output += `S for Sentinel Node(NIL)\n`;
|
|
1533
1595
|
const display = (root) => {
|
|
1534
|
-
const [lines
|
|
1596
|
+
const [lines] = this._displayAux(root, opts);
|
|
1535
1597
|
let paragraph = '';
|
|
1536
1598
|
for (const line of lines) {
|
|
1537
1599
|
paragraph += line + '\n';
|
|
@@ -1551,7 +1613,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1551
1613
|
* printing options for the binary tree. It is an optional parameter that allows you to customize how
|
|
1552
1614
|
* the binary tree is printed, such as choosing between different traversal orders or formatting
|
|
1553
1615
|
* options.
|
|
1554
|
-
* @param {BTNRep<K, V,
|
|
1616
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
1555
1617
|
* `override print` method is used to specify the starting point for printing the binary tree. It can
|
|
1556
1618
|
* be either a key, a node, an entry, or the root of the tree. If no specific starting point is
|
|
1557
1619
|
* provided, the default value is set to
|
|
@@ -1560,29 +1622,32 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1560
1622
|
console.log(this.toVisual(startNode, options));
|
|
1561
1623
|
}
|
|
1562
1624
|
/**
|
|
1625
|
+
* Time Complexity: O(1)
|
|
1626
|
+
* Space Complexity: O(1)
|
|
1627
|
+
*
|
|
1563
1628
|
* The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
|
|
1564
1629
|
* or returns null.
|
|
1565
|
-
* @param {BTNRep<K, V,
|
|
1566
|
-
* `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `
|
|
1567
|
-
* can be of type `BTNRep<K, V,
|
|
1630
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The
|
|
1631
|
+
* `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeOrEntry`, which
|
|
1632
|
+
* can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. This parameter represents either a key, a
|
|
1568
1633
|
* node, an entry
|
|
1569
1634
|
* @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
|
|
1570
1635
|
* an optional parameter of type `V`. It represents the value associated with the key in the node
|
|
1571
1636
|
* being created. If a `value` is provided, it will be used when creating the node. If
|
|
1572
1637
|
* @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
|
|
1573
|
-
* (`OptNodeOrNull<
|
|
1574
|
-
* input parameter (`
|
|
1638
|
+
* (`OptNodeOrNull<BinaryTreeNode<K, V>>`) based on the input parameters provided. The function checks the type of the
|
|
1639
|
+
* input parameter (`keyNodeOrEntry`) and processes it accordingly to return a node or null
|
|
1575
1640
|
* value.
|
|
1576
1641
|
*/
|
|
1577
|
-
|
|
1578
|
-
if (
|
|
1642
|
+
_keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value) {
|
|
1643
|
+
if (keyNodeOrEntry === undefined)
|
|
1579
1644
|
return [undefined, undefined];
|
|
1580
|
-
if (
|
|
1645
|
+
if (keyNodeOrEntry === null)
|
|
1581
1646
|
return [null, undefined];
|
|
1582
|
-
if (this.isNode(
|
|
1583
|
-
return [
|
|
1584
|
-
if (this.isEntry(
|
|
1585
|
-
const [key, entryValue] =
|
|
1647
|
+
if (this.isNode(keyNodeOrEntry))
|
|
1648
|
+
return [keyNodeOrEntry, value];
|
|
1649
|
+
if (this.isEntry(keyNodeOrEntry)) {
|
|
1650
|
+
const [key, entryValue] = keyNodeOrEntry;
|
|
1586
1651
|
if (key === undefined)
|
|
1587
1652
|
return [undefined, undefined];
|
|
1588
1653
|
else if (key === null)
|
|
@@ -1590,15 +1655,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1590
1655
|
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
1591
1656
|
return [this.createNode(key, finalValue), finalValue];
|
|
1592
1657
|
}
|
|
1593
|
-
|
|
1594
|
-
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
1595
|
-
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
1596
|
-
if (this.isKey(key))
|
|
1597
|
-
return [this.createNode(key, finalValue), finalValue];
|
|
1598
|
-
}
|
|
1599
|
-
if (this.isKey(keyNodeEntryOrRaw))
|
|
1600
|
-
return [this.createNode(keyNodeEntryOrRaw, value), value];
|
|
1601
|
-
return [undefined, undefined];
|
|
1658
|
+
return [this.createNode(keyNodeOrEntry, value), value];
|
|
1602
1659
|
}
|
|
1603
1660
|
/**
|
|
1604
1661
|
* Time complexity: O(n)
|
|
@@ -1608,11 +1665,11 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1608
1665
|
* the specified order pattern and callback function.
|
|
1609
1666
|
* @param {C} callback - The `callback` parameter in the `_dfs` method is a function that will be
|
|
1610
1667
|
* called on each node visited during the depth-first search traversal. It is of type `C`, which
|
|
1611
|
-
* extends `NodeCallback<OptNodeOrNull<
|
|
1668
|
+
* extends `NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>`. The default value for this parameter is `this._DEFAULT
|
|
1612
1669
|
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `_dfs` method specifies the
|
|
1613
1670
|
* order in which the nodes are visited during the Depth-First Search traversal. It can have one of
|
|
1614
1671
|
* the following values:
|
|
1615
|
-
* @param {BTNRep<K, V,
|
|
1672
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `_dfs`
|
|
1616
1673
|
* method is used to specify the starting point for the depth-first search traversal in a binary
|
|
1617
1674
|
* tree. It can be provided as either a `BTNRep` object or a reference to the root node
|
|
1618
1675
|
* of the tree. If no specific
|
|
@@ -1856,12 +1913,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1856
1913
|
* Space Complexity: O(1)
|
|
1857
1914
|
*
|
|
1858
1915
|
* The _swapProperties function swaps key and value properties between two nodes in a binary tree.
|
|
1859
|
-
* @param {BTNRep<K, V,
|
|
1916
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} srcNode - The `srcNode` parameter in the
|
|
1860
1917
|
* `_swapProperties` method can be either a BTNRep object containing key and value
|
|
1861
1918
|
* properties, or it can be of type R.
|
|
1862
|
-
* @param {BTNRep<K, V,
|
|
1919
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} destNode - The `destNode` parameter in the
|
|
1863
1920
|
* `_swapProperties` method represents the node or entry where the properties will be swapped with
|
|
1864
|
-
* the `srcNode`. It can be of type `BTNRep<K, V,
|
|
1921
|
+
* the `srcNode`. It can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. The method ensures that
|
|
1865
1922
|
* both `srcNode
|
|
1866
1923
|
* @returns The `_swapProperties` method returns either the `destNode` with its key and value swapped
|
|
1867
1924
|
* with the `srcNode`, or `undefined` if either `srcNode` or `destNode` is falsy.
|
|
@@ -1889,9 +1946,9 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1889
1946
|
* Space Complexity: O(1)
|
|
1890
1947
|
*
|
|
1891
1948
|
* The _replaceNode function replaces an old node with a new node in a binary tree structure.
|
|
1892
|
-
* @param {
|
|
1949
|
+
* @param {BinaryTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that you want to replace in a
|
|
1893
1950
|
* tree data structure.
|
|
1894
|
-
* @param {
|
|
1951
|
+
* @param {BinaryTreeNode<K, V>} newNode - The `newNode` parameter in the `_replaceNode` function represents the node
|
|
1895
1952
|
* that will replace the `oldNode` in a tree data structure. This function is responsible for
|
|
1896
1953
|
* updating the parent, left child, right child, and root (if necessary) references when replacing a
|
|
1897
1954
|
* node in the tree.
|
|
@@ -1921,8 +1978,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1921
1978
|
*
|
|
1922
1979
|
* The function _setRoot sets the root node of a data structure while updating the parent reference
|
|
1923
1980
|
* of the previous root node.
|
|
1924
|
-
* @param v - The parameter `v` in the `_setRoot` method is of type `OptNodeOrNull<
|
|
1925
|
-
* it can either be an optional `
|
|
1981
|
+
* @param v - The parameter `v` in the `_setRoot` method is of type `OptNodeOrNull<BinaryTreeNode<K, V>>`, which means
|
|
1982
|
+
* it can either be an optional `BinaryTreeNode<K, V>` type or `null`.
|
|
1926
1983
|
*/
|
|
1927
1984
|
_setRoot(v) {
|
|
1928
1985
|
if (v) {
|
|
@@ -1936,30 +1993,24 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1936
1993
|
*
|
|
1937
1994
|
* The function `_ensurePredicate` in TypeScript ensures that the input is converted into a valid
|
|
1938
1995
|
* predicate function for a binary tree node.
|
|
1939
|
-
* @param {BTNRep<K, V,
|
|
1996
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The
|
|
1940
1997
|
* `_ensurePredicate` method in the provided code snippet is responsible for ensuring that the input
|
|
1941
|
-
* parameter `
|
|
1998
|
+
* parameter `keyNodeEntryOrPredicate` is transformed into a valid predicate function that can be
|
|
1942
1999
|
* used for filtering nodes in a binary tree.
|
|
1943
|
-
* @returns A NodePredicate<
|
|
2000
|
+
* @returns A NodePredicate<BinaryTreeNode<K, V>> function is being returned.
|
|
1944
2001
|
*/
|
|
1945
|
-
_ensurePredicate(
|
|
1946
|
-
if (
|
|
2002
|
+
_ensurePredicate(keyNodeEntryOrPredicate) {
|
|
2003
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === undefined)
|
|
1947
2004
|
return (node) => (node ? false : false);
|
|
1948
|
-
if (this._isPredicate(
|
|
1949
|
-
return
|
|
1950
|
-
if (this.isRealNode(
|
|
1951
|
-
return (node) => node ===
|
|
1952
|
-
if (this.isEntry(
|
|
1953
|
-
const [key] =
|
|
1954
|
-
return (node) => node.key === key;
|
|
1955
|
-
}
|
|
1956
|
-
if (this.isKey(keyNodeEntryRawOrPredicate))
|
|
1957
|
-
return (node) => node.key === keyNodeEntryRawOrPredicate;
|
|
1958
|
-
if (this._toEntryFn) {
|
|
1959
|
-
const [key] = this._toEntryFn(keyNodeEntryRawOrPredicate);
|
|
2005
|
+
if (this._isPredicate(keyNodeEntryOrPredicate))
|
|
2006
|
+
return keyNodeEntryOrPredicate;
|
|
2007
|
+
if (this.isRealNode(keyNodeEntryOrPredicate))
|
|
2008
|
+
return (node) => node === keyNodeEntryOrPredicate;
|
|
2009
|
+
if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
2010
|
+
const [key] = keyNodeEntryOrPredicate;
|
|
1960
2011
|
return (node) => node.key === key;
|
|
1961
2012
|
}
|
|
1962
|
-
return (node) => node.key ===
|
|
2013
|
+
return (node) => node.key === keyNodeEntryOrPredicate;
|
|
1963
2014
|
}
|
|
1964
2015
|
/**
|
|
1965
2016
|
* Time Complexity: O(1)
|
|
@@ -1968,7 +2019,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1968
2019
|
* The function `_isPredicate` checks if a given parameter is a function.
|
|
1969
2020
|
* @param {any} p - The parameter `p` is a variable of type `any`, which means it can hold any type
|
|
1970
2021
|
* of value. In this context, the function `_isPredicate` is checking if `p` is a function that
|
|
1971
|
-
* satisfies the type `NodePredicate<
|
|
2022
|
+
* satisfies the type `NodePredicate<BinaryTreeNode<K, V>>`.
|
|
1972
2023
|
* @returns The function is checking if the input `p` is a function and returning a boolean value
|
|
1973
2024
|
* based on that check. If `p` is a function, it will return `true`, indicating that `p` is a
|
|
1974
2025
|
* predicate function for a binary tree node. If `p` is not a function, it will return `false`.
|
|
@@ -1982,32 +2033,25 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1982
2033
|
*
|
|
1983
2034
|
* The function `_extractKey` in TypeScript returns the key from a given input, which can be a node,
|
|
1984
2035
|
* entry, raw data, or null/undefined.
|
|
1985
|
-
* @param {BTNRep<K, V,
|
|
1986
|
-
* TypeScript method that takes in a parameter `
|
|
1987
|
-
* where `BTNRep` is a generic type with keys `K`, `V`, and `
|
|
1988
|
-
* @returns The `_extractKey` method returns the key value extracted from the `
|
|
2036
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `_extractKey` method you provided is a
|
|
2037
|
+
* TypeScript method that takes in a parameter `keyNodeOrEntry` of type `BTNRep<K, V, BinaryTreeNode<K, V>>`,
|
|
2038
|
+
* where `BTNRep` is a generic type with keys `K`, `V`, and `BinaryTreeNode<K, V>`, and `
|
|
2039
|
+
* @returns The `_extractKey` method returns the key value extracted from the `keyNodeOrEntry`
|
|
1989
2040
|
* parameter. The return value can be a key value of type `K`, `null`, or `undefined`, depending on
|
|
1990
2041
|
* the conditions checked in the method.
|
|
1991
2042
|
*/
|
|
1992
|
-
_extractKey(
|
|
1993
|
-
if (
|
|
2043
|
+
_extractKey(keyNodeOrEntry) {
|
|
2044
|
+
if (keyNodeOrEntry === null)
|
|
1994
2045
|
return null;
|
|
1995
|
-
if (
|
|
1996
|
-
return;
|
|
1997
|
-
if (keyNodeEntryOrRaw === this._NIL)
|
|
2046
|
+
if (keyNodeOrEntry === undefined)
|
|
1998
2047
|
return;
|
|
1999
|
-
if (this.
|
|
2000
|
-
return keyNodeEntryOrRaw.key;
|
|
2001
|
-
if (this.isEntry(keyNodeEntryOrRaw))
|
|
2002
|
-
return keyNodeEntryOrRaw[0];
|
|
2003
|
-
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
2004
|
-
if (this._toEntryFn) {
|
|
2005
|
-
const [key] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
2006
|
-
return key;
|
|
2007
|
-
}
|
|
2048
|
+
if (keyNodeOrEntry === this._NIL)
|
|
2008
2049
|
return;
|
|
2009
|
-
|
|
2010
|
-
|
|
2050
|
+
if (this.isNode(keyNodeOrEntry))
|
|
2051
|
+
return keyNodeOrEntry.key;
|
|
2052
|
+
if (this.isEntry(keyNodeOrEntry))
|
|
2053
|
+
return keyNodeOrEntry[0];
|
|
2054
|
+
return keyNodeOrEntry;
|
|
2011
2055
|
}
|
|
2012
2056
|
/**
|
|
2013
2057
|
* Time Complexity: O(1)
|