deque-typed 1.51.8 → 1.51.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +104 -66
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
- package/dist/data-structures/binary-tree/avl-tree.d.ts +80 -60
- package/dist/data-structures/binary-tree/avl-tree.js +78 -59
- package/dist/data-structures/binary-tree/binary-tree.d.ts +316 -224
- package/dist/data-structures/binary-tree/binary-tree.js +471 -361
- package/dist/data-structures/binary-tree/bst.d.ts +198 -200
- package/dist/data-structures/binary-tree/bst.js +215 -249
- package/dist/data-structures/binary-tree/rb-tree.d.ts +71 -72
- package/dist/data-structures/binary-tree/rb-tree.js +107 -98
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +90 -73
- package/dist/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/data-structures/graph/abstract-graph.js +10 -15
- package/dist/data-structures/hash/hash-map.d.ts +31 -38
- package/dist/data-structures/hash/hash-map.js +40 -55
- package/dist/data-structures/queue/deque.d.ts +2 -3
- package/dist/data-structures/queue/deque.js +2 -3
- package/dist/data-structures/trie/trie.d.ts +1 -1
- package/dist/data-structures/trie/trie.js +1 -1
- package/dist/interfaces/binary-tree.d.ts +6 -6
- package/dist/types/common.d.ts +1 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +5 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -3
- package/dist/utils/utils.js +3 -5
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +142 -92
- package/src/data-structures/binary-tree/avl-tree.ts +94 -66
- package/src/data-structures/binary-tree/binary-tree.ts +530 -398
- package/src/data-structures/binary-tree/bst.ts +251 -270
- package/src/data-structures/binary-tree/rb-tree.ts +121 -100
- package/src/data-structures/binary-tree/tree-multi-map.ts +125 -99
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/hash/hash-map.ts +42 -49
- package/src/data-structures/queue/deque.ts +2 -2
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/trie/trie.ts +2 -2
- package/src/interfaces/binary-tree.ts +8 -7
- package/src/types/common.ts +1 -2
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -2
- package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +5 -4
- package/src/types/data-structures/binary-tree/bst.ts +4 -4
- package/src/types/data-structures/binary-tree/rb-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -3
- package/src/utils/utils.ts +3 -3
|
@@ -60,31 +60,42 @@ exports.BSTNode = BSTNode;
|
|
|
60
60
|
*/
|
|
61
61
|
class BST extends binary_tree_1.BinaryTree {
|
|
62
62
|
/**
|
|
63
|
-
* This is the constructor function for a Binary Search Tree class in TypeScript
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
constructor(keysOrNodesOrEntries = [], options) {
|
|
63
|
+
* This is the constructor function for a Binary Search Tree class in TypeScript.
|
|
64
|
+
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
65
|
+
* iterable that can contain either keys, nodes, entries, or raw elements. These elements will be
|
|
66
|
+
* added to the binary search tree during the construction of the object.
|
|
67
|
+
* @param [options] - An optional object that contains additional options for the Binary Search Tree.
|
|
68
|
+
* It can include a comparator function that defines the order of the elements in the tree.
|
|
69
|
+
*/
|
|
70
|
+
constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
|
|
72
71
|
super([], options);
|
|
73
72
|
this._root = undefined;
|
|
74
|
-
|
|
73
|
+
/**
|
|
74
|
+
* Time complexity: O(n)
|
|
75
|
+
* Space complexity: O(n)
|
|
76
|
+
*/
|
|
77
|
+
this._DEFAULT_COMPARATOR = (a, b) => {
|
|
78
|
+
if (typeof a === 'object' && typeof b === 'object' && this.comparator === this._DEFAULT_COMPARATOR) {
|
|
79
|
+
throw TypeError('When comparing two object types, it is necessary to customize a [comparator] function of options parameter during the instantiation of the data structure.');
|
|
80
|
+
}
|
|
75
81
|
if (a > b)
|
|
76
82
|
return 1;
|
|
77
83
|
if (a < b)
|
|
78
84
|
return -1;
|
|
79
85
|
return 0;
|
|
80
86
|
};
|
|
87
|
+
/**
|
|
88
|
+
* Time complexity: O(n)
|
|
89
|
+
* Space complexity: O(n)
|
|
90
|
+
*/
|
|
91
|
+
this._comparator = this._DEFAULT_COMPARATOR;
|
|
81
92
|
if (options) {
|
|
82
93
|
const { comparator } = options;
|
|
83
94
|
if (comparator)
|
|
84
95
|
this._comparator = comparator;
|
|
85
96
|
}
|
|
86
|
-
if (
|
|
87
|
-
this.addMany(
|
|
97
|
+
if (keysOrNodesOrEntriesOrRawElements)
|
|
98
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
88
99
|
}
|
|
89
100
|
/**
|
|
90
101
|
* The function returns the root node of a tree structure.
|
|
@@ -93,13 +104,6 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
93
104
|
get root() {
|
|
94
105
|
return this._root;
|
|
95
106
|
}
|
|
96
|
-
/**
|
|
97
|
-
* The function returns the value of the _comparator property.
|
|
98
|
-
* @returns The `_comparator` property is being returned.
|
|
99
|
-
*/
|
|
100
|
-
get comparator() {
|
|
101
|
-
return this._comparator;
|
|
102
|
-
}
|
|
103
107
|
/**
|
|
104
108
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
105
109
|
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
@@ -114,104 +118,68 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
114
118
|
/**
|
|
115
119
|
* The function creates a new binary search tree with the specified options.
|
|
116
120
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
117
|
-
* behavior of the `createTree` method. It
|
|
118
|
-
*
|
|
119
|
-
* @returns a new instance of the BST class
|
|
120
|
-
* options. The returned value is casted as TREE.
|
|
121
|
+
* behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which has the
|
|
122
|
+
* following properties:
|
|
123
|
+
* @returns a new instance of the BST class with the provided options.
|
|
121
124
|
*/
|
|
122
125
|
createTree(options) {
|
|
123
126
|
return new BST([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
|
|
124
127
|
}
|
|
125
128
|
/**
|
|
126
|
-
* The function
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
* `
|
|
131
|
-
*
|
|
132
|
-
|
|
133
|
-
keyValueOrEntryToNode(keyOrNodeOrEntry, value) {
|
|
134
|
-
let node;
|
|
135
|
-
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
else if (this.isNode(keyOrNodeOrEntry)) {
|
|
139
|
-
node = keyOrNodeOrEntry;
|
|
140
|
-
}
|
|
141
|
-
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
142
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
143
|
-
if (key === undefined || key === null) {
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
else {
|
|
147
|
-
node = this.createNode(key, value);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
151
|
-
node = this.createNode(keyOrNodeOrEntry, value);
|
|
152
|
-
}
|
|
153
|
-
else {
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
return node;
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Time Complexity: O(log n)
|
|
160
|
-
* Space Complexity: O(log n)
|
|
129
|
+
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
130
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - A variable that can be of
|
|
131
|
+
* type R or KeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
132
|
+
* element.
|
|
133
|
+
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
134
|
+
* value associated with a key in a key-value pair.
|
|
135
|
+
* @returns either a NODE object or undefined.
|
|
161
136
|
*/
|
|
137
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) {
|
|
138
|
+
var _a;
|
|
139
|
+
return (_a = super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value)) !== null && _a !== void 0 ? _a : undefined;
|
|
140
|
+
}
|
|
162
141
|
/**
|
|
163
142
|
* Time Complexity: O(log n)
|
|
164
143
|
* Space Complexity: O(log n)
|
|
165
144
|
*
|
|
166
|
-
* The function
|
|
167
|
-
*
|
|
168
|
-
* @param {
|
|
169
|
-
* `
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
if (this.isEntry(keyOrNodeOrEntry)) {
|
|
181
|
-
const key = keyOrNodeOrEntry[0];
|
|
182
|
-
if (key === null || key === undefined)
|
|
183
|
-
return;
|
|
184
|
-
return this.getNodeByKey(key, iterationType);
|
|
185
|
-
}
|
|
186
|
-
const key = keyOrNodeOrEntry;
|
|
187
|
-
if (key === null || key === undefined)
|
|
188
|
-
return;
|
|
189
|
-
return this.getNodeByKey(key, iterationType);
|
|
145
|
+
* The function ensures the existence of a node in a data structure and returns it, or undefined if
|
|
146
|
+
* it doesn't exist.
|
|
147
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
148
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, which represents the key, node,
|
|
149
|
+
* entry, or raw element that needs to be ensured in the tree.
|
|
150
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
151
|
+
* parameter that specifies the type of iteration to be used when ensuring a node. It has a default
|
|
152
|
+
* value of `'ITERATIVE'`.
|
|
153
|
+
* @returns The method is returning either the node that was ensured or `undefined` if the node could
|
|
154
|
+
* not be ensured.
|
|
155
|
+
*/
|
|
156
|
+
ensureNode(keyOrNodeOrEntryOrRawElement, iterationType = 'ITERATIVE') {
|
|
157
|
+
var _a;
|
|
158
|
+
return (_a = super.ensureNode(keyOrNodeOrEntryOrRawElement, iterationType)) !== null && _a !== void 0 ? _a : undefined;
|
|
190
159
|
}
|
|
191
160
|
/**
|
|
192
|
-
* The function checks if
|
|
193
|
-
* @param
|
|
194
|
-
*
|
|
161
|
+
* The function checks if the input is an instance of the BSTNode class.
|
|
162
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
163
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
164
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
165
|
+
* an instance of the `BSTNode` class.
|
|
195
166
|
*/
|
|
196
|
-
isNode(
|
|
197
|
-
return
|
|
167
|
+
isNode(keyOrNodeOrEntryOrRawElement) {
|
|
168
|
+
return keyOrNodeOrEntryOrRawElement instanceof BSTNode;
|
|
198
169
|
}
|
|
199
|
-
/**
|
|
200
|
-
* Time Complexity: O(log n)
|
|
201
|
-
* Space Complexity: O(1)
|
|
202
|
-
*/
|
|
203
170
|
/**
|
|
204
171
|
* Time Complexity: O(log n)
|
|
205
172
|
* Space Complexity: O(1)
|
|
206
173
|
*
|
|
207
|
-
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
* @param {V} [value] - The value
|
|
211
|
-
*
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
174
|
+
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
|
|
175
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
176
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
177
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
178
|
+
* key in the binary search tree. If provided, it will be stored in the node along with the key.
|
|
179
|
+
* @returns a boolean value.
|
|
180
|
+
*/
|
|
181
|
+
add(keyOrNodeOrEntryOrRawElement, value) {
|
|
182
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
|
|
215
183
|
if (newNode === undefined)
|
|
216
184
|
return false;
|
|
217
185
|
if (this.root === undefined) {
|
|
@@ -222,15 +190,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
222
190
|
let current = this.root;
|
|
223
191
|
while (current !== undefined) {
|
|
224
192
|
if (this.comparator(current.key, newNode.key) === 0) {
|
|
225
|
-
// if (current !== newNode) {
|
|
226
|
-
// The key value is the same but the reference is different, update the value of the existing node
|
|
227
193
|
this._replaceNode(current, newNode);
|
|
228
194
|
return true;
|
|
229
|
-
// } else {
|
|
230
|
-
// The key value is the same and the reference is the same, replace the entire node
|
|
231
|
-
// this._replaceNode(current, newNode);
|
|
232
|
-
// return;
|
|
233
|
-
// }
|
|
234
195
|
}
|
|
235
196
|
else if (this.comparator(current.key, newNode.key) > 0) {
|
|
236
197
|
if (current.left === undefined) {
|
|
@@ -252,18 +213,17 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
252
213
|
return false;
|
|
253
214
|
}
|
|
254
215
|
/**
|
|
255
|
-
* Time Complexity: O(
|
|
256
|
-
* Space Complexity: O(
|
|
216
|
+
* Time Complexity: O(log n)
|
|
217
|
+
* Space Complexity: O(log n)
|
|
257
218
|
*/
|
|
258
219
|
/**
|
|
259
220
|
* Time Complexity: O(k log n)
|
|
260
221
|
* Space Complexity: O(k + log n)
|
|
261
222
|
*
|
|
262
|
-
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure
|
|
263
|
-
*
|
|
264
|
-
*
|
|
265
|
-
*
|
|
266
|
-
* data structure.
|
|
223
|
+
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure and returns
|
|
224
|
+
* an array indicating whether each key or node was successfully inserted.
|
|
225
|
+
* @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
|
|
226
|
+
* elements to be added to the data structure.
|
|
267
227
|
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
268
228
|
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
269
229
|
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
@@ -272,20 +232,19 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
272
232
|
* algorithm. If set to false, the elements will be added without balancing the tree. The default
|
|
273
233
|
* value is true.
|
|
274
234
|
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
275
|
-
* specifies the type of iteration to use when adding multiple keys or nodes to the binary
|
|
276
|
-
*
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
* or entry was successfully inserted into the data structure.
|
|
235
|
+
* specifies the type of iteration to use when adding multiple keys or nodes to the binary search
|
|
236
|
+
* tree. It can have two possible values:
|
|
237
|
+
* @returns The function `addMany` returns an array of booleans indicating whether each element was
|
|
238
|
+
* successfully inserted into the data structure.
|
|
280
239
|
*/
|
|
281
|
-
addMany(
|
|
240
|
+
addMany(keysOrNodesOrEntriesOrRawElements, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
282
241
|
const inserted = [];
|
|
283
242
|
let valuesIterator;
|
|
284
243
|
if (values) {
|
|
285
244
|
valuesIterator = values[Symbol.iterator]();
|
|
286
245
|
}
|
|
287
246
|
if (!isBalanceAdd) {
|
|
288
|
-
for (const kve of
|
|
247
|
+
for (const kve of keysOrNodesOrEntriesOrRawElements) {
|
|
289
248
|
const value = valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value;
|
|
290
249
|
const nn = this.add(kve, value);
|
|
291
250
|
inserted.push(nn);
|
|
@@ -298,25 +257,32 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
298
257
|
return false;
|
|
299
258
|
return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
|
|
300
259
|
};
|
|
301
|
-
for (const kve of
|
|
260
|
+
for (const kve of keysOrNodesOrEntriesOrRawElements) {
|
|
302
261
|
isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
|
|
303
262
|
}
|
|
304
263
|
let sorted = [];
|
|
305
264
|
sorted = realBTNExemplars.sort((a, b) => {
|
|
306
265
|
let keyA, keyB;
|
|
307
|
-
if (this.isEntry(a))
|
|
266
|
+
if (this.isEntry(a))
|
|
308
267
|
keyA = a[0];
|
|
309
|
-
}
|
|
310
268
|
else if (this.isRealNode(a))
|
|
311
269
|
keyA = a.key;
|
|
312
|
-
else
|
|
270
|
+
else if (this.toEntryFn) {
|
|
271
|
+
keyA = this.toEntryFn(a)[0];
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
313
274
|
keyA = a;
|
|
275
|
+
}
|
|
314
276
|
if (this.isEntry(b))
|
|
315
277
|
keyB = b[0];
|
|
316
278
|
else if (this.isRealNode(b))
|
|
317
279
|
keyB = b.key;
|
|
318
|
-
else
|
|
280
|
+
else if (this.toEntryFn) {
|
|
281
|
+
keyB = this.toEntryFn(b)[0];
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
319
284
|
keyB = b;
|
|
285
|
+
}
|
|
320
286
|
if (keyA !== undefined && keyA !== null && keyB !== undefined && keyB !== null) {
|
|
321
287
|
return this.comparator(keyA, keyB);
|
|
322
288
|
}
|
|
@@ -357,32 +323,26 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
357
323
|
return inserted;
|
|
358
324
|
}
|
|
359
325
|
/**
|
|
360
|
-
* Time Complexity: O(log n)
|
|
361
|
-
* Space Complexity: O(k + log n)
|
|
362
|
-
* /
|
|
363
|
-
|
|
364
|
-
/**
|
|
365
326
|
* Time Complexity: O(log n)
|
|
366
327
|
* Space Complexity: O(k + log n)
|
|
367
328
|
*
|
|
368
|
-
* The
|
|
369
|
-
*
|
|
329
|
+
* The `getNodes` function in TypeScript retrieves nodes from a binary tree based on a given
|
|
330
|
+
* identifier and callback function.
|
|
370
331
|
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
371
|
-
* want to search for in the
|
|
372
|
-
*
|
|
373
|
-
* @param {C} callback - The `callback` parameter is a function that takes a node
|
|
374
|
-
*
|
|
375
|
-
* function
|
|
376
|
-
* @param [onlyOne=false] - A boolean
|
|
377
|
-
*
|
|
378
|
-
*
|
|
379
|
-
*
|
|
380
|
-
*
|
|
381
|
-
*
|
|
382
|
-
*
|
|
383
|
-
*
|
|
384
|
-
*
|
|
385
|
-
* @returns The method returns an array of nodes (`NODE[]`).
|
|
332
|
+
* want to search for in the binary tree. It can be of any type that is returned by the callback
|
|
333
|
+
* function.
|
|
334
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node as input and
|
|
335
|
+
* returns a value. This value is used to identify the nodes that match the given identifier. The
|
|
336
|
+
* `callback` function is optional and defaults to `this._DEFAULT_CALLBACK`.
|
|
337
|
+
* @param [onlyOne=false] - A boolean value indicating whether to return only the first matching node
|
|
338
|
+
* or all matching nodes. If set to true, only the first matching node will be returned. If set to
|
|
339
|
+
* false, all matching nodes will be returned. The default value is false.
|
|
340
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
341
|
+
* point for the search in the binary tree. It can be either a node object, a key-value pair, or an
|
|
342
|
+
* entry object. If it is not provided, the `root` of the binary tree is used as the starting point.
|
|
343
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
344
|
+
* iteration to be performed. It can have two possible values:
|
|
345
|
+
* @returns The method `getNodes` returns an array of `NODE` objects.
|
|
386
346
|
*/
|
|
387
347
|
getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
388
348
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -453,51 +413,50 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
453
413
|
* Time Complexity: O(log n)
|
|
454
414
|
* Space Complexity: O(1)
|
|
455
415
|
*
|
|
456
|
-
* The `getNode`
|
|
457
|
-
*
|
|
458
|
-
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value
|
|
459
|
-
*
|
|
460
|
-
*
|
|
461
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
462
|
-
* the
|
|
463
|
-
*
|
|
464
|
-
*
|
|
416
|
+
* The function `getNode` returns the first node that matches the given identifier and callback
|
|
417
|
+
* function in a binary search tree.
|
|
418
|
+
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
419
|
+
* want to search for in the binary search tree. It can be of any type that is compatible with the
|
|
420
|
+
* type returned by the callback function.
|
|
421
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
422
|
+
* node matches the desired criteria. It should be a function that takes a node as an argument and
|
|
423
|
+
* returns a boolean value indicating whether the node matches the criteria or not. If no callback is
|
|
424
|
+
* provided, the default callback will be
|
|
465
425
|
* @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
|
|
466
|
-
* search tree. It can be either a key or a node. If it is a key,
|
|
467
|
-
*
|
|
468
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type
|
|
469
|
-
* be performed when searching for nodes in the binary search tree. It
|
|
470
|
-
*
|
|
471
|
-
* @returns The method is returning a
|
|
426
|
+
* search tree. It can be either a key or a node. If it is a key, the search will start from the node
|
|
427
|
+
* with that key. If it is a node, the search will start from that node.
|
|
428
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
429
|
+
* of iteration to be performed when searching for nodes in the binary search tree. It can have one
|
|
430
|
+
* of the following values:
|
|
431
|
+
* @returns The method is returning a NODE object or undefined.
|
|
472
432
|
*/
|
|
473
433
|
getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
474
434
|
var _a;
|
|
475
435
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
476
436
|
}
|
|
477
437
|
/**
|
|
478
|
-
* Time Complexity: O(log n)
|
|
479
|
-
* Space Complexity: O(
|
|
438
|
+
* Time Complexity: O(k log n)
|
|
439
|
+
* Space Complexity: O(k + log n)
|
|
480
440
|
*/
|
|
481
441
|
/**
|
|
482
442
|
* Time Complexity: O(log n)
|
|
483
443
|
* Space Complexity: O(1)
|
|
484
444
|
*
|
|
485
|
-
* The function `getNodeByKey`
|
|
486
|
-
*
|
|
487
|
-
*
|
|
488
|
-
*
|
|
489
|
-
* @param iterationType - The `iterationType` parameter is an optional
|
|
490
|
-
* type of iteration to
|
|
491
|
-
*
|
|
492
|
-
* @returns The
|
|
493
|
-
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
445
|
+
* The function `getNodeByKey` returns a node with a specific key from a tree data structure.
|
|
446
|
+
* @param {K} key - The key parameter is the value used to search for a specific node in the tree. It
|
|
447
|
+
* is typically a unique identifier or a value that can be used to determine the position of the node
|
|
448
|
+
* in the tree structure.
|
|
449
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
450
|
+
* parameter that specifies the type of iteration to be used when searching for a node in the tree.
|
|
451
|
+
* It has a default value of `'ITERATIVE'`.
|
|
452
|
+
* @returns The method is returning a NODE object or undefined.
|
|
494
453
|
*/
|
|
495
454
|
getNodeByKey(key, iterationType = 'ITERATIVE') {
|
|
496
455
|
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
497
456
|
}
|
|
498
457
|
/**
|
|
499
|
-
* Time
|
|
500
|
-
* Space
|
|
458
|
+
* Time Complexity: O(log n)
|
|
459
|
+
* Space Complexity: O(k + log n)
|
|
501
460
|
*/
|
|
502
461
|
/**
|
|
503
462
|
* Time complexity: O(n)
|
|
@@ -506,15 +465,16 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
506
465
|
* The function overrides the depth-first search method and returns an array of the return types of
|
|
507
466
|
* the callback function.
|
|
508
467
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
509
|
-
* during the depth-first search traversal. It is an optional parameter and
|
|
510
|
-
*
|
|
511
|
-
* @param {DFSOrderPattern} [pattern=
|
|
512
|
-
*
|
|
513
|
-
*
|
|
514
|
-
*
|
|
515
|
-
*
|
|
516
|
-
*
|
|
517
|
-
*
|
|
468
|
+
* during the depth-first search traversal. It is an optional parameter and defaults to
|
|
469
|
+
* `this._DEFAULT_CALLBACK`. The type `C` represents the type of the callback function.
|
|
470
|
+
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
471
|
+
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
472
|
+
* take one of the following values:
|
|
473
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
474
|
+
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
475
|
+
* node entry. If not specified, the default value is the root of the tree.
|
|
476
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
477
|
+
* type of iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the
|
|
518
478
|
* following values:
|
|
519
479
|
* @returns The method is returning an array of the return type of the callback function.
|
|
520
480
|
*/
|
|
@@ -522,8 +482,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
522
482
|
return super.dfs(callback, pattern, beginRoot, iterationType, false);
|
|
523
483
|
}
|
|
524
484
|
/**
|
|
525
|
-
* Time
|
|
526
|
-
* Space
|
|
485
|
+
* Time Complexity: O(log n)
|
|
486
|
+
* Space Complexity: O(1)
|
|
527
487
|
*/
|
|
528
488
|
/**
|
|
529
489
|
* Time complexity: O(n)
|
|
@@ -532,38 +492,38 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
532
492
|
* The function overrides the breadth-first search method and returns an array of the return types of
|
|
533
493
|
* the callback function.
|
|
534
494
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
535
|
-
* visited during the breadth-first search
|
|
536
|
-
*
|
|
537
|
-
* @param beginRoot - The `beginRoot` parameter is the starting
|
|
538
|
-
*
|
|
539
|
-
* the
|
|
540
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type
|
|
541
|
-
* be performed during the breadth-first search (BFS) traversal. It
|
|
542
|
-
*
|
|
543
|
-
* @returns
|
|
495
|
+
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
496
|
+
* node being visited, and it can return a value of any type.
|
|
497
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
498
|
+
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
499
|
+
* object. If no value is provided, the default value is the root of the tree.
|
|
500
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
501
|
+
* of iteration to be performed during the breadth-first search (BFS) traversal. It can have one of
|
|
502
|
+
* the following values:
|
|
503
|
+
* @returns an array of the return type of the callback function.
|
|
544
504
|
*/
|
|
545
505
|
bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
546
506
|
return super.bfs(callback, beginRoot, iterationType, false);
|
|
547
507
|
}
|
|
548
508
|
/**
|
|
549
|
-
* Time
|
|
550
|
-
* Space
|
|
509
|
+
* Time Complexity: O(log n)
|
|
510
|
+
* Space Complexity: O(1)
|
|
551
511
|
*/
|
|
552
512
|
/**
|
|
553
513
|
* Time complexity: O(n)
|
|
554
514
|
* Space complexity: O(n)
|
|
555
515
|
*
|
|
556
|
-
* The function overrides the listLevels method and returns an array of arrays
|
|
557
|
-
*
|
|
516
|
+
* The function overrides the listLevels method from the superclass and returns an array of arrays
|
|
517
|
+
* containing the results of the callback function applied to each level of the tree.
|
|
558
518
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
559
|
-
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
560
|
-
* during the
|
|
561
|
-
* @param beginRoot - The `beginRoot` parameter is
|
|
562
|
-
* levels of
|
|
563
|
-
*
|
|
564
|
-
*
|
|
565
|
-
*
|
|
566
|
-
* iteration.
|
|
519
|
+
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
520
|
+
* tree during the iteration process.
|
|
521
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
522
|
+
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
523
|
+
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
524
|
+
* value is provided, the root of
|
|
525
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
526
|
+
* of iteration to be performed on the tree. It can have one of the following values:
|
|
567
527
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
568
528
|
* function.
|
|
569
529
|
*/
|
|
@@ -571,27 +531,27 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
571
531
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
572
532
|
}
|
|
573
533
|
/**
|
|
574
|
-
* Time
|
|
575
|
-
* Space
|
|
534
|
+
* Time complexity: O(n)
|
|
535
|
+
* Space complexity: O(n)
|
|
576
536
|
*/
|
|
577
537
|
/**
|
|
578
|
-
* Time
|
|
579
|
-
* Space
|
|
538
|
+
* Time complexity: O(n)
|
|
539
|
+
* Space complexity: O(n)
|
|
580
540
|
*
|
|
581
|
-
* The `lesserOrGreaterTraverse` function traverses a binary tree and
|
|
582
|
-
*
|
|
541
|
+
* The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
|
|
542
|
+
* each node that meets a certain condition based on a target node and a comparison value.
|
|
583
543
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
584
|
-
* that
|
|
585
|
-
*
|
|
544
|
+
* that meets the condition specified by the `lesserOrGreater` parameter. It takes a single argument,
|
|
545
|
+
* which is the current node being traversed, and returns a value of any type.
|
|
586
546
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
587
|
-
* traverse nodes that are lesser
|
|
588
|
-
*
|
|
589
|
-
* `
|
|
590
|
-
*
|
|
591
|
-
*
|
|
592
|
-
*
|
|
593
|
-
* @param iterationType - The `iterationType` parameter determines the type of
|
|
594
|
-
* performed on the binary tree. It can have two possible values:
|
|
547
|
+
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
548
|
+
* 0, or 1, where:
|
|
549
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} targetNode - The `targetNode` parameter is the node in
|
|
550
|
+
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
551
|
+
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
552
|
+
* `targetNode` is provided,
|
|
553
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
554
|
+
* traversal to be performed on the binary tree. It can have two possible values:
|
|
595
555
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
596
556
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
597
557
|
*/
|
|
@@ -634,18 +594,19 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
634
594
|
}
|
|
635
595
|
}
|
|
636
596
|
/**
|
|
637
|
-
* Time
|
|
638
|
-
* Space
|
|
597
|
+
* Time complexity: O(n)
|
|
598
|
+
* Space complexity: O(n)
|
|
639
599
|
*/
|
|
640
600
|
/**
|
|
641
|
-
* Time
|
|
642
|
-
* Space
|
|
601
|
+
* Time complexity: O(n)
|
|
602
|
+
* Space complexity: O(n)
|
|
643
603
|
*
|
|
644
|
-
* The `perfectlyBalance` function
|
|
645
|
-
*
|
|
646
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
647
|
-
* type of iteration to use when building a balanced binary search tree. It
|
|
648
|
-
*
|
|
604
|
+
* The `perfectlyBalance` function takes an optional `iterationType` parameter and returns `true` if
|
|
605
|
+
* the binary search tree is perfectly balanced, otherwise it returns `false`.
|
|
606
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
607
|
+
* specifies the type of iteration to use when building a balanced binary search tree. It has a
|
|
608
|
+
* default value of `this.iterationType`, which means it will use the iteration type specified in the
|
|
609
|
+
* current instance of the class.
|
|
649
610
|
* @returns The function `perfectlyBalance` returns a boolean value.
|
|
650
611
|
*/
|
|
651
612
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
@@ -685,25 +646,19 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
685
646
|
}
|
|
686
647
|
}
|
|
687
648
|
/**
|
|
688
|
-
*
|
|
689
|
-
*
|
|
690
|
-
* AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
|
|
691
|
-
*
|
|
692
|
-
* Use Cases and Efficiency:
|
|
693
|
-
* Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
|
|
694
|
-
* AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
|
|
695
|
-
*/
|
|
696
|
-
/**
|
|
697
|
-
* Time Complexity: O(n)
|
|
698
|
-
* Space Complexity: O(log n)
|
|
649
|
+
* Time complexity: O(n)
|
|
650
|
+
* Space complexity: O(n)
|
|
699
651
|
*/
|
|
700
652
|
/**
|
|
701
653
|
* Time Complexity: O(n)
|
|
702
654
|
* Space Complexity: O(log n)
|
|
703
655
|
*
|
|
704
|
-
* The function checks if a binary tree is AVL balanced using either recursive or
|
|
705
|
-
*
|
|
706
|
-
*
|
|
656
|
+
* The function `isAVLBalanced` checks if a binary tree is AVL balanced using either a recursive or
|
|
657
|
+
* iterative approach.
|
|
658
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
659
|
+
* specifies the type of iteration to use when checking if the AVL tree is balanced. It has a default
|
|
660
|
+
* value of `this.iterationType`, which means it will use the iteration type specified in the current
|
|
661
|
+
* instance of the AVL tree.
|
|
707
662
|
* @returns a boolean value.
|
|
708
663
|
*/
|
|
709
664
|
isAVLBalanced(iterationType = this.iterationType) {
|
|
@@ -753,9 +708,20 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
753
708
|
return balanced;
|
|
754
709
|
}
|
|
755
710
|
/**
|
|
756
|
-
*
|
|
757
|
-
*
|
|
758
|
-
|
|
711
|
+
* Time Complexity: O(n)
|
|
712
|
+
* Space Complexity: O(log n)
|
|
713
|
+
*/
|
|
714
|
+
/**
|
|
715
|
+
* The function returns the value of the _comparator property.
|
|
716
|
+
* @returns The `_comparator` property is being returned.
|
|
717
|
+
*/
|
|
718
|
+
get comparator() {
|
|
719
|
+
return this._comparator;
|
|
720
|
+
}
|
|
721
|
+
/**
|
|
722
|
+
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
723
|
+
* root.
|
|
724
|
+
* @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
|
|
759
725
|
*/
|
|
760
726
|
_setRoot(v) {
|
|
761
727
|
if (v) {
|