data-structure-typed 1.53.9 → 1.54.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -17
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +1 -20
- 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 +9 -24
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +18 -34
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +40 -37
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +68 -53
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +36 -50
- package/dist/cjs/data-structures/binary-tree/bst.js +45 -60
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/red-black-tree.d.ts +29 -43
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js +45 -59
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +33 -45
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +70 -83
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +4 -3
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -2
- package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +3 -2
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +3 -2
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +5 -4
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -2
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -17
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +1 -20
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +9 -24
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +18 -35
- package/dist/mjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +40 -37
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +68 -53
- package/dist/mjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +36 -50
- package/dist/mjs/data-structures/binary-tree/bst.js +65 -80
- package/dist/mjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/red-black-tree.d.ts +29 -43
- package/dist/mjs/data-structures/binary-tree/red-black-tree.js +44 -59
- package/dist/mjs/data-structures/binary-tree/red-black-tree.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +33 -45
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +70 -83
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/mjs/interfaces/binary-tree.d.ts +4 -3
- package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -2
- package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +3 -2
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +3 -2
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +5 -4
- package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -2
- package/dist/umd/data-structure-typed.js +223 -298
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +5 -5
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +23 -31
- package/src/data-structures/binary-tree/avl-tree.ts +35 -46
- package/src/data-structures/binary-tree/binary-tree.ts +105 -66
- package/src/data-structures/binary-tree/bst.ts +105 -104
- package/src/data-structures/binary-tree/red-black-tree.ts +68 -73
- package/src/data-structures/binary-tree/tree-multi-map.ts +98 -102
- package/src/interfaces/binary-tree.ts +16 -3
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +4 -2
- package/src/types/data-structures/binary-tree/avl-tree.ts +4 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/bst.ts +4 -2
- package/src/types/data-structures/binary-tree/rb-tree.ts +6 -4
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.54.0",
|
|
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/mjs/index.js",
|
|
@@ -70,11 +70,11 @@
|
|
|
70
70
|
"@typescript-eslint/eslint-plugin": "^8.12.1",
|
|
71
71
|
"@typescript-eslint/parser": "^8.12.1",
|
|
72
72
|
"auto-changelog": "^2.5.0",
|
|
73
|
-
"avl-tree-typed": "^1.53.
|
|
73
|
+
"avl-tree-typed": "^1.53.9",
|
|
74
74
|
"benchmark": "^2.1.4",
|
|
75
75
|
"binary-tree-typed": "^1.53.8",
|
|
76
|
-
"bst-typed": "^1.53.
|
|
77
|
-
"data-structure-typed": "^1.53.
|
|
76
|
+
"bst-typed": "^1.53.9",
|
|
77
|
+
"data-structure-typed": "^1.53.9",
|
|
78
78
|
"dependency-cruiser": "^16.5.0",
|
|
79
79
|
"doctoc": "^2.2.1",
|
|
80
80
|
"eslint": "^9.13.0",
|
|
@@ -83,7 +83,7 @@
|
|
|
83
83
|
"eslint-import-resolver-typescript": "^3.6.3",
|
|
84
84
|
"eslint-plugin-import": "^2.31.0",
|
|
85
85
|
"fast-glob": "^3.3.2",
|
|
86
|
-
"heap-typed": "^1.53.
|
|
86
|
+
"heap-typed": "^1.53.9",
|
|
87
87
|
"istanbul-badges-readme": "^1.9.0",
|
|
88
88
|
"jest": "^29.7.0",
|
|
89
89
|
"js-sdsl": "^4.4.2",
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type {
|
|
9
|
+
AVLTreeMultiMapNested,
|
|
9
10
|
AVLTreeMultiMapNodeNested,
|
|
10
11
|
AVLTreeMultiMapOptions,
|
|
11
12
|
BinaryTreeDeleteResult,
|
|
@@ -36,25 +37,6 @@ export class AVLTreeMultiMapNode<
|
|
|
36
37
|
super(key, value);
|
|
37
38
|
this.count = count;
|
|
38
39
|
}
|
|
39
|
-
|
|
40
|
-
protected _count: number = 1;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* The function returns the value of the protected variable _count.
|
|
44
|
-
* @returns The count property of the object, which is of type number.
|
|
45
|
-
*/
|
|
46
|
-
get count(): number {
|
|
47
|
-
return this._count;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* The above function sets the value of the count property.
|
|
52
|
-
* @param {number} value - The value parameter is of type number, which means it can accept any
|
|
53
|
-
* numeric value.
|
|
54
|
-
*/
|
|
55
|
-
set count(value: number) {
|
|
56
|
-
this._count = value;
|
|
57
|
-
}
|
|
58
40
|
}
|
|
59
41
|
|
|
60
42
|
/**
|
|
@@ -64,10 +46,23 @@ export class AVLTreeMultiMap<
|
|
|
64
46
|
K = any,
|
|
65
47
|
V = any,
|
|
66
48
|
R = object,
|
|
67
|
-
|
|
49
|
+
MK = any,
|
|
50
|
+
MV = any,
|
|
51
|
+
MR = object,
|
|
52
|
+
NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>,
|
|
53
|
+
TREE extends AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, TREE> = AVLTreeMultiMap<
|
|
54
|
+
K,
|
|
55
|
+
V,
|
|
56
|
+
R,
|
|
57
|
+
MK,
|
|
58
|
+
MV,
|
|
59
|
+
MR,
|
|
60
|
+
NODE,
|
|
61
|
+
AVLTreeMultiMapNested<K, V, R, MK, MV, MR, NODE>
|
|
62
|
+
>
|
|
68
63
|
>
|
|
69
|
-
extends AVLTree<K, V, R, NODE>
|
|
70
|
-
implements IBinaryTree<K, V, R, NODE>
|
|
64
|
+
extends AVLTree<K, V, R, MK, MV, MR, NODE, TREE>
|
|
65
|
+
implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE>
|
|
71
66
|
{
|
|
72
67
|
/**
|
|
73
68
|
* The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
|
|
@@ -132,16 +127,15 @@ export class AVLTreeMultiMap<
|
|
|
132
127
|
* @returns a new instance of the AVLTreeMultiMap class, with the specified options, as a TREE
|
|
133
128
|
* object.
|
|
134
129
|
*/
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
return new AVLTreeMultiMap<K, V, R, NODE>([], {
|
|
130
|
+
override createTree(options?: AVLTreeMultiMapOptions<K, V, R>): TREE {
|
|
131
|
+
return new AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, TREE>([], {
|
|
138
132
|
iterationType: this.iterationType,
|
|
139
133
|
isMapMode: this._isMapMode,
|
|
140
134
|
specifyComparable: this._specifyComparable,
|
|
141
135
|
toEntryFn: this._toEntryFn,
|
|
142
136
|
isReverse: this._isReverse,
|
|
143
137
|
...options
|
|
144
|
-
});
|
|
138
|
+
}) as TREE;
|
|
145
139
|
}
|
|
146
140
|
|
|
147
141
|
/**
|
|
@@ -220,7 +214,7 @@ export class AVLTreeMultiMap<
|
|
|
220
214
|
} else {
|
|
221
215
|
if (!curr.left) {
|
|
222
216
|
if (!parent) {
|
|
223
|
-
if (curr.right !== undefined) this._setRoot(curr.right);
|
|
217
|
+
if (curr.right !== undefined && curr.right !== null) this._setRoot(curr.right);
|
|
224
218
|
} else {
|
|
225
219
|
const { familyPosition: fp } = curr;
|
|
226
220
|
if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
|
|
@@ -330,8 +324,7 @@ export class AVLTreeMultiMap<
|
|
|
330
324
|
* The function overrides the clone method to create a deep copy of a tree object.
|
|
331
325
|
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
332
326
|
*/
|
|
333
|
-
|
|
334
|
-
override clone() {
|
|
327
|
+
override clone(): TREE {
|
|
335
328
|
const cloned = this.createTree();
|
|
336
329
|
if (this._isMapMode) this.bfs(node => cloned.add(node.key, undefined, node.count));
|
|
337
330
|
else this.bfs(node => cloned.add(node.key, node.value, node.count));
|
|
@@ -357,12 +350,11 @@ export class AVLTreeMultiMap<
|
|
|
357
350
|
* `callback` function along with the index and the original tree itself. The transformed entries are
|
|
358
351
|
* then added to the new `AVLTreeMultiMap` instance, which is returned at the end.
|
|
359
352
|
*/
|
|
360
|
-
// @ts-ignore
|
|
361
353
|
override map<MK, MV, MR>(
|
|
362
354
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
363
355
|
options?: AVLTreeMultiMapOptions<MK, MV, MR>,
|
|
364
356
|
thisArg?: any
|
|
365
|
-
) {
|
|
357
|
+
): AVLTreeMultiMap<MK, MV, MR> {
|
|
366
358
|
const newTree = new AVLTreeMultiMap<MK, MV, MR>([], options);
|
|
367
359
|
let index = 0;
|
|
368
360
|
for (const [key, value] of this) {
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
9
|
import type {
|
|
10
|
+
AVLTreeNested,
|
|
10
11
|
AVLTreeNodeNested,
|
|
11
12
|
AVLTreeOptions,
|
|
12
13
|
BinaryTreeDeleteResult,
|
|
@@ -31,26 +32,6 @@ export class AVLTreeNode<
|
|
|
31
32
|
*/
|
|
32
33
|
constructor(key: K, value?: V) {
|
|
33
34
|
super(key, value);
|
|
34
|
-
this._height = 0;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
protected _height: number;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* The function returns the value of the height property.
|
|
41
|
-
* @returns The height of the object.
|
|
42
|
-
*/
|
|
43
|
-
get height(): number {
|
|
44
|
-
return this._height;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* The above function sets the value of the height property.
|
|
49
|
-
* @param {number} value - The value parameter is a number that represents the new height value to be
|
|
50
|
-
* set.
|
|
51
|
-
*/
|
|
52
|
-
set height(value: number) {
|
|
53
|
-
this._height = value;
|
|
54
35
|
}
|
|
55
36
|
}
|
|
56
37
|
|
|
@@ -67,10 +48,23 @@ export class AVLTree<
|
|
|
67
48
|
K = any,
|
|
68
49
|
V = any,
|
|
69
50
|
R = object,
|
|
70
|
-
|
|
51
|
+
MK = any,
|
|
52
|
+
MV = any,
|
|
53
|
+
MR = object,
|
|
54
|
+
NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>,
|
|
55
|
+
TREE extends AVLTree<K, V, R, MK, MV, MR, NODE, TREE> = AVLTree<
|
|
56
|
+
K,
|
|
57
|
+
V,
|
|
58
|
+
R,
|
|
59
|
+
MK,
|
|
60
|
+
MV,
|
|
61
|
+
MR,
|
|
62
|
+
NODE,
|
|
63
|
+
AVLTreeNested<K, V, R, MK, MV, MR, NODE>
|
|
64
|
+
>
|
|
71
65
|
>
|
|
72
|
-
extends BST<K, V, R, NODE>
|
|
73
|
-
implements IBinaryTree<K, V, R, NODE>
|
|
66
|
+
extends BST<K, V, R, MK, MV, MR, NODE, TREE>
|
|
67
|
+
implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE>
|
|
74
68
|
{
|
|
75
69
|
/**
|
|
76
70
|
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
|
|
@@ -102,25 +96,21 @@ export class AVLTree<
|
|
|
102
96
|
}
|
|
103
97
|
|
|
104
98
|
/**
|
|
105
|
-
* The function
|
|
106
|
-
* options
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
* creates a
|
|
111
|
-
* @returns An AVLTree object is being returned with the specified options and properties inherited
|
|
112
|
-
* from the current object.
|
|
99
|
+
* The function creates a new AVL tree with the specified options and returns it.
|
|
100
|
+
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
101
|
+
* passed to the `createTree` function. It is used to customize the behavior of the AVL tree that is
|
|
102
|
+
* being created.
|
|
103
|
+
* @returns a new AVLTree object.
|
|
113
104
|
*/
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
return new AVLTree<K, V, R, NODE>([], {
|
|
105
|
+
override createTree(options?: AVLTreeOptions<K, V, R>): TREE {
|
|
106
|
+
return new AVLTree<K, V, R, MK, MV, MR, NODE, TREE>([], {
|
|
117
107
|
iterationType: this.iterationType,
|
|
118
108
|
isMapMode: this._isMapMode,
|
|
119
109
|
specifyComparable: this._specifyComparable,
|
|
120
110
|
toEntryFn: this._toEntryFn,
|
|
121
111
|
isReverse: this._isReverse,
|
|
122
112
|
...options
|
|
123
|
-
});
|
|
113
|
+
}) as TREE;
|
|
124
114
|
}
|
|
125
115
|
|
|
126
116
|
/**
|
|
@@ -177,12 +167,11 @@ export class AVLTree<
|
|
|
177
167
|
return deletedResults;
|
|
178
168
|
}
|
|
179
169
|
|
|
180
|
-
|
|
181
|
-
override map<MK, MV, MR>(
|
|
170
|
+
override map(
|
|
182
171
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
183
172
|
options?: AVLTreeOptions<MK, MV, MR>,
|
|
184
173
|
thisArg?: any
|
|
185
|
-
) {
|
|
174
|
+
): AVLTree<MK, MV, MR> {
|
|
186
175
|
const newTree = new AVLTree<MK, MV, MR>([], options);
|
|
187
176
|
let index = 0;
|
|
188
177
|
for (const [key, value] of this) {
|
|
@@ -279,7 +268,7 @@ export class AVLTree<
|
|
|
279
268
|
protected _balanceLL(A: NODE): void {
|
|
280
269
|
const parentOfA = A.parent;
|
|
281
270
|
const B = A.left;
|
|
282
|
-
A.parent = B;
|
|
271
|
+
if (B !== null) A.parent = B;
|
|
283
272
|
if (B && B.right) {
|
|
284
273
|
B.right.parent = A;
|
|
285
274
|
}
|
|
@@ -316,12 +305,12 @@ export class AVLTree<
|
|
|
316
305
|
if (B) {
|
|
317
306
|
C = B.right;
|
|
318
307
|
}
|
|
319
|
-
if (A) A.parent = C;
|
|
320
|
-
if (B) B.parent = C;
|
|
308
|
+
if (A && C !== null) A.parent = C;
|
|
309
|
+
if (B && C !== null) B.parent = C;
|
|
321
310
|
|
|
322
311
|
if (C) {
|
|
323
312
|
if (C.left) {
|
|
324
|
-
C.left.parent = B;
|
|
313
|
+
if (B !== null) C.left.parent = B;
|
|
325
314
|
}
|
|
326
315
|
if (C.right) {
|
|
327
316
|
C.right.parent = A;
|
|
@@ -363,7 +352,7 @@ export class AVLTree<
|
|
|
363
352
|
protected _balanceRR(A: NODE): void {
|
|
364
353
|
const parentOfA = A.parent;
|
|
365
354
|
const B = A.right;
|
|
366
|
-
A.parent = B;
|
|
355
|
+
if (B !== null) A.parent = B;
|
|
367
356
|
if (B) {
|
|
368
357
|
if (B.left) {
|
|
369
358
|
B.left.parent = A;
|
|
@@ -406,15 +395,15 @@ export class AVLTree<
|
|
|
406
395
|
C = B.left;
|
|
407
396
|
}
|
|
408
397
|
|
|
409
|
-
A.parent = C;
|
|
410
|
-
if (B) B.parent = C;
|
|
398
|
+
if (C !== null) A.parent = C;
|
|
399
|
+
if (B && C !== null) B.parent = C;
|
|
411
400
|
|
|
412
401
|
if (C) {
|
|
413
402
|
if (C.left) {
|
|
414
403
|
C.left.parent = A;
|
|
415
404
|
}
|
|
416
405
|
if (C.right) {
|
|
417
|
-
C.right.parent = B;
|
|
406
|
+
if (B !== null) C.right.parent = B;
|
|
418
407
|
}
|
|
419
408
|
C.parent = parentOfA;
|
|
420
409
|
}
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
import {
|
|
10
10
|
BinaryTreeDeleteResult,
|
|
11
|
+
BinaryTreeNested,
|
|
11
12
|
BinaryTreeNodeNested,
|
|
12
13
|
BinaryTreeOptions,
|
|
13
14
|
BinaryTreePrintOptions,
|
|
@@ -22,6 +23,7 @@ import {
|
|
|
22
23
|
NodeDisplayLayout,
|
|
23
24
|
NodePredicate,
|
|
24
25
|
OptNodeOrNull,
|
|
26
|
+
type RBTNColor,
|
|
25
27
|
ToEntryFn
|
|
26
28
|
} from '../../types';
|
|
27
29
|
import { IBinaryTree } from '../../interfaces';
|
|
@@ -51,7 +53,7 @@ export class BinaryTreeNode<
|
|
|
51
53
|
this.value = value;
|
|
52
54
|
}
|
|
53
55
|
|
|
54
|
-
|
|
56
|
+
_left?: OptNodeOrNull<NODE>;
|
|
55
57
|
|
|
56
58
|
get left(): OptNodeOrNull<NODE> {
|
|
57
59
|
return this._left;
|
|
@@ -64,7 +66,7 @@ export class BinaryTreeNode<
|
|
|
64
66
|
this._left = v;
|
|
65
67
|
}
|
|
66
68
|
|
|
67
|
-
|
|
69
|
+
_right?: OptNodeOrNull<NODE>;
|
|
68
70
|
|
|
69
71
|
get right(): OptNodeOrNull<NODE> {
|
|
70
72
|
return this._right;
|
|
@@ -77,6 +79,36 @@ export class BinaryTreeNode<
|
|
|
77
79
|
this._right = v;
|
|
78
80
|
}
|
|
79
81
|
|
|
82
|
+
_height: number = 0;
|
|
83
|
+
|
|
84
|
+
get height(): number {
|
|
85
|
+
return this._height;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
set height(value: number) {
|
|
89
|
+
this._height = value;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
_color: RBTNColor = 'BLACK';
|
|
93
|
+
|
|
94
|
+
get color(): RBTNColor {
|
|
95
|
+
return this._color;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
set color(value: RBTNColor) {
|
|
99
|
+
this._color = value;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
_count: number = 1;
|
|
103
|
+
|
|
104
|
+
get count(): number {
|
|
105
|
+
return this._count;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
set count(value: number) {
|
|
109
|
+
this._count = value;
|
|
110
|
+
}
|
|
111
|
+
|
|
80
112
|
get familyPosition(): FamilyPosition {
|
|
81
113
|
const that = this as unknown as NODE;
|
|
82
114
|
if (!this.parent) {
|
|
@@ -104,10 +136,23 @@ export class BinaryTree<
|
|
|
104
136
|
K = any,
|
|
105
137
|
V = any,
|
|
106
138
|
R = object,
|
|
107
|
-
|
|
139
|
+
MK = any,
|
|
140
|
+
MV = any,
|
|
141
|
+
MR = object,
|
|
142
|
+
NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>,
|
|
143
|
+
TREE extends BinaryTree<K, V, R, MK, MV, MR, NODE, TREE> = BinaryTree<
|
|
144
|
+
K,
|
|
145
|
+
V,
|
|
146
|
+
R,
|
|
147
|
+
MK,
|
|
148
|
+
MV,
|
|
149
|
+
MR,
|
|
150
|
+
NODE,
|
|
151
|
+
BinaryTreeNested<K, V, R, MK, MV, MR, NODE>
|
|
152
|
+
>
|
|
108
153
|
>
|
|
109
154
|
extends IterableEntryBase<K, V | undefined>
|
|
110
|
-
implements IBinaryTree<K, V, R, NODE>
|
|
155
|
+
implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE>
|
|
111
156
|
{
|
|
112
157
|
iterationType: IterationType = 'ITERATIVE';
|
|
113
158
|
|
|
@@ -186,26 +231,63 @@ export class BinaryTree<
|
|
|
186
231
|
}
|
|
187
232
|
|
|
188
233
|
/**
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
* binary tree, such as the iteration type, whether the tree is in map mode, and functions for
|
|
196
|
-
* converting entries.
|
|
197
|
-
* @returns The `createTree` method is returning an instance of the `BinaryTree` class with the
|
|
198
|
-
* provided options. The method is creating a new `BinaryTree` object with an empty array as the
|
|
199
|
-
* initial data, and then setting various options such as `iterationType`, `isMapMode`, and
|
|
200
|
-
* `toEntryFn` based on the current object's properties and the provided `options`. Finally, it
|
|
234
|
+
* The function creates a binary tree with the specified options.
|
|
235
|
+
* @param [options] - The `options` parameter in the `createTree` function is an optional parameter
|
|
236
|
+
* that allows you to provide partial configuration options for creating a binary tree. It is of type
|
|
237
|
+
* `Partial<BinaryTreeOptions<K, V, R>>`, which means you can pass in an object containing a subset
|
|
238
|
+
* of properties
|
|
239
|
+
* @returns A new instance of a binary tree with the specified options is being returned.
|
|
201
240
|
*/
|
|
202
|
-
createTree(options?: BinaryTreeOptions<K, V, R>):
|
|
203
|
-
return new BinaryTree<K, V, R>([], {
|
|
241
|
+
createTree(options?: BinaryTreeOptions<K, V, R>): TREE {
|
|
242
|
+
return new BinaryTree<K, V, R, MK, MV, MR, NODE, TREE>([], {
|
|
204
243
|
iterationType: this.iterationType,
|
|
205
244
|
isMapMode: this._isMapMode,
|
|
206
245
|
toEntryFn: this._toEntryFn,
|
|
207
246
|
...options
|
|
208
|
-
}) as
|
|
247
|
+
}) as TREE;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
|
|
252
|
+
* or returns null.
|
|
253
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
254
|
+
* `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeEntryOrRaw`, which
|
|
255
|
+
* can be of type `BTNRep<K, V, NODE>` or `R`. This parameter represents either a key, a
|
|
256
|
+
* node, an entry
|
|
257
|
+
* @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
|
|
258
|
+
* an optional parameter of type `V`. It represents the value associated with the key in the node
|
|
259
|
+
* being created. If a `value` is provided, it will be used when creating the node. If
|
|
260
|
+
* @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
|
|
261
|
+
* (`OptNodeOrNull<NODE>`) based on the input parameters provided. The function checks the type of the
|
|
262
|
+
* input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null
|
|
263
|
+
* value.
|
|
264
|
+
*/
|
|
265
|
+
protected _keyValueNodeEntryRawToNodeAndValue(
|
|
266
|
+
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
|
|
267
|
+
value?: V
|
|
268
|
+
): [OptNodeOrNull<NODE>, V | undefined] {
|
|
269
|
+
if (keyNodeEntryOrRaw === undefined) return [undefined, undefined];
|
|
270
|
+
if (keyNodeEntryOrRaw === null) return [null, undefined];
|
|
271
|
+
|
|
272
|
+
if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
|
|
273
|
+
|
|
274
|
+
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
275
|
+
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
276
|
+
if (key === undefined) return [undefined, undefined];
|
|
277
|
+
else if (key === null) return [null, undefined];
|
|
278
|
+
const finalValue = value ?? entryValue;
|
|
279
|
+
return [this.createNode(key, finalValue), finalValue];
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
283
|
+
const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw);
|
|
284
|
+
const finalValue = value ?? entryValue;
|
|
285
|
+
if (this.isKey(key)) return [this.createNode(key, finalValue), finalValue];
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value), value];
|
|
289
|
+
|
|
290
|
+
return [undefined, undefined];
|
|
209
291
|
}
|
|
210
292
|
|
|
211
293
|
/**
|
|
@@ -490,7 +572,7 @@ export class BinaryTree<
|
|
|
490
572
|
* elements from the other tree.
|
|
491
573
|
* @param anotherTree - `BinaryTree<K, V, R, NODE, TREE>`
|
|
492
574
|
*/
|
|
493
|
-
merge(anotherTree:
|
|
575
|
+
merge(anotherTree: BinaryTree<K, V, R, NODE, TREE>) {
|
|
494
576
|
this.addMany(anotherTree, []);
|
|
495
577
|
}
|
|
496
578
|
|
|
@@ -1599,7 +1681,7 @@ export class BinaryTree<
|
|
|
1599
1681
|
* original tree using breadth-first search (bfs), and adds the nodes to the new tree. If a node in
|
|
1600
1682
|
* the original tree is null, a null node is added to the cloned tree. If a node
|
|
1601
1683
|
*/
|
|
1602
|
-
clone() {
|
|
1684
|
+
clone(): TREE {
|
|
1603
1685
|
const cloned = this.createTree();
|
|
1604
1686
|
this.bfs(
|
|
1605
1687
|
node => {
|
|
@@ -1663,11 +1745,11 @@ export class BinaryTree<
|
|
|
1663
1745
|
* @returns The `map` function is returning a new `BinaryTree` instance filled with entries that are
|
|
1664
1746
|
* the result of applying the provided `callback` function to each entry in the original tree.
|
|
1665
1747
|
*/
|
|
1666
|
-
map
|
|
1748
|
+
map(
|
|
1667
1749
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
1668
1750
|
options?: BinaryTreeOptions<MK, MV, MR>,
|
|
1669
1751
|
thisArg?: any
|
|
1670
|
-
) {
|
|
1752
|
+
): BinaryTree<MK, MV, MR> {
|
|
1671
1753
|
const newTree = new BinaryTree<MK, MV, MR>([], options);
|
|
1672
1754
|
let index = 0;
|
|
1673
1755
|
for (const [key, value] of this) {
|
|
@@ -1736,49 +1818,6 @@ export class BinaryTree<
|
|
|
1736
1818
|
console.log(this.toVisual(startNode, options));
|
|
1737
1819
|
}
|
|
1738
1820
|
|
|
1739
|
-
/**
|
|
1740
|
-
* The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
|
|
1741
|
-
* or returns null.
|
|
1742
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
1743
|
-
* `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeEntryOrRaw`, which
|
|
1744
|
-
* can be of type `BTNRep<K, V, NODE>` or `R`. This parameter represents either a key, a
|
|
1745
|
-
* node, an entry
|
|
1746
|
-
* @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
|
|
1747
|
-
* an optional parameter of type `V`. It represents the value associated with the key in the node
|
|
1748
|
-
* being created. If a `value` is provided, it will be used when creating the node. If
|
|
1749
|
-
* @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
|
|
1750
|
-
* (`OptNodeOrNull<NODE>`) based on the input parameters provided. The function checks the type of the
|
|
1751
|
-
* input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null
|
|
1752
|
-
* value.
|
|
1753
|
-
*/
|
|
1754
|
-
protected _keyValueNodeEntryRawToNodeAndValue(
|
|
1755
|
-
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
|
|
1756
|
-
value?: V
|
|
1757
|
-
): [OptNodeOrNull<NODE>, V | undefined] {
|
|
1758
|
-
if (keyNodeEntryOrRaw === undefined) return [undefined, undefined];
|
|
1759
|
-
if (keyNodeEntryOrRaw === null) return [null, undefined];
|
|
1760
|
-
|
|
1761
|
-
if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
|
|
1762
|
-
|
|
1763
|
-
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
1764
|
-
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
1765
|
-
if (key === undefined) return [undefined, undefined];
|
|
1766
|
-
else if (key === null) return [null, undefined];
|
|
1767
|
-
const finalValue = value ?? entryValue;
|
|
1768
|
-
return [this.createNode(key, finalValue), finalValue];
|
|
1769
|
-
}
|
|
1770
|
-
|
|
1771
|
-
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
1772
|
-
const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw);
|
|
1773
|
-
const finalValue = value ?? entryValue;
|
|
1774
|
-
if (this.isKey(key)) return [this.createNode(key, finalValue), finalValue];
|
|
1775
|
-
}
|
|
1776
|
-
|
|
1777
|
-
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value), value];
|
|
1778
|
-
|
|
1779
|
-
return [undefined, undefined];
|
|
1780
|
-
}
|
|
1781
|
-
|
|
1782
1821
|
/**
|
|
1783
1822
|
* Time complexity: O(n)
|
|
1784
1823
|
* Space complexity: O(n)
|