directed-graph-typed 1.52.5 → 1.52.8
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/constants/index.d.ts +4 -0
- package/dist/constants/index.js +8 -0
- package/dist/data-structures/base/iterable-element-base.d.ts +8 -1
- package/dist/data-structures/base/iterable-element-base.js +10 -1
- package/dist/data-structures/base/iterable-entry-base.d.ts +8 -1
- package/dist/data-structures/base/iterable-entry-base.js +10 -10
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +31 -32
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +43 -44
- package/dist/data-structures/binary-tree/avl-tree.d.ts +23 -24
- package/dist/data-structures/binary-tree/avl-tree.js +71 -64
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +534 -402
- package/dist/data-structures/binary-tree/binary-tree.js +669 -598
- package/dist/data-structures/binary-tree/bst.d.ts +72 -65
- package/dist/data-structures/binary-tree/bst.js +115 -113
- package/dist/data-structures/binary-tree/rb-tree.d.ts +21 -24
- package/dist/data-structures/binary-tree/rb-tree.js +40 -39
- package/dist/data-structures/binary-tree/segment-tree.d.ts +2 -2
- package/dist/data-structures/binary-tree/segment-tree.js +2 -2
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +28 -31
- package/dist/data-structures/binary-tree/tree-multi-map.js +44 -43
- package/dist/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/data-structures/graph/abstract-graph.js +7 -4
- package/dist/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/data-structures/graph/directed-graph.js +4 -2
- package/dist/data-structures/graph/undirected-graph.d.ts +2 -2
- package/dist/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/data-structures/hash/hash-map.js +1 -1
- package/dist/data-structures/heap/heap.js +3 -3
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.js +7 -7
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/singly-linked-list.js +6 -6
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/data-structures/matrix/matrix.d.ts +2 -2
- package/dist/data-structures/matrix/navigator.d.ts +2 -2
- package/dist/data-structures/matrix/navigator.js +4 -2
- package/dist/data-structures/queue/deque.d.ts +3 -3
- package/dist/data-structures/queue/deque.js +29 -29
- package/dist/data-structures/queue/queue.d.ts +1 -1
- package/dist/data-structures/stack/stack.d.ts +2 -2
- package/dist/data-structures/trie/trie.d.ts +2 -2
- package/dist/data-structures/trie/trie.js +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/interfaces/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -4
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -6
- package/package.json +2 -2
- package/src/constants/index.ts +4 -0
- package/src/data-structures/base/iterable-element-base.ts +11 -1
- package/src/data-structures/base/iterable-entry-base.ts +11 -19
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +47 -50
- package/src/data-structures/binary-tree/avl-tree.ts +69 -71
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +698 -726
- package/src/data-structures/binary-tree/bst.ts +123 -129
- package/src/data-structures/binary-tree/rb-tree.ts +44 -46
- package/src/data-structures/binary-tree/segment-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-multi-map.ts +48 -49
- package/src/data-structures/graph/abstract-graph.ts +6 -6
- package/src/data-structures/graph/directed-graph.ts +4 -4
- package/src/data-structures/graph/undirected-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +3 -3
- package/src/data-structures/heap/heap.ts +3 -3
- package/src/data-structures/linked-list/doubly-linked-list.ts +9 -9
- package/src/data-structures/linked-list/singly-linked-list.ts +8 -8
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/navigator.ts +4 -4
- package/src/data-structures/queue/deque.ts +31 -31
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/stack/stack.ts +2 -2
- package/src/data-structures/trie/trie.ts +3 -3
- package/src/index.ts +2 -1
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +3 -5
|
@@ -62,13 +62,13 @@ exports.BSTNode = BSTNode;
|
|
|
62
62
|
class BST extends binary_tree_1.BinaryTree {
|
|
63
63
|
/**
|
|
64
64
|
* This is the constructor function for a Binary Search Tree class in TypeScript.
|
|
65
|
-
* @param
|
|
65
|
+
* @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter is an
|
|
66
66
|
* iterable that can contain either keys, nodes, entries, or raw elements. These elements will be
|
|
67
67
|
* added to the binary search tree during the construction of the object.
|
|
68
68
|
* @param [options] - An optional object that contains additional options for the Binary Search Tree.
|
|
69
69
|
* It can include a comparator function that defines the order of the elements in the tree.
|
|
70
70
|
*/
|
|
71
|
-
constructor(
|
|
71
|
+
constructor(keysOrNodesOrEntriesOrRaws = [], options) {
|
|
72
72
|
super([], options);
|
|
73
73
|
this._root = undefined;
|
|
74
74
|
this._DEFAULT_COMPARATOR = (a, b) => {
|
|
@@ -87,8 +87,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
87
87
|
if (comparator)
|
|
88
88
|
this._comparator = comparator;
|
|
89
89
|
}
|
|
90
|
-
if (
|
|
91
|
-
this.addMany(
|
|
90
|
+
if (keysOrNodesOrEntriesOrRaws)
|
|
91
|
+
this.addMany(keysOrNodesOrEntriesOrRaws);
|
|
92
92
|
}
|
|
93
93
|
/**
|
|
94
94
|
* The function returns the root node of a tree structure.
|
|
@@ -116,20 +116,20 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
116
116
|
* @returns a new instance of the BST class with the provided options.
|
|
117
117
|
*/
|
|
118
118
|
createTree(options) {
|
|
119
|
-
return new BST([], Object.assign({ iterationType: this.iterationType, comparator: this.
|
|
119
|
+
return new BST([], Object.assign({ iterationType: this.iterationType, comparator: this._comparator, toEntryFn: this._toEntryFn }, options));
|
|
120
120
|
}
|
|
121
121
|
/**
|
|
122
122
|
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
123
|
-
* @param {
|
|
123
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - A variable that can be of
|
|
124
124
|
* type R or BTNKeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
125
125
|
* element.
|
|
126
126
|
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
127
127
|
* value associated with a key in a key-value pair.
|
|
128
128
|
* @returns either a NODE object or undefined.
|
|
129
129
|
*/
|
|
130
|
-
keyValueOrEntryOrRawElementToNode(
|
|
130
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value) {
|
|
131
131
|
var _a;
|
|
132
|
-
return (_a = super.keyValueOrEntryOrRawElementToNode(
|
|
132
|
+
return (_a = super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value)) !== null && _a !== void 0 ? _a : undefined;
|
|
133
133
|
}
|
|
134
134
|
/**
|
|
135
135
|
* Time Complexity: O(log n)
|
|
@@ -137,8 +137,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
137
137
|
*
|
|
138
138
|
* The function ensures the existence of a node in a data structure and returns it, or undefined if
|
|
139
139
|
* it doesn't exist.
|
|
140
|
-
* @param {
|
|
141
|
-
* `
|
|
140
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
141
|
+
* `keyOrNodeOrEntryOrRaw` can accept a value of type `R`, which represents the key, node,
|
|
142
142
|
* entry, or raw element that needs to be ensured in the tree.
|
|
143
143
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
144
144
|
* parameter that specifies the type of iteration to be used when ensuring a node. It has a default
|
|
@@ -146,20 +146,28 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
146
146
|
* @returns The method is returning either the node that was ensured or `undefined` if the node could
|
|
147
147
|
* not be ensured.
|
|
148
148
|
*/
|
|
149
|
-
ensureNode(
|
|
149
|
+
ensureNode(keyOrNodeOrEntryOrRaw, iterationType = this.iterationType) {
|
|
150
150
|
var _a;
|
|
151
|
-
return (_a = super.ensureNode(
|
|
151
|
+
return (_a = super.ensureNode(keyOrNodeOrEntryOrRaw, iterationType)) !== null && _a !== void 0 ? _a : undefined;
|
|
152
152
|
}
|
|
153
153
|
/**
|
|
154
154
|
* The function checks if the input is an instance of the BSTNode class.
|
|
155
|
-
* @param {
|
|
156
|
-
* `
|
|
157
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
155
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
156
|
+
* `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
157
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRaw` is
|
|
158
158
|
* an instance of the `BSTNode` class.
|
|
159
159
|
*/
|
|
160
|
-
isNode(
|
|
161
|
-
return
|
|
160
|
+
isNode(keyOrNodeOrEntryOrRaw) {
|
|
161
|
+
return keyOrNodeOrEntryOrRaw instanceof BSTNode;
|
|
162
162
|
}
|
|
163
|
+
/**
|
|
164
|
+
* The function "override isKey" checks if a key is comparable based on a given comparator.
|
|
165
|
+
* @param {any} key - The `key` parameter is a value that will be checked to determine if it is of
|
|
166
|
+
* type `K`.
|
|
167
|
+
* @returns The `override isKey(key: any): key is K` function is returning a boolean value based on
|
|
168
|
+
* the result of the `isComparable` function with the condition `this.comparator !==
|
|
169
|
+
* this._DEFAULT_COMPARATOR`.
|
|
170
|
+
*/
|
|
163
171
|
isKey(key) {
|
|
164
172
|
return (0, utils_1.isComparable)(key, this.comparator !== this._DEFAULT_COMPARATOR);
|
|
165
173
|
}
|
|
@@ -168,22 +176,22 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
168
176
|
* Space Complexity: O(1)
|
|
169
177
|
*
|
|
170
178
|
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
|
|
171
|
-
* @param {
|
|
172
|
-
* `
|
|
179
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
180
|
+
* `keyOrNodeOrEntryOrRaw` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
173
181
|
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
174
182
|
* key in the binary search tree. If provided, it will be stored in the node along with the key.
|
|
175
183
|
* @returns a boolean value.
|
|
176
184
|
*/
|
|
177
|
-
add(
|
|
178
|
-
const newNode = this.keyValueOrEntryOrRawElementToNode(
|
|
185
|
+
add(keyOrNodeOrEntryOrRaw, value) {
|
|
186
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value);
|
|
179
187
|
if (newNode === undefined)
|
|
180
188
|
return false;
|
|
181
|
-
if (this.
|
|
189
|
+
if (this._root === undefined) {
|
|
182
190
|
this._setRoot(newNode);
|
|
183
191
|
this._size++;
|
|
184
192
|
return true;
|
|
185
193
|
}
|
|
186
|
-
let current = this.
|
|
194
|
+
let current = this._root;
|
|
187
195
|
while (current !== undefined) {
|
|
188
196
|
if (this.comparator(current.key, newNode.key) === 0) {
|
|
189
197
|
this._replaceNode(current, newNode);
|
|
@@ -214,7 +222,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
214
222
|
*
|
|
215
223
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure and returns
|
|
216
224
|
* an array indicating whether each key or node was successfully inserted.
|
|
217
|
-
* @param
|
|
225
|
+
* @param keysOrNodesOrEntriesOrRaws - An iterable containing keys, nodes, entries, or raw
|
|
218
226
|
* elements to be added to the data structure.
|
|
219
227
|
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
220
228
|
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
@@ -229,14 +237,14 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
229
237
|
* @returns The function `addMany` returns an array of booleans indicating whether each element was
|
|
230
238
|
* successfully inserted into the data structure.
|
|
231
239
|
*/
|
|
232
|
-
addMany(
|
|
240
|
+
addMany(keysOrNodesOrEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
233
241
|
const inserted = [];
|
|
234
242
|
let valuesIterator;
|
|
235
243
|
if (values) {
|
|
236
244
|
valuesIterator = values[Symbol.iterator]();
|
|
237
245
|
}
|
|
238
246
|
if (!isBalanceAdd) {
|
|
239
|
-
for (const kve of
|
|
247
|
+
for (const kve of keysOrNodesOrEntriesOrRaws) {
|
|
240
248
|
const value = valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value;
|
|
241
249
|
const nn = this.add(kve, value);
|
|
242
250
|
inserted.push(nn);
|
|
@@ -249,8 +257,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
249
257
|
return false;
|
|
250
258
|
return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
|
|
251
259
|
};
|
|
252
|
-
for (const kve of
|
|
253
|
-
isRealBTNExemplar(kve)
|
|
260
|
+
for (const kve of keysOrNodesOrEntriesOrRaws) {
|
|
261
|
+
if (isRealBTNExemplar(kve))
|
|
262
|
+
realBTNExemplars.push(kve);
|
|
254
263
|
}
|
|
255
264
|
let sorted = [];
|
|
256
265
|
sorted = realBTNExemplars.sort((a, b) => {
|
|
@@ -259,8 +268,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
259
268
|
keyA = a[0];
|
|
260
269
|
else if (this.isRealNode(a))
|
|
261
270
|
keyA = a.key;
|
|
262
|
-
else if (this.
|
|
263
|
-
keyA = this.
|
|
271
|
+
else if (this._toEntryFn) {
|
|
272
|
+
keyA = this._toEntryFn(a)[0];
|
|
264
273
|
}
|
|
265
274
|
else {
|
|
266
275
|
keyA = a;
|
|
@@ -269,8 +278,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
269
278
|
keyB = b[0];
|
|
270
279
|
else if (this.isRealNode(b))
|
|
271
280
|
keyB = b.key;
|
|
272
|
-
else if (this.
|
|
273
|
-
keyB = this.
|
|
281
|
+
else if (this._toEntryFn) {
|
|
282
|
+
keyB = this._toEntryFn(b)[0];
|
|
274
283
|
}
|
|
275
284
|
else {
|
|
276
285
|
keyB = b;
|
|
@@ -318,54 +327,55 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
318
327
|
* Time Complexity: O(log n)
|
|
319
328
|
* Space Complexity: O(k + log n)
|
|
320
329
|
*
|
|
321
|
-
* The `getNodes`
|
|
322
|
-
*
|
|
323
|
-
* @param {
|
|
324
|
-
*
|
|
325
|
-
* function
|
|
326
|
-
*
|
|
327
|
-
*
|
|
328
|
-
*
|
|
329
|
-
*
|
|
330
|
-
*
|
|
331
|
-
*
|
|
332
|
-
*
|
|
333
|
-
*
|
|
334
|
-
*
|
|
335
|
-
* @param {IterationType} iterationType - The `iterationType` parameter
|
|
336
|
-
* iteration to be performed
|
|
337
|
-
*
|
|
330
|
+
* The function `getNodes` in TypeScript overrides the base class method to retrieve nodes based on a
|
|
331
|
+
* given predicate and iteration type.
|
|
332
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
|
|
333
|
+
* parameter in the `getNodes` method is used to filter the nodes that will be returned. It can be a
|
|
334
|
+
* key, a node, an entry, or a custom predicate function that determines whether a node should be
|
|
335
|
+
* included in the result.
|
|
336
|
+
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` method is a boolean flag that
|
|
337
|
+
* determines whether to return only the first node that matches the predicate (`true`) or all nodes
|
|
338
|
+
* that match the predicate (`false`). If `onlyOne` is set to `true`, the method will stop iterating
|
|
339
|
+
* and
|
|
340
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
341
|
+
* `getNodes` method is used to specify the starting point for traversing the tree when searching for
|
|
342
|
+
* nodes that match a given predicate. It represents the root node of the subtree where the search
|
|
343
|
+
* should begin. If not explicitly provided, the default value for `begin
|
|
344
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` method
|
|
345
|
+
* specifies the type of iteration to be performed when traversing the nodes of a binary tree. It can
|
|
346
|
+
* have two possible values:
|
|
347
|
+
* @returns The `getNodes` method returns an array of nodes that satisfy the given predicate.
|
|
338
348
|
*/
|
|
339
|
-
getNodes(
|
|
340
|
-
if (
|
|
349
|
+
getNodes(predicate, onlyOne = false, beginRoot = this._root, iterationType = this.iterationType) {
|
|
350
|
+
if (predicate === undefined)
|
|
341
351
|
return [];
|
|
342
|
-
if (
|
|
352
|
+
if (predicate === null)
|
|
343
353
|
return [];
|
|
344
354
|
beginRoot = this.ensureNode(beginRoot);
|
|
345
355
|
if (!beginRoot)
|
|
346
356
|
return [];
|
|
347
|
-
callback = this.
|
|
357
|
+
const callback = this._ensurePredicate(predicate);
|
|
348
358
|
const ans = [];
|
|
349
359
|
if (iterationType === 'RECURSIVE') {
|
|
350
360
|
const dfs = (cur) => {
|
|
351
|
-
|
|
352
|
-
if (callbackResult === identifier) {
|
|
361
|
+
if (callback(cur)) {
|
|
353
362
|
ans.push(cur);
|
|
354
363
|
if (onlyOne)
|
|
355
364
|
return;
|
|
356
365
|
}
|
|
357
366
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
358
367
|
return;
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
if (this.isRealNode(cur.left) && this.comparator(cur.key, identifier) > 0)
|
|
368
|
+
if (this.isKey(predicate)) {
|
|
369
|
+
if (this.isRealNode(cur.left) && this.comparator(cur.key, predicate) > 0)
|
|
362
370
|
dfs(cur.left);
|
|
363
|
-
if (this.isRealNode(cur.right) && this.comparator(cur.key,
|
|
371
|
+
if (this.isRealNode(cur.right) && this.comparator(cur.key, predicate) < 0)
|
|
364
372
|
dfs(cur.right);
|
|
365
373
|
}
|
|
366
374
|
else {
|
|
367
|
-
this.isRealNode(cur.left)
|
|
368
|
-
|
|
375
|
+
if (this.isRealNode(cur.left))
|
|
376
|
+
dfs(cur.left);
|
|
377
|
+
if (this.isRealNode(cur.right))
|
|
378
|
+
dfs(cur.right);
|
|
369
379
|
}
|
|
370
380
|
};
|
|
371
381
|
dfs(beginRoot);
|
|
@@ -374,28 +384,22 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
374
384
|
const stack = [beginRoot];
|
|
375
385
|
while (stack.length > 0) {
|
|
376
386
|
const cur = stack.pop();
|
|
377
|
-
|
|
378
|
-
if (callbackResult === identifier) {
|
|
387
|
+
if (callback(cur)) {
|
|
379
388
|
ans.push(cur);
|
|
380
389
|
if (onlyOne)
|
|
381
390
|
return ans;
|
|
382
391
|
}
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
if (this.isRealNode(cur.right) && this.comparator(cur.key, identifier) < 0)
|
|
392
|
+
if (this.isKey(predicate)) {
|
|
393
|
+
if (this.isRealNode(cur.right) && this.comparator(cur.key, predicate) < 0)
|
|
386
394
|
stack.push(cur.right);
|
|
387
|
-
if (this.isRealNode(cur.left) && this.comparator(cur.key,
|
|
395
|
+
if (this.isRealNode(cur.left) && this.comparator(cur.key, predicate) > 0)
|
|
388
396
|
stack.push(cur.left);
|
|
389
|
-
// if (this.isRealNode(cur.right) && this._lt(cur.key, identifier as K)) stack.push(cur.right);
|
|
390
|
-
// if (this.isRealNode(cur.left) && this._gt(cur.key, identifier as K)) stack.push(cur.left);
|
|
391
|
-
// // @ts-ignore
|
|
392
|
-
// if (this.isRealNode(cur.right) && cur.key > identifier) stack.push(cur.right);
|
|
393
|
-
// // @ts-ignore
|
|
394
|
-
// if (this.isRealNode(cur.left) && cur.key < identifier) stack.push(cur.left);
|
|
395
397
|
}
|
|
396
398
|
else {
|
|
397
|
-
this.isRealNode(cur.right)
|
|
398
|
-
|
|
399
|
+
if (this.isRealNode(cur.right))
|
|
400
|
+
stack.push(cur.right);
|
|
401
|
+
if (this.isRealNode(cur.left))
|
|
402
|
+
stack.push(cur.left);
|
|
399
403
|
}
|
|
400
404
|
}
|
|
401
405
|
}
|
|
@@ -405,26 +409,25 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
405
409
|
* Time Complexity: O(log n)
|
|
406
410
|
* Space Complexity: O(1)
|
|
407
411
|
*
|
|
408
|
-
*
|
|
409
|
-
*
|
|
410
|
-
*
|
|
411
|
-
*
|
|
412
|
-
*
|
|
413
|
-
*
|
|
414
|
-
* node
|
|
415
|
-
*
|
|
416
|
-
*
|
|
417
|
-
*
|
|
418
|
-
*
|
|
419
|
-
*
|
|
420
|
-
*
|
|
421
|
-
*
|
|
422
|
-
*
|
|
423
|
-
* @returns The method is returning a NODE object or undefined.
|
|
412
|
+
* This function retrieves a node based on a given predicate within a binary search tree structure.
|
|
413
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
|
|
414
|
+
* parameter can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>`, `R`, or `BTNPredicate<NODE>`.
|
|
415
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} beginRoot - The `beginRoot` parameter in the `getNode` method
|
|
416
|
+
* is used to specify the starting point for searching nodes in the binary search tree. If no
|
|
417
|
+
* specific starting point is provided, the default value is set to `this._root`, which is the root
|
|
418
|
+
* node of the binary search tree.
|
|
419
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNode` method is a
|
|
420
|
+
* parameter that specifies the type of iteration to be used. It has a default value of
|
|
421
|
+
* `this.iterationType`, which means it will use the iteration type defined in the class instance if
|
|
422
|
+
* no value is provided when calling the method.
|
|
423
|
+
* @returns The `getNode` method is returning an optional binary search tree node (`OptBSTN<NODE>`).
|
|
424
|
+
* It is using the `getNodes` method to find the node based on the provided predicate, beginning at
|
|
425
|
+
* the specified root node (`beginRoot`) and using the specified iteration type. The method then
|
|
426
|
+
* returns the first node found or `undefined` if no node is found.
|
|
424
427
|
*/
|
|
425
|
-
getNode(
|
|
428
|
+
getNode(predicate, beginRoot = this._root, iterationType = this.iterationType) {
|
|
426
429
|
var _a;
|
|
427
|
-
return (_a = this.getNodes(
|
|
430
|
+
return (_a = this.getNodes(predicate, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
428
431
|
}
|
|
429
432
|
/**
|
|
430
433
|
* Time Complexity: O(log n)
|
|
@@ -440,7 +443,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
440
443
|
* @returns The method is returning a NODE object or undefined.
|
|
441
444
|
*/
|
|
442
445
|
getNodeByKey(key, iterationType = this.iterationType) {
|
|
443
|
-
return this.getNode(key, this.
|
|
446
|
+
return this.getNode(key, this._root, iterationType);
|
|
444
447
|
}
|
|
445
448
|
/**
|
|
446
449
|
* Time complexity: O(n)
|
|
@@ -450,11 +453,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
450
453
|
* the callback function.
|
|
451
454
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
452
455
|
* during the depth-first search traversal. It is an optional parameter and defaults to
|
|
453
|
-
* `this.
|
|
456
|
+
* `this._DEFAULT_BTN_CALLBACK`. The type `C` represents the type of the callback function.
|
|
454
457
|
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
455
458
|
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
456
459
|
* take one of the following values:
|
|
457
|
-
* @param {
|
|
460
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
|
|
458
461
|
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
459
462
|
* node entry. If not specified, the default value is the root of the tree.
|
|
460
463
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
@@ -462,7 +465,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
462
465
|
* following values:
|
|
463
466
|
* @returns The method is returning an array of the return type of the callback function.
|
|
464
467
|
*/
|
|
465
|
-
dfs(callback = this.
|
|
468
|
+
dfs(callback = this._DEFAULT_BTN_CALLBACK, pattern = 'IN', beginRoot = this._root, iterationType = this.iterationType) {
|
|
466
469
|
return super.dfs(callback, pattern, beginRoot, iterationType);
|
|
467
470
|
}
|
|
468
471
|
/**
|
|
@@ -474,7 +477,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
474
477
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
475
478
|
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
476
479
|
* node being visited, and it can return a value of any type.
|
|
477
|
-
* @param {
|
|
480
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
|
|
478
481
|
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
479
482
|
* object. If no value is provided, the default value is the root of the tree.
|
|
480
483
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
@@ -482,7 +485,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
482
485
|
* the following values:
|
|
483
486
|
* @returns an array of the return type of the callback function.
|
|
484
487
|
*/
|
|
485
|
-
bfs(callback = this.
|
|
488
|
+
bfs(callback = this._DEFAULT_BTN_CALLBACK, beginRoot = this._root, iterationType = this.iterationType) {
|
|
486
489
|
return super.bfs(callback, beginRoot, iterationType, false);
|
|
487
490
|
}
|
|
488
491
|
/**
|
|
@@ -494,7 +497,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
494
497
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
495
498
|
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
496
499
|
* tree during the iteration process.
|
|
497
|
-
* @param {
|
|
500
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
|
|
498
501
|
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
499
502
|
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
500
503
|
* value is provided, the root of
|
|
@@ -503,7 +506,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
503
506
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
504
507
|
* function.
|
|
505
508
|
*/
|
|
506
|
-
listLevels(callback = this.
|
|
509
|
+
listLevels(callback = this._DEFAULT_BTN_CALLBACK, beginRoot = this._root, iterationType = this.iterationType) {
|
|
507
510
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
508
511
|
}
|
|
509
512
|
/**
|
|
@@ -518,7 +521,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
518
521
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
519
522
|
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
520
523
|
* 0, or 1, where:
|
|
521
|
-
* @param {
|
|
524
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} targetNode - The `targetNode` parameter is the node in
|
|
522
525
|
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
523
526
|
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
524
527
|
* `targetNode` is provided,
|
|
@@ -527,12 +530,12 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
527
530
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
528
531
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
529
532
|
*/
|
|
530
|
-
lesserOrGreaterTraverse(callback = this.
|
|
533
|
+
lesserOrGreaterTraverse(callback = this._DEFAULT_BTN_CALLBACK, lesserOrGreater = -1, targetNode = this._root, iterationType = this.iterationType) {
|
|
531
534
|
const targetNodeEnsured = this.ensureNode(targetNode);
|
|
532
535
|
const ans = [];
|
|
533
|
-
if (!
|
|
536
|
+
if (!this._root)
|
|
534
537
|
return ans;
|
|
535
|
-
if (!
|
|
538
|
+
if (!targetNodeEnsured)
|
|
536
539
|
return ans;
|
|
537
540
|
const targetKey = targetNodeEnsured.key;
|
|
538
541
|
if (iterationType === 'RECURSIVE') {
|
|
@@ -545,11 +548,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
545
548
|
if (this.isRealNode(cur.right))
|
|
546
549
|
dfs(cur.right);
|
|
547
550
|
};
|
|
548
|
-
dfs(this.
|
|
551
|
+
dfs(this._root);
|
|
549
552
|
return ans;
|
|
550
553
|
}
|
|
551
554
|
else {
|
|
552
|
-
const queue = new queue_1.Queue([this.
|
|
555
|
+
const queue = new queue_1.Queue([this._root]);
|
|
553
556
|
while (queue.size > 0) {
|
|
554
557
|
const cur = queue.shift();
|
|
555
558
|
if (this.isRealNode(cur)) {
|
|
@@ -626,8 +629,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
626
629
|
* @returns a boolean value.
|
|
627
630
|
*/
|
|
628
631
|
isAVLBalanced(iterationType = this.iterationType) {
|
|
629
|
-
|
|
630
|
-
if (!this.root)
|
|
632
|
+
if (!this._root)
|
|
631
633
|
return true;
|
|
632
634
|
let balanced = true;
|
|
633
635
|
if (iterationType === 'RECURSIVE') {
|
|
@@ -639,11 +641,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
639
641
|
balanced = false;
|
|
640
642
|
return Math.max(leftHeight, rightHeight) + 1;
|
|
641
643
|
};
|
|
642
|
-
_height(this.
|
|
644
|
+
_height(this._root);
|
|
643
645
|
}
|
|
644
646
|
else {
|
|
645
647
|
const stack = [];
|
|
646
|
-
let node = this.
|
|
648
|
+
let node = this._root, last = undefined;
|
|
647
649
|
const depths = new Map();
|
|
648
650
|
while (stack.length > 0 || node) {
|
|
649
651
|
if (node) {
|
|
@@ -655,8 +657,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
655
657
|
if (!node.right || last === node.right) {
|
|
656
658
|
node = stack.pop();
|
|
657
659
|
if (node) {
|
|
658
|
-
const left = node.left ?
|
|
659
|
-
const right = node.right ?
|
|
660
|
+
const left = node.left ? depths.get(node.left) : -1;
|
|
661
|
+
const right = node.right ? depths.get(node.right) : -1;
|
|
660
662
|
if (Math.abs(left - right) > 1)
|
|
661
663
|
return false;
|
|
662
664
|
depths.set(node, 1 + Math.max(left, right));
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type { BinaryTreeDeleteResult,
|
|
2
|
-
import { BTNEntry } from '../../types';
|
|
1
|
+
import type { BinaryTreeDeleteResult, BTNKeyOrNodeOrEntry, BTNPredicate, CRUD, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested, BTNEntry } from '../../types';
|
|
3
2
|
import { BST, BSTNode } from './bst';
|
|
4
3
|
import { IBinaryTree } from '../../interfaces';
|
|
5
4
|
export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
|
|
@@ -30,7 +29,7 @@ export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTre
|
|
|
30
29
|
export declare class RedBlackTree<K = any, V = any, R = BTNEntry<K, V>, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>, TREE extends RedBlackTree<K, V, R, NODE, TREE> = RedBlackTree<K, V, R, NODE, RedBlackTreeNested<K, V, R, NODE>>> extends BST<K, V, R, NODE, TREE> implements IBinaryTree<K, V, R, NODE, TREE> {
|
|
31
30
|
/**
|
|
32
31
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
|
|
33
|
-
* @param
|
|
32
|
+
* @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter is an
|
|
34
33
|
* iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
|
|
35
34
|
* initialize the RBTree with the provided elements.
|
|
36
35
|
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
@@ -38,7 +37,7 @@ export declare class RedBlackTree<K = any, V = any, R = BTNEntry<K, V>, NODE ext
|
|
|
38
37
|
* configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
|
|
39
38
|
* depend on the implementation
|
|
40
39
|
*/
|
|
41
|
-
constructor(
|
|
40
|
+
constructor(keysOrNodesOrEntriesOrRaws?: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>, options?: RBTreeOptions<K, V, R>);
|
|
42
41
|
protected _root: NODE | undefined;
|
|
43
42
|
/**
|
|
44
43
|
* The function returns the root node of a tree or undefined if there is no root.
|
|
@@ -72,12 +71,12 @@ export declare class RedBlackTree<K = any, V = any, R = BTNEntry<K, V>, NODE ext
|
|
|
72
71
|
* Space Complexity: O(1)
|
|
73
72
|
*
|
|
74
73
|
* The function checks if the input is an instance of the RedBlackTreeNode class.
|
|
75
|
-
* @param {
|
|
76
|
-
* `
|
|
77
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
74
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
75
|
+
* `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
76
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRaw` is
|
|
78
77
|
* an instance of the `RedBlackTreeNode` class.
|
|
79
78
|
*/
|
|
80
|
-
isNode(
|
|
79
|
+
isNode(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): keyOrNodeOrEntryOrRaw is NODE;
|
|
81
80
|
/**
|
|
82
81
|
* Time Complexity: O(1)
|
|
83
82
|
* Space Complexity: O(1)
|
|
@@ -92,8 +91,8 @@ export declare class RedBlackTree<K = any, V = any, R = BTNEntry<K, V>, NODE ext
|
|
|
92
91
|
*
|
|
93
92
|
* The function adds a new node to a binary search tree and returns true if the node was successfully
|
|
94
93
|
* added.
|
|
95
|
-
* @param {
|
|
96
|
-
* `
|
|
94
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
95
|
+
* `keyOrNodeOrEntryOrRaw` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
97
96
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
98
97
|
* the key in the data structure. It represents the value that you want to add or update in the data
|
|
99
98
|
* structure.
|
|
@@ -101,24 +100,22 @@ export declare class RedBlackTree<K = any, V = any, R = BTNEntry<K, V>, NODE ext
|
|
|
101
100
|
* the method returns true. If the node already exists and its value is updated, the method also
|
|
102
101
|
* returns true. If the node cannot be added or updated, the method returns false.
|
|
103
102
|
*/
|
|
104
|
-
add(
|
|
103
|
+
add(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R, value?: V): boolean;
|
|
105
104
|
/**
|
|
106
105
|
* Time Complexity: O(log n)
|
|
107
106
|
* Space Complexity: O(1)
|
|
108
107
|
*
|
|
109
|
-
* The function overrides the delete method
|
|
110
|
-
*
|
|
111
|
-
* @param {
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
* @
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
*/
|
|
121
|
-
delete<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | null | undefined, callback?: C): BinaryTreeDeleteResult<NODE>[];
|
|
108
|
+
* The function overrides the delete method in a binary tree data structure to remove a node based on
|
|
109
|
+
* a given predicate and maintain the binary search tree properties.
|
|
110
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
|
|
111
|
+
* parameter in the `override delete` method is used to specify the condition or key based on which a
|
|
112
|
+
* node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
|
|
113
|
+
* function that determines which node(s) should be deleted.
|
|
114
|
+
* @returns The `override delete` method is returning an array of `BinaryTreeDeleteResult<NODE>`
|
|
115
|
+
* objects. Each object in the array contains information about the deleted node and whether
|
|
116
|
+
* balancing is needed.
|
|
117
|
+
*/
|
|
118
|
+
delete(predicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>): BinaryTreeDeleteResult<NODE>[];
|
|
122
119
|
/**
|
|
123
120
|
* Time Complexity: O(1)
|
|
124
121
|
* Space Complexity: O(1)
|