min-heap-typed 1.52.6 → 1.52.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/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 +591 -402
- package/dist/data-structures/binary-tree/binary-tree.js +690 -604
- package/dist/data-structures/binary-tree/bst.d.ts +72 -65
- package/dist/data-structures/binary-tree/bst.js +122 -125
- package/dist/data-structures/binary-tree/rb-tree.d.ts +21 -24
- package/dist/data-structures/binary-tree/rb-tree.js +42 -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 +27 -31
- package/dist/data-structures/binary-tree/tree-multi-map.js +45 -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 +46 -50
- package/src/data-structures/binary-tree/avl-tree.ts +68 -71
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +716 -748
- package/src/data-structures/binary-tree/bst.ts +137 -146
- package/src/data-structures/binary-tree/rb-tree.ts +46 -46
- package/src/data-structures/binary-tree/segment-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-multi-map.ts +49 -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,38 +237,34 @@ 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
|
-
|
|
242
|
-
inserted.push(nn);
|
|
249
|
+
inserted.push(this.add(kve, value));
|
|
243
250
|
}
|
|
244
251
|
return inserted;
|
|
245
252
|
}
|
|
246
253
|
const realBTNExemplars = [];
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
};
|
|
252
|
-
for (const kve of keysOrNodesOrEntriesOrRawElements) {
|
|
253
|
-
isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
|
|
254
|
+
let i = 0;
|
|
255
|
+
for (const kve of keysOrNodesOrEntriesOrRaws) {
|
|
256
|
+
realBTNExemplars.push({ key: kve, value: valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value, orgIndex: i });
|
|
257
|
+
i++;
|
|
254
258
|
}
|
|
255
259
|
let sorted = [];
|
|
256
|
-
sorted = realBTNExemplars.sort((a, b) => {
|
|
260
|
+
sorted = realBTNExemplars.sort(({ key: a }, { key: b }) => {
|
|
257
261
|
let keyA, keyB;
|
|
258
262
|
if (this.isEntry(a))
|
|
259
263
|
keyA = a[0];
|
|
260
264
|
else if (this.isRealNode(a))
|
|
261
265
|
keyA = a.key;
|
|
262
|
-
else if (this.
|
|
263
|
-
keyA = this.
|
|
266
|
+
else if (this._toEntryFn) {
|
|
267
|
+
keyA = this._toEntryFn(a)[0];
|
|
264
268
|
}
|
|
265
269
|
else {
|
|
266
270
|
keyA = a;
|
|
@@ -269,8 +273,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
269
273
|
keyB = b[0];
|
|
270
274
|
else if (this.isRealNode(b))
|
|
271
275
|
keyB = b.key;
|
|
272
|
-
else if (this.
|
|
273
|
-
keyB = this.
|
|
276
|
+
else if (this._toEntryFn) {
|
|
277
|
+
keyB = this._toEntryFn(b)[0];
|
|
274
278
|
}
|
|
275
279
|
else {
|
|
276
280
|
keyB = b;
|
|
@@ -284,8 +288,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
284
288
|
if (arr.length === 0)
|
|
285
289
|
return;
|
|
286
290
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
287
|
-
const
|
|
288
|
-
inserted.
|
|
291
|
+
const { key, value, orgIndex } = arr[mid];
|
|
292
|
+
inserted[orgIndex] = this.add(key, value);
|
|
289
293
|
_dfs(arr.slice(0, mid));
|
|
290
294
|
_dfs(arr.slice(mid + 1));
|
|
291
295
|
};
|
|
@@ -298,8 +302,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
298
302
|
const [l, r] = popped;
|
|
299
303
|
if (l <= r) {
|
|
300
304
|
const m = l + Math.floor((r - l) / 2);
|
|
301
|
-
const
|
|
302
|
-
inserted.
|
|
305
|
+
const { key, value, orgIndex } = sorted[m];
|
|
306
|
+
inserted[orgIndex] = this.add(key, value);
|
|
303
307
|
stack.push([m + 1, r]);
|
|
304
308
|
stack.push([l, m - 1]);
|
|
305
309
|
}
|
|
@@ -318,54 +322,55 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
318
322
|
* Time Complexity: O(log n)
|
|
319
323
|
* Space Complexity: O(k + log n)
|
|
320
324
|
*
|
|
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
|
-
*
|
|
325
|
+
* The function `getNodes` in TypeScript overrides the base class method to retrieve nodes based on a
|
|
326
|
+
* given predicate and iteration type.
|
|
327
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
|
|
328
|
+
* parameter in the `getNodes` method is used to filter the nodes that will be returned. It can be a
|
|
329
|
+
* key, a node, an entry, or a custom predicate function that determines whether a node should be
|
|
330
|
+
* included in the result.
|
|
331
|
+
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` method is a boolean flag that
|
|
332
|
+
* determines whether to return only the first node that matches the predicate (`true`) or all nodes
|
|
333
|
+
* that match the predicate (`false`). If `onlyOne` is set to `true`, the method will stop iterating
|
|
334
|
+
* and
|
|
335
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
336
|
+
* `getNodes` method is used to specify the starting point for traversing the tree when searching for
|
|
337
|
+
* nodes that match a given predicate. It represents the root node of the subtree where the search
|
|
338
|
+
* should begin. If not explicitly provided, the default value for `begin
|
|
339
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` method
|
|
340
|
+
* specifies the type of iteration to be performed when traversing the nodes of a binary tree. It can
|
|
341
|
+
* have two possible values:
|
|
342
|
+
* @returns The `getNodes` method returns an array of nodes that satisfy the given predicate.
|
|
338
343
|
*/
|
|
339
|
-
getNodes(
|
|
340
|
-
if (
|
|
344
|
+
getNodes(predicate, onlyOne = false, beginRoot = this._root, iterationType = this.iterationType) {
|
|
345
|
+
if (predicate === undefined)
|
|
341
346
|
return [];
|
|
342
|
-
if (
|
|
347
|
+
if (predicate === null)
|
|
343
348
|
return [];
|
|
344
349
|
beginRoot = this.ensureNode(beginRoot);
|
|
345
350
|
if (!beginRoot)
|
|
346
351
|
return [];
|
|
347
|
-
callback = this.
|
|
352
|
+
const callback = this._ensurePredicate(predicate);
|
|
348
353
|
const ans = [];
|
|
349
354
|
if (iterationType === 'RECURSIVE') {
|
|
350
355
|
const dfs = (cur) => {
|
|
351
|
-
|
|
352
|
-
if (callbackResult === identifier) {
|
|
356
|
+
if (callback(cur)) {
|
|
353
357
|
ans.push(cur);
|
|
354
358
|
if (onlyOne)
|
|
355
359
|
return;
|
|
356
360
|
}
|
|
357
361
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
358
362
|
return;
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
if (this.isRealNode(cur.left) && this.comparator(cur.key, identifier) > 0)
|
|
363
|
+
if (this.isKey(predicate)) {
|
|
364
|
+
if (this.isRealNode(cur.left) && this.comparator(cur.key, predicate) > 0)
|
|
362
365
|
dfs(cur.left);
|
|
363
|
-
if (this.isRealNode(cur.right) && this.comparator(cur.key,
|
|
366
|
+
if (this.isRealNode(cur.right) && this.comparator(cur.key, predicate) < 0)
|
|
364
367
|
dfs(cur.right);
|
|
365
368
|
}
|
|
366
369
|
else {
|
|
367
|
-
this.isRealNode(cur.left)
|
|
368
|
-
|
|
370
|
+
if (this.isRealNode(cur.left))
|
|
371
|
+
dfs(cur.left);
|
|
372
|
+
if (this.isRealNode(cur.right))
|
|
373
|
+
dfs(cur.right);
|
|
369
374
|
}
|
|
370
375
|
};
|
|
371
376
|
dfs(beginRoot);
|
|
@@ -374,28 +379,22 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
374
379
|
const stack = [beginRoot];
|
|
375
380
|
while (stack.length > 0) {
|
|
376
381
|
const cur = stack.pop();
|
|
377
|
-
|
|
378
|
-
if (callbackResult === identifier) {
|
|
382
|
+
if (callback(cur)) {
|
|
379
383
|
ans.push(cur);
|
|
380
384
|
if (onlyOne)
|
|
381
385
|
return ans;
|
|
382
386
|
}
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
if (this.isRealNode(cur.right) && this.comparator(cur.key, identifier) < 0)
|
|
387
|
+
if (this.isKey(predicate)) {
|
|
388
|
+
if (this.isRealNode(cur.right) && this.comparator(cur.key, predicate) < 0)
|
|
386
389
|
stack.push(cur.right);
|
|
387
|
-
if (this.isRealNode(cur.left) && this.comparator(cur.key,
|
|
390
|
+
if (this.isRealNode(cur.left) && this.comparator(cur.key, predicate) > 0)
|
|
388
391
|
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
392
|
}
|
|
396
393
|
else {
|
|
397
|
-
this.isRealNode(cur.right)
|
|
398
|
-
|
|
394
|
+
if (this.isRealNode(cur.right))
|
|
395
|
+
stack.push(cur.right);
|
|
396
|
+
if (this.isRealNode(cur.left))
|
|
397
|
+
stack.push(cur.left);
|
|
399
398
|
}
|
|
400
399
|
}
|
|
401
400
|
}
|
|
@@ -405,26 +404,25 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
405
404
|
* Time Complexity: O(log n)
|
|
406
405
|
* Space Complexity: O(1)
|
|
407
406
|
*
|
|
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.
|
|
407
|
+
* This function retrieves a node based on a given predicate within a binary search tree structure.
|
|
408
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
|
|
409
|
+
* parameter can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>`, `R`, or `BTNPredicate<NODE>`.
|
|
410
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} beginRoot - The `beginRoot` parameter in the `getNode` method
|
|
411
|
+
* is used to specify the starting point for searching nodes in the binary search tree. If no
|
|
412
|
+
* specific starting point is provided, the default value is set to `this._root`, which is the root
|
|
413
|
+
* node of the binary search tree.
|
|
414
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNode` method is a
|
|
415
|
+
* parameter that specifies the type of iteration to be used. It has a default value of
|
|
416
|
+
* `this.iterationType`, which means it will use the iteration type defined in the class instance if
|
|
417
|
+
* no value is provided when calling the method.
|
|
418
|
+
* @returns The `getNode` method is returning an optional binary search tree node (`OptBSTN<NODE>`).
|
|
419
|
+
* It is using the `getNodes` method to find the node based on the provided predicate, beginning at
|
|
420
|
+
* the specified root node (`beginRoot`) and using the specified iteration type. The method then
|
|
421
|
+
* returns the first node found or `undefined` if no node is found.
|
|
424
422
|
*/
|
|
425
|
-
getNode(
|
|
423
|
+
getNode(predicate, beginRoot = this._root, iterationType = this.iterationType) {
|
|
426
424
|
var _a;
|
|
427
|
-
return (_a = this.getNodes(
|
|
425
|
+
return (_a = this.getNodes(predicate, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
428
426
|
}
|
|
429
427
|
/**
|
|
430
428
|
* Time Complexity: O(log n)
|
|
@@ -440,7 +438,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
440
438
|
* @returns The method is returning a NODE object or undefined.
|
|
441
439
|
*/
|
|
442
440
|
getNodeByKey(key, iterationType = this.iterationType) {
|
|
443
|
-
return this.getNode(key, this.
|
|
441
|
+
return this.getNode(key, this._root, iterationType);
|
|
444
442
|
}
|
|
445
443
|
/**
|
|
446
444
|
* Time complexity: O(n)
|
|
@@ -450,11 +448,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
450
448
|
* the callback function.
|
|
451
449
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
452
450
|
* during the depth-first search traversal. It is an optional parameter and defaults to
|
|
453
|
-
* `this.
|
|
451
|
+
* `this._DEFAULT_BTN_CALLBACK`. The type `C` represents the type of the callback function.
|
|
454
452
|
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
455
453
|
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
456
454
|
* take one of the following values:
|
|
457
|
-
* @param {
|
|
455
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
|
|
458
456
|
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
459
457
|
* node entry. If not specified, the default value is the root of the tree.
|
|
460
458
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
@@ -462,7 +460,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
462
460
|
* following values:
|
|
463
461
|
* @returns The method is returning an array of the return type of the callback function.
|
|
464
462
|
*/
|
|
465
|
-
dfs(callback = this.
|
|
463
|
+
dfs(callback = this._DEFAULT_BTN_CALLBACK, pattern = 'IN', beginRoot = this._root, iterationType = this.iterationType) {
|
|
466
464
|
return super.dfs(callback, pattern, beginRoot, iterationType);
|
|
467
465
|
}
|
|
468
466
|
/**
|
|
@@ -474,7 +472,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
474
472
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
475
473
|
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
476
474
|
* node being visited, and it can return a value of any type.
|
|
477
|
-
* @param {
|
|
475
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
|
|
478
476
|
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
479
477
|
* object. If no value is provided, the default value is the root of the tree.
|
|
480
478
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
@@ -482,7 +480,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
482
480
|
* the following values:
|
|
483
481
|
* @returns an array of the return type of the callback function.
|
|
484
482
|
*/
|
|
485
|
-
bfs(callback = this.
|
|
483
|
+
bfs(callback = this._DEFAULT_BTN_CALLBACK, beginRoot = this._root, iterationType = this.iterationType) {
|
|
486
484
|
return super.bfs(callback, beginRoot, iterationType, false);
|
|
487
485
|
}
|
|
488
486
|
/**
|
|
@@ -494,7 +492,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
494
492
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
495
493
|
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
496
494
|
* tree during the iteration process.
|
|
497
|
-
* @param {
|
|
495
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
|
|
498
496
|
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
499
497
|
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
500
498
|
* value is provided, the root of
|
|
@@ -503,7 +501,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
503
501
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
504
502
|
* function.
|
|
505
503
|
*/
|
|
506
|
-
listLevels(callback = this.
|
|
504
|
+
listLevels(callback = this._DEFAULT_BTN_CALLBACK, beginRoot = this._root, iterationType = this.iterationType) {
|
|
507
505
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
508
506
|
}
|
|
509
507
|
/**
|
|
@@ -518,7 +516,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
518
516
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
519
517
|
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
520
518
|
* 0, or 1, where:
|
|
521
|
-
* @param {
|
|
519
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} targetNode - The `targetNode` parameter is the node in
|
|
522
520
|
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
523
521
|
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
524
522
|
* `targetNode` is provided,
|
|
@@ -527,12 +525,12 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
527
525
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
528
526
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
529
527
|
*/
|
|
530
|
-
lesserOrGreaterTraverse(callback = this.
|
|
528
|
+
lesserOrGreaterTraverse(callback = this._DEFAULT_BTN_CALLBACK, lesserOrGreater = -1, targetNode = this._root, iterationType = this.iterationType) {
|
|
531
529
|
const targetNodeEnsured = this.ensureNode(targetNode);
|
|
532
530
|
const ans = [];
|
|
533
|
-
if (!
|
|
531
|
+
if (!this._root)
|
|
534
532
|
return ans;
|
|
535
|
-
if (!
|
|
533
|
+
if (!targetNodeEnsured)
|
|
536
534
|
return ans;
|
|
537
535
|
const targetKey = targetNodeEnsured.key;
|
|
538
536
|
if (iterationType === 'RECURSIVE') {
|
|
@@ -545,11 +543,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
545
543
|
if (this.isRealNode(cur.right))
|
|
546
544
|
dfs(cur.right);
|
|
547
545
|
};
|
|
548
|
-
dfs(this.
|
|
546
|
+
dfs(this._root);
|
|
549
547
|
return ans;
|
|
550
548
|
}
|
|
551
549
|
else {
|
|
552
|
-
const queue = new queue_1.Queue([this.
|
|
550
|
+
const queue = new queue_1.Queue([this._root]);
|
|
553
551
|
while (queue.size > 0) {
|
|
554
552
|
const cur = queue.shift();
|
|
555
553
|
if (this.isRealNode(cur)) {
|
|
@@ -626,8 +624,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
626
624
|
* @returns a boolean value.
|
|
627
625
|
*/
|
|
628
626
|
isAVLBalanced(iterationType = this.iterationType) {
|
|
629
|
-
|
|
630
|
-
if (!this.root)
|
|
627
|
+
if (!this._root)
|
|
631
628
|
return true;
|
|
632
629
|
let balanced = true;
|
|
633
630
|
if (iterationType === 'RECURSIVE') {
|
|
@@ -639,11 +636,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
639
636
|
balanced = false;
|
|
640
637
|
return Math.max(leftHeight, rightHeight) + 1;
|
|
641
638
|
};
|
|
642
|
-
_height(this.
|
|
639
|
+
_height(this._root);
|
|
643
640
|
}
|
|
644
641
|
else {
|
|
645
642
|
const stack = [];
|
|
646
|
-
let node = this.
|
|
643
|
+
let node = this._root, last = undefined;
|
|
647
644
|
const depths = new Map();
|
|
648
645
|
while (stack.length > 0 || node) {
|
|
649
646
|
if (node) {
|
|
@@ -655,8 +652,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
655
652
|
if (!node.right || last === node.right) {
|
|
656
653
|
node = stack.pop();
|
|
657
654
|
if (node) {
|
|
658
|
-
const left = node.left ?
|
|
659
|
-
const right = node.right ?
|
|
655
|
+
const left = node.left ? depths.get(node.left) : -1;
|
|
656
|
+
const right = node.right ? depths.get(node.right) : -1;
|
|
660
657
|
if (Math.abs(left - right) > 1)
|
|
661
658
|
return false;
|
|
662
659
|
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, 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} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw`
|
|
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(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[];
|
|
122
119
|
/**
|
|
123
120
|
* Time Complexity: O(1)
|
|
124
121
|
* Space Complexity: O(1)
|