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