undirected-graph-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
|
@@ -11,7 +11,7 @@ import type {
|
|
|
11
11
|
BSTNodeNested,
|
|
12
12
|
BSTOptions,
|
|
13
13
|
BTNCallback,
|
|
14
|
-
|
|
14
|
+
BTNPureKeyOrNodeOrEntry,
|
|
15
15
|
Comparable,
|
|
16
16
|
Comparator,
|
|
17
17
|
CP,
|
|
@@ -19,6 +19,7 @@ import type {
|
|
|
19
19
|
IterationType,
|
|
20
20
|
KeyOrNodeOrEntry
|
|
21
21
|
} from '../../types';
|
|
22
|
+
import { BTNEntry } from '../../types';
|
|
22
23
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
23
24
|
import { IBinaryTree } from '../../interfaces';
|
|
24
25
|
import { Queue } from '../queue';
|
|
@@ -95,21 +96,24 @@ export class BSTNode<
|
|
|
95
96
|
export class BST<
|
|
96
97
|
K extends Comparable,
|
|
97
98
|
V = any,
|
|
99
|
+
R = BTNEntry<K, V>,
|
|
98
100
|
NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>,
|
|
99
|
-
TREE extends BST<K, V, NODE, TREE> = BST<K, V, NODE, BSTNested<K, V, NODE>>
|
|
101
|
+
TREE extends BST<K, V, R, NODE, TREE> = BST<K, V, R, NODE, BSTNested<K, V, R, NODE>>
|
|
100
102
|
>
|
|
101
|
-
extends BinaryTree<K, V, NODE, TREE>
|
|
102
|
-
implements IBinaryTree<K, V, NODE, TREE> {
|
|
103
|
-
/**
|
|
104
|
-
* This is the constructor function for a Binary Search Tree class in TypeScript
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
103
|
+
extends BinaryTree<K, V, R, NODE, TREE>
|
|
104
|
+
implements IBinaryTree<K, V, R, NODE, TREE> {
|
|
105
|
+
/**
|
|
106
|
+
* This is the constructor function for a Binary Search Tree class in TypeScript.
|
|
107
|
+
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
108
|
+
* iterable that can contain either keys, nodes, entries, or raw elements. These elements will be
|
|
109
|
+
* added to the binary search tree during the construction of the object.
|
|
110
|
+
* @param [options] - An optional object that contains additional options for the Binary Search Tree.
|
|
111
|
+
* It can include a comparator function that defines the order of the elements in the tree.
|
|
112
|
+
*/
|
|
113
|
+
constructor(
|
|
114
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
115
|
+
options?: BSTOptions<K, V, R>
|
|
116
|
+
) {
|
|
113
117
|
super([], options);
|
|
114
118
|
|
|
115
119
|
if (options) {
|
|
@@ -117,7 +121,7 @@ export class BST<
|
|
|
117
121
|
if (comparator) this._comparator = comparator;
|
|
118
122
|
}
|
|
119
123
|
|
|
120
|
-
if (
|
|
124
|
+
if (keysOrNodesOrEntriesOrRawElements) this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
121
125
|
}
|
|
122
126
|
|
|
123
127
|
protected override _root?: NODE = undefined;
|
|
@@ -130,20 +134,6 @@ export class BST<
|
|
|
130
134
|
return this._root;
|
|
131
135
|
}
|
|
132
136
|
|
|
133
|
-
protected _comparator: Comparator<K> = (a: K, b: K): CP => {
|
|
134
|
-
if (a > b) return 1;
|
|
135
|
-
if (a < b) return -1;
|
|
136
|
-
return 0;
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* The function returns the value of the _comparator property.
|
|
141
|
-
* @returns The `_comparator` property is being returned.
|
|
142
|
-
*/
|
|
143
|
-
get comparator() {
|
|
144
|
-
return this._comparator;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
137
|
/**
|
|
148
138
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
149
139
|
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
@@ -159,13 +149,12 @@ export class BST<
|
|
|
159
149
|
/**
|
|
160
150
|
* The function creates a new binary search tree with the specified options.
|
|
161
151
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
162
|
-
* behavior of the `createTree` method. It
|
|
163
|
-
*
|
|
164
|
-
* @returns a new instance of the BST class
|
|
165
|
-
* options. The returned value is casted as TREE.
|
|
152
|
+
* behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which has the
|
|
153
|
+
* following properties:
|
|
154
|
+
* @returns a new instance of the BST class with the provided options.
|
|
166
155
|
*/
|
|
167
|
-
override createTree(options?: Partial<BSTOptions<K>>): TREE {
|
|
168
|
-
return new BST<K, V, NODE, TREE>([], {
|
|
156
|
+
override createTree(options?: Partial<BSTOptions<K, V, R>>): TREE {
|
|
157
|
+
return new BST<K, V, R, NODE, TREE>([], {
|
|
169
158
|
iterationType: this.iterationType,
|
|
170
159
|
comparator: this.comparator,
|
|
171
160
|
...options
|
|
@@ -173,97 +162,69 @@ export class BST<
|
|
|
173
162
|
}
|
|
174
163
|
|
|
175
164
|
/**
|
|
176
|
-
* The function
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
* `
|
|
181
|
-
*
|
|
182
|
-
|
|
183
|
-
override keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
|
|
184
|
-
let node: NODE | undefined;
|
|
185
|
-
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
186
|
-
return;
|
|
187
|
-
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
188
|
-
node = keyOrNodeOrEntry;
|
|
189
|
-
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
190
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
191
|
-
if (key === undefined || key === null) {
|
|
192
|
-
return;
|
|
193
|
-
} else {
|
|
194
|
-
node = this.createNode(key, value);
|
|
195
|
-
}
|
|
196
|
-
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
197
|
-
node = this.createNode(keyOrNodeOrEntry, value);
|
|
198
|
-
} else {
|
|
199
|
-
return;
|
|
200
|
-
}
|
|
201
|
-
return node;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Time Complexity: O(log n)
|
|
206
|
-
* Space Complexity: O(log n)
|
|
165
|
+
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
166
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - A variable that can be of
|
|
167
|
+
* type R or KeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
168
|
+
* element.
|
|
169
|
+
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
170
|
+
* value associated with a key in a key-value pair.
|
|
171
|
+
* @returns either a NODE object or undefined.
|
|
207
172
|
*/
|
|
173
|
+
override keyValueOrEntryOrRawElementToNode(
|
|
174
|
+
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
175
|
+
value?: V
|
|
176
|
+
): NODE | undefined {
|
|
177
|
+
return super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) ?? undefined;
|
|
178
|
+
}
|
|
208
179
|
|
|
209
180
|
/**
|
|
210
181
|
* Time Complexity: O(log n)
|
|
211
182
|
* Space Complexity: O(log n)
|
|
212
183
|
*
|
|
213
|
-
* The function
|
|
214
|
-
*
|
|
215
|
-
* @param {
|
|
216
|
-
* `
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
*
|
|
184
|
+
* The function ensures the existence of a node in a data structure and returns it, or undefined if
|
|
185
|
+
* it doesn't exist.
|
|
186
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
187
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, which represents the key, node,
|
|
188
|
+
* entry, or raw element that needs to be ensured in the tree.
|
|
189
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
190
|
+
* parameter that specifies the type of iteration to be used when ensuring a node. It has a default
|
|
191
|
+
* value of `'ITERATIVE'`.
|
|
192
|
+
* @returns The method is returning either the node that was ensured or `undefined` if the node could
|
|
193
|
+
* not be ensured.
|
|
220
194
|
*/
|
|
221
195
|
override ensureNode(
|
|
222
|
-
|
|
196
|
+
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
223
197
|
iterationType: IterationType = 'ITERATIVE'
|
|
224
198
|
): NODE | undefined {
|
|
225
|
-
|
|
226
|
-
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
227
|
-
return keyOrNodeOrEntry;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
if (this.isEntry(keyOrNodeOrEntry)) {
|
|
231
|
-
const key = keyOrNodeOrEntry[0];
|
|
232
|
-
if (key === null || key === undefined) return;
|
|
233
|
-
return this.getNodeByKey(key, iterationType);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
const key = keyOrNodeOrEntry;
|
|
237
|
-
if (key === null || key === undefined) return;
|
|
238
|
-
return this.getNodeByKey(key, iterationType);
|
|
199
|
+
return super.ensureNode(keyOrNodeOrEntryOrRawElement, iterationType) ?? undefined;
|
|
239
200
|
}
|
|
240
201
|
|
|
241
202
|
/**
|
|
242
|
-
* The function checks if
|
|
243
|
-
* @param
|
|
244
|
-
*
|
|
203
|
+
* The function checks if the input is an instance of the BSTNode class.
|
|
204
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
205
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
206
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
207
|
+
* an instance of the `BSTNode` class.
|
|
245
208
|
*/
|
|
246
|
-
override isNode(
|
|
247
|
-
|
|
209
|
+
override isNode(
|
|
210
|
+
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
|
|
211
|
+
): keyOrNodeOrEntryOrRawElement is NODE {
|
|
212
|
+
return keyOrNodeOrEntryOrRawElement instanceof BSTNode;
|
|
248
213
|
}
|
|
249
214
|
|
|
250
|
-
/**
|
|
251
|
-
* Time Complexity: O(log n)
|
|
252
|
-
* Space Complexity: O(1)
|
|
253
|
-
*/
|
|
254
|
-
|
|
255
215
|
/**
|
|
256
216
|
* Time Complexity: O(log n)
|
|
257
217
|
* Space Complexity: O(1)
|
|
258
218
|
*
|
|
259
|
-
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value
|
|
260
|
-
*
|
|
261
|
-
*
|
|
262
|
-
* @param {V} [value] - The value
|
|
263
|
-
*
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
219
|
+
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
|
|
220
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
221
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
222
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
223
|
+
* key in the binary search tree. If provided, it will be stored in the node along with the key.
|
|
224
|
+
* @returns a boolean value.
|
|
225
|
+
*/
|
|
226
|
+
override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
|
|
227
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
|
|
267
228
|
if (newNode === undefined) return false;
|
|
268
229
|
|
|
269
230
|
if (this.root === undefined) {
|
|
@@ -275,17 +236,8 @@ export class BST<
|
|
|
275
236
|
let current = this.root;
|
|
276
237
|
while (current !== undefined) {
|
|
277
238
|
if (this.comparator(current.key, newNode.key) === 0) {
|
|
278
|
-
// if (current !== newNode) {
|
|
279
|
-
// The key value is the same but the reference is different, update the value of the existing node
|
|
280
239
|
this._replaceNode(current, newNode);
|
|
281
240
|
return true;
|
|
282
|
-
|
|
283
|
-
// } else {
|
|
284
|
-
// The key value is the same and the reference is the same, replace the entire node
|
|
285
|
-
// this._replaceNode(current, newNode);
|
|
286
|
-
|
|
287
|
-
// return;
|
|
288
|
-
// }
|
|
289
241
|
} else if (this.comparator(current.key, newNode.key) > 0) {
|
|
290
242
|
if (current.left === undefined) {
|
|
291
243
|
current.left = newNode;
|
|
@@ -307,19 +259,18 @@ export class BST<
|
|
|
307
259
|
}
|
|
308
260
|
|
|
309
261
|
/**
|
|
310
|
-
* Time Complexity: O(
|
|
311
|
-
* Space Complexity: O(
|
|
262
|
+
* Time Complexity: O(log n)
|
|
263
|
+
* Space Complexity: O(log n)
|
|
312
264
|
*/
|
|
313
265
|
|
|
314
266
|
/**
|
|
315
267
|
* Time Complexity: O(k log n)
|
|
316
268
|
* Space Complexity: O(k + log n)
|
|
317
269
|
*
|
|
318
|
-
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure
|
|
319
|
-
*
|
|
320
|
-
*
|
|
321
|
-
*
|
|
322
|
-
* data structure.
|
|
270
|
+
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure and returns
|
|
271
|
+
* an array indicating whether each key or node was successfully inserted.
|
|
272
|
+
* @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
|
|
273
|
+
* elements to be added to the data structure.
|
|
323
274
|
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
324
275
|
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
325
276
|
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
@@ -328,14 +279,13 @@ export class BST<
|
|
|
328
279
|
* algorithm. If set to false, the elements will be added without balancing the tree. The default
|
|
329
280
|
* value is true.
|
|
330
281
|
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
331
|
-
* specifies the type of iteration to use when adding multiple keys or nodes to the binary
|
|
332
|
-
*
|
|
333
|
-
*
|
|
334
|
-
*
|
|
335
|
-
* or entry was successfully inserted into the data structure.
|
|
282
|
+
* specifies the type of iteration to use when adding multiple keys or nodes to the binary search
|
|
283
|
+
* tree. It can have two possible values:
|
|
284
|
+
* @returns The function `addMany` returns an array of booleans indicating whether each element was
|
|
285
|
+
* successfully inserted into the data structure.
|
|
336
286
|
*/
|
|
337
287
|
override addMany(
|
|
338
|
-
|
|
288
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>>,
|
|
339
289
|
values?: Iterable<V | undefined>,
|
|
340
290
|
isBalanceAdd = true,
|
|
341
291
|
iterationType: IterationType = this.iterationType
|
|
@@ -349,7 +299,7 @@ export class BST<
|
|
|
349
299
|
}
|
|
350
300
|
|
|
351
301
|
if (!isBalanceAdd) {
|
|
352
|
-
for (const kve of
|
|
302
|
+
for (const kve of keysOrNodesOrEntriesOrRawElements) {
|
|
353
303
|
const value = valuesIterator?.next().value;
|
|
354
304
|
const nn = this.add(kve, value);
|
|
355
305
|
inserted.push(nn);
|
|
@@ -357,29 +307,36 @@ export class BST<
|
|
|
357
307
|
return inserted;
|
|
358
308
|
}
|
|
359
309
|
|
|
360
|
-
const realBTNExemplars:
|
|
310
|
+
const realBTNExemplars: (R | BTNPureKeyOrNodeOrEntry<K, V, NODE>)[] = [];
|
|
361
311
|
|
|
362
|
-
const isRealBTNExemplar = (kve: KeyOrNodeOrEntry<K, V, NODE>): kve is
|
|
312
|
+
const isRealBTNExemplar = (kve: R | KeyOrNodeOrEntry<K, V, NODE>): kve is BTNPureKeyOrNodeOrEntry<K, V, NODE> => {
|
|
363
313
|
if (kve === undefined || kve === null) return false;
|
|
364
314
|
return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
|
|
365
315
|
};
|
|
366
316
|
|
|
367
|
-
for (const kve of
|
|
317
|
+
for (const kve of keysOrNodesOrEntriesOrRawElements) {
|
|
368
318
|
isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
|
|
369
319
|
}
|
|
370
320
|
|
|
371
|
-
let sorted:
|
|
321
|
+
let sorted: (R | BTNPureKeyOrNodeOrEntry<K, V, NODE>)[] = [];
|
|
372
322
|
|
|
373
323
|
sorted = realBTNExemplars.sort((a, b) => {
|
|
374
324
|
let keyA: K | undefined | null, keyB: K | undefined | null;
|
|
375
|
-
if (this.isEntry(a))
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
325
|
+
if (this.isEntry(a)) keyA = a[0];
|
|
326
|
+
else if (this.isRealNode(a)) keyA = a.key;
|
|
327
|
+
else if (this.toEntryFn) {
|
|
328
|
+
keyA = this.toEntryFn(a as R)[0];
|
|
329
|
+
} else {
|
|
330
|
+
keyA = a as K;
|
|
331
|
+
}
|
|
379
332
|
|
|
380
333
|
if (this.isEntry(b)) keyB = b[0];
|
|
381
334
|
else if (this.isRealNode(b)) keyB = b.key;
|
|
382
|
-
else
|
|
335
|
+
else if (this.toEntryFn) {
|
|
336
|
+
keyB = this.toEntryFn(b as R)[0];
|
|
337
|
+
} else {
|
|
338
|
+
keyB = b as K;
|
|
339
|
+
}
|
|
383
340
|
|
|
384
341
|
if (keyA !== undefined && keyA !== null && keyB !== undefined && keyB !== null) {
|
|
385
342
|
return this.comparator(keyA, keyB);
|
|
@@ -387,7 +344,7 @@ export class BST<
|
|
|
387
344
|
return 0;
|
|
388
345
|
});
|
|
389
346
|
|
|
390
|
-
const _dfs = (arr:
|
|
347
|
+
const _dfs = (arr: (R | BTNPureKeyOrNodeOrEntry<K, V, NODE>)[]) => {
|
|
391
348
|
if (arr.length === 0) return;
|
|
392
349
|
|
|
393
350
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
@@ -425,38 +382,32 @@ export class BST<
|
|
|
425
382
|
}
|
|
426
383
|
|
|
427
384
|
/**
|
|
428
|
-
* Time Complexity: O(log n)
|
|
429
|
-
* Space Complexity: O(k + log n)
|
|
430
|
-
* /
|
|
431
|
-
|
|
432
|
-
/**
|
|
433
385
|
* Time Complexity: O(log n)
|
|
434
386
|
* Space Complexity: O(k + log n)
|
|
435
387
|
*
|
|
436
|
-
* The
|
|
437
|
-
*
|
|
388
|
+
* The `getNodes` function in TypeScript retrieves nodes from a binary tree based on a given
|
|
389
|
+
* identifier and callback function.
|
|
438
390
|
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
439
|
-
* want to search for in the
|
|
440
|
-
*
|
|
441
|
-
* @param {C} callback - The `callback` parameter is a function that takes a node
|
|
442
|
-
*
|
|
443
|
-
* function
|
|
444
|
-
* @param [onlyOne=false] - A boolean
|
|
445
|
-
*
|
|
446
|
-
*
|
|
447
|
-
*
|
|
448
|
-
*
|
|
449
|
-
*
|
|
450
|
-
*
|
|
451
|
-
*
|
|
452
|
-
*
|
|
453
|
-
* @returns The method returns an array of nodes (`NODE[]`).
|
|
391
|
+
* want to search for in the binary tree. It can be of any type that is returned by the callback
|
|
392
|
+
* function.
|
|
393
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node as input and
|
|
394
|
+
* returns a value. This value is used to identify the nodes that match the given identifier. The
|
|
395
|
+
* `callback` function is optional and defaults to `this._DEFAULT_CALLBACK`.
|
|
396
|
+
* @param [onlyOne=false] - A boolean value indicating whether to return only the first matching node
|
|
397
|
+
* or all matching nodes. If set to true, only the first matching node will be returned. If set to
|
|
398
|
+
* false, all matching nodes will be returned. The default value is false.
|
|
399
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
400
|
+
* point for the search in the binary tree. It can be either a node object, a key-value pair, or an
|
|
401
|
+
* entry object. If it is not provided, the `root` of the binary tree is used as the starting point.
|
|
402
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
403
|
+
* iteration to be performed. It can have two possible values:
|
|
404
|
+
* @returns The method `getNodes` returns an array of `NODE` objects.
|
|
454
405
|
*/
|
|
455
406
|
override getNodes<C extends BTNCallback<NODE>>(
|
|
456
407
|
identifier: ReturnType<C> | undefined,
|
|
457
408
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
458
409
|
onlyOne = false,
|
|
459
|
-
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
410
|
+
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
460
411
|
iterationType: IterationType = this.iterationType
|
|
461
412
|
): NODE[] {
|
|
462
413
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -524,58 +475,57 @@ export class BST<
|
|
|
524
475
|
* Time Complexity: O(log n)
|
|
525
476
|
* Space Complexity: O(1)
|
|
526
477
|
*
|
|
527
|
-
* The `getNode`
|
|
528
|
-
*
|
|
529
|
-
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value
|
|
530
|
-
*
|
|
531
|
-
*
|
|
532
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
533
|
-
* the
|
|
534
|
-
*
|
|
535
|
-
*
|
|
478
|
+
* The function `getNode` returns the first node that matches the given identifier and callback
|
|
479
|
+
* function in a binary search tree.
|
|
480
|
+
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
481
|
+
* want to search for in the binary search tree. It can be of any type that is compatible with the
|
|
482
|
+
* type returned by the callback function.
|
|
483
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
484
|
+
* node matches the desired criteria. It should be a function that takes a node as an argument and
|
|
485
|
+
* returns a boolean value indicating whether the node matches the criteria or not. If no callback is
|
|
486
|
+
* provided, the default callback will be
|
|
536
487
|
* @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
|
|
537
|
-
* search tree. It can be either a key or a node. If it is a key,
|
|
538
|
-
*
|
|
539
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type
|
|
540
|
-
* be performed when searching for nodes in the binary search tree. It
|
|
541
|
-
*
|
|
542
|
-
* @returns The method is returning a
|
|
488
|
+
* search tree. It can be either a key or a node. If it is a key, the search will start from the node
|
|
489
|
+
* with that key. If it is a node, the search will start from that node.
|
|
490
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
491
|
+
* of iteration to be performed when searching for nodes in the binary search tree. It can have one
|
|
492
|
+
* of the following values:
|
|
493
|
+
* @returns The method is returning a NODE object or undefined.
|
|
543
494
|
*/
|
|
544
495
|
override getNode<C extends BTNCallback<NODE>>(
|
|
545
496
|
identifier: ReturnType<C> | undefined,
|
|
546
497
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
547
|
-
beginRoot: BSTNKeyOrNode<K, NODE> = this.root,
|
|
498
|
+
beginRoot: R | BSTNKeyOrNode<K, NODE> = this.root,
|
|
548
499
|
iterationType: IterationType = this.iterationType
|
|
549
500
|
): NODE | undefined {
|
|
550
501
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
551
502
|
}
|
|
552
503
|
|
|
553
504
|
/**
|
|
554
|
-
* Time Complexity: O(log n)
|
|
555
|
-
* Space Complexity: O(
|
|
505
|
+
* Time Complexity: O(k log n)
|
|
506
|
+
* Space Complexity: O(k + log n)
|
|
556
507
|
*/
|
|
557
508
|
|
|
558
509
|
/**
|
|
559
510
|
* Time Complexity: O(log n)
|
|
560
511
|
* Space Complexity: O(1)
|
|
561
512
|
*
|
|
562
|
-
* The function `getNodeByKey`
|
|
563
|
-
*
|
|
564
|
-
*
|
|
565
|
-
*
|
|
566
|
-
* @param iterationType - The `iterationType` parameter is an optional
|
|
567
|
-
* type of iteration to
|
|
568
|
-
*
|
|
569
|
-
* @returns The
|
|
570
|
-
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
513
|
+
* The function `getNodeByKey` returns a node with a specific key from a tree data structure.
|
|
514
|
+
* @param {K} key - The key parameter is the value used to search for a specific node in the tree. It
|
|
515
|
+
* is typically a unique identifier or a value that can be used to determine the position of the node
|
|
516
|
+
* in the tree structure.
|
|
517
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
518
|
+
* parameter that specifies the type of iteration to be used when searching for a node in the tree.
|
|
519
|
+
* It has a default value of `'ITERATIVE'`.
|
|
520
|
+
* @returns The method is returning a NODE object or undefined.
|
|
571
521
|
*/
|
|
572
522
|
override getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | undefined {
|
|
573
523
|
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
574
524
|
}
|
|
575
525
|
|
|
576
526
|
/**
|
|
577
|
-
* Time
|
|
578
|
-
* Space
|
|
527
|
+
* Time Complexity: O(log n)
|
|
528
|
+
* Space Complexity: O(k + log n)
|
|
579
529
|
*/
|
|
580
530
|
|
|
581
531
|
/**
|
|
@@ -585,30 +535,31 @@ export class BST<
|
|
|
585
535
|
* The function overrides the depth-first search method and returns an array of the return types of
|
|
586
536
|
* the callback function.
|
|
587
537
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
588
|
-
* during the depth-first search traversal. It is an optional parameter and
|
|
589
|
-
*
|
|
590
|
-
* @param {DFSOrderPattern} [pattern=
|
|
591
|
-
*
|
|
592
|
-
*
|
|
593
|
-
*
|
|
594
|
-
*
|
|
595
|
-
*
|
|
596
|
-
*
|
|
538
|
+
* during the depth-first search traversal. It is an optional parameter and defaults to
|
|
539
|
+
* `this._DEFAULT_CALLBACK`. The type `C` represents the type of the callback function.
|
|
540
|
+
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
541
|
+
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
542
|
+
* take one of the following values:
|
|
543
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
544
|
+
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
545
|
+
* node entry. If not specified, the default value is the root of the tree.
|
|
546
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
547
|
+
* type of iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the
|
|
597
548
|
* following values:
|
|
598
549
|
* @returns The method is returning an array of the return type of the callback function.
|
|
599
550
|
*/
|
|
600
551
|
override dfs<C extends BTNCallback<NODE>>(
|
|
601
552
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
602
553
|
pattern: DFSOrderPattern = 'IN',
|
|
603
|
-
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
554
|
+
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
604
555
|
iterationType: IterationType = 'ITERATIVE'
|
|
605
556
|
): ReturnType<C>[] {
|
|
606
557
|
return super.dfs(callback, pattern, beginRoot, iterationType, false);
|
|
607
558
|
}
|
|
608
559
|
|
|
609
560
|
/**
|
|
610
|
-
* Time
|
|
611
|
-
* Space
|
|
561
|
+
* Time Complexity: O(log n)
|
|
562
|
+
* Space Complexity: O(1)
|
|
612
563
|
*/
|
|
613
564
|
|
|
614
565
|
/**
|
|
@@ -618,85 +569,85 @@ export class BST<
|
|
|
618
569
|
* The function overrides the breadth-first search method and returns an array of the return types of
|
|
619
570
|
* the callback function.
|
|
620
571
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
621
|
-
* visited during the breadth-first search
|
|
622
|
-
*
|
|
623
|
-
* @param beginRoot - The `beginRoot` parameter is the starting
|
|
624
|
-
*
|
|
625
|
-
* the
|
|
626
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type
|
|
627
|
-
* be performed during the breadth-first search (BFS) traversal. It
|
|
628
|
-
*
|
|
629
|
-
* @returns
|
|
572
|
+
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
573
|
+
* node being visited, and it can return a value of any type.
|
|
574
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
575
|
+
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
576
|
+
* object. If no value is provided, the default value is the root of the tree.
|
|
577
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
578
|
+
* of iteration to be performed during the breadth-first search (BFS) traversal. It can have one of
|
|
579
|
+
* the following values:
|
|
580
|
+
* @returns an array of the return type of the callback function.
|
|
630
581
|
*/
|
|
631
582
|
override bfs<C extends BTNCallback<NODE>>(
|
|
632
583
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
633
|
-
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
584
|
+
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
634
585
|
iterationType: IterationType = this.iterationType
|
|
635
586
|
): ReturnType<C>[] {
|
|
636
587
|
return super.bfs(callback, beginRoot, iterationType, false);
|
|
637
588
|
}
|
|
638
589
|
|
|
639
590
|
/**
|
|
640
|
-
* Time
|
|
641
|
-
* Space
|
|
591
|
+
* Time Complexity: O(log n)
|
|
592
|
+
* Space Complexity: O(1)
|
|
642
593
|
*/
|
|
643
594
|
|
|
644
595
|
/**
|
|
645
596
|
* Time complexity: O(n)
|
|
646
597
|
* Space complexity: O(n)
|
|
647
598
|
*
|
|
648
|
-
* The function overrides the listLevels method and returns an array of arrays
|
|
649
|
-
*
|
|
599
|
+
* The function overrides the listLevels method from the superclass and returns an array of arrays
|
|
600
|
+
* containing the results of the callback function applied to each level of the tree.
|
|
650
601
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
651
|
-
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
652
|
-
* during the
|
|
653
|
-
* @param beginRoot - The `beginRoot` parameter is
|
|
654
|
-
* levels of
|
|
655
|
-
*
|
|
656
|
-
*
|
|
657
|
-
*
|
|
658
|
-
* iteration.
|
|
602
|
+
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
603
|
+
* tree during the iteration process.
|
|
604
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
605
|
+
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
606
|
+
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
607
|
+
* value is provided, the root of
|
|
608
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
609
|
+
* of iteration to be performed on the tree. It can have one of the following values:
|
|
659
610
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
660
611
|
* function.
|
|
661
612
|
*/
|
|
662
613
|
override listLevels<C extends BTNCallback<NODE>>(
|
|
663
614
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
664
|
-
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
615
|
+
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
665
616
|
iterationType: IterationType = this.iterationType
|
|
666
617
|
): ReturnType<C>[][] {
|
|
667
618
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
668
619
|
}
|
|
669
620
|
|
|
670
621
|
/**
|
|
671
|
-
* Time
|
|
672
|
-
* Space
|
|
622
|
+
* Time complexity: O(n)
|
|
623
|
+
* Space complexity: O(n)
|
|
673
624
|
*/
|
|
674
625
|
|
|
675
626
|
/**
|
|
676
|
-
* Time
|
|
677
|
-
* Space
|
|
627
|
+
* Time complexity: O(n)
|
|
628
|
+
* Space complexity: O(n)
|
|
678
629
|
*
|
|
679
|
-
* The `lesserOrGreaterTraverse` function traverses a binary tree and
|
|
680
|
-
*
|
|
630
|
+
* The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
|
|
631
|
+
* each node that meets a certain condition based on a target node and a comparison value.
|
|
681
632
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
682
|
-
* that
|
|
683
|
-
*
|
|
633
|
+
* that meets the condition specified by the `lesserOrGreater` parameter. It takes a single argument,
|
|
634
|
+
* which is the current node being traversed, and returns a value of any type.
|
|
684
635
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
685
|
-
* traverse nodes that are lesser
|
|
686
|
-
*
|
|
687
|
-
* `
|
|
688
|
-
*
|
|
689
|
-
*
|
|
690
|
-
*
|
|
691
|
-
* @param iterationType - The `iterationType` parameter determines the type of
|
|
692
|
-
* performed on the binary tree. It can have two possible values:
|
|
636
|
+
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
637
|
+
* 0, or 1, where:
|
|
638
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} targetNode - The `targetNode` parameter is the node in
|
|
639
|
+
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
640
|
+
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
641
|
+
* `targetNode` is provided,
|
|
642
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
643
|
+
* traversal to be performed on the binary tree. It can have two possible values:
|
|
693
644
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
694
645
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
695
646
|
*/
|
|
696
647
|
lesserOrGreaterTraverse<C extends BTNCallback<NODE>>(
|
|
697
648
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
698
649
|
lesserOrGreater: CP = -1,
|
|
699
|
-
targetNode: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
650
|
+
targetNode: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
700
651
|
iterationType: IterationType = this.iterationType
|
|
701
652
|
): ReturnType<C>[] {
|
|
702
653
|
const targetNodeEnsured = this.ensureNode(targetNode);
|
|
@@ -734,19 +685,20 @@ export class BST<
|
|
|
734
685
|
}
|
|
735
686
|
|
|
736
687
|
/**
|
|
737
|
-
* Time
|
|
738
|
-
* Space
|
|
688
|
+
* Time complexity: O(n)
|
|
689
|
+
* Space complexity: O(n)
|
|
739
690
|
*/
|
|
740
691
|
|
|
741
692
|
/**
|
|
742
|
-
* Time
|
|
743
|
-
* Space
|
|
693
|
+
* Time complexity: O(n)
|
|
694
|
+
* Space complexity: O(n)
|
|
744
695
|
*
|
|
745
|
-
* The `perfectlyBalance` function
|
|
746
|
-
*
|
|
747
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
748
|
-
* type of iteration to use when building a balanced binary search tree. It
|
|
749
|
-
*
|
|
696
|
+
* The `perfectlyBalance` function takes an optional `iterationType` parameter and returns `true` if
|
|
697
|
+
* the binary search tree is perfectly balanced, otherwise it returns `false`.
|
|
698
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
699
|
+
* specifies the type of iteration to use when building a balanced binary search tree. It has a
|
|
700
|
+
* default value of `this.iterationType`, which means it will use the iteration type specified in the
|
|
701
|
+
* current instance of the class.
|
|
750
702
|
* @returns The function `perfectlyBalance` returns a boolean value.
|
|
751
703
|
*/
|
|
752
704
|
perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
|
|
@@ -787,27 +739,20 @@ export class BST<
|
|
|
787
739
|
}
|
|
788
740
|
|
|
789
741
|
/**
|
|
790
|
-
*
|
|
791
|
-
*
|
|
792
|
-
* 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.
|
|
793
|
-
*
|
|
794
|
-
* Use Cases and Efficiency:
|
|
795
|
-
* 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.
|
|
796
|
-
* 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).
|
|
797
|
-
*/
|
|
798
|
-
|
|
799
|
-
/**
|
|
800
|
-
* Time Complexity: O(n)
|
|
801
|
-
* Space Complexity: O(log n)
|
|
742
|
+
* Time complexity: O(n)
|
|
743
|
+
* Space complexity: O(n)
|
|
802
744
|
*/
|
|
803
745
|
|
|
804
746
|
/**
|
|
805
747
|
* Time Complexity: O(n)
|
|
806
748
|
* Space Complexity: O(log n)
|
|
807
749
|
*
|
|
808
|
-
* The function checks if a binary tree is AVL balanced using either recursive or
|
|
809
|
-
*
|
|
810
|
-
*
|
|
750
|
+
* The function `isAVLBalanced` checks if a binary tree is AVL balanced using either a recursive or
|
|
751
|
+
* iterative approach.
|
|
752
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
753
|
+
* specifies the type of iteration to use when checking if the AVL tree is balanced. It has a default
|
|
754
|
+
* value of `this.iterationType`, which means it will use the iteration type specified in the current
|
|
755
|
+
* instance of the AVL tree.
|
|
811
756
|
* @returns a boolean value.
|
|
812
757
|
*/
|
|
813
758
|
isAVLBalanced(iterationType: IterationType = this.iterationType): boolean {
|
|
@@ -855,9 +800,45 @@ export class BST<
|
|
|
855
800
|
}
|
|
856
801
|
|
|
857
802
|
/**
|
|
858
|
-
*
|
|
859
|
-
*
|
|
860
|
-
|
|
803
|
+
* Time complexity: O(n)
|
|
804
|
+
* Space complexity: O(n)
|
|
805
|
+
*/
|
|
806
|
+
|
|
807
|
+
protected _DEFAULT_COMPARATOR = (a: K, b: K): number => {
|
|
808
|
+
if (typeof a === 'object' && typeof b === 'object' && this.comparator === this._DEFAULT_COMPARATOR) {
|
|
809
|
+
throw TypeError(
|
|
810
|
+
'When comparing two object types, it is necessary to customize a [comparator] function of options parameter during the instantiation of the data structure.'
|
|
811
|
+
);
|
|
812
|
+
}
|
|
813
|
+
if (a > b) return 1;
|
|
814
|
+
if (a < b) return -1;
|
|
815
|
+
return 0;
|
|
816
|
+
};
|
|
817
|
+
|
|
818
|
+
/**
|
|
819
|
+
* Time complexity: O(n)
|
|
820
|
+
* Space complexity: O(n)
|
|
821
|
+
*/
|
|
822
|
+
|
|
823
|
+
protected _comparator: Comparator<K> = this._DEFAULT_COMPARATOR;
|
|
824
|
+
|
|
825
|
+
/**
|
|
826
|
+
* Time Complexity: O(n)
|
|
827
|
+
* Space Complexity: O(log n)
|
|
828
|
+
*/
|
|
829
|
+
|
|
830
|
+
/**
|
|
831
|
+
* The function returns the value of the _comparator property.
|
|
832
|
+
* @returns The `_comparator` property is being returned.
|
|
833
|
+
*/
|
|
834
|
+
get comparator() {
|
|
835
|
+
return this._comparator;
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
/**
|
|
839
|
+
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
840
|
+
* root.
|
|
841
|
+
* @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
|
|
861
842
|
*/
|
|
862
843
|
protected override _setRoot(v: NODE | undefined) {
|
|
863
844
|
if (v) {
|