queue-typed 1.54.0 → 1.54.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/coverage/lcov-report/index.ts.html +2 -2
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
- package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -177
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -340
- package/dist/data-structures/binary-tree/avl-tree.d.ts +102 -57
- package/dist/data-structures/binary-tree/avl-tree.js +110 -47
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +240 -190
- package/dist/data-structures/binary-tree/binary-tree.js +269 -240
- package/dist/data-structures/binary-tree/bst.d.ts +145 -112
- package/dist/data-structures/binary-tree/bst.js +180 -129
- package/dist/data-structures/binary-tree/index.d.ts +2 -0
- package/dist/data-structures/binary-tree/index.js +2 -0
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +100 -82
- package/dist/data-structures/binary-tree/red-black-tree.js +115 -79
- package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
- package/dist/data-structures/binary-tree/tree-counter.js +444 -0
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -174
- package/dist/data-structures/binary-tree/tree-multi-map.js +142 -377
- package/dist/data-structures/graph/directed-graph.d.ts +3 -0
- package/dist/data-structures/graph/directed-graph.js +3 -0
- package/dist/data-structures/graph/map-graph.d.ts +3 -0
- package/dist/data-structures/graph/map-graph.js +3 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
- package/dist/data-structures/graph/undirected-graph.js +3 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +3 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +3 -0
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
- package/dist/data-structures/matrix/matrix.d.ts +3 -0
- package/dist/data-structures/matrix/matrix.js +3 -0
- package/dist/data-structures/matrix/navigator.d.ts +3 -0
- package/dist/data-structures/matrix/navigator.js +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
- package/dist/data-structures/trie/trie.d.ts +0 -4
- package/dist/data-structures/trie/trie.js +0 -4
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -3
- package/dist/interfaces/binary-tree.d.ts +8 -8
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -4
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -3
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -1
- package/dist/types/data-structures/binary-tree/index.js +3 -1
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -5
- package/package.json +3 -3
- package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +155 -393
- package/src/data-structures/binary-tree/avl-tree.ts +144 -93
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-tree.ts +433 -405
- package/src/data-structures/binary-tree/bst.ts +261 -239
- package/src/data-structures/binary-tree/index.ts +2 -0
- package/src/data-structures/binary-tree/red-black-tree.ts +163 -134
- package/src/data-structures/binary-tree/tree-counter.ts +504 -0
- package/src/data-structures/binary-tree/tree-multi-map.ts +161 -429
- package/src/data-structures/graph/directed-graph.ts +3 -0
- package/src/data-structures/graph/map-graph.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +3 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
- package/src/data-structures/matrix/matrix.ts +3 -0
- package/src/data-structures/matrix/navigator.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
- package/src/data-structures/trie/trie.ts +0 -4
- package/src/index.ts +2 -3
- package/src/interfaces/binary-tree.ts +10 -24
- package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -6
- package/src/types/data-structures/binary-tree/avl-tree.ts +0 -5
- package/src/types/data-structures/binary-tree/binary-tree.ts +0 -5
- package/src/types/data-structures/binary-tree/bst.ts +5 -5
- package/src/types/data-structures/binary-tree/index.ts +3 -1
- package/src/types/data-structures/binary-tree/red-black-tree.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -7
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -6
- package/src/types/data-structures/binary-tree/rb-tree.ts +0 -10
- /package/dist/types/data-structures/binary-tree/{rb-tree.js → avl-tree-counter.js} +0 -0
|
@@ -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,33 +150,36 @@ 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
|
}
|
|
159
|
+
get comparator() {
|
|
160
|
+
return this._comparator;
|
|
161
|
+
}
|
|
162
|
+
get specifyComparable() {
|
|
163
|
+
return this._specifyComparable;
|
|
164
|
+
}
|
|
177
165
|
/**
|
|
166
|
+
* Time Complexity: O(1)
|
|
167
|
+
* Space Complexity: O(1)
|
|
168
|
+
*
|
|
178
169
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
179
170
|
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
180
171
|
* being created.
|
|
181
172
|
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
182
173
|
* value associated with the key in the node being created.
|
|
183
|
-
* @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.
|
|
184
175
|
*/
|
|
185
176
|
createNode(key, value) {
|
|
186
177
|
return new BSTNode(key, this._isMapMode ? undefined : value);
|
|
187
178
|
}
|
|
188
179
|
/**
|
|
180
|
+
* Time Complexity: O(1)
|
|
181
|
+
* Space Complexity: O(1)
|
|
182
|
+
*
|
|
189
183
|
* The function creates a new binary search tree with the specified options.
|
|
190
184
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
191
185
|
* behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which has the
|
|
@@ -195,29 +189,14 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
195
189
|
createTree(options) {
|
|
196
190
|
return new BST([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
|
|
197
191
|
}
|
|
198
|
-
/**
|
|
199
|
-
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
200
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of
|
|
201
|
-
* type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
202
|
-
* element.
|
|
203
|
-
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
204
|
-
* value associated with a key in a key-value pair.
|
|
205
|
-
* @returns either a NODE object or undefined.
|
|
206
|
-
*/
|
|
207
|
-
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
|
|
208
|
-
const [node, entryValue] = super._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
209
|
-
if (node === null)
|
|
210
|
-
return [undefined, undefined];
|
|
211
|
-
return [node, value !== null && value !== void 0 ? value : entryValue];
|
|
212
|
-
}
|
|
213
192
|
/**
|
|
214
193
|
* Time Complexity: O(log n)
|
|
215
194
|
* Space Complexity: O(log n)
|
|
216
195
|
*
|
|
217
196
|
* The function ensures the existence of a node in a data structure and returns it, or undefined if
|
|
218
197
|
* it doesn't exist.
|
|
219
|
-
* @param {BTNRep<K, V,
|
|
220
|
-
* `
|
|
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,
|
|
221
200
|
* entry, or raw element that needs to be ensured in the tree.
|
|
222
201
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
223
202
|
* parameter that specifies the type of iteration to be used when ensuring a node. It has a default
|
|
@@ -225,44 +204,50 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
225
204
|
* @returns The method is returning either the node that was ensured or `undefined` if the node could
|
|
226
205
|
* not be ensured.
|
|
227
206
|
*/
|
|
228
|
-
ensureNode(
|
|
207
|
+
ensureNode(keyNodeOrEntry, iterationType = this.iterationType) {
|
|
229
208
|
var _a;
|
|
230
|
-
return (_a = super.ensureNode(
|
|
209
|
+
return (_a = super.ensureNode(keyNodeOrEntry, iterationType)) !== null && _a !== void 0 ? _a : undefined;
|
|
231
210
|
}
|
|
232
211
|
/**
|
|
212
|
+
* Time Complexity: O(1)
|
|
213
|
+
* Space Complexity: O(1)
|
|
214
|
+
*
|
|
233
215
|
* The function checks if the input is an instance of the BSTNode class.
|
|
234
|
-
* @param {BTNRep<K, V,
|
|
235
|
-
* `
|
|
236
|
-
* @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
|
|
237
219
|
* an instance of the `BSTNode` class.
|
|
238
220
|
*/
|
|
239
|
-
isNode(
|
|
240
|
-
return
|
|
221
|
+
isNode(keyNodeOrEntry) {
|
|
222
|
+
return keyNodeOrEntry instanceof BSTNode;
|
|
241
223
|
}
|
|
242
224
|
/**
|
|
243
|
-
*
|
|
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.
|
|
244
229
|
* @param {any} key - The `key` parameter is a value that will be checked to determine if it is of
|
|
245
230
|
* type `K`.
|
|
246
|
-
* @returns The `override
|
|
231
|
+
* @returns The `override isValidKey(key: any): key is K` function is returning a boolean value based on
|
|
247
232
|
* the result of the `isComparable` function with the condition `this._compare !==
|
|
248
233
|
* this._DEFAULT_COMPARATOR`.
|
|
249
234
|
*/
|
|
250
|
-
|
|
235
|
+
isValidKey(key) {
|
|
251
236
|
return (0, utils_1.isComparable)(key, this._specifyComparable !== undefined);
|
|
252
237
|
}
|
|
253
238
|
/**
|
|
254
239
|
* Time Complexity: O(log n)
|
|
255
|
-
* Space Complexity: O(
|
|
240
|
+
* Space Complexity: O(log n)
|
|
256
241
|
*
|
|
257
242
|
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
|
|
258
|
-
* @param {BTNRep<K, V,
|
|
259
|
-
* `
|
|
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>>`.
|
|
260
245
|
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
261
246
|
* key in the binary search tree. If provided, it will be stored in the node along with the key.
|
|
262
247
|
* @returns a boolean value.
|
|
263
248
|
*/
|
|
264
|
-
add(
|
|
265
|
-
const [newNode, newValue] = this.
|
|
249
|
+
add(keyNodeOrEntry, value) {
|
|
250
|
+
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
266
251
|
if (newNode === undefined)
|
|
267
252
|
return false;
|
|
268
253
|
if (this._root === undefined) {
|
|
@@ -333,8 +318,10 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
333
318
|
valuesIterator = values[Symbol.iterator]();
|
|
334
319
|
}
|
|
335
320
|
if (!isBalanceAdd) {
|
|
336
|
-
for (
|
|
321
|
+
for (let kve of keysNodesEntriesOrRaws) {
|
|
337
322
|
const value = valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value;
|
|
323
|
+
if (this.isRaw(kve))
|
|
324
|
+
kve = this._toEntryFn(kve);
|
|
338
325
|
inserted.push(this.add(kve, value));
|
|
339
326
|
}
|
|
340
327
|
return inserted;
|
|
@@ -348,23 +335,21 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
348
335
|
let sorted = [];
|
|
349
336
|
sorted = realBTNExemplars.sort(({ key: a }, { key: b }) => {
|
|
350
337
|
let keyA, keyB;
|
|
351
|
-
if (this.
|
|
338
|
+
if (this.isRaw(a))
|
|
339
|
+
keyA = this._toEntryFn(a)[0];
|
|
340
|
+
else if (this.isEntry(a))
|
|
352
341
|
keyA = a[0];
|
|
353
342
|
else if (this.isRealNode(a))
|
|
354
343
|
keyA = a.key;
|
|
355
|
-
else if (this._toEntryFn) {
|
|
356
|
-
keyA = this._toEntryFn(a)[0];
|
|
357
|
-
}
|
|
358
344
|
else {
|
|
359
345
|
keyA = a;
|
|
360
346
|
}
|
|
361
|
-
if (this.
|
|
347
|
+
if (this.isRaw(b))
|
|
348
|
+
keyB = this._toEntryFn(b)[0];
|
|
349
|
+
else if (this.isEntry(b))
|
|
362
350
|
keyB = b[0];
|
|
363
351
|
else if (this.isRealNode(b))
|
|
364
352
|
keyB = b.key;
|
|
365
|
-
else if (this._toEntryFn) {
|
|
366
|
-
keyB = this._toEntryFn(b)[0];
|
|
367
|
-
}
|
|
368
353
|
else {
|
|
369
354
|
keyB = b;
|
|
370
355
|
}
|
|
@@ -374,15 +359,23 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
374
359
|
return 0;
|
|
375
360
|
});
|
|
376
361
|
const _dfs = (arr) => {
|
|
362
|
+
var _a;
|
|
377
363
|
if (arr.length === 0)
|
|
378
364
|
return;
|
|
379
365
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
380
|
-
|
|
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
|
+
}
|
|
381
373
|
inserted[orgIndex] = this.add(key, value);
|
|
382
374
|
_dfs(arr.slice(0, mid));
|
|
383
375
|
_dfs(arr.slice(mid + 1));
|
|
384
376
|
};
|
|
385
377
|
const _iterate = () => {
|
|
378
|
+
var _a;
|
|
386
379
|
const n = sorted.length;
|
|
387
380
|
const stack = [[0, n - 1]];
|
|
388
381
|
while (stack.length > 0) {
|
|
@@ -391,7 +384,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
391
384
|
const [l, r] = popped;
|
|
392
385
|
if (l <= r) {
|
|
393
386
|
const m = l + Math.floor((r - l) / 2);
|
|
394
|
-
|
|
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
|
+
}
|
|
395
394
|
inserted[orgIndex] = this.add(key, value);
|
|
396
395
|
stack.push([m + 1, r]);
|
|
397
396
|
stack.push([l, m - 1]);
|
|
@@ -413,17 +412,17 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
413
412
|
*
|
|
414
413
|
* The function `search` in TypeScript overrides the search behavior in a binary tree structure based
|
|
415
414
|
* on specified criteria.
|
|
416
|
-
* @param {BTNRep<K, V,
|
|
417
|
-
* `
|
|
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
|
|
418
417
|
* following types:
|
|
419
418
|
* @param [onlyOne=false] - The `onlyOne` parameter is a boolean flag that determines whether the
|
|
420
419
|
* search should stop after finding the first matching node. If `onlyOne` is set to `true`, the
|
|
421
420
|
* search will return as soon as a matching node is found. If `onlyOne` is set to `false`, the
|
|
422
421
|
* @param {C} callback - The `callback` parameter in the `override search` function is a function
|
|
423
422
|
* that will be called on each node that matches the search criteria. It is of type `C`, which
|
|
424
|
-
* extends `NodeCallback<
|
|
423
|
+
* extends `NodeCallback<BSTNode<K, V>>`. The callback function should accept a node of type `BSTNode<K, V>` as its
|
|
425
424
|
* argument and
|
|
426
|
-
* @param {BTNRep<K, V,
|
|
425
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `override search`
|
|
427
426
|
* method represents the node from which the search operation will begin. It is the starting point
|
|
428
427
|
* for searching within the tree data structure. The method ensures that the `startNode` is a valid
|
|
429
428
|
* node before proceeding with the search operation. If the `
|
|
@@ -435,26 +434,26 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
435
434
|
* structure based on the provided key, predicate, and other options. The search results are
|
|
436
435
|
* collected in an array and returned as the output of the method.
|
|
437
436
|
*/
|
|
438
|
-
search(
|
|
439
|
-
if (
|
|
437
|
+
search(keyNodeEntryOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
438
|
+
if (keyNodeEntryOrPredicate === undefined)
|
|
440
439
|
return [];
|
|
441
|
-
if (
|
|
440
|
+
if (keyNodeEntryOrPredicate === null)
|
|
442
441
|
return [];
|
|
443
442
|
startNode = this.ensureNode(startNode);
|
|
444
443
|
if (!startNode)
|
|
445
444
|
return [];
|
|
446
445
|
let predicate;
|
|
447
|
-
const isRange = this.isRange(
|
|
446
|
+
const isRange = this.isRange(keyNodeEntryOrPredicate);
|
|
448
447
|
// Set predicate based on parameter type
|
|
449
448
|
if (isRange) {
|
|
450
|
-
predicate = node =>
|
|
449
|
+
predicate = node => keyNodeEntryOrPredicate.isInRange(node.key, this._comparator);
|
|
451
450
|
}
|
|
452
451
|
else {
|
|
453
|
-
predicate = this._ensurePredicate(
|
|
452
|
+
predicate = this._ensurePredicate(keyNodeEntryOrPredicate);
|
|
454
453
|
}
|
|
455
454
|
const isToLeftByRange = (cur) => {
|
|
456
455
|
if (isRange) {
|
|
457
|
-
const range =
|
|
456
|
+
const range = keyNodeEntryOrPredicate;
|
|
458
457
|
const leftS = this.isReverse ? range.high : range.low;
|
|
459
458
|
const leftI = this.isReverse ? range.includeHigh : range.includeLow;
|
|
460
459
|
return (leftI && this._compare(cur.key, leftS) >= 0) || (!leftI && this._compare(cur.key, leftS) > 0);
|
|
@@ -463,7 +462,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
463
462
|
};
|
|
464
463
|
const isToRightByRange = (cur) => {
|
|
465
464
|
if (isRange) {
|
|
466
|
-
const range =
|
|
465
|
+
const range = keyNodeEntryOrPredicate;
|
|
467
466
|
const rightS = this.isReverse ? range.low : range.high;
|
|
468
467
|
const rightI = this.isReverse ? range.includeLow : range.includeLow;
|
|
469
468
|
return (rightI && this._compare(cur.key, rightS) <= 0) || (!rightI && this._compare(cur.key, rightS) < 0);
|
|
@@ -486,8 +485,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
486
485
|
if (this.isRealNode(cur.right) && isToRightByRange(cur))
|
|
487
486
|
dfs(cur.right);
|
|
488
487
|
}
|
|
489
|
-
else if (!this._isPredicate(
|
|
490
|
-
const benchmarkKey = this._extractKey(
|
|
488
|
+
else if (!this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
489
|
+
const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate);
|
|
491
490
|
if (this.isRealNode(cur.left) &&
|
|
492
491
|
benchmarkKey !== null &&
|
|
493
492
|
benchmarkKey !== undefined &&
|
|
@@ -523,8 +522,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
523
522
|
if (this.isRealNode(cur.right) && isToRightByRange(cur))
|
|
524
523
|
stack.push(cur.right);
|
|
525
524
|
}
|
|
526
|
-
else if (!this._isPredicate(
|
|
527
|
-
const benchmarkKey = this._extractKey(
|
|
525
|
+
else if (!this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
526
|
+
const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate);
|
|
528
527
|
if (this.isRealNode(cur.right) &&
|
|
529
528
|
benchmarkKey !== null &&
|
|
530
529
|
benchmarkKey !== undefined &&
|
|
@@ -548,16 +547,16 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
548
547
|
}
|
|
549
548
|
/**
|
|
550
549
|
* Time Complexity: O(log n)
|
|
551
|
-
* Space Complexity: O(n)
|
|
550
|
+
* Space Complexity: O(k + log n)
|
|
552
551
|
*
|
|
553
552
|
* The `rangeSearch` function searches for nodes within a specified range in a binary search tree.
|
|
554
553
|
* @param {Range<K> | [K, K]} range - The `range` parameter in the `rangeSearch` function can be
|
|
555
554
|
* either a `Range` object or an array of two elements representing the range boundaries.
|
|
556
555
|
* @param {C} callback - The `callback` parameter in the `rangeSearch` function is a callback
|
|
557
556
|
* function that is used to process each node that is found within the specified range during the
|
|
558
|
-
* 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
|
|
559
558
|
* data structure.
|
|
560
|
-
* @param {BTNRep<K, V,
|
|
559
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `rangeSearch`
|
|
561
560
|
* function represents the node from which the search for nodes within the specified range will
|
|
562
561
|
* begin. It is the starting point for the range search operation.
|
|
563
562
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `rangeSearch` function
|
|
@@ -573,12 +572,12 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
573
572
|
}
|
|
574
573
|
/**
|
|
575
574
|
* Time Complexity: O(log n)
|
|
576
|
-
* Space Complexity: O(
|
|
575
|
+
* Space Complexity: O(log n)
|
|
577
576
|
*
|
|
578
|
-
* This function retrieves a node based on a given
|
|
579
|
-
* @param {BTNRep<K, V,
|
|
580
|
-
* parameter can be of type `BTNRep<K, V,
|
|
581
|
-
* @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
|
|
582
581
|
* is used to specify the starting point for searching nodes in the binary search tree. If no
|
|
583
582
|
* specific starting point is provided, the default value is set to `this._root`, which is the root
|
|
584
583
|
* node of the binary search tree.
|
|
@@ -586,14 +585,14 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
586
585
|
* parameter that specifies the type of iteration to be used. It has a default value of
|
|
587
586
|
* `this.iterationType`, which means it will use the iteration type defined in the class instance if
|
|
588
587
|
* no value is provided when calling the method.
|
|
589
|
-
* @returns The `getNode` method is returning an optional binary search tree node (`OptNode<
|
|
590
|
-
* 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
|
|
591
590
|
* the specified root node (`startNode`) and using the specified iteration type. The method then
|
|
592
591
|
* returns the first node found or `undefined` if no node is found.
|
|
593
592
|
*/
|
|
594
|
-
getNode(
|
|
593
|
+
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
595
594
|
var _a;
|
|
596
|
-
return (_a = this.getNodes(
|
|
595
|
+
return (_a = this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
597
596
|
}
|
|
598
597
|
/**
|
|
599
598
|
* Time complexity: O(n)
|
|
@@ -607,7 +606,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
607
606
|
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
608
607
|
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
609
608
|
* take one of the following values:
|
|
610
|
-
* @param {BTNRep<K, V,
|
|
609
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting
|
|
611
610
|
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
612
611
|
* node entry. If not specified, the default value is the root of the tree.
|
|
613
612
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
@@ -627,7 +626,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
627
626
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
628
627
|
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
629
628
|
* node being visited, and it can return a value of any type.
|
|
630
|
-
* @param {BTNRep<K, V,
|
|
629
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting
|
|
631
630
|
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
632
631
|
* object. If no value is provided, the default value is the root of the tree.
|
|
633
632
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
@@ -645,9 +644,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
645
644
|
* The function overrides the listLevels method from the superclass and returns an array of arrays
|
|
646
645
|
* containing the results of the callback function applied to each level of the tree.
|
|
647
646
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
648
|
-
* `NodeCallback<
|
|
647
|
+
* `NodeCallback<BSTNode<K, V>>`. It represents a callback function that will be called for each node in the
|
|
649
648
|
* tree during the iteration process.
|
|
650
|
-
* @param {BTNRep<K, V,
|
|
649
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting
|
|
651
650
|
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
652
651
|
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
653
652
|
* value is provided, the root of
|
|
@@ -671,7 +670,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
671
670
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
672
671
|
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
673
672
|
* 0, or 1, where:
|
|
674
|
-
* @param {BTNRep<K, V,
|
|
673
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} targetNode - The `targetNode` parameter is the node in
|
|
675
674
|
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
676
675
|
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
677
676
|
* `targetNode` is provided,
|
|
@@ -832,24 +831,70 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
832
831
|
return balanced;
|
|
833
832
|
}
|
|
834
833
|
/**
|
|
835
|
-
*
|
|
836
|
-
*
|
|
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.
|
|
837
851
|
*/
|
|
838
|
-
|
|
839
|
-
|
|
852
|
+
map(callback, options, thisArg) {
|
|
853
|
+
const newTree = new BST([], options);
|
|
854
|
+
let index = 0;
|
|
855
|
+
for (const [key, value] of this) {
|
|
856
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
857
|
+
}
|
|
858
|
+
return newTree;
|
|
840
859
|
}
|
|
841
860
|
/**
|
|
842
|
-
*
|
|
843
|
-
*
|
|
844
|
-
*
|
|
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.
|
|
845
867
|
*/
|
|
846
|
-
|
|
847
|
-
|
|
868
|
+
clone() {
|
|
869
|
+
const cloned = this.createTree();
|
|
870
|
+
this._clone(cloned);
|
|
871
|
+
return cloned;
|
|
848
872
|
}
|
|
849
873
|
/**
|
|
874
|
+
* Time Complexity: O(1)
|
|
875
|
+
* Space Complexity: O(1)
|
|
876
|
+
*
|
|
877
|
+
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
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
|
|
880
|
+
* element.
|
|
881
|
+
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
882
|
+
* value associated with a key in a key-value pair.
|
|
883
|
+
* @returns either a BSTNode<K, V> object or undefined.
|
|
884
|
+
*/
|
|
885
|
+
_keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value) {
|
|
886
|
+
const [node, entryValue] = super._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
887
|
+
if (node === null)
|
|
888
|
+
return [undefined, undefined];
|
|
889
|
+
return [node, value !== null && value !== void 0 ? value : entryValue];
|
|
890
|
+
}
|
|
891
|
+
/**
|
|
892
|
+
* Time Complexity: O(1)
|
|
893
|
+
* Space Complexity: O(1)
|
|
894
|
+
*
|
|
850
895
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
851
896
|
* root.
|
|
852
|
-
* @param {OptNode<
|
|
897
|
+
* @param {OptNode<BSTNode<K, V>>} v - v is a parameter of type BSTNode<K, V> or undefined.
|
|
853
898
|
*/
|
|
854
899
|
_setRoot(v) {
|
|
855
900
|
if (v) {
|
|
@@ -857,16 +902,22 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
857
902
|
}
|
|
858
903
|
this._root = v;
|
|
859
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
|
+
*/
|
|
860
919
|
_compare(a, b) {
|
|
861
920
|
return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
|
|
862
921
|
}
|
|
863
|
-
map(callback, options, thisArg) {
|
|
864
|
-
const newTree = new BST([], options);
|
|
865
|
-
let index = 0;
|
|
866
|
-
for (const [key, value] of this) {
|
|
867
|
-
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
868
|
-
}
|
|
869
|
-
return newTree;
|
|
870
|
-
}
|
|
871
922
|
}
|
|
872
923
|
exports.BST = BST;
|
|
@@ -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);
|