bst-typed 1.52.9 → 1.53.1
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 +21 -21
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +64 -47
- package/dist/data-structures/binary-tree/avl-tree.d.ts +20 -20
- package/dist/data-structures/binary-tree/avl-tree.js +29 -27
- package/dist/data-structures/binary-tree/binary-tree.d.ts +186 -144
- package/dist/data-structures/binary-tree/binary-tree.js +376 -265
- package/dist/data-structures/binary-tree/bst.d.ts +56 -56
- package/dist/data-structures/binary-tree/bst.js +108 -78
- package/dist/data-structures/binary-tree/rb-tree.d.ts +13 -13
- package/dist/data-structures/binary-tree/rb-tree.js +42 -36
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +21 -21
- package/dist/data-structures/binary-tree/tree-multi-map.js +59 -49
- package/dist/data-structures/trie/trie.js +3 -3
- package/dist/interfaces/binary-tree.d.ts +5 -5
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +13 -13
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -3
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +60 -54
- package/src/data-structures/binary-tree/avl-tree.ts +32 -35
- package/src/data-structures/binary-tree/binary-tree.ts +440 -360
- package/src/data-structures/binary-tree/bst.ts +144 -113
- package/src/data-structures/binary-tree/rb-tree.ts +44 -43
- package/src/data-structures/binary-tree/tree-multi-map.ts +57 -61
- package/src/data-structures/trie/trie.ts +3 -3
- package/src/interfaces/binary-tree.ts +6 -6
- package/src/types/data-structures/binary-tree/binary-tree.ts +13 -14
- package/src/types/data-structures/binary-tree/bst.ts +3 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bst-typed",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.53.1",
|
|
4
4
|
"description": "BST (Binary Search Tree). Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -144,6 +144,6 @@
|
|
|
144
144
|
"typescript": "^4.9.5"
|
|
145
145
|
},
|
|
146
146
|
"dependencies": {
|
|
147
|
-
"data-structure-typed": "^1.
|
|
147
|
+
"data-structure-typed": "^1.53.1"
|
|
148
148
|
}
|
|
149
149
|
}
|
|
@@ -10,10 +10,9 @@ import type {
|
|
|
10
10
|
AVLTreeMultiMapNodeNested,
|
|
11
11
|
AVLTreeMultiMapOptions,
|
|
12
12
|
BinaryTreeDeleteResult,
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
IterationType
|
|
16
|
-
BTNEntry
|
|
13
|
+
BSTNOptKeyOrNode,
|
|
14
|
+
BTNRep,
|
|
15
|
+
IterationType
|
|
17
16
|
} from '../../types';
|
|
18
17
|
import { IBinaryTree } from '../../interfaces';
|
|
19
18
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
@@ -64,7 +63,7 @@ export class AVLTreeMultiMapNode<
|
|
|
64
63
|
export class AVLTreeMultiMap<
|
|
65
64
|
K = any,
|
|
66
65
|
V = any,
|
|
67
|
-
R =
|
|
66
|
+
R = object,
|
|
68
67
|
NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>,
|
|
69
68
|
TREE extends AVLTreeMultiMap<K, V, R, NODE, TREE> = AVLTreeMultiMap<
|
|
70
69
|
K,
|
|
@@ -79,18 +78,18 @@ export class AVLTreeMultiMap<
|
|
|
79
78
|
{
|
|
80
79
|
/**
|
|
81
80
|
* The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
|
|
82
|
-
* @param
|
|
81
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
83
82
|
* iterable object that can contain either keys, nodes, entries, or raw elements.
|
|
84
83
|
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
85
84
|
* behavior of the AVLTreeMultiMap. It can include properties such as `compareKeys` and
|
|
86
85
|
* `compareValues` functions to define custom comparison logic for keys and values, respectively.
|
|
87
86
|
*/
|
|
88
87
|
constructor(
|
|
89
|
-
|
|
88
|
+
keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>> = [],
|
|
90
89
|
options?: AVLTreeMultiMapOptions<K, V, R>
|
|
91
90
|
) {
|
|
92
91
|
super([], options);
|
|
93
|
-
if (
|
|
92
|
+
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
94
93
|
}
|
|
95
94
|
|
|
96
95
|
protected _count = 0;
|
|
@@ -130,7 +129,7 @@ export class AVLTreeMultiMap<
|
|
|
130
129
|
* @returns a new instance of the AVLTreeMultiMapNode class, casted as NODE.
|
|
131
130
|
*/
|
|
132
131
|
override createNode(key: K, value?: V, count?: number): NODE {
|
|
133
|
-
return new AVLTreeMultiMapNode(key, value, count) as NODE;
|
|
132
|
+
return new AVLTreeMultiMapNode(key, this._isMapMode ? undefined : value, count) as NODE;
|
|
134
133
|
}
|
|
135
134
|
|
|
136
135
|
/**
|
|
@@ -143,6 +142,7 @@ export class AVLTreeMultiMap<
|
|
|
143
142
|
override createTree(options?: AVLTreeMultiMapOptions<K, V, R>): TREE {
|
|
144
143
|
return new AVLTreeMultiMap<K, V, R, NODE, TREE>([], {
|
|
145
144
|
iterationType: this.iterationType,
|
|
145
|
+
isMapMode: this._isMapMode,
|
|
146
146
|
comparator: this._comparator,
|
|
147
147
|
toEntryFn: this._toEntryFn,
|
|
148
148
|
...options
|
|
@@ -151,20 +151,20 @@ export class AVLTreeMultiMap<
|
|
|
151
151
|
|
|
152
152
|
/**
|
|
153
153
|
* The function checks if the input is an instance of AVLTreeMultiMapNode.
|
|
154
|
-
* @param {
|
|
155
|
-
* `
|
|
156
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
154
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
155
|
+
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
156
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
|
|
157
157
|
* an instance of the `AVLTreeMultiMapNode` class.
|
|
158
158
|
*/
|
|
159
|
-
override isNode(
|
|
160
|
-
return
|
|
159
|
+
override isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE {
|
|
160
|
+
return keyNodeEntryOrRaw instanceof AVLTreeMultiMapNode;
|
|
161
161
|
}
|
|
162
162
|
|
|
163
163
|
/**
|
|
164
|
-
* The function `
|
|
164
|
+
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
|
|
165
165
|
* a node object.
|
|
166
|
-
* @param {
|
|
167
|
-
* `
|
|
166
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
167
|
+
* `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
168
168
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
169
169
|
* `override` function. It represents the value associated with the key in the data structure. If no
|
|
170
170
|
* value is provided, it will default to `undefined`.
|
|
@@ -172,28 +172,33 @@ export class AVLTreeMultiMap<
|
|
|
172
172
|
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
173
173
|
* @returns either a NODE object or undefined.
|
|
174
174
|
*/
|
|
175
|
-
override
|
|
176
|
-
|
|
175
|
+
override keyValueNodeEntryRawToNodeAndValue(
|
|
176
|
+
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
|
|
177
177
|
value?: V,
|
|
178
178
|
count = 1
|
|
179
|
-
): NODE | undefined {
|
|
180
|
-
if (
|
|
181
|
-
if (this.isNode(
|
|
182
|
-
|
|
183
|
-
if (this.isEntry(
|
|
184
|
-
const [key, entryValue] =
|
|
185
|
-
if (key === undefined || key === null) return;
|
|
186
|
-
|
|
179
|
+
): [NODE | undefined, V | undefined] {
|
|
180
|
+
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) return [undefined, undefined];
|
|
181
|
+
if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
|
|
182
|
+
|
|
183
|
+
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
184
|
+
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
185
|
+
if (key === undefined || key === null) return [undefined, undefined];
|
|
186
|
+
const finalValue = value ?? entryValue;
|
|
187
|
+
return [this.createNode(key, finalValue, count), finalValue];
|
|
187
188
|
}
|
|
188
189
|
|
|
189
|
-
if (this.
|
|
190
|
-
const [key, entryValue] = this._toEntryFn(keyOrNodeOrEntryOrRaw as R);
|
|
191
|
-
if (this.isKey(key)) return this.createNode(key, value ?? entryValue, count);
|
|
192
|
-
}
|
|
190
|
+
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, count), value];
|
|
193
191
|
|
|
194
|
-
if (this.
|
|
192
|
+
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
193
|
+
if (this._toEntryFn) {
|
|
194
|
+
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw as R);
|
|
195
|
+
const finalValue = value ?? entryValue;
|
|
196
|
+
if (this.isKey(key)) return [this.createNode(key, finalValue, count), finalValue];
|
|
197
|
+
}
|
|
198
|
+
return [undefined, undefined];
|
|
199
|
+
}
|
|
195
200
|
|
|
196
|
-
return;
|
|
201
|
+
return [undefined, undefined];
|
|
197
202
|
}
|
|
198
203
|
|
|
199
204
|
/**
|
|
@@ -202,9 +207,9 @@ export class AVLTreeMultiMap<
|
|
|
202
207
|
*
|
|
203
208
|
* The function overrides the add method of a TypeScript class to add a new node to a data structure
|
|
204
209
|
* and update the count.
|
|
205
|
-
* @param {
|
|
206
|
-
* `
|
|
207
|
-
* can also accept a value of type `
|
|
210
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
211
|
+
* `keyNodeEntryOrRaw` parameter can accept a value of type `R`, which can be any type. It
|
|
212
|
+
* can also accept a value of type `BTNRep<K, V, NODE>`, which represents a key, node,
|
|
208
213
|
* entry, or raw element
|
|
209
214
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
210
215
|
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
@@ -213,12 +218,12 @@ export class AVLTreeMultiMap<
|
|
|
213
218
|
* be added once. However, you can specify a different value for `count` if you want to add
|
|
214
219
|
* @returns a boolean value.
|
|
215
220
|
*/
|
|
216
|
-
override add(
|
|
217
|
-
const newNode = this.
|
|
221
|
+
override add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count = 1): boolean {
|
|
222
|
+
const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
|
|
218
223
|
if (newNode === undefined) return false;
|
|
219
224
|
|
|
220
225
|
const orgNodeCount = newNode?.count || 0;
|
|
221
|
-
const inserted = super.add(newNode);
|
|
226
|
+
const inserted = super.add(newNode, newValue);
|
|
222
227
|
if (inserted) {
|
|
223
228
|
this._count += orgNodeCount;
|
|
224
229
|
}
|
|
@@ -231,7 +236,7 @@ export class AVLTreeMultiMap<
|
|
|
231
236
|
*
|
|
232
237
|
* The function overrides the delete method in a binary tree data structure, handling deletion of
|
|
233
238
|
* nodes and maintaining balance in the tree.
|
|
234
|
-
* @param {
|
|
239
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `predicate`
|
|
235
240
|
* parameter in the `delete` method is used to specify the condition for deleting a node from the
|
|
236
241
|
* binary tree. It can be a key, node, or entry that determines which
|
|
237
242
|
* node(s) should be deleted.
|
|
@@ -244,14 +249,11 @@ export class AVLTreeMultiMap<
|
|
|
244
249
|
* method returns an array of `BinaryTreeDeleteResult` objects, each containing information about the
|
|
245
250
|
* deleted node and whether balancing is needed in the tree.
|
|
246
251
|
*/
|
|
247
|
-
override delete(
|
|
248
|
-
keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
249
|
-
ignoreCount = false
|
|
250
|
-
): BinaryTreeDeleteResult<NODE>[] {
|
|
252
|
+
override delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, ignoreCount = false): BinaryTreeDeleteResult<NODE>[] {
|
|
251
253
|
const deletedResult: BinaryTreeDeleteResult<NODE>[] = [];
|
|
252
254
|
if (!this.root) return deletedResult;
|
|
253
255
|
|
|
254
|
-
const curr: NODE | undefined = this.getNode(
|
|
256
|
+
const curr: NODE | undefined = this.getNode(keyNodeEntryOrRaw) ?? undefined;
|
|
255
257
|
if (!curr) return deletedResult;
|
|
256
258
|
|
|
257
259
|
const parent: NODE | undefined = curr?.parent ? curr.parent : undefined;
|
|
@@ -339,7 +341,8 @@ export class AVLTreeMultiMap<
|
|
|
339
341
|
if (l > r) return;
|
|
340
342
|
const m = l + Math.floor((r - l) / 2);
|
|
341
343
|
const midNode = sorted[m];
|
|
342
|
-
this.add(midNode.key,
|
|
344
|
+
if (this._isMapMode) this.add(midNode.key, undefined, midNode.count);
|
|
345
|
+
else this.add(midNode.key, midNode.value, midNode.count);
|
|
343
346
|
buildBalanceBST(l, m - 1);
|
|
344
347
|
buildBalanceBST(m + 1, r);
|
|
345
348
|
};
|
|
@@ -355,7 +358,8 @@ export class AVLTreeMultiMap<
|
|
|
355
358
|
if (l <= r) {
|
|
356
359
|
const m = l + Math.floor((r - l) / 2);
|
|
357
360
|
const midNode = sorted[m];
|
|
358
|
-
this.add(midNode.key,
|
|
361
|
+
if (this._isMapMode) this.add(midNode.key, undefined, midNode.count);
|
|
362
|
+
else this.add(midNode.key, midNode.value, midNode.count);
|
|
359
363
|
stack.push([m + 1, r]);
|
|
360
364
|
stack.push([l, m - 1]);
|
|
361
365
|
}
|
|
@@ -374,7 +378,9 @@ export class AVLTreeMultiMap<
|
|
|
374
378
|
*/
|
|
375
379
|
override clone(): TREE {
|
|
376
380
|
const cloned = this.createTree();
|
|
377
|
-
this.bfs(node => cloned.add(node.key,
|
|
381
|
+
if (this._isMapMode) this.bfs(node => cloned.add(node.key, undefined, node.count));
|
|
382
|
+
else this.bfs(node => cloned.add(node.key, node.value, node.count));
|
|
383
|
+
if (this._isMapMode) cloned._store = this._store;
|
|
378
384
|
return cloned;
|
|
379
385
|
}
|
|
380
386
|
|
|
@@ -384,16 +390,16 @@ export class AVLTreeMultiMap<
|
|
|
384
390
|
*
|
|
385
391
|
* The `_swapProperties` function swaps the properties (key, value, count, height) between two nodes
|
|
386
392
|
* in a binary search tree.
|
|
387
|
-
* @param {R |
|
|
393
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
|
|
388
394
|
* that will be swapped with the `destNode`.
|
|
389
|
-
* @param {R |
|
|
395
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
|
|
390
396
|
* node where the properties will be swapped with the source node.
|
|
391
397
|
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
392
398
|
* If either `srcNode` or `destNode` is undefined, it returns `undefined`.
|
|
393
399
|
*/
|
|
394
400
|
protected override _swapProperties(
|
|
395
|
-
srcNode: R |
|
|
396
|
-
destNode: R |
|
|
401
|
+
srcNode: R | BSTNOptKeyOrNode<K, NODE>,
|
|
402
|
+
destNode: R | BSTNOptKeyOrNode<K, NODE>
|
|
397
403
|
): NODE | undefined {
|
|
398
404
|
srcNode = this.ensureNode(srcNode);
|
|
399
405
|
destNode = this.ensureNode(destNode);
|
|
@@ -404,12 +410,12 @@ export class AVLTreeMultiMap<
|
|
|
404
410
|
tempNode.height = height;
|
|
405
411
|
|
|
406
412
|
destNode.key = srcNode.key;
|
|
407
|
-
destNode.value = srcNode.value;
|
|
413
|
+
if (!this._isMapMode) destNode.value = srcNode.value;
|
|
408
414
|
destNode.count = srcNode.count;
|
|
409
415
|
destNode.height = srcNode.height;
|
|
410
416
|
|
|
411
417
|
srcNode.key = tempNode.key;
|
|
412
|
-
srcNode.value = tempNode.value;
|
|
418
|
+
if (!this._isMapMode) srcNode.value = tempNode.value;
|
|
413
419
|
srcNode.count = tempNode.count;
|
|
414
420
|
srcNode.height = tempNode.height;
|
|
415
421
|
}
|
|
@@ -11,9 +11,8 @@ import type {
|
|
|
11
11
|
AVLTreeNodeNested,
|
|
12
12
|
AVLTreeOptions,
|
|
13
13
|
BinaryTreeDeleteResult,
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
BTNEntry
|
|
14
|
+
BSTNOptKeyOrNode,
|
|
15
|
+
BTNRep
|
|
17
16
|
} from '../../types';
|
|
18
17
|
import { IBinaryTree } from '../../interfaces';
|
|
19
18
|
|
|
@@ -67,7 +66,7 @@ export class AVLTreeNode<
|
|
|
67
66
|
export class AVLTree<
|
|
68
67
|
K = any,
|
|
69
68
|
V = any,
|
|
70
|
-
R =
|
|
69
|
+
R = object,
|
|
71
70
|
NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>,
|
|
72
71
|
TREE extends AVLTree<K, V, R, NODE, TREE> = AVLTree<K, V, R, NODE, AVLTreeNested<K, V, R, NODE>>
|
|
73
72
|
>
|
|
@@ -77,7 +76,7 @@ export class AVLTree<
|
|
|
77
76
|
/**
|
|
78
77
|
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
|
|
79
78
|
* entries, or raw elements.
|
|
80
|
-
* @param
|
|
79
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
81
80
|
* iterable object that can contain either keys, nodes, entries, or raw elements. These elements will
|
|
82
81
|
* be used to initialize the AVLTree.
|
|
83
82
|
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
@@ -85,12 +84,9 @@ export class AVLTree<
|
|
|
85
84
|
* keys), `allowDuplicates` (a boolean indicating whether duplicate keys are allowed), and
|
|
86
85
|
* `nodeBuilder` (
|
|
87
86
|
*/
|
|
88
|
-
constructor(
|
|
89
|
-
keysOrNodesOrEntriesOrRaws: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
90
|
-
options?: AVLTreeOptions<K, V, R>
|
|
91
|
-
) {
|
|
87
|
+
constructor(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>> = [], options?: AVLTreeOptions<K, V, R>) {
|
|
92
88
|
super([], options);
|
|
93
|
-
if (
|
|
89
|
+
if (keysNodesEntriesOrRaws) super.addMany(keysNodesEntriesOrRaws);
|
|
94
90
|
}
|
|
95
91
|
|
|
96
92
|
/**
|
|
@@ -103,7 +99,7 @@ export class AVLTree<
|
|
|
103
99
|
* type NODE.
|
|
104
100
|
*/
|
|
105
101
|
override createNode(key: K, value?: V): NODE {
|
|
106
|
-
return new AVLTreeNode<K, V, NODE>(key, value) as NODE;
|
|
102
|
+
return new AVLTreeNode<K, V, NODE>(key, this._isMapMode ? undefined : value) as NODE;
|
|
107
103
|
}
|
|
108
104
|
|
|
109
105
|
/**
|
|
@@ -116,6 +112,7 @@ export class AVLTree<
|
|
|
116
112
|
override createTree(options?: AVLTreeOptions<K, V, R>): TREE {
|
|
117
113
|
return new AVLTree<K, V, R, NODE, TREE>([], {
|
|
118
114
|
iterationType: this.iterationType,
|
|
115
|
+
isMapMode: this._isMapMode,
|
|
119
116
|
comparator: this._comparator,
|
|
120
117
|
toEntryFn: this._toEntryFn,
|
|
121
118
|
...options
|
|
@@ -124,13 +121,13 @@ export class AVLTree<
|
|
|
124
121
|
|
|
125
122
|
/**
|
|
126
123
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
127
|
-
* @param {
|
|
128
|
-
* `
|
|
129
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
124
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
125
|
+
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
126
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
|
|
130
127
|
* an instance of the `AVLTreeNode` class.
|
|
131
128
|
*/
|
|
132
|
-
override isNode(
|
|
133
|
-
return
|
|
129
|
+
override isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE {
|
|
130
|
+
return keyNodeEntryOrRaw instanceof AVLTreeNode;
|
|
134
131
|
}
|
|
135
132
|
|
|
136
133
|
/**
|
|
@@ -139,17 +136,17 @@ export class AVLTree<
|
|
|
139
136
|
*
|
|
140
137
|
* The function overrides the add method of a class and inserts a key-value pair into a data
|
|
141
138
|
* structure, then balances the path.
|
|
142
|
-
* @param {
|
|
143
|
-
* `
|
|
139
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
140
|
+
* `keyNodeEntryOrRaw` can accept values of type `R`, `BTNRep<K, V, NODE>`, or
|
|
144
141
|
* `RawElement`.
|
|
145
142
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
146
143
|
* the key or node being added to the data structure.
|
|
147
144
|
* @returns The method is returning a boolean value.
|
|
148
145
|
*/
|
|
149
|
-
override add(
|
|
150
|
-
if (
|
|
151
|
-
const inserted = super.add(
|
|
152
|
-
if (inserted) this._balancePath(
|
|
146
|
+
override add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): boolean {
|
|
147
|
+
if (keyNodeEntryOrRaw === null) return false;
|
|
148
|
+
const inserted = super.add(keyNodeEntryOrRaw, value);
|
|
149
|
+
if (inserted) this._balancePath(keyNodeEntryOrRaw);
|
|
153
150
|
return inserted;
|
|
154
151
|
}
|
|
155
152
|
|
|
@@ -159,15 +156,15 @@ export class AVLTree<
|
|
|
159
156
|
*
|
|
160
157
|
* The function overrides the delete method in a TypeScript class, performs deletion, and then
|
|
161
158
|
* balances the tree if necessary.
|
|
162
|
-
* @param {
|
|
159
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw`
|
|
163
160
|
* parameter in the `override delete` method can be one of the following types:
|
|
164
161
|
* @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
|
|
165
162
|
* method from the superclass (presumably a parent class) with the provided `predicate`, which could
|
|
166
163
|
* be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
|
|
167
164
|
* `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
|
|
168
165
|
*/
|
|
169
|
-
override delete(
|
|
170
|
-
const deletedResults = super.delete(
|
|
166
|
+
override delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[] {
|
|
167
|
+
const deletedResults = super.delete(keyNodeEntryOrRaw);
|
|
171
168
|
for (const { needBalanced } of deletedResults) {
|
|
172
169
|
if (needBalanced) {
|
|
173
170
|
this._balancePath(needBalanced);
|
|
@@ -182,16 +179,16 @@ export class AVLTree<
|
|
|
182
179
|
*
|
|
183
180
|
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a
|
|
184
181
|
* binary search tree.
|
|
185
|
-
* @param {R |
|
|
182
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents either a node
|
|
186
183
|
* object (`NODE`) or a key-value pair (`R`) that is being swapped with another node.
|
|
187
|
-
* @param {R |
|
|
188
|
-
* `R` or an instance of `
|
|
184
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter is either an instance of
|
|
185
|
+
* `R` or an instance of `BSTNOptKeyOrNode<K, NODE>`.
|
|
189
186
|
* @returns The method is returning the `destNodeEnsured` object if both `srcNodeEnsured` and
|
|
190
187
|
* `destNodeEnsured` are truthy. Otherwise, it returns `undefined`.
|
|
191
188
|
*/
|
|
192
189
|
protected override _swapProperties(
|
|
193
|
-
srcNode: R |
|
|
194
|
-
destNode: R |
|
|
190
|
+
srcNode: R | BSTNOptKeyOrNode<K, NODE>,
|
|
191
|
+
destNode: R | BSTNOptKeyOrNode<K, NODE>
|
|
195
192
|
): NODE | undefined {
|
|
196
193
|
const srcNodeEnsured = this.ensureNode(srcNode);
|
|
197
194
|
const destNodeEnsured = this.ensureNode(destNode);
|
|
@@ -204,11 +201,11 @@ export class AVLTree<
|
|
|
204
201
|
tempNode.height = height;
|
|
205
202
|
|
|
206
203
|
destNodeEnsured.key = srcNodeEnsured.key;
|
|
207
|
-
destNodeEnsured.value = srcNodeEnsured.value;
|
|
204
|
+
if (!this._isMapMode) destNodeEnsured.value = srcNodeEnsured.value;
|
|
208
205
|
destNodeEnsured.height = srcNodeEnsured.height;
|
|
209
206
|
|
|
210
207
|
srcNodeEnsured.key = tempNode.key;
|
|
211
|
-
srcNodeEnsured.value = tempNode.value;
|
|
208
|
+
if (!this._isMapMode) srcNodeEnsured.value = tempNode.value;
|
|
212
209
|
srcNodeEnsured.height = tempNode.height;
|
|
213
210
|
}
|
|
214
211
|
|
|
@@ -432,10 +429,10 @@ export class AVLTree<
|
|
|
432
429
|
*
|
|
433
430
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
434
431
|
* to restore balance in an AVL tree after inserting a node.
|
|
435
|
-
* @param {
|
|
436
|
-
* `
|
|
432
|
+
* @param {BTNRep<K, V, NODE> | R} node - The `node` parameter can be of type `R` or
|
|
433
|
+
* `BTNRep<K, V, NODE>`.
|
|
437
434
|
*/
|
|
438
|
-
protected _balancePath(node:
|
|
435
|
+
protected _balancePath(node: BTNRep<K, V, NODE> | R): void {
|
|
439
436
|
node = this.ensureNode(node);
|
|
440
437
|
const path = this.getPathToRoot(node => node, node, false); // first O(log n) + O(log n)
|
|
441
438
|
for (let i = 0; i < path.length; i++) {
|