queue-typed 1.53.9 → 1.54.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
- package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -189
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +133 -357
- package/dist/data-structures/binary-tree/avl-tree.d.ts +108 -78
- package/dist/data-structures/binary-tree/avl-tree.js +126 -79
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +243 -190
- package/dist/data-structures/binary-tree/binary-tree.js +273 -229
- package/dist/data-structures/binary-tree/bst.d.ts +141 -122
- package/dist/data-structures/binary-tree/bst.js +170 -134
- package/dist/data-structures/binary-tree/index.d.ts +2 -0
- package/dist/data-structures/binary-tree/index.js +2 -0
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +84 -80
- package/dist/data-structures/binary-tree/red-black-tree.js +101 -79
- package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
- package/dist/data-structures/binary-tree/tree-counter.js +444 -0
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -186
- package/dist/data-structures/binary-tree/tree-multi-map.js +140 -388
- package/dist/data-structures/graph/directed-graph.d.ts +3 -0
- package/dist/data-structures/graph/directed-graph.js +3 -0
- package/dist/data-structures/graph/map-graph.d.ts +3 -0
- package/dist/data-structures/graph/map-graph.js +3 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
- package/dist/data-structures/graph/undirected-graph.js +3 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +3 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +3 -0
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
- package/dist/data-structures/matrix/matrix.d.ts +3 -0
- package/dist/data-structures/matrix/matrix.js +3 -0
- package/dist/data-structures/matrix/navigator.d.ts +3 -0
- package/dist/data-structures/matrix/navigator.js +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
- package/dist/data-structures/trie/trie.d.ts +0 -4
- package/dist/data-structures/trie/trie.js +0 -4
- package/dist/interfaces/binary-tree.d.ts +7 -6
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.js +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/index.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/index.js +2 -0
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -3
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -3
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +148 -394
- package/src/data-structures/binary-tree/avl-tree.ts +152 -112
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-tree.ts +446 -379
- package/src/data-structures/binary-tree/bst.ts +224 -201
- package/src/data-structures/binary-tree/index.ts +2 -0
- package/src/data-structures/binary-tree/red-black-tree.ts +138 -114
- package/src/data-structures/binary-tree/tree-counter.ts +504 -0
- package/src/data-structures/binary-tree/tree-multi-map.ts +156 -428
- package/src/data-structures/graph/directed-graph.ts +3 -0
- package/src/data-structures/graph/map-graph.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +3 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
- package/src/data-structures/matrix/matrix.ts +3 -0
- package/src/data-structures/matrix/navigator.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
- package/src/data-structures/trie/trie.ts +0 -4
- package/src/interfaces/binary-tree.ts +10 -11
- package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -4
- package/src/types/data-structures/binary-tree/avl-tree.ts +0 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +0 -5
- package/src/types/data-structures/binary-tree/bst.ts +5 -3
- package/src/types/data-structures/binary-tree/index.ts +2 -0
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -4
- package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -4
|
@@ -6,43 +6,33 @@ const queue_1 = require("../queue");
|
|
|
6
6
|
const utils_1 = require("../../utils");
|
|
7
7
|
const common_1 = require("../../common");
|
|
8
8
|
class BSTNode extends binary_tree_1.BinaryTreeNode {
|
|
9
|
+
/**
|
|
10
|
+
* This TypeScript constructor function initializes an instance with a key and an optional value.
|
|
11
|
+
* @param {K} key - The `key` parameter is typically used to uniquely identify an object or element
|
|
12
|
+
* within a data structure. It serves as a reference or identifier for accessing or manipulating the
|
|
13
|
+
* associated value.
|
|
14
|
+
* @param {V} [value] - The `value` parameter in the constructor is optional, meaning it does not
|
|
15
|
+
* have to be provided when creating an instance of the class. If a value is not provided, it will
|
|
16
|
+
* default to `undefined`.
|
|
17
|
+
*/
|
|
9
18
|
constructor(key, value) {
|
|
10
19
|
super(key, value);
|
|
11
20
|
this.parent = undefined;
|
|
12
21
|
this._left = undefined;
|
|
13
22
|
this._right = undefined;
|
|
14
23
|
}
|
|
15
|
-
/**
|
|
16
|
-
* The function returns the value of the `_left` property.
|
|
17
|
-
* @returns The `_left` property of the current object is being returned.
|
|
18
|
-
*/
|
|
19
24
|
get left() {
|
|
20
25
|
return this._left;
|
|
21
26
|
}
|
|
22
|
-
/**
|
|
23
|
-
* The function sets the left child of a node and updates the parent reference of the child.
|
|
24
|
-
* @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be an
|
|
25
|
-
* instance of the `NODE` class or `undefined`.
|
|
26
|
-
*/
|
|
27
27
|
set left(v) {
|
|
28
28
|
if (v) {
|
|
29
29
|
v.parent = this;
|
|
30
30
|
}
|
|
31
31
|
this._left = v;
|
|
32
32
|
}
|
|
33
|
-
/**
|
|
34
|
-
* The function returns the right node of a binary tree or undefined if there is no right node.
|
|
35
|
-
* @returns The method is returning the value of the `_right` property, which is of type `NODE` or
|
|
36
|
-
* `undefined`.
|
|
37
|
-
*/
|
|
38
33
|
get right() {
|
|
39
34
|
return this._right;
|
|
40
35
|
}
|
|
41
|
-
/**
|
|
42
|
-
* The function sets the right child of a node and updates the parent reference of the child.
|
|
43
|
-
* @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be a
|
|
44
|
-
* `NODE` object or `undefined`.
|
|
45
|
-
*/
|
|
46
36
|
set right(v) {
|
|
47
37
|
if (v) {
|
|
48
38
|
v.parent = this;
|
|
@@ -118,12 +108,13 @@ exports.BSTNode = BSTNode;
|
|
|
118
108
|
*/
|
|
119
109
|
class BST extends binary_tree_1.BinaryTree {
|
|
120
110
|
/**
|
|
121
|
-
* This
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
111
|
+
* This TypeScript constructor initializes a binary search tree with optional options and adds
|
|
112
|
+
* elements if provided.
|
|
113
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
|
|
114
|
+
* iterable that can contain elements of type `BTNRep<K, V, BSTNode<K, V>>` or `R`. It is used to
|
|
115
|
+
* initialize the binary search tree with keys, nodes, entries, or raw data.
|
|
116
|
+
* @param [options] - The `options` parameter is an optional object that can contain the following
|
|
117
|
+
* properties:
|
|
127
118
|
*/
|
|
128
119
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
129
120
|
super([], options);
|
|
@@ -159,43 +150,28 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
159
150
|
if (keysNodesEntriesOrRaws)
|
|
160
151
|
this.addMany(keysNodesEntriesOrRaws);
|
|
161
152
|
}
|
|
162
|
-
/**
|
|
163
|
-
* The function returns the root node of a tree structure.
|
|
164
|
-
* @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
|
|
165
|
-
*/
|
|
166
153
|
get root() {
|
|
167
154
|
return this._root;
|
|
168
155
|
}
|
|
169
|
-
/**
|
|
170
|
-
* The above function is a getter method in TypeScript that returns the value of the private property
|
|
171
|
-
* `_isReverse`.
|
|
172
|
-
* @returns The `isReverse` property of the object, which is a boolean value.
|
|
173
|
-
*/
|
|
174
156
|
get isReverse() {
|
|
175
157
|
return this._isReverse;
|
|
176
158
|
}
|
|
177
|
-
/**
|
|
178
|
-
* The function returns the value of the _comparator property.
|
|
179
|
-
* @returns The `_comparator` property is being returned.
|
|
180
|
-
*/
|
|
181
159
|
get comparator() {
|
|
182
160
|
return this._comparator;
|
|
183
161
|
}
|
|
184
|
-
/**
|
|
185
|
-
* This function returns the value of the `_specifyComparable` property.
|
|
186
|
-
* @returns The method `specifyComparable()` is being returned, which is a getter method for the
|
|
187
|
-
* `_specifyComparable` property.
|
|
188
|
-
*/
|
|
189
162
|
get specifyComparable() {
|
|
190
163
|
return this._specifyComparable;
|
|
191
164
|
}
|
|
192
165
|
/**
|
|
166
|
+
* Time Complexity: O(1)
|
|
167
|
+
* Space Complexity: O(1)
|
|
168
|
+
*
|
|
193
169
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
194
170
|
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
195
171
|
* being created.
|
|
196
172
|
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
197
173
|
* value associated with the key in the node being created.
|
|
198
|
-
* @returns The method is returning a new instance of the BSTNode class, casted as the
|
|
174
|
+
* @returns The method is returning a new instance of the BSTNode class, casted as the BSTNode<K, V> type.
|
|
199
175
|
*/
|
|
200
176
|
createNode(key, value) {
|
|
201
177
|
return new BSTNode(key, this._isMapMode ? undefined : value);
|
|
@@ -204,14 +180,12 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
204
180
|
* Time Complexity: O(1)
|
|
205
181
|
* Space Complexity: O(1)
|
|
206
182
|
*
|
|
207
|
-
* The
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
* @returns
|
|
212
|
-
* and properties inherited from the current instance.
|
|
183
|
+
* The function creates a new binary search tree with the specified options.
|
|
184
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
185
|
+
* behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which has the
|
|
186
|
+
* following properties:
|
|
187
|
+
* @returns a new instance of the BST class with the provided options.
|
|
213
188
|
*/
|
|
214
|
-
// @ts-ignore
|
|
215
189
|
createTree(options) {
|
|
216
190
|
return new BST([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
|
|
217
191
|
}
|
|
@@ -221,8 +195,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
221
195
|
*
|
|
222
196
|
* The function ensures the existence of a node in a data structure and returns it, or undefined if
|
|
223
197
|
* it doesn't exist.
|
|
224
|
-
* @param {BTNRep<K, V,
|
|
225
|
-
* `
|
|
198
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter
|
|
199
|
+
* `keyNodeOrEntry` can accept a value of type `R`, which represents the key, node,
|
|
226
200
|
* entry, or raw element that needs to be ensured in the tree.
|
|
227
201
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
228
202
|
* parameter that specifies the type of iteration to be used when ensuring a node. It has a default
|
|
@@ -230,44 +204,50 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
230
204
|
* @returns The method is returning either the node that was ensured or `undefined` if the node could
|
|
231
205
|
* not be ensured.
|
|
232
206
|
*/
|
|
233
|
-
ensureNode(
|
|
207
|
+
ensureNode(keyNodeOrEntry, iterationType = this.iterationType) {
|
|
234
208
|
var _a;
|
|
235
|
-
return (_a = super.ensureNode(
|
|
209
|
+
return (_a = super.ensureNode(keyNodeOrEntry, iterationType)) !== null && _a !== void 0 ? _a : undefined;
|
|
236
210
|
}
|
|
237
211
|
/**
|
|
212
|
+
* Time Complexity: O(1)
|
|
213
|
+
* Space Complexity: O(1)
|
|
214
|
+
*
|
|
238
215
|
* The function checks if the input is an instance of the BSTNode class.
|
|
239
|
-
* @param {BTNRep<K, V,
|
|
240
|
-
* `
|
|
241
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
216
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter
|
|
217
|
+
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, BSTNode<K, V>>`.
|
|
218
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
|
|
242
219
|
* an instance of the `BSTNode` class.
|
|
243
220
|
*/
|
|
244
|
-
isNode(
|
|
245
|
-
return
|
|
221
|
+
isNode(keyNodeOrEntry) {
|
|
222
|
+
return keyNodeOrEntry instanceof BSTNode;
|
|
246
223
|
}
|
|
247
224
|
/**
|
|
248
|
-
*
|
|
225
|
+
* Time Complexity: O(1)
|
|
226
|
+
* Space Complexity: O(1)
|
|
227
|
+
*
|
|
228
|
+
* The function "override isValidKey" checks if a key is comparable based on a given comparator.
|
|
249
229
|
* @param {any} key - The `key` parameter is a value that will be checked to determine if it is of
|
|
250
230
|
* type `K`.
|
|
251
|
-
* @returns The `override
|
|
231
|
+
* @returns The `override isValidKey(key: any): key is K` function is returning a boolean value based on
|
|
252
232
|
* the result of the `isComparable` function with the condition `this._compare !==
|
|
253
233
|
* this._DEFAULT_COMPARATOR`.
|
|
254
234
|
*/
|
|
255
|
-
|
|
235
|
+
isValidKey(key) {
|
|
256
236
|
return (0, utils_1.isComparable)(key, this._specifyComparable !== undefined);
|
|
257
237
|
}
|
|
258
238
|
/**
|
|
259
239
|
* Time Complexity: O(log n)
|
|
260
|
-
* Space Complexity: O(
|
|
240
|
+
* Space Complexity: O(log n)
|
|
261
241
|
*
|
|
262
242
|
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
|
|
263
|
-
* @param {BTNRep<K, V,
|
|
264
|
-
* `
|
|
243
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter
|
|
244
|
+
* `keyNodeOrEntry` can accept a value of type `R` or `BTNRep<K, V, BSTNode<K, V>>`.
|
|
265
245
|
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
266
246
|
* key in the binary search tree. If provided, it will be stored in the node along with the key.
|
|
267
247
|
* @returns a boolean value.
|
|
268
248
|
*/
|
|
269
|
-
add(
|
|
270
|
-
const [newNode, newValue] = this.
|
|
249
|
+
add(keyNodeOrEntry, value) {
|
|
250
|
+
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
271
251
|
if (newNode === undefined)
|
|
272
252
|
return false;
|
|
273
253
|
if (this._root === undefined) {
|
|
@@ -293,7 +273,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
293
273
|
this._size++;
|
|
294
274
|
return true;
|
|
295
275
|
}
|
|
296
|
-
|
|
276
|
+
if (current.left !== null)
|
|
277
|
+
current = current.left;
|
|
297
278
|
}
|
|
298
279
|
else {
|
|
299
280
|
if (current.right === undefined) {
|
|
@@ -303,7 +284,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
303
284
|
this._size++;
|
|
304
285
|
return true;
|
|
305
286
|
}
|
|
306
|
-
|
|
287
|
+
if (current.right !== null)
|
|
288
|
+
current = current.right;
|
|
307
289
|
}
|
|
308
290
|
}
|
|
309
291
|
return false;
|
|
@@ -336,8 +318,10 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
336
318
|
valuesIterator = values[Symbol.iterator]();
|
|
337
319
|
}
|
|
338
320
|
if (!isBalanceAdd) {
|
|
339
|
-
for (
|
|
321
|
+
for (let kve of keysNodesEntriesOrRaws) {
|
|
340
322
|
const value = valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value;
|
|
323
|
+
if (this.isRaw(kve))
|
|
324
|
+
kve = this._toEntryFn(kve);
|
|
341
325
|
inserted.push(this.add(kve, value));
|
|
342
326
|
}
|
|
343
327
|
return inserted;
|
|
@@ -351,23 +335,21 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
351
335
|
let sorted = [];
|
|
352
336
|
sorted = realBTNExemplars.sort(({ key: a }, { key: b }) => {
|
|
353
337
|
let keyA, keyB;
|
|
354
|
-
if (this.
|
|
338
|
+
if (this.isRaw(a))
|
|
339
|
+
keyA = this._toEntryFn(a)[0];
|
|
340
|
+
else if (this.isEntry(a))
|
|
355
341
|
keyA = a[0];
|
|
356
342
|
else if (this.isRealNode(a))
|
|
357
343
|
keyA = a.key;
|
|
358
|
-
else if (this._toEntryFn) {
|
|
359
|
-
keyA = this._toEntryFn(a)[0];
|
|
360
|
-
}
|
|
361
344
|
else {
|
|
362
345
|
keyA = a;
|
|
363
346
|
}
|
|
364
|
-
if (this.
|
|
347
|
+
if (this.isRaw(b))
|
|
348
|
+
keyB = this._toEntryFn(b)[0];
|
|
349
|
+
else if (this.isEntry(b))
|
|
365
350
|
keyB = b[0];
|
|
366
351
|
else if (this.isRealNode(b))
|
|
367
352
|
keyB = b.key;
|
|
368
|
-
else if (this._toEntryFn) {
|
|
369
|
-
keyB = this._toEntryFn(b)[0];
|
|
370
|
-
}
|
|
371
353
|
else {
|
|
372
354
|
keyB = b;
|
|
373
355
|
}
|
|
@@ -377,15 +359,23 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
377
359
|
return 0;
|
|
378
360
|
});
|
|
379
361
|
const _dfs = (arr) => {
|
|
362
|
+
var _a;
|
|
380
363
|
if (arr.length === 0)
|
|
381
364
|
return;
|
|
382
365
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
383
|
-
|
|
366
|
+
let { key, value } = arr[mid];
|
|
367
|
+
const { orgIndex } = arr[mid];
|
|
368
|
+
if (this.isRaw(key)) {
|
|
369
|
+
const entry = this._toEntryFn(key);
|
|
370
|
+
key = entry[0];
|
|
371
|
+
value = (_a = entry[1]) !== null && _a !== void 0 ? _a : value;
|
|
372
|
+
}
|
|
384
373
|
inserted[orgIndex] = this.add(key, value);
|
|
385
374
|
_dfs(arr.slice(0, mid));
|
|
386
375
|
_dfs(arr.slice(mid + 1));
|
|
387
376
|
};
|
|
388
377
|
const _iterate = () => {
|
|
378
|
+
var _a;
|
|
389
379
|
const n = sorted.length;
|
|
390
380
|
const stack = [[0, n - 1]];
|
|
391
381
|
while (stack.length > 0) {
|
|
@@ -394,7 +384,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
394
384
|
const [l, r] = popped;
|
|
395
385
|
if (l <= r) {
|
|
396
386
|
const m = l + Math.floor((r - l) / 2);
|
|
397
|
-
|
|
387
|
+
let { key, value } = sorted[m];
|
|
388
|
+
const { orgIndex } = sorted[m];
|
|
389
|
+
if (this.isRaw(key)) {
|
|
390
|
+
const entry = this._toEntryFn(key);
|
|
391
|
+
key = entry[0];
|
|
392
|
+
value = (_a = entry[1]) !== null && _a !== void 0 ? _a : value;
|
|
393
|
+
}
|
|
398
394
|
inserted[orgIndex] = this.add(key, value);
|
|
399
395
|
stack.push([m + 1, r]);
|
|
400
396
|
stack.push([l, m - 1]);
|
|
@@ -410,35 +406,23 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
410
406
|
}
|
|
411
407
|
return inserted;
|
|
412
408
|
}
|
|
413
|
-
/**
|
|
414
|
-
* Time Complexity: O(n)
|
|
415
|
-
* Space Complexity: O(1)
|
|
416
|
-
*
|
|
417
|
-
* The `merge` function overrides the base class method by adding elements from another
|
|
418
|
-
* binary search tree.
|
|
419
|
-
* @param anotherTree - `anotherTree` is an instance of a Binary Search Tree (BST) with key type `K`,
|
|
420
|
-
* value type `V`, return type `R`, node type `NODE`, and tree type `TREE`.
|
|
421
|
-
*/
|
|
422
|
-
merge(anotherTree) {
|
|
423
|
-
this.addMany(anotherTree, [], false);
|
|
424
|
-
}
|
|
425
409
|
/**
|
|
426
410
|
* Time Complexity: O(log n)
|
|
427
411
|
* Space Complexity: O(k + log n)
|
|
428
412
|
*
|
|
429
413
|
* The function `search` in TypeScript overrides the search behavior in a binary tree structure based
|
|
430
414
|
* on specified criteria.
|
|
431
|
-
* @param {BTNRep<K, V,
|
|
432
|
-
* `
|
|
415
|
+
* @param {BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The
|
|
416
|
+
* `keyNodeEntryOrPredicate` parameter in the `override search` method can accept one of the
|
|
433
417
|
* following types:
|
|
434
418
|
* @param [onlyOne=false] - The `onlyOne` parameter is a boolean flag that determines whether the
|
|
435
419
|
* search should stop after finding the first matching node. If `onlyOne` is set to `true`, the
|
|
436
420
|
* search will return as soon as a matching node is found. If `onlyOne` is set to `false`, the
|
|
437
421
|
* @param {C} callback - The `callback` parameter in the `override search` function is a function
|
|
438
422
|
* that will be called on each node that matches the search criteria. It is of type `C`, which
|
|
439
|
-
* extends `NodeCallback<
|
|
423
|
+
* extends `NodeCallback<BSTNode<K, V>>`. The callback function should accept a node of type `BSTNode<K, V>` as its
|
|
440
424
|
* argument and
|
|
441
|
-
* @param {BTNRep<K, V,
|
|
425
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `override search`
|
|
442
426
|
* method represents the node from which the search operation will begin. It is the starting point
|
|
443
427
|
* for searching within the tree data structure. The method ensures that the `startNode` is a valid
|
|
444
428
|
* node before proceeding with the search operation. If the `
|
|
@@ -450,26 +434,26 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
450
434
|
* structure based on the provided key, predicate, and other options. The search results are
|
|
451
435
|
* collected in an array and returned as the output of the method.
|
|
452
436
|
*/
|
|
453
|
-
search(
|
|
454
|
-
if (
|
|
437
|
+
search(keyNodeEntryOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
438
|
+
if (keyNodeEntryOrPredicate === undefined)
|
|
455
439
|
return [];
|
|
456
|
-
if (
|
|
440
|
+
if (keyNodeEntryOrPredicate === null)
|
|
457
441
|
return [];
|
|
458
442
|
startNode = this.ensureNode(startNode);
|
|
459
443
|
if (!startNode)
|
|
460
444
|
return [];
|
|
461
445
|
let predicate;
|
|
462
|
-
const isRange = this.isRange(
|
|
446
|
+
const isRange = this.isRange(keyNodeEntryOrPredicate);
|
|
463
447
|
// Set predicate based on parameter type
|
|
464
448
|
if (isRange) {
|
|
465
|
-
predicate = node =>
|
|
449
|
+
predicate = node => keyNodeEntryOrPredicate.isInRange(node.key, this._comparator);
|
|
466
450
|
}
|
|
467
451
|
else {
|
|
468
|
-
predicate = this._ensurePredicate(
|
|
452
|
+
predicate = this._ensurePredicate(keyNodeEntryOrPredicate);
|
|
469
453
|
}
|
|
470
454
|
const isToLeftByRange = (cur) => {
|
|
471
455
|
if (isRange) {
|
|
472
|
-
const range =
|
|
456
|
+
const range = keyNodeEntryOrPredicate;
|
|
473
457
|
const leftS = this.isReverse ? range.high : range.low;
|
|
474
458
|
const leftI = this.isReverse ? range.includeHigh : range.includeLow;
|
|
475
459
|
return (leftI && this._compare(cur.key, leftS) >= 0) || (!leftI && this._compare(cur.key, leftS) > 0);
|
|
@@ -478,7 +462,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
478
462
|
};
|
|
479
463
|
const isToRightByRange = (cur) => {
|
|
480
464
|
if (isRange) {
|
|
481
|
-
const range =
|
|
465
|
+
const range = keyNodeEntryOrPredicate;
|
|
482
466
|
const rightS = this.isReverse ? range.low : range.high;
|
|
483
467
|
const rightI = this.isReverse ? range.includeLow : range.includeLow;
|
|
484
468
|
return (rightI && this._compare(cur.key, rightS) <= 0) || (!rightI && this._compare(cur.key, rightS) < 0);
|
|
@@ -501,8 +485,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
501
485
|
if (this.isRealNode(cur.right) && isToRightByRange(cur))
|
|
502
486
|
dfs(cur.right);
|
|
503
487
|
}
|
|
504
|
-
else if (!this._isPredicate(
|
|
505
|
-
const benchmarkKey = this._extractKey(
|
|
488
|
+
else if (!this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
489
|
+
const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate);
|
|
506
490
|
if (this.isRealNode(cur.left) &&
|
|
507
491
|
benchmarkKey !== null &&
|
|
508
492
|
benchmarkKey !== undefined &&
|
|
@@ -538,8 +522,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
538
522
|
if (this.isRealNode(cur.right) && isToRightByRange(cur))
|
|
539
523
|
stack.push(cur.right);
|
|
540
524
|
}
|
|
541
|
-
else if (!this._isPredicate(
|
|
542
|
-
const benchmarkKey = this._extractKey(
|
|
525
|
+
else if (!this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
526
|
+
const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate);
|
|
543
527
|
if (this.isRealNode(cur.right) &&
|
|
544
528
|
benchmarkKey !== null &&
|
|
545
529
|
benchmarkKey !== undefined &&
|
|
@@ -563,16 +547,16 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
563
547
|
}
|
|
564
548
|
/**
|
|
565
549
|
* Time Complexity: O(log n)
|
|
566
|
-
* Space Complexity: O(n)
|
|
550
|
+
* Space Complexity: O(k + log n)
|
|
567
551
|
*
|
|
568
552
|
* The `rangeSearch` function searches for nodes within a specified range in a binary search tree.
|
|
569
553
|
* @param {Range<K> | [K, K]} range - The `range` parameter in the `rangeSearch` function can be
|
|
570
554
|
* either a `Range` object or an array of two elements representing the range boundaries.
|
|
571
555
|
* @param {C} callback - The `callback` parameter in the `rangeSearch` function is a callback
|
|
572
556
|
* function that is used to process each node that is found within the specified range during the
|
|
573
|
-
* search operation. It is of type `NodeCallback<
|
|
557
|
+
* search operation. It is of type `NodeCallback<BSTNode<K, V>>`, where `BSTNode<K, V>` is the type of nodes in the
|
|
574
558
|
* data structure.
|
|
575
|
-
* @param {BTNRep<K, V,
|
|
559
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `rangeSearch`
|
|
576
560
|
* function represents the node from which the search for nodes within the specified range will
|
|
577
561
|
* begin. It is the starting point for the range search operation.
|
|
578
562
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `rangeSearch` function
|
|
@@ -588,12 +572,12 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
588
572
|
}
|
|
589
573
|
/**
|
|
590
574
|
* Time Complexity: O(log n)
|
|
591
|
-
* Space Complexity: O(
|
|
575
|
+
* Space Complexity: O(log n)
|
|
592
576
|
*
|
|
593
|
-
* This function retrieves a node based on a given
|
|
594
|
-
* @param {BTNRep<K, V,
|
|
595
|
-
* parameter can be of type `BTNRep<K, V,
|
|
596
|
-
* @param {
|
|
577
|
+
* This function retrieves a node based on a given keyNodeEntryOrPredicate within a binary search tree structure.
|
|
578
|
+
* @param {BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The `keyNodeEntryOrPredicate`
|
|
579
|
+
* parameter can be of type `BTNRep<K, V, BSTNode<K, V>>`, `R`, or `NodePredicate<BSTNode<K, V>>`.
|
|
580
|
+
* @param {BSTNOptKeyOrNode<K, BSTNode<K, V>>} startNode - The `startNode` parameter in the `getNode` method
|
|
597
581
|
* is used to specify the starting point for searching nodes in the binary search tree. If no
|
|
598
582
|
* specific starting point is provided, the default value is set to `this._root`, which is the root
|
|
599
583
|
* node of the binary search tree.
|
|
@@ -601,14 +585,14 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
601
585
|
* parameter that specifies the type of iteration to be used. It has a default value of
|
|
602
586
|
* `this.iterationType`, which means it will use the iteration type defined in the class instance if
|
|
603
587
|
* no value is provided when calling the method.
|
|
604
|
-
* @returns The `getNode` method is returning an optional binary search tree node (`OptNode<
|
|
605
|
-
* It is using the `getNodes` method to find the node based on the provided
|
|
588
|
+
* @returns The `getNode` method is returning an optional binary search tree node (`OptNode<BSTNode<K, V>>`).
|
|
589
|
+
* It is using the `getNodes` method to find the node based on the provided keyNodeEntryOrPredicate, beginning at
|
|
606
590
|
* the specified root node (`startNode`) and using the specified iteration type. The method then
|
|
607
591
|
* returns the first node found or `undefined` if no node is found.
|
|
608
592
|
*/
|
|
609
|
-
getNode(
|
|
593
|
+
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
610
594
|
var _a;
|
|
611
|
-
return (_a = this.getNodes(
|
|
595
|
+
return (_a = this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
612
596
|
}
|
|
613
597
|
/**
|
|
614
598
|
* Time complexity: O(n)
|
|
@@ -622,7 +606,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
622
606
|
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
623
607
|
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
624
608
|
* take one of the following values:
|
|
625
|
-
* @param {BTNRep<K, V,
|
|
609
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting
|
|
626
610
|
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
627
611
|
* node entry. If not specified, the default value is the root of the tree.
|
|
628
612
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
@@ -642,7 +626,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
642
626
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
643
627
|
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
644
628
|
* node being visited, and it can return a value of any type.
|
|
645
|
-
* @param {BTNRep<K, V,
|
|
629
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting
|
|
646
630
|
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
647
631
|
* object. If no value is provided, the default value is the root of the tree.
|
|
648
632
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
@@ -660,9 +644,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
660
644
|
* The function overrides the listLevels method from the superclass and returns an array of arrays
|
|
661
645
|
* containing the results of the callback function applied to each level of the tree.
|
|
662
646
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
663
|
-
* `NodeCallback<
|
|
647
|
+
* `NodeCallback<BSTNode<K, V>>`. It represents a callback function that will be called for each node in the
|
|
664
648
|
* tree during the iteration process.
|
|
665
|
-
* @param {BTNRep<K, V,
|
|
649
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting
|
|
666
650
|
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
667
651
|
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
668
652
|
* value is provided, the root of
|
|
@@ -686,7 +670,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
686
670
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
687
671
|
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
688
672
|
* 0, or 1, where:
|
|
689
|
-
* @param {BTNRep<K, V,
|
|
673
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} targetNode - The `targetNode` parameter is the node in
|
|
690
674
|
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
691
675
|
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
692
676
|
* `targetNode` is provided,
|
|
@@ -822,7 +806,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
822
806
|
while (stack.length > 0 || node) {
|
|
823
807
|
if (node) {
|
|
824
808
|
stack.push(node);
|
|
825
|
-
|
|
809
|
+
if (node.left !== null)
|
|
810
|
+
node = node.left;
|
|
826
811
|
}
|
|
827
812
|
else {
|
|
828
813
|
node = stack[stack.length - 1];
|
|
@@ -845,7 +830,25 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
845
830
|
}
|
|
846
831
|
return balanced;
|
|
847
832
|
}
|
|
848
|
-
|
|
833
|
+
/**
|
|
834
|
+
* Time complexity: O(n)
|
|
835
|
+
* Space complexity: O(n)
|
|
836
|
+
*
|
|
837
|
+
* The `map` function in TypeScript overrides the default map behavior for a binary search tree by
|
|
838
|
+
* applying a callback function to each entry and creating a new tree with the results.
|
|
839
|
+
* @param callback - A function that will be called for each entry in the BST. It takes four
|
|
840
|
+
* arguments: the key, the value (which can be undefined), the index of the entry, and a reference to
|
|
841
|
+
* the BST itself.
|
|
842
|
+
* @param [options] - The `options` parameter in the `override map` method is of type `BSTOptions<MK,
|
|
843
|
+
* MV, MR>`. It is an optional parameter that allows you to specify additional options for the Binary
|
|
844
|
+
* Search Tree (BST) being created in the `map` method. These options could include configuration
|
|
845
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` method is used to specify
|
|
846
|
+
* the value of `this` that should be used when executing the `callback` function. It allows you to
|
|
847
|
+
* set the context or scope in which the callback function will be called. This can be useful when
|
|
848
|
+
* you want
|
|
849
|
+
* @returns The `map` method is returning a new Binary Search Tree (`BST`) instance with the entries
|
|
850
|
+
* transformed by the provided callback function.
|
|
851
|
+
*/
|
|
849
852
|
map(callback, options, thisArg) {
|
|
850
853
|
const newTree = new BST([], options);
|
|
851
854
|
let index = 0;
|
|
@@ -855,24 +858,43 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
855
858
|
return newTree;
|
|
856
859
|
}
|
|
857
860
|
/**
|
|
861
|
+
* Time complexity: O(n)
|
|
862
|
+
* Space complexity: O(n)
|
|
863
|
+
*
|
|
864
|
+
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree
|
|
865
|
+
* structure.
|
|
866
|
+
* @returns The `cloned` object is being returned.
|
|
867
|
+
*/
|
|
868
|
+
clone() {
|
|
869
|
+
const cloned = this.createTree();
|
|
870
|
+
this._clone(cloned);
|
|
871
|
+
return cloned;
|
|
872
|
+
}
|
|
873
|
+
/**
|
|
874
|
+
* Time Complexity: O(1)
|
|
875
|
+
* Space Complexity: O(1)
|
|
876
|
+
*
|
|
858
877
|
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
859
|
-
* @param {BTNRep<K, V,
|
|
860
|
-
* type R or BTNRep<K, V,
|
|
878
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - A variable that can be of
|
|
879
|
+
* type R or BTNRep<K, V, BSTNode<K, V>>. It represents either a key, a node, an entry, or a raw
|
|
861
880
|
* element.
|
|
862
881
|
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
863
882
|
* value associated with a key in a key-value pair.
|
|
864
|
-
* @returns either a
|
|
883
|
+
* @returns either a BSTNode<K, V> object or undefined.
|
|
865
884
|
*/
|
|
866
|
-
|
|
867
|
-
const [node, entryValue] = super.
|
|
885
|
+
_keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value) {
|
|
886
|
+
const [node, entryValue] = super._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
868
887
|
if (node === null)
|
|
869
888
|
return [undefined, undefined];
|
|
870
889
|
return [node, value !== null && value !== void 0 ? value : entryValue];
|
|
871
890
|
}
|
|
872
891
|
/**
|
|
892
|
+
* Time Complexity: O(1)
|
|
893
|
+
* Space Complexity: O(1)
|
|
894
|
+
*
|
|
873
895
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
874
896
|
* root.
|
|
875
|
-
* @param {OptNode<
|
|
897
|
+
* @param {OptNode<BSTNode<K, V>>} v - v is a parameter of type BSTNode<K, V> or undefined.
|
|
876
898
|
*/
|
|
877
899
|
_setRoot(v) {
|
|
878
900
|
if (v) {
|
|
@@ -880,6 +902,20 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
880
902
|
}
|
|
881
903
|
this._root = v;
|
|
882
904
|
}
|
|
905
|
+
/**
|
|
906
|
+
* Time Complexity: O(1)
|
|
907
|
+
* Space Complexity: O(1)
|
|
908
|
+
*
|
|
909
|
+
* The _compare function compares two values using a specified comparator function and optionally
|
|
910
|
+
* reverses the result.
|
|
911
|
+
* @param {K} a - The parameter `a` is of type `K`, which is used as an input for comparison in the
|
|
912
|
+
* `_compare` method.
|
|
913
|
+
* @param {K} b - The parameter `b` in the `_compare` function is of type `K`.
|
|
914
|
+
* @returns The `_compare` method is returning the result of the ternary expression. If `_isReverse`
|
|
915
|
+
* is true, it returns the negation of the result of calling the `_comparator` function with
|
|
916
|
+
* arguments `a` and `b`. If `_isReverse` is false, it returns the result of calling the
|
|
917
|
+
* `_comparator` function with arguments `a` and `b`.
|
|
918
|
+
*/
|
|
883
919
|
_compare(a, b) {
|
|
884
920
|
return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
|
|
885
921
|
}
|
|
@@ -22,3 +22,5 @@ __exportStar(require("./avl-tree"), exports);
|
|
|
22
22
|
__exportStar(require("./red-black-tree"), exports);
|
|
23
23
|
__exportStar(require("./avl-tree-multi-map"), exports);
|
|
24
24
|
__exportStar(require("./tree-multi-map"), exports);
|
|
25
|
+
__exportStar(require("./tree-counter"), exports);
|
|
26
|
+
__exportStar(require("./avl-tree-counter"), exports);
|