data-structure-typed 1.54.2 → 1.54.3
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/LICENSE +2 -2
- package/README.md +14 -1
- package/README_zh-CN.md +1 -1
- package/benchmark/report.html +4 -1
- package/benchmark/report.json +76 -17
- package/dist/cjs/data-structures/binary-tree/avl-tree-counter.d.ts +21 -20
- package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js +8 -7
- package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +12 -12
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +2 -2
- 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 +25 -21
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +12 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +111 -225
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +177 -144
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +59 -53
- package/dist/cjs/data-structures/binary-tree/bst.js +75 -119
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/red-black-tree.d.ts +18 -18
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js +6 -6
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-counter.d.ts +19 -19
- package/dist/cjs/data-structures/binary-tree/tree-counter.js +12 -12
- package/dist/cjs/data-structures/binary-tree/tree-counter.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +12 -12
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +2 -2
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/cjs/utils/utils.d.ts +2 -2
- package/dist/esm/data-structures/binary-tree/avl-tree-counter.d.ts +21 -20
- package/dist/esm/data-structures/binary-tree/avl-tree-counter.js +9 -8
- package/dist/esm/data-structures/binary-tree/avl-tree-counter.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.d.ts +12 -12
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js +3 -3
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/avl-tree.d.ts +25 -21
- package/dist/esm/data-structures/binary-tree/avl-tree.js +13 -9
- package/dist/esm/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/binary-tree.d.ts +111 -225
- package/dist/esm/data-structures/binary-tree/binary-tree.js +181 -148
- package/dist/esm/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/bst.d.ts +59 -53
- package/dist/esm/data-structures/binary-tree/bst.js +76 -120
- package/dist/esm/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/red-black-tree.d.ts +18 -18
- package/dist/esm/data-structures/binary-tree/red-black-tree.js +7 -7
- package/dist/esm/data-structures/binary-tree/red-black-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/tree-counter.d.ts +19 -19
- package/dist/esm/data-structures/binary-tree/tree-counter.js +13 -13
- package/dist/esm/data-structures/binary-tree/tree-counter.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/tree-multi-map.d.ts +12 -12
- package/dist/esm/data-structures/binary-tree/tree-multi-map.js +3 -3
- package/dist/esm/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/esm/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
- package/dist/esm/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/esm/utils/utils.d.ts +2 -2
- package/dist/umd/data-structure-typed.js +296 -279
- package/dist/umd/data-structure-typed.min.js +5 -12
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +7 -7
- package/src/data-structures/binary-tree/avl-tree-counter.ts +30 -23
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +25 -15
- package/src/data-structures/binary-tree/avl-tree.ts +35 -29
- package/src/data-structures/binary-tree/binary-tree.ts +469 -252
- package/src/data-structures/binary-tree/bst.ts +141 -143
- package/src/data-structures/binary-tree/red-black-tree.ts +27 -35
- package/src/data-structures/binary-tree/tree-counter.ts +33 -27
- package/src/data-structures/binary-tree/tree-multi-map.ts +25 -17
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/utils/utils.ts +2 -2
- package/test/integration/compile.mjs +21 -21
- package/test/performance/data-structures/binary-tree/avl-tree.test.mjs +71 -0
- package/test/performance/data-structures/binary-tree/red-black-tree.test.mjs +81 -0
- package/test/performance/{reportor.js → reportor.mjs} +264 -8
- package/test/performance/reportor.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +7 -7
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +4 -5
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +72 -5
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +132 -82
- package/test/unit/data-structures/binary-tree/bst.test.ts +12 -12
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +4 -12
- package/test/unit/data-structures/binary-tree/tree-counter.test.ts +4 -4
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +5 -5
- package/test/utils/json2html.ts +0 -154
- package/test/performance/data-structures/binary-tree/avl-tree.test.js +0 -45
- /package/test/performance/data-structures/binary-tree/{rb-tree.test.ts → red-black-tree.test.ts} +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.54.
|
|
3
|
+
"version": "1.54.3",
|
|
4
4
|
"description": "Javascript Data Structure. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java.util. Usability is comparable to Python",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"gen:examples": "ts-node scripts/testToExample.ts",
|
|
19
19
|
"test:in-band": "jest --runInBand",
|
|
20
20
|
"test": "npm run test:in-band",
|
|
21
|
-
"test:integration": "npm run update:subs && jest --config jest.integration.config.js && tsc test/integration/compile.ts",
|
|
21
|
+
"test:integration": "npm run update:subs && jest --config jest.integration.config.js && tsc test/integration/compile.ts && node test/integration/compile.mjs",
|
|
22
22
|
"test:perf": "npm run build:cjs && npm run build:esm && ts-node test/performance/reportor.ts",
|
|
23
23
|
"check": "tsc --noEmit",
|
|
24
24
|
"check:circular-refs": "dependency-cruiser src",
|
|
@@ -65,11 +65,11 @@
|
|
|
65
65
|
"@typescript-eslint/eslint-plugin": "^8.12.1",
|
|
66
66
|
"@typescript-eslint/parser": "^8.12.1",
|
|
67
67
|
"auto-changelog": "^2.5.0",
|
|
68
|
-
"avl-tree-typed": "^1.54.
|
|
68
|
+
"avl-tree-typed": "^1.54.2",
|
|
69
69
|
"benchmark": "^2.1.4",
|
|
70
|
-
"binary-tree-typed": "^1.54.
|
|
71
|
-
"bst-typed": "^1.54.
|
|
72
|
-
"data-structure-typed": "^1.54.
|
|
70
|
+
"binary-tree-typed": "^1.54.2",
|
|
71
|
+
"bst-typed": "^1.54.2",
|
|
72
|
+
"data-structure-typed": "^1.54.2",
|
|
73
73
|
"dependency-cruiser": "^16.5.0",
|
|
74
74
|
"doctoc": "^2.2.1",
|
|
75
75
|
"eslint": "^9.13.0",
|
|
@@ -78,7 +78,7 @@
|
|
|
78
78
|
"eslint-import-resolver-typescript": "^3.6.3",
|
|
79
79
|
"eslint-plugin-import": "^2.31.0",
|
|
80
80
|
"fast-glob": "^3.3.2",
|
|
81
|
-
"heap-typed": "^1.54.
|
|
81
|
+
"heap-typed": "^1.54.2",
|
|
82
82
|
"istanbul-badges-readme": "^1.9.0",
|
|
83
83
|
"jest": "^29.7.0",
|
|
84
84
|
"js-sdsl": "^4.4.2",
|
|
@@ -9,15 +9,15 @@ import type {
|
|
|
9
9
|
AVLTreeCounterOptions,
|
|
10
10
|
BinaryTreeDeleteResult,
|
|
11
11
|
BSTNOptKeyOrNode,
|
|
12
|
-
BTNRep,
|
|
13
12
|
EntryCallback,
|
|
14
|
-
IterationType
|
|
15
|
-
OptNodeOrNull
|
|
13
|
+
IterationType
|
|
16
14
|
} from '../../types';
|
|
17
15
|
import { IBinaryTree } from '../../interfaces';
|
|
18
16
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
19
17
|
|
|
20
18
|
export class AVLTreeCounterNode<K = any, V = any> extends AVLTreeNode<K, V> {
|
|
19
|
+
override parent?: AVLTreeCounterNode<K, V> = undefined;
|
|
20
|
+
|
|
21
21
|
/**
|
|
22
22
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
23
23
|
* @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
|
|
@@ -33,28 +33,26 @@ export class AVLTreeCounterNode<K = any, V = any> extends AVLTreeNode<K, V> {
|
|
|
33
33
|
this.count = count;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
override
|
|
37
|
-
|
|
38
|
-
override _left?: OptNodeOrNull<AVLTreeCounterNode<K, V>> = undefined;
|
|
36
|
+
override _left?: AVLTreeCounterNode<K, V> | null | undefined = undefined;
|
|
39
37
|
|
|
40
|
-
override get left():
|
|
38
|
+
override get left(): AVLTreeCounterNode<K, V> | null | undefined {
|
|
41
39
|
return this._left;
|
|
42
40
|
}
|
|
43
41
|
|
|
44
|
-
override set left(v:
|
|
42
|
+
override set left(v: AVLTreeCounterNode<K, V> | null | undefined) {
|
|
45
43
|
if (v) {
|
|
46
44
|
v.parent = this;
|
|
47
45
|
}
|
|
48
46
|
this._left = v;
|
|
49
47
|
}
|
|
50
48
|
|
|
51
|
-
override _right?:
|
|
49
|
+
override _right?: AVLTreeCounterNode<K, V> | null | undefined = undefined;
|
|
52
50
|
|
|
53
|
-
override get right():
|
|
51
|
+
override get right(): AVLTreeCounterNode<K, V> | null | undefined {
|
|
54
52
|
return this._right;
|
|
55
53
|
}
|
|
56
54
|
|
|
57
|
-
override set right(v:
|
|
55
|
+
override set right(v: AVLTreeCounterNode<K, V> | null | undefined) {
|
|
58
56
|
if (v) {
|
|
59
57
|
v.parent = this;
|
|
60
58
|
}
|
|
@@ -78,7 +76,9 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
|
|
|
78
76
|
* `compareValues` functions to define custom comparison logic for keys and values, respectively.
|
|
79
77
|
*/
|
|
80
78
|
constructor(
|
|
81
|
-
keysNodesEntriesOrRaws: Iterable<
|
|
79
|
+
keysNodesEntriesOrRaws: Iterable<
|
|
80
|
+
K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
|
|
81
|
+
> = [],
|
|
82
82
|
options?: AVLTreeCounterOptions<K, V, R>
|
|
83
83
|
) {
|
|
84
84
|
super([], options);
|
|
@@ -145,12 +145,14 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
|
|
|
145
145
|
|
|
146
146
|
/**
|
|
147
147
|
* The function checks if the input is an instance of AVLTreeCounterNode.
|
|
148
|
-
* @param {
|
|
149
|
-
* `keyNodeOrEntry` can be of type `R` or `
|
|
148
|
+
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter
|
|
149
|
+
* `keyNodeOrEntry` can be of type `R` or `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
|
|
150
150
|
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
|
|
151
151
|
* an instance of the `AVLTreeCounterNode` class.
|
|
152
152
|
*/
|
|
153
|
-
override isNode(
|
|
153
|
+
override isNode(
|
|
154
|
+
keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
|
|
155
|
+
): keyNodeOrEntry is AVLTreeCounterNode<K, V> {
|
|
154
156
|
return keyNodeOrEntry instanceof AVLTreeCounterNode;
|
|
155
157
|
}
|
|
156
158
|
|
|
@@ -160,9 +162,9 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
|
|
|
160
162
|
*
|
|
161
163
|
* The function overrides the add method of a TypeScript class to add a new node to a data structure
|
|
162
164
|
* and update the count.
|
|
163
|
-
* @param {
|
|
165
|
+
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The
|
|
164
166
|
* `keyNodeOrEntry` parameter can accept a value of type `R`, which can be any type. It
|
|
165
|
-
* can also accept a value of type `
|
|
167
|
+
* can also accept a value of type `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`, which represents a key, node,
|
|
166
168
|
* entry, or raw element
|
|
167
169
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
168
170
|
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
@@ -171,7 +173,11 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
|
|
|
171
173
|
* be added once. However, you can specify a different value for `count` if you want to add
|
|
172
174
|
* @returns a boolean value.
|
|
173
175
|
*/
|
|
174
|
-
override add(
|
|
176
|
+
override add(
|
|
177
|
+
keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
178
|
+
value?: V,
|
|
179
|
+
count = 1
|
|
180
|
+
): boolean {
|
|
175
181
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count);
|
|
176
182
|
if (newNode === undefined) return false;
|
|
177
183
|
|
|
@@ -189,7 +195,7 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
|
|
|
189
195
|
*
|
|
190
196
|
* The function overrides the delete method in a binary tree data structure, handling deletion of
|
|
191
197
|
* nodes and maintaining balance in the tree.
|
|
192
|
-
* @param {
|
|
198
|
+
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The `predicate`
|
|
193
199
|
* parameter in the `delete` method is used to specify the condition for deleting a node from the
|
|
194
200
|
* binary tree. It can be a key, node, or entry that determines which
|
|
195
201
|
* node(s) should be deleted.
|
|
@@ -203,7 +209,7 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
|
|
|
203
209
|
* deleted node and whether balancing is needed in the tree.
|
|
204
210
|
*/
|
|
205
211
|
override delete(
|
|
206
|
-
keyNodeOrEntry:
|
|
212
|
+
keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
207
213
|
ignoreCount = false
|
|
208
214
|
): BinaryTreeDeleteResult<AVLTreeCounterNode<K, V>>[] {
|
|
209
215
|
const deletedResult: BinaryTreeDeleteResult<AVLTreeCounterNode<K, V>>[] = [];
|
|
@@ -276,6 +282,7 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
|
|
|
276
282
|
/**
|
|
277
283
|
* Time Complexity: O(n log n)
|
|
278
284
|
* Space Complexity: O(log n)
|
|
285
|
+
*
|
|
279
286
|
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
280
287
|
* tree using either a recursive or iterative approach.
|
|
281
288
|
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
@@ -374,8 +381,8 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
|
|
|
374
381
|
/**
|
|
375
382
|
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
|
|
376
383
|
* a node object.
|
|
377
|
-
* @param {
|
|
378
|
-
* `keyNodeOrEntry` parameter can be of type `R` or `
|
|
384
|
+
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The
|
|
385
|
+
* `keyNodeOrEntry` parameter can be of type `R` or `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
|
|
379
386
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
380
387
|
* `override` function. It represents the value associated with the key in the data structure. If no
|
|
381
388
|
* value is provided, it will default to `undefined`.
|
|
@@ -384,7 +391,7 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
|
|
|
384
391
|
* @returns either a AVLTreeCounterNode<K, V> object or undefined.
|
|
385
392
|
*/
|
|
386
393
|
protected override _keyValueNodeOrEntryToNodeAndValue(
|
|
387
|
-
keyNodeOrEntry:
|
|
394
|
+
keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
388
395
|
value?: V,
|
|
389
396
|
count = 1
|
|
390
397
|
): [AVLTreeCounterNode<K, V> | undefined, V | undefined] {
|
|
@@ -5,11 +5,13 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { AVLTreeMultiMapOptions, BTNOptKeyOrNull
|
|
8
|
+
import { AVLTreeMultiMapOptions, BTNOptKeyOrNull } from '../../types';
|
|
9
9
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
|
|
12
12
|
export class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> {
|
|
13
|
+
override parent?: AVLTreeMultiMapNode<K, V> = undefined;
|
|
14
|
+
|
|
13
15
|
/**
|
|
14
16
|
* This TypeScript constructor initializes an object with a key of type K and an array of values of
|
|
15
17
|
* type V.
|
|
@@ -23,28 +25,26 @@ export class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> {
|
|
|
23
25
|
super(key, value);
|
|
24
26
|
}
|
|
25
27
|
|
|
26
|
-
override
|
|
27
|
-
|
|
28
|
-
override _left?: OptNodeOrNull<AVLTreeMultiMapNode<K, V>> = undefined;
|
|
28
|
+
override _left?: AVLTreeMultiMapNode<K, V> | null | undefined = undefined;
|
|
29
29
|
|
|
30
|
-
override get left():
|
|
30
|
+
override get left(): AVLTreeMultiMapNode<K, V> | null | undefined {
|
|
31
31
|
return this._left;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
override set left(v:
|
|
34
|
+
override set left(v: AVLTreeMultiMapNode<K, V> | null | undefined) {
|
|
35
35
|
if (v) {
|
|
36
36
|
v.parent = this;
|
|
37
37
|
}
|
|
38
38
|
this._left = v;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
override _right?:
|
|
41
|
+
override _right?: AVLTreeMultiMapNode<K, V> | null | undefined = undefined;
|
|
42
42
|
|
|
43
|
-
override get right():
|
|
43
|
+
override get right(): AVLTreeMultiMapNode<K, V> | null | undefined {
|
|
44
44
|
return this._right;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
override set right(v:
|
|
47
|
+
override set right(v: AVLTreeMultiMapNode<K, V> | null | undefined) {
|
|
48
48
|
if (v) {
|
|
49
49
|
v.parent = this;
|
|
50
50
|
}
|
|
@@ -71,7 +71,9 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
|
|
|
71
71
|
* additional options for configuring the AVLTreeMultiMap instance.
|
|
72
72
|
*/
|
|
73
73
|
constructor(
|
|
74
|
-
keysNodesEntriesOrRaws: Iterable<
|
|
74
|
+
keysNodesEntriesOrRaws: Iterable<
|
|
75
|
+
K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | R
|
|
76
|
+
> = [],
|
|
75
77
|
options?: AVLTreeMultiMapOptions<K, V[], R>
|
|
76
78
|
) {
|
|
77
79
|
super([], { ...options, isMapMode: true });
|
|
@@ -117,7 +119,9 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
|
|
|
117
119
|
return new AVLTreeMultiMapNode<K, V>(key, []);
|
|
118
120
|
}
|
|
119
121
|
|
|
120
|
-
override add(
|
|
122
|
+
override add(
|
|
123
|
+
node: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined
|
|
124
|
+
): boolean;
|
|
121
125
|
|
|
122
126
|
override add(key: K, value: V): boolean;
|
|
123
127
|
|
|
@@ -127,7 +131,7 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
|
|
|
127
131
|
*
|
|
128
132
|
* The function `add` in TypeScript overrides the superclass method to add key-value pairs to an AVL
|
|
129
133
|
* tree multi-map.
|
|
130
|
-
* @param {
|
|
134
|
+
* @param {K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
131
135
|
* parameter in the `override add` method can be either a key-value pair entry or just a key. If it
|
|
132
136
|
* is a key-value pair entry, it will be in the format `[key, values]`, where `key` is the key and
|
|
133
137
|
* `values`
|
|
@@ -137,7 +141,10 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
|
|
|
137
141
|
* @returns The `override add` method is returning a boolean value, which indicates whether the
|
|
138
142
|
* addition operation was successful or not.
|
|
139
143
|
*/
|
|
140
|
-
override add(
|
|
144
|
+
override add(
|
|
145
|
+
keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K,
|
|
146
|
+
value?: V
|
|
147
|
+
): boolean {
|
|
141
148
|
if (this.isRealNode(keyNodeOrEntry)) return super.add(keyNodeOrEntry);
|
|
142
149
|
|
|
143
150
|
const _commonAdd = (key?: BTNOptKeyOrNull<K>, values?: V[]) => {
|
|
@@ -180,7 +187,7 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
|
|
|
180
187
|
*
|
|
181
188
|
* The function `deleteValue` removes a specific value from a key in an AVLTreeMultiMap data
|
|
182
189
|
* structure and deletes the entire node if no values are left for that key.
|
|
183
|
-
* @param {
|
|
190
|
+
* @param {K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
184
191
|
* parameter in the `deleteValue` function can be either a `BTNRep` object representing a key-value
|
|
185
192
|
* pair in the AVLTreeMultiMapNode, or just the key itself.
|
|
186
193
|
* @param {V} value - The `value` parameter in the `deleteValue` function represents the specific
|
|
@@ -191,7 +198,10 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
|
|
|
191
198
|
* `value` was successfully deleted from the array of values associated with the `keyNodeOrEntry`. If
|
|
192
199
|
* the value was not found in the array, it returns `false`.
|
|
193
200
|
*/
|
|
194
|
-
deleteValue(
|
|
201
|
+
deleteValue(
|
|
202
|
+
keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K,
|
|
203
|
+
value: V
|
|
204
|
+
): boolean {
|
|
195
205
|
const values = this.get(keyNodeOrEntry);
|
|
196
206
|
if (Array.isArray(values)) {
|
|
197
207
|
const index = values.indexOf(value);
|
|
@@ -6,17 +6,12 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
|
-
import type {
|
|
10
|
-
AVLTreeOptions,
|
|
11
|
-
BinaryTreeDeleteResult,
|
|
12
|
-
BSTNOptKeyOrNode,
|
|
13
|
-
BTNRep,
|
|
14
|
-
EntryCallback,
|
|
15
|
-
OptNodeOrNull
|
|
16
|
-
} from '../../types';
|
|
9
|
+
import type { AVLTreeOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, EntryCallback } from '../../types';
|
|
17
10
|
import { IBinaryTree } from '../../interfaces';
|
|
18
11
|
|
|
19
12
|
export class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
13
|
+
override parent?: AVLTreeNode<K, V> = undefined;
|
|
14
|
+
|
|
20
15
|
/**
|
|
21
16
|
* This TypeScript constructor function initializes an instance with a key and an optional value.
|
|
22
17
|
* @param {K} key - The `key` parameter is typically used to uniquely identify an object or element
|
|
@@ -30,28 +25,26 @@ export class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
30
25
|
super(key, value);
|
|
31
26
|
}
|
|
32
27
|
|
|
33
|
-
override
|
|
34
|
-
|
|
35
|
-
override _left?: OptNodeOrNull<AVLTreeNode<K, V>> = undefined;
|
|
28
|
+
override _left?: AVLTreeNode<K, V> | null | undefined = undefined;
|
|
36
29
|
|
|
37
|
-
override get left():
|
|
30
|
+
override get left(): AVLTreeNode<K, V> | null | undefined {
|
|
38
31
|
return this._left;
|
|
39
32
|
}
|
|
40
33
|
|
|
41
|
-
override set left(v:
|
|
34
|
+
override set left(v: AVLTreeNode<K, V> | null | undefined) {
|
|
42
35
|
if (v) {
|
|
43
36
|
v.parent = this;
|
|
44
37
|
}
|
|
45
38
|
this._left = v;
|
|
46
39
|
}
|
|
47
40
|
|
|
48
|
-
override _right?:
|
|
41
|
+
override _right?: AVLTreeNode<K, V> | null | undefined = undefined;
|
|
49
42
|
|
|
50
|
-
override get right():
|
|
43
|
+
override get right(): AVLTreeNode<K, V> | null | undefined {
|
|
51
44
|
return this._right;
|
|
52
45
|
}
|
|
53
46
|
|
|
54
|
-
override set right(v:
|
|
47
|
+
override set right(v: AVLTreeNode<K, V> | null | undefined) {
|
|
55
48
|
if (v) {
|
|
56
49
|
v.parent = this;
|
|
57
50
|
}
|
|
@@ -76,7 +69,8 @@ export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = obje
|
|
|
76
69
|
* This TypeScript constructor initializes an AVLTree with keys, nodes, entries, or raw data provided
|
|
77
70
|
* in an iterable format.
|
|
78
71
|
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
|
|
79
|
-
* iterable that can contain either `
|
|
72
|
+
* iterable that can contain either `
|
|
73
|
+
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` objects or `R` objects. It is
|
|
80
74
|
* used to initialize the AVLTree with key-value pairs or raw data entries. If provided
|
|
81
75
|
* @param [options] - The `options` parameter in the constructor is of type `AVLTreeOptions<K, V,
|
|
82
76
|
* R>`. It is an optional parameter that allows you to specify additional options for configuring the
|
|
@@ -84,7 +78,9 @@ export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = obje
|
|
|
84
78
|
* other configuration settings specific
|
|
85
79
|
*/
|
|
86
80
|
constructor(
|
|
87
|
-
keysNodesEntriesOrRaws: Iterable<
|
|
81
|
+
keysNodesEntriesOrRaws: Iterable<
|
|
82
|
+
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
|
|
83
|
+
> = [],
|
|
88
84
|
options?: AVLTreeOptions<K, V, R>
|
|
89
85
|
) {
|
|
90
86
|
super([], options);
|
|
@@ -133,12 +129,15 @@ export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = obje
|
|
|
133
129
|
* Space Complexity: O(1)
|
|
134
130
|
*
|
|
135
131
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
136
|
-
* @param {
|
|
137
|
-
* `keyNodeOrEntry` can be of type `R` or `
|
|
132
|
+
* @param {K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
|
|
133
|
+
* `keyNodeOrEntry` can be of type `R` or `
|
|
134
|
+
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `.
|
|
138
135
|
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
|
|
139
136
|
* an instance of the `AVLTreeNode` class.
|
|
140
137
|
*/
|
|
141
|
-
override isNode(
|
|
138
|
+
override isNode(
|
|
139
|
+
keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
|
|
140
|
+
): keyNodeOrEntry is AVLTreeNode<K, V> {
|
|
142
141
|
return keyNodeOrEntry instanceof AVLTreeNode;
|
|
143
142
|
}
|
|
144
143
|
|
|
@@ -148,13 +147,17 @@ export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = obje
|
|
|
148
147
|
*
|
|
149
148
|
* The function overrides the add method of a class and inserts a key-value pair into a data
|
|
150
149
|
* structure, then balances the path.
|
|
151
|
-
* @param {
|
|
152
|
-
* `keyNodeOrEntry` can accept values of type `R`, `
|
|
150
|
+
* @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
|
|
151
|
+
* `keyNodeOrEntry` can accept values of type `R`, `
|
|
152
|
+
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `
|
|
153
153
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
154
154
|
* the key or node being added to the data structure.
|
|
155
155
|
* @returns The method is returning a boolean value.
|
|
156
156
|
*/
|
|
157
|
-
override add(
|
|
157
|
+
override add(
|
|
158
|
+
keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
159
|
+
value?: V
|
|
160
|
+
): boolean {
|
|
158
161
|
if (keyNodeOrEntry === null) return false;
|
|
159
162
|
const inserted = super.add(keyNodeOrEntry, value);
|
|
160
163
|
if (inserted) this._balancePath(keyNodeOrEntry);
|
|
@@ -167,14 +170,16 @@ export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = obje
|
|
|
167
170
|
*
|
|
168
171
|
* The function overrides the delete method in a TypeScript class, performs deletion, and then
|
|
169
172
|
* balances the tree if necessary.
|
|
170
|
-
* @param {
|
|
173
|
+
* @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
|
|
171
174
|
* parameter in the `override delete` method can be one of the following types:
|
|
172
175
|
* @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
|
|
173
176
|
* method from the superclass (presumably a parent class) with the provided `predicate`, which could
|
|
174
177
|
* be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
|
|
175
178
|
* `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
|
|
176
179
|
*/
|
|
177
|
-
override delete(
|
|
180
|
+
override delete(
|
|
181
|
+
keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
|
|
182
|
+
): BinaryTreeDeleteResult<AVLTreeNode<K, V>>[] {
|
|
178
183
|
const deletedResults = super.delete(keyNodeOrEntry);
|
|
179
184
|
for (const { needBalanced } of deletedResults) {
|
|
180
185
|
if (needBalanced) {
|
|
@@ -487,10 +492,11 @@ export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = obje
|
|
|
487
492
|
*
|
|
488
493
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
489
494
|
* to restore balance in an AVL tree after inserting a node.
|
|
490
|
-
* @param {
|
|
491
|
-
* `
|
|
495
|
+
* @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } node - The `node` parameter can be of type `R` or
|
|
496
|
+
* `
|
|
497
|
+
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `.
|
|
492
498
|
*/
|
|
493
|
-
protected _balancePath(node:
|
|
499
|
+
protected _balancePath(node: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): void {
|
|
494
500
|
node = this.ensureNode(node);
|
|
495
501
|
const path = this.getPathToRoot(node, node => node, false); // first O(log n) + O(log n)
|
|
496
502
|
for (let i = 0; i < path.length; i++) {
|