directed-graph-typed 1.54.0 → 1.54.2
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/LICENSE +1 -1
- package/coverage/lcov-report/index.ts.html +2 -2
- 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 -177
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -340
- package/dist/data-structures/binary-tree/avl-tree.d.ts +102 -57
- package/dist/data-structures/binary-tree/avl-tree.js +110 -47
- 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 +240 -190
- package/dist/data-structures/binary-tree/binary-tree.js +269 -240
- package/dist/data-structures/binary-tree/bst.d.ts +145 -112
- package/dist/data-structures/binary-tree/bst.js +180 -129
- 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 +100 -82
- package/dist/data-structures/binary-tree/red-black-tree.js +115 -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 -174
- package/dist/data-structures/binary-tree/tree-multi-map.js +142 -377
- 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/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/interfaces/binary-tree.d.ts +8 -8
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -4
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -3
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -1
- package/dist/types/data-structures/binary-tree/index.js +3 -1
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.js +2 -0
- 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 +2 -5
- package/package.json +3 -3
- package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +155 -393
- package/src/data-structures/binary-tree/avl-tree.ts +144 -93
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-tree.ts +433 -405
- package/src/data-structures/binary-tree/bst.ts +261 -239
- package/src/data-structures/binary-tree/index.ts +2 -0
- package/src/data-structures/binary-tree/red-black-tree.ts +163 -134
- package/src/data-structures/binary-tree/tree-counter.ts +504 -0
- package/src/data-structures/binary-tree/tree-multi-map.ts +161 -429
- 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/index.ts +2 -2
- package/src/interfaces/binary-tree.ts +10 -24
- 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 -6
- package/src/types/data-structures/binary-tree/avl-tree.ts +0 -5
- package/src/types/data-structures/binary-tree/binary-tree.ts +0 -5
- package/src/types/data-structures/binary-tree/bst.ts +5 -5
- package/src/types/data-structures/binary-tree/index.ts +3 -1
- package/src/types/data-structures/binary-tree/red-black-tree.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -7
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -6
- package/src/types/data-structures/binary-tree/rb-tree.ts +0 -10
- /package/dist/types/data-structures/binary-tree/{rb-tree.js → avl-tree-counter.js} +0 -0
|
@@ -15,10 +15,21 @@ 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;
|
|
22
33
|
this._height = 0;
|
|
23
34
|
this._color = 'BLACK';
|
|
24
35
|
this._count = 1;
|
|
@@ -62,14 +73,13 @@ class BinaryTreeNode {
|
|
|
62
73
|
this._count = value;
|
|
63
74
|
}
|
|
64
75
|
get familyPosition() {
|
|
65
|
-
const that = this;
|
|
66
76
|
if (!this.parent) {
|
|
67
77
|
return this.left || this.right ? 'ROOT' : 'ISOLATED';
|
|
68
78
|
}
|
|
69
|
-
if (this.parent.left ===
|
|
79
|
+
if (this.parent.left === this) {
|
|
70
80
|
return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
|
|
71
81
|
}
|
|
72
|
-
else if (this.parent.right ===
|
|
82
|
+
else if (this.parent.right === this) {
|
|
73
83
|
return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
|
|
74
84
|
}
|
|
75
85
|
return 'MAL_NODE';
|
|
@@ -85,13 +95,13 @@ exports.BinaryTreeNode = BinaryTreeNode;
|
|
|
85
95
|
*/
|
|
86
96
|
class BinaryTree extends base_1.IterableEntryBase {
|
|
87
97
|
/**
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
* @param [options] - The `options` parameter in the constructor is an object that can
|
|
94
|
-
* 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:
|
|
95
105
|
*/
|
|
96
106
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
97
107
|
super();
|
|
@@ -143,12 +153,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
143
153
|
* not required to be provided when calling the function. If a `value` is provided, it should be of
|
|
144
154
|
* type `V`, which is the type of the value associated with the node.
|
|
145
155
|
* @returns A new BinaryTreeNode instance with the provided key and value is being returned, casted
|
|
146
|
-
* as
|
|
156
|
+
* as BinaryTreeNode<K, V>.
|
|
147
157
|
*/
|
|
148
158
|
createNode(key, value) {
|
|
149
159
|
return new BinaryTreeNode(key, this._isMapMode ? undefined : value);
|
|
150
160
|
}
|
|
151
161
|
/**
|
|
162
|
+
* Time Complexity: O(1)
|
|
163
|
+
* Space Complexity: O(1)
|
|
164
|
+
*
|
|
152
165
|
* The function creates a binary tree with the specified options.
|
|
153
166
|
* @param [options] - The `options` parameter in the `createTree` function is an optional parameter
|
|
154
167
|
* that allows you to provide partial configuration options for creating a binary tree. It is of type
|
|
@@ -159,55 +172,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
159
172
|
createTree(options) {
|
|
160
173
|
return new BinaryTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, toEntryFn: this._toEntryFn }, options));
|
|
161
174
|
}
|
|
162
|
-
/**
|
|
163
|
-
* The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
|
|
164
|
-
* or returns null.
|
|
165
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
166
|
-
* `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeEntryOrRaw`, which
|
|
167
|
-
* can be of type `BTNRep<K, V, NODE>` or `R`. This parameter represents either a key, a
|
|
168
|
-
* node, an entry
|
|
169
|
-
* @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
|
|
170
|
-
* an optional parameter of type `V`. It represents the value associated with the key in the node
|
|
171
|
-
* being created. If a `value` is provided, it will be used when creating the node. If
|
|
172
|
-
* @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
|
|
173
|
-
* (`OptNodeOrNull<NODE>`) based on the input parameters provided. The function checks the type of the
|
|
174
|
-
* input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null
|
|
175
|
-
* value.
|
|
176
|
-
*/
|
|
177
|
-
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
|
|
178
|
-
if (keyNodeEntryOrRaw === undefined)
|
|
179
|
-
return [undefined, undefined];
|
|
180
|
-
if (keyNodeEntryOrRaw === null)
|
|
181
|
-
return [null, undefined];
|
|
182
|
-
if (this.isNode(keyNodeEntryOrRaw))
|
|
183
|
-
return [keyNodeEntryOrRaw, value];
|
|
184
|
-
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
185
|
-
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
186
|
-
if (key === undefined)
|
|
187
|
-
return [undefined, undefined];
|
|
188
|
-
else if (key === null)
|
|
189
|
-
return [null, undefined];
|
|
190
|
-
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
191
|
-
return [this.createNode(key, finalValue), finalValue];
|
|
192
|
-
}
|
|
193
|
-
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
194
|
-
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
195
|
-
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
196
|
-
if (this.isKey(key))
|
|
197
|
-
return [this.createNode(key, finalValue), finalValue];
|
|
198
|
-
}
|
|
199
|
-
if (this.isKey(keyNodeEntryOrRaw))
|
|
200
|
-
return [this.createNode(keyNodeEntryOrRaw, value), value];
|
|
201
|
-
return [undefined, undefined];
|
|
202
|
-
}
|
|
203
175
|
/**
|
|
204
176
|
* Time Complexity: O(n)
|
|
205
177
|
* Space Complexity: O(log n)
|
|
206
178
|
*
|
|
207
179
|
* The function `ensureNode` in TypeScript checks if a given input is a node, entry, key, or raw
|
|
208
180
|
* value and returns the corresponding node or null.
|
|
209
|
-
* @param {BTNRep<K, V,
|
|
210
|
-
* 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
|
|
211
183
|
* is used to determine whether the input is a key, node, entry, or raw data. The
|
|
212
184
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `ensureNode` function
|
|
213
185
|
* is used to specify the type of iteration to be performed. It has a default value of
|
|
@@ -215,49 +187,48 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
215
187
|
* @returns The `ensureNode` function returns either a node, `null`, or `undefined` based on the
|
|
216
188
|
* conditions specified in the code snippet.
|
|
217
189
|
*/
|
|
218
|
-
ensureNode(
|
|
219
|
-
if (
|
|
190
|
+
ensureNode(keyNodeOrEntry, iterationType = this.iterationType) {
|
|
191
|
+
if (keyNodeOrEntry === null)
|
|
220
192
|
return null;
|
|
221
|
-
if (
|
|
193
|
+
if (keyNodeOrEntry === undefined)
|
|
222
194
|
return;
|
|
223
|
-
if (
|
|
195
|
+
if (keyNodeOrEntry === this._NIL)
|
|
224
196
|
return;
|
|
225
|
-
if (this.isNode(
|
|
226
|
-
return
|
|
227
|
-
if (this.isEntry(
|
|
228
|
-
const key =
|
|
197
|
+
if (this.isNode(keyNodeOrEntry))
|
|
198
|
+
return keyNodeOrEntry;
|
|
199
|
+
if (this.isEntry(keyNodeOrEntry)) {
|
|
200
|
+
const key = keyNodeOrEntry[0];
|
|
229
201
|
if (key === null)
|
|
230
202
|
return null;
|
|
231
203
|
if (key === undefined)
|
|
232
204
|
return;
|
|
233
205
|
return this.getNode(key, this._root, iterationType);
|
|
234
206
|
}
|
|
235
|
-
|
|
236
|
-
const [key] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
237
|
-
if (this.isKey(key))
|
|
238
|
-
return this.getNode(key);
|
|
239
|
-
}
|
|
240
|
-
if (this.isKey(keyNodeEntryOrRaw))
|
|
241
|
-
return this.getNode(keyNodeEntryOrRaw, this._root, iterationType);
|
|
242
|
-
return;
|
|
207
|
+
return this.getNode(keyNodeOrEntry, this._root, iterationType);
|
|
243
208
|
}
|
|
244
209
|
/**
|
|
210
|
+
* Time Complexity: O(1)
|
|
211
|
+
* Space Complexity: O(1)
|
|
212
|
+
*
|
|
245
213
|
* The function isNode checks if the input is an instance of BinaryTreeNode.
|
|
246
|
-
* @param {BTNRep<K, V,
|
|
247
|
-
* `
|
|
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
|
|
248
216
|
* checking if the input is an instance of a `BinaryTreeNode` and returning a boolean value
|
|
249
217
|
* accordingly.
|
|
250
|
-
* @returns The function `isNode` is checking if the input `
|
|
218
|
+
* @returns The function `isNode` is checking if the input `keyNodeOrEntry` is an instance of
|
|
251
219
|
* `BinaryTreeNode`. If it is, the function returns `true`, indicating that the input is a node. If
|
|
252
220
|
* it is not an instance of `BinaryTreeNode`, the function returns `false`, indicating that the input
|
|
253
221
|
* is not a node.
|
|
254
222
|
*/
|
|
255
|
-
isNode(
|
|
256
|
-
return
|
|
223
|
+
isNode(keyNodeOrEntry) {
|
|
224
|
+
return keyNodeOrEntry instanceof BinaryTreeNode;
|
|
257
225
|
}
|
|
258
226
|
/**
|
|
227
|
+
* Time Complexity: O(1)
|
|
228
|
+
* Space Complexity: O(1)
|
|
229
|
+
*
|
|
259
230
|
* The function `isRaw` checks if the input parameter is of type `R` by verifying if it is an object.
|
|
260
|
-
* @param {BTNRep<K, V,
|
|
231
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | R} keyNodeEntryOrRaw - BTNRep<K, V, BinaryTreeNode<K, V>>
|
|
261
232
|
* @returns The function `isRaw` is checking if the `keyNodeEntryOrRaw` parameter is of type `R` by
|
|
262
233
|
* checking if it is an object. If the parameter is an object, the function will return `true`,
|
|
263
234
|
* indicating that it is of type `R`.
|
|
@@ -266,88 +237,117 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
266
237
|
return this._toEntryFn !== undefined && typeof keyNodeEntryOrRaw === 'object';
|
|
267
238
|
}
|
|
268
239
|
/**
|
|
240
|
+
* Time Complexity: O(1)
|
|
241
|
+
* Space Complexity: O(1)
|
|
242
|
+
*
|
|
269
243
|
* The function `isRealNode` checks if a given input is a valid node in a binary tree.
|
|
270
|
-
* @param {BTNRep<K, V,
|
|
271
|
-
* parameter in the `isRealNode` function can be of type `BTNRep<K, V,
|
|
272
|
-
* The function checks if the input parameter is a `
|
|
273
|
-
* @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
|
|
274
248
|
* node by comparing it to `this._NIL`, `null`, and `undefined`. If the input is not one of these
|
|
275
249
|
* values, it then calls the `isNode` method to further determine if the input is a node. The
|
|
276
250
|
* function will return a boolean value indicating whether the
|
|
277
251
|
*/
|
|
278
|
-
isRealNode(
|
|
279
|
-
if (
|
|
252
|
+
isRealNode(keyNodeOrEntry) {
|
|
253
|
+
if (keyNodeOrEntry === this._NIL || keyNodeOrEntry === null || keyNodeOrEntry === undefined)
|
|
280
254
|
return false;
|
|
281
|
-
return this.isNode(
|
|
255
|
+
return this.isNode(keyNodeOrEntry);
|
|
282
256
|
}
|
|
283
257
|
/**
|
|
258
|
+
* Time Complexity: O(1)
|
|
259
|
+
* Space Complexity: O(1)
|
|
260
|
+
*
|
|
284
261
|
* The function checks if a given input is a valid node or null.
|
|
285
|
-
* @param {BTNRep<K, V,
|
|
286
|
-
* `
|
|
287
|
-
* 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
|
|
288
265
|
* @returns The function `isRealNodeOrNull` is returning a boolean value. It checks if the input
|
|
289
|
-
* `
|
|
266
|
+
* `keyNodeOrEntry` is either `null` or a real node, and returns `true` if it is a node or
|
|
290
267
|
* `null`, and `false` otherwise.
|
|
291
268
|
*/
|
|
292
|
-
isRealNodeOrNull(
|
|
293
|
-
return
|
|
269
|
+
isRealNodeOrNull(keyNodeOrEntry) {
|
|
270
|
+
return keyNodeOrEntry === null || this.isRealNode(keyNodeOrEntry);
|
|
294
271
|
}
|
|
295
272
|
/**
|
|
273
|
+
* Time Complexity: O(1)
|
|
274
|
+
* Space Complexity: O(1)
|
|
275
|
+
*
|
|
296
276
|
* The function isNIL checks if a given key, node, entry, or raw value is equal to the _NIL value.
|
|
297
|
-
* @param {BTNRep<K, V,
|
|
298
|
-
*
|
|
299
|
-
* @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`
|
|
300
280
|
* property of the current object and returning a boolean value based on that comparison.
|
|
301
281
|
*/
|
|
302
|
-
isNIL(
|
|
303
|
-
return
|
|
282
|
+
isNIL(keyNodeOrEntry) {
|
|
283
|
+
return keyNodeOrEntry === this._NIL;
|
|
304
284
|
}
|
|
305
|
-
|
|
306
|
-
|
|
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;
|
|
307
301
|
}
|
|
308
302
|
/**
|
|
303
|
+
* Time Complexity: O(1)
|
|
304
|
+
* Space Complexity: O(1)
|
|
305
|
+
*
|
|
309
306
|
* The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
|
|
310
307
|
* tree.
|
|
311
|
-
* @param {BTNRep<K, V,
|
|
312
|
-
* `
|
|
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
|
|
313
310
|
* key, node, entry, or raw data in a binary tree structure. The function `isLeaf` checks whether the
|
|
314
311
|
* provided
|
|
315
312
|
* @returns The function `isLeaf` returns a boolean value indicating whether the input
|
|
316
|
-
* `
|
|
313
|
+
* `keyNodeOrEntry` is a leaf node in a binary tree.
|
|
317
314
|
*/
|
|
318
|
-
isLeaf(
|
|
319
|
-
|
|
320
|
-
if (
|
|
315
|
+
isLeaf(keyNodeOrEntry) {
|
|
316
|
+
keyNodeOrEntry = this.ensureNode(keyNodeOrEntry);
|
|
317
|
+
if (keyNodeOrEntry === undefined)
|
|
321
318
|
return false;
|
|
322
|
-
if (
|
|
319
|
+
if (keyNodeOrEntry === null)
|
|
323
320
|
return true;
|
|
324
|
-
return !this.isRealNode(
|
|
321
|
+
return !this.isRealNode(keyNodeOrEntry.left) && !this.isRealNode(keyNodeOrEntry.right);
|
|
325
322
|
}
|
|
326
323
|
/**
|
|
324
|
+
* Time Complexity: O(1)
|
|
325
|
+
* Space Complexity: O(1)
|
|
326
|
+
*
|
|
327
327
|
* The function `isEntry` checks if the input is a BTNEntry object by verifying if it is an array
|
|
328
328
|
* with a length of 2.
|
|
329
|
-
* @param {BTNRep<K, V,
|
|
330
|
-
* parameter in the `isEntry` function can be of type `BTNRep<K, V,
|
|
331
|
-
* The function checks if the provided `
|
|
332
|
-
* @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
|
|
333
333
|
* with a length of 2. If it is, then it returns `true`, indicating that the parameter is of type
|
|
334
334
|
* `BTNEntry<K, V>`. If the condition is not met, it returns `false`.
|
|
335
335
|
*/
|
|
336
|
-
isEntry(
|
|
337
|
-
return Array.isArray(
|
|
336
|
+
isEntry(keyNodeOrEntry) {
|
|
337
|
+
return Array.isArray(keyNodeOrEntry) && keyNodeOrEntry.length === 2;
|
|
338
338
|
}
|
|
339
339
|
/**
|
|
340
340
|
* Time Complexity O(1)
|
|
341
341
|
* Space Complexity O(1)
|
|
342
342
|
*
|
|
343
|
-
* The function `
|
|
343
|
+
* The function `isValidKey` checks if a given key is comparable.
|
|
344
344
|
* @param {any} key - The `key` parameter is of type `any`, which means it can be any data type in
|
|
345
345
|
* TypeScript.
|
|
346
|
-
* @returns The function `
|
|
346
|
+
* @returns The function `isValidKey` is checking if the `key` parameter is `null` or if it is comparable.
|
|
347
347
|
* If the `key` is `null`, the function returns `true`. Otherwise, it returns the result of the
|
|
348
348
|
* `isComparable` function, which is not provided in the code snippet.
|
|
349
349
|
*/
|
|
350
|
-
|
|
350
|
+
isValidKey(key) {
|
|
351
351
|
if (key === null)
|
|
352
352
|
return true;
|
|
353
353
|
return (0, utils_1.isComparable)(key);
|
|
@@ -358,8 +358,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
358
358
|
*
|
|
359
359
|
* The `add` function in TypeScript adds a new node to a binary tree while handling duplicate keys
|
|
360
360
|
* and finding the correct insertion position.
|
|
361
|
-
* @param {BTNRep<K, V,
|
|
362
|
-
* 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`
|
|
363
363
|
* parameter in the method can accept different types of values:
|
|
364
364
|
* @param {V} [value] - The `value` parameter in the `add` method represents the value associated
|
|
365
365
|
* with the key that you want to add to the binary tree. When adding a key-value pair to the binary
|
|
@@ -369,8 +369,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
369
369
|
* node was successful, and `false` if the insertion position could not be found or if a duplicate
|
|
370
370
|
* key was found and the node was replaced instead of inserted.
|
|
371
371
|
*/
|
|
372
|
-
add(
|
|
373
|
-
const [newNode, newValue] = this.
|
|
372
|
+
add(keyNodeOrEntry, value) {
|
|
373
|
+
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
374
374
|
if (newNode === undefined)
|
|
375
375
|
return false;
|
|
376
376
|
// If the tree is empty, directly set the new node as the root node
|
|
@@ -425,14 +425,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
425
425
|
}
|
|
426
426
|
/**
|
|
427
427
|
* Time Complexity: O(k * n)
|
|
428
|
-
* Space Complexity: O(
|
|
428
|
+
* Space Complexity: O(k)
|
|
429
429
|
*
|
|
430
430
|
* The `addMany` function takes in multiple keys or nodes or entries or raw values along with
|
|
431
431
|
* optional values, and adds them to a data structure while returning an array indicating whether
|
|
432
432
|
* each insertion was successful.
|
|
433
433
|
* @param keysNodesEntriesOrRaws - `keysNodesEntriesOrRaws` is an iterable that can contain a
|
|
434
434
|
* mix of keys, nodes, entries, or raw values. Each element in this iterable can be of type
|
|
435
|
-
* `BTNRep<K, V,
|
|
435
|
+
* `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`.
|
|
436
436
|
* @param [values] - The `values` parameter in the `addMany` function is an optional parameter that
|
|
437
437
|
* accepts an iterable of values. These values correspond to the keys or nodes being added in the
|
|
438
438
|
* `keysNodesEntriesOrRaws` parameter. If provided, the function will iterate over the values and
|
|
@@ -448,7 +448,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
448
448
|
if (values) {
|
|
449
449
|
valuesIterator = values[Symbol.iterator]();
|
|
450
450
|
}
|
|
451
|
-
for (
|
|
451
|
+
for (let keyNodeEntryOrRaw of keysNodesEntriesOrRaws) {
|
|
452
452
|
let value = undefined;
|
|
453
453
|
if (valuesIterator) {
|
|
454
454
|
const valueResult = valuesIterator.next();
|
|
@@ -456,6 +456,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
456
456
|
value = valueResult.value;
|
|
457
457
|
}
|
|
458
458
|
}
|
|
459
|
+
if (this.isRaw(keyNodeEntryOrRaw))
|
|
460
|
+
keyNodeEntryOrRaw = this._toEntryFn(keyNodeEntryOrRaw);
|
|
459
461
|
inserted.push(this.add(keyNodeEntryOrRaw, value));
|
|
460
462
|
}
|
|
461
463
|
return inserted;
|
|
@@ -466,7 +468,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
466
468
|
*
|
|
467
469
|
* The `merge` function in TypeScript merges another binary tree into the current tree by adding all
|
|
468
470
|
* elements from the other tree.
|
|
469
|
-
* @param anotherTree -
|
|
471
|
+
* @param anotherTree - BinaryTree<K, V, R, MK, MV, MR>
|
|
470
472
|
*/
|
|
471
473
|
merge(anotherTree) {
|
|
472
474
|
this.addMany(anotherTree, []);
|
|
@@ -478,7 +480,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
478
480
|
* The `refill` function clears the existing data structure and then adds new key-value pairs based
|
|
479
481
|
* on the provided input.
|
|
480
482
|
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the `refill`
|
|
481
|
-
* 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`
|
|
482
484
|
* objects.
|
|
483
485
|
* @param [values] - The `values` parameter in the `refill` method is an optional parameter that
|
|
484
486
|
* accepts an iterable of values of type `V` or `undefined`.
|
|
@@ -493,7 +495,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
493
495
|
*
|
|
494
496
|
* The function `delete` in TypeScript implements the deletion of a node in a binary tree and returns
|
|
495
497
|
* the deleted node along with information for tree balancing.
|
|
496
|
-
* @param {BTNRep<K, V,
|
|
498
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry
|
|
497
499
|
* - The `delete` method you provided is used to delete a node from a binary tree based on the key,
|
|
498
500
|
* node, entry or raw data. The method returns an array of
|
|
499
501
|
* `BinaryTreeDeleteResult` objects containing information about the deleted node and whether
|
|
@@ -502,11 +504,11 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
502
504
|
* the array contains information about the node that was deleted (`deleted`) and the node that may
|
|
503
505
|
* need to be balanced (`needBalanced`).
|
|
504
506
|
*/
|
|
505
|
-
delete(
|
|
507
|
+
delete(keyNodeOrEntry) {
|
|
506
508
|
const deletedResult = [];
|
|
507
509
|
if (!this._root)
|
|
508
510
|
return deletedResult;
|
|
509
|
-
const curr = this.getNode(
|
|
511
|
+
const curr = this.getNode(keyNodeOrEntry);
|
|
510
512
|
if (!curr)
|
|
511
513
|
return deletedResult;
|
|
512
514
|
const parent = curr === null || curr === void 0 ? void 0 : curr.parent;
|
|
@@ -555,15 +557,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
555
557
|
*
|
|
556
558
|
* The `search` function in TypeScript performs a depth-first or breadth-first search on a tree
|
|
557
559
|
* structure based on a given predicate or key, with options to return multiple results or just one.
|
|
558
|
-
* @param {BTNRep<K, V,
|
|
559
|
-
* `
|
|
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:
|
|
560
562
|
* @param [onlyOne=false] - The `onlyOne` parameter in the `search` function is a boolean flag that
|
|
561
563
|
* determines whether the search should stop after finding the first matching node. If `onlyOne` is
|
|
562
564
|
* set to `true`, the search will return as soon as a matching node is found. If `onlyOne` is
|
|
563
565
|
* @param {C} callback - The `callback` parameter in the `search` function is a callback function
|
|
564
566
|
* that will be called on each node that matches the search criteria. It is of type `C`, which
|
|
565
|
-
* extends `NodeCallback<
|
|
566
|
-
* @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
|
|
567
569
|
* used to specify the node from which the search operation should begin. It represents the starting
|
|
568
570
|
* point in the binary tree where the search will be performed. If no specific `startNode` is
|
|
569
571
|
* provided, the search operation will start from the root
|
|
@@ -573,15 +575,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
573
575
|
* @returns The `search` function returns an array of values that match the provided criteria based
|
|
574
576
|
* on the search algorithm implemented within the function.
|
|
575
577
|
*/
|
|
576
|
-
search(
|
|
577
|
-
if (
|
|
578
|
+
search(keyNodeEntryOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
579
|
+
if (keyNodeEntryOrPredicate === undefined)
|
|
578
580
|
return [];
|
|
579
|
-
if (
|
|
581
|
+
if (keyNodeEntryOrPredicate === null)
|
|
580
582
|
return [];
|
|
581
583
|
startNode = this.ensureNode(startNode);
|
|
582
584
|
if (!startNode)
|
|
583
585
|
return [];
|
|
584
|
-
const predicate = this._ensurePredicate(
|
|
586
|
+
const predicate = this._ensurePredicate(keyNodeEntryOrPredicate);
|
|
585
587
|
const ans = [];
|
|
586
588
|
if (iterationType === 'RECURSIVE') {
|
|
587
589
|
const dfs = (cur) => {
|
|
@@ -624,12 +626,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
624
626
|
*
|
|
625
627
|
* The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
|
|
626
628
|
* or predicate, with options for recursive or iterative traversal.
|
|
627
|
-
* @param {BTNRep<K, V,
|
|
629
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
|
|
628
630
|
* - The `getNodes` function you provided takes several parameters:
|
|
629
631
|
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
|
|
630
632
|
* determines whether to return only the first node that matches the criteria specified by the
|
|
631
|
-
* `
|
|
632
|
-
* @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
|
|
633
635
|
* `getNodes` function is used to specify the starting point for traversing the binary tree. It
|
|
634
636
|
* represents the root node of the binary tree or the node from which the traversal should begin. If
|
|
635
637
|
* not provided, the default value is set to `this._root
|
|
@@ -639,19 +641,19 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
639
641
|
* @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
|
|
640
642
|
* based on the input parameters and the iteration type specified.
|
|
641
643
|
*/
|
|
642
|
-
getNodes(
|
|
643
|
-
return this.search(
|
|
644
|
+
getNodes(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
645
|
+
return this.search(keyNodeEntryOrPredicate, onlyOne, node => node, startNode, iterationType);
|
|
644
646
|
}
|
|
645
647
|
/**
|
|
646
648
|
* Time Complexity: O(n)
|
|
647
|
-
* Space Complexity: O(log n)
|
|
649
|
+
* Space Complexity: O(log n)
|
|
648
650
|
*
|
|
649
651
|
* The `getNode` function retrieves a node based on the provided key, node, entry, raw data, or
|
|
650
652
|
* predicate.
|
|
651
|
-
* @param {BTNRep<K, V,
|
|
652
|
-
* - 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,
|
|
653
655
|
* node, entry, raw data, or a predicate function.
|
|
654
|
-
* @param {BTNRep<K, V,
|
|
656
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
655
657
|
* `getNode` function is used to specify the starting point for searching for a node in a binary
|
|
656
658
|
* tree. If no specific starting point is provided, the default value is set to `this._root`, which
|
|
657
659
|
* is typically the root node of the binary tree.
|
|
@@ -662,9 +664,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
662
664
|
* @returns The `getNode` function is returning the first node that matches the specified criteria,
|
|
663
665
|
* or `null` if no matching node is found.
|
|
664
666
|
*/
|
|
665
|
-
getNode(
|
|
666
|
-
|
|
667
|
-
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];
|
|
668
669
|
}
|
|
669
670
|
/**
|
|
670
671
|
* Time Complexity: O(n)
|
|
@@ -672,10 +673,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
672
673
|
*
|
|
673
674
|
* This function overrides the `get` method to retrieve the value associated with a specified key,
|
|
674
675
|
* node, entry, raw data, or predicate in a data structure.
|
|
675
|
-
* @param {BTNRep<K, V,
|
|
676
|
-
* - 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
|
|
677
678
|
* following types:
|
|
678
|
-
* @param {BTNRep<K, V,
|
|
679
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `get`
|
|
679
680
|
* method is used to specify the starting point for searching for a key or node in the binary tree.
|
|
680
681
|
* If no specific starting point is provided, the default starting point is the root of the binary
|
|
681
682
|
* tree (`this._root`).
|
|
@@ -688,15 +689,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
688
689
|
* the method returns the corresponding value. If the key or node is not found, it returns
|
|
689
690
|
* `undefined`.
|
|
690
691
|
*/
|
|
691
|
-
get(
|
|
692
|
+
get(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
692
693
|
var _a;
|
|
693
694
|
if (this._isMapMode) {
|
|
694
|
-
const key = this._extractKey(
|
|
695
|
+
const key = this._extractKey(keyNodeEntryOrPredicate);
|
|
695
696
|
if (key === null || key === undefined)
|
|
696
697
|
return;
|
|
697
698
|
return this._store.get(key);
|
|
698
699
|
}
|
|
699
|
-
return (_a = this.getNode(
|
|
700
|
+
return (_a = this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)) === null || _a === void 0 ? void 0 : _a.value;
|
|
700
701
|
}
|
|
701
702
|
/**
|
|
702
703
|
* Time Complexity: O(n)
|
|
@@ -704,10 +705,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
704
705
|
*
|
|
705
706
|
* The `has` function in TypeScript checks if a specified key, node, entry, raw data, or predicate
|
|
706
707
|
* exists in the data structure.
|
|
707
|
-
* @param {BTNRep<K, V,
|
|
708
|
-
* - 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
|
|
709
710
|
* the following types:
|
|
710
|
-
* @param {BTNRep<K, V,
|
|
711
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
711
712
|
* `override` method is used to specify the starting point for the search operation within the data
|
|
712
713
|
* structure. It defaults to `this._root` if not provided explicitly.
|
|
713
714
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `override has` method
|
|
@@ -719,14 +720,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
719
720
|
* are matching nodes, it returns `true`, indicating that the tree contains the specified element.
|
|
720
721
|
* Otherwise, it returns `false`.
|
|
721
722
|
*/
|
|
722
|
-
has(
|
|
723
|
-
return this.search(
|
|
723
|
+
has(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
724
|
+
return this.search(keyNodeEntryOrPredicate, true, node => node, startNode, iterationType).length > 0;
|
|
724
725
|
}
|
|
725
726
|
/**
|
|
726
727
|
* Time Complexity: O(1)
|
|
727
728
|
* Space Complexity: O(1)
|
|
728
729
|
*
|
|
729
|
-
* The
|
|
730
|
+
* The clear function removes nodes and values in map mode.
|
|
730
731
|
*/
|
|
731
732
|
clear() {
|
|
732
733
|
this._clearNodes();
|
|
@@ -751,7 +752,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
751
752
|
*
|
|
752
753
|
* The function checks if a binary tree is perfectly balanced by comparing its minimum height with
|
|
753
754
|
* its height.
|
|
754
|
-
* @param {BTNRep<K, V,
|
|
755
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter is the starting
|
|
755
756
|
* point for checking if the binary tree is perfectly balanced. It represents the root node of the
|
|
756
757
|
* binary tree or a specific node from which the balance check should begin.
|
|
757
758
|
* @returns The method `isPerfectlyBalanced` is returning a boolean value, which indicates whether
|
|
@@ -765,11 +766,11 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
765
766
|
}
|
|
766
767
|
/**
|
|
767
768
|
* Time Complexity: O(n)
|
|
768
|
-
* Space Complexity: O(
|
|
769
|
+
* Space Complexity: O(log n)
|
|
769
770
|
*
|
|
770
771
|
* The function `isBST` in TypeScript checks if a binary search tree is valid using either recursive
|
|
771
772
|
* or iterative methods.
|
|
772
|
-
* @param {BTNRep<K, V,
|
|
773
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `isBST`
|
|
773
774
|
* function represents the starting point for checking whether a binary search tree (BST) is valid.
|
|
774
775
|
* It can be a node in the BST or a reference to the root of the BST. If no specific node is
|
|
775
776
|
* provided, the function will default to
|
|
@@ -825,13 +826,13 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
825
826
|
}
|
|
826
827
|
/**
|
|
827
828
|
* Time Complexity: O(n)
|
|
828
|
-
* Space Complexity: O(
|
|
829
|
+
* Space Complexity: O(log n)
|
|
829
830
|
*
|
|
830
831
|
* The `getDepth` function calculates the depth between two nodes in a binary tree.
|
|
831
|
-
* @param {BTNRep<K, V,
|
|
832
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} dist - The `dist` parameter in the `getDepth`
|
|
832
833
|
* function represents the node or entry in a binary tree map, or a reference to a node in the tree.
|
|
833
834
|
* It is the target node for which you want to calculate the depth from the `startNode` node.
|
|
834
|
-
* @param {BTNRep<K, V,
|
|
835
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
835
836
|
* `getDepth` function represents the starting point from which you want to calculate the depth of a
|
|
836
837
|
* given node or entry in a binary tree. If no specific starting point is provided, the default value
|
|
837
838
|
* for `startNode` is set to the root of the binary
|
|
@@ -854,11 +855,11 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
854
855
|
}
|
|
855
856
|
/**
|
|
856
857
|
* Time Complexity: O(n)
|
|
857
|
-
* Space Complexity: O(
|
|
858
|
+
* Space Complexity: O(log n)
|
|
858
859
|
*
|
|
859
860
|
* The `getHeight` function calculates the maximum height of a binary tree using either a recursive
|
|
860
861
|
* or iterative approach in TypeScript.
|
|
861
|
-
* @param {BTNRep<K, V,
|
|
862
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter is the starting
|
|
862
863
|
* point from which the height of the binary tree will be calculated. It can be a node in the binary
|
|
863
864
|
* tree or a reference to the root of the tree. If not provided, it defaults to the root of the
|
|
864
865
|
* binary tree data structure.
|
|
@@ -903,7 +904,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
903
904
|
*
|
|
904
905
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
905
906
|
* recursive or iterative approach in TypeScript.
|
|
906
|
-
* @param {BTNRep<K, V,
|
|
907
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
907
908
|
* `getMinHeight` function represents the starting node from which the minimum height of the binary
|
|
908
909
|
* tree will be calculated. It is either a node in the binary tree or a reference to the root of the
|
|
909
910
|
* tree. If not provided, the default value is the root
|
|
@@ -969,7 +970,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
969
970
|
* the path to the root. It is expected to be a function that takes a node as an argument and returns
|
|
970
971
|
* a value based on that node. The return type of the callback function is determined by the generic
|
|
971
972
|
* type `C
|
|
972
|
-
* @param {BTNRep<K, V,
|
|
973
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} beginNode - The `beginNode` parameter in the
|
|
973
974
|
* `getPathToRoot` function can be either a key, a node, an entry, or any other value of type `R`.
|
|
974
975
|
* @param [isReverse=true] - The `isReverse` parameter in the `getPathToRoot` function determines
|
|
975
976
|
* whether the resulting path from the given `beginNode` to the root should be in reverse order or
|
|
@@ -994,14 +995,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
994
995
|
}
|
|
995
996
|
/**
|
|
996
997
|
* Time Complexity: O(log n)
|
|
997
|
-
* Space Complexity: O(
|
|
998
|
+
* Space Complexity: O(log n)
|
|
998
999
|
*
|
|
999
1000
|
* The function `getLeftMost` retrieves the leftmost node in a binary tree using either recursive or
|
|
1000
1001
|
* tail-recursive iteration.
|
|
1001
1002
|
* @param {C} callback - The `callback` parameter is a function that will be called with the leftmost
|
|
1002
1003
|
* node of a binary tree or with `undefined` if the tree is empty. It is provided with a default
|
|
1003
1004
|
* value of `_DEFAULT_NODE_CALLBACK` if not specified.
|
|
1004
|
-
* @param {BTNRep<K, V,
|
|
1005
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
1005
1006
|
* `getLeftMost` function represents the starting point for finding the leftmost node in a binary
|
|
1006
1007
|
* tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
|
|
1007
1008
|
* starting point is provided, the function will default
|
|
@@ -1039,15 +1040,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1039
1040
|
}
|
|
1040
1041
|
/**
|
|
1041
1042
|
* Time Complexity: O(log n)
|
|
1042
|
-
* Space Complexity: O(
|
|
1043
|
+
* Space Complexity: O(log n)
|
|
1043
1044
|
*
|
|
1044
1045
|
* The function `getRightMost` retrieves the rightmost node in a binary tree using either recursive
|
|
1045
1046
|
* or iterative traversal methods.
|
|
1046
1047
|
* @param {C} callback - The `callback` parameter is a function that will be called with the result
|
|
1047
|
-
* 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>>>`,
|
|
1048
1049
|
* which means it is a callback function that can accept either an optional binary tree node or null
|
|
1049
1050
|
* as
|
|
1050
|
-
* @param {BTNRep<K, V,
|
|
1051
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
1051
1052
|
* `getRightMost` function represents the starting point for finding the rightmost node in a binary
|
|
1052
1053
|
* tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
|
|
1053
1054
|
* starting point is provided, the function will default
|
|
@@ -1085,14 +1086,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1085
1086
|
}
|
|
1086
1087
|
/**
|
|
1087
1088
|
* Time Complexity: O(log n)
|
|
1088
|
-
* Space Complexity: O(
|
|
1089
|
+
* Space Complexity: O(log n)
|
|
1089
1090
|
*
|
|
1090
1091
|
* The function `getPredecessor` in TypeScript returns the predecessor node of a given node in a
|
|
1091
1092
|
* binary tree.
|
|
1092
|
-
* @param {
|
|
1093
|
+
* @param {BinaryTreeNode<K, V>} node - The `getPredecessor` function you provided seems to be attempting to find the
|
|
1093
1094
|
* predecessor of a given node in a binary tree. However, there seems to be a logical issue in the
|
|
1094
1095
|
* while loop condition that might cause an infinite loop.
|
|
1095
|
-
* @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.
|
|
1096
1097
|
* If the left child of the input node exists, it traverses to the rightmost node of the left subtree
|
|
1097
1098
|
* to find the predecessor. If the left child does not exist, it returns the input node itself.
|
|
1098
1099
|
*/
|
|
@@ -1112,12 +1113,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1112
1113
|
}
|
|
1113
1114
|
/**
|
|
1114
1115
|
* Time Complexity: O(log n)
|
|
1115
|
-
* Space Complexity: O(
|
|
1116
|
+
* Space Complexity: O(log n)
|
|
1116
1117
|
*
|
|
1117
1118
|
* The function `getSuccessor` in TypeScript returns the next node in an in-order traversal of a
|
|
1118
1119
|
* binary tree.
|
|
1119
|
-
* @param {K |
|
|
1120
|
-
* 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`.
|
|
1121
1122
|
* @returns The `getSuccessor` function returns the successor node of the input node `x`. If `x` has
|
|
1122
1123
|
* a right child, the function returns the leftmost node in the right subtree of `x`. If `x` does not
|
|
1123
1124
|
* have a right child, the function traverses up the parent nodes until it finds a node that is not
|
|
@@ -1144,12 +1145,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1144
1145
|
* The function `dfs` performs a depth-first search traversal on a binary tree structure based on the
|
|
1145
1146
|
* specified parameters.
|
|
1146
1147
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends the
|
|
1147
|
-
* `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
|
|
1148
1149
|
* `this._DEFAULT_NODE_CALLBACK as C`.
|
|
1149
1150
|
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `dfs` method specifies the
|
|
1150
1151
|
* order in which the Depth-First Search (DFS) algorithm should traverse the nodes in the tree. The
|
|
1151
1152
|
* possible values for the `pattern` parameter are:
|
|
1152
|
-
* @param {BTNRep<K, V,
|
|
1153
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `dfs`
|
|
1153
1154
|
* method is used to specify the starting point for the Depth-First Search traversal. It can be
|
|
1154
1155
|
* either a `BTNRep` object representing a key, node, or entry in the binary tree map,
|
|
1155
1156
|
* or it can be a
|
|
@@ -1177,8 +1178,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1177
1178
|
* tree, executing a specified callback function on each node visited.
|
|
1178
1179
|
* @param {C} callback - The `callback` parameter in the `bfs` function is a function that will be
|
|
1179
1180
|
* called on each node visited during the breadth-first search traversal. It is a generic type `C`
|
|
1180
|
-
* that extends the `NodeCallback` type, which takes a parameter of type `
|
|
1181
|
-
* @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`
|
|
1182
1183
|
* function represents the starting point for the breadth-first search traversal in a binary tree. It
|
|
1183
1184
|
* can be specified as a key, node, or entry in the binary tree structure. If not provided, the
|
|
1184
1185
|
* default value is the root node of the binary
|
|
@@ -1198,7 +1199,9 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1198
1199
|
return [];
|
|
1199
1200
|
const ans = [];
|
|
1200
1201
|
if (iterationType === 'RECURSIVE') {
|
|
1201
|
-
const queue = new queue_1.Queue([
|
|
1202
|
+
const queue = new queue_1.Queue([
|
|
1203
|
+
startNode
|
|
1204
|
+
]);
|
|
1202
1205
|
const dfs = (level) => {
|
|
1203
1206
|
if (queue.size === 0)
|
|
1204
1207
|
return;
|
|
@@ -1252,7 +1255,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1252
1255
|
* structure based on a specified callback and iteration type.
|
|
1253
1256
|
* @param {C} callback - The `callback` parameter is a function that will be called on each leaf node
|
|
1254
1257
|
* in the binary tree. It is optional and defaults to a default callback function if not provided.
|
|
1255
|
-
* @param {BTNRep<K, V,
|
|
1258
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `leaves`
|
|
1256
1259
|
* method is used to specify the starting point for finding and processing the leaves of a binary
|
|
1257
1260
|
* tree. It can be provided as either a key, a node, or an entry in the binary tree structure. If not
|
|
1258
1261
|
* explicitly provided, the default value
|
|
@@ -1307,7 +1310,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1307
1310
|
* @param {C} callback - The `callback` parameter is a function that will be applied to each node in
|
|
1308
1311
|
* the binary tree during the traversal. It is used to process each node and determine what
|
|
1309
1312
|
* information to include in the output for each level of the tree.
|
|
1310
|
-
* @param {BTNRep<K, V,
|
|
1313
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
1311
1314
|
* `listLevels` function represents the starting point for traversing the binary tree. It can be
|
|
1312
1315
|
* either a key, a node, or an entry in the binary tree. If not provided, the default value is the
|
|
1313
1316
|
* root of the binary tree.
|
|
@@ -1379,11 +1382,11 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1379
1382
|
* Morris Traversal algorithm with different order patterns.
|
|
1380
1383
|
* @param {C} callback - The `callback` parameter in the `morris` function is a function that will be
|
|
1381
1384
|
* called on each node in the binary tree during the traversal. It is of type `C`, which extends the
|
|
1382
|
-
* `NodeCallback<
|
|
1385
|
+
* `NodeCallback<BinaryTreeNode<K, V>>` type. The default value for `callback` is `this._DEFAULT
|
|
1383
1386
|
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function specifies
|
|
1384
1387
|
* the type of Depth-First Search (DFS) order pattern to traverse the binary tree. The possible
|
|
1385
1388
|
* values for the `pattern` parameter are:
|
|
1386
|
-
* @param {BTNRep<K, V,
|
|
1389
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `morris`
|
|
1387
1390
|
* function is the starting point for the Morris traversal algorithm. It represents the root node of
|
|
1388
1391
|
* the binary tree or the node from which the traversal should begin. It can be provided as either a
|
|
1389
1392
|
* key, a node, an entry, or a reference
|
|
@@ -1489,6 +1492,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1489
1492
|
*/
|
|
1490
1493
|
clone() {
|
|
1491
1494
|
const cloned = this.createTree();
|
|
1495
|
+
this._clone(cloned);
|
|
1496
|
+
return cloned;
|
|
1497
|
+
}
|
|
1498
|
+
_clone(cloned) {
|
|
1492
1499
|
this.bfs(node => {
|
|
1493
1500
|
if (node === null)
|
|
1494
1501
|
cloned.add(null);
|
|
@@ -1501,7 +1508,6 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1501
1508
|
}, this._root, this.iterationType, true);
|
|
1502
1509
|
if (this._isMapMode)
|
|
1503
1510
|
cloned._store = this._store;
|
|
1504
|
-
return cloned;
|
|
1505
1511
|
}
|
|
1506
1512
|
/**
|
|
1507
1513
|
* Time Complexity: O(n)
|
|
@@ -1562,7 +1568,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1562
1568
|
*
|
|
1563
1569
|
* The function `toVisual` in TypeScript overrides the visual representation of a binary tree with
|
|
1564
1570
|
* customizable options for displaying undefined, null, and sentinel nodes.
|
|
1565
|
-
* @param {BTNRep<K, V,
|
|
1571
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
1566
1572
|
* `toVisual` method is used to specify the starting point for visualizing the binary tree structure.
|
|
1567
1573
|
* It can be a node, key, entry, or the root of the tree. If no specific starting point is provided,
|
|
1568
1574
|
* the default is set to the root
|
|
@@ -1587,7 +1593,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1587
1593
|
if (opts.isShowRedBlackNIL)
|
|
1588
1594
|
output += `S for Sentinel Node(NIL)\n`;
|
|
1589
1595
|
const display = (root) => {
|
|
1590
|
-
const [lines
|
|
1596
|
+
const [lines] = this._displayAux(root, opts);
|
|
1591
1597
|
let paragraph = '';
|
|
1592
1598
|
for (const line of lines) {
|
|
1593
1599
|
paragraph += line + '\n';
|
|
@@ -1607,7 +1613,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1607
1613
|
* printing options for the binary tree. It is an optional parameter that allows you to customize how
|
|
1608
1614
|
* the binary tree is printed, such as choosing between different traversal orders or formatting
|
|
1609
1615
|
* options.
|
|
1610
|
-
* @param {BTNRep<K, V,
|
|
1616
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
|
|
1611
1617
|
* `override print` method is used to specify the starting point for printing the binary tree. It can
|
|
1612
1618
|
* be either a key, a node, an entry, or the root of the tree. If no specific starting point is
|
|
1613
1619
|
* provided, the default value is set to
|
|
@@ -1615,6 +1621,42 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1615
1621
|
print(options, startNode = this._root) {
|
|
1616
1622
|
console.log(this.toVisual(startNode, options));
|
|
1617
1623
|
}
|
|
1624
|
+
/**
|
|
1625
|
+
* Time Complexity: O(1)
|
|
1626
|
+
* Space Complexity: O(1)
|
|
1627
|
+
*
|
|
1628
|
+
* The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
|
|
1629
|
+
* or returns null.
|
|
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
|
|
1633
|
+
* node, an entry
|
|
1634
|
+
* @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
|
|
1635
|
+
* an optional parameter of type `V`. It represents the value associated with the key in the node
|
|
1636
|
+
* being created. If a `value` is provided, it will be used when creating the node. If
|
|
1637
|
+
* @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
|
|
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
|
|
1640
|
+
* value.
|
|
1641
|
+
*/
|
|
1642
|
+
_keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value) {
|
|
1643
|
+
if (keyNodeOrEntry === undefined)
|
|
1644
|
+
return [undefined, undefined];
|
|
1645
|
+
if (keyNodeOrEntry === null)
|
|
1646
|
+
return [null, undefined];
|
|
1647
|
+
if (this.isNode(keyNodeOrEntry))
|
|
1648
|
+
return [keyNodeOrEntry, value];
|
|
1649
|
+
if (this.isEntry(keyNodeOrEntry)) {
|
|
1650
|
+
const [key, entryValue] = keyNodeOrEntry;
|
|
1651
|
+
if (key === undefined)
|
|
1652
|
+
return [undefined, undefined];
|
|
1653
|
+
else if (key === null)
|
|
1654
|
+
return [null, undefined];
|
|
1655
|
+
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
1656
|
+
return [this.createNode(key, finalValue), finalValue];
|
|
1657
|
+
}
|
|
1658
|
+
return [this.createNode(keyNodeOrEntry, value), value];
|
|
1659
|
+
}
|
|
1618
1660
|
/**
|
|
1619
1661
|
* Time complexity: O(n)
|
|
1620
1662
|
* Space complexity: O(n)
|
|
@@ -1623,11 +1665,11 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1623
1665
|
* the specified order pattern and callback function.
|
|
1624
1666
|
* @param {C} callback - The `callback` parameter in the `_dfs` method is a function that will be
|
|
1625
1667
|
* called on each node visited during the depth-first search traversal. It is of type `C`, which
|
|
1626
|
-
* extends `NodeCallback<OptNodeOrNull<
|
|
1668
|
+
* extends `NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>`. The default value for this parameter is `this._DEFAULT
|
|
1627
1669
|
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `_dfs` method specifies the
|
|
1628
1670
|
* order in which the nodes are visited during the Depth-First Search traversal. It can have one of
|
|
1629
1671
|
* the following values:
|
|
1630
|
-
* @param {BTNRep<K, V,
|
|
1672
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `_dfs`
|
|
1631
1673
|
* method is used to specify the starting point for the depth-first search traversal in a binary
|
|
1632
1674
|
* tree. It can be provided as either a `BTNRep` object or a reference to the root node
|
|
1633
1675
|
* of the tree. If no specific
|
|
@@ -1871,12 +1913,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1871
1913
|
* Space Complexity: O(1)
|
|
1872
1914
|
*
|
|
1873
1915
|
* The _swapProperties function swaps key and value properties between two nodes in a binary tree.
|
|
1874
|
-
* @param {BTNRep<K, V,
|
|
1916
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} srcNode - The `srcNode` parameter in the
|
|
1875
1917
|
* `_swapProperties` method can be either a BTNRep object containing key and value
|
|
1876
1918
|
* properties, or it can be of type R.
|
|
1877
|
-
* @param {BTNRep<K, V,
|
|
1919
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} destNode - The `destNode` parameter in the
|
|
1878
1920
|
* `_swapProperties` method represents the node or entry where the properties will be swapped with
|
|
1879
|
-
* 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
|
|
1880
1922
|
* both `srcNode
|
|
1881
1923
|
* @returns The `_swapProperties` method returns either the `destNode` with its key and value swapped
|
|
1882
1924
|
* with the `srcNode`, or `undefined` if either `srcNode` or `destNode` is falsy.
|
|
@@ -1904,9 +1946,9 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1904
1946
|
* Space Complexity: O(1)
|
|
1905
1947
|
*
|
|
1906
1948
|
* The _replaceNode function replaces an old node with a new node in a binary tree structure.
|
|
1907
|
-
* @param {
|
|
1949
|
+
* @param {BinaryTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that you want to replace in a
|
|
1908
1950
|
* tree data structure.
|
|
1909
|
-
* @param {
|
|
1951
|
+
* @param {BinaryTreeNode<K, V>} newNode - The `newNode` parameter in the `_replaceNode` function represents the node
|
|
1910
1952
|
* that will replace the `oldNode` in a tree data structure. This function is responsible for
|
|
1911
1953
|
* updating the parent, left child, right child, and root (if necessary) references when replacing a
|
|
1912
1954
|
* node in the tree.
|
|
@@ -1936,8 +1978,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1936
1978
|
*
|
|
1937
1979
|
* The function _setRoot sets the root node of a data structure while updating the parent reference
|
|
1938
1980
|
* of the previous root node.
|
|
1939
|
-
* @param v - The parameter `v` in the `_setRoot` method is of type `OptNodeOrNull<
|
|
1940
|
-
* 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`.
|
|
1941
1983
|
*/
|
|
1942
1984
|
_setRoot(v) {
|
|
1943
1985
|
if (v) {
|
|
@@ -1951,30 +1993,24 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1951
1993
|
*
|
|
1952
1994
|
* The function `_ensurePredicate` in TypeScript ensures that the input is converted into a valid
|
|
1953
1995
|
* predicate function for a binary tree node.
|
|
1954
|
-
* @param {BTNRep<K, V,
|
|
1996
|
+
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The
|
|
1955
1997
|
* `_ensurePredicate` method in the provided code snippet is responsible for ensuring that the input
|
|
1956
|
-
* parameter `
|
|
1998
|
+
* parameter `keyNodeEntryOrPredicate` is transformed into a valid predicate function that can be
|
|
1957
1999
|
* used for filtering nodes in a binary tree.
|
|
1958
|
-
* @returns A NodePredicate<
|
|
2000
|
+
* @returns A NodePredicate<BinaryTreeNode<K, V>> function is being returned.
|
|
1959
2001
|
*/
|
|
1960
|
-
_ensurePredicate(
|
|
1961
|
-
if (
|
|
2002
|
+
_ensurePredicate(keyNodeEntryOrPredicate) {
|
|
2003
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === undefined)
|
|
1962
2004
|
return (node) => (node ? false : false);
|
|
1963
|
-
if (this._isPredicate(
|
|
1964
|
-
return
|
|
1965
|
-
if (this.isRealNode(
|
|
1966
|
-
return (node) => node ===
|
|
1967
|
-
if (this.isEntry(
|
|
1968
|
-
const [key] =
|
|
1969
|
-
return (node) => node.key === key;
|
|
1970
|
-
}
|
|
1971
|
-
if (this.isKey(keyNodeEntryRawOrPredicate))
|
|
1972
|
-
return (node) => node.key === keyNodeEntryRawOrPredicate;
|
|
1973
|
-
if (this._toEntryFn) {
|
|
1974
|
-
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;
|
|
1975
2011
|
return (node) => node.key === key;
|
|
1976
2012
|
}
|
|
1977
|
-
return (node) => node.key ===
|
|
2013
|
+
return (node) => node.key === keyNodeEntryOrPredicate;
|
|
1978
2014
|
}
|
|
1979
2015
|
/**
|
|
1980
2016
|
* Time Complexity: O(1)
|
|
@@ -1983,7 +2019,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1983
2019
|
* The function `_isPredicate` checks if a given parameter is a function.
|
|
1984
2020
|
* @param {any} p - The parameter `p` is a variable of type `any`, which means it can hold any type
|
|
1985
2021
|
* of value. In this context, the function `_isPredicate` is checking if `p` is a function that
|
|
1986
|
-
* satisfies the type `NodePredicate<
|
|
2022
|
+
* satisfies the type `NodePredicate<BinaryTreeNode<K, V>>`.
|
|
1987
2023
|
* @returns The function is checking if the input `p` is a function and returning a boolean value
|
|
1988
2024
|
* based on that check. If `p` is a function, it will return `true`, indicating that `p` is a
|
|
1989
2025
|
* predicate function for a binary tree node. If `p` is not a function, it will return `false`.
|
|
@@ -1997,32 +2033,25 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1997
2033
|
*
|
|
1998
2034
|
* The function `_extractKey` in TypeScript returns the key from a given input, which can be a node,
|
|
1999
2035
|
* entry, raw data, or null/undefined.
|
|
2000
|
-
* @param {BTNRep<K, V,
|
|
2001
|
-
* TypeScript method that takes in a parameter `
|
|
2002
|
-
* where `BTNRep` is a generic type with keys `K`, `V`, and `
|
|
2003
|
-
* @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`
|
|
2004
2040
|
* parameter. The return value can be a key value of type `K`, `null`, or `undefined`, depending on
|
|
2005
2041
|
* the conditions checked in the method.
|
|
2006
2042
|
*/
|
|
2007
|
-
_extractKey(
|
|
2008
|
-
if (
|
|
2043
|
+
_extractKey(keyNodeOrEntry) {
|
|
2044
|
+
if (keyNodeOrEntry === null)
|
|
2009
2045
|
return null;
|
|
2010
|
-
if (
|
|
2046
|
+
if (keyNodeOrEntry === undefined)
|
|
2011
2047
|
return;
|
|
2012
|
-
if (
|
|
2048
|
+
if (keyNodeOrEntry === this._NIL)
|
|
2013
2049
|
return;
|
|
2014
|
-
if (this.isNode(
|
|
2015
|
-
return
|
|
2016
|
-
if (this.isEntry(
|
|
2017
|
-
return
|
|
2018
|
-
|
|
2019
|
-
if (this._toEntryFn) {
|
|
2020
|
-
const [key] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
2021
|
-
return key;
|
|
2022
|
-
}
|
|
2023
|
-
return;
|
|
2024
|
-
}
|
|
2025
|
-
return keyNodeEntryOrRaw;
|
|
2050
|
+
if (this.isNode(keyNodeOrEntry))
|
|
2051
|
+
return keyNodeOrEntry.key;
|
|
2052
|
+
if (this.isEntry(keyNodeOrEntry))
|
|
2053
|
+
return keyNodeOrEntry[0];
|
|
2054
|
+
return keyNodeOrEntry;
|
|
2026
2055
|
}
|
|
2027
2056
|
/**
|
|
2028
2057
|
* Time Complexity: O(1)
|