data-structure-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/CHANGELOG.md +1 -1
- package/README.md +13 -13
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +151 -151
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +22 -22
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +64 -47
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +20 -20
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +28 -26
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +240 -141
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +395 -269
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +56 -56
- package/dist/cjs/data-structures/binary-tree/bst.js +114 -91
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +13 -13
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +35 -31
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +22 -23
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +59 -48
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.js +3 -3
- package/dist/cjs/interfaces/binary-tree.d.ts +5 -5
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +13 -13
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +22 -22
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +64 -46
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +20 -20
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +28 -25
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +240 -141
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +395 -268
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +56 -56
- package/dist/mjs/data-structures/binary-tree/bst.js +114 -89
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +13 -13
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +35 -30
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +22 -23
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +59 -47
- package/dist/mjs/data-structures/trie/trie.js +3 -3
- package/dist/mjs/interfaces/binary-tree.d.ts +5 -5
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +13 -13
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/umd/data-structure-typed.js +617 -482
- package/dist/umd/data-structure-typed.min.js +5 -5
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +6 -6
- 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
- package/test/integration/bst.test.ts +2 -2
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +1 -1
- package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +2 -2
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +1 -1
- package/test/performance/data-structures/binary-tree/bst.test.ts +1 -1
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +113 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +107 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +169 -26
- package/test/unit/data-structures/binary-tree/bst.test.ts +326 -21
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +139 -1
|
@@ -37,16 +37,16 @@ export class AVLTreeMultiMapNode extends AVLTreeNode {
|
|
|
37
37
|
export class AVLTreeMultiMap extends AVLTree {
|
|
38
38
|
/**
|
|
39
39
|
* The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
|
|
40
|
-
* @param
|
|
40
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
41
41
|
* iterable object that can contain either keys, nodes, entries, or raw elements.
|
|
42
42
|
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
43
43
|
* behavior of the AVLTreeMultiMap. It can include properties such as `compareKeys` and
|
|
44
44
|
* `compareValues` functions to define custom comparison logic for keys and values, respectively.
|
|
45
45
|
*/
|
|
46
|
-
constructor(
|
|
46
|
+
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
47
47
|
super([], options);
|
|
48
|
-
if (
|
|
49
|
-
this.addMany(
|
|
48
|
+
if (keysNodesEntriesOrRaws)
|
|
49
|
+
this.addMany(keysNodesEntriesOrRaws);
|
|
50
50
|
}
|
|
51
51
|
_count = 0;
|
|
52
52
|
/**
|
|
@@ -94,6 +94,7 @@ export class AVLTreeMultiMap extends AVLTree {
|
|
|
94
94
|
createTree(options) {
|
|
95
95
|
return new AVLTreeMultiMap([], {
|
|
96
96
|
iterationType: this.iterationType,
|
|
97
|
+
isMapMode: this._isMapMode,
|
|
97
98
|
comparator: this._comparator,
|
|
98
99
|
toEntryFn: this._toEntryFn,
|
|
99
100
|
...options
|
|
@@ -101,19 +102,19 @@ export class AVLTreeMultiMap extends AVLTree {
|
|
|
101
102
|
}
|
|
102
103
|
/**
|
|
103
104
|
* The function checks if the input is an instance of AVLTreeMultiMapNode.
|
|
104
|
-
* @param {
|
|
105
|
-
* `
|
|
106
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
105
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
106
|
+
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
107
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
|
|
107
108
|
* an instance of the `AVLTreeMultiMapNode` class.
|
|
108
109
|
*/
|
|
109
|
-
isNode(
|
|
110
|
-
return
|
|
110
|
+
isNode(keyNodeEntryOrRaw) {
|
|
111
|
+
return keyNodeEntryOrRaw instanceof AVLTreeMultiMapNode;
|
|
111
112
|
}
|
|
112
113
|
/**
|
|
113
|
-
* The function `
|
|
114
|
+
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
|
|
114
115
|
* a node object.
|
|
115
|
-
* @param {
|
|
116
|
-
* `
|
|
116
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
117
|
+
* `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
117
118
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
118
119
|
* `override` function. It represents the value associated with the key in the data structure. If no
|
|
119
120
|
* value is provided, it will default to `undefined`.
|
|
@@ -121,26 +122,30 @@ export class AVLTreeMultiMap extends AVLTree {
|
|
|
121
122
|
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
122
123
|
* @returns either a NODE object or undefined.
|
|
123
124
|
*/
|
|
124
|
-
|
|
125
|
-
if (
|
|
126
|
-
return;
|
|
127
|
-
if (this.isNode(
|
|
128
|
-
return
|
|
129
|
-
if (this.isEntry(
|
|
130
|
-
const [key, entryValue] =
|
|
125
|
+
keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count = 1) {
|
|
126
|
+
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null)
|
|
127
|
+
return [undefined, undefined];
|
|
128
|
+
if (this.isNode(keyNodeEntryOrRaw))
|
|
129
|
+
return [keyNodeEntryOrRaw, value];
|
|
130
|
+
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
131
|
+
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
131
132
|
if (key === undefined || key === null)
|
|
132
|
-
return;
|
|
133
|
-
|
|
134
|
-
|
|
133
|
+
return [undefined, undefined];
|
|
134
|
+
const finalValue = value ?? entryValue;
|
|
135
|
+
return [this.createNode(key, finalValue, count), finalValue];
|
|
135
136
|
}
|
|
136
|
-
if (this.
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
137
|
+
if (this.isKey(keyNodeEntryOrRaw))
|
|
138
|
+
return [this.createNode(keyNodeEntryOrRaw, value, count), value];
|
|
139
|
+
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
140
|
+
if (this._toEntryFn) {
|
|
141
|
+
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
142
|
+
const finalValue = value ?? entryValue;
|
|
143
|
+
if (this.isKey(key))
|
|
144
|
+
return [this.createNode(key, finalValue, count), finalValue];
|
|
145
|
+
}
|
|
146
|
+
return [undefined, undefined];
|
|
140
147
|
}
|
|
141
|
-
|
|
142
|
-
return this.createNode(keyOrNodeOrEntryOrRaw, value, count);
|
|
143
|
-
return;
|
|
148
|
+
return [undefined, undefined];
|
|
144
149
|
}
|
|
145
150
|
/**
|
|
146
151
|
* Time Complexity: O(log n)
|
|
@@ -148,9 +153,9 @@ export class AVLTreeMultiMap extends AVLTree {
|
|
|
148
153
|
*
|
|
149
154
|
* The function overrides the add method of a TypeScript class to add a new node to a data structure
|
|
150
155
|
* and update the count.
|
|
151
|
-
* @param {
|
|
152
|
-
* `
|
|
153
|
-
* can also accept a value of type `
|
|
156
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
157
|
+
* `keyNodeEntryOrRaw` parameter can accept a value of type `R`, which can be any type. It
|
|
158
|
+
* can also accept a value of type `BTNRep<K, V, NODE>`, which represents a key, node,
|
|
154
159
|
* entry, or raw element
|
|
155
160
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
156
161
|
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
@@ -159,12 +164,12 @@ export class AVLTreeMultiMap extends AVLTree {
|
|
|
159
164
|
* be added once. However, you can specify a different value for `count` if you want to add
|
|
160
165
|
* @returns a boolean value.
|
|
161
166
|
*/
|
|
162
|
-
add(
|
|
163
|
-
const newNode = this.
|
|
167
|
+
add(keyNodeEntryOrRaw, value, count = 1) {
|
|
168
|
+
const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
|
|
164
169
|
if (newNode === undefined)
|
|
165
170
|
return false;
|
|
166
171
|
const orgNodeCount = newNode?.count || 0;
|
|
167
|
-
const inserted = super.add(newNode);
|
|
172
|
+
const inserted = super.add(newNode, newValue);
|
|
168
173
|
if (inserted) {
|
|
169
174
|
this._count += orgNodeCount;
|
|
170
175
|
}
|
|
@@ -176,9 +181,9 @@ export class AVLTreeMultiMap extends AVLTree {
|
|
|
176
181
|
*
|
|
177
182
|
* The function overrides the delete method in a binary tree data structure, handling deletion of
|
|
178
183
|
* nodes and maintaining balance in the tree.
|
|
179
|
-
* @param {
|
|
184
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `predicate`
|
|
180
185
|
* parameter in the `delete` method is used to specify the condition for deleting a node from the
|
|
181
|
-
* binary tree. It can be a key, node,
|
|
186
|
+
* binary tree. It can be a key, node, or entry that determines which
|
|
182
187
|
* node(s) should be deleted.
|
|
183
188
|
* @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
|
|
184
189
|
* boolean flag that determines whether to ignore the count of the node being deleted. If
|
|
@@ -189,11 +194,11 @@ export class AVLTreeMultiMap extends AVLTree {
|
|
|
189
194
|
* method returns an array of `BinaryTreeDeleteResult` objects, each containing information about the
|
|
190
195
|
* deleted node and whether balancing is needed in the tree.
|
|
191
196
|
*/
|
|
192
|
-
delete(
|
|
197
|
+
delete(keyNodeEntryOrRaw, ignoreCount = false) {
|
|
193
198
|
const deletedResult = [];
|
|
194
199
|
if (!this.root)
|
|
195
200
|
return deletedResult;
|
|
196
|
-
const curr = this.getNode(
|
|
201
|
+
const curr = this.getNode(keyNodeEntryOrRaw) ?? undefined;
|
|
197
202
|
if (!curr)
|
|
198
203
|
return deletedResult;
|
|
199
204
|
const parent = curr?.parent ? curr.parent : undefined;
|
|
@@ -280,7 +285,10 @@ export class AVLTreeMultiMap extends AVLTree {
|
|
|
280
285
|
return;
|
|
281
286
|
const m = l + Math.floor((r - l) / 2);
|
|
282
287
|
const midNode = sorted[m];
|
|
283
|
-
this.
|
|
288
|
+
if (this._isMapMode)
|
|
289
|
+
this.add(midNode.key, undefined, midNode.count);
|
|
290
|
+
else
|
|
291
|
+
this.add(midNode.key, midNode.value, midNode.count);
|
|
284
292
|
buildBalanceBST(l, m - 1);
|
|
285
293
|
buildBalanceBST(m + 1, r);
|
|
286
294
|
};
|
|
@@ -296,7 +304,10 @@ export class AVLTreeMultiMap extends AVLTree {
|
|
|
296
304
|
if (l <= r) {
|
|
297
305
|
const m = l + Math.floor((r - l) / 2);
|
|
298
306
|
const midNode = sorted[m];
|
|
299
|
-
this.
|
|
307
|
+
if (this._isMapMode)
|
|
308
|
+
this.add(midNode.key, undefined, midNode.count);
|
|
309
|
+
else
|
|
310
|
+
this.add(midNode.key, midNode.value, midNode.count);
|
|
300
311
|
stack.push([m + 1, r]);
|
|
301
312
|
stack.push([l, m - 1]);
|
|
302
313
|
}
|
|
@@ -314,7 +325,12 @@ export class AVLTreeMultiMap extends AVLTree {
|
|
|
314
325
|
*/
|
|
315
326
|
clone() {
|
|
316
327
|
const cloned = this.createTree();
|
|
317
|
-
|
|
328
|
+
if (this._isMapMode)
|
|
329
|
+
this.bfs(node => cloned.add(node.key, undefined, node.count));
|
|
330
|
+
else
|
|
331
|
+
this.bfs(node => cloned.add(node.key, node.value, node.count));
|
|
332
|
+
if (this._isMapMode)
|
|
333
|
+
cloned._store = this._store;
|
|
318
334
|
return cloned;
|
|
319
335
|
}
|
|
320
336
|
/**
|
|
@@ -323,9 +339,9 @@ export class AVLTreeMultiMap extends AVLTree {
|
|
|
323
339
|
*
|
|
324
340
|
* The `_swapProperties` function swaps the properties (key, value, count, height) between two nodes
|
|
325
341
|
* in a binary search tree.
|
|
326
|
-
* @param {R |
|
|
342
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
|
|
327
343
|
* that will be swapped with the `destNode`.
|
|
328
|
-
* @param {R |
|
|
344
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
|
|
329
345
|
* node where the properties will be swapped with the source node.
|
|
330
346
|
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
331
347
|
* If either `srcNode` or `destNode` is undefined, it returns `undefined`.
|
|
@@ -339,11 +355,13 @@ export class AVLTreeMultiMap extends AVLTree {
|
|
|
339
355
|
if (tempNode) {
|
|
340
356
|
tempNode.height = height;
|
|
341
357
|
destNode.key = srcNode.key;
|
|
342
|
-
|
|
358
|
+
if (!this._isMapMode)
|
|
359
|
+
destNode.value = srcNode.value;
|
|
343
360
|
destNode.count = srcNode.count;
|
|
344
361
|
destNode.height = srcNode.height;
|
|
345
362
|
srcNode.key = tempNode.key;
|
|
346
|
-
|
|
363
|
+
if (!this._isMapMode)
|
|
364
|
+
srcNode.value = tempNode.value;
|
|
347
365
|
srcNode.count = tempNode.count;
|
|
348
366
|
srcNode.height = tempNode.height;
|
|
349
367
|
}
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
|
-
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeleteResult,
|
|
9
|
+
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
export declare class AVLTreeNode<K = any, V = any, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
|
|
12
12
|
/**
|
|
@@ -40,11 +40,11 @@ export declare class AVLTreeNode<K = any, V = any, NODE extends AVLTreeNode<K, V
|
|
|
40
40
|
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
|
41
41
|
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
|
|
42
42
|
*/
|
|
43
|
-
export declare class AVLTree<K = any, V = any, R =
|
|
43
|
+
export declare class AVLTree<K = any, V = any, R = object, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>, TREE extends AVLTree<K, V, R, NODE, TREE> = AVLTree<K, V, R, NODE, AVLTreeNested<K, V, R, NODE>>> extends BST<K, V, R, NODE, TREE> implements IBinaryTree<K, V, R, NODE, TREE> {
|
|
44
44
|
/**
|
|
45
45
|
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
|
|
46
46
|
* entries, or raw elements.
|
|
47
|
-
* @param
|
|
47
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
48
48
|
* iterable object that can contain either keys, nodes, entries, or raw elements. These elements will
|
|
49
49
|
* be used to initialize the AVLTree.
|
|
50
50
|
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
@@ -52,7 +52,7 @@ export declare class AVLTree<K = any, V = any, R = BTNEntry<K, V>, NODE extends
|
|
|
52
52
|
* keys), `allowDuplicates` (a boolean indicating whether duplicate keys are allowed), and
|
|
53
53
|
* `nodeBuilder` (
|
|
54
54
|
*/
|
|
55
|
-
constructor(
|
|
55
|
+
constructor(keysNodesEntriesOrRaws?: Iterable<R | BTNRep<K, V, NODE>>, options?: AVLTreeOptions<K, V, R>);
|
|
56
56
|
/**
|
|
57
57
|
* The function creates a new AVL tree node with the given key and value.
|
|
58
58
|
* @param {K} key - The key parameter is of type K, which represents the key of the node being
|
|
@@ -73,54 +73,54 @@ export declare class AVLTree<K = any, V = any, R = BTNEntry<K, V>, NODE extends
|
|
|
73
73
|
createTree(options?: AVLTreeOptions<K, V, R>): TREE;
|
|
74
74
|
/**
|
|
75
75
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
76
|
-
* @param {
|
|
77
|
-
* `
|
|
78
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
76
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
77
|
+
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
78
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
|
|
79
79
|
* an instance of the `AVLTreeNode` class.
|
|
80
80
|
*/
|
|
81
|
-
isNode(
|
|
81
|
+
isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE;
|
|
82
82
|
/**
|
|
83
83
|
* Time Complexity: O(log n)
|
|
84
84
|
* Space Complexity: O(1)
|
|
85
85
|
*
|
|
86
86
|
* The function overrides the add method of a class and inserts a key-value pair into a data
|
|
87
87
|
* structure, then balances the path.
|
|
88
|
-
* @param {
|
|
89
|
-
* `
|
|
88
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
89
|
+
* `keyNodeEntryOrRaw` can accept values of type `R`, `BTNRep<K, V, NODE>`, or
|
|
90
90
|
* `RawElement`.
|
|
91
91
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
92
92
|
* the key or node being added to the data structure.
|
|
93
93
|
* @returns The method is returning a boolean value.
|
|
94
94
|
*/
|
|
95
|
-
add(
|
|
95
|
+
add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): boolean;
|
|
96
96
|
/**
|
|
97
97
|
* Time Complexity: O(log n)
|
|
98
98
|
* Space Complexity: O(1)
|
|
99
99
|
*
|
|
100
100
|
* The function overrides the delete method in a TypeScript class, performs deletion, and then
|
|
101
101
|
* balances the tree if necessary.
|
|
102
|
-
* @param {
|
|
102
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw`
|
|
103
103
|
* parameter in the `override delete` method can be one of the following types:
|
|
104
104
|
* @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
|
|
105
105
|
* method from the superclass (presumably a parent class) with the provided `predicate`, which could
|
|
106
106
|
* be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
|
|
107
107
|
* `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
|
|
108
108
|
*/
|
|
109
|
-
delete(
|
|
109
|
+
delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[];
|
|
110
110
|
/**
|
|
111
111
|
* Time Complexity: O(1)
|
|
112
112
|
* Space Complexity: O(1)
|
|
113
113
|
*
|
|
114
114
|
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a
|
|
115
115
|
* binary search tree.
|
|
116
|
-
* @param {R |
|
|
116
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents either a node
|
|
117
117
|
* object (`NODE`) or a key-value pair (`R`) that is being swapped with another node.
|
|
118
|
-
* @param {R |
|
|
119
|
-
* `R` or an instance of `
|
|
118
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter is either an instance of
|
|
119
|
+
* `R` or an instance of `BSTNOptKeyOrNode<K, NODE>`.
|
|
120
120
|
* @returns The method is returning the `destNodeEnsured` object if both `srcNodeEnsured` and
|
|
121
121
|
* `destNodeEnsured` are truthy. Otherwise, it returns `undefined`.
|
|
122
122
|
*/
|
|
123
|
-
protected _swapProperties(srcNode: R |
|
|
123
|
+
protected _swapProperties(srcNode: R | BSTNOptKeyOrNode<K, NODE>, destNode: R | BSTNOptKeyOrNode<K, NODE>): NODE | undefined;
|
|
124
124
|
/**
|
|
125
125
|
* Time Complexity: O(1)
|
|
126
126
|
* Space Complexity: O(1)
|
|
@@ -179,10 +179,10 @@ export declare class AVLTree<K = any, V = any, R = BTNEntry<K, V>, NODE extends
|
|
|
179
179
|
*
|
|
180
180
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
181
181
|
* to restore balance in an AVL tree after inserting a node.
|
|
182
|
-
* @param {
|
|
183
|
-
* `
|
|
182
|
+
* @param {BTNRep<K, V, NODE> | R} node - The `node` parameter can be of type `R` or
|
|
183
|
+
* `BTNRep<K, V, NODE>`.
|
|
184
184
|
*/
|
|
185
|
-
protected _balancePath(node:
|
|
185
|
+
protected _balancePath(node: BTNRep<K, V, NODE> | R): void;
|
|
186
186
|
/**
|
|
187
187
|
* Time Complexity: O(1)
|
|
188
188
|
* Space Complexity: O(1)
|
|
@@ -49,7 +49,7 @@ export class AVLTree extends BST {
|
|
|
49
49
|
/**
|
|
50
50
|
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
|
|
51
51
|
* entries, or raw elements.
|
|
52
|
-
* @param
|
|
52
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
53
53
|
* iterable object that can contain either keys, nodes, entries, or raw elements. These elements will
|
|
54
54
|
* be used to initialize the AVLTree.
|
|
55
55
|
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
@@ -57,10 +57,10 @@ export class AVLTree extends BST {
|
|
|
57
57
|
* keys), `allowDuplicates` (a boolean indicating whether duplicate keys are allowed), and
|
|
58
58
|
* `nodeBuilder` (
|
|
59
59
|
*/
|
|
60
|
-
constructor(
|
|
60
|
+
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
61
61
|
super([], options);
|
|
62
|
-
if (
|
|
63
|
-
super.addMany(
|
|
62
|
+
if (keysNodesEntriesOrRaws)
|
|
63
|
+
super.addMany(keysNodesEntriesOrRaws);
|
|
64
64
|
}
|
|
65
65
|
/**
|
|
66
66
|
* The function creates a new AVL tree node with the given key and value.
|
|
@@ -84,6 +84,7 @@ export class AVLTree extends BST {
|
|
|
84
84
|
createTree(options) {
|
|
85
85
|
return new AVLTree([], {
|
|
86
86
|
iterationType: this.iterationType,
|
|
87
|
+
isMapMode: this._isMapMode,
|
|
87
88
|
comparator: this._comparator,
|
|
88
89
|
toEntryFn: this._toEntryFn,
|
|
89
90
|
...options
|
|
@@ -91,13 +92,13 @@ export class AVLTree extends BST {
|
|
|
91
92
|
}
|
|
92
93
|
/**
|
|
93
94
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
94
|
-
* @param {
|
|
95
|
-
* `
|
|
96
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
95
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
96
|
+
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
97
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
|
|
97
98
|
* an instance of the `AVLTreeNode` class.
|
|
98
99
|
*/
|
|
99
|
-
isNode(
|
|
100
|
-
return
|
|
100
|
+
isNode(keyNodeEntryOrRaw) {
|
|
101
|
+
return keyNodeEntryOrRaw instanceof AVLTreeNode;
|
|
101
102
|
}
|
|
102
103
|
/**
|
|
103
104
|
* Time Complexity: O(log n)
|
|
@@ -105,19 +106,19 @@ export class AVLTree extends BST {
|
|
|
105
106
|
*
|
|
106
107
|
* The function overrides the add method of a class and inserts a key-value pair into a data
|
|
107
108
|
* structure, then balances the path.
|
|
108
|
-
* @param {
|
|
109
|
-
* `
|
|
109
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
110
|
+
* `keyNodeEntryOrRaw` can accept values of type `R`, `BTNRep<K, V, NODE>`, or
|
|
110
111
|
* `RawElement`.
|
|
111
112
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
112
113
|
* the key or node being added to the data structure.
|
|
113
114
|
* @returns The method is returning a boolean value.
|
|
114
115
|
*/
|
|
115
|
-
add(
|
|
116
|
-
if (
|
|
116
|
+
add(keyNodeEntryOrRaw, value) {
|
|
117
|
+
if (keyNodeEntryOrRaw === null)
|
|
117
118
|
return false;
|
|
118
|
-
const inserted = super.add(
|
|
119
|
+
const inserted = super.add(keyNodeEntryOrRaw, value);
|
|
119
120
|
if (inserted)
|
|
120
|
-
this._balancePath(
|
|
121
|
+
this._balancePath(keyNodeEntryOrRaw);
|
|
121
122
|
return inserted;
|
|
122
123
|
}
|
|
123
124
|
/**
|
|
@@ -126,15 +127,15 @@ export class AVLTree extends BST {
|
|
|
126
127
|
*
|
|
127
128
|
* The function overrides the delete method in a TypeScript class, performs deletion, and then
|
|
128
129
|
* balances the tree if necessary.
|
|
129
|
-
* @param {
|
|
130
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw`
|
|
130
131
|
* parameter in the `override delete` method can be one of the following types:
|
|
131
132
|
* @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
|
|
132
133
|
* method from the superclass (presumably a parent class) with the provided `predicate`, which could
|
|
133
134
|
* be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
|
|
134
135
|
* `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
|
|
135
136
|
*/
|
|
136
|
-
delete(
|
|
137
|
-
const deletedResults = super.delete(
|
|
137
|
+
delete(keyNodeEntryOrRaw) {
|
|
138
|
+
const deletedResults = super.delete(keyNodeEntryOrRaw);
|
|
138
139
|
for (const { needBalanced } of deletedResults) {
|
|
139
140
|
if (needBalanced) {
|
|
140
141
|
this._balancePath(needBalanced);
|
|
@@ -148,10 +149,10 @@ export class AVLTree extends BST {
|
|
|
148
149
|
*
|
|
149
150
|
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a
|
|
150
151
|
* binary search tree.
|
|
151
|
-
* @param {R |
|
|
152
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents either a node
|
|
152
153
|
* object (`NODE`) or a key-value pair (`R`) that is being swapped with another node.
|
|
153
|
-
* @param {R |
|
|
154
|
-
* `R` or an instance of `
|
|
154
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter is either an instance of
|
|
155
|
+
* `R` or an instance of `BSTNOptKeyOrNode<K, NODE>`.
|
|
155
156
|
* @returns The method is returning the `destNodeEnsured` object if both `srcNodeEnsured` and
|
|
156
157
|
* `destNodeEnsured` are truthy. Otherwise, it returns `undefined`.
|
|
157
158
|
*/
|
|
@@ -164,10 +165,12 @@ export class AVLTree extends BST {
|
|
|
164
165
|
if (tempNode) {
|
|
165
166
|
tempNode.height = height;
|
|
166
167
|
destNodeEnsured.key = srcNodeEnsured.key;
|
|
167
|
-
|
|
168
|
+
if (!this._isMapMode)
|
|
169
|
+
destNodeEnsured.value = srcNodeEnsured.value;
|
|
168
170
|
destNodeEnsured.height = srcNodeEnsured.height;
|
|
169
171
|
srcNodeEnsured.key = tempNode.key;
|
|
170
|
-
|
|
172
|
+
if (!this._isMapMode)
|
|
173
|
+
srcNodeEnsured.value = tempNode.value;
|
|
171
174
|
srcNodeEnsured.height = tempNode.height;
|
|
172
175
|
}
|
|
173
176
|
return destNodeEnsured;
|
|
@@ -404,8 +407,8 @@ export class AVLTree extends BST {
|
|
|
404
407
|
*
|
|
405
408
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
406
409
|
* to restore balance in an AVL tree after inserting a node.
|
|
407
|
-
* @param {
|
|
408
|
-
* `
|
|
410
|
+
* @param {BTNRep<K, V, NODE> | R} node - The `node` parameter can be of type `R` or
|
|
411
|
+
* `BTNRep<K, V, NODE>`.
|
|
409
412
|
*/
|
|
410
413
|
_balancePath(node) {
|
|
411
414
|
node = this.ensureNode(node);
|