doubly-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,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep, EntryCallback, IterationType, RBTNColor, TreeMultiMapNodeNested, TreeMultiMapOptions } from '../../types';
|
|
8
|
+
import type { BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep, EntryCallback, IterationType, RBTNColor, TreeMultiMapNested, TreeMultiMapNodeNested, TreeMultiMapOptions } from '../../types';
|
|
9
9
|
import { IBinaryTree } from '../../interfaces';
|
|
10
10
|
import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
|
|
11
11
|
export declare class TreeMultiMapNode<K = any, V = any, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNodeNested<K, V>> extends RedBlackTreeNode<K, V, NODE> {
|
|
@@ -22,20 +22,8 @@ export declare class TreeMultiMapNode<K = any, V = any, NODE extends TreeMultiMa
|
|
|
22
22
|
* in a Red-Black Tree. It is optional and has a default value of `'BLACK'`.
|
|
23
23
|
*/
|
|
24
24
|
constructor(key: K, value?: V, count?: number, color?: RBTNColor);
|
|
25
|
-
protected _count: number;
|
|
26
|
-
/**
|
|
27
|
-
* The function returns the value of the private variable _count.
|
|
28
|
-
* @returns The count property of the object, which is of type number.
|
|
29
|
-
*/
|
|
30
|
-
get count(): number;
|
|
31
|
-
/**
|
|
32
|
-
* The above function sets the value of the count property.
|
|
33
|
-
* @param {number} value - The value parameter is of type number, which means it can accept any
|
|
34
|
-
* numeric value.
|
|
35
|
-
*/
|
|
36
|
-
set count(value: number);
|
|
37
25
|
}
|
|
38
|
-
export declare class TreeMultiMap<K = any, V = any, R = object, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>> extends RedBlackTree<K, V, R, NODE> implements IBinaryTree<K, V, R, NODE> {
|
|
26
|
+
export declare class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR = object, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>, TREE extends TreeMultiMap<K, V, R, MK, MV, MR, NODE, TREE> = TreeMultiMap<K, V, R, MK, MV, MR, NODE, TreeMultiMapNested<K, V, R, MK, MV, MR, NODE>>> extends RedBlackTree<K, V, R, MK, MV, MR, NODE, TREE> implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE> {
|
|
39
27
|
/**
|
|
40
28
|
* The constructor function initializes a TreeMultiMap object with optional initial data.
|
|
41
29
|
* @param keysNodesEntriesOrRaws - The parameter `keysNodesEntriesOrRaws` is an
|
|
@@ -83,7 +71,20 @@ export declare class TreeMultiMap<K = any, V = any, R = object, NODE extends Tre
|
|
|
83
71
|
* @returns a new instance of the `TreeMultiMap` class, with the provided options merged with the
|
|
84
72
|
* existing `iterationType` property. The returned value is casted as `TREE`.
|
|
85
73
|
*/
|
|
86
|
-
createTree(options?: TreeMultiMapOptions<K, V, R>):
|
|
74
|
+
createTree(options?: TreeMultiMapOptions<K, V, R>): TREE;
|
|
75
|
+
/**
|
|
76
|
+
* The function `keyValueNodeEntryRawToNodeAndValue` takes in a key, value, and count and returns a
|
|
77
|
+
* node based on the input.
|
|
78
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
79
|
+
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
80
|
+
* @param {V} [value] - The `value` parameter is an optional value that represents the value
|
|
81
|
+
* associated with the key in the node. It is used when creating a new node or updating the value of
|
|
82
|
+
* an existing node.
|
|
83
|
+
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
84
|
+
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
85
|
+
* @returns either a NODE object or undefined.
|
|
86
|
+
*/
|
|
87
|
+
protected _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count?: number): [NODE | undefined, V | undefined];
|
|
87
88
|
/**
|
|
88
89
|
* The function checks if the input is an instance of the TreeMultiMapNode class.
|
|
89
90
|
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
@@ -154,36 +155,7 @@ export declare class TreeMultiMap<K = any, V = any, R = object, NODE extends Tre
|
|
|
154
155
|
* The function overrides the clone method to create a deep copy of a tree object.
|
|
155
156
|
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
156
157
|
*/
|
|
157
|
-
clone():
|
|
158
|
-
/**
|
|
159
|
-
* The `map` function in TypeScript overrides the default behavior to create a new TreeMultiMap with
|
|
160
|
-
* modified entries based on a provided callback.
|
|
161
|
-
* @param callback - The `callback` parameter is a function that will be called for each entry in the
|
|
162
|
-
* map. It takes four arguments:
|
|
163
|
-
* @param [options] - The `options` parameter in the `override map` function is of type
|
|
164
|
-
* `TreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional configuration
|
|
165
|
-
* options when creating a new `TreeMultiMap` instance within the `map` function. These options could
|
|
166
|
-
* include things like
|
|
167
|
-
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
168
|
-
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
169
|
-
* (value of `this`) for the callback function when it is called within the `map` function. This
|
|
170
|
-
* @returns A new TreeMultiMap instance is being returned, which is populated with entries generated
|
|
171
|
-
* by the provided callback function.
|
|
172
|
-
*/
|
|
173
|
-
map<MK, MV, MR>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: TreeMultiMapOptions<MK, MV, MR>, thisArg?: any): TreeMultiMap<MK, MV, MR, TreeMultiMapNode<MK, MV, TreeMultiMapNodeNested<MK, MV>>>;
|
|
174
|
-
/**
|
|
175
|
-
* The function `keyValueNodeEntryRawToNodeAndValue` takes in a key, value, and count and returns a
|
|
176
|
-
* node based on the input.
|
|
177
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
178
|
-
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
179
|
-
* @param {V} [value] - The `value` parameter is an optional value that represents the value
|
|
180
|
-
* associated with the key in the node. It is used when creating a new node or updating the value of
|
|
181
|
-
* an existing node.
|
|
182
|
-
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
183
|
-
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
184
|
-
* @returns either a NODE object or undefined.
|
|
185
|
-
*/
|
|
186
|
-
protected _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count?: number): [NODE | undefined, V | undefined];
|
|
158
|
+
clone(): TREE;
|
|
187
159
|
/**
|
|
188
160
|
* Time Complexity: O(1)
|
|
189
161
|
* Space Complexity: O(1)
|
|
@@ -211,4 +183,20 @@ export declare class TreeMultiMap<K = any, V = any, R = object, NODE extends Tre
|
|
|
211
183
|
* superclass, which is of type `NODE`.
|
|
212
184
|
*/
|
|
213
185
|
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE;
|
|
186
|
+
/**
|
|
187
|
+
* The `map` function in TypeScript overrides the default behavior to create a new TreeMultiMap with
|
|
188
|
+
* modified entries based on a provided callback.
|
|
189
|
+
* @param callback - The `callback` parameter is a function that will be called for each entry in the
|
|
190
|
+
* map. It takes four arguments:
|
|
191
|
+
* @param [options] - The `options` parameter in the `override map` function is of type
|
|
192
|
+
* `TreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional configuration
|
|
193
|
+
* options when creating a new `TreeMultiMap` instance within the `map` function. These options could
|
|
194
|
+
* include things like
|
|
195
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
196
|
+
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
197
|
+
* (value of `this`) for the callback function when it is called within the `map` function. This
|
|
198
|
+
* @returns A new TreeMultiMap instance is being returned, which is populated with entries generated
|
|
199
|
+
* by the provided callback function.
|
|
200
|
+
*/
|
|
201
|
+
map(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: TreeMultiMapOptions<MK, MV, MR>, thisArg?: any): TreeMultiMap<MK, MV, MR>;
|
|
214
202
|
}
|
|
@@ -17,24 +17,8 @@ class TreeMultiMapNode extends red_black_tree_1.RedBlackTreeNode {
|
|
|
17
17
|
*/
|
|
18
18
|
constructor(key, value, count = 1, color = 'BLACK') {
|
|
19
19
|
super(key, value, color);
|
|
20
|
-
this._count = 1;
|
|
21
20
|
this.count = count;
|
|
22
21
|
}
|
|
23
|
-
/**
|
|
24
|
-
* The function returns the value of the private variable _count.
|
|
25
|
-
* @returns The count property of the object, which is of type number.
|
|
26
|
-
*/
|
|
27
|
-
get count() {
|
|
28
|
-
return this._count;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* The above function sets the value of the count property.
|
|
32
|
-
* @param {number} value - The value parameter is of type number, which means it can accept any
|
|
33
|
-
* numeric value.
|
|
34
|
-
*/
|
|
35
|
-
set count(value) {
|
|
36
|
-
this._count = value;
|
|
37
|
-
}
|
|
38
22
|
}
|
|
39
23
|
exports.TreeMultiMapNode = TreeMultiMapNode;
|
|
40
24
|
class TreeMultiMap extends red_black_tree_1.RedBlackTree {
|
|
@@ -98,10 +82,44 @@ class TreeMultiMap extends red_black_tree_1.RedBlackTree {
|
|
|
98
82
|
* @returns a new instance of the `TreeMultiMap` class, with the provided options merged with the
|
|
99
83
|
* existing `iterationType` property. The returned value is casted as `TREE`.
|
|
100
84
|
*/
|
|
101
|
-
// @ts-ignore
|
|
102
85
|
createTree(options) {
|
|
103
86
|
return new TreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn }, options));
|
|
104
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* The function `keyValueNodeEntryRawToNodeAndValue` takes in a key, value, and count and returns a
|
|
90
|
+
* node based on the input.
|
|
91
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
92
|
+
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
93
|
+
* @param {V} [value] - The `value` parameter is an optional value that represents the value
|
|
94
|
+
* associated with the key in the node. It is used when creating a new node or updating the value of
|
|
95
|
+
* an existing node.
|
|
96
|
+
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
97
|
+
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
98
|
+
* @returns either a NODE object or undefined.
|
|
99
|
+
*/
|
|
100
|
+
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count = 1) {
|
|
101
|
+
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null)
|
|
102
|
+
return [undefined, undefined];
|
|
103
|
+
if (this.isNode(keyNodeEntryOrRaw))
|
|
104
|
+
return [keyNodeEntryOrRaw, value];
|
|
105
|
+
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
106
|
+
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
107
|
+
if (key === undefined || key === null)
|
|
108
|
+
return [undefined, undefined];
|
|
109
|
+
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
110
|
+
if (this.isKey(key))
|
|
111
|
+
return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
|
|
112
|
+
}
|
|
113
|
+
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
114
|
+
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
115
|
+
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
116
|
+
if (this.isKey(key))
|
|
117
|
+
return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
|
|
118
|
+
}
|
|
119
|
+
if (this.isKey(keyNodeEntryOrRaw))
|
|
120
|
+
return [this.createNode(keyNodeEntryOrRaw, value, 'BLACK', count), value];
|
|
121
|
+
return [undefined, undefined];
|
|
122
|
+
}
|
|
105
123
|
/**
|
|
106
124
|
* The function checks if the input is an instance of the TreeMultiMapNode class.
|
|
107
125
|
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
@@ -170,10 +188,13 @@ class TreeMultiMap extends red_black_tree_1.RedBlackTree {
|
|
|
170
188
|
let originalColor = nodeToDelete.color;
|
|
171
189
|
let replacementNode;
|
|
172
190
|
if (!this.isRealNode(nodeToDelete.left)) {
|
|
173
|
-
|
|
191
|
+
if (nodeToDelete.right !== null)
|
|
192
|
+
replacementNode = nodeToDelete.right;
|
|
174
193
|
if (ignoreCount || nodeToDelete.count <= 1) {
|
|
175
|
-
|
|
176
|
-
|
|
194
|
+
if (nodeToDelete.right !== null) {
|
|
195
|
+
this._transplant(nodeToDelete, nodeToDelete.right);
|
|
196
|
+
this._count -= nodeToDelete.count;
|
|
197
|
+
}
|
|
177
198
|
}
|
|
178
199
|
else {
|
|
179
200
|
nodeToDelete.count--;
|
|
@@ -199,7 +220,8 @@ class TreeMultiMap extends red_black_tree_1.RedBlackTree {
|
|
|
199
220
|
const successor = this.getLeftMost(node => node, nodeToDelete.right);
|
|
200
221
|
if (successor) {
|
|
201
222
|
originalColor = successor.color;
|
|
202
|
-
|
|
223
|
+
if (successor.right !== null)
|
|
224
|
+
replacementNode = successor.right;
|
|
203
225
|
if (successor.parent === nodeToDelete) {
|
|
204
226
|
if (this.isRealNode(replacementNode)) {
|
|
205
227
|
replacementNode.parent = successor;
|
|
@@ -207,8 +229,10 @@ class TreeMultiMap extends red_black_tree_1.RedBlackTree {
|
|
|
207
229
|
}
|
|
208
230
|
else {
|
|
209
231
|
if (ignoreCount || nodeToDelete.count <= 1) {
|
|
210
|
-
|
|
211
|
-
|
|
232
|
+
if (successor.right !== null) {
|
|
233
|
+
this._transplant(successor, successor.right);
|
|
234
|
+
this._count -= nodeToDelete.count;
|
|
235
|
+
}
|
|
212
236
|
}
|
|
213
237
|
else {
|
|
214
238
|
nodeToDelete.count--;
|
|
@@ -319,7 +343,6 @@ class TreeMultiMap extends red_black_tree_1.RedBlackTree {
|
|
|
319
343
|
* The function overrides the clone method to create a deep copy of a tree object.
|
|
320
344
|
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
321
345
|
*/
|
|
322
|
-
// @ts-ignore
|
|
323
346
|
clone() {
|
|
324
347
|
const cloned = this.createTree();
|
|
325
348
|
this.bfs(node => cloned.add(node.key, undefined, node.count));
|
|
@@ -327,65 +350,6 @@ class TreeMultiMap extends red_black_tree_1.RedBlackTree {
|
|
|
327
350
|
cloned._store = this._store;
|
|
328
351
|
return cloned;
|
|
329
352
|
}
|
|
330
|
-
/**
|
|
331
|
-
* The `map` function in TypeScript overrides the default behavior to create a new TreeMultiMap with
|
|
332
|
-
* modified entries based on a provided callback.
|
|
333
|
-
* @param callback - The `callback` parameter is a function that will be called for each entry in the
|
|
334
|
-
* map. It takes four arguments:
|
|
335
|
-
* @param [options] - The `options` parameter in the `override map` function is of type
|
|
336
|
-
* `TreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional configuration
|
|
337
|
-
* options when creating a new `TreeMultiMap` instance within the `map` function. These options could
|
|
338
|
-
* include things like
|
|
339
|
-
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
340
|
-
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
341
|
-
* (value of `this`) for the callback function when it is called within the `map` function. This
|
|
342
|
-
* @returns A new TreeMultiMap instance is being returned, which is populated with entries generated
|
|
343
|
-
* by the provided callback function.
|
|
344
|
-
*/
|
|
345
|
-
// @ts-ignore
|
|
346
|
-
map(callback, options, thisArg) {
|
|
347
|
-
const newTree = new TreeMultiMap([], options);
|
|
348
|
-
let index = 0;
|
|
349
|
-
for (const [key, value] of this) {
|
|
350
|
-
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
351
|
-
}
|
|
352
|
-
return newTree;
|
|
353
|
-
}
|
|
354
|
-
/**
|
|
355
|
-
* The function `keyValueNodeEntryRawToNodeAndValue` takes in a key, value, and count and returns a
|
|
356
|
-
* node based on the input.
|
|
357
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
358
|
-
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
359
|
-
* @param {V} [value] - The `value` parameter is an optional value that represents the value
|
|
360
|
-
* associated with the key in the node. It is used when creating a new node or updating the value of
|
|
361
|
-
* an existing node.
|
|
362
|
-
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
363
|
-
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
364
|
-
* @returns either a NODE object or undefined.
|
|
365
|
-
*/
|
|
366
|
-
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count = 1) {
|
|
367
|
-
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null)
|
|
368
|
-
return [undefined, undefined];
|
|
369
|
-
if (this.isNode(keyNodeEntryOrRaw))
|
|
370
|
-
return [keyNodeEntryOrRaw, value];
|
|
371
|
-
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
372
|
-
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
373
|
-
if (key === undefined || key === null)
|
|
374
|
-
return [undefined, undefined];
|
|
375
|
-
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
376
|
-
if (this.isKey(key))
|
|
377
|
-
return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
|
|
378
|
-
}
|
|
379
|
-
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
380
|
-
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
381
|
-
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
382
|
-
if (this.isKey(key))
|
|
383
|
-
return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
|
|
384
|
-
}
|
|
385
|
-
if (this.isKey(keyNodeEntryOrRaw))
|
|
386
|
-
return [this.createNode(keyNodeEntryOrRaw, value, 'BLACK', count), value];
|
|
387
|
-
return [undefined, undefined];
|
|
388
|
-
}
|
|
389
353
|
/**
|
|
390
354
|
* Time Complexity: O(1)
|
|
391
355
|
* Space Complexity: O(1)
|
|
@@ -438,5 +402,28 @@ class TreeMultiMap extends red_black_tree_1.RedBlackTree {
|
|
|
438
402
|
newNode.count = oldNode.count + newNode.count;
|
|
439
403
|
return super._replaceNode(oldNode, newNode);
|
|
440
404
|
}
|
|
405
|
+
/**
|
|
406
|
+
* The `map` function in TypeScript overrides the default behavior to create a new TreeMultiMap with
|
|
407
|
+
* modified entries based on a provided callback.
|
|
408
|
+
* @param callback - The `callback` parameter is a function that will be called for each entry in the
|
|
409
|
+
* map. It takes four arguments:
|
|
410
|
+
* @param [options] - The `options` parameter in the `override map` function is of type
|
|
411
|
+
* `TreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional configuration
|
|
412
|
+
* options when creating a new `TreeMultiMap` instance within the `map` function. These options could
|
|
413
|
+
* include things like
|
|
414
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
415
|
+
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
416
|
+
* (value of `this`) for the callback function when it is called within the `map` function. This
|
|
417
|
+
* @returns A new TreeMultiMap instance is being returned, which is populated with entries generated
|
|
418
|
+
* by the provided callback function.
|
|
419
|
+
*/
|
|
420
|
+
map(callback, options, thisArg) {
|
|
421
|
+
const newTree = new TreeMultiMap([], options);
|
|
422
|
+
let index = 0;
|
|
423
|
+
for (const [key, value] of this) {
|
|
424
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
425
|
+
}
|
|
426
|
+
return newTree;
|
|
427
|
+
}
|
|
441
428
|
}
|
|
442
429
|
exports.TreeMultiMap = TreeMultiMap;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import type { BinaryTreeDeleteResult, BinaryTreeNodeNested, BTNRep, NodePredicate } from '../types';
|
|
3
|
-
export interface IBinaryTree<K = any, V = any, R = object, NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNodeNested<K, V>> {
|
|
1
|
+
import { BinaryTree, BinaryTreeNode } from '../data-structures';
|
|
2
|
+
import type { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BTNRep, NodePredicate } from '../types';
|
|
3
|
+
export interface IBinaryTree<K = any, V = any, R = object, MK = any, MV = any, MR = object, NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNodeNested<K, V>, TREE extends BinaryTree<K, V, R, MK, MV, MR, NODE, TREE> = BinaryTreeNested<K, V, R, MK, MV, MR, NODE>> {
|
|
4
4
|
createNode(key: K, value?: NODE['value']): NODE;
|
|
5
|
+
createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): TREE;
|
|
5
6
|
add(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, NODE>, value?: V, count?: number): boolean;
|
|
6
7
|
addMany(nodes: Iterable<BTNRep<K, V, NODE>>, values?: Iterable<V | undefined>): boolean[];
|
|
7
8
|
delete(predicate: R | BTNRep<K, V, NODE> | NodePredicate<NODE>): BinaryTreeDeleteResult<NODE>[];
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { AVLTreeMultiMapNode } from '../../../data-structures';
|
|
1
|
+
import { AVLTreeMultiMap, AVLTreeMultiMapNode } from '../../../data-structures';
|
|
2
2
|
import type { AVLTreeOptions } from './avl-tree';
|
|
3
|
-
export type AVLTreeMultiMapNodeNested<K, V> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V,
|
|
3
|
+
export type AVLTreeMultiMapNodeNested<K, V> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, any>>>;
|
|
4
|
+
export type AVLTreeMultiMapNested<K, V, R, MK, MV, MR, NODE extends AVLTreeMultiMapNode<K, V, NODE>> = AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, any>>>;
|
|
4
5
|
export type AVLTreeMultiMapOptions<K, V, R> = AVLTreeOptions<K, V, R> & {};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { AVLTreeNode } from '../../../data-structures';
|
|
1
|
+
import { AVLTree, AVLTreeNode } from '../../../data-structures';
|
|
2
2
|
import { BSTOptions } from './bst';
|
|
3
|
-
export type AVLTreeNodeNested<K, V> = AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V,
|
|
3
|
+
export type AVLTreeNodeNested<K, V> = AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, any>>>;
|
|
4
|
+
export type AVLTreeNested<K, V, R, MK, MV, MR, NODE extends AVLTreeNode<K, V, NODE>> = AVLTree<K, V, R, MK, MV, MR, NODE, AVLTree<K, V, R, MK, MV, MR, NODE, AVLTree<K, V, R, MK, MV, MR, NODE, any>>>;
|
|
4
5
|
export type AVLTreeOptions<K, V, R> = BSTOptions<K, V, R> & {};
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { BinaryTreeNode } from '../../../data-structures';
|
|
1
|
+
import { BinaryTree, BinaryTreeNode } from '../../../data-structures';
|
|
2
2
|
import { IterationType, OptValue } from '../../common';
|
|
3
3
|
import { DFSOperation } from '../../../common';
|
|
4
|
-
export type BinaryTreeNodeNested<K, V> = BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V,
|
|
4
|
+
export type BinaryTreeNodeNested<K, V> = BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, any>>>;
|
|
5
|
+
export type BinaryTreeNested<K, V, R, MK, MV, MR, NODE extends BinaryTreeNode<K, V, NODE>> = BinaryTree<K, V, R, MK, MV, MR, NODE, BinaryTree<K, V, R, MK, MV, MR, NODE, BinaryTree<K, V, R, MK, MV, MR, NODE, any>>>;
|
|
5
6
|
export type ToEntryFn<K, V, R> = (rawElement: R) => BTNEntry<K, V>;
|
|
6
7
|
export type BinaryTreeOptions<K, V, R> = {
|
|
7
8
|
iterationType?: IterationType;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { BSTNode } from '../../../data-structures';
|
|
1
|
+
import { BST, BSTNode } from '../../../data-structures';
|
|
2
2
|
import type { BinaryTreeOptions } from './binary-tree';
|
|
3
3
|
import { Comparable } from '../../utils';
|
|
4
|
-
export type BSTNodeNested<K, V> = BSTNode<K, V, BSTNode<K, V, BSTNode<K, V,
|
|
4
|
+
export type BSTNodeNested<K, V> = BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, any>>>;
|
|
5
|
+
export type BSTNested<K, V, R, MK, MV, MR, NODE extends BSTNode<K, V, NODE>> = BST<K, V, R, MK, MV, MR, NODE, BST<K, V, R, MK, MV, MR, NODE, BST<K, V, R, MK, MV, MR, NODE, any>>>;
|
|
5
6
|
export type BSTOptions<K, V, R> = BinaryTreeOptions<K, V, R> & {
|
|
6
7
|
specifyComparable?: (key: K) => Comparable;
|
|
7
8
|
isReverse?: boolean;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { RedBlackTreeNode } from '../../../data-structures';
|
|
2
|
-
import type { BSTOptions } from
|
|
1
|
+
import { RedBlackTree, RedBlackTreeNode } from '../../../data-structures';
|
|
2
|
+
import type { BSTOptions } from "./bst";
|
|
3
3
|
export type RBTNColor = 'RED' | 'BLACK';
|
|
4
|
-
export type RedBlackTreeNodeNested<K, V> = RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V,
|
|
5
|
-
export type
|
|
4
|
+
export type RedBlackTreeNodeNested<K, V> = RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, any>>>;
|
|
5
|
+
export type RedBlackTreeNested<K, V, R, MK, MV, MR, NODE extends RedBlackTreeNode<K, V, NODE>> = RedBlackTree<K, V, R, MK, MV, MR, NODE, RedBlackTree<K, V, R, MK, MV, MR, NODE, RedBlackTree<K, V, R, MK, MV, MR, NODE, any>>>;
|
|
6
|
+
export type RedBlackTreeOptions<K, V, R> = BSTOptions<K, V, R> & {};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { TreeMultiMapNode } from '../../../data-structures';
|
|
1
|
+
import { TreeMultiMap, TreeMultiMapNode } from '../../../data-structures';
|
|
2
2
|
import type { RedBlackTreeOptions } from './rb-tree';
|
|
3
|
-
export type TreeMultiMapNodeNested<K, V> = TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V,
|
|
3
|
+
export type TreeMultiMapNodeNested<K, V> = TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, any>>>;
|
|
4
|
+
export type TreeMultiMapNested<K, V, R, MK, MV, MR, NODE extends TreeMultiMapNode<K, V, NODE>> = TreeMultiMap<K, V, R, MK, MV, MR, NODE, TreeMultiMap<K, V, R, MK, MV, MR, NODE, TreeMultiMap<K, V, R, MK, MV, MR, NODE, any>>>;
|
|
4
5
|
export type TreeMultiMapOptions<K, V, R> = RedBlackTreeOptions<K, V, R> & {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "doubly-linked-list-typed",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.54.0",
|
|
4
4
|
"description": "Doubly Linked List. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -64,6 +64,6 @@
|
|
|
64
64
|
"typescript": "^4.9.5"
|
|
65
65
|
},
|
|
66
66
|
"dependencies": {
|
|
67
|
-
"data-structure-typed": "^1.
|
|
67
|
+
"data-structure-typed": "^1.54.0"
|
|
68
68
|
}
|
|
69
69
|
}
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type {
|
|
9
|
+
AVLTreeMultiMapNested,
|
|
9
10
|
AVLTreeMultiMapNodeNested,
|
|
10
11
|
AVLTreeMultiMapOptions,
|
|
11
12
|
BinaryTreeDeleteResult,
|
|
@@ -36,25 +37,6 @@ export class AVLTreeMultiMapNode<
|
|
|
36
37
|
super(key, value);
|
|
37
38
|
this.count = count;
|
|
38
39
|
}
|
|
39
|
-
|
|
40
|
-
protected _count: number = 1;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* The function returns the value of the protected variable _count.
|
|
44
|
-
* @returns The count property of the object, which is of type number.
|
|
45
|
-
*/
|
|
46
|
-
get count(): number {
|
|
47
|
-
return this._count;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* The above function sets the value of the count property.
|
|
52
|
-
* @param {number} value - The value parameter is of type number, which means it can accept any
|
|
53
|
-
* numeric value.
|
|
54
|
-
*/
|
|
55
|
-
set count(value: number) {
|
|
56
|
-
this._count = value;
|
|
57
|
-
}
|
|
58
40
|
}
|
|
59
41
|
|
|
60
42
|
/**
|
|
@@ -64,10 +46,23 @@ export class AVLTreeMultiMap<
|
|
|
64
46
|
K = any,
|
|
65
47
|
V = any,
|
|
66
48
|
R = object,
|
|
67
|
-
|
|
49
|
+
MK = any,
|
|
50
|
+
MV = any,
|
|
51
|
+
MR = object,
|
|
52
|
+
NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>,
|
|
53
|
+
TREE extends AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, TREE> = AVLTreeMultiMap<
|
|
54
|
+
K,
|
|
55
|
+
V,
|
|
56
|
+
R,
|
|
57
|
+
MK,
|
|
58
|
+
MV,
|
|
59
|
+
MR,
|
|
60
|
+
NODE,
|
|
61
|
+
AVLTreeMultiMapNested<K, V, R, MK, MV, MR, NODE>
|
|
62
|
+
>
|
|
68
63
|
>
|
|
69
|
-
extends AVLTree<K, V, R, NODE>
|
|
70
|
-
implements IBinaryTree<K, V, R, NODE>
|
|
64
|
+
extends AVLTree<K, V, R, MK, MV, MR, NODE, TREE>
|
|
65
|
+
implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE>
|
|
71
66
|
{
|
|
72
67
|
/**
|
|
73
68
|
* The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
|
|
@@ -132,16 +127,15 @@ export class AVLTreeMultiMap<
|
|
|
132
127
|
* @returns a new instance of the AVLTreeMultiMap class, with the specified options, as a TREE
|
|
133
128
|
* object.
|
|
134
129
|
*/
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
return new AVLTreeMultiMap<K, V, R, NODE>([], {
|
|
130
|
+
override createTree(options?: AVLTreeMultiMapOptions<K, V, R>): TREE {
|
|
131
|
+
return new AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, TREE>([], {
|
|
138
132
|
iterationType: this.iterationType,
|
|
139
133
|
isMapMode: this._isMapMode,
|
|
140
134
|
specifyComparable: this._specifyComparable,
|
|
141
135
|
toEntryFn: this._toEntryFn,
|
|
142
136
|
isReverse: this._isReverse,
|
|
143
137
|
...options
|
|
144
|
-
});
|
|
138
|
+
}) as TREE;
|
|
145
139
|
}
|
|
146
140
|
|
|
147
141
|
/**
|
|
@@ -220,7 +214,7 @@ export class AVLTreeMultiMap<
|
|
|
220
214
|
} else {
|
|
221
215
|
if (!curr.left) {
|
|
222
216
|
if (!parent) {
|
|
223
|
-
if (curr.right !== undefined) this._setRoot(curr.right);
|
|
217
|
+
if (curr.right !== undefined && curr.right !== null) this._setRoot(curr.right);
|
|
224
218
|
} else {
|
|
225
219
|
const { familyPosition: fp } = curr;
|
|
226
220
|
if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
|
|
@@ -330,8 +324,7 @@ export class AVLTreeMultiMap<
|
|
|
330
324
|
* The function overrides the clone method to create a deep copy of a tree object.
|
|
331
325
|
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
332
326
|
*/
|
|
333
|
-
|
|
334
|
-
override clone() {
|
|
327
|
+
override clone(): TREE {
|
|
335
328
|
const cloned = this.createTree();
|
|
336
329
|
if (this._isMapMode) this.bfs(node => cloned.add(node.key, undefined, node.count));
|
|
337
330
|
else this.bfs(node => cloned.add(node.key, node.value, node.count));
|
|
@@ -357,12 +350,11 @@ export class AVLTreeMultiMap<
|
|
|
357
350
|
* `callback` function along with the index and the original tree itself. The transformed entries are
|
|
358
351
|
* then added to the new `AVLTreeMultiMap` instance, which is returned at the end.
|
|
359
352
|
*/
|
|
360
|
-
// @ts-ignore
|
|
361
353
|
override map<MK, MV, MR>(
|
|
362
354
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
363
355
|
options?: AVLTreeMultiMapOptions<MK, MV, MR>,
|
|
364
356
|
thisArg?: any
|
|
365
|
-
) {
|
|
357
|
+
): AVLTreeMultiMap<MK, MV, MR> {
|
|
366
358
|
const newTree = new AVLTreeMultiMap<MK, MV, MR>([], options);
|
|
367
359
|
let index = 0;
|
|
368
360
|
for (const [key, value] of this) {
|