bst-typed 1.51.0 → 1.51.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/dist/data-structures/binary-tree/avl-tree-multi-map.js +1 -1
- package/dist/data-structures/binary-tree/avl-tree.d.ts +1 -1
- package/dist/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +17 -11
- package/dist/data-structures/binary-tree/binary-tree.js +98 -92
- package/dist/data-structures/binary-tree/bst.d.ts +27 -1
- package/dist/data-structures/binary-tree/bst.js +68 -39
- package/dist/data-structures/binary-tree/rb-tree.d.ts +2 -48
- package/dist/data-structures/binary-tree/rb-tree.js +8 -63
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/data-structures/binary-tree/tree-multi-map.js +2 -2
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +98 -93
- package/src/data-structures/binary-tree/bst.ts +69 -34
- package/src/data-structures/binary-tree/rb-tree.ts +8 -74
- package/src/data-structures/binary-tree/tree-multi-map.ts +2 -2
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
BinaryTreeDeleteResult,
|
|
3
|
-
BSTNKeyOrNode,
|
|
4
3
|
BTNCallback,
|
|
5
|
-
IterationType,
|
|
6
4
|
KeyOrNodeOrEntry,
|
|
7
5
|
RBTreeOptions,
|
|
8
6
|
RedBlackTreeNested,
|
|
@@ -73,23 +71,13 @@ export class RedBlackTree<
|
|
|
73
71
|
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: RBTreeOptions<K>) {
|
|
74
72
|
super([], options);
|
|
75
73
|
|
|
76
|
-
this._root = this.
|
|
74
|
+
this._root = this.NIL;
|
|
77
75
|
|
|
78
76
|
if (keysOrNodesOrEntries) {
|
|
79
77
|
this.addMany(keysOrNodesOrEntries);
|
|
80
78
|
}
|
|
81
79
|
}
|
|
82
80
|
|
|
83
|
-
protected _SENTINEL: NODE = new RedBlackTreeNode<K, V>(NaN as K) as unknown as NODE;
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* The function returns the value of the _SENTINEL property.
|
|
87
|
-
* @returns The method is returning the value of the `_SENTINEL` property.
|
|
88
|
-
*/
|
|
89
|
-
get SENTINEL(): NODE {
|
|
90
|
-
return this._SENTINEL;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
81
|
protected override _root: NODE | undefined;
|
|
94
82
|
|
|
95
83
|
/**
|
|
@@ -184,60 +172,6 @@ export class RedBlackTree<
|
|
|
184
172
|
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
|
|
185
173
|
}
|
|
186
174
|
|
|
187
|
-
/**
|
|
188
|
-
* Time Complexity: O(1)
|
|
189
|
-
* Space Complexity: O(1)
|
|
190
|
-
*/
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* Time Complexity: O(1)
|
|
194
|
-
* Space Complexity: O(1)
|
|
195
|
-
*
|
|
196
|
-
* The function checks if a given node is a real node in a Red-Black Tree.
|
|
197
|
-
* @param {NODE | undefined} node - The `node` parameter is of type `NODE | undefined`, which means
|
|
198
|
-
* it can either be of type `NODE` or `undefined`.
|
|
199
|
-
* @returns a boolean value.
|
|
200
|
-
*/
|
|
201
|
-
override isRealNode(node: NODE | undefined): node is NODE {
|
|
202
|
-
if (node === this.SENTINEL || node === undefined) return false;
|
|
203
|
-
return node instanceof RedBlackTreeNode;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* Time Complexity: O(log n)
|
|
208
|
-
* Space Complexity: O(1)
|
|
209
|
-
*/
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Time Complexity: O(log n)
|
|
213
|
-
* Space Complexity: O(1)
|
|
214
|
-
*
|
|
215
|
-
* The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
|
|
216
|
-
* callback function.
|
|
217
|
-
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
|
|
218
|
-
* that you want to search for in the binary search tree. It can be of any type that is compatible
|
|
219
|
-
* with the type of nodes in the tree.
|
|
220
|
-
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
221
|
-
* the tree. It is used to determine whether a node matches the given identifier. The `callback`
|
|
222
|
-
* function should take a node as its parameter and return a value that can be compared to the
|
|
223
|
-
* `identifier` parameter.
|
|
224
|
-
* @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
|
|
225
|
-
* search tree. It can be either a key or a node. If it is a key, it will be converted to a node
|
|
226
|
-
* using the `ensureNode` method. If it is not provided, the `root`
|
|
227
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
228
|
-
* be performed when searching for nodes in the binary search tree. It is an optional parameter and
|
|
229
|
-
* its default value is taken from the `iterationType` property of the class.
|
|
230
|
-
* @returns The method is returning a value of type `NODE | null | undefined`.
|
|
231
|
-
*/
|
|
232
|
-
override getNode<C extends BTNCallback<NODE>>(
|
|
233
|
-
identifier: ReturnType<C> | undefined,
|
|
234
|
-
callback: C = this._defaultOneParamCallback as C,
|
|
235
|
-
beginRoot: BSTNKeyOrNode<K, NODE> = this.root,
|
|
236
|
-
iterationType: IterationType = this.iterationType
|
|
237
|
-
): NODE | null | undefined {
|
|
238
|
-
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
175
|
/**
|
|
242
176
|
* Time Complexity: O(1)
|
|
243
177
|
* Space Complexity: O(1)
|
|
@@ -252,7 +186,7 @@ export class RedBlackTree<
|
|
|
252
186
|
*/
|
|
253
187
|
override clear() {
|
|
254
188
|
super.clear();
|
|
255
|
-
this._root = this.
|
|
189
|
+
this._root = this.NIL;
|
|
256
190
|
}
|
|
257
191
|
|
|
258
192
|
/**
|
|
@@ -308,13 +242,13 @@ export class RedBlackTree<
|
|
|
308
242
|
* deleted is not found.
|
|
309
243
|
* @param {C} callback - The `callback` parameter is a function that is used to retrieve a node from
|
|
310
244
|
* the binary tree based on its identifier. It is an optional parameter and if not provided, the
|
|
311
|
-
* `
|
|
245
|
+
* `_DEFAULT_CALLBACK` function is used as the default callback. The callback function should
|
|
312
246
|
* return the identifier of the node to
|
|
313
247
|
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
314
248
|
*/
|
|
315
249
|
override delete<C extends BTNCallback<NODE>>(
|
|
316
250
|
identifier: ReturnType<C> | null | undefined,
|
|
317
|
-
callback: C = this.
|
|
251
|
+
callback: C = this._DEFAULT_CALLBACK as C
|
|
318
252
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
319
253
|
if (identifier === null) return [];
|
|
320
254
|
const results: BinaryTreeDeleteResult<NODE>[] = [];
|
|
@@ -430,9 +364,9 @@ export class RedBlackTree<
|
|
|
430
364
|
while (this.isRealNode(current)) {
|
|
431
365
|
parent = current;
|
|
432
366
|
if (node.key < current.key) {
|
|
433
|
-
current = current.left ?? this.
|
|
367
|
+
current = current.left ?? this.NIL;
|
|
434
368
|
} else if (node.key > current.key) {
|
|
435
|
-
current = current.right ?? this.
|
|
369
|
+
current = current.right ?? this.NIL;
|
|
436
370
|
} else {
|
|
437
371
|
this._replaceNode(current, node);
|
|
438
372
|
return 'UPDATED';
|
|
@@ -449,8 +383,8 @@ export class RedBlackTree<
|
|
|
449
383
|
parent.right = node;
|
|
450
384
|
}
|
|
451
385
|
|
|
452
|
-
node.left = this.
|
|
453
|
-
node.right = this.
|
|
386
|
+
node.left = this.NIL;
|
|
387
|
+
node.right = this.NIL;
|
|
454
388
|
node.color = 'RED';
|
|
455
389
|
|
|
456
390
|
this._insertFixup(node);
|
|
@@ -242,7 +242,7 @@ export class TreeMultiMap<
|
|
|
242
242
|
* function. It can also be null or undefined if no node needs to be deleted.
|
|
243
243
|
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
|
|
244
244
|
* input and returns a value of type `ReturnType<C>`. It is used to determine if a node matches the
|
|
245
|
-
* identifier for deletion. If no callback is provided, the `
|
|
245
|
+
* identifier for deletion. If no callback is provided, the `_DEFAULT_CALLBACK` function is
|
|
246
246
|
* used
|
|
247
247
|
* @param [ignoreCount=false] - A boolean value indicating whether to ignore the count of the target
|
|
248
248
|
* node when performing deletion. If set to true, the count of the target node will not be considered
|
|
@@ -252,7 +252,7 @@ export class TreeMultiMap<
|
|
|
252
252
|
*/
|
|
253
253
|
override delete<C extends BTNCallback<NODE>>(
|
|
254
254
|
identifier: ReturnType<C> | null | undefined,
|
|
255
|
-
callback: C = this.
|
|
255
|
+
callback: C = this._DEFAULT_CALLBACK as C,
|
|
256
256
|
ignoreCount = false
|
|
257
257
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
258
258
|
if (identifier === null) return [];
|