undirected-graph-typed 1.51.7 → 1.51.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +103 -74
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +116 -93
- package/dist/data-structures/binary-tree/avl-tree.d.ts +82 -62
- package/dist/data-structures/binary-tree/avl-tree.js +90 -71
- package/dist/data-structures/binary-tree/binary-tree.d.ts +318 -233
- package/dist/data-structures/binary-tree/binary-tree.js +492 -392
- package/dist/data-structures/binary-tree/bst.d.ts +204 -251
- package/dist/data-structures/binary-tree/bst.js +256 -358
- package/dist/data-structures/binary-tree/rb-tree.d.ts +74 -85
- package/dist/data-structures/binary-tree/rb-tree.js +111 -119
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +92 -76
- 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/heap/heap.d.ts +1 -3
- 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 +7 -7
- package/dist/types/common.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +4 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +4 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +6 -5
- package/dist/types/data-structures/binary-tree/bst.d.ts +6 -5
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +4 -3
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -3
- package/dist/types/utils/utils.d.ts +10 -1
- package/dist/utils/utils.d.ts +2 -1
- package/dist/utils/utils.js +27 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +142 -100
- package/src/data-structures/binary-tree/avl-tree.ts +109 -80
- package/src/data-structures/binary-tree/binary-tree.ts +556 -433
- package/src/data-structures/binary-tree/bst.ts +286 -375
- package/src/data-structures/binary-tree/rb-tree.ts +132 -125
- package/src/data-structures/binary-tree/tree-multi-map.ts +129 -102
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/hash/hash-map.ts +42 -49
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/queue/deque.ts +2 -2
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/trie/trie.ts +2 -2
- package/src/interfaces/binary-tree.ts +11 -9
- package/src/types/common.ts +2 -3
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +4 -3
- package/src/types/data-structures/binary-tree/avl-tree.ts +4 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +7 -6
- package/src/types/data-structures/binary-tree/bst.ts +6 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +4 -3
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -3
- package/src/types/utils/utils.ts +14 -1
- package/src/utils/utils.ts +20 -1
|
@@ -60,24 +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
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
* to
|
|
67
|
-
* @param [options] -
|
|
68
|
-
*
|
|
69
|
-
*/
|
|
70
|
-
constructor(
|
|
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) {
|
|
71
71
|
super([], options);
|
|
72
|
-
this.
|
|
72
|
+
this._root = undefined;
|
|
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
|
+
}
|
|
81
|
+
if (a > b)
|
|
82
|
+
return 1;
|
|
83
|
+
if (a < b)
|
|
84
|
+
return -1;
|
|
85
|
+
return 0;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Time complexity: O(n)
|
|
89
|
+
* Space complexity: O(n)
|
|
90
|
+
*/
|
|
91
|
+
this._comparator = this._DEFAULT_COMPARATOR;
|
|
73
92
|
if (options) {
|
|
74
|
-
const {
|
|
75
|
-
if (
|
|
76
|
-
this.
|
|
93
|
+
const { comparator } = options;
|
|
94
|
+
if (comparator)
|
|
95
|
+
this._comparator = comparator;
|
|
77
96
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
this.addMany(keysOrNodesOrEntries);
|
|
97
|
+
if (keysOrNodesOrEntriesOrRawElements)
|
|
98
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
81
99
|
}
|
|
82
100
|
/**
|
|
83
101
|
* The function returns the root node of a tree structure.
|
|
@@ -86,13 +104,6 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
86
104
|
get root() {
|
|
87
105
|
return this._root;
|
|
88
106
|
}
|
|
89
|
-
/**
|
|
90
|
-
* The function returns the value of the _variant property.
|
|
91
|
-
* @returns The value of the `_variant` property.
|
|
92
|
-
*/
|
|
93
|
-
get variant() {
|
|
94
|
-
return this._variant;
|
|
95
|
-
}
|
|
96
107
|
/**
|
|
97
108
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
98
109
|
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
@@ -107,106 +118,68 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
107
118
|
/**
|
|
108
119
|
* The function creates a new binary search tree with the specified options.
|
|
109
120
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
110
|
-
* behavior of the `createTree` method. It
|
|
111
|
-
*
|
|
112
|
-
* @returns a new instance of the BST class
|
|
113
|
-
* 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.
|
|
114
124
|
*/
|
|
115
125
|
createTree(options) {
|
|
116
|
-
return new BST([], Object.assign({ iterationType: this.iterationType,
|
|
126
|
+
return new BST([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
|
|
117
127
|
}
|
|
118
128
|
/**
|
|
119
|
-
* The function
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
* `
|
|
124
|
-
*
|
|
125
|
-
|
|
126
|
-
keyValueOrEntryToNode(keyOrNodeOrEntry, value) {
|
|
127
|
-
let node;
|
|
128
|
-
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
else if (this.isNode(keyOrNodeOrEntry)) {
|
|
132
|
-
node = keyOrNodeOrEntry;
|
|
133
|
-
}
|
|
134
|
-
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
135
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
136
|
-
if (key === undefined || key === null) {
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
else {
|
|
140
|
-
node = this.createNode(key, value);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
144
|
-
node = this.createNode(keyOrNodeOrEntry, value);
|
|
145
|
-
}
|
|
146
|
-
else {
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
return node;
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Time Complexity: O(log n)
|
|
153
|
-
* 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.
|
|
154
136
|
*/
|
|
137
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) {
|
|
138
|
+
var _a;
|
|
139
|
+
return (_a = super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value)) !== null && _a !== void 0 ? _a : undefined;
|
|
140
|
+
}
|
|
155
141
|
/**
|
|
156
142
|
* Time Complexity: O(log n)
|
|
157
143
|
* Space Complexity: O(log n)
|
|
158
144
|
*
|
|
159
|
-
* The function
|
|
160
|
-
*
|
|
161
|
-
* @param {
|
|
162
|
-
* `
|
|
163
|
-
*
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
if (this.isEntry(keyOrNodeOrEntry)) {
|
|
174
|
-
const key = keyOrNodeOrEntry[0];
|
|
175
|
-
if (key === null || key === undefined)
|
|
176
|
-
return;
|
|
177
|
-
return this.getNodeByKey(key, iterationType);
|
|
178
|
-
}
|
|
179
|
-
const key = keyOrNodeOrEntry;
|
|
180
|
-
if (key === null || key === undefined)
|
|
181
|
-
return;
|
|
182
|
-
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;
|
|
183
159
|
}
|
|
184
160
|
/**
|
|
185
|
-
* The function checks if
|
|
186
|
-
* @param
|
|
187
|
-
*
|
|
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.
|
|
188
166
|
*/
|
|
189
|
-
isNode(
|
|
190
|
-
return
|
|
167
|
+
isNode(keyOrNodeOrEntryOrRawElement) {
|
|
168
|
+
return keyOrNodeOrEntryOrRawElement instanceof BSTNode;
|
|
191
169
|
}
|
|
192
|
-
/**
|
|
193
|
-
* Time Complexity: O(log n)
|
|
194
|
-
* Space Complexity: O(1)
|
|
195
|
-
*/
|
|
196
170
|
/**
|
|
197
171
|
* Time Complexity: O(log n)
|
|
198
172
|
* Space Complexity: O(1)
|
|
199
173
|
*
|
|
200
|
-
* The `add` function adds a new node to a binary tree
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
* @param {V} [value] - The `value` parameter
|
|
204
|
-
*
|
|
205
|
-
* @returns
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
|
|
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);
|
|
210
183
|
if (newNode === undefined)
|
|
211
184
|
return false;
|
|
212
185
|
if (this.root === undefined) {
|
|
@@ -216,18 +189,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
216
189
|
}
|
|
217
190
|
let current = this.root;
|
|
218
191
|
while (current !== undefined) {
|
|
219
|
-
if (this.
|
|
220
|
-
// if (current !== newNode) {
|
|
221
|
-
// The key value is the same but the reference is different, update the value of the existing node
|
|
192
|
+
if (this.comparator(current.key, newNode.key) === 0) {
|
|
222
193
|
this._replaceNode(current, newNode);
|
|
223
194
|
return true;
|
|
224
|
-
// } else {
|
|
225
|
-
// The key value is the same and the reference is the same, replace the entire node
|
|
226
|
-
// this._replaceNode(current, newNode);
|
|
227
|
-
// return;
|
|
228
|
-
// }
|
|
229
195
|
}
|
|
230
|
-
else if (this.
|
|
196
|
+
else if (this.comparator(current.key, newNode.key) > 0) {
|
|
231
197
|
if (current.left === undefined) {
|
|
232
198
|
current.left = newNode;
|
|
233
199
|
this._size++;
|
|
@@ -247,37 +213,38 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
247
213
|
return false;
|
|
248
214
|
}
|
|
249
215
|
/**
|
|
250
|
-
* Time Complexity: O(
|
|
251
|
-
* Space Complexity: O(
|
|
216
|
+
* Time Complexity: O(log n)
|
|
217
|
+
* Space Complexity: O(log n)
|
|
252
218
|
*/
|
|
253
219
|
/**
|
|
254
220
|
* Time Complexity: O(k log n)
|
|
255
221
|
* Space Complexity: O(k + log n)
|
|
256
222
|
*
|
|
257
|
-
* The `addMany` function in TypeScript adds multiple keys or nodes to a
|
|
258
|
-
*
|
|
259
|
-
* @param
|
|
260
|
-
* the
|
|
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.
|
|
261
227
|
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
262
228
|
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
263
229
|
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
264
|
-
* @param [isBalanceAdd=true] - A boolean flag indicating whether the
|
|
265
|
-
*
|
|
266
|
-
* algorithm. If set to false, the
|
|
267
|
-
*
|
|
268
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
269
|
-
* type of iteration to use when adding multiple keys or nodes
|
|
270
|
-
*
|
|
271
|
-
* @returns The function `addMany` returns an array of
|
|
272
|
-
|
|
273
|
-
|
|
230
|
+
* @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
|
|
231
|
+
* adding the elements. If set to true, the tree will be balanced using a binary search tree
|
|
232
|
+
* algorithm. If set to false, the elements will be added without balancing the tree. The default
|
|
233
|
+
* value is true.
|
|
234
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
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.
|
|
239
|
+
*/
|
|
240
|
+
addMany(keysOrNodesOrEntriesOrRawElements, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
274
241
|
const inserted = [];
|
|
275
242
|
let valuesIterator;
|
|
276
243
|
if (values) {
|
|
277
244
|
valuesIterator = values[Symbol.iterator]();
|
|
278
245
|
}
|
|
279
246
|
if (!isBalanceAdd) {
|
|
280
|
-
for (const kve of
|
|
247
|
+
for (const kve of keysOrNodesOrEntriesOrRawElements) {
|
|
281
248
|
const value = valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value;
|
|
282
249
|
const nn = this.add(kve, value);
|
|
283
250
|
inserted.push(nn);
|
|
@@ -290,25 +257,36 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
290
257
|
return false;
|
|
291
258
|
return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
|
|
292
259
|
};
|
|
293
|
-
for (const kve of
|
|
260
|
+
for (const kve of keysOrNodesOrEntriesOrRawElements) {
|
|
294
261
|
isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
|
|
295
262
|
}
|
|
296
263
|
let sorted = [];
|
|
297
264
|
sorted = realBTNExemplars.sort((a, b) => {
|
|
298
|
-
let
|
|
265
|
+
let keyA, keyB;
|
|
299
266
|
if (this.isEntry(a))
|
|
300
|
-
|
|
267
|
+
keyA = a[0];
|
|
301
268
|
else if (this.isRealNode(a))
|
|
302
|
-
|
|
303
|
-
else
|
|
304
|
-
|
|
269
|
+
keyA = a.key;
|
|
270
|
+
else if (this.toEntryFn) {
|
|
271
|
+
keyA = this.toEntryFn(a)[0];
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
keyA = a;
|
|
275
|
+
}
|
|
305
276
|
if (this.isEntry(b))
|
|
306
|
-
|
|
277
|
+
keyB = b[0];
|
|
307
278
|
else if (this.isRealNode(b))
|
|
308
|
-
|
|
309
|
-
else
|
|
310
|
-
|
|
311
|
-
|
|
279
|
+
keyB = b.key;
|
|
280
|
+
else if (this.toEntryFn) {
|
|
281
|
+
keyB = this.toEntryFn(b)[0];
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
keyB = b;
|
|
285
|
+
}
|
|
286
|
+
if (keyA !== undefined && keyA !== null && keyB !== undefined && keyB !== null) {
|
|
287
|
+
return this.comparator(keyA, keyB);
|
|
288
|
+
}
|
|
289
|
+
return 0;
|
|
312
290
|
});
|
|
313
291
|
const _dfs = (arr) => {
|
|
314
292
|
if (arr.length === 0)
|
|
@@ -345,32 +323,26 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
345
323
|
return inserted;
|
|
346
324
|
}
|
|
347
325
|
/**
|
|
348
|
-
* Time Complexity: O(log n)
|
|
349
|
-
* Space Complexity: O(k + log n)
|
|
350
|
-
* /
|
|
351
|
-
|
|
352
|
-
/**
|
|
353
326
|
* Time Complexity: O(log n)
|
|
354
327
|
* Space Complexity: O(k + log n)
|
|
355
328
|
*
|
|
356
|
-
* The
|
|
357
|
-
*
|
|
329
|
+
* The `getNodes` function in TypeScript retrieves nodes from a binary tree based on a given
|
|
330
|
+
* identifier and callback function.
|
|
358
331
|
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
359
|
-
* want to search for in the
|
|
360
|
-
*
|
|
361
|
-
* @param {C} callback - The `callback` parameter is a function that takes a node
|
|
362
|
-
*
|
|
363
|
-
* function
|
|
364
|
-
* @param [onlyOne=false] - A boolean
|
|
365
|
-
*
|
|
366
|
-
*
|
|
367
|
-
*
|
|
368
|
-
*
|
|
369
|
-
*
|
|
370
|
-
*
|
|
371
|
-
*
|
|
372
|
-
*
|
|
373
|
-
* @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.
|
|
374
346
|
*/
|
|
375
347
|
getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
376
348
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -390,9 +362,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
390
362
|
return;
|
|
391
363
|
// TODO potential bug
|
|
392
364
|
if (callback === this._DEFAULT_CALLBACK) {
|
|
393
|
-
if (this.isRealNode(cur.left) && this.
|
|
365
|
+
if (this.isRealNode(cur.left) && this.comparator(cur.key, identifier) > 0)
|
|
394
366
|
dfs(cur.left);
|
|
395
|
-
if (this.isRealNode(cur.right) && this.
|
|
367
|
+
if (this.isRealNode(cur.right) && this.comparator(cur.key, identifier) < 0)
|
|
396
368
|
dfs(cur.right);
|
|
397
369
|
}
|
|
398
370
|
else {
|
|
@@ -414,9 +386,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
414
386
|
}
|
|
415
387
|
// TODO potential bug
|
|
416
388
|
if (callback === this._DEFAULT_CALLBACK) {
|
|
417
|
-
if (this.isRealNode(cur.right) && this.
|
|
389
|
+
if (this.isRealNode(cur.right) && this.comparator(cur.key, identifier) < 0)
|
|
418
390
|
stack.push(cur.right);
|
|
419
|
-
if (this.isRealNode(cur.left) && this.
|
|
391
|
+
if (this.isRealNode(cur.left) && this.comparator(cur.key, identifier) > 0)
|
|
420
392
|
stack.push(cur.left);
|
|
421
393
|
// if (this.isRealNode(cur.right) && this._lt(cur.key, identifier as K)) stack.push(cur.right);
|
|
422
394
|
// if (this.isRealNode(cur.left) && this._gt(cur.key, identifier as K)) stack.push(cur.left);
|
|
@@ -441,51 +413,50 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
441
413
|
* Time Complexity: O(log n)
|
|
442
414
|
* Space Complexity: O(1)
|
|
443
415
|
*
|
|
444
|
-
* The `getNode`
|
|
445
|
-
*
|
|
446
|
-
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value
|
|
447
|
-
*
|
|
448
|
-
*
|
|
449
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
450
|
-
* the
|
|
451
|
-
*
|
|
452
|
-
*
|
|
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
|
|
453
425
|
* @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
|
|
454
|
-
* search tree. It can be either a key or a node. If it is a key,
|
|
455
|
-
*
|
|
456
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type
|
|
457
|
-
* be performed when searching for nodes in the binary search tree. It
|
|
458
|
-
*
|
|
459
|
-
* @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.
|
|
460
432
|
*/
|
|
461
433
|
getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
462
434
|
var _a;
|
|
463
435
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
464
436
|
}
|
|
465
437
|
/**
|
|
466
|
-
* Time Complexity: O(log n)
|
|
467
|
-
* Space Complexity: O(
|
|
438
|
+
* Time Complexity: O(k log n)
|
|
439
|
+
* Space Complexity: O(k + log n)
|
|
468
440
|
*/
|
|
469
441
|
/**
|
|
470
442
|
* Time Complexity: O(log n)
|
|
471
443
|
* Space Complexity: O(1)
|
|
472
444
|
*
|
|
473
|
-
* The function `getNodeByKey`
|
|
474
|
-
*
|
|
475
|
-
*
|
|
476
|
-
*
|
|
477
|
-
* @param iterationType - The `iterationType` parameter is an optional
|
|
478
|
-
* type of iteration to
|
|
479
|
-
*
|
|
480
|
-
* @returns The
|
|
481
|
-
* 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.
|
|
482
453
|
*/
|
|
483
454
|
getNodeByKey(key, iterationType = 'ITERATIVE') {
|
|
484
455
|
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
485
456
|
}
|
|
486
457
|
/**
|
|
487
|
-
* Time
|
|
488
|
-
* Space
|
|
458
|
+
* Time Complexity: O(log n)
|
|
459
|
+
* Space Complexity: O(k + log n)
|
|
489
460
|
*/
|
|
490
461
|
/**
|
|
491
462
|
* Time complexity: O(n)
|
|
@@ -494,15 +465,16 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
494
465
|
* The function overrides the depth-first search method and returns an array of the return types of
|
|
495
466
|
* the callback function.
|
|
496
467
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
497
|
-
* during the depth-first search traversal. It is an optional parameter and
|
|
498
|
-
*
|
|
499
|
-
* @param {DFSOrderPattern} [pattern=
|
|
500
|
-
*
|
|
501
|
-
*
|
|
502
|
-
*
|
|
503
|
-
*
|
|
504
|
-
*
|
|
505
|
-
*
|
|
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
|
|
506
478
|
* following values:
|
|
507
479
|
* @returns The method is returning an array of the return type of the callback function.
|
|
508
480
|
*/
|
|
@@ -510,8 +482,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
510
482
|
return super.dfs(callback, pattern, beginRoot, iterationType, false);
|
|
511
483
|
}
|
|
512
484
|
/**
|
|
513
|
-
* Time
|
|
514
|
-
* Space
|
|
485
|
+
* Time Complexity: O(log n)
|
|
486
|
+
* Space Complexity: O(1)
|
|
515
487
|
*/
|
|
516
488
|
/**
|
|
517
489
|
* Time complexity: O(n)
|
|
@@ -520,38 +492,38 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
520
492
|
* The function overrides the breadth-first search method and returns an array of the return types of
|
|
521
493
|
* the callback function.
|
|
522
494
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
523
|
-
* visited during the breadth-first search
|
|
524
|
-
*
|
|
525
|
-
* @param beginRoot - The `beginRoot` parameter is the starting
|
|
526
|
-
*
|
|
527
|
-
* the
|
|
528
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type
|
|
529
|
-
* be performed during the breadth-first search (BFS) traversal. It
|
|
530
|
-
*
|
|
531
|
-
* @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.
|
|
532
504
|
*/
|
|
533
505
|
bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
534
506
|
return super.bfs(callback, beginRoot, iterationType, false);
|
|
535
507
|
}
|
|
536
508
|
/**
|
|
537
|
-
* Time
|
|
538
|
-
* Space
|
|
509
|
+
* Time Complexity: O(log n)
|
|
510
|
+
* Space Complexity: O(1)
|
|
539
511
|
*/
|
|
540
512
|
/**
|
|
541
513
|
* Time complexity: O(n)
|
|
542
514
|
* Space complexity: O(n)
|
|
543
515
|
*
|
|
544
|
-
* The function overrides the listLevels method and returns an array of arrays
|
|
545
|
-
*
|
|
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.
|
|
546
518
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
547
|
-
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
548
|
-
* during the
|
|
549
|
-
* @param beginRoot - The `beginRoot` parameter is
|
|
550
|
-
* levels of
|
|
551
|
-
*
|
|
552
|
-
*
|
|
553
|
-
*
|
|
554
|
-
* 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:
|
|
555
527
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
556
528
|
* function.
|
|
557
529
|
*/
|
|
@@ -559,77 +531,42 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
559
531
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
560
532
|
}
|
|
561
533
|
/**
|
|
562
|
-
* Time
|
|
563
|
-
* Space
|
|
564
|
-
*/
|
|
565
|
-
/**
|
|
566
|
-
* Time Complexity: O(log n)
|
|
567
|
-
* Space Complexity: O(1)
|
|
568
|
-
*
|
|
569
|
-
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
570
|
-
* leftmost node if the comparison result is greater than.
|
|
571
|
-
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
572
|
-
* type `K`, `NODE`, or `undefined`. It represents the starting point for finding the last key in
|
|
573
|
-
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
574
|
-
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
575
|
-
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
576
|
-
* rightmost node otherwise. If no node is found, it returns 0.
|
|
577
|
-
*/
|
|
578
|
-
lastKey(beginRoot = this.root) {
|
|
579
|
-
let current = this.ensureNode(beginRoot);
|
|
580
|
-
if (!current)
|
|
581
|
-
return undefined;
|
|
582
|
-
if (this._variant === 'STANDARD') {
|
|
583
|
-
// For 'STANDARD', find the rightmost node
|
|
584
|
-
while (current.right !== undefined) {
|
|
585
|
-
current = current.right;
|
|
586
|
-
}
|
|
587
|
-
}
|
|
588
|
-
else {
|
|
589
|
-
// For BSTVariant.MAX, find the leftmost node
|
|
590
|
-
while (current.left !== undefined) {
|
|
591
|
-
current = current.left;
|
|
592
|
-
}
|
|
593
|
-
}
|
|
594
|
-
return current.key;
|
|
595
|
-
}
|
|
596
|
-
/**
|
|
597
|
-
* Time Complexity: O(log n)
|
|
598
|
-
* Space Complexity: O(log n)
|
|
534
|
+
* Time complexity: O(n)
|
|
535
|
+
* Space complexity: O(n)
|
|
599
536
|
*/
|
|
600
537
|
/**
|
|
601
|
-
* Time
|
|
602
|
-
* Space
|
|
538
|
+
* Time complexity: O(n)
|
|
539
|
+
* Space complexity: O(n)
|
|
603
540
|
*
|
|
604
|
-
* The `lesserOrGreaterTraverse` function traverses a binary tree and
|
|
605
|
-
*
|
|
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.
|
|
606
543
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
607
|
-
* that
|
|
608
|
-
*
|
|
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.
|
|
609
546
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
610
|
-
* traverse nodes that are lesser
|
|
611
|
-
*
|
|
612
|
-
* `
|
|
613
|
-
*
|
|
614
|
-
*
|
|
615
|
-
*
|
|
616
|
-
* @param iterationType - The `iterationType` parameter determines the type of
|
|
617
|
-
* 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:
|
|
618
555
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
619
556
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
620
557
|
*/
|
|
621
|
-
lesserOrGreaterTraverse(callback = this._DEFAULT_CALLBACK, lesserOrGreater =
|
|
622
|
-
|
|
558
|
+
lesserOrGreaterTraverse(callback = this._DEFAULT_CALLBACK, lesserOrGreater = -1, targetNode = this.root, iterationType = this.iterationType) {
|
|
559
|
+
const targetNodeEnsured = this.ensureNode(targetNode);
|
|
623
560
|
const ans = [];
|
|
624
|
-
if (!
|
|
561
|
+
if (!targetNodeEnsured)
|
|
625
562
|
return ans;
|
|
626
563
|
if (!this.root)
|
|
627
564
|
return ans;
|
|
628
|
-
const targetKey =
|
|
565
|
+
const targetKey = targetNodeEnsured.key;
|
|
629
566
|
if (iterationType === 'RECURSIVE') {
|
|
630
567
|
const dfs = (cur) => {
|
|
631
|
-
const compared = this.
|
|
632
|
-
if (compared === lesserOrGreater)
|
|
568
|
+
const compared = this.comparator(cur.key, targetKey);
|
|
569
|
+
if (Math.sign(compared) === lesserOrGreater)
|
|
633
570
|
ans.push(callback(cur));
|
|
634
571
|
if (this.isRealNode(cur.left))
|
|
635
572
|
dfs(cur.left);
|
|
@@ -644,8 +581,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
644
581
|
while (queue.size > 0) {
|
|
645
582
|
const cur = queue.shift();
|
|
646
583
|
if (this.isRealNode(cur)) {
|
|
647
|
-
const compared = this.
|
|
648
|
-
if (compared === lesserOrGreater)
|
|
584
|
+
const compared = this.comparator(cur.key, targetKey);
|
|
585
|
+
if (Math.sign(compared) === lesserOrGreater)
|
|
649
586
|
ans.push(callback(cur));
|
|
650
587
|
if (this.isRealNode(cur.left))
|
|
651
588
|
queue.push(cur.left);
|
|
@@ -657,18 +594,19 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
657
594
|
}
|
|
658
595
|
}
|
|
659
596
|
/**
|
|
660
|
-
* Time
|
|
661
|
-
* Space
|
|
597
|
+
* Time complexity: O(n)
|
|
598
|
+
* Space complexity: O(n)
|
|
662
599
|
*/
|
|
663
600
|
/**
|
|
664
|
-
* Time
|
|
665
|
-
* Space
|
|
601
|
+
* Time complexity: O(n)
|
|
602
|
+
* Space complexity: O(n)
|
|
666
603
|
*
|
|
667
|
-
* The `perfectlyBalance` function
|
|
668
|
-
*
|
|
669
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
670
|
-
* type of iteration to use when building a balanced binary search tree. It
|
|
671
|
-
*
|
|
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.
|
|
672
610
|
* @returns The function `perfectlyBalance` returns a boolean value.
|
|
673
611
|
*/
|
|
674
612
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
@@ -708,25 +646,19 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
708
646
|
}
|
|
709
647
|
}
|
|
710
648
|
/**
|
|
711
|
-
*
|
|
712
|
-
*
|
|
713
|
-
* 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.
|
|
714
|
-
*
|
|
715
|
-
* Use Cases and Efficiency:
|
|
716
|
-
* 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.
|
|
717
|
-
* 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).
|
|
718
|
-
*/
|
|
719
|
-
/**
|
|
720
|
-
* Time Complexity: O(n)
|
|
721
|
-
* Space Complexity: O(log n)
|
|
649
|
+
* Time complexity: O(n)
|
|
650
|
+
* Space complexity: O(n)
|
|
722
651
|
*/
|
|
723
652
|
/**
|
|
724
653
|
* Time Complexity: O(n)
|
|
725
654
|
* Space Complexity: O(log n)
|
|
726
655
|
*
|
|
727
|
-
* The function checks if a binary tree is AVL balanced using either recursive or
|
|
728
|
-
*
|
|
729
|
-
*
|
|
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.
|
|
730
662
|
* @returns a boolean value.
|
|
731
663
|
*/
|
|
732
664
|
isAVLBalanced(iterationType = this.iterationType) {
|
|
@@ -776,9 +708,20 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
776
708
|
return balanced;
|
|
777
709
|
}
|
|
778
710
|
/**
|
|
779
|
-
*
|
|
780
|
-
*
|
|
781
|
-
|
|
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.
|
|
782
725
|
*/
|
|
783
726
|
_setRoot(v) {
|
|
784
727
|
if (v) {
|
|
@@ -786,50 +729,5 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
786
729
|
}
|
|
787
730
|
this._root = v;
|
|
788
731
|
}
|
|
789
|
-
/**
|
|
790
|
-
* The function compares two values using a comparator function and returns whether the first value
|
|
791
|
-
* is greater than, less than, or equal to the second value.
|
|
792
|
-
* @param {K} a - The parameter "a" is of type K.
|
|
793
|
-
* @param {K} b - The parameter "b" in the above code represents a K.
|
|
794
|
-
* @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
|
|
795
|
-
* than), 'LT' (less than), or 'EQ' (equal).
|
|
796
|
-
*/
|
|
797
|
-
_compare(a, b) {
|
|
798
|
-
const extractedA = this.extractor(a);
|
|
799
|
-
const extractedB = this.extractor(b);
|
|
800
|
-
const compared = this.variant === 'STANDARD' ? extractedA - extractedB : extractedB - extractedA;
|
|
801
|
-
if (compared > 0)
|
|
802
|
-
return 'GT';
|
|
803
|
-
if (compared < 0)
|
|
804
|
-
return 'LT';
|
|
805
|
-
return 'EQ';
|
|
806
|
-
}
|
|
807
|
-
/**
|
|
808
|
-
* The function `_lt` compares two values `a` and `b` using an extractor function and returns true if
|
|
809
|
-
* `a` is less than `b` based on the specified variant.
|
|
810
|
-
* @param {K} a - The parameter "a" is of type "K", which means it can be any type. It represents the
|
|
811
|
-
* first value to be compared in the function.
|
|
812
|
-
* @param {K} b - The parameter `b` is of type `K`, which means it can be any type. It is used as one
|
|
813
|
-
* of the arguments for the comparison in the `_lt` function.
|
|
814
|
-
* @returns a boolean value.
|
|
815
|
-
*/
|
|
816
|
-
_lt(a, b) {
|
|
817
|
-
const extractedA = this.extractor(a);
|
|
818
|
-
const extractedB = this.extractor(b);
|
|
819
|
-
return this.variant === 'STANDARD' ? extractedA < extractedB : extractedA > extractedB;
|
|
820
|
-
}
|
|
821
|
-
/**
|
|
822
|
-
* The function compares two values using a custom extractor function and returns true if the first
|
|
823
|
-
* value is greater than the second value.
|
|
824
|
-
* @param {K} a - The parameter "a" is of type K, which means it can be any type.
|
|
825
|
-
* @param {K} b - The parameter "b" is of type K, which means it can be any type. It is used as one
|
|
826
|
-
* of the arguments for the comparison in the function.
|
|
827
|
-
* @returns a boolean value.
|
|
828
|
-
*/
|
|
829
|
-
_gt(a, b) {
|
|
830
|
-
const extractedA = this.extractor(a);
|
|
831
|
-
const extractedB = this.extractor(b);
|
|
832
|
-
return this.variant === 'STANDARD' ? extractedA > extractedB : extractedA < extractedB;
|
|
833
|
-
}
|
|
834
732
|
}
|
|
835
733
|
exports.BST = BST;
|