linked-list-typed 1.53.9 → 1.54.0
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.d.ts +5 -17
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +1 -20
- package/dist/data-structures/binary-tree/avl-tree.d.ts +9 -24
- package/dist/data-structures/binary-tree/avl-tree.js +18 -34
- package/dist/data-structures/binary-tree/binary-tree.d.ts +40 -37
- package/dist/data-structures/binary-tree/binary-tree.js +68 -53
- package/dist/data-structures/binary-tree/bst.d.ts +36 -50
- package/dist/data-structures/binary-tree/bst.js +45 -60
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +29 -43
- package/dist/data-structures/binary-tree/red-black-tree.js +45 -59
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +33 -45
- package/dist/data-structures/binary-tree/tree-multi-map.js +70 -83
- package/dist/interfaces/binary-tree.d.ts +4 -3
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +5 -4
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -2
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +23 -31
- package/src/data-structures/binary-tree/avl-tree.ts +35 -46
- package/src/data-structures/binary-tree/binary-tree.ts +105 -66
- package/src/data-structures/binary-tree/bst.ts +105 -104
- package/src/data-structures/binary-tree/red-black-tree.ts +68 -73
- package/src/data-structures/binary-tree/tree-multi-map.ts +98 -102
- package/src/interfaces/binary-tree.ts +16 -3
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +4 -2
- package/src/types/data-structures/binary-tree/avl-tree.ts +4 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/bst.ts +4 -2
- package/src/types/data-structures/binary-tree/rb-tree.ts +6 -4
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -2
|
@@ -5,38 +5,38 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { BSTNodeNested, BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparable, Comparator, CP, DFSOrderPattern, EntryCallback, IterationType, NodeCallback, NodePredicate, OptNode } from '../../types';
|
|
8
|
+
import { BSTNested, BSTNodeNested, BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparable, Comparator, CP, DFSOrderPattern, EntryCallback, IterationType, NodeCallback, NodePredicate, OptNode, OptNodeOrNull } from '../../types';
|
|
9
9
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { Range } from '../../common';
|
|
12
12
|
export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNodeNested<K, V>> extends BinaryTreeNode<K, V, NODE> {
|
|
13
13
|
parent?: NODE;
|
|
14
14
|
constructor(key: K, value?: V);
|
|
15
|
-
|
|
15
|
+
_left?: OptNodeOrNull<NODE>;
|
|
16
16
|
/**
|
|
17
17
|
* The function returns the value of the `_left` property.
|
|
18
18
|
* @returns The `_left` property of the current object is being returned.
|
|
19
19
|
*/
|
|
20
|
-
get left():
|
|
20
|
+
get left(): OptNodeOrNull<NODE>;
|
|
21
21
|
/**
|
|
22
22
|
* The function sets the left child of a node and updates the parent reference of the child.
|
|
23
23
|
* @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be an
|
|
24
24
|
* instance of the `NODE` class or `undefined`.
|
|
25
25
|
*/
|
|
26
|
-
set left(v:
|
|
27
|
-
|
|
26
|
+
set left(v: OptNodeOrNull<NODE>);
|
|
27
|
+
_right?: OptNodeOrNull<NODE>;
|
|
28
28
|
/**
|
|
29
29
|
* The function returns the right node of a binary tree or undefined if there is no right node.
|
|
30
30
|
* @returns The method is returning the value of the `_right` property, which is of type `NODE` or
|
|
31
31
|
* `undefined`.
|
|
32
32
|
*/
|
|
33
|
-
get right():
|
|
33
|
+
get right(): OptNodeOrNull<NODE>;
|
|
34
34
|
/**
|
|
35
35
|
* The function sets the right child of a node and updates the parent reference of the child.
|
|
36
36
|
* @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be a
|
|
37
37
|
* `NODE` object or `undefined`.
|
|
38
38
|
*/
|
|
39
|
-
set right(v:
|
|
39
|
+
set right(v: OptNodeOrNull<NODE>);
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
42
42
|
* 1. Node Order: Each node's left child has a lesser value, and the right child has a greater value.
|
|
@@ -103,7 +103,7 @@ export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE>
|
|
|
103
103
|
* console.log(findLCA(5, 35)); // 15
|
|
104
104
|
* console.log(findLCA(20, 30)); // 25
|
|
105
105
|
*/
|
|
106
|
-
export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>> extends BinaryTree<K, V, R, NODE> implements IBinaryTree<K, V, R, NODE> {
|
|
106
|
+
export declare class BST<K = any, V = any, R = object, MK = any, MV = any, MR = object, NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>, TREE extends BST<K, V, R, MK, MV, MR, NODE, TREE> = BST<K, V, R, MK, MV, MR, NODE, BSTNested<K, V, R, MK, MV, MR, NODE>>> extends BinaryTree<K, V, R, MK, MV, MR, NODE, TREE> implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE> {
|
|
107
107
|
/**
|
|
108
108
|
* This is the constructor function for a Binary Search Tree class in TypeScript.
|
|
109
109
|
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
@@ -126,19 +126,6 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
126
126
|
* @returns The `isReverse` property of the object, which is a boolean value.
|
|
127
127
|
*/
|
|
128
128
|
get isReverse(): boolean;
|
|
129
|
-
protected _comparator: Comparator<K>;
|
|
130
|
-
/**
|
|
131
|
-
* The function returns the value of the _comparator property.
|
|
132
|
-
* @returns The `_comparator` property is being returned.
|
|
133
|
-
*/
|
|
134
|
-
get comparator(): Comparator<K>;
|
|
135
|
-
protected _specifyComparable?: (key: K) => Comparable;
|
|
136
|
-
/**
|
|
137
|
-
* This function returns the value of the `_specifyComparable` property.
|
|
138
|
-
* @returns The method `specifyComparable()` is being returned, which is a getter method for the
|
|
139
|
-
* `_specifyComparable` property.
|
|
140
|
-
*/
|
|
141
|
-
get specifyComparable(): ((key: K) => Comparable) | undefined;
|
|
142
129
|
/**
|
|
143
130
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
144
131
|
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
@@ -149,17 +136,23 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
149
136
|
*/
|
|
150
137
|
createNode(key: K, value?: V): NODE;
|
|
151
138
|
/**
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
* and
|
|
139
|
+
* The function creates a new binary search tree with the specified options.
|
|
140
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
141
|
+
* behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which has the
|
|
142
|
+
* following properties:
|
|
143
|
+
* @returns a new instance of the BST class with the provided options.
|
|
144
|
+
*/
|
|
145
|
+
createTree(options?: BSTOptions<K, V, R>): TREE;
|
|
146
|
+
/**
|
|
147
|
+
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
148
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of
|
|
149
|
+
* type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
150
|
+
* element.
|
|
151
|
+
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
152
|
+
* value associated with a key in a key-value pair.
|
|
153
|
+
* @returns either a NODE object or undefined.
|
|
161
154
|
*/
|
|
162
|
-
|
|
155
|
+
protected _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): [OptNode<NODE>, V | undefined];
|
|
163
156
|
/**
|
|
164
157
|
* Time Complexity: O(log n)
|
|
165
158
|
* Space Complexity: O(log n)
|
|
@@ -227,16 +220,6 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
227
220
|
* successfully inserted into the data structure.
|
|
228
221
|
*/
|
|
229
222
|
addMany(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
|
|
230
|
-
/**
|
|
231
|
-
* Time Complexity: O(n)
|
|
232
|
-
* Space Complexity: O(1)
|
|
233
|
-
*
|
|
234
|
-
* The `merge` function overrides the base class method by adding elements from another
|
|
235
|
-
* binary search tree.
|
|
236
|
-
* @param anotherTree - `anotherTree` is an instance of a Binary Search Tree (BST) with key type `K`,
|
|
237
|
-
* value type `V`, return type `R`, node type `NODE`, and tree type `TREE`.
|
|
238
|
-
*/
|
|
239
|
-
merge(anotherTree: this): void;
|
|
240
223
|
/**
|
|
241
224
|
* Time Complexity: O(log n)
|
|
242
225
|
* Space Complexity: O(k + log n)
|
|
@@ -415,17 +398,19 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
415
398
|
* @returns a boolean value.
|
|
416
399
|
*/
|
|
417
400
|
isAVLBalanced(iterationType?: IterationType): boolean;
|
|
418
|
-
|
|
401
|
+
protected _comparator: Comparator<K>;
|
|
419
402
|
/**
|
|
420
|
-
* The function
|
|
421
|
-
* @
|
|
422
|
-
* type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
423
|
-
* element.
|
|
424
|
-
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
425
|
-
* value associated with a key in a key-value pair.
|
|
426
|
-
* @returns either a NODE object or undefined.
|
|
403
|
+
* The function returns the value of the _comparator property.
|
|
404
|
+
* @returns The `_comparator` property is being returned.
|
|
427
405
|
*/
|
|
428
|
-
|
|
406
|
+
get comparator(): Comparator<K>;
|
|
407
|
+
protected _specifyComparable?: (key: K) => Comparable;
|
|
408
|
+
/**
|
|
409
|
+
* This function returns the value of the `_specifyComparable` property.
|
|
410
|
+
* @returns The method `specifyComparable()` is being returned, which is a getter method for the
|
|
411
|
+
* `_specifyComparable` property.
|
|
412
|
+
*/
|
|
413
|
+
get specifyComparable(): ((key: K) => Comparable) | undefined;
|
|
429
414
|
/**
|
|
430
415
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
431
416
|
* root.
|
|
@@ -433,4 +418,5 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
433
418
|
*/
|
|
434
419
|
protected _setRoot(v: OptNode<NODE>): void;
|
|
435
420
|
protected _compare(a: K, b: K): number;
|
|
421
|
+
map(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: BSTOptions<MK, MV, MR>, thisArg?: any): BST<MK, MV, MR>;
|
|
436
422
|
}
|
|
@@ -174,21 +174,6 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
174
174
|
get isReverse() {
|
|
175
175
|
return this._isReverse;
|
|
176
176
|
}
|
|
177
|
-
/**
|
|
178
|
-
* The function returns the value of the _comparator property.
|
|
179
|
-
* @returns The `_comparator` property is being returned.
|
|
180
|
-
*/
|
|
181
|
-
get comparator() {
|
|
182
|
-
return this._comparator;
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* This function returns the value of the `_specifyComparable` property.
|
|
186
|
-
* @returns The method `specifyComparable()` is being returned, which is a getter method for the
|
|
187
|
-
* `_specifyComparable` property.
|
|
188
|
-
*/
|
|
189
|
-
get specifyComparable() {
|
|
190
|
-
return this._specifyComparable;
|
|
191
|
-
}
|
|
192
177
|
/**
|
|
193
178
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
194
179
|
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
@@ -201,20 +186,30 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
201
186
|
return new BSTNode(key, this._isMapMode ? undefined : value);
|
|
202
187
|
}
|
|
203
188
|
/**
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
* @param [options] - The `options` parameter in the `createTree` method is an optional object that
|
|
210
|
-
* can contain the following properties:
|
|
211
|
-
* @returns A new instance of a Binary Search Tree (BST) is being returned with the specified options
|
|
212
|
-
* and properties inherited from the current instance.
|
|
189
|
+
* The function creates a new binary search tree with the specified options.
|
|
190
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
191
|
+
* behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which has the
|
|
192
|
+
* following properties:
|
|
193
|
+
* @returns a new instance of the BST class with the provided options.
|
|
213
194
|
*/
|
|
214
|
-
// @ts-ignore
|
|
215
195
|
createTree(options) {
|
|
216
196
|
return new BST([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
|
|
217
197
|
}
|
|
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
|
+
}
|
|
218
213
|
/**
|
|
219
214
|
* Time Complexity: O(log n)
|
|
220
215
|
* Space Complexity: O(log n)
|
|
@@ -293,7 +288,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
293
288
|
this._size++;
|
|
294
289
|
return true;
|
|
295
290
|
}
|
|
296
|
-
|
|
291
|
+
if (current.left !== null)
|
|
292
|
+
current = current.left;
|
|
297
293
|
}
|
|
298
294
|
else {
|
|
299
295
|
if (current.right === undefined) {
|
|
@@ -303,7 +299,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
303
299
|
this._size++;
|
|
304
300
|
return true;
|
|
305
301
|
}
|
|
306
|
-
|
|
302
|
+
if (current.right !== null)
|
|
303
|
+
current = current.right;
|
|
307
304
|
}
|
|
308
305
|
}
|
|
309
306
|
return false;
|
|
@@ -410,18 +407,6 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
410
407
|
}
|
|
411
408
|
return inserted;
|
|
412
409
|
}
|
|
413
|
-
/**
|
|
414
|
-
* Time Complexity: O(n)
|
|
415
|
-
* Space Complexity: O(1)
|
|
416
|
-
*
|
|
417
|
-
* The `merge` function overrides the base class method by adding elements from another
|
|
418
|
-
* binary search tree.
|
|
419
|
-
* @param anotherTree - `anotherTree` is an instance of a Binary Search Tree (BST) with key type `K`,
|
|
420
|
-
* value type `V`, return type `R`, node type `NODE`, and tree type `TREE`.
|
|
421
|
-
*/
|
|
422
|
-
merge(anotherTree) {
|
|
423
|
-
this.addMany(anotherTree, [], false);
|
|
424
|
-
}
|
|
425
410
|
/**
|
|
426
411
|
* Time Complexity: O(log n)
|
|
427
412
|
* Space Complexity: O(k + log n)
|
|
@@ -822,7 +807,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
822
807
|
while (stack.length > 0 || node) {
|
|
823
808
|
if (node) {
|
|
824
809
|
stack.push(node);
|
|
825
|
-
|
|
810
|
+
if (node.left !== null)
|
|
811
|
+
node = node.left;
|
|
826
812
|
}
|
|
827
813
|
else {
|
|
828
814
|
node = stack[stack.length - 1];
|
|
@@ -845,29 +831,20 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
845
831
|
}
|
|
846
832
|
return balanced;
|
|
847
833
|
}
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
}
|
|
855
|
-
return newTree;
|
|
834
|
+
/**
|
|
835
|
+
* The function returns the value of the _comparator property.
|
|
836
|
+
* @returns The `_comparator` property is being returned.
|
|
837
|
+
*/
|
|
838
|
+
get comparator() {
|
|
839
|
+
return this._comparator;
|
|
856
840
|
}
|
|
857
841
|
/**
|
|
858
|
-
*
|
|
859
|
-
* @
|
|
860
|
-
*
|
|
861
|
-
* element.
|
|
862
|
-
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
863
|
-
* value associated with a key in a key-value pair.
|
|
864
|
-
* @returns either a NODE object or undefined.
|
|
842
|
+
* This function returns the value of the `_specifyComparable` property.
|
|
843
|
+
* @returns The method `specifyComparable()` is being returned, which is a getter method for the
|
|
844
|
+
* `_specifyComparable` property.
|
|
865
845
|
*/
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
if (node === null)
|
|
869
|
-
return [undefined, undefined];
|
|
870
|
-
return [node, value !== null && value !== void 0 ? value : entryValue];
|
|
846
|
+
get specifyComparable() {
|
|
847
|
+
return this._specifyComparable;
|
|
871
848
|
}
|
|
872
849
|
/**
|
|
873
850
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
@@ -883,5 +860,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
883
860
|
_compare(a, b) {
|
|
884
861
|
return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
|
|
885
862
|
}
|
|
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
|
+
}
|
|
886
871
|
}
|
|
887
872
|
exports.BST = BST;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { BinaryTreeDeleteResult, BTNRep, CRUD, EntryCallback, RBTNColor, RedBlackTreeOptions, RedBlackTreeNodeNested } from '../../types';
|
|
1
|
+
import type { BinaryTreeDeleteResult, BTNRep, CRUD, EntryCallback, RBTNColor, RedBlackTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
2
2
|
import { BST, BSTNode } from './bst';
|
|
3
3
|
import { IBinaryTree } from '../../interfaces';
|
|
4
4
|
export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
|
|
@@ -14,17 +14,6 @@ export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTre
|
|
|
14
14
|
* Tree Node. It is an optional parameter with a default value of `'BLACK'`.
|
|
15
15
|
*/
|
|
16
16
|
constructor(key: K, value?: V, color?: RBTNColor);
|
|
17
|
-
protected _color: RBTNColor;
|
|
18
|
-
/**
|
|
19
|
-
* The function returns the color value of a variable.
|
|
20
|
-
* @returns The color value stored in the private variable `_color`.
|
|
21
|
-
*/
|
|
22
|
-
get color(): RBTNColor;
|
|
23
|
-
/**
|
|
24
|
-
* The function sets the color property to the specified value.
|
|
25
|
-
* @param {RBTNColor} value - The value parameter is of type RBTNColor.
|
|
26
|
-
*/
|
|
27
|
-
set color(value: RBTNColor);
|
|
28
17
|
}
|
|
29
18
|
/**
|
|
30
19
|
* 1. Efficient self-balancing, but not completely balanced. Compared with AVLTree, the addition and deletion efficiency is high but the query efficiency is slightly lower.
|
|
@@ -79,12 +68,12 @@ export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTre
|
|
|
79
68
|
* );
|
|
80
69
|
* console.log(stocksInRange); // ['GOOGL', 'MSFT', 'META']
|
|
81
70
|
*/
|
|
82
|
-
export declare class RedBlackTree<K = any, V = any, R = object, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>> extends BST<K, V, R, NODE> implements IBinaryTree<K, V, R, NODE> {
|
|
71
|
+
export declare class RedBlackTree<K = any, V = any, R = object, MK = any, MV = any, MR = object, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>, TREE extends RedBlackTree<K, V, R, MK, MV, MR, NODE, TREE> = RedBlackTree<K, V, R, MK, MV, MR, NODE, RedBlackTreeNested<K, V, R, MK, MV, MR, NODE>>> extends BST<K, V, R, MK, MV, MR, NODE, TREE> implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE> {
|
|
83
72
|
/**
|
|
84
73
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
|
|
85
74
|
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
86
75
|
* iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
|
|
87
|
-
* initialize the
|
|
76
|
+
* initialize the RBTree with the provided elements.
|
|
88
77
|
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
89
78
|
* constructor. It is of type `RedBlackTreeOptions<K, V, R>`. This object can contain various options for
|
|
90
79
|
* configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
|
|
@@ -113,15 +102,12 @@ export declare class RedBlackTree<K = any, V = any, R = object, NODE extends Red
|
|
|
113
102
|
*/
|
|
114
103
|
createNode(key: K, value?: V, color?: RBTNColor): NODE;
|
|
115
104
|
/**
|
|
116
|
-
* The function
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
* allows you to pass additional configuration options when creating a new Red-
|
|
121
|
-
* @returns A new instance of a RedBlackTree with the specified options and properties from the
|
|
122
|
-
* current object is being returned.
|
|
105
|
+
* The function creates a new Red-Black Tree with the specified options.
|
|
106
|
+
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
107
|
+
* configuration options for creating the Red-Black Tree. It has the following properties:
|
|
108
|
+
* @returns a new instance of a RedBlackTree object.
|
|
123
109
|
*/
|
|
124
|
-
createTree(options?: RedBlackTreeOptions<K, V, R>):
|
|
110
|
+
createTree(options?: RedBlackTreeOptions<K, V, R>): TREE;
|
|
125
111
|
/**
|
|
126
112
|
* Time Complexity: O(1)
|
|
127
113
|
* Space Complexity: O(1)
|
|
@@ -172,27 +158,6 @@ export declare class RedBlackTree<K = any, V = any, R = object, NODE extends Red
|
|
|
172
158
|
* balancing is needed.
|
|
173
159
|
*/
|
|
174
160
|
delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[];
|
|
175
|
-
/**
|
|
176
|
-
* Time Complexity: O(n)
|
|
177
|
-
* Space Complexity: O(n)
|
|
178
|
-
*
|
|
179
|
-
* The `map` function in TypeScript overrides the default behavior to create a new Red-Black Tree by
|
|
180
|
-
* applying a callback to each entry in the original tree.
|
|
181
|
-
* @param callback - A function that will be called for each entry in the tree, with parameters
|
|
182
|
-
* representing the key, value, index, and the tree itself. It should return an entry for the new
|
|
183
|
-
* tree.
|
|
184
|
-
* @param [options] - The `options` parameter in the `map` method is of type `RedBlackTreeOptions<MK, MV,
|
|
185
|
-
* MR>`. This parameter allows you to specify additional options or configurations for the Red-Black
|
|
186
|
-
* Tree that will be created during the mapping process. These options could include things like
|
|
187
|
-
* custom comparators
|
|
188
|
-
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
189
|
-
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
190
|
-
* (value of `this`) for the callback function. This can be useful when you want to access properties
|
|
191
|
-
* or
|
|
192
|
-
* @returns A new Red-Black Tree is being returned, where each entry has been transformed using the
|
|
193
|
-
* provided callback function.
|
|
194
|
-
*/
|
|
195
|
-
map<MK, MV, MR>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: RedBlackTreeOptions<MK, MV, MR>, thisArg?: any): RedBlackTree<MK, MV, MR, RedBlackTreeNode<MK, MV, RedBlackTreeNodeNested<MK, MV>>>;
|
|
196
161
|
/**
|
|
197
162
|
* Time Complexity: O(1)
|
|
198
163
|
* Space Complexity: O(1)
|
|
@@ -279,4 +244,25 @@ export declare class RedBlackTree<K = any, V = any, R = object, NODE extends Red
|
|
|
279
244
|
* @returns void, which means it does not return any value.
|
|
280
245
|
*/
|
|
281
246
|
protected _rightRotate(y: NODE | undefined): void;
|
|
247
|
+
/**
|
|
248
|
+
* Time Complexity: O(n)
|
|
249
|
+
* Space Complexity: O(n)
|
|
250
|
+
*
|
|
251
|
+
* The `map` function in TypeScript overrides the default behavior to create a new Red-Black Tree by
|
|
252
|
+
* applying a callback to each entry in the original tree.
|
|
253
|
+
* @param callback - A function that will be called for each entry in the tree, with parameters
|
|
254
|
+
* representing the key, value, index, and the tree itself. It should return an entry for the new
|
|
255
|
+
* tree.
|
|
256
|
+
* @param [options] - The `options` parameter in the `map` method is of type `RedBlackTreeOptions<MK, MV,
|
|
257
|
+
* MR>`. This parameter allows you to specify additional options or configurations for the Red-Black
|
|
258
|
+
* Tree that will be created during the mapping process. These options could include things like
|
|
259
|
+
* custom comparators
|
|
260
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
261
|
+
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
262
|
+
* (value of `this`) for the callback function. This can be useful when you want to access properties
|
|
263
|
+
* or
|
|
264
|
+
* @returns A new Red-Black Tree is being returned, where each entry has been transformed using the
|
|
265
|
+
* provided callback function.
|
|
266
|
+
*/
|
|
267
|
+
map(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: RedBlackTreeOptions<MK, MV, MR>, thisArg?: any): RedBlackTree<MK, MV, MR>;
|
|
282
268
|
}
|
|
@@ -18,20 +18,6 @@ class RedBlackTreeNode extends bst_1.BSTNode {
|
|
|
18
18
|
super(key, value);
|
|
19
19
|
this._color = color;
|
|
20
20
|
}
|
|
21
|
-
/**
|
|
22
|
-
* The function returns the color value of a variable.
|
|
23
|
-
* @returns The color value stored in the private variable `_color`.
|
|
24
|
-
*/
|
|
25
|
-
get color() {
|
|
26
|
-
return this._color;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* The function sets the color property to the specified value.
|
|
30
|
-
* @param {RBTNColor} value - The value parameter is of type RBTNColor.
|
|
31
|
-
*/
|
|
32
|
-
set color(value) {
|
|
33
|
-
this._color = value;
|
|
34
|
-
}
|
|
35
21
|
}
|
|
36
22
|
exports.RedBlackTreeNode = RedBlackTreeNode;
|
|
37
23
|
/**
|
|
@@ -92,7 +78,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
92
78
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
|
|
93
79
|
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
94
80
|
* iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
|
|
95
|
-
* initialize the
|
|
81
|
+
* initialize the RBTree with the provided elements.
|
|
96
82
|
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
97
83
|
* constructor. It is of type `RedBlackTreeOptions<K, V, R>`. This object can contain various options for
|
|
98
84
|
* configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
|
|
@@ -130,15 +116,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
130
116
|
return new RedBlackTreeNode(key, this._isMapMode ? undefined : value, color);
|
|
131
117
|
}
|
|
132
118
|
/**
|
|
133
|
-
* The function
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
* allows you to pass additional configuration options when creating a new Red-
|
|
138
|
-
* @returns A new instance of a RedBlackTree with the specified options and properties from the
|
|
139
|
-
* current object is being returned.
|
|
119
|
+
* The function creates a new Red-Black Tree with the specified options.
|
|
120
|
+
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
121
|
+
* configuration options for creating the Red-Black Tree. It has the following properties:
|
|
122
|
+
* @returns a new instance of a RedBlackTree object.
|
|
140
123
|
*/
|
|
141
|
-
// @ts-ignore
|
|
142
124
|
createTree(options) {
|
|
143
125
|
return new RedBlackTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn }, options));
|
|
144
126
|
}
|
|
@@ -235,8 +217,10 @@ class RedBlackTree extends bst_1.BST {
|
|
|
235
217
|
let originalColor = nodeToDelete.color;
|
|
236
218
|
let replacementNode;
|
|
237
219
|
if (!this.isRealNode(nodeToDelete.left)) {
|
|
238
|
-
|
|
239
|
-
|
|
220
|
+
if (nodeToDelete.right !== null) {
|
|
221
|
+
replacementNode = nodeToDelete.right;
|
|
222
|
+
this._transplant(nodeToDelete, nodeToDelete.right);
|
|
223
|
+
}
|
|
240
224
|
}
|
|
241
225
|
else if (!this.isRealNode(nodeToDelete.right)) {
|
|
242
226
|
replacementNode = nodeToDelete.left;
|
|
@@ -246,15 +230,18 @@ class RedBlackTree extends bst_1.BST {
|
|
|
246
230
|
const successor = this.getLeftMost(node => node, nodeToDelete.right);
|
|
247
231
|
if (successor) {
|
|
248
232
|
originalColor = successor.color;
|
|
249
|
-
|
|
233
|
+
if (successor.right !== null)
|
|
234
|
+
replacementNode = successor.right;
|
|
250
235
|
if (successor.parent === nodeToDelete) {
|
|
251
236
|
if (this.isRealNode(replacementNode)) {
|
|
252
237
|
replacementNode.parent = successor;
|
|
253
238
|
}
|
|
254
239
|
}
|
|
255
240
|
else {
|
|
256
|
-
|
|
257
|
-
|
|
241
|
+
if (successor.right !== null) {
|
|
242
|
+
this._transplant(successor, successor.right);
|
|
243
|
+
successor.right = nodeToDelete.right;
|
|
244
|
+
}
|
|
258
245
|
if (this.isRealNode(successor.right)) {
|
|
259
246
|
successor.right.parent = successor;
|
|
260
247
|
}
|
|
@@ -277,35 +264,6 @@ class RedBlackTree extends bst_1.BST {
|
|
|
277
264
|
results.push({ deleted: nodeToDelete, needBalanced: undefined });
|
|
278
265
|
return results;
|
|
279
266
|
}
|
|
280
|
-
/**
|
|
281
|
-
* Time Complexity: O(n)
|
|
282
|
-
* Space Complexity: O(n)
|
|
283
|
-
*
|
|
284
|
-
* The `map` function in TypeScript overrides the default behavior to create a new Red-Black Tree by
|
|
285
|
-
* applying a callback to each entry in the original tree.
|
|
286
|
-
* @param callback - A function that will be called for each entry in the tree, with parameters
|
|
287
|
-
* representing the key, value, index, and the tree itself. It should return an entry for the new
|
|
288
|
-
* tree.
|
|
289
|
-
* @param [options] - The `options` parameter in the `map` method is of type `RedBlackTreeOptions<MK, MV,
|
|
290
|
-
* MR>`. This parameter allows you to specify additional options or configurations for the Red-Black
|
|
291
|
-
* Tree that will be created during the mapping process. These options could include things like
|
|
292
|
-
* custom comparators
|
|
293
|
-
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
294
|
-
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
295
|
-
* (value of `this`) for the callback function. This can be useful when you want to access properties
|
|
296
|
-
* or
|
|
297
|
-
* @returns A new Red-Black Tree is being returned, where each entry has been transformed using the
|
|
298
|
-
* provided callback function.
|
|
299
|
-
*/
|
|
300
|
-
// @ts-ignore
|
|
301
|
-
map(callback, options, thisArg) {
|
|
302
|
-
const newTree = new RedBlackTree([], options);
|
|
303
|
-
let index = 0;
|
|
304
|
-
for (const [key, value] of this) {
|
|
305
|
-
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
306
|
-
}
|
|
307
|
-
return newTree;
|
|
308
|
-
}
|
|
309
267
|
/**
|
|
310
268
|
* Time Complexity: O(1)
|
|
311
269
|
* Space Complexity: O(1)
|
|
@@ -414,7 +372,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
414
372
|
* structure. It can either be a valid node or `undefined`.
|
|
415
373
|
*/
|
|
416
374
|
_insertFixup(z) {
|
|
417
|
-
var _a, _b, _c, _d;
|
|
375
|
+
var _a, _b, _c, _d, _e;
|
|
418
376
|
// Continue fixing the tree as long as the parent of z is red
|
|
419
377
|
while (((_a = z === null || z === void 0 ? void 0 : z.parent) === null || _a === void 0 ? void 0 : _a.color) === 'RED') {
|
|
420
378
|
// Check if the parent of z is the left child of its parent
|
|
@@ -448,7 +406,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
448
406
|
else {
|
|
449
407
|
// Symmetric case for the right child (left and right exchanged)
|
|
450
408
|
// Follow the same logic as above with left and right exchanged
|
|
451
|
-
const y = (_d = (_c = z === null || z === void 0 ? void 0 : z.parent) === null || _c === void 0 ? void 0 : _c.parent) === null || _d === void 0 ? void 0 : _d.left;
|
|
409
|
+
const y = (_e = (_d = (_c = z === null || z === void 0 ? void 0 : z.parent) === null || _c === void 0 ? void 0 : _c.parent) === null || _d === void 0 ? void 0 : _d.left) !== null && _e !== void 0 ? _e : undefined;
|
|
452
410
|
if ((y === null || y === void 0 ? void 0 : y.color) === 'RED') {
|
|
453
411
|
z.parent.color = 'BLACK';
|
|
454
412
|
y.color = 'BLACK';
|
|
@@ -621,5 +579,33 @@ class RedBlackTree extends bst_1.BST {
|
|
|
621
579
|
x.right = y;
|
|
622
580
|
y.parent = x;
|
|
623
581
|
}
|
|
582
|
+
/**
|
|
583
|
+
* Time Complexity: O(n)
|
|
584
|
+
* Space Complexity: O(n)
|
|
585
|
+
*
|
|
586
|
+
* The `map` function in TypeScript overrides the default behavior to create a new Red-Black Tree by
|
|
587
|
+
* applying a callback to each entry in the original tree.
|
|
588
|
+
* @param callback - A function that will be called for each entry in the tree, with parameters
|
|
589
|
+
* representing the key, value, index, and the tree itself. It should return an entry for the new
|
|
590
|
+
* tree.
|
|
591
|
+
* @param [options] - The `options` parameter in the `map` method is of type `RedBlackTreeOptions<MK, MV,
|
|
592
|
+
* MR>`. This parameter allows you to specify additional options or configurations for the Red-Black
|
|
593
|
+
* Tree that will be created during the mapping process. These options could include things like
|
|
594
|
+
* custom comparators
|
|
595
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
596
|
+
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
597
|
+
* (value of `this`) for the callback function. This can be useful when you want to access properties
|
|
598
|
+
* or
|
|
599
|
+
* @returns A new Red-Black Tree is being returned, where each entry has been transformed using the
|
|
600
|
+
* provided callback function.
|
|
601
|
+
*/
|
|
602
|
+
map(callback, options, thisArg) {
|
|
603
|
+
const newTree = new RedBlackTree([], options);
|
|
604
|
+
let index = 0;
|
|
605
|
+
for (const [key, value] of this) {
|
|
606
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
607
|
+
}
|
|
608
|
+
return newTree;
|
|
609
|
+
}
|
|
624
610
|
}
|
|
625
611
|
exports.RedBlackTree = RedBlackTree;
|